ox: Handle BIND keywords in SETUPFILE files

* lisp/ox.el (org-export-get-environment): Update comment.
(org-export--install-letbind-maybe): Go into SETUPFILE files and
handle BIND keywords there.
* testing/examples/setupfile.org: Update test file.
* testing/lisp/test-ox.el: Add tests.
This commit is contained in:
Nicolas Goaziou 2013-03-29 21:21:48 +01:00
parent 2a0e45f701
commit 7736c2d7fb
3 changed files with 48 additions and 14 deletions

View File

@ -1427,7 +1427,8 @@ done against the current sub-tree.
Third optional argument EXT-PLIST is a property list with Third optional argument EXT-PLIST is a property list with
external parameters overriding Org default settings, but still external parameters overriding Org default settings, but still
inferior to file-local settings." inferior to file-local settings."
;; First install #+BIND variables. ;; First install #+BIND variables since these must be set before
;; global options are read.
(org-export--install-letbind-maybe) (org-export--install-letbind-maybe)
;; Get and prioritize export options... ;; Get and prioritize export options...
(org-combine-plists (org-combine-plists
@ -1713,20 +1714,39 @@ process."
plist)) plist))
(defun org-export--install-letbind-maybe () (defun org-export--install-letbind-maybe ()
"Install the values from #+BIND lines as local variables. "Install the values from #+BIND lines as local variables."
Variables must be installed before in-buffer options are
retrieved."
(when org-export-allow-bind-keywords (when org-export-allow-bind-keywords
(let ((case-fold-search t) letbind) (let* (collect-bind ; For byte-compiler.
(org-with-wide-buffer (collect-bind
(goto-char (point-min)) (lambda (files alist)
(while (re-search-forward "^[ \t]*#\\+BIND:" nil t) ;; Return an alist between variable names and their
(let* ((element (org-element-at-point)) ;; value. FILES is a list of setup files names read so
(value (org-element-property :value element))) ;; far, used to avoid circular dependencies. ALIST is
(when (and (eq (org-element-type element) 'keyword) ;; the alist collected so far.
(not (equal value ""))) (let ((case-fold-search t))
(push (read (format "(%s)" value)) letbind))))) (org-with-wide-buffer
(dolist (pair (nreverse letbind)) (goto-char (point-min))
(while (re-search-forward
"^[ \t]*#\\+\\(BIND\\|SETUPFILE\\):" nil t)
(let ((element (org-element-at-point)))
(when (eq (org-element-type element) 'keyword)
(let ((val (org-element-property :value element)))
(if (equal (org-element-property :key element) "BIND")
(push (read (format "(%s)" val)) alist)
;; Enter setup file.
(let ((file (expand-file-name
(org-remove-double-quotes val))))
(unless (member file files)
(with-temp-buffer
(org-mode)
(insert (org-file-contents file 'noerror))
(setq alist
(funcall collect-bind
(cons file files)
alist))))))))))
alist)))))
;; Install each variable in current buffer.
(dolist (pair (nreverse (funcall collect-bind nil nil)))
(org-set-local (car pair) (nth 1 pair)))))) (org-set-local (car pair) (nth 1 pair))))))

View File

@ -1,3 +1,4 @@
#+BIND: variable value
#+DESCRIPTION: l2 #+DESCRIPTION: l2
#+LANGUAGE: en #+LANGUAGE: en
#+SELECT_TAGS: b #+SELECT_TAGS: b

View File

@ -88,6 +88,19 @@ already filled in `info'."
;; BIND keywords are case-insensitive. ;; BIND keywords are case-insensitive.
(should (should
(org-test-with-temp-text "#+bind: variable value" (org-test-with-temp-text "#+bind: variable value"
(let ((org-export-allow-bind-keywords t))
(org-export--install-letbind-maybe)
(eq variable 'value))))
;; Preserve order of BIND keywords.
(should
(org-test-with-temp-text "#+BIND: variable 1\n#+BIND: variable 2"
(let ((org-export-allow-bind-keywords t))
(org-export--install-letbind-maybe)
(eq variable 2))))
;; Read BIND keywords in setup files.
(should
(org-test-with-temp-text
(format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
(let ((org-export-allow-bind-keywords t)) (let ((org-export-allow-bind-keywords t))
(org-export--install-letbind-maybe) (org-export--install-letbind-maybe)
(eq variable 'value))))) (eq variable 'value)))))