mirror of
https://git.savannah.gnu.org/git/emacs/org-mode.git
synced 2024-12-26 10:49:38 +00:00
implemented infrastructure for loading source-blocks from files
This commit is contained in:
parent
82e9587c72
commit
fb2dcaf58a
@ -11,37 +11,6 @@
|
||||
(maphash (lambda (key val) (insert key)) lob)
|
||||
|
||||
|
||||
|
||||
* One Liner
|
||||
Here's a different type of syntax that may work.
|
||||
|
||||
This uses one-liners of the form
|
||||
|
||||
: #+lob:source-block-name variables
|
||||
|
||||
This takes advantage of the functionality already present in
|
||||
[[file:lisp/org-babel-ref.el][org-babel-ref]] for resolving references to other source blocks which
|
||||
may contain variable specifications. See the bottom half of
|
||||
[[file:lisp/org-babel-lob.el][org-babel-lob]] for the new implementation. To test it out load
|
||||
org-babel-lob and press =C-cC-c= on the =#+lob:= line further down.
|
||||
|
||||
#+resname: R-plot-default-data
|
||||
| 0 | 0 |
|
||||
|
||||
#+srcname: my-R-plot
|
||||
#+begin_src R :results silent :var data=R-plot-default-data
|
||||
plot(data)
|
||||
#+end_src
|
||||
|
||||
#+tblname: example-R-plot-data
|
||||
| 1 | 2 |
|
||||
| 2 | 4 |
|
||||
| 3 | 9 |
|
||||
| 4 | 16 |
|
||||
| 5 | 25 |
|
||||
|
||||
#+lob:my-R-plot data=example-R-plot-data
|
||||
|
||||
* Plotting code
|
||||
Plot column 2 (y axis) against column 1 (x axis). Columns 3 and beyond, if present, are ignored.
|
||||
|
||||
@ -53,43 +22,11 @@ plot(data)
|
||||
plot(data)
|
||||
#+end_src
|
||||
|
||||
#+begin_src babel :srcname plot :var data=10
|
||||
#+end_src
|
||||
#+tblname: example-R-plot-data
|
||||
| 1 | 2 |
|
||||
| 2 | 4 |
|
||||
| 3 | 9 |
|
||||
| 4 | 16 |
|
||||
| 5 | 25 |
|
||||
|
||||
#+srcname: plot
|
||||
#+begin_src R
|
||||
plot(__data__)
|
||||
#+end_src
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#+srcname: plot
|
||||
#+begin_src R
|
||||
plot(__data__)
|
||||
#+end_src
|
||||
|
||||
#+resname: plot
|
||||
: org_babel_R_eoe
|
||||
: org_babel_R_eoe
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#+begin_src R
|
||||
79
|
||||
#+end_src
|
||||
|
||||
#+resname:
|
||||
: 79
|
||||
: 78
|
||||
: 77
|
||||
#+lob:R-plot data=example-R-plot-data
|
||||
|
@ -30,40 +30,34 @@
|
||||
|
||||
;;; Code:
|
||||
(require 'org-babel)
|
||||
|
||||
(org-babel-add-interpreter "babel")
|
||||
|
||||
(setq org-babel-library-of-babel
|
||||
(progn (set-buffer
|
||||
(find-file-noselect "../library-of-babel.org"))
|
||||
(org-babel-get-all-src-block-infos)))
|
||||
|
||||
(defun org-babel-execute:babel (body params)
|
||||
"Execute a library-of-babel block.
|
||||
|
||||
These blocks do not have their own body. Instead they use
|
||||
a :srcname header argument to reference a different source
|
||||
block, whose body they use. Source blocks in the library of
|
||||
babel should use a standard naming scheme for the variable
|
||||
containing the input data for analysis / plotting. E.g. if that
|
||||
variable is always called __data__ then one of these bodyless
|
||||
babel blocks will call a library of babel block using :var
|
||||
__data__=<some reference>. The header args from a babel block
|
||||
are appended to the header args from the target block.
|
||||
|
||||
This function is called by `org-babel-execute-src-block'."
|
||||
(message "executing babel source code block...")
|
||||
(save-window-excursion
|
||||
(let* ((srcname (cdr (assoc :srcname params)))
|
||||
(info (or (save-excursion
|
||||
(goto-char (org-babel-find-named-block srcname))
|
||||
(org-babel-get-src-block-info))
|
||||
(gethash srcname org-babel-library-of-babel))))
|
||||
(org-babel-execute-src-block nil info params))))
|
||||
|
||||
;; alternate 1-liner syntax, this uses `seb' from org-babel-table.el
|
||||
(require 'org-babel-table)
|
||||
|
||||
(defvar org-babel-library-of-babel nil
|
||||
"Library of source-code blocks. This is an association list.
|
||||
Populate the library by adding files to `org-babel-lob-files'.")
|
||||
|
||||
(defcustom org-babel-lob-files '()
|
||||
"Files used to populate the `org-babel-library-of-babel'. To
|
||||
add files to this list use the `org-babel-lob-ingest' command."
|
||||
:group 'org-babel
|
||||
:type 'list)
|
||||
|
||||
(defun org-babel-lob-ingest (&optional file)
|
||||
"Add all source-blocks defined in FILE to `org-babel-library-of-babel'."
|
||||
(interactive "f")
|
||||
(org-babel-map-source-blocks file
|
||||
(let ((source-name (intern (org-babel-get-src-block-name)))
|
||||
(info (org-babel-get-src-block-info)))
|
||||
(setq org-babel-library-of-babel
|
||||
(cons (cons source-name info)
|
||||
(assq-delete-all source-name org-babel-library-of-babel))))))
|
||||
|
||||
(org-babel-lob-ingest ;; actually add the source-blocks defined in library-of-babel.org
|
||||
(expand-file-name "library-of-babel.org"
|
||||
(expand-file-name ".." (file-name-directory (or load-file-name buffer-file-name)))))
|
||||
|
||||
;; functions for executing lob one-liners
|
||||
|
||||
(defvar org-babel-lob-one-liner-regexp
|
||||
"#\\+lob:\\([^ \t\n\r]+\\)\\([ \t]+\\([^\n]+\\)\\)?\n")
|
||||
|
||||
@ -104,7 +98,7 @@ a list of the following form.
|
||||
(format "%s=%s" (first var-spec) (second var-spec)))
|
||||
(cdr info) ", ")
|
||||
")"))))
|
||||
(message "params=%S" params)
|
||||
(org-babel-execute-src-block t (list "emacs-lisp" "results" params))))
|
||||
|
||||
(provide 'org-babel-lob)
|
||||
;;; org-babel-lob.el ends here
|
||||
|
@ -186,16 +186,15 @@ of the following form. (language body header-arguments-alist)"
|
||||
(org-babel-parse-inline-src-block-match)
|
||||
nil)))) ;; indicate that no source block was found
|
||||
|
||||
(defun org-babel-get-all-src-block-infos ()
|
||||
"Get source-code block info for all blocks in buffer."
|
||||
(save-excursion
|
||||
(goto-char (point-min))
|
||||
(let ((blocks (make-hash-table :test 'equal)))
|
||||
(while (re-search-forward
|
||||
org-babel-named-src-block-regexp nil t)
|
||||
(puthash (match-string-no-properties 1) ;; srcname
|
||||
(org-babel-get-src-block-info) blocks)
|
||||
blocks))))
|
||||
(defmacro org-babel-map-source-blocks (file &rest body)
|
||||
"Evaluate BODY forms on each source-block in FILE."
|
||||
(declare (indent 1))
|
||||
`(save-window-excursion
|
||||
(find-file ,file) (goto-char (point-min))
|
||||
(while (re-search-forward org-babel-src-block-regexp nil t)
|
||||
(goto-char (match-beginning 0))
|
||||
(save-match-data ,@body)
|
||||
(goto-char (match-end 0)))))
|
||||
|
||||
(defun org-babel-parse-src-block-match ()
|
||||
(list (org-babel-clean-text-properties (match-string 1))
|
||||
|
Loading…
Reference in New Issue
Block a user