emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Org babel does not work properly with included files
@ 2011-05-02  0:44 Robert Goldman
  2011-05-02  1:33 ` Nick Dokos
  2011-05-02  2:47 ` Robert Goldman
  0 siblings, 2 replies; 12+ messages in thread
From: Robert Goldman @ 2011-05-02  0:44 UTC (permalink / raw)
  To: Org Mode

[-- Attachment #1: Type: text/plain, Size: 7623 bytes --]


Here's the problem:  when org-babel goes to look for parameters when executing a source block, it looks for them in the parent source file, and not in the parent source file with the included materials.  Here is the function that goes awry:

(defmacro org-babel-exp-in-export-file (lang &rest body)
  (declare (indent 1))
  `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
	  (heading (nth 4 (ignore-errors (org-heading-components))))
	  (link (when org-current-export-file
		  (org-make-link-string
		   (if heading
		       (concat org-current-export-file "::" heading)
		     org-current-export-file))))
	  (export-buffer (current-buffer)) results)
     (when link
       ;; resolve parameters in the original file so that
       ;; headline and file-wide parameters are included, attempt
       ;; to go to the same heading in the original file
       (set-buffer (get-file-buffer org-current-export-file))
       (save-restriction
	 (condition-case nil
**	     (org-open-link-from-string link)
	   (error (when heading
		    (goto-char (point-min))
		    (re-search-forward (regexp-quote heading) nil t))))
	 (setq results ,@body))
       (set-buffer export-buffer)
       results)))

The line that blows up, AFAICT is the one marked with an asterisk.  It goes to
look in the file *without* incorporated materials, instead of the file *with*
incorporated materials.

I believe that what needs to happen is that the link must be searched for in the
export-buffer, rather than in org-current-export-file.

Unfortunately, I don't understand the code well enough to figure out how to make
this modification.  I believe that the first trick is to NOT concatenate
org-current-export-file onto the link string.  The second would be to change
org-open-link-from-string to something that looks for the link match in the
current buffer.

To see the bug, it should be sufficient to install the attached to .org files
into a temporary directory and then try to export foo.org to latex.  You may
need to tweak your configuration so that evaluation is enabled for sh scripts
(require 'ob-sh)

The error messages one gets are deeply cryptic.  To debug this I modified
org-link-search as follows.  I added a DEBUG call when the link is unfound.
This is, of course, not generally good practice, but you will want this debug
call in place when you test the export per my instructions above.  You will get
a debug window with a backtrace that should make things relatively clear.  Here
is the modified org-link-search --- you can just evaluate the definition before
testing the export.  To see what I've changed, just search for debug...

Cheers,
r

(defun org-link-search (s &optional type avoid-pos)
  "Search for a link search option.
If S is surrounded by forward slashes, it is interpreted as a
regular expression.  In org-mode files, this will create an `org-occur'
sparse tree.  In ordinary files, `occur' will be used to list matches.
If the current buffer is in `dired-mode', grep will be used to search
in all files.  If AVOID-POS is given, ignore matches near that position."
  (let ((case-fold-search t)
	(s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
	(markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
					    (append '(("") (" ") ("\t") ("\n"))
						    org-emphasis-alist)
					    "\\|") "\\)"))
	(pos (point))
	(pre nil) (post nil)
	words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
    (cond
     ;; First check if there are any special search functions
     ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
     ;; Now try the builtin stuff
     ((and (equal (string-to-char s0) ?#)
	   (> (length s0) 1)
	   (save-excursion
	     (goto-char (point-min))
	     (and
	      (re-search-forward
	       (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
	      (setq type 'dedicated
		    pos (match-beginning 0))))
	   ;; There is an exact target for this
	   (goto-char pos)
	   (org-back-to-heading t)))
     ((save-excursion
	(goto-char (point-min))
	(and
	 (re-search-forward
	  (concat "<<" (regexp-quote s0) ">>") nil t)
	 (setq type 'dedicated
	       pos (match-beginning 0))))
      ;; There is an exact target for this
      (goto-char pos))
     ((and (string-match "^(\\(.*\\))$" s0)
	   (save-excursion
	     (goto-char (point-min))
	     (and
	      (re-search-forward
	       (concat "[^[]" (regexp-quote
			       (format org-coderef-label-format
				       (match-string 1 s0))))
	       nil t)
	      (setq type 'dedicated
		    pos (1+ (match-beginning 0))))))
      ;; There is a coderef target for this
      (goto-char pos))
     ((string-match "^/\\(.*\\)/$" s)
      ;; A regular expression
      (cond
       ((org-mode-p)
	(org-occur (match-string 1 s)))
       ;;((eq major-mode 'dired-mode)
       ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
       (t (org-do-occur (match-string 1 s)))))
     ((and (org-mode-p) org-link-search-must-match-exact-headline)
      (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
      (goto-char (point-min))
      (cond
       ((let (case-fold-search)
	  (re-search-forward (format org-complex-heading-regexp-format
				     (regexp-quote s))
			     nil t))
	;; OK, found a match
	(setq type 'dedicated)
	(goto-char (match-beginning 0)))
       ((and (not org-link-search-inhibit-query)
	     (eq org-link-search-must-match-exact-headline 'query-to-create)
	     (debug nil (format "No match for target %s" s))
	     (y-or-n-p "No match - create this as a new heading? "))
	(goto-char (point-max))
	(or (bolp) (newline))
	(insert "* " s "\n")
	(beginning-of-line 0))
       (t
	(goto-char pos)
	(error "No match"))))
     (t
      ;; A normal search string
      (when (equal (string-to-char s) ?*)
	;; Anchor on headlines, post may include tags.
	(setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
	      post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
	      s (substring s 1)))
      (remove-text-properties
       0 (length s)
       '(face nil mouse-face nil keymap nil fontified nil) s)
      ;; Make a series of regular expressions to find a match
      (setq words (org-split-string s "[ \n\r\t]+")

	    re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
	    re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
			"\\)" markers)
	    re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
	    re2a (concat "[ \t\r\n]" re2a_)
	    re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
	    re4 (concat "[^a-zA-Z_]" re4_)

	    re1 (concat pre re2 post)
	    re3 (concat pre (if pre re4_ re4) post)
	    re5 (concat pre ".*" re4)
	    re2 (concat pre re2)
	    re2a (concat pre (if pre re2a_ re2a))
	    re4 (concat pre (if pre re4_ re4))
	    reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
			  "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
			  re5 "\\)"
			  ))
      (cond
       ((eq type 'org-occur) (org-occur reall))
       ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
       (t (goto-char (point-min))
	  (setq type 'fuzzy)
	  (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
		  (org-search-not-self 1 re1 nil t)
		  (org-search-not-self 1 re2 nil t)
		  (org-search-not-self 1 re2a nil t)
		  (org-search-not-self 1 re3 nil t)
		  (org-search-not-self 1 re4 nil t)
		  (org-search-not-self 1 re5 nil t)
		  )
	      (goto-char (match-beginning 1))
	    (goto-char pos)
	    (error "No match"))))))
    (and (org-mode-p) (org-show-context 'link-search))
    type))

[-- Attachment #2: foo.org --]
[-- Type: text/plain, Size: 170 bytes --]

* Purpose

This document is intended to demonstrate that src buffer evaluation in
subsidiary, included files, does not work.

* Demo

#+include "~/src/org-test/bar.org" 

[-- Attachment #3: bar.org --]
[-- Type: text/plain, Size: 89 bytes --]

* Here's the demo

#+begin_src sh :exports results :results output
echo "foo!"
#+end_src

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2011-05-11 14:24 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-02  0:44 Org babel does not work properly with included files Robert Goldman
2011-05-02  1:33 ` Nick Dokos
2011-05-02  1:48   ` Robert P. Goldman
2011-05-02  3:43     ` Nick Dokos
2011-05-02  4:15       ` Robert P. Goldman
2011-05-02  1:52   ` Robert P. Goldman
2011-05-02  2:47 ` Robert Goldman
2011-05-05 16:56   ` Eric Schulte
2011-05-05 21:12     ` Robert Goldman
2011-05-05 21:27       ` Eric Schulte
2011-05-05 22:10         ` Robert Goldman
2011-05-11 14:24           ` Robert Goldman

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).