Merge branch 'master' of git@github.com:eschulte/org-babel

This commit is contained in:
Dan Davison 2009-08-20 17:21:57 +01:00
commit 22fbad3554
7 changed files with 313 additions and 112 deletions

View file

@ -52,6 +52,24 @@ plot(data)
** Gnuplot
* Table/Matrix manipulation
Elegant lisp code for transposing a matrix.
#+tblname: transpose-example
| 1 | 2 | 3 |
| 4 | 5 | 6 |
#+srcname: transpose
#+begin_src emacs-lisp :var table=transpose-example
(apply #'mapcar* #'list table)
#+end_src
#+resname:
| 1 | 4 |
| 2 | 5 |
| 3 | 6 |
* Misc
#+srcname: python-identity(a=1)
#+begin_src python

View file

@ -0,0 +1,68 @@
;;; org-babel-sass.el --- org-babel functions for the sass css generation language
;; Copyright (C) 2009 Eric Schulte
;; Author: Eric Schulte
;; 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:
;; For more information on sass see http://sass-lang.com/
;;
;; This accepts a 'file' header argument which is the target of the
;; compiled sass. The default output type for sass evaluation is
;; either file (if a 'file' header argument was given) or scalar if no
;; such header argument was supplied.
;;
;; A 'cmdline' header argument can be supplied to pass arguments to
;; the sass command line.
;;; Requirements:
;; - sass-mode :: http://github.com/nex3/haml/blob/master/extra/sass-mode.el
;;; Code:
(require 'org-babel)
(require 'sass-mode)
(org-babel-add-interpreter "sass")
(add-to-list 'org-babel-tangle-langs '("sass" "sass"))
(defun org-babel-execute:sass (body params)
"Execute a block of Sass code with org-babel. This function is
called by `org-babel-execute-src-block'."
(message "executing Sass source code block")
(let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
(file (cdr (assoc :file params)))
(out-file (or file (make-temp-file "org-babel-sass-out")))
(cmdline (cdr (assoc :cmdline params)))
(in-file (make-temp-file "org-babel-sass-in"))
(cmd (concat "sass " (or cmdline "") in-file " " out-file)))
(with-temp-file in-file (insert body)) (shell-command cmd)
(or file (with-temp-buffer (insert-file-contents out-file) (buffer-string)))))
(defun org-babel-prep-session:sass (session params)
(error "Sass does not support sessions"))
(provide 'org-babel-sass)
;;; org-babel-sass.el ends here

View file

@ -31,16 +31,17 @@
;;; Code:
(require 'org-babel)
(require 'org-exp-blocks)
(add-to-list 'org-export-blocks '(src org-babel-exp-src-blocks))
(org-export-blocks-add-block '(src org-babel-exp-src-blocks nil))
(add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
(defun org-babel-exp-src-blocks (body &rest headers)
"Process src block for export. Depending on the 'export'
headers argument in replace the source code block with...
both ---- the default, display the code and the results
both ---- display the code and the results
code ---- display the code inside the block but do not process
code ---- the default, display the code inside the block but do
not process
results - process the block and replace it with the results of
execution
@ -48,7 +49,7 @@ results - process the block and replace it with the results of
none ----- do not display either code or results upon export"
(interactive)
(unless headers (error "org-babel can't process a source block without knowing the source code"))
(message "org-babel processing...")
(message "org-babel-exp processing...")
(let ((lang (car headers))
(params (org-babel-parse-header-arguments (mapconcat #'identity (cdr headers) " "))))
(org-babel-exp-do-export lang body params)))
@ -70,12 +71,12 @@ options and are taken from `org-babel-defualt-inline-header-args'."
(defun org-babel-exp-do-export (lang body params &optional inline)
(case (intern (or (cdr (assoc :exports params)) "code"))
('none "")
('code (org-babel-exp-code body lang params inline))
('results (org-babel-exp-results body lang params inline))
('both (concat (org-babel-exp-code body lang params inline)
"\n\n"
(org-babel-exp-results body lang params inline)))))
('none "")
('code (org-babel-exp-code body lang params inline))
('results (org-babel-exp-results body lang params inline))
('both (concat (org-babel-exp-code body lang params inline)
"\n\n"
(org-babel-exp-results body lang params inline)))))
(defun org-babel-exp-code (body lang params &optional inline)
(if inline

View file

@ -39,6 +39,15 @@ shebang(#!) line to use when writing out the language to file,
and an optional flag indicating that the language is not
commentable.")
;; This is just a place holder until this variable (or comparable) is
;; inserted into org-mode
(defcustom org-src-lang-modes
'(:ocaml "tuareg")
"Property list mapping languages to their major mode.
The key is the language name, the value is the string that should
be inserted as the name of the major mode."
:type 'plist)
(defun org-babel-load-file (file)
"Load the contents of the Emacs Lisp source code blocks in the
org-mode formatted FILE. This function will first export the
@ -78,7 +87,11 @@ exported source code blocks by language."
(lambda (by-lang)
(let* ((lang (car by-lang))
(specs (cdr by-lang))
(lang-f (intern (concat lang "-mode")))
(lang-f (intern (concat
(or (plist-get org-src-lang-modes
(intern (concat ":" lang)))
lang)
"-mode")))
(lang-specs (cdr (assoc lang org-babel-tangle-langs)))
(ext (first lang-specs))
(she-bang (second lang-specs))
@ -93,7 +106,9 @@ exported source code blocks by language."
((> (length tangle) 0) tangle))
target-file))
(file-name (when base-name
(concat base-name "." ext))))
(if (string= base-name
(file-name-sans-extension base-name))
(concat base-name "." ext) base-name))))
;; ;; debugging
;; (message "tangle=%S base-name=%S file-name=%S"
;; tangle base-name file-name)
@ -108,7 +123,8 @@ exported source code blocks by language."
(when she-bang (insert (concat she-bang "\n")))
(when commentable
(comment-region
(point) (progn (insert "generated by org-babel-tangle") (point))))
(point) (progn (insert "generated by org-babel-tangle") (point)))
(move-end-of-line nil))
(org-babel-spec-to-string spec)
(append-to-file nil nil file-name))
;; update counter
@ -161,7 +177,8 @@ form
(link source-name params body)"
(flet ((insert-comment (text)
(when commentable
(comment-region (point) (progn (insert text) (point))))))
(comment-region (point) (progn (insert text) (point)))
(move-end-of-line nil))))
(let ((link (first spec))
(source-name (second spec))
(body (fourth spec))
@ -193,12 +210,14 @@ the source-code block which holds BODY."
(setq new-body (concat new-body text))))
(with-temp-buffer
(insert body) (goto-char (point-min))
(funcall (intern (concat lang "-mode")))
(funcall (intern (concat (or (plist-get org-src-lang-modes
(intern (concat ":" lang)))
lang) "-mode")))
(setq index (point))
(while (and (re-search-forward "<<\\(.+\\)>>" nil t))
(save-match-data (setf source-name (match-string 1)))
;; add interval to new-body
(goto-char (match-end 0))
(goto-char (match-end 0)) (move-end-of-line nil)
(nb-add (buffer-substring index (point)))
(setq index (point))
;; if found, add body of referenced source-block
@ -208,7 +227,8 @@ the source-code block which holds BODY."
(if point
(save-excursion
(goto-char point)
(concat "\n" (second (org-babel-get-src-block-info))))
(concat "\n" (org-babel-expand-noweb-references
(org-babel-get-src-block-info))))
"")))))
(nb-add (buffer-substring index (point-max)))))
new-body))

