More details about LaTeX setup
[org-mode.git] / lisp / org.el
blob19bb2a5806c2b67eecbb74edad8464520c1fc707
1 ;;; org.el --- Outline-based notes management and organizer
2 ;; Carstens outline-mode for keeping track of everything.
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
5 ;;
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 6.34trans
11 ;; This file is part of GNU Emacs.
13 ;; 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/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
30 ;; project planning with a fast and effective plain-text system.
32 ;; Org-mode develops organizational tasks around NOTES files that contain
33 ;; information about projects as plain text. Org-mode is implemented on
34 ;; top of outline-mode, which makes it possible to keep the content of
35 ;; large files well structured. Visibility cycling and structure editing
36 ;; help to work with the tree. Tables are easily created with a built-in
37 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
38 ;; and scheduling. It dynamically compiles entries into an agenda that
39 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
40 ;; Plain text URL-like links connect to websites, emails, Usenet
41 ;; messages, BBDB entries, and any files related to the projects. For
42 ;; printing and sharing of notes, an Org-mode file can be exported as a
43 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
44 ;; iCalendar file. It can also serve as a publishing tool for a set of
45 ;; linked webpages.
47 ;; Installation and Activation
48 ;; ---------------------------
49 ;; See the corresponding sections in the manual at
51 ;; http://orgmode.org/org.html#Installation
53 ;; Documentation
54 ;; -------------
55 ;; The documentation of Org-mode can be found in the TeXInfo file. The
56 ;; distribution also contains a PDF version of it. At the homepage of
57 ;; Org-mode, you can read the same text online as HTML. There is also an
58 ;; excellent reference card made by Philip Rooke. This card can be found
59 ;; in the etc/ directory of Emacs 22.
61 ;; A list of recent changes can be found at
62 ;; http://orgmode.org/Changes.html
64 ;;; Code:
66 (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
67 (defvar org-table-formula-constants-local nil
68 "Local version of `org-table-formula-constants'.")
69 (make-variable-buffer-local 'org-table-formula-constants-local)
71 ;;;; Require other packages
73 (eval-when-compile
74 (require 'cl)
75 (require 'gnus-sum)
76 (require 'calendar))
77 ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
78 ;; the file noutline.el being loaded.
79 (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
80 ;; We require noutline, which might be provided in outline.el
81 (require 'outline) (require 'noutline)
82 ;; Other stuff we need.
83 (require 'time-date)
84 (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
85 (require 'easymenu)
87 (require 'org-macs)
88 (require 'org-entities)
89 (require 'org-compat)
90 (require 'org-faces)
91 (require 'org-list)
92 (require 'org-src)
93 (require 'org-footnote)
95 ;;;; Customization variables
97 ;;; Version
99 (defconst org-version "6.34trans"
100 "The version number of the file org.el.")
102 (defun org-version (&optional here)
103 "Show the org-mode version in the echo area.
104 With prefix arg HERE, insert it at point."
105 (interactive "P")
106 (let* ((origin default-directory)
107 (version org-version)
108 (git-version)
109 (dir (concat (file-name-directory (locate-library "org")) "../" )))
110 (when (and (file-exists-p (expand-file-name ".git" dir))
111 (executable-find "git"))
112 (unwind-protect
113 (progn
114 (cd dir)
115 (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
116 (with-current-buffer "*Shell Command Output*"
117 (goto-char (point-min))
118 (setq git-version (buffer-substring (point) (point-at-eol))))
119 (subst-char-in-string ?- ?. git-version t)
120 (when (string-match "\\S-"
121 (shell-command-to-string
122 "git diff-index --name-only HEAD --"))
123 (setq git-version (concat git-version ".dirty")))
124 (setq version (concat version " (" git-version ")"))))
125 (cd origin)))
126 (setq version (format "Org-mode version %s" version))
127 (if here (insert version))
128 (message version)))
130 ;;; Compatibility constants
132 ;;; The custom variables
134 (defgroup org nil
135 "Outline-based notes management and organizer."
136 :tag "Org"
137 :group 'outlines
138 :group 'calendar)
140 (defcustom org-mode-hook nil
141 "Mode hook for Org-mode, run after the mode was turned on."
142 :group 'org
143 :type 'hook)
145 (defcustom org-load-hook nil
146 "Hook that is run after org.el has been loaded."
147 :group 'org
148 :type 'hook)
150 (defvar org-modules) ; defined below
151 (defvar org-modules-loaded nil
152 "Have the modules been loaded already?")
154 (defun org-load-modules-maybe (&optional force)
155 "Load all extensions listed in `org-modules'."
156 (when (or force (not org-modules-loaded))
157 (mapc (lambda (ext)
158 (condition-case nil (require ext)
159 (error (message "Problems while trying to load feature `%s'" ext))))
160 org-modules)
161 (setq org-modules-loaded t)))
163 (defun org-set-modules (var value)
164 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
165 (set var value)
166 (when (featurep 'org)
167 (org-load-modules-maybe 'force)))
169 (when (org-bound-and-true-p org-modules)
170 (let ((a (member 'org-infojs org-modules)))
171 (and a (setcar a 'org-jsinfo))))
173 (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
174 "Modules that should always be loaded together with org.el.
175 If a description starts with <C>, the file is not part of Emacs
176 and loading it will require that you have downloaded and properly installed
177 the org-mode distribution.
179 You can also use this system to load external packages (i.e. neither Org
180 core modules, nor modules from the CONTRIB directory). Just add symbols
181 to the end of the list. If the package is called org-xyz.el, then you need
182 to add the symbol `xyz', and the package must have a call to
184 (provide 'org-xyz)"
185 :group 'org
186 :set 'org-set-modules
187 :type
188 '(set :greedy t
189 (const :tag " bbdb: Links to BBDB entries" org-bbdb)
190 (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
191 (const :tag " crypt: Encryption of subtrees" org-crypt)
192 (const :tag " ctags: Access to Emacs tags with links" org-ctags)
193 (const :tag " docview: Links to doc-view buffers" org-docview)
194 (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
195 (const :tag " id: Global IDs for identifying entries" org-id)
196 (const :tag " info: Links to Info nodes" org-info)
197 (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
198 (const :tag " habit: Track your consistency with habits" org-habit)
199 (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
200 (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
201 (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
202 (const :tag " mew Links to Mew folders/messages" org-mew)
203 (const :tag " mhe: Links to MHE folders/messages" org-mhe)
204 (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
205 (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
206 (const :tag " vm: Links to VM folders/messages" org-vm)
207 (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
208 (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
209 (const :tag " mouse: Additional mouse support" org-mouse)
211 (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
212 (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
213 (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
214 (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
215 (const :tag "C collector: Collect properties into tables" org-collector)
216 (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
217 (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
218 (const :tag "C eval: Include command output as text" org-eval)
219 (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
220 (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
221 (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
222 (const :tag "C git-link: Provide org links to specific file version" org-git-link)
223 (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
225 (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
227 (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
228 (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
229 (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
230 (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
231 (const :tag "C man: Support for links to manpages in Org-mode" org-man)
232 (const :tag "C mtags: Support for muse-like tags" org-mtags)
233 (const :tag "C panel: Simple routines for us with bad memory" org-panel)
234 (const :tag "C registry: A registry for Org-mode links" org-registry)
235 (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
236 (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
237 (const :tag "C secretary: Team management with org-mode" org-secretary)
238 (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
239 (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
240 (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
241 (const :tag "C track: Keep up with Org-mode development" org-track)
242 (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
244 (defcustom org-support-shift-select nil
245 "Non-nil means make shift-cursor commands select text when possible.
247 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
248 selecting a region, or enlarge thusly regions started in this way.
249 In Org-mode, in special contexts, these same keys are used for other
250 purposes, important enough to compete with shift selection. Org tries
251 to balance these needs by supporting `shift-select-mode' outside these
252 special contexts, under control of this variable.
254 The default of this variable is nil, to avoid confusing behavior. Shifted
255 cursor keys will then execute Org commands in the following contexts:
256 - on a headline, changing TODO state (left/right) and priority (up/down)
257 - on a time stamp, changing the time
258 - in a plain list item, changing the bullet type
259 - in a property definition line, switching between allowed values
260 - in the BEGIN line of a clock table (changing the time block).
261 Outside these contexts, the commands will throw an error.
263 When this variable is t and the cursor is not in a special context,
264 Org-mode will support shift-selection for making and enlarging regions.
265 To make this more effective, the bullet cycling will no longer happen
266 anywhere in an item line, but only if the cursor is exactly on the bullet.
268 If you set this variable to the symbol `always', then the keys
269 will not be special in headlines, property lines, and item lines, to make
270 shift selection work there as well. If this is what you want, you can
271 use the following alternative commands: `C-c C-t' and `C-c ,' to
272 change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
273 TODO sets, `C-c -' to cycle item bullet types, and properties can be
274 edited by hand or in column view.
276 However, when the cursor is on a timestamp, shift-cursor commands
277 will still edit the time stamp - this is just too good to give up.
279 XEmacs user should have this variable set to nil, because shift-select-mode
280 is Emacs 23 only."
281 :group 'org
282 :type '(choice
283 (const :tag "Never" nil)
284 (const :tag "When outside special context" t)
285 (const :tag "Everywhere except timestamps" always)))
287 (defgroup org-startup nil
288 "Options concerning startup of Org-mode."
289 :tag "Org Startup"
290 :group 'org)
292 (defcustom org-startup-folded t
293 "Non-nil means entering Org-mode will switch to OVERVIEW.
294 This can also be configured on a per-file basis by adding one of
295 the following lines anywhere in the buffer:
297 #+STARTUP: fold (or `overview', this is equivalent)
298 #+STARTUP: nofold (or `showall', this is equivalent)
299 #+STARTUP: content
300 #+STARTUP: showeverything"
301 :group 'org-startup
302 :type '(choice
303 (const :tag "nofold: show all" nil)
304 (const :tag "fold: overview" t)
305 (const :tag "content: all headlines" content)
306 (const :tag "show everything, even drawers" showeverything)))
308 (defcustom org-startup-truncated t
309 "Non-nil means entering Org-mode will set `truncate-lines'.
310 This is useful since some lines containing links can be very long and
311 uninteresting. Also tables look terrible when wrapped."
312 :group 'org-startup
313 :type 'boolean)
315 (defcustom org-startup-indented nil
316 "Non-nil means turn on `org-indent-mode' on startup.
317 This can also be configured on a per-file basis by adding one of
318 the following lines anywhere in the buffer:
320 #+STARTUP: indent
321 #+STARTUP: noindent"
322 :group 'org-structure
323 :type '(choice
324 (const :tag "Not" nil)
325 (const :tag "Globally (slow on startup in large files)" t)))
327 (defcustom org-startup-with-beamer-mode nil
328 "Non-nil means turn on `org-beamer-mode' on startup.
329 This can also be configured on a per-file basis by adding one of
330 the following lines anywhere in the buffer:
332 #+STARTUP: beamer"
333 :group 'org-startup
334 :type 'boolean)
336 (defcustom org-startup-align-all-tables nil
337 "Non-nil means align all tables when visiting a file.
338 This is useful when the column width in tables is forced with <N> cookies
339 in table fields. Such tables will look correct only after the first re-align.
340 This can also be configured on a per-file basis by adding one of
341 the following lines anywhere in the buffer:
342 #+STARTUP: align
343 #+STARTUP: noalign"
344 :group 'org-startup
345 :type 'boolean)
347 (defcustom org-insert-mode-line-in-empty-file nil
348 "Non-nil means insert the first line setting Org-mode in empty files.
349 When the function `org-mode' is called interactively in an empty file, this
350 normally means that the file name does not automatically trigger Org-mode.
351 To ensure that the file will always be in Org-mode in the future, a
352 line enforcing Org-mode will be inserted into the buffer, if this option
353 has been set."
354 :group 'org-startup
355 :type 'boolean)
357 (defcustom org-replace-disputed-keys nil
358 "Non-nil means use alternative key bindings for some keys.
359 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
360 These keys are also used by other packages like shift-selection-mode'
361 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
362 If you want to use Org-mode together with one of these other modes,
363 or more generally if you would like to move some Org-mode commands to
364 other keys, set this variable and configure the keys with the variable
365 `org-disputed-keys'.
367 This option is only relevant at load-time of Org-mode, and must be set
368 *before* org.el is loaded. Changing it requires a restart of Emacs to
369 become effective."
370 :group 'org-startup
371 :type 'boolean)
373 (defcustom org-use-extra-keys nil
374 "Non-nil means use extra key sequence definitions for certain
375 commands. This happens automatically if you run XEmacs or if
376 window-system is nil. This variable lets you do the same
377 manually. You must set it before loading org.
379 Example: on Carbon Emacs 22 running graphically, with an external
380 keyboard on a Powerbook, the default way of setting M-left might
381 not work for either Alt or ESC. Setting this variable will make
382 it work for ESC."
383 :group 'org-startup
384 :type 'boolean)
386 (if (fboundp 'defvaralias)
387 (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
389 (defcustom org-disputed-keys
390 '(([(shift up)] . [(meta p)])
391 ([(shift down)] . [(meta n)])
392 ([(shift left)] . [(meta -)])
393 ([(shift right)] . [(meta +)])
394 ([(control shift right)] . [(meta shift +)])
395 ([(control shift left)] . [(meta shift -)]))
396 "Keys for which Org-mode and other modes compete.
397 This is an alist, cars are the default keys, second element specifies
398 the alternative to use when `org-replace-disputed-keys' is t.
400 Keys can be specified in any syntax supported by `define-key'.
401 The value of this option takes effect only at Org-mode's startup,
402 therefore you'll have to restart Emacs to apply it after changing."
403 :group 'org-startup
404 :type 'alist)
406 (defun org-key (key)
407 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
408 Or return the original if not disputed."
409 (if org-replace-disputed-keys
410 (let* ((nkey (key-description key))
411 (x (org-find-if (lambda (x)
412 (equal (key-description (car x)) nkey))
413 org-disputed-keys)))
414 (if x (cdr x) key))
415 key))
417 (defun org-find-if (predicate seq)
418 (catch 'exit
419 (while seq
420 (if (funcall predicate (car seq))
421 (throw 'exit (car seq))
422 (pop seq)))))
424 (defun org-defkey (keymap key def)
425 "Define a key, possibly translated, as returned by `org-key'."
426 (define-key keymap (org-key key) def))
428 (defcustom org-ellipsis nil
429 "The ellipsis to use in the Org-mode outline.
430 When nil, just use the standard three dots. When a string, use that instead,
431 When a face, use the standard 3 dots, but with the specified face.
432 The change affects only Org-mode (which will then use its own display table).
433 Changing this requires executing `M-x org-mode' in a buffer to become
434 effective."
435 :group 'org-startup
436 :type '(choice (const :tag "Default" nil)
437 (face :tag "Face" :value org-warning)
438 (string :tag "String" :value "...#")))
440 (defvar org-display-table nil
441 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
443 (defgroup org-keywords nil
444 "Keywords in Org-mode."
445 :tag "Org Keywords"
446 :group 'org)
448 (defcustom org-deadline-string "DEADLINE:"
449 "String to mark deadline entries.
450 A deadline is this string, followed by a time stamp. Should be a word,
451 terminated by a colon. You can insert a schedule keyword and
452 a timestamp with \\[org-deadline].
453 Changes become only effective after restarting Emacs."
454 :group 'org-keywords
455 :type 'string)
457 (defcustom org-scheduled-string "SCHEDULED:"
458 "String to mark scheduled TODO entries.
459 A schedule is this string, followed by a time stamp. Should be a word,
460 terminated by a colon. You can insert a schedule keyword and
461 a timestamp with \\[org-schedule].
462 Changes become only effective after restarting Emacs."
463 :group 'org-keywords
464 :type 'string)
466 (defcustom org-closed-string "CLOSED:"
467 "String used as the prefix for timestamps logging closing a TODO entry."
468 :group 'org-keywords
469 :type 'string)
471 (defcustom org-clock-string "CLOCK:"
472 "String used as prefix for timestamps clocking work hours on an item."
473 :group 'org-keywords
474 :type 'string)
476 (defcustom org-comment-string "COMMENT"
477 "Entries starting with this keyword will never be exported.
478 An entry can be toggled between COMMENT and normal with
479 \\[org-toggle-comment].
480 Changes become only effective after restarting Emacs."
481 :group 'org-keywords
482 :type 'string)
484 (defcustom org-quote-string "QUOTE"
485 "Entries starting with this keyword will be exported in fixed-width font.
486 Quoting applies only to the text in the entry following the headline, and does
487 not extend beyond the next headline, even if that is lower level.
488 An entry can be toggled between QUOTE and normal with
489 \\[org-toggle-fixed-width-section]."
490 :group 'org-keywords
491 :type 'string)
493 (defconst org-repeat-re
494 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
495 "Regular expression for specifying repeated events.
496 After a match, group 1 contains the repeat expression.")
498 (defgroup org-structure nil
499 "Options concerning the general structure of Org-mode files."
500 :tag "Org Structure"
501 :group 'org)
503 (defgroup org-reveal-location nil
504 "Options about how to make context of a location visible."
505 :tag "Org Reveal Location"
506 :group 'org-structure)
508 (defconst org-context-choice
509 '(choice
510 (const :tag "Always" t)
511 (const :tag "Never" nil)
512 (repeat :greedy t :tag "Individual contexts"
513 (cons
514 (choice :tag "Context"
515 (const agenda)
516 (const org-goto)
517 (const occur-tree)
518 (const tags-tree)
519 (const link-search)
520 (const mark-goto)
521 (const bookmark-jump)
522 (const isearch)
523 (const default))
524 (boolean))))
525 "Contexts for the reveal options.")
527 (defcustom org-show-hierarchy-above '((default . t))
528 "Non-nil means show full hierarchy when revealing a location.
529 Org-mode often shows locations in an org-mode file which might have
530 been invisible before. When this is set, the hierarchy of headings
531 above the exposed location is shown.
532 Turning this off for example for sparse trees makes them very compact.
533 Instead of t, this can also be an alist specifying this option for different
534 contexts. Valid contexts are
535 agenda when exposing an entry from the agenda
536 org-goto when using the command `org-goto' on key C-c C-j
537 occur-tree when using the command `org-occur' on key C-c /
538 tags-tree when constructing a sparse tree based on tags matches
539 link-search when exposing search matches associated with a link
540 mark-goto when exposing the jump goal of a mark
541 bookmark-jump when exposing a bookmark location
542 isearch when exiting from an incremental search
543 default default for all contexts not set explicitly"
544 :group 'org-reveal-location
545 :type org-context-choice)
547 (defcustom org-show-following-heading '((default . nil))
548 "Non-nil means show following heading when revealing a location.
549 Org-mode often shows locations in an org-mode file which might have
550 been invisible before. When this is set, the heading following the
551 match is shown.
552 Turning this off for example for sparse trees makes them very compact,
553 but makes it harder to edit the location of the match. In such a case,
554 use the command \\[org-reveal] to show more context.
555 Instead of t, this can also be an alist specifying this option for different
556 contexts. See `org-show-hierarchy-above' for valid contexts."
557 :group 'org-reveal-location
558 :type org-context-choice)
560 (defcustom org-show-siblings '((default . nil) (isearch t))
561 "Non-nil means show all sibling heading when revealing a location.
562 Org-mode often shows locations in an org-mode file which might have
563 been invisible before. When this is set, the sibling of the current entry
564 heading are all made visible. If `org-show-hierarchy-above' is t,
565 the same happens on each level of the hierarchy above the current entry.
567 By default this is on for the isearch context, off for all other contexts.
568 Turning this off for example for sparse trees makes them very compact,
569 but makes it harder to edit the location of the match. In such a case,
570 use the command \\[org-reveal] to show more context.
571 Instead of t, this can also be an alist specifying this option for different
572 contexts. See `org-show-hierarchy-above' for valid contexts."
573 :group 'org-reveal-location
574 :type org-context-choice)
576 (defcustom org-show-entry-below '((default . nil))
577 "Non-nil means show the entry below a headline when revealing a location.
578 Org-mode often shows locations in an org-mode file which might have
579 been invisible before. When this is set, the text below the headline that is
580 exposed is also shown.
582 By default this is off for all contexts.
583 Instead of t, this can also be an alist specifying this option for different
584 contexts. See `org-show-hierarchy-above' for valid contexts."
585 :group 'org-reveal-location
586 :type org-context-choice)
588 (defcustom org-indirect-buffer-display 'other-window
589 "How should indirect tree buffers be displayed?
590 This applies to indirect buffers created with the commands
591 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
592 Valid values are:
593 current-window Display in the current window
594 other-window Just display in another window.
595 dedicated-frame Create one new frame, and re-use it each time.
596 new-frame Make a new frame each time. Note that in this case
597 previously-made indirect buffers are kept, and you need to
598 kill these buffers yourself."
599 :group 'org-structure
600 :group 'org-agenda-windows
601 :type '(choice
602 (const :tag "In current window" current-window)
603 (const :tag "In current frame, other window" other-window)
604 (const :tag "Each time a new frame" new-frame)
605 (const :tag "One dedicated frame" dedicated-frame)))
607 (defcustom org-use-speed-commands nil
608 "Non-nil means activate single letter commands at beginning of a headline.
609 This may also be a function to test for appropriate locations where speed
610 commands should be active."
611 :group 'org-structure
612 :type '(choice
613 (const :tag "Never" nil)
614 (const :tag "At beginning of headline stars" t)
615 (function)))
617 (defcustom org-speed-commands-user nil
618 "Alist of additional speed commands.
619 This list will be checked before `org-speed-commands-default'
620 when the variable `org-use-speed-commands' is non-nil
621 and when the cursor is at the beginning of a headline.
622 The car if each entry is a string with a single letter, which must
623 be assigned to `self-insert-command' in the global map.
624 The cdr is either a command to be called interactively, a function
625 to be called, or a form to be evaluated.
626 An entry that is just a list with a single string will be interpreted
627 as a descriptive headline that will be added when listing the speed
628 copmmands in the Help buffer using the `?' speed command."
629 :group 'org-structure
630 :type '(repeat :value ("k" . ignore)
631 (choice :value ("k" . ignore)
632 (list :tag "Descriptive Headline" (string :tag "Headline"))
633 (cons :tag "Letter and Command"
634 (string :tag "Command letter")
635 (choice
636 (function)
637 (sexp))))))
639 (defgroup org-cycle nil
640 "Options concerning visibility cycling in Org-mode."
641 :tag "Org Cycle"
642 :group 'org-structure)
644 (defcustom org-cycle-skip-children-state-if-no-children t
645 "Non-nil means skip CHILDREN state in entries that don't have any."
646 :group 'org-cycle
647 :type 'boolean)
649 (defcustom org-cycle-max-level nil
650 "Maximum level which should still be subject to visibility cycling.
651 Levels higher than this will, for cycling, be treated as text, not a headline.
652 When `org-odd-levels-only' is set, a value of N in this variable actually
653 means 2N-1 stars as the limiting headline.
654 When nil, cycle all levels.
655 Note that the limiting level of cycling is also influenced by
656 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
657 `org-inlinetask-min-level' is, cycling will be limited to levels one less
658 than its value."
659 :group 'org-cycle
660 :type '(choice
661 (const :tag "No limit" nil)
662 (integer :tag "Maximum level")))
664 (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
665 "Names of drawers. Drawers are not opened by cycling on the headline above.
666 Drawers only open with a TAB on the drawer line itself. A drawer looks like
667 this:
668 :DRAWERNAME:
669 .....
670 :END:
671 The drawer \"PROPERTIES\" is special for capturing properties through
672 the property API.
674 Drawers can be defined on the per-file basis with a line like:
676 #+DRAWERS: HIDDEN STATE PROPERTIES"
677 :group 'org-structure
678 :group 'org-cycle
679 :type '(repeat (string :tag "Drawer Name")))
681 (defcustom org-hide-block-startup nil
682 "Non-nil means entering Org-mode will fold all blocks.
683 This can also be set in on a per-file basis with
685 #+STARTUP: hideblocks
686 #+STARTUP: showblocks"
687 :group 'org-startup
688 :group 'org-cycle
689 :type 'boolean)
691 (defcustom org-cycle-global-at-bob nil
692 "Cycle globally if cursor is at beginning of buffer and not at a headline.
693 This makes it possible to do global cycling without having to use S-TAB or
694 C-u TAB. For this special case to work, the first line of the buffer
695 must not be a headline - it may be empty or some other text. When used in
696 this way, `org-cycle-hook' is disables temporarily, to make sure the
697 cursor stays at the beginning of the buffer.
698 When this option is nil, don't do anything special at the beginning
699 of the buffer."
700 :group 'org-cycle
701 :type 'boolean)
703 (defcustom org-cycle-level-after-item/entry-creation t
704 "Non-nil means cycle entry level or item indentation in new empty entries.
706 When the cursor is at the end of an empty headline, i.e with only stars
707 and maybe a TODO keyword, TAB will then switch the entry to become a child,
708 and then all possible anchestor states, before returning to the original state.
709 This makes data entry extremely fast: M-RET to create a new headline,
710 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
712 When the cursor is at the end of an empty plain list item, one TAB will
713 make it a subitem, two or more tabs will back up to make this an item
714 higher up in the item hierarchy."
715 :group 'org-cycle
716 :type 'boolean)
718 (defcustom org-cycle-emulate-tab t
719 "Where should `org-cycle' emulate TAB.
720 nil Never
721 white Only in completely white lines
722 whitestart Only at the beginning of lines, before the first non-white char
723 t Everywhere except in headlines
724 exc-hl-bol Everywhere except at the start of a headline
725 If TAB is used in a place where it does not emulate TAB, the current subtree
726 visibility is cycled."
727 :group 'org-cycle
728 :type '(choice (const :tag "Never" nil)
729 (const :tag "Only in completely white lines" white)
730 (const :tag "Before first char in a line" whitestart)
731 (const :tag "Everywhere except in headlines" t)
732 (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
735 (defcustom org-cycle-separator-lines 2
736 "Number of empty lines needed to keep an empty line between collapsed trees.
737 If you leave an empty line between the end of a subtree and the following
738 headline, this empty line is hidden when the subtree is folded.
739 Org-mode will leave (exactly) one empty line visible if the number of
740 empty lines is equal or larger to the number given in this variable.
741 So the default 2 means at least 2 empty lines after the end of a subtree
742 are needed to produce free space between a collapsed subtree and the
743 following headline.
745 If the number is negative, and the number of empty lines is at least -N,
746 all empty lines are shown.
748 Special case: when 0, never leave empty lines in collapsed view."
749 :group 'org-cycle
750 :type 'integer)
751 (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
753 (defcustom org-pre-cycle-hook nil
754 "Hook that is run before visibility cycling is happening.
755 The function(s) in this hook must accept a single argument which indicates
756 the new state that will be set right after running this hook. The
757 argument is a symbol. Before a global state change, it can have the values
758 `overview', `content', or `all'. Before a local state change, it can have
759 the values `folded', `children', or `subtree'."
760 :group 'org-cycle
761 :type 'hook)
763 (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
764 org-cycle-hide-drawers
765 org-cycle-show-empty-lines
766 org-optimize-window-after-visibility-change)
767 "Hook that is run after `org-cycle' has changed the buffer visibility.
768 The function(s) in this hook must accept a single argument which indicates
769 the new state that was set by the most recent `org-cycle' command. The
770 argument is a symbol. After a global state change, it can have the values
771 `overview', `content', or `all'. After a local state change, it can have
772 the values `folded', `children', or `subtree'."
773 :group 'org-cycle
774 :type 'hook)
776 (defgroup org-edit-structure nil
777 "Options concerning structure editing in Org-mode."
778 :tag "Org Edit Structure"
779 :group 'org-structure)
781 (defcustom org-odd-levels-only nil
782 "Non-nil means skip even levels and only use odd levels for the outline.
783 This has the effect that two stars are being added/taken away in
784 promotion/demotion commands. It also influences how levels are
785 handled by the exporters.
786 Changing it requires restart of `font-lock-mode' to become effective
787 for fontification also in regions already fontified.
788 You may also set this on a per-file basis by adding one of the following
789 lines to the buffer:
791 #+STARTUP: odd
792 #+STARTUP: oddeven"
793 :group 'org-edit-structure
794 :group 'org-appearance
795 :type 'boolean)
797 (defcustom org-adapt-indentation t
798 "Non-nil means adapt indentation to outline node level.
800 When this variable is set, Org assumes that you write outlines by
801 indenting text in each node to align with the headline (after the stars).
802 The following issues are influenced by this variable:
804 - When this is set and the *entire* text in an entry is indented, the
805 indentation is increased by one space in a demotion command, and
806 decreased by one in a promotion command. If any line in the entry
807 body starts with text at column 0, indentation is not changed at all.
809 - Property drawers and planning information is inserted indented when
810 this variable s set. When nil, they will not be indented.
812 - TAB indents a line relative to context. The lines below a headline
813 will be indented when this variable is set.
815 Note that this is all about true indentation, by adding and removing
816 space characters. See also `org-indent.el' which does level-dependent
817 indentation in a virtual way, i.e. at display time in Emacs."
818 :group 'org-edit-structure
819 :type 'boolean)
821 (defcustom org-special-ctrl-a/e nil
822 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
824 When t, `C-a' will bring back the cursor to the beginning of the
825 headline text, i.e. after the stars and after a possible TODO keyword.
826 In an item, this will be the position after the bullet.
827 When the cursor is already at that position, another `C-a' will bring
828 it to the beginning of the line.
830 `C-e' will jump to the end of the headline, ignoring the presence of tags
831 in the headline. A second `C-e' will then jump to the true end of the
832 line, after any tags. This also means that, when this variable is
833 non-nil, `C-e' also will never jump beyond the end of the heading of a
834 folded section, i.e. not after the ellipses.
836 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
837 going to the true line boundary first. Only a directly following, identical
838 keypress will bring the cursor to the special positions.
840 This may also be a cons cell where the behavior for `C-a' and `C-e' is
841 set separately."
842 :group 'org-edit-structure
843 :type '(choice
844 (const :tag "off" nil)
845 (const :tag "on: after stars/bullet and before tags first" t)
846 (const :tag "reversed: true line boundary first" reversed)
847 (cons :tag "Set C-a and C-e separately"
848 (choice :tag "Special C-a"
849 (const :tag "off" nil)
850 (const :tag "on: after stars/bullet first" t)
851 (const :tag "reversed: before stars/bullet first" reversed))
852 (choice :tag "Special C-e"
853 (const :tag "off" nil)
854 (const :tag "on: before tags first" t)
855 (const :tag "reversed: after tags first" reversed)))))
856 (if (fboundp 'defvaralias)
857 (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
859 (defcustom org-special-ctrl-k nil
860 "Non-nil means `C-k' will behave specially in headlines.
861 When nil, `C-k' will call the default `kill-line' command.
862 When t, the following will happen while the cursor is in the headline:
864 - When the cursor is at the beginning of a headline, kill the entire
865 line and possible the folded subtree below the line.
866 - When in the middle of the headline text, kill the headline up to the tags.
867 - When after the headline text, kill the tags."
868 :group 'org-edit-structure
869 :type 'boolean)
871 (defcustom org-yank-folded-subtrees t
872 "Non-nil means when yanking subtrees, fold them.
873 If the kill is a single subtree, or a sequence of subtrees, i.e. if
874 it starts with a heading and all other headings in it are either children
875 or siblings, then fold all the subtrees. However, do this only if no
876 text after the yank would be swallowed into a folded tree by this action."
877 :group 'org-edit-structure
878 :type 'boolean)
880 (defcustom org-yank-adjusted-subtrees nil
881 "Non-nil means when yanking subtrees, adjust the level.
882 With this setting, `org-paste-subtree' is used to insert the subtree, see
883 this function for details."
884 :group 'org-edit-structure
885 :type 'boolean)
887 (defcustom org-M-RET-may-split-line '((default . t))
888 "Non-nil means M-RET will split the line at the cursor position.
889 When nil, it will go to the end of the line before making a
890 new line.
891 You may also set this option in a different way for different
892 contexts. Valid contexts are:
894 headline when creating a new headline
895 item when creating a new item
896 table in a table field
897 default the value to be used for all contexts not explicitly
898 customized"
899 :group 'org-structure
900 :group 'org-table
901 :type '(choice
902 (const :tag "Always" t)
903 (const :tag "Never" nil)
904 (repeat :greedy t :tag "Individual contexts"
905 (cons
906 (choice :tag "Context"
907 (const headline)
908 (const item)
909 (const table)
910 (const default))
911 (boolean)))))
914 (defcustom org-insert-heading-respect-content nil
915 "Non-nil means insert new headings after the current subtree.
916 When nil, the new heading is created directly after the current line.
917 The commands \\[org-insert-heading-respect-content] and
918 \\[org-insert-todo-heading-respect-content] turn this variable on
919 for the duration of the command."
920 :group 'org-structure
921 :type 'boolean)
923 (defcustom org-blank-before-new-entry '((heading . auto)
924 (plain-list-item . auto))
925 "Should `org-insert-heading' leave a blank line before new heading/item?
926 The value is an alist, with `heading' and `plain-list-item' as car,
927 and a boolean flag as cdr. For plain lists, if the variable
928 `org-empty-line-terminates-plain-lists' is set, the setting here
929 is ignored and no empty line is inserted, to keep the list in tact."
930 :group 'org-edit-structure
931 :type '(list
932 (cons (const heading)
933 (choice (const :tag "Never" nil)
934 (const :tag "Always" t)
935 (const :tag "Auto" auto)))
936 (cons (const plain-list-item)
937 (choice (const :tag "Never" nil)
938 (const :tag "Always" t)
939 (const :tag "Auto" auto)))))
941 (defcustom org-insert-heading-hook nil
942 "Hook being run after inserting a new heading."
943 :group 'org-edit-structure
944 :type 'hook)
946 (defcustom org-enable-fixed-width-editor t
947 "Non-nil means lines starting with \":\" are treated as fixed-width.
948 This currently only means they are never auto-wrapped.
949 When nil, such lines will be treated like ordinary lines.
950 See also the QUOTE keyword."
951 :group 'org-edit-structure
952 :type 'boolean)
955 (defcustom org-goto-auto-isearch t
956 "Non-nil means typing characters in org-goto starts incremental search."
957 :group 'org-edit-structure
958 :type 'boolean)
960 (defgroup org-sparse-trees nil
961 "Options concerning sparse trees in Org-mode."
962 :tag "Org Sparse Trees"
963 :group 'org-structure)
965 (defcustom org-highlight-sparse-tree-matches t
966 "Non-nil means highlight all matches that define a sparse tree.
967 The highlights will automatically disappear the next time the buffer is
968 changed by an edit command."
969 :group 'org-sparse-trees
970 :type 'boolean)
972 (defcustom org-remove-highlights-with-change t
973 "Non-nil means any change to the buffer will remove temporary highlights.
974 Such highlights are created by `org-occur' and `org-clock-display'.
975 When nil, `C-c C-c needs to be used to get rid of the highlights.
976 The highlights created by `org-preview-latex-fragment' always need
977 `C-c C-c' to be removed."
978 :group 'org-sparse-trees
979 :group 'org-time
980 :type 'boolean)
983 (defcustom org-occur-hook '(org-first-headline-recenter)
984 "Hook that is run after `org-occur' has constructed a sparse tree.
985 This can be used to recenter the window to show as much of the structure
986 as possible."
987 :group 'org-sparse-trees
988 :type 'hook)
990 (defgroup org-imenu-and-speedbar nil
991 "Options concerning imenu and speedbar in Org-mode."
992 :tag "Org Imenu and Speedbar"
993 :group 'org-structure)
995 (defcustom org-imenu-depth 2
996 "The maximum level for Imenu access to Org-mode headlines.
997 This also applied for speedbar access."
998 :group 'org-imenu-and-speedbar
999 :type 'integer)
1001 (defgroup org-table nil
1002 "Options concerning tables in Org-mode."
1003 :tag "Org Table"
1004 :group 'org)
1006 (defcustom org-enable-table-editor 'optimized
1007 "Non-nil means lines starting with \"|\" are handled by the table editor.
1008 When nil, such lines will be treated like ordinary lines.
1010 When equal to the symbol `optimized', the table editor will be optimized to
1011 do the following:
1012 - Automatic overwrite mode in front of whitespace in table fields.
1013 This makes the structure of the table stay in tact as long as the edited
1014 field does not exceed the column width.
1015 - Minimize the number of realigns. Normally, the table is aligned each time
1016 TAB or RET are pressed to move to another field. With optimization this
1017 happens only if changes to a field might have changed the column width.
1018 Optimization requires replacing the functions `self-insert-command',
1019 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1020 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1021 very good at guessing when a re-align will be necessary, but you can always
1022 force one with \\[org-ctrl-c-ctrl-c].
1024 If you would like to use the optimized version in Org-mode, but the
1025 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1027 This variable can be used to turn on and off the table editor during a session,
1028 but in order to toggle optimization, a restart is required.
1030 See also the variable `org-table-auto-blank-field'."
1031 :group 'org-table
1032 :type '(choice
1033 (const :tag "off" nil)
1034 (const :tag "on" t)
1035 (const :tag "on, optimized" optimized)))
1037 (defcustom org-self-insert-cluster-for-undo t
1038 "Non-nil means cluster self-insert commands for undo when possible.
1039 If this is set, then, like in the Emacs command loop, 20 consecutive
1040 characters will be undone together.
1041 This is configurable, because there is some impact on typing performance."
1042 :group 'org-table
1043 :type 'boolean)
1045 (defcustom org-table-tab-recognizes-table.el t
1046 "Non-nil means TAB will automatically notice a table.el table.
1047 When it sees such a table, it moves point into it and - if necessary -
1048 calls `table-recognize-table'."
1049 :group 'org-table-editing
1050 :type 'boolean)
1052 (defgroup org-link nil
1053 "Options concerning links in Org-mode."
1054 :tag "Org Link"
1055 :group 'org)
1057 (defvar org-link-abbrev-alist-local nil
1058 "Buffer-local version of `org-link-abbrev-alist', which see.
1059 The value of this is taken from the #+LINK lines.")
1060 (make-variable-buffer-local 'org-link-abbrev-alist-local)
1062 (defcustom org-link-abbrev-alist nil
1063 "Alist of link abbreviations.
1064 The car of each element is a string, to be replaced at the start of a link.
1065 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1066 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1068 [[linkkey:tag][description]]
1070 The 'linkkey' must be a word word, starting with a letter, followed
1071 by letters, numbers, '-' or '_'.
1073 If REPLACE is a string, the tag will simply be appended to create the link.
1074 If the string contains \"%s\", the tag will be inserted there. Alternatively,
1075 the placeholder \"%h\" will cause a url-encoded version of the tag to
1076 be inserted at that point (see the function `url-hexify-string').
1078 REPLACE may also be a function that will be called with the tag as the
1079 only argument to create the link, which should be returned as a string.
1081 See the manual for examples."
1082 :group 'org-link
1083 :type '(repeat
1084 (cons
1085 (string :tag "Protocol")
1086 (choice
1087 (string :tag "Format")
1088 (function)))))
1090 (defcustom org-descriptive-links t
1091 "Non-nil means hide link part and only show description of bracket links.
1092 Bracket links are like [[link][description]]. This variable sets the initial
1093 state in new org-mode buffers. The setting can then be toggled on a
1094 per-buffer basis from the Org->Hyperlinks menu."
1095 :group 'org-link
1096 :type 'boolean)
1098 (defcustom org-link-file-path-type 'adaptive
1099 "How the path name in file links should be stored.
1100 Valid values are:
1102 relative Relative to the current directory, i.e. the directory of the file
1103 into which the link is being inserted.
1104 absolute Absolute path, if possible with ~ for home directory.
1105 noabbrev Absolute path, no abbreviation of home directory.
1106 adaptive Use relative path for files in the current directory and sub-
1107 directories of it. For other files, use an absolute path."
1108 :group 'org-link
1109 :type '(choice
1110 (const relative)
1111 (const absolute)
1112 (const noabbrev)
1113 (const adaptive)))
1115 (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
1116 "Types of links that should be activated in Org-mode files.
1117 This is a list of symbols, each leading to the activation of a certain link
1118 type. In principle, it does not hurt to turn on most link types - there may
1119 be a small gain when turning off unused link types. The types are:
1121 bracket The recommended [[link][description]] or [[link]] links with hiding.
1122 angular Links in angular brackets that may contain whitespace like
1123 <bbdb:Carsten Dominik>.
1124 plain Plain links in normal text, no whitespace, like http://google.com.
1125 radio Text that is matched by a radio target, see manual for details.
1126 tag Tag settings in a headline (link to tag search).
1127 date Time stamps (link to calendar).
1128 footnote Footnote labels.
1130 Changing this variable requires a restart of Emacs to become effective."
1131 :group 'org-link
1132 :type '(set :greedy t
1133 (const :tag "Double bracket links (new style)" bracket)
1134 (const :tag "Angular bracket links (old style)" angular)
1135 (const :tag "Plain text links" plain)
1136 (const :tag "Radio target matches" radio)
1137 (const :tag "Tags" tag)
1138 (const :tag "Timestamps" date)
1139 (const :tag "Footnotes" footnote)))
1141 (defcustom org-make-link-description-function nil
1142 "Function to use to generate link descriptions from links. If
1143 nil the link location will be used. This function must take two
1144 parameters; the first is the link and the second the description
1145 org-insert-link has generated, and should return the description
1146 to use."
1147 :group 'org-link
1148 :type 'function)
1150 (defgroup org-link-store nil
1151 "Options concerning storing links in Org-mode."
1152 :tag "Org Store Link"
1153 :group 'org-link)
1155 (defcustom org-email-link-description-format "Email %c: %.30s"
1156 "Format of the description part of a link to an email or usenet message.
1157 The following %-escapes will be replaced by corresponding information:
1159 %F full \"From\" field
1160 %f name, taken from \"From\" field, address if no name
1161 %T full \"To\" field
1162 %t first name in \"To\" field, address if no name
1163 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1164 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1165 %s subject
1166 %m message-id.
1168 You may use normal field width specification between the % and the letter.
1169 This is for example useful to limit the length of the subject.
1171 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1172 :group 'org-link-store
1173 :type 'string)
1175 (defcustom org-from-is-user-regexp
1176 (let (r1 r2)
1177 (when (and user-mail-address (not (string= user-mail-address "")))
1178 (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
1179 (when (and user-full-name (not (string= user-full-name "")))
1180 (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
1181 (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
1182 "Regexp matched against the \"From:\" header of an email or usenet message.
1183 It should match if the message is from the user him/herself."
1184 :group 'org-link-store
1185 :type 'regexp)
1187 (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
1188 "Non-nil means storing a link to an Org file will use entry IDs.
1190 Note that before this variable is even considered, org-id must be loaded,
1191 so please customize `org-modules' and turn it on.
1193 The variable can have the following values:
1195 t Create an ID if needed to make a link to the current entry.
1197 create-if-interactive
1198 If `org-store-link' is called directly (interactively, as a user
1199 command), do create an ID to support the link. But when doing the
1200 job for remember, only use the ID if it already exists. The
1201 purpose of this setting is to avoid proliferation of unwanted
1202 IDs, just because you happen to be in an Org file when you
1203 call `org-remember' that automatically and preemptively
1204 creates a link. If you do want to get an ID link in a remember
1205 template to an entry not having an ID, create it first by
1206 explicitly creating a link to it, using `C-c C-l' first.
1208 create-if-interactive-and-no-custom-id
1209 Like create-if-interactive, but do not create an ID if there is
1210 a CUSTOM_ID property defined in the entry. This is the default.
1212 use-existing
1213 Use existing ID, do not create one.
1215 nil Never use an ID to make a link, instead link using a text search for
1216 the headline text."
1217 :group 'org-link-store
1218 :type '(choice
1219 (const :tag "Create ID to make link" t)
1220 (const :tag "Create if storing link interactively"
1221 create-if-interactive)
1222 (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
1223 create-if-interactive-and-no-custom-id)
1224 (const :tag "Only use existing" use-existing)
1225 (const :tag "Do not use ID to create link" nil)))
1227 (defcustom org-context-in-file-links t
1228 "Non-nil means file links from `org-store-link' contain context.
1229 A search string will be added to the file name with :: as separator and
1230 used to find the context when the link is activated by the command
1231 `org-open-at-point'.
1232 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1233 negates this setting for the duration of the command."
1234 :group 'org-link-store
1235 :type 'boolean)
1237 (defcustom org-keep-stored-link-after-insertion nil
1238 "Non-nil means keep link in list for entire session.
1240 The command `org-store-link' adds a link pointing to the current
1241 location to an internal list. These links accumulate during a session.
1242 The command `org-insert-link' can be used to insert links into any
1243 Org-mode file (offering completion for all stored links). When this
1244 option is nil, every link which has been inserted once using \\[org-insert-link]
1245 will be removed from the list, to make completing the unused links
1246 more efficient."
1247 :group 'org-link-store
1248 :type 'boolean)
1250 (defgroup org-link-follow nil
1251 "Options concerning following links in Org-mode."
1252 :tag "Org Follow Link"
1253 :group 'org-link)
1255 (defcustom org-link-translation-function nil
1256 "Function to translate links with different syntax to Org syntax.
1257 This can be used to translate links created for example by the Planner
1258 or emacs-wiki packages to Org syntax.
1259 The function must accept two parameters, a TYPE containing the link
1260 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1261 which is everything after the link protocol. It should return a cons
1262 with possibly modified values of type and path.
1263 Org contains a function for this, so if you set this variable to
1264 `org-translate-link-from-planner', you should be able follow many
1265 links created by planner."
1266 :group 'org-link-follow
1267 :type 'function)
1269 (defcustom org-follow-link-hook nil
1270 "Hook that is run after a link has been followed."
1271 :group 'org-link-follow
1272 :type 'hook)
1274 (defcustom org-tab-follows-link nil
1275 "Non-nil means on links TAB will follow the link.
1276 Needs to be set before org.el is loaded.
1277 This really should not be used, it does not make sense, and the
1278 implementation is bad."
1279 :group 'org-link-follow
1280 :type 'boolean)
1282 (defcustom org-return-follows-link nil
1283 "Non-nil means on links RET will follow the link.
1284 Needs to be set before org.el is loaded."
1285 :group 'org-link-follow
1286 :type 'boolean)
1288 (defcustom org-mouse-1-follows-link
1289 (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
1290 "Non-nil means mouse-1 on a link will follow the link.
1291 A longer mouse click will still set point. Does not work on XEmacs.
1292 Needs to be set before org.el is loaded."
1293 :group 'org-link-follow
1294 :type 'boolean)
1296 (defcustom org-mark-ring-length 4
1297 "Number of different positions to be recorded in the ring
1298 Changing this requires a restart of Emacs to work correctly."
1299 :group 'org-link-follow
1300 :type 'integer)
1302 (defcustom org-link-frame-setup
1303 '((vm . vm-visit-folder-other-frame)
1304 (gnus . gnus-other-frame)
1305 (file . find-file-other-window))
1306 "Setup the frame configuration for following links.
1307 When following a link with Emacs, it may often be useful to display
1308 this link in another window or frame. This variable can be used to
1309 set this up for the different types of links.
1310 For VM, use any of
1311 `vm-visit-folder'
1312 `vm-visit-folder-other-frame'
1313 For Gnus, use any of
1314 `gnus'
1315 `gnus-other-frame'
1316 `org-gnus-no-new-news'
1317 For FILE, use any of
1318 `find-file'
1319 `find-file-other-window'
1320 `find-file-other-frame'
1321 For the calendar, use the variable `calendar-setup'.
1322 For BBDB, it is currently only possible to display the matches in
1323 another window."
1324 :group 'org-link-follow
1325 :type '(list
1326 (cons (const vm)
1327 (choice
1328 (const vm-visit-folder)
1329 (const vm-visit-folder-other-window)
1330 (const vm-visit-folder-other-frame)))
1331 (cons (const gnus)
1332 (choice
1333 (const gnus)
1334 (const gnus-other-frame)
1335 (const org-gnus-no-new-news)))
1336 (cons (const file)
1337 (choice
1338 (const find-file)
1339 (const find-file-other-window)
1340 (const find-file-other-frame)))))
1342 (defcustom org-display-internal-link-with-indirect-buffer nil
1343 "Non-nil means use indirect buffer to display infile links.
1344 Activating internal links (from one location in a file to another location
1345 in the same file) normally just jumps to the location. When the link is
1346 activated with a C-u prefix (or with mouse-3), the link is displayed in
1347 another window. When this option is set, the other window actually displays
1348 an indirect buffer clone of the current buffer, to avoid any visibility
1349 changes to the current buffer."
1350 :group 'org-link-follow
1351 :type 'boolean)
1353 (defcustom org-open-non-existing-files nil
1354 "Non-nil means `org-open-file' will open non-existing files.
1355 When nil, an error will be generated.
1356 This variable applies only to external applications because they
1357 might choke on non-existing files. If the link is to a file that
1358 will be opened in Emacs, the variable is ignored."
1359 :group 'org-link-follow
1360 :type 'boolean)
1362 (defcustom org-open-directory-means-index-dot-org nil
1363 "Non-nil means a link to a directory really means to index.org.
1364 When nil, following a directory link will run dired or open a finder/explorer
1365 window on that directory."
1366 :group 'org-link-follow
1367 :type 'boolean)
1369 (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
1370 "Function and arguments to call for following mailto links.
1371 This is a list with the first element being a lisp function, and the
1372 remaining elements being arguments to the function. In string arguments,
1373 %a will be replaced by the address, and %s will be replaced by the subject
1374 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1375 :group 'org-link-follow
1376 :type '(choice
1377 (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1378 (const :tag "compose-mail" (compose-mail "%a" "%s"))
1379 (const :tag "message-mail" (message-mail "%a" "%s"))
1380 (cons :tag "other" (function) (repeat :tag "argument" sexp))))
1382 (defcustom org-confirm-shell-link-function 'yes-or-no-p
1383 "Non-nil means ask for confirmation before executing shell links.
1384 Shell links can be dangerous: just think about a link
1386 [[shell:rm -rf ~/*][Google Search]]
1388 This link would show up in your Org-mode document as \"Google Search\",
1389 but really it would remove your entire home directory.
1390 Therefore we advise against setting this variable to nil.
1391 Just change it to `y-or-n-p' if you want to confirm with a
1392 single keystroke rather than having to type \"yes\"."
1393 :group 'org-link-follow
1394 :type '(choice
1395 (const :tag "with yes-or-no (safer)" yes-or-no-p)
1396 (const :tag "with y-or-n (faster)" y-or-n-p)
1397 (const :tag "no confirmation (dangerous)" nil)))
1399 (defcustom org-confirm-elisp-link-function 'yes-or-no-p
1400 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1401 Elisp links can be dangerous: just think about a link
1403 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1405 This link would show up in your Org-mode document as \"Google Search\",
1406 but really it would remove your entire home directory.
1407 Therefore we advise against setting this variable to nil.
1408 Just change it to `y-or-n-p' if you want to confirm with a
1409 single keystroke rather than having to type \"yes\"."
1410 :group 'org-link-follow
1411 :type '(choice
1412 (const :tag "with yes-or-no (safer)" yes-or-no-p)
1413 (const :tag "with y-or-n (faster)" y-or-n-p)
1414 (const :tag "no confirmation (dangerous)" nil)))
1416 (defconst org-file-apps-defaults-gnu
1417 '((remote . emacs)
1418 (system . mailcap)
1419 (t . mailcap))
1420 "Default file applications on a UNIX or GNU/Linux system.
1421 See `org-file-apps'.")
1423 (defconst org-file-apps-defaults-macosx
1424 '((remote . emacs)
1425 (t . "open %s")
1426 (system . "open %s")
1427 ("ps.gz" . "gv %s")
1428 ("eps.gz" . "gv %s")
1429 ("dvi" . "xdvi %s")
1430 ("fig" . "xfig %s"))
1431 "Default file applications on a MacOS X system.
1432 The system \"open\" is known as a default, but we use X11 applications
1433 for some files for which the OS does not have a good default.
1434 See `org-file-apps'.")
1436 (defconst org-file-apps-defaults-windowsnt
1437 (list
1438 '(remote . emacs)
1439 (cons t
1440 (list (if (featurep 'xemacs)
1441 'mswindows-shell-execute
1442 'w32-shell-execute)
1443 "open" 'file))
1444 (cons 'system
1445 (list (if (featurep 'xemacs)
1446 'mswindows-shell-execute
1447 'w32-shell-execute)
1448 "open" 'file)))
1449 "Default file applications on a Windows NT system.
1450 The system \"open\" is used for most files.
1451 See `org-file-apps'.")
1453 (defcustom org-file-apps
1455 (auto-mode . emacs)
1456 ("\\.mm\\'" . default)
1457 ("\\.x?html?\\'" . default)
1458 ("\\.pdf\\'" . default)
1460 "External applications for opening `file:path' items in a document.
1461 Org-mode uses system defaults for different file types, but
1462 you can use this variable to set the application for a given file
1463 extension. The entries in this list are cons cells where the car identifies
1464 files and the cdr the corresponding command. Possible values for the
1465 file identifier are
1466 \"regex\" Regular expression matched against the file: link. For
1467 backward compatibility, this can also be a string with only
1468 alphanumeric characters, which is then interpreted as an
1469 extension.
1470 `directory' Matches a directory
1471 `remote' Matches a remote file, accessible through tramp or efs.
1472 Remote files most likely should be visited through Emacs
1473 because external applications cannot handle such paths.
1474 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1475 so all files Emacs knows how to handle. Using this with
1476 command `emacs' will open most files in Emacs. Beware that this
1477 will also open html files inside Emacs, unless you add
1478 (\"html\" . default) to the list as well.
1479 t Default for files not matched by any of the other options.
1480 `system' The system command to open files, like `open' on Windows
1481 and Mac OS X, and mailcap under GNU/Linux. This is the command
1482 that will be selected if you call `C-c C-o' with a double
1483 `C-u C-u' prefix.
1485 Possible values for the command are:
1486 `emacs' The file will be visited by the current Emacs process.
1487 `default' Use the default application for this file type, which is the
1488 association for t in the list, most likely in the system-specific
1489 part.
1490 This can be used to overrule an unwanted setting in the
1491 system-specific variable.
1492 `system' Use the system command for opening files, like \"open\".
1493 This command is specified by the entry whose car is `system'.
1494 Most likely, the system-specific version of this variable
1495 does define this command, but you can overrule/replace it
1496 here.
1497 string A command to be executed by a shell; %s will be replaced
1498 by the path to the file. If the file identifier is a regex,
1499 %n will be replaced by the match of the nth match group.
1500 sexp A Lisp form which will be evaluated. The file path will
1501 be available in the Lisp variable `file', the link itself
1502 in the Lisp variable `link'. If the file identifier is a regex,
1503 the original match data will be restored, so subexpression
1504 matches are accessible using (match-string n link).
1505 For more examples, see the system specific constants
1506 `org-file-apps-defaults-macosx'
1507 `org-file-apps-defaults-windowsnt'
1508 `org-file-apps-defaults-gnu'."
1509 :group 'org-link-follow
1510 :type '(repeat
1511 (cons (choice :value ""
1512 (string :tag "Extension")
1513 (const :tag "System command to open files" system)
1514 (const :tag "Default for unrecognized files" t)
1515 (const :tag "Remote file" remote)
1516 (const :tag "Links to a directory" directory)
1517 (const :tag "Any files that have Emacs modes"
1518 auto-mode))
1519 (choice :value ""
1520 (const :tag "Visit with Emacs" emacs)
1521 (const :tag "Use default" default)
1522 (const :tag "Use the system command" system)
1523 (string :tag "Command")
1524 (sexp :tag "Lisp form")))))
1526 (defgroup org-refile nil
1527 "Options concerning refiling entries in Org-mode."
1528 :tag "Org Refile"
1529 :group 'org)
1531 (defcustom org-directory "~/org"
1532 "Directory with org files.
1533 This is just a default location to look for Org files. There is no need
1534 at all to put your files into this directory. It is only used in the
1535 following situations:
1537 1. When a remember template specifies a target file that is not an
1538 absolute path. The path will then be interpreted relative to
1539 `org-directory'
1540 2. When a remember note is filed away in an interactive way (when exiting the
1541 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1542 with `org-directory' as the default path."
1543 :group 'org-refile
1544 :group 'org-remember
1545 :type 'directory)
1547 (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
1548 "Default target for storing notes.
1549 Used by the hooks for remember.el. This can be a string, or nil to mean
1550 the value of `remember-data-file'.
1551 You can set this on a per-template basis with the variable
1552 `org-remember-templates'."
1553 :group 'org-refile
1554 :group 'org-remember
1555 :type '(choice
1556 (const :tag "Default from remember-data-file" nil)
1557 file))
1559 (defcustom org-goto-interface 'outline
1560 "The default interface to be used for `org-goto'.
1561 Allowed values are:
1562 outline The interface shows an outline of the relevant file
1563 and the correct heading is found by moving through
1564 the outline or by searching with incremental search.
1565 outline-path-completion Headlines in the current buffer are offered via
1566 completion. This is the interface also used by
1567 the refile command."
1568 :group 'org-refile
1569 :type '(choice
1570 (const :tag "Outline" outline)
1571 (const :tag "Outline-path-completion" outline-path-completion)))
1573 (defcustom org-goto-max-level 5
1574 "Maximum level to be considered when running org-goto with refile interface."
1575 :group 'org-refile
1576 :type 'integer)
1578 (defcustom org-reverse-note-order nil
1579 "Non-nil means store new notes at the beginning of a file or entry.
1580 When nil, new notes will be filed to the end of a file or entry.
1581 This can also be a list with cons cells of regular expressions that
1582 are matched against file names, and values."
1583 :group 'org-remember
1584 :group 'org-refile
1585 :type '(choice
1586 (const :tag "Reverse always" t)
1587 (const :tag "Reverse never" nil)
1588 (repeat :tag "By file name regexp"
1589 (cons regexp boolean))))
1591 (defcustom org-log-refile nil
1592 "Information to record when a task is refiled.
1594 Possible values are:
1596 nil Don't add anything
1597 time Add a time stamp to the task
1598 note Prompt for a note and add it with template `org-log-note-headings'
1600 This option can also be set with on a per-file-basis with
1602 #+STARTUP: nologrefile
1603 #+STARTUP: logrefile
1604 #+STARTUP: lognoterefile
1606 You can have local logging settings for a subtree by setting the LOGGING
1607 property to one or more of these keywords.
1609 When bulk-refiling from the agenda, the value `note' is forbidden and
1610 will temporarily be changed to `time'."
1611 :group 'org-refile
1612 :group 'org-progress
1613 :type '(choice
1614 (const :tag "No logging" nil)
1615 (const :tag "Record timestamp" time)
1616 (const :tag "Record timestamp with note." note)))
1618 (defcustom org-refile-targets nil
1619 "Targets for refiling entries with \\[org-refile].
1620 This is list of cons cells. Each cell contains:
1621 - a specification of the files to be considered, either a list of files,
1622 or a symbol whose function or variable value will be used to retrieve
1623 a file name or a list of file names. If you use `org-agenda-files' for
1624 that, all agenda files will be scanned for targets. Nil means consider
1625 headings in the current buffer.
1626 - A specification of how to find candidate refile targets. This may be
1627 any of:
1628 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1629 This tag has to be present in all target headlines, inheritance will
1630 not be considered.
1631 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1632 todo keyword.
1633 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1634 headlines that are refiling targets.
1635 - a cons cell (:level . N). Any headline of level N is considered a target.
1636 Note that, when `org-odd-levels-only' is set, level corresponds to
1637 order in hierarchy, not to the number of stars.
1638 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1639 Note that, when `org-odd-levels-only' is set, level corresponds to
1640 order in hierarchy, not to the number of stars.
1642 You can set the variable `org-refile-target-verify-function' to a function
1643 to verify each headline found by the simple critery above.
1645 When this variable is nil, all top-level headlines in the current buffer
1646 are used, equivalent to the value `((nil . (:level . 1))'."
1647 :group 'org-refile
1648 :type '(repeat
1649 (cons
1650 (choice :value org-agenda-files
1651 (const :tag "All agenda files" org-agenda-files)
1652 (const :tag "Current buffer" nil)
1653 (function) (variable) (file))
1654 (choice :tag "Identify target headline by"
1655 (cons :tag "Specific tag" (const :value :tag) (string))
1656 (cons :tag "TODO keyword" (const :value :todo) (string))
1657 (cons :tag "Regular expression" (const :value :regexp) (regexp))
1658 (cons :tag "Level number" (const :value :level) (integer))
1659 (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
1661 (defcustom org-refile-target-verify-function nil
1662 "Function to verify if the headline at point should be a refile target.
1663 The function will be called without arguments, with point at the
1664 beginning of the headline. It should return t and leave point
1665 where it is if the headline is a valid target for refiling.
1667 If the target should not be selected, the function must return nil.
1668 In addition to this, it may move point to a place from where the search
1669 should be continued. For example, the function may decide that the entire
1670 subtree of the current entry should be excluded and move point to the end
1671 of the subtree."
1672 :group 'org-refile
1673 :type 'function)
1675 (defcustom org-refile-use-outline-path nil
1676 "Non-nil means provide refile targets as paths.
1677 So a level 3 headline will be available as level1/level2/level3.
1679 When the value is `file', also include the file name (without directory)
1680 into the path. In this case, you can also stop the completion after
1681 the file name, to get entries inserted as top level in the file.
1683 When `full-file-path', include the full file path."
1684 :group 'org-refile
1685 :type '(choice
1686 (const :tag "Not" nil)
1687 (const :tag "Yes" t)
1688 (const :tag "Start with file name" file)
1689 (const :tag "Start with full file path" full-file-path)))
1691 (defcustom org-outline-path-complete-in-steps t
1692 "Non-nil means complete the outline path in hierarchical steps.
1693 When Org-mode uses the refile interface to select an outline path
1694 \(see variable `org-refile-use-outline-path'), the completion of
1695 the path can be done is a single go, or if can be done in steps down
1696 the headline hierarchy. Going in steps is probably the best if you
1697 do not use a special completion package like `ido' or `icicles'.
1698 However, when using these packages, going in one step can be very
1699 fast, while still showing the whole path to the entry."
1700 :group 'org-refile
1701 :type 'boolean)
1703 (defcustom org-refile-allow-creating-parent-nodes nil
1704 "Non-nil means allow to create new nodes as refile targets.
1705 New nodes are then created by adding \"/new node name\" to the completion
1706 of an existing node. When the value of this variable is `confirm',
1707 new node creation must be confirmed by the user (recommended)
1708 When nil, the completion must match an existing entry.
1710 Note that, if the new heading is not seen by the criteria
1711 listed in `org-refile-targets', multiple instances of the same
1712 heading would be created by trying again to file under the new
1713 heading."
1714 :group 'org-refile
1715 :type '(choice
1716 (const :tag "Never" nil)
1717 (const :tag "Always" t)
1718 (const :tag "Prompt for confirmation" confirm)))
1720 (defgroup org-todo nil
1721 "Options concerning TODO items in Org-mode."
1722 :tag "Org TODO"
1723 :group 'org)
1725 (defgroup org-progress nil
1726 "Options concerning Progress logging in Org-mode."
1727 :tag "Org Progress"
1728 :group 'org-time)
1730 (defvar org-todo-interpretation-widgets
1732 (:tag "Sequence (cycling hits every state)" sequence)
1733 (:tag "Type (cycling directly to DONE)" type))
1734 "The available interpretation symbols for customizing
1735 `org-todo-keywords'.
1736 Interested libraries should add to this list.")
1738 (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
1739 "List of TODO entry keyword sequences and their interpretation.
1740 \\<org-mode-map>This is a list of sequences.
1742 Each sequence starts with a symbol, either `sequence' or `type',
1743 indicating if the keywords should be interpreted as a sequence of
1744 action steps, or as different types of TODO items. The first
1745 keywords are states requiring action - these states will select a headline
1746 for inclusion into the global TODO list Org-mode produces. If one of
1747 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1748 signify that no further action is necessary. If \"|\" is not found,
1749 the last keyword is treated as the only DONE state of the sequence.
1751 The command \\[org-todo] cycles an entry through these states, and one
1752 additional state where no keyword is present. For details about this
1753 cycling, see the manual.
1755 TODO keywords and interpretation can also be set on a per-file basis with
1756 the special #+SEQ_TODO and #+TYP_TODO lines.
1758 Each keyword can optionally specify a character for fast state selection
1759 \(in combination with the variable `org-use-fast-todo-selection')
1760 and specifiers for state change logging, using the same syntax
1761 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1762 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1763 indicates to record a time stamp each time this state is selected.
1765 Each keyword may also specify if a timestamp or a note should be
1766 recorded when entering or leaving the state, by adding additional
1767 characters in the parenthesis after the keyword. This looks like this:
1768 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1769 record only the time of the state change. With X and Y being either
1770 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1771 Y when leaving the state if and only if the *target* state does not
1772 define X. You may omit any of the fast-selection key or X or /Y,
1773 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1775 For backward compatibility, this variable may also be just a list
1776 of keywords - in this case the interpretation (sequence or type) will be
1777 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1778 :group 'org-todo
1779 :group 'org-keywords
1780 :type '(choice
1781 (repeat :tag "Old syntax, just keywords"
1782 (string :tag "Keyword"))
1783 (repeat :tag "New syntax"
1784 (cons
1785 (choice
1786 :tag "Interpretation"
1787 ;;Quick and dirty way to see
1788 ;;`org-todo-interpretations'. This takes the
1789 ;;place of item arguments
1790 :convert-widget
1791 (lambda (widget)
1792 (widget-put widget
1793 :args (mapcar
1794 #'(lambda (x)
1795 (widget-convert
1796 (cons 'const x)))
1797 org-todo-interpretation-widgets))
1798 widget))
1799 (repeat
1800 (string :tag "Keyword"))))))
1802 (defvar org-todo-keywords-1 nil
1803 "All TODO and DONE keywords active in a buffer.")
1804 (make-variable-buffer-local 'org-todo-keywords-1)
1805 (defvar org-todo-keywords-for-agenda nil)
1806 (defvar org-done-keywords-for-agenda nil)
1807 (defvar org-drawers-for-agenda nil)
1808 (defvar org-todo-keyword-alist-for-agenda nil)
1809 (defvar org-tag-alist-for-agenda nil)
1810 (defvar org-agenda-contributing-files nil)
1811 (defvar org-not-done-keywords nil)
1812 (make-variable-buffer-local 'org-not-done-keywords)
1813 (defvar org-done-keywords nil)
1814 (make-variable-buffer-local 'org-done-keywords)
1815 (defvar org-todo-heads nil)
1816 (make-variable-buffer-local 'org-todo-heads)
1817 (defvar org-todo-sets nil)
1818 (make-variable-buffer-local 'org-todo-sets)
1819 (defvar org-todo-log-states nil)
1820 (make-variable-buffer-local 'org-todo-log-states)
1821 (defvar org-todo-kwd-alist nil)
1822 (make-variable-buffer-local 'org-todo-kwd-alist)
1823 (defvar org-todo-key-alist nil)
1824 (make-variable-buffer-local 'org-todo-key-alist)
1825 (defvar org-todo-key-trigger nil)
1826 (make-variable-buffer-local 'org-todo-key-trigger)
1828 (defcustom org-todo-interpretation 'sequence
1829 "Controls how TODO keywords are interpreted.
1830 This variable is in principle obsolete and is only used for
1831 backward compatibility, if the interpretation of todo keywords is
1832 not given already in `org-todo-keywords'. See that variable for
1833 more information."
1834 :group 'org-todo
1835 :group 'org-keywords
1836 :type '(choice (const sequence)
1837 (const type)))
1839 (defcustom org-use-fast-todo-selection t
1840 "Non-nil means use the fast todo selection scheme with C-c C-t.
1841 This variable describes if and under what circumstances the cycling
1842 mechanism for TODO keywords will be replaced by a single-key, direct
1843 selection scheme.
1845 When nil, fast selection is never used.
1847 When the symbol `prefix', it will be used when `org-todo' is called with
1848 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1849 in an agenda buffer.
1851 When t, fast selection is used by default. In this case, the prefix
1852 argument forces cycling instead.
1854 In all cases, the special interface is only used if access keys have actually
1855 been assigned by the user, i.e. if keywords in the configuration are followed
1856 by a letter in parenthesis, like TODO(t)."
1857 :group 'org-todo
1858 :type '(choice
1859 (const :tag "Never" nil)
1860 (const :tag "By default" t)
1861 (const :tag "Only with C-u C-c C-t" prefix)))
1863 (defcustom org-provide-todo-statistics t
1864 "Non-nil means update todo statistics after insert and toggle.
1865 ALL-HEADLINES means update todo statistics by including headlines
1866 with no TODO keyword as well, counting them as not done.
1867 A list of TODO keywords means the same, but skip keywords that are
1868 not in this list.
1870 When this is set, todo statistics is updated in the parent of the
1871 current entry each time a todo state is changed."
1872 :group 'org-todo
1873 :type '(choice
1874 (const :tag "Yes, only for TODO entries" t)
1875 (const :tag "Yes, including all entries" 'all-headlines)
1876 (repeat :tag "Yes, for TODOs in this list"
1877 (string :tag "TODO keyword"))
1878 (other :tag "No TODO statistics" nil)))
1880 (defcustom org-hierarchical-todo-statistics t
1881 "Non-nil means TODO statistics covers just direct children.
1882 When nil, all entries in the subtree are considered.
1883 This has only an effect if `org-provide-todo-statistics' is set.
1884 To set this to nil for only a single subtree, use a COOKIE_DATA
1885 property and include the word \"recursive\" into the value."
1886 :group 'org-todo
1887 :type 'boolean)
1889 (defcustom org-after-todo-state-change-hook nil
1890 "Hook which is run after the state of a TODO item was changed.
1891 The new state (a string with a TODO keyword, or nil) is available in the
1892 Lisp variable `state'."
1893 :group 'org-todo
1894 :type 'hook)
1896 (defvar org-blocker-hook nil
1897 "Hook for functions that are allowed to block a state change.
1899 Each function gets as its single argument a property list, see
1900 `org-trigger-hook' for more information about this list.
1902 If any of the functions in this hook returns nil, the state change
1903 is blocked.")
1905 (defvar org-trigger-hook nil
1906 "Hook for functions that are triggered by a state change.
1908 Each function gets as its single argument a property list with at least
1909 the following elements:
1911 (:type type-of-change :position pos-at-entry-start
1912 :from old-state :to new-state)
1914 Depending on the type, more properties may be present.
1916 This mechanism is currently implemented for:
1918 TODO state changes
1919 ------------------
1920 :type todo-state-change
1921 :from previous state (keyword as a string), or nil, or a symbol
1922 'todo' or 'done', to indicate the general type of state.
1923 :to new state, like in :from")
1925 (defcustom org-enforce-todo-dependencies nil
1926 "Non-nil means undone TODO entries will block switching the parent to DONE.
1927 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
1928 be blocked if any prior sibling is not yet done.
1929 Finally, if the parent is blocked because of ordered siblings of its own,
1930 the child will also be blocked.
1931 This variable needs to be set before org.el is loaded, and you need to
1932 restart Emacs after a change to make the change effective. The only way
1933 to change is while Emacs is running is through the customize interface."
1934 :set (lambda (var val)
1935 (set var val)
1936 (if val
1937 (add-hook 'org-blocker-hook
1938 'org-block-todo-from-children-or-siblings-or-parent)
1939 (remove-hook 'org-blocker-hook
1940 'org-block-todo-from-children-or-siblings-or-parent)))
1941 :group 'org-todo
1942 :type 'boolean)
1944 (defcustom org-enforce-todo-checkbox-dependencies nil
1945 "Non-nil means unchecked boxes will block switching the parent to DONE.
1946 When this is nil, checkboxes have no influence on switching TODO states.
1947 When non-nil, you first need to check off all check boxes before the TODO
1948 entry can be switched to DONE.
1949 This variable needs to be set before org.el is loaded, and you need to
1950 restart Emacs after a change to make the change effective. The only way
1951 to change is while Emacs is running is through the customize interface."
1952 :set (lambda (var val)
1953 (set var val)
1954 (if val
1955 (add-hook 'org-blocker-hook
1956 'org-block-todo-from-checkboxes)
1957 (remove-hook 'org-blocker-hook
1958 'org-block-todo-from-checkboxes)))
1959 :group 'org-todo
1960 :type 'boolean)
1962 (defcustom org-treat-insert-todo-heading-as-state-change nil
1963 "Non-nil means inserting a TODO heading is treated as state change.
1964 So when the command \\[org-insert-todo-heading] is used, state change
1965 logging will apply if appropriate. When nil, the new TODO item will
1966 be inserted directly, and no logging will take place."
1967 :group 'org-todo
1968 :type 'boolean)
1970 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
1971 "Non-nil means switching TODO states with S-cursor counts as state change.
1972 This is the default behavior. However, setting this to nil allows a
1973 convenient way to select a TODO state and bypass any logging associated
1974 with that."
1975 :group 'org-todo
1976 :type 'boolean)
1978 (defcustom org-todo-state-tags-triggers nil
1979 "Tag changes that should be triggered by TODO state changes.
1980 This is a list. Each entry is
1982 (state-change (tag . flag) .......)
1984 State-change can be a string with a state, and empty string to indicate the
1985 state that has no TODO keyword, or it can be one of the symbols `todo'
1986 or `done', meaning any not-done or done state, respectively."
1987 :group 'org-todo
1988 :group 'org-tags
1989 :type '(repeat
1990 (cons (choice :tag "When changing to"
1991 (const :tag "Not-done state" todo)
1992 (const :tag "Done state" done)
1993 (string :tag "State"))
1994 (repeat
1995 (cons :tag "Tag action"
1996 (string :tag "Tag")
1997 (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
1999 (defcustom org-log-done nil
2000 "Information to record when a task moves to the DONE state.
2002 Possible values are:
2004 nil Don't add anything, just change the keyword
2005 time Add a time stamp to the task
2006 note Prompt for a note and add it with template `org-log-note-headings'
2008 This option can also be set with on a per-file-basis with
2010 #+STARTUP: nologdone
2011 #+STARTUP: logdone
2012 #+STARTUP: lognotedone
2014 You can have local logging settings for a subtree by setting the LOGGING
2015 property to one or more of these keywords."
2016 :group 'org-todo
2017 :group 'org-progress
2018 :type '(choice
2019 (const :tag "No logging" nil)
2020 (const :tag "Record CLOSED timestamp" time)
2021 (const :tag "Record CLOSED timestamp with note." note)))
2023 ;; Normalize old uses of org-log-done.
2024 (cond
2025 ((eq org-log-done t) (setq org-log-done 'time))
2026 ((and (listp org-log-done) (memq 'done org-log-done))
2027 (setq org-log-done 'note)))
2029 (defcustom org-log-reschedule nil
2030 "Information to record when the scheduling date of a tasks is modified.
2032 Possible values are:
2034 nil Don't add anything, just change the date
2035 time Add a time stamp to the task
2036 note Prompt for a note and add it with template `org-log-note-headings'
2038 This option can also be set with on a per-file-basis with
2040 #+STARTUP: nologreschedule
2041 #+STARTUP: logreschedule
2042 #+STARTUP: lognotereschedule"
2043 :group 'org-todo
2044 :group 'org-progress
2045 :type '(choice
2046 (const :tag "No logging" nil)
2047 (const :tag "Record timestamp" time)
2048 (const :tag "Record timestamp with note." note)))
2050 (defcustom org-log-redeadline nil
2051 "Information to record when the deadline date of a tasks is modified.
2053 Possible values are:
2055 nil Don't add anything, just change the date
2056 time Add a time stamp to the task
2057 note Prompt for a note and add it with template `org-log-note-headings'
2059 This option can also be set with on a per-file-basis with
2061 #+STARTUP: nologredeadline
2062 #+STARTUP: logredeadline
2063 #+STARTUP: lognoteredeadline
2065 You can have local logging settings for a subtree by setting the LOGGING
2066 property to one or more of these keywords."
2067 :group 'org-todo
2068 :group 'org-progress
2069 :type '(choice
2070 (const :tag "No logging" nil)
2071 (const :tag "Record timestamp" time)
2072 (const :tag "Record timestamp with note." note)))
2074 (defcustom org-log-note-clock-out nil
2075 "Non-nil means record a note when clocking out of an item.
2076 This can also be configured on a per-file basis by adding one of
2077 the following lines anywhere in the buffer:
2079 #+STARTUP: lognoteclock-out
2080 #+STARTUP: nolognoteclock-out"
2081 :group 'org-todo
2082 :group 'org-progress
2083 :type 'boolean)
2085 (defcustom org-log-done-with-time t
2086 "Non-nil means the CLOSED time stamp will contain date and time.
2087 When nil, only the date will be recorded."
2088 :group 'org-progress
2089 :type 'boolean)
2091 (defcustom org-log-note-headings
2092 '((done . "CLOSING NOTE %t")
2093 (state . "State %-12s from %-12S %t")
2094 (note . "Note taken on %t")
2095 (reschedule . "Rescheduled from %S on %t")
2096 (delschedule . "Not scheduled, was %S on %t")
2097 (redeadline . "New deadline from %S on %t")
2098 (deldeadline . "Removed deadline, was %S on %t")
2099 (refile . "Refiled on %t")
2100 (clock-out . ""))
2101 "Headings for notes added to entries.
2102 The value is an alist, with the car being a symbol indicating the note
2103 context, and the cdr is the heading to be used. The heading may also be the
2104 empty string.
2105 %t in the heading will be replaced by a time stamp.
2106 %s will be replaced by the new TODO state, in double quotes.
2107 %S will be replaced by the old TODO state, in double quotes.
2108 %u will be replaced by the user name.
2109 %U will be replaced by the full user name.
2111 In fact, it is not a good idea to change the `state' entry, because
2112 agenda log mode depends on the format of these entries."
2113 :group 'org-todo
2114 :group 'org-progress
2115 :type '(list :greedy t
2116 (cons (const :tag "Heading when closing an item" done) string)
2117 (cons (const :tag
2118 "Heading when changing todo state (todo sequence only)"
2119 state) string)
2120 (cons (const :tag "Heading when just taking a note" note) string)
2121 (cons (const :tag "Heading when clocking out" clock-out) string)
2122 (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
2123 (cons (const :tag "Heading when rescheduling" reschedule) string)
2124 (cons (const :tag "Heading when changing deadline" redeadline) string)
2125 (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
2126 (cons (const :tag "Heading when refiling" refile) string)))
2128 (unless (assq 'note org-log-note-headings)
2129 (push '(note . "%t") org-log-note-headings))
2131 (defcustom org-log-into-drawer nil
2132 "Non-nil means insert state change notes and time stamps into a drawer.
2133 When nil, state changes notes will be inserted after the headline and
2134 any scheduling and clock lines, but not inside a drawer.
2136 The value of this variable should be the name of the drawer to use.
2137 LOGBOOK is proposed at the default drawer for this purpose, you can
2138 also set this to a string to define the drawer of your choice.
2140 A value of t is also allowed, representing \"LOGBOOK\".
2142 If this variable is set, `org-log-state-notes-insert-after-drawers'
2143 will be ignored.
2145 You can set the property LOG_INTO_DRAWER to overrule this setting for
2146 a subtree."
2147 :group 'org-todo
2148 :group 'org-progress
2149 :type '(choice
2150 (const :tag "Not into a drawer" nil)
2151 (const :tag "LOGBOOK" t)
2152 (string :tag "Other")))
2154 (if (fboundp 'defvaralias)
2155 (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
2157 (defun org-log-into-drawer ()
2158 "Return the value of `org-log-into-drawer', but let properties overrule.
2159 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2160 used instead of the default value."
2161 (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
2162 (cond
2163 ((or (not p) (equal p "nil")) org-log-into-drawer)
2164 ((equal p "t") "LOGBOOK")
2165 (t p))))
2167 (defcustom org-log-state-notes-insert-after-drawers nil
2168 "Non-nil means insert state change notes after any drawers in entry.
2169 Only the drawers that *immediately* follow the headline and the
2170 deadline/scheduled line are skipped.
2171 When nil, insert notes right after the heading and perhaps the line
2172 with deadline/scheduling if present.
2174 This variable will have no effect if `org-log-into-drawer' is
2175 set."
2176 :group 'org-todo
2177 :group 'org-progress
2178 :type 'boolean)
2180 (defcustom org-log-states-order-reversed t
2181 "Non-nil means the latest state note will be directly after heading.
2182 When nil, the state change notes will be ordered according to time."
2183 :group 'org-todo
2184 :group 'org-progress
2185 :type 'boolean)
2187 (defcustom org-log-repeat 'time
2188 "Non-nil means record moving through the DONE state when triggering repeat.
2189 An auto-repeating task is immediately switched back to TODO when
2190 marked DONE. If you are not logging state changes (by adding \"@\"
2191 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2192 record a closing note, there will be no record of the task moving
2193 through DONE. This variable forces taking a note anyway.
2195 nil Don't force a record
2196 time Record a time stamp
2197 note Record a note
2199 This option can also be set with on a per-file-basis with
2201 #+STARTUP: logrepeat
2202 #+STARTUP: lognoterepeat
2203 #+STARTUP: nologrepeat
2205 You can have local logging settings for a subtree by setting the LOGGING
2206 property to one or more of these keywords."
2207 :group 'org-todo
2208 :group 'org-progress
2209 :type '(choice
2210 (const :tag "Don't force a record" nil)
2211 (const :tag "Force recording the DONE state" time)
2212 (const :tag "Force recording a note with the DONE state" note)))
2215 (defgroup org-priorities nil
2216 "Priorities in Org-mode."
2217 :tag "Org Priorities"
2218 :group 'org-todo)
2220 (defcustom org-enable-priority-commands t
2221 "Non-nil means priority commands are active.
2222 When nil, these commands will be disabled, so that you never accidentally
2223 set a priority."
2224 :group 'org-priorities
2225 :type 'boolean)
2227 (defcustom org-highest-priority ?A
2228 "The highest priority of TODO items. A character like ?A, ?B etc.
2229 Must have a smaller ASCII number than `org-lowest-priority'."
2230 :group 'org-priorities
2231 :type 'character)
2233 (defcustom org-lowest-priority ?C
2234 "The lowest priority of TODO items. A character like ?A, ?B etc.
2235 Must have a larger ASCII number than `org-highest-priority'."
2236 :group 'org-priorities
2237 :type 'character)
2239 (defcustom org-default-priority ?B
2240 "The default priority of TODO items.
2241 This is the priority an item get if no explicit priority is given."
2242 :group 'org-priorities
2243 :type 'character)
2245 (defcustom org-priority-start-cycle-with-default t
2246 "Non-nil means start with default priority when starting to cycle.
2247 When this is nil, the first step in the cycle will be (depending on the
2248 command used) one higher or lower that the default priority."
2249 :group 'org-priorities
2250 :type 'boolean)
2252 (defgroup org-time nil
2253 "Options concerning time stamps and deadlines in Org-mode."
2254 :tag "Org Time"
2255 :group 'org)
2257 (defcustom org-insert-labeled-timestamps-at-point nil
2258 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2259 When nil, these labeled time stamps are forces into the second line of an
2260 entry, just after the headline. When scheduling from the global TODO list,
2261 the time stamp will always be forced into the second line."
2262 :group 'org-time
2263 :type 'boolean)
2265 (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
2266 "Formats for `format-time-string' which are used for time stamps.
2267 It is not recommended to change this constant.")
2269 (defcustom org-time-stamp-rounding-minutes '(0 5)
2270 "Number of minutes to round time stamps to.
2271 These are two values, the first applies when first creating a time stamp.
2272 The second applies when changing it with the commands `S-up' and `S-down'.
2273 When changing the time stamp, this means that it will change in steps
2274 of N minutes, as given by the second value.
2276 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2277 numbers should be factors of 60, so for example 5, 10, 15.
2279 When this is larger than 1, you can still force an exact time-stamp by using
2280 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
2281 and by using a prefix arg to `S-up/down' to specify the exact number
2282 of minutes to shift."
2283 :group 'org-time
2284 :get '(lambda (var) ; Make sure all entries have 5 elements
2285 (if (integerp (default-value var))
2286 (list (default-value var) 5)
2287 (default-value var)))
2288 :type '(list
2289 (integer :tag "when inserting times")
2290 (integer :tag "when modifying times")))
2292 ;; Normalize old customizations of this variable.
2293 (when (integerp org-time-stamp-rounding-minutes)
2294 (setq org-time-stamp-rounding-minutes
2295 (list org-time-stamp-rounding-minutes
2296 org-time-stamp-rounding-minutes)))
2298 (defcustom org-display-custom-times nil
2299 "Non-nil means overlay custom formats over all time stamps.
2300 The formats are defined through the variable `org-time-stamp-custom-formats'.
2301 To turn this on on a per-file basis, insert anywhere in the file:
2302 #+STARTUP: customtime"
2303 :group 'org-time
2304 :set 'set-default
2305 :type 'sexp)
2306 (make-variable-buffer-local 'org-display-custom-times)
2308 (defcustom org-time-stamp-custom-formats
2309 '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
2310 "Custom formats for time stamps. See `format-time-string' for the syntax.
2311 These are overlayed over the default ISO format if the variable
2312 `org-display-custom-times' is set. Time like %H:%M should be at the
2313 end of the second format. The custom formats are also honored by export
2314 commands, if custom time display is turned on at the time of export."
2315 :group 'org-time
2316 :type 'sexp)
2318 (defun org-time-stamp-format (&optional long inactive)
2319 "Get the right format for a time string."
2320 (let ((f (if long (cdr org-time-stamp-formats)
2321 (car org-time-stamp-formats))))
2322 (if inactive
2323 (concat "[" (substring f 1 -1) "]")
2324 f)))
2326 (defcustom org-time-clocksum-format "%d:%02d"
2327 "The format string used when creating CLOCKSUM lines, or when
2328 org-mode generates a time duration."
2329 :group 'org-time
2330 :type 'string)
2332 (defcustom org-time-clocksum-use-fractional nil
2333 "If non-nil, \\[org-clock-display] uses fractional times.
2334 org-mode generates a time duration."
2335 :group 'org-time
2336 :type 'boolean)
2338 (defcustom org-time-clocksum-fractional-format "%.2f"
2339 "The format string used when creating CLOCKSUM lines, or when
2340 org-mode generates a time duration."
2341 :group 'org-time
2342 :type 'string)
2344 (defcustom org-deadline-warning-days 14
2345 "No. of days before expiration during which a deadline becomes active.
2346 This variable governs the display in sparse trees and in the agenda.
2347 When 0 or negative, it means use this number (the absolute value of it)
2348 even if a deadline has a different individual lead time specified.
2350 Custom commands can set this variable in the options section."
2351 :group 'org-time
2352 :group 'org-agenda-daily/weekly
2353 :type 'integer)
2355 (defcustom org-read-date-prefer-future t
2356 "Non-nil means assume future for incomplete date input from user.
2357 This affects the following situations:
2358 1. The user gives a month but not a year.
2359 For example, if it is april and you enter \"feb 2\", this will be read
2360 as feb 2, *next* year. \"May 5\", however, will be this year.
2361 2. The user gives a day, but no month.
2362 For example, if today is the 15th, and you enter \"3\", Org-mode will
2363 read this as the third of *next* month. However, if you enter \"17\",
2364 it will be considered as *this* month.
2366 If you set this variable to the symbol `time', then also the following
2367 will work:
2369 3. If the user gives a time, but no day. If the time is before now,
2370 to will be interpreted as tomorrow.
2372 Currently none of this works for ISO week specifications.
2374 When this option is nil, the current day, month and year will always be
2375 used as defaults."
2376 :group 'org-time
2377 :type '(choice
2378 (const :tag "Never" nil)
2379 (const :tag "Check month and day" t)
2380 (const :tag "Check month, day, and time" time)))
2382 (defcustom org-read-date-display-live t
2383 "Non-nil means display current interpretation of date prompt live.
2384 This display will be in an overlay, in the minibuffer."
2385 :group 'org-time
2386 :type 'boolean)
2388 (defcustom org-read-date-popup-calendar t
2389 "Non-nil means pop up a calendar when prompting for a date.
2390 In the calendar, the date can be selected with mouse-1. However, the
2391 minibuffer will also be active, and you can simply enter the date as well.
2392 When nil, only the minibuffer will be available."
2393 :group 'org-time
2394 :type 'boolean)
2395 (if (fboundp 'defvaralias)
2396 (defvaralias 'org-popup-calendar-for-date-prompt
2397 'org-read-date-popup-calendar))
2399 (defcustom org-read-date-minibuffer-setup-hook nil
2400 "Hook to be used to set up keys for the date/time interface.
2401 Add key definitions to `minibuffer-local-map', which will be a temporary
2402 copy."
2403 :group 'org-time
2404 :type 'hook)
2406 (defcustom org-extend-today-until 0
2407 "The hour when your day really ends. Must be an integer.
2408 This has influence for the following applications:
2409 - When switching the agenda to \"today\". It it is still earlier than
2410 the time given here, the day recognized as TODAY is actually yesterday.
2411 - When a date is read from the user and it is still before the time given
2412 here, the current date and time will be assumed to be yesterday, 23:59.
2413 Also, timestamps inserted in remember templates follow this rule.
2415 IMPORTANT: This is a feature whose implementation is and likely will
2416 remain incomplete. Really, it is only here because past midnight seems to
2417 be the favorite working time of John Wiegley :-)"
2418 :group 'org-time
2419 :type 'integer)
2421 (defcustom org-edit-timestamp-down-means-later nil
2422 "Non-nil means S-down will increase the time in a time stamp.
2423 When nil, S-up will increase."
2424 :group 'org-time
2425 :type 'boolean)
2427 (defcustom org-calendar-follow-timestamp-change t
2428 "Non-nil means make the calendar window follow timestamp changes.
2429 When a timestamp is modified and the calendar window is visible, it will be
2430 moved to the new date."
2431 :group 'org-time
2432 :type 'boolean)
2434 (defgroup org-tags nil
2435 "Options concerning tags in Org-mode."
2436 :tag "Org Tags"
2437 :group 'org)
2439 (defcustom org-tag-alist nil
2440 "List of tags allowed in Org-mode files.
2441 When this list is nil, Org-mode will base TAG input on what is already in the
2442 buffer.
2443 The value of this variable is an alist, the car of each entry must be a
2444 keyword as a string, the cdr may be a character that is used to select
2445 that tag through the fast-tag-selection interface.
2446 See the manual for details."
2447 :group 'org-tags
2448 :type '(repeat
2449 (choice
2450 (cons (string :tag "Tag name")
2451 (character :tag "Access char"))
2452 (list :tag "Start radio group"
2453 (const :startgroup)
2454 (option (string :tag "Group description")))
2455 (list :tag "End radio group"
2456 (const :endgroup)
2457 (option (string :tag "Group description")))
2458 (const :tag "New line" (:newline)))))
2460 (defcustom org-tag-persistent-alist nil
2461 "List of tags that will always appear in all Org-mode files.
2462 This is in addition to any in buffer settings or customizations
2463 of `org-tag-alist'.
2464 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2465 The value of this variable is an alist, the car of each entry must be a
2466 keyword as a string, the cdr may be a character that is used to select
2467 that tag through the fast-tag-selection interface.
2468 See the manual for details.
2469 To disable these tags on a per-file basis, insert anywhere in the file:
2470 #+STARTUP: noptag"
2471 :group 'org-tags
2472 :type '(repeat
2473 (choice
2474 (cons (string :tag "Tag name")
2475 (character :tag "Access char"))
2476 (const :tag "Start radio group" (:startgroup))
2477 (const :tag "End radio group" (:endgroup))
2478 (const :tag "New line" (:newline)))))
2480 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2481 "If non-nil, always offer completion for all tags of all agenda files.
2482 Instead of customizing this variable directly, you might want to
2483 set it locally for remember buffers, because there no list of
2484 tags in that file can be created dynamically (there are none).
2486 (add-hook 'org-remember-mode-hook
2487 (lambda ()
2488 (set (make-local-variable
2489 'org-complete-tags-always-offer-all-agenda-tags)
2490 t)))"
2491 :group 'org-tags
2492 :type 'boolean)
2494 (defvar org-file-tags nil
2495 "List of tags that can be inherited by all entries in the file.
2496 The tags will be inherited if the variable `org-use-tag-inheritance'
2497 says they should be.
2498 This variable is populated from #+FILETAGS lines.")
2500 (defcustom org-use-fast-tag-selection 'auto
2501 "Non-nil means use fast tag selection scheme.
2502 This is a special interface to select and deselect tags with single keys.
2503 When nil, fast selection is never used.
2504 When the symbol `auto', fast selection is used if and only if selection
2505 characters for tags have been configured, either through the variable
2506 `org-tag-alist' or through a #+TAGS line in the buffer.
2507 When t, fast selection is always used and selection keys are assigned
2508 automatically if necessary."
2509 :group 'org-tags
2510 :type '(choice
2511 (const :tag "Always" t)
2512 (const :tag "Never" nil)
2513 (const :tag "When selection characters are configured" 'auto)))
2515 (defcustom org-fast-tag-selection-single-key nil
2516 "Non-nil means fast tag selection exits after first change.
2517 When nil, you have to press RET to exit it.
2518 During fast tag selection, you can toggle this flag with `C-c'.
2519 This variable can also have the value `expert'. In this case, the window
2520 displaying the tags menu is not even shown, until you press C-c again."
2521 :group 'org-tags
2522 :type '(choice
2523 (const :tag "No" nil)
2524 (const :tag "Yes" t)
2525 (const :tag "Expert" expert)))
2527 (defvar org-fast-tag-selection-include-todo nil
2528 "Non-nil means fast tags selection interface will also offer TODO states.
2529 This is an undocumented feature, you should not rely on it.")
2531 (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
2532 "The column to which tags should be indented in a headline.
2533 If this number is positive, it specifies the column. If it is negative,
2534 it means that the tags should be flushright to that column. For example,
2535 -80 works well for a normal 80 character screen."
2536 :group 'org-tags
2537 :type 'integer)
2539 (defcustom org-auto-align-tags t
2540 "Non-nil means realign tags after pro/demotion of TODO state change.
2541 These operations change the length of a headline and therefore shift
2542 the tags around. With this options turned on, after each such operation
2543 the tags are again aligned to `org-tags-column'."
2544 :group 'org-tags
2545 :type 'boolean)
2547 (defcustom org-use-tag-inheritance t
2548 "Non-nil means tags in levels apply also for sublevels.
2549 When nil, only the tags directly given in a specific line apply there.
2550 This may also be a list of tags that should be inherited, or a regexp that
2551 matches tags that should be inherited. Additional control is possible
2552 with the variable `org-tags-exclude-from-inheritance' which gives an
2553 explicit list of tags to be excluded from inheritance., even if the value of
2554 `org-use-tag-inheritance' would select it for inheritance.
2556 If this option is t, a match early-on in a tree can lead to a large
2557 number of matches in the subtree when constructing the agenda or creating
2558 a sparse tree. If you only want to see the first match in a tree during
2559 a search, check out the variable `org-tags-match-list-sublevels'."
2560 :group 'org-tags
2561 :type '(choice
2562 (const :tag "Not" nil)
2563 (const :tag "Always" t)
2564 (repeat :tag "Specific tags" (string :tag "Tag"))
2565 (regexp :tag "Tags matched by regexp")))
2567 (defcustom org-tags-exclude-from-inheritance nil
2568 "List of tags that should never be inherited.
2569 This is a way to exclude a few tags from inheritance. For way to do
2570 the opposite, to actively allow inheritance for selected tags,
2571 see the variable `org-use-tag-inheritance'."
2572 :group 'org-tags
2573 :type '(repeat (string :tag "Tag")))
2575 (defun org-tag-inherit-p (tag)
2576 "Check if TAG is one that should be inherited."
2577 (cond
2578 ((member tag org-tags-exclude-from-inheritance) nil)
2579 ((eq org-use-tag-inheritance t) t)
2580 ((not org-use-tag-inheritance) nil)
2581 ((stringp org-use-tag-inheritance)
2582 (string-match org-use-tag-inheritance tag))
2583 ((listp org-use-tag-inheritance)
2584 (member tag org-use-tag-inheritance))
2585 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
2587 (defcustom org-tags-match-list-sublevels t
2588 "Non-nil means list also sublevels of headlines matching a search.
2589 This variable applies to tags/property searches, and also to stuck
2590 projects because this search is based on a tags match as well.
2592 When set to the symbol `indented', sublevels are indented with
2593 leading dots.
2595 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2596 the sublevels of a headline matching a tag search often also match
2597 the same search. Listing all of them can create very long lists.
2598 Setting this variable to nil causes subtrees of a match to be skipped.
2600 This variable is semi-obsolete and probably should always be true. It
2601 is better to limit inheritance to certain tags using the variables
2602 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
2603 :group 'org-tags
2604 :type '(choice
2605 (const :tag "No, don't list them" nil)
2606 (const :tag "Yes, do list them" t)
2607 (const :tag "List them, indented with leading dots" indented)))
2609 (defcustom org-tags-sort-function nil
2610 "When set, tags are sorted using this function as a comparator"
2611 :group 'org-tags
2612 :type '(choice
2613 (const :tag "No sorting" nil)
2614 (const :tag "Alphabetical" string<)
2615 (const :tag "Reverse alphabetical" string>)
2616 (function :tag "Custom function" nil)))
2618 (defvar org-tags-history nil
2619 "History of minibuffer reads for tags.")
2620 (defvar org-last-tags-completion-table nil
2621 "The last used completion table for tags.")
2622 (defvar org-after-tags-change-hook nil
2623 "Hook that is run after the tags in a line have changed.")
2625 (defgroup org-properties nil
2626 "Options concerning properties in Org-mode."
2627 :tag "Org Properties"
2628 :group 'org)
2630 (defcustom org-property-format "%-10s %s"
2631 "How property key/value pairs should be formatted by `indent-line'.
2632 When `indent-line' hits a property definition, it will format the line
2633 according to this format, mainly to make sure that the values are
2634 lined-up with respect to each other."
2635 :group 'org-properties
2636 :type 'string)
2638 (defcustom org-use-property-inheritance nil
2639 "Non-nil means properties apply also for sublevels.
2641 This setting is chiefly used during property searches. Turning it on can
2642 cause significant overhead when doing a search, which is why it is not
2643 on by default.
2645 When nil, only the properties directly given in the current entry count.
2646 When t, every property is inherited. The value may also be a list of
2647 properties that should have inheritance, or a regular expression matching
2648 properties that should be inherited.
2650 However, note that some special properties use inheritance under special
2651 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2652 and the properties ending in \"_ALL\" when they are used as descriptor
2653 for valid values of a property.
2655 Note for programmers:
2656 When querying an entry with `org-entry-get', you can control if inheritance
2657 should be used. By default, `org-entry-get' looks only at the local
2658 properties. You can request inheritance by setting the inherit argument
2659 to t (to force inheritance) or to `selective' (to respect the setting
2660 in this variable)."
2661 :group 'org-properties
2662 :type '(choice
2663 (const :tag "Not" nil)
2664 (const :tag "Always" t)
2665 (repeat :tag "Specific properties" (string :tag "Property"))
2666 (regexp :tag "Properties matched by regexp")))
2668 (defun org-property-inherit-p (property)
2669 "Check if PROPERTY is one that should be inherited."
2670 (cond
2671 ((eq org-use-property-inheritance t) t)
2672 ((not org-use-property-inheritance) nil)
2673 ((stringp org-use-property-inheritance)
2674 (string-match org-use-property-inheritance property))
2675 ((listp org-use-property-inheritance)
2676 (member property org-use-property-inheritance))
2677 (t (error "Invalid setting of `org-use-property-inheritance'"))))
2679 (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
2680 "The default column format, if no other format has been defined.
2681 This variable can be set on the per-file basis by inserting a line
2683 #+COLUMNS: %25ITEM ....."
2684 :group 'org-properties
2685 :type 'string)
2687 (defcustom org-columns-ellipses ".."
2688 "The ellipses to be used when a field in column view is truncated.
2689 When this is the empty string, as many characters as possible are shown,
2690 but then there will be no visual indication that the field has been truncated.
2691 When this is a string of length N, the last N characters of a truncated
2692 field are replaced by this string. If the column is narrower than the
2693 ellipses string, only part of the ellipses string will be shown."
2694 :group 'org-properties
2695 :type 'string)
2697 (defcustom org-columns-modify-value-for-display-function nil
2698 "Function that modifies values for display in column view.
2699 For example, it can be used to cut out a certain part from a time stamp.
2700 The function must take 2 arguments:
2702 column-title The title of the column (*not* the property name)
2703 value The value that should be modified.
2705 The function should return the value that should be displayed,
2706 or nil if the normal value should be used."
2707 :group 'org-properties
2708 :type 'function)
2710 (defcustom org-effort-property "Effort"
2711 "The property that is being used to keep track of effort estimates.
2712 Effort estimates given in this property need to have the format H:MM."
2713 :group 'org-properties
2714 :group 'org-progress
2715 :type '(string :tag "Property"))
2717 (defconst org-global-properties-fixed
2718 '(("VISIBILITY_ALL" . "folded children content all")
2719 ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
2720 "List of property/value pairs that can be inherited by any entry.
2722 These are fixed values, for the preset properties. The user variable
2723 that can be used to add to this list is `org-global-properties'.
2725 The entries in this list are cons cells where the car is a property
2726 name and cdr is a string with the value. If the value represents
2727 multiple items like an \"_ALL\" property, separate the items by
2728 spaces.")
2730 (defcustom org-global-properties nil
2731 "List of property/value pairs that can be inherited by any entry.
2733 This list will be combined with the constant `org-global-properties-fixed'.
2735 The entries in this list are cons cells where the car is a property
2736 name and cdr is a string with the value.
2738 You can set buffer-local values for the same purpose in the variable
2739 `org-file-properties' this by adding lines like
2741 #+PROPERTY: NAME VALUE"
2742 :group 'org-properties
2743 :type '(repeat
2744 (cons (string :tag "Property")
2745 (string :tag "Value"))))
2747 (defvar org-file-properties nil
2748 "List of property/value pairs that can be inherited by any entry.
2749 Valid for the current buffer.
2750 This variable is populated from #+PROPERTY lines.")
2751 (make-variable-buffer-local 'org-file-properties)
2753 (defgroup org-agenda nil
2754 "Options concerning agenda views in Org-mode."
2755 :tag "Org Agenda"
2756 :group 'org)
2758 (defvar org-category nil
2759 "Variable used by org files to set a category for agenda display.
2760 Such files should use a file variable to set it, for example
2762 # -*- mode: org; org-category: \"ELisp\"
2764 or contain a special line
2766 #+CATEGORY: ELisp
2768 If the file does not specify a category, then file's base name
2769 is used instead.")
2770 (make-variable-buffer-local 'org-category)
2771 (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
2773 (defcustom org-agenda-files nil
2774 "The files to be used for agenda display.
2775 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2776 \\[org-remove-file]. You can also use customize to edit the list.
2778 If an entry is a directory, all files in that directory that are matched by
2779 `org-agenda-file-regexp' will be part of the file list.
2781 If the value of the variable is not a list but a single file name, then
2782 the list of agenda files is actually stored and maintained in that file, one
2783 agenda file per line. In this file paths can be given relative to
2784 `org-directory'. Tilde expansion and environment variable substitution
2785 are also made."
2786 :group 'org-agenda
2787 :type '(choice
2788 (repeat :tag "List of files and directories" file)
2789 (file :tag "Store list in a file\n" :value "~/.agenda_files")))
2791 (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
2792 "Regular expression to match files for `org-agenda-files'.
2793 If any element in the list in that variable contains a directory instead
2794 of a normal file, all files in that directory that are matched by this
2795 regular expression will be included."
2796 :group 'org-agenda
2797 :type 'regexp)
2799 (defcustom org-agenda-text-search-extra-files nil
2800 "List of extra files to be searched by text search commands.
2801 These files will be search in addition to the agenda files by the
2802 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
2803 Note that these files will only be searched for text search commands,
2804 not for the other agenda views like todo lists, tag searches or the weekly
2805 agenda. This variable is intended to list notes and possibly archive files
2806 that should also be searched by these two commands.
2807 In fact, if the first element in the list is the symbol `agenda-archives',
2808 than all archive files of all agenda files will be added to the search
2809 scope."
2810 :group 'org-agenda
2811 :type '(set :greedy t
2812 (const :tag "Agenda Archives" agenda-archives)
2813 (repeat :inline t (file))))
2815 (if (fboundp 'defvaralias)
2816 (defvaralias 'org-agenda-multi-occur-extra-files
2817 'org-agenda-text-search-extra-files))
2819 (defcustom org-agenda-skip-unavailable-files nil
2820 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
2821 A nil value means to remove them, after a query, from the list."
2822 :group 'org-agenda
2823 :type 'boolean)
2825 (defcustom org-calendar-to-agenda-key [?c]
2826 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2827 The command `org-calendar-goto-agenda' will be bound to this key. The
2828 default is the character `c' because then `c' can be used to switch back and
2829 forth between agenda and calendar."
2830 :group 'org-agenda
2831 :type 'sexp)
2833 (defcustom org-calendar-agenda-action-key [?k]
2834 "The key to be installed in `calendar-mode-map' for agenda-action.
2835 The command `org-agenda-action' will be bound to this key. The
2836 default is the character `k' because we use the same key in the agenda."
2837 :group 'org-agenda
2838 :type 'sexp)
2840 (defcustom org-calendar-insert-diary-entry-key [?i]
2841 "The key to be installed in `calendar-mode-map' for adding diary entries.
2842 This option is irrelevant until `org-agenda-diary-file' has been configured
2843 to point to an Org-mode file. When that is the case, the command
2844 `org-agenda-diary-entry' will be bound to the key given here, by default
2845 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
2846 if you want to continue doing this, you need to change this to a different
2847 key."
2848 :group 'org-agenda
2849 :type 'sexp)
2851 (defcustom org-agenda-diary-file 'diary-file
2852 "File to which to add new entries with the `i' key in agenda and calendar.
2853 When this is the symbol `diary-file', the functionality in the Emacs
2854 calendar will be used to add entries to the `diary-file'. But when this
2855 points to a file, `org-agenda-diary-entry' will be used instead."
2856 :group 'org-agenda
2857 :type '(choice
2858 (const :tag "The standard Emacs diary file" diary-file)
2859 (file :tag "Special Org file diary entries")))
2861 (eval-after-load "calendar"
2862 '(progn
2863 (org-defkey calendar-mode-map org-calendar-to-agenda-key
2864 'org-calendar-goto-agenda)
2865 (org-defkey calendar-mode-map org-calendar-agenda-action-key
2866 'org-agenda-action)
2867 (add-hook 'calendar-mode-hook
2868 (lambda ()
2869 (unless (eq org-agenda-diary-file 'diary-file)
2870 (define-key calendar-mode-map
2871 org-calendar-insert-diary-entry-key
2872 'org-agenda-diary-entry))))))
2874 (defgroup org-latex nil
2875 "Options for embedding LaTeX code into Org-mode."
2876 :tag "Org LaTeX"
2877 :group 'org)
2879 (defcustom org-format-latex-options
2880 '(:foreground default :background default :scale 1.0
2881 :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
2882 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
2883 "Options for creating images from LaTeX fragments.
2884 This is a property list with the following properties:
2885 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
2886 `default' means use the foreground of the default face.
2887 :background the background color, or \"Transparent\".
2888 `default' means use the background of the default face.
2889 :scale a scaling factor for the size of the images.
2890 :html-foreground, :html-background, :html-scale
2891 the same numbers for HTML export.
2892 :matchers a list indicating which matchers should be used to
2893 find LaTeX fragments. Valid members of this list are:
2894 \"begin\" find environments
2895 \"$1\" find single characters surrounded by $.$
2896 \"$\" find math expressions surrounded by $...$
2897 \"$$\" find math expressions surrounded by $$....$$
2898 \"\\(\" find math expressions surrounded by \\(...\\)
2899 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2900 :group 'org-latex
2901 :type 'plist)
2903 (defcustom org-format-latex-signal-error t
2904 "Non-nil means signal an error when image creation of LaTeX snippets fails.
2905 When nil, just push out a message."
2906 :group 'org-latex
2907 :type 'boolean)
2909 (defcustom org-format-latex-header "\\documentclass{article}
2910 \\usepackage[usenames]{color}
2911 \\usepackage{amsmath}
2912 \\usepackage[mathscr]{eucal}
2913 \\pagestyle{empty} % do not remove
2914 \[PACKAGES]
2915 \[DEFAULT-PACKAGES]
2916 % The settings below are copied from fullpage.sty
2917 \\setlength{\\textwidth}{\\paperwidth}
2918 \\addtolength{\\textwidth}{-3cm}
2919 \\setlength{\\oddsidemargin}{1.5cm}
2920 \\addtolength{\\oddsidemargin}{-2.54cm}
2921 \\setlength{\\evensidemargin}{\\oddsidemargin}
2922 \\setlength{\\textheight}{\\paperheight}
2923 \\addtolength{\\textheight}{-\\headheight}
2924 \\addtolength{\\textheight}{-\\headsep}
2925 \\addtolength{\\textheight}{-\\footskip}
2926 \\addtolength{\\textheight}{-3cm}
2927 \\setlength{\\topmargin}{1.5cm}
2928 \\addtolength{\\topmargin}{-2.54cm}"
2929 "The document header used for processing LaTeX fragments.
2930 It is imperative that this header make sure that no page number
2931 appears on the page. The package defined in the variables
2932 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
2933 will either replace the placeholder \"[PACKAGES]\" in this header, or they
2934 will be appended."
2935 :group 'org-latex
2936 :type 'string)
2938 (defvar org-format-latex-header-extra nil)
2940 ;; The following variables are defined here because is it also used
2941 ;; when formatting latex fragments. Originally it was part of the
2942 ;; LaTeX exporter, which is why the name includes "export".
2943 (defcustom org-export-latex-default-packages-alist
2944 '(("AUTO" "inputenc")
2945 ("T1" "fontenc")
2946 ("" "graphicx")
2947 ("" "longtable")
2948 ("" "float")
2949 ("" "wrapfig")
2950 ("" "soul")
2951 ("" "t1enc")
2952 ("" "textcomp")
2953 ("" "marvosym")
2954 ("" "wasysym")
2955 ("" "latexsym")
2956 ("" "amssymb")
2957 ("" "hyperref"))
2958 "Alist of default packages to be inserted in the header.
2959 Change this only if one of the packages here causes an incompatibility
2960 with another package you are using.
2961 The packages in this list are needed by one part or another of Org-mode
2962 to function properly.
2964 - inputenc, fontenc, t1enc: for basic font and character selection
2965 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
2966 for interpreting the entities in `org-entities'. You can skip some of these
2967 packages if you don't use any of the symbols in it.
2968 - graphicx: for including images
2969 - float, wrapfig: for figure placement
2970 - longtable: for long tables
2971 - hyperref: for cross references
2973 Therefore you should not modify this variable unless you know what you
2974 are doing. The one reason to change it anyway is that you might be loading
2975 some other package that conflicts with one of the default packages.
2976 Each cell is of the format \( \"options\" \"package\" \)."
2977 :group 'org-export-latex
2978 :type '(repeat
2979 (list
2980 (string :tag "options")
2981 (string :tag "package"))))
2983 (defcustom org-export-latex-packages-alist nil
2984 "Alist of packages to be inserted in every LaTeX the header.
2985 These will be inserted after `org-export-latex-default-packages-alist'.
2986 Each cell is of the format \( \"options\" \"package\" \).
2987 Make sure that you only lis packages here which:
2988 - you want in every file
2989 - do not conflict with the default packages in
2990 `org-export-latex-default-packages-alist'
2991 - do not conflict with the setup in `org-format-latex-header'."
2992 :group 'org-export-latex
2993 :type '(repeat
2994 (list
2995 (string :tag "options")
2996 (string :tag "package"))))
2998 (defgroup org-appearance nil
2999 "Settings for Org-mode appearance."
3000 :tag "Org Appearance"
3001 :group 'org)
3003 (defcustom org-level-color-stars-only nil
3004 "Non-nil means fontify only the stars in each headline.
3005 When nil, the entire headline is fontified.
3006 Changing it requires restart of `font-lock-mode' to become effective
3007 also in regions already fontified."
3008 :group 'org-appearance
3009 :type 'boolean)
3011 (defcustom org-hide-leading-stars nil
3012 "Non-nil means hide the first N-1 stars in a headline.
3013 This works by using the face `org-hide' for these stars. This
3014 face is white for a light background, and black for a dark
3015 background. You may have to customize the face `org-hide' to
3016 make this work.
3017 Changing it requires restart of `font-lock-mode' to become effective
3018 also in regions already fontified.
3019 You may also set this on a per-file basis by adding one of the following
3020 lines to the buffer:
3022 #+STARTUP: hidestars
3023 #+STARTUP: showstars"
3024 :group 'org-appearance
3025 :type 'boolean)
3027 (defcustom org-hidden-keywords nil
3028 "List of keywords that should be hidden when typed in the org buffer.
3029 For example, add #+TITLE to this list in order to make the
3030 document title appear in the buffer without the initial #+TITLE:
3031 keyword."
3032 :group 'org-appearance
3033 :type '(set (const :tag "#+AUTHOR" author)
3034 (const :tag "#+DATE" date)
3035 (const :tag "#+EMAIL" email)
3036 (const :tag "#+TITLE" title)))
3038 (defcustom org-fontify-done-headline nil
3039 "Non-nil means change the face of a headline if it is marked DONE.
3040 Normally, only the TODO/DONE keyword indicates the state of a headline.
3041 When this is non-nil, the headline after the keyword is set to the
3042 `org-headline-done' as an additional indication."
3043 :group 'org-appearance
3044 :type 'boolean)
3046 (defcustom org-fontify-emphasized-text t
3047 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3048 Changing this variable requires a restart of Emacs to take effect."
3049 :group 'org-appearance
3050 :type 'boolean)
3052 (defcustom org-fontify-whole-heading-line nil
3053 "Non-nil means fontify the whole line for headings.
3054 This is useful when setting a background color for the
3055 org-level-* faces."
3056 :group 'org-appearance
3057 :type 'boolean)
3059 (defcustom org-highlight-latex-fragments-and-specials nil
3060 "Non-nil means fontify what is treated specially by the exporters."
3061 :group 'org-appearance
3062 :type 'boolean)
3064 (defcustom org-hide-emphasis-markers nil
3065 "Non-nil mean font-lock should hide the emphasis marker characters."
3066 :group 'org-appearance
3067 :type 'boolean)
3069 (defvar org-emph-re nil
3070 "Regular expression for matching emphasis.")
3071 (defvar org-verbatim-re nil
3072 "Regular expression for matching verbatim text.")
3073 (defvar org-emphasis-regexp-components) ; defined just below
3074 (defvar org-emphasis-alist) ; defined just below
3075 (defun org-set-emph-re (var val)
3076 "Set variable and compute the emphasis regular expression."
3077 (set var val)
3078 (when (and (boundp 'org-emphasis-alist)
3079 (boundp 'org-emphasis-regexp-components)
3080 org-emphasis-alist org-emphasis-regexp-components)
3081 (let* ((e org-emphasis-regexp-components)
3082 (pre (car e))
3083 (post (nth 1 e))
3084 (border (nth 2 e))
3085 (body (nth 3 e))
3086 (nl (nth 4 e))
3087 (body1 (concat body "*?"))
3088 (markers (mapconcat 'car org-emphasis-alist ""))
3089 (vmarkers (mapconcat
3090 (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
3091 org-emphasis-alist "")))
3092 ;; make sure special characters appear at the right position in the class
3093 (if (string-match "\\^" markers)
3094 (setq markers (concat (replace-match "" t t markers) "^")))
3095 (if (string-match "-" markers)
3096 (setq markers (concat (replace-match "" t t markers) "-")))
3097 (if (string-match "\\^" vmarkers)
3098 (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
3099 (if (string-match "-" vmarkers)
3100 (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
3101 (if (> nl 0)
3102 (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
3103 (int-to-string nl) "\\}")))
3104 ;; Make the regexp
3105 (setq org-emph-re
3106 (concat "\\([" pre "]\\|^\\)"
3107 "\\("
3108 "\\([" markers "]\\)"
3109 "\\("
3110 "[^" border "]\\|"
3111 "[^" border "]"
3112 body1
3113 "[^" border "]"
3114 "\\)"
3115 "\\3\\)"
3116 "\\([" post "]\\|$\\)"))
3117 (setq org-verbatim-re
3118 (concat "\\([" pre "]\\|^\\)"
3119 "\\("
3120 "\\([" vmarkers "]\\)"
3121 "\\("
3122 "[^" border "]\\|"
3123 "[^" border "]"
3124 body1
3125 "[^" border "]"
3126 "\\)"
3127 "\\3\\)"
3128 "\\([" post "]\\|$\\)")))))
3130 (defcustom org-emphasis-regexp-components
3131 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3132 "Components used to build the regular expression for emphasis.
3133 This is a list with 6 entries. Terminology: In an emphasis string
3134 like \" *strong word* \", we call the initial space PREMATCH, the final
3135 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3136 and \"trong wor\" is the body. The different components in this variable
3137 specify what is allowed/forbidden in each part:
3139 pre Chars allowed as prematch. Beginning of line will be allowed too.
3140 post Chars allowed as postmatch. End of line will be allowed too.
3141 border The chars *forbidden* as border characters.
3142 body-regexp A regexp like \".\" to match a body character. Don't use
3143 non-shy groups here, and don't allow newline here.
3144 newline The maximum number of newlines allowed in an emphasis exp.
3146 Use customize to modify this, or restart Emacs after changing it."
3147 :group 'org-appearance
3148 :set 'org-set-emph-re
3149 :type '(list
3150 (sexp :tag "Allowed chars in pre ")
3151 (sexp :tag "Allowed chars in post ")
3152 (sexp :tag "Forbidden chars in border ")
3153 (sexp :tag "Regexp for body ")
3154 (integer :tag "number of newlines allowed")
3155 (option (boolean :tag "Please ignore this button"))))
3157 (defcustom org-emphasis-alist
3158 `(("*" bold "<b>" "</b>")
3159 ("/" italic "<i>" "</i>")
3160 ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
3161 ("=" org-code "<code>" "</code>" verbatim)
3162 ("~" org-verbatim "<code>" "</code>" verbatim)
3163 ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
3164 "<del>" "</del>")
3166 "Special syntax for emphasized text.
3167 Text starting and ending with a special character will be emphasized, for
3168 example *bold*, _underlined_ and /italic/. This variable sets the marker
3169 characters, the face to be used by font-lock for highlighting in Org-mode
3170 Emacs buffers, and the HTML tags to be used for this.
3171 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3172 Use customize to modify this, or restart Emacs after changing it."
3173 :group 'org-appearance
3174 :set 'org-set-emph-re
3175 :type '(repeat
3176 (list
3177 (string :tag "Marker character")
3178 (choice
3179 (face :tag "Font-lock-face")
3180 (plist :tag "Face property list"))
3181 (string :tag "HTML start tag")
3182 (string :tag "HTML end tag")
3183 (option (const verbatim)))))
3185 (defvar org-protecting-blocks
3186 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3187 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3188 This is needed for font-lock setup.")
3190 ;;; Miscellaneous options
3192 (defgroup org-completion nil
3193 "Completion in Org-mode."
3194 :tag "Org Completion"
3195 :group 'org)
3197 (defcustom org-completion-use-ido nil
3198 "Non-nil means use ido completion wherever possible.
3199 Note that `ido-mode' must be active for this variable to be relevant.
3200 If you decide to turn this variable on, you might well want to turn off
3201 `org-outline-path-complete-in-steps'.
3202 See also `org-completion-use-iswitchb'."
3203 :group 'org-completion
3204 :type 'boolean)
3206 (defcustom org-completion-use-iswitchb nil
3207 "Non-nil means use iswitchb completion wherever possible.
3208 Note that `iswitchb-mode' must be active for this variable to be relevant.
3209 If you decide to turn this variable on, you might well want to turn off
3210 `org-outline-path-complete-in-steps'.
3211 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3212 :group 'org-completion
3213 :type 'boolean)
3215 (defcustom org-completion-fallback-command 'hippie-expand
3216 "The expansion command called by \\[org-complete] in normal context.
3217 Normal means no org-mode-specific context."
3218 :group 'org-completion
3219 :type 'function)
3221 ;;; Functions and variables from their packages
3222 ;; Declared here to avoid compiler warnings
3224 ;; XEmacs only
3225 (defvar outline-mode-menu-heading)
3226 (defvar outline-mode-menu-show)
3227 (defvar outline-mode-menu-hide)
3228 (defvar zmacs-regions) ; XEmacs regions
3230 ;; Emacs only
3231 (defvar mark-active)
3233 ;; Various packages
3234 (declare-function calendar-absolute-from-iso "cal-iso" (date))
3235 (declare-function calendar-forward-day "cal-move" (arg))
3236 (declare-function calendar-goto-date "cal-move" (date))
3237 (declare-function calendar-goto-today "cal-move" ())
3238 (declare-function calendar-iso-from-absolute "cal-iso" (date))
3239 (defvar calc-embedded-close-formula)
3240 (defvar calc-embedded-open-formula)
3241 (declare-function cdlatex-tab "ext:cdlatex" ())
3242 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
3243 (defvar font-lock-unfontify-region-function)
3244 (declare-function iswitchb-read-buffer "iswitchb"
3245 (prompt &optional default require-match start matches-set))
3246 (defvar iswitchb-temp-buflist)
3247 (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
3248 (defvar org-agenda-tags-todo-honor-ignore-options)
3249 (declare-function org-agenda-skip "org-agenda" ())
3250 (declare-function
3251 org-format-agenda-item "org-agenda"
3252 (extra txt &optional category tags dotime noprefix remove-re habitp))
3253 (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
3254 (declare-function org-agenda-change-all-lines "org-agenda"
3255 (newhead hdmarker &optional fixface just-this))
3256 (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
3257 (declare-function org-agenda-maybe-redo "org-agenda" ())
3258 (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
3259 (beg end))
3260 (declare-function org-agenda-copy-local-variable "org-agenda" (var))
3261 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3262 "org-agenda" (&optional end))
3263 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
3264 (declare-function org-indent-mode "org-indent" (&optional arg))
3265 (declare-function parse-time-string "parse-time" (string))
3266 (declare-function org-attach-reveal "org-attach" (&optional if-exists))
3267 (defvar remember-data-file)
3268 (defvar texmathp-why)
3269 (declare-function speedbar-line-directory "speedbar" (&optional depth))
3270 (declare-function table--at-cell-p "table" (position &optional object at-column))
3272 (defvar w3m-current-url)
3273 (defvar w3m-current-title)
3275 (defvar org-latex-regexps)
3277 ;;; Autoload and prepare some org modules
3279 ;; Some table stuff that needs to be defined here, because it is used
3280 ;; by the functions setting up org-mode or checking for table context.
3282 (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
3283 "Detects an org-type or table-type table.")
3284 (defconst org-table-line-regexp "^[ \t]*|"
3285 "Detects an org-type table line.")
3286 (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
3287 "Detects an org-type table line.")
3288 (defconst org-table-hline-regexp "^[ \t]*|-"
3289 "Detects an org-type table hline.")
3290 (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
3291 "Detects a table-type table hline.")
3292 (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
3293 "Searching from within a table (any type) this finds the first line
3294 outside the table.")
3296 ;; Autoload the functions in org-table.el that are needed by functions here.
3298 (eval-and-compile
3299 (org-autoload "org-table"
3300 '(org-table-align org-table-begin org-table-blank-field
3301 org-table-convert org-table-convert-region org-table-copy-down
3302 org-table-copy-region org-table-create
3303 org-table-create-or-convert-from-region
3304 org-table-create-with-table.el org-table-current-dline
3305 org-table-cut-region org-table-delete-column org-table-edit-field
3306 org-table-edit-formulas org-table-end org-table-eval-formula
3307 org-table-export org-table-field-info
3308 org-table-get-stored-formulas org-table-goto-column
3309 org-table-hline-and-move org-table-import org-table-insert-column
3310 org-table-insert-hline org-table-insert-row org-table-iterate
3311 org-table-justify-field-maybe org-table-kill-row
3312 org-table-maybe-eval-formula org-table-maybe-recalculate-line
3313 org-table-move-column org-table-move-column-left
3314 org-table-move-column-right org-table-move-row
3315 org-table-move-row-down org-table-move-row-up
3316 org-table-next-field org-table-next-row org-table-paste-rectangle
3317 org-table-previous-field org-table-recalculate
3318 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
3319 org-table-toggle-coordinate-overlays
3320 org-table-toggle-formula-debugger org-table-wrap-region
3321 orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
3323 (defun org-at-table-p (&optional table-type)
3324 "Return t if the cursor is inside an org-type table.
3325 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3326 (if org-enable-table-editor
3327 (save-excursion
3328 (beginning-of-line 1)
3329 (looking-at (if table-type org-table-any-line-regexp
3330 org-table-line-regexp)))
3331 nil))
3332 (defsubst org-table-p () (org-at-table-p))
3334 (defun org-at-table.el-p ()
3335 "Return t if and only if we are at a table.el table."
3336 (and (org-at-table-p 'any)
3337 (save-excursion
3338 (goto-char (org-table-begin 'any))
3339 (looking-at org-table1-hline-regexp))))
3340 (defun org-table-recognize-table.el ()
3341 "If there is a table.el table nearby, recognize it and move into it."
3342 (if org-table-tab-recognizes-table.el
3343 (if (org-at-table.el-p)
3344 (progn
3345 (beginning-of-line 1)
3346 (if (looking-at org-table-dataline-regexp)
3348 (if (looking-at org-table1-hline-regexp)
3349 (progn
3350 (beginning-of-line 2)
3351 (if (looking-at org-table-any-border-regexp)
3352 (beginning-of-line -1)))))
3353 (if (re-search-forward "|" (org-table-end t) t)
3354 (progn
3355 (require 'table)
3356 (if (table--at-cell-p (point))
3358 (message "recognizing table.el table...")
3359 (table-recognize-table)
3360 (message "recognizing table.el table...done")))
3361 (error "This should not happen..."))
3363 nil)
3364 nil))
3366 (defun org-at-table-hline-p ()
3367 "Return t if the cursor is inside a hline in a table."
3368 (if org-enable-table-editor
3369 (save-excursion
3370 (beginning-of-line 1)
3371 (looking-at org-table-hline-regexp))
3372 nil))
3374 (defvar org-table-clean-did-remove-column nil)
3376 (defun org-table-map-tables (function)
3377 "Apply FUNCTION to the start of all tables in the buffer."
3378 (save-excursion
3379 (save-restriction
3380 (widen)
3381 (goto-char (point-min))
3382 (while (re-search-forward org-table-any-line-regexp nil t)
3383 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
3384 (beginning-of-line 1)
3385 (when (looking-at org-table-line-regexp)
3386 (save-excursion (funcall function))
3387 (or (looking-at org-table-line-regexp)
3388 (forward-char 1)))
3389 (re-search-forward org-table-any-border-regexp nil 1))))
3390 (message "Mapping tables: done"))
3392 ;; Declare and autoload functions from org-exp.el & Co
3394 (declare-function org-default-export-plist "org-exp")
3395 (declare-function org-infile-export-plist "org-exp")
3396 (declare-function org-get-current-options "org-exp")
3397 (eval-and-compile
3398 (org-autoload "org-exp"
3399 '(org-export org-export-visible
3400 org-insert-export-options-template
3401 org-table-clean-before-export))
3402 (org-autoload "org-ascii"
3403 '(org-export-as-ascii org-export-ascii-preprocess
3404 org-export-as-ascii-to-buffer org-replace-region-by-ascii
3405 org-export-region-as-ascii))
3406 (org-autoload "org-latex"
3407 '(org-export-as-latex-batch org-export-as-latex-to-buffer
3408 org-replace-region-by-latex org-export-region-as-latex
3409 org-export-as-latex org-export-as-pdf
3410 org-export-as-pdf-and-open))
3411 (org-autoload "org-html"
3412 '(org-export-as-html-and-open
3413 org-export-as-html-batch org-export-as-html-to-buffer
3414 org-replace-region-by-html org-export-region-as-html
3415 org-export-as-html))
3416 (org-autoload "org-docbook"
3417 '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
3418 org-replace-region-by-docbook org-export-region-as-docbook
3419 org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
3420 org-export-as-docbook))
3421 (org-autoload "org-icalendar"
3422 '(org-export-icalendar-this-file
3423 org-export-icalendar-all-agenda-files
3424 org-export-icalendar-combine-agenda-files))
3425 (org-autoload "org-xoxo" '(org-export-as-xoxo))
3426 (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
3428 ;; Declare and autoload functions from org-agenda.el
3430 (eval-and-compile
3431 (org-autoload "org-agenda"
3432 '(org-agenda org-agenda-list org-search-view
3433 org-todo-list org-tags-view org-agenda-list-stuck-projects
3434 org-diary org-agenda-to-appt
3435 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
3437 ;; Autoload org-remember
3439 (eval-and-compile
3440 (org-autoload "org-remember"
3441 '(org-remember-insinuate org-remember-annotation
3442 org-remember-apply-template org-remember org-remember-handler)))
3444 ;; Autoload org-clock.el
3447 (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
3448 (beg end))
3449 (declare-function org-clock-update-mode-line "org-clock" ())
3450 (declare-function org-resolve-clocks "org-clock"
3451 (&optional also-non-dangling-p prompt last-valid))
3452 (defvar org-clock-start-time)
3453 (defvar org-clock-marker (make-marker)
3454 "Marker recording the last clock-in.")
3455 (defvar org-clock-hd-marker (make-marker)
3456 "Marker recording the last clock-in, but the headline position.")
3457 (defvar org-clock-heading ""
3458 "The heading of the current clock entry.")
3459 (defun org-clock-is-active ()
3460 "Return non-nil if clock is currently running.
3461 The return value is actually the clock marker."
3462 (marker-buffer org-clock-marker))
3464 (eval-and-compile
3465 (org-autoload
3466 "org-clock"
3467 '(org-clock-in org-clock-out org-clock-cancel
3468 org-clock-goto org-clock-sum org-clock-display
3469 org-clock-remove-overlays org-clock-report
3470 org-clocktable-shift org-dblock-write:clocktable
3471 org-get-clocktable org-resolve-clocks)))
3473 (defun org-clock-update-time-maybe ()
3474 "If this is a CLOCK line, update it and return t.
3475 Otherwise, return nil."
3476 (interactive)
3477 (save-excursion
3478 (beginning-of-line 1)
3479 (skip-chars-forward " \t")
3480 (when (looking-at org-clock-string)
3481 (let ((re (concat "[ \t]*" org-clock-string
3482 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
3483 "\\([ \t]*=>.*\\)?\\)?"))
3484 ts te h m s neg)
3485 (cond
3486 ((not (looking-at re))
3487 nil)
3488 ((not (match-end 2))
3489 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
3490 (> org-clock-marker (point))
3491 (<= org-clock-marker (point-at-eol)))
3492 ;; The clock is running here
3493 (setq org-clock-start-time
3494 (apply 'encode-time
3495 (org-parse-time-string (match-string 1))))
3496 (org-clock-update-mode-line)))
3498 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
3499 (end-of-line 1)
3500 (setq ts (match-string 1)
3501 te (match-string 3))
3502 (setq s (- (org-float-time
3503 (apply 'encode-time (org-parse-time-string te)))
3504 (org-float-time
3505 (apply 'encode-time (org-parse-time-string ts))))
3506 neg (< s 0)
3507 s (abs s)
3508 h (floor (/ s 3600))
3509 s (- s (* 3600 h))
3510 m (floor (/ s 60))
3511 s (- s (* 60 s)))
3512 (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
3513 t))))))
3515 (defun org-check-running-clock ()
3516 "Check if the current buffer contains the running clock.
3517 If yes, offer to stop it and to save the buffer with the changes."
3518 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
3519 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
3520 (buffer-name))))
3521 (org-clock-out)
3522 (when (y-or-n-p "Save changed buffer?")
3523 (save-buffer))))
3525 (defun org-clocktable-try-shift (dir n)
3526 "Check if this line starts a clock table, if yes, shift the time block."
3527 (when (org-match-line "#\\+BEGIN: clocktable\\>")
3528 (org-clocktable-shift dir n)))
3530 ;; Autoload org-timer.el
3532 (eval-and-compile
3533 (org-autoload
3534 "org-timer"
3535 '(org-timer-start org-timer org-timer-item
3536 org-timer-change-times-in-region
3537 org-timer-set-timer
3538 org-timer-reset-timers
3539 org-timer-show-remaining-time)))
3541 ;; Autoload org-feed.el
3543 (eval-and-compile
3544 (org-autoload
3545 "org-feed"
3546 '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
3549 ;; Autoload org-indent.el
3551 ;; Define the variable already here, to make sure we have it.
3552 (defvar org-indent-mode nil
3553 "Non-nil if Org-Indent mode is enabled.
3554 Use the command `org-indent-mode' to change this variable.")
3556 (eval-and-compile
3557 (org-autoload
3558 "org-indent"
3559 '(org-indent-mode)))
3561 ;; Autoload org-mobile.el
3563 (eval-and-compile
3564 (org-autoload
3565 "org-mobile"
3566 '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
3568 ;; Autoload archiving code
3569 ;; The stuff that is needed for cycling and tags has to be defined here.
3571 (defgroup org-archive nil
3572 "Options concerning archiving in Org-mode."
3573 :tag "Org Archive"
3574 :group 'org-structure)
3576 (defcustom org-archive-location "%s_archive::"
3577 "The location where subtrees should be archived.
3579 The value of this variable is a string, consisting of two parts,
3580 separated by a double-colon. The first part is a filename and
3581 the second part is a headline.
3583 When the filename is omitted, archiving happens in the same file.
3584 %s in the filename will be replaced by the current file
3585 name (without the directory part). Archiving to a different file
3586 is useful to keep archived entries from contributing to the
3587 Org-mode Agenda.
3589 The archived entries will be filed as subtrees of the specified
3590 headline. When the headline is omitted, the subtrees are simply
3591 filed away at the end of the file, as top-level entries. Also in
3592 the heading you can use %s to represent the file name, this can be
3593 useful when using the same archive for a number of different files.
3595 Here are a few examples:
3596 \"%s_archive::\"
3597 If the current file is Projects.org, archive in file
3598 Projects.org_archive, as top-level trees. This is the default.
3600 \"::* Archived Tasks\"
3601 Archive in the current file, under the top-level headline
3602 \"* Archived Tasks\".
3604 \"~/org/archive.org::\"
3605 Archive in file ~/org/archive.org (absolute path), as top-level trees.
3607 \"~/org/archive.org::From %s\"
3608 Archive in file ~/org/archive.org (absolute path), under headlines
3609 \"From FILENAME\" where file name is the current file name.
3611 \"basement::** Finished Tasks\"
3612 Archive in file ./basement (relative path), as level 3 trees
3613 below the level 2 heading \"** Finished Tasks\".
3615 You may set this option on a per-file basis by adding to the buffer a
3616 line like
3618 #+ARCHIVE: basement::** Finished Tasks
3620 You may also define it locally for a subtree by setting an ARCHIVE property
3621 in the entry. If such a property is found in an entry, or anywhere up
3622 the hierarchy, it will be used."
3623 :group 'org-archive
3624 :type 'string)
3626 (defcustom org-archive-tag "ARCHIVE"
3627 "The tag that marks a subtree as archived.
3628 An archived subtree does not open during visibility cycling, and does
3629 not contribute to the agenda listings.
3630 After changing this, font-lock must be restarted in the relevant buffers to
3631 get the proper fontification."
3632 :group 'org-archive
3633 :group 'org-keywords
3634 :type 'string)
3636 (defcustom org-agenda-skip-archived-trees t
3637 "Non-nil means the agenda will skip any items located in archived trees.
3638 An archived tree is a tree marked with the tag ARCHIVE. The use of this
3639 variable is no longer recommended, you should leave it at the value t.
3640 Instead, use the key `v' to cycle the archives-mode in the agenda."
3641 :group 'org-archive
3642 :group 'org-agenda-skip
3643 :type 'boolean)
3645 (defcustom org-columns-skip-archived-trees t
3646 "Non-nil means ignore archived trees when creating column view."
3647 :group 'org-archive
3648 :group 'org-properties
3649 :type 'boolean)
3651 (defcustom org-cycle-open-archived-trees nil
3652 "Non-nil means `org-cycle' will open archived trees.
3653 An archived tree is a tree marked with the tag ARCHIVE.
3654 When nil, archived trees will stay folded. You can still open them with
3655 normal outline commands like `show-all', but not with the cycling commands."
3656 :group 'org-archive
3657 :group 'org-cycle
3658 :type 'boolean)
3660 (defcustom org-sparse-tree-open-archived-trees nil
3661 "Non-nil means sparse tree construction shows matches in archived trees.
3662 When nil, matches in these trees are highlighted, but the trees are kept in
3663 collapsed state."
3664 :group 'org-archive
3665 :group 'org-sparse-trees
3666 :type 'boolean)
3668 (defun org-cycle-hide-archived-subtrees (state)
3669 "Re-hide all archived subtrees after a visibility state change."
3670 (when (and (not org-cycle-open-archived-trees)
3671 (not (memq state '(overview folded))))
3672 (save-excursion
3673 (let* ((globalp (memq state '(contents all)))
3674 (beg (if globalp (point-min) (point)))
3675 (end (if globalp (point-max) (org-end-of-subtree t))))
3676 (org-hide-archived-subtrees beg end)
3677 (goto-char beg)
3678 (if (looking-at (concat ".*:" org-archive-tag ":"))
3679 (message "%s" (substitute-command-keys
3680 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
3682 (defun org-force-cycle-archived ()
3683 "Cycle subtree even if it is archived."
3684 (interactive)
3685 (setq this-command 'org-cycle)
3686 (let ((org-cycle-open-archived-trees t))
3687 (call-interactively 'org-cycle)))
3689 (defun org-hide-archived-subtrees (beg end)
3690 "Re-hide all archived subtrees after a visibility state change."
3691 (save-excursion
3692 (let* ((re (concat ":" org-archive-tag ":")))
3693 (goto-char beg)
3694 (while (re-search-forward re end t)
3695 (when (org-on-heading-p)
3696 (org-flag-subtree t)
3697 (org-end-of-subtree t))))))
3699 (defun org-flag-subtree (flag)
3700 (save-excursion
3701 (org-back-to-heading t)
3702 (outline-end-of-heading)
3703 (outline-flag-region (point)
3704 (progn (org-end-of-subtree t) (point))
3705 flag)))
3707 (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
3709 (eval-and-compile
3710 (org-autoload "org-archive"
3711 '(org-add-archive-files org-archive-subtree
3712 org-archive-to-archive-sibling org-toggle-archive-tag
3713 org-archive-subtree-default
3714 org-archive-subtree-default-with-confirmation)))
3716 ;; Autoload Column View Code
3718 (declare-function org-columns-number-to-string "org-colview")
3719 (declare-function org-columns-get-format-and-top-level "org-colview")
3720 (declare-function org-columns-compute "org-colview")
3722 (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
3723 '(org-columns-number-to-string org-columns-get-format-and-top-level
3724 org-columns-compute org-agenda-columns org-columns-remove-overlays
3725 org-columns org-insert-columns-dblock org-dblock-write:columnview))
3727 ;; Autoload ID code
3729 (declare-function org-id-store-link "org-id")
3730 (declare-function org-id-locations-load "org-id")
3731 (declare-function org-id-locations-save "org-id")
3732 (defvar org-id-track-globally)
3733 (org-autoload "org-id"
3734 '(org-id-get-create org-id-new org-id-copy org-id-get
3735 org-id-get-with-outline-path-completion
3736 org-id-get-with-outline-drilling
3737 org-id-goto org-id-find org-id-store-link))
3739 ;; Autoload Plotting Code
3741 (org-autoload "org-plot"
3742 '(org-plot/gnuplot))
3744 ;;; Variables for pre-computed regular expressions, all buffer local
3746 (defvar org-drawer-regexp nil
3747 "Matches first line of a hidden block.")
3748 (make-variable-buffer-local 'org-drawer-regexp)
3749 (defvar org-todo-regexp nil
3750 "Matches any of the TODO state keywords.")
3751 (make-variable-buffer-local 'org-todo-regexp)
3752 (defvar org-not-done-regexp nil
3753 "Matches any of the TODO state keywords except the last one.")
3754 (make-variable-buffer-local 'org-not-done-regexp)
3755 (defvar org-not-done-heading-regexp nil
3756 "Matches a TODO headline that is not done.")
3757 (make-variable-buffer-local 'org-not-done-regexp)
3758 (defvar org-todo-line-regexp nil
3759 "Matches a headline and puts TODO state into group 2 if present.")
3760 (make-variable-buffer-local 'org-todo-line-regexp)
3761 (defvar org-complex-heading-regexp nil
3762 "Matches a headline and puts everything into groups:
3763 group 1: the stars
3764 group 2: The todo keyword, maybe
3765 group 3: Priority cookie
3766 group 4: True headline
3767 group 5: Tags")
3768 (make-variable-buffer-local 'org-complex-heading-regexp)
3769 (defvar org-complex-heading-regexp-format nil)
3770 (make-variable-buffer-local 'org-complex-heading-regexp-format)
3771 (defvar org-todo-line-tags-regexp nil
3772 "Matches a headline and puts TODO state into group 2 if present.
3773 Also put tags into group 4 if tags are present.")
3774 (make-variable-buffer-local 'org-todo-line-tags-regexp)
3775 (defvar org-nl-done-regexp nil
3776 "Matches newline followed by a headline with the DONE keyword.")
3777 (make-variable-buffer-local 'org-nl-done-regexp)
3778 (defvar org-looking-at-done-regexp nil
3779 "Matches the DONE keyword a point.")
3780 (make-variable-buffer-local 'org-looking-at-done-regexp)
3781 (defvar org-ds-keyword-length 12
3782 "Maximum length of the Deadline and SCHEDULED keywords.")
3783 (make-variable-buffer-local 'org-ds-keyword-length)
3784 (defvar org-deadline-regexp nil
3785 "Matches the DEADLINE keyword.")
3786 (make-variable-buffer-local 'org-deadline-regexp)
3787 (defvar org-deadline-time-regexp nil
3788 "Matches the DEADLINE keyword together with a time stamp.")
3789 (make-variable-buffer-local 'org-deadline-time-regexp)
3790 (defvar org-deadline-line-regexp nil
3791 "Matches the DEADLINE keyword and the rest of the line.")
3792 (make-variable-buffer-local 'org-deadline-line-regexp)
3793 (defvar org-scheduled-regexp nil
3794 "Matches the SCHEDULED keyword.")
3795 (make-variable-buffer-local 'org-scheduled-regexp)
3796 (defvar org-scheduled-time-regexp nil
3797 "Matches the SCHEDULED keyword together with a time stamp.")
3798 (make-variable-buffer-local 'org-scheduled-time-regexp)
3799 (defvar org-closed-time-regexp nil
3800 "Matches the CLOSED keyword together with a time stamp.")
3801 (make-variable-buffer-local 'org-closed-time-regexp)
3803 (defvar org-keyword-time-regexp nil
3804 "Matches any of the 4 keywords, together with the time stamp.")
3805 (make-variable-buffer-local 'org-keyword-time-regexp)
3806 (defvar org-keyword-time-not-clock-regexp nil
3807 "Matches any of the 3 keywords, together with the time stamp.")
3808 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
3809 (defvar org-maybe-keyword-time-regexp nil
3810 "Matches a timestamp, possibly preceeded by a keyword.")
3811 (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
3812 (defvar org-planning-or-clock-line-re nil
3813 "Matches a line with planning or clock info.")
3814 (make-variable-buffer-local 'org-planning-or-clock-line-re)
3815 (defvar org-all-time-keywords nil
3816 "List of time keywords.")
3817 (make-variable-buffer-local 'org-all-time-keywords)
3819 (defconst org-plain-time-of-day-regexp
3820 (concat
3821 "\\(\\<[012]?[0-9]"
3822 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3823 "\\(--?"
3824 "\\(\\<[012]?[0-9]"
3825 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3826 "\\)?")
3827 "Regular expression to match a plain time or time range.
3828 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3829 groups carry important information:
3830 0 the full match
3831 1 the first time, range or not
3832 8 the second time, if it is a range.")
3834 (defconst org-plain-time-extension-regexp
3835 (concat
3836 "\\(\\<[012]?[0-9]"
3837 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3838 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
3839 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
3840 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3841 groups carry important information:
3842 0 the full match
3843 7 hours of duration
3844 9 minutes of duration")
3846 (defconst org-stamp-time-of-day-regexp
3847 (concat
3848 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
3849 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
3850 "\\(--?"
3851 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
3852 "Regular expression to match a timestamp time or time range.
3853 After a match, the following groups carry important information:
3854 0 the full match
3855 1 date plus weekday, for back referencing to make sure both times are on the same day
3856 2 the first time, range or not
3857 4 the second time, if it is a range.")
3859 (defconst org-startup-options
3860 '(("fold" org-startup-folded t)
3861 ("overview" org-startup-folded t)
3862 ("nofold" org-startup-folded nil)
3863 ("showall" org-startup-folded nil)
3864 ("showeverything" org-startup-folded showeverything)
3865 ("content" org-startup-folded content)
3866 ("indent" org-startup-indented t)
3867 ("noindent" org-startup-indented nil)
3868 ("hidestars" org-hide-leading-stars t)
3869 ("showstars" org-hide-leading-stars nil)
3870 ("odd" org-odd-levels-only t)
3871 ("oddeven" org-odd-levels-only nil)
3872 ("align" org-startup-align-all-tables t)
3873 ("noalign" org-startup-align-all-tables nil)
3874 ("customtime" org-display-custom-times t)
3875 ("logdone" org-log-done time)
3876 ("lognotedone" org-log-done note)
3877 ("nologdone" org-log-done nil)
3878 ("lognoteclock-out" org-log-note-clock-out t)
3879 ("nolognoteclock-out" org-log-note-clock-out nil)
3880 ("logrepeat" org-log-repeat state)
3881 ("lognoterepeat" org-log-repeat note)
3882 ("nologrepeat" org-log-repeat nil)
3883 ("logreschedule" org-log-reschedule time)
3884 ("lognotereschedule" org-log-reschedule note)
3885 ("nologreschedule" org-log-reschedule nil)
3886 ("logredeadline" org-log-redeadline time)
3887 ("lognoteredeadline" org-log-redeadline note)
3888 ("nologredeadline" org-log-redeadline nil)
3889 ("logrefile" org-log-refile time)
3890 ("lognoterefile" org-log-refile note)
3891 ("nologrefile" org-log-refile nil)
3892 ("fninline" org-footnote-define-inline t)
3893 ("nofninline" org-footnote-define-inline nil)
3894 ("fnlocal" org-footnote-section nil)
3895 ("fnauto" org-footnote-auto-label t)
3896 ("fnprompt" org-footnote-auto-label nil)
3897 ("fnconfirm" org-footnote-auto-label confirm)
3898 ("fnplain" org-footnote-auto-label plain)
3899 ("fnadjust" org-footnote-auto-adjust t)
3900 ("nofnadjust" org-footnote-auto-adjust nil)
3901 ("constcgs" constants-unit-system cgs)
3902 ("constSI" constants-unit-system SI)
3903 ("noptag" org-tag-persistent-alist nil)
3904 ("hideblocks" org-hide-block-startup t)
3905 ("nohideblocks" org-hide-block-startup nil)
3906 ("beamer" org-startup-with-beamer-mode t))
3907 "Variable associated with STARTUP options for org-mode.
3908 Each element is a list of three items: The startup options as written
3909 in the #+STARTUP line, the corresponding variable, and the value to
3910 set this variable to if the option is found. An optional forth element PUSH
3911 means to push this value onto the list in the variable.")
3913 (defun org-set-regexps-and-options ()
3914 "Precompute regular expressions for current buffer."
3915 (when (org-mode-p)
3916 (org-set-local 'org-todo-kwd-alist nil)
3917 (org-set-local 'org-todo-key-alist nil)
3918 (org-set-local 'org-todo-key-trigger nil)
3919 (org-set-local 'org-todo-keywords-1 nil)
3920 (org-set-local 'org-done-keywords nil)
3921 (org-set-local 'org-todo-heads nil)
3922 (org-set-local 'org-todo-sets nil)
3923 (org-set-local 'org-todo-log-states nil)
3924 (org-set-local 'org-file-properties nil)
3925 (org-set-local 'org-file-tags nil)
3926 (let ((re (org-make-options-regexp
3927 '("CATEGORY" "TODO" "COLUMNS"
3928 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
3929 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
3930 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
3931 (splitre "[ \t]+")
3932 kwds kws0 kwsa key log value cat arch tags const links hw dws
3933 tail sep kws1 prio props ftags drawers beamer-p
3934 ext-setup-or-nil setup-contents (start 0))
3935 (save-excursion
3936 (save-restriction
3937 (widen)
3938 (goto-char (point-min))
3939 (while (or (and ext-setup-or-nil
3940 (string-match re ext-setup-or-nil start)
3941 (setq start (match-end 0)))
3942 (and (setq ext-setup-or-nil nil start 0)
3943 (re-search-forward re nil t)))
3944 (setq key (upcase (match-string 1 ext-setup-or-nil))
3945 value (org-match-string-no-properties 2 ext-setup-or-nil))
3946 (cond
3947 ((equal key "CATEGORY")
3948 (if (string-match "[ \t]+$" value)
3949 (setq value (replace-match "" t t value)))
3950 (setq cat value))
3951 ((member key '("SEQ_TODO" "TODO"))
3952 (push (cons 'sequence (org-split-string value splitre)) kwds))
3953 ((equal key "TYP_TODO")
3954 (push (cons 'type (org-split-string value splitre)) kwds))
3955 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
3956 ;; general TODO-like setup
3957 (push (cons (intern (downcase (match-string 1 key)))
3958 (org-split-string value splitre)) kwds))
3959 ((equal key "TAGS")
3960 (setq tags (append tags (if tags '("\\n") nil)
3961 (org-split-string value splitre))))
3962 ((equal key "COLUMNS")
3963 (org-set-local 'org-columns-default-format value))
3964 ((equal key "LINK")
3965 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
3966 (push (cons (match-string 1 value)
3967 (org-trim (match-string 2 value)))
3968 links)))
3969 ((equal key "PRIORITIES")
3970 (setq prio (org-split-string value " +")))
3971 ((equal key "PROPERTY")
3972 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
3973 (push (cons (match-string 1 value) (match-string 2 value))
3974 props)))
3975 ((equal key "FILETAGS")
3976 (when (string-match "\\S-" value)
3977 (setq ftags
3978 (append
3979 ftags
3980 (apply 'append
3981 (mapcar (lambda (x) (org-split-string x ":"))
3982 (org-split-string value)))))))
3983 ((equal key "DRAWERS")
3984 (setq drawers (org-split-string value splitre)))
3985 ((equal key "CONSTANTS")
3986 (setq const (append const (org-split-string value splitre))))
3987 ((equal key "STARTUP")
3988 (let ((opts (org-split-string value splitre))
3989 l var val)
3990 (while (setq l (pop opts))
3991 (when (setq l (assoc l org-startup-options))
3992 (setq var (nth 1 l) val (nth 2 l))
3993 (if (not (nth 3 l))
3994 (set (make-local-variable var) val)
3995 (if (not (listp (symbol-value var)))
3996 (set (make-local-variable var) nil))
3997 (set (make-local-variable var) (symbol-value var))
3998 (add-to-list var val))))))
3999 ((equal key "ARCHIVE")
4000 (string-match " *$" value)
4001 (setq arch (replace-match "" t t value))
4002 (remove-text-properties 0 (length arch)
4003 '(face t fontified t) arch))
4004 ((equal key "LATEX_CLASS")
4005 (setq beamer-p (equal value "beamer")))
4006 ((equal key "SETUPFILE")
4007 (setq setup-contents (org-file-contents
4008 (expand-file-name
4009 (org-remove-double-quotes value))
4010 'noerror))
4011 (if (not ext-setup-or-nil)
4012 (setq ext-setup-or-nil setup-contents start 0)
4013 (setq ext-setup-or-nil
4014 (concat (substring ext-setup-or-nil 0 start)
4015 "\n" setup-contents "\n"
4016 (substring ext-setup-or-nil start)))))
4017 ))))
4018 (when cat
4019 (org-set-local 'org-category (intern cat))
4020 (push (cons "CATEGORY" cat) props))
4021 (when prio
4022 (if (< (length prio) 3) (setq prio '("A" "C" "B")))
4023 (setq prio (mapcar 'string-to-char prio))
4024 (org-set-local 'org-highest-priority (nth 0 prio))
4025 (org-set-local 'org-lowest-priority (nth 1 prio))
4026 (org-set-local 'org-default-priority (nth 2 prio)))
4027 (and props (org-set-local 'org-file-properties (nreverse props)))
4028 (and ftags (org-set-local 'org-file-tags
4029 (mapcar 'org-add-prop-inherited ftags)))
4030 (and drawers (org-set-local 'org-drawers drawers))
4031 (and arch (org-set-local 'org-archive-location arch))
4032 (and links (setq org-link-abbrev-alist-local (nreverse links)))
4033 ;; Process the TODO keywords
4034 (unless kwds
4035 ;; Use the global values as if they had been given locally.
4036 (setq kwds (default-value 'org-todo-keywords))
4037 (if (stringp (car kwds))
4038 (setq kwds (list (cons org-todo-interpretation
4039 (default-value 'org-todo-keywords)))))
4040 (setq kwds (reverse kwds)))
4041 (setq kwds (nreverse kwds))
4042 (let (inter kws kw)
4043 (while (setq kws (pop kwds))
4044 (let ((kws (or
4045 (run-hook-with-args-until-success
4046 'org-todo-setup-filter-hook kws)
4047 kws)))
4048 (setq inter (pop kws) sep (member "|" kws)
4049 kws0 (delete "|" (copy-sequence kws))
4050 kwsa nil
4051 kws1 (mapcar
4052 (lambda (x)
4053 ;; 1 2
4054 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
4055 (progn
4056 (setq kw (match-string 1 x)
4057 key (and (match-end 2) (match-string 2 x))
4058 log (org-extract-log-state-settings x))
4059 (push (cons kw (and key (string-to-char key))) kwsa)
4060 (and log (push log org-todo-log-states))
4062 (error "Invalid TODO keyword %s" x)))
4063 kws0)
4064 kwsa (if kwsa (append '((:startgroup))
4065 (nreverse kwsa)
4066 '((:endgroup))))
4067 hw (car kws1)
4068 dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
4069 tail (list inter hw (car dws) (org-last dws))))
4070 (add-to-list 'org-todo-heads hw 'append)
4071 (push kws1 org-todo-sets)
4072 (setq org-done-keywords (append org-done-keywords dws nil))
4073 (setq org-todo-key-alist (append org-todo-key-alist kwsa))
4074 (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
4075 (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
4076 (setq org-todo-sets (nreverse org-todo-sets)
4077 org-todo-kwd-alist (nreverse org-todo-kwd-alist)
4078 org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
4079 org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
4080 ;; Process the constants
4081 (when const
4082 (let (e cst)
4083 (while (setq e (pop const))
4084 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
4085 (push (cons (match-string 1 e) (match-string 2 e)) cst)))
4086 (setq org-table-formula-constants-local cst)))
4088 ;; Process the tags.
4089 (when tags
4090 (let (e tgs)
4091 (while (setq e (pop tags))
4092 (cond
4093 ((equal e "{") (push '(:startgroup) tgs))
4094 ((equal e "}") (push '(:endgroup) tgs))
4095 ((equal e "\\n") (push '(:newline) tgs))
4096 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
4097 (push (cons (match-string 1 e)
4098 (string-to-char (match-string 2 e)))
4099 tgs))
4100 (t (push (list e) tgs))))
4101 (org-set-local 'org-tag-alist nil)
4102 (while (setq e (pop tgs))
4103 (or (and (stringp (car e))
4104 (assoc (car e) org-tag-alist))
4105 (push e org-tag-alist)))))
4107 ;; Compute the regular expressions and other local variables
4108 (if (not org-done-keywords)
4109 (setq org-done-keywords (and org-todo-keywords-1
4110 (list (org-last org-todo-keywords-1)))))
4111 (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
4112 (length org-scheduled-string)
4113 (length org-clock-string)
4114 (length org-closed-string)))
4115 org-drawer-regexp
4116 (concat "^[ \t]*:\\("
4117 (mapconcat 'regexp-quote org-drawers "\\|")
4118 "\\):[ \t]*$")
4119 org-not-done-keywords
4120 (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
4121 org-todo-regexp
4122 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
4123 "\\|") "\\)\\>")
4124 org-not-done-regexp
4125 (concat "\\<\\("
4126 (mapconcat 'regexp-quote org-not-done-keywords "\\|")
4127 "\\)\\>")
4128 org-not-done-heading-regexp
4129 (concat "^\\(\\*+\\)[ \t]+\\("
4130 (mapconcat 'regexp-quote org-not-done-keywords "\\|")
4131 "\\)\\>")
4132 org-todo-line-regexp
4133 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4134 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
4135 "\\)\\>\\)?[ \t]*\\(.*\\)")
4136 org-complex-heading-regexp
4137 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4138 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
4139 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
4140 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4141 org-complex-heading-regexp-format
4142 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4143 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
4144 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
4145 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4146 org-nl-done-regexp
4147 (concat "\n\\*+[ \t]+"
4148 "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
4149 "\\)" "\\>")
4150 org-todo-line-tags-regexp
4151 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4152 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
4153 (org-re
4154 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
4155 org-looking-at-done-regexp
4156 (concat "^" "\\(?:"
4157 (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
4158 "\\>")
4159 org-deadline-regexp (concat "\\<" org-deadline-string)
4160 org-deadline-time-regexp
4161 (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
4162 org-deadline-line-regexp
4163 (concat "\\<\\(" org-deadline-string "\\).*")
4164 org-scheduled-regexp
4165 (concat "\\<" org-scheduled-string)
4166 org-scheduled-time-regexp
4167 (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
4168 org-closed-time-regexp
4169 (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
4170 org-keyword-time-regexp
4171 (concat "\\<\\(" org-scheduled-string
4172 "\\|" org-deadline-string
4173 "\\|" org-closed-string
4174 "\\|" org-clock-string "\\)"
4175 " *[[<]\\([^]>]+\\)[]>]")
4176 org-keyword-time-not-clock-regexp
4177 (concat "\\<\\(" org-scheduled-string
4178 "\\|" org-deadline-string
4179 "\\|" org-closed-string
4180 "\\)"
4181 " *[[<]\\([^]>]+\\)[]>]")
4182 org-maybe-keyword-time-regexp
4183 (concat "\\(\\<\\(" org-scheduled-string
4184 "\\|" org-deadline-string
4185 "\\|" org-closed-string
4186 "\\|" org-clock-string "\\)\\)?"
4187 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4188 org-planning-or-clock-line-re
4189 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
4190 "\\|" org-deadline-string
4191 "\\|" org-closed-string "\\|" org-clock-string
4192 "\\)\\>\\)")
4193 org-all-time-keywords
4194 (mapcar (lambda (w) (substring w 0 -1))
4195 (list org-scheduled-string org-deadline-string
4196 org-clock-string org-closed-string))
4198 (org-compute-latex-and-specials-regexp)
4199 (org-set-font-lock-defaults))))
4201 (defun org-file-contents (file &optional noerror)
4202 "Return the contents of FILE, as a string."
4203 (if (or (not file)
4204 (not (file-readable-p file)))
4205 (if noerror
4206 (progn
4207 (message "Cannot read file %s" file)
4208 (ding) (sit-for 2)
4210 (error "Cannot read file %s" file))
4211 (with-temp-buffer
4212 (insert-file-contents file)
4213 (buffer-string))))
4215 (defun org-extract-log-state-settings (x)
4216 "Extract the log state setting from a TODO keyword string.
4217 This will extract info from a string like \"WAIT(w@/!)\"."
4218 (let (kw key log1 log2)
4219 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
4220 (setq kw (match-string 1 x)
4221 key (and (match-end 2) (match-string 2 x))
4222 log1 (and (match-end 3) (match-string 3 x))
4223 log2 (and (match-end 4) (match-string 4 x)))
4224 (and (or log1 log2)
4225 (list kw
4226 (and log1 (if (equal log1 "!") 'time 'note))
4227 (and log2 (if (equal log2 "!") 'time 'note)))))))
4229 (defun org-remove-keyword-keys (list)
4230 "Remove a pair of parenthesis at the end of each string in LIST."
4231 (mapcar (lambda (x)
4232 (if (string-match "(.*)$" x)
4233 (substring x 0 (match-beginning 0))
4235 list))
4237 (defun org-assign-fast-keys (alist)
4238 "Assign fast keys to a keyword-key alist.
4239 Respect keys that are already there."
4240 (let (new e (alt ?0))
4241 (while (setq e (pop alist))
4242 (if (or (memq (car e) '(:newline :endgroup :startgroup))
4243 (cdr e)) ;; Key already assigned.
4244 (push e new)
4245 (let ((clist (string-to-list (downcase (car e))))
4246 (used (append new alist)))
4247 (when (= (car clist) ?@)
4248 (pop clist))
4249 (while (and clist (rassoc (car clist) used))
4250 (pop clist))
4251 (unless clist
4252 (while (rassoc alt used)
4253 (incf alt)))
4254 (push (cons (car e) (or (car clist) alt)) new))))
4255 (nreverse new)))
4257 ;;; Some variables used in various places
4259 (defvar org-window-configuration nil
4260 "Used in various places to store a window configuration.")
4261 (defvar org-selected-window nil
4262 "Used in various places to store a window configuration.")
4263 (defvar org-finish-function nil
4264 "Function to be called when `C-c C-c' is used.
4265 This is for getting out of special buffers like remember.")
4268 ;; FIXME: Occasionally check by commenting these, to make sure
4269 ;; no other functions uses these, forgetting to let-bind them.
4270 (defvar entry)
4271 (defvar last-state)
4272 (defvar date)
4274 ;; Defined somewhere in this file, but used before definition.
4275 (defvar org-entities) ;; defined in org-entities.el
4276 (defvar org-struct-menu)
4277 (defvar org-org-menu)
4278 (defvar org-tbl-menu)
4280 ;;;; Define the Org-mode
4282 (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
4283 (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22."))
4286 ;; We use a before-change function to check if a table might need
4287 ;; an update.
4288 (defvar org-table-may-need-update t
4289 "Indicates that a table might need an update.
4290 This variable is set by `org-before-change-function'.
4291 `org-table-align' sets it back to nil.")
4292 (defun org-before-change-function (beg end)
4293 "Every change indicates that a table might need an update."
4294 (setq org-table-may-need-update t))
4295 (defvar org-mode-map)
4296 (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
4297 (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
4298 (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
4299 (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
4300 (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
4301 (defvar org-table-buffer-is-an nil)
4302 (defconst org-outline-regexp "\\*+ ")
4304 ;;;###autoload
4305 (define-derived-mode org-mode outline-mode "Org"
4306 "Outline-based notes management and organizer, alias
4307 \"Carsten's outline-mode for keeping track of everything.\"
4309 Org-mode develops organizational tasks around a NOTES file which
4310 contains information about projects as plain text. Org-mode is
4311 implemented on top of outline-mode, which is ideal to keep the content
4312 of large files well structured. It supports ToDo items, deadlines and
4313 time stamps, which magically appear in the diary listing of the Emacs
4314 calendar. Tables are easily created with a built-in table editor.
4315 Plain text URL-like links connect to websites, emails (VM), Usenet
4316 messages (Gnus), BBDB entries, and any files related to the project.
4317 For printing and sharing of notes, an Org-mode file (or a part of it)
4318 can be exported as a structured ASCII or HTML file.
4320 The following commands are available:
4322 \\{org-mode-map}"
4324 ;; Get rid of Outline menus, they are not needed
4325 ;; Need to do this here because define-derived-mode sets up
4326 ;; the keymap so late. Still, it is a waste to call this each time
4327 ;; we switch another buffer into org-mode.
4328 (if (featurep 'xemacs)
4329 (when (boundp 'outline-mode-menu-heading)
4330 ;; Assume this is Greg's port, it used easymenu
4331 (easy-menu-remove outline-mode-menu-heading)
4332 (easy-menu-remove outline-mode-menu-show)
4333 (easy-menu-remove outline-mode-menu-hide))
4334 (define-key org-mode-map [menu-bar headings] 'undefined)
4335 (define-key org-mode-map [menu-bar hide] 'undefined)
4336 (define-key org-mode-map [menu-bar show] 'undefined))
4338 (org-load-modules-maybe)
4339 (easy-menu-add org-org-menu)
4340 (easy-menu-add org-tbl-menu)
4341 (org-install-agenda-files-menu)
4342 (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
4343 (org-add-to-invisibility-spec '(org-cwidth))
4344 (org-add-to-invisibility-spec '(org-hide-block . t))
4345 (when (featurep 'xemacs)
4346 (org-set-local 'line-move-ignore-invisible t))
4347 (org-set-local 'outline-regexp org-outline-regexp)
4348 (org-set-local 'outline-level 'org-outline-level)
4349 (when (and org-ellipsis
4350 (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
4351 (fboundp 'make-glyph-code))
4352 (unless org-display-table
4353 (setq org-display-table (make-display-table)))
4354 (set-display-table-slot
4355 org-display-table 4
4356 (vconcat (mapcar
4357 (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
4358 org-ellipsis)))
4359 (if (stringp org-ellipsis) org-ellipsis "..."))))
4360 (setq buffer-display-table org-display-table))
4361 (org-set-regexps-and-options)
4362 (when (and org-tag-faces (not org-tags-special-faces-re))
4363 ;; tag faces set outside customize.... force initialization.
4364 (org-set-tag-faces 'org-tag-faces org-tag-faces))
4365 ;; Calc embedded
4366 (org-set-local 'calc-embedded-open-mode "# ")
4367 (modify-syntax-entry ?# "<")
4368 (modify-syntax-entry ?@ "w")
4369 (if org-startup-truncated (setq truncate-lines t))
4370 (org-set-local 'font-lock-unfontify-region-function
4371 'org-unfontify-region)
4372 ;; Activate before-change-function
4373 (org-set-local 'org-table-may-need-update t)
4374 (org-add-hook 'before-change-functions 'org-before-change-function nil
4375 'local)
4376 ;; Check for running clock before killing a buffer
4377 (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
4378 ;; Paragraphs and auto-filling
4379 (org-set-autofill-regexps)
4380 (setq indent-line-function 'org-indent-line-function)
4381 (org-update-radio-target-regexp)
4382 ;; Make sure dependence stuff works reliably, even for users who set it
4383 ;; too late :-(
4384 (if org-enforce-todo-dependencies
4385 (add-hook 'org-blocker-hook
4386 'org-block-todo-from-children-or-siblings-or-parent)
4387 (remove-hook 'org-blocker-hook
4388 'org-block-todo-from-children-or-siblings-or-parent))
4389 (if org-enforce-todo-checkbox-dependencies
4390 (add-hook 'org-blocker-hook
4391 'org-block-todo-from-checkboxes)
4392 (remove-hook 'org-blocker-hook
4393 'org-block-todo-from-checkboxes))
4395 ;; Comment characters
4396 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
4397 (org-set-local 'comment-padding " ")
4399 ;; Align options lines
4400 (org-set-local
4401 'align-mode-rules-list
4402 '((org-in-buffer-settings
4403 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
4404 (modes . '(org-mode)))))
4406 ;; Imenu
4407 (org-set-local 'imenu-create-index-function
4408 'org-imenu-get-tree)
4410 ;; Make isearch reveal context
4411 (if (or (featurep 'xemacs)
4412 (not (boundp 'outline-isearch-open-invisible-function)))
4413 ;; Emacs 21 and XEmacs make use of the hook
4414 (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
4415 ;; Emacs 22 deals with this through a special variable
4416 (org-set-local 'outline-isearch-open-invisible-function
4417 (lambda (&rest ignore) (org-show-context 'isearch))))
4419 ;; Turn on org-beamer-mode?
4420 (and org-startup-with-beamer-mode (org-beamer-mode 1))
4422 ;; If empty file that did not turn on org-mode automatically, make it to.
4423 (if (and org-insert-mode-line-in-empty-file
4424 (interactive-p)
4425 (= (point-min) (point-max)))
4426 (insert "# -*- mode: org -*-\n\n"))
4427 (unless org-inhibit-startup
4428 (when org-startup-align-all-tables
4429 (let ((bmp (buffer-modified-p)))
4430 (org-table-map-tables 'org-table-align)
4431 (set-buffer-modified-p bmp)))
4432 (when org-startup-indented
4433 (require 'org-indent)
4434 (org-indent-mode 1))
4435 (unless org-inhibit-startup-visibility-stuff
4436 (org-set-startup-visibility))))
4438 (when (fboundp 'abbrev-table-put)
4439 (abbrev-table-put org-mode-abbrev-table
4440 :parents (list text-mode-abbrev-table)))
4442 (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
4444 (defun org-current-time ()
4445 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
4446 (if (> (car org-time-stamp-rounding-minutes) 1)
4447 (let ((r (car org-time-stamp-rounding-minutes))
4448 (time (decode-time)))
4449 (apply 'encode-time
4450 (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
4451 (nthcdr 2 time))))
4452 (current-time)))
4454 ;;;; Font-Lock stuff, including the activators
4456 (defvar org-mouse-map (make-sparse-keymap))
4457 (org-defkey org-mouse-map
4458 (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
4459 (org-defkey org-mouse-map
4460 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
4461 (when org-mouse-1-follows-link
4462 (org-defkey org-mouse-map [follow-link] 'mouse-face))
4463 (when org-tab-follows-link
4464 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
4465 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
4467 (require 'font-lock)
4469 (defconst org-non-link-chars "]\t\n\r<>")
4470 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
4471 "shell" "elisp"))
4472 (defvar org-link-types-re nil
4473 "Matches a link that has a url-like prefix like \"http:\"")
4474 (defvar org-link-re-with-space nil
4475 "Matches a link with spaces, optional angular brackets around it.")
4476 (defvar org-link-re-with-space2 nil
4477 "Matches a link with spaces, optional angular brackets around it.")
4478 (defvar org-link-re-with-space3 nil
4479 "Matches a link with spaces, only for internal part in bracket links.")
4480 (defvar org-angle-link-re nil
4481 "Matches link with angular brackets, spaces are allowed.")
4482 (defvar org-plain-link-re nil
4483 "Matches plain link, without spaces.")
4484 (defvar org-bracket-link-regexp nil
4485 "Matches a link in double brackets.")
4486 (defvar org-bracket-link-analytic-regexp nil
4487 "Regular expression used to analyze links.
4488 Here is what the match groups contain after a match:
4489 1: http:
4490 2: http
4491 3: path
4492 4: [desc]
4493 5: desc")
4494 (defvar org-bracket-link-analytic-regexp++ nil
4495 "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
4496 (defvar org-any-link-re nil
4497 "Regular expression matching any link.")
4499 (defun org-make-link-regexps ()
4500 "Update the link regular expressions.
4501 This should be called after the variable `org-link-types' has changed."
4502 (setq org-link-types-re
4503 (concat
4504 "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
4505 org-link-re-with-space
4506 (concat
4507 "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4508 "\\([^" org-non-link-chars " ]"
4509 "[^" org-non-link-chars "]*"
4510 "[^" org-non-link-chars " ]\\)>?")
4511 org-link-re-with-space2
4512 (concat
4513 "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4514 "\\([^" org-non-link-chars " ]"
4515 "[^\t\n\r]*"
4516 "[^" org-non-link-chars " ]\\)>?")
4517 org-link-re-with-space3
4518 (concat
4519 "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4520 "\\([^" org-non-link-chars " ]"
4521 "[^\t\n\r]*\\)")
4522 org-angle-link-re
4523 (concat
4524 "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4525 "\\([^" org-non-link-chars " ]"
4526 "[^" org-non-link-chars "]*"
4527 "\\)>")
4528 org-plain-link-re
4529 (concat
4530 "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4531 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
4532 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
4533 org-bracket-link-regexp
4534 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
4535 org-bracket-link-analytic-regexp
4536 (concat
4537 "\\[\\["
4538 "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
4539 "\\([^]]+\\)"
4540 "\\]"
4541 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4542 "\\]")
4543 org-bracket-link-analytic-regexp++
4544 (concat
4545 "\\[\\["
4546 "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
4547 "\\([^]]+\\)"
4548 "\\]"
4549 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4550 "\\]")
4551 org-any-link-re
4552 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
4553 org-angle-link-re "\\)\\|\\("
4554 org-plain-link-re "\\)")))
4556 (org-make-link-regexps)
4558 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
4559 "Regular expression for fast time stamp matching.")
4560 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
4561 "Regular expression for fast time stamp matching.")
4562 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4563 "Regular expression matching time strings for analysis.
4564 This one does not require the space after the date, so it can be used
4565 on a string that terminates immediately after the date.")
4566 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4567 "Regular expression matching time strings for analysis.")
4568 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
4569 "Regular expression matching time stamps, with groups.")
4570 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
4571 "Regular expression matching time stamps (also [..]), with groups.")
4572 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
4573 "Regular expression matching a time stamp range.")
4574 (defconst org-tr-regexp-both
4575 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
4576 "Regular expression matching a time stamp range.")
4577 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
4578 org-ts-regexp "\\)?")
4579 "Regular expression matching a time stamp or time stamp range.")
4580 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
4581 org-ts-regexp-both "\\)?")
4582 "Regular expression matching a time stamp or time stamp range.
4583 The time stamps may be either active or inactive.")
4585 (defvar org-emph-face nil)
4587 (defun org-do-emphasis-faces (limit)
4588 "Run through the buffer and add overlays to links."
4589 (let (rtn a)
4590 (while (and (not rtn) (re-search-forward org-emph-re limit t))
4591 (if (not (= (char-after (match-beginning 3))
4592 (char-after (match-beginning 4))))
4593 (progn
4594 (setq rtn t)
4595 (setq a (assoc (match-string 3) org-emphasis-alist))
4596 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
4597 'face
4598 (nth 1 a))
4599 (and (nth 4 a)
4600 (org-remove-flyspell-overlays-in
4601 (match-beginning 0) (match-end 0)))
4602 (add-text-properties (match-beginning 2) (match-end 2)
4603 '(font-lock-multiline t))
4604 (when org-hide-emphasis-markers
4605 (add-text-properties (match-end 4) (match-beginning 5)
4606 '(invisible org-link))
4607 (add-text-properties (match-beginning 3) (match-end 3)
4608 '(invisible org-link)))))
4609 (backward-char 1))
4610 rtn))
4612 (defun org-emphasize (&optional char)
4613 "Insert or change an emphasis, i.e. a font like bold or italic.
4614 If there is an active region, change that region to a new emphasis.
4615 If there is no region, just insert the marker characters and position
4616 the cursor between them.
4617 CHAR should be either the marker character, or the first character of the
4618 HTML tag associated with that emphasis. If CHAR is a space, the means
4619 to remove the emphasis of the selected region.
4620 If char is not given (for example in an interactive call) it
4621 will be prompted for."
4622 (interactive)
4623 (let ((eal org-emphasis-alist) e det
4624 (erc org-emphasis-regexp-components)
4625 (prompt "")
4626 (string "") beg end move tag c s)
4627 (if (org-region-active-p)
4628 (setq beg (region-beginning) end (region-end)
4629 string (buffer-substring beg end))
4630 (setq move t))
4632 (while (setq e (pop eal))
4633 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
4634 c (aref tag 0))
4635 (push (cons c (string-to-char (car e))) det)
4636 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
4637 (substring tag 1)))))
4638 (setq det (nreverse det))
4639 (unless char
4640 (message "%s" (concat "Emphasis marker or tag:" prompt))
4641 (setq char (read-char-exclusive)))
4642 (setq char (or (cdr (assoc char det)) char))
4643 (if (equal char ?\ )
4644 (setq s "" move nil)
4645 (unless (assoc (char-to-string char) org-emphasis-alist)
4646 (error "No such emphasis marker: \"%c\"" char))
4647 (setq s (char-to-string char)))
4648 (while (and (> (length string) 1)
4649 (equal (substring string 0 1) (substring string -1))
4650 (assoc (substring string 0 1) org-emphasis-alist))
4651 (setq string (substring string 1 -1)))
4652 (setq string (concat s string s))
4653 (if beg (delete-region beg end))
4654 (unless (or (bolp)
4655 (string-match (concat "[" (nth 0 erc) "\n]")
4656 (char-to-string (char-before (point)))))
4657 (insert " "))
4658 (unless (or (eobp)
4659 (string-match (concat "[" (nth 1 erc) "\n]")
4660 (char-to-string (char-after (point)))))
4661 (insert " ") (backward-char 1))
4662 (insert string)
4663 (and move (backward-char 1))))
4665 (defconst org-nonsticky-props
4666 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
4668 (defsubst org-rear-nonsticky-at (pos)
4669 (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
4671 (defun org-activate-plain-links (limit)
4672 "Run through the buffer and add overlays to links."
4673 (catch 'exit
4674 (let (f)
4675 (if (re-search-forward org-plain-link-re limit t)
4676 (progn
4677 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4678 (setq f (get-text-property (match-beginning 0) 'face))
4679 (if (or (eq f 'org-tag)
4680 (and (listp f) (memq 'org-tag f)))
4682 (add-text-properties (match-beginning 0) (match-end 0)
4683 (list 'mouse-face 'highlight
4684 'face 'org-link
4685 'keymap org-mouse-map))
4686 (org-rear-nonsticky-at (match-end 0)))
4687 t)))))
4689 (defun org-activate-code (limit)
4690 (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
4691 (progn
4692 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4693 (remove-text-properties (match-beginning 0) (match-end 0)
4694 '(display t invisible t intangible t))
4695 t)))
4697 (defun org-fontify-meta-lines-and-blocks (limit)
4698 "Fontify #+ lines and blocks, in the correct ways."
4699 (let ((case-fold-search t))
4700 (if (re-search-forward
4701 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
4702 limit t)
4703 (let ((beg (match-beginning 0))
4704 (beg1 (line-beginning-position 2))
4705 (dc1 (downcase (match-string 2)))
4706 (dc3 (downcase (match-string 3)))
4707 end end1 quoting block-type)
4708 (cond
4709 ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
4710 ;; a single line of backend-specific content
4711 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4712 (remove-text-properties (match-beginning 0) (match-end 0)
4713 '(display t invisible t intangible t))
4714 (add-text-properties (match-beginning 1) (match-end 3)
4715 '(font-lock-fontified t face org-meta-line))
4716 (add-text-properties (match-beginning 6) (match-end 6)
4717 '(font-lock-fontified t face org-block))
4719 ((and (match-end 4) (equal dc3 "begin"))
4720 ;; Truely a block
4721 (setq block-type (downcase (match-string 5))
4722 quoting (member block-type org-protecting-blocks))
4723 (when (re-search-forward
4724 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
4725 nil t) ;; on purpose, we look further than LIMIT
4726 (setq end (match-end 0) end1 (1- (match-beginning 0)))
4727 (when quoting
4728 (remove-text-properties beg end
4729 '(display t invisible t intangible t)))
4730 (add-text-properties
4731 beg end
4732 '(font-lock-fontified t font-lock-multiline t))
4733 (add-text-properties beg beg1 '(face org-meta-line))
4734 (add-text-properties end1 end '(face org-meta-line))
4735 (cond
4736 (quoting
4737 (add-text-properties beg1 end1 '(face org-block)))
4738 ((not org-fontify-quote-and-verse-blocks))
4739 ((string= block-type "quote")
4740 (add-text-properties beg1 end1 '(face org-quote)))
4741 ((string= block-type "verse")
4742 (add-text-properties beg1 end1 '(face org-verse))))
4744 ((member dc1 '("title:" "author:" "email:" "date:"))
4745 (add-text-properties
4746 beg (match-end 3)
4747 (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
4748 '(font-lock-fontified t invisible t)
4749 '(font-lock-fontified t face org-document-info-keyword)))
4750 (add-text-properties
4751 (match-beginning 6) (match-end 6)
4752 (if (string-equal dc1 "title:")
4753 '(font-lock-fontified t face org-document-title)
4754 '(font-lock-fontified t face org-document-info))))
4755 ((not (member (char-after beg) '(?\ ?\t)))
4756 ;; just any other in-buffer setting, but not indented
4757 (add-text-properties
4758 beg (match-end 0)
4759 '(font-lock-fontified t face org-meta-line))
4761 ((or (member dc1 '("begin:" "end:" "caption:" "label:"
4762 "orgtbl:" "tblfm:" "tblname:"))
4763 (and (match-end 4) (equal dc3 "attr")))
4764 (add-text-properties
4765 beg (match-end 0)
4766 '(font-lock-fontified t face org-meta-line))
4768 ((member dc3 '(" " ""))
4769 (add-text-properties
4770 beg (match-end 0)
4771 '(font-lock-fontified t face font-lock-comment-face)))
4772 (t nil))))))
4774 (defun org-activate-angle-links (limit)
4775 "Run through the buffer and add overlays to links."
4776 (if (re-search-forward org-angle-link-re limit t)
4777 (progn
4778 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4779 (add-text-properties (match-beginning 0) (match-end 0)
4780 (list 'mouse-face 'highlight
4781 'keymap org-mouse-map))
4782 (org-rear-nonsticky-at (match-end 0))
4783 t)))
4785 (defun org-activate-footnote-links (limit)
4786 "Run through the buffer and add overlays to links."
4787 (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
4788 limit t)
4789 (progn
4790 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4791 (add-text-properties (match-beginning 2) (match-end 2)
4792 (list 'mouse-face 'highlight
4793 'keymap org-mouse-map
4794 'help-echo
4795 (if (= (point-at-bol) (match-beginning 2))
4796 "Footnote definition"
4797 "Footnote reference")
4799 (org-rear-nonsticky-at (match-end 2))
4800 t)))
4802 (defun org-activate-bracket-links (limit)
4803 "Run through the buffer and add overlays to bracketed links."
4804 (if (re-search-forward org-bracket-link-regexp limit t)
4805 (let* ((help (concat "LINK: "
4806 (org-match-string-no-properties 1)))
4807 ;; FIXME: above we should remove the escapes.
4808 ;; but that requires another match, protecting match data,
4809 ;; a lot of overhead for font-lock.
4810 (ip (org-maybe-intangible
4811 (list 'invisible 'org-link
4812 'keymap org-mouse-map 'mouse-face 'highlight
4813 'font-lock-multiline t 'help-echo help)))
4814 (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
4815 'font-lock-multiline t 'help-echo help)))
4816 ;; We need to remove the invisible property here. Table narrowing
4817 ;; may have made some of this invisible.
4818 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4819 (remove-text-properties (match-beginning 0) (match-end 0)
4820 '(invisible nil))
4821 (if (match-end 3)
4822 (progn
4823 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
4824 (org-rear-nonsticky-at (match-beginning 3))
4825 (add-text-properties (match-beginning 3) (match-end 3) vp)
4826 (org-rear-nonsticky-at (match-end 3))
4827 (add-text-properties (match-end 3) (match-end 0) ip)
4828 (org-rear-nonsticky-at (match-end 0)))
4829 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
4830 (org-rear-nonsticky-at (match-beginning 1))
4831 (add-text-properties (match-beginning 1) (match-end 1) vp)
4832 (org-rear-nonsticky-at (match-end 1))
4833 (add-text-properties (match-end 1) (match-end 0) ip)
4834 (org-rear-nonsticky-at (match-end 0)))
4835 t)))
4837 (defun org-activate-dates (limit)
4838 "Run through the buffer and add overlays to dates."
4839 (if (re-search-forward org-tsr-regexp-both limit t)
4840 (progn
4841 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4842 (add-text-properties (match-beginning 0) (match-end 0)
4843 (list 'mouse-face 'highlight
4844 'keymap org-mouse-map))
4845 (org-rear-nonsticky-at (match-end 0))
4846 (when org-display-custom-times
4847 (if (match-end 3)
4848 (org-display-custom-time (match-beginning 3) (match-end 3)))
4849 (org-display-custom-time (match-beginning 1) (match-end 1)))
4850 t)))
4852 (defvar org-target-link-regexp nil
4853 "Regular expression matching radio targets in plain text.")
4854 (make-variable-buffer-local 'org-target-link-regexp)
4855 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
4856 "Regular expression matching a link target.")
4857 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
4858 "Regular expression matching a radio target.")
4859 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
4860 "Regular expression matching any target.")
4862 (defun org-activate-target-links (limit)
4863 "Run through the buffer and add overlays to target matches."
4864 (when org-target-link-regexp
4865 (let ((case-fold-search t))
4866 (if (re-search-forward org-target-link-regexp limit t)
4867 (progn
4868 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4869 (add-text-properties (match-beginning 0) (match-end 0)
4870 (list 'mouse-face 'highlight
4871 'keymap org-mouse-map
4872 'help-echo "Radio target link"
4873 'org-linked-text t))
4874 (org-rear-nonsticky-at (match-end 0))
4875 t)))))
4877 (defun org-update-radio-target-regexp ()
4878 "Find all radio targets in this file and update the regular expression."
4879 (interactive)
4880 (when (memq 'radio org-activate-links)
4881 (setq org-target-link-regexp
4882 (org-make-target-link-regexp (org-all-targets 'radio)))
4883 (org-restart-font-lock)))
4885 (defun org-hide-wide-columns (limit)
4886 (let (s e)
4887 (setq s (text-property-any (point) (or limit (point-max))
4888 'org-cwidth t))
4889 (when s
4890 (setq e (next-single-property-change s 'org-cwidth))
4891 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
4892 (goto-char e)
4893 t)))
4895 (defvar org-latex-and-specials-regexp nil
4896 "Regular expression for highlighting export special stuff.")
4897 (defvar org-match-substring-regexp)
4898 (defvar org-match-substring-with-braces-regexp)
4900 ;; This should be with the exporter code, but we also use if for font-locking
4901 (defconst org-export-html-special-string-regexps
4902 '(("\\\\-" . "&shy;")
4903 ("---\\([^-]\\)" . "&mdash;\\1")
4904 ("--\\([^-]\\)" . "&ndash;\\1")
4905 ("\\.\\.\\." . "&hellip;"))
4906 "Regular expressions for special string conversion.")
4909 (defun org-compute-latex-and-specials-regexp ()
4910 "Compute regular expression for stuff treated specially by exporters."
4911 (if (not org-highlight-latex-fragments-and-specials)
4912 (org-set-local 'org-latex-and-specials-regexp nil)
4913 (require 'org-exp)
4914 (let*
4915 ((matchers (plist-get org-format-latex-options :matchers))
4916 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
4917 org-latex-regexps)))
4918 (org-export-allow-BIND nil)
4919 (options (org-combine-plists (org-default-export-plist)
4920 (org-infile-export-plist)))
4921 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
4922 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
4923 (org-export-with-TeX-macros (plist-get options :TeX-macros))
4924 (org-export-html-expand (plist-get options :expand-quoted-html))
4925 (org-export-with-special-strings (plist-get options :special-strings))
4926 (re-sub
4927 (cond
4928 ((equal org-export-with-sub-superscripts '{})
4929 (list org-match-substring-with-braces-regexp))
4930 (org-export-with-sub-superscripts
4931 (list org-match-substring-regexp))
4932 (t nil)))
4933 (re-latex
4934 (if org-export-with-LaTeX-fragments
4935 (mapcar (lambda (x) (nth 1 x)) latexs)))
4936 (re-macros
4937 (if org-export-with-TeX-macros
4938 (list (concat "\\\\"
4939 (regexp-opt
4940 (append (mapcar 'car (append org-entities-user
4941 org-entities))
4942 (if (boundp 'org-latex-entities)
4943 (mapcar (lambda (x)
4944 (or (car-safe x) x))
4945 org-latex-entities)
4946 nil))
4947 'words))) ; FIXME
4949 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
4950 (re-special (if org-export-with-special-strings
4951 (mapcar (lambda (x) (car x))
4952 org-export-html-special-string-regexps)))
4953 (re-rest
4954 (delq nil
4955 (list
4956 (if org-export-html-expand "@<[^>\n]+>")
4957 ))))
4958 (org-set-local
4959 'org-latex-and-specials-regexp
4960 (mapconcat 'identity (append re-latex re-sub re-macros re-special
4961 re-rest) "\\|")))))
4963 (defun org-do-latex-and-special-faces (limit)
4964 "Run through the buffer and add overlays to links."
4965 (when org-latex-and-specials-regexp
4966 (let (rtn d)
4967 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
4968 limit t))
4969 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
4970 'face))
4971 '(org-code org-verbatim underline)))
4972 (progn
4973 (setq rtn t
4974 d (cond ((member (char-after (1+ (match-beginning 0)))
4975 '(?_ ?^)) 1)
4976 (t 0)))
4977 (font-lock-prepend-text-property
4978 (+ d (match-beginning 0)) (match-end 0)
4979 'face 'org-latex-and-export-specials)
4980 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
4981 '(font-lock-multiline t)))))
4982 rtn)))
4984 (defun org-restart-font-lock ()
4985 "Restart font-lock-mode, to force refontification."
4986 (when (and (boundp 'font-lock-mode) font-lock-mode)
4987 (font-lock-mode -1)
4988 (font-lock-mode 1)))
4990 (defun org-all-targets (&optional radio)
4991 "Return a list of all targets in this file.
4992 With optional argument RADIO, only find radio targets."
4993 (let ((re (if radio org-radio-target-regexp org-target-regexp))
4994 rtn)
4995 (save-excursion
4996 (goto-char (point-min))
4997 (while (re-search-forward re nil t)
4998 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
4999 rtn)))
5001 (defun org-make-target-link-regexp (targets)
5002 "Make regular expression matching all strings in TARGETS.
5003 The regular expression finds the targets also if there is a line break
5004 between words."
5005 (and targets
5006 (concat
5007 "\\<\\("
5008 (mapconcat
5009 (lambda (x)
5010 (while (string-match " +" x)
5011 (setq x (replace-match "\\s-+" t t x)))
5013 targets
5014 "\\|")
5015 "\\)\\>")))
5017 (defun org-activate-tags (limit)
5018 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
5019 (progn
5020 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5021 (add-text-properties (match-beginning 1) (match-end 1)
5022 (list 'mouse-face 'highlight
5023 'keymap org-mouse-map))
5024 (org-rear-nonsticky-at (match-end 1))
5025 t)))
5027 (defun org-outline-level ()
5028 "Compute the outline level of the heading at point.
5029 This function assumes that the cursor is at the beginning of a line matched
5030 by outline-regexp. Otherwise it returns garbage.
5031 If this is called at a normal headline, the level is the number of stars.
5032 Use `org-reduced-level' to remove the effect of `org-odd-levels'.
5033 For plain list items, if they are matched by `outline-regexp', this returns
5034 1000 plus the line indentation."
5035 (save-excursion
5036 (looking-at outline-regexp)
5037 (if (match-beginning 1)
5038 (+ (org-get-string-indentation (match-string 1)) 1000)
5039 (1- (- (match-end 0) (match-beginning 0))))))
5041 (defvar org-font-lock-keywords nil)
5043 (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5044 "Regular expression matching a property line.")
5046 (defvar org-font-lock-hook nil
5047 "Functions to be called for special font lock stuff.")
5049 (defun org-font-lock-hook (limit)
5050 (run-hook-with-args 'org-font-lock-hook limit))
5052 (defun org-set-font-lock-defaults ()
5053 (let* ((em org-fontify-emphasized-text)
5054 (lk org-activate-links)
5055 (org-font-lock-extra-keywords
5056 (list
5057 ;; Call the hook
5058 '(org-font-lock-hook)
5059 ;; Headlines
5060 `(,(if org-fontify-whole-heading-line
5061 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5062 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5063 (1 (org-get-level-face 1))
5064 (2 (org-get-level-face 2))
5065 (3 (org-get-level-face 3)))
5066 ;; Table lines
5067 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5068 (1 'org-table t))
5069 ;; Table internals
5070 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
5071 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
5072 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
5073 '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
5074 ;; Drawers
5075 (list org-drawer-regexp '(0 'org-special-keyword t))
5076 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
5077 ;; Properties
5078 (list org-property-re
5079 '(1 'org-special-keyword t)
5080 '(3 'org-property-value t))
5081 ;; Links
5082 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
5083 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
5084 (if (memq 'plain lk) '(org-activate-plain-links))
5085 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
5086 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
5087 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
5088 (if (memq 'footnote lk) '(org-activate-footnote-links
5089 (2 'org-footnote t)))
5090 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
5091 '(org-hide-wide-columns (0 nil append))
5092 ;; TODO lines
5093 (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
5094 '(1 (org-get-todo-face 1) t))
5095 ;; DONE
5096 (if org-fontify-done-headline
5097 (list (concat "^[*]+ +\\<\\("
5098 (mapconcat 'regexp-quote org-done-keywords "\\|")
5099 "\\)\\(.*\\)")
5100 '(2 'org-headline-done t))
5101 nil)
5102 ;; Priorities
5103 '(org-font-lock-add-priority-faces)
5104 ;; Tags
5105 '(org-font-lock-add-tag-faces)
5106 ;; Special keywords
5107 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
5108 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
5109 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
5110 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
5111 ;; Emphasis
5112 (if em
5113 (if (featurep 'xemacs)
5114 '(org-do-emphasis-faces (0 nil append))
5115 '(org-do-emphasis-faces)))
5116 ;; Checkboxes
5117 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
5118 2 'org-checkbox prepend)
5119 (if org-provide-checkbox-statistics
5120 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5121 (0 (org-get-checkbox-statistics-face) t)))
5122 ;; Description list items
5123 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
5124 2 'bold prepend)
5125 ;; ARCHIVEd headings
5126 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
5127 '(1 'org-archived prepend))
5128 ;; Specials
5129 '(org-do-latex-and-special-faces)
5130 ;; Code
5131 '(org-activate-code (1 'org-code t))
5132 ;; COMMENT
5133 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
5134 "\\|" org-quote-string "\\)\\>")
5135 '(1 'org-special-keyword t))
5136 '("^#.*" (0 'font-lock-comment-face t))
5137 ;; Blocks and meta lines
5138 '(org-fontify-meta-lines-and-blocks)
5140 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
5141 ;; Now set the full font-lock-keywords
5142 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
5143 (org-set-local 'font-lock-defaults
5144 '(org-font-lock-keywords t nil nil backward-paragraph))
5145 (kill-local-variable 'font-lock-keywords) nil))
5147 (defun org-fontify-like-in-org-mode (s &optional odd-levels)
5148 "Fontify string S like in Org-mode"
5149 (with-temp-buffer
5150 (insert s)
5151 (let ((org-odd-levels-only odd-levels))
5152 (org-mode)
5153 (font-lock-fontify-buffer)
5154 (buffer-string))))
5156 (defvar org-m nil)
5157 (defvar org-l nil)
5158 (defvar org-f nil)
5159 (defun org-get-level-face (n)
5160 "Get the right face for match N in font-lock matching of headlines."
5161 (setq org-l (- (match-end 2) (match-beginning 1) 1))
5162 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
5163 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
5164 (cond
5165 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
5166 ((eq n 2) org-f)
5167 (t (if org-level-color-stars-only nil org-f))))
5169 (defun org-get-todo-face (kwd)
5170 "Get the right face for a TODO keyword KWD.
5171 If KWD is a number, get the corresponding match group."
5172 (if (numberp kwd) (setq kwd (match-string kwd)))
5173 (or (org-face-from-face-or-color
5174 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
5175 (and (member kwd org-done-keywords) 'org-done)
5176 'org-todo))
5178 (defun org-face-from-face-or-color (context inherit face-or-color)
5179 "Create a face list that inherits INHERIT, but sets the foreground color.
5180 When FACE-OR-COLOR is not a string, just return it."
5181 (if (stringp face-or-color)
5182 (list :inherit inherit
5183 (cdr (assoc context org-faces-easy-properties))
5184 face-or-color)
5185 face-or-color))
5187 (defun org-font-lock-add-tag-faces (limit)
5188 "Add the special tag faces."
5189 (when (and org-tag-faces org-tags-special-faces-re)
5190 (while (re-search-forward org-tags-special-faces-re limit t)
5191 (add-text-properties (match-beginning 1) (match-end 1)
5192 (list 'face (org-get-tag-face 1)
5193 'font-lock-fontified t))
5194 (backward-char 1))))
5196 (defun org-font-lock-add-priority-faces (limit)
5197 "Add the special priority faces."
5198 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
5199 (add-text-properties
5200 (match-beginning 0) (match-end 0)
5201 (list 'face (or (org-face-from-face-or-color
5202 'priority 'org-special-keyword
5203 (cdr (assoc (char-after (match-beginning 1))
5204 org-priority-faces)))
5205 'org-special-keyword)
5206 'font-lock-fontified t))))
5208 (defun org-get-tag-face (kwd)
5209 "Get the right face for a TODO keyword KWD.
5210 If KWD is a number, get the corresponding match group."
5211 (if (numberp kwd) (setq kwd (match-string kwd)))
5212 (or (org-face-from-face-or-color
5213 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
5214 'org-tag))
5216 (defun org-unfontify-region (beg end &optional maybe_loudly)
5217 "Remove fontification and activation overlays from links."
5218 (font-lock-default-unfontify-region beg end)
5219 (let* ((buffer-undo-list t)
5220 (inhibit-read-only t) (inhibit-point-motion-hooks t)
5221 (inhibit-modification-hooks t)
5222 deactivate-mark buffer-file-name buffer-file-truename)
5223 (remove-text-properties
5224 beg end
5225 (if org-indent-mode
5226 ;; also remove line-prefix and wrap-prefix properties
5227 '(mouse-face t keymap t org-linked-text t
5228 invisible t intangible t
5229 line-prefix t wrap-prefix t
5230 org-no-flyspell t)
5231 '(mouse-face t keymap t org-linked-text t
5232 invisible t intangible t
5233 org-no-flyspell t)))))
5235 ;;;; Visibility cycling, including org-goto and indirect buffer
5237 ;;; Cycling
5239 (defvar org-cycle-global-status nil)
5240 (make-variable-buffer-local 'org-cycle-global-status)
5241 (defvar org-cycle-subtree-status nil)
5242 (make-variable-buffer-local 'org-cycle-subtree-status)
5244 ;;;###autoload
5246 (defvar org-inlinetask-min-level)
5248 (defun org-cycle (&optional arg)
5249 "TAB-action and visibility cycling for Org-mode.
5251 This is the command invoked in Org-mode by the TAB key. Its main purpose
5252 is outline visibility cycling, but it also invokes other actions
5253 in special contexts.
5255 - When this function is called with a prefix argument, rotate the entire
5256 buffer through 3 states (global cycling)
5257 1. OVERVIEW: Show only top-level headlines.
5258 2. CONTENTS: Show all headlines of all levels, but no body text.
5259 3. SHOW ALL: Show everything.
5260 When called with two `C-u C-u' prefixes, switch to the startup visibility,
5261 determined by the variable `org-startup-folded', and by any VISIBILITY
5262 properties in the buffer.
5263 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
5264 including any drawers.
5266 - When inside a table, re-align the table and move to the next field.
5268 - When point is at the beginning of a headline, rotate the subtree started
5269 by this line through 3 different states (local cycling)
5270 1. FOLDED: Only the main headline is shown.
5271 2. CHILDREN: The main headline and the direct children are shown.
5272 From this state, you can move to one of the children
5273 and zoom in further.
5274 3. SUBTREE: Show the entire subtree, including body text.
5275 If there is no subtree, switch directly from CHILDREN to FOLDED.
5277 - When point is at the beginning of an empty headline and the variable
5278 `org-cycle-level-after-item/entry-creation' is set, cycle the level
5279 of the headline by demoting and promoting it to likely levels. This
5280 speeds up creation document structure by presing TAB once or several
5281 times right after creating a new headline.
5283 - When there is a numeric prefix, go up to a heading with level ARG, do
5284 a `show-subtree' and return to the previous cursor position. If ARG
5285 is negative, go up that many levels.
5287 - When point is not at the beginning of a headline, execute the global
5288 binding for TAB, which is re-indenting the line. See the option
5289 `org-cycle-emulate-tab' for details.
5291 - Special case: if point is at the beginning of the buffer and there is
5292 no headline in line 1, this function will act as if called with prefix arg.
5293 But only if also the variable `org-cycle-global-at-bob' is t."
5294 (interactive "P")
5295 (org-load-modules-maybe)
5296 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
5297 (and org-cycle-level-after-item/entry-creation
5298 (or (org-cycle-level)
5299 (org-cycle-item-indentation))))
5300 (let* ((limit-level
5301 (or org-cycle-max-level
5302 (and (boundp 'org-inlinetask-min-level)
5303 org-inlinetask-min-level
5304 (1- org-inlinetask-min-level))))
5305 (nstars (and limit-level
5306 (if org-odd-levels-only
5307 (and limit-level (1- (* limit-level 2)))
5308 limit-level)))
5309 (outline-regexp
5310 (cond
5311 ((not (org-mode-p)) outline-regexp)
5312 ((or (eq org-cycle-include-plain-lists 'integrate)
5313 (and org-cycle-include-plain-lists (org-at-item-p)))
5314 (concat "\\(?:\\*"
5315 (if nstars (format "\\{1,%d\\}" nstars) "+")
5316 " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
5317 (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
5318 (bob-special (and org-cycle-global-at-bob (bobp)
5319 (not (looking-at outline-regexp))))
5320 (org-cycle-hook
5321 (if bob-special
5322 (delq 'org-optimize-window-after-visibility-change
5323 (copy-sequence org-cycle-hook))
5324 org-cycle-hook))
5325 (pos (point)))
5327 (if (or bob-special (equal arg '(4)))
5328 ;; special case: use global cycling
5329 (setq arg t))
5331 (cond
5333 ((equal arg '(16))
5334 (org-set-startup-visibility)
5335 (message "Startup visibility, plus VISIBILITY properties"))
5337 ((equal arg '(64))
5338 (show-all)
5339 (message "Entire buffer visible, including drawers"))
5341 ((org-at-table-p 'any)
5342 ;; Enter the table or move to the next field in the table
5343 (if (org-at-table.el-p)
5344 (message "Use C-c ' to edit table.el tables")
5345 (if arg (org-table-edit-field t)
5346 (org-table-justify-field-maybe)
5347 (call-interactively 'org-table-next-field))))
5349 ((run-hook-with-args-until-success
5350 'org-tab-after-check-for-table-hook))
5352 ((eq arg t) ;; Global cycling
5353 (org-cycle-internal-global))
5355 ((and org-drawers org-drawer-regexp
5356 (save-excursion
5357 (beginning-of-line 1)
5358 (looking-at org-drawer-regexp)))
5359 ;; Toggle block visibility
5360 (org-flag-drawer
5361 (not (get-char-property (match-end 0) 'invisible))))
5363 ((integerp arg)
5364 ;; Show-subtree, ARG levels up from here.
5365 (save-excursion
5366 (org-back-to-heading)
5367 (outline-up-heading (if (< arg 0) (- arg)
5368 (- (funcall outline-level) arg)))
5369 (org-show-subtree)))
5371 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
5372 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
5374 (org-cycle-internal-local))
5376 ;; TAB emulation and template completion
5377 (buffer-read-only (org-back-to-heading))
5379 ((run-hook-with-args-until-success
5380 'org-tab-after-check-for-cycling-hook))
5382 ((org-try-structure-completion))
5384 ((org-try-cdlatex-tab))
5386 ((run-hook-with-args-until-success
5387 'org-tab-before-tab-emulation-hook))
5389 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
5390 (or (not (bolp))
5391 (not (looking-at outline-regexp))))
5392 (call-interactively (global-key-binding "\t")))
5394 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
5395 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
5396 (or (and (eq org-cycle-emulate-tab 'white)
5397 (= (match-end 0) (point-at-eol)))
5398 (and (eq org-cycle-emulate-tab 'whitestart)
5399 (>= (match-end 0) pos))))
5401 (eq org-cycle-emulate-tab t))
5402 (call-interactively (global-key-binding "\t")))
5404 (t (save-excursion
5405 (org-back-to-heading)
5406 (org-cycle)))))))
5408 (defun org-cycle-internal-global ()
5409 "Do the global cycling action."
5410 (cond
5411 ((and (eq last-command this-command)
5412 (eq org-cycle-global-status 'overview))
5413 ;; We just created the overview - now do table of contents
5414 ;; This can be slow in very large buffers, so indicate action
5415 (run-hook-with-args 'org-pre-cycle-hook 'contents)
5416 (message "CONTENTS...")
5417 (org-content)
5418 (message "CONTENTS...done")
5419 (setq org-cycle-global-status 'contents)
5420 (run-hook-with-args 'org-cycle-hook 'contents))
5422 ((and (eq last-command this-command)
5423 (eq org-cycle-global-status 'contents))
5424 ;; We just showed the table of contents - now show everything
5425 (run-hook-with-args 'org-pre-cycle-hook 'all)
5426 (show-all)
5427 (message "SHOW ALL")
5428 (setq org-cycle-global-status 'all)
5429 (run-hook-with-args 'org-cycle-hook 'all))
5432 ;; Default action: go to overview
5433 (run-hook-with-args 'org-pre-cycle-hook 'overview)
5434 (org-overview)
5435 (message "OVERVIEW")
5436 (setq org-cycle-global-status 'overview)
5437 (run-hook-with-args 'org-cycle-hook 'overview))))
5439 (defun org-cycle-internal-local ()
5440 "Do the local cycling action."
5441 (org-back-to-heading)
5442 (let ((goal-column 0) eoh eol eos level has-children children-skipped)
5443 ;; First, some boundaries
5444 (save-excursion
5445 (org-back-to-heading)
5446 (setq level (funcall outline-level))
5447 (save-excursion
5448 (beginning-of-line 2)
5449 (if (or (featurep 'xemacs) (<= emacs-major-version 21))
5450 ; XEmacs does not have `next-single-char-property-change'
5451 ; I'm not sure about Emacs 21.
5452 (while (and (not (eobp)) ;; this is like `next-line'
5453 (get-char-property (1- (point)) 'invisible))
5454 (beginning-of-line 2))
5455 (while (and (not (eobp)) ;; this is like `next-line'
5456 (get-char-property (1- (point)) 'invisible))
5457 (goto-char (next-single-char-property-change (point) 'invisible))
5458 ;;;??? (or (bolp) (beginning-of-line 2))))
5459 (and (eolp) (beginning-of-line 2))))
5460 (setq eol (point)))
5461 (outline-end-of-heading) (setq eoh (point))
5462 (save-excursion
5463 (outline-next-heading)
5464 (setq has-children (and (org-at-heading-p t)
5465 (> (funcall outline-level) level))))
5466 (org-end-of-subtree t)
5467 (unless (eobp)
5468 (skip-chars-forward " \t\n")
5469 (beginning-of-line 1) ; in case this is an item
5471 (setq eos (if (eobp) (point) (1- (point)))))
5472 ;; Find out what to do next and set `this-command'
5473 (cond
5474 ((= eos eoh)
5475 ;; Nothing is hidden behind this heading
5476 (run-hook-with-args 'org-pre-cycle-hook 'empty)
5477 (message "EMPTY ENTRY")
5478 (setq org-cycle-subtree-status nil)
5479 (save-excursion
5480 (goto-char eos)
5481 (outline-next-heading)
5482 (if (org-invisible-p) (org-flag-heading nil))))
5483 ((and (or (>= eol eos)
5484 (not (string-match "\\S-" (buffer-substring eol eos))))
5485 (or has-children
5486 (not (setq children-skipped
5487 org-cycle-skip-children-state-if-no-children))))
5488 ;; Entire subtree is hidden in one line: children view
5489 (run-hook-with-args 'org-pre-cycle-hook 'children)
5490 (org-show-entry)
5491 (show-children)
5492 (message "CHILDREN")
5493 (save-excursion
5494 (goto-char eos)
5495 (outline-next-heading)
5496 (if (org-invisible-p) (org-flag-heading nil)))
5497 (setq org-cycle-subtree-status 'children)
5498 (run-hook-with-args 'org-cycle-hook 'children))
5499 ((or children-skipped
5500 (and (eq last-command this-command)
5501 (eq org-cycle-subtree-status 'children)))
5502 ;; We just showed the children, or no children are there,
5503 ;; now show everything.
5504 (run-hook-with-args 'org-pre-cycle-hook 'subtree)
5505 (org-show-subtree)
5506 (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
5507 (setq org-cycle-subtree-status 'subtree)
5508 (run-hook-with-args 'org-cycle-hook 'subtree))
5510 ;; Default action: hide the subtree.
5511 (run-hook-with-args 'org-pre-cycle-hook 'folded)
5512 (hide-subtree)
5513 (message "FOLDED")
5514 (setq org-cycle-subtree-status 'folded)
5515 (run-hook-with-args 'org-cycle-hook 'folded)))))
5517 ;;;###autoload
5518 (defun org-global-cycle (&optional arg)
5519 "Cycle the global visibility. For details see `org-cycle'.
5520 With C-u prefix arg, switch to startup visibility.
5521 With a numeric prefix, show all headlines up to that level."
5522 (interactive "P")
5523 (let ((org-cycle-include-plain-lists
5524 (if (org-mode-p) org-cycle-include-plain-lists nil)))
5525 (cond
5526 ((integerp arg)
5527 (show-all)
5528 (hide-sublevels arg)
5529 (setq org-cycle-global-status 'contents))
5530 ((equal arg '(4))
5531 (org-set-startup-visibility)
5532 (message "Startup visibility, plus VISIBILITY properties."))
5534 (org-cycle '(4))))))
5536 (defun org-set-startup-visibility ()
5537 "Set the visibility required by startup options and properties."
5538 (cond
5539 ((eq org-startup-folded t)
5540 (org-cycle '(4)))
5541 ((eq org-startup-folded 'content)
5542 (let ((this-command 'org-cycle) (last-command 'org-cycle))
5543 (org-cycle '(4)) (org-cycle '(4)))))
5544 (unless (eq org-startup-folded 'showeverything)
5545 (if org-hide-block-startup (org-hide-block-all))
5546 (org-set-visibility-according-to-property 'no-cleanup)
5547 (org-cycle-hide-archived-subtrees 'all)
5548 (org-cycle-hide-drawers 'all)
5549 (org-cycle-show-empty-lines 'all)))
5551 (defun org-set-visibility-according-to-property (&optional no-cleanup)
5552 "Switch subtree visibilities according to :VISIBILITY: property."
5553 (interactive)
5554 (let (org-show-entry-below state)
5555 (save-excursion
5556 (goto-char (point-min))
5557 (while (re-search-forward
5558 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
5559 nil t)
5560 (setq state (match-string 1))
5561 (save-excursion
5562 (org-back-to-heading t)
5563 (hide-subtree)
5564 (org-reveal)
5565 (cond
5566 ((equal state '("fold" "folded"))
5567 (hide-subtree))
5568 ((equal state "children")
5569 (org-show-hidden-entry)
5570 (show-children))
5571 ((equal state "content")
5572 (save-excursion
5573 (save-restriction
5574 (org-narrow-to-subtree)
5575 (org-content))))
5576 ((member state '("all" "showall"))
5577 (show-subtree)))))
5578 (unless no-cleanup
5579 (org-cycle-hide-archived-subtrees 'all)
5580 (org-cycle-hide-drawers 'all)
5581 (org-cycle-show-empty-lines 'all)))))
5583 (defun org-overview ()
5584 "Switch to overview mode, showing only top-level headlines.
5585 Really, this shows all headlines with level equal or greater than the level
5586 of the first headline in the buffer. This is important, because if the
5587 first headline is not level one, then (hide-sublevels 1) gives confusing
5588 results."
5589 (interactive)
5590 (let ((level (save-excursion
5591 (goto-char (point-min))
5592 (if (re-search-forward (concat "^" outline-regexp) nil t)
5593 (progn
5594 (goto-char (match-beginning 0))
5595 (funcall outline-level))))))
5596 (and level (hide-sublevels level))))
5598 (defun org-content (&optional arg)
5599 "Show all headlines in the buffer, like a table of contents.
5600 With numerical argument N, show content up to level N."
5601 (interactive "P")
5602 (save-excursion
5603 ;; Visit all headings and show their offspring
5604 (and (integerp arg) (org-overview))
5605 (goto-char (point-max))
5606 (catch 'exit
5607 (while (and (progn (condition-case nil
5608 (outline-previous-visible-heading 1)
5609 (error (goto-char (point-min))))
5611 (looking-at outline-regexp))
5612 (if (integerp arg)
5613 (show-children (1- arg))
5614 (show-branches))
5615 (if (bobp) (throw 'exit nil))))))
5618 (defun org-optimize-window-after-visibility-change (state)
5619 "Adjust the window after a change in outline visibility.
5620 This function is the default value of the hook `org-cycle-hook'."
5621 (when (get-buffer-window (current-buffer))
5622 (cond
5623 ((eq state 'content) nil)
5624 ((eq state 'all) nil)
5625 ((eq state 'folded) nil)
5626 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
5627 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
5629 (defun org-remove-empty-overlays-at (pos)
5630 "Remove outline overlays that do not contain non-white stuff."
5631 (mapc
5632 (lambda (o)
5633 (and (eq 'outline (org-overlay-get o 'invisible))
5634 (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
5635 (org-overlay-end o))))
5636 (org-delete-overlay o)))
5637 (org-overlays-at pos)))
5639 (defun org-clean-visibility-after-subtree-move ()
5640 "Fix visibility issues after moving a subtree."
5641 ;; First, find a reasonable region to look at:
5642 ;; Start two siblings above, end three below
5643 (let* ((beg (save-excursion
5644 (and (org-get-last-sibling)
5645 (org-get-last-sibling))
5646 (point)))
5647 (end (save-excursion
5648 (and (org-get-next-sibling)
5649 (org-get-next-sibling)
5650 (org-get-next-sibling))
5651 (if (org-at-heading-p)
5652 (point-at-eol)
5653 (point))))
5654 (level (looking-at "\\*+"))
5655 (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
5656 (save-excursion
5657 (save-restriction
5658 (narrow-to-region beg end)
5659 (when re
5660 ;; Properly fold already folded siblings
5661 (goto-char (point-min))
5662 (while (re-search-forward re nil t)
5663 (if (and (not (org-invisible-p))
5664 (save-excursion
5665 (goto-char (point-at-eol)) (org-invisible-p)))
5666 (hide-entry))))
5667 (org-cycle-show-empty-lines 'overview)
5668 (org-cycle-hide-drawers 'overview)))))
5670 (defun org-cycle-show-empty-lines (state)
5671 "Show empty lines above all visible headlines.
5672 The region to be covered depends on STATE when called through
5673 `org-cycle-hook'. Lisp program can use t for STATE to get the
5674 entire buffer covered. Note that an empty line is only shown if there
5675 are at least `org-cycle-separator-lines' empty lines before the headline."
5676 (when (not (= org-cycle-separator-lines 0))
5677 (save-excursion
5678 (let* ((n (abs org-cycle-separator-lines))
5679 (re (cond
5680 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
5681 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
5682 (t (let ((ns (number-to-string (- n 2))))
5683 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
5684 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
5685 beg end b e)
5686 (cond
5687 ((memq state '(overview contents t))
5688 (setq beg (point-min) end (point-max)))
5689 ((memq state '(children folded))
5690 (setq beg (point) end (progn (org-end-of-subtree t t)
5691 (beginning-of-line 2)
5692 (point)))))
5693 (when beg
5694 (goto-char beg)
5695 (while (re-search-forward re end t)
5696 (unless (get-char-property (match-end 1) 'invisible)
5697 (setq e (match-end 1))
5698 (if (< org-cycle-separator-lines 0)
5699 (setq b (save-excursion
5700 (goto-char (match-beginning 0))
5701 (org-back-over-empty-lines)
5702 (if (save-excursion
5703 (goto-char (max (point-min) (1- (point))))
5704 (org-on-heading-p))
5705 (1- (point))
5706 (point))))
5707 (setq b (match-beginning 1)))
5708 (outline-flag-region b e nil)))))))
5709 ;; Never hide empty lines at the end of the file.
5710 (save-excursion
5711 (goto-char (point-max))
5712 (outline-previous-heading)
5713 (outline-end-of-heading)
5714 (if (and (looking-at "[ \t\n]+")
5715 (= (match-end 0) (point-max)))
5716 (outline-flag-region (point) (match-end 0) nil))))
5718 (defun org-show-empty-lines-in-parent ()
5719 "Move to the parent and re-show empty lines before visible headlines."
5720 (save-excursion
5721 (let ((context (if (org-up-heading-safe) 'children 'overview)))
5722 (org-cycle-show-empty-lines context))))
5724 (defun org-files-list ()
5725 "Return `org-agenda-files' list, plus all open org-mode files.
5726 This is useful for operations that need to scan all of a user's
5727 open and agenda-wise Org files."
5728 (let ((files (mapcar 'expand-file-name (org-agenda-files))))
5729 (dolist (buf (buffer-list))
5730 (with-current-buffer buf
5731 (if (and (eq major-mode 'org-mode) (buffer-file-name))
5732 (let ((file (expand-file-name (buffer-file-name))))
5733 (unless (member file files)
5734 (push file files))))))
5735 files))
5737 (defsubst org-entry-beginning-position ()
5738 "Return the beginning position of the current entry."
5739 (save-excursion (outline-back-to-heading t) (point)))
5741 (defsubst org-entry-end-position ()
5742 "Return the end position of the current entry."
5743 (save-excursion (outline-next-heading) (point)))
5745 (defun org-cycle-hide-drawers (state)
5746 "Re-hide all drawers after a visibility state change."
5747 (when (and (org-mode-p)
5748 (not (memq state '(overview folded contents))))
5749 (save-excursion
5750 (let* ((globalp (memq state '(contents all)))
5751 (beg (if globalp (point-min) (point)))
5752 (end (if globalp (point-max)
5753 (if (eq state 'children)
5754 (save-excursion (outline-next-heading) (point))
5755 (org-end-of-subtree t)))))
5756 (goto-char beg)
5757 (while (re-search-forward org-drawer-regexp end t)
5758 (org-flag-drawer t))))))
5760 (defun org-flag-drawer (flag)
5761 (save-excursion
5762 (beginning-of-line 1)
5763 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
5764 (let ((b (match-end 0))
5765 (outline-regexp org-outline-regexp))
5766 (if (re-search-forward
5767 "^[ \t]*:END:"
5768 (save-excursion (outline-next-heading) (point)) t)
5769 (outline-flag-region b (point-at-eol) flag)
5770 (error ":END: line missing at position %s" b))))))
5772 (defun org-subtree-end-visible-p ()
5773 "Is the end of the current subtree visible?"
5774 (pos-visible-in-window-p
5775 (save-excursion (org-end-of-subtree t) (point))))
5777 (defun org-first-headline-recenter (&optional N)
5778 "Move cursor to the first headline and recenter the headline.
5779 Optional argument N means put the headline into the Nth line of the window."
5780 (goto-char (point-min))
5781 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
5782 (beginning-of-line)
5783 (recenter (prefix-numeric-value N))))
5785 ;;; Saving and restoring visibility
5787 (defun org-outline-overlay-data (&optional use-markers)
5788 "Return a list of the locations of all outline overlays.
5789 The are overlays with the `invisible' property value `outline'.
5790 The return valus is a list of cons cells, with start and stop
5791 positions for each overlay.
5792 If USE-MARKERS is set, return the positions as markers."
5793 (let (beg end)
5794 (save-excursion
5795 (save-restriction
5796 (widen)
5797 (delq nil
5798 (mapcar (lambda (o)
5799 (when (eq (org-overlay-get o 'invisible) 'outline)
5800 (setq beg (org-overlay-start o)
5801 end (org-overlay-end o))
5802 (and beg end (> end beg)
5803 (if use-markers
5804 (cons (move-marker (make-marker) beg)
5805 (move-marker (make-marker) end))
5806 (cons beg end)))))
5807 (org-overlays-in (point-min) (point-max))))))))
5809 (defun org-set-outline-overlay-data (data)
5810 "Create visibility overlays for all positions in DATA.
5811 DATA should have been made by `org-outline-overlay-data'."
5812 (let (o)
5813 (save-excursion
5814 (save-restriction
5815 (widen)
5816 (show-all)
5817 (mapc (lambda (c)
5818 (setq o (org-make-overlay (car c) (cdr c)))
5819 (org-overlay-put o 'invisible 'outline))
5820 data)))))
5822 (defmacro org-save-outline-visibility (use-markers &rest body)
5823 "Save and restore outline visibility around BODY.
5824 If USE-MARKERS is non-nil, use markers for the positions.
5825 This means that the buffer may change while running BODY,
5826 but it also means that the buffer should stay alive
5827 during the operation, because otherwise all these markers will
5828 point nowhere."
5829 `(let ((data (org-outline-overlay-data ,use-markers)))
5830 (unwind-protect
5831 (progn
5832 ,@body
5833 (org-set-outline-overlay-data data))
5834 (when ,use-markers
5835 (mapc (lambda (c)
5836 (and (markerp (car c)) (move-marker (car c) nil))
5837 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
5838 data)))))
5841 ;;; Folding of blocks
5843 (defconst org-block-regexp
5845 "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
5846 "Regular expression for hiding blocks.")
5848 (defvar org-hide-block-overlays nil
5849 "Overlays hiding blocks.")
5850 (make-variable-buffer-local 'org-hide-block-overlays)
5852 (defun org-block-map (function &optional start end)
5853 "Call func at the head of all source blocks in the current
5854 buffer. Optional arguments START and END can be used to limit
5855 the range."
5856 (let ((start (or start (point-min)))
5857 (end (or end (point-max))))
5858 (save-excursion
5859 (goto-char start)
5860 (while (and (< (point) end) (re-search-forward org-block-regexp end t))
5861 (save-excursion
5862 (save-match-data
5863 (goto-char (match-beginning 0))
5864 (funcall function)))))))
5866 (defun org-hide-block-toggle-all ()
5867 "Toggle the visibility of all blocks in the current buffer."
5868 (org-block-map #'org-hide-block-toggle))
5870 (defun org-hide-block-all ()
5871 "Fold all blocks in the current buffer."
5872 (interactive)
5873 (org-show-block-all)
5874 (org-block-map #'org-hide-block-toggle-maybe))
5876 (defun org-show-block-all ()
5877 "Unfold all blocks in the current buffer."
5878 (mapc 'org-delete-overlay org-hide-block-overlays)
5879 (setq org-hide-block-overlays nil))
5881 (defun org-hide-block-toggle-maybe ()
5882 "Toggle visibility of block at point."
5883 (interactive)
5884 (let ((case-fold-search t))
5885 (if (save-excursion
5886 (beginning-of-line 1)
5887 (looking-at org-block-regexp))
5888 (progn (org-hide-block-toggle)
5889 t) ;; to signal that we took action
5890 nil))) ;; to signal that we did not
5892 (defun org-hide-block-toggle (&optional force)
5893 "Toggle the visibility of the current block."
5894 (interactive)
5895 (save-excursion
5896 (beginning-of-line)
5897 (if (re-search-forward org-block-regexp nil t)
5898 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
5899 (end (match-end 0)) ;; end of entire body
5901 (if (memq t (mapcar (lambda (overlay)
5902 (eq (org-overlay-get overlay 'invisible)
5903 'org-hide-block))
5904 (org-overlays-at start)))
5905 (if (or (not force) (eq force 'off))
5906 (mapc (lambda (ov)
5907 (when (member ov org-hide-block-overlays)
5908 (setq org-hide-block-overlays
5909 (delq ov org-hide-block-overlays)))
5910 (when (eq (org-overlay-get ov 'invisible)
5911 'org-hide-block)
5912 (org-delete-overlay ov)))
5913 (org-overlays-at start)))
5914 (setq ov (org-make-overlay start end))
5915 (org-overlay-put ov 'invisible 'org-hide-block)
5916 ;; make the block accessible to isearch
5917 (org-overlay-put
5918 ov 'isearch-open-invisible
5919 (lambda (ov)
5920 (when (member ov org-hide-block-overlays)
5921 (setq org-hide-block-overlays
5922 (delq ov org-hide-block-overlays)))
5923 (when (eq (org-overlay-get ov 'invisible)
5924 'org-hide-block)
5925 (org-delete-overlay ov))))
5926 (push ov org-hide-block-overlays)))
5927 (error "Not looking at a source block"))))
5929 ;; org-tab-after-check-for-cycling-hook
5930 (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
5931 ;; Remove overlays when changing major mode
5932 (add-hook 'org-mode-hook
5933 (lambda () (org-add-hook 'change-major-mode-hook
5934 'org-show-block-all 'append 'local)))
5936 ;;; Org-goto
5938 (defvar org-goto-window-configuration nil)
5939 (defvar org-goto-marker nil)
5940 (defvar org-goto-map
5941 (let ((map (make-sparse-keymap)))
5942 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
5943 (while (setq cmd (pop cmds))
5944 (substitute-key-definition cmd cmd map global-map)))
5945 (suppress-keymap map)
5946 (org-defkey map "\C-m" 'org-goto-ret)
5947 (org-defkey map [(return)] 'org-goto-ret)
5948 (org-defkey map [(left)] 'org-goto-left)
5949 (org-defkey map [(right)] 'org-goto-right)
5950 (org-defkey map [(control ?g)] 'org-goto-quit)
5951 (org-defkey map "\C-i" 'org-cycle)
5952 (org-defkey map [(tab)] 'org-cycle)
5953 (org-defkey map [(down)] 'outline-next-visible-heading)
5954 (org-defkey map [(up)] 'outline-previous-visible-heading)
5955 (if org-goto-auto-isearch
5956 (if (fboundp 'define-key-after)
5957 (define-key-after map [t] 'org-goto-local-auto-isearch)
5958 nil)
5959 (org-defkey map "q" 'org-goto-quit)
5960 (org-defkey map "n" 'outline-next-visible-heading)
5961 (org-defkey map "p" 'outline-previous-visible-heading)
5962 (org-defkey map "f" 'outline-forward-same-level)
5963 (org-defkey map "b" 'outline-backward-same-level)
5964 (org-defkey map "u" 'outline-up-heading))
5965 (org-defkey map "/" 'org-occur)
5966 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
5967 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
5968 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
5969 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
5970 (org-defkey map "\C-c\C-u" 'outline-up-heading)
5971 map))
5973 (defconst org-goto-help
5974 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
5975 RET=jump to location [Q]uit and return to previous location
5976 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
5978 (defvar org-goto-start-pos) ; dynamically scoped parameter
5980 ;; FIXME: Docstring does not mention both interfaces
5981 (defun org-goto (&optional alternative-interface)
5982 "Look up a different location in the current file, keeping current visibility.
5984 When you want look-up or go to a different location in a document, the
5985 fastest way is often to fold the entire buffer and then dive into the tree.
5986 This method has the disadvantage, that the previous location will be folded,
5987 which may not be what you want.
5989 This command works around this by showing a copy of the current buffer
5990 in an indirect buffer, in overview mode. You can dive into the tree in
5991 that copy, use org-occur and incremental search to find a location.
5992 When pressing RET or `Q', the command returns to the original buffer in
5993 which the visibility is still unchanged. After RET is will also jump to
5994 the location selected in the indirect buffer and expose the
5995 the headline hierarchy above."
5996 (interactive "P")
5997 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
5998 (org-refile-use-outline-path t)
5999 (org-refile-target-verify-function nil)
6000 (interface
6001 (if (not alternative-interface)
6002 org-goto-interface
6003 (if (eq org-goto-interface 'outline)
6004 'outline-path-completion
6005 'outline)))
6006 (org-goto-start-pos (point))
6007 (selected-point
6008 (if (eq interface 'outline)
6009 (car (org-get-location (current-buffer) org-goto-help))
6010 (nth 3 (org-refile-get-location "Goto: ")))))
6011 (if selected-point
6012 (progn
6013 (org-mark-ring-push org-goto-start-pos)
6014 (goto-char selected-point)
6015 (if (or (org-invisible-p) (org-invisible-p2))
6016 (org-show-context 'org-goto)))
6017 (message "Quit"))))
6019 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6020 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6021 (defvar org-goto-local-auto-isearch-map) ; defined below
6023 (defun org-get-location (buf help)
6024 "Let the user select a location in the Org-mode buffer BUF.
6025 This function uses a recursive edit. It returns the selected position
6026 or nil."
6027 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6028 (isearch-hide-immediately nil)
6029 (isearch-search-fun-function
6030 (lambda () 'org-goto-local-search-headings))
6031 (org-goto-selected-point org-goto-exit-command)
6032 (pop-up-frames nil)
6033 (special-display-buffer-names nil)
6034 (special-display-regexps nil)
6035 (special-display-function nil))
6036 (save-excursion
6037 (save-window-excursion
6038 (delete-other-windows)
6039 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6040 (switch-to-buffer
6041 (condition-case nil
6042 (make-indirect-buffer (current-buffer) "*org-goto*")
6043 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6044 (with-output-to-temp-buffer "*Help*"
6045 (princ help))
6046 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
6047 (setq buffer-read-only nil)
6048 (let ((org-startup-truncated t)
6049 (org-startup-folded nil)
6050 (org-startup-align-all-tables nil))
6051 (org-mode)
6052 (org-overview))
6053 (setq buffer-read-only t)
6054 (if (and (boundp 'org-goto-start-pos)
6055 (integer-or-marker-p org-goto-start-pos))
6056 (let ((org-show-hierarchy-above t)
6057 (org-show-siblings t)
6058 (org-show-following-heading t))
6059 (goto-char org-goto-start-pos)
6060 (and (org-invisible-p) (org-show-context)))
6061 (goto-char (point-min)))
6062 (let (org-special-ctrl-a/e) (org-beginning-of-line))
6063 (message "Select location and press RET")
6064 (use-local-map org-goto-map)
6065 (recursive-edit)
6067 (kill-buffer "*org-goto*")
6068 (cons org-goto-selected-point org-goto-exit-command)))
6070 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6071 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6072 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6073 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6075 (defun org-goto-local-search-headings (string bound noerror)
6076 "Search and make sure that any matches are in headlines."
6077 (catch 'return
6078 (while (if isearch-forward
6079 (search-forward string bound noerror)
6080 (search-backward string bound noerror))
6081 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6082 (and (member :headline context)
6083 (not (member :tags context))))
6084 (throw 'return (point))))))
6086 (defun org-goto-local-auto-isearch ()
6087 "Start isearch."
6088 (interactive)
6089 (goto-char (point-min))
6090 (let ((keys (this-command-keys)))
6091 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6092 (isearch-mode t)
6093 (isearch-process-search-char (string-to-char keys)))))
6095 (defun org-goto-ret (&optional arg)
6096 "Finish `org-goto' by going to the new location."
6097 (interactive "P")
6098 (setq org-goto-selected-point (point)
6099 org-goto-exit-command 'return)
6100 (throw 'exit nil))
6102 (defun org-goto-left ()
6103 "Finish `org-goto' by going to the new location."
6104 (interactive)
6105 (if (org-on-heading-p)
6106 (progn
6107 (beginning-of-line 1)
6108 (setq org-goto-selected-point (point)
6109 org-goto-exit-command 'left)
6110 (throw 'exit nil))
6111 (error "Not on a heading")))
6113 (defun org-goto-right ()
6114 "Finish `org-goto' by going to the new location."
6115 (interactive)
6116 (if (org-on-heading-p)
6117 (progn
6118 (setq org-goto-selected-point (point)
6119 org-goto-exit-command 'right)
6120 (throw 'exit nil))
6121 (error "Not on a heading")))
6123 (defun org-goto-quit ()
6124 "Finish `org-goto' without cursor motion."
6125 (interactive)
6126 (setq org-goto-selected-point nil)
6127 (setq org-goto-exit-command 'quit)
6128 (throw 'exit nil))
6130 ;;; Indirect buffer display of subtrees
6132 (defvar org-indirect-dedicated-frame nil
6133 "This is the frame being used for indirect tree display.")
6134 (defvar org-last-indirect-buffer nil)
6136 (defun org-tree-to-indirect-buffer (&optional arg)
6137 "Create indirect buffer and narrow it to current subtree.
6138 With numerical prefix ARG, go up to this level and then take that tree.
6139 If ARG is negative, go up that many levels.
6140 If `org-indirect-buffer-display' is not `new-frame', the command removes the
6141 indirect buffer previously made with this command, to avoid proliferation of
6142 indirect buffers. However, when you call the command with a `C-u' prefix, or
6143 when `org-indirect-buffer-display' is `new-frame', the last buffer
6144 is kept so that you can work with several indirect buffers at the same time.
6145 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
6146 requests that a new frame be made for the new buffer, so that the dedicated
6147 frame is not changed."
6148 (interactive "P")
6149 (let ((cbuf (current-buffer))
6150 (cwin (selected-window))
6151 (pos (point))
6152 beg end level heading ibuf)
6153 (save-excursion
6154 (org-back-to-heading t)
6155 (when (numberp arg)
6156 (setq level (org-outline-level))
6157 (if (< arg 0) (setq arg (+ level arg)))
6158 (while (> (setq level (org-outline-level)) arg)
6159 (outline-up-heading 1 t)))
6160 (setq beg (point)
6161 heading (org-get-heading))
6162 (org-end-of-subtree t t)
6163 (if (org-on-heading-p) (backward-char 1))
6164 (setq end (point)))
6165 (if (and (buffer-live-p org-last-indirect-buffer)
6166 (not (eq org-indirect-buffer-display 'new-frame))
6167 (not arg))
6168 (kill-buffer org-last-indirect-buffer))
6169 (setq ibuf (org-get-indirect-buffer cbuf)
6170 org-last-indirect-buffer ibuf)
6171 (cond
6172 ((or (eq org-indirect-buffer-display 'new-frame)
6173 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
6174 (select-frame (make-frame))
6175 (delete-other-windows)
6176 (switch-to-buffer ibuf)
6177 (org-set-frame-title heading))
6178 ((eq org-indirect-buffer-display 'dedicated-frame)
6179 (raise-frame
6180 (select-frame (or (and org-indirect-dedicated-frame
6181 (frame-live-p org-indirect-dedicated-frame)
6182 org-indirect-dedicated-frame)
6183 (setq org-indirect-dedicated-frame (make-frame)))))
6184 (delete-other-windows)
6185 (switch-to-buffer ibuf)
6186 (org-set-frame-title (concat "Indirect: " heading)))
6187 ((eq org-indirect-buffer-display 'current-window)
6188 (switch-to-buffer ibuf))
6189 ((eq org-indirect-buffer-display 'other-window)
6190 (pop-to-buffer ibuf))
6191 (t (error "Invalid value")))
6192 (if (featurep 'xemacs)
6193 (save-excursion (org-mode) (turn-on-font-lock)))
6194 (narrow-to-region beg end)
6195 (show-all)
6196 (goto-char pos)
6197 (and (window-live-p cwin) (select-window cwin))))
6199 (defun org-get-indirect-buffer (&optional buffer)
6200 (setq buffer (or buffer (current-buffer)))
6201 (let ((n 1) (base (buffer-name buffer)) bname)
6202 (while (buffer-live-p
6203 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
6204 (setq n (1+ n)))
6205 (condition-case nil
6206 (make-indirect-buffer buffer bname 'clone)
6207 (error (make-indirect-buffer buffer bname)))))
6209 (defun org-set-frame-title (title)
6210 "Set the title of the current frame to the string TITLE."
6211 ;; FIXME: how to name a single frame in XEmacs???
6212 (unless (featurep 'xemacs)
6213 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
6215 ;;;; Structure editing
6217 ;;; Inserting headlines
6219 (defun org-previous-line-empty-p ()
6220 (save-excursion
6221 (and (not (bobp))
6222 (or (beginning-of-line 0) t)
6223 (save-match-data
6224 (looking-at "[ \t]*$")))))
6226 (defun org-insert-heading (&optional force-heading invisible-ok)
6227 "Insert a new heading or item with same depth at point.
6228 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
6229 If point is at the beginning of a headline, insert a sibling before the
6230 current headline. If point is not at the beginning, do not split the line,
6231 but create the new headline after the current line.
6232 When INVISIBLE-OK is set, stop at invisible headlines when going back.
6233 This is important for non-interactive uses of the command."
6234 (interactive "P")
6235 (if (or (= (buffer-size) 0)
6236 (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
6237 (org-on-heading-p))))
6238 (not (org-in-item-p))))
6239 (insert "\n* ")
6240 (when (or force-heading (not (org-insert-item)))
6241 (let* ((empty-line-p nil)
6242 (head (save-excursion
6243 (condition-case nil
6244 (progn
6245 (org-back-to-heading invisible-ok)
6246 (setq empty-line-p (org-previous-line-empty-p))
6247 (match-string 0))
6248 (error "*"))))
6249 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
6250 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
6251 pos hide-previous previous-pos)
6252 (cond
6253 ((and (org-on-heading-p) (bolp)
6254 (or (bobp)
6255 (save-excursion (backward-char 1) (not (org-invisible-p)))))
6256 ;; insert before the current line
6257 (open-line (if blank 2 1)))
6258 ((and (bolp)
6259 (not org-insert-heading-respect-content)
6260 (or (bobp)
6261 (save-excursion
6262 (backward-char 1) (not (org-invisible-p)))))
6263 ;; insert right here
6264 nil)
6266 ;; somewhere in the line
6267 (save-excursion
6268 (setq previous-pos (point-at-bol))
6269 (end-of-line)
6270 (setq hide-previous (org-invisible-p)))
6271 (and org-insert-heading-respect-content (org-show-subtree))
6272 (let ((split
6273 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
6274 (save-excursion
6275 (let ((p (point)))
6276 (goto-char (point-at-bol))
6277 (and (looking-at org-complex-heading-regexp)
6278 (> p (match-beginning 4)))))))
6279 tags pos)
6280 (cond
6281 (org-insert-heading-respect-content
6282 (org-end-of-subtree nil t)
6283 (or (bolp) (newline))
6284 (or (org-previous-line-empty-p)
6285 (and blank (newline)))
6286 (open-line 1))
6287 ((org-on-heading-p)
6288 (when hide-previous
6289 (show-children)
6290 (org-show-entry))
6291 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
6292 (setq tags (and (match-end 2) (match-string 2)))
6293 (and (match-end 1)
6294 (delete-region (match-beginning 1) (match-end 1)))
6295 (setq pos (point-at-bol))
6296 (or split (end-of-line 1))
6297 (delete-horizontal-space)
6298 (if (string-match "\\`\\*+\\'"
6299 (buffer-substring (point-at-bol) (point)))
6300 (insert " "))
6301 (newline (if blank 2 1))
6302 (when tags
6303 (save-excursion
6304 (goto-char pos)
6305 (end-of-line 1)
6306 (insert " " tags)
6307 (org-set-tags nil 'align))))
6309 (or split (end-of-line 1))
6310 (newline (if blank 2 1)))))))
6311 (insert head) (just-one-space)
6312 (setq pos (point))
6313 (end-of-line 1)
6314 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
6315 (when (and org-insert-heading-respect-content hide-previous)
6316 (save-excursion
6317 (goto-char previous-pos)
6318 (hide-subtree)))
6319 (run-hooks 'org-insert-heading-hook)))))
6321 (defun org-get-heading (&optional no-tags)
6322 "Return the heading of the current entry, without the stars."
6323 (save-excursion
6324 (org-back-to-heading t)
6325 (if (looking-at
6326 (if no-tags
6327 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
6328 "\\*+[ \t]+\\([^\r\n]*\\)"))
6329 (match-string 1) "")))
6331 (defun org-heading-components ()
6332 "Return the components of the current heading.
6333 This is a list with the following elements:
6334 - the level as an integer
6335 - the reduced level, different if `org-odd-levels-only' is set.
6336 - the TODO keyword, or nil
6337 - the priority character, like ?A, or nil if no priority is given
6338 - the headline text itself, or the tags string if no headline text
6339 - the tags string, or nil."
6340 (save-excursion
6341 (org-back-to-heading t)
6342 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
6343 (list (length (match-string 1))
6344 (org-reduced-level (length (match-string 1)))
6345 (org-match-string-no-properties 2)
6346 (and (match-end 3) (aref (match-string 3) 2))
6347 (org-match-string-no-properties 4)
6348 (org-match-string-no-properties 5)))))
6350 (defun org-get-entry ()
6351 "Get the entry text, after heading, entire subtree."
6352 (save-excursion
6353 (org-back-to-heading t)
6354 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
6356 (defun org-insert-heading-after-current ()
6357 "Insert a new heading with same level as current, after current subtree."
6358 (interactive)
6359 (org-back-to-heading)
6360 (org-insert-heading)
6361 (org-move-subtree-down)
6362 (end-of-line 1))
6364 (defun org-insert-heading-respect-content ()
6365 (interactive)
6366 (let ((org-insert-heading-respect-content t))
6367 (org-insert-heading t)))
6369 (defun org-insert-todo-heading-respect-content (&optional force-state)
6370 (interactive "P")
6371 (let ((org-insert-heading-respect-content t))
6372 (org-insert-todo-heading force-state t)))
6374 (defun org-insert-todo-heading (arg &optional force-heading)
6375 "Insert a new heading with the same level and TODO state as current heading.
6376 If the heading has no TODO state, or if the state is DONE, use the first
6377 state (TODO by default). Also with prefix arg, force first state."
6378 (interactive "P")
6379 (when (or force-heading (not (org-insert-item 'checkbox)))
6380 (org-insert-heading force-heading)
6381 (save-excursion
6382 (org-back-to-heading)
6383 (outline-previous-heading)
6384 (looking-at org-todo-line-regexp))
6385 (let*
6386 ((new-mark-x
6387 (if (or arg
6388 (not (match-beginning 2))
6389 (member (match-string 2) org-done-keywords))
6390 (car org-todo-keywords-1)
6391 (match-string 2)))
6392 (new-mark
6394 (run-hook-with-args-until-success
6395 'org-todo-get-default-hook new-mark-x nil)
6396 new-mark-x)))
6397 (beginning-of-line 1)
6398 (and (looking-at "\\*+ ") (goto-char (match-end 0))
6399 (if org-treat-insert-todo-heading-as-state-change
6400 (org-todo new-mark)
6401 (insert new-mark " "))))
6402 (when org-provide-todo-statistics
6403 (org-update-parent-todo-statistics))))
6405 (defun org-insert-subheading (arg)
6406 "Insert a new subheading and demote it.
6407 Works for outline headings and for plain lists alike."
6408 (interactive "P")
6409 (org-insert-heading arg)
6410 (cond
6411 ((org-on-heading-p) (org-do-demote))
6412 ((org-at-item-p) (org-indent-item 1))))
6414 (defun org-insert-todo-subheading (arg)
6415 "Insert a new subheading with TODO keyword or checkbox and demote it.
6416 Works for outline headings and for plain lists alike."
6417 (interactive "P")
6418 (org-insert-todo-heading arg)
6419 (cond
6420 ((org-on-heading-p) (org-do-demote))
6421 ((org-at-item-p) (org-indent-item 1))))
6423 ;;; Promotion and Demotion
6425 (defvar org-after-demote-entry-hook nil
6426 "Hook run after an entry has been demoted.
6427 The cursor will be at the beginning of the entry.
6428 When a subtree is being demoted, the hook will be called for each node.")
6430 (defvar org-after-promote-entry-hook nil
6431 "Hook run after an entry has been promoted.
6432 The cursor will be at the beginning of the entry.
6433 When a subtree is being promoted, the hook will be called for each node.")
6435 (defun org-promote-subtree ()
6436 "Promote the entire subtree.
6437 See also `org-promote'."
6438 (interactive)
6439 (save-excursion
6440 (org-map-tree 'org-promote))
6441 (org-fix-position-after-promote))
6443 (defun org-demote-subtree ()
6444 "Demote the entire subtree. See `org-demote'.
6445 See also `org-promote'."
6446 (interactive)
6447 (save-excursion
6448 (org-map-tree 'org-demote))
6449 (org-fix-position-after-promote))
6452 (defun org-do-promote ()
6453 "Promote the current heading higher up the tree.
6454 If the region is active in `transient-mark-mode', promote all headings
6455 in the region."
6456 (interactive)
6457 (save-excursion
6458 (if (org-region-active-p)
6459 (org-map-region 'org-promote (region-beginning) (region-end))
6460 (org-promote)))
6461 (org-fix-position-after-promote))
6463 (defun org-do-demote ()
6464 "Demote the current heading lower down the tree.
6465 If the region is active in `transient-mark-mode', demote all headings
6466 in the region."
6467 (interactive)
6468 (save-excursion
6469 (if (org-region-active-p)
6470 (org-map-region 'org-demote (region-beginning) (region-end))
6471 (org-demote)))
6472 (org-fix-position-after-promote))
6474 (defun org-fix-position-after-promote ()
6475 "Make sure that after pro/demotion cursor position is right."
6476 (let ((pos (point)))
6477 (when (save-excursion
6478 (beginning-of-line 1)
6479 (looking-at org-todo-line-regexp)
6480 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
6481 (cond ((eobp) (insert " "))
6482 ((eolp) (insert " "))
6483 ((equal (char-after) ?\ ) (forward-char 1))))))
6485 (defun org-current-level ()
6486 "Return the level of the current entry, or nil if before the first headline.
6487 The level is the number of stars at the beginning of the headline."
6488 (save-excursion
6489 (condition-case nil
6490 (progn
6491 (org-back-to-heading t)
6492 (funcall outline-level))
6493 (error nil))))
6495 (defun org-get-previous-line-level ()
6496 "Return the outline depth of the last headline before the current line.
6497 Returns 0 for the first headline in the buffer, and nil if before the
6498 first headline."
6499 (let ((current-level (org-current-level))
6500 (prev-level (when (> (line-number-at-pos) 1)
6501 (save-excursion
6502 (beginning-of-line 0)
6503 (org-current-level)))))
6504 (cond ((null current-level) nil) ; Before first headline
6505 ((null prev-level) 0) ; At first headline
6506 (prev-level))))
6508 (defun org-reduced-level (l)
6509 "Compute the effective level of a heading.
6510 This takes into account the setting of `org-odd-levels-only'."
6511 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
6513 (defun org-level-increment ()
6514 "Return the number of stars that will be added or removed at a
6515 time to headlines when structure editing, based on the value of
6516 `org-odd-levels-only'."
6517 (if org-odd-levels-only 2 1))
6519 (defun org-get-valid-level (level &optional change)
6520 "Rectify a level change under the influence of `org-odd-levels-only'
6521 LEVEL is a current level, CHANGE is by how much the level should be
6522 modified. Even if CHANGE is nil, LEVEL may be returned modified because
6523 even level numbers will become the next higher odd number."
6524 (if org-odd-levels-only
6525 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
6526 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
6527 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
6528 (max 1 (+ level (or change 0)))))
6530 (if (boundp 'define-obsolete-function-alias)
6531 (if (or (featurep 'xemacs) (< emacs-major-version 23))
6532 (define-obsolete-function-alias 'org-get-legal-level
6533 'org-get-valid-level)
6534 (define-obsolete-function-alias 'org-get-legal-level
6535 'org-get-valid-level "23.1")))
6537 (defun org-promote ()
6538 "Promote the current heading higher up the tree.
6539 If the region is active in `transient-mark-mode', promote all headings
6540 in the region."
6541 (org-back-to-heading t)
6542 (let* ((level (save-match-data (funcall outline-level)))
6543 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
6544 (diff (abs (- level (length up-head) -1))))
6545 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
6546 (replace-match up-head nil t)
6547 ;; Fixup tag positioning
6548 (and org-auto-align-tags (org-set-tags nil t))
6549 (if org-adapt-indentation (org-fixup-indentation (- diff)))
6550 (run-hooks 'org-after-promote-entry-hook)))
6552 (defun org-demote ()
6553 "Demote the current heading lower down the tree.
6554 If the region is active in `transient-mark-mode', demote all headings
6555 in the region."
6556 (org-back-to-heading t)
6557 (let* ((level (save-match-data (funcall outline-level)))
6558 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
6559 (diff (abs (- level (length down-head) -1))))
6560 (replace-match down-head nil t)
6561 ;; Fixup tag positioning
6562 (and org-auto-align-tags (org-set-tags nil t))
6563 (if org-adapt-indentation (org-fixup-indentation diff))
6564 (run-hooks 'org-after-demote-entry-hook)))
6566 (defun org-cycle-level ()
6567 "Cycle the level of an empty headline through possible states.
6568 This goes first to child, then to parent, level, then up the hierarchy.
6569 After top level, it switches back to sibling level."
6570 (interactive)
6571 (let ((org-adapt-indentation nil))
6572 (when (org-point-at-end-of-empty-headline)
6573 (setq this-command 'org-cycle-level) ; Only needed for caching
6574 (let ((cur-level (org-current-level))
6575 (prev-level (org-get-previous-line-level)))
6576 (cond
6577 ;; If first headline in file, promote to top-level.
6578 ((= prev-level 0)
6579 (loop repeat (/ (- cur-level 1) (org-level-increment))
6580 do (org-do-promote)))
6581 ;; If same level as prev, demote one.
6582 ((= prev-level cur-level)
6583 (org-do-demote))
6584 ;; If parent is top-level, promote to top level if not already.
6585 ((= prev-level 1)
6586 (loop repeat (/ (- cur-level 1) (org-level-increment))
6587 do (org-do-promote)))
6588 ;; If top-level, return to prev-level.
6589 ((= cur-level 1)
6590 (loop repeat (/ (- prev-level 1) (org-level-increment))
6591 do (org-do-demote)))
6592 ;; If less than prev-level, promote one.
6593 ((< cur-level prev-level)
6594 (org-do-promote))
6595 ;; If deeper than prev-level, promote until higher than
6596 ;; prev-level.
6597 ((> cur-level prev-level)
6598 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
6599 do (org-do-promote))))
6600 t))))
6602 (defun org-map-tree (fun)
6603 "Call FUN for every heading underneath the current one."
6604 (org-back-to-heading)
6605 (let ((level (funcall outline-level)))
6606 (save-excursion
6607 (funcall fun)
6608 (while (and (progn
6609 (outline-next-heading)
6610 (> (funcall outline-level) level))
6611 (not (eobp)))
6612 (funcall fun)))))
6614 (defun org-map-region (fun beg end)
6615 "Call FUN for every heading between BEG and END."
6616 (let ((org-ignore-region t))
6617 (save-excursion
6618 (setq end (copy-marker end))
6619 (goto-char beg)
6620 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
6621 (< (point) end))
6622 (funcall fun))
6623 (while (and (progn
6624 (outline-next-heading)
6625 (< (point) end))
6626 (not (eobp)))
6627 (funcall fun)))))
6629 (defun org-fixup-indentation (diff)
6630 "Change the indentation in the current entry by DIFF
6631 However, if any line in the current entry has no indentation, or if it
6632 would end up with no indentation after the change, nothing at all is done."
6633 (save-excursion
6634 (let ((end (save-excursion (outline-next-heading)
6635 (point-marker)))
6636 (prohibit (if (> diff 0)
6637 "^\\S-"
6638 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
6639 col)
6640 (unless (save-excursion (end-of-line 1)
6641 (re-search-forward prohibit end t))
6642 (while (and (< (point) end)
6643 (re-search-forward "^[ \t]+" end t))
6644 (goto-char (match-end 0))
6645 (setq col (current-column))
6646 (if (< diff 0) (replace-match ""))
6647 (org-indent-to-column (+ diff col))))
6648 (move-marker end nil))))
6650 (defun org-convert-to-odd-levels ()
6651 "Convert an org-mode file with all levels allowed to one with odd levels.
6652 This will leave level 1 alone, convert level 2 to level 3, level 3 to
6653 level 5 etc."
6654 (interactive)
6655 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
6656 (let ((outline-regexp org-outline-regexp)
6657 (outline-level 'org-outline-level)
6658 (org-odd-levels-only nil) n)
6659 (save-excursion
6660 (goto-char (point-min))
6661 (while (re-search-forward "^\\*\\*+ " nil t)
6662 (setq n (- (length (match-string 0)) 2))
6663 (while (>= (setq n (1- n)) 0)
6664 (org-demote))
6665 (end-of-line 1))))))
6667 (defun org-convert-to-oddeven-levels ()
6668 "Convert an org-mode file with only odd levels to one with odd and even levels.
6669 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
6670 section with an even level, conversion would destroy the structure of the file. An error
6671 is signaled in this case."
6672 (interactive)
6673 (goto-char (point-min))
6674 ;; First check if there are no even levels
6675 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
6676 (org-show-context t)
6677 (error "Not all levels are odd in this file. Conversion not possible"))
6678 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
6679 (let ((outline-regexp org-outline-regexp)
6680 (outline-level 'org-outline-level)
6681 (org-odd-levels-only nil) n)
6682 (save-excursion
6683 (goto-char (point-min))
6684 (while (re-search-forward "^\\*\\*+ " nil t)
6685 (setq n (/ (1- (length (match-string 0))) 2))
6686 (while (>= (setq n (1- n)) 0)
6687 (org-promote))
6688 (end-of-line 1))))))
6690 (defun org-tr-level (n)
6691 "Make N odd if required."
6692 (if org-odd-levels-only (1+ (/ n 2)) n))
6694 ;;; Vertical tree motion, cutting and pasting of subtrees
6696 (defun org-move-subtree-up (&optional arg)
6697 "Move the current subtree up past ARG headlines of the same level."
6698 (interactive "p")
6699 (org-move-subtree-down (- (prefix-numeric-value arg))))
6701 (defun org-move-subtree-down (&optional arg)
6702 "Move the current subtree down past ARG headlines of the same level."
6703 (interactive "p")
6704 (setq arg (prefix-numeric-value arg))
6705 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
6706 'org-get-last-sibling))
6707 (ins-point (make-marker))
6708 (cnt (abs arg))
6709 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
6710 ;; Select the tree
6711 (org-back-to-heading)
6712 (setq beg0 (point))
6713 (save-excursion
6714 (setq ne-beg (org-back-over-empty-lines))
6715 (setq beg (point)))
6716 (save-match-data
6717 (save-excursion (outline-end-of-heading)
6718 (setq folded (org-invisible-p)))
6719 (outline-end-of-subtree))
6720 (outline-next-heading)
6721 (setq ne-end (org-back-over-empty-lines))
6722 (setq end (point))
6723 (goto-char beg0)
6724 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
6725 ;; include less whitespace
6726 (save-excursion
6727 (goto-char beg)
6728 (forward-line (- ne-beg ne-end))
6729 (setq beg (point))))
6730 ;; Find insertion point, with error handling
6731 (while (> cnt 0)
6732 (or (and (funcall movfunc) (looking-at outline-regexp))
6733 (progn (goto-char beg0)
6734 (error "Cannot move past superior level or buffer limit")))
6735 (setq cnt (1- cnt)))
6736 (if (> arg 0)
6737 ;; Moving forward - still need to move over subtree
6738 (progn (org-end-of-subtree t t)
6739 (save-excursion
6740 (org-back-over-empty-lines)
6741 (or (bolp) (newline)))))
6742 (setq ne-ins (org-back-over-empty-lines))
6743 (move-marker ins-point (point))
6744 (setq txt (buffer-substring beg end))
6745 (org-save-markers-in-region beg end)
6746 (delete-region beg end)
6747 (org-remove-empty-overlays-at beg)
6748 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
6749 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
6750 (and (not (bolp)) (looking-at "\n") (forward-char 1))
6751 (let ((bbb (point)))
6752 (insert-before-markers txt)
6753 (org-reinstall-markers-in-region bbb)
6754 (move-marker ins-point bbb))
6755 (or (bolp) (insert "\n"))
6756 (setq ins-end (point))
6757 (goto-char ins-point)
6758 (org-skip-whitespace)
6759 (when (and (< arg 0)
6760 (org-first-sibling-p)
6761 (> ne-ins ne-beg))
6762 ;; Move whitespace back to beginning
6763 (save-excursion
6764 (goto-char ins-end)
6765 (let ((kill-whole-line t))
6766 (kill-line (- ne-ins ne-beg)) (point)))
6767 (insert (make-string (- ne-ins ne-beg) ?\n)))
6768 (move-marker ins-point nil)
6769 (if folded
6770 (hide-subtree)
6771 (org-show-entry)
6772 (show-children)
6773 (org-cycle-hide-drawers 'children))
6774 (org-clean-visibility-after-subtree-move)))
6776 (defvar org-subtree-clip ""
6777 "Clipboard for cut and paste of subtrees.
6778 This is actually only a copy of the kill, because we use the normal kill
6779 ring. We need it to check if the kill was created by `org-copy-subtree'.")
6781 (defvar org-subtree-clip-folded nil
6782 "Was the last copied subtree folded?
6783 This is used to fold the tree back after pasting.")
6785 (defun org-cut-subtree (&optional n)
6786 "Cut the current subtree into the clipboard.
6787 With prefix arg N, cut this many sequential subtrees.
6788 This is a short-hand for marking the subtree and then cutting it."
6789 (interactive "p")
6790 (org-copy-subtree n 'cut))
6792 (defun org-copy-subtree (&optional n cut force-store-markers)
6793 "Cut the current subtree into the clipboard.
6794 With prefix arg N, cut this many sequential subtrees.
6795 This is a short-hand for marking the subtree and then copying it.
6796 If CUT is non-nil, actually cut the subtree.
6797 If FORCE-STORE-MARKERS is non-nil, store the relative locations
6798 of some markers in the region, even if CUT is non-nil. This is
6799 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
6800 (interactive "p")
6801 (let (beg end folded (beg0 (point)))
6802 (if (interactive-p)
6803 (org-back-to-heading nil) ; take what looks like a subtree
6804 (org-back-to-heading t)) ; take what is really there
6805 (org-back-over-empty-lines)
6806 (setq beg (point))
6807 (skip-chars-forward " \t\r\n")
6808 (save-match-data
6809 (save-excursion (outline-end-of-heading)
6810 (setq folded (org-invisible-p)))
6811 (condition-case nil
6812 (org-forward-same-level (1- n) t)
6813 (error nil))
6814 (org-end-of-subtree t t))
6815 (org-back-over-empty-lines)
6816 (setq end (point))
6817 (goto-char beg0)
6818 (when (> end beg)
6819 (setq org-subtree-clip-folded folded)
6820 (when (or cut force-store-markers)
6821 (org-save-markers-in-region beg end))
6822 (if cut (kill-region beg end) (copy-region-as-kill beg end))
6823 (setq org-subtree-clip (current-kill 0))
6824 (message "%s: Subtree(s) with %d characters"
6825 (if cut "Cut" "Copied")
6826 (length org-subtree-clip)))))
6828 (defun org-paste-subtree (&optional level tree for-yank)
6829 "Paste the clipboard as a subtree, with modification of headline level.
6830 The entire subtree is promoted or demoted in order to match a new headline
6831 level.
6833 If the cursor is at the beginning of a headline, the same level as
6834 that headline is used to paste the tree
6836 If not, the new level is derived from the *visible* headings
6837 before and after the insertion point, and taken to be the inferior headline
6838 level of the two. So if the previous visible heading is level 3 and the
6839 next is level 4 (or vice versa), level 4 will be used for insertion.
6840 This makes sure that the subtree remains an independent subtree and does
6841 not swallow low level entries.
6843 You can also force a different level, either by using a numeric prefix
6844 argument, or by inserting the heading marker by hand. For example, if the
6845 cursor is after \"*****\", then the tree will be shifted to level 5.
6847 If optional TREE is given, use this text instead of the kill ring.
6849 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
6850 move back over whitespace before inserting, and move point to the end of
6851 the inserted text when done."
6852 (interactive "P")
6853 (setq tree (or tree (and kill-ring (current-kill 0))))
6854 (unless (org-kill-is-subtree-p tree)
6855 (error "%s"
6856 (substitute-command-keys
6857 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
6858 (let* ((visp (not (org-invisible-p)))
6859 (txt tree)
6860 (^re (concat "^\\(" outline-regexp "\\)"))
6861 (re (concat "\\(" outline-regexp "\\)"))
6862 (^re_ (concat "\\(\\*+\\)[ \t]*"))
6864 (old-level (if (string-match ^re txt)
6865 (- (match-end 0) (match-beginning 0) 1)
6866 -1))
6867 (force-level (cond (level (prefix-numeric-value level))
6868 ((and (looking-at "[ \t]*$")
6869 (string-match
6870 ^re_ (buffer-substring
6871 (point-at-bol) (point))))
6872 (- (match-end 1) (match-beginning 1)))
6873 ((and (bolp)
6874 (looking-at org-outline-regexp))
6875 (- (match-end 0) (point) 1))
6876 (t nil)))
6877 (previous-level (save-excursion
6878 (condition-case nil
6879 (progn
6880 (outline-previous-visible-heading 1)
6881 (if (looking-at re)
6882 (- (match-end 0) (match-beginning 0) 1)
6884 (error 1))))
6885 (next-level (save-excursion
6886 (condition-case nil
6887 (progn
6888 (or (looking-at outline-regexp)
6889 (outline-next-visible-heading 1))
6890 (if (looking-at re)
6891 (- (match-end 0) (match-beginning 0) 1)
6893 (error 1))))
6894 (new-level (or force-level (max previous-level next-level)))
6895 (shift (if (or (= old-level -1)
6896 (= new-level -1)
6897 (= old-level new-level))
6899 (- new-level old-level)))
6900 (delta (if (> shift 0) -1 1))
6901 (func (if (> shift 0) 'org-demote 'org-promote))
6902 (org-odd-levels-only nil)
6903 beg end newend)
6904 ;; Remove the forced level indicator
6905 (if force-level
6906 (delete-region (point-at-bol) (point)))
6907 ;; Paste
6908 (beginning-of-line 1)
6909 (unless for-yank (org-back-over-empty-lines))
6910 (setq beg (point))
6911 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
6912 (insert-before-markers txt)
6913 (unless (string-match "\n\\'" txt) (insert "\n"))
6914 (setq newend (point))
6915 (org-reinstall-markers-in-region beg)
6916 (setq end (point))
6917 (goto-char beg)
6918 (skip-chars-forward " \t\n\r")
6919 (setq beg (point))
6920 (if (and (org-invisible-p) visp)
6921 (save-excursion (outline-show-heading)))
6922 ;; Shift if necessary
6923 (unless (= shift 0)
6924 (save-restriction
6925 (narrow-to-region beg end)
6926 (while (not (= shift 0))
6927 (org-map-region func (point-min) (point-max))
6928 (setq shift (+ delta shift)))
6929 (goto-char (point-min))
6930 (setq newend (point-max))))
6931 (when (or (interactive-p) for-yank)
6932 (message "Clipboard pasted as level %d subtree" new-level))
6933 (if (and (not for-yank) ; in this case, org-yank will decide about folding
6934 kill-ring
6935 (eq org-subtree-clip (current-kill 0))
6936 org-subtree-clip-folded)
6937 ;; The tree was folded before it was killed/copied
6938 (hide-subtree))
6939 (and for-yank (goto-char newend))))
6941 (defun org-kill-is-subtree-p (&optional txt)
6942 "Check if the current kill is an outline subtree, or a set of trees.
6943 Returns nil if kill does not start with a headline, or if the first
6944 headline level is not the largest headline level in the tree.
6945 So this will actually accept several entries of equal levels as well,
6946 which is OK for `org-paste-subtree'.
6947 If optional TXT is given, check this string instead of the current kill."
6948 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
6949 (start-level (and kill
6950 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
6951 org-outline-regexp "\\)")
6952 kill)
6953 (- (match-end 2) (match-beginning 2) 1)))
6954 (re (concat "^" org-outline-regexp))
6955 (start (1+ (or (match-beginning 2) -1))))
6956 (if (not start-level)
6957 (progn
6958 nil) ;; does not even start with a heading
6959 (catch 'exit
6960 (while (setq start (string-match re kill (1+ start)))
6961 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
6962 (throw 'exit nil)))
6963 t))))
6965 (defvar org-markers-to-move nil
6966 "Markers that should be moved with a cut-and-paste operation.
6967 Those markers are stored together with their positions relative to
6968 the start of the region.")
6970 (defun org-save-markers-in-region (beg end)
6971 "Check markers in region.
6972 If these markers are between BEG and END, record their position relative
6973 to BEG, so that after moving the block of text, we can put the markers back
6974 into place.
6975 This function gets called just before an entry or tree gets cut from the
6976 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
6977 called immediately, to move the markers with the entries."
6978 (setq org-markers-to-move nil)
6979 (when (featurep 'org-clock)
6980 (org-clock-save-markers-for-cut-and-paste beg end))
6981 (when (featurep 'org-agenda)
6982 (org-agenda-save-markers-for-cut-and-paste beg end)))
6984 (defun org-check-and-save-marker (marker beg end)
6985 "Check if MARKER is between BEG and END.
6986 If yes, remember the marker and the distance to BEG."
6987 (when (and (marker-buffer marker)
6988 (equal (marker-buffer marker) (current-buffer)))
6989 (if (and (>= marker beg) (< marker end))
6990 (push (cons marker (- marker beg)) org-markers-to-move))))
6992 (defun org-reinstall-markers-in-region (beg)
6993 "Move all remembered markers to their position relative to BEG."
6994 (mapc (lambda (x)
6995 (move-marker (car x) (+ beg (cdr x))))
6996 org-markers-to-move)
6997 (setq org-markers-to-move nil))
6999 (defun org-narrow-to-subtree ()
7000 "Narrow buffer to the current subtree."
7001 (interactive)
7002 (save-excursion
7003 (save-match-data
7004 (narrow-to-region
7005 (progn (org-back-to-heading t) (point))
7006 (progn (org-end-of-subtree t t)
7007 (if (org-on-heading-p) (backward-char 1))
7008 (point))))))
7010 (defun org-clone-subtree-with-time-shift (n &optional shift)
7011 "Clone the task (subtree) at point N times.
7012 The clones will be inserted as siblings.
7014 In interactive use, the user will be prompted for the number of clones
7015 to be produced, and for a time SHIFT, which may be a repeater as used
7016 in time stamps, for example `+3d'.
7018 When a valid repeater is given and the entry contains any time stamps,
7019 the clones will become a sequence in time, with time stamps in the
7020 subtree shifted for each clone produced. If SHIFT is nil or the
7021 empty string, time stamps will be left alone.
7023 If the original subtree did contain time stamps with a repeater,
7024 the following will happen:
7025 - the repeater will be removed in each clone
7026 - an additional clone will be produced, with the current, unshifted
7027 date(s) in the entry.
7028 - the original entry will be placed *after* all the clones, with
7029 repeater intact.
7030 - the start days in the repeater in the original entry will be shifted
7031 to past the last clone.
7032 I this way you can spell out a number of instances of a repeating task,
7033 and still retain the repeater to cover future instances of the task."
7034 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
7035 (let (beg end template task
7036 shift-n shift-what doshift nmin nmax (n-no-remove -1))
7037 (if (not (and (integerp n) (> n 0)))
7038 (error "Invalid number of replications %s" n))
7039 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
7040 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
7041 shift)))
7042 (error "Invalid shift specification %s" shift))
7043 (when doshift
7044 (setq shift-n (string-to-number (match-string 1 shift))
7045 shift-what (cdr (assoc (match-string 2 shift)
7046 '(("d" . day) ("w" . week)
7047 ("m" . month) ("y" . year))))))
7048 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
7049 (setq nmin 1 nmax n)
7050 (org-back-to-heading t)
7051 (setq beg (point))
7052 (org-end-of-subtree t t)
7053 (or (bolp) (insert "\n"))
7054 (setq end (point))
7055 (setq template (buffer-substring beg end))
7056 (when (and doshift
7057 (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
7058 (delete-region beg end)
7059 (setq end beg)
7060 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
7061 (goto-char end)
7062 (loop for n from nmin to nmax do
7063 (if (not doshift)
7064 (setq task template)
7065 (with-temp-buffer
7066 (insert template)
7067 (org-mode)
7068 (goto-char (point-min))
7069 (while (re-search-forward org-ts-regexp-both nil t)
7070 (org-timestamp-change (* n shift-n) shift-what))
7071 (unless (= n n-no-remove)
7072 (goto-char (point-min))
7073 (while (re-search-forward org-ts-regexp nil t)
7074 (save-excursion
7075 (goto-char (match-beginning 0))
7076 (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
7077 (delete-region (match-beginning 1) (match-end 1))))))
7078 (setq task (buffer-string))))
7079 (insert task))
7080 (goto-char beg)))
7082 ;;; Outline Sorting
7084 (defun org-sort (with-case)
7085 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
7086 Optional argument WITH-CASE means sort case-sensitively.
7087 With a double prefix argument, also remove duplicate entries."
7088 (interactive "P")
7089 (if (org-at-table-p)
7090 (org-call-with-arg 'org-table-sort-lines with-case)
7091 (org-call-with-arg 'org-sort-entries-or-items with-case)))
7093 (defun org-sort-remove-invisible (s)
7094 (remove-text-properties 0 (length s) org-rm-props s)
7095 (while (string-match org-bracket-link-regexp s)
7096 (setq s (replace-match (if (match-end 2)
7097 (match-string 3 s)
7098 (match-string 1 s)) t t s)))
7101 (defvar org-priority-regexp) ; defined later in the file
7103 (defvar org-after-sorting-entries-or-items-hook nil
7104 "Hook that is run after a bunch of entries or items have been sorted.
7105 When children are sorted, the cursor is in the parent line when this
7106 hook gets called. When a region or a plain list is sorted, the cursor
7107 will be in the first entry of the sorted region/list.")
7109 (defun org-sort-entries-or-items
7110 (&optional with-case sorting-type getkey-func compare-func property)
7111 "Sort entries on a certain level of an outline tree, or plain list items.
7112 If there is an active region, the entries in the region are sorted.
7113 Else, if the cursor is before the first entry, sort the top-level items.
7114 Else, the children of the entry at point are sorted.
7115 If the cursor is at the first item in a plain list, the list items will be
7116 sorted.
7118 Sorting can be alphabetically, numerically, by date/time as given by
7119 a time stamp, by a property or by priority.
7121 The command prompts for the sorting type unless it has been given to the
7122 function through the SORTING-TYPE argument, which needs to a character,
7123 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
7124 precise meaning of each character:
7126 n Numerically, by converting the beginning of the entry/item to a number.
7127 a Alphabetically, ignoring the TODO keyword and the priority, if any.
7128 t By date/time, either the first active time stamp in the entry, or, if
7129 none exist, by the first inactive one.
7130 In items, only the first line will be checked.
7131 s By the scheduled date/time.
7132 d By deadline date/time.
7133 c By creation time, which is assumed to be the first inactive time stamp
7134 at the beginning of a line.
7135 p By priority according to the cookie.
7136 r By the value of a property.
7138 Capital letters will reverse the sort order.
7140 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
7141 called with point at the beginning of the record. It must return either
7142 a string or a number that should serve as the sorting key for that record.
7144 Comparing entries ignores case by default. However, with an optional argument
7145 WITH-CASE, the sorting considers case as well."
7146 (interactive "P")
7147 (let ((case-func (if with-case 'identity 'downcase))
7148 start beg end stars re re2
7149 txt what tmp plain-list-p)
7150 ;; Find beginning and end of region to sort
7151 (cond
7152 ((org-region-active-p)
7153 ;; we will sort the region
7154 (setq end (region-end)
7155 what "region")
7156 (goto-char (region-beginning))
7157 (if (not (org-on-heading-p)) (outline-next-heading))
7158 (setq start (point)))
7159 ((org-at-item-p)
7160 ;; we will sort this plain list
7161 (org-beginning-of-item-list) (setq start (point))
7162 (org-end-of-item-list)
7163 (or (bolp) (insert "\n"))
7164 (setq end (point))
7165 (goto-char start)
7166 (setq plain-list-p t
7167 what "plain list"))
7168 ((or (org-on-heading-p)
7169 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
7170 ;; we will sort the children of the current headline
7171 (org-back-to-heading)
7172 (setq start (point)
7173 end (progn (org-end-of-subtree t t)
7174 (or (bolp) (insert "\n"))
7175 (org-back-over-empty-lines)
7176 (point))
7177 what "children")
7178 (goto-char start)
7179 (show-subtree)
7180 (outline-next-heading))
7182 ;; we will sort the top-level entries in this file
7183 (goto-char (point-min))
7184 (or (org-on-heading-p) (outline-next-heading))
7185 (setq start (point))
7186 (goto-char (point-max))
7187 (beginning-of-line 1)
7188 (when (looking-at ".*?\\S-")
7189 ;; File ends in a non-white line
7190 (end-of-line 1)
7191 (insert "\n"))
7192 (setq end (point-max))
7193 (setq what "top-level")
7194 (goto-char start)
7195 (show-all)))
7197 (setq beg (point))
7198 (if (>= beg end) (error "Nothing to sort"))
7200 (unless plain-list-p
7201 (looking-at "\\(\\*+\\)")
7202 (setq stars (match-string 1)
7203 re (concat "^" (regexp-quote stars) " +")
7204 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
7205 txt (buffer-substring beg end))
7206 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
7207 (if (and (not (equal stars "*")) (string-match re2 txt))
7208 (error "Region to sort contains a level above the first entry")))
7210 (unless sorting-type
7211 (message
7212 (if plain-list-p
7213 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
7214 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
7215 [t]ime [s]cheduled [d]eadline [c]reated
7216 A/N/T/S/D/C/P/O/F means reversed:")
7217 what)
7218 (setq sorting-type (read-char-exclusive))
7220 (and (= (downcase sorting-type) ?f)
7221 (setq getkey-func
7222 (org-icompleting-read "Sort using function: "
7223 obarray 'fboundp t nil nil))
7224 (setq getkey-func (intern getkey-func)))
7226 (and (= (downcase sorting-type) ?r)
7227 (setq property
7228 (org-icompleting-read "Property: "
7229 (mapcar 'list (org-buffer-property-keys t))
7230 nil t))))
7232 (message "Sorting entries...")
7234 (save-restriction
7235 (narrow-to-region start end)
7237 (let ((dcst (downcase sorting-type))
7238 (case-fold-search nil)
7239 (now (current-time)))
7240 (sort-subr
7241 (/= dcst sorting-type)
7242 ;; This function moves to the beginning character of the "record" to
7243 ;; be sorted.
7244 (if plain-list-p
7245 (lambda nil
7246 (if (org-at-item-p) t (goto-char (point-max))))
7247 (lambda nil
7248 (if (re-search-forward re nil t)
7249 (goto-char (match-beginning 0))
7250 (goto-char (point-max)))))
7251 ;; This function moves to the last character of the "record" being
7252 ;; sorted.
7253 (if plain-list-p
7254 'org-end-of-item
7255 (lambda nil
7256 (save-match-data
7257 (condition-case nil
7258 (outline-forward-same-level 1)
7259 (error
7260 (goto-char (point-max)))))))
7262 ;; This function returns the value that gets sorted against.
7263 (if plain-list-p
7264 (lambda nil
7265 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
7266 (cond
7267 ((= dcst ?n)
7268 (string-to-number (buffer-substring (match-end 0)
7269 (point-at-eol))))
7270 ((= dcst ?a)
7271 (buffer-substring (match-end 0) (point-at-eol)))
7272 ((= dcst ?t)
7273 (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
7274 (re-search-forward org-ts-regexp-both
7275 (point-at-eol) t))
7276 (org-time-string-to-seconds (match-string 0))
7277 (org-float-time now)))
7278 ((= dcst ?f)
7279 (if getkey-func
7280 (progn
7281 (setq tmp (funcall getkey-func))
7282 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
7283 tmp)
7284 (error "Invalid key function `%s'" getkey-func)))
7285 (t (error "Invalid sorting type `%c'" sorting-type)))))
7286 (lambda nil
7287 (cond
7288 ((= dcst ?n)
7289 (if (looking-at org-complex-heading-regexp)
7290 (string-to-number (match-string 4))
7291 nil))
7292 ((= dcst ?a)
7293 (if (looking-at org-complex-heading-regexp)
7294 (funcall case-func (match-string 4))
7295 nil))
7296 ((= dcst ?t)
7297 (let ((end (save-excursion (outline-next-heading) (point))))
7298 (if (or (re-search-forward org-ts-regexp end t)
7299 (re-search-forward org-ts-regexp-both end t))
7300 (org-time-string-to-seconds (match-string 0))
7301 (org-float-time now))))
7302 ((= dcst ?c)
7303 (let ((end (save-excursion (outline-next-heading) (point))))
7304 (if (re-search-forward
7305 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
7306 end t)
7307 (org-time-string-to-seconds (match-string 0))
7308 (org-float-time now))))
7309 ((= dcst ?s)
7310 (let ((end (save-excursion (outline-next-heading) (point))))
7311 (if (re-search-forward org-scheduled-time-regexp end t)
7312 (org-time-string-to-seconds (match-string 1))
7313 (org-float-time now))))
7314 ((= dcst ?d)
7315 (let ((end (save-excursion (outline-next-heading) (point))))
7316 (if (re-search-forward org-deadline-time-regexp end t)
7317 (org-time-string-to-seconds (match-string 1))
7318 (org-float-time now))))
7319 ((= dcst ?p)
7320 (if (re-search-forward org-priority-regexp (point-at-eol) t)
7321 (string-to-char (match-string 2))
7322 org-default-priority))
7323 ((= dcst ?r)
7324 (or (org-entry-get nil property) ""))
7325 ((= dcst ?o)
7326 (if (looking-at org-complex-heading-regexp)
7327 (- 9999 (length (member (match-string 2)
7328 org-todo-keywords-1)))))
7329 ((= dcst ?f)
7330 (if getkey-func
7331 (progn
7332 (setq tmp (funcall getkey-func))
7333 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
7334 tmp)
7335 (error "Invalid key function `%s'" getkey-func)))
7336 (t (error "Invalid sorting type `%c'" sorting-type)))))
7338 (cond
7339 ((= dcst ?a) 'string<)
7340 ((= dcst ?f) compare-func)
7341 ((member dcst '(?p ?t ?s ?d ?c)) '<)
7342 (t nil)))))
7343 (run-hooks 'org-after-sorting-entries-or-items-hook)
7344 (message "Sorting entries...done")))
7346 (defun org-do-sort (table what &optional with-case sorting-type)
7347 "Sort TABLE of WHAT according to SORTING-TYPE.
7348 The user will be prompted for the SORTING-TYPE if the call to this
7349 function does not specify it. WHAT is only for the prompt, to indicate
7350 what is being sorted. The sorting key will be extracted from
7351 the car of the elements of the table.
7352 If WITH-CASE is non-nil, the sorting will be case-sensitive."
7353 (unless sorting-type
7354 (message
7355 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
7356 what)
7357 (setq sorting-type (read-char-exclusive)))
7358 (let ((dcst (downcase sorting-type))
7359 extractfun comparefun)
7360 ;; Define the appropriate functions
7361 (cond
7362 ((= dcst ?n)
7363 (setq extractfun 'string-to-number
7364 comparefun (if (= dcst sorting-type) '< '>)))
7365 ((= dcst ?a)
7366 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
7367 (lambda(x) (downcase (org-sort-remove-invisible x))))
7368 comparefun (if (= dcst sorting-type)
7369 'string<
7370 (lambda (a b) (and (not (string< a b))
7371 (not (string= a b)))))))
7372 ((= dcst ?t)
7373 (setq extractfun
7374 (lambda (x)
7375 (if (or (string-match org-ts-regexp x)
7376 (string-match org-ts-regexp-both x))
7377 (org-float-time
7378 (org-time-string-to-time (match-string 0 x)))
7380 comparefun (if (= dcst sorting-type) '< '>)))
7381 (t (error "Invalid sorting type `%c'" sorting-type)))
7383 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
7384 table)
7385 (lambda (a b) (funcall comparefun (car a) (car b))))))
7388 ;;; The orgstruct minor mode
7390 ;; Define a minor mode which can be used in other modes in order to
7391 ;; integrate the org-mode structure editing commands.
7393 ;; This is really a hack, because the org-mode structure commands use
7394 ;; keys which normally belong to the major mode. Here is how it
7395 ;; works: The minor mode defines all the keys necessary to operate the
7396 ;; structure commands, but wraps the commands into a function which
7397 ;; tests if the cursor is currently at a headline or a plain list
7398 ;; item. If that is the case, the structure command is used,
7399 ;; temporarily setting many Org-mode variables like regular
7400 ;; expressions for filling etc. However, when any of those keys is
7401 ;; used at a different location, function uses `key-binding' to look
7402 ;; up if the key has an associated command in another currently active
7403 ;; keymap (minor modes, major mode, global), and executes that
7404 ;; command. There might be problems if any of the keys is otherwise
7405 ;; used as a prefix key.
7407 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
7408 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
7409 ;; addresses this by checking explicitly for both bindings.
7411 (defvar orgstruct-mode-map (make-sparse-keymap)
7412 "Keymap for the minor `orgstruct-mode'.")
7414 (defvar org-local-vars nil
7415 "List of local variables, for use by `orgstruct-mode'")
7417 ;;;###autoload
7418 (define-minor-mode orgstruct-mode
7419 "Toggle the minor more `orgstruct-mode'.
7420 This mode is for using Org-mode structure commands in other modes.
7421 The following key behave as if Org-mode was active, if the cursor
7422 is on a headline, or on a plain list item (both in the definition
7423 of Org-mode).
7425 M-up Move entry/item up
7426 M-down Move entry/item down
7427 M-left Promote
7428 M-right Demote
7429 M-S-up Move entry/item up
7430 M-S-down Move entry/item down
7431 M-S-left Promote subtree
7432 M-S-right Demote subtree
7433 M-q Fill paragraph and items like in Org-mode
7434 C-c ^ Sort entries
7435 C-c - Cycle list bullet
7436 TAB Cycle item visibility
7437 M-RET Insert new heading/item
7438 S-M-RET Insert new TODO heading / Checkbox item
7439 C-c C-c Set tags / toggle checkbox"
7440 nil " OrgStruct" nil
7441 (org-load-modules-maybe)
7442 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
7444 ;;;###autoload
7445 (defun turn-on-orgstruct ()
7446 "Unconditionally turn on `orgstruct-mode'."
7447 (orgstruct-mode 1))
7449 (defun orgstruct++-mode (&optional arg)
7450 "Toggle `orgstruct-mode', the enhanced version of it.
7451 In addition to setting orgstruct-mode, this also exports all indentation
7452 and autofilling variables from org-mode into the buffer. It will also
7453 recognize item context in multiline items.
7454 Note that turning off orgstruct-mode will *not* remove the
7455 indentation/paragraph settings. This can only be done by refreshing the
7456 major mode, for example with \\[normal-mode]."
7457 (interactive "P")
7458 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
7459 (if (< arg 1)
7460 (orgstruct-mode -1)
7461 (orgstruct-mode 1)
7462 (let (var val)
7463 (mapc
7464 (lambda (x)
7465 (when (string-match
7466 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7467 (symbol-name (car x)))
7468 (setq var (car x) val (nth 1 x))
7469 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
7470 org-local-vars)
7471 (org-set-local 'orgstruct-is-++ t))))
7473 (defvar orgstruct-is-++ nil
7474 "Is orgstruct-mode in ++ version in the current-buffer?")
7475 (make-variable-buffer-local 'orgstruct-is-++)
7477 ;;;###autoload
7478 (defun turn-on-orgstruct++ ()
7479 "Unconditionally turn on `orgstruct++-mode'."
7480 (orgstruct++-mode 1))
7482 (defun orgstruct-error ()
7483 "Error when there is no default binding for a structure key."
7484 (interactive)
7485 (error "This key has no function outside structure elements"))
7487 (defun orgstruct-setup ()
7488 "Setup orgstruct keymaps."
7489 (let ((nfunc 0)
7490 (bindings
7491 (list
7492 '([(meta up)] org-metaup)
7493 '([(meta down)] org-metadown)
7494 '([(meta left)] org-metaleft)
7495 '([(meta right)] org-metaright)
7496 '([(meta shift up)] org-shiftmetaup)
7497 '([(meta shift down)] org-shiftmetadown)
7498 '([(meta shift left)] org-shiftmetaleft)
7499 '([(meta shift right)] org-shiftmetaright)
7500 '([?\e (up)] org-metaup)
7501 '([?\e (down)] org-metadown)
7502 '([?\e (left)] org-metaleft)
7503 '([?\e (right)] org-metaright)
7504 '([?\e (shift up)] org-shiftmetaup)
7505 '([?\e (shift down)] org-shiftmetadown)
7506 '([?\e (shift left)] org-shiftmetaleft)
7507 '([?\e (shift right)] org-shiftmetaright)
7508 '([(shift up)] org-shiftup)
7509 '([(shift down)] org-shiftdown)
7510 '([(shift left)] org-shiftleft)
7511 '([(shift right)] org-shiftright)
7512 '("\C-c\C-c" org-ctrl-c-ctrl-c)
7513 '("\M-q" fill-paragraph)
7514 '("\C-c^" org-sort)
7515 '("\C-c-" org-cycle-list-bullet)))
7516 elt key fun cmd)
7517 (while (setq elt (pop bindings))
7518 (setq nfunc (1+ nfunc))
7519 (setq key (org-key (car elt))
7520 fun (nth 1 elt)
7521 cmd (orgstruct-make-binding fun nfunc key))
7522 (org-defkey orgstruct-mode-map key cmd))
7524 ;; Special treatment needed for TAB and RET
7525 (org-defkey orgstruct-mode-map [(tab)]
7526 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
7527 (org-defkey orgstruct-mode-map "\C-i"
7528 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
7530 (org-defkey orgstruct-mode-map "\M-\C-m"
7531 (orgstruct-make-binding 'org-insert-heading 105
7532 "\M-\C-m" [(meta return)]))
7533 (org-defkey orgstruct-mode-map [(meta return)]
7534 (orgstruct-make-binding 'org-insert-heading 106
7535 [(meta return)] "\M-\C-m"))
7537 (org-defkey orgstruct-mode-map [(shift meta return)]
7538 (orgstruct-make-binding 'org-insert-todo-heading 107
7539 [(meta return)] "\M-\C-m"))
7541 (org-defkey orgstruct-mode-map "\e\C-m"
7542 (orgstruct-make-binding 'org-insert-heading 108
7543 "\e\C-m" [?\e (return)]))
7544 (org-defkey orgstruct-mode-map [?\e (return)]
7545 (orgstruct-make-binding 'org-insert-heading 109
7546 [?\e (return)] "\e\C-m"))
7547 (org-defkey orgstruct-mode-map [?\e (shift return)]
7548 (orgstruct-make-binding 'org-insert-todo-heading 110
7549 [?\e (return)] "\e\C-m"))
7551 (unless org-local-vars
7552 (setq org-local-vars (org-get-local-variables)))
7556 (defun orgstruct-make-binding (fun n &rest keys)
7557 "Create a function for binding in the structure minor mode.
7558 FUN is the command to call inside a table. N is used to create a unique
7559 command name. KEYS are keys that should be checked in for a command
7560 to execute outside of tables."
7561 (eval
7562 (list 'defun
7563 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
7564 '(arg)
7565 (concat "In Structure, run `" (symbol-name fun) "'.\n"
7566 "Outside of structure, run the binding of `"
7567 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
7568 "'.")
7569 '(interactive "p")
7570 (list 'if
7571 `(org-context-p 'headline 'item
7572 (and orgstruct-is-++
7573 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
7574 'item-body))
7575 (list 'org-run-like-in-org-mode (list 'quote fun))
7576 (list 'let '(orgstruct-mode)
7577 (list 'call-interactively
7578 (append '(or)
7579 (mapcar (lambda (k)
7580 (list 'key-binding k))
7581 keys)
7582 '('orgstruct-error))))))))
7584 (defun org-context-p (&rest contexts)
7585 "Check if local context is any of CONTEXTS.
7586 Possible values in the list of contexts are `table', `headline', and `item'."
7587 (let ((pos (point)))
7588 (goto-char (point-at-bol))
7589 (prog1 (or (and (memq 'table contexts)
7590 (looking-at "[ \t]*|"))
7591 (and (memq 'headline contexts)
7592 ;;????????? (looking-at "\\*+"))
7593 (looking-at outline-regexp))
7594 (and (memq 'item contexts)
7595 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
7596 (and (memq 'item-body contexts)
7597 (org-in-item-p)))
7598 (goto-char pos))))
7600 (defun org-get-local-variables ()
7601 "Return a list of all local variables in an org-mode buffer."
7602 (let (varlist)
7603 (with-current-buffer (get-buffer-create "*Org tmp*")
7604 (erase-buffer)
7605 (org-mode)
7606 (setq varlist (buffer-local-variables)))
7607 (kill-buffer "*Org tmp*")
7608 (delq nil
7609 (mapcar
7610 (lambda (x)
7611 (setq x
7612 (if (symbolp x)
7613 (list x)
7614 (list (car x) (list 'quote (cdr x)))))
7615 (if (string-match
7616 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7617 (symbol-name (car x)))
7618 x nil))
7619 varlist))))
7621 ;;;###autoload
7622 (defun org-run-like-in-org-mode (cmd)
7623 "Run a command, pretending that the current buffer is in Org-mode.
7624 This will temporarily bind local variables that are typically bound in
7625 Org-mode to the values they have in Org-mode, and then interactively
7626 call CMD."
7627 (org-load-modules-maybe)
7628 (unless org-local-vars
7629 (setq org-local-vars (org-get-local-variables)))
7630 (eval (list 'let org-local-vars
7631 (list 'call-interactively (list 'quote cmd)))))
7633 ;;;; Archiving
7635 (defun org-get-category (&optional pos)
7636 "Get the category applying to position POS."
7637 (get-text-property (or pos (point)) 'org-category))
7639 (defun org-refresh-category-properties ()
7640 "Refresh category text properties in the buffer."
7641 (let ((def-cat (cond
7642 ((null org-category)
7643 (if buffer-file-name
7644 (file-name-sans-extension
7645 (file-name-nondirectory buffer-file-name))
7646 "???"))
7647 ((symbolp org-category) (symbol-name org-category))
7648 (t org-category)))
7649 beg end cat pos optionp)
7650 (org-unmodified
7651 (save-excursion
7652 (save-restriction
7653 (widen)
7654 (goto-char (point-min))
7655 (put-text-property (point) (point-max) 'org-category def-cat)
7656 (while (re-search-forward
7657 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
7658 (setq pos (match-end 0)
7659 optionp (equal (char-after (match-beginning 0)) ?#)
7660 cat (org-trim (match-string 2)))
7661 (if optionp
7662 (setq beg (point-at-bol) end (point-max))
7663 (org-back-to-heading t)
7664 (setq beg (point) end (org-end-of-subtree t t)))
7665 (put-text-property beg end 'org-category cat)
7666 (goto-char pos)))))))
7669 ;;;; Link Stuff
7671 ;;; Link abbreviations
7673 (defun org-link-expand-abbrev (link)
7674 "Apply replacements as defined in `org-link-abbrev-alist."
7675 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
7676 (let* ((key (match-string 1 link))
7677 (as (or (assoc key org-link-abbrev-alist-local)
7678 (assoc key org-link-abbrev-alist)))
7679 (tag (and (match-end 2) (match-string 3 link)))
7680 rpl)
7681 (if (not as)
7682 link
7683 (setq rpl (cdr as))
7684 (cond
7685 ((symbolp rpl) (funcall rpl tag))
7686 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
7687 ((string-match "%h" rpl)
7688 (replace-match (url-hexify-string (or tag "")) t t rpl))
7689 (t (concat rpl tag)))))
7690 link))
7692 ;;; Storing and inserting links
7694 (defvar org-insert-link-history nil
7695 "Minibuffer history for links inserted with `org-insert-link'.")
7697 (defvar org-stored-links nil
7698 "Contains the links stored with `org-store-link'.")
7700 (defvar org-store-link-plist nil
7701 "Plist with info about the most recently link created with `org-store-link'.")
7703 (defvar org-link-protocols nil
7704 "Link protocols added to Org-mode using `org-add-link-type'.")
7706 (defvar org-store-link-functions nil
7707 "List of functions that are called to create and store a link.
7708 Each function will be called in turn until one returns a non-nil
7709 value. Each function should check if it is responsible for creating
7710 this link (for example by looking at the major mode).
7711 If not, it must exit and return nil.
7712 If yes, it should return a non-nil value after a calling
7713 `org-store-link-props' with a list of properties and values.
7714 Special properties are:
7716 :type The link prefix. like \"http\". This must be given.
7717 :link The link, like \"http://www.astro.uva.nl/~dominik\".
7718 This is obligatory as well.
7719 :description Optional default description for the second pair
7720 of brackets in an Org-mode link. The user can still change
7721 this when inserting this link into an Org-mode buffer.
7723 In addition to these, any additional properties can be specified
7724 and then used in remember templates.")
7726 (defun org-add-link-type (type &optional follow export)
7727 "Add TYPE to the list of `org-link-types'.
7728 Re-compute all regular expressions depending on `org-link-types'
7730 FOLLOW and EXPORT are two functions.
7732 FOLLOW should take the link path as the single argument and do whatever
7733 is necessary to follow the link, for example find a file or display
7734 a mail message.
7736 EXPORT should format the link path for export to one of the export formats.
7737 It should be a function accepting three arguments:
7739 path the path of the link, the text after the prefix (like \"http:\")
7740 desc the description of the link, if any, nil if there was no description
7741 format the export format, a symbol like `html' or `latex'.
7743 The function may use the FORMAT information to return different values
7744 depending on the format. The return value will be put literally into
7745 the exported file.
7746 Org-mode has a built-in default for exporting links. If you are happy with
7747 this default, there is no need to define an export function for the link
7748 type. For a simple example of an export function, see `org-bbdb.el'."
7749 (add-to-list 'org-link-types type t)
7750 (org-make-link-regexps)
7751 (if (assoc type org-link-protocols)
7752 (setcdr (assoc type org-link-protocols) (list follow export))
7753 (push (list type follow export) org-link-protocols)))
7755 (defvar org-agenda-buffer-name)
7757 ;;;###autoload
7758 (defun org-store-link (arg)
7759 "\\<org-mode-map>Store an org-link to the current location.
7760 This link is added to `org-stored-links' and can later be inserted
7761 into an org-buffer with \\[org-insert-link].
7763 For some link types, a prefix arg is interpreted:
7764 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
7765 For file links, arg negates `org-context-in-file-links'."
7766 (interactive "P")
7767 (org-load-modules-maybe)
7768 (setq org-store-link-plist nil) ; reset
7769 (let ((outline-regexp (org-get-limited-outline-regexp))
7770 link cpltxt desc description search txt custom-id)
7771 (cond
7773 ((run-hook-with-args-until-success 'org-store-link-functions)
7774 (setq link (plist-get org-store-link-plist :link)
7775 desc (or (plist-get org-store-link-plist :description) link)))
7777 ((equal (buffer-name) "*Org Edit Src Example*")
7778 (let (label gc)
7779 (while (or (not label)
7780 (save-excursion
7781 (save-restriction
7782 (widen)
7783 (goto-char (point-min))
7784 (re-search-forward
7785 (regexp-quote (format org-coderef-label-format label))
7786 nil t))))
7787 (when label (message "Label exists already") (sit-for 2))
7788 (setq label (read-string "Code line label: " label)))
7789 (end-of-line 1)
7790 (setq link (format org-coderef-label-format label))
7791 (setq gc (- 79 (length link)))
7792 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
7793 (insert link)
7794 (setq link (concat "(" label ")") desc nil)))
7796 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
7797 ;; We are in the agenda, link to referenced location
7798 (let ((m (or (get-text-property (point) 'org-hd-marker)
7799 (get-text-property (point) 'org-marker))))
7800 (when m
7801 (org-with-point-at m
7802 (call-interactively 'org-store-link)))))
7804 ((eq major-mode 'calendar-mode)
7805 (let ((cd (calendar-cursor-to-date)))
7806 (setq link
7807 (format-time-string
7808 (car org-time-stamp-formats)
7809 (apply 'encode-time
7810 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
7811 nil nil nil))))
7812 (org-store-link-props :type "calendar" :date cd)))
7814 ((eq major-mode 'w3-mode)
7815 (setq cpltxt (if (and (buffer-name)
7816 (not (string-match "Untitled" (buffer-name))))
7817 (buffer-name)
7818 (url-view-url t))
7819 link (org-make-link (url-view-url t)))
7820 (org-store-link-props :type "w3" :url (url-view-url t)))
7822 ((eq major-mode 'w3m-mode)
7823 (setq cpltxt (or w3m-current-title w3m-current-url)
7824 link (org-make-link w3m-current-url))
7825 (org-store-link-props :type "w3m" :url (url-view-url t)))
7827 ((setq search (run-hook-with-args-until-success
7828 'org-create-file-search-functions))
7829 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
7830 "::" search))
7831 (setq cpltxt (or description link)))
7833 ((eq major-mode 'image-mode)
7834 (setq cpltxt (concat "file:"
7835 (abbreviate-file-name buffer-file-name))
7836 link (org-make-link cpltxt))
7837 (org-store-link-props :type "image" :file buffer-file-name))
7839 ((eq major-mode 'dired-mode)
7840 ;; link to the file in the current line
7841 (let ((file (dired-get-filename nil t)))
7842 (setq file (if file
7843 (abbreviate-file-name
7844 (expand-file-name (dired-get-filename nil t)))
7845 ;; otherwise, no file so use current directory.
7846 default-directory))
7847 (setq cpltxt (concat "file:" file)
7848 link (org-make-link cpltxt))))
7850 ((and buffer-file-name (org-mode-p))
7851 (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
7852 (cond
7853 ((org-in-regexp "<<\\(.*?\\)>>")
7854 (setq cpltxt
7855 (concat "file:"
7856 (abbreviate-file-name buffer-file-name)
7857 "::" (match-string 1))
7858 link (org-make-link cpltxt)))
7859 ((and (featurep 'org-id)
7860 (or (eq org-link-to-org-use-id t)
7861 (and (eq org-link-to-org-use-id 'create-if-interactive)
7862 (interactive-p))
7863 (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
7864 (interactive-p)
7865 (not custom-id))
7866 (and org-link-to-org-use-id
7867 (condition-case nil
7868 (org-entry-get nil "ID")
7869 (error nil)))))
7870 ;; We can make a link using the ID.
7871 (setq link (condition-case nil
7872 (prog1 (org-id-store-link)
7873 (setq desc (plist-get org-store-link-plist
7874 :description)))
7875 (error
7876 ;; probably before first headline, link to file only
7877 (concat "file:"
7878 (abbreviate-file-name buffer-file-name))))))
7880 ;; Just link to current headline
7881 (setq cpltxt (concat "file:"
7882 (abbreviate-file-name buffer-file-name)))
7883 ;; Add a context search string
7884 (when (org-xor org-context-in-file-links arg)
7885 (setq txt (cond
7886 ((org-on-heading-p) nil)
7887 ((org-region-active-p)
7888 (buffer-substring (region-beginning) (region-end)))
7889 (t nil)))
7890 (when (or (null txt) (string-match "\\S-" txt))
7891 (setq cpltxt
7892 (concat cpltxt "::"
7893 (condition-case nil
7894 (org-make-org-heading-search-string txt)
7895 (error "")))
7896 desc (or (nth 4 (ignore-errors
7897 (org-heading-components))) "NONE"))))
7898 (if (string-match "::\\'" cpltxt)
7899 (setq cpltxt (substring cpltxt 0 -2)))
7900 (setq link (org-make-link cpltxt)))))
7902 ((buffer-file-name (buffer-base-buffer))
7903 ;; Just link to this file here.
7904 (setq cpltxt (concat "file:"
7905 (abbreviate-file-name
7906 (buffer-file-name (buffer-base-buffer)))))
7907 ;; Add a context string
7908 (when (org-xor org-context-in-file-links arg)
7909 (setq txt (if (org-region-active-p)
7910 (buffer-substring (region-beginning) (region-end))
7911 (buffer-substring (point-at-bol) (point-at-eol))))
7912 ;; Only use search option if there is some text.
7913 (when (string-match "\\S-" txt)
7914 (setq cpltxt
7915 (concat cpltxt "::" (org-make-org-heading-search-string txt))
7916 desc "NONE")))
7917 (setq link (org-make-link cpltxt)))
7919 ((interactive-p)
7920 (error "Cannot link to a buffer which is not visiting a file"))
7922 (t (setq link nil)))
7924 (if (consp link) (setq cpltxt (car link) link (cdr link)))
7925 (setq link (or link cpltxt)
7926 desc (or desc cpltxt))
7927 (if (equal desc "NONE") (setq desc nil))
7929 (if (and (or (interactive-p) executing-kbd-macro) link)
7930 (progn
7931 (setq org-stored-links
7932 (cons (list link desc) org-stored-links))
7933 (message "Stored: %s" (or desc link))
7934 (when custom-id
7935 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
7936 "::#" custom-id))
7937 (setq org-stored-links
7938 (cons (list link desc) org-stored-links))))
7939 (and link (org-make-link-string link desc)))))
7941 (defun org-store-link-props (&rest plist)
7942 "Store link properties, extract names and addresses."
7943 (let (x adr)
7944 (when (setq x (plist-get plist :from))
7945 (setq adr (mail-extract-address-components x))
7946 (setq plist (plist-put plist :fromname (car adr)))
7947 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
7948 (when (setq x (plist-get plist :to))
7949 (setq adr (mail-extract-address-components x))
7950 (setq plist (plist-put plist :toname (car adr)))
7951 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
7952 (let ((from (plist-get plist :from))
7953 (to (plist-get plist :to)))
7954 (when (and from to org-from-is-user-regexp)
7955 (setq plist
7956 (plist-put plist :fromto
7957 (if (string-match org-from-is-user-regexp from)
7958 (concat "to %t")
7959 (concat "from %f"))))))
7960 (setq org-store-link-plist plist))
7962 (defun org-add-link-props (&rest plist)
7963 "Add these properties to the link property list."
7964 (let (key value)
7965 (while plist
7966 (setq key (pop plist) value (pop plist))
7967 (setq org-store-link-plist
7968 (plist-put org-store-link-plist key value)))))
7970 (defun org-email-link-description (&optional fmt)
7971 "Return the description part of an email link.
7972 This takes information from `org-store-link-plist' and formats it
7973 according to FMT (default from `org-email-link-description-format')."
7974 (setq fmt (or fmt org-email-link-description-format))
7975 (let* ((p org-store-link-plist)
7976 (to (plist-get p :toaddress))
7977 (from (plist-get p :fromaddress))
7978 (table
7979 (list
7980 (cons "%c" (plist-get p :fromto))
7981 (cons "%F" (plist-get p :from))
7982 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
7983 (cons "%T" (plist-get p :to))
7984 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
7985 (cons "%s" (plist-get p :subject))
7986 (cons "%m" (plist-get p :message-id)))))
7987 (when (string-match "%c" fmt)
7988 ;; Check if the user wrote this message
7989 (if (and org-from-is-user-regexp from to
7990 (save-match-data (string-match org-from-is-user-regexp from)))
7991 (setq fmt (replace-match "to %t" t t fmt))
7992 (setq fmt (replace-match "from %f" t t fmt))))
7993 (org-replace-escapes fmt table)))
7995 (defun org-make-org-heading-search-string (&optional string heading)
7996 "Make search string for STRING or current headline."
7997 (interactive)
7998 (let ((s (or string (org-get-heading))))
7999 (unless (and string (not heading))
8000 ;; We are using a headline, clean up garbage in there.
8001 (if (string-match org-todo-regexp s)
8002 (setq s (replace-match "" t t s)))
8003 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
8004 (setq s (replace-match "" t t s)))
8005 (setq s (org-trim s))
8006 (if (string-match (concat "^\\(" org-quote-string "\\|"
8007 org-comment-string "\\)") s)
8008 (setq s (replace-match "" t t s)))
8009 (while (string-match org-ts-regexp s)
8010 (setq s (replace-match "" t t s))))
8011 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
8012 (setq s (replace-match " " t t s)))
8013 (or string (setq s (concat "*" s))) ; Add * for headlines
8014 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
8016 (defun org-make-link (&rest strings)
8017 "Concatenate STRINGS."
8018 (apply 'concat strings))
8020 (defun org-make-link-string (link &optional description)
8021 "Make a link with brackets, consisting of LINK and DESCRIPTION."
8022 (unless (string-match "\\S-" link)
8023 (error "Empty link"))
8024 (when (and description
8025 (stringp description)
8026 (not (string-match "\\S-" description)))
8027 (setq description nil))
8028 (when (stringp description)
8029 ;; Remove brackets from the description, they are fatal.
8030 (while (string-match "\\[" description)
8031 (setq description (replace-match "{" t t description)))
8032 (while (string-match "\\]" description)
8033 (setq description (replace-match "}" t t description))))
8034 (when (equal (org-link-escape link) description)
8035 ;; No description needed, it is identical
8036 (setq description nil))
8037 (when (and (not description)
8038 (not (equal link (org-link-escape link))))
8039 (setq description (org-extract-attributes link)))
8040 (concat "[[" (org-link-escape link) "]"
8041 (if description (concat "[" description "]") "")
8042 "]"))
8044 (defconst org-link-escape-chars
8045 '((?\ . "%20")
8046 (?\[ . "%5B")
8047 (?\] . "%5D")
8048 (?\340 . "%E0") ; `a
8049 (?\342 . "%E2") ; ^a
8050 (?\347 . "%E7") ; ,c
8051 (?\350 . "%E8") ; `e
8052 (?\351 . "%E9") ; 'e
8053 (?\352 . "%EA") ; ^e
8054 (?\356 . "%EE") ; ^i
8055 (?\364 . "%F4") ; ^o
8056 (?\371 . "%F9") ; `u
8057 (?\373 . "%FB") ; ^u
8058 (?\; . "%3B")
8059 ;; (?? . "%3F")
8060 (?= . "%3D")
8061 (?+ . "%2B")
8063 "Association list of escapes for some characters problematic in links.
8064 This is the list that is used for internal purposes.")
8066 (defvar org-url-encoding-use-url-hexify nil)
8068 (defconst org-link-escape-chars-browser
8069 '((?\ . "%20")) ; 32 for the SPC char
8070 "Association list of escapes for some characters problematic in links.
8071 This is the list that is used before handing over to the browser.")
8073 (defun org-link-escape (text &optional table)
8074 "Escape characters in TEXT that are problematic for links."
8075 (if (and org-url-encoding-use-url-hexify (not table))
8076 (url-hexify-string text)
8077 (setq table (or table org-link-escape-chars))
8078 (when text
8079 (let ((re (mapconcat (lambda (x) (regexp-quote
8080 (char-to-string (car x))))
8081 table "\\|")))
8082 (while (string-match re text)
8083 (setq text
8084 (replace-match
8085 (cdr (assoc (string-to-char (match-string 0 text))
8086 table))
8087 t t text)))
8088 text))))
8090 (defun org-link-unescape (text &optional table)
8091 "Reverse the action of `org-link-escape'."
8092 (if (and org-url-encoding-use-url-hexify (not table))
8093 (url-unhex-string text)
8094 (setq table (or table org-link-escape-chars))
8095 (when text
8096 (let ((case-fold-search t)
8097 (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
8098 table "\\|")))
8099 (while (string-match re text)
8100 (setq text
8101 (replace-match
8102 (char-to-string (car (rassoc (upcase (match-string 0 text))
8103 table)))
8104 t t text)))
8105 text))))
8107 (defun org-xor (a b)
8108 "Exclusive or."
8109 (if a (not b) b))
8111 (defun org-fixup-message-id-for-http (s)
8112 "Replace special characters in a message id, so it can be used in an http query."
8113 (while (string-match "<" s)
8114 (setq s (replace-match "%3C" t t s)))
8115 (while (string-match ">" s)
8116 (setq s (replace-match "%3E" t t s)))
8117 (while (string-match "@" s)
8118 (setq s (replace-match "%40" t t s)))
8121 ;;;###autoload
8122 (defun org-insert-link-global ()
8123 "Insert a link like Org-mode does.
8124 This command can be called in any mode to insert a link in Org-mode syntax."
8125 (interactive)
8126 (org-load-modules-maybe)
8127 (org-run-like-in-org-mode 'org-insert-link))
8129 (defun org-insert-link (&optional complete-file link-location)
8130 "Insert a link. At the prompt, enter the link.
8132 Completion can be used to insert any of the link protocol prefixes like
8133 http or ftp in use.
8135 The history can be used to select a link previously stored with
8136 `org-store-link'. When the empty string is entered (i.e. if you just
8137 press RET at the prompt), the link defaults to the most recently
8138 stored link. As SPC triggers completion in the minibuffer, you need to
8139 use M-SPC or C-q SPC to force the insertion of a space character.
8141 You will also be prompted for a description, and if one is given, it will
8142 be displayed in the buffer instead of the link.
8144 If there is already a link at point, this command will allow you to edit link
8145 and description parts.
8147 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
8148 be selected using completion. The path to the file will be relative to the
8149 current directory if the file is in the current directory or a subdirectory.
8150 Otherwise, the link will be the absolute path as completed in the minibuffer
8151 \(i.e. normally ~/path/to/file). You can configure this behavior using the
8152 option `org-link-file-path-type'.
8154 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
8155 the current directory or below.
8157 With three \\[universal-argument] prefixes, negate the meaning of
8158 `org-keep-stored-link-after-insertion'.
8160 If `org-make-link-description-function' is non-nil, this function will be
8161 called with the link target, and the result will be the default
8162 link description.
8164 If the LINK-LOCATION parameter is non-nil, this value will be
8165 used as the link location instead of reading one interactively."
8166 (interactive "P")
8167 (let* ((wcf (current-window-configuration))
8168 (region (if (org-region-active-p)
8169 (buffer-substring (region-beginning) (region-end))))
8170 (remove (and region (list (region-beginning) (region-end))))
8171 (desc region)
8172 tmphist ; byte-compile incorrectly complains about this
8173 (link link-location)
8174 entry file all-prefixes)
8175 (cond
8176 (link-location) ; specified by arg, just use it.
8177 ((org-in-regexp org-bracket-link-regexp 1)
8178 ;; We do have a link at point, and we are going to edit it.
8179 (setq remove (list (match-beginning 0) (match-end 0)))
8180 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
8181 (setq link (read-string "Link: "
8182 (org-link-unescape
8183 (org-match-string-no-properties 1)))))
8184 ((or (org-in-regexp org-angle-link-re)
8185 (org-in-regexp org-plain-link-re))
8186 ;; Convert to bracket link
8187 (setq remove (list (match-beginning 0) (match-end 0))
8188 link (read-string "Link: "
8189 (org-remove-angle-brackets (match-string 0)))))
8190 ((member complete-file '((4) (16)))
8191 ;; Completing read for file names.
8192 (setq link (org-file-complete-link complete-file)))
8194 ;; Read link, with completion for stored links.
8195 (with-output-to-temp-buffer "*Org Links*"
8196 (princ "Insert a link.
8197 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
8198 (when org-stored-links
8199 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
8200 (princ (mapconcat
8201 (lambda (x)
8202 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
8203 (reverse org-stored-links) "\n"))))
8204 (let ((cw (selected-window)))
8205 (select-window (get-buffer-window "*Org Links*"))
8206 (setq truncate-lines t)
8207 (unless (pos-visible-in-window-p (point-max))
8208 (org-fit-window-to-buffer))
8209 (and (window-live-p cw) (select-window cw)))
8210 ;; Fake a link history, containing the stored links.
8211 (setq tmphist (append (mapcar 'car org-stored-links)
8212 org-insert-link-history))
8213 (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
8214 (mapcar 'car org-link-abbrev-alist)
8215 org-link-types))
8216 (unwind-protect
8217 (progn
8218 (setq link
8219 (let ((org-completion-use-ido nil)
8220 (org-completion-use-iswitchb nil))
8221 (org-completing-read
8222 "Link: "
8223 (append
8224 (mapcar (lambda (x) (list (concat x ":")))
8225 all-prefixes)
8226 (mapcar 'car org-stored-links))
8227 nil nil nil
8228 'tmphist
8229 (car (car org-stored-links)))))
8230 (if (not (string-match "\\S-" link))
8231 (error "No link selected"))
8232 (if (or (member link all-prefixes)
8233 (and (equal ":" (substring link -1))
8234 (member (substring link 0 -1) all-prefixes)
8235 (setq link (substring link 0 -1))))
8236 (setq link (org-link-try-special-completion link))))
8237 (set-window-configuration wcf)
8238 (kill-buffer "*Org Links*"))
8239 (setq entry (assoc link org-stored-links))
8240 (or entry (push link org-insert-link-history))
8241 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
8242 (not org-keep-stored-link-after-insertion))
8243 (setq org-stored-links (delq (assoc link org-stored-links)
8244 org-stored-links)))
8245 (setq desc (or desc (nth 1 entry)))))
8247 (if (string-match org-plain-link-re link)
8248 ;; URL-like link, normalize the use of angular brackets.
8249 (setq link (org-make-link (org-remove-angle-brackets link))))
8251 ;; Check if we are linking to the current file with a search option
8252 ;; If yes, simplify the link by using only the search option.
8253 (when (and buffer-file-name
8254 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
8255 (let* ((path (match-string 1 link))
8256 (case-fold-search nil)
8257 (search (match-string 2 link)))
8258 (save-match-data
8259 (if (equal (file-truename buffer-file-name) (file-truename path))
8260 ;; We are linking to this same file, with a search option
8261 (setq link search)))))
8263 ;; Check if we can/should use a relative path. If yes, simplify the link
8264 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
8265 (let* ((type (match-string 1 link))
8266 (path (match-string 2 link))
8267 (origpath path)
8268 (case-fold-search nil))
8269 (cond
8270 ((or (eq org-link-file-path-type 'absolute)
8271 (equal complete-file '(16)))
8272 (setq path (abbreviate-file-name (expand-file-name path))))
8273 ((eq org-link-file-path-type 'noabbrev)
8274 (setq path (expand-file-name path)))
8275 ((eq org-link-file-path-type 'relative)
8276 (setq path (file-relative-name path)))
8278 (save-match-data
8279 (if (string-match (concat "^" (regexp-quote
8280 (file-name-as-directory
8281 (expand-file-name "."))))
8282 (expand-file-name path))
8283 ;; We are linking a file with relative path name.
8284 (setq path (substring (expand-file-name path)
8285 (match-end 0)))
8286 (setq path (abbreviate-file-name (expand-file-name path)))))))
8287 (setq link (concat type path))
8288 (if (equal desc origpath)
8289 (setq desc path))))
8291 (if org-make-link-description-function
8292 (setq desc (funcall org-make-link-description-function link desc)))
8294 (setq desc (read-string "Description: " desc))
8295 (unless (string-match "\\S-" desc) (setq desc nil))
8296 (if remove (apply 'delete-region remove))
8297 (insert (org-make-link-string link desc))))
8299 (defun org-link-try-special-completion (type)
8300 "If there is completion support for link type TYPE, offer it."
8301 (let ((fun (intern (concat "org-" type "-complete-link"))))
8302 (if (functionp fun)
8303 (funcall fun)
8304 (read-string "Link (no completion support): " (concat type ":")))))
8306 (defun org-file-complete-link (&optional arg)
8307 "Create a file link using completion."
8308 (let (file link)
8309 (setq file (read-file-name "File: "))
8310 (let ((pwd (file-name-as-directory (expand-file-name ".")))
8311 (pwd1 (file-name-as-directory (abbreviate-file-name
8312 (expand-file-name ".")))))
8313 (cond
8314 ((equal arg '(16))
8315 (setq link (org-make-link
8316 "file:"
8317 (abbreviate-file-name (expand-file-name file)))))
8318 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
8319 (setq link (org-make-link "file:" (match-string 1 file))))
8320 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
8321 (expand-file-name file))
8322 (setq link (org-make-link
8323 "file:" (match-string 1 (expand-file-name file)))))
8324 (t (setq link (org-make-link "file:" file)))))
8325 link))
8327 (defun org-completing-read (&rest args)
8328 "Completing-read with SPACE being a normal character."
8329 (let ((minibuffer-local-completion-map
8330 (copy-keymap minibuffer-local-completion-map)))
8331 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
8332 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
8333 (apply 'org-icompleting-read args)))
8335 (defun org-completing-read-no-i (&rest args)
8336 (let (org-completion-use-ido org-completion-use-iswitchb)
8337 (apply 'org-completing-read args)))
8339 (defun org-iswitchb-completing-read (prompt choices &rest args)
8340 "Use iswitch as a completing-read replacement to choose from choices.
8341 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
8342 from."
8343 (let* ((iswitchb-use-virtual-buffers nil)
8344 (iswitchb-make-buflist-hook
8345 (lambda ()
8346 (setq iswitchb-temp-buflist choices))))
8347 (iswitchb-read-buffer prompt)))
8349 (defun org-icompleting-read (&rest args)
8350 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
8351 (org-without-partial-completion
8352 (if (and org-completion-use-ido
8353 (fboundp 'ido-completing-read)
8354 (boundp 'ido-mode) ido-mode
8355 (listp (second args)))
8356 (let ((ido-enter-matching-directory nil))
8357 (apply 'ido-completing-read (concat (car args))
8358 (if (consp (car (nth 1 args)))
8359 (mapcar (lambda (x) (car x)) (nth 1 args))
8360 (nth 1 args))
8361 (cddr args)))
8362 (if (and org-completion-use-iswitchb
8363 (boundp 'iswitchb-mode) iswitchb-mode
8364 (listp (second args)))
8365 (apply 'org-iswitchb-completing-read (concat (car args))
8366 (if (consp (car (nth 1 args)))
8367 (mapcar (lambda (x) (car x)) (nth 1 args))
8368 (nth 1 args))
8369 (cddr args))
8370 (apply 'completing-read args)))))
8372 (defun org-extract-attributes (s)
8373 "Extract the attributes cookie from a string and set as text property."
8374 (let (a attr (start 0) key value)
8375 (save-match-data
8376 (when (string-match "{{\\([^}]+\\)}}$" s)
8377 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
8378 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
8379 (setq key (match-string 1 a) value (match-string 2 a)
8380 start (match-end 0)
8381 attr (plist-put attr (intern key) value))))
8382 (org-add-props s nil 'org-attr attr))
8385 (defun org-extract-attributes-from-string (tag)
8386 (let (key value attr)
8387 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
8388 (setq key (match-string 1 tag) value (match-string 2 tag)
8389 tag (replace-match "" t t tag)
8390 attr (plist-put attr (intern key) value)))
8391 (cons tag attr)))
8393 (defun org-attributes-to-string (plist)
8394 "Format a property list into an HTML attribute list."
8395 (let ((s "") key value)
8396 (while plist
8397 (setq key (pop plist) value (pop plist))
8398 (and value
8399 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
8402 ;;; Opening/following a link
8404 (defvar org-link-search-failed nil)
8406 (defvar org-open-link-functions nil
8407 "Hook for functions finding a plain text link.
8408 These functions must take a single argument, the link content.
8409 They will be called for links that look like [[link text][description]]
8410 when LINK TEXT does not have a protocol like \"http:\" and does not look
8411 like a filename (e.g. \"./blue.png\").
8413 These functions will be called *before* Org attempts to resolve the
8414 link by doing text searches in the current buffer - so if you want a
8415 link \"[[target]]\" to still find \"<<target>>\", your function should
8416 handle this as a special case.
8418 When the function does handle the link, it must return a non-nil value.
8419 If it decides that it is not responsible for this link, it must return
8420 nil to indicate that that Org-mode can continue with other options
8421 like exact and fuzzy text search.")
8423 (defun org-next-link ()
8424 "Move forward to the next link.
8425 If the link is in hidden text, expose it."
8426 (interactive)
8427 (when (and org-link-search-failed (eq this-command last-command))
8428 (goto-char (point-min))
8429 (message "Link search wrapped back to beginning of buffer"))
8430 (setq org-link-search-failed nil)
8431 (let* ((pos (point))
8432 (ct (org-context))
8433 (a (assoc :link ct)))
8434 (if a (goto-char (nth 2 a)))
8435 (if (re-search-forward org-any-link-re nil t)
8436 (progn
8437 (goto-char (match-beginning 0))
8438 (if (org-invisible-p) (org-show-context)))
8439 (goto-char pos)
8440 (setq org-link-search-failed t)
8441 (error "No further link found"))))
8443 (defun org-previous-link ()
8444 "Move backward to the previous link.
8445 If the link is in hidden text, expose it."
8446 (interactive)
8447 (when (and org-link-search-failed (eq this-command last-command))
8448 (goto-char (point-max))
8449 (message "Link search wrapped back to end of buffer"))
8450 (setq org-link-search-failed nil)
8451 (let* ((pos (point))
8452 (ct (org-context))
8453 (a (assoc :link ct)))
8454 (if a (goto-char (nth 1 a)))
8455 (if (re-search-backward org-any-link-re nil t)
8456 (progn
8457 (goto-char (match-beginning 0))
8458 (if (org-invisible-p) (org-show-context)))
8459 (goto-char pos)
8460 (setq org-link-search-failed t)
8461 (error "No further link found"))))
8463 (defun org-translate-link (s)
8464 "Translate a link string if a translation function has been defined."
8465 (if (and org-link-translation-function
8466 (fboundp org-link-translation-function)
8467 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
8468 (progn
8469 (setq s (funcall org-link-translation-function
8470 (match-string 1) (match-string 2)))
8471 (concat (car s) ":" (cdr s)))
8474 (defun org-translate-link-from-planner (type path)
8475 "Translate a link from Emacs Planner syntax so that Org can follow it.
8476 This is still an experimental function, your mileage may vary."
8477 (cond
8478 ((member type '("http" "https" "news" "ftp"))
8479 ;; standard Internet links are the same.
8480 nil)
8481 ((and (equal type "irc") (string-match "^//" path))
8482 ;; Planner has two / at the beginning of an irc link, we have 1.
8483 ;; We should have zero, actually....
8484 (setq path (substring path 1)))
8485 ((and (equal type "lisp") (string-match "^/" path))
8486 ;; Planner has a slash, we do not.
8487 (setq type "elisp" path (substring path 1)))
8488 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
8489 ;; A typical message link. Planner has the id after the final slash,
8490 ;; we separate it with a hash mark
8491 (setq path (concat (match-string 1 path) "#"
8492 (org-remove-angle-brackets (match-string 2 path)))))
8494 (cons type path))
8496 (defun org-find-file-at-mouse (ev)
8497 "Open file link or URL at mouse."
8498 (interactive "e")
8499 (mouse-set-point ev)
8500 (org-open-at-point 'in-emacs))
8502 (defun org-open-at-mouse (ev)
8503 "Open file link or URL at mouse."
8504 (interactive "e")
8505 (mouse-set-point ev)
8506 (if (eq major-mode 'org-agenda-mode)
8507 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
8508 (org-open-at-point))
8510 (defvar org-window-config-before-follow-link nil
8511 "The window configuration before following a link.
8512 This is saved in case the need arises to restore it.")
8514 (defvar org-open-link-marker (make-marker)
8515 "Marker pointing to the location where `org-open-at-point; was called.")
8517 ;;;###autoload
8518 (defun org-open-at-point-global ()
8519 "Follow a link like Org-mode does.
8520 This command can be called in any mode to follow a link that has
8521 Org-mode syntax."
8522 (interactive)
8523 (org-run-like-in-org-mode 'org-open-at-point))
8525 ;;;###autoload
8526 (defun org-open-link-from-string (s &optional arg reference-buffer)
8527 "Open a link in the string S, as if it was in Org-mode."
8528 (interactive "sLink: \nP")
8529 (let ((reference-buffer (or reference-buffer (current-buffer))))
8530 (with-temp-buffer
8531 (let ((org-inhibit-startup t))
8532 (org-mode)
8533 (insert s)
8534 (goto-char (point-min))
8535 (when reference-buffer
8536 (setq org-link-abbrev-alist-local
8537 (with-current-buffer reference-buffer
8538 org-link-abbrev-alist-local)))
8539 (org-open-at-point arg reference-buffer)))))
8541 (defun org-open-at-point (&optional in-emacs reference-buffer)
8542 "Open link at or after point.
8543 If there is no link at point, this function will search forward up to
8544 the end of the current line.
8545 Normally, files will be opened by an appropriate application. If the
8546 optional argument IN-EMACS is non-nil, Emacs will visit the file.
8547 With a double prefix argument, try to open outside of Emacs, in the
8548 application the system uses for this file type."
8549 (interactive "P")
8550 (org-load-modules-maybe)
8551 (move-marker org-open-link-marker (point))
8552 (setq org-window-config-before-follow-link (current-window-configuration))
8553 (org-remove-occur-highlights nil nil t)
8554 (cond
8555 ((and (org-on-heading-p)
8556 (not (org-in-regexp
8557 (concat org-plain-link-re "\\|"
8558 org-bracket-link-regexp "\\|"
8559 org-angle-link-re "\\|"
8560 "[ \t]:[^ \t\n]+:[ \t]*$")))
8561 (not (get-text-property (point) 'org-linked-text)))
8562 (or (org-offer-links-in-entry in-emacs)
8563 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
8564 ((org-at-timestamp-p t) (org-follow-timestamp-link))
8565 ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
8566 (org-footnote-action))
8568 (let (type path link line search (pos (point)))
8569 (catch 'match
8570 (save-excursion
8571 (skip-chars-forward "^]\n\r")
8572 (when (org-in-regexp org-bracket-link-regexp 1)
8573 (setq link (org-extract-attributes
8574 (org-link-unescape (org-match-string-no-properties 1))))
8575 (while (string-match " *\n *" link)
8576 (setq link (replace-match " " t t link)))
8577 (setq link (org-link-expand-abbrev link))
8578 (cond
8579 ((or (file-name-absolute-p link)
8580 (string-match "^\\.\\.?/" link))
8581 (setq type "file" path link))
8582 ((string-match org-link-re-with-space3 link)
8583 (setq type (match-string 1 link) path (match-string 2 link)))
8584 (t (setq type "thisfile" path link)))
8585 (throw 'match t)))
8587 (when (get-text-property (point) 'org-linked-text)
8588 (setq type "thisfile"
8589 pos (if (get-text-property (1+ (point)) 'org-linked-text)
8590 (1+ (point)) (point))
8591 path (buffer-substring
8592 (previous-single-property-change pos 'org-linked-text)
8593 (next-single-property-change pos 'org-linked-text)))
8594 (throw 'match t))
8596 (save-excursion
8597 (when (or (org-in-regexp org-angle-link-re)
8598 (org-in-regexp org-plain-link-re))
8599 (setq type (match-string 1) path (match-string 2))
8600 (throw 'match t)))
8601 (save-excursion
8602 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
8603 (setq type "tags"
8604 path (match-string 1))
8605 (while (string-match ":" path)
8606 (setq path (replace-match "+" t t path)))
8607 (throw 'match t)))
8608 (when (org-in-regexp "<\\([^><\n]+\\)>")
8609 (setq type "tree-match"
8610 path (match-string 1))
8611 (throw 'match t)))
8612 (unless path
8613 (error "No link found"))
8615 ;; switch back to reference buffer
8616 ;; needed when if called in a temporary buffer through
8617 ;; org-open-link-from-string
8618 (with-current-buffer (or reference-buffer (current-buffer))
8620 ;; Remove any trailing spaces in path
8621 (if (string-match " +\\'" path)
8622 (setq path (replace-match "" t t path)))
8623 (if (and org-link-translation-function
8624 (fboundp org-link-translation-function))
8625 ;; Check if we need to translate the link
8626 (let ((tmp (funcall org-link-translation-function type path)))
8627 (setq type (car tmp) path (cdr tmp))))
8629 (cond
8631 ((assoc type org-link-protocols)
8632 (funcall (nth 1 (assoc type org-link-protocols)) path))
8634 ((equal type "mailto")
8635 (let ((cmd (car org-link-mailto-program))
8636 (args (cdr org-link-mailto-program)) args1
8637 (address path) (subject "") a)
8638 (if (string-match "\\(.*\\)::\\(.*\\)" path)
8639 (setq address (match-string 1 path)
8640 subject (org-link-escape (match-string 2 path))))
8641 (while args
8642 (cond
8643 ((not (stringp (car args))) (push (pop args) args1))
8644 (t (setq a (pop args))
8645 (if (string-match "%a" a)
8646 (setq a (replace-match address t t a)))
8647 (if (string-match "%s" a)
8648 (setq a (replace-match subject t t a)))
8649 (push a args1))))
8650 (apply cmd (nreverse args1))))
8652 ((member type '("http" "https" "ftp" "news"))
8653 (browse-url (concat type ":" (org-link-escape
8654 path org-link-escape-chars-browser))))
8656 ((member type '("message"))
8657 (browse-url (concat type ":" path)))
8659 ((string= type "tags")
8660 (org-tags-view in-emacs path))
8662 ((string= type "tree-match")
8663 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
8665 ((string= type "file")
8666 (if (string-match "::\\([0-9]+\\)\\'" path)
8667 (setq line (string-to-number (match-string 1 path))
8668 path (substring path 0 (match-beginning 0)))
8669 (if (string-match "::\\(.+\\)\\'" path)
8670 (setq search (match-string 1 path)
8671 path (substring path 0 (match-beginning 0)))))
8672 (if (string-match "[*?{]" (file-name-nondirectory path))
8673 (dired path)
8674 (org-open-file path in-emacs line search)))
8676 ((string= type "news")
8677 (require 'org-gnus)
8678 (org-gnus-follow-link path))
8680 ((string= type "shell")
8681 (let ((cmd path))
8682 (if (or (not org-confirm-shell-link-function)
8683 (funcall org-confirm-shell-link-function
8684 (format "Execute \"%s\" in shell? "
8685 (org-add-props cmd nil
8686 'face 'org-warning))))
8687 (progn
8688 (message "Executing %s" cmd)
8689 (shell-command cmd))
8690 (error "Abort"))))
8692 ((string= type "elisp")
8693 (let ((cmd path))
8694 (if (or (not org-confirm-elisp-link-function)
8695 (funcall org-confirm-elisp-link-function
8696 (format "Execute \"%s\" as elisp? "
8697 (org-add-props cmd nil
8698 'face 'org-warning))))
8699 (message "%s => %s" cmd
8700 (if (equal (string-to-char cmd) ?\()
8701 (eval (read cmd))
8702 (call-interactively (read cmd))))
8703 (error "Abort"))))
8705 ((and (string= type "thisfile")
8706 (run-hook-with-args-until-success
8707 'org-open-link-functions path)))
8709 ((string= type "thisfile")
8710 (if in-emacs
8711 (switch-to-buffer-other-window
8712 (org-get-buffer-for-internal-link (current-buffer)))
8713 (org-mark-ring-push))
8714 (let ((cmd `(org-link-search
8715 ,path
8716 ,(cond ((equal in-emacs '(4)) 'occur)
8717 ((equal in-emacs '(16)) 'org-occur)
8718 (t nil))
8719 ,pos)))
8720 (condition-case nil (eval cmd)
8721 (error (progn (widen) (eval cmd))))))
8724 (browse-url-at-point)))))))
8725 (move-marker org-open-link-marker nil)
8726 (run-hook-with-args 'org-follow-link-hook))
8728 (defun org-offer-links-in-entry (&optional nth zero)
8729 "Offer links in the current entry and follow the selected link.
8730 If there is only one link, follow it immediately as well.
8731 If NTH is an integer, immediately pick the NTH link found.
8732 If ZERO is a string, check also this string for a link, and if
8733 there is one, offer it as link number zero."
8734 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
8735 "\\(" org-angle-link-re "\\)\\|"
8736 "\\(" org-plain-link-re "\\)"))
8737 (cnt ?0)
8738 (in-emacs (if (integerp nth) nil nth))
8739 have-zero end links link c)
8740 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
8741 (push (match-string 0 zero) links)
8742 (setq cnt (1- cnt) have-zero t))
8743 (save-excursion
8744 (org-back-to-heading t)
8745 (setq end (save-excursion (outline-next-heading) (point)))
8746 (while (re-search-forward re end t)
8747 (push (match-string 0) links))
8748 (setq links (org-uniquify (reverse links))))
8750 (cond
8751 ((null links)
8752 (message "No links"))
8753 ((equal (length links) 1)
8754 (setq link (list (car links))))
8755 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
8756 (setq link (nth (if have-zero nth (1- nth)) links)))
8757 (t ; we have to select a link
8758 (save-excursion
8759 (save-window-excursion
8760 (delete-other-windows)
8761 (with-output-to-temp-buffer "*Select Link*"
8762 (mapc (lambda (l)
8763 (if (not (string-match org-bracket-link-regexp l))
8764 (princ (format "[%c] %s\n" (incf cnt)
8765 (org-remove-angle-brackets l)))
8766 (if (match-end 3)
8767 (princ (format "[%c] %s (%s)\n" (incf cnt)
8768 (match-string 3 l) (match-string 1 l)))
8769 (princ (format "[%c] %s\n" (incf cnt)
8770 (match-string 1 l))))))
8771 links))
8772 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
8773 (message "Select link to open, RET to open all:")
8774 (setq c (read-char-exclusive))
8775 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
8776 (when (equal c ?q) (error "Abort"))
8777 (if (equal c ?\C-m)
8778 (setq link links)
8779 (setq nth (- c ?0))
8780 (if have-zero (setq nth (1+ nth)))
8781 (unless (and (integerp nth) (>= (length links) nth))
8782 (error "Invalid link selection"))
8783 (setq link (list (nth (1- nth) links))))))
8784 (if link
8785 (let ((buf (current-buffer)))
8786 (dolist (l link)
8787 (org-open-link-from-string l in-emacs buf))
8789 nil)))
8791 ;; Add special file links that specify the way of opening
8793 (org-add-link-type "file+sys" 'org-open-file-with-system)
8794 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
8795 (defun org-open-file-with-system (path)
8796 "Open file at PATH using the system way of opeing it."
8797 (org-open-file path 'system))
8798 (defun org-open-file-with-emacs (path)
8799 "Open file at PATH in emacs."
8800 (org-open-file path 'emacs))
8801 (defun org-remove-file-link-modifiers ()
8802 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
8803 (goto-char (point-min))
8804 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
8805 (org-if-unprotected
8806 (replace-match "file:" t t))))
8807 (eval-after-load "org-exp"
8808 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
8809 'org-remove-file-link-modifiers))
8811 ;;;; Time estimates
8813 (defun org-get-effort (&optional pom)
8814 "Get the effort estimate for the current entry."
8815 (org-entry-get pom org-effort-property))
8817 ;;; File search
8819 (defvar org-create-file-search-functions nil
8820 "List of functions to construct the right search string for a file link.
8821 These functions are called in turn with point at the location to
8822 which the link should point.
8824 A function in the hook should first test if it would like to
8825 handle this file type, for example by checking the major-mode or
8826 the file extension. If it decides not to handle this file, it
8827 should just return nil to give other functions a chance. If it
8828 does handle the file, it must return the search string to be used
8829 when following the link. The search string will be part of the
8830 file link, given after a double colon, and `org-open-at-point'
8831 will automatically search for it. If special measures must be
8832 taken to make the search successful, another function should be
8833 added to the companion hook `org-execute-file-search-functions',
8834 which see.
8836 A function in this hook may also use `setq' to set the variable
8837 `description' to provide a suggestion for the descriptive text to
8838 be used for this link when it gets inserted into an Org-mode
8839 buffer with \\[org-insert-link].")
8841 (defvar org-execute-file-search-functions nil
8842 "List of functions to execute a file search triggered by a link.
8844 Functions added to this hook must accept a single argument, the
8845 search string that was part of the file link, the part after the
8846 double colon. The function must first check if it would like to
8847 handle this search, for example by checking the major-mode or the
8848 file extension. If it decides not to handle this search, it
8849 should just return nil to give other functions a chance. If it
8850 does handle the search, it must return a non-nil value to keep
8851 other functions from trying.
8853 Each function can access the current prefix argument through the
8854 variable `current-prefix-argument'. Note that a single prefix is
8855 used to force opening a link in Emacs, so it may be good to only
8856 use a numeric or double prefix to guide the search function.
8858 In case this is needed, a function in this hook can also restore
8859 the window configuration before `org-open-at-point' was called using:
8861 (set-window-configuration org-window-config-before-follow-link)")
8863 (defun org-link-search (s &optional type avoid-pos)
8864 "Search for a link search option.
8865 If S is surrounded by forward slashes, it is interpreted as a
8866 regular expression. In org-mode files, this will create an `org-occur'
8867 sparse tree. In ordinary files, `occur' will be used to list matches.
8868 If the current buffer is in `dired-mode', grep will be used to search
8869 in all files. If AVOID-POS is given, ignore matches near that position."
8870 (let ((case-fold-search t)
8871 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
8872 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
8873 (append '(("") (" ") ("\t") ("\n"))
8874 org-emphasis-alist)
8875 "\\|") "\\)"))
8876 (pos (point))
8877 (pre nil) (post nil)
8878 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
8879 (cond
8880 ;; First check if there are any special
8881 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
8882 ;; Now try the builtin stuff
8883 ((and (equal (string-to-char s0) ?#)
8884 (> (length s0) 1)
8885 (save-excursion
8886 (goto-char (point-min))
8887 (and
8888 (re-search-forward
8889 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
8890 (setq type 'dedicated
8891 pos (match-beginning 0))))
8892 ;; There is an exact target for this
8893 (goto-char pos)
8894 (org-back-to-heading t)))
8895 ((save-excursion
8896 (goto-char (point-min))
8897 (and
8898 (re-search-forward
8899 (concat "<<" (regexp-quote s0) ">>") nil t)
8900 (setq type 'dedicated
8901 pos (match-beginning 0))))
8902 ;; There is an exact target for this
8903 (goto-char pos))
8904 ((and (string-match "^(\\(.*\\))$" s0)
8905 (save-excursion
8906 (goto-char (point-min))
8907 (and
8908 (re-search-forward
8909 (concat "[^[]" (regexp-quote
8910 (format org-coderef-label-format
8911 (match-string 1 s0))))
8912 nil t)
8913 (setq type 'dedicated
8914 pos (1+ (match-beginning 0))))))
8915 ;; There is a coderef target for this
8916 (goto-char pos))
8917 ((string-match "^/\\(.*\\)/$" s)
8918 ;; A regular expression
8919 (cond
8920 ((org-mode-p)
8921 (org-occur (match-string 1 s)))
8922 ;;((eq major-mode 'dired-mode)
8923 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
8924 (t (org-do-occur (match-string 1 s)))))
8926 ;; A normal search strings
8927 (when (equal (string-to-char s) ?*)
8928 ;; Anchor on headlines, post may include tags.
8929 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
8930 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
8931 s (substring s 1)))
8932 (remove-text-properties
8933 0 (length s)
8934 '(face nil mouse-face nil keymap nil fontified nil) s)
8935 ;; Make a series of regular expressions to find a match
8936 (setq words (org-split-string s "[ \n\r\t]+")
8938 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
8939 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
8940 "\\)" markers)
8941 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
8942 re2a (concat "[ \t\r\n]" re2a_)
8943 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
8944 re4 (concat "[^a-zA-Z_]" re4_)
8946 re1 (concat pre re2 post)
8947 re3 (concat pre (if pre re4_ re4) post)
8948 re5 (concat pre ".*" re4)
8949 re2 (concat pre re2)
8950 re2a (concat pre (if pre re2a_ re2a))
8951 re4 (concat pre (if pre re4_ re4))
8952 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
8953 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
8954 re5 "\\)"
8956 (cond
8957 ((eq type 'org-occur) (org-occur reall))
8958 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
8959 (t (goto-char (point-min))
8960 (setq type 'fuzzy)
8961 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
8962 (org-search-not-self 1 re1 nil t)
8963 (org-search-not-self 1 re2 nil t)
8964 (org-search-not-self 1 re2a nil t)
8965 (org-search-not-self 1 re3 nil t)
8966 (org-search-not-self 1 re4 nil t)
8967 (org-search-not-self 1 re5 nil t)
8969 (goto-char (match-beginning 1))
8970 (goto-char pos)
8971 (error "No match")))))
8973 ;; Normal string-search
8974 (goto-char (point-min))
8975 (if (search-forward s nil t)
8976 (goto-char (match-beginning 0))
8977 (error "No match"))))
8978 (and (org-mode-p) (org-show-context 'link-search))
8979 type))
8981 (defun org-search-not-self (group &rest args)
8982 "Execute `re-search-forward', but only accept matches that do not
8983 enclose the position of `org-open-link-marker'."
8984 (let ((m org-open-link-marker))
8985 (catch 'exit
8986 (while (apply 're-search-forward args)
8987 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
8988 (goto-char (match-end group))
8989 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
8990 (> (match-beginning 0) (marker-position m))
8991 (< (match-end 0) (marker-position m)))
8992 (save-match-data
8993 (or (not (org-in-regexp
8994 org-bracket-link-analytic-regexp 1))
8995 (not (match-end 4)) ; no description
8996 (and (<= (match-beginning 4) (point))
8997 (>= (match-end 4) (point))))))
8998 (throw 'exit (point))))))))
9000 (defun org-get-buffer-for-internal-link (buffer)
9001 "Return a buffer to be used for displaying the link target of internal links."
9002 (cond
9003 ((not org-display-internal-link-with-indirect-buffer)
9004 buffer)
9005 ((string-match "(Clone)$" (buffer-name buffer))
9006 (message "Buffer is already a clone, not making another one")
9007 ;; we also do not modify visibility in this case
9008 buffer)
9009 (t ; make a new indirect buffer for displaying the link
9010 (let* ((bn (buffer-name buffer))
9011 (ibn (concat bn "(Clone)"))
9012 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
9013 (with-current-buffer ib (org-overview))
9014 ib))))
9016 (defun org-do-occur (regexp &optional cleanup)
9017 "Call the Emacs command `occur'.
9018 If CLEANUP is non-nil, remove the printout of the regular expression
9019 in the *Occur* buffer. This is useful if the regex is long and not useful
9020 to read."
9021 (occur regexp)
9022 (when cleanup
9023 (let ((cwin (selected-window)) win beg end)
9024 (when (setq win (get-buffer-window "*Occur*"))
9025 (select-window win))
9026 (goto-char (point-min))
9027 (when (re-search-forward "match[a-z]+" nil t)
9028 (setq beg (match-end 0))
9029 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
9030 (setq end (1- (match-beginning 0)))))
9031 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
9032 (goto-char (point-min))
9033 (select-window cwin))))
9035 ;;; The mark ring for links jumps
9037 (defvar org-mark-ring nil
9038 "Mark ring for positions before jumps in Org-mode.")
9039 (defvar org-mark-ring-last-goto nil
9040 "Last position in the mark ring used to go back.")
9041 ;; Fill and close the ring
9042 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
9043 (loop for i from 1 to org-mark-ring-length do
9044 (push (make-marker) org-mark-ring))
9045 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
9046 org-mark-ring)
9048 (defun org-mark-ring-push (&optional pos buffer)
9049 "Put the current position or POS into the mark ring and rotate it."
9050 (interactive)
9051 (setq pos (or pos (point)))
9052 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
9053 (move-marker (car org-mark-ring)
9054 (or pos (point))
9055 (or buffer (current-buffer)))
9056 (message "%s"
9057 (substitute-command-keys
9058 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
9060 (defun org-mark-ring-goto (&optional n)
9061 "Jump to the previous position in the mark ring.
9062 With prefix arg N, jump back that many stored positions. When
9063 called several times in succession, walk through the entire ring.
9064 Org-mode commands jumping to a different position in the current file,
9065 or to another Org-mode file, automatically push the old position
9066 onto the ring."
9067 (interactive "p")
9068 (let (p m)
9069 (if (eq last-command this-command)
9070 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
9071 (setq p org-mark-ring))
9072 (setq org-mark-ring-last-goto p)
9073 (setq m (car p))
9074 (switch-to-buffer (marker-buffer m))
9075 (goto-char m)
9076 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
9078 (defun org-remove-angle-brackets (s)
9079 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
9080 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
9082 (defun org-add-angle-brackets (s)
9083 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
9084 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
9086 (defun org-remove-double-quotes (s)
9087 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
9088 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
9091 ;;; Following specific links
9093 (defun org-follow-timestamp-link ()
9094 (cond
9095 ((org-at-date-range-p t)
9096 (let ((org-agenda-start-on-weekday)
9097 (t1 (match-string 1))
9098 (t2 (match-string 2)))
9099 (setq t1 (time-to-days (org-time-string-to-time t1))
9100 t2 (time-to-days (org-time-string-to-time t2)))
9101 (org-agenda-list nil t1 (1+ (- t2 t1)))))
9102 ((org-at-timestamp-p t)
9103 (org-agenda-list nil (time-to-days (org-time-string-to-time
9104 (substring (match-string 1) 0 10)))
9106 (t (error "This should not happen"))))
9109 ;;; Following file links
9110 (defvar org-wait nil)
9111 (defun org-open-file (path &optional in-emacs line search)
9112 "Open the file at PATH.
9113 First, this expands any special file name abbreviations. Then the
9114 configuration variable `org-file-apps' is checked if it contains an
9115 entry for this file type, and if yes, the corresponding command is launched.
9117 If no application is found, Emacs simply visits the file.
9119 With optional prefix argument IN-EMACS, Emacs will visit the file.
9120 With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
9121 and to use an external application to visit the file.
9123 Optional LINE specifies a line to go to, optional SEARCH a string to
9124 search for. If LINE or SEARCH is given, but IN-EMACS is nil, it will
9125 be assumed that org-open-file was called to open a file: link, and the
9126 original link to match against org-file-apps will be reconstructed
9127 from PATH and whichever of LINE or SEARCH is given.
9129 If the file does not exist, an error is thrown."
9130 (let* ((file (if (equal path "")
9131 buffer-file-name
9132 (substitute-in-file-name (expand-file-name path))))
9133 (apps (append org-file-apps (org-default-apps)))
9134 (remp (and (assq 'remote apps) (org-file-remote-p file)))
9135 (dirp (if remp nil (file-directory-p file)))
9136 (file (if (and dirp org-open-directory-means-index-dot-org)
9137 (concat (file-name-as-directory file) "index.org")
9138 file))
9139 (a-m-a-p (assq 'auto-mode apps))
9140 (dfile (downcase file))
9141 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
9142 (link (cond ((and (eq line nil)
9143 (eq search nil))
9144 file)
9145 (line
9146 (concat file "::" (number-to-string line)))
9147 (search
9148 (concat file "::" search))))
9149 (dlink (downcase link))
9150 (old-buffer (current-buffer))
9151 (old-pos (point))
9152 (old-mode major-mode)
9153 ext cmd link-match-data)
9154 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
9155 (setq ext (match-string 1 dfile))
9156 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
9157 (setq ext (match-string 1 dfile))))
9158 (cond
9159 ((member in-emacs '((16) system))
9160 (setq cmd (cdr (assoc 'system apps))))
9161 (in-emacs (setq cmd 'emacs))
9163 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
9164 (and dirp (cdr (assoc 'directory apps)))
9165 ;; if we find a match in org-file-apps, store the match
9166 ;; data for later
9167 (let ((match (assoc-default dlink (org-apps-regexp-alist
9168 apps a-m-a-p)
9169 'string-match)))
9170 (if match
9171 (progn (setq link-match-data (match-data))
9172 match)
9173 nil))
9174 (cdr (assoc ext apps))
9175 (cdr (assoc t apps))))))
9176 (when (eq cmd 'system)
9177 (setq cmd (cdr (assoc 'system apps))))
9178 (when (eq cmd 'default)
9179 (setq cmd (cdr (assoc t apps))))
9180 (when (eq cmd 'mailcap)
9181 (require 'mailcap)
9182 (mailcap-parse-mailcaps)
9183 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
9184 (command (mailcap-mime-info mime-type)))
9185 (if (stringp command)
9186 (setq cmd command)
9187 (setq cmd 'emacs))))
9188 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
9189 (not (file-exists-p file))
9190 (not org-open-non-existing-files))
9191 (error "No such file: %s" file))
9192 (cond
9193 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
9194 ;; Remove quotes around the file name - we'll use shell-quote-argument.
9195 (while (string-match "['\"]%s['\"]" cmd)
9196 (setq cmd (replace-match "%s" t t cmd)))
9197 (while (string-match "%s" cmd)
9198 (setq cmd (replace-match
9199 (save-match-data
9200 (shell-quote-argument
9201 (convert-standard-filename file)))
9202 t t cmd)))
9203 ;; Replace "%1", "%2" etc. in command with group matches from regex
9204 (save-match-data
9205 (let ((match-index 1)
9206 (number-of-groups (- (/ (length link-match-data) 2) 1)))
9207 (set-match-data link-match-data)
9208 (while (<= match-index number-of-groups)
9209 (let ((regex (concat "%" (number-to-string match-index)))
9210 (replace-with (match-string match-index dlink)))
9211 (while (string-match regex cmd)
9212 (setq cmd (replace-match replace-with t t cmd))))
9213 (setq match-index (+ match-index 1)))))
9215 (save-window-excursion
9216 (start-process-shell-command cmd nil cmd)
9217 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
9219 ((or (stringp cmd)
9220 (eq cmd 'emacs))
9221 (funcall (cdr (assq 'file org-link-frame-setup)) file)
9222 (widen)
9223 (if line (org-goto-line line)
9224 (if search (org-link-search search))))
9225 ((consp cmd)
9226 (let ((file (convert-standard-filename file)))
9227 (save-match-data
9228 (set-match-data link-match-data)
9229 (eval cmd))))
9230 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
9231 (and (org-mode-p) (eq old-mode 'org-mode)
9232 (or (not (equal old-buffer (current-buffer)))
9233 (not (equal old-pos (point))))
9234 (org-mark-ring-push old-pos old-buffer))))
9236 (defun org-default-apps ()
9237 "Return the default applications for this operating system."
9238 (cond
9239 ((eq system-type 'darwin)
9240 org-file-apps-defaults-macosx)
9241 ((eq system-type 'windows-nt)
9242 org-file-apps-defaults-windowsnt)
9243 (t org-file-apps-defaults-gnu)))
9245 (defun org-apps-regexp-alist (list &optional add-auto-mode)
9246 "Convert extensions to regular expressions in the cars of LIST.
9247 Also, weed out any non-string entries, because the return value is used
9248 only for regexp matching.
9249 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
9250 point to the symbol `emacs', indicating that the file should
9251 be opened in Emacs."
9252 (append
9253 (delq nil
9254 (mapcar (lambda (x)
9255 (if (not (stringp (car x)))
9257 (if (string-match "\\W" (car x))
9259 (cons (concat "\\." (car x) "\\(::.*\\)?\\'")
9260 (cdr x)))))
9261 list))
9262 (if add-auto-mode
9263 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
9265 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
9266 (defun org-file-remote-p (file)
9267 "Test whether FILE specifies a location on a remote system.
9268 Return non-nil if the location is indeed remote.
9270 For example, the filename \"/user@host:/foo\" specifies a location
9271 on the system \"/user@host:\"."
9272 (cond ((fboundp 'file-remote-p)
9273 (file-remote-p file))
9274 ((fboundp 'tramp-handle-file-remote-p)
9275 (tramp-handle-file-remote-p file))
9276 ((and (boundp 'ange-ftp-name-format)
9277 (string-match (car ange-ftp-name-format) file))
9279 (t nil)))
9282 ;;;; Refiling
9284 (defun org-get-org-file ()
9285 "Read a filename, with default directory `org-directory'."
9286 (let ((default (or org-default-notes-file remember-data-file)))
9287 (read-file-name (format "File name [%s]: " default)
9288 (file-name-as-directory org-directory)
9289 default)))
9291 (defun org-notes-order-reversed-p ()
9292 "Check if the current file should receive notes in reversed order."
9293 (cond
9294 ((not org-reverse-note-order) nil)
9295 ((eq t org-reverse-note-order) t)
9296 ((not (listp org-reverse-note-order)) nil)
9297 (t (catch 'exit
9298 (let ((all org-reverse-note-order)
9299 entry)
9300 (while (setq entry (pop all))
9301 (if (string-match (car entry) buffer-file-name)
9302 (throw 'exit (cdr entry))))
9303 nil)))))
9305 (defvar org-refile-target-table nil
9306 "The list of refile targets, created by `org-refile'.")
9308 (defvar org-agenda-new-buffers nil
9309 "Buffers created to visit agenda files.")
9311 (defun org-get-refile-targets (&optional default-buffer)
9312 "Produce a table with refile targets."
9313 (let ((case-fold-search nil)
9314 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
9315 (entries (or org-refile-targets '((nil . (:level . 1)))))
9316 targets txt re files f desc descre fast-path-p level pos0)
9317 (message "Getting targets...")
9318 (with-current-buffer (or default-buffer (current-buffer))
9319 (while (setq entry (pop entries))
9320 (setq files (car entry) desc (cdr entry))
9321 (setq fast-path-p nil)
9322 (cond
9323 ((null files) (setq files (list (current-buffer))))
9324 ((eq files 'org-agenda-files)
9325 (setq files (org-agenda-files 'unrestricted)))
9326 ((and (symbolp files) (fboundp files))
9327 (setq files (funcall files)))
9328 ((and (symbolp files) (boundp files))
9329 (setq files (symbol-value files))))
9330 (if (stringp files) (setq files (list files)))
9331 (cond
9332 ((eq (car desc) :tag)
9333 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
9334 ((eq (car desc) :todo)
9335 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
9336 ((eq (car desc) :regexp)
9337 (setq descre (cdr desc)))
9338 ((eq (car desc) :level)
9339 (setq descre (concat "^\\*\\{" (number-to-string
9340 (if org-odd-levels-only
9341 (1- (* 2 (cdr desc)))
9342 (cdr desc)))
9343 "\\}[ \t]")))
9344 ((eq (car desc) :maxlevel)
9345 (setq fast-path-p t)
9346 (setq descre (concat "^\\*\\{1," (number-to-string
9347 (if org-odd-levels-only
9348 (1- (* 2 (cdr desc)))
9349 (cdr desc)))
9350 "\\}[ \t]")))
9351 (t (error "Bad refiling target description %s" desc)))
9352 (while (setq f (pop files))
9353 (with-current-buffer
9354 (if (bufferp f) f (org-get-agenda-file-buffer f))
9355 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
9356 (setq f (and f (expand-file-name f)))
9357 (if (eq org-refile-use-outline-path 'file)
9358 (push (list (file-name-nondirectory f) f nil nil) targets))
9359 (save-excursion
9360 (save-restriction
9361 (widen)
9362 (goto-char (point-min))
9363 (while (re-search-forward descre nil t)
9364 (goto-char (setq pos0 (point-at-bol)))
9365 (catch 'next
9366 (when org-refile-target-verify-function
9367 (save-match-data
9368 (or (funcall org-refile-target-verify-function)
9369 (throw 'next t))))
9370 (when (looking-at org-complex-heading-regexp)
9371 (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
9372 txt (org-link-display-format (match-string 4))
9373 re (concat "^" (regexp-quote
9374 (buffer-substring (match-beginning 1)
9375 (match-end 4)))))
9376 (if (match-end 5) (setq re (concat re "[ \t]+"
9377 (regexp-quote
9378 (match-string 5)))))
9379 (setq re (concat re "[ \t]*$"))
9380 (when org-refile-use-outline-path
9381 (setq txt (mapconcat 'org-protect-slash
9382 (append
9383 (if (eq org-refile-use-outline-path 'file)
9384 (list (file-name-nondirectory
9385 (buffer-file-name (buffer-base-buffer))))
9386 (if (eq org-refile-use-outline-path 'full-file-path)
9387 (list (buffer-file-name (buffer-base-buffer)))))
9388 (org-get-outline-path fast-path-p level txt)
9389 (list txt))
9390 "/")))
9391 (push (list txt f re (point)) targets)))
9392 (when (= (point) pos0)
9393 ;; verification function has not moved point
9394 (goto-char (point-at-eol))))))))))
9395 (message "Getting targets...done")
9396 (nreverse targets)))
9398 (defun org-protect-slash (s)
9399 (while (string-match "/" s)
9400 (setq s (replace-match "\\" t t s)))
9403 (defvar org-olpa (make-vector 20 nil))
9405 (defun org-get-outline-path (&optional fastp level heading)
9406 "Return the outline path to the current entry, as a list.
9407 The parameters FASTP, LEVEL, and HEADING are for use be a scanner
9408 routine which makes outline path derivations for an entire file,
9409 avoiding backtracing."
9410 (if fastp
9411 (progn
9412 (if (> level 19)
9413 (error "Outline path failure, more than 19 levels."))
9414 (loop for i from level upto 19 do
9415 (aset org-olpa i nil))
9416 (prog1
9417 (delq nil (append org-olpa nil))
9418 (aset org-olpa level heading)))
9419 (let (rtn case-fold-search)
9420 (save-excursion
9421 (save-restriction
9422 (widen)
9423 (while (org-up-heading-safe)
9424 (when (looking-at org-complex-heading-regexp)
9425 (push (org-match-string-no-properties 4) rtn)))
9426 rtn)))))
9428 (defun org-format-outline-path (path &optional width prefix)
9429 "Format the outlie path PATH for display.
9430 Width is the maximum number of characters that is available.
9431 Prefix is a prefix to be included in the returned string,
9432 such as the file name."
9433 (setq width (or width 79))
9434 (if prefix (setq width (- width (length prefix))))
9435 (if (not path)
9436 (or prefix "")
9437 (let* ((nsteps (length path))
9438 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
9439 (maxwidth (if (<= total-width width)
9440 10000 ;; everything fits
9441 ;; we need to shorten the level headings
9442 (/ (- width nsteps) nsteps)))
9443 (org-odd-levels-only nil)
9444 (n 0)
9445 (total (1+ (length prefix))))
9446 (setq maxwidth (max maxwidth 10))
9447 (concat prefix
9448 (mapconcat
9449 (lambda (h)
9450 (setq n (1+ n))
9451 (if (and (= n nsteps) (< maxwidth 10000))
9452 (setq maxwidth (- total-width total)))
9453 (if (< (length h) maxwidth)
9454 (progn (setq total (+ total (length h) 1)) h)
9455 (setq h (substring h 0 (- maxwidth 2))
9456 total (+ total maxwidth 1))
9457 (if (string-match "[ \t]+\\'" h)
9458 (setq h (substring h 0 (match-beginning 0))))
9459 (setq h (concat h "..")))
9460 (org-add-props h nil 'face
9461 (nth (% (1- n) org-n-level-faces)
9462 org-level-faces))
9464 path "/")))))
9466 (defun org-display-outline-path (&optional file current)
9467 "Display the current outline path in the echo area."
9468 (interactive "P")
9469 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
9470 (case-fold-search nil)
9471 (path (and (org-mode-p) (org-get-outline-path))))
9472 (if current (setq path (append path
9473 (save-excursion
9474 (org-back-to-heading t)
9475 (if (looking-at org-complex-heading-regexp)
9476 (list (match-string 4)))))))
9477 (message "%s"
9478 (org-format-outline-path
9479 path
9480 (1- (frame-width))
9481 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
9483 (defvar org-refile-history nil
9484 "History for refiling operations.")
9486 (defvar org-after-refile-insert-hook nil
9487 "Hook run after `org-refile' has inserted its stuff at the new location.
9488 Note that this is still *before* the stuff will be removed from
9489 the *old* location.")
9491 (defun org-refile (&optional goto default-buffer rfloc)
9492 "Move the entry at point to another heading.
9493 The list of target headings is compiled using the information in
9494 `org-refile-targets', which see. This list is created before each use
9495 and will therefore always be up-to-date.
9497 At the target location, the entry is filed as a subitem of the target heading.
9498 Depending on `org-reverse-note-order', the new subitem will either be the
9499 first or the last subitem.
9501 If there is an active region, all entries in that region will be moved.
9502 However, the region must fulfil the requirement that the first heading
9503 is the first one sets the top-level of the moved text - at most siblings
9504 below it are allowed.
9506 With prefix arg GOTO, the command will only visit the target location,
9507 not actually move anything.
9508 With a double prefix `C-u C-u', go to the location where the last refiling
9509 operation has put the subtree.
9510 With a prefix argument of `2', refile to the running clock.
9512 RFLOC can be a refile location obtained in a different way.
9514 See also `org-refile-use-outline-path' and `org-completion-use-ido'"
9515 (interactive "P")
9516 (let* ((cbuf (current-buffer))
9517 (regionp (org-region-active-p))
9518 (region-start (and regionp (region-beginning)))
9519 (region-end (and regionp (region-end)))
9520 (region-length (and regionp (- region-end region-start)))
9521 (filename (buffer-file-name (buffer-base-buffer cbuf)))
9522 pos it nbuf file re level reversed)
9523 (setq last-command nil)
9524 (when regionp
9525 (goto-char region-start)
9526 (or (bolp) (goto-char (point-at-bol)))
9527 (setq region-start (point))
9528 (unless (org-kill-is-subtree-p
9529 (buffer-substring region-start region-end))
9530 (error "The region is not a (sequence of) subtree(s)")))
9531 (if (equal goto '(16))
9532 (org-refile-goto-last-stored)
9533 (when (or
9534 (and (equal goto 2)
9535 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
9536 (prog1
9537 (setq it (list (or org-clock-heading "running clock")
9538 (buffer-file-name
9539 (marker-buffer org-clock-hd-marker))
9541 (marker-position org-clock-hd-marker)))
9542 (setq goto nil)))
9543 (setq it (or rfloc
9544 (save-excursion
9545 (org-refile-get-location
9546 (if goto "Goto: " "Refile to: ") default-buffer
9547 org-refile-allow-creating-parent-nodes)))))
9548 (setq file (nth 1 it)
9549 re (nth 2 it)
9550 pos (nth 3 it))
9551 (if (and (not goto)
9553 (equal (buffer-file-name) file)
9554 (if regionp
9555 (and (>= pos region-start)
9556 (<= pos region-end))
9557 (and (>= pos (point))
9558 (< pos (save-excursion
9559 (org-end-of-subtree t t))))))
9560 (error "Cannot refile to position inside the tree or region"))
9562 (setq nbuf (or (find-buffer-visiting file)
9563 (find-file-noselect file)))
9564 (if goto
9565 (progn
9566 (switch-to-buffer nbuf)
9567 (goto-char pos)
9568 (org-show-context 'org-goto))
9569 (if regionp
9570 (progn
9571 (org-kill-new (buffer-substring region-start region-end))
9572 (org-save-markers-in-region region-start region-end))
9573 (org-copy-subtree 1 nil t))
9574 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
9575 (find-file-noselect file)))
9576 (setq reversed (org-notes-order-reversed-p))
9577 (save-excursion
9578 (save-restriction
9579 (widen)
9580 (if pos
9581 (progn
9582 (goto-char pos)
9583 (looking-at outline-regexp)
9584 (setq level (org-get-valid-level (funcall outline-level) 1))
9585 (goto-char
9586 (if reversed
9587 (or (outline-next-heading) (point-max))
9588 (or (save-excursion (org-get-next-sibling))
9589 (org-end-of-subtree t t)
9590 (point-max)))))
9591 (setq level 1)
9592 (if (not reversed)
9593 (goto-char (point-max))
9594 (goto-char (point-min))
9595 (or (outline-next-heading) (goto-char (point-max)))))
9596 (if (not (bolp)) (newline))
9597 (org-paste-subtree level)
9598 (when org-log-refile
9599 (org-add-log-setup 'refile nil nil 'findpos
9600 org-log-refile)
9601 (unless (eq org-log-refile 'note)
9602 (save-excursion (org-add-log-note))))
9603 (and org-auto-align-tags (org-set-tags nil t))
9604 (bookmark-set "org-refile-last-stored")
9605 (if (fboundp 'deactivate-mark) (deactivate-mark))
9606 (run-hooks 'org-after-refile-insert-hook))))
9607 (if regionp
9608 (delete-region (point) (+ (point) region-length))
9609 (org-cut-subtree))
9610 (when (featurep 'org-inlinetask)
9611 (org-inlinetask-remove-END-maybe))
9612 (setq org-markers-to-move nil)
9613 (message "Refiled to \"%s\"" (car it))))))
9614 (org-reveal))
9616 (defun org-refile-goto-last-stored ()
9617 "Go to the location where the last refile was stored."
9618 (interactive)
9619 (bookmark-jump "org-refile-last-stored")
9620 (message "This is the location of the last refile"))
9622 (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
9623 "Prompt the user for a refile location, using PROMPT."
9624 (let ((org-refile-targets org-refile-targets)
9625 (org-refile-use-outline-path org-refile-use-outline-path))
9626 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
9627 (unless org-refile-target-table
9628 (error "No refile targets"))
9629 (let* ((cbuf (current-buffer))
9630 (partial-completion-mode nil)
9631 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
9632 (cfunc (if (and org-refile-use-outline-path
9633 org-outline-path-complete-in-steps)
9634 'org-olpath-completing-read
9635 'org-icompleting-read))
9636 (extra (if org-refile-use-outline-path "/" ""))
9637 (filename (and cfn (expand-file-name cfn)))
9638 (tbl (mapcar
9639 (lambda (x)
9640 (if (and (not (member org-refile-use-outline-path
9641 '(file full-file-path)))
9642 (not (equal filename (nth 1 x))))
9643 (cons (concat (car x) extra " ("
9644 (file-name-nondirectory (nth 1 x)) ")")
9645 (cdr x))
9646 (cons (concat (car x) extra) (cdr x))))
9647 org-refile-target-table))
9648 (completion-ignore-case t)
9649 pa answ parent-target child parent old-hist)
9650 (setq old-hist org-refile-history)
9651 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
9652 nil 'org-refile-history))
9653 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
9654 (if pa
9655 (progn
9656 (when (or (not org-refile-history)
9657 (not (eq old-hist org-refile-history))
9658 (not (equal (car pa) (car org-refile-history))))
9659 (setq org-refile-history
9660 (cons (car pa) (if (assoc (car org-refile-history) tbl)
9661 org-refile-history
9662 (cdr org-refile-history))))
9663 (if (equal (car org-refile-history) (nth 1 org-refile-history))
9664 (pop org-refile-history)))
9666 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
9667 (progn
9668 (setq parent (match-string 1 answ)
9669 child (match-string 2 answ))
9670 (setq parent-target (or (assoc parent tbl)
9671 (assoc (concat parent "/") tbl)))
9672 (when (and parent-target
9673 (or (eq new-nodes t)
9674 (and (eq new-nodes 'confirm)
9675 (y-or-n-p (format "Create new node \"%s\"? "
9676 child)))))
9677 (org-refile-new-child parent-target child)))
9678 (error "Invalid target location")))))
9680 (defun org-refile-new-child (parent-target child)
9681 "Use refile target PARENT-TARGET to add new CHILD below it."
9682 (unless parent-target
9683 (error "Cannot find parent for new node"))
9684 (let ((file (nth 1 parent-target))
9685 (pos (nth 3 parent-target))
9686 level)
9687 (with-current-buffer (or (find-buffer-visiting file)
9688 (find-file-noselect file))
9689 (save-excursion
9690 (save-restriction
9691 (widen)
9692 (if pos
9693 (goto-char pos)
9694 (goto-char (point-max))
9695 (if (not (bolp)) (newline)))
9696 (when (looking-at outline-regexp)
9697 (setq level (funcall outline-level))
9698 (org-end-of-subtree t t))
9699 (org-back-over-empty-lines)
9700 (insert "\n" (make-string
9701 (if pos (org-get-valid-level level 1) 1) ?*)
9702 " " child "\n")
9703 (beginning-of-line 0)
9704 (list (concat (car parent-target) "/" child) file "" (point)))))))
9706 (defun org-olpath-completing-read (prompt collection &rest args)
9707 "Read an outline path like a file name."
9708 (let ((thetable collection)
9709 (org-completion-use-ido nil) ; does not work with ido.
9710 (org-completion-use-iswitchb nil)) ; or iswitchb
9711 (apply
9712 'org-icompleting-read prompt
9713 (lambda (string predicate &optional flag)
9714 (let (rtn r f (l (length string)))
9715 (cond
9716 ((eq flag nil)
9717 ;; try completion
9718 (try-completion string thetable))
9719 ((eq flag t)
9720 ;; all-completions
9721 (setq rtn (all-completions string thetable predicate))
9722 (mapcar
9723 (lambda (x)
9724 (setq r (substring x l))
9725 (if (string-match " ([^)]*)$" x)
9726 (setq f (match-string 0 x))
9727 (setq f ""))
9728 (if (string-match "/" r)
9729 (concat string (substring r 0 (match-end 0)) f)
9731 rtn))
9732 ((eq flag 'lambda)
9733 ;; exact match?
9734 (assoc string thetable)))
9736 args)))
9738 ;;;; Dynamic blocks
9740 (defun org-find-dblock (name)
9741 "Find the first dynamic block with name NAME in the buffer.
9742 If not found, stay at current position and return nil."
9743 (let (pos)
9744 (save-excursion
9745 (goto-char (point-min))
9746 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
9747 nil t)
9748 (match-beginning 0))))
9749 (if pos (goto-char pos))
9750 pos))
9752 (defconst org-dblock-start-re
9753 "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
9754 "Matches the start line of a dynamic block, with parameters.")
9756 (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
9757 "Matches the end of a dynamic block.")
9759 (defun org-create-dblock (plist)
9760 "Create a dynamic block section, with parameters taken from PLIST.
9761 PLIST must contain a :name entry which is used as name of the block."
9762 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
9763 (end-of-line 1)
9764 (newline))
9765 (let ((col (current-column))
9766 (name (plist-get plist :name)))
9767 (insert "#+BEGIN: " name)
9768 (while plist
9769 (if (eq (car plist) :name)
9770 (setq plist (cddr plist))
9771 (insert " " (prin1-to-string (pop plist)))))
9772 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
9773 (beginning-of-line -2)))
9775 (defun org-prepare-dblock ()
9776 "Prepare dynamic block for refresh.
9777 This empties the block, puts the cursor at the insert position and returns
9778 the property list including an extra property :name with the block name."
9779 (unless (looking-at org-dblock-start-re)
9780 (error "Not at a dynamic block"))
9781 (let* ((begdel (1+ (match-end 0)))
9782 (name (org-no-properties (match-string 1)))
9783 (params (append (list :name name)
9784 (read (concat "(" (match-string 3) ")")))))
9785 (save-excursion
9786 (beginning-of-line 1)
9787 (skip-chars-forward " \t")
9788 (setq params (plist-put params :indentation-column (current-column))))
9789 (unless (re-search-forward org-dblock-end-re nil t)
9790 (error "Dynamic block not terminated"))
9791 (setq params
9792 (append params
9793 (list :content (buffer-substring
9794 begdel (match-beginning 0)))))
9795 (delete-region begdel (match-beginning 0))
9796 (goto-char begdel)
9797 (open-line 1)
9798 params))
9800 (defun org-map-dblocks (&optional command)
9801 "Apply COMMAND to all dynamic blocks in the current buffer.
9802 If COMMAND is not given, use `org-update-dblock'."
9803 (let ((cmd (or command 'org-update-dblock)))
9804 (save-excursion
9805 (goto-char (point-min))
9806 (while (re-search-forward org-dblock-start-re nil t)
9807 (goto-char (match-beginning 0))
9808 (save-excursion
9809 (condition-case nil
9810 (funcall cmd)
9811 (error (message "Error during update of dynamic block"))))
9812 (unless (re-search-forward org-dblock-end-re nil t)
9813 (error "Dynamic block not terminated"))))))
9815 (defun org-dblock-update (&optional arg)
9816 "User command for updating dynamic blocks.
9817 Update the dynamic block at point. With prefix ARG, update all dynamic
9818 blocks in the buffer."
9819 (interactive "P")
9820 (if arg
9821 (org-update-all-dblocks)
9822 (or (looking-at org-dblock-start-re)
9823 (org-beginning-of-dblock))
9824 (org-update-dblock)))
9826 (defun org-update-dblock ()
9827 "Update the dynamic block at point
9828 This means to empty the block, parse for parameters and then call
9829 the correct writing function."
9830 (save-window-excursion
9831 (let* ((pos (point))
9832 (line (org-current-line))
9833 (params (org-prepare-dblock))
9834 (name (plist-get params :name))
9835 (indent (plist-get params :indentation-column))
9836 (cmd (intern (concat "org-dblock-write:" name))))
9837 (message "Updating dynamic block `%s' at line %d..." name line)
9838 (funcall cmd params)
9839 (message "Updating dynamic block `%s' at line %d...done" name line)
9840 (goto-char pos)
9841 (when (and indent (> indent 0))
9842 (setq indent (make-string indent ?\ ))
9843 (save-excursion
9844 (org-beginning-of-dblock)
9845 (forward-line 1)
9846 (while (not (looking-at org-dblock-end-re))
9847 (insert indent)
9848 (beginning-of-line 2))
9849 (when (looking-at org-dblock-end-re)
9850 (and (looking-at "[ \t]+")
9851 (replace-match ""))
9852 (insert indent)))))))
9854 (defun org-beginning-of-dblock ()
9855 "Find the beginning of the dynamic block at point.
9856 Error if there is no such block at point."
9857 (let ((pos (point))
9858 beg)
9859 (end-of-line 1)
9860 (if (and (re-search-backward org-dblock-start-re nil t)
9861 (setq beg (match-beginning 0))
9862 (re-search-forward org-dblock-end-re nil t)
9863 (> (match-end 0) pos))
9864 (goto-char beg)
9865 (goto-char pos)
9866 (error "Not in a dynamic block"))))
9868 (defun org-update-all-dblocks ()
9869 "Update all dynamic blocks in the buffer.
9870 This function can be used in a hook."
9871 (when (org-mode-p)
9872 (org-map-dblocks 'org-update-dblock)))
9875 ;;;; Completion
9877 (defconst org-additional-option-like-keywords
9878 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
9879 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
9880 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
9881 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
9882 "BEGIN:" "END:"
9883 "ORGTBL" "TBLFM:" "TBLNAME:"
9884 "BEGIN_EXAMPLE" "END_EXAMPLE"
9885 "BEGIN_QUOTE" "END_QUOTE"
9886 "BEGIN_VERSE" "END_VERSE"
9887 "BEGIN_CENTER" "END_CENTER"
9888 "BEGIN_SRC" "END_SRC"
9889 "CATEGORY" "COLUMNS"
9890 "CAPTION" "LABEL"
9891 "SETUPFILE"
9892 "BIND"
9893 "MACRO"))
9895 (defcustom org-structure-template-alist
9897 ("s" "#+begin_src ?\n\n#+end_src"
9898 "<src lang=\"?\">\n\n</src>")
9899 ("e" "#+begin_example\n?\n#+end_example"
9900 "<example>\n?\n</example>")
9901 ("q" "#+begin_quote\n?\n#+end_quote"
9902 "<quote>\n?\n</quote>")
9903 ("v" "#+begin_verse\n?\n#+end_verse"
9904 "<verse>\n?\n/verse>")
9905 ("c" "#+begin_center\n?\n#+end_center"
9906 "<center>\n?\n/center>")
9907 ("l" "#+begin_latex\n?\n#+end_latex"
9908 "<literal style=\"latex\">\n?\n</literal>")
9909 ("L" "#+latex: "
9910 "<literal style=\"latex\">?</literal>")
9911 ("h" "#+begin_html\n?\n#+end_html"
9912 "<literal style=\"html\">\n?\n</literal>")
9913 ("H" "#+html: "
9914 "<literal style=\"html\">?</literal>")
9915 ("a" "#+begin_ascii\n?\n#+end_ascii")
9916 ("A" "#+ascii: ")
9917 ("i" "#+include %file ?"
9918 "<include file=%file markup=\"?\">")
9920 "Structure completion elements.
9921 This is a list of abbreviation keys and values. The value gets inserted
9922 it you type @samp{.} followed by the key and then the completion key,
9923 usually `M-TAB'. %file will be replaced by a file name after prompting
9924 for the file using completion.
9925 There are two templates for each key, the first uses the original Org syntax,
9926 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
9927 the default when the /org-mtags.el/ module has been loaded. See also the
9928 variable `org-mtags-prefer-muse-templates'.
9929 This is an experimental feature, it is undecided if it is going to stay in."
9930 :group 'org-completion
9931 :type '(repeat
9932 (string :tag "Key")
9933 (string :tag "Template")
9934 (string :tag "Muse Template")))
9936 (defun org-try-structure-completion ()
9937 "Try to complete a structure template before point.
9938 This looks for strings like \"<e\" on an otherwise empty line and
9939 expands them."
9940 (let ((l (buffer-substring (point-at-bol) (point)))
9942 (when (and (looking-at "[ \t]*$")
9943 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
9944 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
9945 (org-complete-expand-structure-template (+ -1 (point-at-bol)
9946 (match-beginning 1)) a)
9947 t)))
9949 (defun org-complete-expand-structure-template (start cell)
9950 "Expand a structure template."
9951 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
9952 (rpl (nth (if musep 2 1) cell))
9953 (ind ""))
9954 (delete-region start (point))
9955 (when (string-match "\\`#\\+" rpl)
9956 (cond
9957 ((bolp))
9958 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
9959 (setq ind (buffer-substring (point-at-bol) (point))))
9960 (t (newline))))
9961 (setq start (point))
9962 (if (string-match "%file" rpl)
9963 (setq rpl (replace-match
9964 (concat
9965 "\""
9966 (save-match-data
9967 (abbreviate-file-name (read-file-name "Include file: ")))
9968 "\"")
9969 t t rpl)))
9970 (setq rpl (mapconcat 'identity (split-string rpl "\n")
9971 (concat "\n" ind)))
9972 (insert rpl)
9973 (if (re-search-backward "\\?" start t) (delete-char 1))))
9976 (defun org-complete (&optional arg)
9977 "Perform completion on word at point.
9978 At the beginning of a headline, this completes TODO keywords as given in
9979 `org-todo-keywords'.
9980 If the current word is preceded by a backslash, completes the TeX symbols
9981 that are supported for HTML support.
9982 If the current word is preceded by \"#+\", completes special words for
9983 setting file options.
9984 In the line after \"#+STARTUP:, complete valid keywords.\"
9985 At all other locations, this simply calls the value of
9986 `org-completion-fallback-command'."
9987 (interactive "P")
9988 (org-without-partial-completion
9989 (catch 'exit
9990 (let* ((a nil)
9991 (end (point))
9992 (beg1 (save-excursion
9993 (skip-chars-backward (org-re "[:alnum:]_@"))
9994 (point)))
9995 (beg (save-excursion
9996 (skip-chars-backward "a-zA-Z0-9_:$")
9997 (point)))
9998 (confirm (lambda (x) (stringp (car x))))
9999 (searchhead (equal (char-before beg) ?*))
10000 (struct
10001 (when (and (member (char-before beg1) '(?. ?<))
10002 (setq a (assoc (buffer-substring beg1 (point))
10003 org-structure-template-alist)))
10004 (org-complete-expand-structure-template (1- beg1) a)
10005 (throw 'exit t)))
10006 (tag (and (equal (char-before beg1) ?:)
10007 (equal (char-after (point-at-bol)) ?*)))
10008 (prop (and (equal (char-before beg1) ?:)
10009 (not (equal (char-after (point-at-bol)) ?*))))
10010 (texp (equal (char-before beg) ?\\))
10011 (link (equal (char-before beg) ?\[))
10012 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
10013 beg)
10014 "#+"))
10015 (startup (string-match "^#\\+STARTUP:.*"
10016 (buffer-substring (point-at-bol) (point))))
10017 (completion-ignore-case opt)
10018 (type nil)
10019 (tbl nil)
10020 (table (cond
10021 (opt
10022 (setq type :opt)
10023 (require 'org-exp)
10024 (append
10025 (delq nil
10026 (mapcar
10027 (lambda (x)
10028 (if (string-match
10029 "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
10030 (cons (match-string 2 x)
10031 (match-string 1 x))))
10032 (org-split-string (org-get-current-options) "\n")))
10033 (mapcar 'list org-additional-option-like-keywords)))
10034 (startup
10035 (setq type :startup)
10036 org-startup-options)
10037 (link (append org-link-abbrev-alist-local
10038 org-link-abbrev-alist))
10039 (texp
10040 (setq type :tex)
10041 (append org-entities-user org-entities))
10042 ((string-match "\\`\\*+[ \t]+\\'"
10043 (buffer-substring (point-at-bol) beg))
10044 (setq type :todo)
10045 (mapcar 'list org-todo-keywords-1))
10046 (searchhead
10047 (setq type :searchhead)
10048 (save-excursion
10049 (goto-char (point-min))
10050 (while (re-search-forward org-todo-line-regexp nil t)
10051 (push (list
10052 (org-make-org-heading-search-string
10053 (match-string 3) t))
10054 tbl)))
10055 tbl)
10056 (tag (setq type :tag beg beg1)
10057 (or org-tag-alist (org-get-buffer-tags)))
10058 (prop (setq type :prop beg beg1)
10059 (mapcar 'list (org-buffer-property-keys nil t t)))
10060 (t (progn
10061 (call-interactively org-completion-fallback-command)
10062 (throw 'exit nil)))))
10063 (pattern (buffer-substring-no-properties beg end))
10064 (completion (try-completion pattern table confirm)))
10065 (cond ((eq completion t)
10066 (if (not (assoc (upcase pattern) table))
10067 (message "Already complete")
10068 (if (and (equal type :opt)
10069 (not (member (car (assoc (upcase pattern) table))
10070 org-additional-option-like-keywords)))
10071 (insert (substring (cdr (assoc (upcase pattern) table))
10072 (length pattern)))
10073 (if (memq type '(:tag :prop)) (insert ":")))))
10074 ((null completion)
10075 (message "Can't find completion for \"%s\"" pattern)
10076 (ding))
10077 ((not (string= pattern completion))
10078 (delete-region beg end)
10079 (if (string-match " +$" completion)
10080 (setq completion (replace-match "" t t completion)))
10081 (insert completion)
10082 (if (get-buffer-window "*Completions*")
10083 (delete-window (get-buffer-window "*Completions*")))
10084 (if (assoc completion table)
10085 (if (eq type :todo) (insert " ")
10086 (if (memq type '(:tag :prop)) (insert ":"))))
10087 (if (and (equal type :opt) (assoc completion table))
10088 (message "%s" (substitute-command-keys
10089 "Press \\[org-complete] again to insert example settings"))))
10091 (message "Making completion list...")
10092 (let ((list (sort (all-completions pattern table confirm)
10093 'string<)))
10094 (with-output-to-temp-buffer "*Completions*"
10095 (condition-case nil
10096 ;; Protection needed for XEmacs and emacs 21
10097 (display-completion-list list pattern)
10098 (error (display-completion-list list)))))
10099 (message "Making completion list...%s" "done")))))))
10101 ;;;; TODO, DEADLINE, Comments
10103 (defun org-toggle-comment ()
10104 "Change the COMMENT state of an entry."
10105 (interactive)
10106 (save-excursion
10107 (org-back-to-heading)
10108 (let (case-fold-search)
10109 (if (looking-at (concat outline-regexp
10110 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
10111 (replace-match "" t t nil 1)
10112 (if (looking-at outline-regexp)
10113 (progn
10114 (goto-char (match-end 0))
10115 (insert org-comment-string " ")))))))
10117 (defvar org-last-todo-state-is-todo nil
10118 "This is non-nil when the last TODO state change led to a TODO state.
10119 If the last change removed the TODO tag or switched to DONE, then
10120 this is nil.")
10122 (defvar org-setting-tags nil) ; dynamically skipped
10124 (defun org-parse-local-options (string var)
10125 "Parse STRING for startup setting relevant for variable VAR."
10126 (let ((rtn (symbol-value var))
10127 e opts)
10128 (save-match-data
10129 (if (or (not string) (not (string-match "\\S-" string)))
10131 (setq opts (delq nil (mapcar (lambda (x)
10132 (setq e (assoc x org-startup-options))
10133 (if (eq (nth 1 e) var) e nil))
10134 (org-split-string string "[ \t]+"))))
10135 (if (not opts)
10137 (setq rtn nil)
10138 (while (setq e (pop opts))
10139 (if (not (nth 3 e))
10140 (setq rtn (nth 2 e))
10141 (if (not (listp rtn)) (setq rtn nil))
10142 (push (nth 2 e) rtn)))
10143 rtn)))))
10145 (defvar org-todo-setup-filter-hook nil
10146 "Hook for functions that pre-filter todo specs.
10148 Each function takes a todo spec and returns either `nil' or the spec
10149 transformed into canonical form." )
10151 (defvar org-todo-get-default-hook nil
10152 "Hook for functions that get a default item for todo.
10154 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
10155 `nil' or a string to be used for the todo mark." )
10157 (defvar org-agenda-headline-snapshot-before-repeat)
10159 (defun org-todo (&optional arg)
10160 "Change the TODO state of an item.
10161 The state of an item is given by a keyword at the start of the heading,
10162 like
10163 *** TODO Write paper
10164 *** DONE Call mom
10166 The different keywords are specified in the variable `org-todo-keywords'.
10167 By default the available states are \"TODO\" and \"DONE\".
10168 So for this example: when the item starts with TODO, it is changed to DONE.
10169 When it starts with DONE, the DONE is removed. And when neither TODO nor
10170 DONE are present, add TODO at the beginning of the heading.
10172 With C-u prefix arg, use completion to determine the new state.
10173 With numeric prefix arg, switch to that state.
10174 With a double C-u prefix, switch to the next set of TODO keywords (nextset).
10175 With a triple C-u prefix, circumvent any state blocking.
10177 For calling through lisp, arg is also interpreted in the following way:
10178 'none -> empty state
10179 \"\"(empty string) -> switch to empty state
10180 'done -> switch to DONE
10181 'nextset -> switch to the next set of keywords
10182 'previousset -> switch to the previous set of keywords
10183 \"WAITING\" -> switch to the specified keyword, but only if it
10184 really is a member of `org-todo-keywords'."
10185 (interactive "P")
10186 (if (equal arg '(16)) (setq arg 'nextset))
10187 (let ((org-blocker-hook org-blocker-hook)
10188 (case-fold-search nil))
10189 (when (equal arg '(64))
10190 (setq arg nil org-blocker-hook nil))
10191 (when (and org-blocker-hook
10192 (or org-inhibit-blocking
10193 (org-entry-get nil "NOBLOCKING")))
10194 (setq org-blocker-hook nil))
10195 (save-excursion
10196 (catch 'exit
10197 (org-back-to-heading t)
10198 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
10199 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
10200 (looking-at " *"))
10201 (let* ((match-data (match-data))
10202 (startpos (point-at-bol))
10203 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
10204 (org-log-done org-log-done)
10205 (org-log-repeat org-log-repeat)
10206 (org-todo-log-states org-todo-log-states)
10207 (this (match-string 1))
10208 (hl-pos (match-beginning 0))
10209 (head (org-get-todo-sequence-head this))
10210 (ass (assoc head org-todo-kwd-alist))
10211 (interpret (nth 1 ass))
10212 (done-word (nth 3 ass))
10213 (final-done-word (nth 4 ass))
10214 (last-state (or this ""))
10215 (completion-ignore-case t)
10216 (member (member this org-todo-keywords-1))
10217 (tail (cdr member))
10218 (state (cond
10219 ((and org-todo-key-trigger
10220 (or (and (equal arg '(4))
10221 (eq org-use-fast-todo-selection 'prefix))
10222 (and (not arg) org-use-fast-todo-selection
10223 (not (eq org-use-fast-todo-selection
10224 'prefix)))))
10225 ;; Use fast selection
10226 (org-fast-todo-selection))
10227 ((and (equal arg '(4))
10228 (or (not org-use-fast-todo-selection)
10229 (not org-todo-key-trigger)))
10230 ;; Read a state with completion
10231 (org-icompleting-read
10232 "State: " (mapcar (lambda(x) (list x))
10233 org-todo-keywords-1)
10234 nil t))
10235 ((eq arg 'right)
10236 (if this
10237 (if tail (car tail) nil)
10238 (car org-todo-keywords-1)))
10239 ((eq arg 'left)
10240 (if (equal member org-todo-keywords-1)
10242 (if this
10243 (nth (- (length org-todo-keywords-1)
10244 (length tail) 2)
10245 org-todo-keywords-1)
10246 (org-last org-todo-keywords-1))))
10247 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
10248 (setq arg nil))) ; hack to fall back to cycling
10249 (arg
10250 ;; user or caller requests a specific state
10251 (cond
10252 ((equal arg "") nil)
10253 ((eq arg 'none) nil)
10254 ((eq arg 'done) (or done-word (car org-done-keywords)))
10255 ((eq arg 'nextset)
10256 (or (car (cdr (member head org-todo-heads)))
10257 (car org-todo-heads)))
10258 ((eq arg 'previousset)
10259 (let ((org-todo-heads (reverse org-todo-heads)))
10260 (or (car (cdr (member head org-todo-heads)))
10261 (car org-todo-heads))))
10262 ((car (member arg org-todo-keywords-1)))
10263 ((stringp arg)
10264 (error "State `%s' not valid in this file" arg))
10265 ((nth (1- (prefix-numeric-value arg))
10266 org-todo-keywords-1))))
10267 ((null member) (or head (car org-todo-keywords-1)))
10268 ((equal this final-done-word) nil) ;; -> make empty
10269 ((null tail) nil) ;; -> first entry
10270 ((memq interpret '(type priority))
10271 (if (eq this-command last-command)
10272 (car tail)
10273 (if (> (length tail) 0)
10274 (or done-word (car org-done-keywords))
10275 nil)))
10277 (car tail))))
10278 (state (or
10279 (run-hook-with-args-until-success
10280 'org-todo-get-default-hook state last-state)
10281 state))
10282 (next (if state (concat " " state " ") " "))
10283 (change-plist (list :type 'todo-state-change :from this :to state
10284 :position startpos))
10285 dolog now-done-p)
10286 (when org-blocker-hook
10287 (setq org-last-todo-state-is-todo
10288 (not (member this org-done-keywords)))
10289 (unless (save-excursion
10290 (save-match-data
10291 (run-hook-with-args-until-failure
10292 'org-blocker-hook change-plist)))
10293 (if (interactive-p)
10294 (error "TODO state change from %s to %s blocked" this state)
10295 ;; fail silently
10296 (message "TODO state change from %s to %s blocked" this state)
10297 (throw 'exit nil))))
10298 (store-match-data match-data)
10299 (replace-match next t t)
10300 (unless (pos-visible-in-window-p hl-pos)
10301 (message "TODO state changed to %s" (org-trim next)))
10302 (unless head
10303 (setq head (org-get-todo-sequence-head state)
10304 ass (assoc head org-todo-kwd-alist)
10305 interpret (nth 1 ass)
10306 done-word (nth 3 ass)
10307 final-done-word (nth 4 ass)))
10308 (when (memq arg '(nextset previousset))
10309 (message "Keyword-Set %d/%d: %s"
10310 (- (length org-todo-sets) -1
10311 (length (memq (assoc state org-todo-sets) org-todo-sets)))
10312 (length org-todo-sets)
10313 (mapconcat 'identity (assoc state org-todo-sets) " ")))
10314 (setq org-last-todo-state-is-todo
10315 (not (member state org-done-keywords)))
10316 (setq now-done-p (and (member state org-done-keywords)
10317 (not (member this org-done-keywords))))
10318 (and logging (org-local-logging logging))
10319 (when (and (or org-todo-log-states org-log-done)
10320 (not (eq org-inhibit-logging t))
10321 (not (memq arg '(nextset previousset))))
10322 ;; we need to look at recording a time and note
10323 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
10324 (nth 2 (assoc this org-todo-log-states))))
10325 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
10326 (setq dolog 'time))
10327 (when (and state
10328 (member state org-not-done-keywords)
10329 (not (member this org-not-done-keywords)))
10330 ;; This is now a todo state and was not one before
10331 ;; If there was a CLOSED time stamp, get rid of it.
10332 (org-add-planning-info nil nil 'closed))
10333 (when (and now-done-p org-log-done)
10334 ;; It is now done, and it was not done before
10335 (org-add-planning-info 'closed (org-current-time))
10336 (if (and (not dolog) (eq 'note org-log-done))
10337 (org-add-log-setup 'done state this 'findpos 'note)))
10338 (when (and state dolog)
10339 ;; This is a non-nil state, and we need to log it
10340 (org-add-log-setup 'state state this 'findpos dolog)))
10341 ;; Fixup tag positioning
10342 (org-todo-trigger-tag-changes state)
10343 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
10344 (when org-provide-todo-statistics
10345 (org-update-parent-todo-statistics))
10346 (run-hooks 'org-after-todo-state-change-hook)
10347 (if (and arg (not (member state org-done-keywords)))
10348 (setq head (org-get-todo-sequence-head state)))
10349 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
10350 ;; Do we need to trigger a repeat?
10351 (when now-done-p
10352 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
10353 ;; This is for the agenda, take a snapshot of the headline.
10354 (save-match-data
10355 (setq org-agenda-headline-snapshot-before-repeat
10356 (org-get-heading))))
10357 (org-auto-repeat-maybe state))
10358 ;; Fixup cursor location if close to the keyword
10359 (if (and (outline-on-heading-p)
10360 (not (bolp))
10361 (save-excursion (beginning-of-line 1)
10362 (looking-at org-todo-line-regexp))
10363 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
10364 (progn
10365 (goto-char (or (match-end 2) (match-end 1)))
10366 (and (looking-at " ") (just-one-space))))
10367 (when org-trigger-hook
10368 (save-excursion
10369 (run-hook-with-args 'org-trigger-hook change-plist))))))))
10371 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
10372 "Block turning an entry into a TODO, using the hierarchy.
10373 This checks whether the current task should be blocked from state
10374 changes. Such blocking occurs when:
10376 1. The task has children which are not all in a completed state.
10378 2. A task has a parent with the property :ORDERED:, and there
10379 are siblings prior to the current task with incomplete
10380 status.
10382 3. The parent of the task is blocked because it has siblings that should
10383 be done first, or is child of a block grandparent TODO entry."
10385 (if (not org-enforce-todo-dependencies)
10386 t ; if locally turned off don't block
10387 (catch 'dont-block
10388 ;; If this is not a todo state change, or if this entry is already DONE,
10389 ;; do not block
10390 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
10391 (member (plist-get change-plist :from)
10392 (cons 'done org-done-keywords))
10393 (member (plist-get change-plist :to)
10394 (cons 'todo org-not-done-keywords))
10395 (not (plist-get change-plist :to)))
10396 (throw 'dont-block t))
10397 ;; If this task has children, and any are undone, it's blocked
10398 (save-excursion
10399 (org-back-to-heading t)
10400 (let ((this-level (funcall outline-level)))
10401 (outline-next-heading)
10402 (let ((child-level (funcall outline-level)))
10403 (while (and (not (eobp))
10404 (> child-level this-level))
10405 ;; this todo has children, check whether they are all
10406 ;; completed
10407 (if (and (not (org-entry-is-done-p))
10408 (org-entry-is-todo-p))
10409 (throw 'dont-block nil))
10410 (outline-next-heading)
10411 (setq child-level (funcall outline-level))))))
10412 ;; Otherwise, if the task's parent has the :ORDERED: property, and
10413 ;; any previous siblings are undone, it's blocked
10414 (save-excursion
10415 (org-back-to-heading t)
10416 (let* ((pos (point))
10417 (parent-pos (and (org-up-heading-safe) (point))))
10418 (if (not parent-pos) (throw 'dont-block t)) ; no parent
10419 (when (and (org-entry-get (point) "ORDERED")
10420 (forward-line 1)
10421 (re-search-forward org-not-done-heading-regexp pos t))
10422 (throw 'dont-block nil)) ; block, there is an older sibling not done.
10423 ;; Search further up the hierarchy, to see if an anchestor is blocked
10424 (while t
10425 (goto-char parent-pos)
10426 (if (not (looking-at org-not-done-heading-regexp))
10427 (throw 'dont-block t)) ; do not block, parent is not a TODO
10428 (setq pos (point))
10429 (setq parent-pos (and (org-up-heading-safe) (point)))
10430 (if (not parent-pos) (throw 'dont-block t)) ; no parent
10431 (when (and (org-entry-get (point) "ORDERED")
10432 (forward-line 1)
10433 (re-search-forward org-not-done-heading-regexp pos t))
10434 (throw 'dont-block nil)))))))) ; block, older sibling not done.
10436 (defcustom org-track-ordered-property-with-tag nil
10437 "Should the ORDERED property also be shown as a tag?
10438 The ORDERED property decides if an entry should require subtasks to be
10439 completed in sequence. Since a property is not very visible, setting
10440 this option means that toggling the ORDERED property with the command
10441 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
10442 not relevant for the behavior, but it makes things more visible.
10444 Note that toggling the tag with tags commands will not change the property
10445 and therefore not influence behavior!
10447 This can be t, meaning the tag ORDERED should be used, It can also be a
10448 string to select a different tag for this task."
10449 :group 'org-todo
10450 :type '(choice
10451 (const :tag "No tracking" nil)
10452 (const :tag "Track with ORDERED tag" t)
10453 (string :tag "Use other tag")))
10455 (defun org-toggle-ordered-property ()
10456 "Toggle the ORDERED property of the current entry.
10457 For better visibility, you can track the value of this property with a tag.
10458 See variable `org-track-ordered-property-with-tag'."
10459 (interactive)
10460 (let* ((t1 org-track-ordered-property-with-tag)
10461 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
10462 (save-excursion
10463 (org-back-to-heading)
10464 (if (org-entry-get nil "ORDERED")
10465 (progn
10466 (org-delete-property "ORDERED")
10467 (and tag (org-toggle-tag tag 'off))
10468 (message "Subtasks can be completed in arbitrary order"))
10469 (org-entry-put nil "ORDERED" "t")
10470 (and tag (org-toggle-tag tag 'on))
10471 (message "Subtasks must be completed in sequence")))))
10473 (defvar org-blocked-by-checkboxes) ; dynamically scoped
10474 (defun org-block-todo-from-checkboxes (change-plist)
10475 "Block turning an entry into a TODO, using checkboxes.
10476 This checks whether the current task should be blocked from state
10477 changes because there are unchecked boxes in this entry."
10478 (if (not org-enforce-todo-checkbox-dependencies)
10479 t ; if locally turned off don't block
10480 (catch 'dont-block
10481 ;; If this is not a todo state change, or if this entry is already DONE,
10482 ;; do not block
10483 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
10484 (member (plist-get change-plist :from)
10485 (cons 'done org-done-keywords))
10486 (member (plist-get change-plist :to)
10487 (cons 'todo org-not-done-keywords))
10488 (not (plist-get change-plist :to)))
10489 (throw 'dont-block t))
10490 ;; If this task has checkboxes that are not checked, it's blocked
10491 (save-excursion
10492 (org-back-to-heading t)
10493 (let ((beg (point)) end)
10494 (outline-next-heading)
10495 (setq end (point))
10496 (goto-char beg)
10497 (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
10498 end t)
10499 (progn
10500 (if (boundp 'org-blocked-by-checkboxes)
10501 (setq org-blocked-by-checkboxes t))
10502 (throw 'dont-block nil)))))
10503 t))) ; do not block
10505 (defun org-entry-blocked-p ()
10506 "Is the current entry blocked?"
10507 (if (org-entry-get nil "NOBLOCKING")
10508 nil ;; Never block this entry
10509 (not
10510 (run-hook-with-args-until-failure
10511 'org-blocker-hook
10512 (list :type 'todo-state-change
10513 :position (point)
10514 :from 'todo
10515 :to 'done)))))
10517 (defun org-update-statistics-cookies (all)
10518 "Update the statistics cookie, either from TODO or from checkboxes.
10519 This should be called with the cursor in a line with a statistics cookie."
10520 (interactive "P")
10521 (if all
10522 (progn
10523 (org-update-checkbox-count 'all)
10524 (org-map-entries 'org-update-parent-todo-statistics))
10525 (if (not (org-on-heading-p))
10526 (org-update-checkbox-count)
10527 (let ((pos (move-marker (make-marker) (point)))
10528 end l1 l2)
10529 (ignore-errors (org-back-to-heading t))
10530 (if (not (org-on-heading-p))
10531 (org-update-checkbox-count)
10532 (setq l1 (org-outline-level))
10533 (setq end (save-excursion
10534 (outline-next-heading)
10535 (if (org-on-heading-p) (setq l2 (org-outline-level)))
10536 (point)))
10537 (if (and (save-excursion
10538 (re-search-forward
10539 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
10540 (not (save-excursion (re-search-forward
10541 ":COOKIE_DATA:.*\\<todo\\>" end t))))
10542 (org-update-checkbox-count)
10543 (if (and l2 (> l2 l1))
10544 (progn
10545 (goto-char end)
10546 (org-update-parent-todo-statistics))
10547 (goto-char pos)
10548 (beginning-of-line 1)
10549 (while (re-search-forward
10550 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
10551 (point-at-eol) t)
10552 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
10553 (goto-char pos)
10554 (move-marker pos nil)))))
10556 (defvar org-entry-property-inherited-from) ;; defined below
10557 (defun org-update-parent-todo-statistics ()
10558 "Update any statistics cookie in the parent of the current headline.
10559 When `org-hierarchical-todo-statistics' is nil, statistics will cover
10560 the entire subtree and this will travel up the hierarchy and update
10561 statistics everywhere."
10562 (interactive)
10563 (let* ((lim 0) prop
10564 (recursive (or (not org-hierarchical-todo-statistics)
10565 (string-match
10566 "\\<recursive\\>"
10567 (or (setq prop (org-entry-get
10568 nil "COOKIE_DATA" 'inherit)) ""))))
10569 (lim (or (and prop (marker-position
10570 org-entry-property-inherited-from))
10571 lim))
10572 (first t)
10573 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
10574 level ltoggle l1 new ndel
10575 (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
10576 (catch 'exit
10577 (save-excursion
10578 (beginning-of-line 1)
10579 (if (org-at-heading-p)
10580 (setq ltoggle (funcall outline-level))
10581 (error "This should not happen"))
10582 (while (and (setq level (org-up-heading-safe))
10583 (or recursive first)
10584 (>= (point) lim))
10585 (setq first nil cookie-present nil)
10586 (unless (and level
10587 (not (string-match
10588 "\\<checkbox\\>"
10589 (downcase
10590 (or (org-entry-get
10591 nil "COOKIE_DATA")
10592 "")))))
10593 (throw 'exit nil))
10594 (while (re-search-forward box-re (point-at-eol) t)
10595 (setq cnt-all 0 cnt-done 0 cookie-present t)
10596 (setq is-percent (match-end 2))
10597 (save-match-data
10598 (unless (outline-next-heading) (throw 'exit nil))
10599 (while (and (looking-at org-complex-heading-regexp)
10600 (> (setq l1 (length (match-string 1))) level))
10601 (setq kwd (and (or recursive (= l1 ltoggle))
10602 (match-string 2)))
10603 (if (or (eq org-provide-todo-statistics 'all-headlines)
10604 (and (listp org-provide-todo-statistics)
10605 (or (member kwd org-provide-todo-statistics)
10606 (member kwd org-done-keywords))))
10607 (setq cnt-all (1+ cnt-all))
10608 (if (eq org-provide-todo-statistics t)
10609 (and kwd (setq cnt-all (1+ cnt-all)))))
10610 (and (member kwd org-done-keywords)
10611 (setq cnt-done (1+ cnt-done)))
10612 (outline-next-heading)))
10613 (setq new
10614 (if is-percent
10615 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
10616 (format "[%d/%d]" cnt-done cnt-all))
10617 ndel (- (match-end 0) (match-beginning 0)))
10618 (goto-char (match-beginning 0))
10619 (insert new)
10620 (delete-region (point) (+ (point) ndel)))
10621 (when cookie-present
10622 (run-hook-with-args 'org-after-todo-statistics-hook
10623 cnt-done (- cnt-all cnt-done))))))
10624 (run-hooks 'org-todo-statistics-hook)))
10626 (defvar org-after-todo-statistics-hook nil
10627 "Hook that is called after a TODO statistics cookie has been updated.
10628 Each function is called with two arguments: the number of not-done entries
10629 and the number of done entries.
10631 For example, the following function, when added to this hook, will switch
10632 an entry to DONE when all children are done, and back to TODO when new
10633 entries are set to a TODO status. Note that this hook is only called
10634 when there is a statistics cookie in the headline!
10636 (defun org-summary-todo (n-done n-not-done)
10637 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
10638 (let (org-log-done org-log-states) ; turn off logging
10639 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
10642 (defvar org-todo-statistics-hook nil
10643 "Hook that is run whenever Org thinks TODO statistics should be updated.
10644 This hook runs even if there is no statistics cookie present, in which case
10645 `org-after-todo-statistics-hook' would not run.")
10647 (defun org-todo-trigger-tag-changes (state)
10648 "Apply the changes defined in `org-todo-state-tags-triggers'."
10649 (let ((l org-todo-state-tags-triggers)
10650 changes)
10651 (when (or (not state) (equal state ""))
10652 (setq changes (append changes (cdr (assoc "" l)))))
10653 (when (and (stringp state) (> (length state) 0))
10654 (setq changes (append changes (cdr (assoc state l)))))
10655 (when (member state org-not-done-keywords)
10656 (setq changes (append changes (cdr (assoc 'todo l)))))
10657 (when (member state org-done-keywords)
10658 (setq changes (append changes (cdr (assoc 'done l)))))
10659 (dolist (c changes)
10660 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
10662 (defun org-local-logging (value)
10663 "Get logging settings from a property VALUE."
10664 (let* (words w a)
10665 ;; directly set the variables, they are already local.
10666 (setq org-log-done nil
10667 org-log-repeat nil
10668 org-todo-log-states nil)
10669 (setq words (org-split-string value))
10670 (while (setq w (pop words))
10671 (cond
10672 ((setq a (assoc w org-startup-options))
10673 (and (member (nth 1 a) '(org-log-done org-log-repeat))
10674 (set (nth 1 a) (nth 2 a))))
10675 ((setq a (org-extract-log-state-settings w))
10676 (and (member (car a) org-todo-keywords-1)
10677 (push a org-todo-log-states)))))))
10679 (defun org-get-todo-sequence-head (kwd)
10680 "Return the head of the TODO sequence to which KWD belongs.
10681 If KWD is not set, check if there is a text property remembering the
10682 right sequence."
10683 (let (p)
10684 (cond
10685 ((not kwd)
10686 (or (get-text-property (point-at-bol) 'org-todo-head)
10687 (progn
10688 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
10689 nil (point-at-eol)))
10690 (get-text-property p 'org-todo-head))))
10691 ((not (member kwd org-todo-keywords-1))
10692 (car org-todo-keywords-1))
10693 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
10695 (defun org-fast-todo-selection ()
10696 "Fast TODO keyword selection with single keys.
10697 Returns the new TODO keyword, or nil if no state change should occur."
10698 (let* ((fulltable org-todo-key-alist)
10699 (done-keywords org-done-keywords) ;; needed for the faces.
10700 (maxlen (apply 'max (mapcar
10701 (lambda (x)
10702 (if (stringp (car x)) (string-width (car x)) 0))
10703 fulltable)))
10704 (expert nil)
10705 (fwidth (+ maxlen 3 1 3))
10706 (ncol (/ (- (window-width) 4) fwidth))
10707 tg cnt e c tbl
10708 groups ingroup)
10709 (save-excursion
10710 (save-window-excursion
10711 (if expert
10712 (set-buffer (get-buffer-create " *Org todo*"))
10713 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
10714 (erase-buffer)
10715 (org-set-local 'org-done-keywords done-keywords)
10716 (setq tbl fulltable cnt 0)
10717 (while (setq e (pop tbl))
10718 (cond
10719 ((equal e '(:startgroup))
10720 (push '() groups) (setq ingroup t)
10721 (when (not (= cnt 0))
10722 (setq cnt 0)
10723 (insert "\n"))
10724 (insert "{ "))
10725 ((equal e '(:endgroup))
10726 (setq ingroup nil cnt 0)
10727 (insert "}\n"))
10728 ((equal e '(:newline))
10729 (when (not (= cnt 0))
10730 (setq cnt 0)
10731 (insert "\n")
10732 (setq e (car tbl))
10733 (while (equal (car tbl) '(:newline))
10734 (insert "\n")
10735 (setq tbl (cdr tbl)))))
10737 (setq tg (car e) c (cdr e))
10738 (if ingroup (push tg (car groups)))
10739 (setq tg (org-add-props tg nil 'face
10740 (org-get-todo-face tg)))
10741 (if (and (= cnt 0) (not ingroup)) (insert " "))
10742 (insert "[" c "] " tg (make-string
10743 (- fwidth 4 (length tg)) ?\ ))
10744 (when (= (setq cnt (1+ cnt)) ncol)
10745 (insert "\n")
10746 (if ingroup (insert " "))
10747 (setq cnt 0)))))
10748 (insert "\n")
10749 (goto-char (point-min))
10750 (if (not expert) (org-fit-window-to-buffer))
10751 (message "[a-z..]:Set [SPC]:clear")
10752 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
10753 (cond
10754 ((or (= c ?\C-g)
10755 (and (= c ?q) (not (rassoc c fulltable))))
10756 (setq quit-flag t))
10757 ((= c ?\ ) nil)
10758 ((setq e (rassoc c fulltable) tg (car e))
10760 (t (setq quit-flag t)))))))
10762 (defun org-entry-is-todo-p ()
10763 (member (org-get-todo-state) org-not-done-keywords))
10765 (defun org-entry-is-done-p ()
10766 (member (org-get-todo-state) org-done-keywords))
10768 (defun org-get-todo-state ()
10769 (save-excursion
10770 (org-back-to-heading t)
10771 (and (looking-at org-todo-line-regexp)
10772 (match-end 2)
10773 (match-string 2))))
10775 (defun org-at-date-range-p (&optional inactive-ok)
10776 "Is the cursor inside a date range?"
10777 (interactive)
10778 (save-excursion
10779 (catch 'exit
10780 (let ((pos (point)))
10781 (skip-chars-backward "^[<\r\n")
10782 (skip-chars-backward "<[")
10783 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
10784 (>= (match-end 0) pos)
10785 (throw 'exit t))
10786 (skip-chars-backward "^<[\r\n")
10787 (skip-chars-backward "<[")
10788 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
10789 (>= (match-end 0) pos)
10790 (throw 'exit t)))
10791 nil)))
10793 (defun org-get-repeat (&optional tagline)
10794 "Check if there is a deadline/schedule with repeater in this entry."
10795 (save-match-data
10796 (save-excursion
10797 (org-back-to-heading t)
10798 (and (re-search-forward (if tagline
10799 (concat tagline "\\s-*" org-repeat-re)
10800 org-repeat-re)
10801 (org-entry-end-position) t)
10802 (match-string-no-properties 1)))))
10804 (defvar org-last-changed-timestamp)
10805 (defvar org-last-inserted-timestamp)
10806 (defvar org-log-post-message)
10807 (defvar org-log-note-purpose)
10808 (defvar org-log-note-how)
10809 (defvar org-log-note-extra)
10810 (defun org-auto-repeat-maybe (done-word)
10811 "Check if the current headline contains a repeated deadline/schedule.
10812 If yes, set TODO state back to what it was and change the base date
10813 of repeating deadline/scheduled time stamps to new date.
10814 This function is run automatically after each state change to a DONE state."
10815 ;; last-state is dynamically scoped into this function
10816 (let* ((repeat (org-get-repeat))
10817 (aa (assoc last-state org-todo-kwd-alist))
10818 (interpret (nth 1 aa))
10819 (head (nth 2 aa))
10820 (whata '(("d" . day) ("m" . month) ("y" . year)))
10821 (msg "Entry repeats: ")
10822 (org-log-done nil)
10823 (org-todo-log-states nil)
10824 (nshiftmax 10) (nshift 0)
10825 re type n what ts time)
10826 (when repeat
10827 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
10828 (org-todo (if (eq interpret 'type) last-state head))
10829 (org-entry-put nil "LAST_REPEAT" (format-time-string
10830 (org-time-stamp-format t t)))
10831 (when org-log-repeat
10832 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
10833 (memq 'org-add-log-note post-command-hook))
10834 ;; OK, we are already setup for some record
10835 (if (eq org-log-repeat 'note)
10836 ;; make sure we take a note, not only a time stamp
10837 (setq org-log-note-how 'note))
10838 ;; Set up for taking a record
10839 (org-add-log-setup 'state (or done-word (car org-done-keywords))
10840 last-state
10841 'findpos org-log-repeat)))
10842 (org-back-to-heading t)
10843 (org-add-planning-info nil nil 'closed)
10844 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
10845 org-deadline-time-regexp "\\)\\|\\("
10846 org-ts-regexp "\\)"))
10847 (while (re-search-forward
10848 re (save-excursion (outline-next-heading) (point)) t)
10849 (setq type (if (match-end 1) org-scheduled-string
10850 (if (match-end 3) org-deadline-string "Plain:"))
10851 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
10852 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
10853 (setq n (string-to-number (match-string 2 ts))
10854 what (match-string 3 ts))
10855 (if (equal what "w") (setq n (* n 7) what "d"))
10856 ;; Preparation, see if we need to modify the start date for the change
10857 (when (match-end 1)
10858 (setq time (save-match-data (org-time-string-to-time ts)))
10859 (cond
10860 ((equal (match-string 1 ts) ".")
10861 ;; Shift starting date to today
10862 (org-timestamp-change
10863 (- (time-to-days (current-time)) (time-to-days time))
10864 'day))
10865 ((equal (match-string 1 ts) "+")
10866 (while (or (= nshift 0)
10867 (<= (time-to-days time) (time-to-days (current-time))))
10868 (when (= (incf nshift) nshiftmax)
10869 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
10870 (error "Abort")))
10871 (org-timestamp-change n (cdr (assoc what whata)))
10872 (org-at-timestamp-p t)
10873 (setq ts (match-string 1))
10874 (setq time (save-match-data (org-time-string-to-time ts))))
10875 (org-timestamp-change (- n) (cdr (assoc what whata)))
10876 ;; rematch, so that we have everything in place for the real shift
10877 (org-at-timestamp-p t)
10878 (setq ts (match-string 1))
10879 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
10880 (org-timestamp-change n (cdr (assoc what whata)))
10881 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
10882 (setq org-log-post-message msg)
10883 (message "%s" msg))))
10885 (defun org-show-todo-tree (arg)
10886 "Make a compact tree which shows all headlines marked with TODO.
10887 The tree will show the lines where the regexp matches, and all higher
10888 headlines above the match.
10889 With a \\[universal-argument] prefix, prompt for a regexp to match.
10890 With a numeric prefix N, construct a sparse tree for the Nth element
10891 of `org-todo-keywords-1'."
10892 (interactive "P")
10893 (let ((case-fold-search nil)
10894 (kwd-re
10895 (cond ((null arg) org-not-done-regexp)
10896 ((equal arg '(4))
10897 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
10898 (mapcar 'list org-todo-keywords-1))))
10899 (concat "\\("
10900 (mapconcat 'identity (org-split-string kwd "|") "\\|")
10901 "\\)\\>")))
10902 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
10903 (regexp-quote (nth (1- (prefix-numeric-value arg))
10904 org-todo-keywords-1)))
10905 (t (error "Invalid prefix argument: %s" arg)))))
10906 (message "%d TODO entries found"
10907 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
10909 (defun org-deadline (&optional remove time)
10910 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
10911 With argument REMOVE, remove any deadline from the item.
10912 When TIME is set, it should be an internal time specification, and the
10913 scheduling will use the corresponding date."
10914 (interactive "P")
10915 (let* ((old-date (org-entry-get nil "DEADLINE"))
10916 (repeater (and old-date
10917 (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
10918 (match-string 1 old-date))))
10919 (if remove
10920 (progn
10921 (when (and old-date org-log-redeadline)
10922 (org-add-log-setup 'deldeadline nil old-date 'findpos
10923 org-log-redeadline))
10924 (org-remove-timestamp-with-keyword org-deadline-string)
10925 (message "Item no longer has a deadline."))
10926 (org-add-planning-info 'deadline time 'closed)
10927 (when (and old-date org-log-redeadline
10928 (not (equal old-date
10929 (substring org-last-inserted-timestamp 1 -1))))
10930 (org-add-log-setup 'redeadline nil old-date 'findpos
10931 org-log-redeadline))
10932 (when repeater
10933 (save-excursion
10934 (org-back-to-heading t)
10935 (when (re-search-forward (concat org-deadline-string " "
10936 org-last-inserted-timestamp)
10937 (save-excursion
10938 (outline-next-heading) (point)) t)
10939 (goto-char (1- (match-end 0)))
10940 (insert " " repeater)
10941 (setq org-last-inserted-timestamp
10942 (concat (substring org-last-inserted-timestamp 0 -1)
10943 " " repeater
10944 (substring org-last-inserted-timestamp -1))))))
10945 (message "Deadline on %s" org-last-inserted-timestamp))))
10947 (defun org-schedule (&optional remove time)
10948 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
10949 With argument REMOVE, remove any scheduling date from the item.
10950 When TIME is set, it should be an internal time specification, and the
10951 scheduling will use the corresponding date."
10952 (interactive "P")
10953 (let* ((old-date (org-entry-get nil "SCHEDULED"))
10954 (repeater (and old-date
10955 (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
10956 (match-string 1 old-date))))
10957 (if remove
10958 (progn
10959 (when (and old-date org-log-reschedule)
10960 (org-add-log-setup 'delschedule nil old-date 'findpos
10961 org-log-reschedule))
10962 (org-remove-timestamp-with-keyword org-scheduled-string)
10963 (message "Item is no longer scheduled."))
10964 (org-add-planning-info 'scheduled time 'closed)
10965 (when (and old-date org-log-reschedule
10966 (not (equal old-date
10967 (substring org-last-inserted-timestamp 1 -1))))
10968 (org-add-log-setup 'reschedule nil old-date 'findpos
10969 org-log-reschedule))
10970 (when repeater
10971 (save-excursion
10972 (org-back-to-heading t)
10973 (when (re-search-forward (concat org-scheduled-string " "
10974 org-last-inserted-timestamp)
10975 (save-excursion
10976 (outline-next-heading) (point)) t)
10977 (goto-char (1- (match-end 0)))
10978 (insert " " repeater)
10979 (setq org-last-inserted-timestamp
10980 (concat (substring org-last-inserted-timestamp 0 -1)
10981 " " repeater
10982 (substring org-last-inserted-timestamp -1))))))
10983 (message "Scheduled to %s" org-last-inserted-timestamp))))
10985 (defun org-get-scheduled-time (pom &optional inherit)
10986 "Get the scheduled time as a time tuple, of a format suitable
10987 for calling org-schedule with, or if there is no scheduling,
10988 returns nil."
10989 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
10990 (when time
10991 (apply 'encode-time (org-parse-time-string time)))))
10993 (defun org-get-deadline-time (pom &optional inherit)
10994 "Get the deadine as a time tuple, of a format suitable for
10995 calling org-deadline with, or if there is no scheduling, returns
10996 nil."
10997 (let ((time (org-entry-get pom "DEADLINE" inherit)))
10998 (when time
10999 (apply 'encode-time (org-parse-time-string time)))))
11001 (defun org-remove-timestamp-with-keyword (keyword)
11002 "Remove all time stamps with KEYWORD in the current entry."
11003 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
11004 beg)
11005 (save-excursion
11006 (org-back-to-heading t)
11007 (setq beg (point))
11008 (outline-next-heading)
11009 (while (re-search-backward re beg t)
11010 (replace-match "")
11011 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
11012 (equal (char-before) ?\ ))
11013 (backward-delete-char 1)
11014 (if (string-match "^[ \t]*$" (buffer-substring
11015 (point-at-bol) (point-at-eol)))
11016 (delete-region (point-at-bol)
11017 (min (point-max) (1+ (point-at-eol))))))))))
11019 (defun org-add-planning-info (what &optional time &rest remove)
11020 "Insert new timestamp with keyword in the line directly after the headline.
11021 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
11022 If non is given, the user is prompted for a date.
11023 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
11024 be removed."
11025 (interactive)
11026 (let (org-time-was-given org-end-time-was-given ts
11027 end default-time default-input)
11029 (catch 'exit
11030 (when (and (not time) (memq what '(scheduled deadline)))
11031 ;; Try to get a default date/time from existing timestamp
11032 (save-excursion
11033 (org-back-to-heading t)
11034 (setq end (save-excursion (outline-next-heading) (point)))
11035 (when (re-search-forward (if (eq what 'scheduled)
11036 org-scheduled-time-regexp
11037 org-deadline-time-regexp)
11038 end t)
11039 (setq ts (match-string 1)
11040 default-time
11041 (apply 'encode-time (org-parse-time-string ts))
11042 default-input (and ts (org-get-compact-tod ts))))))
11043 (when what
11044 ;; If necessary, get the time from the user
11045 (setq time (or time (org-read-date nil 'to-time nil nil
11046 default-time default-input))))
11048 (when (and org-insert-labeled-timestamps-at-point
11049 (member what '(scheduled deadline)))
11050 (insert
11051 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
11052 (org-insert-time-stamp time org-time-was-given
11053 nil nil nil (list org-end-time-was-given))
11054 (setq what nil))
11055 (save-excursion
11056 (save-restriction
11057 (let (col list elt ts buffer-invisibility-spec)
11058 (org-back-to-heading t)
11059 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
11060 (goto-char (match-end 1))
11061 (setq col (current-column))
11062 (goto-char (match-end 0))
11063 (if (eobp) (insert "\n") (forward-char 1))
11064 (when (and (not what)
11065 (not (looking-at
11066 (concat "[ \t]*"
11067 org-keyword-time-not-clock-regexp))))
11068 ;; Nothing to add, nothing to remove...... :-)
11069 (throw 'exit nil))
11070 (if (and (not (looking-at outline-regexp))
11071 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
11072 "[^\r\n]*"))
11073 (not (equal (match-string 1) org-clock-string)))
11074 (narrow-to-region (match-beginning 0) (match-end 0))
11075 (insert-before-markers "\n")
11076 (backward-char 1)
11077 (narrow-to-region (point) (point))
11078 (and org-adapt-indentation (org-indent-to-column col)))
11079 ;; Check if we have to remove something.
11080 (setq list (cons what remove))
11081 (while list
11082 (setq elt (pop list))
11083 (goto-char (point-min))
11084 (when (or (and (eq elt 'scheduled)
11085 (re-search-forward org-scheduled-time-regexp nil t))
11086 (and (eq elt 'deadline)
11087 (re-search-forward org-deadline-time-regexp nil t))
11088 (and (eq elt 'closed)
11089 (re-search-forward org-closed-time-regexp nil t)))
11090 (replace-match "")
11091 (if (looking-at "--+<[^>]+>") (replace-match ""))
11092 (skip-chars-backward " ")
11093 (if (looking-at " +") (replace-match ""))))
11094 (goto-char (point-max))
11095 (and org-adapt-indentation (bolp) (org-indent-to-column col))
11096 (when what
11097 (insert
11098 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
11099 (cond ((eq what 'scheduled) org-scheduled-string)
11100 ((eq what 'deadline) org-deadline-string)
11101 ((eq what 'closed) org-closed-string))
11102 " ")
11103 (setq ts (org-insert-time-stamp
11104 time
11105 (or org-time-was-given
11106 (and (eq what 'closed) org-log-done-with-time))
11107 (eq what 'closed)
11108 nil nil (list org-end-time-was-given)))
11109 (end-of-line 1))
11110 (goto-char (point-min))
11111 (widen)
11112 (if (and (looking-at "[ \t]+\n")
11113 (equal (char-before) ?\n))
11114 (delete-region (1- (point)) (point-at-eol)))
11115 ts))))))
11117 (defvar org-log-note-marker (make-marker))
11118 (defvar org-log-note-purpose nil)
11119 (defvar org-log-note-state nil)
11120 (defvar org-log-note-previous-state nil)
11121 (defvar org-log-note-how nil)
11122 (defvar org-log-note-extra nil)
11123 (defvar org-log-note-window-configuration nil)
11124 (defvar org-log-note-return-to (make-marker))
11125 (defvar org-log-post-message nil
11126 "Message to be displayed after a log note has been stored.
11127 The auto-repeater uses this.")
11129 (defun org-add-note ()
11130 "Add a note to the current entry.
11131 This is done in the same way as adding a state change note."
11132 (interactive)
11133 (org-add-log-setup 'note nil nil 'findpos nil))
11135 (defvar org-property-end-re)
11136 (defun org-add-log-setup (&optional purpose state prev-state
11137 findpos how &optional extra)
11138 "Set up the post command hook to take a note.
11139 If this is about to TODO state change, the new state is expected in STATE.
11140 When FINDPOS is non-nil, find the correct position for the note in
11141 the current entry. If not, assume that it can be inserted at point.
11142 HOW is an indicator what kind of note should be created.
11143 EXTRA is additional text that will be inserted into the notes buffer."
11144 (let* ((org-log-into-drawer (org-log-into-drawer))
11145 (drawer (cond ((stringp org-log-into-drawer)
11146 org-log-into-drawer)
11147 (org-log-into-drawer "LOGBOOK")
11148 (t nil))))
11149 (save-restriction
11150 (save-excursion
11151 (when findpos
11152 (org-back-to-heading t)
11153 (narrow-to-region (point) (save-excursion
11154 (outline-next-heading) (point)))
11155 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
11156 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
11157 "[^\r\n]*\\)?"))
11158 (goto-char (match-end 0))
11159 (cond
11160 (drawer
11161 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
11162 nil t)
11163 (progn
11164 (goto-char (match-end 0))
11165 (or org-log-states-order-reversed
11166 (and (re-search-forward org-property-end-re nil t)
11167 (goto-char (1- (match-beginning 0))))))
11168 (insert "\n:" drawer ":\n:END:")
11169 (beginning-of-line 0)
11170 (org-indent-line-function)
11171 (beginning-of-line 2)
11172 (org-indent-line-function)
11173 (end-of-line 0)))
11174 ((and org-log-state-notes-insert-after-drawers
11175 (save-excursion
11176 (forward-line) (looking-at org-drawer-regexp)))
11177 (forward-line)
11178 (while (looking-at org-drawer-regexp)
11179 (goto-char (match-end 0))
11180 (re-search-forward org-property-end-re (point-max) t)
11181 (forward-line))
11182 (forward-line -1)))
11183 (unless org-log-states-order-reversed
11184 (and (= (char-after) ?\n) (forward-char 1))
11185 (org-skip-over-state-notes)
11186 (skip-chars-backward " \t\n\r")))
11187 (move-marker org-log-note-marker (point))
11188 (setq org-log-note-purpose purpose
11189 org-log-note-state state
11190 org-log-note-previous-state prev-state
11191 org-log-note-how how
11192 org-log-note-extra extra)
11193 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
11195 (defun org-skip-over-state-notes ()
11196 "Skip past the list of State notes in an entry."
11197 (if (looking-at "\n[ \t]*- State") (forward-char 1))
11198 (while (looking-at "[ \t]*- State")
11199 (condition-case nil
11200 (org-next-item)
11201 (error (org-end-of-item)))))
11203 (defun org-add-log-note (&optional purpose)
11204 "Pop up a window for taking a note, and add this note later at point."
11205 (remove-hook 'post-command-hook 'org-add-log-note)
11206 (setq org-log-note-window-configuration (current-window-configuration))
11207 (delete-other-windows)
11208 (move-marker org-log-note-return-to (point))
11209 (switch-to-buffer (marker-buffer org-log-note-marker))
11210 (goto-char org-log-note-marker)
11211 (org-switch-to-buffer-other-window "*Org Note*")
11212 (erase-buffer)
11213 (if (memq org-log-note-how '(time state))
11214 (let (current-prefix-arg) (org-store-log-note))
11215 (let ((org-inhibit-startup t)) (org-mode))
11216 (insert (format "# Insert note for %s.
11217 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
11218 (cond
11219 ((eq org-log-note-purpose 'clock-out) "stopped clock")
11220 ((eq org-log-note-purpose 'done) "closed todo item")
11221 ((eq org-log-note-purpose 'state)
11222 (format "state change from \"%s\" to \"%s\""
11223 (or org-log-note-previous-state "")
11224 (or org-log-note-state "")))
11225 ((eq org-log-note-purpose 'reschedule)
11226 "rescheduling")
11227 ((eq org-log-note-purpose 'delschedule)
11228 "no longer scheduled")
11229 ((eq org-log-note-purpose 'redeadline)
11230 "changing deadline")
11231 ((eq org-log-note-purpose 'deldeadline)
11232 "removing deadline")
11233 ((eq org-log-note-purpose 'refile)
11234 "refiling")
11235 ((eq org-log-note-purpose 'note)
11236 "this entry")
11237 (t (error "This should not happen")))))
11238 (if org-log-note-extra (insert org-log-note-extra))
11239 (org-set-local 'org-finish-function 'org-store-log-note)))
11241 (defvar org-note-abort nil) ; dynamically scoped
11242 (defun org-store-log-note ()
11243 "Finish taking a log note, and insert it to where it belongs."
11244 (let ((txt (buffer-string))
11245 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
11246 lines ind)
11247 (kill-buffer (current-buffer))
11248 (while (string-match "\\`#.*\n[ \t\n]*" txt)
11249 (setq txt (replace-match "" t t txt)))
11250 (if (string-match "\\s-+\\'" txt)
11251 (setq txt (replace-match "" t t txt)))
11252 (setq lines (org-split-string txt "\n"))
11253 (when (and note (string-match "\\S-" note))
11254 (setq note
11255 (org-replace-escapes
11256 note
11257 (list (cons "%u" (user-login-name))
11258 (cons "%U" user-full-name)
11259 (cons "%t" (format-time-string
11260 (org-time-stamp-format 'long 'inactive)
11261 (current-time)))
11262 (cons "%s" (if org-log-note-state
11263 (concat "\"" org-log-note-state "\"")
11264 ""))
11265 (cons "%S" (if org-log-note-previous-state
11266 (concat "\"" org-log-note-previous-state "\"")
11267 "\"\"")))))
11268 (if lines (setq note (concat note " \\\\")))
11269 (push note lines))
11270 (when (or current-prefix-arg org-note-abort)
11271 (when org-log-into-drawer
11272 (org-remove-empty-drawer-at
11273 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
11274 org-log-note-marker))
11275 (setq lines nil))
11276 (when lines
11277 (with-current-buffer (marker-buffer org-log-note-marker)
11278 (save-excursion
11279 (goto-char org-log-note-marker)
11280 (move-marker org-log-note-marker nil)
11281 (end-of-line 1)
11282 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
11283 (insert "- " (pop lines))
11284 (org-indent-line-function)
11285 (beginning-of-line 1)
11286 (looking-at "[ \t]*")
11287 (setq ind (concat (match-string 0) " "))
11288 (end-of-line 1)
11289 (while lines (insert "\n" ind (pop lines)))
11290 (message "Note stored")
11291 (org-back-to-heading t)
11292 (org-cycle-hide-drawers 'children)))))
11293 (set-window-configuration org-log-note-window-configuration)
11294 (with-current-buffer (marker-buffer org-log-note-return-to)
11295 (goto-char org-log-note-return-to))
11296 (move-marker org-log-note-return-to nil)
11297 (and org-log-post-message (message "%s" org-log-post-message)))
11299 (defun org-remove-empty-drawer-at (drawer pos)
11300 "Remove an empty drawer DRAWER at position POS.
11301 POS may also be a marker."
11302 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
11303 (save-excursion
11304 (save-restriction
11305 (widen)
11306 (goto-char pos)
11307 (if (org-in-regexp
11308 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
11309 (replace-match ""))))))
11311 (defun org-sparse-tree (&optional arg)
11312 "Create a sparse tree, prompt for the details.
11313 This command can create sparse trees. You first need to select the type
11314 of match used to create the tree:
11316 t Show entries with a specific TODO keyword.
11317 m Show entries selected by a tags/property match.
11318 p Enter a property name and its value (both with completion on existing
11319 names/values) and show entries with that property.
11320 / Show entries matching a regular expression (`r' can be used as well)
11321 d Show deadlines due within `org-deadline-warning-days'.
11322 b Show deadlines and scheduled items before a date.
11323 a Show deadlines and scheduled items after a date."
11324 (interactive "P")
11325 (let (ans kwd value)
11326 (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
11327 (setq ans (read-char-exclusive))
11328 (cond
11329 ((equal ans ?d)
11330 (call-interactively 'org-check-deadlines))
11331 ((equal ans ?b)
11332 (call-interactively 'org-check-before-date))
11333 ((equal ans ?a)
11334 (call-interactively 'org-check-after-date))
11335 ((equal ans ?t)
11336 (org-show-todo-tree '(4)))
11337 ((member ans '(?T ?m))
11338 (call-interactively 'org-match-sparse-tree))
11339 ((member ans '(?p ?P))
11340 (setq kwd (org-icompleting-read "Property: "
11341 (mapcar 'list (org-buffer-property-keys))))
11342 (setq value (org-icompleting-read "Value: "
11343 (mapcar 'list (org-property-values kwd))))
11344 (unless (string-match "\\`{.*}\\'" value)
11345 (setq value (concat "\"" value "\"")))
11346 (org-match-sparse-tree arg (concat kwd "=" value)))
11347 ((member ans '(?r ?R ?/))
11348 (call-interactively 'org-occur))
11349 (t (error "No such sparse tree command \"%c\"" ans)))))
11351 (defvar org-occur-highlights nil
11352 "List of overlays used for occur matches.")
11353 (make-variable-buffer-local 'org-occur-highlights)
11354 (defvar org-occur-parameters nil
11355 "Parameters of the active org-occur calls.
11356 This is a list, each call to org-occur pushes as cons cell,
11357 containing the regular expression and the callback, onto the list.
11358 The list can contain several entries if `org-occur' has been called
11359 several time with the KEEP-PREVIOUS argument. Otherwise, this list
11360 will only contain one set of parameters. When the highlights are
11361 removed (for example with `C-c C-c', or with the next edit (depending
11362 on `org-remove-highlights-with-change'), this variable is emptied
11363 as well.")
11364 (make-variable-buffer-local 'org-occur-parameters)
11366 (defun org-occur (regexp &optional keep-previous callback)
11367 "Make a compact tree which shows all matches of REGEXP.
11368 The tree will show the lines where the regexp matches, and all higher
11369 headlines above the match. It will also show the heading after the match,
11370 to make sure editing the matching entry is easy.
11371 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
11372 call to `org-occur' will be kept, to allow stacking of calls to this
11373 command.
11374 If CALLBACK is non-nil, it is a function which is called to confirm
11375 that the match should indeed be shown."
11376 (interactive "sRegexp: \nP")
11377 (when (equal regexp "")
11378 (error "Regexp cannot be empty"))
11379 (unless keep-previous
11380 (org-remove-occur-highlights nil nil t))
11381 (push (cons regexp callback) org-occur-parameters)
11382 (let ((cnt 0))
11383 (save-excursion
11384 (goto-char (point-min))
11385 (if (or (not keep-previous) ; do not want to keep
11386 (not org-occur-highlights)) ; no previous matches
11387 ;; hide everything
11388 (org-overview))
11389 (while (re-search-forward regexp nil t)
11390 (when (or (not callback)
11391 (save-match-data (funcall callback)))
11392 (setq cnt (1+ cnt))
11393 (when org-highlight-sparse-tree-matches
11394 (org-highlight-new-match (match-beginning 0) (match-end 0)))
11395 (org-show-context 'occur-tree))))
11396 (when org-remove-highlights-with-change
11397 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
11398 nil 'local))
11399 (unless org-sparse-tree-open-archived-trees
11400 (org-hide-archived-subtrees (point-min) (point-max)))
11401 (run-hooks 'org-occur-hook)
11402 (if (interactive-p)
11403 (message "%d match(es) for regexp %s" cnt regexp))
11404 cnt))
11406 (defun org-show-context (&optional key)
11407 "Make sure point and context and visible.
11408 How much context is shown depends upon the variables
11409 `org-show-hierarchy-above', `org-show-following-heading'. and
11410 `org-show-siblings'."
11411 (let ((heading-p (org-on-heading-p t))
11412 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
11413 (following-p (org-get-alist-option org-show-following-heading key))
11414 (entry-p (org-get-alist-option org-show-entry-below key))
11415 (siblings-p (org-get-alist-option org-show-siblings key)))
11416 (catch 'exit
11417 ;; Show heading or entry text
11418 (if (and heading-p (not entry-p))
11419 (org-flag-heading nil) ; only show the heading
11420 (and (or entry-p (org-invisible-p) (org-invisible-p2))
11421 (org-show-hidden-entry))) ; show entire entry
11422 (when following-p
11423 ;; Show next sibling, or heading below text
11424 (save-excursion
11425 (and (if heading-p (org-goto-sibling) (outline-next-heading))
11426 (org-flag-heading nil))))
11427 (when siblings-p (org-show-siblings))
11428 (when hierarchy-p
11429 ;; show all higher headings, possibly with siblings
11430 (save-excursion
11431 (while (and (condition-case nil
11432 (progn (org-up-heading-all 1) t)
11433 (error nil))
11434 (not (bobp)))
11435 (org-flag-heading nil)
11436 (when siblings-p (org-show-siblings))))))))
11438 (defvar org-reveal-start-hook nil
11439 "Hook run before revealing a location.")
11441 (defun org-reveal (&optional siblings)
11442 "Show current entry, hierarchy above it, and the following headline.
11443 This can be used to show a consistent set of context around locations
11444 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
11445 not t for the search context.
11447 With optional argument SIBLINGS, on each level of the hierarchy all
11448 siblings are shown. This repairs the tree structure to what it would
11449 look like when opened with hierarchical calls to `org-cycle'.
11450 With double optional argument `C-u C-u', go to the parent and show the
11451 entire tree."
11452 (interactive "P")
11453 (run-hooks 'org-reveal-start-hook)
11454 (let ((org-show-hierarchy-above t)
11455 (org-show-following-heading t)
11456 (org-show-siblings (if siblings t org-show-siblings)))
11457 (org-show-context nil))
11458 (when (equal siblings '(16))
11459 (save-excursion
11460 (when (org-up-heading-safe)
11461 (org-show-subtree)
11462 (run-hook-with-args 'org-cycle-hook 'subtree)))))
11464 (defun org-highlight-new-match (beg end)
11465 "Highlight from BEG to END and mark the highlight is an occur headline."
11466 (let ((ov (org-make-overlay beg end)))
11467 (org-overlay-put ov 'face 'secondary-selection)
11468 (push ov org-occur-highlights)))
11470 (defun org-remove-occur-highlights (&optional beg end noremove)
11471 "Remove the occur highlights from the buffer.
11472 BEG and END are ignored. If NOREMOVE is nil, remove this function
11473 from the `before-change-functions' in the current buffer."
11474 (interactive)
11475 (unless org-inhibit-highlight-removal
11476 (mapc 'org-delete-overlay org-occur-highlights)
11477 (setq org-occur-highlights nil)
11478 (setq org-occur-parameters nil)
11479 (unless noremove
11480 (remove-hook 'before-change-functions
11481 'org-remove-occur-highlights 'local))))
11483 ;;;; Priorities
11485 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
11486 "Regular expression matching the priority indicator.")
11488 (defvar org-remove-priority-next-time nil)
11490 (defun org-priority-up ()
11491 "Increase the priority of the current item."
11492 (interactive)
11493 (org-priority 'up))
11495 (defun org-priority-down ()
11496 "Decrease the priority of the current item."
11497 (interactive)
11498 (org-priority 'down))
11500 (defun org-priority (&optional action)
11501 "Change the priority of an item by ARG.
11502 ACTION can be `set', `up', `down', or a character."
11503 (interactive)
11504 (unless org-enable-priority-commands
11505 (error "Priority commands are disabled"))
11506 (setq action (or action 'set))
11507 (let (current new news have remove)
11508 (save-excursion
11509 (org-back-to-heading t)
11510 (if (looking-at org-priority-regexp)
11511 (setq current (string-to-char (match-string 2))
11512 have t)
11513 (setq current org-default-priority))
11514 (cond
11515 ((eq action 'remove)
11516 (setq remove t new ?\ ))
11517 ((or (eq action 'set)
11518 (if (featurep 'xemacs) (characterp action) (integerp action)))
11519 (if (not (eq action 'set))
11520 (setq new action)
11521 (message "Priority %c-%c, SPC to remove: "
11522 org-highest-priority org-lowest-priority)
11523 (setq new (read-char-exclusive)))
11524 (if (and (= (upcase org-highest-priority) org-highest-priority)
11525 (= (upcase org-lowest-priority) org-lowest-priority))
11526 (setq new (upcase new)))
11527 (cond ((equal new ?\ ) (setq remove t))
11528 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
11529 (error "Priority must be between `%c' and `%c'"
11530 org-highest-priority org-lowest-priority))))
11531 ((eq action 'up)
11532 (if (and (not have) (eq last-command this-command))
11533 (setq new org-lowest-priority)
11534 (setq new (if (and org-priority-start-cycle-with-default (not have))
11535 org-default-priority (1- current)))))
11536 ((eq action 'down)
11537 (if (and (not have) (eq last-command this-command))
11538 (setq new org-highest-priority)
11539 (setq new (if (and org-priority-start-cycle-with-default (not have))
11540 org-default-priority (1+ current)))))
11541 (t (error "Invalid action")))
11542 (if (or (< (upcase new) org-highest-priority)
11543 (> (upcase new) org-lowest-priority))
11544 (setq remove t))
11545 (setq news (format "%c" new))
11546 (if have
11547 (if remove
11548 (replace-match "" t t nil 1)
11549 (replace-match news t t nil 2))
11550 (if remove
11551 (error "No priority cookie found in line")
11552 (let ((case-fold-search nil))
11553 (looking-at org-todo-line-regexp))
11554 (if (match-end 2)
11555 (progn
11556 (goto-char (match-end 2))
11557 (insert " [#" news "]"))
11558 (goto-char (match-beginning 3))
11559 (insert "[#" news "] "))))
11560 (org-preserve-lc (org-set-tags nil 'align)))
11561 (if remove
11562 (message "Priority removed")
11563 (message "Priority of current item set to %s" news))))
11565 (defun org-get-priority (s)
11566 "Find priority cookie and return priority."
11567 (save-match-data
11568 (if (not (string-match org-priority-regexp s))
11569 (* 1000 (- org-lowest-priority org-default-priority))
11570 (* 1000 (- org-lowest-priority
11571 (string-to-char (match-string 2 s)))))))
11573 ;;;; Tags
11575 (defvar org-agenda-archives-mode)
11576 (defvar org-map-continue-from nil
11577 "Position from where mapping should continue.
11578 Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
11580 (defvar org-scanner-tags nil
11581 "The current tag list while the tags scanner is running.")
11582 (defvar org-trust-scanner-tags nil
11583 "Should `org-get-tags-at' use the tags fro the scanner.
11584 This is for internal dynamical scoping only.
11585 When this is non-nil, the function `org-get-tags-at' will return the value
11586 of `org-scanner-tags' instead of building the list by itself. This
11587 can lead to large speed-ups when the tags scanner is used in a file with
11588 many entries, and when the list of tags is retrieved, for example to
11589 obtain a list of properties. Building the tags list for each entry in such
11590 a file becomes an N^2 operation - but with this variable set, it scales
11591 as N.")
11593 (defun org-scan-tags (action matcher &optional todo-only)
11594 "Scan headline tags with inheritance and produce output ACTION.
11596 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
11597 or `agenda' to produce an entry list for an agenda view. It can also be
11598 a Lisp form or a function that should be called at each matched headline, in
11599 this case the return value is a list of all return values from these calls.
11601 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
11602 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
11603 only lines with a TODO keyword are included in the output."
11604 (require 'org-agenda)
11605 (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
11606 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
11607 (org-re
11608 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
11609 (props (list 'face 'default
11610 'done-face 'org-agenda-done
11611 'undone-face 'default
11612 'mouse-face 'highlight
11613 'org-not-done-regexp org-not-done-regexp
11614 'org-todo-regexp org-todo-regexp
11615 'help-echo
11616 (format "mouse-2 or RET jump to org file %s"
11617 (abbreviate-file-name
11618 (or (buffer-file-name (buffer-base-buffer))
11619 (buffer-name (buffer-base-buffer)))))))
11620 (case-fold-search nil)
11621 (org-map-continue-from nil)
11622 lspos tags tags-list
11623 (tags-alist (list (cons 0 org-file-tags)))
11624 (llast 0) rtn rtn1 level category i txt
11625 todo marker entry priority)
11626 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
11627 (setq action (list 'lambda nil action)))
11628 (save-excursion
11629 (goto-char (point-min))
11630 (when (eq action 'sparse-tree)
11631 (org-overview)
11632 (org-remove-occur-highlights))
11633 (while (re-search-forward re nil t)
11634 (catch :skip
11635 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
11636 tags (if (match-end 4) (org-match-string-no-properties 4)))
11637 (goto-char (setq lspos (match-beginning 0)))
11638 (setq level (org-reduced-level (funcall outline-level))
11639 category (org-get-category))
11640 (setq i llast llast level)
11641 ;; remove tag lists from same and sublevels
11642 (while (>= i level)
11643 (when (setq entry (assoc i tags-alist))
11644 (setq tags-alist (delete entry tags-alist)))
11645 (setq i (1- i)))
11646 ;; add the next tags
11647 (when tags
11648 (setq tags (org-split-string tags ":")
11649 tags-alist
11650 (cons (cons level tags) tags-alist)))
11651 ;; compile tags for current headline
11652 (setq tags-list
11653 (if org-use-tag-inheritance
11654 (apply 'append (mapcar 'cdr (reverse tags-alist)))
11655 tags)
11656 org-scanner-tags tags-list)
11657 (when org-use-tag-inheritance
11658 (setcdr (car tags-alist)
11659 (mapcar (lambda (x)
11660 (setq x (copy-sequence x))
11661 (org-add-prop-inherited x))
11662 (cdar tags-alist))))
11663 (when (and tags org-use-tag-inheritance
11664 (or (not (eq t org-use-tag-inheritance))
11665 org-tags-exclude-from-inheritance))
11666 ;; selective inheritance, remove uninherited ones
11667 (setcdr (car tags-alist)
11668 (org-remove-uniherited-tags (cdar tags-alist))))
11669 (when (and (or (not todo-only)
11670 (and (member todo org-not-done-keywords)
11671 (or (not org-agenda-tags-todo-honor-ignore-options)
11672 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
11673 (let ((case-fold-search t)) (eval matcher))
11675 (not (member org-archive-tag tags-list))
11676 ;; we have an archive tag, should we use this anyway?
11677 (or (not org-agenda-skip-archived-trees)
11678 (and (eq action 'agenda) org-agenda-archives-mode))))
11679 (unless (eq action 'sparse-tree) (org-agenda-skip))
11681 ;; select this headline
11683 (cond
11684 ((eq action 'sparse-tree)
11685 (and org-highlight-sparse-tree-matches
11686 (org-get-heading) (match-end 0)
11687 (org-highlight-new-match
11688 (match-beginning 0) (match-beginning 1)))
11689 (org-show-context 'tags-tree))
11690 ((eq action 'agenda)
11691 (setq txt (org-format-agenda-item
11693 (concat
11694 (if (eq org-tags-match-list-sublevels 'indented)
11695 (make-string (1- level) ?.) "")
11696 (org-get-heading))
11697 category
11698 tags-list
11700 priority (org-get-priority txt))
11701 (goto-char lspos)
11702 (setq marker (org-agenda-new-marker))
11703 (org-add-props txt props
11704 'org-marker marker 'org-hd-marker marker 'org-category category
11705 'todo-state todo
11706 'priority priority 'type "tagsmatch")
11707 (push txt rtn))
11708 ((functionp action)
11709 (setq org-map-continue-from nil)
11710 (save-excursion
11711 (setq rtn1 (funcall action))
11712 (push rtn1 rtn)))
11713 (t (error "Invalid action")))
11715 ;; if we are to skip sublevels, jump to end of subtree
11716 (unless org-tags-match-list-sublevels
11717 (org-end-of-subtree t)
11718 (backward-char 1))))
11719 ;; Get the correct position from where to continue
11720 (if org-map-continue-from
11721 (goto-char org-map-continue-from)
11722 (and (= (point) lspos) (end-of-line 1)))))
11723 (when (and (eq action 'sparse-tree)
11724 (not org-sparse-tree-open-archived-trees))
11725 (org-hide-archived-subtrees (point-min) (point-max)))
11726 (nreverse rtn)))
11728 (defun org-remove-uniherited-tags (tags)
11729 "Remove all tags that are not inherited from the list TAGS."
11730 (cond
11731 ((eq org-use-tag-inheritance t)
11732 (if org-tags-exclude-from-inheritance
11733 (org-delete-all org-tags-exclude-from-inheritance tags)
11734 tags))
11735 ((not org-use-tag-inheritance) nil)
11736 ((stringp org-use-tag-inheritance)
11737 (delq nil (mapcar
11738 (lambda (x)
11739 (if (and (string-match org-use-tag-inheritance x)
11740 (not (member x org-tags-exclude-from-inheritance)))
11741 x nil))
11742 tags)))
11743 ((listp org-use-tag-inheritance)
11744 (delq nil (mapcar
11745 (lambda (x)
11746 (if (member x org-use-tag-inheritance) x nil))
11747 tags)))))
11749 (defvar todo-only) ;; dynamically scoped
11751 (defun org-match-sparse-tree (&optional todo-only match)
11752 "Create a sparse tree according to tags string MATCH.
11753 MATCH can contain positive and negative selection of tags, like
11754 \"+WORK+URGENT-WITHBOSS\".
11755 If optional argument TODO-ONLY is non-nil, only select lines that are
11756 also TODO lines."
11757 (interactive "P")
11758 (org-prepare-agenda-buffers (list (current-buffer)))
11759 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
11761 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
11763 (defvar org-cached-props nil)
11764 (defun org-cached-entry-get (pom property)
11765 (if (or (eq t org-use-property-inheritance)
11766 (and (stringp org-use-property-inheritance)
11767 (string-match org-use-property-inheritance property))
11768 (and (listp org-use-property-inheritance)
11769 (member property org-use-property-inheritance)))
11770 ;; Caching is not possible, check it directly
11771 (org-entry-get pom property 'inherit)
11772 ;; Get all properties, so that we can do complicated checks easily
11773 (cdr (assoc property (or org-cached-props
11774 (setq org-cached-props
11775 (org-entry-properties pom)))))))
11777 (defun org-global-tags-completion-table (&optional files)
11778 "Return the list of all tags in all agenda buffer/files."
11779 (save-excursion
11780 (org-uniquify
11781 (delq nil
11782 (apply 'append
11783 (mapcar
11784 (lambda (file)
11785 (set-buffer (find-file-noselect file))
11786 (append (org-get-buffer-tags)
11787 (mapcar (lambda (x) (if (stringp (car-safe x))
11788 (list (car-safe x)) nil))
11789 org-tag-alist)))
11790 (if (and files (car files))
11791 files
11792 (org-agenda-files))))))))
11794 (defun org-make-tags-matcher (match)
11795 "Create the TAGS//TODO matcher form for the selection string MATCH."
11796 ;; todo-only is scoped dynamically into this function, and the function
11797 ;; may change it if the matcher asks for it.
11798 (unless match
11799 ;; Get a new match request, with completion
11800 (let ((org-last-tags-completion-table
11801 (org-global-tags-completion-table)))
11802 (setq match (org-completing-read-no-i
11803 "Match: " 'org-tags-completion-function nil nil nil
11804 'org-tags-history))))
11806 ;; Parse the string and create a lisp form
11807 (let ((match0 match)
11808 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
11809 minus tag mm
11810 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
11811 orterms term orlist re-p str-p level-p level-op time-p
11812 prop-p pn pv po cat-p gv rest)
11813 (if (string-match "/+" match)
11814 ;; match contains also a todo-matching request
11815 (progn
11816 (setq tagsmatch (substring match 0 (match-beginning 0))
11817 todomatch (substring match (match-end 0)))
11818 (if (string-match "^!" todomatch)
11819 (setq todo-only t todomatch (substring todomatch 1)))
11820 (if (string-match "^\\s-*$" todomatch)
11821 (setq todomatch nil)))
11822 ;; only matching tags
11823 (setq tagsmatch match todomatch nil))
11825 ;; Make the tags matcher
11826 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
11827 (setq tagsmatcher t)
11828 (setq orterms (org-split-string tagsmatch "|") orlist nil)
11829 (while (setq term (pop orterms))
11830 (while (and (equal (substring term -1) "\\") orterms)
11831 (setq term (concat term "|" (pop orterms)))) ; repair bad split
11832 (while (string-match re term)
11833 (setq rest (substring term (match-end 0))
11834 minus (and (match-end 1)
11835 (equal (match-string 1 term) "-"))
11836 tag (match-string 2 term)
11837 re-p (equal (string-to-char tag) ?{)
11838 level-p (match-end 4)
11839 prop-p (match-end 5)
11840 mm (cond
11841 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
11842 (level-p
11843 (setq level-op (org-op-to-function (match-string 3 term)))
11844 `(,level-op level ,(string-to-number
11845 (match-string 4 term))))
11846 (prop-p
11847 (setq pn (match-string 5 term)
11848 po (match-string 6 term)
11849 pv (match-string 7 term)
11850 cat-p (equal pn "CATEGORY")
11851 re-p (equal (string-to-char pv) ?{)
11852 str-p (equal (string-to-char pv) ?\")
11853 time-p (save-match-data
11854 (string-match "^\"[[<].*[]>]\"$" pv))
11855 pv (if (or re-p str-p) (substring pv 1 -1) pv))
11856 (if time-p (setq pv (org-matcher-time pv)))
11857 (setq po (org-op-to-function po (if time-p 'time str-p)))
11858 (cond
11859 ((equal pn "CATEGORY")
11860 (setq gv '(get-text-property (point) 'org-category)))
11861 ((equal pn "TODO")
11862 (setq gv 'todo))
11864 (setq gv `(org-cached-entry-get nil ,pn))))
11865 (if re-p
11866 (if (eq po 'org<>)
11867 `(not (string-match ,pv (or ,gv "")))
11868 `(string-match ,pv (or ,gv "")))
11869 (if str-p
11870 `(,po (or ,gv "") ,pv)
11871 `(,po (string-to-number (or ,gv ""))
11872 ,(string-to-number pv) ))))
11873 (t `(member ,tag tags-list)))
11874 mm (if minus (list 'not mm) mm)
11875 term rest)
11876 (push mm tagsmatcher))
11877 (push (if (> (length tagsmatcher) 1)
11878 (cons 'and tagsmatcher)
11879 (car tagsmatcher))
11880 orlist)
11881 (setq tagsmatcher nil))
11882 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
11883 (setq tagsmatcher
11884 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
11885 ;; Make the todo matcher
11886 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
11887 (setq todomatcher t)
11888 (setq orterms (org-split-string todomatch "|") orlist nil)
11889 (while (setq term (pop orterms))
11890 (while (string-match re term)
11891 (setq minus (and (match-end 1)
11892 (equal (match-string 1 term) "-"))
11893 kwd (match-string 2 term)
11894 re-p (equal (string-to-char kwd) ?{)
11895 term (substring term (match-end 0))
11896 mm (if re-p
11897 `(string-match ,(substring kwd 1 -1) todo)
11898 (list 'equal 'todo kwd))
11899 mm (if minus (list 'not mm) mm))
11900 (push mm todomatcher))
11901 (push (if (> (length todomatcher) 1)
11902 (cons 'and todomatcher)
11903 (car todomatcher))
11904 orlist)
11905 (setq todomatcher nil))
11906 (setq todomatcher (if (> (length orlist) 1)
11907 (cons 'or orlist) (car orlist))))
11909 ;; Return the string and lisp forms of the matcher
11910 (setq matcher (if todomatcher
11911 (list 'and tagsmatcher todomatcher)
11912 tagsmatcher))
11913 (cons match0 matcher)))
11915 (defun org-op-to-function (op &optional stringp)
11916 "Turn an operator into the appropriate function."
11917 (setq op
11918 (cond
11919 ((equal op "<" ) '(< string< org-time<))
11920 ((equal op ">" ) '(> org-string> org-time>))
11921 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
11922 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
11923 ((member op '("=" "==")) '(= string= org-time=))
11924 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
11925 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
11927 (defun org<> (a b) (not (= a b)))
11928 (defun org-string<= (a b) (or (string= a b) (string< a b)))
11929 (defun org-string>= (a b) (not (string< a b)))
11930 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
11931 (defun org-string<> (a b) (not (string= a b)))
11932 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
11933 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
11934 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
11935 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
11936 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
11937 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
11938 (defun org-2ft (s)
11939 "Convert S to a floating point time.
11940 If S is already a number, just return it. If it is a string, parse
11941 it as a time string and apply `float-time' to it. If S is nil, just return 0."
11942 (cond
11943 ((numberp s) s)
11944 ((stringp s)
11945 (condition-case nil
11946 (float-time (apply 'encode-time (org-parse-time-string s)))
11947 (error 0.)))
11948 (t 0.)))
11950 (defun org-time-today ()
11951 "Time in seconds today at 0:00.
11952 Returns the float number of seconds since the beginning of the
11953 epoch to the beginning of today (00:00)."
11954 (float-time (apply 'encode-time
11955 (append '(0 0 0) (nthcdr 3 (decode-time))))))
11957 (defun org-matcher-time (s)
11958 "Interpret a time comparison value."
11959 (save-match-data
11960 (cond
11961 ((string= s "<now>") (float-time))
11962 ((string= s "<today>") (org-time-today))
11963 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
11964 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
11965 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
11966 (+ (org-time-today)
11967 (* (string-to-number (match-string 1 s))
11968 (cdr (assoc (match-string 2 s)
11969 '(("d" . 86400.0) ("w" . 604800.0)
11970 ("m" . 2678400.0) ("y" . 31557600.0)))))))
11971 (t (org-2ft s)))))
11973 (defun org-match-any-p (re list)
11974 "Does re match any element of list?"
11975 (setq list (mapcar (lambda (x) (string-match re x)) list))
11976 (delq nil list))
11978 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
11979 (defvar org-tags-overlay (org-make-overlay 1 1))
11980 (org-detach-overlay org-tags-overlay)
11982 (defun org-get-local-tags-at (&optional pos)
11983 "Get a list of tags defined in the current headline."
11984 (org-get-tags-at pos 'local))
11986 (defun org-get-local-tags ()
11987 "Get a list of tags defined in the current headline."
11988 (org-get-tags-at nil 'local))
11990 (defun org-get-tags-at (&optional pos local)
11991 "Get a list of all headline tags applicable at POS.
11992 POS defaults to point. If tags are inherited, the list contains
11993 the targets in the same sequence as the headlines appear, i.e.
11994 the tags of the current headline come last.
11995 When LOCAL is non-nil, only return tags from the current headline,
11996 ignore inherited ones."
11997 (interactive)
11998 (if (and org-trust-scanner-tags
11999 (or (not pos) (equal pos (point)))
12000 (not local))
12001 org-scanner-tags
12002 (let (tags ltags lastpos parent)
12003 (save-excursion
12004 (save-restriction
12005 (widen)
12006 (goto-char (or pos (point)))
12007 (save-match-data
12008 (catch 'done
12009 (condition-case nil
12010 (progn
12011 (org-back-to-heading t)
12012 (while (not (equal lastpos (point)))
12013 (setq lastpos (point))
12014 (when (looking-at
12015 (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
12016 (setq ltags (org-split-string
12017 (org-match-string-no-properties 1) ":"))
12018 (when parent
12019 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
12020 (setq tags (append
12021 (if parent
12022 (org-remove-uniherited-tags ltags)
12023 ltags)
12024 tags)))
12025 (or org-use-tag-inheritance (throw 'done t))
12026 (if local (throw 'done t))
12027 (or (org-up-heading-safe) (error nil))
12028 (setq parent t)))
12029 (error nil)))))
12030 (append (org-remove-uniherited-tags org-file-tags) tags)))))
12032 (defun org-add-prop-inherited (s)
12033 (add-text-properties 0 (length s) '(inherited t) s)
12036 (defun org-toggle-tag (tag &optional onoff)
12037 "Toggle the tag TAG for the current line.
12038 If ONOFF is `on' or `off', don't toggle but set to this state."
12039 (let (res current)
12040 (save-excursion
12041 (org-back-to-heading t)
12042 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
12043 (point-at-eol) t)
12044 (progn
12045 (setq current (match-string 1))
12046 (replace-match ""))
12047 (setq current ""))
12048 (setq current (nreverse (org-split-string current ":")))
12049 (cond
12050 ((eq onoff 'on)
12051 (setq res t)
12052 (or (member tag current) (push tag current)))
12053 ((eq onoff 'off)
12054 (or (not (member tag current)) (setq current (delete tag current))))
12055 (t (if (member tag current)
12056 (setq current (delete tag current))
12057 (setq res t)
12058 (push tag current))))
12059 (end-of-line 1)
12060 (if current
12061 (progn
12062 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
12063 (org-set-tags nil t))
12064 (delete-horizontal-space))
12065 (run-hooks 'org-after-tags-change-hook))
12066 res))
12068 (defun org-align-tags-here (to-col)
12069 ;; Assumes that this is a headline
12070 (let ((pos (point)) (col (current-column)) ncol tags-l p)
12071 (beginning-of-line 1)
12072 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
12073 (< pos (match-beginning 2)))
12074 (progn
12075 (setq tags-l (- (match-end 2) (match-beginning 2)))
12076 (goto-char (match-beginning 1))
12077 (insert " ")
12078 (delete-region (point) (1+ (match-beginning 2)))
12079 (setq ncol (max (1+ (current-column))
12080 (1+ col)
12081 (if (> to-col 0)
12082 to-col
12083 (- (abs to-col) tags-l))))
12084 (setq p (point))
12085 (insert (make-string (- ncol (current-column)) ?\ ))
12086 (setq ncol (current-column))
12087 (when indent-tabs-mode (tabify p (point-at-eol)))
12088 (org-move-to-column (min ncol col) t))
12089 (goto-char pos))))
12091 (defun org-set-tags-command (&optional arg just-align)
12092 "Call the set-tags command for the current entry."
12093 (interactive "P")
12094 (if (org-on-heading-p)
12095 (org-set-tags arg just-align)
12096 (save-excursion
12097 (org-back-to-heading t)
12098 (org-set-tags arg just-align))))
12100 (defun org-set-tags-to (data)
12101 "Set the tags of the current entry to DATA, replacing the current tags.
12102 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
12103 If DATA is nil or the empty string, any tags will be removed."
12104 (interactive "sTags: ")
12105 (setq data
12106 (cond
12107 ((eq data nil) "")
12108 ((equal data "") "")
12109 ((stringp data)
12110 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
12111 ":"))
12112 ((listp data)
12113 (concat ":" (mapconcat 'identity data ":") ":"))
12114 (t nil)))
12115 (when data
12116 (save-excursion
12117 (org-back-to-heading t)
12118 (when (looking-at org-complex-heading-regexp)
12119 (if (match-end 5)
12120 (progn
12121 (goto-char (match-beginning 5))
12122 (insert data)
12123 (delete-region (point) (point-at-eol))
12124 (org-set-tags nil 'align))
12125 (goto-char (point-at-eol))
12126 (insert " " data)
12127 (org-set-tags nil 'align)))
12128 (beginning-of-line 1)
12129 (if (looking-at ".*?\\([ \t]+\\)$")
12130 (delete-region (match-beginning 1) (match-end 1))))))
12132 (defun org-set-tags (&optional arg just-align)
12133 "Set the tags for the current headline.
12134 With prefix ARG, realign all tags in headings in the current buffer."
12135 (interactive "P")
12136 (let* ((re (concat "^" outline-regexp))
12137 (current (org-get-tags-string))
12138 (col (current-column))
12139 (org-setting-tags t)
12140 table current-tags inherited-tags ; computed below when needed
12141 tags p0 c0 c1 rpl)
12142 (if arg
12143 (save-excursion
12144 (goto-char (point-min))
12145 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
12146 (while (re-search-forward re nil t)
12147 (org-set-tags nil t)
12148 (end-of-line 1)))
12149 (message "All tags realigned to column %d" org-tags-column))
12150 (if just-align
12151 (setq tags current)
12152 ;; Get a new set of tags from the user
12153 (save-excursion
12154 (setq table (append org-tag-persistent-alist
12155 (or org-tag-alist (org-get-buffer-tags))
12156 (and org-complete-tags-always-offer-all-agenda-tags
12157 (org-global-tags-completion-table (org-agenda-files))))
12158 org-last-tags-completion-table table
12159 current-tags (org-split-string current ":")
12160 inherited-tags (nreverse
12161 (nthcdr (length current-tags)
12162 (nreverse (org-get-tags-at))))
12163 tags
12164 (if (or (eq t org-use-fast-tag-selection)
12165 (and org-use-fast-tag-selection
12166 (delq nil (mapcar 'cdr table))))
12167 (org-fast-tag-selection
12168 current-tags inherited-tags table
12169 (if org-fast-tag-selection-include-todo org-todo-key-alist))
12170 (let ((org-add-colon-after-tag-completion t))
12171 (org-trim
12172 (org-without-partial-completion
12173 (org-icompleting-read "Tags: " 'org-tags-completion-function
12174 nil nil current 'org-tags-history)))))))
12175 (while (string-match "[-+&]+" tags)
12176 ;; No boolean logic, just a list
12177 (setq tags (replace-match ":" t t tags))))
12179 (if org-tags-sort-function
12180 (setq tags (mapconcat 'identity
12181 (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
12182 org-tags-sort-function) ":")))
12184 (if (string-match "\\`[\t ]*\\'" tags)
12185 (setq tags "")
12186 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
12187 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
12189 ;; Insert new tags at the correct column
12190 (beginning-of-line 1)
12191 (cond
12192 ((and (equal current "") (equal tags "")))
12193 ((re-search-forward
12194 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
12195 (point-at-eol) t)
12196 (if (equal tags "")
12197 (setq rpl "")
12198 (goto-char (match-beginning 0))
12199 (setq c0 (current-column) p0 (if (equal (char-before) ?*)
12200 (1+ (point)) (point))
12201 c1 (max (1+ c0) (if (> org-tags-column 0)
12202 org-tags-column
12203 (- (- org-tags-column) (length tags))))
12204 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
12205 (replace-match rpl t t)
12206 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
12207 tags)
12208 (t (error "Tags alignment failed")))
12209 (org-move-to-column col)
12210 (unless just-align
12211 (run-hooks 'org-after-tags-change-hook)))))
12213 (defun org-change-tag-in-region (beg end tag off)
12214 "Add or remove TAG for each entry in the region.
12215 This works in the agenda, and also in an org-mode buffer."
12216 (interactive
12217 (list (region-beginning) (region-end)
12218 (let ((org-last-tags-completion-table
12219 (if (org-mode-p)
12220 (org-get-buffer-tags)
12221 (org-global-tags-completion-table))))
12222 (org-icompleting-read
12223 "Tag: " 'org-tags-completion-function nil nil nil
12224 'org-tags-history))
12225 (progn
12226 (message "[s]et or [r]emove? ")
12227 (equal (read-char-exclusive) ?r))))
12228 (if (fboundp 'deactivate-mark) (deactivate-mark))
12229 (let ((agendap (equal major-mode 'org-agenda-mode))
12230 l1 l2 m buf pos newhead (cnt 0))
12231 (goto-char end)
12232 (setq l2 (1- (org-current-line)))
12233 (goto-char beg)
12234 (setq l1 (org-current-line))
12235 (loop for l from l1 to l2 do
12236 (org-goto-line l)
12237 (setq m (get-text-property (point) 'org-hd-marker))
12238 (when (or (and (org-mode-p) (org-on-heading-p))
12239 (and agendap m))
12240 (setq buf (if agendap (marker-buffer m) (current-buffer))
12241 pos (if agendap m (point)))
12242 (with-current-buffer buf
12243 (save-excursion
12244 (save-restriction
12245 (goto-char pos)
12246 (setq cnt (1+ cnt))
12247 (org-toggle-tag tag (if off 'off 'on))
12248 (setq newhead (org-get-heading)))))
12249 (and agendap (org-agenda-change-all-lines newhead m))))
12250 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
12252 (defun org-tags-completion-function (string predicate &optional flag)
12253 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
12254 (confirm (lambda (x) (stringp (car x)))))
12255 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
12256 (setq s1 (match-string 1 string)
12257 s2 (match-string 2 string))
12258 (setq s1 "" s2 string))
12259 (cond
12260 ((eq flag nil)
12261 ;; try completion
12262 (setq rtn (try-completion s2 ctable confirm))
12263 (if (stringp rtn)
12264 (setq rtn
12265 (concat s1 s2 (substring rtn (length s2))
12266 (if (and org-add-colon-after-tag-completion
12267 (assoc rtn ctable))
12268 ":" ""))))
12269 rtn)
12270 ((eq flag t)
12271 ;; all-completions
12272 (all-completions s2 ctable confirm)
12274 ((eq flag 'lambda)
12275 ;; exact match?
12276 (assoc s2 ctable)))
12279 (defun org-fast-tag-insert (kwd tags face &optional end)
12280 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
12281 (insert (format "%-12s" (concat kwd ":"))
12282 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
12283 (or end "")))
12285 (defun org-fast-tag-show-exit (flag)
12286 (save-excursion
12287 (org-goto-line 3)
12288 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
12289 (replace-match ""))
12290 (when flag
12291 (end-of-line 1)
12292 (org-move-to-column (- (window-width) 19) t)
12293 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
12295 (defun org-set-current-tags-overlay (current prefix)
12296 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
12297 (if (featurep 'xemacs)
12298 (org-overlay-display org-tags-overlay (concat prefix s)
12299 'secondary-selection)
12300 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
12301 (org-overlay-display org-tags-overlay (concat prefix s)))))
12303 (defvar org-last-tag-selection-key nil)
12304 (defun org-fast-tag-selection (current inherited table &optional todo-table)
12305 "Fast tag selection with single keys.
12306 CURRENT is the current list of tags in the headline, INHERITED is the
12307 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
12308 possibly with grouping information. TODO-TABLE is a similar table with
12309 TODO keywords, should these have keys assigned to them.
12310 If the keys are nil, a-z are automatically assigned.
12311 Returns the new tags string, or nil to not change the current settings."
12312 (let* ((fulltable (append table todo-table))
12313 (maxlen (apply 'max (mapcar
12314 (lambda (x)
12315 (if (stringp (car x)) (string-width (car x)) 0))
12316 fulltable)))
12317 (buf (current-buffer))
12318 (expert (eq org-fast-tag-selection-single-key 'expert))
12319 (buffer-tags nil)
12320 (fwidth (+ maxlen 3 1 3))
12321 (ncol (/ (- (window-width) 4) fwidth))
12322 (i-face 'org-done)
12323 (c-face 'org-todo)
12324 tg cnt e c char c1 c2 ntable tbl rtn
12325 ov-start ov-end ov-prefix
12326 (exit-after-next org-fast-tag-selection-single-key)
12327 (done-keywords org-done-keywords)
12328 groups ingroup)
12329 (save-excursion
12330 (beginning-of-line 1)
12331 (if (looking-at
12332 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
12333 (setq ov-start (match-beginning 1)
12334 ov-end (match-end 1)
12335 ov-prefix "")
12336 (setq ov-start (1- (point-at-eol))
12337 ov-end (1+ ov-start))
12338 (skip-chars-forward "^\n\r")
12339 (setq ov-prefix
12340 (concat
12341 (buffer-substring (1- (point)) (point))
12342 (if (> (current-column) org-tags-column)
12344 (make-string (- org-tags-column (current-column)) ?\ ))))))
12345 (org-move-overlay org-tags-overlay ov-start ov-end)
12346 (save-window-excursion
12347 (if expert
12348 (set-buffer (get-buffer-create " *Org tags*"))
12349 (delete-other-windows)
12350 (split-window-vertically)
12351 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
12352 (erase-buffer)
12353 (org-set-local 'org-done-keywords done-keywords)
12354 (org-fast-tag-insert "Inherited" inherited i-face "\n")
12355 (org-fast-tag-insert "Current" current c-face "\n\n")
12356 (org-fast-tag-show-exit exit-after-next)
12357 (org-set-current-tags-overlay current ov-prefix)
12358 (setq tbl fulltable char ?a cnt 0)
12359 (while (setq e (pop tbl))
12360 (cond
12361 ((equal (car e) :startgroup)
12362 (push '() groups) (setq ingroup t)
12363 (when (not (= cnt 0))
12364 (setq cnt 0)
12365 (insert "\n"))
12366 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
12367 ((equal (car e) :endgroup)
12368 (setq ingroup nil cnt 0)
12369 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
12370 ((equal e '(:newline))
12371 (when (not (= cnt 0))
12372 (setq cnt 0)
12373 (insert "\n")
12374 (setq e (car tbl))
12375 (while (equal (car tbl) '(:newline))
12376 (insert "\n")
12377 (setq tbl (cdr tbl)))))
12379 (setq tg (copy-sequence (car e)) c2 nil)
12380 (if (cdr e)
12381 (setq c (cdr e))
12382 ;; automatically assign a character.
12383 (setq c1 (string-to-char
12384 (downcase (substring
12385 tg (if (= (string-to-char tg) ?@) 1 0)))))
12386 (if (or (rassoc c1 ntable) (rassoc c1 table))
12387 (while (or (rassoc char ntable) (rassoc char table))
12388 (setq char (1+ char)))
12389 (setq c2 c1))
12390 (setq c (or c2 char)))
12391 (if ingroup (push tg (car groups)))
12392 (setq tg (org-add-props tg nil 'face
12393 (cond
12394 ((not (assoc tg table))
12395 (org-get-todo-face tg))
12396 ((member tg current) c-face)
12397 ((member tg inherited) i-face)
12398 (t nil))))
12399 (if (and (= cnt 0) (not ingroup)) (insert " "))
12400 (insert "[" c "] " tg (make-string
12401 (- fwidth 4 (length tg)) ?\ ))
12402 (push (cons tg c) ntable)
12403 (when (= (setq cnt (1+ cnt)) ncol)
12404 (insert "\n")
12405 (if ingroup (insert " "))
12406 (setq cnt 0)))))
12407 (setq ntable (nreverse ntable))
12408 (insert "\n")
12409 (goto-char (point-min))
12410 (if (not expert) (org-fit-window-to-buffer))
12411 (setq rtn
12412 (catch 'exit
12413 (while t
12414 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
12415 (if (not groups) "no " "")
12416 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
12417 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
12418 (setq org-last-tag-selection-key c)
12419 (cond
12420 ((= c ?\r) (throw 'exit t))
12421 ((= c ?!)
12422 (setq groups (not groups))
12423 (goto-char (point-min))
12424 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
12425 ((= c ?\C-c)
12426 (if (not expert)
12427 (org-fast-tag-show-exit
12428 (setq exit-after-next (not exit-after-next)))
12429 (setq expert nil)
12430 (delete-other-windows)
12431 (split-window-vertically)
12432 (org-switch-to-buffer-other-window " *Org tags*")
12433 (org-fit-window-to-buffer)))
12434 ((or (= c ?\C-g)
12435 (and (= c ?q) (not (rassoc c ntable))))
12436 (org-detach-overlay org-tags-overlay)
12437 (setq quit-flag t))
12438 ((= c ?\ )
12439 (setq current nil)
12440 (if exit-after-next (setq exit-after-next 'now)))
12441 ((= c ?\t)
12442 (condition-case nil
12443 (setq tg (org-icompleting-read
12444 "Tag: "
12445 (or buffer-tags
12446 (with-current-buffer buf
12447 (org-get-buffer-tags)))))
12448 (quit (setq tg "")))
12449 (when (string-match "\\S-" tg)
12450 (add-to-list 'buffer-tags (list tg))
12451 (if (member tg current)
12452 (setq current (delete tg current))
12453 (push tg current)))
12454 (if exit-after-next (setq exit-after-next 'now)))
12455 ((setq e (rassoc c todo-table) tg (car e))
12456 (with-current-buffer buf
12457 (save-excursion (org-todo tg)))
12458 (if exit-after-next (setq exit-after-next 'now)))
12459 ((setq e (rassoc c ntable) tg (car e))
12460 (if (member tg current)
12461 (setq current (delete tg current))
12462 (loop for g in groups do
12463 (if (member tg g)
12464 (mapc (lambda (x)
12465 (setq current (delete x current)))
12466 g)))
12467 (push tg current))
12468 (if exit-after-next (setq exit-after-next 'now))))
12470 ;; Create a sorted list
12471 (setq current
12472 (sort current
12473 (lambda (a b)
12474 (assoc b (cdr (memq (assoc a ntable) ntable))))))
12475 (if (eq exit-after-next 'now) (throw 'exit t))
12476 (goto-char (point-min))
12477 (beginning-of-line 2)
12478 (delete-region (point) (point-at-eol))
12479 (org-fast-tag-insert "Current" current c-face)
12480 (org-set-current-tags-overlay current ov-prefix)
12481 (while (re-search-forward
12482 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
12483 (setq tg (match-string 1))
12484 (add-text-properties
12485 (match-beginning 1) (match-end 1)
12486 (list 'face
12487 (cond
12488 ((member tg current) c-face)
12489 ((member tg inherited) i-face)
12490 (t (get-text-property (match-beginning 1) 'face))))))
12491 (goto-char (point-min)))))
12492 (org-detach-overlay org-tags-overlay)
12493 (if rtn
12494 (mapconcat 'identity current ":")
12495 nil))))
12497 (defun org-get-tags-string ()
12498 "Get the TAGS string in the current headline."
12499 (unless (org-on-heading-p t)
12500 (error "Not on a heading"))
12501 (save-excursion
12502 (beginning-of-line 1)
12503 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
12504 (org-match-string-no-properties 1)
12505 "")))
12507 (defun org-get-tags ()
12508 "Get the list of tags specified in the current headline."
12509 (org-split-string (org-get-tags-string) ":"))
12511 (defun org-get-buffer-tags ()
12512 "Get a table of all tags used in the buffer, for completion."
12513 (let (tags)
12514 (save-excursion
12515 (goto-char (point-min))
12516 (while (re-search-forward
12517 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
12518 (when (equal (char-after (point-at-bol 0)) ?*)
12519 (mapc (lambda (x) (add-to-list 'tags x))
12520 (org-split-string (org-match-string-no-properties 1) ":")))))
12521 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
12522 (mapcar 'list tags)))
12524 ;;;; The mapping API
12526 ;;;###autoload
12527 (defun org-map-entries (func &optional match scope &rest skip)
12528 "Call FUNC at each headline selected by MATCH in SCOPE.
12530 FUNC is a function or a lisp form. The function will be called without
12531 arguments, with the cursor positioned at the beginning of the headline.
12532 The return values of all calls to the function will be collected and
12533 returned as a list.
12535 The call to FUNC will be wrapped into a save-excursion form, so FUNC
12536 does not need to preserve point. After evaluation, the cursor will be
12537 moved to the end of the line (presumably of the headline of the
12538 processed entry) and search continues from there. Under some
12539 circumstances, this may not produce the wanted results. For example,
12540 if you have removed (e.g. archived) the current (sub)tree it could
12541 mean that the next entry will be skipped entirely. In such cases, you
12542 can specify the position from where search should continue by making
12543 FUNC set the variable `org-map-continue-from' to the desired buffer
12544 position.
12546 MATCH is a tags/property/todo match as it is used in the agenda tags view.
12547 Only headlines that are matched by this query will be considered during
12548 the iteration. When MATCH is nil or t, all headlines will be
12549 visited by the iteration.
12551 SCOPE determines the scope of this command. It can be any of:
12553 nil The current buffer, respecting the restriction if any
12554 tree The subtree started with the entry at point
12555 file The current buffer, without restriction
12556 file-with-archives
12557 The current buffer, and any archives associated with it
12558 agenda All agenda files
12559 agenda-with-archives
12560 All agenda files with any archive files associated with them
12561 \(file1 file2 ...)
12562 If this is a list, all files in the list will be scanned
12564 The remaining args are treated as settings for the skipping facilities of
12565 the scanner. The following items can be given here:
12567 archive skip trees with the archive tag.
12568 comment skip trees with the COMMENT keyword
12569 function or Emacs Lisp form:
12570 will be used as value for `org-agenda-skip-function', so whenever
12571 the function returns t, FUNC will not be called for that
12572 entry and search will continue from the point where the
12573 function leaves it.
12575 If your function needs to retrieve the tags including inherited tags
12576 at the *current* entry, you can use the value of the variable
12577 `org-scanner-tags' which will be much faster than getting the value
12578 with `org-get-tags-at'. If your function gets properties with
12579 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
12580 to t around the call to `org-entry-properties' to get the same speedup.
12581 Note that if your function moves around to retrieve tags and properties at
12582 a *different* entry, you cannot use these techniques."
12583 (let* ((org-agenda-archives-mode nil) ; just to make sure
12584 (org-agenda-skip-archived-trees (memq 'archive skip))
12585 (org-agenda-skip-comment-trees (memq 'comment skip))
12586 (org-agenda-skip-function
12587 (car (org-delete-all '(comment archive) skip)))
12588 (org-tags-match-list-sublevels t)
12589 matcher file res
12590 org-todo-keywords-for-agenda
12591 org-done-keywords-for-agenda
12592 org-todo-keyword-alist-for-agenda
12593 org-drawers-for-agenda
12594 org-tag-alist-for-agenda)
12596 (cond
12597 ((eq match t) (setq matcher t))
12598 ((eq match nil) (setq matcher t))
12599 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
12601 (save-excursion
12602 (save-restriction
12603 (when (eq scope 'tree)
12604 (org-back-to-heading t)
12605 (org-narrow-to-subtree)
12606 (setq scope nil))
12608 (if (not scope)
12609 (progn
12610 (org-prepare-agenda-buffers
12611 (list (buffer-file-name (current-buffer))))
12612 (setq res (org-scan-tags func matcher)))
12613 ;; Get the right scope
12614 (cond
12615 ((and scope (listp scope) (symbolp (car scope)))
12616 (setq scope (eval scope)))
12617 ((eq scope 'agenda)
12618 (setq scope (org-agenda-files t)))
12619 ((eq scope 'agenda-with-archives)
12620 (setq scope (org-agenda-files t))
12621 (setq scope (org-add-archive-files scope)))
12622 ((eq scope 'file)
12623 (setq scope (list (buffer-file-name))))
12624 ((eq scope 'file-with-archives)
12625 (setq scope (org-add-archive-files (list (buffer-file-name))))))
12626 (org-prepare-agenda-buffers scope)
12627 (while (setq file (pop scope))
12628 (with-current-buffer (org-find-base-buffer-visiting file)
12629 (save-excursion
12630 (save-restriction
12631 (widen)
12632 (goto-char (point-min))
12633 (setq res (append res (org-scan-tags func matcher))))))))))
12634 res))
12636 ;;;; Properties
12638 ;;; Setting and retrieving properties
12640 (defconst org-special-properties
12641 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
12642 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
12643 "The special properties valid in Org-mode.
12645 These are properties that are not defined in the property drawer,
12646 but in some other way.")
12648 (defconst org-default-properties
12649 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
12650 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
12651 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
12652 "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
12653 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
12654 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
12655 "Some properties that are used by Org-mode for various purposes.
12656 Being in this list makes sure that they are offered for completion.")
12658 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
12659 "Regular expression matching the first line of a property drawer.")
12661 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
12662 "Regular expression matching the last line of a property drawer.")
12664 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
12665 "Regular expression matching the first line of a property drawer.")
12667 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
12668 "Regular expression matching the first line of a property drawer.")
12670 (defconst org-property-drawer-re
12671 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
12672 org-property-end-re "\\)\n?")
12673 "Matches an entire property drawer.")
12675 (defconst org-clock-drawer-re
12676 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
12677 org-property-end-re "\\)\n?")
12678 "Matches an entire clock drawer.")
12680 (defun org-property-action ()
12681 "Do an action on properties."
12682 (interactive)
12683 (let (c)
12684 (org-at-property-p)
12685 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
12686 (setq c (read-char-exclusive))
12687 (cond
12688 ((equal c ?s)
12689 (call-interactively 'org-set-property))
12690 ((equal c ?d)
12691 (call-interactively 'org-delete-property))
12692 ((equal c ?D)
12693 (call-interactively 'org-delete-property-globally))
12694 ((equal c ?c)
12695 (call-interactively 'org-compute-property-at-point))
12696 (t (error "No such property action %c" c)))))
12698 (defun org-set-effort (&optional value)
12699 "Set the effort property of the current entry.
12700 With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
12701 allowed value."
12702 (interactive "P")
12703 (if (equal value 0) (setq value 10))
12704 (let* ((completion-ignore-case t)
12705 (prop org-effort-property)
12706 (cur (org-entry-get nil prop))
12707 (allowed (org-property-get-allowed-values nil prop 'table))
12708 (existing (mapcar 'list (org-property-values prop)))
12710 (val (cond
12711 ((stringp value) value)
12712 ((and allowed (integerp value))
12713 (or (car (nth (1- value) allowed))
12714 (car (org-last allowed))))
12715 (allowed
12716 (message "Select 1-9,0, [RET%s]: %s"
12717 (if cur (concat "=" cur) "")
12718 (mapconcat 'car allowed " "))
12719 (setq rpl (read-char-exclusive))
12720 (if (equal rpl ?\r)
12722 (setq rpl (- rpl ?0))
12723 (if (equal rpl 0) (setq rpl 10))
12724 (if (and (> rpl 0) (<= rpl (length allowed)))
12725 (car (nth (1- rpl) allowed))
12726 (org-completing-read "Effort: " allowed nil))))
12728 (let (org-completion-use-ido org-completion-use-iswitchb)
12729 (org-completing-read
12730 (concat "Effort " (if (and cur (string-match "\\S-" cur))
12731 (concat "[" cur "]") "")
12732 ": ")
12733 existing nil nil "" nil cur))))))
12734 (unless (equal (org-entry-get nil prop) val)
12735 (org-entry-put nil prop val))
12736 (message "%s is now %s" prop val)))
12738 (defun org-at-property-p ()
12739 "Is cursor inside a property drawer?"
12740 (save-excursion
12741 (beginning-of-line 1)
12742 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
12743 (let ((match (match-data)) ;; Keep match-data for use by calling
12744 (p (point)) ;; procedures.
12745 (range (unless (org-before-first-heading-p)
12746 (org-get-property-block))))
12747 (prog1 (and range (<= (car range) p) (< p (cdr range)))
12748 (set-match-data match))))))
12750 (defun org-get-property-block (&optional beg end force)
12751 "Return the (beg . end) range of the body of the property drawer.
12752 BEG and END can be beginning and end of subtree, if not given
12753 they will be found.
12754 If the drawer does not exist and FORCE is non-nil, create the drawer."
12755 (catch 'exit
12756 (save-excursion
12757 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
12758 (end (or end (progn (outline-next-heading) (point)))))
12759 (goto-char beg)
12760 (if (re-search-forward org-property-start-re end t)
12761 (setq beg (1+ (match-end 0)))
12762 (if force
12763 (save-excursion
12764 (org-insert-property-drawer)
12765 (setq end (progn (outline-next-heading) (point))))
12766 (throw 'exit nil))
12767 (goto-char beg)
12768 (if (re-search-forward org-property-start-re end t)
12769 (setq beg (1+ (match-end 0)))))
12770 (if (re-search-forward org-property-end-re end t)
12771 (setq end (match-beginning 0))
12772 (or force (throw 'exit nil))
12773 (goto-char beg)
12774 (setq end beg)
12775 (org-indent-line-function)
12776 (insert ":END:\n"))
12777 (cons beg end)))))
12779 (defun org-entry-properties (&optional pom which specific)
12780 "Get all properties of the entry at point-or-marker POM.
12781 This includes the TODO keyword, the tags, time strings for deadline,
12782 scheduled, and clocking, and any additional properties defined in the
12783 entry. The return value is an alist, keys may occur multiple times
12784 if the property key was used several times.
12785 POM may also be nil, in which case the current entry is used.
12786 If WHICH is nil or `all', get all properties. If WHICH is
12787 `special' or `standard', only get that subclass. If WHICH
12788 is a string only get exactly this property. Specific can be a string, the
12789 specific property we are interested in. Specifying it can speed
12790 things up because then unnecessary parsing is avoided."
12791 (setq which (or which 'all))
12792 (org-with-point-at pom
12793 (let ((clockstr (substring org-clock-string 0 -1))
12794 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
12795 (case-fold-search nil)
12796 beg end range props sum-props key value string clocksum)
12797 (save-excursion
12798 (when (condition-case nil
12799 (and (org-mode-p) (org-back-to-heading t))
12800 (error nil))
12801 (setq beg (point))
12802 (setq sum-props (get-text-property (point) 'org-summaries))
12803 (setq clocksum (get-text-property (point) :org-clock-minutes))
12804 (outline-next-heading)
12805 (setq end (point))
12806 (when (memq which '(all special))
12807 ;; Get the special properties, like TODO and tags
12808 (goto-char beg)
12809 (when (and (or (not specific) (string= specific "TODO"))
12810 (looking-at org-todo-line-regexp) (match-end 2))
12811 (push (cons "TODO" (org-match-string-no-properties 2)) props))
12812 (when (and (or (not specific) (string= specific "PRIORITY"))
12813 (looking-at org-priority-regexp))
12814 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
12815 (when (and (or (not specific) (string= specific "TAGS"))
12816 (setq value (org-get-tags-string))
12817 (string-match "\\S-" value))
12818 (push (cons "TAGS" value) props))
12819 (when (and (or (not specific) (string= specific "ALLTAGS"))
12820 (setq value (org-get-tags-at)))
12821 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
12822 ":"))
12823 props))
12824 (when (or (not specific) (string= specific "BLOCKED"))
12825 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
12826 (when (or (not specific)
12827 (member specific org-all-time-keywords)
12828 (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
12829 (while (re-search-forward org-maybe-keyword-time-regexp end t)
12830 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
12831 string (if (equal key clockstr)
12832 (org-no-properties
12833 (org-trim
12834 (buffer-substring
12835 (match-beginning 3) (goto-char (point-at-eol)))))
12836 (substring (org-match-string-no-properties 3) 1 -1)))
12837 (unless key
12838 (if (= (char-after (match-beginning 3)) ?\[)
12839 (setq key "TIMESTAMP_IA")
12840 (setq key "TIMESTAMP")))
12841 (when (or (equal key clockstr) (not (assoc key props)))
12842 (push (cons key string) props))))
12846 (when (memq which '(all standard))
12847 ;; Get the standard properties, like :PROP: ...
12848 (setq range (org-get-property-block beg end))
12849 (when range
12850 (goto-char (car range))
12851 (while (re-search-forward
12852 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
12853 (cdr range) t)
12854 (setq key (org-match-string-no-properties 1)
12855 value (org-trim (or (org-match-string-no-properties 2) "")))
12856 (unless (member key excluded)
12857 (push (cons key (or value "")) props)))))
12858 (if clocksum
12859 (push (cons "CLOCKSUM"
12860 (org-columns-number-to-string (/ (float clocksum) 60.)
12861 'add_times))
12862 props))
12863 (unless (assoc "CATEGORY" props)
12864 (setq value (or (org-get-category)
12865 (progn (org-refresh-category-properties)
12866 (org-get-category))))
12867 (push (cons "CATEGORY" value) props))
12868 (append sum-props (nreverse props)))))))
12870 (defun org-entry-get (pom property &optional inherit)
12871 "Get value of PROPERTY for entry at point-or-marker POM.
12872 If INHERIT is non-nil and the entry does not have the property,
12873 then also check higher levels of the hierarchy.
12874 If INHERIT is the symbol `selective', use inheritance only if the setting
12875 in `org-use-property-inheritance' selects PROPERTY for inheritance.
12876 If the property is present but empty, the return value is the empty string.
12877 If the property is not present at all, nil is returned."
12878 (org-with-point-at pom
12879 (if (and inherit (if (eq inherit 'selective)
12880 (org-property-inherit-p property)
12882 (org-entry-get-with-inheritance property)
12883 (if (member property org-special-properties)
12884 ;; We need a special property. Use `org-entry-properties' to
12885 ;; retrieve it, but specify the wanted property
12886 (cdr (assoc property (org-entry-properties nil 'special property)))
12887 (let ((range (org-get-property-block)))
12888 (if (and range
12889 (goto-char (car range))
12890 (re-search-forward
12891 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
12892 (cdr range) t))
12893 ;; Found the property, return it.
12894 (if (match-end 1)
12895 (org-match-string-no-properties 1)
12896 "")))))))
12898 (defun org-property-or-variable-value (var &optional inherit)
12899 "Check if there is a property fixing the value of VAR.
12900 If yes, return this value. If not, return the current value of the variable."
12901 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
12902 (if (and prop (stringp prop) (string-match "\\S-" prop))
12903 (read prop)
12904 (symbol-value var))))
12906 (defun org-entry-delete (pom property)
12907 "Delete the property PROPERTY from entry at point-or-marker POM."
12908 (org-with-point-at pom
12909 (if (member property org-special-properties)
12910 nil ; cannot delete these properties.
12911 (let ((range (org-get-property-block)))
12912 (if (and range
12913 (goto-char (car range))
12914 (re-search-forward
12915 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
12916 (cdr range) t))
12917 (progn
12918 (delete-region (match-beginning 0) (1+ (point-at-eol)))
12920 nil)))))
12922 ;; Multi-values properties are properties that contain multiple values
12923 ;; These values are assumed to be single words, separated by whitespace.
12924 (defun org-entry-add-to-multivalued-property (pom property value)
12925 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
12926 (let* ((old (org-entry-get pom property))
12927 (values (and old (org-split-string old "[ \t]"))))
12928 (setq value (org-entry-protect-space value))
12929 (unless (member value values)
12930 (setq values (cons value values))
12931 (org-entry-put pom property
12932 (mapconcat 'identity values " ")))))
12934 (defun org-entry-remove-from-multivalued-property (pom property value)
12935 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
12936 (let* ((old (org-entry-get pom property))
12937 (values (and old (org-split-string old "[ \t]"))))
12938 (setq value (org-entry-protect-space value))
12939 (when (member value values)
12940 (setq values (delete value values))
12941 (org-entry-put pom property
12942 (mapconcat 'identity values " ")))))
12944 (defun org-entry-member-in-multivalued-property (pom property value)
12945 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
12946 (let* ((old (org-entry-get pom property))
12947 (values (and old (org-split-string old "[ \t]"))))
12948 (setq value (org-entry-protect-space value))
12949 (member value values)))
12951 (defun org-entry-get-multivalued-property (pom property)
12952 "Return a list of values in a multivalued property."
12953 (let* ((value (org-entry-get pom property))
12954 (values (and value (org-split-string value "[ \t]"))))
12955 (mapcar 'org-entry-restore-space values)))
12957 (defun org-entry-put-multivalued-property (pom property &rest values)
12958 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
12959 VALUES should be a list of strings. Spaces will be protected."
12960 (org-entry-put pom property
12961 (mapconcat 'org-entry-protect-space values " "))
12962 (let* ((value (org-entry-get pom property))
12963 (values (and value (org-split-string value "[ \t]"))))
12964 (mapcar 'org-entry-restore-space values)))
12966 (defun org-entry-protect-space (s)
12967 "Protect spaces and newline in string S."
12968 (while (string-match " " s)
12969 (setq s (replace-match "%20" t t s)))
12970 (while (string-match "\n" s)
12971 (setq s (replace-match "%0A" t t s)))
12974 (defun org-entry-restore-space (s)
12975 "Restore spaces and newline in string S."
12976 (while (string-match "%20" s)
12977 (setq s (replace-match " " t t s)))
12978 (while (string-match "%0A" s)
12979 (setq s (replace-match "\n" t t s)))
12982 (defvar org-entry-property-inherited-from (make-marker)
12983 "Marker pointing to the entry from where a property was inherited.
12984 Each call to `org-entry-get-with-inheritance' will set this marker to the
12985 location of the entry where the inheritance search matched. If there was
12986 no match, the marker will point nowhere.
12987 Note that also `org-entry-get' calls this function, if the INHERIT flag
12988 is set.")
12990 (defun org-entry-get-with-inheritance (property)
12991 "Get entry property, and search higher levels if not present."
12992 (move-marker org-entry-property-inherited-from nil)
12993 (let (tmp)
12994 (save-excursion
12995 (save-restriction
12996 (widen)
12997 (catch 'ex
12998 (while t
12999 (when (setq tmp (org-entry-get nil property))
13000 (org-back-to-heading t)
13001 (move-marker org-entry-property-inherited-from (point))
13002 (throw 'ex tmp))
13003 (or (org-up-heading-safe) (throw 'ex nil)))))
13004 (or tmp
13005 (cdr (assoc property org-file-properties))
13006 (cdr (assoc property org-global-properties))
13007 (cdr (assoc property org-global-properties-fixed))))))
13009 (defvar org-property-changed-functions nil
13010 "Hook called when the value of a property has changed.
13011 Each hook function should accept two arguments, the name of the property
13012 and the new value.")
13014 (defun org-entry-put (pom property value)
13015 "Set PROPERTY to VALUE for entry at point-or-marker POM."
13016 (org-with-point-at pom
13017 (org-back-to-heading t)
13018 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
13019 range)
13020 (cond
13021 ((equal property "TODO")
13022 (when (and (stringp value) (string-match "\\S-" value)
13023 (not (member value org-todo-keywords-1)))
13024 (error "\"%s\" is not a valid TODO state" value))
13025 (if (or (not value)
13026 (not (string-match "\\S-" value)))
13027 (setq value 'none))
13028 (org-todo value)
13029 (org-set-tags nil 'align))
13030 ((equal property "PRIORITY")
13031 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
13032 (string-to-char value) ?\ ))
13033 (org-set-tags nil 'align))
13034 ((equal property "SCHEDULED")
13035 (if (re-search-forward org-scheduled-time-regexp end t)
13036 (cond
13037 ((eq value 'earlier) (org-timestamp-change -1 'day))
13038 ((eq value 'later) (org-timestamp-change 1 'day))
13039 (t (call-interactively 'org-schedule)))
13040 (call-interactively 'org-schedule)))
13041 ((equal property "DEADLINE")
13042 (if (re-search-forward org-deadline-time-regexp end t)
13043 (cond
13044 ((eq value 'earlier) (org-timestamp-change -1 'day))
13045 ((eq value 'later) (org-timestamp-change 1 'day))
13046 (t (call-interactively 'org-deadline)))
13047 (call-interactively 'org-deadline)))
13048 ((member property org-special-properties)
13049 (error "The %s property can not yet be set with `org-entry-put'"
13050 property))
13051 (t ; a non-special property
13052 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
13053 (setq range (org-get-property-block beg end 'force))
13054 (goto-char (car range))
13055 (if (re-search-forward
13056 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
13057 (progn
13058 (delete-region (match-beginning 1) (match-end 1))
13059 (goto-char (match-beginning 1)))
13060 (goto-char (cdr range))
13061 (insert "\n")
13062 (backward-char 1)
13063 (org-indent-line-function)
13064 (insert ":" property ":"))
13065 (and value (insert " " value))
13066 (org-indent-line-function)))))
13067 (run-hook-with-args 'org-property-changed-functions property value)))
13069 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
13070 "Get all property keys in the current buffer.
13071 With INCLUDE-SPECIALS, also list the special properties that reflect things
13072 like tags and TODO state.
13073 With INCLUDE-DEFAULTS, also include properties that has special meaning
13074 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
13075 With INCLUDE-COLUMNS, also include property names given in COLUMN
13076 formats in the current buffer."
13077 (let (rtn range cfmt s p)
13078 (save-excursion
13079 (save-restriction
13080 (widen)
13081 (goto-char (point-min))
13082 (while (re-search-forward org-property-start-re nil t)
13083 (setq range (org-get-property-block))
13084 (goto-char (car range))
13085 (while (re-search-forward
13086 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
13087 (cdr range) t)
13088 (add-to-list 'rtn (org-match-string-no-properties 1)))
13089 (outline-next-heading))))
13091 (when include-specials
13092 (setq rtn (append org-special-properties rtn)))
13094 (when include-defaults
13095 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
13096 (add-to-list 'rtn org-effort-property))
13098 (when include-columns
13099 (save-excursion
13100 (save-restriction
13101 (widen)
13102 (goto-char (point-min))
13103 (while (re-search-forward
13104 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
13105 nil t)
13106 (setq cfmt (match-string 2) s 0)
13107 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
13108 cfmt s)
13109 (setq s (match-end 0)
13110 p (match-string 1 cfmt))
13111 (unless (or (equal p "ITEM")
13112 (member p org-special-properties))
13113 (add-to-list 'rtn (match-string 1 cfmt))))))))
13115 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
13117 (defun org-property-values (key)
13118 "Return a list of all values of property KEY."
13119 (save-excursion
13120 (save-restriction
13121 (widen)
13122 (goto-char (point-min))
13123 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
13124 values)
13125 (while (re-search-forward re nil t)
13126 (add-to-list 'values (org-trim (match-string 1))))
13127 (delete "" values)))))
13129 (defun org-insert-property-drawer ()
13130 "Insert a property drawer into the current entry."
13131 (interactive)
13132 (org-back-to-heading t)
13133 (looking-at outline-regexp)
13134 (let ((indent (if org-adapt-indentation
13135 (- (match-end 0)(match-beginning 0))
13137 (beg (point))
13138 (re (concat "^[ \t]*" org-keyword-time-regexp))
13139 end hiddenp)
13140 (outline-next-heading)
13141 (setq end (point))
13142 (goto-char beg)
13143 (while (re-search-forward re end t))
13144 (setq hiddenp (org-invisible-p))
13145 (end-of-line 1)
13146 (and (equal (char-after) ?\n) (forward-char 1))
13147 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
13148 (if (member (match-string 1) '("CLOCK:" ":END:"))
13149 ;; just skip this line
13150 (beginning-of-line 2)
13151 ;; Drawer start, find the end
13152 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
13153 (beginning-of-line 1)))
13154 (org-skip-over-state-notes)
13155 (skip-chars-backward " \t\n\r")
13156 (if (eq (char-before) ?*) (forward-char 1))
13157 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
13158 (beginning-of-line 0)
13159 (org-indent-to-column indent)
13160 (beginning-of-line 2)
13161 (org-indent-to-column indent)
13162 (beginning-of-line 0)
13163 (if hiddenp
13164 (save-excursion
13165 (org-back-to-heading t)
13166 (hide-entry))
13167 (org-flag-drawer t))))
13169 (defun org-set-property (property value)
13170 "In the current entry, set PROPERTY to VALUE.
13171 When called interactively, this will prompt for a property name, offering
13172 completion on existing and default properties. And then it will prompt
13173 for a value, offering completion either on allowed values (via an inherited
13174 xxx_ALL property) or on existing values in other instances of this property
13175 in the current file."
13176 (interactive
13177 (let* ((completion-ignore-case t)
13178 (keys (org-buffer-property-keys nil t t))
13179 (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
13180 (prop (if (member prop0 keys)
13181 prop0
13182 (or (cdr (assoc (downcase prop0)
13183 (mapcar (lambda (x) (cons (downcase x) x))
13184 keys)))
13185 prop0)))
13186 (cur (org-entry-get nil prop))
13187 (prompt (concat prop " value"
13188 (if (and cur (string-match "\\S-" cur))
13189 (concat " [" cur "]") "") ": "))
13190 (allowed (org-property-get-allowed-values nil prop 'table))
13191 (existing (mapcar 'list (org-property-values prop)))
13192 (val (if allowed
13193 (org-completing-read prompt allowed nil
13194 (not (get-text-property 0 'org-unrestricted
13195 (caar allowed))))
13196 (let (org-completion-use-ido org-completion-use-iswitchb)
13197 (org-completing-read prompt existing nil nil "" nil cur)))))
13198 (list prop (if (equal val "") cur val))))
13199 (unless (equal (org-entry-get nil property) value)
13200 (org-entry-put nil property value)))
13202 (defun org-delete-property (property)
13203 "In the current entry, delete PROPERTY."
13204 (interactive
13205 (let* ((completion-ignore-case t)
13206 (prop (org-icompleting-read "Property: " (org-entry-properties nil 'standard))))
13207 (list prop)))
13208 (message "Property %s %s" property
13209 (if (org-entry-delete nil property)
13210 "deleted"
13211 "was not present in the entry")))
13213 (defun org-delete-property-globally (property)
13214 "Remove PROPERTY globally, from all entries."
13215 (interactive
13216 (let* ((completion-ignore-case t)
13217 (prop (org-icompleting-read
13218 "Globally remove property: "
13219 (mapcar 'list (org-buffer-property-keys)))))
13220 (list prop)))
13221 (save-excursion
13222 (save-restriction
13223 (widen)
13224 (goto-char (point-min))
13225 (let ((cnt 0))
13226 (while (re-search-forward
13227 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
13228 nil t)
13229 (setq cnt (1+ cnt))
13230 (replace-match ""))
13231 (message "Property \"%s\" removed from %d entries" property cnt)))))
13233 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
13235 (defun org-compute-property-at-point ()
13236 "Compute the property at point.
13237 This looks for an enclosing column format, extracts the operator and
13238 then applies it to the property in the column format's scope."
13239 (interactive)
13240 (unless (org-at-property-p)
13241 (error "Not at a property"))
13242 (let ((prop (org-match-string-no-properties 2)))
13243 (org-columns-get-format-and-top-level)
13244 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
13245 (error "No operator defined for property %s" prop))
13246 (org-columns-compute prop)))
13248 (defvar org-property-allowed-value-functions nil
13249 "Hook for functions supplying allowed values for a specific property.
13250 The functions must take a single argument, the name of the property, and
13251 return a flat list of allowed values. If \":ETC\" is one of
13252 the values, this means that these values are intended as defaults for
13253 completion, but that other values should be allowed too.
13254 The functions must return nil if they are not responsible for this
13255 property.")
13257 (defun org-property-get-allowed-values (pom property &optional table)
13258 "Get allowed values for the property PROPERTY.
13259 When TABLE is non-nil, return an alist that can directly be used for
13260 completion."
13261 (let (vals)
13262 (cond
13263 ((equal property "TODO")
13264 (setq vals (org-with-point-at pom
13265 (append org-todo-keywords-1 '("")))))
13266 ((equal property "PRIORITY")
13267 (let ((n org-lowest-priority))
13268 (while (>= n org-highest-priority)
13269 (push (char-to-string n) vals)
13270 (setq n (1- n)))))
13271 ((member property org-special-properties))
13272 ((setq vals (run-hook-with-args-until-success
13273 'org-property-allowed-value-functions property)))
13275 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
13276 (when (and vals (string-match "\\S-" vals))
13277 (setq vals (car (read-from-string (concat "(" vals ")"))))
13278 (setq vals (mapcar (lambda (x)
13279 (cond ((stringp x) x)
13280 ((numberp x) (number-to-string x))
13281 ((symbolp x) (symbol-name x))
13282 (t "???")))
13283 vals)))))
13284 (when (member ":ETC" vals)
13285 (setq vals (remove ":ETC" vals))
13286 (org-add-props (car vals) '(org-unrestricted t)))
13287 (if table (mapcar 'list vals) vals)))
13289 (defun org-property-previous-allowed-value (&optional previous)
13290 "Switch to the next allowed value for this property."
13291 (interactive)
13292 (org-property-next-allowed-value t))
13294 (defun org-property-next-allowed-value (&optional previous)
13295 "Switch to the next allowed value for this property."
13296 (interactive)
13297 (unless (org-at-property-p)
13298 (error "Not at a property"))
13299 (let* ((key (match-string 2))
13300 (value (match-string 3))
13301 (allowed (or (org-property-get-allowed-values (point) key)
13302 (and (member value '("[ ]" "[-]" "[X]"))
13303 '("[ ]" "[X]"))))
13304 nval)
13305 (unless allowed
13306 (error "Allowed values for this property have not been defined"))
13307 (if previous (setq allowed (reverse allowed)))
13308 (if (member value allowed)
13309 (setq nval (car (cdr (member value allowed)))))
13310 (setq nval (or nval (car allowed)))
13311 (if (equal nval value)
13312 (error "Only one allowed value for this property"))
13313 (org-at-property-p)
13314 (replace-match (concat " :" key ": " nval) t t)
13315 (org-indent-line-function)
13316 (beginning-of-line 1)
13317 (skip-chars-forward " \t")
13318 (run-hook-with-args 'org-property-changed-functions key nval)))
13320 (defun org-find-entry-with-id (ident)
13321 "Locate the entry that contains the ID property with exact value IDENT.
13322 IDENT can be a string, a symbol or a number, this function will search for
13323 the string representation of it.
13324 Return the position where this entry starts, or nil if there is no such entry."
13325 (interactive "sID: ")
13326 (let ((id (cond
13327 ((stringp ident) ident)
13328 ((symbol-name ident) (symbol-name ident))
13329 ((numberp ident) (number-to-string ident))
13330 (t (error "IDENT %s must be a string, symbol or number" ident))))
13331 (case-fold-search nil))
13332 (save-excursion
13333 (save-restriction
13334 (widen)
13335 (goto-char (point-min))
13336 (when (re-search-forward
13337 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
13338 nil t)
13339 (org-back-to-heading t)
13340 (point))))))
13342 ;;;; Timestamps
13344 (defvar org-last-changed-timestamp nil)
13345 (defvar org-last-inserted-timestamp nil
13346 "The last time stamp inserted with `org-insert-time-stamp'.")
13347 (defvar org-time-was-given) ; dynamically scoped parameter
13348 (defvar org-end-time-was-given) ; dynamically scoped parameter
13349 (defvar org-ts-what) ; dynamically scoped parameter
13351 (defun org-time-stamp (arg &optional inactive)
13352 "Prompt for a date/time and insert a time stamp.
13353 If the user specifies a time like HH:MM, or if this command is called
13354 with a prefix argument, the time stamp will contain date and time.
13355 Otherwise, only the date will be included. All parts of a date not
13356 specified by the user will be filled in from the current date/time.
13357 So if you press just return without typing anything, the time stamp
13358 will represent the current date/time. If there is already a timestamp
13359 at the cursor, it will be modified."
13360 (interactive "P")
13361 (let* ((ts nil)
13362 (default-time
13363 ;; Default time is either today, or, when entering a range,
13364 ;; the range start.
13365 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
13366 (save-excursion
13367 (re-search-backward
13368 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
13369 (- (point) 20) t)))
13370 (apply 'encode-time (org-parse-time-string (match-string 1)))
13371 (current-time)))
13372 (default-input (and ts (org-get-compact-tod ts)))
13373 org-time-was-given org-end-time-was-given time)
13374 (cond
13375 ((and (org-at-timestamp-p t)
13376 (memq last-command '(org-time-stamp org-time-stamp-inactive))
13377 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
13378 (insert "--")
13379 (setq time (let ((this-command this-command))
13380 (org-read-date arg 'totime nil nil
13381 default-time default-input)))
13382 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
13383 ((org-at-timestamp-p t)
13384 (setq time (let ((this-command this-command))
13385 (org-read-date arg 'totime nil nil default-time default-input)))
13386 (when (org-at-timestamp-p t) ; just to get the match data
13387 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
13388 (replace-match "")
13389 (setq org-last-changed-timestamp
13390 (org-insert-time-stamp
13391 time (or org-time-was-given arg)
13392 inactive nil nil (list org-end-time-was-given))))
13393 (message "Timestamp updated"))
13395 (setq time (let ((this-command this-command))
13396 (org-read-date arg 'totime nil nil default-time default-input)))
13397 (org-insert-time-stamp time (or org-time-was-given arg) inactive
13398 nil nil (list org-end-time-was-given))))))
13400 ;; FIXME: can we use this for something else, like computing time differences?
13401 (defun org-get-compact-tod (s)
13402 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
13403 (let* ((t1 (match-string 1 s))
13404 (h1 (string-to-number (match-string 2 s)))
13405 (m1 (string-to-number (match-string 3 s)))
13406 (t2 (and (match-end 4) (match-string 5 s)))
13407 (h2 (and t2 (string-to-number (match-string 6 s))))
13408 (m2 (and t2 (string-to-number (match-string 7 s))))
13409 dh dm)
13410 (if (not t2)
13412 (setq dh (- h2 h1) dm (- m2 m1))
13413 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
13414 (concat t1 "+" (number-to-string dh)
13415 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
13417 (defun org-time-stamp-inactive (&optional arg)
13418 "Insert an inactive time stamp.
13419 An inactive time stamp is enclosed in square brackets instead of angle
13420 brackets. It is inactive in the sense that it does not trigger agenda entries,
13421 does not link to the calendar and cannot be changed with the S-cursor keys.
13422 So these are more for recording a certain time/date."
13423 (interactive "P")
13424 (org-time-stamp arg 'inactive))
13426 (defvar org-date-ovl (org-make-overlay 1 1))
13427 (org-overlay-put org-date-ovl 'face 'org-warning)
13428 (org-detach-overlay org-date-ovl)
13430 (defvar org-ans1) ; dynamically scoped parameter
13431 (defvar org-ans2) ; dynamically scoped parameter
13433 (defvar org-plain-time-of-day-regexp) ; defined below
13435 (defvar org-overriding-default-time nil) ; dynamically scoped
13436 (defvar org-read-date-overlay nil)
13437 (defvar org-dcst nil) ; dynamically scoped
13438 (defvar org-read-date-history nil)
13439 (defvar org-read-date-final-answer nil)
13441 (defun org-read-date (&optional with-time to-time from-string prompt
13442 default-time default-input)
13443 "Read a date, possibly a time, and make things smooth for the user.
13444 The prompt will suggest to enter an ISO date, but you can also enter anything
13445 which will at least partially be understood by `parse-time-string'.
13446 Unrecognized parts of the date will default to the current day, month, year,
13447 hour and minute. If this command is called to replace a timestamp at point,
13448 of to enter the second timestamp of a range, the default time is taken from the
13449 existing stamp. For example,
13450 3-2-5 --> 2003-02-05
13451 feb 15 --> currentyear-02-15
13452 sep 12 9 --> 2009-09-12
13453 12:45 --> today 12:45
13454 22 sept 0:34 --> currentyear-09-22 0:34
13455 12 --> currentyear-currentmonth-12
13456 Fri --> nearest Friday (today or later)
13457 etc.
13459 Furthermore you can specify a relative date by giving, as the *first* thing
13460 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
13461 change in days weeks, months, years.
13462 With a single plus or minus, the date is relative to today. With a double
13463 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
13464 +4d --> four days from today
13465 +4 --> same as above
13466 +2w --> two weeks from today
13467 ++5 --> five days from default date
13469 The function understands only English month and weekday abbreviations,
13470 but this can be configured with the variables `parse-time-months' and
13471 `parse-time-weekdays'.
13473 While prompting, a calendar is popped up - you can also select the
13474 date with the mouse (button 1). The calendar shows a period of three
13475 months. To scroll it to other months, use the keys `>' and `<'.
13476 If you don't like the calendar, turn it off with
13477 \(setq org-read-date-popup-calendar nil)
13479 With optional argument TO-TIME, the date will immediately be converted
13480 to an internal time.
13481 With an optional argument WITH-TIME, the prompt will suggest to also
13482 insert a time. Note that when WITH-TIME is not set, you can still
13483 enter a time, and this function will inform the calling routine about
13484 this change. The calling routine may then choose to change the format
13485 used to insert the time stamp into the buffer to include the time.
13486 With optional argument FROM-STRING, read from this string instead from
13487 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
13488 the time/date that is used for everything that is not specified by the
13489 user."
13490 (require 'parse-time)
13491 (let* ((org-time-stamp-rounding-minutes
13492 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
13493 (org-dcst org-display-custom-times)
13494 (ct (org-current-time))
13495 (def (or org-overriding-default-time default-time ct))
13496 (defdecode (decode-time def))
13497 (dummy (progn
13498 (when (< (nth 2 defdecode) org-extend-today-until)
13499 (setcar (nthcdr 2 defdecode) -1)
13500 (setcar (nthcdr 1 defdecode) 59)
13501 (setq def (apply 'encode-time defdecode)
13502 defdecode (decode-time def)))))
13503 (calendar-frame-setup nil)
13504 (calendar-move-hook nil)
13505 (calendar-view-diary-initially-flag nil)
13506 (view-diary-entries-initially nil)
13507 (calendar-view-holidays-initially-flag nil)
13508 (view-calendar-holidays-initially nil)
13509 (timestr (format-time-string
13510 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
13511 (prompt (concat (if prompt (concat prompt " ") "")
13512 (format "Date+time [%s]: " timestr)))
13513 ans (org-ans0 "") org-ans1 org-ans2 final)
13515 (cond
13516 (from-string (setq ans from-string))
13517 (org-read-date-popup-calendar
13518 (save-excursion
13519 (save-window-excursion
13520 (calendar)
13521 (calendar-forward-day (- (time-to-days def)
13522 (calendar-absolute-from-gregorian
13523 (calendar-current-date))))
13524 (org-eval-in-calendar nil t)
13525 (let* ((old-map (current-local-map))
13526 (map (copy-keymap calendar-mode-map))
13527 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
13528 (org-defkey map (kbd "RET") 'org-calendar-select)
13529 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
13530 'org-calendar-select-mouse)
13531 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
13532 'org-calendar-select-mouse)
13533 (org-defkey minibuffer-local-map [(meta shift left)]
13534 (lambda () (interactive)
13535 (org-eval-in-calendar '(calendar-backward-month 1))))
13536 (org-defkey minibuffer-local-map [(meta shift right)]
13537 (lambda () (interactive)
13538 (org-eval-in-calendar '(calendar-forward-month 1))))
13539 (org-defkey minibuffer-local-map [(meta shift up)]
13540 (lambda () (interactive)
13541 (org-eval-in-calendar '(calendar-backward-year 1))))
13542 (org-defkey minibuffer-local-map [(meta shift down)]
13543 (lambda () (interactive)
13544 (org-eval-in-calendar '(calendar-forward-year 1))))
13545 (org-defkey minibuffer-local-map [?\e (shift left)]
13546 (lambda () (interactive)
13547 (org-eval-in-calendar '(calendar-backward-month 1))))
13548 (org-defkey minibuffer-local-map [?\e (shift right)]
13549 (lambda () (interactive)
13550 (org-eval-in-calendar '(calendar-forward-month 1))))
13551 (org-defkey minibuffer-local-map [?\e (shift up)]
13552 (lambda () (interactive)
13553 (org-eval-in-calendar '(calendar-backward-year 1))))
13554 (org-defkey minibuffer-local-map [?\e (shift down)]
13555 (lambda () (interactive)
13556 (org-eval-in-calendar '(calendar-forward-year 1))))
13557 (org-defkey minibuffer-local-map [(shift up)]
13558 (lambda () (interactive)
13559 (org-eval-in-calendar '(calendar-backward-week 1))))
13560 (org-defkey minibuffer-local-map [(shift down)]
13561 (lambda () (interactive)
13562 (org-eval-in-calendar '(calendar-forward-week 1))))
13563 (org-defkey minibuffer-local-map [(shift left)]
13564 (lambda () (interactive)
13565 (org-eval-in-calendar '(calendar-backward-day 1))))
13566 (org-defkey minibuffer-local-map [(shift right)]
13567 (lambda () (interactive)
13568 (org-eval-in-calendar '(calendar-forward-day 1))))
13569 (org-defkey minibuffer-local-map ">"
13570 (lambda () (interactive)
13571 (org-eval-in-calendar '(scroll-calendar-left 1))))
13572 (org-defkey minibuffer-local-map "<"
13573 (lambda () (interactive)
13574 (org-eval-in-calendar '(scroll-calendar-right 1))))
13575 (run-hooks 'org-read-date-minibuffer-setup-hook)
13576 (unwind-protect
13577 (progn
13578 (use-local-map map)
13579 (add-hook 'post-command-hook 'org-read-date-display)
13580 (setq org-ans0 (read-string prompt default-input
13581 'org-read-date-history nil))
13582 ;; org-ans0: from prompt
13583 ;; org-ans1: from mouse click
13584 ;; org-ans2: from calendar motion
13585 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
13586 (remove-hook 'post-command-hook 'org-read-date-display)
13587 (use-local-map old-map)
13588 (when org-read-date-overlay
13589 (org-delete-overlay org-read-date-overlay)
13590 (setq org-read-date-overlay nil)))))))
13592 (t ; Naked prompt only
13593 (unwind-protect
13594 (setq ans (read-string prompt default-input
13595 'org-read-date-history timestr))
13596 (when org-read-date-overlay
13597 (org-delete-overlay org-read-date-overlay)
13598 (setq org-read-date-overlay nil)))))
13600 (setq final (org-read-date-analyze ans def defdecode))
13601 (setq org-read-date-final-answer ans)
13603 (if to-time
13604 (apply 'encode-time final)
13605 (if (and (boundp 'org-time-was-given) org-time-was-given)
13606 (format "%04d-%02d-%02d %02d:%02d"
13607 (nth 5 final) (nth 4 final) (nth 3 final)
13608 (nth 2 final) (nth 1 final))
13609 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
13611 (defvar def)
13612 (defvar defdecode)
13613 (defvar with-time)
13614 (defvar org-read-date-analyze-futurep nil)
13615 (defun org-read-date-display ()
13616 "Display the current date prompt interpretation in the minibuffer."
13617 (when org-read-date-display-live
13618 (when org-read-date-overlay
13619 (org-delete-overlay org-read-date-overlay))
13620 (let ((p (point)))
13621 (end-of-line 1)
13622 (while (not (equal (buffer-substring
13623 (max (point-min) (- (point) 4)) (point))
13624 " "))
13625 (insert " "))
13626 (goto-char p))
13627 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
13628 " " (or org-ans1 org-ans2)))
13629 (org-end-time-was-given nil)
13630 (f (org-read-date-analyze ans def defdecode))
13631 (fmts (if org-dcst
13632 org-time-stamp-custom-formats
13633 org-time-stamp-formats))
13634 (fmt (if (or with-time
13635 (and (boundp 'org-time-was-given) org-time-was-given))
13636 (cdr fmts)
13637 (car fmts)))
13638 (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
13639 (when (and org-end-time-was-given
13640 (string-match org-plain-time-of-day-regexp txt))
13641 (setq txt (concat (substring txt 0 (match-end 0)) "-"
13642 org-end-time-was-given
13643 (substring txt (match-end 0)))))
13644 (when org-read-date-analyze-futurep
13645 (setq txt (concat txt " (=>F)")))
13646 (setq org-read-date-overlay
13647 (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
13648 (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
13650 (defun org-read-date-analyze (ans def defdecode)
13651 "Analyse the combined answer of the date prompt."
13652 ;; FIXME: cleanup and comment
13653 (let ((nowdecode (decode-time (current-time)))
13654 delta deltan deltaw deltadef year month day
13655 hour minute second wday pm h2 m2 tl wday1
13656 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
13657 (setq org-read-date-analyze-futurep nil)
13658 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
13659 (setq ans "+0"))
13661 (when (setq delta (org-read-date-get-relative ans (current-time) def))
13662 (setq ans (replace-match "" t t ans)
13663 deltan (car delta)
13664 deltaw (nth 1 delta)
13665 deltadef (nth 2 delta)))
13667 ;; Check if there is an iso week date in there
13668 ;; If yes, store the info and postpone interpreting it until the rest
13669 ;; of the parsing is done
13670 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
13671 (setq iso-year (if (match-end 1)
13672 (org-small-year-to-year
13673 (string-to-number (match-string 1 ans))))
13674 iso-weekday (if (match-end 3)
13675 (string-to-number (match-string 3 ans)))
13676 iso-week (string-to-number (match-string 2 ans)))
13677 (setq ans (replace-match "" t t ans)))
13679 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
13680 (when (string-match
13681 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
13682 (setq year (if (match-end 2)
13683 (string-to-number (match-string 2 ans))
13684 (progn (setq kill-year t)
13685 (string-to-number (format-time-string "%Y"))))
13686 month (string-to-number (match-string 3 ans))
13687 day (string-to-number (match-string 4 ans)))
13688 (if (< year 100) (setq year (+ 2000 year)))
13689 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
13690 t nil ans)))
13691 ;; Help matching american dates, like 5/30 or 5/30/7
13692 (when (string-match
13693 "^ *\\([0-3]?[0-9]\\)/\\([0-1]?[0-9]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
13694 (setq year (if (match-end 4)
13695 (string-to-number (match-string 4 ans))
13696 (progn (setq kill-year t)
13697 (string-to-number (format-time-string "%Y"))))
13698 month (string-to-number (match-string 1 ans))
13699 day (string-to-number (match-string 2 ans)))
13700 (if (< year 100) (setq year (+ 2000 year)))
13701 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
13702 t nil ans)))
13703 ;; Help matching am/pm times, because `parse-time-string' does not do that.
13704 ;; If there is a time with am/pm, and *no* time without it, we convert
13705 ;; so that matching will be successful.
13706 (loop for i from 1 to 2 do ; twice, for end time as well
13707 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
13708 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
13709 (setq hour (string-to-number (match-string 1 ans))
13710 minute (if (match-end 3)
13711 (string-to-number (match-string 3 ans))
13713 pm (equal ?p
13714 (string-to-char (downcase (match-string 4 ans)))))
13715 (if (and (= hour 12) (not pm))
13716 (setq hour 0)
13717 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
13718 (setq ans (replace-match (format "%02d:%02d" hour minute)
13719 t t ans))))
13721 ;; Check if a time range is given as a duration
13722 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
13723 (setq hour (string-to-number (match-string 1 ans))
13724 h2 (+ hour (string-to-number (match-string 3 ans)))
13725 minute (string-to-number (match-string 2 ans))
13726 m2 (+ minute (if (match-end 5) (string-to-number
13727 (match-string 5 ans))0)))
13728 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
13729 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
13730 t t ans)))
13732 ;; Check if there is a time range
13733 (when (boundp 'org-end-time-was-given)
13734 (setq org-time-was-given nil)
13735 (when (and (string-match org-plain-time-of-day-regexp ans)
13736 (match-end 8))
13737 (setq org-end-time-was-given (match-string 8 ans))
13738 (setq ans (concat (substring ans 0 (match-beginning 7))
13739 (substring ans (match-end 7))))))
13741 (setq tl (parse-time-string ans)
13742 day (or (nth 3 tl) (nth 3 defdecode))
13743 month (or (nth 4 tl)
13744 (if (and org-read-date-prefer-future
13745 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
13746 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
13747 (nth 4 defdecode)))
13748 year (or (and (not kill-year) (nth 5 tl))
13749 (if (and org-read-date-prefer-future
13750 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
13751 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
13752 (nth 5 defdecode)))
13753 hour (or (nth 2 tl) (nth 2 defdecode))
13754 minute (or (nth 1 tl) (nth 1 defdecode))
13755 second (or (nth 0 tl) 0)
13756 wday (nth 6 tl))
13758 (when (and (eq org-read-date-prefer-future 'time)
13759 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
13760 (equal day (nth 3 nowdecode))
13761 (equal month (nth 4 nowdecode))
13762 (equal year (nth 5 nowdecode))
13763 (nth 2 tl)
13764 (or (< (nth 2 tl) (nth 2 nowdecode))
13765 (and (= (nth 2 tl) (nth 2 nowdecode))
13766 (nth 1 tl)
13767 (< (nth 1 tl) (nth 1 nowdecode)))))
13768 (setq day (1+ day)
13769 futurep t))
13771 ;; Special date definitions below
13772 (cond
13773 (iso-week
13774 ;; There was an iso week
13775 (require 'cal-iso)
13776 (setq futurep nil)
13777 (setq year (or iso-year year)
13778 day (or iso-weekday wday 1)
13779 wday nil ; to make sure that the trigger below does not match
13780 iso-date (calendar-gregorian-from-absolute
13781 (calendar-absolute-from-iso
13782 (list iso-week day year))))
13783 ; FIXME: Should we also push ISO weeks into the future?
13784 ; (when (and org-read-date-prefer-future
13785 ; (not iso-year)
13786 ; (< (calendar-absolute-from-gregorian iso-date)
13787 ; (time-to-days (current-time))))
13788 ; (setq year (1+ year)
13789 ; iso-date (calendar-gregorian-from-absolute
13790 ; (calendar-absolute-from-iso
13791 ; (list iso-week day year)))))
13792 (setq month (car iso-date)
13793 year (nth 2 iso-date)
13794 day (nth 1 iso-date)))
13795 (deltan
13796 (setq futurep nil)
13797 (unless deltadef
13798 (let ((now (decode-time (current-time))))
13799 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
13800 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
13801 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
13802 ((equal deltaw "m") (setq month (+ month deltan)))
13803 ((equal deltaw "y") (setq year (+ year deltan)))))
13804 ((and wday (not (nth 3 tl)))
13805 (setq futurep nil)
13806 ;; Weekday was given, but no day, so pick that day in the week
13807 ;; on or after the derived date.
13808 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
13809 (unless (equal wday wday1)
13810 (setq day (+ day (% (- wday wday1 -7) 7))))))
13811 (if (and (boundp 'org-time-was-given)
13812 (nth 2 tl))
13813 (setq org-time-was-given t))
13814 (if (< year 100) (setq year (+ 2000 year)))
13815 (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
13816 (setq org-read-date-analyze-futurep futurep)
13817 (list second minute hour day month year)))
13819 (defvar parse-time-weekdays)
13821 (defun org-read-date-get-relative (s today default)
13822 "Check string S for special relative date string.
13823 TODAY and DEFAULT are internal times, for today and for a default.
13824 Return shift list (N what def-flag)
13825 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
13826 N is the number of WHATs to shift.
13827 DEF-FLAG is t when a double ++ or -- indicates shift relative to
13828 the DEFAULT date rather than TODAY."
13829 (when (and
13830 (string-match
13831 (concat
13832 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
13833 "\\([0-9]+\\)?"
13834 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
13835 "\\([ \t]\\|$\\)") s)
13836 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
13837 (let* ((dir (if (> (match-end 1) (match-beginning 1))
13838 (string-to-char (substring (match-string 1 s) -1))
13839 ?+))
13840 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
13841 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
13842 (what (if (match-end 3) (match-string 3 s) "d"))
13843 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
13844 (date (if rel default today))
13845 (wday (nth 6 (decode-time date)))
13846 delta)
13847 (if wday1
13848 (progn
13849 (setq delta (mod (+ 7 (- wday1 wday)) 7))
13850 (if (= dir ?-) (setq delta (- delta 7)))
13851 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
13852 (list delta "d" rel))
13853 (list (* n (if (= dir ?-) -1 1)) what rel)))))
13855 (defun org-order-calendar-date-args (arg1 arg2 arg3)
13856 "Turn a user-specified date into the internal representation.
13857 The internal representation needed by the calendar is (month day year).
13858 This is a wrapper to handle the brain-dead convention in calendar that
13859 user function argument order change dependent on argument order."
13860 (if (boundp 'calendar-date-style)
13861 (cond
13862 ((eq calendar-date-style 'american)
13863 (list arg1 arg2 arg3))
13864 ((eq calendar-date-style 'european)
13865 (list arg2 arg1 arg3))
13866 ((eq calendar-date-style 'iso)
13867 (list arg2 arg3 arg1)))
13868 (if (org-bound-and-true-p european-calendar-style)
13869 (list arg2 arg1 arg3)
13870 (list arg1 arg2 arg3))))
13872 (defun org-eval-in-calendar (form &optional keepdate)
13873 "Eval FORM in the calendar window and return to current window.
13874 Also, store the cursor date in variable org-ans2."
13875 (let ((sf (selected-frame))
13876 (sw (selected-window)))
13877 (select-window (get-buffer-window "*Calendar*" t))
13878 (eval form)
13879 (when (and (not keepdate) (calendar-cursor-to-date))
13880 (let* ((date (calendar-cursor-to-date))
13881 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13882 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
13883 (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
13884 (select-window sw)
13885 (org-select-frame-set-input-focus sf)))
13887 (defun org-calendar-select ()
13888 "Return to `org-read-date' with the date currently selected.
13889 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
13890 (interactive)
13891 (when (calendar-cursor-to-date)
13892 (let* ((date (calendar-cursor-to-date))
13893 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13894 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
13895 (if (active-minibuffer-window) (exit-minibuffer))))
13897 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
13898 "Insert a date stamp for the date given by the internal TIME.
13899 WITH-HM means use the stamp format that includes the time of the day.
13900 INACTIVE means use square brackets instead of angular ones, so that the
13901 stamp will not contribute to the agenda.
13902 PRE and POST are optional strings to be inserted before and after the
13903 stamp.
13904 The command returns the inserted time stamp."
13905 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
13906 stamp)
13907 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
13908 (insert-before-markers (or pre ""))
13909 (insert-before-markers (setq stamp (format-time-string fmt time)))
13910 (when (listp extra)
13911 (setq extra (car extra))
13912 (if (and (stringp extra)
13913 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
13914 (setq extra (format "-%02d:%02d"
13915 (string-to-number (match-string 1 extra))
13916 (string-to-number (match-string 2 extra))))
13917 (setq extra nil)))
13918 (when extra
13919 (backward-char 1)
13920 (insert-before-markers extra)
13921 (forward-char 1))
13922 (insert-before-markers (or post ""))
13923 (setq org-last-inserted-timestamp stamp)))
13925 (defun org-toggle-time-stamp-overlays ()
13926 "Toggle the use of custom time stamp formats."
13927 (interactive)
13928 (setq org-display-custom-times (not org-display-custom-times))
13929 (unless org-display-custom-times
13930 (let ((p (point-min)) (bmp (buffer-modified-p)))
13931 (while (setq p (next-single-property-change p 'display))
13932 (if (and (get-text-property p 'display)
13933 (eq (get-text-property p 'face) 'org-date))
13934 (remove-text-properties
13935 p (setq p (next-single-property-change p 'display))
13936 '(display t))))
13937 (set-buffer-modified-p bmp)))
13938 (if (featurep 'xemacs)
13939 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
13940 (org-restart-font-lock)
13941 (setq org-table-may-need-update t)
13942 (if org-display-custom-times
13943 (message "Time stamps are overlayed with custom format")
13944 (message "Time stamp overlays removed")))
13946 (defun org-display-custom-time (beg end)
13947 "Overlay modified time stamp format over timestamp between BEG and END."
13948 (let* ((ts (buffer-substring beg end))
13949 t1 w1 with-hm tf time str w2 (off 0))
13950 (save-match-data
13951 (setq t1 (org-parse-time-string ts t))
13952 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
13953 (setq off (- (match-end 0) (match-beginning 0)))))
13954 (setq end (- end off))
13955 (setq w1 (- end beg)
13956 with-hm (and (nth 1 t1) (nth 2 t1))
13957 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
13958 time (org-fix-decoded-time t1)
13959 str (org-add-props
13960 (format-time-string
13961 (substring tf 1 -1) (apply 'encode-time time))
13962 nil 'mouse-face 'highlight)
13963 w2 (length str))
13964 (if (not (= w2 w1))
13965 (add-text-properties (1+ beg) (+ 2 beg)
13966 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
13967 (if (featurep 'xemacs)
13968 (progn
13969 (put-text-property beg end 'invisible t)
13970 (put-text-property beg end 'end-glyph (make-glyph str)))
13971 (put-text-property beg end 'display str))))
13973 (defun org-translate-time (string)
13974 "Translate all timestamps in STRING to custom format.
13975 But do this only if the variable `org-display-custom-times' is set."
13976 (when org-display-custom-times
13977 (save-match-data
13978 (let* ((start 0)
13979 (re org-ts-regexp-both)
13980 t1 with-hm inactive tf time str beg end)
13981 (while (setq start (string-match re string start))
13982 (setq beg (match-beginning 0)
13983 end (match-end 0)
13984 t1 (save-match-data
13985 (org-parse-time-string (substring string beg end) t))
13986 with-hm (and (nth 1 t1) (nth 2 t1))
13987 inactive (equal (substring string beg (1+ beg)) "[")
13988 tf (funcall (if with-hm 'cdr 'car)
13989 org-time-stamp-custom-formats)
13990 time (org-fix-decoded-time t1)
13991 str (format-time-string
13992 (concat
13993 (if inactive "[" "<") (substring tf 1 -1)
13994 (if inactive "]" ">"))
13995 (apply 'encode-time time))
13996 string (replace-match str t t string)
13997 start (+ start (length str)))))))
13998 string)
14000 (defun org-fix-decoded-time (time)
14001 "Set 0 instead of nil for the first 6 elements of time.
14002 Don't touch the rest."
14003 (let ((n 0))
14004 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
14006 (defun org-days-to-time (timestamp-string)
14007 "Difference between TIMESTAMP-STRING and now in days."
14008 (- (time-to-days (org-time-string-to-time timestamp-string))
14009 (time-to-days (current-time))))
14011 (defun org-deadline-close (timestamp-string &optional ndays)
14012 "Is the time in TIMESTAMP-STRING close to the current date?"
14013 (setq ndays (or ndays (org-get-wdays timestamp-string)))
14014 (and (< (org-days-to-time timestamp-string) ndays)
14015 (not (org-entry-is-done-p))))
14017 (defun org-get-wdays (ts)
14018 "Get the deadline lead time appropriate for timestring TS."
14019 (cond
14020 ((<= org-deadline-warning-days 0)
14021 ;; 0 or negative, enforce this value no matter what
14022 (- org-deadline-warning-days))
14023 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
14024 ;; lead time is specified.
14025 (floor (* (string-to-number (match-string 1 ts))
14026 (cdr (assoc (match-string 2 ts)
14027 '(("d" . 1) ("w" . 7)
14028 ("m" . 30.4) ("y" . 365.25)))))))
14029 ;; go for the default.
14030 (t org-deadline-warning-days)))
14032 (defun org-calendar-select-mouse (ev)
14033 "Return to `org-read-date' with the date currently selected.
14034 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
14035 (interactive "e")
14036 (mouse-set-point ev)
14037 (when (calendar-cursor-to-date)
14038 (let* ((date (calendar-cursor-to-date))
14039 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
14040 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
14041 (if (active-minibuffer-window) (exit-minibuffer))))
14043 (defun org-check-deadlines (ndays)
14044 "Check if there are any deadlines due or past due.
14045 A deadline is considered due if it happens within `org-deadline-warning-days'
14046 days from today's date. If the deadline appears in an entry marked DONE,
14047 it is not shown. The prefix arg NDAYS can be used to test that many
14048 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
14049 (interactive "P")
14050 (let* ((org-warn-days
14051 (cond
14052 ((equal ndays '(4)) 100000)
14053 (ndays (prefix-numeric-value ndays))
14054 (t (abs org-deadline-warning-days))))
14055 (case-fold-search nil)
14056 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
14057 (callback
14058 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
14060 (message "%d deadlines past-due or due within %d days"
14061 (org-occur regexp nil callback)
14062 org-warn-days)))
14064 (defun org-check-before-date (date)
14065 "Check if there are deadlines or scheduled entries before DATE."
14066 (interactive (list (org-read-date)))
14067 (let ((case-fold-search nil)
14068 (regexp (concat "\\<\\(" org-deadline-string
14069 "\\|" org-scheduled-string
14070 "\\) *<\\([^>]+\\)>"))
14071 (callback
14072 (lambda () (time-less-p
14073 (org-time-string-to-time (match-string 2))
14074 (org-time-string-to-time date)))))
14075 (message "%d entries before %s"
14076 (org-occur regexp nil callback) date)))
14078 (defun org-check-after-date (date)
14079 "Check if there are deadlines or scheduled entries after DATE."
14080 (interactive (list (org-read-date)))
14081 (let ((case-fold-search nil)
14082 (regexp (concat "\\<\\(" org-deadline-string
14083 "\\|" org-scheduled-string
14084 "\\) *<\\([^>]+\\)>"))
14085 (callback
14086 (lambda () (not
14087 (time-less-p
14088 (org-time-string-to-time (match-string 2))
14089 (org-time-string-to-time date))))))
14090 (message "%d entries after %s"
14091 (org-occur regexp nil callback) date)))
14093 (defun org-evaluate-time-range (&optional to-buffer)
14094 "Evaluate a time range by computing the difference between start and end.
14095 Normally the result is just printed in the echo area, but with prefix arg
14096 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
14097 If the time range is actually in a table, the result is inserted into the
14098 next column.
14099 For time difference computation, a year is assumed to be exactly 365
14100 days in order to avoid rounding problems."
14101 (interactive "P")
14103 (org-clock-update-time-maybe)
14104 (save-excursion
14105 (unless (org-at-date-range-p t)
14106 (goto-char (point-at-bol))
14107 (re-search-forward org-tr-regexp-both (point-at-eol) t))
14108 (if (not (org-at-date-range-p t))
14109 (error "Not at a time-stamp range, and none found in current line")))
14110 (let* ((ts1 (match-string 1))
14111 (ts2 (match-string 2))
14112 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
14113 (match-end (match-end 0))
14114 (time1 (org-time-string-to-time ts1))
14115 (time2 (org-time-string-to-time ts2))
14116 (t1 (org-float-time time1))
14117 (t2 (org-float-time time2))
14118 (diff (abs (- t2 t1)))
14119 (negative (< (- t2 t1) 0))
14120 ;; (ys (floor (* 365 24 60 60)))
14121 (ds (* 24 60 60))
14122 (hs (* 60 60))
14123 (fy "%dy %dd %02d:%02d")
14124 (fy1 "%dy %dd")
14125 (fd "%dd %02d:%02d")
14126 (fd1 "%dd")
14127 (fh "%02d:%02d")
14128 y d h m align)
14129 (if havetime
14130 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
14132 d (floor (/ diff ds)) diff (mod diff ds)
14133 h (floor (/ diff hs)) diff (mod diff hs)
14134 m (floor (/ diff 60)))
14135 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
14137 d (floor (+ (/ diff ds) 0.5))
14138 h 0 m 0))
14139 (if (not to-buffer)
14140 (message "%s" (org-make-tdiff-string y d h m))
14141 (if (org-at-table-p)
14142 (progn
14143 (goto-char match-end)
14144 (setq align t)
14145 (and (looking-at " *|") (goto-char (match-end 0))))
14146 (goto-char match-end))
14147 (if (looking-at
14148 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
14149 (replace-match ""))
14150 (if negative (insert " -"))
14151 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
14152 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
14153 (insert " " (format fh h m))))
14154 (if align (org-table-align))
14155 (message "Time difference inserted")))))
14157 (defun org-make-tdiff-string (y d h m)
14158 (let ((fmt "")
14159 (l nil))
14160 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
14161 l (push y l)))
14162 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
14163 l (push d l)))
14164 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
14165 l (push h l)))
14166 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
14167 l (push m l)))
14168 (apply 'format fmt (nreverse l))))
14170 (defun org-time-string-to-time (s)
14171 (apply 'encode-time (org-parse-time-string s)))
14172 (defun org-time-string-to-seconds (s)
14173 (org-float-time (org-time-string-to-time s)))
14175 (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
14176 "Convert a time stamp to an absolute day number.
14177 If there is a specifyer for a cyclic time stamp, get the closest date to
14178 DAYNR.
14179 PREFER and SHOW-ALL are passed through to `org-closest-date'.
14180 the variable date is bound by the calendar when this is called."
14181 (cond
14182 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
14183 (if (org-diary-sexp-entry (match-string 1 s) "" date)
14184 daynr
14185 (+ daynr 1000)))
14186 ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
14187 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
14188 (time-to-days (current-time))) (match-string 0 s)
14189 prefer show-all))
14190 (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
14192 (defun org-days-to-iso-week (days)
14193 "Return the iso week number."
14194 (require 'cal-iso)
14195 (car (calendar-iso-from-absolute days)))
14197 (defun org-small-year-to-year (year)
14198 "Convert 2-digit years into 4-digit years.
14199 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
14200 The year 2000 cannot be abbreviated. Any year larger than 99
14201 is returned unchanged."
14202 (if (< year 38)
14203 (setq year (+ 2000 year))
14204 (if (< year 100)
14205 (setq year (+ 1900 year))))
14206 year)
14208 (defun org-time-from-absolute (d)
14209 "Return the time corresponding to date D.
14210 D may be an absolute day number, or a calendar-type list (month day year)."
14211 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
14212 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
14214 (defun org-calendar-holiday ()
14215 "List of holidays, for Diary display in Org-mode."
14216 (require 'holidays)
14217 (let ((hl (funcall
14218 (if (fboundp 'calendar-check-holidays)
14219 'calendar-check-holidays 'check-calendar-holidays) date)))
14220 (if hl (mapconcat 'identity hl "; "))))
14222 (defun org-diary-sexp-entry (sexp entry date)
14223 "Process a SEXP diary ENTRY for DATE."
14224 (require 'diary-lib)
14225 (let ((result (if calendar-debug-sexp
14226 (let ((stack-trace-on-error t))
14227 (eval (car (read-from-string sexp))))
14228 (condition-case nil
14229 (eval (car (read-from-string sexp)))
14230 (error
14231 (beep)
14232 (message "Bad sexp at line %d in %s: %s"
14233 (org-current-line)
14234 (buffer-file-name) sexp)
14235 (sleep-for 2))))))
14236 (cond ((stringp result) result)
14237 ((and (consp result)
14238 (stringp (cdr result))) (cdr result))
14239 (result entry)
14240 (t nil))))
14242 (defun org-diary-to-ical-string (frombuf)
14243 "Get iCalendar entries from diary entries in buffer FROMBUF.
14244 This uses the icalendar.el library."
14245 (let* ((tmpdir (if (featurep 'xemacs)
14246 (temp-directory)
14247 temporary-file-directory))
14248 (tmpfile (make-temp-name
14249 (expand-file-name "orgics" tmpdir)))
14250 buf rtn b e)
14251 (with-current-buffer frombuf
14252 (icalendar-export-region (point-min) (point-max) tmpfile)
14253 (setq buf (find-buffer-visiting tmpfile))
14254 (set-buffer buf)
14255 (goto-char (point-min))
14256 (if (re-search-forward "^BEGIN:VEVENT" nil t)
14257 (setq b (match-beginning 0)))
14258 (goto-char (point-max))
14259 (if (re-search-backward "^END:VEVENT" nil t)
14260 (setq e (match-end 0)))
14261 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
14262 (kill-buffer buf)
14263 (delete-file tmpfile)
14264 rtn))
14266 (defun org-closest-date (start current change prefer show-all)
14267 "Find the date closest to CURRENT that is consistent with START and CHANGE.
14268 When PREFER is `past' return a date that is either CURRENT or past.
14269 When PREFER is `future', return a date that is either CURRENT or future.
14270 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
14271 ;; Make the proper lists from the dates
14272 (catch 'exit
14273 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
14274 dn dw sday cday n1 n2 n0
14275 d m y y1 y2 date1 date2 nmonths nm ny m2)
14277 (setq start (org-date-to-gregorian start)
14278 current (org-date-to-gregorian
14279 (if show-all
14280 current
14281 (time-to-days (current-time))))
14282 sday (calendar-absolute-from-gregorian start)
14283 cday (calendar-absolute-from-gregorian current))
14285 (if (<= cday sday) (throw 'exit sday))
14287 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
14288 (setq dn (string-to-number (match-string 1 change))
14289 dw (cdr (assoc (match-string 2 change) a1)))
14290 (error "Invalid change specifyer: %s" change))
14291 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
14292 (cond
14293 ((eq dw 'day)
14294 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
14295 n2 (+ n1 dn)))
14296 ((eq dw 'year)
14297 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
14298 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
14299 (setq date1 (list m d y1)
14300 n1 (calendar-absolute-from-gregorian date1)
14301 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
14302 n2 (calendar-absolute-from-gregorian date2)))
14303 ((eq dw 'month)
14304 ;; approx number of month between the two dates
14305 (setq nmonths (floor (/ (- cday sday) 30.436875)))
14306 ;; How often does dn fit in there?
14307 (setq d (nth 1 start) m (car start) y (nth 2 start)
14308 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
14309 m (+ m nm)
14310 ny (floor (/ m 12))
14311 y (+ y ny)
14312 m (- m (* ny 12)))
14313 (while (> m 12) (setq m (- m 12) y (1+ y)))
14314 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
14315 (setq m2 (+ m dn) y2 y)
14316 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
14317 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
14318 (while (<= n2 cday)
14319 (setq n1 n2 m m2 y y2)
14320 (setq m2 (+ m dn) y2 y)
14321 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
14322 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
14323 ;; Make sure n1 is the earlier date
14324 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
14325 (if show-all
14326 (cond
14327 ((eq prefer 'past) (if (= cday n2) n2 n1))
14328 ((eq prefer 'future) (if (= cday n1) n1 n2))
14329 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
14330 (cond
14331 ((eq prefer 'past) (if (= cday n2) n2 n1))
14332 ((eq prefer 'future) (if (= cday n1) n1 n2))
14333 (t (if (= cday n1) n1 n2)))))))
14335 (defun org-date-to-gregorian (date)
14336 "Turn any specification of DATE into a gregorian date for the calendar."
14337 (cond ((integerp date) (calendar-gregorian-from-absolute date))
14338 ((and (listp date) (= (length date) 3)) date)
14339 ((stringp date)
14340 (setq date (org-parse-time-string date))
14341 (list (nth 4 date) (nth 3 date) (nth 5 date)))
14342 ((listp date)
14343 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
14345 (defun org-parse-time-string (s &optional nodefault)
14346 "Parse the standard Org-mode time string.
14347 This should be a lot faster than the normal `parse-time-string'.
14348 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
14349 hour and minute fields will be nil if not given."
14350 (if (string-match org-ts-regexp0 s)
14351 (list 0
14352 (if (or (match-beginning 8) (not nodefault))
14353 (string-to-number (or (match-string 8 s) "0")))
14354 (if (or (match-beginning 7) (not nodefault))
14355 (string-to-number (or (match-string 7 s) "0")))
14356 (string-to-number (match-string 4 s))
14357 (string-to-number (match-string 3 s))
14358 (string-to-number (match-string 2 s))
14359 nil nil nil)
14360 (error "Not a standard Org-mode time string: %s" s)))
14362 (defun org-timestamp-up (&optional arg)
14363 "Increase the date item at the cursor by one.
14364 If the cursor is on the year, change the year. If it is on the month or
14365 the day, change that.
14366 With prefix ARG, change by that many units."
14367 (interactive "p")
14368 (org-timestamp-change (prefix-numeric-value arg)))
14370 (defun org-timestamp-down (&optional arg)
14371 "Decrease the date item at the cursor by one.
14372 If the cursor is on the year, change the year. If it is on the month or
14373 the day, change that.
14374 With prefix ARG, change by that many units."
14375 (interactive "p")
14376 (org-timestamp-change (- (prefix-numeric-value arg))))
14378 (defun org-timestamp-up-day (&optional arg)
14379 "Increase the date in the time stamp by one day.
14380 With prefix ARG, change that many days."
14381 (interactive "p")
14382 (if (and (not (org-at-timestamp-p t))
14383 (org-on-heading-p))
14384 (org-todo 'up)
14385 (org-timestamp-change (prefix-numeric-value arg) 'day)))
14387 (defun org-timestamp-down-day (&optional arg)
14388 "Decrease the date in the time stamp by one day.
14389 With prefix ARG, change that many days."
14390 (interactive "p")
14391 (if (and (not (org-at-timestamp-p t))
14392 (org-on-heading-p))
14393 (org-todo 'down)
14394 (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
14396 (defun org-at-timestamp-p (&optional inactive-ok)
14397 "Determine if the cursor is in or at a timestamp."
14398 (interactive)
14399 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
14400 (pos (point))
14401 (ans (or (looking-at tsr)
14402 (save-excursion
14403 (skip-chars-backward "^[<\n\r\t")
14404 (if (> (point) (point-min)) (backward-char 1))
14405 (and (looking-at tsr)
14406 (> (- (match-end 0) pos) -1))))))
14407 (and ans
14408 (boundp 'org-ts-what)
14409 (setq org-ts-what
14410 (cond
14411 ((= pos (match-beginning 0)) 'bracket)
14412 ((= pos (1- (match-end 0))) 'bracket)
14413 ((org-pos-in-match-range pos 2) 'year)
14414 ((org-pos-in-match-range pos 3) 'month)
14415 ((org-pos-in-match-range pos 7) 'hour)
14416 ((org-pos-in-match-range pos 8) 'minute)
14417 ((or (org-pos-in-match-range pos 4)
14418 (org-pos-in-match-range pos 5)) 'day)
14419 ((and (> pos (or (match-end 8) (match-end 5)))
14420 (< pos (match-end 0)))
14421 (- pos (or (match-end 8) (match-end 5))))
14422 (t 'day))))
14423 ans))
14425 (defun org-toggle-timestamp-type ()
14426 "Toggle the type (<active> or [inactive]) of a time stamp."
14427 (interactive)
14428 (when (org-at-timestamp-p t)
14429 (let ((beg (match-beginning 0)) (end (match-end 0))
14430 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
14431 (save-excursion
14432 (goto-char beg)
14433 (while (re-search-forward "[][<>]" end t)
14434 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
14435 t t)))
14436 (message "Timestamp is now %sactive"
14437 (if (equal (char-after beg) ?<) "" "in")))))
14439 (defun org-timestamp-change (n &optional what)
14440 "Change the date in the time stamp at point.
14441 The date will be changed by N times WHAT. WHAT can be `day', `month',
14442 `year', `minute', `second'. If WHAT is not given, the cursor position
14443 in the timestamp determines what will be changed."
14444 (let ((pos (point))
14445 with-hm inactive
14446 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
14447 org-ts-what
14448 extra rem
14449 ts time time0)
14450 (if (not (org-at-timestamp-p t))
14451 (error "Not at a timestamp"))
14452 (if (and (not what) (eq org-ts-what 'bracket))
14453 (org-toggle-timestamp-type)
14454 (if (and (not what) (not (eq org-ts-what 'day))
14455 org-display-custom-times
14456 (get-text-property (point) 'display)
14457 (not (get-text-property (1- (point)) 'display)))
14458 (setq org-ts-what 'day))
14459 (setq org-ts-what (or what org-ts-what)
14460 inactive (= (char-after (match-beginning 0)) ?\[)
14461 ts (match-string 0))
14462 (replace-match "")
14463 (if (string-match
14464 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
14466 (setq extra (match-string 1 ts)))
14467 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
14468 (setq with-hm t))
14469 (setq time0 (org-parse-time-string ts))
14470 (when (and (eq org-ts-what 'minute)
14471 (eq current-prefix-arg nil))
14472 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
14473 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
14474 (setcar (cdr time0) (+ (nth 1 time0)
14475 (if (> n 0) (- rem) (- dm rem))))))
14476 (setq time
14477 (encode-time (or (car time0) 0)
14478 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
14479 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
14480 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
14481 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
14482 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
14483 (nthcdr 6 time0)))
14484 (when (and (member org-ts-what '(hour minute))
14485 extra
14486 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
14487 (setq extra (org-modify-ts-extra
14488 extra
14489 (if (eq org-ts-what 'hour) 2 5)
14490 n dm)))
14491 (when (integerp org-ts-what)
14492 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
14493 (if (eq what 'calendar)
14494 (let ((cal-date (org-get-date-from-calendar)))
14495 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
14496 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
14497 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
14498 (setcar time0 (or (car time0) 0))
14499 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
14500 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
14501 (setq time (apply 'encode-time time0))))
14502 (setq org-last-changed-timestamp
14503 (org-insert-time-stamp time with-hm inactive nil nil extra))
14504 (org-clock-update-time-maybe)
14505 (goto-char pos)
14506 ;; Try to recenter the calendar window, if any
14507 (if (and org-calendar-follow-timestamp-change
14508 (get-buffer-window "*Calendar*" t)
14509 (memq org-ts-what '(day month year)))
14510 (org-recenter-calendar (time-to-days time))))))
14512 (defun org-modify-ts-extra (s pos n dm)
14513 "Change the different parts of the lead-time and repeat fields in timestamp."
14514 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
14515 ng h m new rem)
14516 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
14517 (cond
14518 ((or (org-pos-in-match-range pos 2)
14519 (org-pos-in-match-range pos 3))
14520 (setq m (string-to-number (match-string 3 s))
14521 h (string-to-number (match-string 2 s)))
14522 (if (org-pos-in-match-range pos 2)
14523 (setq h (+ h n))
14524 (setq n (* dm (org-no-warnings (signum n))))
14525 (when (not (= 0 (setq rem (% m dm))))
14526 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
14527 (setq m (+ m n)))
14528 (if (< m 0) (setq m (+ m 60) h (1- h)))
14529 (if (> m 59) (setq m (- m 60) h (1+ h)))
14530 (setq h (min 24 (max 0 h)))
14531 (setq ng 1 new (format "-%02d:%02d" h m)))
14532 ((org-pos-in-match-range pos 6)
14533 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
14534 ((org-pos-in-match-range pos 5)
14535 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
14537 ((org-pos-in-match-range pos 9)
14538 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
14539 ((org-pos-in-match-range pos 8)
14540 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
14542 (when ng
14543 (setq s (concat
14544 (substring s 0 (match-beginning ng))
14546 (substring s (match-end ng))))))
14549 (defun org-recenter-calendar (date)
14550 "If the calendar is visible, recenter it to DATE."
14551 (let* ((win (selected-window))
14552 (cwin (get-buffer-window "*Calendar*" t))
14553 (calendar-move-hook nil))
14554 (when cwin
14555 (select-window cwin)
14556 (calendar-goto-date (if (listp date) date
14557 (calendar-gregorian-from-absolute date)))
14558 (select-window win))))
14560 (defun org-goto-calendar (&optional arg)
14561 "Go to the Emacs calendar at the current date.
14562 If there is a time stamp in the current line, go to that date.
14563 A prefix ARG can be used to force the current date."
14564 (interactive "P")
14565 (let ((tsr org-ts-regexp) diff
14566 (calendar-move-hook nil)
14567 (calendar-view-holidays-initially-flag nil)
14568 (view-calendar-holidays-initially nil)
14569 (calendar-view-diary-initially-flag nil)
14570 (view-diary-entries-initially nil))
14571 (if (or (org-at-timestamp-p)
14572 (save-excursion
14573 (beginning-of-line 1)
14574 (looking-at (concat ".*" tsr))))
14575 (let ((d1 (time-to-days (current-time)))
14576 (d2 (time-to-days
14577 (org-time-string-to-time (match-string 1)))))
14578 (setq diff (- d2 d1))))
14579 (calendar)
14580 (calendar-goto-today)
14581 (if (and diff (not arg)) (calendar-forward-day diff))))
14583 (defun org-get-date-from-calendar ()
14584 "Return a list (month day year) of date at point in calendar."
14585 (with-current-buffer "*Calendar*"
14586 (save-match-data
14587 (calendar-cursor-to-date))))
14589 (defun org-date-from-calendar ()
14590 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
14591 If there is already a time stamp at the cursor position, update it."
14592 (interactive)
14593 (if (org-at-timestamp-p t)
14594 (org-timestamp-change 0 'calendar)
14595 (let ((cal-date (org-get-date-from-calendar)))
14596 (org-insert-time-stamp
14597 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
14599 (defun org-minutes-to-hh:mm-string (m)
14600 "Compute H:MM from a number of minutes."
14601 (let ((h (/ m 60)))
14602 (setq m (- m (* 60 h)))
14603 (format org-time-clocksum-format h m)))
14605 (defun org-hh:mm-string-to-minutes (s)
14606 "Convert a string H:MM to a number of minutes.
14607 If the string is just a number, interpret it as minutes.
14608 In fact, the first hh:mm or number in the string will be taken,
14609 there can be extra stuff in the string.
14610 If no number is found, the return value is 0."
14611 (cond
14612 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
14613 (+ (* (string-to-number (match-string 1 s)) 60)
14614 (string-to-number (match-string 2 s))))
14615 ((string-match "\\([0-9]+\\)" s)
14616 (string-to-number (match-string 1 s)))
14617 (t 0)))
14619 ;;;; Files
14621 (defun org-save-all-org-buffers ()
14622 "Save all Org-mode buffers without user confirmation."
14623 (interactive)
14624 (message "Saving all Org-mode buffers...")
14625 (save-some-buffers t 'org-mode-p)
14626 (when (featurep 'org-id) (org-id-locations-save))
14627 (message "Saving all Org-mode buffers... done"))
14629 (defun org-revert-all-org-buffers ()
14630 "Revert all Org-mode buffers.
14631 Prompt for confirmation when there are unsaved changes.
14632 Be sure you know what you are doing before letting this function
14633 overwrite your changes.
14635 This function is useful in a setup where one tracks org files
14636 with a version control system, to revert on one machine after pulling
14637 changes from another. I believe the procedure must be like this:
14639 1. M-x org-save-all-org-buffers
14640 2. Pull changes from the other machine, resolve conflicts
14641 3. M-x org-revert-all-org-buffers"
14642 (interactive)
14643 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
14644 (error "Abort"))
14645 (save-excursion
14646 (save-window-excursion
14647 (mapc
14648 (lambda (b)
14649 (when (and (with-current-buffer b (org-mode-p))
14650 (with-current-buffer b buffer-file-name))
14651 (switch-to-buffer b)
14652 (revert-buffer t 'no-confirm)))
14653 (buffer-list))
14654 (when (and (featurep 'org-id) org-id-track-globally)
14655 (org-id-locations-load)))))
14657 ;;;; Agenda files
14659 ;;;###autoload
14660 (defun org-iswitchb (&optional arg)
14661 "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
14662 With a prefix argument, restrict available to files.
14663 With two prefix arguments, restrict available buffers to agenda files."
14664 (interactive "P")
14665 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
14666 ((equal arg '(16)) (org-buffer-list 'agenda))
14667 (t (org-buffer-list)))))
14668 (switch-to-buffer
14669 (org-icompleting-read "Org buffer: "
14670 (mapcar 'list (mapcar 'buffer-name blist))
14671 nil t))))
14673 ;;;###autoload
14674 (defalias 'org-ido-switchb 'org-iswitchb)
14676 (defun org-buffer-list (&optional predicate exclude-tmp)
14677 "Return a list of Org buffers.
14678 PREDICATE can be `export', `files' or `agenda'.
14680 export restrict the list to Export buffers.
14681 files restrict the list to buffers visiting Org files.
14682 agenda restrict the list to buffers visiting agenda files.
14684 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
14685 (let* ((bfn nil)
14686 (agenda-files (and (eq predicate 'agenda)
14687 (mapcar 'file-truename (org-agenda-files t))))
14688 (filter
14689 (cond
14690 ((eq predicate 'files)
14691 (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
14692 ((eq predicate 'export)
14693 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
14694 ((eq predicate 'agenda)
14695 (lambda (b)
14696 (with-current-buffer b
14697 (and (eq major-mode 'org-mode)
14698 (setq bfn (buffer-file-name b))
14699 (member (file-truename bfn) agenda-files)))))
14700 (t (lambda (b) (with-current-buffer b
14701 (or (eq major-mode 'org-mode)
14702 (string-match "\*Org .*Export"
14703 (buffer-name b)))))))))
14704 (delq nil
14705 (mapcar
14706 (lambda(b)
14707 (if (and (funcall filter b)
14708 (or (not exclude-tmp)
14709 (not (string-match "tmp" (buffer-name b)))))
14711 nil))
14712 (buffer-list)))))
14714 (defun org-agenda-files (&optional unrestricted archives)
14715 "Get the list of agenda files.
14716 Optional UNRESTRICTED means return the full list even if a restriction
14717 is currently in place.
14718 When ARCHIVES is t, include all archive files that are really being
14719 used by the agenda files. If ARCHIVE is `ifmode', do this only if
14720 `org-agenda-archives-mode' is t."
14721 (let ((files
14722 (cond
14723 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
14724 ((stringp org-agenda-files) (org-read-agenda-file-list))
14725 ((listp org-agenda-files) org-agenda-files)
14726 (t (error "Invalid value of `org-agenda-files'")))))
14727 (setq files (apply 'append
14728 (mapcar (lambda (f)
14729 (if (file-directory-p f)
14730 (directory-files
14731 f t org-agenda-file-regexp)
14732 (list f)))
14733 files)))
14734 (when org-agenda-skip-unavailable-files
14735 (setq files (delq nil
14736 (mapcar (function
14737 (lambda (file)
14738 (and (file-readable-p file) file)))
14739 files))))
14740 (when (or (eq archives t)
14741 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
14742 (setq files (org-add-archive-files files)))
14743 files))
14745 (defun org-edit-agenda-file-list ()
14746 "Edit the list of agenda files.
14747 Depending on setup, this either uses customize to edit the variable
14748 `org-agenda-files', or it visits the file that is holding the list. In the
14749 latter case, the buffer is set up in a way that saving it automatically kills
14750 the buffer and restores the previous window configuration."
14751 (interactive)
14752 (if (stringp org-agenda-files)
14753 (let ((cw (current-window-configuration)))
14754 (find-file org-agenda-files)
14755 (org-set-local 'org-window-configuration cw)
14756 (org-add-hook 'after-save-hook
14757 (lambda ()
14758 (set-window-configuration
14759 (prog1 org-window-configuration
14760 (kill-buffer (current-buffer))))
14761 (org-install-agenda-files-menu)
14762 (message "New agenda file list installed"))
14763 nil 'local)
14764 (message "%s" (substitute-command-keys
14765 "Edit list and finish with \\[save-buffer]")))
14766 (customize-variable 'org-agenda-files)))
14768 (defun org-store-new-agenda-file-list (list)
14769 "Set new value for the agenda file list and save it correctly."
14770 (if (stringp org-agenda-files)
14771 (let ((fe (org-read-agenda-file-list t)) b u)
14772 (while (setq b (find-buffer-visiting org-agenda-files))
14773 (kill-buffer b))
14774 (with-temp-file org-agenda-files
14775 (insert
14776 (mapconcat
14777 (lambda (f) ;; Keep un-expanded entries.
14778 (if (setq u (assoc f fe))
14779 (cdr u)
14781 list "\n")
14782 "\n")))
14783 (let ((org-mode-hook nil) (org-inhibit-startup t)
14784 (org-insert-mode-line-in-empty-file nil))
14785 (setq org-agenda-files list)
14786 (customize-save-variable 'org-agenda-files org-agenda-files))))
14788 (defun org-read-agenda-file-list (&optional pair-with-expansion)
14789 "Read the list of agenda files from a file.
14790 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
14791 filenames, used by `org-store-new-agenda-file-list' to write back
14792 un-expanded file names."
14793 (when (file-directory-p org-agenda-files)
14794 (error "`org-agenda-files' cannot be a single directory"))
14795 (when (stringp org-agenda-files)
14796 (with-temp-buffer
14797 (insert-file-contents org-agenda-files)
14798 (mapcar
14799 (lambda (f)
14800 (let ((e (expand-file-name (substitute-in-file-name f)
14801 org-directory)))
14802 (if pair-with-expansion
14803 (cons e f)
14804 e)))
14805 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
14807 ;;;###autoload
14808 (defun org-cycle-agenda-files ()
14809 "Cycle through the files in `org-agenda-files'.
14810 If the current buffer visits an agenda file, find the next one in the list.
14811 If the current buffer does not, find the first agenda file."
14812 (interactive)
14813 (let* ((fs (org-agenda-files t))
14814 (files (append fs (list (car fs))))
14815 (tcf (if buffer-file-name (file-truename buffer-file-name)))
14816 file)
14817 (unless files (error "No agenda files"))
14818 (catch 'exit
14819 (while (setq file (pop files))
14820 (if (equal (file-truename file) tcf)
14821 (when (car files)
14822 (find-file (car files))
14823 (throw 'exit t))))
14824 (find-file (car fs)))
14825 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
14827 (defun org-agenda-file-to-front (&optional to-end)
14828 "Move/add the current file to the top of the agenda file list.
14829 If the file is not present in the list, it is added to the front. If it is
14830 present, it is moved there. With optional argument TO-END, add/move to the
14831 end of the list."
14832 (interactive "P")
14833 (let ((org-agenda-skip-unavailable-files nil)
14834 (file-alist (mapcar (lambda (x)
14835 (cons (file-truename x) x))
14836 (org-agenda-files t)))
14837 (ctf (file-truename buffer-file-name))
14838 x had)
14839 (setq x (assoc ctf file-alist) had x)
14841 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
14842 (if to-end
14843 (setq file-alist (append (delq x file-alist) (list x)))
14844 (setq file-alist (cons x (delq x file-alist))))
14845 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
14846 (org-install-agenda-files-menu)
14847 (message "File %s to %s of agenda file list"
14848 (if had "moved" "added") (if to-end "end" "front"))))
14850 (defun org-remove-file (&optional file)
14851 "Remove current file from the list of files in variable `org-agenda-files'.
14852 These are the files which are being checked for agenda entries.
14853 Optional argument FILE means use this file instead of the current."
14854 (interactive)
14855 (let* ((org-agenda-skip-unavailable-files nil)
14856 (file (or file buffer-file-name))
14857 (true-file (file-truename file))
14858 (afile (abbreviate-file-name file))
14859 (files (delq nil (mapcar
14860 (lambda (x)
14861 (if (equal true-file
14862 (file-truename x))
14863 nil x))
14864 (org-agenda-files t)))))
14865 (if (not (= (length files) (length (org-agenda-files t))))
14866 (progn
14867 (org-store-new-agenda-file-list files)
14868 (org-install-agenda-files-menu)
14869 (message "Removed file: %s" afile))
14870 (message "File was not in list: %s (not removed)" afile))))
14872 (defun org-file-menu-entry (file)
14873 (vector file (list 'find-file file) t))
14875 (defun org-check-agenda-file (file)
14876 "Make sure FILE exists. If not, ask user what to do."
14877 (when (not (file-exists-p file))
14878 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
14879 (abbreviate-file-name file))
14880 (let ((r (downcase (read-char-exclusive))))
14881 (cond
14882 ((equal r ?r)
14883 (org-remove-file file)
14884 (throw 'nextfile t))
14885 (t (error "Abort"))))))
14887 (defun org-get-agenda-file-buffer (file)
14888 "Get a buffer visiting FILE. If the buffer needs to be created, add
14889 it to the list of buffers which might be released later."
14890 (let ((buf (org-find-base-buffer-visiting file)))
14891 (if buf
14892 buf ; just return it
14893 ;; Make a new buffer and remember it
14894 (setq buf (find-file-noselect file))
14895 (if buf (push buf org-agenda-new-buffers))
14896 buf)))
14898 (defun org-release-buffers (blist)
14899 "Release all buffers in list, asking the user for confirmation when needed.
14900 When a buffer is unmodified, it is just killed. When modified, it is saved
14901 \(if the user agrees) and then killed."
14902 (let (buf file)
14903 (while (setq buf (pop blist))
14904 (setq file (buffer-file-name buf))
14905 (when (and (buffer-modified-p buf)
14906 file
14907 (y-or-n-p (format "Save file %s? " file)))
14908 (with-current-buffer buf (save-buffer)))
14909 (kill-buffer buf))))
14911 (defun org-prepare-agenda-buffers (files)
14912 "Create buffers for all agenda files, protect archived trees and comments."
14913 (interactive)
14914 (let ((pa '(:org-archived t))
14915 (pc '(:org-comment t))
14916 (pall '(:org-archived t :org-comment t))
14917 (inhibit-read-only t)
14918 (rea (concat ":" org-archive-tag ":"))
14919 bmp file re)
14920 (save-excursion
14921 (save-restriction
14922 (while (setq file (pop files))
14923 (catch 'nextfile
14924 (if (bufferp file)
14925 (set-buffer file)
14926 (org-check-agenda-file file)
14927 (set-buffer (org-get-agenda-file-buffer file)))
14928 (widen)
14929 (setq bmp (buffer-modified-p))
14930 (org-refresh-category-properties)
14931 (setq org-todo-keywords-for-agenda
14932 (append org-todo-keywords-for-agenda org-todo-keywords-1))
14933 (setq org-done-keywords-for-agenda
14934 (append org-done-keywords-for-agenda org-done-keywords))
14935 (setq org-todo-keyword-alist-for-agenda
14936 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
14937 (setq org-drawers-for-agenda
14938 (append org-drawers-for-agenda org-drawers))
14939 (setq org-tag-alist-for-agenda
14940 (append org-tag-alist-for-agenda org-tag-alist))
14942 (save-excursion
14943 (remove-text-properties (point-min) (point-max) pall)
14944 (when org-agenda-skip-archived-trees
14945 (goto-char (point-min))
14946 (while (re-search-forward rea nil t)
14947 (if (org-on-heading-p t)
14948 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
14949 (goto-char (point-min))
14950 (setq re (concat "^\\*+ +" org-comment-string "\\>"))
14951 (while (re-search-forward re nil t)
14952 (add-text-properties
14953 (match-beginning 0) (org-end-of-subtree t) pc)))
14954 (set-buffer-modified-p bmp)))))
14955 (setq org-todo-keyword-alist-for-agenda
14956 (org-uniquify org-todo-keyword-alist-for-agenda)
14957 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
14959 ;;;; Embedded LaTeX
14961 (defvar org-cdlatex-mode-map (make-sparse-keymap)
14962 "Keymap for the minor `org-cdlatex-mode'.")
14964 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
14965 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
14966 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
14967 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
14968 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
14970 (defvar org-cdlatex-texmathp-advice-is-done nil
14971 "Flag remembering if we have applied the advice to texmathp already.")
14973 (define-minor-mode org-cdlatex-mode
14974 "Toggle the minor `org-cdlatex-mode'.
14975 This mode supports entering LaTeX environment and math in LaTeX fragments
14976 in Org-mode.
14977 \\{org-cdlatex-mode-map}"
14978 nil " OCDL" nil
14979 (when org-cdlatex-mode (require 'cdlatex))
14980 (unless org-cdlatex-texmathp-advice-is-done
14981 (setq org-cdlatex-texmathp-advice-is-done t)
14982 (defadvice texmathp (around org-math-always-on activate)
14983 "Always return t in org-mode buffers.
14984 This is because we want to insert math symbols without dollars even outside
14985 the LaTeX math segments. If Orgmode thinks that point is actually inside
14986 an embedded LaTeX fragment, let texmathp do its job.
14987 \\[org-cdlatex-mode-map]"
14988 (interactive)
14989 (let (p)
14990 (cond
14991 ((not (org-mode-p)) ad-do-it)
14992 ((eq this-command 'cdlatex-math-symbol)
14993 (setq ad-return-value t
14994 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
14996 (let ((p (org-inside-LaTeX-fragment-p)))
14997 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
14998 (setq ad-return-value t
14999 texmathp-why '("Org-mode embedded math" . 0))
15000 (if p ad-do-it)))))))))
15002 (defun turn-on-org-cdlatex ()
15003 "Unconditionally turn on `org-cdlatex-mode'."
15004 (org-cdlatex-mode 1))
15006 (defun org-inside-LaTeX-fragment-p ()
15007 "Test if point is inside a LaTeX fragment.
15008 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
15009 sequence appearing also before point.
15010 Even though the matchers for math are configurable, this function assumes
15011 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
15012 delimiters are skipped when they have been removed by customization.
15013 The return value is nil, or a cons cell with the delimiter and
15014 and the position of this delimiter.
15016 This function does a reasonably good job, but can locally be fooled by
15017 for example currency specifications. For example it will assume being in
15018 inline math after \"$22.34\". The LaTeX fragment formatter will only format
15019 fragments that are properly closed, but during editing, we have to live
15020 with the uncertainty caused by missing closing delimiters. This function
15021 looks only before point, not after."
15022 (catch 'exit
15023 (let ((pos (point))
15024 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
15025 (lim (progn
15026 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
15027 (point)))
15028 dd-on str (start 0) m re)
15029 (goto-char pos)
15030 (when dodollar
15031 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
15032 re (nth 1 (assoc "$" org-latex-regexps)))
15033 (while (string-match re str start)
15034 (cond
15035 ((= (match-end 0) (length str))
15036 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
15037 ((= (match-end 0) (- (length str) 5))
15038 (throw 'exit nil))
15039 (t (setq start (match-end 0))))))
15040 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
15041 (goto-char pos)
15042 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
15043 (and (match-beginning 2) (throw 'exit nil))
15044 ;; count $$
15045 (while (re-search-backward "\\$\\$" lim t)
15046 (setq dd-on (not dd-on)))
15047 (goto-char pos)
15048 (if dd-on (cons "$$" m))))))
15050 (defun org-inside-latex-macro-p ()
15051 "Is point inside a LaTeX macro or its arguments?"
15052 (save-match-data
15053 (org-in-regexp
15054 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
15056 (defun test ()
15057 (interactive)
15058 (message "%s" (org-inside-latex-macro-p)))
15060 (defun org-try-cdlatex-tab ()
15061 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
15062 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
15063 - inside a LaTeX fragment, or
15064 - after the first word in a line, where an abbreviation expansion could
15065 insert a LaTeX environment."
15066 (when org-cdlatex-mode
15067 (cond
15068 ((save-excursion
15069 (skip-chars-backward "a-zA-Z0-9*")
15070 (skip-chars-backward " \t")
15071 (bolp))
15072 (cdlatex-tab) t)
15073 ((org-inside-LaTeX-fragment-p)
15074 (cdlatex-tab) t)
15075 (t nil))))
15077 (defun org-cdlatex-underscore-caret (&optional arg)
15078 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
15079 Revert to the normal definition outside of these fragments."
15080 (interactive "P")
15081 (if (org-inside-LaTeX-fragment-p)
15082 (call-interactively 'cdlatex-sub-superscript)
15083 (let (org-cdlatex-mode)
15084 (call-interactively (key-binding (vector last-input-event))))))
15086 (defun org-cdlatex-math-modify (&optional arg)
15087 "Execute `cdlatex-math-modify' in LaTeX fragments.
15088 Revert to the normal definition outside of these fragments."
15089 (interactive "P")
15090 (if (org-inside-LaTeX-fragment-p)
15091 (call-interactively 'cdlatex-math-modify)
15092 (let (org-cdlatex-mode)
15093 (call-interactively (key-binding (vector last-input-event))))))
15095 (defvar org-latex-fragment-image-overlays nil
15096 "List of overlays carrying the images of latex fragments.")
15097 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
15099 (defun org-remove-latex-fragment-image-overlays ()
15100 "Remove all overlays with LaTeX fragment images in current buffer."
15101 (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
15102 (setq org-latex-fragment-image-overlays nil))
15104 (defun org-preview-latex-fragment (&optional subtree)
15105 "Preview the LaTeX fragment at point, or all locally or globally.
15106 If the cursor is in a LaTeX fragment, create the image and overlay
15107 it over the source code. If there is no fragment at point, display
15108 all fragments in the current text, from one headline to the next. With
15109 prefix SUBTREE, display all fragments in the current subtree. With a
15110 double prefix `C-u C-u', or when the cursor is before the first headline,
15111 display all fragments in the buffer.
15112 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
15113 (interactive "P")
15114 (org-remove-latex-fragment-image-overlays)
15115 (save-excursion
15116 (save-restriction
15117 (let (beg end at msg)
15118 (cond
15119 ((or (equal subtree '(16))
15120 (not (save-excursion
15121 (re-search-backward (concat "^" outline-regexp) nil t))))
15122 (setq beg (point-min) end (point-max)
15123 msg "Creating images for buffer...%s"))
15124 ((equal subtree '(4))
15125 (org-back-to-heading)
15126 (setq beg (point) end (org-end-of-subtree t)
15127 msg "Creating images for subtree...%s"))
15129 (if (setq at (org-inside-LaTeX-fragment-p))
15130 (goto-char (max (point-min) (- (cdr at) 2)))
15131 (org-back-to-heading))
15132 (setq beg (point) end (progn (outline-next-heading) (point))
15133 msg (if at "Creating image...%s"
15134 "Creating images for entry...%s"))))
15135 (message msg "")
15136 (narrow-to-region beg end)
15137 (goto-char beg)
15138 (org-format-latex
15139 (concat "ltxpng/" (file-name-sans-extension
15140 (file-name-nondirectory
15141 buffer-file-name)))
15142 default-directory 'overlays msg at 'forbuffer)
15143 (message msg "done. Use `C-c C-c' to remove images.")))))
15145 (defvar org-latex-regexps
15146 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
15147 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
15148 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
15149 ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
15150 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
15151 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
15152 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
15153 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
15154 "Regular expressions for matching embedded LaTeX.")
15156 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
15157 "Replace LaTeX fragments with links to an image, and produce images.
15158 Some of the options can be changed using the variable
15159 `org-format-latex-options'."
15160 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
15161 (let* ((prefixnodir (file-name-nondirectory prefix))
15162 (absprefix (expand-file-name prefix dir))
15163 (todir (file-name-directory absprefix))
15164 (opt org-format-latex-options)
15165 (matchers (plist-get opt :matchers))
15166 (re-list org-latex-regexps)
15167 (org-format-latex-header-extra
15168 (plist-get (org-infile-export-plist) :latex-header-extra))
15169 (cnt 0) txt hash link beg end re e checkdir
15170 executables-checked
15171 m n block linkfile movefile ov)
15172 ;; Check the different regular expressions
15173 (while (setq e (pop re-list))
15174 (setq m (car e) re (nth 1 e) n (nth 2 e)
15175 block (if (nth 3 e) "\n\n" ""))
15176 (when (member m matchers)
15177 (goto-char (point-min))
15178 (while (re-search-forward re nil t)
15179 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
15180 (not (get-text-property (match-beginning n)
15181 'org-protected))
15182 (or (not overlays)
15183 (not (eq (get-char-property (match-beginning n)
15184 'org-overlay-type)
15185 'org-latex-overlay))))
15186 (setq txt (match-string n)
15187 beg (match-beginning n) end (match-end n)
15188 cnt (1+ cnt))
15189 (let (print-length print-level) ; make sure full list is printed
15190 (setq hash (sha1 (prin1-to-string
15191 (list org-format-latex-header
15192 org-format-latex-header-extra
15193 org-export-latex-default-packages-alist
15194 org-export-latex-packages-alist
15195 org-format-latex-options
15196 forbuffer txt)))
15197 linkfile (format "%s_%s.png" prefix hash)
15198 movefile (format "%s_%s.png" absprefix hash)))
15199 (setq link (concat block "[[file:" linkfile "]]" block))
15200 (if msg (message msg cnt))
15201 (goto-char beg)
15202 (unless checkdir ; make sure the directory exists
15203 (setq checkdir t)
15204 (or (file-directory-p todir) (make-directory todir)))
15206 (unless executables-checked
15207 (org-check-external-command
15208 "latex" "needed to convert LaTeX fragments to images")
15209 (org-check-external-command
15210 "dvipng" "needed to convert LaTeX fragments to images")
15211 (setq executables-checked t))
15213 (unless (file-exists-p movefile)
15214 (org-create-formula-image
15215 txt movefile opt forbuffer))
15216 (if overlays
15217 (progn
15218 (mapc (lambda (o)
15219 (if (eq (org-overlay-get o 'org-overlay-type)
15220 'org-latex-overlay)
15221 (org-delete-overlay o)))
15222 (org-overlays-in beg end))
15223 (setq ov (org-make-overlay beg end))
15224 (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
15225 (if (featurep 'xemacs)
15226 (progn
15227 (org-overlay-put ov 'invisible t)
15228 (org-overlay-put
15229 ov 'end-glyph
15230 (make-glyph (vector 'png :file movefile))))
15231 (org-overlay-put
15232 ov 'display
15233 (list 'image :type 'png :file movefile :ascent 'center)))
15234 (push ov org-latex-fragment-image-overlays)
15235 (goto-char end))
15236 (delete-region beg end)
15237 (insert (org-add-props link
15238 (list 'org-latex-src
15239 (replace-regexp-in-string "\"" "" txt)))))))))))
15241 ;; This function borrows from Ganesh Swami's latex2png.el
15242 (defun org-create-formula-image (string tofile options buffer)
15243 "This calls dvipng."
15244 (require 'org-latex)
15245 (let* ((tmpdir (if (featurep 'xemacs)
15246 (temp-directory)
15247 temporary-file-directory))
15248 (texfilebase (make-temp-name
15249 (expand-file-name "orgtex" tmpdir)))
15250 (texfile (concat texfilebase ".tex"))
15251 (dvifile (concat texfilebase ".dvi"))
15252 (pngfile (concat texfilebase ".png"))
15253 (fnh (if (featurep 'xemacs)
15254 (font-height (get-face-font 'default))
15255 (face-attribute 'default :height nil)))
15256 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
15257 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
15258 (fg (or (plist-get options (if buffer :foreground :html-foreground))
15259 "Black"))
15260 (bg (or (plist-get options (if buffer :background :html-background))
15261 "Transparent")))
15262 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
15263 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
15264 (with-temp-file texfile
15265 (insert (org-splice-latex-header
15266 org-format-latex-header
15267 org-export-latex-default-packages-alist
15268 org-export-latex-packages-alist
15269 org-format-latex-header-extra))
15270 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
15271 (require 'org-latex)
15272 (org-export-latex-fix-inputenc))
15273 (let ((dir default-directory))
15274 (condition-case nil
15275 (progn
15276 (cd tmpdir)
15277 (call-process "latex" nil nil nil texfile))
15278 (error nil))
15279 (cd dir))
15280 (if (not (file-exists-p dvifile))
15281 (progn (message "Failed to create dvi file from %s" texfile) nil)
15282 (condition-case nil
15283 (call-process "dvipng" nil nil nil
15284 "-fg" fg "-bg" bg
15285 "-D" dpi
15286 ;;"-x" scale "-y" scale
15287 "-T" "tight"
15288 "-o" pngfile
15289 dvifile)
15290 (error nil))
15291 (if (not (file-exists-p pngfile))
15292 (if org-format-latex-signal-error
15293 (error "Failed to create png file from %s" texfile)
15294 (message "Failed to create png file from %s" texfile)
15295 nil)
15296 ;; Use the requested file name and clean up
15297 (copy-file pngfile tofile 'replace)
15298 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
15299 (delete-file (concat texfilebase e)))
15300 pngfile))))
15302 (defun org-splice-latex-header (tpl def-pkg pkg &optional extra)
15303 "Fill a LaTeX header template TPL.
15304 In the template, the following place holders will be recognized:
15306 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
15307 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
15308 [PACKAGES] \\usepackage statements for PKG
15309 [NO-PACKAGES] do not include PKG
15310 [EXTRA] the string EXTRA
15311 [NO-EXTRA] do not include EXTRA
15313 For backward compatibility, if both the positive and the negative place
15314 holder is missing, the positive one (without the \"NO-\") will be
15315 assumed to be present at the end of the template.
15316 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
15317 EXTRA is a string."
15318 (let (rpl (end ""))
15319 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
15320 (setq rpl (if (or (match-end 1) (not def-pkg))
15321 "" (org-latex-packages-to-string def-pkg t))
15322 tpl (replace-match rpl t t tpl))
15323 (if def-pkg (setq end (org-latex-packages-to-string def-pkg))))
15325 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
15326 (setq rpl (if (or (match-end 1) (not pkg))
15327 "" (org-latex-packages-to-string pkg t))
15328 tpl (replace-match rpl t t tpl))
15329 (if pkg (setq end (concat end "\n" (org-latex-packages-to-string pkg)))))
15331 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
15332 (setq rpl (if (or (match-end 1) (not extra))
15333 "" (concat extra "\n"))
15334 tpl (replace-match rpl t t tpl))
15335 (if pkg (setq end (org-latex-packages-to-string pkg))))
15337 (if (string-match "\\S-" end)
15338 (concat tpl "\n" end)
15339 tpl)))
15341 (defun org-latex-packages-to-string (pkg &optional newline)
15342 "Turn an alist of packages into a string with the \\usepackage macros."
15343 (setq pkg (mapconcat (lambda(p)
15344 (if (equal "" (car p))
15345 (format "\\usepackage{%s}" (cadr p))
15346 (format "\\usepackage[%s]{%s}"
15347 (car p) (cadr p))))
15349 "\n"))
15350 (if newline (concat pkg "\n") pkg))
15352 (defun org-dvipng-color (attr)
15353 "Return an rgb color specification for dvipng."
15354 (apply 'format "rgb %s %s %s"
15355 (mapcar 'org-normalize-color
15356 (color-values (face-attribute 'default attr nil)))))
15358 (defun org-normalize-color (value)
15359 "Return string to be used as color value for an RGB component."
15360 (format "%g" (/ value 65535.0)))
15362 ;;;; Key bindings
15364 ;; Make `C-c C-x' a prefix key
15365 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
15367 ;; TAB key with modifiers
15368 (org-defkey org-mode-map "\C-i" 'org-cycle)
15369 (org-defkey org-mode-map [(tab)] 'org-cycle)
15370 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
15371 (org-defkey org-mode-map [(meta tab)] 'org-complete)
15372 (org-defkey org-mode-map "\M-\t" 'org-complete)
15373 (org-defkey org-mode-map "\M-\C-i" 'org-complete)
15374 ;; The following line is necessary under Suse GNU/Linux
15375 (unless (featurep 'xemacs)
15376 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
15377 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
15378 (define-key org-mode-map [backtab] 'org-shifttab)
15380 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
15381 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
15382 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
15384 ;; Cursor keys with modifiers
15385 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
15386 (org-defkey org-mode-map [(meta right)] 'org-metaright)
15387 (org-defkey org-mode-map [(meta up)] 'org-metaup)
15388 (org-defkey org-mode-map [(meta down)] 'org-metadown)
15390 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
15391 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
15392 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
15393 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
15395 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
15396 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
15397 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
15398 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
15400 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
15401 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
15403 ;;; Extra keys for tty access.
15404 ;; We only set them when really needed because otherwise the
15405 ;; menus don't show the simple keys
15407 (when (or org-use-extra-keys
15408 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
15409 (not window-system))
15410 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
15411 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
15412 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
15413 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
15414 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
15415 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
15416 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
15417 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
15418 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
15419 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
15420 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
15421 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
15422 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
15423 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
15424 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
15425 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
15426 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
15427 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
15428 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
15429 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
15430 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
15431 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
15432 (org-defkey org-mode-map [?\e (tab)] 'org-complete)
15433 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
15434 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
15435 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
15436 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
15437 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
15439 ;; All the other keys
15441 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
15442 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
15443 (if (boundp 'narrow-map)
15444 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
15445 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
15446 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
15447 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
15448 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
15449 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
15450 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
15451 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
15452 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
15453 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
15454 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
15455 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
15456 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
15457 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
15458 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
15459 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
15460 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
15461 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
15462 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
15463 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
15464 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
15465 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
15466 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
15467 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
15468 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
15469 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
15470 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
15471 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
15472 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
15473 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
15474 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
15475 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
15476 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
15477 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
15478 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
15479 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
15480 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
15481 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
15482 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
15483 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
15484 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
15485 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
15486 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
15487 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
15488 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
15489 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
15490 (org-defkey org-mode-map "\C-c^" 'org-sort)
15491 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
15492 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
15493 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
15494 (org-defkey org-mode-map "\C-m" 'org-return)
15495 (org-defkey org-mode-map "\C-j" 'org-return-indent)
15496 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
15497 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
15498 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
15499 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
15500 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
15501 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
15502 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
15503 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
15504 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
15505 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
15506 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
15507 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
15508 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
15509 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
15510 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
15511 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
15512 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
15513 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
15514 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
15515 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
15517 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
15518 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
15519 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
15520 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
15522 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
15523 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
15524 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
15525 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
15526 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
15527 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
15528 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
15529 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
15530 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
15531 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
15532 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
15533 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
15534 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
15535 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
15536 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
15538 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
15539 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
15540 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
15541 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
15543 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
15545 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
15547 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
15548 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
15550 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
15553 (when (featurep 'xemacs)
15554 (org-defkey org-mode-map 'button3 'popup-mode-menu))
15557 (defconst org-speed-commands-default
15559 ("Outline Navigation")
15560 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
15561 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
15562 ("f" . (org-speed-move-safe 'org-forward-same-level))
15563 ("b" . (org-speed-move-safe 'org-backward-same-level))
15564 ("u" . (org-speed-move-safe 'outline-up-heading))
15565 ("j" . org-goto)
15566 ("g" . (org-refile t))
15567 ("Outline Visibility")
15568 ("c" . org-cycle)
15569 ("C" . org-shifttab)
15570 (" " . org-display-outline-path)
15571 ("Outline Structure Editing")
15572 ("U" . org-shiftmetaup)
15573 ("D" . org-shiftmetadown)
15574 ("r" . org-metaright)
15575 ("l" . org-metaleft)
15576 ("R" . org-shiftmetaright)
15577 ("L" . org-shiftmetaleft)
15578 ("i" . (progn (forward-char 1) (call-interactively
15579 'org-insert-heading-respect-content)))
15580 ("^" . org-sort)
15581 ("w" . org-refile)
15582 ("a" . org-archive-subtree-default-with-confirmation)
15583 ("." . outline-mark-subtree)
15584 ("Clock Commands")
15585 ("I" . org-clock-in)
15586 ("O" . org-clock-out)
15587 ("Meta Data Editing")
15588 ("t" . org-todo)
15589 ("0" . (org-priority ?\ ))
15590 ("1" . (org-priority ?A))
15591 ("2" . (org-priority ?B))
15592 ("3" . (org-priority ?C))
15593 (";" . org-set-tags-command)
15594 ("e" . org-set-effort)
15595 ("Agenda Views etc")
15596 ("v" . org-agenda)
15597 ("/" . org-sparse-tree)
15598 ("Misc")
15599 ("o" . org-open-at-point)
15600 ("?" . org-speed-command-help)
15602 "The default speed commands.")
15604 (defun org-print-speed-command (e)
15605 (if (> (length (car e)) 1)
15606 (progn
15607 (princ "\n")
15608 (princ (car e))
15609 (princ "\n")
15610 (princ (make-string (length (car e)) ?-))
15611 (princ "\n"))
15612 (princ (car e))
15613 (princ " ")
15614 (if (symbolp (cdr e))
15615 (princ (symbol-name (cdr e)))
15616 (prin1 (cdr e)))
15617 (princ "\n")))
15619 (defun org-speed-command-help ()
15620 "Show the available speed commands."
15621 (interactive)
15622 (if (not org-use-speed-commands)
15623 (error "Speed commands are not activated, customize `org-use-speed-commands'.")
15624 (with-output-to-temp-buffer "*Help*"
15625 (princ "User-defined Speed commands\n===========================\n")
15626 (mapc 'org-print-speed-command org-speed-commands-user)
15627 (princ "\n")
15628 (princ "Built-in Speed commands\n=======================\n")
15629 (mapc 'org-print-speed-command org-speed-commands-default))
15630 (with-current-buffer "*Help*"
15631 (setq truncate-lines t))))
15633 (defun org-speed-move-safe (cmd)
15634 "Execute CMD, but make sure that the cursor always ends up in a headline.
15635 If not, return to the original position and throw an error."
15636 (interactive)
15637 (let ((pos (point)))
15638 (call-interactively cmd)
15639 (unless (and (bolp) (org-on-heading-p))
15640 (goto-char pos)
15641 (error "Boundary reached while executing %s" cmd))))
15643 (defvar org-self-insert-command-undo-counter 0)
15645 (defvar org-table-auto-blank-field) ; defined in org-table.el
15646 (defvar org-speed-command nil)
15647 (defun org-self-insert-command (N)
15648 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
15649 If the cursor is in a table looking at whitespace, the whitespace is
15650 overwritten, and the table is not marked as requiring realignment."
15651 (interactive "p")
15652 (cond
15653 ((and org-use-speed-commands
15654 (or (and (bolp) (looking-at outline-regexp))
15655 (and (functionp org-use-speed-commands)
15656 (funcall org-use-speed-commands)))
15657 (setq
15658 org-speed-command
15659 (or (cdr (assoc (this-command-keys) org-speed-commands-user))
15660 (cdr (assoc (this-command-keys) org-speed-commands-default)))))
15661 (cond
15662 ((commandp org-speed-command)
15663 (setq this-command org-speed-command)
15664 (call-interactively org-speed-command))
15665 ((functionp org-speed-command)
15666 (funcall org-speed-command))
15667 ((and org-speed-command (listp org-speed-command))
15668 (eval org-speed-command))
15669 (t (let (org-use-speed-commands)
15670 (call-interactively 'org-self-insert-command)))))
15671 ((and
15672 (org-table-p)
15673 (progn
15674 ;; check if we blank the field, and if that triggers align
15675 (and (featurep 'org-table) org-table-auto-blank-field
15676 (member last-command
15677 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
15678 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
15679 ;; got extra space, this field does not determine column width
15680 (let (org-table-may-need-update) (org-table-blank-field))
15681 ;; no extra space, this field may determine column width
15682 (org-table-blank-field)))
15684 (eq N 1)
15685 (looking-at "[^|\n]* |"))
15686 (let (org-table-may-need-update)
15687 (goto-char (1- (match-end 0)))
15688 (delete-backward-char 1)
15689 (goto-char (match-beginning 0))
15690 (self-insert-command N)))
15692 (setq org-table-may-need-update t)
15693 (self-insert-command N)
15694 (org-fix-tags-on-the-fly)
15695 (if org-self-insert-cluster-for-undo
15696 (if (not (eq last-command 'org-self-insert-command))
15697 (setq org-self-insert-command-undo-counter 1)
15698 (if (>= org-self-insert-command-undo-counter 20)
15699 (setq org-self-insert-command-undo-counter 1)
15700 (and (> org-self-insert-command-undo-counter 0)
15701 buffer-undo-list
15702 (not (cadr buffer-undo-list)) ; remove nil entry
15703 (setcdr buffer-undo-list (cddr buffer-undo-list)))
15704 (setq org-self-insert-command-undo-counter
15705 (1+ org-self-insert-command-undo-counter))))))))
15707 (defun org-fix-tags-on-the-fly ()
15708 (when (and (equal (char-after (point-at-bol)) ?*)
15709 (org-on-heading-p))
15710 (org-align-tags-here org-tags-column)))
15712 (defun org-delete-backward-char (N)
15713 "Like `delete-backward-char', insert whitespace at field end in tables.
15714 When deleting backwards, in tables this function will insert whitespace in
15715 front of the next \"|\" separator, to keep the table aligned. The table will
15716 still be marked for re-alignment if the field did fill the entire column,
15717 because, in this case the deletion might narrow the column."
15718 (interactive "p")
15719 (if (and (org-table-p)
15720 (eq N 1)
15721 (string-match "|" (buffer-substring (point-at-bol) (point)))
15722 (looking-at ".*?|"))
15723 (let ((pos (point))
15724 (noalign (looking-at "[^|\n\r]* |"))
15725 (c org-table-may-need-update))
15726 (backward-delete-char N)
15727 (skip-chars-forward "^|")
15728 (insert " ")
15729 (goto-char (1- pos))
15730 ;; noalign: if there were two spaces at the end, this field
15731 ;; does not determine the width of the column.
15732 (if noalign (setq org-table-may-need-update c)))
15733 (backward-delete-char N)
15734 (org-fix-tags-on-the-fly)))
15736 (defun org-delete-char (N)
15737 "Like `delete-char', but insert whitespace at field end in tables.
15738 When deleting characters, in tables this function will insert whitespace in
15739 front of the next \"|\" separator, to keep the table aligned. The table will
15740 still be marked for re-alignment if the field did fill the entire column,
15741 because, in this case the deletion might narrow the column."
15742 (interactive "p")
15743 (if (and (org-table-p)
15744 (not (bolp))
15745 (not (= (char-after) ?|))
15746 (eq N 1))
15747 (if (looking-at ".*?|")
15748 (let ((pos (point))
15749 (noalign (looking-at "[^|\n\r]* |"))
15750 (c org-table-may-need-update))
15751 (replace-match (concat
15752 (substring (match-string 0) 1 -1)
15753 " |"))
15754 (goto-char pos)
15755 ;; noalign: if there were two spaces at the end, this field
15756 ;; does not determine the width of the column.
15757 (if noalign (setq org-table-may-need-update c)))
15758 (delete-char N))
15759 (delete-char N)
15760 (org-fix-tags-on-the-fly)))
15762 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
15763 (put 'org-self-insert-command 'delete-selection t)
15764 (put 'orgtbl-self-insert-command 'delete-selection t)
15765 (put 'org-delete-char 'delete-selection 'supersede)
15766 (put 'org-delete-backward-char 'delete-selection 'supersede)
15767 (put 'org-yank 'delete-selection 'yank)
15769 ;; Make `flyspell-mode' delay after some commands
15770 (put 'org-self-insert-command 'flyspell-delayed t)
15771 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
15772 (put 'org-delete-char 'flyspell-delayed t)
15773 (put 'org-delete-backward-char 'flyspell-delayed t)
15775 ;; Make pabbrev-mode expand after org-mode commands
15776 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
15777 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
15779 ;; How to do this: Measure non-white length of current string
15780 ;; If equal to column width, we should realign.
15782 (defun org-remap (map &rest commands)
15783 "In MAP, remap the functions given in COMMANDS.
15784 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
15785 (let (new old)
15786 (while commands
15787 (setq old (pop commands) new (pop commands))
15788 (if (fboundp 'command-remapping)
15789 (org-defkey map (vector 'remap old) new)
15790 (substitute-key-definition old new map global-map)))))
15792 (when (eq org-enable-table-editor 'optimized)
15793 ;; If the user wants maximum table support, we need to hijack
15794 ;; some standard editing functions
15795 (org-remap org-mode-map
15796 'self-insert-command 'org-self-insert-command
15797 'delete-char 'org-delete-char
15798 'delete-backward-char 'org-delete-backward-char)
15799 (org-defkey org-mode-map "|" 'org-force-self-insert))
15801 (defvar org-ctrl-c-ctrl-c-hook nil
15802 "Hook for functions attaching themselves to `C-c C-c'.
15803 This can be used to add additional functionality to the C-c C-c key which
15804 executes context-dependent commands.
15805 Each function will be called with no arguments. The function must check
15806 if the context is appropriate for it to act. If yes, it should do its
15807 thing and then return a non-nil value. If the context is wrong,
15808 just do nothing and return nil.")
15810 (defvar org-tab-first-hook nil
15811 "Hook for functions to attach themselves to TAB.
15812 See `org-ctrl-c-ctrl-c-hook' for more information.
15813 This hook runs as the first action when TAB is pressed, even before
15814 `org-cycle' messes around with the `outline-regexp' to cater for
15815 inline tasks and plain list item folding.
15816 If any function in this hook returns t, not other actions like table
15817 field motion visibility cycling will be done.")
15819 (defvar org-tab-after-check-for-table-hook nil
15820 "Hook for functions to attach themselves to TAB.
15821 See `org-ctrl-c-ctrl-c-hook' for more information.
15822 This hook runs after it has been established that the cursor is not in a
15823 table, but before checking if the cursor is in a headline or if global cycling
15824 should be done.
15825 If any function in this hook returns t, not other actions like visibility
15826 cycling will be done.")
15828 (defvar org-tab-after-check-for-cycling-hook nil
15829 "Hook for functions to attach themselves to TAB.
15830 See `org-ctrl-c-ctrl-c-hook' for more information.
15831 This hook runs after it has been established that not table field motion and
15832 not visibility should be done because of current context. This is probably
15833 the place where a package like yasnippets can hook in.")
15835 (defvar org-tab-before-tab-emulation-hook nil
15836 "Hook for functions to attach themselves to TAB.
15837 See `org-ctrl-c-ctrl-c-hook' for more information.
15838 This hook runs after every other options for TAB have been exhausted, but
15839 before indentation and \t insertion takes place.")
15841 (defvar org-metaleft-hook nil
15842 "Hook for functions attaching themselves to `M-left'.
15843 See `org-ctrl-c-ctrl-c-hook' for more information.")
15844 (defvar org-metaright-hook nil
15845 "Hook for functions attaching themselves to `M-right'.
15846 See `org-ctrl-c-ctrl-c-hook' for more information.")
15847 (defvar org-metaup-hook nil
15848 "Hook for functions attaching themselves to `M-up'.
15849 See `org-ctrl-c-ctrl-c-hook' for more information.")
15850 (defvar org-metadown-hook nil
15851 "Hook for functions attaching themselves to `M-down'.
15852 See `org-ctrl-c-ctrl-c-hook' for more information.")
15853 (defvar org-shiftmetaleft-hook nil
15854 "Hook for functions attaching themselves to `M-S-left'.
15855 See `org-ctrl-c-ctrl-c-hook' for more information.")
15856 (defvar org-shiftmetaright-hook nil
15857 "Hook for functions attaching themselves to `M-S-right'.
15858 See `org-ctrl-c-ctrl-c-hook' for more information.")
15859 (defvar org-shiftmetaup-hook nil
15860 "Hook for functions attaching themselves to `M-S-up'.
15861 See `org-ctrl-c-ctrl-c-hook' for more information.")
15862 (defvar org-shiftmetadown-hook nil
15863 "Hook for functions attaching themselves to `M-S-down'.
15864 See `org-ctrl-c-ctrl-c-hook' for more information.")
15865 (defvar org-metareturn-hook nil
15866 "Hook for functions attaching themselves to `M-RET'.
15867 See `org-ctrl-c-ctrl-c-hook' for more information.")
15869 (defun org-modifier-cursor-error ()
15870 "Throw an error, a modified cursor command was applied in wrong context."
15871 (error "This command is active in special context like tables, headlines or items"))
15873 (defun org-shiftselect-error ()
15874 "Throw an error because Shift-Cursor command was applied in wrong context."
15875 (if (and (boundp 'shift-select-mode) shift-select-mode)
15876 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
15877 (error "This command works only in special context like headlines or timestamps")))
15879 (defun org-call-for-shift-select (cmd)
15880 (let ((this-command-keys-shift-translated t))
15881 (call-interactively cmd)))
15883 (defun org-shifttab (&optional arg)
15884 "Global visibility cycling or move to previous table field.
15885 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
15886 on context.
15887 See the individual commands for more information."
15888 (interactive "P")
15889 (cond
15890 ((org-at-table-p) (call-interactively 'org-table-previous-field))
15891 ((integerp arg)
15892 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
15893 (message "Content view to level: %d" arg)
15894 (org-content (prefix-numeric-value arg2))
15895 (setq org-cycle-global-status 'overview)))
15896 (t (call-interactively 'org-global-cycle))))
15898 (defun org-shiftmetaleft ()
15899 "Promote subtree or delete table column.
15900 Calls `org-promote-subtree', `org-outdent-item',
15901 or `org-table-delete-column', depending on context.
15902 See the individual commands for more information."
15903 (interactive)
15904 (cond
15905 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
15906 ((org-at-table-p) (call-interactively 'org-table-delete-column))
15907 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
15908 ((org-at-item-p) (call-interactively 'org-outdent-item))
15909 (t (org-modifier-cursor-error))))
15911 (defun org-shiftmetaright ()
15912 "Demote subtree or insert table column.
15913 Calls `org-demote-subtree', `org-indent-item',
15914 or `org-table-insert-column', depending on context.
15915 See the individual commands for more information."
15916 (interactive)
15917 (cond
15918 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
15919 ((org-at-table-p) (call-interactively 'org-table-insert-column))
15920 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
15921 ((org-at-item-p) (call-interactively 'org-indent-item))
15922 (t (org-modifier-cursor-error))))
15924 (defun org-shiftmetaup (&optional arg)
15925 "Move subtree up or kill table row.
15926 Calls `org-move-subtree-up' or `org-table-kill-row' or
15927 `org-move-item-up' depending on context. See the individual commands
15928 for more information."
15929 (interactive "P")
15930 (cond
15931 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
15932 ((org-at-table-p) (call-interactively 'org-table-kill-row))
15933 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
15934 ((org-at-item-p) (call-interactively 'org-move-item-up))
15935 (t (org-modifier-cursor-error))))
15937 (defun org-shiftmetadown (&optional arg)
15938 "Move subtree down or insert table row.
15939 Calls `org-move-subtree-down' or `org-table-insert-row' or
15940 `org-move-item-down', depending on context. See the individual
15941 commands for more information."
15942 (interactive "P")
15943 (cond
15944 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
15945 ((org-at-table-p) (call-interactively 'org-table-insert-row))
15946 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
15947 ((org-at-item-p) (call-interactively 'org-move-item-down))
15948 (t (org-modifier-cursor-error))))
15950 (defun org-metaleft (&optional arg)
15951 "Promote heading or move table column to left.
15952 Calls `org-do-promote' or `org-table-move-column', depending on context.
15953 With no specific context, calls the Emacs default `backward-word'.
15954 See the individual commands for more information."
15955 (interactive "P")
15956 (cond
15957 ((run-hook-with-args-until-success 'org-metaleft-hook))
15958 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
15959 ((or (org-on-heading-p)
15960 (and (org-region-active-p)
15961 (save-excursion
15962 (goto-char (region-beginning))
15963 (org-on-heading-p))))
15964 (call-interactively 'org-do-promote))
15965 ((or (org-at-item-p)
15966 (and (org-region-active-p)
15967 (save-excursion
15968 (goto-char (region-beginning))
15969 (org-at-item-p))))
15970 (call-interactively 'org-outdent-item))
15971 (t (call-interactively 'backward-word))))
15973 (defun org-metaright (&optional arg)
15974 "Demote subtree or move table column to right.
15975 Calls `org-do-demote' or `org-table-move-column', depending on context.
15976 With no specific context, calls the Emacs default `forward-word'.
15977 See the individual commands for more information."
15978 (interactive "P")
15979 (cond
15980 ((run-hook-with-args-until-success 'org-metaright-hook))
15981 ((org-at-table-p) (call-interactively 'org-table-move-column))
15982 ((or (org-on-heading-p)
15983 (and (org-region-active-p)
15984 (save-excursion
15985 (goto-char (region-beginning))
15986 (org-on-heading-p))))
15987 (call-interactively 'org-do-demote))
15988 ((or (org-at-item-p)
15989 (and (org-region-active-p)
15990 (save-excursion
15991 (goto-char (region-beginning))
15992 (org-at-item-p))))
15993 (call-interactively 'org-indent-item))
15994 (t (call-interactively 'forward-word))))
15996 (defun org-metaup (&optional arg)
15997 "Move subtree up or move table row up.
15998 Calls `org-move-subtree-up' or `org-table-move-row' or
15999 `org-move-item-up', depending on context. See the individual commands
16000 for more information."
16001 (interactive "P")
16002 (cond
16003 ((run-hook-with-args-until-success 'org-metaup-hook))
16004 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
16005 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
16006 ((org-at-item-p) (call-interactively 'org-move-item-up))
16007 (t (transpose-lines 1) (beginning-of-line -1))))
16009 (defun org-metadown (&optional arg)
16010 "Move subtree down or move table row down.
16011 Calls `org-move-subtree-down' or `org-table-move-row' or
16012 `org-move-item-down', depending on context. See the individual
16013 commands for more information."
16014 (interactive "P")
16015 (cond
16016 ((run-hook-with-args-until-success 'org-metadown-hook))
16017 ((org-at-table-p) (call-interactively 'org-table-move-row))
16018 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
16019 ((org-at-item-p) (call-interactively 'org-move-item-down))
16020 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
16022 (defun org-shiftup (&optional arg)
16023 "Increase item in timestamp or increase priority of current headline.
16024 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
16025 depending on context. See the individual commands for more information."
16026 (interactive "P")
16027 (cond
16028 ((and org-support-shift-select (org-region-active-p))
16029 (org-call-for-shift-select 'previous-line))
16030 ((org-at-timestamp-p t)
16031 (call-interactively (if org-edit-timestamp-down-means-later
16032 'org-timestamp-down 'org-timestamp-up)))
16033 ((and (not (eq org-support-shift-select 'always))
16034 org-enable-priority-commands
16035 (org-on-heading-p))
16036 (call-interactively 'org-priority-up))
16037 ((and (not org-support-shift-select) (org-at-item-p))
16038 (call-interactively 'org-previous-item))
16039 ((org-clocktable-try-shift 'up arg))
16040 (org-support-shift-select
16041 (org-call-for-shift-select 'previous-line))
16042 (t (org-shiftselect-error))))
16044 (defun org-shiftdown (&optional arg)
16045 "Decrease item in timestamp or decrease priority of current headline.
16046 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
16047 depending on context. See the individual commands for more information."
16048 (interactive "P")
16049 (cond
16050 ((and org-support-shift-select (org-region-active-p))
16051 (org-call-for-shift-select 'next-line))
16052 ((org-at-timestamp-p t)
16053 (call-interactively (if org-edit-timestamp-down-means-later
16054 'org-timestamp-up 'org-timestamp-down)))
16055 ((and (not (eq org-support-shift-select 'always))
16056 org-enable-priority-commands
16057 (org-on-heading-p))
16058 (call-interactively 'org-priority-down))
16059 ((and (not org-support-shift-select) (org-at-item-p))
16060 (call-interactively 'org-next-item))
16061 ((org-clocktable-try-shift 'down arg))
16062 (org-support-shift-select
16063 (org-call-for-shift-select 'next-line))
16064 (t (org-shiftselect-error))))
16066 (defun org-shiftright (&optional arg)
16067 "Cycle the thing at point or in the current line, depending on context.
16068 Depending on context, this does one of the following:
16070 - switch a timestamp at point one day into the future
16071 - on a headline, switch to the next TODO keyword.
16072 - on an item, switch entire list to the next bullet type
16073 - on a property line, switch to the next allowed value
16074 - on a clocktable definition line, move time block into the future"
16075 (interactive "P")
16076 (cond
16077 ((and org-support-shift-select (org-region-active-p))
16078 (org-call-for-shift-select 'forward-char))
16079 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
16080 ((and (not (eq org-support-shift-select 'always))
16081 (org-on-heading-p))
16082 (let ((org-inhibit-logging
16083 (not org-treat-S-cursor-todo-selection-as-state-change))
16084 (org-inhibit-blocking
16085 (not org-treat-S-cursor-todo-selection-as-state-change)))
16086 (org-call-with-arg 'org-todo 'right)))
16087 ((or (and org-support-shift-select
16088 (not (eq org-support-shift-select 'always))
16089 (org-at-item-bullet-p))
16090 (and (not org-support-shift-select) (org-at-item-p)))
16091 (org-call-with-arg 'org-cycle-list-bullet nil))
16092 ((and (not (eq org-support-shift-select 'always))
16093 (org-at-property-p))
16094 (call-interactively 'org-property-next-allowed-value))
16095 ((org-clocktable-try-shift 'right arg))
16096 (org-support-shift-select
16097 (org-call-for-shift-select 'forward-char))
16098 (t (org-shiftselect-error))))
16100 (defun org-shiftleft (&optional arg)
16101 "Cycle the thing at point or in the current line, depending on context.
16102 Depending on context, this does one of the following:
16104 - switch a timestamp at point one day into the past
16105 - on a headline, switch to the previous TODO keyword.
16106 - on an item, switch entire list to the previous bullet type
16107 - on a property line, switch to the previous allowed value
16108 - on a clocktable definition line, move time block into the past"
16109 (interactive "P")
16110 (cond
16111 ((and org-support-shift-select (org-region-active-p))
16112 (org-call-for-shift-select 'backward-char))
16113 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
16114 ((and (not (eq org-support-shift-select 'always))
16115 (org-on-heading-p))
16116 (let ((org-inhibit-logging
16117 (not org-treat-S-cursor-todo-selection-as-state-change))
16118 (org-inhibit-blocking
16119 (not org-treat-S-cursor-todo-selection-as-state-change)))
16120 (org-call-with-arg 'org-todo 'left)))
16121 ((or (and org-support-shift-select
16122 (not (eq org-support-shift-select 'always))
16123 (org-at-item-bullet-p))
16124 (and (not org-support-shift-select) (org-at-item-p)))
16125 (org-call-with-arg 'org-cycle-list-bullet 'previous))
16126 ((and (not (eq org-support-shift-select 'always))
16127 (org-at-property-p))
16128 (call-interactively 'org-property-previous-allowed-value))
16129 ((org-clocktable-try-shift 'left arg))
16130 (org-support-shift-select
16131 (org-call-for-shift-select 'backward-char))
16132 (t (org-shiftselect-error))))
16134 (defun org-shiftcontrolright ()
16135 "Switch to next TODO set."
16136 (interactive)
16137 (cond
16138 ((and org-support-shift-select (org-region-active-p))
16139 (org-call-for-shift-select 'forward-word))
16140 ((and (not (eq org-support-shift-select 'always))
16141 (org-on-heading-p))
16142 (org-call-with-arg 'org-todo 'nextset))
16143 (org-support-shift-select
16144 (org-call-for-shift-select 'forward-word))
16145 (t (org-shiftselect-error))))
16147 (defun org-shiftcontrolleft ()
16148 "Switch to previous TODO set."
16149 (interactive)
16150 (cond
16151 ((and org-support-shift-select (org-region-active-p))
16152 (org-call-for-shift-select 'backward-word))
16153 ((and (not (eq org-support-shift-select 'always))
16154 (org-on-heading-p))
16155 (org-call-with-arg 'org-todo 'previousset))
16156 (org-support-shift-select
16157 (org-call-for-shift-select 'backward-word))
16158 (t (org-shiftselect-error))))
16160 (defun org-ctrl-c-ret ()
16161 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
16162 (interactive)
16163 (cond
16164 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
16165 (t (call-interactively 'org-insert-heading))))
16167 (defun org-copy-special ()
16168 "Copy region in table or copy current subtree.
16169 Calls `org-table-copy' or `org-copy-subtree', depending on context.
16170 See the individual commands for more information."
16171 (interactive)
16172 (call-interactively
16173 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
16175 (defun org-cut-special ()
16176 "Cut region in table or cut current subtree.
16177 Calls `org-table-copy' or `org-cut-subtree', depending on context.
16178 See the individual commands for more information."
16179 (interactive)
16180 (call-interactively
16181 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
16183 (defun org-paste-special (arg)
16184 "Paste rectangular region into table, or past subtree relative to level.
16185 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
16186 See the individual commands for more information."
16187 (interactive "P")
16188 (if (org-at-table-p)
16189 (org-table-paste-rectangle)
16190 (org-paste-subtree arg)))
16192 (defun org-edit-special ()
16193 "Call a special editor for the stuff at point.
16194 When at a table, call the formula editor with `org-table-edit-formulas'.
16195 When at the first line of an src example, call `org-edit-src-code'.
16196 When in an #+include line, visit the include file. Otherwise call
16197 `ffap' to visit the file at point."
16198 (interactive)
16199 (cond
16200 ((org-at-table.el-p)
16201 (org-edit-src-code))
16202 ((org-at-table-p)
16203 (call-interactively 'org-table-edit-formulas))
16204 ((save-excursion
16205 (beginning-of-line 1)
16206 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
16207 (find-file (org-trim (match-string 1))))
16208 ((org-edit-src-code))
16209 ((org-edit-fixed-width-region))
16210 (t (call-interactively 'ffap))))
16213 (defun org-ctrl-c-ctrl-c (&optional arg)
16214 "Set tags in headline, or update according to changed information at point.
16216 This command does many different things, depending on context:
16218 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
16219 this is what we do.
16221 - If the cursor is on a statistics cookie, update it.
16223 - If the cursor is in a headline, prompt for tags and insert them
16224 into the current line, aligned to `org-tags-column'. When called
16225 with prefix arg, realign all tags in the current buffer.
16227 - If the cursor is in one of the special #+KEYWORD lines, this
16228 triggers scanning the buffer for these lines and updating the
16229 information.
16231 - If the cursor is inside a table, realign the table. This command
16232 works even if the automatic table editor has been turned off.
16234 - If the cursor is on a #+TBLFM line, re-apply the formulas to
16235 the entire table.
16237 - If the cursor is at a footnote reference or definition, jump to
16238 the corresponding definition or references, respectively.
16240 - If the cursor is a the beginning of a dynamic block, update it.
16242 - If the current buffer is a remember buffer, close note and file
16243 it. A prefix argument of 1 files to the default location
16244 without further interaction. A prefix argument of 2 files to
16245 the currently clocking task.
16247 - If the cursor is on a <<<target>>>, update radio targets and corresponding
16248 links in this buffer.
16250 - If the cursor is on a numbered item in a plain list, renumber the
16251 ordered list.
16253 - If the cursor is on a checkbox, toggle it."
16254 (interactive "P")
16255 (let ((org-enable-table-editor t))
16256 (cond
16257 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
16258 org-occur-highlights
16259 org-latex-fragment-image-overlays)
16260 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
16261 (org-remove-occur-highlights)
16262 (org-remove-latex-fragment-image-overlays)
16263 (message "Temporary highlights/overlays removed from current buffer"))
16264 ((and (local-variable-p 'org-finish-function (current-buffer))
16265 (fboundp org-finish-function))
16266 (funcall org-finish-function))
16267 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
16268 ((or (looking-at (org-re org-property-start-re))
16269 (org-at-property-p))
16270 (call-interactively 'org-property-action))
16271 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
16272 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
16273 (or (org-on-heading-p) (org-at-item-p)))
16274 (call-interactively 'org-update-statistics-cookies))
16275 ((org-on-heading-p) (call-interactively 'org-set-tags))
16276 ((org-at-table.el-p)
16277 (message "Use C-c ' to edit table.el tables"))
16278 ((org-at-table-p)
16279 (org-table-maybe-eval-formula)
16280 (if arg
16281 (call-interactively 'org-table-recalculate)
16282 (org-table-maybe-recalculate-line))
16283 (call-interactively 'org-table-align))
16284 ((or (org-footnote-at-reference-p)
16285 (org-footnote-at-definition-p))
16286 (call-interactively 'org-footnote-action))
16287 ((org-at-item-checkbox-p)
16288 (call-interactively 'org-toggle-checkbox))
16289 ((org-at-item-p)
16290 (if arg
16291 (call-interactively 'org-toggle-checkbox)
16292 (call-interactively 'org-maybe-renumber-ordered-list)))
16293 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
16294 ;; Dynamic block
16295 (beginning-of-line 1)
16296 (save-excursion (org-update-dblock)))
16297 ((save-excursion
16298 (beginning-of-line 1)
16299 (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
16300 (cond
16301 ((equal (match-string 1) "TBLFM")
16302 ;; Recalculate the table before this line
16303 (save-excursion
16304 (beginning-of-line 1)
16305 (skip-chars-backward " \r\n\t")
16306 (if (org-at-table-p)
16307 (org-call-with-arg 'org-table-recalculate (or arg t)))))
16309 (let ((org-inhibit-startup-visibility-stuff t)
16310 (org-startup-align-all-tables nil))
16311 (org-save-outline-visibility 'use-markers (org-mode-restart)))
16312 (message "Local setup has been refreshed"))))
16313 ((org-clock-update-time-maybe))
16314 (t (error "C-c C-c can do nothing useful at this location")))))
16316 (defun org-mode-restart ()
16317 "Restart Org-mode, to scan again for special lines.
16318 Also updates the keyword regular expressions."
16319 (interactive)
16320 (org-mode)
16321 (message "Org-mode restarted"))
16323 (defun org-kill-note-or-show-branches ()
16324 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
16325 (interactive)
16326 (if (not org-finish-function)
16327 (call-interactively 'show-branches)
16328 (let ((org-note-abort t))
16329 (funcall org-finish-function))))
16331 (defun org-return (&optional indent)
16332 "Goto next table row or insert a newline.
16333 Calls `org-table-next-row' or `newline', depending on context.
16334 See the individual commands for more information."
16335 (interactive)
16336 (cond
16337 ((bobp) (if indent (newline-and-indent) (newline)))
16338 ((org-at-table-p)
16339 (org-table-justify-field-maybe)
16340 (call-interactively 'org-table-next-row))
16341 ((and org-return-follows-link
16342 (eq (get-text-property (point) 'face) 'org-link))
16343 (call-interactively 'org-open-at-point))
16344 ((and (org-at-heading-p)
16345 (looking-at
16346 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
16347 (org-show-entry)
16348 (end-of-line 1)
16349 (newline))
16350 (t (if indent (newline-and-indent) (newline)))))
16352 (defun org-return-indent ()
16353 "Goto next table row or insert a newline and indent.
16354 Calls `org-table-next-row' or `newline-and-indent', depending on
16355 context. See the individual commands for more information."
16356 (interactive)
16357 (org-return t))
16359 (defun org-ctrl-c-star ()
16360 "Compute table, or change heading status of lines.
16361 Calls `org-table-recalculate' or `org-toggle-heading',
16362 depending on context."
16363 (interactive)
16364 (cond
16365 ((org-at-table-p)
16366 (call-interactively 'org-table-recalculate))
16368 ;; Convert all lines in region to list items
16369 (call-interactively 'org-toggle-heading))))
16371 (defun org-ctrl-c-minus ()
16372 "Insert separator line in table or modify bullet status of line.
16373 Also turns a plain line or a region of lines into list items.
16374 Calls `org-table-insert-hline', `org-toggle-item', or
16375 `org-cycle-list-bullet', depending on context."
16376 (interactive)
16377 (cond
16378 ((org-at-table-p)
16379 (call-interactively 'org-table-insert-hline))
16380 ((org-region-active-p)
16381 (call-interactively 'org-toggle-item))
16382 ((org-in-item-p)
16383 (call-interactively 'org-cycle-list-bullet))
16385 (call-interactively 'org-toggle-item))))
16387 (defun org-toggle-item ()
16388 "Convert headings or normal lines to items, items to normal lines.
16389 If there is no active region, only the current line is considered.
16391 If the first line in the region is a headline, convert all headlines to items.
16393 If the first line in the region is an item, convert all items to normal lines.
16395 If the first line is normal text, add an item bullet to each line."
16396 (interactive)
16397 (let (l2 l beg end)
16398 (if (org-region-active-p)
16399 (setq beg (region-beginning) end (region-end))
16400 (setq beg (point-at-bol)
16401 end (min (1+ (point-at-eol)) (point-max))))
16402 (save-excursion
16403 (goto-char end)
16404 (setq l2 (org-current-line))
16405 (goto-char beg)
16406 (beginning-of-line 1)
16407 (setq l (1- (org-current-line)))
16408 (if (org-at-item-p)
16409 ;; We already have items, de-itemize
16410 (while (< (setq l (1+ l)) l2)
16411 (when (org-at-item-p)
16412 (goto-char (match-beginning 2))
16413 (delete-region (match-beginning 2) (match-end 2))
16414 (and (looking-at "[ \t]+") (replace-match "")))
16415 (beginning-of-line 2))
16416 (if (org-on-heading-p)
16417 ;; Headings, convert to items
16418 (while (< (setq l (1+ l)) l2)
16419 (if (looking-at org-outline-regexp)
16420 (replace-match "- " t t))
16421 (beginning-of-line 2))
16422 ;; normal lines, turn them into items
16423 (while (< (setq l (1+ l)) l2)
16424 (unless (org-at-item-p)
16425 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
16426 (replace-match "\\1- \\2")))
16427 (beginning-of-line 2)))))))
16429 (defun org-toggle-heading (&optional nstars)
16430 "Convert headings to normal text, or items or text to headings.
16431 If there is no active region, only the current line is considered.
16433 If the first line is a heading, remove the stars from all headlines
16434 in the region.
16436 If the first line is a plain list item, turn all plain list items
16437 into headings.
16439 If the first line is a normal line, turn each and every line in the
16440 region into a heading.
16442 When converting a line into a heading, the number of stars is chosen
16443 such that the lines become children of the current entry. However,
16444 when a prefix argument is given, its value determines the number of
16445 stars to add."
16446 (interactive "P")
16447 (let (l2 l itemp beg end)
16448 (if (org-region-active-p)
16449 (setq beg (region-beginning) end (region-end))
16450 (setq beg (point-at-bol)
16451 end (min (1+ (point-at-eol)) (point-max))))
16452 (save-excursion
16453 (goto-char end)
16454 (setq l2 (org-current-line))
16455 (goto-char beg)
16456 (beginning-of-line 1)
16457 (setq l (1- (org-current-line)))
16458 (if (org-on-heading-p)
16459 ;; We already have headlines, de-star them
16460 (while (< (setq l (1+ l)) l2)
16461 (when (org-on-heading-p t)
16462 (and (looking-at outline-regexp) (replace-match "")))
16463 (beginning-of-line 2))
16464 (setq itemp (org-at-item-p))
16465 (let* ((stars
16466 (if nstars
16467 (make-string (prefix-numeric-value current-prefix-arg)
16469 (save-excursion
16470 (if (re-search-backward org-complex-heading-regexp nil t)
16471 (match-string 1) ""))))
16472 (add-stars (cond (nstars "")
16473 ((equal stars "") "*")
16474 (org-odd-levels-only "**")
16475 (t "*")))
16476 (rpl (concat stars add-stars " ")))
16477 (while (< (setq l (1+ l)) l2)
16478 (if itemp
16479 (and (org-at-item-p) (replace-match rpl t t))
16480 (unless (org-on-heading-p)
16481 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
16482 (replace-match (concat rpl (match-string 2))))))
16483 (beginning-of-line 2)))))))
16485 (defun org-meta-return (&optional arg)
16486 "Insert a new heading or wrap a region in a table.
16487 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
16488 See the individual commands for more information."
16489 (interactive "P")
16490 (cond
16491 ((run-hook-with-args-until-success 'org-metareturn-hook))
16492 ((org-at-table-p)
16493 (call-interactively 'org-table-wrap-region))
16494 (t (call-interactively 'org-insert-heading))))
16496 ;;; Menu entries
16498 ;; Define the Org-mode menus
16499 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
16500 '("Tbl"
16501 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
16502 ["Next Field" org-cycle (org-at-table-p)]
16503 ["Previous Field" org-shifttab (org-at-table-p)]
16504 ["Next Row" org-return (org-at-table-p)]
16505 "--"
16506 ["Blank Field" org-table-blank-field (org-at-table-p)]
16507 ["Edit Field" org-table-edit-field (org-at-table-p)]
16508 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
16509 "--"
16510 ("Column"
16511 ["Move Column Left" org-metaleft (org-at-table-p)]
16512 ["Move Column Right" org-metaright (org-at-table-p)]
16513 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
16514 ["Insert Column" org-shiftmetaright (org-at-table-p)])
16515 ("Row"
16516 ["Move Row Up" org-metaup (org-at-table-p)]
16517 ["Move Row Down" org-metadown (org-at-table-p)]
16518 ["Delete Row" org-shiftmetaup (org-at-table-p)]
16519 ["Insert Row" org-shiftmetadown (org-at-table-p)]
16520 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
16521 "--"
16522 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
16523 ("Rectangle"
16524 ["Copy Rectangle" org-copy-special (org-at-table-p)]
16525 ["Cut Rectangle" org-cut-special (org-at-table-p)]
16526 ["Paste Rectangle" org-paste-special (org-at-table-p)]
16527 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
16528 "--"
16529 ("Calculate"
16530 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
16531 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
16532 ["Edit Formulas" org-edit-special (org-at-table-p)]
16533 "--"
16534 ["Recalculate line" org-table-recalculate (org-at-table-p)]
16535 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
16536 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
16537 "--"
16538 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
16539 "--"
16540 ["Sum Column/Rectangle" org-table-sum
16541 (or (org-at-table-p) (org-region-active-p))]
16542 ["Which Column?" org-table-current-column (org-at-table-p)])
16543 ["Debug Formulas"
16544 org-table-toggle-formula-debugger
16545 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
16546 ["Show Col/Row Numbers"
16547 org-table-toggle-coordinate-overlays
16548 :style toggle
16549 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
16550 "--"
16551 ["Create" org-table-create (and (not (org-at-table-p))
16552 org-enable-table-editor)]
16553 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
16554 ["Import from File" org-table-import (not (org-at-table-p))]
16555 ["Export to File" org-table-export (org-at-table-p)]
16556 "--"
16557 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
16559 (easy-menu-define org-org-menu org-mode-map "Org menu"
16560 '("Org"
16561 ("Show/Hide"
16562 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
16563 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
16564 ["Sparse Tree..." org-sparse-tree t]
16565 ["Reveal Context" org-reveal t]
16566 ["Show All" show-all t]
16567 "--"
16568 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
16569 "--"
16570 ["New Heading" org-insert-heading t]
16571 ("Navigate Headings"
16572 ["Up" outline-up-heading t]
16573 ["Next" outline-next-visible-heading t]
16574 ["Previous" outline-previous-visible-heading t]
16575 ["Next Same Level" outline-forward-same-level t]
16576 ["Previous Same Level" outline-backward-same-level t]
16577 "--"
16578 ["Jump" org-goto t])
16579 ("Edit Structure"
16580 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
16581 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
16582 "--"
16583 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
16584 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
16585 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
16586 "--"
16587 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
16588 "--"
16589 ["Promote Heading" org-metaleft (not (org-at-table-p))]
16590 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
16591 ["Demote Heading" org-metaright (not (org-at-table-p))]
16592 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
16593 "--"
16594 ["Sort Region/Children" org-sort (not (org-at-table-p))]
16595 "--"
16596 ["Convert to odd levels" org-convert-to-odd-levels t]
16597 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
16598 ("Editing"
16599 ["Emphasis..." org-emphasize t]
16600 ["Edit Source Example" org-edit-special t]
16601 "--"
16602 ["Footnote new/jump" org-footnote-action t]
16603 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
16604 ("Archive"
16605 ["Archive (default method)" org-archive-subtree-default t]
16606 "--"
16607 ["Move Subtree to Archive file" org-advertized-archive-subtree t]
16608 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
16609 ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
16611 "--"
16612 ("Hyperlinks"
16613 ["Store Link (Global)" org-store-link t]
16614 ["Find existing link to here" org-occur-link-in-agenda-files t]
16615 ["Insert Link" org-insert-link t]
16616 ["Follow Link" org-open-at-point t]
16617 "--"
16618 ["Next link" org-next-link t]
16619 ["Previous link" org-previous-link t]
16620 "--"
16621 ["Descriptive Links"
16622 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
16623 :style radio
16624 :selected (member '(org-link) buffer-invisibility-spec)]
16625 ["Literal Links"
16626 (progn
16627 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
16628 :style radio
16629 :selected (not (member '(org-link) buffer-invisibility-spec))])
16630 "--"
16631 ("TODO Lists"
16632 ["TODO/DONE/-" org-todo t]
16633 ("Select keyword"
16634 ["Next keyword" org-shiftright (org-on-heading-p)]
16635 ["Previous keyword" org-shiftleft (org-on-heading-p)]
16636 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
16637 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
16638 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
16639 ["Show TODO Tree" org-show-todo-tree t]
16640 ["Global TODO list" org-todo-list t]
16641 "--"
16642 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
16643 :selected org-enforce-todo-dependencies :style toggle :active t]
16644 "Settings for tree at point"
16645 ["Do Children sequentially" org-toggle-ordered-property :style radio
16646 :selected (ignore-errors (org-entry-get nil "ORDERED"))
16647 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
16648 ["Do Children parallel" org-toggle-ordered-property :style radio
16649 :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
16650 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
16651 "--"
16652 ["Set Priority" org-priority t]
16653 ["Priority Up" org-shiftup t]
16654 ["Priority Down" org-shiftdown t]
16655 "--"
16656 ["Get news from all feeds" org-feed-update-all t]
16657 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
16658 ["Customize feeds" (customize-variable 'org-feed-alist) t])
16659 ("TAGS and Properties"
16660 ["Set Tags" org-set-tags-command t]
16661 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
16662 "--"
16663 ["Set property" org-set-property t]
16664 ["Column view of properties" org-columns t]
16665 ["Insert Column View DBlock" org-insert-columns-dblock t])
16666 ("Dates and Scheduling"
16667 ["Timestamp" org-time-stamp t]
16668 ["Timestamp (inactive)" org-time-stamp-inactive t]
16669 ("Change Date"
16670 ["1 Day Later" org-shiftright t]
16671 ["1 Day Earlier" org-shiftleft t]
16672 ["1 ... Later" org-shiftup t]
16673 ["1 ... Earlier" org-shiftdown t])
16674 ["Compute Time Range" org-evaluate-time-range t]
16675 ["Schedule Item" org-schedule t]
16676 ["Deadline" org-deadline t]
16677 "--"
16678 ["Custom time format" org-toggle-time-stamp-overlays
16679 :style radio :selected org-display-custom-times]
16680 "--"
16681 ["Goto Calendar" org-goto-calendar t]
16682 ["Date from Calendar" org-date-from-calendar t]
16683 "--"
16684 ["Start/Restart Timer" org-timer-start t]
16685 ["Pause/Continue Timer" org-timer-pause-or-continue t]
16686 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
16687 ["Insert Timer String" org-timer t]
16688 ["Insert Timer Item" org-timer-item t])
16689 ("Logging work"
16690 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
16691 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
16692 ["Clock out" org-clock-out t]
16693 ["Clock cancel" org-clock-cancel t]
16694 "--"
16695 ["Mark as default task" org-clock-mark-default-task t]
16696 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
16697 ["Goto running clock" org-clock-goto t]
16698 "--"
16699 ["Display times" org-clock-display t]
16700 ["Create clock table" org-clock-report t]
16701 "--"
16702 ["Record DONE time"
16703 (progn (setq org-log-done (not org-log-done))
16704 (message "Switching to %s will %s record a timestamp"
16705 (car org-done-keywords)
16706 (if org-log-done "automatically" "not")))
16707 :style toggle :selected org-log-done])
16708 "--"
16709 ["Agenda Command..." org-agenda t]
16710 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
16711 ("File List for Agenda")
16712 ("Special views current file"
16713 ["TODO Tree" org-show-todo-tree t]
16714 ["Check Deadlines" org-check-deadlines t]
16715 ["Timeline" org-timeline t]
16716 ["Tags/Property tree" org-match-sparse-tree t])
16717 "--"
16718 ["Export/Publish..." org-export t]
16719 ("LaTeX"
16720 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
16721 :selected org-cdlatex-mode]
16722 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
16723 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
16724 ["Modify math symbol" org-cdlatex-math-modify
16725 (org-inside-LaTeX-fragment-p)]
16726 ["Insert citation" org-reftex-citation t]
16727 "--"
16728 ["Export LaTeX fragments as images"
16729 (if (featurep 'org-exp)
16730 (setq org-export-with-LaTeX-fragments
16731 (not org-export-with-LaTeX-fragments))
16732 (require 'org-exp))
16733 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
16734 org-export-with-LaTeX-fragments)]
16735 "--"
16736 ["Template for BEAMER" org-beamer-settings-template t])
16737 "--"
16738 ("MobileOrg"
16739 ["Push Files and Views" org-mobile-push t]
16740 ["Get Captured and Flagged" org-mobile-pull t]
16741 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
16742 "--"
16743 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
16744 "--"
16745 ("Documentation"
16746 ["Show Version" org-version t]
16747 ["Info Documentation" org-info t])
16748 ("Customize"
16749 ["Browse Org Group" org-customize t]
16750 "--"
16751 ["Expand This Menu" org-create-customize-menu
16752 (fboundp 'customize-menu-create)])
16753 ["Send bug report" org-submit-bug-report t]
16754 "--"
16755 ("Refresh/Reload"
16756 ["Refresh setup current buffer" org-mode-restart t]
16757 ["Reload Org (after update)" org-reload t]
16758 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
16761 (defun org-info (&optional node)
16762 "Read documentation for Org-mode in the info system.
16763 With optional NODE, go directly to that node."
16764 (interactive)
16765 (info (format "(org)%s" (or node ""))))
16767 ;;;###autoload
16768 (defun org-submit-bug-report ()
16769 "Submit a bug report on Org-mode via mail.
16771 Don't hesitate to report any problems or inaccurate documentation.
16773 If you don't have setup sending mail from (X)Emacs, please copy the
16774 output buffer into your mail program, as it gives us important
16775 information about your Org-mode version and configuration."
16776 (interactive)
16777 (require 'reporter)
16778 (org-load-modules-maybe)
16779 (org-require-autoloaded-modules)
16780 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
16781 (reporter-submit-bug-report
16782 "emacs-orgmode@gnu.org"
16783 (org-version)
16784 (let (list)
16785 (save-window-excursion
16786 (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
16787 (delete-other-windows)
16788 (erase-buffer)
16789 (insert "You are about to submit a bug report to the Org-mode mailing list.
16791 We would like to add your full Org-mode and Outline configuration to the
16792 bug report. This greatly simplifies the work of the maintainer and
16793 other experts on the mailing list.
16795 HOWEVER, some variables you have customized may contain private
16796 information. The names of customers, colleagues, or friends, might
16797 appear in the form of file names, tags, todo states, or search strings.
16798 If you answer yes to the prompt, you might want to check and remove
16799 such private information before sending the email.")
16800 (add-text-properties (point-min) (point-max) '(face org-warning))
16801 (when (yes-or-no-p "Include your Org-mode configuration ")
16802 (mapatoms
16803 (lambda (v)
16804 (and (boundp v)
16805 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
16806 (or (and (symbol-value v)
16807 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
16808 (and
16809 (get v 'custom-type) (get v 'standard-value)
16810 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
16811 (push v list)))))
16812 (kill-buffer (get-buffer "*Warn about privacy*"))
16813 list))
16814 nil nil
16815 "Remember to cover the basics, that is, what you expected to happen and
16816 what in fact did happen. You don't know how to make a good report? See
16818 http://orgmode.org/manual/Feedback.html#Feedback
16820 Your bug report will be posted to the Org-mode mailing list.
16821 ------------------------------------------------------------------------")
16822 (save-excursion
16823 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
16824 (replace-match "\\1Bug: \\3 [\\2]")))))
16827 (defun org-install-agenda-files-menu ()
16828 (let ((bl (buffer-list)))
16829 (save-excursion
16830 (while bl
16831 (set-buffer (pop bl))
16832 (if (org-mode-p) (setq bl nil)))
16833 (when (org-mode-p)
16834 (easy-menu-change
16835 '("Org") "File List for Agenda"
16836 (append
16837 (list
16838 ["Edit File List" (org-edit-agenda-file-list) t]
16839 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
16840 ["Remove Current File from List" org-remove-file t]
16841 ["Cycle through agenda files" org-cycle-agenda-files t]
16842 ["Occur in all agenda files" org-occur-in-agenda-files t]
16843 "--")
16844 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
16846 ;;;; Documentation
16848 ;;;###autoload
16849 (defun org-require-autoloaded-modules ()
16850 (interactive)
16851 (mapc 'require
16852 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
16853 org-docbook org-exp org-html org-icalendar
16854 org-id org-latex
16855 org-publish org-remember org-table
16856 org-timer org-xoxo)))
16858 ;;;###autoload
16859 (defun org-reload (&optional uncompiled)
16860 "Reload all org lisp files.
16861 With prefix arg UNCOMPILED, load the uncompiled versions."
16862 (interactive "P")
16863 (require 'find-func)
16864 (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
16865 (dir-org (file-name-directory (org-find-library-name "org")))
16866 (dir-org-contrib (ignore-errors
16867 (file-name-directory
16868 (org-find-library-name "org-contribdir"))))
16869 (files
16870 (append (directory-files dir-org t file-re)
16871 (and dir-org-contrib
16872 (directory-files dir-org-contrib t file-re))))
16873 (remove-re (concat (if (featurep 'xemacs)
16874 "org-colview" "org-colview-xemacs")
16875 "\\'")))
16876 (setq files (mapcar 'file-name-sans-extension files))
16877 (setq files (mapcar
16878 (lambda (x) (if (string-match remove-re x) nil x))
16879 files))
16880 (setq files (delq nil files))
16881 (mapc
16882 (lambda (f)
16883 (when (featurep (intern (file-name-nondirectory f)))
16884 (if (and (not uncompiled)
16885 (file-exists-p (concat f ".elc")))
16886 (load (concat f ".elc") nil nil t)
16887 (load (concat f ".el") nil nil t))))
16888 files))
16889 (org-version))
16891 ;;;###autoload
16892 (defun org-customize ()
16893 "Call the customize function with org as argument."
16894 (interactive)
16895 (org-load-modules-maybe)
16896 (org-require-autoloaded-modules)
16897 (customize-browse 'org))
16899 (defun org-create-customize-menu ()
16900 "Create a full customization menu for Org-mode, insert it into the menu."
16901 (interactive)
16902 (org-load-modules-maybe)
16903 (org-require-autoloaded-modules)
16904 (if (fboundp 'customize-menu-create)
16905 (progn
16906 (easy-menu-change
16907 '("Org") "Customize"
16908 `(["Browse Org group" org-customize t]
16909 "--"
16910 ,(customize-menu-create 'org)
16911 ["Set" Custom-set t]
16912 ["Save" Custom-save t]
16913 ["Reset to Current" Custom-reset-current t]
16914 ["Reset to Saved" Custom-reset-saved t]
16915 ["Reset to Standard Settings" Custom-reset-standard t]))
16916 (message "\"Org\"-menu now contains full customization menu"))
16917 (error "Cannot expand menu (outdated version of cus-edit.el)")))
16919 ;;;; Miscellaneous stuff
16921 ;;; Generally useful functions
16923 (defun org-get-at-bol (property)
16924 "Get text property PROPERTY at beginning of line."
16925 (get-text-property (point-at-bol) property))
16927 (defun org-find-text-property-in-string (prop s)
16928 "Return the first non-nil value of property PROP in string S."
16929 (or (get-text-property 0 prop s)
16930 (get-text-property (or (next-single-property-change 0 prop s) 0)
16931 prop s)))
16933 (defun org-display-warning (message) ;; Copied from Emacs-Muse
16934 "Display the given MESSAGE as a warning."
16935 (if (fboundp 'display-warning)
16936 (display-warning 'org message
16937 (if (featurep 'xemacs)
16938 'warning
16939 :warning))
16940 (let ((buf (get-buffer-create "*Org warnings*")))
16941 (with-current-buffer buf
16942 (goto-char (point-max))
16943 (insert "Warning (Org): " message)
16944 (unless (bolp)
16945 (newline)))
16946 (display-buffer buf)
16947 (sit-for 0))))
16949 (defun org-in-commented-line ()
16950 "Is point in a line starting with `#'?"
16951 (equal (char-after (point-at-bol)) ?#))
16953 (defun org-in-verbatim-emphasis ()
16954 (save-match-data
16955 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
16957 (defun org-goto-marker-or-bmk (marker &optional bookmark)
16958 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
16959 (if (and marker (marker-buffer marker)
16960 (buffer-live-p (marker-buffer marker)))
16961 (progn
16962 (switch-to-buffer (marker-buffer marker))
16963 (if (or (> marker (point-max)) (< marker (point-min)))
16964 (widen))
16965 (goto-char marker)
16966 (org-show-context 'org-goto))
16967 (if bookmark
16968 (bookmark-jump bookmark)
16969 (error "Cannot find location"))))
16971 (defun org-quote-csv-field (s)
16972 "Quote field for inclusion in CSV material."
16973 (if (string-match "[\",]" s)
16974 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
16977 (defun org-plist-delete (plist property)
16978 "Delete PROPERTY from PLIST.
16979 This is in contrast to merely setting it to 0."
16980 (let (p)
16981 (while plist
16982 (if (not (eq property (car plist)))
16983 (setq p (plist-put p (car plist) (nth 1 plist))))
16984 (setq plist (cddr plist)))
16987 (defun org-force-self-insert (N)
16988 "Needed to enforce self-insert under remapping."
16989 (interactive "p")
16990 (self-insert-command N))
16992 (defun org-string-width (s)
16993 "Compute width of string, ignoring invisible characters.
16994 This ignores character with invisibility property `org-link', and also
16995 characters with property `org-cwidth', because these will become invisible
16996 upon the next fontification round."
16997 (let (b l)
16998 (when (or (eq t buffer-invisibility-spec)
16999 (assq 'org-link buffer-invisibility-spec))
17000 (while (setq b (text-property-any 0 (length s)
17001 'invisible 'org-link s))
17002 (setq s (concat (substring s 0 b)
17003 (substring s (or (next-single-property-change
17004 b 'invisible s) (length s)))))))
17005 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
17006 (setq s (concat (substring s 0 b)
17007 (substring s (or (next-single-property-change
17008 b 'org-cwidth s) (length s))))))
17009 (setq l (string-width s) b -1)
17010 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
17011 (setq l (- l (get-text-property b 'org-dwidth-n s))))
17014 (defun org-get-indentation (&optional line)
17015 "Get the indentation of the current line, interpreting tabs.
17016 When LINE is given, assume it represents a line and compute its indentation."
17017 (if line
17018 (if (string-match "^ *" (org-remove-tabs line))
17019 (match-end 0))
17020 (save-excursion
17021 (beginning-of-line 1)
17022 (skip-chars-forward " \t")
17023 (current-column))))
17025 (defun org-remove-tabs (s &optional width)
17026 "Replace tabulators in S with spaces.
17027 Assumes that s is a single line, starting in column 0."
17028 (setq width (or width tab-width))
17029 (while (string-match "\t" s)
17030 (setq s (replace-match
17031 (make-string
17032 (- (* width (/ (+ (match-beginning 0) width) width))
17033 (match-beginning 0)) ?\ )
17034 t t s)))
17037 (defun org-fix-indentation (line ind)
17038 "Fix indentation in LINE.
17039 IND is a cons cell with target and minimum indentation.
17040 If the current indentation in LINE is smaller than the minimum,
17041 leave it alone. If it is larger than ind, set it to the target."
17042 (let* ((l (org-remove-tabs line))
17043 (i (org-get-indentation l))
17044 (i1 (car ind)) (i2 (cdr ind)))
17045 (if (>= i i2) (setq l (substring line i2)))
17046 (if (> i1 0)
17047 (concat (make-string i1 ?\ ) l)
17048 l)))
17050 (defun org-remove-indentation (code &optional n)
17051 "Remove the maximum common indentation from the lines in CODE.
17052 N may optionally be the number of spaces to remove."
17053 (with-temp-buffer
17054 (insert code)
17055 (org-do-remove-indentation n)
17056 (buffer-string)))
17058 (defun org-do-remove-indentation (&optional n)
17059 "Remove the maximum common indentation from the buffer."
17060 (untabify (point-min) (point-max))
17061 (let ((min 10000) re)
17062 (if n
17063 (setq min n)
17064 (goto-char (point-min))
17065 (while (re-search-forward "^ *[^ \n]" nil t)
17066 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
17067 (unless (or (= min 0) (= min 10000))
17068 (setq re (format "^ \\{%d\\}" min))
17069 (goto-char (point-min))
17070 (while (re-search-forward re nil t)
17071 (replace-match "")
17072 (end-of-line 1))
17073 min)))
17075 (defun org-fill-template (template alist)
17076 "Find each %key of ALIST in TEMPLATE and replace it."
17077 (let ((case-fold-search nil)
17078 entry key value)
17079 (setq alist (sort (copy-sequence alist)
17080 (lambda (a b) (< (length (car a)) (length (car b))))))
17081 (while (setq entry (pop alist))
17082 (setq template
17083 (replace-regexp-in-string
17084 (concat "%" (regexp-quote (car entry)))
17085 (cdr entry) template t t)))
17086 template))
17088 (defun org-base-buffer (buffer)
17089 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
17090 (if (not buffer)
17091 buffer
17092 (or (buffer-base-buffer buffer)
17093 buffer)))
17095 (defun org-trim (s)
17096 "Remove whitespace at beginning and end of string."
17097 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
17098 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
17101 (defun org-wrap (string &optional width lines)
17102 "Wrap string to either a number of lines, or a width in characters.
17103 If WIDTH is non-nil, the string is wrapped to that width, however many lines
17104 that costs. If there is a word longer than WIDTH, the text is actually
17105 wrapped to the length of that word.
17106 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
17107 many lines, whatever width that takes.
17108 The return value is a list of lines, without newlines at the end."
17109 (let* ((words (org-split-string string "[ \t\n]+"))
17110 (maxword (apply 'max (mapcar 'org-string-width words)))
17111 w ll)
17112 (cond (width
17113 (org-do-wrap words (max maxword width)))
17114 (lines
17115 (setq w maxword)
17116 (setq ll (org-do-wrap words maxword))
17117 (if (<= (length ll) lines)
17119 (setq ll words)
17120 (while (> (length ll) lines)
17121 (setq w (1+ w))
17122 (setq ll (org-do-wrap words w)))
17123 ll))
17124 (t (error "Cannot wrap this")))))
17126 (defun org-do-wrap (words width)
17127 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
17128 (let (lines line)
17129 (while words
17130 (setq line (pop words))
17131 (while (and words (< (+ (length line) (length (car words))) width))
17132 (setq line (concat line " " (pop words))))
17133 (setq lines (push line lines)))
17134 (nreverse lines)))
17136 (defun org-split-string (string &optional separators)
17137 "Splits STRING into substrings at SEPARATORS.
17138 No empty strings are returned if there are matches at the beginning
17139 and end of string."
17140 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
17141 (start 0)
17142 notfirst
17143 (list nil))
17144 (while (and (string-match rexp string
17145 (if (and notfirst
17146 (= start (match-beginning 0))
17147 (< start (length string)))
17148 (1+ start) start))
17149 (< (match-beginning 0) (length string)))
17150 (setq notfirst t)
17151 (or (eq (match-beginning 0) 0)
17152 (and (eq (match-beginning 0) (match-end 0))
17153 (eq (match-beginning 0) start))
17154 (setq list
17155 (cons (substring string start (match-beginning 0))
17156 list)))
17157 (setq start (match-end 0)))
17158 (or (eq start (length string))
17159 (setq list
17160 (cons (substring string start)
17161 list)))
17162 (nreverse list)))
17164 (defun org-quote-vert (s)
17165 "Replace \"|\" with \"\\vert\"."
17166 (while (string-match "|" s)
17167 (setq s (replace-match "\\vert" t t s)))
17170 (defun org-uuidgen-p (s)
17171 "Is S an ID created by UUIDGEN?"
17172 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
17174 (defun org-context ()
17175 "Return a list of contexts of the current cursor position.
17176 If several contexts apply, all are returned.
17177 Each context entry is a list with a symbol naming the context, and
17178 two positions indicating start and end of the context. Possible
17179 contexts are:
17181 :headline anywhere in a headline
17182 :headline-stars on the leading stars in a headline
17183 :todo-keyword on a TODO keyword (including DONE) in a headline
17184 :tags on the TAGS in a headline
17185 :priority on the priority cookie in a headline
17186 :item on the first line of a plain list item
17187 :item-bullet on the bullet/number of a plain list item
17188 :checkbox on the checkbox in a plain list item
17189 :table in an org-mode table
17190 :table-special on a special filed in a table
17191 :table-table in a table.el table
17192 :link on a hyperlink
17193 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
17194 :target on a <<target>>
17195 :radio-target on a <<<radio-target>>>
17196 :latex-fragment on a LaTeX fragment
17197 :latex-preview on a LaTeX fragment with overlayed preview image
17199 This function expects the position to be visible because it uses font-lock
17200 faces as a help to recognize the following contexts: :table-special, :link,
17201 and :keyword."
17202 (let* ((f (get-text-property (point) 'face))
17203 (faces (if (listp f) f (list f)))
17204 (p (point)) clist o)
17205 ;; First the large context
17206 (cond
17207 ((org-on-heading-p t)
17208 (push (list :headline (point-at-bol) (point-at-eol)) clist)
17209 (when (progn
17210 (beginning-of-line 1)
17211 (looking-at org-todo-line-tags-regexp))
17212 (push (org-point-in-group p 1 :headline-stars) clist)
17213 (push (org-point-in-group p 2 :todo-keyword) clist)
17214 (push (org-point-in-group p 4 :tags) clist))
17215 (goto-char p)
17216 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
17217 (if (looking-at "\\[#[A-Z0-9]\\]")
17218 (push (org-point-in-group p 0 :priority) clist)))
17220 ((org-at-item-p)
17221 (push (org-point-in-group p 2 :item-bullet) clist)
17222 (push (list :item (point-at-bol)
17223 (save-excursion (org-end-of-item) (point)))
17224 clist)
17225 (and (org-at-item-checkbox-p)
17226 (push (org-point-in-group p 0 :checkbox) clist)))
17228 ((org-at-table-p)
17229 (push (list :table (org-table-begin) (org-table-end)) clist)
17230 (if (memq 'org-formula faces)
17231 (push (list :table-special
17232 (previous-single-property-change p 'face)
17233 (next-single-property-change p 'face)) clist)))
17234 ((org-at-table-p 'any)
17235 (push (list :table-table) clist)))
17236 (goto-char p)
17238 ;; Now the small context
17239 (cond
17240 ((org-at-timestamp-p)
17241 (push (org-point-in-group p 0 :timestamp) clist))
17242 ((memq 'org-link faces)
17243 (push (list :link
17244 (previous-single-property-change p 'face)
17245 (next-single-property-change p 'face)) clist))
17246 ((memq 'org-special-keyword faces)
17247 (push (list :keyword
17248 (previous-single-property-change p 'face)
17249 (next-single-property-change p 'face)) clist))
17250 ((org-on-target-p)
17251 (push (org-point-in-group p 0 :target) clist)
17252 (goto-char (1- (match-beginning 0)))
17253 (if (looking-at org-radio-target-regexp)
17254 (push (org-point-in-group p 0 :radio-target) clist))
17255 (goto-char p))
17256 ((setq o (car (delq nil
17257 (mapcar
17258 (lambda (x)
17259 (if (memq x org-latex-fragment-image-overlays) x))
17260 (org-overlays-at (point))))))
17261 (push (list :latex-fragment
17262 (org-overlay-start o) (org-overlay-end o)) clist)
17263 (push (list :latex-preview
17264 (org-overlay-start o) (org-overlay-end o)) clist))
17265 ((org-inside-LaTeX-fragment-p)
17266 ;; FIXME: positions wrong.
17267 (push (list :latex-fragment (point) (point)) clist)))
17269 (setq clist (nreverse (delq nil clist)))
17270 clist))
17272 ;; FIXME: Compare with at-regexp-p Do we need both?
17273 (defun org-in-regexp (re &optional nlines visually)
17274 "Check if point is inside a match of regexp.
17275 Normally only the current line is checked, but you can include NLINES extra
17276 lines both before and after point into the search.
17277 If VISUALLY is set, require that the cursor is not after the match but
17278 really on, so that the block visually is on the match."
17279 (catch 'exit
17280 (let ((pos (point))
17281 (eol (point-at-eol (+ 1 (or nlines 0))))
17282 (inc (if visually 1 0)))
17283 (save-excursion
17284 (beginning-of-line (- 1 (or nlines 0)))
17285 (while (re-search-forward re eol t)
17286 (if (and (<= (match-beginning 0) pos)
17287 (>= (+ inc (match-end 0)) pos))
17288 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
17290 (defun org-at-regexp-p (regexp)
17291 "Is point inside a match of REGEXP in the current line?"
17292 (catch 'exit
17293 (save-excursion
17294 (let ((pos (point)) (end (point-at-eol)))
17295 (beginning-of-line 1)
17296 (while (re-search-forward regexp end t)
17297 (if (and (<= (match-beginning 0) pos)
17298 (>= (match-end 0) pos))
17299 (throw 'exit t)))
17300 nil))))
17302 (defun org-in-regexps-block-p (start-re end-re)
17303 "Returns t if the current point is between matches of START-RE and END-RE.
17304 This will also return to if point is on one of the two matches."
17305 (interactive)
17306 (let ((p (point)))
17307 (save-excursion
17308 (and (or (org-at-regexp-p start-re)
17309 (re-search-backward start-re nil t))
17310 (re-search-forward end-re nil t)
17311 (>= (point) p)))))
17313 (defun org-occur-in-agenda-files (regexp &optional nlines)
17314 "Call `multi-occur' with buffers for all agenda files."
17315 (interactive "sOrg-files matching: \np")
17316 (let* ((files (org-agenda-files))
17317 (tnames (mapcar 'file-truename files))
17318 (extra org-agenda-text-search-extra-files)
17320 (when (eq (car extra) 'agenda-archives)
17321 (setq extra (cdr extra))
17322 (setq files (org-add-archive-files files)))
17323 (while (setq f (pop extra))
17324 (unless (member (file-truename f) tnames)
17325 (add-to-list 'files f 'append)
17326 (add-to-list 'tnames (file-truename f) 'append)))
17327 (multi-occur
17328 (mapcar (lambda (x)
17329 (with-current-buffer
17330 (or (get-file-buffer x) (find-file-noselect x))
17331 (widen)
17332 (current-buffer)))
17333 files)
17334 regexp)))
17336 (if (boundp 'occur-mode-find-occurrence-hook)
17337 ;; Emacs 23
17338 (add-hook 'occur-mode-find-occurrence-hook
17339 (lambda ()
17340 (when (org-mode-p)
17341 (org-reveal))))
17342 ;; Emacs 22
17343 (defadvice occur-mode-goto-occurrence
17344 (after org-occur-reveal activate)
17345 (and (org-mode-p) (org-reveal)))
17346 (defadvice occur-mode-goto-occurrence-other-window
17347 (after org-occur-reveal activate)
17348 (and (org-mode-p) (org-reveal)))
17349 (defadvice occur-mode-display-occurrence
17350 (after org-occur-reveal activate)
17351 (when (org-mode-p)
17352 (let ((pos (occur-mode-find-occurrence)))
17353 (with-current-buffer (marker-buffer pos)
17354 (save-excursion
17355 (goto-char pos)
17356 (org-reveal)))))))
17358 (defun org-occur-link-in-agenda-files ()
17359 "Create a link and search for it in the agendas.
17360 The link is not stored in `org-stored-links', it is just created
17361 for the search purpose."
17362 (interactive)
17363 (let ((link (condition-case nil
17364 (org-store-link nil)
17365 (error "Unable to create a link to here"))))
17366 (org-occur-in-agenda-files (regexp-quote link))))
17368 (defun org-uniquify (list)
17369 "Remove duplicate elements from LIST."
17370 (let (res)
17371 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
17372 res))
17374 (defun org-delete-all (elts list)
17375 "Remove all elements in ELTS from LIST."
17376 (while elts
17377 (setq list (delete (pop elts) list)))
17378 list)
17380 (defun org-back-over-empty-lines ()
17381 "Move backwards over whitespace, to the beginning of the first empty line.
17382 Returns the number of empty lines passed."
17383 (let ((pos (point)))
17384 (skip-chars-backward " \t\n\r")
17385 (beginning-of-line 2)
17386 (goto-char (min (point) pos))
17387 (count-lines (point) pos)))
17389 (defun org-skip-whitespace ()
17390 (skip-chars-forward " \t\n\r"))
17392 (defun org-point-in-group (point group &optional context)
17393 "Check if POINT is in match-group GROUP.
17394 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
17395 match. If the match group does ot exist or point is not inside it,
17396 return nil."
17397 (and (match-beginning group)
17398 (>= point (match-beginning group))
17399 (<= point (match-end group))
17400 (if context
17401 (list context (match-beginning group) (match-end group))
17402 t)))
17404 (defun org-switch-to-buffer-other-window (&rest args)
17405 "Switch to buffer in a second window on the current frame.
17406 In particular, do not allow pop-up frames."
17407 (let (pop-up-frames special-display-buffer-names special-display-regexps
17408 special-display-function)
17409 (apply 'switch-to-buffer-other-window args)))
17411 (defun org-combine-plists (&rest plists)
17412 "Create a single property list from all plists in PLISTS.
17413 The process starts by copying the first list, and then setting properties
17414 from the other lists. Settings in the last list are the most significant
17415 ones and overrule settings in the other lists."
17416 (let ((rtn (copy-sequence (pop plists)))
17417 p v ls)
17418 (while plists
17419 (setq ls (pop plists))
17420 (while ls
17421 (setq p (pop ls) v (pop ls))
17422 (setq rtn (plist-put rtn p v))))
17423 rtn))
17425 (defun org-move-line-down (arg)
17426 "Move the current line down. With prefix argument, move it past ARG lines."
17427 (interactive "p")
17428 (let ((col (current-column))
17429 beg end pos)
17430 (beginning-of-line 1) (setq beg (point))
17431 (beginning-of-line 2) (setq end (point))
17432 (beginning-of-line (+ 1 arg))
17433 (setq pos (move-marker (make-marker) (point)))
17434 (insert (delete-and-extract-region beg end))
17435 (goto-char pos)
17436 (org-move-to-column col)))
17438 (defun org-move-line-up (arg)
17439 "Move the current line up. With prefix argument, move it past ARG lines."
17440 (interactive "p")
17441 (let ((col (current-column))
17442 beg end pos)
17443 (beginning-of-line 1) (setq beg (point))
17444 (beginning-of-line 2) (setq end (point))
17445 (beginning-of-line (- arg))
17446 (setq pos (move-marker (make-marker) (point)))
17447 (insert (delete-and-extract-region beg end))
17448 (goto-char pos)
17449 (org-move-to-column col)))
17451 (defun org-replace-escapes (string table)
17452 "Replace %-escapes in STRING with values in TABLE.
17453 TABLE is an association list with keys like \"%a\" and string values.
17454 The sequences in STRING may contain normal field width and padding information,
17455 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
17456 so values can contain further %-escapes if they are define later in TABLE."
17457 (let ((case-fold-search nil)
17458 e re rpl)
17459 (while (setq e (pop table))
17460 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
17461 (while (string-match re string)
17462 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
17463 (cdr e)))
17464 (setq string (replace-match rpl t t string))))
17465 string))
17468 (defun org-sublist (list start end)
17469 "Return a section of LIST, from START to END.
17470 Counting starts at 1."
17471 (let (rtn (c start))
17472 (setq list (nthcdr (1- start) list))
17473 (while (and list (<= c end))
17474 (push (pop list) rtn)
17475 (setq c (1+ c)))
17476 (nreverse rtn)))
17478 (defun org-find-base-buffer-visiting (file)
17479 "Like `find-buffer-visiting' but always return the base buffer and
17480 not an indirect buffer."
17481 (let ((buf (or (get-file-buffer file)
17482 (find-buffer-visiting file))))
17483 (if buf
17484 (or (buffer-base-buffer buf) buf)
17485 nil)))
17487 (defun org-image-file-name-regexp (&optional extensions)
17488 "Return regexp matching the file names of images.
17489 If EXTENSIONS is given, only match these."
17490 (if (and (not extensions) (fboundp 'image-file-name-regexp))
17491 (image-file-name-regexp)
17492 (let ((image-file-name-extensions
17493 (or extensions
17494 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
17495 "xbm" "xpm" "pbm" "pgm" "ppm"))))
17496 (concat "\\."
17497 (regexp-opt (nconc (mapcar 'upcase
17498 image-file-name-extensions)
17499 image-file-name-extensions)
17501 "\\'"))))
17503 (defun org-file-image-p (file &optional extensions)
17504 "Return non-nil if FILE is an image."
17505 (save-match-data
17506 (string-match (org-image-file-name-regexp extensions) file)))
17508 (defun org-get-cursor-date ()
17509 "Return the date at cursor in as a time.
17510 This works in the calendar and in the agenda, anywhere else it just
17511 returns the current time."
17512 (let (date day defd)
17513 (cond
17514 ((eq major-mode 'calendar-mode)
17515 (setq date (calendar-cursor-to-date)
17516 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
17517 ((eq major-mode 'org-agenda-mode)
17518 (setq day (get-text-property (point) 'day))
17519 (if day
17520 (setq date (calendar-gregorian-from-absolute day)
17521 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
17522 (nth 2 date))))))
17523 (or defd (current-time))))
17525 (defvar org-agenda-action-marker (make-marker)
17526 "Marker pointing to the entry for the next agenda action.")
17528 (defun org-mark-entry-for-agenda-action ()
17529 "Mark the current entry as target of an agenda action.
17530 Agenda actions are actions executed from the agenda with the key `k',
17531 which make use of the date at the cursor."
17532 (interactive)
17533 (move-marker org-agenda-action-marker
17534 (save-excursion (org-back-to-heading t) (point))
17535 (current-buffer))
17536 (message
17537 "Entry marked for action; press `k' at desired date in agenda or calendar"))
17539 ;;; Paragraph filling stuff.
17540 ;; We want this to be just right, so use the full arsenal.
17542 (defun org-indent-line-function ()
17543 "Indent line like previous, but further if previous was headline or item."
17544 (interactive)
17545 (let* ((pos (point))
17546 (itemp (org-at-item-p))
17547 (case-fold-search t)
17548 (org-drawer-regexp (or org-drawer-regexp "\000"))
17549 column bpos bcol tpos tcol bullet btype bullet-type)
17550 ;; Find the previous relevant line
17551 (beginning-of-line 1)
17552 (cond
17553 ((looking-at "#") (setq column 0))
17554 ((looking-at "\\*+ ") (setq column 0))
17555 ((and (looking-at "[ \t]*:END:")
17556 (save-excursion (re-search-backward org-drawer-regexp nil t)))
17557 (save-excursion
17558 (goto-char (1- (match-beginning 1)))
17559 (setq column (current-column))))
17560 ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
17561 (save-excursion
17562 (re-search-backward
17563 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
17564 (setq column (org-get-indentation (match-string 0))))
17566 (beginning-of-line 0)
17567 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
17568 (not (looking-at "[ \t]*:END:"))
17569 (not (looking-at org-drawer-regexp)))
17570 (beginning-of-line 0))
17571 (cond
17572 ((looking-at "\\*+[ \t]+")
17573 (if (not org-adapt-indentation)
17574 (setq column 0)
17575 (goto-char (match-end 0))
17576 (setq column (current-column))))
17577 ((looking-at org-drawer-regexp)
17578 (goto-char (1- (match-beginning 1)))
17579 (setq column (current-column)))
17580 ((looking-at "\\([ \t]*\\):END:")
17581 (goto-char (match-end 1))
17582 (setq column (current-column)))
17583 ((org-in-item-p)
17584 (org-beginning-of-item)
17585 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
17586 (setq bpos (match-beginning 1) tpos (match-end 0)
17587 bcol (progn (goto-char bpos) (current-column))
17588 tcol (progn (goto-char tpos) (current-column))
17589 bullet (match-string 1)
17590 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
17591 (if (> tcol (+ bcol org-description-max-indent))
17592 (setq tcol (+ bcol 5)))
17593 (if (not itemp)
17594 (setq column tcol)
17595 (goto-char pos)
17596 (beginning-of-line 1)
17597 (if (looking-at "\\S-")
17598 (progn
17599 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
17600 (setq bullet (match-string 1)
17601 btype (if (string-match "[0-9]" bullet) "n" bullet))
17602 (setq column (if (equal btype bullet-type) bcol tcol)))
17603 (setq column (org-get-indentation)))))
17604 (t (setq column (org-get-indentation))))))
17605 (goto-char pos)
17606 (if (<= (current-column) (current-indentation))
17607 (org-indent-line-to column)
17608 (save-excursion (org-indent-line-to column)))
17609 (setq column (current-column))
17610 (beginning-of-line 1)
17611 (if (looking-at
17612 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
17613 (replace-match (concat (match-string 1)
17614 (format org-property-format
17615 (match-string 2) (match-string 3)))
17616 t t))
17617 (org-move-to-column column)))
17619 (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
17620 "Variable to store copy of `adaptive-fill-regexp'.
17621 Since `adaptive-fill-regexp' is set to never match, we need to
17622 store a backup of its value before entering `org-mode' so that
17623 the functionality can be provided as a fall-back.")
17625 (defun org-set-autofill-regexps ()
17626 (interactive)
17627 ;; In the paragraph separator we include headlines, because filling
17628 ;; text in a line directly attached to a headline would otherwise
17629 ;; fill the headline as well.
17630 (org-set-local 'comment-start-skip "^#+[ \t]*")
17631 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
17632 ;; The paragraph starter includes hand-formatted lists.
17633 (org-set-local
17634 'paragraph-start
17635 (concat
17636 "\f" "\\|"
17637 "[ ]*$" "\\|"
17638 "\\*+ " "\\|"
17639 "[ \t]*#" "\\|"
17640 "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
17641 "[ \t]*[:|]" "\\|"
17642 "\\$\\$" "\\|"
17643 "\\\\\\(begin\\|end\\|[][]\\)"))
17644 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
17645 ;; But only if the user has not turned off tables or fixed-width regions
17646 (org-set-local
17647 'auto-fill-inhibit-regexp
17648 (concat "\\*+ \\|#\\+"
17649 "\\|[ \t]*" org-keyword-time-regexp
17650 (if (or org-enable-table-editor org-enable-fixed-width-editor)
17651 (concat
17652 "\\|[ \t]*["
17653 (if org-enable-table-editor "|" "")
17654 (if org-enable-fixed-width-editor ":" "")
17655 "]"))))
17656 ;; We use our own fill-paragraph function, to make sure that tables
17657 ;; and fixed-width regions are not wrapped. That function will pass
17658 ;; through to `fill-paragraph' when appropriate.
17659 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
17660 ;; Adaptive filling: To get full control, first make sure that
17661 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
17662 (unless (local-variable-p 'adaptive-fill-regexp)
17663 (org-set-local 'org-adaptive-fill-regexp-backup
17664 adaptive-fill-regexp))
17665 (org-set-local 'adaptive-fill-regexp "\000")
17666 (org-set-local 'adaptive-fill-function
17667 'org-adaptive-fill-function)
17668 (org-set-local
17669 'align-mode-rules-list
17670 '((org-in-buffer-settings
17671 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
17672 (modes . '(org-mode))))))
17674 (defun org-fill-paragraph (&optional justify)
17675 "Re-align a table, pass through to fill-paragraph if no table."
17676 (let ((table-p (org-at-table-p))
17677 (table.el-p (org-at-table.el-p)))
17678 (cond ((and (equal (char-after (point-at-bol)) ?*)
17679 (save-excursion (goto-char (point-at-bol))
17680 (looking-at outline-regexp)))
17681 t) ; skip headlines
17682 (table.el-p t) ; skip table.el tables
17683 (table-p (org-table-align) t) ; align org-mode tables
17684 (t nil)))) ; call paragraph-fill
17686 ;; For reference, this is the default value of adaptive-fill-regexp
17687 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
17689 (defun org-adaptive-fill-function ()
17690 "Return a fill prefix for org-mode files.
17691 In particular, this makes sure hanging paragraphs for hand-formatted lists
17692 work correctly."
17693 (cond
17694 ;; Comment line
17695 ((looking-at "#[ \t]+")
17696 (match-string-no-properties 0))
17697 ;; Description list
17698 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
17699 (save-excursion
17700 (if (> (match-end 1) (+ (match-beginning 1)
17701 org-description-max-indent))
17702 (goto-char (+ (match-beginning 1) 5))
17703 (goto-char (match-end 0)))
17704 (make-string (current-column) ?\ )))
17705 ;; Ordered or unordered list
17706 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
17707 (save-excursion
17708 (goto-char (match-end 0))
17709 (make-string (current-column) ?\ )))
17710 ;; Other text
17711 ((looking-at org-adaptive-fill-regexp-backup)
17712 (match-string-no-properties 0))))
17714 ;;; Other stuff.
17716 (defun org-toggle-fixed-width-section (arg)
17717 "Toggle the fixed-width export.
17718 If there is no active region, the QUOTE keyword at the current headline is
17719 inserted or removed. When present, it causes the text between this headline
17720 and the next to be exported as fixed-width text, and unmodified.
17721 If there is an active region, this command adds or removes a colon as the
17722 first character of this line. If the first character of a line is a colon,
17723 this line is also exported in fixed-width font."
17724 (interactive "P")
17725 (let* ((cc 0)
17726 (regionp (org-region-active-p))
17727 (beg (if regionp (region-beginning) (point)))
17728 (end (if regionp (region-end)))
17729 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
17730 (case-fold-search nil)
17731 (re "[ \t]*\\(: \\)")
17732 off)
17733 (if regionp
17734 (save-excursion
17735 (goto-char beg)
17736 (setq cc (current-column))
17737 (beginning-of-line 1)
17738 (setq off (looking-at re))
17739 (while (> nlines 0)
17740 (setq nlines (1- nlines))
17741 (beginning-of-line 1)
17742 (cond
17743 (arg
17744 (org-move-to-column cc t)
17745 (insert ": \n")
17746 (forward-line -1))
17747 ((and off (looking-at re))
17748 (replace-match "" t t nil 1))
17749 ((not off) (org-move-to-column cc t) (insert ": ")))
17750 (forward-line 1)))
17751 (save-excursion
17752 (org-back-to-heading)
17753 (if (looking-at (concat outline-regexp
17754 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
17755 (replace-match "" t t nil 1)
17756 (if (looking-at outline-regexp)
17757 (progn
17758 (goto-char (match-end 0))
17759 (insert org-quote-string " "))))))))
17761 (defun org-reftex-citation ()
17762 "Use reftex-citation to insert a citation into the buffer.
17763 This looks for a line like
17765 #+BIBLIOGRAPHY: foo plain option:-d
17767 and derives from it that foo.bib is the bibliography file relevant
17768 for this document. It then installs the necessary environment for RefTeX
17769 to work in this buffer and calls `reftex-citation' to insert a citation
17770 into the buffer.
17772 Export of such citations to both LaTeX and HTML is handled by the contributed
17773 package org-exp-bibtex by Taru Karttunen."
17774 (interactive)
17775 (let ((reftex-docstruct-symbol 'rds)
17776 (reftex-cite-format "\\cite{%l}")
17777 rds bib)
17778 (save-excursion
17779 (save-restriction
17780 (widen)
17781 (let ((case-fold-search t)
17782 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
17783 (if (not (save-excursion
17784 (or (re-search-forward re nil t)
17785 (re-search-backward re nil t))))
17786 (error "No bibliography defined in file")
17787 (setq bib (concat (match-string 1) ".bib")
17788 rds (list (list 'bib bib)))))))
17789 (call-interactively 'reftex-citation)))
17791 ;;;; Functions extending outline functionality
17793 (defun org-beginning-of-line (&optional arg)
17794 "Go to the beginning of the current line. If that is invisible, continue
17795 to a visible line beginning. This makes the function of C-a more intuitive.
17796 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
17797 first attempt, and only move to after the tags when the cursor is already
17798 beyond the end of the headline."
17799 (interactive "P")
17800 (let ((pos (point))
17801 (special (if (consp org-special-ctrl-a/e)
17802 (car org-special-ctrl-a/e)
17803 org-special-ctrl-a/e))
17804 refpos)
17805 (if (org-bound-and-true-p line-move-visual)
17806 (beginning-of-visual-line 1)
17807 (beginning-of-line 1))
17808 (if (and arg (fboundp 'move-beginning-of-line))
17809 (call-interactively 'move-beginning-of-line)
17810 (if (bobp)
17812 (backward-char 1)
17813 (if (org-invisible-p)
17814 (while (and (not (bobp)) (org-invisible-p))
17815 (backward-char 1)
17816 (beginning-of-line 1))
17817 (forward-char 1))))
17818 (when special
17819 (cond
17820 ((and (looking-at org-complex-heading-regexp)
17821 (= (char-after (match-end 1)) ?\ ))
17822 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
17823 (point-at-eol)))
17824 (goto-char
17825 (if (eq special t)
17826 (cond ((> pos refpos) refpos)
17827 ((= pos (point)) refpos)
17828 (t (point)))
17829 (cond ((> pos (point)) (point))
17830 ((not (eq last-command this-command)) (point))
17831 (t refpos)))))
17832 ((org-at-item-p)
17833 (goto-char
17834 (if (eq special t)
17835 (cond ((> pos (match-end 4)) (match-end 4))
17836 ((= pos (point)) (match-end 4))
17837 (t (point)))
17838 (cond ((> pos (point)) (point))
17839 ((not (eq last-command this-command)) (point))
17840 (t (match-end 4))))))))
17841 (org-no-warnings
17842 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
17844 (defun org-end-of-line (&optional arg)
17845 "Go to the end of the line.
17846 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
17847 first attempt, and only move to after the tags when the cursor is already
17848 beyond the end of the headline."
17849 (interactive "P")
17850 (let ((special (if (consp org-special-ctrl-a/e)
17851 (cdr org-special-ctrl-a/e)
17852 org-special-ctrl-a/e)))
17853 (if (or (not special)
17854 (not (org-on-heading-p))
17855 arg)
17856 (call-interactively
17857 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
17858 ((fboundp 'move-end-of-line) 'move-end-of-line)
17859 (t 'end-of-line)))
17860 (let ((pos (point)))
17861 (beginning-of-line 1)
17862 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
17863 (if (eq special t)
17864 (if (or (< pos (match-beginning 1))
17865 (= pos (match-end 0)))
17866 (goto-char (match-beginning 1))
17867 (goto-char (match-end 0)))
17868 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
17869 (goto-char (match-end 0))
17870 (goto-char (match-beginning 1))))
17871 (call-interactively (if (fboundp 'move-end-of-line)
17872 'move-end-of-line
17873 'end-of-line)))))
17874 (org-no-warnings
17875 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
17877 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
17878 (define-key org-mode-map "\C-e" 'org-end-of-line)
17879 (define-key org-mode-map [home] 'org-beginning-of-line)
17880 (define-key org-mode-map [end] 'org-end-of-line)
17882 (defun org-backward-sentence (&optional arg)
17883 "Go to beginning of sentence, or beginning of table field.
17884 This will call `backward-sentence' or `org-table-beginning-of-field',
17885 depending on context."
17886 (interactive "P")
17887 (cond
17888 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
17889 (t (call-interactively 'backward-sentence))))
17891 (defun org-forward-sentence (&optional arg)
17892 "Go to end of sentence, or end of table field.
17893 This will call `forward-sentence' or `org-table-end-of-field',
17894 depending on context."
17895 (interactive "P")
17896 (cond
17897 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
17898 (t (call-interactively 'forward-sentence))))
17900 (define-key org-mode-map "\M-a" 'org-backward-sentence)
17901 (define-key org-mode-map "\M-e" 'org-forward-sentence)
17903 (defun org-kill-line (&optional arg)
17904 "Kill line, to tags or end of line."
17905 (interactive "P")
17906 (cond
17907 ((or (not org-special-ctrl-k)
17908 (bolp)
17909 (not (org-on-heading-p)))
17910 (call-interactively 'kill-line))
17911 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
17912 (kill-region (point) (match-beginning 1))
17913 (org-set-tags nil t))
17914 (t (kill-region (point) (point-at-eol)))))
17916 (define-key org-mode-map "\C-k" 'org-kill-line)
17918 (defun org-yank (&optional arg)
17919 "Yank. If the kill is a subtree, treat it specially.
17920 This command will look at the current kill and check if is a single
17921 subtree, or a series of subtrees[1]. If it passes the test, and if the
17922 cursor is at the beginning of a line or after the stars of a currently
17923 empty headline, then the yank is handled specially. How exactly depends
17924 on the value of the following variables, both set by default.
17926 org-yank-folded-subtrees
17927 When set, the subtree(s) will be folded after insertion, but only
17928 if doing so would now swallow text after the yanked text.
17930 org-yank-adjusted-subtrees
17931 When set, the subtree will be promoted or demoted in order to
17932 fit into the local outline tree structure, which means that the level
17933 will be adjusted so that it becomes the smaller one of the two
17934 *visible* surrounding headings.
17936 Any prefix to this command will cause `yank' to be called directly with
17937 no special treatment. In particular, a simple `C-u' prefix will just
17938 plainly yank the text as it is.
17940 \[1] The test checks if the first non-white line is a heading
17941 and if there are no other headings with fewer stars."
17942 (interactive "P")
17943 (org-yank-generic 'yank arg))
17945 (defun org-yank-generic (command arg)
17946 "Perform some yank-like command.
17948 This function implements the behavior described in the `org-yank'
17949 documentation. However, it has been generalized to work for any
17950 interactive command with similar behavior."
17952 ;; pretend to be command COMMAND
17953 (setq this-command command)
17955 (if arg
17956 (call-interactively command)
17958 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
17959 (and (org-kill-is-subtree-p)
17960 (or (bolp)
17961 (and (looking-at "[ \t]*$")
17962 (string-match
17963 "\\`\\*+\\'"
17964 (buffer-substring (point-at-bol) (point)))))))
17965 swallowp)
17966 (cond
17967 ((and subtreep org-yank-folded-subtrees)
17968 (let ((beg (point))
17969 end)
17970 (if (and subtreep org-yank-adjusted-subtrees)
17971 (org-paste-subtree nil nil 'for-yank)
17972 (call-interactively command))
17974 (setq end (point))
17975 (goto-char beg)
17976 (when (and (bolp) subtreep
17977 (not (setq swallowp
17978 (org-yank-folding-would-swallow-text beg end))))
17979 (or (looking-at outline-regexp)
17980 (re-search-forward (concat "^" outline-regexp) end t))
17981 (while (and (< (point) end) (looking-at outline-regexp))
17982 (hide-subtree)
17983 (org-cycle-show-empty-lines 'folded)
17984 (condition-case nil
17985 (outline-forward-same-level 1)
17986 (error (goto-char end)))))
17987 (when swallowp
17988 (message
17989 "Inserted text not folded because that would swallow text"))
17991 (goto-char end)
17992 (skip-chars-forward " \t\n\r")
17993 (beginning-of-line 1)
17994 (push-mark beg 'nomsg)))
17995 ((and subtreep org-yank-adjusted-subtrees)
17996 (let ((beg (point-at-bol)))
17997 (org-paste-subtree nil nil 'for-yank)
17998 (push-mark beg 'nomsg)))
18000 (call-interactively command))))))
18002 (defun org-yank-folding-would-swallow-text (beg end)
18003 "Would hide-subtree at BEG swallow any text after END?"
18004 (let (level)
18005 (save-excursion
18006 (goto-char beg)
18007 (when (or (looking-at outline-regexp)
18008 (re-search-forward (concat "^" outline-regexp) end t))
18009 (setq level (org-outline-level)))
18010 (goto-char end)
18011 (skip-chars-forward " \t\r\n\v\f")
18012 (if (or (eobp)
18013 (and (bolp) (looking-at org-outline-regexp)
18014 (<= (org-outline-level) level)))
18015 nil ; Nothing would be swallowed
18016 t)))) ; something would swallow
18018 (define-key org-mode-map "\C-y" 'org-yank)
18020 (defun org-invisible-p ()
18021 "Check if point is at a character currently not visible."
18022 ;; Early versions of noutline don't have `outline-invisible-p'.
18023 (if (fboundp 'outline-invisible-p)
18024 (outline-invisible-p)
18025 (get-char-property (point) 'invisible)))
18027 (defun org-invisible-p2 ()
18028 "Check if point is at a character currently not visible."
18029 (save-excursion
18030 (if (and (eolp) (not (bobp))) (backward-char 1))
18031 ;; Early versions of noutline don't have `outline-invisible-p'.
18032 (if (fboundp 'outline-invisible-p)
18033 (outline-invisible-p)
18034 (get-char-property (point) 'invisible))))
18036 (defun org-back-to-heading (&optional invisible-ok)
18037 "Call `outline-back-to-heading', but provide a better error message."
18038 (condition-case nil
18039 (outline-back-to-heading invisible-ok)
18040 (error (error "Before first headline at position %d in buffer %s"
18041 (point) (current-buffer)))))
18043 (defun org-before-first-heading-p ()
18044 "Before first heading?"
18045 (save-excursion
18046 (null (re-search-backward "^\\*+ " nil t))))
18048 (defun org-on-heading-p (&optional ignored)
18049 (outline-on-heading-p t))
18050 (defun org-at-heading-p (&optional ignored)
18051 (outline-on-heading-p t))
18053 (defun org-point-at-end-of-empty-headline ()
18054 "If point is at the end of an empty headline, return t, else nil.
18055 If the heading only contains a TODO keyword, it is still still considered
18056 empty."
18057 (and (looking-at "[ \t]*$")
18058 (save-excursion
18059 (beginning-of-line 1)
18060 (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
18061 "\\)?[ \t]*$")))))
18062 (defun org-at-heading-or-item-p ()
18063 (or (org-on-heading-p) (org-at-item-p)))
18065 (defun org-on-target-p ()
18066 (or (org-in-regexp org-radio-target-regexp)
18067 (org-in-regexp org-target-regexp)))
18069 (defun org-up-heading-all (arg)
18070 "Move to the heading line of which the present line is a subheading.
18071 This function considers both visible and invisible heading lines.
18072 With argument, move up ARG levels."
18073 (if (fboundp 'outline-up-heading-all)
18074 (outline-up-heading-all arg) ; emacs 21 version of outline.el
18075 (outline-up-heading arg t))) ; emacs 22 version of outline.el
18077 (defun org-up-heading-safe ()
18078 "Move to the heading line of which the present line is a subheading.
18079 This version will not throw an error. It will return the level of the
18080 headline found, or nil if no higher level is found.
18082 Also, this function will be a lot faster than `outline-up-heading',
18083 because it relies on stars being the outline starters. This can really
18084 make a significant difference in outlines with very many siblings."
18085 (let (start-level re)
18086 (org-back-to-heading t)
18087 (setq start-level (funcall outline-level))
18088 (if (equal start-level 1)
18090 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
18091 (if (re-search-backward re nil t)
18092 (funcall outline-level)))))
18094 (defun org-first-sibling-p ()
18095 "Is this heading the first child of its parents?"
18096 (interactive)
18097 (let ((re (concat "^" outline-regexp))
18098 level l)
18099 (unless (org-at-heading-p t)
18100 (error "Not at a heading"))
18101 (setq level (funcall outline-level))
18102 (save-excursion
18103 (if (not (re-search-backward re nil t))
18105 (setq l (funcall outline-level))
18106 (< l level)))))
18108 (defun org-goto-sibling (&optional previous)
18109 "Goto the next sibling, even if it is invisible.
18110 When PREVIOUS is set, go to the previous sibling instead. Returns t
18111 when a sibling was found. When none is found, return nil and don't
18112 move point."
18113 (let ((fun (if previous 're-search-backward 're-search-forward))
18114 (pos (point))
18115 (re (concat "^" outline-regexp))
18116 level l)
18117 (when (condition-case nil (org-back-to-heading t) (error nil))
18118 (setq level (funcall outline-level))
18119 (catch 'exit
18120 (or previous (forward-char 1))
18121 (while (funcall fun re nil t)
18122 (setq l (funcall outline-level))
18123 (when (< l level) (goto-char pos) (throw 'exit nil))
18124 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
18125 (goto-char pos)
18126 nil))))
18128 (defun org-show-siblings ()
18129 "Show all siblings of the current headline."
18130 (save-excursion
18131 (while (org-goto-sibling) (org-flag-heading nil)))
18132 (save-excursion
18133 (while (org-goto-sibling 'previous)
18134 (org-flag-heading nil))))
18136 (defun org-show-hidden-entry ()
18137 "Show an entry where even the heading is hidden."
18138 (save-excursion
18139 (org-show-entry)))
18141 (defun org-flag-heading (flag &optional entry)
18142 "Flag the current heading. FLAG non-nil means make invisible.
18143 When ENTRY is non-nil, show the entire entry."
18144 (save-excursion
18145 (org-back-to-heading t)
18146 ;; Check if we should show the entire entry
18147 (if entry
18148 (progn
18149 (org-show-entry)
18150 (save-excursion
18151 (and (outline-next-heading)
18152 (org-flag-heading nil))))
18153 (outline-flag-region (max (point-min) (1- (point)))
18154 (save-excursion (outline-end-of-heading) (point))
18155 flag))))
18157 (defun org-get-next-sibling ()
18158 "Move to next heading of the same level, and return point.
18159 If there is no such heading, return nil.
18160 This is like outline-next-sibling, but invisible headings are ok."
18161 (let ((level (funcall outline-level)))
18162 (outline-next-heading)
18163 (while (and (not (eobp)) (> (funcall outline-level) level))
18164 (outline-next-heading))
18165 (if (or (eobp) (< (funcall outline-level) level))
18167 (point))))
18169 (defun org-get-last-sibling ()
18170 "Move to previous heading of the same level, and return point.
18171 If there is no such heading, return nil."
18172 (let ((opoint (point))
18173 (level (funcall outline-level)))
18174 (outline-previous-heading)
18175 (when (and (/= (point) opoint) (outline-on-heading-p t))
18176 (while (and (> (funcall outline-level) level)
18177 (not (bobp)))
18178 (outline-previous-heading))
18179 (if (< (funcall outline-level) level)
18181 (point)))))
18183 (defun org-end-of-subtree (&optional invisible-OK to-heading)
18184 ;; This contains an exact copy of the original function, but it uses
18185 ;; `org-back-to-heading', to make it work also in invisible
18186 ;; trees. And is uses an invisible-OK argument.
18187 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
18188 ;; Furthermore, when used inside Org, finding the end of a large subtree
18189 ;; with many children and grandchildren etc, this can be much faster
18190 ;; than the outline version.
18191 (org-back-to-heading invisible-OK)
18192 (let ((first t)
18193 (level (funcall outline-level)))
18194 (if (and (org-mode-p) (< level 1000))
18195 ;; A true heading (not a plain list item), in Org-mode
18196 ;; This means we can easily find the end by looking
18197 ;; only for the right number of stars. Using a regexp to do
18198 ;; this is so much faster than using a Lisp loop.
18199 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
18200 (forward-char 1)
18201 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
18202 ;; something else, do it the slow way
18203 (while (and (not (eobp))
18204 (or first (> (funcall outline-level) level)))
18205 (setq first nil)
18206 (outline-next-heading)))
18207 (unless to-heading
18208 (if (memq (preceding-char) '(?\n ?\^M))
18209 (progn
18210 ;; Go to end of line before heading
18211 (forward-char -1)
18212 (if (memq (preceding-char) '(?\n ?\^M))
18213 ;; leave blank line before heading
18214 (forward-char -1))))))
18215 (point))
18217 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
18218 "Use Org version in org-mode, for dramatic speed-up."
18219 (if (eq major-mode 'org-mode)
18220 (progn
18221 (org-end-of-subtree nil t)
18222 (unless (eobp) (backward-char 1)))
18223 ad-do-it))
18225 (defun org-forward-same-level (arg &optional invisible-ok)
18226 "Move forward to the arg'th subheading at same level as this one.
18227 Stop at the first and last subheadings of a superior heading."
18228 (interactive "p")
18229 (org-back-to-heading invisible-ok)
18230 (org-on-heading-p)
18231 (let* ((level (- (match-end 0) (match-beginning 0) 1))
18232 (re (format "^\\*\\{1,%d\\} " level))
18234 (forward-char 1)
18235 (while (> arg 0)
18236 (while (and (re-search-forward re nil 'move)
18237 (setq l (- (match-end 0) (match-beginning 0) 1))
18238 (= l level)
18239 (not invisible-ok)
18240 (progn (backward-char 1) (org-invisible-p)))
18241 (if (< l level) (setq arg 1)))
18242 (setq arg (1- arg)))
18243 (beginning-of-line 1)))
18245 (defun org-backward-same-level (arg &optional invisible-ok)
18246 "Move backward to the arg'th subheading at same level as this one.
18247 Stop at the first and last subheadings of a superior heading."
18248 (interactive "p")
18249 (org-back-to-heading)
18250 (org-on-heading-p)
18251 (let* ((level (- (match-end 0) (match-beginning 0) 1))
18252 (re (format "^\\*\\{1,%d\\} " level))
18254 (while (> arg 0)
18255 (while (and (re-search-backward re nil 'move)
18256 (setq l (- (match-end 0) (match-beginning 0) 1))
18257 (= l level)
18258 (not invisible-ok)
18259 (org-invisible-p))
18260 (if (< l level) (setq arg 1)))
18261 (setq arg (1- arg)))))
18263 (defun org-show-subtree ()
18264 "Show everything after this heading at deeper levels."
18265 (outline-flag-region
18266 (point)
18267 (save-excursion
18268 (org-end-of-subtree t t))
18269 nil))
18271 (defun org-show-entry ()
18272 "Show the body directly following this heading.
18273 Show the heading too, if it is currently invisible."
18274 (interactive)
18275 (save-excursion
18276 (condition-case nil
18277 (progn
18278 (org-back-to-heading t)
18279 (outline-flag-region
18280 (max (point-min) (1- (point)))
18281 (save-excursion
18282 (if (re-search-forward
18283 (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
18284 (match-beginning 1)
18285 (point-max)))
18286 nil)
18287 (org-cycle-hide-drawers 'children))
18288 (error nil))))
18290 (defun org-make-options-regexp (kwds &optional extra)
18291 "Make a regular expression for keyword lines."
18292 (concat
18294 "#?[ \t]*\\+\\("
18295 (mapconcat 'regexp-quote kwds "\\|")
18296 (if extra (concat "\\|" extra))
18297 "\\):[ \t]*"
18298 "\\(.*\\)"))
18300 ;; Make isearch reveal the necessary context
18301 (defun org-isearch-end ()
18302 "Reveal context after isearch exits."
18303 (when isearch-success ; only if search was successful
18304 (if (featurep 'xemacs)
18305 ;; Under XEmacs, the hook is run in the correct place,
18306 ;; we directly show the context.
18307 (org-show-context 'isearch)
18308 ;; In Emacs the hook runs *before* restoring the overlays.
18309 ;; So we have to use a one-time post-command-hook to do this.
18310 ;; (Emacs 22 has a special variable, see function `org-mode')
18311 (unless (and (boundp 'isearch-mode-end-hook-quit)
18312 isearch-mode-end-hook-quit)
18313 ;; Only when the isearch was not quitted.
18314 (org-add-hook 'post-command-hook 'org-isearch-post-command
18315 'append 'local)))))
18317 (defun org-isearch-post-command ()
18318 "Remove self from hook, and show context."
18319 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
18320 (org-show-context 'isearch))
18323 ;;;; Integration with and fixes for other packages
18325 ;;; Imenu support
18327 (defvar org-imenu-markers nil
18328 "All markers currently used by Imenu.")
18329 (make-variable-buffer-local 'org-imenu-markers)
18331 (defun org-imenu-new-marker (&optional pos)
18332 "Return a new marker for use by Imenu, and remember the marker."
18333 (let ((m (make-marker)))
18334 (move-marker m (or pos (point)))
18335 (push m org-imenu-markers)
18338 (defun org-imenu-get-tree ()
18339 "Produce the index for Imenu."
18340 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
18341 (setq org-imenu-markers nil)
18342 (let* ((n org-imenu-depth)
18343 (re (concat "^" outline-regexp))
18344 (subs (make-vector (1+ n) nil))
18345 (last-level 0)
18346 m level head)
18347 (save-excursion
18348 (save-restriction
18349 (widen)
18350 (goto-char (point-max))
18351 (while (re-search-backward re nil t)
18352 (setq level (org-reduced-level (funcall outline-level)))
18353 (when (<= level n)
18354 (looking-at org-complex-heading-regexp)
18355 (setq head (org-link-display-format
18356 (org-match-string-no-properties 4))
18357 m (org-imenu-new-marker))
18358 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
18359 (if (>= level last-level)
18360 (push (cons head m) (aref subs level))
18361 (push (cons head (aref subs (1+ level))) (aref subs level))
18362 (loop for i from (1+ level) to n do (aset subs i nil)))
18363 (setq last-level level)))))
18364 (aref subs 1)))
18366 (eval-after-load "imenu"
18367 '(progn
18368 (add-hook 'imenu-after-jump-hook
18369 (lambda ()
18370 (if (eq major-mode 'org-mode)
18371 (org-show-context 'org-goto))))))
18373 (defun org-link-display-format (link)
18374 "Replace a link with either the description, or the link target
18375 if no description is present"
18376 (save-match-data
18377 (if (string-match org-bracket-link-analytic-regexp link)
18378 (replace-match (if (match-end 5)
18379 (match-string 5 link)
18380 (concat (match-string 1 link)
18381 (match-string 3 link)))
18382 nil t link)
18383 link)))
18385 ;; Speedbar support
18387 (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
18388 "Overlay marking the agenda restriction line in speedbar.")
18389 (org-overlay-put org-speedbar-restriction-lock-overlay
18390 'face 'org-agenda-restriction-lock)
18391 (org-overlay-put org-speedbar-restriction-lock-overlay
18392 'help-echo "Agendas are currently limited to this item.")
18393 (org-detach-overlay org-speedbar-restriction-lock-overlay)
18395 (defun org-speedbar-set-agenda-restriction ()
18396 "Restrict future agenda commands to the location at point in speedbar.
18397 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
18398 (interactive)
18399 (require 'org-agenda)
18400 (let (p m tp np dir txt)
18401 (cond
18402 ((setq p (text-property-any (point-at-bol) (point-at-eol)
18403 'org-imenu t))
18404 (setq m (get-text-property p 'org-imenu-marker))
18405 (with-current-buffer (marker-buffer m)
18406 (goto-char m)
18407 (org-agenda-set-restriction-lock 'subtree)))
18408 ((setq p (text-property-any (point-at-bol) (point-at-eol)
18409 'speedbar-function 'speedbar-find-file))
18410 (setq tp (previous-single-property-change
18411 (1+ p) 'speedbar-function)
18412 np (next-single-property-change
18413 tp 'speedbar-function)
18414 dir (speedbar-line-directory)
18415 txt (buffer-substring-no-properties (or tp (point-min))
18416 (or np (point-max))))
18417 (with-current-buffer (find-file-noselect
18418 (let ((default-directory dir))
18419 (expand-file-name txt)))
18420 (unless (org-mode-p)
18421 (error "Cannot restrict to non-Org-mode file"))
18422 (org-agenda-set-restriction-lock 'file)))
18423 (t (error "Don't know how to restrict Org-mode's agenda")))
18424 (org-move-overlay org-speedbar-restriction-lock-overlay
18425 (point-at-bol) (point-at-eol))
18426 (setq current-prefix-arg nil)
18427 (org-agenda-maybe-redo)))
18429 (eval-after-load "speedbar"
18430 '(progn
18431 (speedbar-add-supported-extension ".org")
18432 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
18433 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
18434 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
18435 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
18436 (add-hook 'speedbar-visiting-tag-hook
18437 (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
18440 ;;; Fixes and Hacks for problems with other packages
18442 ;; Make flyspell not check words in links, to not mess up our keymap
18443 (defun org-mode-flyspell-verify ()
18444 "Don't let flyspell put overlays at active buttons."
18445 (and (not (get-text-property (point) 'keymap))
18446 (not (get-text-property (point) 'org-no-flyspell))))
18448 (defun org-remove-flyspell-overlays-in (beg end)
18449 "Remove flyspell overlays in region."
18450 (and (org-bound-and-true-p flyspell-mode)
18451 (fboundp 'flyspell-delete-region-overlays)
18452 (flyspell-delete-region-overlays beg end))
18453 (add-text-properties beg end '(org-no-flyspell t)))
18455 ;; Make `bookmark-jump' shows the jump location if it was hidden.
18456 (eval-after-load "bookmark"
18457 '(if (boundp 'bookmark-after-jump-hook)
18458 ;; We can use the hook
18459 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
18460 ;; Hook not available, use advice
18461 (defadvice bookmark-jump (after org-make-visible activate)
18462 "Make the position visible."
18463 (org-bookmark-jump-unhide))))
18465 ;; Make sure saveplace shows the location if it was hidden
18466 (eval-after-load "saveplace"
18467 '(defadvice save-place-find-file-hook (after org-make-visible activate)
18468 "Make the position visible."
18469 (org-bookmark-jump-unhide)))
18471 ;; Make sure ecb shows the location if it was hidden
18472 (eval-after-load "ecb"
18473 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
18474 "Make hierarchy visible when jumping into location from ECB tree buffer."
18475 (if (eq major-mode 'org-mode)
18476 (org-show-context))))
18478 (defun org-bookmark-jump-unhide ()
18479 "Unhide the current position, to show the bookmark location."
18480 (and (org-mode-p)
18481 (or (org-invisible-p)
18482 (save-excursion (goto-char (max (point-min) (1- (point))))
18483 (org-invisible-p)))
18484 (org-show-context 'bookmark-jump)))
18486 ;; Make session.el ignore our circular variable
18487 (eval-after-load "session"
18488 '(add-to-list 'session-globals-exclude 'org-mark-ring))
18490 ;;;; Experimental code
18492 (defun org-closed-in-range ()
18493 "Sparse tree of items closed in a certain time range.
18494 Still experimental, may disappear in the future."
18495 (interactive)
18496 ;; Get the time interval from the user.
18497 (let* ((time1 (org-float-time
18498 (org-read-date nil 'to-time nil "Starting date: ")))
18499 (time2 (org-float-time
18500 (org-read-date nil 'to-time nil "End date:")))
18501 ;; callback function
18502 (callback (lambda ()
18503 (let ((time
18504 (org-float-time
18505 (apply 'encode-time
18506 (org-parse-time-string
18507 (match-string 1))))))
18508 ;; check if time in interval
18509 (and (>= time time1) (<= time time2))))))
18510 ;; make tree, check each match with the callback
18511 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
18513 ;;;; Finish up
18515 (provide 'org)
18517 (run-hooks 'org-load-hook)
18519 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
18521 ;;; org.el ends here