This commit is contained in:
Eric Schulte 2009-05-09 18:28:08 -07:00
parent 9865923f84
commit d7cbcd54d8
2 changed files with 55 additions and 112 deletions

View file

@ -37,15 +37,16 @@
;; - file :: path to the file containing the resource, or omitted if ;; - file :: path to the file containing the resource, or omitted if
;; resource is in the current file ;; resource is in the current file
;; ;;
;; - resource-id :: the id or name of the resource, or 'previous' to ;; - resource-id :: the id or name of the resource
;; grab the previous table, or 'next' to grab the
;; next table
;; ;;
;; So an example of a simple src block referencing table data in the ;; So an example of a simple src block referencing table data in the
;; same file would be ;; same file would be
;; ;;
;; #+var: table previous ;; #+TBLNAME: sandbox
;; #+begin_src emacs-lisp ;; | 1 | 2 | 3 |
;; | 4 | litorgy | 6 |
;;
;; #+begin_src emacs-lisp :var table=sandbox
;; (message table) ;; (message table)
;; #+end_src ;; #+end_src
;; ;;
@ -67,7 +68,6 @@ and find it's value using `litorgy-ref-resolve-reference'.
Return a list with two elements. The first element of the list Return a list with two elements. The first element of the list
will be the name of the variable, and the second will be an will be the name of the variable, and the second will be an
emacs-lisp representation of the value of the variable." emacs-lisp representation of the value of the variable."
(message (format "assignment=%S" assignment))
(if (string-match "\\(.+?\\)=\\(.+\\)" assignment) (if (string-match "\\(.+?\\)=\\(.+\\)" assignment)
(let ((var (match-string 1 assignment)) (let ((var (match-string 1 assignment))
(ref (match-string 2 assignment))) (ref (match-string 2 assignment)))
@ -88,7 +88,7 @@ return nil."
(defun litorgy-ref-resolve-reference (ref) (defun litorgy-ref-resolve-reference (ref)
"Resolve the reference and return it's value" "Resolve the reference and return it's value"
(save-excursion (save-excursion
(let (direction type args new-ref) (let (type args new-ref)
;; assign any arguments to pass to source block ;; assign any arguments to pass to source block
(when (string-match "\\(.+\\)\(\\(.+\\)\)" ref) (when (string-match "\\(.+\\)\(\\(.+\\)\)" ref)
(save-match-data (save-match-data
@ -98,12 +98,7 @@ return nil."
(when (string-match "\\(.+\\):\\(.+\\)" ref) (when (string-match "\\(.+\\):\\(.+\\)" ref)
(find-file (match-string 1 ref)) (find-file (match-string 1 ref))
(setf ref (match-string 2 ref))) (setf ref (match-string 2 ref)))
(cond ;; follow the reference in the current file
((string= ref "previous") (setq direction -1))
((string= ref "next") (setq direction 1))
(t
(goto-char (point-min)) (goto-char (point-min))
(setq direction 1)
(unless (let ((regexp (concat "^#\\+\\(TBL\\|SRC\\)NAME:[ \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)
@ -113,9 +108,9 @@ return nil."
;; 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 "reference '%s' not found in this buffer" ref))))) (error (format "reference '%s' not found in this buffer" ref)))
(while (not (setq type (litorgy-ref-at-ref-p))) (while (not (setq type (litorgy-ref-at-ref-p)))
(forward-line direction) (forward-line 1)
(beginning-of-line) (beginning-of-line)
(if (or (= (point) (point-min)) (= (point) (point-max))) (if (or (= (point) (point-min)) (= (point) (point-max)))
(error "reference not found"))) (error "reference not found")))

116
rorg.org
View file

@ -296,16 +296,9 @@ recognition of ruby arrays as such.
:PROPERTIES: :PROPERTIES:
:CUSTOM_ID: sandbox :CUSTOM_ID: sandbox
:END: :END:
This is a place for code examples To run these examples evaluate [[file:litorgy/litorgy-init.el][litorgy-init.el]]
** litorgy.el beginning functionality ** litorgy.el beginning functionality
After evaluating litorgy.el and litorgy-script.el, you should be able
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.
To run these examples open both [[file:litorgy/litorgy.el][litorgy.el]], [[file:litorgy/litorgy-script.el][litorgy-script.el]] and
evaluate them with =M-x eval-buffer=
#+begin_src sh :results replace #+begin_src sh :results replace
date date
@ -314,17 +307,19 @@ date
: Sun Apr 5 10:10:05 PDT 2009 : Sun Apr 5 10:10:05 PDT 2009
#+begin_src ruby #+begin_src ruby
puts Time.now Time.now
#+end_src #+end_src
: Sat May 09 18:18:33 -0700 2009
#+begin_src python #+begin_src python
print "Hello world!" "Hello World"
#+end_src #+end_src
: Hello World
** litorgy-R ** litorgy-R
To run these examples open both [[file:litorgy/litorgy.el][litorgy.el]], [[file:litorgy/litorgy-R.el][litorgy-R.el]] and evaluate
them with =M-x eval-buffer=
#+begin_src R :results replace #+begin_src R :results replace
a <- 9 a <- 9
@ -332,60 +327,13 @@ b <- 17
a + b a + b
#+end_src #+end_src
| 26 | : 26
#+begin_src R #+begin_src R
hist(rgamma(20,3,3)) hist(rgamma(20,3,3))
#+end_src #+end_src
** free variables
First assign the variable with some sort of interpreted line
- this is independent of any particular type of source code
- this could use references to table ranges
** resource reference example
*Note*: this example is largely *defunct*, see the
[[* litorgy plays with tables][litorgy-plays-with-tables]] section below.
This block holds an array of information written in [[http://www.yaml.org][YAML]]
#name: yaml-array
#+begin_src yaml
---
- 1
- 2
- 3
- 4
- 5
#+end_src
This next block saves the information in the YAML array into the ruby
variable =ya= and then in ruby it multiplies each variable in the =ya=
by 2.
#name: ruby-array
#assign: ya = yaml-array
#+begin_src ruby
ya.map{ |e| e * 2 }
#+end_src
This final block takes the output of the ruby block, and writes it to
cell =0,0= through =0,3= of the table
#name: example-table
#assign: self[0, (1..3)] = ruby-array
| example results |
|-----------------|
| |
| |
| |
** litorgy plays with tables ** litorgy plays with tables
Alright, this should demonstrate both the ability of litorgy to read Alright, this should demonstrate both the ability of litorgy to read
tables into a lisp source code block, and to then convert the results tables into a lisp source code block, and to then convert the results
@ -403,7 +351,7 @@ out...
*** Emacs lisp *** Emacs lisp
#+begin_src emacs-lisp #+begin_src emacs-lisp :results silent
(defun transpose (table) (defun transpose (table)
(apply #'mapcar* #'list table)) (apply #'mapcar* #'list table))
#+end_src #+end_src
@ -412,21 +360,20 @@ out...
| 1 | 2 | 3 | | 1 | 2 | 3 |
| 4 | schulte | 6 | | 4 | schulte | 6 |
#+begin_src emacs-lisp :var table=previous :results replace
(transpose table)
#+end_src
#+begin_src emacs-lisp :var table=sandbox :results replace #+begin_src emacs-lisp :var table=sandbox :results replace
(transpose table) (transpose table)
#+end_src #+end_src
| 1 | 4 |
| 2 | "schulte" |
| 3 | 6 |
#+begin_src emacs-lisp #+begin_src emacs-lisp
'(1 2 3 4 5) '(1 2 3 4 5)
#+end_src #+end_src
| 1 | 2 | 3 | 4 | 5 | | 1 | 2 | 3 | 4 | 5 |
*** Ruby and Python *** Ruby and Python
#+begin_src ruby :var table=sandbox :results replace #+begin_src ruby :var table=sandbox :results replace
@ -473,25 +420,25 @@ tabel
| 1 | 2 | 3 | | 1 | 2 | 3 |
| 4 | "schulte" | 6 | | 4 | "schulte" | 6 |
*** shell *** shell
Now shell commands are converted to tables using =org-table-import= Now shell commands are converted to tables using =org-table-import=
and if these tables are non-trivial (i.e. have multiple elements) then and if these tables are non-trivial (i.e. have multiple elements) then
they are imported as org-mode tables... they are imported as org-mode tables...
#+begin_src sh #+begin_src sh :results replace
ls -l ls -l
#+end_src #+end_src
| "total" | 96 | "" | "" | "" | "" | "" | "" | | "total" | 224 | "" | "" | "" | "" | "" | "" | "" |
| "-rw-r--r--" | 1 | "eschulte" | "eschulte" | 57 | 2009 | 7 | "block" | | "-rw-r--r--" | 1 | "eschulte" | "staff" | 35147 | "Apr" | 15 | 14 | "COPYING" |
| "-rw-r--r--" | 1 | "eschulte" | "eschulte" | 35147 | 2009 | 7 | "COPYING" | | "-rw-r--r--" | 1 | "eschulte" | "staff" | 277 | "Apr" | 15 | 14 | "README.markdown" |
| "drwxr-xr-x" | 4 | "eschulte" | "eschulte" | 4096 | 2009 | 16 | "existing_tools" | | "-rw-r--r--" | 1 | "eschulte" | "staff" | 57 | "Apr" | 15 | 14 | "block" |
| "drwxr-xr-x" | 2 | "eschulte" | "eschulte" | 4096 | 2009 | 10 | "litorgy" | | "drwxr-xr-x" | 6 | "eschulte" | "staff" | 204 | "Apr" | 15 | 14 | "existing_tools" |
| "-rw-r--r--" | 1 | "eschulte" | "eschulte" | 277 | 2009 | 15 | "README.markdown" | | "drwxr-xr-x" | 12 | "eschulte" | "staff" | 408 | "May" | 9 | 18 | "litorgy" |
| "-rw-r--r--" | 1 | "eschulte" | "eschulte" | 41385 | 2009 | 10 | "rorg.org" | | "-rw-r--r--" | 1 | "eschulte" | "staff" | 790 | "May" | 6 | 6 | "litorgy.org" |
| "-rw-r--r--" | 1 | "eschulte" | "staff" | 49904 | "May" | 9 | 18 | "rorg.org" |
| "-rw-r--r--" | 1 | "eschulte" | "staff" | 5469 | "Apr" | 26 | 13 | "test-export.html" |
| "-rw-r--r--" | 1 | "eschulte" | "staff" | 972 | "Apr" | 26 | 13 | "test-export.org" |
** silent evaluation ** silent evaluation
@ -576,7 +523,6 @@ another*3
: 294 : 294
*** mixed languages *** mixed languages
Since all variables are converted into Emacs Lisp it is no problem to Since all variables are converted into Emacs Lisp it is no problem to
reference variables specified in another language. reference variables specified in another language.
@ -595,8 +541,9 @@ reference variables specified in another language.
lisp_var + 4 lisp_var + 4
#+end_src #+end_src
: 20
*** R *** R
not yet implemented
#+srcname: first_r #+srcname: first_r
#+begin_src R :results replace #+begin_src R :results replace
@ -613,7 +560,6 @@ other + 2
: 11 : 11
** (sandbox) selective export ** (sandbox) selective export
For exportation tests and examples see (including exportation of For exportation tests and examples see (including exportation of
@ -623,7 +569,7 @@ inline source code blocks) [[file:test-export.org]]
** (sandbox) source blocks as functions ** (sandbox) source blocks as functions
#+srcname: default #+srcname: default
#+begin_src emacs-lisp #+begin_src emacs-lisp :results silent
5 5
#+end_src #+end_src
@ -634,14 +580,16 @@ inline source code blocks) [[file:test-export.org]]
: 15 : 15
#+begin_src emacs-lisp :var result=triple(n=default, m=98) :results replace #+begin_src emacs-lisp :var result=triple(n=2, m=98) :results replace
result result
#+end_src #+end_src
: 15 : 6
The following just demonstrates the ability to assign variables to
literal values, which was not implemented until recently.
#+begin_src ruby :var num="eric" #+begin_src ruby :var num="eric" :results replace
num+" schulte" num+" schulte"
#+end_src #+end_src