updated babel key binding documentation in manual
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-inlinetask.el
bloba6331975f559d982618777b6339519621dd08fc2
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
9 ;; This file is part of GNU Emacs.
11 ;; 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 of the License, or
15 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
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.
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.
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.
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.
53 ;; An inline task is identified solely by a minimum outline level, given
54 ;; by the variable `org-inlinetask-min-level', default 15.
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.
61 ;; As an example, here are two valid inline tasks:
63 ;; **************** TODO a small task
65 ;; and
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
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.
78 ;; This package installs one new command:
80 ;; C-c C-x t Insert a new inline task with END line
82 ;;; Code:
84 (require 'org)
86 (defgroup org-inlinetask nil
87 "Options concerning inline tasks in Org mode."
88 :tag "Org Inline Tasks"
89 :group 'org-structure)
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)))
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)
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)))
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.
137 This variable is an alist in the shape of \(BACKEND STRING OBJECTS\).
139 BACKEND is the name of the backend for the template \(ascii, html...\).
141 STRING is a format control string.
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.
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.
154 As an example, valid associations are:
156 \(html \"<ul><li>%s <p>%s</p></li></ul>\" \(heading content\)\)
158 or, with the additional package \"todonotes\" for LaTeX,
160 \(latex \"\\todo[inline]{\\textbf{\\textsf{%s %s}}\\linebreak{} %s}\"
161 '\(\(unless \(eq todo \"\"\)
162 \(format \"\\textsc{%s%s}\" todo priority\)\)
163 heading content\)\)\)")
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)
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 :type '(choice
177 (const :tag "No state" nil)
178 (string :tag "Specific state")))
180 (defun org-inlinetask-insert-task (&optional no-state)
181 "Insert an inline task.
182 If prefix arg NO-STATE is set, ignore `org-inlinetask-default-state'."
183 (interactive "P")
184 ;; Error when inside an inline task, except if point was at its very
185 ;; beginning, in which case the new inline task will be inserted
186 ;; before this one.
187 (when (and (org-inlinetask-in-task-p)
188 (not (and (org-inlinetask-at-task-p) (bolp))))
189 (error "Cannot nest inline tasks"))
190 (or (bolp) (newline))
191 (let* ((indent (if org-odd-levels-only
192 (1- (* 2 org-inlinetask-min-level))
193 org-inlinetask-min-level))
194 (indent-string (concat (make-string indent ?*) " ")))
195 (insert indent-string
196 (if (or no-state (not org-inlinetask-default-state))
197 "\n"
198 (concat org-inlinetask-default-state " \n"))
199 indent-string "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 Return point."
242 (save-match-data
243 (beginning-of-line)
244 (let* ((case-fold-search t)
245 (inlinetask-re (org-inlinetask-outline-regexp))
246 (task-end-re (concat inlinetask-re "END[ \t]*$")))
247 (cond
248 ((looking-at task-end-re) (forward-line))
249 ((looking-at inlinetask-re)
250 (forward-line)
251 (cond
252 ((looking-at task-end-re) (forward-line))
253 ((looking-at inlinetask-re))
254 ((org-inlinetask-in-task-p)
255 (re-search-forward inlinetask-re nil t)
256 (forward-line))))
257 (t (re-search-forward inlinetask-re nil t)
258 (forward-line)))
259 (point))))
261 (defun org-inlinetask-get-task-level ()
262 "Get the level of the inline task around.
263 This assumes the point is inside an inline task."
264 (save-excursion
265 (end-of-line)
266 (re-search-backward (org-inlinetask-outline-regexp) nil t)
267 (- (match-end 1) (match-beginning 1))))
269 (defun org-inlinetask-promote ()
270 "Promote the inline task at point.
271 If the task has an end part, promote it. Also, prevents level from
272 going below `org-inlinetask-min-level'."
273 (interactive)
274 (if (not (org-inlinetask-in-task-p))
275 (error "Not in an inline task")
276 (save-excursion
277 (let* ((lvl (org-inlinetask-get-task-level))
278 (next-lvl (org-get-valid-level lvl -1))
279 (diff (- next-lvl lvl))
280 (down-task (concat (make-string next-lvl ?*)))
281 beg)
282 (if (< next-lvl org-inlinetask-min-level)
283 (error "Cannot promote an inline task at minimum level")
284 (org-inlinetask-goto-beginning)
285 (setq beg (point))
286 (replace-match down-task nil t nil 1)
287 (org-inlinetask-goto-end)
288 (if (eobp) (beginning-of-line) (forward-line -1))
289 (unless (= (point) beg)
290 (replace-match down-task nil t nil 1)
291 (when org-adapt-indentation
292 (goto-char beg)
293 (org-fixup-indentation diff))))))))
295 (defun org-inlinetask-demote ()
296 "Demote the inline task at point.
297 If the task has an end part, also demote it."
298 (interactive)
299 (if (not (org-inlinetask-in-task-p))
300 (error "Not in an inline task")
301 (save-excursion
302 (let* ((lvl (org-inlinetask-get-task-level))
303 (next-lvl (org-get-valid-level lvl 1))
304 (diff (- next-lvl lvl))
305 (down-task (concat (make-string next-lvl ?*)))
306 beg)
307 (org-inlinetask-goto-beginning)
308 (setq beg (point))
309 (replace-match down-task nil t nil 1)
310 (org-inlinetask-goto-end)
311 (if (eobp) (beginning-of-line) (forward-line -1))
312 (unless (= (point) beg)
313 (replace-match down-task nil t nil 1)
314 (when org-adapt-indentation
315 (goto-char beg)
316 (org-fixup-indentation diff)))))))
318 (defvar org-export-current-backend) ; dynamically bound in org-exp.el
319 (defun org-inlinetask-export-handler ()
320 "Handle headlines with level larger or equal to `org-inlinetask-min-level'.
321 Either remove headline and meta data, or do special formatting."
322 (goto-char (point-min))
323 (let* ((keywords-re (concat "^[ \t]*" org-keyword-time-regexp))
324 (inline-re (concat (org-inlinetask-outline-regexp) ".*")))
325 (while (re-search-forward inline-re nil t)
326 (let ((headline (match-string 0))
327 (beg (point-at-bol))
328 (end (copy-marker (save-excursion
329 (org-inlinetask-goto-end) (point))))
330 content)
331 ;; Delete SCHEDULED, DEADLINE...
332 (while (re-search-forward keywords-re end t)
333 (delete-region (point-at-bol) (1+ (point-at-eol))))
334 (goto-char beg)
335 ;; Delete drawers
336 (while (re-search-forward org-drawer-regexp end t)
337 (when (save-excursion (re-search-forward org-property-end-re nil t))
338 (delete-region beg (1+ (match-end 0)))))
339 ;; Get CONTENT, if any.
340 (goto-char beg)
341 (forward-line 1)
342 (unless (= (point) end)
343 (setq content (buffer-substring (point)
344 (save-excursion (goto-char end)
345 (forward-line -1)
346 (point)))))
347 ;; Remove the task.
348 (goto-char beg)
349 (delete-region beg end)
350 (when (and org-inlinetask-export
351 (assq org-export-current-backend
352 org-inlinetask-export-templates))
353 ;; Format CONTENT, if appropriate.
354 (setq content
355 (if (not (and content (string-match "\\S-" content)))
357 ;; Ensure CONTENT has minimal indentation, a single
358 ;; newline character at its boundaries, and isn't
359 ;; protected.
360 (when (string-match "\\`\\([ \t]*\n\\)+" content)
361 (setq content (substring content (match-end 0))))
362 (when (string-match "[ \t\n]+\\'" content)
363 (setq content (substring content 0 (match-beginning 0))))
364 (org-add-props
365 (concat "\n\n" (org-remove-indentation content) "\n\n")
366 '(org-protected nil org-native-text nil))))
368 (when (string-match org-complex-heading-regexp headline)
369 (let* ((nil-to-str
370 (function
371 ;; Change nil arguments into empty strings.
372 (lambda (el) (or (eval el) ""))))
373 ;; Set up keywords provided to templates.
374 (todo (or (match-string 2 headline) ""))
375 (class (or (and (eq "" todo) "")
376 (if (member todo org-done-keywords) "done" "todo")))
377 (priority (or (match-string 3 headline) ""))
378 (heading (or (match-string 4 headline) ""))
379 (tags (or (match-string 5 headline) ""))
380 ;; Read `org-inlinetask-export-templates'.
381 (backend-spec (assq org-export-current-backend
382 org-inlinetask-export-templates))
383 (format-str (org-add-props (nth 1 backend-spec)
384 '(org-protected t org-native-text t)))
385 (tokens (cadr (nth 2 backend-spec)))
386 ;; Build export string. Ensure it won't break
387 ;; surrounding lists by giving it arbitrary high
388 ;; indentation.
389 (export-str (org-add-props
390 (eval (append '(format format-str)
391 (mapcar nil-to-str tokens)))
392 '(original-indentation 1000))))
393 ;; Ensure task starts a new paragraph.
394 (unless (or (bobp)
395 (save-excursion (forward-line -1)
396 (looking-at "[ \t]*$")))
397 (insert "\n"))
398 (insert export-str)
399 (unless (bolp) (insert "\n")))))))))
401 (defun org-inlinetask-get-current-indentation ()
402 "Get the indentation of the last non-while line above this one."
403 (save-excursion
404 (beginning-of-line 1)
405 (skip-chars-backward " \t\n")
406 (beginning-of-line 1)
407 (or (org-at-item-p)
408 (looking-at "[ \t]*"))
409 (goto-char (match-end 0))
410 (current-column)))
412 (defvar org-indent-indentation-per-level) ; defined in org-indent.el
414 (defface org-inlinetask
415 (org-compatible-face 'shadow '((t (:bold t))))
416 "Face for inlinetask headlines."
417 :group 'org-faces)
419 (defun org-inlinetask-fontify (limit)
420 "Fontify the inline tasks down to LIMIT."
421 (let* ((nstars (if org-odd-levels-only
422 (1- (* 2 (or org-inlinetask-min-level 200)))
423 (or org-inlinetask-min-level 200)))
424 (re (concat "^\\(\\*\\)\\(\\*\\{"
425 (format "%d" (- nstars 3))
426 ",\\}\\)\\(\\*\\* .*\\)"))
427 ;; Virtual indentation will add the warning face on the first
428 ;; star. Thus, in that case, only hide it.
429 (start-face (if (and (org-bound-and-true-p org-indent-mode)
430 (> org-indent-indentation-per-level 1))
431 'org-hide
432 'org-warning)))
433 (while (re-search-forward re limit t)
434 (add-text-properties (match-beginning 1) (match-end 1)
435 `(face ,start-face font-lock-fontified t))
436 (add-text-properties (match-beginning 2) (match-end 2)
437 '(face org-hide font-lock-fontified t))
438 (add-text-properties (match-beginning 3) (match-end 3)
439 '(face org-inlinetask font-lock-fontified t)))))
441 (defun org-inlinetask-toggle-visibility ()
442 "Toggle visibility of inline task at point."
443 (let ((end (save-excursion
444 (org-inlinetask-goto-end)
445 (if (bolp) (1- (point)) (point))))
446 (start (save-excursion
447 (org-inlinetask-goto-beginning)
448 (point-at-eol))))
449 (cond
450 ;; Nothing to show/hide.
451 ((= end start))
452 ;; Inlinetask was folded: expand it.
453 ((get-char-property (1+ start) 'invisible)
454 (outline-flag-region start end nil))
455 (t (outline-flag-region start end t)))))
457 (defun org-inlinetask-remove-END-maybe ()
458 "Remove an END line when present."
459 (when (looking-at (format "\\([ \t]*\n\\)*\\*\\{%d,\\}[ \t]+END[ \t]*$"
460 org-inlinetask-min-level))
461 (replace-match "")))
463 (eval-after-load "org-exp"
464 '(add-hook 'org-export-preprocess-before-backend-specifics-hook
465 'org-inlinetask-export-handler))
466 (eval-after-load "org"
467 '(add-hook 'org-font-lock-hook 'org-inlinetask-fontify))
469 (provide 'org-inlinetask)
471 ;;; org-inlinetask.el ends here