View file

@ -272,10 +272,16 @@ concerned with creating elisp versions of results. "
This function is analogous to org-babel-lob-get-info. For both
functions, after they are called, (match-string 1) matches the
function name, and (match-string 2) matches the function
function name, and (match-string 3) matches the function
arguments inside the parentheses. I think perhaps these functions
should be renamed to bring out this similarity, perhaps involving
the word 'call'."
the word 'call'.
Currently the function `org-babel-get-src-block-function-args'
relies on the match-data from a match in this function. I think
splitting a match and the use of it's data is bad form, and we
should re-work these two functions, perhaps combining them into
one function which returns more data than just the name. [Eric]"
(let ((case-fold-search t)
(head (org-babel-where-is-src-block-head)))
(if head
@ -283,7 +289,10 @@ the word 'call'."
(goto-char head)
(if (save-excursion
(forward-line -1)
(looking-at "#\\+srcname:[ \f\t\n\r\v]*\\([^ ()\f\t\n\r\v]+\\)\(\\(.*\\)\)"))
;; the second match of this regexp is used later to
;; find arguments in the "functional" style, where
;; they are passed as part of the source name line
(looking-at "#\\+srcname:[ \f\t\n\r\v]*\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
(org-babel-clean-text-properties (match-string 1)))))))
(defun org-babel-get-src-block-info ()
@ -302,7 +311,7 @@ of the following form. (language body header-arguments-alist)"
(defun org-babel-get-src-block-function-args ()
(when (org-babel-get-src-block-name)
(mapcar (lambda (ref) (cons :var ref))
(org-babel-ref-split-args (match-string 2)))))
(org-babel-ref-split-args (match-string 3)))))
(defmacro org-babel-map-source-blocks (file &rest body)
"Evaluate BODY forms on each source-block in FILE."
@ -434,7 +443,7 @@ buffer or nil if no such result exists."
(goto-char (point-min))
(when (re-search-forward ;; ellow end-of-buffer in following regexp?
(concat "#\\+resname:[ \t]*" (regexp-quote name) "[ \t\n\f\v\r]") nil t)
(move-beginning-of-line 1) (point))))
(move-beginning-of-line 0) (point))))
(defun org-babel-where-is-src-block-result (&optional insert)
"Return the point at the beginning of the result of the current
@ -447,7 +456,7 @@ line. If no result exists for this block then create a
(name (if on-lob-line (org-babel-lob-get-info) (org-babel-get-src-block-name)))
(head (unless on-lob-line (org-babel-where-is-src-block-head))) end)
(when head (goto-char head))
(or (and name (message name) (org-babel-find-named-result name))
(or (and name (org-babel-find-named-result name))
(and (or on-lob-line (re-search-forward "#\\+end_src" nil t))
(progn (move-end-of-line 1)
(if (eobp) (insert "\n") (forward-char 1))
@ -455,7 +464,8 @@ line. If no result exists for this block then create a
(or (progn ;; either an unnamed #+resname: line already exists
(re-search-forward "[^ \f\t\n\r\v]" nil t)
(move-beginning-of-line 1) (looking-at "#\\+resname:"))
(when insert ;; or (with optional insert) we need to back up and make one ourselves
;; or (with optional insert) we need to back up and make one ourselves
(when insert
(goto-char end) (open-line 2) (forward-char 1)
(insert (concat "#+resname:" (if name (concat " " name))))
(move-beginning-of-line 1) t)))
@ -492,13 +502,24 @@ current source block. With optional argument INSERT controls
insertion of results in the org-mode file. INSERT can take the
following values...
t ------ the default option, simply insert the results after the
source block
replace - (default option) insert results after the source block
replacing any previously inserted results
replace - insert results after the source block replacing any
previously inserted results
silent -- no results are inserted
silent -- no results are inserted"
raw ----- results are added directly to the org-mode file. This
is a good option if you code block will output org-mode
formatted text.
org ----- this is the same as the 'raw' option
html ---- results are added inside of a #+BEGIN_HTML block. This
is a good option if you code block will output html
formatted text.
latex --- results are added inside of a #+BEGIN_LATEX block.
This is a good option if you code block will output
latex formatted text."
(if (stringp result)
(progn
(setq result (org-babel-clean-text-properties result))
@ -522,10 +543,14 @@ silent -- no results are inserted"
(if (stringp result) ;; assume the result is a table if it's not a string
(if (member "file" insert)
(insert result)
(if (or (member "raw" insert) (member "org" insert))
(progn (save-excursion (insert result))
(if (org-at-table-p) (org-cycle)))
(org-babel-examplize-region (point) (progn (insert result) (point)))))
(if (member "html" insert)
(insert (format "#+BEGIN_HTML\n%s#+END_HTML\n" result))
(if (member "latex" insert)
(insert (format "#+BEGIN_LaTeX\n%s#+END_LaTeX\n" result))
(if (or (member "raw" insert) (member "org" insert))
(progn (save-excursion (insert result))
(if (org-at-table-p) (org-cycle)))
(org-babel-examplize-region (point) (progn (insert result) (point)))))))
(progn
(insert
(concat (orgtbl-to-orgtbl
@ -553,12 +578,20 @@ relies on `org-babel-insert-result'."
(save-excursion
(if (org-at-table-p)
(progn (goto-char (org-table-end)) (forward-line 1) (point))
(let ((case-fold-search nil))
(if (looking-at "#\\+begin_example")
(search-forward "#+end_example" nil t)
(progn (while (looking-at "\\(: \\|\\[\\[\\)")
(forward-line 1))
(forward-line 1))))
(let ((case-fold-search t))
(cond
((looking-at "#\\+begin_latex")
(search-forward "#+end_latex" nil t)
(forward-line 2))
((looking-at "#\\+begin_html")
(search-forward "#+end_html" nil t)
(forward-line 2))
((looking-at "#\\+begin_example")
(search-forward "#+end_example" nil t)
(forward-line 2))
(t (progn (while (looking-at "\\(: \\|\\[\\[\\)")
(forward-line 1))
(forward-line 1)))))
(point))))
(defun org-babel-result-to-file (result)
@ -621,10 +654,11 @@ parameters when merging lists."
ref (match-string 2 (cdr pair))
vars (cons (cons var ref) (assq-delete-all var vars)))))
(:results
(setq results (e-merge '(("file" "vector" "scalar")
("replace" "silent")
("output" "value"))
results (split-string (cdr pair)))))
(setq results (e-merge
'(("file" "vector" "scalar" "raw" "org" "html" "latex")
("replace" "silent")
("output" "value"))
results (split-string (cdr pair)))))
(:exports
(setq exports (e-merge '(("code" "results" "both"))
exports (split-string (cdr pair)))))

