1 ;;; org-inlinetask.el --- Tasks independent of outline hierarchy
3 ;; Copyright (C) 2009, 2010 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 part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; This module implements inline tasks in Org-mode. Inline tasks are
31 ;; tasks that have all the properties of normal outline nodes, including
32 ;; the ability to store meta data like scheduling dates, TODO state, tags
33 ;; and properties. However, these nodes are treated specially by the
34 ;; visibility cycling and export commands.
36 ;; Visibility cycling exempts these nodes from cycling. So whenever their
37 ;; parent is opened, so are these tasks. This will only work with
38 ;; `org-cycle', so if you are also using other commands to show/hide
39 ;; entries, you will occasionally find these tasks to behave like
40 ;; all other outline nodes, seemingly splitting the text of the parent
43 ;; Export commands do not treat these nodes as part of the sectioning
44 ;; structure, but as a special inline text that is either removed, or
45 ;; formatted in some special way. This in handled by
46 ;; `org-inlinetask-export' and `org-inlinetask-export-templates'
49 ;; Special fontification of inline tasks, so that they can be immediately
50 ;; recognized. From the stars of the headline, only the first and the
51 ;; last two will be visible, the others will be hidden using the
54 ;; An inline task is identified solely by a minimum outline level, given
55 ;; by the variable `org-inlinetask-min-level', default 15.
57 ;; If you need to have a time planning line (DEADLINE etc), drawers,
58 ;; for example LOGBOOK of PROPERTIES, or even normal text as part of
59 ;; the inline task, you must add an "END" headline with the same
62 ;; As an example, here are two valid inline tasks:
64 ;; **************** TODO a small task
68 ;; **************** TODO another small task
69 ;; DEADLINE: <2009-03-30 Mon>
71 ;; :SOMETHING: or other
73 ;; And here is some extra text
74 ;; **************** END
76 ;; Also, if you want to use refiling and archiving for inline tasks,
77 ;; The END line must be present to make things work properly.
79 ;; This package installs one new command:
81 ;; C-c C-x t Insert a new inline task with END line
87 (defgroup org-inlinetask nil
88 "Options concerning inline tasks in Org mode."
89 :tag
"Org Inline Tasks"
90 :group
'org-structure
)
92 (defcustom org-inlinetask-min-level
15
93 "Minimum level a headline must have before it is treated as an inline task.
94 It is strongly recommended that you set `org-cycle-max-level' not at all,
95 or to a number smaller than this one. In fact, when `org-cycle-max-level' is
96 not set, it will be assumed to be one less than the value of smaller than
97 the value of this variable."
98 :group
'org-inlinetask
100 (const :tag
"Off" nil
)
103 (defcustom org-inlinetask-export t
104 "Non-nil means export inline tasks.
105 When nil, they will not be exported."
106 :group
'org-inlinetask
109 (defvar org-inlinetask-export-templates
110 '((html "<pre class=\"inlinetask\"><b>%s%s</b><br />\n%s\n</pre>"
111 '((unless (eq todo
"")
112 (format "<span class=\"%s %s\">%s%s</span> "
113 class todo todo priority
))
115 (latex "\\begin\{description\}\n\\item[%s%s]~\n%s\n\\end\{description\}"
116 '((unless (eq todo
"") (format "\\textsc\{%s%s\} " todo priority
))
119 '((unless (eq todo
"") (format "%s%s " todo priority
))
121 (unless (eq content
"")
123 (mapconcat 'identity
(org-split-string content
"\n")
125 (docbook "<variablelist>
128 <listitem><para>%s</para></listitem>
131 '((unless (eq todo
"") (format "%s%s " todo priority
))
133 "Templates for inline tasks in various exporters.
135 This variable is an alist in the shape of (BACKEND STRING OBJECTS).
137 BACKEND is the name of the backend for the template (ascii, html...).
139 STRING is a format control string.
141 OBJECTS is a list of elements to be substituted into the format
142 string. They can be of any type, from a string to a form
143 returning a value (thus allowing conditional insertion). A nil
144 object will be substituted as the empty string. Obviously, there
145 must be at least as many objects as %-sequences in the format
148 Moreover, the following special keywords are provided: `todo',
149 `priority', `heading', `content', `tags'. If some of them are not
150 defined in an inline task, their value is the empty string.
152 As an example, valid associations are:
154 (html \"<ul><li>%s <p>%s</p></li></ul>\" (heading content))
156 or, with the additional package \"todonotes\" for LaTeX,
158 (latex \"\\todo[inline]{\\textbf{\\textsf{%s %s}}\\linebreak{} %s}\"
159 '((unless (eq todo \"\")
160 (format \"\\textsc{%s%s}\" todo priority))
163 (defvar org-odd-levels-only
)
164 (defvar org-keyword-time-regexp
)
165 (defvar org-drawer-regexp
)
166 (defvar org-complex-heading-regexp
)
167 (defvar org-property-end-re
)
169 (defcustom org-inlinetask-default-state nil
170 "Non-nil means make inline tasks have a TODO keyword initially.
171 This should be the state `org-inlinetask-insert-task' should use by
172 default, or nil of no state should be assigned."
173 :group
'org-inlinetask
175 (const :tag
"No state" nil
)
176 (string :tag
"Specific state")))
178 (defun org-inlinetask-insert-task (&optional no-state
)
179 "Insert an inline task.
180 If prefix arg NO-STATE is set, ignore `org-inlinetask-default-state'."
182 (or (bolp) (newline))
183 (let ((indent org-inlinetask-min-level
))
184 (if org-odd-levels-only
185 (setq indent
(- (* 2 indent
) 1)))
186 (insert (make-string indent ?
*)
187 (if (or no-state
(not org-inlinetask-default-state
))
189 (concat " " org-inlinetask-default-state
" \n"))
190 (make-string indent ?
*) " END\n"))
192 (define-key org-mode-map
"\C-c\C-xt" 'org-inlinetask-insert-task
)
194 (defun org-inlinetask-outline-regexp ()
195 "Return string matching an inline task heading.
196 The number of levels is controlled by `org-inlinetask-min-level'."
197 (let ((nstars (if org-odd-levels-only
198 (1- (* org-inlinetask-min-level
2))
199 org-inlinetask-min-level
)))
200 (format "^\\(\\*\\{%d,\\}\\)[ \t]+" nstars
)))
202 (defun org-inlinetask-at-task-p ()
203 "Return true if point is at beginning of an inline task."
206 (and (looking-at (concat (org-inlinetask-outline-regexp) "\\(.*\\)"))
207 (not (string-match "^end[ \t]*$" (downcase (match-string 2)))))))
209 (defun org-inlinetask-in-task-p ()
210 "Return true if point is inside an inline task."
213 (let* ((case-fold-search t
)
214 (stars-re (org-inlinetask-outline-regexp))
215 (task-beg-re (concat stars-re
"\\(?:.*\\)"))
216 (task-end-re (concat stars-re
"END[ \t]*$")))
217 (or (org-looking-at-p task-beg-re
)
218 (and (re-search-forward "^\\*+[ \t]+" nil t
)
219 (progn (beginning-of-line) (org-looking-at-p task-end-re
)))))))
221 (defun org-inlinetask-goto-beginning ()
222 "Go to the beginning of the inline task at point."
224 (let ((case-fold-search t
)
225 (inlinetask-re (org-inlinetask-outline-regexp)))
226 (re-search-backward inlinetask-re nil t
)
227 (when (org-looking-at-p (concat inlinetask-re
"END[ \t]*$"))
228 (re-search-backward inlinetask-re nil t
))))
230 (defun org-inlinetask-goto-end ()
231 "Go to the end of the inline task at point."
233 (let ((case-fold-search t
)
234 (inlinetask-re (org-inlinetask-outline-regexp)))
236 ((org-looking-at-p (concat inlinetask-re
"END[ \t]*$"))
238 ((org-looking-at-p inlinetask-re
)
240 (when (org-inlinetask-in-task-p)
241 (re-search-forward inlinetask-re nil t
)
244 (re-search-forward inlinetask-re nil t
)
247 (defun org-inlinetask-get-task-level ()
248 "Get the level of the inline task around.
249 This assumes the point is inside an inline task."
252 (re-search-backward (org-inlinetask-outline-regexp) nil t
)
253 (- (match-end 1) (match-beginning 1))))
255 (defvar org-export-current-backend
) ; dynamically bound in org-exp.el
256 (defun org-inlinetask-export-handler ()
257 "Handle headlines with level larger or equal to `org-inlinetask-min-level'.
258 Either remove headline and meta data, or do special formatting."
259 (goto-char (point-min))
260 (let* ((nstars (if org-odd-levels-only
261 (1- (* 2 (or org-inlinetask-min-level
200)))
262 (or org-inlinetask-min-level
200)))
263 (re1 (format "^\\(\\*\\{%d,\\}\\)[ \t]+.*\n" nstars
))
264 (re2 (concat "^[ \t]*" org-keyword-time-regexp
))
265 headline beg end stars content
)
266 (while (re-search-forward re1 nil t
)
267 (setq headline
(match-string 0)
268 stars
(match-string 1)
271 (while (looking-at re2
)
272 (delete-region (point) (1+ (point-at-eol))))
273 (while (looking-at org-drawer-regexp
)
275 (if (re-search-forward org-property-end-re nil t
)
276 (delete-region beg
(1+ (match-end 0)))))
278 (when (and (re-search-forward "^\\(\\*+\\)[ \t]+" nil t
)
279 (= (length (match-string 1)) (length stars
))
280 (progn (goto-char (match-end 0))
281 (looking-at "END[ \t]*$")))
282 (setq content
(buffer-substring beg
(1- (point-at-bol))))
283 (delete-region beg
(1+ (match-end 0))))
285 (when org-inlinetask-export
286 ;; content formatting
288 (if (not (string-match "\\S-" content
))
290 (if (string-match "[ \t\n]+\\'" content
)
291 (setq content
(substring content
0 (match-beginning 0))))
292 (setq content
(org-remove-indentation content
))))
293 ;; Prevent from protecting content if there's any
294 (setq content
(or (and content
295 (org-add-props content
'(org-protected nil
)))
297 ;; grab elements to export
298 (when (string-match org-complex-heading-regexp headline
)
299 (let* ((todo (or (match-string 2 headline
) ""))
300 (class (or (and (eq "" todo
) "")
301 (if (member todo org-done-keywords
) "done" "todo")))
302 (priority (or (match-string 3 headline
) ""))
303 (heading (or (match-string 4 headline
) ""))
304 (tags (or (match-string 5 headline
) ""))
305 (backend-spec (assq org-export-current-backend
306 org-inlinetask-export-templates
))
307 (format-str (org-add-props (nth 1 backend-spec
)
309 (tokens (cadr (nth 2 backend-spec
)))
311 ;; Change nil arguments into empty strings
312 (lambda (el) (or (eval el
) "")))
313 ;; Build and ensure export string will not break lists
314 (export-str (org-add-props
315 (eval (append '(format format-str
)
316 (mapcar nil-to-str tokens
)))
317 '(original-indentation 1000))))
318 ;; Eventually insert it
319 (insert export-str
"\n")))))))
321 (defun org-inlinetask-get-current-indentation ()
322 "Get the indentation of the last non-while line above this one."
324 (beginning-of-line 1)
325 (skip-chars-backward " \t\n")
326 (beginning-of-line 1)
328 (looking-at "[ \t]*"))
329 (goto-char (match-end 0))
332 (defun org-inlinetask-fontify (limit)
333 "Fontify the inline tasks."
334 (let* ((nstars (if org-odd-levels-only
335 (1- (* 2 (or org-inlinetask-min-level
200)))
336 (or org-inlinetask-min-level
200)))
337 (re (concat "^\\(\\*\\)\\(\\*\\{"
338 (format "%d" (- nstars
3))
339 ",\\}\\)\\(\\*\\* .*\\)")))
340 (while (re-search-forward re limit t
)
341 (add-text-properties (match-beginning 1) (match-end 1)
342 '(face org-warning font-lock-fontified t
))
343 (add-text-properties (match-beginning 2) (match-end 2)
344 '(face org-hide font-lock-fontified t
))
345 (add-text-properties (match-beginning 3) (match-end 3)
346 '(face shadow font-lock-fontified t
)))))
348 (defun org-inlinetask-toggle-visibility ()
349 "Toggle visibility of inline task at point."
350 (let ((end (save-excursion
351 (org-inlinetask-goto-end)
352 (if (bolp) (1- (point)) (point))))
353 (start (save-excursion
354 (org-inlinetask-goto-beginning)
357 ;; Nothing to show/hide.
359 ;; Inlinetask was folded: expand it.
360 ((get-char-property (1+ start
) 'invisible
)
361 (outline-flag-region start end nil
))
362 (t (outline-flag-region start end t
)))))
364 (defun org-inlinetask-remove-END-maybe ()
365 "Remove an END line when present."
366 (when (looking-at (format "\\([ \t]*\n\\)*\\*\\{%d,\\}[ \t]+END[ \t]*$"
367 org-inlinetask-min-level
))
370 (eval-after-load "org-exp"
371 '(add-hook 'org-export-preprocess-before-backend-specifics-hook
372 'org-inlinetask-export-handler
))
373 (eval-after-load "org"
374 '(add-hook 'org-font-lock-hook
'org-inlinetask-fontify
))
376 (provide 'org-inlinetask
)
378 ;;; org-inlinetask.el ends here