working on sessions

This commit is contained in:
Eric Schulte 2009-06-02 16:26:57 -07:00
parent e4263daf76
commit bba80bb837
10 changed files with 405 additions and 31 deletions

View File

@ -36,6 +36,7 @@
(require 'org-babel-ref)
(require 'org-babel-exp)
(require 'org-babel-table)
(require 'org-babel-session)
;; language specific files
(require 'org-babel-script)

View File

@ -34,7 +34,7 @@
(defun org-babel-script-add-interpreter (var cmds)
(set-default var cmds)
(mapc (lambda (cmd)
(setq org-babel-interpreters (cons cmd org-babel-interpreters))
(org-babel-add-interpreter cmd)
(eval
`(defun ,(intern (concat "org-babel-execute:" cmd)) (body params)
,(concat "Evaluate a block of " cmd " script with org-babel. This function is
@ -65,6 +65,8 @@ executed through org-babel."
:group 'org-babel
:set 'org-babel-script-add-interpreter)
(mapc #'org-babel-add-interpreter org-babel-script-interpreters)
(defun org-babel-script-execute (cmd body params)
"Run CMD on BODY obeying any options set with PARAMS."
(message (format "executing %s code block..." cmd))

46
lisp/org-babel-session.el Normal file
View File

@ -0,0 +1,46 @@
;;; org-babel-session.el --- session management for org-babel
;; Copyright (C) 2009 Eric Schulte, Dan Davison
;; Author: Eric Schulte, Dan Davison
;; 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:
;; Org-Babel evaluates code in the context of consistent sessions.
;; This file will hold functions for interacting with these sections.
;;
;; For more information see org-babel.org in the top level directory.
;;; Code:
(require 'org-babel)
(defcustom org-babel-session-defaults nil
"An a-list associating each org-babel interpreter with a
default session buffer."
:group 'org-babel
:type 'alist)
(provide 'org-babel-session)
;;; org-babel-session.el ends here

View File

@ -34,7 +34,7 @@
(defun org-babel-shell-add-interpreter (var cmds)
(set-default var cmds)
(mapc (lambda (cmd)
(setq org-babel-interpreters (cons cmd org-babel-interpreters))
(org-babel-add-interpreter cmd)
(eval
`(defun ,(intern (concat "org-babel-execute:" cmd)) (body params)
,(concat "Evaluate a block of " cmd " shell with org-babel. This function is

View File

@ -26,7 +26,7 @@
;;; Commentary:
;; See rorg.org in the parent directory for more information
;; See org-babel.org in the parent directory for more information
;;; Code:
(require 'org)
@ -72,6 +72,7 @@ then run `org-babel-execute-src-block'."
`org-babel-src-block-regexp' appropriately."
(unless (member interpreter org-babel-interpreters)
(setq org-babel-interpreters (cons interpreter org-babel-interpreters))
(add-to-list 'org-babel-session-defaults (cons interpreter (format "org-babel-%s" interpreter)))
(org-babel-set-interpreters 'org-babel-interpreters org-babel-interpreters)))
(defcustom org-babel-interpreters '()
@ -174,13 +175,13 @@ of the following form. (language body header-arguments-alist)"
(defun org-babel-parse-src-block-match ()
(list (org-babel-clean-text-properties (match-string 1))
(org-babel-clean-text-properties (match-string 4))
(org-babel-strip-protective-comas (org-babel-clean-text-properties (match-string 4)))
(org-combine-plists org-babel-default-header-args
(org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 3) ""))))))
(defun org-babel-parse-inline-src-block-match ()
(list (org-babel-clean-text-properties (match-string 1))
(org-babel-clean-text-properties (match-string 4))
(org-babel-strip-protective-comas (org-babel-clean-text-properties (match-string 4)))
(org-combine-plists org-babel-default-inline-header-args
(org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 3) ""))))))
@ -334,6 +335,10 @@ non-nil."
"Strip all properties from text return."
(set-text-properties 0 (length text) nil text) text)
(defun org-babel-strip-protective-comas (body)
"Strip protective comas from bodies of source blocks."
(replace-regexp-in-string "^,#" "#" body))
(defun org-babel-read (cell)
"Convert the string value of CELL to a number if appropriate.
Otherwise if cell looks like a list (meaning it starts with a

View File

@ -68,12 +68,12 @@ pie(dirs[,1], labels = dirs[,2])
#+tblname: grades-table
| student | grade | letter |
|---------+-------+--------|
| 1 | 88 | B |
| 2 | 63 | D |
| 3 | 89 | B |
| 4 | 7 | F |
| 5 | 86 | B |
| 6 | 77 | C |
| 1 | 99 | A |
| 2 | 59 | F |
| 3 | 75 | C |
| 4 | 15 | F |
| 5 | 7 | F |
| 6 | 13 | F |
#+TBLFM: $2='(sbe random-score-generator)::$3='(sbe assign-grade (score $2))
#+srcname: assign-grade
@ -114,8 +114,10 @@ table, allowing the test suite to be run be evaluation of the table
and the results to be collected in the same table.
* Tasks [21/34]
** TODO Create objects in top level (global) environment in R?
* Tasks [22/36]
** TODO Create objects in top level (global) environment [0/5]
*sessions*
*** initial requirement statement [DED]
At the moment, objects created by computations performed in the
code block are evaluated in the scope of the
@ -187,11 +189,145 @@ Please let me know if you foresee any problems with this proposed setup,
or if you think any parts might be confusing for people coming from
Sweave. I'll hopefully find some time to work on this later in the
week.
*** implementation
in [[file:lisp/org-babel-comint.el][org-babel-comint.el]]
Currently I've coppied and begun generalizing the functions for
interacting with R buffers.
*** TODO rework all source codes to use inferior-processes-buffers [0/3]
this will involve...
1) [ ] creating an a-list of default *session* buffers for each source
language
2) [ ] functions for dumping code to the *session* buffers which can
be overridden by each source code language
3) [ ] functions for retrieving results from the *sessions* buffers
which can be overridden by each source code
#+srcname: task-sessions-for-r
#+begin_src R
a <- 9
b <- 10
a + b
#+end_src
*** TODO implement a *session* header argument
use this header argument to override the default *session* buffer
*** TODO remove source bodies from their functional wrappers
The current functional wrappers should be removed in favor of
incremental evaluation in inferior-source-buffers
*** TODO function to bring up inferior-process buffer
This should be callable from inside of a source-code block in an
org-mode buffer. It should evaluate the header arguments, then bring
up the inf-proc buffer using =pop-to-buffer=.
*** TODO function to dump last N lines from inf-proc buffer into the current source block
Callable with a prefix argument to specify how many lines should be
dumped into the source-code buffer.
** TODO support for working with =*Org Edit Src Example*= buffers [1/4]
*** TODO set buffer-local-process variables appropriately [DED]
I think something like this would be great. You've probably
already thought of this, but just to note it down: it would be really
nice if org-babel's notion of a buffer's 'session/process' played
nicely with ESS's notion of the buffer's session/process. ESS keeps
the current process name for a buffer in a buffer-local variable
ess-local-process-name. So one thing we will probably want to do is
make sure that the *Org Edit Src Example* buffer sets that variable
appropriately. [DED]
I had not thought of that, but I agree whole heartedly. [Eric]
Once this is done every variable should be able to dump regions into
their inferior-process buffer using major-mode functions.
*** TODO some possible requests/proposed changes for Carsten [2/3]
While I remember, some possible requests/proposed changes for Carsten
come to mind in that regard:
**** DONE Remap C-x C-s to save the source to the org buffer?
I've done this personally and I find it essential. I'm using
#+begin_src emacs-lisp
(defun org-edit-src-save ()
"Update the parent org buffer with the edited source code, save
the parent org-buffer, and return to the source code edit
buffer."
(interactive)
(let ((p (point)))
(org-edit-src-exit)
(save-buffer)
(org-edit-src-code)
(goto-char p)))
(define-key org-exit-edit-mode-map "\C-x\C-s" 'org-edit-src-save)
#+end_src
which seems to work.
I think this is great, but I think it should be implemented in the
org-mode core
**** TODO Rename buffer and minor mode?
Something shorter than *Org Edit Src Example* for the buffer
name. org-babel is bringing org's source code interaction to a
level of maturity where the 'example' is no longer
appropriate. And if further keybindings are going to be added to
the minor mode then maybe org-edit-src-mode is a better name than
org-exit-edit-mode.
Maybe we should name the buffer with a combination of the source
code and the session. I think that makes sense.
[ES] Are you also suggesting a new org-edit-src minor mode?
[DED] org-exit-edit-mode is a minor mode that already exists:
Minor mode installing a single key binding, "C-c '" to exit special edit.
org-edit-src-save now has a binding in that mode, so I guess all
I'm saying at this stage is that it's a bit of a misnomer. But
perhaps we will also have more functionality to add to that minor
mode, making it even more of a misnomer. Perhaps something like
org-src-mode would be better.
**** DEFERRED a hook called when the src edit buffer is created
This should be implemented in the org-mode core
*** DEFERRED send code to inferior process
Another thought on this topic: I think we will want users to send
chunks of code to the interpreter from within the *Org Edit Src*
buffer, and I think that's what you have in mind already. In ESS that
is done using the ess-eval-* functions. [DED]
I think we can leave this up to the major-mode in the source code
buffer, as almost every source-code major mode will have functions for
doing things like sending regions to the inferior process. If
anything we might need to set the value of the buffer local inferior
process variable. [Eric]
*** TODO optionally evaluate header references when we switch to =*Org Edit Src*= buffer
That seems to imply that the header references need to be evaluated
and transformed into the target language object when we hit C-c ' to
enter the *Org Edit Src* buffer [DED]
Good point, I heartily agree that this should be supported [Eric]
(or at least before the first time we attempt to evaluate code in that
buffer -- I suppose there might be an argument for lazy evaluation, in
case someone hits C-c ' but is "just looking" and not actually
evaluating anything.) Of course if evaluating the reference is
computationally intensive then the user might have to wait before they
get the *Org Edit Src* buffer. [DED]
I fear that it may be hard to anticipate when the references will be
needed, some major-modes do on-the-fly evaluation while the buffer is
being edited. I think that we should either do this before the buffer
is opened or not at all, specifically I think we should resolve
references if the user calls C-c ' with a prefix argument. Does that
sound reasonable? [Eric]
Yes [Dan]
** TODO fully purge org-babel-R of direct comint interaction
try to remove all code under the [[file:lisp/org-babel-R.el::functions%20for%20evaluation%20of%20R%20code][;; functions for evaluation of R code]] line
@ -318,10 +454,21 @@ should use a span class, and should show original source in tool-tip
| 1 | 2 | 3 |
| 4 | schulte | 6 |
#+TBLNAME: egtable2
| 1 | 2 | 3 |
| 4 | schulte | 6 |
#+begin_src R var tabel=egtable
tabel
#+end_src
#+resname:
| "col1" | "col2" | "col3" |
|--------+-----------+--------|
| 1 | 2 | 3 |
| 4 | "schulte" | 6 |
Another example is in the [[*operations%20in%20on%20tables][grades example]].
** PROPOSED conversion between org-babel and noweb (e.g. .Rnw) format
@ -378,20 +525,6 @@ one that comes to mind is the ability to treat a source-code block
like a function which accepts arguments and returns results. Actually
this can be it's own TODO (see [[* source blocks as functions][source blocks as functions]]).
** DONE pass multiple reference arguments into R
Can we do this? I wasn't sure how to supply multiple 'var' header
args. Just delete this if I'm being dense.
This should be working, see the following example...
#+srcname: two-arg-example
#+begin_src R :var n=2 :var m=8
n + m
#+end_src
#+resname: two-arg-example
: 10
** 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
@ -544,6 +677,31 @@ to the command if BUFF is not given.)
posterity. Same for a shell session either in a *shell* buffer, or
pasted from another terminal emulator. And python of course.
** DONE Remove protective commas from # comments before evaluating
org inserts protective commas in front of ## comments in language
modes that use them. We need to remove them prior to sending code
to the interpreter.
#+srcname: testing-removal-of-protective-comas
#+begin_src ruby
,# this one might break it??
:comma_protection
#+end_src
** DONE pass multiple reference arguments into R
Can we do this? I wasn't sure how to supply multiple 'var' header
args. Just delete this if I'm being dense.
This should be working, see the following example...
#+srcname: two-arg-example
#+begin_src R :var n=2 :var m=8
n + m
#+end_src
#+resname: two-arg-example
: 10
** DONE ensure that table ranges work
when a table range is passed to org-babel as an argument, it should be
interpreted as a vector.

View File

@ -0,0 +1,3 @@
text line 1
text line 2
text line 3

View File

@ -0,0 +1,114 @@
From: "Eric H. Neilsen, Jr." <neilsen@fnal.gov>
Subject: [Orgmode] A tool for creating source code files from example and src blocks in org files
To: emacs-orgmode@gnu.org
Date: Tue, 02 Jun 2009 13:50:58 -0500
Hi,
First, thank you Carsten and others, for putting this thing together;
org-mode is the most useful tool I've run across in a long time.
I recently put together a few commands to create source code files
from SRC and EXAMPLE blocks in org-mode files. The original idea is to
be able to compile examples in my notes without needing to merge them
into a separate source file by hand. As I was writing it I realized
this is exactly the same tool one needs to turn org-mode into a
literate programming tool (see http://www.literateprogramming.com/ ),
so I adopted some of the nomenclature. (I tried literate programming a
few years ago, and gave up in frustration with the tools. I am tempted
to try again with org-mode.)
The two user level commands are org-tangle and org-untangle, best
explained through example. Running org-tangle in a buffer that looks
like this:
---------------------- begin /tmp/test.org ---------------------------
* Some file
Some text here
#+CHUNK file1.sh 1
#+BEGIN_SRC bash
echo "line 1"
echo "line 2"
#+END_SRC
more text
** Some subsection
#+CHUNK file1.sh 2
#+BEGIN_SRC bash
echo "line 5"
echo "line 6"
#+END_SRC
blah blah blah
#+CHUNK README.txt 1
#+BEGIN_EXAMPLE
text line 1
text line 2
text line 3
#+END_EXAMPLE
foo foo bar baz
#+CHUNK file1.sh 1.5
#+BEGIN_SRC bash
echo "line 3"
echo "line 4"
#+END_SRC
-------------- end /tmp/test.org ----------------
results in the creation of these two files, opened in emacs buffers:
------------- begin /tmp/file1.sh ----------------
# ORGCHUNK /tmp/test.org 1
echo "line 1"
echo "line 2"
# ORGCHUNK /tmp/test.org 1.5
echo "line 3"
echo "line 4"
# ORGCHUNK /tmp/test.org 2
echo "line 5"
echo "line 6"
------------- end /tmp/file1.sh ---------------------
and
------------ begin /tmp/README.txt -------------
text line 1
text line 2
text line 3
------------ end /tmp/README.txt ---------------
If you then edit file1.sh and run org-untangle in the buffer, changes
to it will overwrite the literal blocks in the org-mode file from
whence they came.
Is there any interest in this? My organization's rules for releasing
code to open source projects are being revised, but I am likely to be
able to do so under a BSD-like license. If there is interest, I will
push to try and figure out how to do this, and maybe even see if I can
do the assignment of copyright stuff needed for it to get under the
main umbrella (although I am not optimistic).
(If anyone feels like rewriting it, that would be even better; aside
from a fairly straightforward .emacs, this is my first ever lisp code,
so it is likely to need cleaning and refactoring.)
-Eric
--
Eric H. Neilsen, Jr.
http://home.fnal.gov/~neilsen
_______________________________________________
Emacs-orgmode mailing list
Remember: use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

View File

@ -0,0 +1,9 @@
# ORGCHUNK /tmp/test.org 1
echo "line 1"
echo "line 2"
# ORGCHUNK /tmp/test.org 1.5
echo "line 3"
echo "line 4"
# ORGCHUNK /tmp/test.org 2
echo "line 5"
echo "line 6"

View File

@ -0,0 +1,36 @@
* Some file
Some text here
#+CHUNK file1.sh 1
#+BEGIN_SRC bash
echo "line 1"
echo "line 2"
#+END_SRC
more text
** Some subsection
#+CHUNK file1.sh 2
#+BEGIN_SRC bash
echo "line 5"
echo "line 6"
#+END_SRC
blah blah blah
#+CHUNK README.txt 1
#+BEGIN_EXAMPLE
text line 1
text line 2
text line 3
#+END_EXAMPLE
foo foo bar baz
#+CHUNK file1.sh 1.5
#+BEGIN_SRC bash
echo "line 3"
echo "line 4"
#+END_SRC