From 6cf9c4e49729ffa739532b79a4446bc49b9e9b6a Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Tue, 17 Mar 2009 14:44:50 -0700 Subject: [PATCH 1/4] replacing litorgy-make-region-example with the standard org-toggle-fixed-width-section. --- litorgy/litorgy.el | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/litorgy/litorgy.el b/litorgy/litorgy.el index baa9658b5..3ae1cb029 100644 --- a/litorgy/litorgy.el +++ b/litorgy/litorgy.el @@ -93,8 +93,8 @@ lisp code use the `litorgy-add-interpreter' function." (defun litorgy-execute-src-block (&optional arg) "Execute the current source code block, and dump the results into the buffer immediately following the block. Results are -commented by `litorgy-make-region-example'. With optional prefix -don't dump results into buffer." +commented by `org-toggle-fixed-width-section'. With optional +prefix don't dump results into buffer." (interactive "P") (let* ((info (litorgy-get-src-block-info)) (lang (first info)) @@ -159,7 +159,7 @@ existing results currently located after the source block." (let ((beg (point)) (end (progn (insert result) (point)))) - (litorgy-make-region-example beg end)))) + (org-toggle-fixed-width-section beg end)))) (defun litorgy-remove-result () "Remove the result following the current source block" @@ -175,20 +175,6 @@ existing results currently located after the source block." (forward-line -1) (point))))) -(defun litorgy-make-region-example (beg end) - "Comment out region using the ': ' org example quote." - (interactive "*r") - (let ((size (abs (- (line-number-at-pos end) - (line-number-at-pos beg))))) - (if (= size 0) - (let ((result (buffer-substring beg end))) - (delete-region beg end) - (insert (concat ": " result))) - (save-excursion - (goto-char beg) - (dotimes (n size) - (move-beginning-of-line 1) (insert ": ") (forward-line 1)))))) - (defun litorgy-clean-text-properties (text) "Strip all properties from text return." (set-text-properties 0 (length text) nil text) text) From 63839f7c50cf11e205dc1d998497712a8b10aaf3 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 23 Mar 2009 16:07:02 -0700 Subject: [PATCH 2/4] adding support for emacs-lisp blocks --- litorgy/litorgy-R.el | 105 ---------------------------------------- litorgy/litorgy-lisp.el | 45 +++++++++++++++++ litorgy/litorgy.el | 5 +- 3 files changed, 49 insertions(+), 106 deletions(-) delete mode 100644 litorgy/litorgy-R.el create mode 100644 litorgy/litorgy-lisp.el diff --git a/litorgy/litorgy-R.el b/litorgy/litorgy-R.el deleted file mode 100644 index 5cfe26d4d..000000000 --- a/litorgy/litorgy-R.el +++ /dev/null @@ -1,105 +0,0 @@ -;;; litorgy-R.el --- litorgy functions for R code evaluation - -;; Copyright (C) 2009 Eric Schulte, Dan Davison, Austin F. Frank - -;; Author: Eric Schulte, Dan Davison, Austin F. Frank -;; Keywords: literate programming, reproducible research -;; Homepage: http://orgmode.org -;; Version: 0.01 - -;;; License: - -;; This program is free software; you can redistribute it and/or modify -;; it under the terms of the GNU General Public License as published by -;; the Free Software Foundation; either version 3, or (at your option) -;; any later version. -;; -;; This program is distributed in the hope that it will be useful, -;; but WITHOUT ANY WARRANTY; without even the implied warranty of -;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -;; GNU General Public License for more details. -;; -;; You should have received a copy of the GNU General Public License -;; along with GNU Emacs; see the file COPYING. If not, write to the -;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, -;; Boston, MA 02110-1301, USA. - -;;; Commentary: - -;; Litorgy support for evaluating R code - -;;; Code: -(require 'litorgy) - -(litorgy-add-interpreter "R") - -(defun litorgy-execute:R (body params) - "Execute a block of R code with litorgy. This function is -called by `litorgy-execute-src-block'." - (save-window-excursion - (let (results) - (message "executing R code block...") - (litorgy-initiate-R-buffer) - (mapc (lambda (line) (litorgy-R-input-command line)) (butlast (split-string body "[\r\n]"))) - (litorgy-R-last-output)))) - -;; Maybe the following be replaced with a method using `ess-execute', -;; I went with the following functions because I wrote them and they -;; are what I know -;; -;; (not the best reasons for making design decisions) - -(defvar litorgy-R-buffer nil - "Holds the buffer for the current R process") - -(defun litorgy-initiate-R-buffer () - "If there is not a current R process then create one." - (unless (and (buffer-live-p litorgy-R-buffer) (get-buffer litorgy-R-buffer)) - (save-excursion - (R) - (setf litorgy-R-buffer (current-buffer)) - (litorgy-R-wait-for-output) - (litorgy-R-input-command "")))) - -(defun litorgy-R-command-to-string (command) - "Send a command to R, and return the results as a string." - (litorgy-R-input-command command) - (litorgy-R-last-output)) - -(defun litorgy-R-input-command (command) - "Pass COMMAND to the R process running in `litorgy-R-buffer'." - (save-excursion - (save-match-data - (set-buffer litorgy-R-buffer) - (goto-char (process-mark (get-buffer-process (current-buffer)))) - (insert command) - (comint-send-input) - (litorgy-R-wait-for-output)))) - -(defun litorgy-R-wait-for-output () - "Wait until output arrives" - (save-excursion - (save-match-data - (set-buffer litorgy-R-buffer) - (while (progn - (goto-char comint-last-input-end) - (not (re-search-forward comint-prompt-regexp nil t))) - (accept-process-output (get-buffer-process (current-buffer))))))) - -(defun litorgy-R-last-output () - "Return the last R output as a string" - (save-excursion - (save-match-data - (set-buffer litorgy-R-buffer) - (goto-char (process-mark (get-buffer-process (current-buffer)))) - (forward-line 0) - (let ((raw (buffer-substring comint-last-input-end (- (point) 1)))) - (if (string-match "\n" raw) - raw - (and (string-match "\\[[[:digit:]+]\\] *\\(.*\\)$" raw) - (message raw) - (message (match-string 1 raw)) - (match-string 1 raw))))))) - -(provide 'litorgy-R) -;;; litorgy-R.el ends here diff --git a/litorgy/litorgy-lisp.el b/litorgy/litorgy-lisp.el new file mode 100644 index 000000000..c06b0811d --- /dev/null +++ b/litorgy/litorgy-lisp.el @@ -0,0 +1,45 @@ +;;; litorgy-lisp.el --- litorgy functions for lisp code evaluation + +;; Copyright (C) 2009 Eric Schulte, Dan Davison, Austin F. Frank + +;; Author: Eric Schulte, Dan Davison, Austin F. Frank +;; Keywords: literate programming, reproducible research +;; Homepage: http://orgmode.org +;; Version: 0.01 + +;;; License: + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 3, or (at your option) +;; any later version. +;; +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. +;; +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to the +;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +;; Boston, MA 02110-1301, USA. + +;;; Commentary: + +;; Litorgy support for evaluating lisp code + +;;; Code: +(require 'litorgy) + +(litorgy-add-interpreter "emacs-lisp") + +(defun litorgy-execute:emacs-lisp (body params) + "Execute a block of emacs-lisp code with litorgy. This +function is called by `litorgy-execute-src-block'." + (save-window-excursion + (let ((print-level nil) (print-length nil) results) + (message "executing emacs-lisp code block...") + (format "%S" (eval (read body)))))) + +(provide 'litorgy-lisp) +;;; litorgy-lisp.el ends here diff --git a/litorgy/litorgy.el b/litorgy/litorgy.el index 3ae1cb029..937b11dba 100644 --- a/litorgy/litorgy.el +++ b/litorgy/litorgy.el @@ -159,7 +159,10 @@ existing results currently located after the source block." (let ((beg (point)) (end (progn (insert result) (point)))) - (org-toggle-fixed-width-section beg end)))) + (save-excursion + (set-mark beg) + (goto-char end) + (org-toggle-fixed-width-section nil))))) (defun litorgy-remove-result () "Remove the result following the current source block" From a95cf1c4425399067cefc2f81ccd9463b9aa60df Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 23 Mar 2009 16:29:31 -0700 Subject: [PATCH 3/4] stubbing out a file to hold functions for referencing data from header arguments of source blocks --- litorgy/litorgy-reference.el | 57 ++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 litorgy/litorgy-reference.el diff --git a/litorgy/litorgy-reference.el b/litorgy/litorgy-reference.el new file mode 100644 index 000000000..c9efc4e0a --- /dev/null +++ b/litorgy/litorgy-reference.el @@ -0,0 +1,57 @@ +;;; litorgy-reference.el --- litorgical functions for referencing external data + +;; Copyright (C) 2009 Eric Schulte, Dan Davison, Austin F. Frank + +;; Author: Eric Schulte, Dan Davison, Austin F. Frank +;; Keywords: literate programming, reproducible research +;; Homepage: http://orgmode.org +;; Version: 0.01 + +;;; License: + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 3, or (at your option) +;; any later version. +;; +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. +;; +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to the +;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +;; Boston, MA 02110-1301, USA. + +;;; Commentary: + +;; Functions for referencing data from the header arguments of a +;; litorgical block. The syntax of such a reference should be +;; +;; #+VAR: variable-name file resource-id:name +;; +;; - variable-name :: the name of the variable to which the value +;; will be assigned +;; +;; - file :: path to the file containing the resource, or omitted if +;; resource is in the current file +;; +;; - resource-id :: the id or name of the resource, or 'previous' to +;; grab the previous table, or 'next' to grab the +;; next table +;; +;; So an example of a simple src block referencing table data in the +;; same file would be +;; +;; #+var: table previous +;; #+begin_src emacs-lisp +;; (message table) +;; #+end_src +;; + +;;; Code: +(require 'litorgy) + +(provide 'litorgy-reference) +;;; litorgy-reference.el ends here From 74498d60559951865ff82e0bf6bc4f83feb132d4 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 23 Mar 2009 17:01:11 -0700 Subject: [PATCH 4/4] thinking about and stubbing out functions for referencing external resources --- litorgy/litorgy-lisp.el | 3 ++- litorgy/litorgy-reference.el | 28 +++++++++++++++++++++++++++- litorgy/litorgy.el | 4 ++-- rorg.org | 5 +++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/litorgy/litorgy-lisp.el b/litorgy/litorgy-lisp.el index c06b0811d..d3418f5a8 100644 --- a/litorgy/litorgy-lisp.el +++ b/litorgy/litorgy-lisp.el @@ -37,7 +37,8 @@ "Execute a block of emacs-lisp code with litorgy. This function is called by `litorgy-execute-src-block'." (save-window-excursion - (let ((print-level nil) (print-length nil) results) + (let ((vars (litorgy-reference-variables params)) + (print-level nil) (print-length nil) results) (message "executing emacs-lisp code block...") (format "%S" (eval (read body)))))) diff --git a/litorgy/litorgy-reference.el b/litorgy/litorgy-reference.el index c9efc4e0a..bc0504bc0 100644 --- a/litorgy/litorgy-reference.el +++ b/litorgy/litorgy-reference.el @@ -29,7 +29,7 @@ ;; Functions for referencing data from the header arguments of a ;; litorgical block. The syntax of such a reference should be ;; -;; #+VAR: variable-name file resource-id:name +;; #+VAR: variable-name=file:resource-id ;; ;; - variable-name :: the name of the variable to which the value ;; will be assigned @@ -53,5 +53,31 @@ ;;; Code: (require 'litorgy) +(defun litorgy-reference-variables (params) + "Takes a parameter alist, and return an alist of variable +names, and the string representation of the related value." + (mapcar #'litorgy-reference-parse + (delq nil (mapcar (lambda (pair) (if (= (car pair) :var) (cdr pair))) params)))) + +(defun litorgy-reference-parse (reference) + "Parse a reference to an external resource returning a list +with two elements. The first element of the list will be the +name of the variable, and the second will be an emacs-lisp +representation of the value of the variable." + (save-excursion + (if (string-match "(.+)=(.+)" reference) + (let ((var (match-string 1 reference)) + (ref (match-string 2 reference))) + (when (string-match "(.+):(.+)" reference) + (find-file (match-string 1 reference)) + (setf ref (match-string 2 reference))) + ;; follow the reference in the current file + (case ref + ("previous" + ) + ("next") + (t )) + )))) + (provide 'litorgy-reference) ;;; litorgy-reference.el ends here diff --git a/litorgy/litorgy.el b/litorgy/litorgy.el index 937b11dba..61a1939de 100644 --- a/litorgy/litorgy.el +++ b/litorgy/litorgy.el @@ -32,8 +32,8 @@ (require 'org) (defun litorgy-execute-src-block-maybe () - "Detect if this is context for a litorgical src-block and run if -so then run `litorgy-execute-src-block'." + "Detect if this is context for a litorgical src-block and if so +then run `litorgy-execute-src-block'." (let ((case-fold-search t)) (if (save-excursion (beginning-of-line 1) diff --git a/rorg.org b/rorg.org index 48e5772ea..57d0e66a5 100644 --- a/rorg.org +++ b/rorg.org @@ -238,6 +238,11 @@ internal to the source-code evaluation process? need to look more closely at that and we should try to come up with a formats for referencing data from source-code in such a way that it will be as source-code-language independent as possible. + + Org tables already have a sophisticated reference system in place + that allows referencing table ranges in other files, as well as + specifying constants in the header arguments of a table. This is + described in [[info:org:References]]. **** Dan: thinking aloud re: referencing data from R Suppose in some R code, we want to reference data in an org