1 ;;; org-mtags.el --- Muse-like tags in Org-mode
3 ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
10 ;; This file is not yet part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; This modules implements some of the formatting tags available in
31 ;; Emacs Muse. This is not a way if adding new functionality, but just
32 ;; a different way to write some formatting directives. The advantage is
33 ;; that files written in this way can be read by Muse reasonably well,
34 ;; and that this provides an alternative way of writing formatting
35 ;; directives in Org, a way that some might find more pleasant to type
36 ;; and look at that the Org's #+BEGIN..#+END notation.
38 ;; The goal of this development is to make it easier for people to
39 ;; move between both worlds as they see fit for different tasks.
41 ;; The following muse tags will be translated during export into their
42 ;; native Org equivalents:
45 ;; Needs to be at the end of a line. Will be translated to "\\".
47 ;; <example switches="-n -r">
48 ;; Needs to be on a line by itself, similarly the </example> tag.
49 ;; Will be translated into Org's #+BEGIN_EXAMPLE construct.
52 ;; Needs to be on a line by itself, similarly the </quote> tag.
53 ;; Will be translated into Org's #+BEGIN_QUOTE construct.
56 ;; Needs to be on a line by itself, similarly the </comment> tag.
57 ;; Will be translated into Org's #+BEGIN_COMMENT construct.
60 ;; Needs to be on a line by itself, similarly the </verse> tag.
61 ;; Will be translated into Org's #+BEGIN_VERSE construct.
64 ;; This gets translated into "[TABLE-OF-CONTENTS]". It will not
65 ;; trigger the production of a table of contents - that is done
66 ;; in Org with the "#+OPTIONS: toc:t" setting. But it will define
67 ;; the location where the TOC will be placed.
69 ;; <literal style="STYLE"> ;; only latex, html, and docbook supported
71 ;; Needs to be on a line by itself, similarly the </literal> tag.
73 ;; <src lang="LANG" switches="-n -r">
74 ;; Needs to be on a line by itself, similarly the </src> tag.
75 ;; Will be translated into Org's BEGIN_SRC construct.
77 ;; <include file="FILE" markup="MARKUP" lang="LANG"
78 ;; prefix="str" prefix1="str" switches="-n -r">
79 ;; Needs to be on a line by itself.
80 ;; Will be translated into Org's #+INCLUDE construct.
82 ;; The lisp/perl/ruby/python tags can be implemented using the
83 ;; `org-eval.el' module, which see.
89 (defgroup org-mtags nil
90 "Options concerning Muse tags in Org mode."
94 (defface org-mtags
; similar to shadow
95 (org-compatible-face 'shadow
96 '((((class color grayscale
) (min-colors 88) (background light
))
97 (:foreground
"grey50"))
98 (((class color grayscale
) (min-colors 88) (background dark
))
99 (:foreground
"grey70"))
100 (((class color
) (min-colors 8) (background light
))
101 (:foreground
"green"))
102 (((class color
) (min-colors 8) (background dark
))
103 (:foreground
"yellow"))))
104 "Face for Muse-like tags in Org."
108 (defcustom org-mtags-prefer-muse-templates t
109 "Non-nil means prefere Muse tags for structure elements.
110 This is relevane when expanding the templates defined in the variable
111 `org-structure-templates'."
115 (defconst org-mtags-supported-tags
116 '("example" "quote" "comment" "verse" "contents" "literal" "src" "include")
117 "The tags that are supported by org-mtags.el for conversion.
118 In addition to this list, the <br> tag is supported as well.")
120 (defconst org-mtags-fontification-re
123 (mapconcat 'identity org-mtags-supported-tags
"\\|")
124 "\\)\\>[^>]*>\\|<br>[ \t]*$")
125 "Regular expression used for fontifying muse tags.")
127 (defun org-mtags-replace ()
128 "Replace Muse-like tags with the appropriate Org constructs.
129 The is done in the entire buffer."
130 (interactive) ;; FIXME
131 (let ((re (concat "^[ \t]*\\(</?\\("
132 (mapconcat 'identity org-mtags-supported-tags
"\\|")
134 info tag rpl style markup lang file prefix prefix1 switches
)
135 ;; First, do the <br> tag
136 (goto-char (point-min))
137 (while (re-search-forward "<br>[ \t]*$" nil t
)
138 (replace-match "\\\\" t t
))
139 ;; Now, all the other tags
140 (goto-char (point-min))
141 (while (re-search-forward re nil t
)
142 (goto-char (match-beginning 1))
143 (setq info
(org-mtags-get-tag-and-attributes))
146 (setq tag
(plist-get info
:tag
))
148 ((equal tag
"contents")
149 (setq rpl
"[TABLE-OF-CONTENTS]")
150 ;; FIXME: also trigger TOC in options-plist?????
152 ((member tag
'("quote" "comment" "verse"))
153 (if (plist-get info
:closing
)
154 (setq rpl
(format "#+END_%s" (upcase tag
)))
155 (setq rpl
(format "#+BEGIN_%s" (upcase tag
)))))
156 ((equal tag
"literal")
157 (setq style
(plist-get info
:style
))
158 (and style
(setq style
(downcase style
)))
159 (if (plist-get info
:closing
)
161 ((member style
'("latex"))
163 ((member style
'("html"))
165 ((member style
'("docbook"))
167 ((member style
'("ascii"))
170 ((member style
'("latex"))
172 ((member style
'("html"))
174 ((member style
'("ascii"))
176 ((equal tag
"example")
177 (if (plist-get info
:closing
)
178 (setq rpl
"#+END_EXAMPLE")
179 (setq rpl
"#+BEGIN_EXAMPLE")
180 (when (setq switches
(plist-get info
:switches
))
181 (setq rpl
(concat rpl
" " switches
)))))
183 (if (plist-get info
:closing
)
184 (setq rpl
"#+END_SRC")
185 (setq rpl
"#+BEGIN_SRC")
186 (when (setq lang
(plist-get info
:lang
))
187 (setq rpl
(concat rpl
" " lang
))
188 (when (setq switches
(plist-get info
:switches
))
189 (setq rpl
(concat rpl
" " switches
))))))
190 ((equal tag
"include")
191 (setq file
(plist-get info
:file
)
192 markup
(downcase (plist-get info
:markup
))
193 lang
(plist-get info
:lang
)
194 prefix
(plist-get info
:prefix
)
195 prefix1
(plist-get info
:prefix1
)
196 switches
(plist-get info
:switches
))
197 (setq rpl
"#+INCLUDE")
198 (setq rpl
(concat rpl
" " (prin1-to-string file
)))
200 (setq rpl
(concat rpl
" " markup
))
201 (when (and (equal markup
"src") lang
)
202 (setq rpl
(concat rpl
" " lang
))))
204 (setq rpl
(concat rpl
" :prefix " (prin1-to-string prefix
))))
206 (setq rpl
(concat rpl
" :prefix1 " (prin1-to-string prefix1
))))
208 (setq rpl
(concat rpl
" " switches
)))))
210 (goto-char (plist-get info
:match-beginning
))
211 (delete-region (point-at-bol) (plist-get info
:match-end
))
214 (defun org-mtags-get-tag-and-attributes ()
215 "Parse a Muse-like tag at point ant rturn the information about it.
216 The return value is a property list which contains all the attributes
217 with string values. In addition, it reutnrs the following properties:
219 :tag The tag as a string.
220 :match-beginning The beginning of the match, just before \"<\".
221 :match-end The end of the match, just after \">\".
222 :closing t when the tag starts with \"</\"."
223 (when (looking-at "<\\(/\\)?\\([a-zA-Z]+\\>\\)\\([^>]*\\)>")
225 tag rest prop attributes endp val
)
226 (setq tag
(org-match-string-no-properties 2)
228 rest
(and (match-end 3)
229 (org-match-string-no-properties 3))
230 attributes
(list :tag tag
231 :match-beginning
(match-beginning 0)
232 :match-end
(match-end 0)
235 (while (string-match "\\([a-zA-Z]+\\)=\\([^ \t\n>]+\\|\"[^>]+\"\\)"
237 (setq start
(match-end 0)
238 prop
(org-match-string-no-properties 1 rest
)
239 val
(org-remove-double-quotes
240 (org-match-string-no-properties 2 rest
)))
241 (setq attributes
(plist-put attributes
242 (intern (concat ":" prop
)) val
))))
245 (defun org-mtags-fontify-tags (limit)
246 "Fontify the muse-like tags."
247 (while (re-search-forward org-mtags-fontification-re limit t
)
248 (add-text-properties (match-beginning 0) (match-end 0)
249 '(face org-mtags font-lock-multiline t
250 font-lock-fontified t
))))
252 (add-hook 'org-export-preprocess-hook
'org-mtags-replace
)
253 (add-hook 'org-font-lock-hook
'org-mtags-fontify-tags
)
257 ;;; org-mtags.el ends here