org.el: minor fix: delete trailing whitespaces.
[org-mode.git] / lisp / org-inlinetask.el
blobcd25c9577d0ba28195d108ef063ff47712445299
1 ;;; org-inlinetask.el --- Tasks independent of outline hierarchy
3 ;; Copyright (C) 2009-2011 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 ;; Version: 7.7
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Commentary:
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
41 ;; into children.
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'
47 ;; variables.
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
52 ;; `org-hide' face.
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
60 ;; number of stars.
62 ;; As an example, here are two valid inline tasks:
64 ;; **************** TODO a small task
66 ;; and
68 ;; **************** TODO another small task
69 ;; DEADLINE: <2009-03-30 Mon>
70 ;; :PROPERTIES:
71 ;; :SOMETHING: or other
72 ;; :END:
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
83 ;;; Code:
85 (require 'org)
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
99 :type '(choice
100 (const :tag "Off" nil)
101 (integer)))
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
107 :type 'boolean)
109 (defvar org-inlinetask-export-templates
110 '((html "<div class=\"inlinetask\"><b>%s%s</b><br />%s</div>"
111 '((unless (eq todo "")
112 (format "<span class=\"%s %s\">%s%s</span> "
113 class todo todo priority))
114 heading content))
115 (odt "%s" '((org-odt-format-inlinetask heading content
116 todo priority tags)))
118 (latex "\\begin\{description\}\n\\item[%s%s]~%s\\end\{description\}"
119 '((unless (eq todo "") (format "\\textsc\{%s%s\} " todo priority))
120 heading content))
121 (ascii " -- %s%s%s"
122 '((unless (eq todo "") (format "%s%s " todo priority))
123 heading
124 (unless (eq content "")
125 (format "\n ¦ %s"
126 (mapconcat 'identity (org-split-string content "\n")
127 "\n ¦ ")))))
128 (docbook "<variablelist>
129 <varlistentry>
130 <term>%s%s</term>
131 <listitem><para>%s</para></listitem>
132 </varlistentry>
133 </variablelist>"
134 '((unless (eq todo "") (format "%s%s " todo priority))
135 heading content)))
136 "Templates for inline tasks in various exporters.
138 This variable is an alist in the shape of \(BACKEND STRING OBJECTS\).
140 BACKEND is the name of the backend for the template \(ascii, html...\).
142 STRING is a format control string.
144 OBJECTS is a list of elements to be substituted into the format
145 string. They can be of any type, from a string to a form
146 returning a value (thus allowing conditional insertion). A nil
147 object will be substituted as the empty string. Obviously, there
148 must be at least as many objects as %-sequences in the format
149 string.
151 Moreover, the following special keywords are provided: `todo',
152 `priority', `heading', `content', `tags'. If some of them are not
153 defined in an inline task, their value is the empty string.
155 As an example, valid associations are:
157 \(html \"<ul><li>%s <p>%s</p></li></ul>\" \(heading content\)\)
159 or, with the additional package \"todonotes\" for LaTeX,
161 \(latex \"\\todo[inline]{\\textbf{\\textsf{%s %s}}\\linebreak{} %s}\"
162 '\(\(unless \(eq todo \"\"\)
163 \(format \"\\textsc{%s%s}\" todo priority\)\)
164 heading content\)\)\)")
166 (defvar org-odd-levels-only)
167 (defvar org-keyword-time-regexp)
168 (defvar org-drawer-regexp)
169 (defvar org-complex-heading-regexp)
170 (defvar org-property-end-re)
172 (defcustom org-inlinetask-default-state nil
173 "Non-nil means make inline tasks have a TODO keyword initially.
174 This should be the state `org-inlinetask-insert-task' should use by
175 default, or nil of no state should be assigned."
176 :group 'org-inlinetask
177 :type '(choice
178 (const :tag "No state" nil)
179 (string :tag "Specific state")))
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 org-inlinetask-min-level))
193 (if org-odd-levels-only
194 (setq indent (- (* 2 indent) 1)))
195 (insert (make-string indent ?*)
196 (if (or no-state (not org-inlinetask-default-state))
197 " \n"
198 (concat " " org-inlinetask-default-state " \n"))
199 (make-string indent ?*) " END\n"))
200 (end-of-line -1))
201 (define-key org-mode-map "\C-c\C-xt" 'org-inlinetask-insert-task)
203 (defun org-inlinetask-outline-regexp ()
204 "Return string matching an inline task heading.
205 The number of levels is controlled by `org-inlinetask-min-level'."
206 (let ((nstars (if org-odd-levels-only
207 (1- (* org-inlinetask-min-level 2))
208 org-inlinetask-min-level)))
209 (format "^\\(\\*\\{%d,\\}\\)[ \t]+" nstars)))
211 (defun org-inlinetask-at-task-p ()
212 "Return true if point is at beginning of an inline task."
213 (save-excursion
214 (beginning-of-line)
215 (and (looking-at (concat (org-inlinetask-outline-regexp) "\\(.*\\)"))
216 (not (string-match "^end[ \t]*$" (downcase (match-string 2)))))))
218 (defun org-inlinetask-in-task-p ()
219 "Return true if point is inside an inline task."
220 (save-excursion
221 (beginning-of-line)
222 (let* ((case-fold-search t)
223 (stars-re (org-inlinetask-outline-regexp))
224 (task-beg-re (concat stars-re "\\(?:.*\\)"))
225 (task-end-re (concat stars-re "END[ \t]*$")))
226 (or (org-looking-at-p task-beg-re)
227 (and (re-search-forward "^\\*+[ \t]+" nil t)
228 (progn (beginning-of-line) (org-looking-at-p task-end-re)))))))
230 (defun org-inlinetask-goto-beginning ()
231 "Go to the beginning of the inline task at point."
232 (end-of-line)
233 (let ((case-fold-search t)
234 (inlinetask-re (org-inlinetask-outline-regexp)))
235 (re-search-backward inlinetask-re nil t)
236 (when (org-looking-at-p (concat inlinetask-re "END[ \t]*$"))
237 (re-search-backward inlinetask-re nil t))))
239 (defun org-inlinetask-goto-end ()
240 "Go to the end of the inline task at point."
241 (beginning-of-line)
242 (let ((case-fold-search t)
243 (inlinetask-re (org-inlinetask-outline-regexp)))
244 (cond
245 ((org-looking-at-p (concat inlinetask-re "END[ \t]*$"))
246 (forward-line 1))
247 ((org-looking-at-p inlinetask-re)
248 (forward-line 1)
249 (when (org-inlinetask-in-task-p)
250 (re-search-forward inlinetask-re nil t)
251 (forward-line 1)))
253 (re-search-forward inlinetask-re nil t)
254 (forward-line 1)))))
256 (defun org-inlinetask-get-task-level ()
257 "Get the level of the inline task around.
258 This assumes the point is inside an inline task."
259 (save-excursion
260 (end-of-line)
261 (re-search-backward (org-inlinetask-outline-regexp) nil t)
262 (- (match-end 1) (match-beginning 1))))
264 (defun org-inlinetask-promote ()
265 "Promote the inline task at point.
266 If the task has an end part, promote it. Also, prevents level from
267 going below `org-inlinetask-min-level'."
268 (interactive)
269 (if (not (org-inlinetask-in-task-p))
270 (error "Not in an inline task")
271 (save-excursion
272 (let* ((lvl (org-inlinetask-get-task-level))
273 (next-lvl (org-get-valid-level lvl -1))
274 (diff (- next-lvl lvl))
275 (down-task (concat (make-string next-lvl ?*)))
276 beg)
277 (if (< next-lvl org-inlinetask-min-level)
278 (error "Cannot promote an inline task at minimum level")
279 (org-inlinetask-goto-beginning)
280 (setq beg (point))
281 (replace-match down-task nil t nil 1)
282 (org-inlinetask-goto-end)
283 (if (eobp) (beginning-of-line) (forward-line -1))
284 (unless (= (point) beg)
285 (replace-match down-task nil t nil 1)
286 (when org-adapt-indentation
287 (goto-char beg)
288 (org-fixup-indentation diff))))))))
290 (defun org-inlinetask-demote ()
291 "Demote the inline task at point.
292 If the task has an end part, also demote it."
293 (interactive)
294 (if (not (org-inlinetask-in-task-p))
295 (error "Not in an inline task")
296 (save-excursion
297 (let* ((lvl (org-inlinetask-get-task-level))
298 (next-lvl (org-get-valid-level lvl 1))
299 (diff (- next-lvl lvl))
300 (down-task (concat (make-string next-lvl ?*)))
301 beg)
302 (org-inlinetask-goto-beginning)
303 (setq beg (point))
304 (replace-match down-task nil t nil 1)
305 (org-inlinetask-goto-end)
306 (if (eobp) (beginning-of-line) (forward-line -1))
307 (unless (= (point) beg)
308 (replace-match down-task nil t nil 1)
309 (when org-adapt-indentation
310 (goto-char beg)
311 (org-fixup-indentation diff)))))))
313 (defvar org-export-current-backend) ; dynamically bound in org-exp.el
314 (defun org-inlinetask-export-handler ()
315 "Handle headlines with level larger or equal to `org-inlinetask-min-level'.
316 Either remove headline and meta data, or do special formatting."
317 (goto-char (point-min))
318 (let* ((keywords-re (concat "^[ \t]*" org-keyword-time-regexp))
319 (inline-re (concat (org-inlinetask-outline-regexp) ".*")))
320 (while (re-search-forward inline-re nil t)
321 (let ((headline (match-string 0))
322 (beg (point-at-bol))
323 (end (copy-marker (save-excursion
324 (org-inlinetask-goto-end) (point))))
325 content)
326 ;; Delete SCHEDULED, DEADLINE...
327 (while (re-search-forward keywords-re end t)
328 (delete-region (point-at-bol) (1+ (point-at-eol))))
329 (goto-char beg)
330 ;; Delete drawers
331 (while (re-search-forward org-drawer-regexp end t)
332 (when (save-excursion (re-search-forward org-property-end-re nil t))
333 (delete-region beg (1+ (match-end 0)))))
334 ;; Get CONTENT, if any.
335 (goto-char beg)
336 (forward-line 1)
337 (unless (= (point) end)
338 (setq content (buffer-substring (point)
339 (save-excursion (goto-char end)
340 (forward-line -1)
341 (point)))))
342 ;; Remove the task.
343 (goto-char beg)
344 (delete-region beg end)
345 (when (and org-inlinetask-export
346 (assq org-export-current-backend
347 org-inlinetask-export-templates))
348 ;; Format CONTENT, if appropriate.
349 (setq content
350 (if (not (and content (string-match "\\S-" content)))
352 ;; Ensure CONTENT has minimal indentation, a single
353 ;; newline character at its boundaries, and isn't
354 ;; protected.
355 (when (string-match "\\`\\([ \t]*\n\\)+" content)
356 (setq content (substring content (match-end 0))))
357 (when (string-match "[ \t\n]+\\'" content)
358 (setq content (substring content 0 (match-beginning 0))))
359 (org-add-props
360 (concat "\n\n" (org-remove-indentation content) "\n\n")
361 '(org-protected nil org-native-text nil))))
363 (when (string-match org-complex-heading-regexp headline)
364 (let* ((nil-to-str
365 (function
366 ;; Change nil arguments into empty strings.
367 (lambda (el) (or (eval el) ""))))
368 ;; Set up keywords provided to templates.
369 (todo (or (match-string 2 headline) ""))
370 (class (or (and (eq "" todo) "")
371 (if (member todo org-done-keywords) "done" "todo")))
372 (priority (or (match-string 3 headline) ""))
373 (heading (or (match-string 4 headline) ""))
374 (tags (or (match-string 5 headline) ""))
375 ;; Read `org-inlinetask-export-templates'.
376 (backend-spec (assq org-export-current-backend
377 org-inlinetask-export-templates))
378 (format-str (org-add-props (nth 1 backend-spec)
379 '(org-protected t org-native-text t)))
380 (tokens (cadr (nth 2 backend-spec)))
381 ;; Build export string. Ensure it won't break
382 ;; surrounding lists by giving it arbitrary high
383 ;; indentation.
384 (export-str (org-add-props
385 (eval (append '(format format-str)
386 (mapcar nil-to-str tokens)))
387 '(original-indentation 1000))))
388 ;; Ensure task starts a new paragraph.
389 (unless (or (bobp)
390 (save-excursion (forward-line -1)
391 (looking-at "[ \t]*$")))
392 (insert "\n"))
393 (insert export-str)
394 (unless (bolp) (insert "\n")))))))))
396 (defun org-inlinetask-get-current-indentation ()
397 "Get the indentation of the last non-while line above this one."
398 (save-excursion
399 (beginning-of-line 1)
400 (skip-chars-backward " \t\n")
401 (beginning-of-line 1)
402 (or (org-at-item-p)
403 (looking-at "[ \t]*"))
404 (goto-char (match-end 0))
405 (current-column)))
407 (defun org-inlinetask-fontify (limit)
408 "Fontify the inline tasks."
409 (let* ((nstars (if org-odd-levels-only
410 (1- (* 2 (or org-inlinetask-min-level 200)))
411 (or org-inlinetask-min-level 200)))
412 (re (concat "^\\(\\*\\)\\(\\*\\{"
413 (format "%d" (- nstars 3))
414 ",\\}\\)\\(\\*\\* .*\\)")))
415 (while (re-search-forward re limit t)
416 (add-text-properties (match-beginning 1) (match-end 1)
417 '(face org-warning font-lock-fontified t))
418 (add-text-properties (match-beginning 2) (match-end 2)
419 '(face org-hide font-lock-fontified t))
420 (add-text-properties (match-beginning 3) (match-end 3)
421 '(face shadow font-lock-fontified t)))))
423 (defun org-inlinetask-toggle-visibility ()
424 "Toggle visibility of inline task at point."
425 (let ((end (save-excursion
426 (org-inlinetask-goto-end)
427 (if (bolp) (1- (point)) (point))))
428 (start (save-excursion
429 (org-inlinetask-goto-beginning)
430 (point-at-eol))))
431 (cond
432 ;; Nothing to show/hide.
433 ((= end start))
434 ;; Inlinetask was folded: expand it.
435 ((get-char-property (1+ start) 'invisible)
436 (outline-flag-region start end nil))
437 (t (outline-flag-region start end t)))))
439 (defun org-inlinetask-remove-END-maybe ()
440 "Remove an END line when present."
441 (when (looking-at (format "\\([ \t]*\n\\)*\\*\\{%d,\\}[ \t]+END[ \t]*$"
442 org-inlinetask-min-level))
443 (replace-match "")))
445 (eval-after-load "org-exp"
446 '(add-hook 'org-export-preprocess-before-backend-specifics-hook
447 'org-inlinetask-export-handler))
448 (eval-after-load "org"
449 '(add-hook 'org-font-lock-hook 'org-inlinetask-fontify))
451 (provide 'org-inlinetask)
453 ;;; org-inlinetask.el ends here