]> code.delx.au - gnu-emacs/blob - lisp/org/org-inlinetask.el
Add :version tag to new Org options in Emacs 24.1
[gnu-emacs] / lisp / org / org-inlinetask.el
1 ;;; org-inlinetask.el --- Tasks independent of outline hierarchy
2
3 ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
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.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;
27 ;;; Commentary:
28 ;;
29 ;; This module implements inline tasks in Org-mode. Inline tasks are
30 ;; tasks that have all the properties of normal outline nodes, including
31 ;; the ability to store meta data like scheduling dates, TODO state, tags
32 ;; and properties. However, these nodes are treated specially by the
33 ;; visibility cycling and export commands.
34 ;;
35 ;; Visibility cycling exempts these nodes from cycling. So whenever their
36 ;; parent is opened, so are these tasks. This will only work with
37 ;; `org-cycle', so if you are also using other commands to show/hide
38 ;; entries, you will occasionally find these tasks to behave like
39 ;; all other outline nodes, seemingly splitting the text of the parent
40 ;; into children.
41 ;;
42 ;; Export commands do not treat these nodes as part of the sectioning
43 ;; structure, but as a special inline text that is either removed, or
44 ;; formatted in some special way. This in handled by
45 ;; `org-inlinetask-export' and `org-inlinetask-export-templates'
46 ;; variables.
47 ;;
48 ;; Special fontification of inline tasks, so that they can be immediately
49 ;; recognized. From the stars of the headline, only the first and the
50 ;; last two will be visible, the others will be hidden using the
51 ;; `org-hide' face.
52 ;;
53 ;; An inline task is identified solely by a minimum outline level, given
54 ;; by the variable `org-inlinetask-min-level', default 15.
55 ;;
56 ;; If you need to have a time planning line (DEADLINE etc), drawers,
57 ;; for example LOGBOOK of PROPERTIES, or even normal text as part of
58 ;; the inline task, you must add an "END" headline with the same
59 ;; number of stars.
60 ;;
61 ;; As an example, here are two valid inline tasks:
62 ;;
63 ;; **************** TODO a small task
64 ;;
65 ;; and
66 ;;
67 ;; **************** TODO another small task
68 ;; DEADLINE: <2009-03-30 Mon>
69 ;; :PROPERTIES:
70 ;; :SOMETHING: or other
71 ;; :END:
72 ;; And here is some extra text
73 ;; **************** END
74 ;;
75 ;; Also, if you want to use refiling and archiving for inline tasks,
76 ;; The END line must be present to make things work properly.
77 ;;
78 ;; This package installs one new command:
79 ;;
80 ;; C-c C-x t Insert a new inline task with END line
81
82 ;;; Code:
83
84 (require 'org)
85
86 (defgroup org-inlinetask nil
87 "Options concerning inline tasks in Org mode."
88 :tag "Org Inline Tasks"
89 :group 'org-structure)
90
91 (defcustom org-inlinetask-min-level 15
92 "Minimum level a headline must have before it is treated as an inline task.
93 It is strongly recommended that you set `org-cycle-max-level' not at all,
94 or to a number smaller than this one. In fact, when `org-cycle-max-level' is
95 not set, it will be assumed to be one less than the value of smaller than
96 the value of this variable."
97 :group 'org-inlinetask
98 :type '(choice
99 (const :tag "Off" nil)
100 (integer)))
101
102 (defcustom org-inlinetask-export t
103 "Non-nil means export inline tasks.
104 When nil, they will not be exported."
105 :group 'org-inlinetask
106 :type 'boolean)
107
108 (defvar org-inlinetask-export-templates
109 '((html "<div class=\"inlinetask\"><b>%s%s</b><br />%s</div>"
110 '((unless (eq todo "")
111 (format "<span class=\"%s %s\">%s%s</span> "
112 class todo todo priority))
113 heading content))
114 (odt "%s" '((org-odt-format-inlinetask heading content
115 todo priority tags)))
116
117 (latex "\\begin\{description\}\n\\item[%s%s]~%s\\end\{description\}"
118 '((unless (eq todo "") (format "\\textsc\{%s%s\} " todo priority))
119 heading content))
120 (ascii " -- %s%s%s"
121 '((unless (eq todo "") (format "%s%s " todo priority))
122 heading
123 (unless (eq content "")
124 (format "\n ¦ %s"
125 (mapconcat 'identity (org-split-string content "\n")
126 "\n ¦ ")))))
127 (docbook "<variablelist>
128 <varlistentry>
129 <term>%s%s</term>
130 <listitem><para>%s</para></listitem>
131 </varlistentry>
132 </variablelist>"
133 '((unless (eq todo "") (format "%s%s " todo priority))
134 heading content)))
135 "Templates for inline tasks in various exporters.
136
137 This variable is an alist in the shape of \(BACKEND STRING OBJECTS\).
138
139 BACKEND is the name of the backend for the template \(ascii, html...\).
140
141 STRING is a format control string.
142
143 OBJECTS is a list of elements to be substituted into the format
144 string. They can be of any type, from a string to a form
145 returning a value (thus allowing conditional insertion). A nil
146 object will be substituted as the empty string. Obviously, there
147 must be at least as many objects as %-sequences in the format
148 string.
149
150 Moreover, the following special keywords are provided: `todo',
151 `priority', `heading', `content', `tags'. If some of them are not
152 defined in an inline task, their value is the empty string.
153
154 As an example, valid associations are:
155
156 \(html \"<ul><li>%s <p>%s</p></li></ul>\" \(heading content\)\)
157
158 or, with the additional package \"todonotes\" for LaTeX,
159
160 \(latex \"\\todo[inline]{\\textbf{\\textsf{%s %s}}\\linebreak{} %s}\"
161 '\(\(unless \(eq todo \"\"\)
162 \(format \"\\textsc{%s%s}\" todo priority\)\)
163 heading content\)\)\)")
164
165 (defvar org-odd-levels-only)
166 (defvar org-keyword-time-regexp)
167 (defvar org-drawer-regexp)
168 (defvar org-complex-heading-regexp)
169 (defvar org-property-end-re)
170
171 (defcustom org-inlinetask-default-state nil
172 "Non-nil means make inline tasks have a TODO keyword initially.
173 This should be the state `org-inlinetask-insert-task' should use by
174 default, or nil of no state should be assigned."
175 :group 'org-inlinetask
176 :version "24.1"
177 :type '(choice
178 (const :tag "No state" nil)
179 (string :tag "Specific state")))
180
181 (defun org-inlinetask-insert-task (&optional no-state)
182 "Insert an inline task.
183 If prefix arg NO-STATE is set, ignore `org-inlinetask-default-state'."
184 (interactive "P")
185 ;; Error when inside an inline task, except if point was at its very
186 ;; beginning, in which case the new inline task will be inserted
187 ;; before this one.
188 (when (and (org-inlinetask-in-task-p)
189 (not (and (org-inlinetask-at-task-p) (bolp))))
190 (error "Cannot nest inline tasks"))
191 (or (bolp) (newline))
192 (let* ((indent (if org-odd-levels-only
193 (1- (* 2 org-inlinetask-min-level))
194 org-inlinetask-min-level))
195 (indent-string (concat (make-string indent ?*) " ")))
196 (insert indent-string
197 (if (or no-state (not org-inlinetask-default-state))
198 "\n"
199 (concat org-inlinetask-default-state " \n"))
200 indent-string "END\n"))
201 (end-of-line -1))
202 (define-key org-mode-map "\C-c\C-xt" 'org-inlinetask-insert-task)
203
204 (defun org-inlinetask-outline-regexp ()
205 "Return string matching an inline task heading.
206 The number of levels is controlled by `org-inlinetask-min-level'."
207 (let ((nstars (if org-odd-levels-only
208 (1- (* org-inlinetask-min-level 2))
209 org-inlinetask-min-level)))
210 (format "^\\(\\*\\{%d,\\}\\)[ \t]+" nstars)))
211
212 (defun org-inlinetask-at-task-p ()
213 "Return true if point is at beginning of an inline task."
214 (save-excursion
215 (beginning-of-line)
216 (and (looking-at (concat (org-inlinetask-outline-regexp) "\\(.*\\)"))
217 (not (string-match "^end[ \t]*$" (downcase (match-string 2)))))))
218
219 (defun org-inlinetask-in-task-p ()
220 "Return true if point is inside an inline task."
221 (save-excursion
222 (beginning-of-line)
223 (let* ((case-fold-search t)
224 (stars-re (org-inlinetask-outline-regexp))
225 (task-beg-re (concat stars-re "\\(?:.*\\)"))
226 (task-end-re (concat stars-re "END[ \t]*$")))
227 (or (org-looking-at-p task-beg-re)
228 (and (re-search-forward "^\\*+[ \t]+" nil t)
229 (progn (beginning-of-line) (org-looking-at-p task-end-re)))))))
230
231 (defun org-inlinetask-goto-beginning ()
232 "Go to the beginning of the inline task at point."
233 (end-of-line)
234 (let ((case-fold-search t)
235 (inlinetask-re (org-inlinetask-outline-regexp)))
236 (re-search-backward inlinetask-re nil t)
237 (when (org-looking-at-p (concat inlinetask-re "END[ \t]*$"))
238 (re-search-backward inlinetask-re nil t))))
239
240 (defun org-inlinetask-goto-end ()
241 "Go to the end of the inline task at point.
242 Return point."
243 (save-match-data
244 (beginning-of-line)
245 (let* ((case-fold-search t)
246 (inlinetask-re (org-inlinetask-outline-regexp))
247 (task-end-re (concat inlinetask-re "END[ \t]*$")))
248 (cond
249 ((looking-at task-end-re) (forward-line))
250 ((looking-at inlinetask-re)
251 (forward-line)
252 (cond
253 ((looking-at task-end-re) (forward-line))
254 ((looking-at inlinetask-re))
255 ((org-inlinetask-in-task-p)
256 (re-search-forward inlinetask-re nil t)
257 (forward-line))))
258 (t (re-search-forward inlinetask-re nil t)
259 (forward-line)))
260 (point))))
261
262 (defun org-inlinetask-get-task-level ()
263 "Get the level of the inline task around.
264 This assumes the point is inside an inline task."
265 (save-excursion
266 (end-of-line)
267 (re-search-backward (org-inlinetask-outline-regexp) nil t)
268 (- (match-end 1) (match-beginning 1))))
269
270 (defun org-inlinetask-promote ()
271 "Promote the inline task at point.
272 If the task has an end part, promote it. Also, prevents level from
273 going below `org-inlinetask-min-level'."
274 (interactive)
275 (if (not (org-inlinetask-in-task-p))
276 (error "Not in an inline task")
277 (save-excursion
278 (let* ((lvl (org-inlinetask-get-task-level))
279 (next-lvl (org-get-valid-level lvl -1))
280 (diff (- next-lvl lvl))
281 (down-task (concat (make-string next-lvl ?*)))
282 beg)
283 (if (< next-lvl org-inlinetask-min-level)
284 (error "Cannot promote an inline task at minimum level")
285 (org-inlinetask-goto-beginning)
286 (setq beg (point))
287 (replace-match down-task nil t nil 1)
288 (org-inlinetask-goto-end)
289 (if (eobp) (beginning-of-line) (forward-line -1))
290 (unless (= (point) beg)
291 (replace-match down-task nil t nil 1)
292 (when org-adapt-indentation
293 (goto-char beg)
294 (org-fixup-indentation diff))))))))
295
296 (defun org-inlinetask-demote ()
297 "Demote the inline task at point.
298 If the task has an end part, also demote it."
299 (interactive)
300 (if (not (org-inlinetask-in-task-p))
301 (error "Not in an inline task")
302 (save-excursion
303 (let* ((lvl (org-inlinetask-get-task-level))
304 (next-lvl (org-get-valid-level lvl 1))
305 (diff (- next-lvl lvl))
306 (down-task (concat (make-string next-lvl ?*)))
307 beg)
308 (org-inlinetask-goto-beginning)
309 (setq beg (point))
310 (replace-match down-task nil t nil 1)
311 (org-inlinetask-goto-end)
312 (if (eobp) (beginning-of-line) (forward-line -1))
313 (unless (= (point) beg)
314 (replace-match down-task nil t nil 1)
315 (when org-adapt-indentation
316 (goto-char beg)
317 (org-fixup-indentation diff)))))))
318
319 (defvar org-export-current-backend) ; dynamically bound in org-exp.el
320 (defun org-inlinetask-export-handler ()
321 "Handle headlines with level larger or equal to `org-inlinetask-min-level'.
322 Either remove headline and meta data, or do special formatting."
323 (goto-char (point-min))
324 (let* ((keywords-re (concat "^[ \t]*" org-keyword-time-regexp))
325 (inline-re (concat (org-inlinetask-outline-regexp) ".*")))
326 (while (re-search-forward inline-re nil t)
327 (let ((headline (match-string 0))
328 (beg (point-at-bol))
329 (end (copy-marker (save-excursion
330 (org-inlinetask-goto-end) (point))))
331 content)
332 ;; Delete SCHEDULED, DEADLINE...
333 (while (re-search-forward keywords-re end t)
334 (delete-region (point-at-bol) (1+ (point-at-eol))))
335 (goto-char beg)
336 ;; Delete drawers
337 (while (re-search-forward org-drawer-regexp end t)
338 (when (save-excursion (re-search-forward org-property-end-re nil t))
339 (delete-region beg (1+ (match-end 0)))))
340 ;; Get CONTENT, if any.
341 (goto-char beg)
342 (forward-line 1)
343 (unless (= (point) end)
344 (setq content (buffer-substring (point)
345 (save-excursion (goto-char end)
346 (forward-line -1)
347 (point)))))
348 ;; Remove the task.
349 (goto-char beg)
350 (delete-region beg end)
351 (when (and org-inlinetask-export
352 (assq org-export-current-backend
353 org-inlinetask-export-templates))
354 ;; Format CONTENT, if appropriate.
355 (setq content
356 (if (not (and content (string-match "\\S-" content)))
357 ""
358 ;; Ensure CONTENT has minimal indentation, a single
359 ;; newline character at its boundaries, and isn't
360 ;; protected.
361 (when (string-match "\\`\\([ \t]*\n\\)+" content)
362 (setq content (substring content (match-end 0))))
363 (when (string-match "[ \t\n]+\\'" content)
364 (setq content (substring content 0 (match-beginning 0))))
365 (org-add-props
366 (concat "\n\n" (org-remove-indentation content) "\n\n")
367 '(org-protected nil org-native-text nil))))
368
369 (when (string-match org-complex-heading-regexp headline)
370 (let* ((nil-to-str
371 (function
372 ;; Change nil arguments into empty strings.
373 (lambda (el) (or (eval el) ""))))
374 ;; Set up keywords provided to templates.
375 (todo (or (match-string 2 headline) ""))
376 (class (or (and (eq "" todo) "")
377 (if (member todo org-done-keywords) "done" "todo")))
378 (priority (or (match-string 3 headline) ""))
379 (heading (or (match-string 4 headline) ""))
380 (tags (or (match-string 5 headline) ""))
381 ;; Read `org-inlinetask-export-templates'.
382 (backend-spec (assq org-export-current-backend
383 org-inlinetask-export-templates))
384 (format-str (org-add-props (nth 1 backend-spec)
385 '(org-protected t org-native-text t)))
386 (tokens (cadr (nth 2 backend-spec)))
387 ;; Build export string. Ensure it won't break
388 ;; surrounding lists by giving it arbitrary high
389 ;; indentation.
390 (export-str (org-add-props
391 (eval (append '(format format-str)
392 (mapcar nil-to-str tokens)))
393 '(original-indentation 1000))))
394 ;; Ensure task starts a new paragraph.
395 (unless (or (bobp)
396 (save-excursion (forward-line -1)
397 (looking-at "[ \t]*$")))
398 (insert "\n"))
399 (insert export-str)
400 (unless (bolp) (insert "\n")))))))))
401
402 (defun org-inlinetask-get-current-indentation ()
403 "Get the indentation of the last non-while line above this one."
404 (save-excursion
405 (beginning-of-line 1)
406 (skip-chars-backward " \t\n")
407 (beginning-of-line 1)
408 (or (org-at-item-p)
409 (looking-at "[ \t]*"))
410 (goto-char (match-end 0))
411 (current-column)))
412
413 (defvar org-indent-indentation-per-level) ; defined in org-indent.el
414
415 (defface org-inlinetask
416 (org-compatible-face 'shadow '((t (:bold t))))
417 "Face for inlinetask headlines."
418 :group 'org-faces)
419
420 (defun org-inlinetask-fontify (limit)
421 "Fontify the inline tasks down to LIMIT."
422 (let* ((nstars (if org-odd-levels-only
423 (1- (* 2 (or org-inlinetask-min-level 200)))
424 (or org-inlinetask-min-level 200)))
425 (re (concat "^\\(\\*\\)\\(\\*\\{"
426 (format "%d" (- nstars 3))
427 ",\\}\\)\\(\\*\\* .*\\)"))
428 ;; Virtual indentation will add the warning face on the first
429 ;; star. Thus, in that case, only hide it.
430 (start-face (if (and (org-bound-and-true-p org-indent-mode)
431 (> org-indent-indentation-per-level 1))
432 'org-hide
433 'org-warning)))
434 (while (re-search-forward re limit t)
435 (add-text-properties (match-beginning 1) (match-end 1)
436 `(face ,start-face font-lock-fontified t))
437 (add-text-properties (match-beginning 2) (match-end 2)
438 '(face org-hide font-lock-fontified t))
439 (add-text-properties (match-beginning 3) (match-end 3)
440 '(face org-inlinetask font-lock-fontified t)))))
441
442 (defun org-inlinetask-toggle-visibility ()
443 "Toggle visibility of inline task at point."
444 (let ((end (save-excursion
445 (org-inlinetask-goto-end)
446 (if (bolp) (1- (point)) (point))))
447 (start (save-excursion
448 (org-inlinetask-goto-beginning)
449 (point-at-eol))))
450 (cond
451 ;; Nothing to show/hide.
452 ((= end start))
453 ;; Inlinetask was folded: expand it.
454 ((get-char-property (1+ start) 'invisible)
455 (outline-flag-region start end nil))
456 (t (outline-flag-region start end t)))))
457
458 (defun org-inlinetask-remove-END-maybe ()
459 "Remove an END line when present."
460 (when (looking-at (format "\\([ \t]*\n\\)*\\*\\{%d,\\}[ \t]+END[ \t]*$"
461 org-inlinetask-min-level))
462 (replace-match "")))
463
464 (eval-after-load "org-exp"
465 '(add-hook 'org-export-preprocess-before-backend-specifics-hook
466 'org-inlinetask-export-handler))
467 (eval-after-load "org"
468 '(add-hook 'org-font-lock-hook 'org-inlinetask-fontify))
469
470 (provide 'org-inlinetask)
471
472 ;;; org-inlinetask.el ends here