Browse Source

org-babel: #+srcname: can now be called #+source: or #+function:

  source name regular expression has been extracted into a variable
  and multiple aliases are allowed.
Eric Schulte 15 years ago
parent
commit
91661d2f3b

+ 6 - 6
contrib/babel/lisp/org-babel-exp.el

@@ -37,14 +37,14 @@
 
 
 (defvar org-babel-function-def-export-keyword "function"
 (defvar org-babel-function-def-export-keyword "function"
   "When exporting a source block function, this keyword will
   "When exporting a source block function, this keyword will
-appear in the exported version in the place of #+srcname:. A
-source block is considered to be a source block function if the
-srcname is present and is followed by a parenthesised argument
-list. The parentheses may be empty or contain whitespace. An
-example is the following which generates n random
+appear in the exported version in the place of source name
+line. A source block is considered to be a source block function
+if the source name is present and is followed by a parenthesized
+argument list. The parentheses may be empty or contain
+whitespace. An example is the following which generates n random
 (uniform) numbers.
 (uniform) numbers.
 
 
-#+srcname: rand(n)
+#+source: rand(n)
 #+begin_src R
 #+begin_src R
   runif(n)
   runif(n)
 #+end_src
 #+end_src

+ 1 - 1
contrib/babel/lisp/org-babel-ref.el

@@ -120,7 +120,7 @@ return nil."
       (goto-char (point-min))
       (goto-char (point-min))
       (if (let ((result_regexp (concat "^#\\+\\(TBLNAME\\|RESNAME\\|RESULT\\):[ \t]*"
       (if (let ((result_regexp (concat "^#\\+\\(TBLNAME\\|RESNAME\\|RESULT\\):[ \t]*"
                                        (regexp-quote ref) "[ \t]*$"))
                                        (regexp-quote ref) "[ \t]*$"))
-                (regexp (concat "^#\\+SRCNAME:[ \t]*"
+                (regexp (concat org-babel-source-name-regexp
                                 (regexp-quote ref) "\\(\(.*\)\\)?" "[ \t]*$")))
                                 (regexp-quote ref) "\\(\(.*\)\\)?" "[ \t]*$")))
             ;; goto ref in the current buffer
             ;; goto ref in the current buffer
             (or (and (not args)
             (or (and (not args)

+ 15 - 10
contrib/babel/lisp/org-babel.el

@@ -95,10 +95,14 @@ then run `org-babel-pop-to-session'."
 
 
 (defvar org-babel-result-regexp
 (defvar org-babel-result-regexp
   "#\\+res\\(ults\\|name\\)\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:"
   "#\\+res\\(ults\\|name\\)\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:"
-  "Regular expressions used to match result lines.  If the
+  "Regular expression used to match result lines.  If the
 results are associated with a hash key then the hash will be
 results are associated with a hash key then the hash will be
 saved in the second match data.")
 saved in the second match data.")
 
 
+(defvar org-babel-source-name-regexp
+  "#\\+\\(srcname\\|source\\|function\\):[ \t]*"
+  "Regular expression used to match a source name line.")
+
 (defvar org-babel-min-lines-for-block-output 10
 (defvar org-babel-min-lines-for-block-output 10
   "If number of lines of output is equal to or exceeds this
   "If number of lines of output is equal to or exceeds this
 value, the output is placed in a #+begin_example...#+end_example
 value, the output is placed in a #+begin_example...#+end_example
@@ -116,7 +120,7 @@ can not be resolved.")
 
 
 (defun org-babel-named-src-block-regexp-for-name (name)
 (defun org-babel-named-src-block-regexp-for-name (name)
   "Regexp used to match named src block."
   "Regexp used to match named src block."
-  (concat "#\\+srcname:[ \t]*" (regexp-quote name) "[ \t\n]*"
+  (concat org-babel-source-name-regexp (regexp-quote name) "[ \t\n]*"
 	  (substring org-babel-src-block-regexp 1)))
 	  (substring org-babel-src-block-regexp 1)))
 
 
 (defun org-babel-set-interpreters (var value)
 (defun org-babel-set-interpreters (var value)
@@ -316,15 +320,16 @@ added to the header-arguments-alist."
 	  (goto-char head)
 	  (goto-char head)
 	  (setq info (org-babel-parse-src-block-match))
 	  (setq info (org-babel-parse-src-block-match))
 	  (forward-line -1)
 	  (forward-line -1)
-	  (when (looking-at "#\\+srcname:[ \t]*\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)")
-	    (setq info (append info (list (org-babel-clean-text-properties (match-string 1)))))
+	  (when (looking-at (concat org-babel-source-name-regexp
+                                    "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
+	    (setq info (append info (list (org-babel-clean-text-properties (match-string 2)))))
 	    ;; Note that e.g. "name()" and "name( )" result in ((:var . "")).
 	    ;; Note that e.g. "name()" and "name( )" result in ((:var . "")).
 	    ;; We maintain that behaviour, and the resulting non-nil sixth
 	    ;; We maintain that behaviour, and the resulting non-nil sixth
 	    ;; element is relied upon in org-babel-exp-code to detect a functional-style
 	    ;; element is relied upon in org-babel-exp-code to detect a functional-style
 	    ;; block in those cases. However, "name" without any
 	    ;; block in those cases. However, "name" without any
 	    ;; parentheses would result in the same thing, so we
 	    ;; parentheses would result in the same thing, so we
 	    ;; explicitly avoid that.
 	    ;; explicitly avoid that.
-	    (if (setq args (match-string 3))
+	    (if (setq args (match-string 4))
 		(setq info (append info (list (mapcar (lambda (ref) (cons :var ref))
 		(setq info (append info (list (mapcar (lambda (ref) (cons :var ref))
 						      (org-babel-ref-split-args args))))))
 						      (org-babel-ref-split-args args))))))
 	    (unless header-vars-only
 	    (unless header-vars-only
@@ -479,9 +484,9 @@ block.  Specifically at the beginning of the #+BEGIN_SRC line.
 If the point is not on a source block then return nil."
 If the point is not on a source block then return nil."
   (let ((initial (point)) top bottom)
   (let ((initial (point)) top bottom)
     (or
     (or
-     (save-excursion ;; on a #+srcname: line
+     (save-excursion ;; on a source name line
        (beginning-of-line 1)
        (beginning-of-line 1)
-       (and (looking-at "#\\+srcname") (forward-line 1)
+       (and (looking-at org-babel-source-name-regexp) (forward-line 1)
             (looking-at org-babel-src-block-regexp)
             (looking-at org-babel-src-block-regexp)
             (point)))
             (point)))
      (save-excursion ;; on a #+begin_src line
      (save-excursion ;; on a #+begin_src line
@@ -508,9 +513,9 @@ If the point is not on a source block then return nil."
 
 
 (defun org-babel-find-named-block (name)
 (defun org-babel-find-named-block (name)
   "Find a named source-code block.
   "Find a named source-code block.
-Return the location of the source block identified by
-#+srcname NAME, or nil if no such block exists. Set match data
-according to org-babel-named-src-block-regexp."
+Return the location of the source block identified by source
+NAME, or nil if no such block exists. Set match data according to
+org-babel-named-src-block-regexp."
   (save-excursion
   (save-excursion
     (let ((case-fold-search t)
     (let ((case-fold-search t)
 	  (regexp (org-babel-named-src-block-regexp-for-name name)) msg)
 	  (regexp (org-babel-named-src-block-regexp-for-name name)) msg)