View file

@ -71,6 +71,7 @@
;; (require 'org-babel-ocaml) ;; ocaml, and tuareg-mode
;; (require 'org-babel-python) ;; python, and python-mode
;; (require 'org-babel-ruby) ;; ruby, irb, ruby-mode, and inf-ruby mode
;; (require 'org-babel-sass) ;; sass, sass-mode
;; (require 'org-babel-sql) ;; none
;;
;; Once you've activated languages, load the library of babel for
@ -353,7 +354,7 @@ to coexist and cooperate inside of a single document.
:END:
#+begin_quote
Let us change our traditional attitude to the con- struction of
Let us change our traditional attitude to the construction of
programs: Instead of imagining that our main task is to instruct a
/computer/ what to do, let us concentrate rather on explaining to
/human beings/ what we want a computer to do.

View file

@ -218,70 +218,12 @@ would then be [[#sandbox][the sandbox]].
#+end_src
* Tasks [41/64]
** TODO org-bable-tangle: no default extension if one already exists
* Tasks [43/63]
** TODO source-name visible in LaTeX and html exports
Maybe this should be done in backend specific manners.
The listings package may provide for naming a source-code block...
** STARTED new results types (org, html, latex)
Thanks to Tom Short for this recommendation.
- raw or org :: in which case the results are implemented raw, unquoted
into the org-mode file. This would also handle links as
source block output.
- html :: the results are inserted inside of a #+BEGIN_HTML block
- latex :: the results are inserted inside of a #+BEGIN_LATEX block
It might look like:
: #+begin_src R :session *R* :results org
: cat("***** This is a table\n")
: cat("| 1 | 2 | 3 |\n")
: cat("[[http://google.com][Google it here]]\n"
: #+end_src
:
: #+resname:
: ***** This is a table
: | 1 | 2 | 3 |
[[http://google.com][: Google it here]]
We actually might want to remove the =#+resname= line if the results
type is org-mode, not sure... Either way I don't think there is a
good way to capture/remove org type results.
*** raw
Added a =raw= results header argument, which will insert the results
of a source-code block into an org buffer un-escaped. Also, if the
results look like a table, then the table will be aligned.
#+srcname: raw-table-demonstration
#+begin_src ruby :results output raw
puts "| root | square |"
puts "|---"
10.times do |n|
puts "| #{n} | #{n*n} |"
end
#+end_src
#+resname:
| root | square |
|------+--------|
| 0 | 0 |
| 1 | 1 |
| 2 | 4 |
| 3 | 9 |
| 4 | 16 |
| 5 | 25 |
| 6 | 36 |
| 7 | 49 |
| 8 | 64 |
| 9 | 81 |
** PROPOSED raise elisp error when source-blocks return errors
Not sure how/if this would work, but it may be desirable.
** PROPOSED allow `anonymous' function block with function call args?
My question here is simply whether we're going to allow
#+begin_src python(arg=ref)
@ -323,7 +265,6 @@ but with preference given to
update the source-code blocks timestamp. If the user edits the
contents of a source-code block directly I can think of no
efficient way of maintaining the timestamp.
** TODO make tangle files read-only?
With a file-local variable setting, yea that makes sense. Maybe
the header should reference the related org-mode file.
@ -633,7 +574,6 @@ org-mode core
**** DONE a hook called when the src edit buffer is created
This should be implemented in the org-mode core
** TODO resolve references to other org buffers/files
This would allow source blocks to call upon tables, source-blocks,
and results in other org buffers/files.
@ -721,7 +661,6 @@ above would have a couple of points in its favour:
In interactive use we might want to allow the user to choose between
screen and file output. In non-interactive use such as export, it
would be file output (subject to the :exports directives).
** TODO Finalise behaviour regarding vector/scalar output
*** DONE Stop spaces causing vector output
This simple example of multilingual chaining produces vector output if
@ -845,7 +784,6 @@ du -sc ~/*
#+begin_src emacs-lisp :var sizes=sizes :results replace
(mapcar #'car sizes)
#+end_src
** TODO command line execution
Allow source code blocks to be called form the command line. This
will be easy using the =sbe= function in [[file:lisp/org-babel-table.el][org-babel-table.el]].
@ -878,7 +816,6 @@ we should color these blocks differently
*** TODO refine html exportation
should use a span class, and should show original source in tool-tip
** TODO LoB: re-implement plotting and analysis functions from org-R
I'll do this soon, now that we things are a bit more settled and we
have column names in R.
@ -1064,7 +1001,6 @@ tabel
Another example is in the [[*operations%20in%20on%20tables][grades example]].
** DEFERRED use textConnection to pass tsv to R?
When passing args from the org buffer to R, the following route is
used: arg in buffer -> elisp -> tsv on file -> data frame in R. I
@ -1179,7 +1115,6 @@ I have no idea how this could work...
edit out some lines, and then convert it to org-babel format for
posterity. Same for a shell session either in a *shell* buffer, or
pasted from another terminal emulator. And python of course.
** DEFERRED improve the source-block snippet
any real improvement seems somewhat beyond the ability of yasnippet
for now.
@ -1257,7 +1192,100 @@ to the command if BUFF is not given.)
2) The function is called inside of a =write.table= function call
writing the results to a table
3) The table is read using =org-table-import=
** DONE new results types (org, html, latex)
Thanks to Tom Short for this recommendation.
- raw or org :: in which case the results are implemented raw, unquoted
into the org-mode file. This would also handle links as
source block output.
- html :: the results are inserted inside of a #+BEGIN_HTML block
- latex :: the results are inserted inside of a #+BEGIN_LATEX block
It might look like:
: #+begin_src R :session *R* :results org
: cat("***** This is a table\n")
: cat("| 1 | 2 | 3 |\n")
: cat("[[http://google.com][Google it here]]\n"
: #+end_src
:
: #+resname:
: ***** This is a table
: | 1 | 2 | 3 |
[[http://google.com][: Google it here]]
We actually might want to remove the =#+resname= line if the results
type is org-mode, not sure... Either way I don't think there is a
good way to capture/remove org type results.
*** LaTeX
#+srcname: latex-results
#+begin_src emacs-lisp :results latex
"this should be inside of a LaTeX block"
#+end_src
#+resname:
#+BEGIN_LaTeX
this should be inside of a LaTeX block
#+END_LaTeX
*** Html
#+srcname: html-results
#+begin_src emacs-lisp :results html
"this should be inside of a HTML block
and more
is long"
#+end_src
#+resname:
#+BEGIN_HTML
this should be inside of a HTML block
and more
is long
#+END_HTML
*** raw
Added a =raw= results header argument, which will insert the results
of a source-code block into an org buffer un-escaped. Also, if the
results look like a table, then the table will be aligned.
#+srcname: raw-table-demonstration
#+begin_src ruby :results output raw
puts "| root | square |"
puts "|---"
10.times do |n|
puts "| #{n} | #{n*n} |"
end
#+end_src
#+resname:
| root | square |
|------+--------|
| 0 | 0 |
| 1 | 1 |
| 2 | 4 |
| 3 | 9 |
| 4 | 16 |
| 5 | 25 |
| 6 | 36 |
| 7 | 49 |
| 8 | 64 |
| 9 | 81 |
Not sure how/if this would work, but it may be desirable.
** DONE org-bable-tangle: no default extension if one already exists
** DONE take default values for header args from properties
Use file-wide and subtree wide properties to set default values for
header args.
@ -2635,7 +2663,7 @@ This is currently working only with emacs lisp as in the following
example in the [[* emacs lisp source reference][emacs lisp source reference]].
** TODO Add languages [10/14]
** TODO Add languages [11/15]
I'm sure there are many more that aren't listed here. Please add
them, and bubble any that you particularly care about up to the top.
@ -2702,6 +2730,23 @@ Things left to do
| "information_schema" |
| "test" |
*** DONE SASS
Sass is a very nice extension of CSS, which is much nicer to read and
write (see [[http://sass-lang.com/][sass-lang]]).
#+srcname: sass-example
#+begin_src sass :file stylesheet.css :results file
#me
position: absolute
top: 1em
left: 1em
.head
text-align: center
#+end_src
#+resname:
[[file:stylesheet.css][stylesheet.css]]
*** DONE CSS
trivial [[file:lisp/langs/org-babel-css.el][org-babel-css.el]]
@ -2875,7 +2920,21 @@ dot("$(2a,0)$",(2,0),N+E);
*** DONE sh
* Bugs [37/41]
* Bugs [39/43]
** DONE stripping indentation from source-code blocks
This is a problem in [[file:lisp/org-babel-exp.el][org-babel-exp.el]].
** DONE failing to write srcname to resname when evaluating a named block
#+srcname: please-name-my-result
#+begin_src emacs-lisp
"I'm the result"
#+end_src
#+resname: please-name-my-result
: I'm the result
** DONE Python session evaluation bug
The following block evaluates correctly with :session none
(set :results to output), but fails with session-based evaluation