Merge branch 'master' of git@github.com:eschulte/rorg

This commit is contained in:
Dan Davison 2009-03-23 23:42:17 -04:00
commit 080a1634d7
5 changed files with 140 additions and 122 deletions

View File

@ -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

46
litorgy/litorgy-lisp.el Normal file
View File

@ -0,0 +1,46 @@
;;; 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 ((vars (litorgy-reference-variables params))
(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

View File

@ -0,0 +1,83 @@
;;; 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
;;
;; - 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)
(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

View File

@ -93,8 +93,8 @@ lisp code use the `litorgy-add-interpreter' function."
(defun litorgy-execute-src-block (&optional arg) (defun litorgy-execute-src-block (&optional arg)
"Execute the current source code block, and dump the results "Execute the current source code block, and dump the results
into the buffer immediately following the block. Results are into the buffer immediately following the block. Results are
commented by `litorgy-make-region-example'. With optional prefix commented by `org-toggle-fixed-width-section'. With optional
don't dump results into buffer." prefix don't dump results into buffer."
(interactive "P") (interactive "P")
(let* ((info (litorgy-get-src-block-info)) (let* ((info (litorgy-get-src-block-info))
(lang (first info)) (lang (first info))
@ -159,7 +159,10 @@ existing results currently located after the source block."
(let ((beg (point)) (let ((beg (point))
(end (progn (insert result) (end (progn (insert result)
(point)))) (point))))
(litorgy-make-region-example beg end)))) (save-excursion
(set-mark beg)
(goto-char end)
(org-toggle-fixed-width-section nil)))))
(defun litorgy-remove-result () (defun litorgy-remove-result ()
"Remove the result following the current source block" "Remove the result following the current source block"
@ -175,20 +178,6 @@ existing results currently located after the source block."
(forward-line -1) (forward-line -1)
(point))))) (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) (defun litorgy-clean-text-properties (text)
"Strip all properties from text return." "Strip all properties from text return."
(set-text-properties 0 (length text) nil text) text) (set-text-properties 0 (length text) nil text) text)

View File

@ -239,6 +239,11 @@ internal to the source-code evaluation process?
formats for referencing data from source-code in such a way that it formats for referencing data from source-code in such a way that it
will be as source-code-language independent as possible. 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 **** Dan: thinking aloud re: referencing data from R
Suppose in some R code, we want to reference data in an org Suppose in some R code, we want to reference data in an org
table. I think that requires the use of 'header arguments', since table. I think that requires the use of 'header arguments', since