ob-shell: honor the specified shell for :session
[org-mode.git] / contrib / lisp / ox-extra.el
blobf4f0b76592ad4526f82129fc4876934797438dfc
1 ;;; ox-extra.el --- Convenience functions for org export
3 ;; Copyright (C) 2014 Aaron Ecay
5 ;; Author: Aaron Ecay <aaronecay@gmail.com>
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ;;; Commentary:
22 ;; This file contains some convenience functions for org export, which
23 ;; are not part of org's core. Call `ox-extras-activate' passing a
24 ;; list of symbols naming extras, which will be installed globally in
25 ;; your org session.
27 ;; Currently available extras:
29 ;; - `latex-header-blocks' -- allow the use of latex blocks, the
30 ;; contents of which which will be interpreted as #+latex_header lines
31 ;; for export. These blocks should be tagged with #+header: :header
32 ;; yes. For example:
33 ;; #+header: :header yes
34 ;; #+begin_latex
35 ;; ...
36 ;; #+end_latex
38 ;; TODO:
39 ;; - add a function to org-mode-hook that looks for a ox-extras local
40 ;; variable and activates the specified extras buffer-locally
41 ;; - allow specification of desired extras to be activated via
42 ;; customize
44 ;;; Code:
46 (require 'ox)
47 (eval-when-compile (require 'cl))
49 (defun org-latex-header-blocks-filter (backend)
50 (when (org-export-derived-backend-p backend 'latex)
51 (let ((positions
52 (org-element-map (org-element-parse-buffer 'greater-element nil) 'export-block
53 (lambda (block)
54 (when (and (string= (org-element-property :type block) "LATEX")
55 (string= (org-export-read-attribute
56 :header block :header)
57 "yes"))
58 (list (org-element-property :begin block)
59 (org-element-property :end block)
60 (org-element-property :post-affiliated block)))))))
61 (mapc (lambda (pos)
62 (goto-char (nth 2 pos))
63 (destructuring-bind
64 (beg end &rest ignore)
65 (org-edit-src-find-region-and-lang)
66 (let ((contents-lines (split-string
67 (buffer-substring-no-properties beg end)
68 "\n")))
69 (delete-region (nth 0 pos) (nth 1 pos))
70 (dolist (line contents-lines)
71 (insert (concat "#+latex_header: "
72 (replace-regexp-in-string "\\` *" "" line)
73 "\n"))))))
74 ;; go in reverse, to avoid wrecking the numeric positions
75 ;; earlier in the file
76 (reverse positions)))))
78 (defconst ox-extras
79 '((latex-header-blocks org-latex-header-blocks-filter org-export-before-parsing-hook))
80 "A list of org export extras that can be enabled.
82 Should be a list of items of the form (NAME FN HOOK). NAME is a
83 symbol, which can be passed to `ox-extras-activate'. FN is a
84 function which will be added to HOOK.")
86 (defun ox-extras-activate (extras)
87 "Activate certain org export extras.
89 EXTRAS should be a list of extras (defined in `ox-extras') which
90 should be activated."
91 (dolist (extra extras)
92 (let* ((lst (assq extra ox-extras))
93 (fn (nth 1 lst))
94 (hook (nth 2 lst)))
95 (when (and fn hook)
96 (add-hook hook fn)))))
98 (defun ox-extras-deactivate (extras)
99 "Deactivate certain org export extras.
101 This function is the opposite of `ox-extras-activate'. EXTRAS
102 should be a list of extras (defined in `ox-extras') which should
103 be activated."
104 (dolist (extra extras)
105 (let* ((lst (assq extra ox-extras))
106 (fn (nth 1 lst))
107 (hook (nth 2 lst)))
108 (when (and fn hook)
109 (remove-hook hook fn)))))
111 (provide 'ox-extra)
112 ;;; ox-extra.el ends here