Просмотр исходного кода

now able to assign variables from emacs-lisp source blocks

Eric Schulte 17 лет назад
Родитель
Сommit
77c33de593
8 измененных файлов с 96 добавлено и 24 удалено
  1. 1 1
      litorgy/init.el
  2. 1 1
      litorgy/litorgy-R.el
  3. 4 2
      litorgy/litorgy-lisp.el
  4. 27 15
      litorgy/litorgy-ref.el
  5. 1 1
      litorgy/litorgy-script.el
  6. 1 1
      litorgy/litorgy-shell.el
  7. 7 3
      litorgy/litorgy.el
  8. 54 0
      rorg.org

+ 1 - 1
litorgy/init.el

@@ -31,7 +31,7 @@
 ;;; Code:
 ;;; Code:
 (require 'org)
 (require 'org)
 (require 'litorgy)
 (require 'litorgy)
-(require 'litorgy-reference)
+(require 'litorgy-ref)
 
 
 ;; language specific files
 ;; language specific files
 (require 'litorgy-script)
 (require 'litorgy-script)

+ 1 - 1
litorgy/litorgy-R.el

@@ -38,7 +38,7 @@
 called by `litorgy-execute-src-block'."
 called by `litorgy-execute-src-block'."
   (message "executing R source code block...")
   (message "executing R source code block...")
   (save-window-excursion
   (save-window-excursion
-    (let ((vars (litorgy-reference-variables params))
+    (let ((vars (litorgy-ref-variables params))
           results)
           results)
       (litorgy-R-initiate-R-buffer)
       (litorgy-R-initiate-R-buffer)
       (litorgy-R-command-to-string body))))
       (litorgy-R-command-to-string body))))

+ 4 - 2
litorgy/litorgy-lisp.el

@@ -38,12 +38,14 @@
 function is called by `litorgy-execute-src-block'."
 function is called by `litorgy-execute-src-block'."
   (message "executing emacs-lisp code block...")
   (message "executing emacs-lisp code block...")
   (save-window-excursion
   (save-window-excursion
-    (let ((vars (litorgy-reference-variables params))
+    (let ((vars (litorgy-ref-variables params))
           (print-level nil) (print-length nil) results)
           (print-level nil) (print-length nil) results)
       (setq results
       (setq results
             (eval `(let ,(mapcar (lambda (var) `(,(car var) ',(cdr var))) vars)
             (eval `(let ,(mapcar (lambda (var) `(,(car var) ',(cdr var))) vars)
                      ,(read body))))
                      ,(read body))))
-      (if (listp results) results (format "%S" results)))))
+      (if (assoc :raw params)
+          results
+        (if (listp results) results (format "%S" results))))))
 
 
 (provide 'litorgy-lisp)
 (provide 'litorgy-lisp)
 ;;; litorgy-lisp.el ends here
 ;;; litorgy-lisp.el ends here

+ 27 - 15
litorgy/litorgy-reference.el → litorgy/litorgy-ref.el

@@ -1,4 +1,4 @@
-;;; litorgy-reference.el --- litorgical functions for referencing external data
+;;; litorgy-ref.el --- litorgical functions for referencing external data
 
 
 ;; Copyright (C) 2009 Eric Schulte, Dan Davison, Austin F. Frank
 ;; Copyright (C) 2009 Eric Schulte, Dan Davison, Austin F. Frank
 
 
@@ -73,13 +73,13 @@ This is taken almost directly from `org-read-prop'."
 	  out))
 	  out))
     cell))
     cell))
 
 
-(defun litorgy-reference-variables (params)
+(defun litorgy-ref-variables (params)
   "Takes a parameter alist, and return an alist of variable
   "Takes a parameter alist, and return an alist of variable
 names, and the string representation of the related value."
 names, and the string representation of the related value."
-  (mapcar #'litorgy-reference-parse
+  (mapcar #'litorgy-ref-parse
    (delq nil (mapcar (lambda (pair) (if (eq (car pair) :var) (cdr pair))) params))))
    (delq nil (mapcar (lambda (pair) (if (eq (car pair) :var) (cdr pair))) params))))
 
 
-(defun litorgy-reference-parse (reference)
+(defun litorgy-ref-parse (reference)
   "Parse a reference to an external resource returning a list
   "Parse a reference to an external resource returning a list
 with two elements.  The first element of the list will be the
 with two elements.  The first element of the list will be the
 name of the variable, and the second will be an emacs-lisp
 name of the variable, and the second will be an emacs-lisp
@@ -88,7 +88,7 @@ representation of the value of the variable."
     (if (string-match "\\(.+\\)=\\(.+\\)" reference)
     (if (string-match "\\(.+\\)=\\(.+\\)" reference)
         (let ((var (match-string 1 reference))
         (let ((var (match-string 1 reference))
               (ref (match-string 2 reference))
               (ref (match-string 2 reference))
-              direction)
+              direction type)
           (when (string-match "\\(.+\\):\\(.+\\)" reference)
           (when (string-match "\\(.+\\):\\(.+\\)" reference)
             (find-file (match-string 1 reference))
             (find-file (match-string 1 reference))
             (setf ref (match-string 2 reference)))
             (setf ref (match-string 2 reference)))
@@ -100,23 +100,35 @@ representation of the value of the variable."
                    (t
                    (t
                     (goto-char (point-min))
                     (goto-char (point-min))
                     (setq direction 1)
                     (setq direction 1)
-                    (unless (let ((regexp (concat "^#\\+TBLNAME:[ \t]*"
+                    (unless (let ((regexp (concat "^#\\+\\(TBL\\|SRC\\)NAME:[ \t]*"
                                                   (regexp-quote ref) "[ \t]*$")))
                                                   (regexp-quote ref) "[ \t]*$")))
                               (or (re-search-forward regexp nil t)
                               (or (re-search-forward regexp nil t)
                                   (re-search-backward regexp nil t)))
                                   (re-search-backward regexp nil t)))
-                      ;; ;; TODO: allow searching for table in other buffers
+                      ;; ;; TODO: allow searching for names in other buffers
                       ;; (setq id-loc (org-id-find ref 'marker)
                       ;; (setq id-loc (org-id-find ref 'marker)
                       ;;       buffer (marker-buffer id-loc)
                       ;;       buffer (marker-buffer id-loc)
                       ;;       loc (marker-position id-loc))
                       ;;       loc (marker-position id-loc))
                       ;; (move-marker id-loc nil)
                       ;; (move-marker id-loc nil)
-                      (error (format "table '%s' not found in this buffer" ref)))))
-                  (while (not (org-at-table-p))
+                      (error (format "reference '%s' not found in this buffer" ref)))))
+                  (while (not (setq type (litorgy-ref-at-ref-p)))
                     (forward-line direction)
                     (forward-line direction)
+                    (beginning-of-line)
                     (if (or (= (point) (point-min)) (= (point) (point-max)))
                     (if (or (= (point) (point-min)) (= (point) (point-max)))
-                        (error "no table found")))
-                  (mapcar (lambda (row)
-                            (mapcar #'litorgy-read-cell row))
-                          (org-table-to-lisp))))))))
+                        (error "reference not found")))
+                  (case type
+                    ('table
+                     (mapcar (lambda (row)
+                                      (mapcar #'litorgy-read-cell row))
+                                    (org-table-to-lisp)))
+                    ('source-block
+                     (litorgy-execute-src-block t)))))))))
 
 
-(provide 'litorgy-reference)
-;;; litorgy-reference.el ends here
+(defun litorgy-ref-at-ref-p ()
+  "Return the type of reference located at point or nil of none
+of the supported reference types are found.  Supported reference
+types are tables and source blocks."
+  (cond ((org-at-table-p) 'table)
+        ((looking-at "^#\\+BEGIN_SRC") 'source-block)))
+
+(provide 'litorgy-ref)
+;;; litorgy-ref.el ends here

+ 1 - 1
litorgy/litorgy-script.el

@@ -52,7 +52,7 @@ executed through litorgy."
 (defun litorgy-script-execute (cmd body params)
 (defun litorgy-script-execute (cmd body params)
   "Run CMD on BODY obeying any options set with PARAMS."
   "Run CMD on BODY obeying any options set with PARAMS."
   (message (format "executing %s code block..." cmd))
   (message (format "executing %s code block..." cmd))
-  (let ((vars (litorgy-reference-variables params)))
+  (let ((vars (litorgy-ref-variables params)))
     (save-window-excursion
     (save-window-excursion
       (with-temp-buffer
       (with-temp-buffer
         (when (string= "ruby" cmd) (insert "def main\n"))
         (when (string= "ruby" cmd) (insert "def main\n"))

+ 1 - 1
litorgy/litorgy-shell.el

@@ -52,7 +52,7 @@ executed through litorgy."
 (defun litorgy-shell-execute (cmd body params)
 (defun litorgy-shell-execute (cmd body params)
   "Run CMD on BODY obeying any options set with PARAMS."
   "Run CMD on BODY obeying any options set with PARAMS."
   (message (format "executing %s code block..." cmd))
   (message (format "executing %s code block..." cmd))
-  (let ((vars (litorgy-reference-variables params)))
+  (let ((vars (litorgy-ref-variables params)))
     (save-window-excursion
     (save-window-excursion
       (with-temp-buffer
       (with-temp-buffer
         (if (> (length vars) 0)
         (if (> (length vars) 0)

+ 7 - 3
litorgy/litorgy.el

@@ -94,7 +94,9 @@ lisp code use the `litorgy-add-interpreter' function."
   "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 `org-toggle-fixed-width-section'.  With optional
 commented by `org-toggle-fixed-width-section'.  With optional
-prefix don't dump results into buffer."
+prefix don't dump results into buffer but rather return the
+results in raw elisp (this is useful for automated execution of a
+source block)."
   (interactive "P")
   (interactive "P")
   (let* ((info (litorgy-get-src-block-info))
   (let* ((info (litorgy-get-src-block-info))
          (lang (first info))
          (lang (first info))
@@ -104,9 +106,11 @@ prefix don't dump results into buffer."
          result)
          result)
     (unless (member lang litorgy-interpreters)
     (unless (member lang litorgy-interpreters)
       (error "Language is not in `litorgy-interpreters': %s" lang))
       (error "Language is not in `litorgy-interpreters': %s" lang))
+    (when arg
+      (setq params (cons '(:raw . t) params)))
     (setq result (funcall cmd body params))
     (setq result (funcall cmd body params))
     (if arg
     (if arg
-        (message (format "%S" result))
+        result
       (litorgy-insert-result result (cdr (assoc :results params))))))
       (litorgy-insert-result result (cdr (assoc :results params))))))
 
 
 (defun litorgy-eval-buffer (&optional arg)
 (defun litorgy-eval-buffer (&optional arg)
@@ -132,7 +136,7 @@ form.  (language body header-arguments-alist)"
   (unless (save-excursion
   (unless (save-excursion
             (beginning-of-line 1)
             (beginning-of-line 1)
             (looking-at litorgy-src-block-regexp))
             (looking-at litorgy-src-block-regexp))
-    (error "not looking at src-block"))
+    (error (format "not looking at src-block (%s)" (point))))
   (let ((lang (litorgy-clean-text-properties (match-string 1)))
   (let ((lang (litorgy-clean-text-properties (match-string 1)))
         (args (litorgy-clean-text-properties (or (match-string 3) "")))
         (args (litorgy-clean-text-properties (or (match-string 3) "")))
         (body (litorgy-clean-text-properties (match-string 4))))
         (body (litorgy-clean-text-properties (match-string 4))))

+ 54 - 0
rorg.org

@@ -54,6 +54,13 @@ to specify whether the target is a source code block or a table
 (alternately we could just match the first one with the given name
 (alternately we could just match the first one with the given name
 whether it's a table or a source code block).
 whether it's a table or a source code block).
 
 
+At least initially I'll try to implement this so that there is no need
+to specify whether the reference is to a table or a source-code block.
+That seems to be simpler both in terms of use and implementation.
+
+This is currently working only with emacs lisp as in the following
+example in the [[* emacs lisp source reference][emacs lisp source reference]].
+
 
 
 * Bugs
 * Bugs
 ** Args out of range error
 ** Args out of range error
@@ -258,6 +265,53 @@ x
 #+end_src
 #+end_src
 
 
 
 
+** referencing other source blocks
+Doing this in emacs-lisp first because it's trivial to convert
+emacs-lisp results to and from emacs-lisp.
+
+*** emacs lisp source reference
+This first example performs a calculation in the first source block
+named =top=, the results of this calculation are then saved into the
+variable =first= by the header argument =:var first=top=, and it is
+used in the calculations of the second source block.
+
+#+SRCNAME: top
+#+begin_src emacs-lisp
+(+ 4 2)
+#+end_src
+
+#+begin_src emacs-lisp :var first=top :results replace
+(* first 3)
+#+end_src
+
+: 18
+
+This example is the same as the previous only the variable being
+passed through is a table rather than a number.
+
+#+begin_src emacs-lisp :results silent
+(defun transpose (table)
+  (apply #'mapcar* #'list table))
+#+end_src
+
+#+TBLNAME: top_table
+| 1 |       2 | 3 |
+| 4 | schulte | 6 |
+
+#+SRCNAME: second_src_example
+#+begin_src emacs-lisp :var table=top_table
+(transpose table)
+#+end_src
+
+#+begin_src emacs-lisp :var table=second_src_example
+(transpose table)
+#+end_src
+
+| 1 |         2 | 3 |
+| 4 | "schulte" | 6 |
+
+
+
 * COMMENT Commentary
 * COMMENT Commentary
 I'm seeing this as like commit notes, and a place for less formal
 I'm seeing this as like commit notes, and a place for less formal
 communication of the goals of our changes.
 communication of the goals of our changes.