adding a replace option, and simplified result insertion

This commit is contained in:
Eric Schulte 2009-02-22 22:19:34 -08:00
parent 7c51afb642
commit f7d0dd1fda
2 changed files with 59 additions and 31 deletions

View File

@ -1,4 +1,4 @@
;;; litorgy.el --- literate programing in org-mode
;;; litorgy.el --- literate programing in org-mode
;; Copyright (C) 2009 Eric Schulte, Dan Davison, Austin F. Frank
@ -44,12 +44,6 @@ so then run `litorgy-execute-src-block'."
(add-hook 'org-ctrl-c-ctrl-c-hook 'litorgy-execute-src-block-maybe)
(defcustom litorgy-example-size-cutoff 10
"Number at lines at which to switch from using the ': '
org-mode quote sytax to using a '#+BEGIN_EXAMPLE' block"
:group 'litorgy
:type 'integer)
(defvar litorgy-src-block-regexp nil
"Regexp used to test when inside of a litorgical src-block")
@ -65,7 +59,7 @@ org-mode quote sytax to using a '#+BEGIN_EXAMPLE' block"
(defcustom litorgy-interpreters '()
"Interpreters allows for evaluation tags.
This is a list of program names (as strings) that can evaluate code and
insert the output into an Org-mode buffer. Valid choices are
insert the output into an Org-mode buffer. Valid choices are
R Evaluate R code
emacs-lisp Evaluate Emacs Lisp code and display the result
@ -90,7 +84,7 @@ into the buffer immediately following the block. Results are
commented by `litorgy-make-region-example'. With optional prefix
don't dump results into buffer."
(interactive "P")
(let* ((info (litorgy-get-current-src-block-info))
(let* ((info (litorgy-get-src-block-info))
(lang (first info))
(body (second info))
(params (third info))
@ -99,7 +93,7 @@ don't dump results into buffer."
(unless (member lang litorgy-interpreters)
(error "Language is not in `litorgy-interpreters': %s" lang))
(setq result (funcall cmd body params))
(unless arg (litorgy-insert-result result))))
(unless arg (litorgy-insert-result result (assoc :replace params)))))
(defun litorgy-eval-subtree (&optional arg)
"Replace EVAL snippets in the entire subtree."
@ -111,7 +105,7 @@ don't dump results into buffer."
(litorgy-eval-src-block arg))
(widen)))
(defun litorgy-get-current-src-block-info ()
(defun litorgy-get-src-block-info ()
"Return the information of the current source block (the point
should be on the '#+begin_src' line) as a list of the following
form. (language body header-arguments-alist)"
@ -122,25 +116,44 @@ form. (language body header-arguments-alist)"
(let ((lang (litorgy-clean-text-properties (match-string 1)))
(args (litorgy-clean-text-properties (or (match-string 3) "")))
(body (litorgy-clean-text-properties (match-string 4))))
(list lang body
(mapc (lambda (arg)
(if (string-match "\\([^ :]+\\):\\([^ :]+\\)" arg)
(cons (match-string 1 arg) (match-string 2 arg))))
(split-string args)))))
(list lang body (litorgy-parse-header-arguments args))))
(defun litorgy-insert-result (result)
(defun litorgy-parse-header-arguments (arg-string)
"Parse a string of header arguments returning an alist."
(delq nil
(mapcar
(lambda (arg) (if (string-match "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]*\\([^ \f\t\n\r\v]*\\)" arg)
(cons (intern (concat ":" (match-string 1 arg))) (match-string 2 arg))))
(split-string (concat " " arg-string) "[ \f\t\n\r\v]+:"))))
(defun litorgy-insert-result (result &optional replace)
"Insert RESULT into the current buffer after the end of the
current source block. With optional argument REPLACE replace any
existing results currently located after the source block."
(if replace (litorgy-remove-result))
(save-excursion
(re-search-forward "^#\\+end_src" nil t) (open-line 1) (forward-char 2)
(let ((beg (point))
(end (progn (insert result)
(point))))
(message (format "from %S %S" beg end))
(litorgy-make-region-example beg end))))
(defun litorgy-remove-result ()
"Remove the result following the current source block"
(save-excursion
(re-search-forward "^#\\+end_src" nil t)
(forward-char 1)
(delete-region (point)
(save-excursion (forward-line 1)
(while (if (looking-at ": ")
(progn (while (looking-at ": ")
(forward-line 1)) t))
(forward-line 1))
(forward-line -1)
(point)))))
(defun litorgy-make-region-example (beg end)
"Comment out region using either the '^:' or the BEGIN_EXAMPLE
syntax based on the size of the region as compared to
`litorgy-example-size-cutoff'."
"Comment out region using the ': ' org example quote."
(interactive "*r")
(let ((size (abs (- (line-number-at-pos end)
(line-number-at-pos beg)))))
@ -148,14 +161,10 @@ syntax based on the size of the region as compared to
(let ((result (buffer-substring beg end)))
(delete-region beg end)
(insert (concat ": " result)))
(if (<= size litorgy-example-size-cutoff)
(save-excursion
(save-excursion
(goto-char beg)
(dotimes (n size)
(move-beginning-of-line 1) (insert ": ") (forward-line 1)))
(let ((result (buffer-substring beg end)))
(delete-region beg end)
(insert (concat "#+BEGIN_EXAMPLE\n" result "#+END_EXAMPLE\n")))))))
(move-beginning-of-line 1) (insert ": ") (forward-line 1))))))
(defun litorgy-clean-text-properties (text)
"Strip all properties from text return."

View File

@ -24,6 +24,7 @@ The main objectives of this project are...
- [[* evaluation of embedded source code][evaluation of embedded source code]]
- [[* execution on demand and on export][execution on demand and on export]]
- [[* source blocks][source blocks]]
- [[* header arguments][header arguments]]
- [[* inline source evaluation][inline source evaluation]]
- [[* included source file evaluation][included source file evaluation]] ?? maybe
- [[* caching of evaluation][caching of evaluation]]
@ -117,6 +118,14 @@ The main objectives of this project are...
*** source blocks
(see [[* Special editing and evaluation of source code][Special editing and evaluation of source code]])
*** header arguments
(see [[* block headers/parameters][block headers/parameters]])
There are going to be many cases where we want to use header arguments
to change the evaluation options of source code, to pass external
information to a block of source code and control the inclusion of
evaluation results.
*** inline source evaluation
*** included source file evaluation
It may be nice to be able to include an entire external file of source
@ -269,7 +278,6 @@ plus a table reference within that org file. Thus maybe something like
(R-obj-name-2 . (:tblref tbl-name-or-id-2 :file file-2)))
#+end_src emacs-lisp
*** source-target pairs
The following can be used for special considerations based on
@ -287,7 +295,6 @@ plus a table reference within that org file. Thus maybe something like
**** org properties from source block
**** org properties from org table
** export
once the previous objectives are met export should be fairly simple.
Basically it will consist of triggering the evaluation of source code
@ -450,6 +457,17 @@ should include (but should certainly not be limited to)
- file :: names of file to which graphical/textual/numerical/tabular output
should be written. Do we need this, or should this be controlled
through the source code itself?
- results :: indication of where the results should be placed, maybe
the following values...
- append :: *default* meaning just append to the current buffer
immediately following the current source block
- replace :: like append, but replace any results currently there
- file :: save the results in a new file, and place a link to the
file into the current buffer immediately following the
source code block
- table :: save the results into a table, maybe use a table id:range
to identify which table and where therein
- nil :: meaning just discard the results
- not sure of a good name here :: flags for when/if the block should
be evaluated (on export etc...)
- again can't thing of a concise name :: flags for how the results of
@ -459,6 +477,8 @@ should include (but should certainly not be limited to)
- flags specific to the language of the source block
- etc...
I think fleshing out this list is an important next step.
** Interaction with the R process
We should take care to implement this in such a way that all of the
@ -523,7 +543,7 @@ to evaluate the following blocks of code by pressing =\C-c\C-c= on the
header lines. *Note*: your version of org-mode must be at least 6.23
or later.
#+begin_src sh
#+begin_src sh :replace t
date
#+end_src
@ -581,6 +601,5 @@ to use for evaluating R code files.
See the [[* litorgy.el beginning functionality][Sandbox]] for evaluable examples.
* Buffer Dictionary
LocalWords: DBlocks dblocks litorgy el