2010-06-11 19:02:42 -04:00
|
|
|
;;; ob-ref.el --- org-babel functions for referencing external data
|
2009-03-23 19:29:31 -04:00
|
|
|
|
2012-03-17 11:31:04 -04:00
|
|
|
;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
|
2009-03-23 19:29:31 -04:00
|
|
|
|
2011-08-24 14:55:11 -04:00
|
|
|
;; Author: Eric Schulte
|
|
|
|
;; Dan Davison
|
2009-03-23 19:29:31 -04:00
|
|
|
;; Keywords: literate programming, reproducible research
|
|
|
|
;; Homepage: http://orgmode.org
|
|
|
|
|
2010-06-25 12:20:39 -04:00
|
|
|
;; This file is part of GNU Emacs.
|
2009-03-23 19:29:31 -04:00
|
|
|
|
2010-06-25 12:20:39 -04:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-03-23 19:29:31 -04:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2010-06-25 12:20:39 -04:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2009-03-23 19:29:31 -04:00
|
|
|
;; 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.
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2009-03-23 19:29:31 -04:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2010-06-25 12:20:39 -04:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
2009-03-23 19:29:31 -04:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Functions for referencing data from the header arguments of a
|
2009-05-24 16:38:01 -04:00
|
|
|
;; org-babel block. The syntax of such a reference should be
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2009-03-23 20:01:11 -04:00
|
|
|
;; #+VAR: variable-name=file:resource-id
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2009-03-23 19:29:31 -04:00
|
|
|
;; - variable-name :: the name of the variable to which the value
|
|
|
|
;; will be assigned
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2009-03-23 19:29:31 -04:00
|
|
|
;; - file :: path to the file containing the resource, or omitted if
|
|
|
|
;; resource is in the current file
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2009-05-09 21:28:08 -04:00
|
|
|
;; - resource-id :: the id or name of the resource
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2009-03-23 19:29:31 -04:00
|
|
|
;; So an example of a simple src block referencing table data in the
|
|
|
|
;; same file would be
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2009-05-09 21:28:08 -04:00
|
|
|
;; #+TBLNAME: sandbox
|
2010-07-14 20:59:43 -04:00
|
|
|
;; | 1 | 2 | 3 |
|
2009-05-24 16:38:01 -04:00
|
|
|
;; | 4 | org-babel | 6 |
|
2009-05-09 21:28:08 -04:00
|
|
|
;;
|
|
|
|
;; #+begin_src emacs-lisp :var table=sandbox
|
2010-06-25 12:20:39 -04:00
|
|
|
;; (message table)
|
2009-05-09 21:28:08 -04:00
|
|
|
;; #+end_src
|
2009-03-23 19:29:31 -04:00
|
|
|
|
|
|
|
;;; Code:
|
2010-06-11 19:02:42 -04:00
|
|
|
(require 'ob)
|
2010-06-12 21:32:23 -04:00
|
|
|
(eval-when-compile
|
|
|
|
(require 'cl))
|
2009-03-23 19:29:31 -04:00
|
|
|
|
2010-06-23 14:24:33 -04:00
|
|
|
(declare-function org-remove-if-not "org" (predicate seq))
|
|
|
|
(declare-function org-at-table-p "org" (&optional table-type))
|
2010-07-14 20:59:43 -04:00
|
|
|
(declare-function org-count "org" (CL-ITEM CL-SEQ))
|
2011-03-01 01:24:14 -05:00
|
|
|
(declare-function org-at-item-p "org-list" ())
|
2011-06-29 19:30:34 -04:00
|
|
|
(declare-function org-narrow-to-subtree "org" ())
|
2011-07-04 05:05:07 -04:00
|
|
|
(declare-function org-id-find-id-in-file "org-id" (id file &optional markerp))
|
|
|
|
(declare-function org-show-context "org" (&optional key))
|
2012-03-27 20:05:36 -04:00
|
|
|
(declare-function org-pop-to-buffer-same-window
|
2011-07-18 13:25:10 -04:00
|
|
|
"org-compat" (&optional buffer-or-name norecord label))
|
2010-06-23 14:24:33 -04:00
|
|
|
|
2009-10-23 00:56:05 -04:00
|
|
|
(defvar org-babel-ref-split-regexp
|
|
|
|
"[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*")
|
|
|
|
|
2011-12-12 12:04:15 -05:00
|
|
|
(defvar org-babel-update-intermediate nil
|
2011-11-20 15:19:01 -05:00
|
|
|
"Update the in-buffer results of code blocks executed to resolve references.")
|
|
|
|
|
2010-10-16 14:14:20 -04:00
|
|
|
(defun org-babel-ref-parse (assignment)
|
2010-07-13 19:20:08 -04:00
|
|
|
"Parse a variable ASSIGNMENT in a header argument.
|
|
|
|
If the right hand side of the assignment has a literal value
|
|
|
|
return that value, otherwise interpret as a reference to an
|
2011-12-06 13:20:21 -05:00
|
|
|
external resource and find its value using
|
2010-10-16 14:14:20 -04:00
|
|
|
`org-babel-ref-resolve'. 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 emacs-lisp representation of the value of
|
|
|
|
the variable."
|
|
|
|
(when (string-match org-babel-ref-split-regexp assignment)
|
|
|
|
(let ((var (match-string 1 assignment))
|
|
|
|
(ref (match-string 2 assignment)))
|
|
|
|
(cons (intern var)
|
2010-11-07 09:39:05 -05:00
|
|
|
(let ((out (org-babel-read ref)))
|
|
|
|
(if (equal out ref)
|
2011-01-11 22:40:13 -05:00
|
|
|
(if (string-match "^\".*\"$" ref)
|
2010-11-07 09:39:05 -05:00
|
|
|
(read ref)
|
|
|
|
(org-babel-ref-resolve ref))
|
|
|
|
out))))))
|
2009-05-09 20:44:52 -04:00
|
|
|
|
2011-06-28 16:02:01 -04:00
|
|
|
(defun org-babel-ref-goto-headline-id (id)
|
2011-06-28 17:20:10 -04:00
|
|
|
(goto-char (point-min))
|
2011-06-28 16:02:01 -04:00
|
|
|
(let ((rx (regexp-quote id)))
|
|
|
|
(or (re-search-forward
|
|
|
|
(concat "^[ \t]*:CUSTOM_ID:[ \t]+" rx "[ \t]*$") nil t)
|
2011-07-01 15:09:34 -04:00
|
|
|
(let* ((file (org-id-find-id-file id))
|
|
|
|
(m (when file (org-id-find-id-in-file id file 'marker))))
|
|
|
|
(when (and file m)
|
|
|
|
(message "file:%S" file)
|
2011-07-18 13:25:10 -04:00
|
|
|
(org-pop-to-buffer-same-window (marker-buffer m))
|
2011-07-01 15:09:34 -04:00
|
|
|
(goto-char m)
|
|
|
|
(move-marker m nil)
|
|
|
|
(org-show-context)
|
|
|
|
t)))))
|
2011-06-28 16:02:01 -04:00
|
|
|
|
|
|
|
(defun org-babel-ref-headline-body ()
|
|
|
|
(save-restriction
|
|
|
|
(org-narrow-to-subtree)
|
|
|
|
(buffer-substring
|
|
|
|
(save-excursion (goto-char (point-min))
|
|
|
|
(forward-line 1)
|
|
|
|
(when (looking-at "[ \t]*:PROPERTIES:")
|
|
|
|
(re-search-forward ":END:" nil)
|
|
|
|
(forward-char))
|
|
|
|
(point))
|
|
|
|
(point-max))))
|
|
|
|
|
2010-06-12 21:49:02 -04:00
|
|
|
(defvar org-babel-library-of-babel)
|
2010-10-16 14:14:20 -04:00
|
|
|
(defun org-babel-ref-resolve (ref)
|
2010-06-11 20:12:15 -04:00
|
|
|
"Resolve the reference REF and return its value."
|
2011-01-31 14:31:17 -05:00
|
|
|
(save-window-excursion
|
2009-03-23 20:01:11 -04:00
|
|
|
(save-excursion
|
2009-05-10 22:47:22 -04:00
|
|
|
(let ((case-fold-search t)
|
2010-11-07 09:39:05 -05:00
|
|
|
type args new-refere new-header-args new-referent result
|
2011-06-28 14:24:26 -04:00
|
|
|
lob-info split-file split-ref index index-row index-col id)
|
2010-07-14 20:59:43 -04:00
|
|
|
;; if ref is indexed grab the indices -- beware nested indices
|
2010-11-07 09:39:05 -05:00
|
|
|
(when (and (string-match "\\[\\([^\\[]+\\)\\]$" ref)
|
2010-04-24 15:26:34 -04:00
|
|
|
(let ((str (substring ref 0 (match-beginning 0))))
|
2010-07-14 20:59:43 -04:00
|
|
|
(= (org-count ?( str) (org-count ?) str))))
|
2009-11-13 22:26:44 -05:00
|
|
|
(setq index (match-string 1 ref))
|
|
|
|
(setq ref (substring ref 0 (match-beginning 0))))
|
2009-05-09 20:44:52 -04:00
|
|
|
;; assign any arguments to pass to source block
|
2010-11-07 09:39:05 -05:00
|
|
|
(when (string-match
|
|
|
|
"^\\(.+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)\(\\(.*\\)\)$" ref)
|
|
|
|
(setq new-refere (match-string 1 ref))
|
|
|
|
(setq new-header-args (match-string 3 ref))
|
|
|
|
(setq new-referent (match-string 5 ref))
|
2009-05-24 13:35:13 -04:00
|
|
|
(when (> (length new-refere) 0)
|
2010-11-07 09:39:05 -05:00
|
|
|
(when (> (length new-referent) 0)
|
|
|
|
(setq args (mapcar (lambda (ref) (cons :var ref))
|
|
|
|
(org-babel-ref-split-args new-referent))))
|
|
|
|
(when (> (length new-header-args) 0)
|
2011-06-28 14:24:26 -04:00
|
|
|
(setq args (append (org-babel-parse-header-arguments
|
|
|
|
new-header-args) args)))
|
2009-05-24 13:35:13 -04:00
|
|
|
(setq ref new-refere)))
|
2009-10-23 00:56:05 -04:00
|
|
|
(when (string-match "^\\(.+\\):\\(.+\\)$" ref)
|
|
|
|
(setq split-file (match-string 1 ref))
|
|
|
|
(setq split-ref (match-string 2 ref))
|
|
|
|
(find-file split-file) (setq ref split-ref))
|
2010-06-07 20:38:35 -04:00
|
|
|
(save-restriction
|
|
|
|
(widen)
|
|
|
|
(goto-char (point-min))
|
2011-11-15 22:13:41 -05:00
|
|
|
(if (let ((src-rx (org-babel-named-src-block-regexp-for-name ref))
|
|
|
|
(res-rx (org-babel-named-data-regexp-for-name ref)))
|
2010-06-07 20:38:35 -04:00
|
|
|
;; goto ref in the current buffer
|
2011-11-15 22:13:41 -05:00
|
|
|
(or
|
|
|
|
;; check for code blocks
|
|
|
|
(re-search-forward src-rx nil t)
|
|
|
|
;; check for named data
|
|
|
|
(re-search-forward res-rx nil t)
|
|
|
|
;; check for local or global headlines by id
|
|
|
|
(setq id (org-babel-ref-goto-headline-id ref))
|
|
|
|
;; check the Library of Babel
|
|
|
|
(setq lob-info (cdr (assoc (intern ref)
|
|
|
|
org-babel-library-of-babel)))))
|
2011-06-28 16:02:01 -04:00
|
|
|
(unless (or lob-info id) (goto-char (match-beginning 0)))
|
2010-06-07 20:38:35 -04:00
|
|
|
;; ;; TODO: allow searching for names in other buffers
|
|
|
|
;; (setq id-loc (org-id-find ref 'marker)
|
|
|
|
;; buffer (marker-buffer id-loc)
|
|
|
|
;; loc (marker-position id-loc))
|
|
|
|
;; (move-marker id-loc nil)
|
2010-07-06 13:30:58 -04:00
|
|
|
(error "reference '%s' not found in this buffer" ref))
|
2011-06-28 14:24:26 -04:00
|
|
|
(cond
|
|
|
|
(lob-info (setq type 'lob))
|
|
|
|
(id (setq type 'id))
|
Standardized code block keywords
Nick Dokos <nicholas.dokos@hp.com> writes:
> Eric Schulte <schulte.eric@gmail.com> wrote:
>
>> The attached updated patch fixes a bug in the original.
>>
>
> Minor problem in applying:
>
> ,----
> | $ git apply ~/Mail/inbox/724
> | /home/nick/Mail/inbox/724:671: trailing whitespace.
> | #+name:
> | /home/nick/Mail/inbox/724:599: new blank line at EOF.
> | +
> | warning: 2 lines add whitespace errors.
> `----
The attached version fixes these issues, Thanks -- Eric
>From 0e43d59ee8d46a63f86780a502de726271bc39de Mon Sep 17 00:00:00 2001
From: Eric Schulte <schulte.eric@gmail.com>
Date: Fri, 28 Oct 2011 10:44:21 -0600
Subject: [PATCH] removing code block, results and call-line synonyms -- BREAKING CHANGE
Following a round of on-list discussion many code block synonyms have
been removed, moving forward the following syntax is valid.
- call lines are specified with #+call:
- code blocks are named with #+name:
- results are named with #+name:, however results generated by a code
block may still be labeled with #+results:, and tables named with
#+tblname: will be considered to be named results
The following function may be used to update an existing Org-mode
buffer to the new syntax.
(defun update-org-buffer ()
"Update an Org-mode buffer to the new data, code block and call line syntax."
(interactive)
(save-excursion
(flet ((to-re (lst) (concat "^[ \t]*#\\+" (regexp-opt lst t)
"\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:[ \t]*"))
(update (re new)
(goto-char (point-min))
(while (re-search-forward re nil t)
(replace-match new nil nil nil 1))))
(let ((old-re (to-re '("RESULTS" "DATA" "SRCNAME" "SOURCE")))
(lob-re (to-re '("LOB")))
(case-fold-search t))
(update old-re "name")
(update lob-re "call")))))
Note: If an old version of Org-mode (e.g., the one shipped with Emacs)
is installed on your system many of the important variables will
be pre-defined with a defvar and *will not* have their values
automatically updated, these include the following.
- org-babel-data-names
- org-babel-result-regexp
- org-babel-src-block-regexp
- org-babel-src-name-regexp
- org-babel-src-name-w-name-regexp
It may be necessary to either remove the source code of older
versions of Org-mode, or to explicitly evaluate the ob.el file.
* lisp/ob-exp.el (org-exp-res/src-name-cleanup): Updated
Documentation.
* lisp/ob-lob.el (org-babel-block-lob-one-liner-regexp): Updated
regular expression.
(org-babel-inline-lob-one-liner-regexp): Updated regular expression.
* lisp/ob-ref.el (org-babel-ref-resolve): Notice when something that
looks like a data results may actually be a code block.
* lisp/ob-table.el: Updated documentation.
* lisp/ob.el (org-babel-src-name-regexp): Simplified regexp.
(org-babel-get-src-block-info): Updated match strings.
(org-babel-data-names): Simplified acceptable names.
(org-babel-find-named-block): Indentation.
(org-babel-find-named-result): Updated to not return a code block as
a result.
* lisp/org.el (org-fontify-meta-lines-and-blocks-1): Removing
references to old syntactic elements.
(org-additional-option-like-keywords): Removing references to old
syntactic elements.
* contrib/babel/library-of-babel.org: Updated to make use of the new
syntax.
* testing/examples/babel-dangerous.org: Updated to make use of the new
syntax.
* testing/examples/babel.org: Updated to make use of the new syntax.
* testing/examples/ob-awk-test.org: Updated to make use of the new
syntax.
* testing/examples/ob-fortran-test.org: Updated to make use of the new
syntax.
* testing/lisp/test-ob.el: Removed two bad tests which tested the
literal values of old regular expressions rather than their
behavior.
2011-10-28 19:52:48 -04:00
|
|
|
((and (looking-at org-babel-src-name-regexp)
|
2011-11-15 13:18:26 -05:00
|
|
|
(save-excursion
|
|
|
|
(forward-line 1)
|
|
|
|
(or (looking-at org-babel-src-block-regexp)
|
|
|
|
(looking-at org-babel-multi-line-header-regexp))))
|
Standardized code block keywords
Nick Dokos <nicholas.dokos@hp.com> writes:
> Eric Schulte <schulte.eric@gmail.com> wrote:
>
>> The attached updated patch fixes a bug in the original.
>>
>
> Minor problem in applying:
>
> ,----
> | $ git apply ~/Mail/inbox/724
> | /home/nick/Mail/inbox/724:671: trailing whitespace.
> | #+name:
> | /home/nick/Mail/inbox/724:599: new blank line at EOF.
> | +
> | warning: 2 lines add whitespace errors.
> `----
The attached version fixes these issues, Thanks -- Eric
>From 0e43d59ee8d46a63f86780a502de726271bc39de Mon Sep 17 00:00:00 2001
From: Eric Schulte <schulte.eric@gmail.com>
Date: Fri, 28 Oct 2011 10:44:21 -0600
Subject: [PATCH] removing code block, results and call-line synonyms -- BREAKING CHANGE
Following a round of on-list discussion many code block synonyms have
been removed, moving forward the following syntax is valid.
- call lines are specified with #+call:
- code blocks are named with #+name:
- results are named with #+name:, however results generated by a code
block may still be labeled with #+results:, and tables named with
#+tblname: will be considered to be named results
The following function may be used to update an existing Org-mode
buffer to the new syntax.
(defun update-org-buffer ()
"Update an Org-mode buffer to the new data, code block and call line syntax."
(interactive)
(save-excursion
(flet ((to-re (lst) (concat "^[ \t]*#\\+" (regexp-opt lst t)
"\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:[ \t]*"))
(update (re new)
(goto-char (point-min))
(while (re-search-forward re nil t)
(replace-match new nil nil nil 1))))
(let ((old-re (to-re '("RESULTS" "DATA" "SRCNAME" "SOURCE")))
(lob-re (to-re '("LOB")))
(case-fold-search t))
(update old-re "name")
(update lob-re "call")))))
Note: If an old version of Org-mode (e.g., the one shipped with Emacs)
is installed on your system many of the important variables will
be pre-defined with a defvar and *will not* have their values
automatically updated, these include the following.
- org-babel-data-names
- org-babel-result-regexp
- org-babel-src-block-regexp
- org-babel-src-name-regexp
- org-babel-src-name-w-name-regexp
It may be necessary to either remove the source code of older
versions of Org-mode, or to explicitly evaluate the ob.el file.
* lisp/ob-exp.el (org-exp-res/src-name-cleanup): Updated
Documentation.
* lisp/ob-lob.el (org-babel-block-lob-one-liner-regexp): Updated
regular expression.
(org-babel-inline-lob-one-liner-regexp): Updated regular expression.
* lisp/ob-ref.el (org-babel-ref-resolve): Notice when something that
looks like a data results may actually be a code block.
* lisp/ob-table.el: Updated documentation.
* lisp/ob.el (org-babel-src-name-regexp): Simplified regexp.
(org-babel-get-src-block-info): Updated match strings.
(org-babel-data-names): Simplified acceptable names.
(org-babel-find-named-block): Indentation.
(org-babel-find-named-result): Updated to not return a code block as
a result.
* lisp/org.el (org-fontify-meta-lines-and-blocks-1): Removing
references to old syntactic elements.
(org-additional-option-like-keywords): Removing references to old
syntactic elements.
* contrib/babel/library-of-babel.org: Updated to make use of the new
syntax.
* testing/examples/babel-dangerous.org: Updated to make use of the new
syntax.
* testing/examples/babel.org: Updated to make use of the new syntax.
* testing/examples/ob-awk-test.org: Updated to make use of the new
syntax.
* testing/examples/ob-fortran-test.org: Updated to make use of the new
syntax.
* testing/lisp/test-ob.el: Removed two bad tests which tested the
literal values of old regular expressions rather than their
behavior.
2011-10-28 19:52:48 -04:00
|
|
|
(setq type 'source-block))
|
2011-06-28 14:24:26 -04:00
|
|
|
(t (while (not (setq type (org-babel-ref-at-ref-p)))
|
|
|
|
(forward-line 1)
|
|
|
|
(beginning-of-line)
|
|
|
|
(if (or (= (point) (point-min)) (= (point) (point-max)))
|
|
|
|
(error "reference not found")))))
|
2010-10-16 15:21:47 -04:00
|
|
|
(let ((params (append args '((:results . "silent")))))
|
|
|
|
(setq result
|
|
|
|
(case type
|
2011-06-28 14:24:26 -04:00
|
|
|
(results-line (org-babel-read-result))
|
2011-06-28 16:02:01 -04:00
|
|
|
(table (org-babel-read-table))
|
|
|
|
(list (org-babel-read-list))
|
|
|
|
(file (org-babel-read-link))
|
2011-11-20 15:19:01 -05:00
|
|
|
(source-block (org-babel-execute-src-block
|
|
|
|
nil nil (if org-babel-update-intermediate
|
|
|
|
nil params)))
|
2011-06-28 16:02:01 -04:00
|
|
|
(lob (org-babel-execute-src-block
|
|
|
|
nil lob-info params))
|
|
|
|
(id (org-babel-ref-headline-body)))))
|
2010-06-07 20:38:35 -04:00
|
|
|
(if (symbolp result)
|
|
|
|
(format "%S" result)
|
|
|
|
(if (and index (listp result))
|
|
|
|
(org-babel-ref-index-list index result)
|
2011-01-31 14:31:17 -05:00
|
|
|
result)))))))
|
2009-11-13 22:26:44 -05:00
|
|
|
|
|
|
|
(defun org-babel-ref-index-list (index lis)
|
2010-07-13 19:20:08 -04:00
|
|
|
"Return the subset of LIS indexed by INDEX.
|
2010-07-14 14:01:57 -04:00
|
|
|
|
|
|
|
Indices are 0 based and negative indices count from the end of
|
|
|
|
LIS, so 0 references the first element of LIS and -1 references
|
|
|
|
the last. If INDEX is separated by \",\"s then each \"portion\"
|
|
|
|
is assumed to index into the next deepest nesting or dimension.
|
|
|
|
|
|
|
|
A valid \"portion\" can consist of either an integer index, two
|
|
|
|
integers separated by a \":\" in which case the entire range is
|
|
|
|
returned, or an empty string or \"*\" both of which are
|
|
|
|
interpreted to mean the entire range and as such are equivalent
|
|
|
|
to \"0:-1\"."
|
|
|
|
(if (and (> (length index) 0) (string-match "^\\([^,]*\\),?" index))
|
|
|
|
(let ((ind-re "\\(\\([-[:digit:]]+\\):\\([-[:digit:]]+\\)\\|\*\\)")
|
|
|
|
(length (length lis))
|
2009-11-13 22:26:44 -05:00
|
|
|
(portion (match-string 1 index))
|
|
|
|
(remainder (substring index (match-end 0))))
|
2010-04-21 11:35:13 -04:00
|
|
|
(flet ((wrap (num) (if (< num 0) (+ length num) num))
|
2010-07-14 14:01:57 -04:00
|
|
|
(open (ls) (if (and (listp ls) (= (length ls) 1)) (car ls) ls)))
|
2010-04-21 11:35:13 -04:00
|
|
|
(open
|
|
|
|
(mapcar
|
2011-02-27 11:05:15 -05:00
|
|
|
(lambda (sub-lis)
|
|
|
|
(if (listp sub-lis)
|
|
|
|
(org-babel-ref-index-list remainder sub-lis)
|
|
|
|
sub-lis))
|
2010-07-14 14:01:57 -04:00
|
|
|
(if (or (= 0 (length portion)) (string-match ind-re portion))
|
|
|
|
(mapcar
|
|
|
|
(lambda (n) (nth n lis))
|
2010-10-18 08:23:38 -04:00
|
|
|
(apply 'org-number-sequence
|
2010-07-14 14:01:57 -04:00
|
|
|
(if (and (> (length portion) 0) (match-string 2 portion))
|
|
|
|
(list
|
|
|
|
(wrap (string-to-number (match-string 2 portion)))
|
|
|
|
(wrap (string-to-number (match-string 3 portion))))
|
|
|
|
(list (wrap 0) (wrap -1)))))
|
2010-04-21 11:35:13 -04:00
|
|
|
(list (nth (wrap (string-to-number portion)) lis)))))))
|
2009-11-13 22:26:44 -05:00
|
|
|
lis))
|
2009-03-23 20:01:11 -04:00
|
|
|
|
2009-07-22 18:04:19 -04:00
|
|
|
(defun org-babel-ref-split-args (arg-string)
|
|
|
|
"Split ARG-STRING into top-level arguments of balanced parenthesis."
|
2011-12-12 19:19:31 -05:00
|
|
|
(mapcar #'org-babel-trim (org-babel-balanced-split arg-string 44)))
|
2009-07-22 18:04:19 -04:00
|
|
|
|
2010-06-23 14:24:33 -04:00
|
|
|
(defvar org-bracket-link-regexp)
|
2009-05-24 16:38:01 -04:00
|
|
|
(defun org-babel-ref-at-ref-p ()
|
2010-07-13 19:20:08 -04:00
|
|
|
"Return the type of reference located at point.
|
|
|
|
Return nil if none of the supported reference types are found.
|
|
|
|
Supported reference types are tables and source blocks."
|
2009-04-02 20:58:04 -04:00
|
|
|
(cond ((org-at-table-p) 'table)
|
2011-02-22 23:12:13 -05:00
|
|
|
((org-at-item-p) 'list)
|
2010-06-10 17:36:08 -04:00
|
|
|
((looking-at "^[ \t]*#\\+BEGIN_SRC") 'source-block)
|
2010-02-09 21:38:50 -05:00
|
|
|
((looking-at org-bracket-link-regexp) 'file)
|
2009-11-20 12:40:50 -05:00
|
|
|
((looking-at org-babel-result-regexp) 'results-line)))
|
2009-06-13 20:07:16 -04:00
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
(provide 'ob-ref)
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2011-08-15 14:04:38 -04:00
|
|
|
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
;;; ob-ref.el ends here
|