New hooks to attach commands to S-cursor hooks
[org-mode/org-tableheadings.git] / lisp / org.el
blobc0369f35fa789b4d39e9f7d94e62c3872a981a7a
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, 2010
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.35trans
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))
77 (require 'calendar)
78 ;; Emacs 22 calendar compatibility: Make sure the new variables are available
79 (unless (boundp 'calendar-view-holidays-initially-flag)
80 (defvaralias 'calendar-view-holidays-initially-flag
81 'view-calendar-holidays-initially))
82 (unless (boundp 'calendar-view-diary-initially-flag)
83 (defvaralias 'calendar-view-diary-initially-flag
84 'view-diary-entries-initially))
85 (unless (boundp 'diary-fancy-buffer)
86 (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
88 ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
89 ;; the file noutline.el being loaded.
90 (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
91 ;; We require noutline, which might be provided in outline.el
92 (require 'outline) (require 'noutline)
93 ;; Other stuff we need.
94 (require 'time-date)
95 (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
96 (require 'easymenu)
97 (require 'overlay)
99 (require 'org-macs)
100 (require 'org-entities)
101 (require 'org-compat)
102 (require 'org-faces)
103 (require 'org-list)
104 (require 'org-src)
105 (require 'org-footnote)
107 ;;;; Customization variables
109 ;;; Version
111 (defconst org-version "6.35trans"
112 "The version number of the file org.el.")
114 (defun org-version (&optional here)
115 "Show the org-mode version in the echo area.
116 With prefix arg HERE, insert it at point."
117 (interactive "P")
118 (let* ((origin default-directory)
119 (version org-version)
120 (git-version)
121 (dir (concat (file-name-directory (locate-library "org")) "../" )))
122 (when (and (file-exists-p (expand-file-name ".git" dir))
123 (executable-find "git"))
124 (unwind-protect
125 (progn
126 (cd dir)
127 (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
128 (with-current-buffer "*Shell Command Output*"
129 (goto-char (point-min))
130 (setq git-version (buffer-substring (point) (point-at-eol))))
131 (subst-char-in-string ?- ?. git-version t)
132 (when (string-match "\\S-"
133 (shell-command-to-string
134 "git diff-index --name-only HEAD --"))
135 (setq git-version (concat git-version ".dirty")))
136 (setq version (concat version " (" git-version ")"))))
137 (cd origin)))
138 (setq version (format "Org-mode version %s" version))
139 (if here (insert version))
140 (message version)))
142 ;;; Compatibility constants
144 ;;; The custom variables
146 (defgroup org nil
147 "Outline-based notes management and organizer."
148 :tag "Org"
149 :group 'outlines
150 :group 'calendar)
152 (defcustom org-mode-hook nil
153 "Mode hook for Org-mode, run after the mode was turned on."
154 :group 'org
155 :type 'hook)
157 (defcustom org-load-hook nil
158 "Hook that is run after org.el has been loaded."
159 :group 'org
160 :type 'hook)
162 (defvar org-modules) ; defined below
163 (defvar org-modules-loaded nil
164 "Have the modules been loaded already?")
166 (defun org-load-modules-maybe (&optional force)
167 "Load all extensions listed in `org-modules'."
168 (when (or force (not org-modules-loaded))
169 (mapc (lambda (ext)
170 (condition-case nil (require ext)
171 (error (message "Problems while trying to load feature `%s'" ext))))
172 org-modules)
173 (setq org-modules-loaded t)))
175 (defun org-set-modules (var value)
176 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
177 (set var value)
178 (when (featurep 'org)
179 (org-load-modules-maybe 'force)))
181 (when (org-bound-and-true-p org-modules)
182 (let ((a (member 'org-infojs org-modules)))
183 (and a (setcar a 'org-jsinfo))))
185 (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)
186 "Modules that should always be loaded together with org.el.
187 If a description starts with <C>, the file is not part of Emacs
188 and loading it will require that you have downloaded and properly installed
189 the org-mode distribution.
191 You can also use this system to load external packages (i.e. neither Org
192 core modules, nor modules from the CONTRIB directory). Just add symbols
193 to the end of the list. If the package is called org-xyz.el, then you need
194 to add the symbol `xyz', and the package must have a call to
196 (provide 'org-xyz)"
197 :group 'org
198 :set 'org-set-modules
199 :type
200 '(set :greedy t
201 (const :tag " bbdb: Links to BBDB entries" org-bbdb)
202 (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
203 (const :tag " crypt: Encryption of subtrees" org-crypt)
204 (const :tag " ctags: Access to Emacs tags with links" org-ctags)
205 (const :tag " docview: Links to doc-view buffers" org-docview)
206 (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
207 (const :tag " id: Global IDs for identifying entries" org-id)
208 (const :tag " info: Links to Info nodes" org-info)
209 (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
210 (const :tag " habit: Track your consistency with habits" org-habit)
211 (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
212 (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
213 (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
214 (const :tag " mew Links to Mew folders/messages" org-mew)
215 (const :tag " mhe: Links to MHE folders/messages" org-mhe)
216 (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
217 (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
218 (const :tag " vm: Links to VM folders/messages" org-vm)
219 (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
220 (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
221 (const :tag " mouse: Additional mouse support" org-mouse)
223 (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
224 (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
225 (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
226 (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
227 (const :tag "C collector: Collect properties into tables" org-collector)
228 (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
229 (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
230 (const :tag "C eval: Include command output as text" org-eval)
231 (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
232 (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
233 (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
234 (const :tag "C git-link: Provide org links to specific file version" org-git-link)
235 (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
237 (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
239 (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
240 (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
241 (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
242 (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
243 (const :tag "C man: Support for links to manpages in Org-mode" org-man)
244 (const :tag "C mtags: Support for muse-like tags" org-mtags)
245 (const :tag "C panel: Simple routines for us with bad memory" org-panel)
246 (const :tag "C registry: A registry for Org-mode links" org-registry)
247 (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
248 (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
249 (const :tag "C secretary: Team management with org-mode" org-secretary)
250 (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
251 (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
252 (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
253 (const :tag "C track: Keep up with Org-mode development" org-track)
254 (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
256 (defcustom org-support-shift-select nil
257 "Non-nil means make shift-cursor commands select text when possible.
259 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
260 selecting a region, or enlarge thusly regions started in this way.
261 In Org-mode, in special contexts, these same keys are used for other
262 purposes, important enough to compete with shift selection. Org tries
263 to balance these needs by supporting `shift-select-mode' outside these
264 special contexts, under control of this variable.
266 The default of this variable is nil, to avoid confusing behavior. Shifted
267 cursor keys will then execute Org commands in the following contexts:
268 - on a headline, changing TODO state (left/right) and priority (up/down)
269 - on a time stamp, changing the time
270 - in a plain list item, changing the bullet type
271 - in a property definition line, switching between allowed values
272 - in the BEGIN line of a clock table (changing the time block).
273 Outside these contexts, the commands will throw an error.
275 When this variable is t and the cursor is not in a special context,
276 Org-mode will support shift-selection for making and enlarging regions.
277 To make this more effective, the bullet cycling will no longer happen
278 anywhere in an item line, but only if the cursor is exactly on the bullet.
280 If you set this variable to the symbol `always', then the keys
281 will not be special in headlines, property lines, and item lines, to make
282 shift selection work there as well. If this is what you want, you can
283 use the following alternative commands: `C-c C-t' and `C-c ,' to
284 change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
285 TODO sets, `C-c -' to cycle item bullet types, and properties can be
286 edited by hand or in column view.
288 However, when the cursor is on a timestamp, shift-cursor commands
289 will still edit the time stamp - this is just too good to give up.
291 XEmacs user should have this variable set to nil, because shift-select-mode
292 is Emacs 23 only."
293 :group 'org
294 :type '(choice
295 (const :tag "Never" nil)
296 (const :tag "When outside special context" t)
297 (const :tag "Everywhere except timestamps" always)))
299 (defgroup org-startup nil
300 "Options concerning startup of Org-mode."
301 :tag "Org Startup"
302 :group 'org)
304 (defcustom org-startup-folded t
305 "Non-nil means entering Org-mode will switch to OVERVIEW.
306 This can also be configured on a per-file basis by adding one of
307 the following lines anywhere in the buffer:
309 #+STARTUP: fold (or `overview', this is equivalent)
310 #+STARTUP: nofold (or `showall', this is equivalent)
311 #+STARTUP: content
312 #+STARTUP: showeverything"
313 :group 'org-startup
314 :type '(choice
315 (const :tag "nofold: show all" nil)
316 (const :tag "fold: overview" t)
317 (const :tag "content: all headlines" content)
318 (const :tag "show everything, even drawers" showeverything)))
320 (defcustom org-startup-truncated t
321 "Non-nil means entering Org-mode will set `truncate-lines'.
322 This is useful since some lines containing links can be very long and
323 uninteresting. Also tables look terrible when wrapped."
324 :group 'org-startup
325 :type 'boolean)
327 (defcustom org-startup-indented nil
328 "Non-nil means turn on `org-indent-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: indent
333 #+STARTUP: noindent"
334 :group 'org-structure
335 :type '(choice
336 (const :tag "Not" nil)
337 (const :tag "Globally (slow on startup in large files)" t)))
339 (defcustom org-startup-with-beamer-mode nil
340 "Non-nil means turn on `org-beamer-mode' on startup.
341 This can also be configured on a per-file basis by adding one of
342 the following lines anywhere in the buffer:
344 #+STARTUP: beamer"
345 :group 'org-startup
346 :type 'boolean)
348 (defcustom org-startup-align-all-tables nil
349 "Non-nil means align all tables when visiting a file.
350 This is useful when the column width in tables is forced with <N> cookies
351 in table fields. Such tables will look correct only after the first re-align.
352 This can also be configured on a per-file basis by adding one of
353 the following lines anywhere in the buffer:
354 #+STARTUP: align
355 #+STARTUP: noalign"
356 :group 'org-startup
357 :type 'boolean)
359 (defcustom org-insert-mode-line-in-empty-file nil
360 "Non-nil means insert the first line setting Org-mode in empty files.
361 When the function `org-mode' is called interactively in an empty file, this
362 normally means that the file name does not automatically trigger Org-mode.
363 To ensure that the file will always be in Org-mode in the future, a
364 line enforcing Org-mode will be inserted into the buffer, if this option
365 has been set."
366 :group 'org-startup
367 :type 'boolean)
369 (defcustom org-replace-disputed-keys nil
370 "Non-nil means use alternative key bindings for some keys.
371 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
372 These keys are also used by other packages like shift-selection-mode'
373 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
374 If you want to use Org-mode together with one of these other modes,
375 or more generally if you would like to move some Org-mode commands to
376 other keys, set this variable and configure the keys with the variable
377 `org-disputed-keys'.
379 This option is only relevant at load-time of Org-mode, and must be set
380 *before* org.el is loaded. Changing it requires a restart of Emacs to
381 become effective."
382 :group 'org-startup
383 :type 'boolean)
385 (defcustom org-use-extra-keys nil
386 "Non-nil means use extra key sequence definitions for certain
387 commands. This happens automatically if you run XEmacs or if
388 window-system is nil. This variable lets you do the same
389 manually. You must set it before loading org.
391 Example: on Carbon Emacs 22 running graphically, with an external
392 keyboard on a Powerbook, the default way of setting M-left might
393 not work for either Alt or ESC. Setting this variable will make
394 it work for ESC."
395 :group 'org-startup
396 :type 'boolean)
398 (if (fboundp 'defvaralias)
399 (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
401 (defcustom org-disputed-keys
402 '(([(shift up)] . [(meta p)])
403 ([(shift down)] . [(meta n)])
404 ([(shift left)] . [(meta -)])
405 ([(shift right)] . [(meta +)])
406 ([(control shift right)] . [(meta shift +)])
407 ([(control shift left)] . [(meta shift -)]))
408 "Keys for which Org-mode and other modes compete.
409 This is an alist, cars are the default keys, second element specifies
410 the alternative to use when `org-replace-disputed-keys' is t.
412 Keys can be specified in any syntax supported by `define-key'.
413 The value of this option takes effect only at Org-mode's startup,
414 therefore you'll have to restart Emacs to apply it after changing."
415 :group 'org-startup
416 :type 'alist)
418 (defun org-key (key)
419 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
420 Or return the original if not disputed.
421 Also apply the trnaslations defined in `org-xemacs-key-equivalents'."
422 (when org-replace-disputed-keys
423 (let* ((nkey (key-description key))
424 (x (org-find-if (lambda (x)
425 (equal (key-description (car x)) nkey))
426 org-disputed-keys)))
427 (setq key (if x (cdr x) key))))
428 (when (featurep 'xemacs)
429 (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
430 key)
432 (defun org-find-if (predicate seq)
433 (catch 'exit
434 (while seq
435 (if (funcall predicate (car seq))
436 (throw 'exit (car seq))
437 (pop seq)))))
439 (defun org-defkey (keymap key def)
440 "Define a key, possibly translated, as returned by `org-key'."
441 (define-key keymap (org-key key) def))
443 (defcustom org-ellipsis nil
444 "The ellipsis to use in the Org-mode outline.
445 When nil, just use the standard three dots. When a string, use that instead,
446 When a face, use the standard 3 dots, but with the specified face.
447 The change affects only Org-mode (which will then use its own display table).
448 Changing this requires executing `M-x org-mode' in a buffer to become
449 effective."
450 :group 'org-startup
451 :type '(choice (const :tag "Default" nil)
452 (face :tag "Face" :value org-warning)
453 (string :tag "String" :value "...#")))
455 (defvar org-display-table nil
456 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
458 (defgroup org-keywords nil
459 "Keywords in Org-mode."
460 :tag "Org Keywords"
461 :group 'org)
463 (defcustom org-deadline-string "DEADLINE:"
464 "String to mark deadline entries.
465 A deadline is this string, followed by a time stamp. Should be a word,
466 terminated by a colon. You can insert a schedule keyword and
467 a timestamp with \\[org-deadline].
468 Changes become only effective after restarting Emacs."
469 :group 'org-keywords
470 :type 'string)
472 (defcustom org-scheduled-string "SCHEDULED:"
473 "String to mark scheduled TODO entries.
474 A schedule is this string, followed by a time stamp. Should be a word,
475 terminated by a colon. You can insert a schedule keyword and
476 a timestamp with \\[org-schedule].
477 Changes become only effective after restarting Emacs."
478 :group 'org-keywords
479 :type 'string)
481 (defcustom org-closed-string "CLOSED:"
482 "String used as the prefix for timestamps logging closing a TODO entry."
483 :group 'org-keywords
484 :type 'string)
486 (defcustom org-clock-string "CLOCK:"
487 "String used as prefix for timestamps clocking work hours on an item."
488 :group 'org-keywords
489 :type 'string)
491 (defcustom org-comment-string "COMMENT"
492 "Entries starting with this keyword will never be exported.
493 An entry can be toggled between COMMENT and normal with
494 \\[org-toggle-comment].
495 Changes become only effective after restarting Emacs."
496 :group 'org-keywords
497 :type 'string)
499 (defcustom org-quote-string "QUOTE"
500 "Entries starting with this keyword will be exported in fixed-width font.
501 Quoting applies only to the text in the entry following the headline, and does
502 not extend beyond the next headline, even if that is lower level.
503 An entry can be toggled between QUOTE and normal with
504 \\[org-toggle-fixed-width-section]."
505 :group 'org-keywords
506 :type 'string)
508 (defconst org-repeat-re
509 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
510 "Regular expression for specifying repeated events.
511 After a match, group 1 contains the repeat expression.")
513 (defgroup org-structure nil
514 "Options concerning the general structure of Org-mode files."
515 :tag "Org Structure"
516 :group 'org)
518 (defgroup org-reveal-location nil
519 "Options about how to make context of a location visible."
520 :tag "Org Reveal Location"
521 :group 'org-structure)
523 (defconst org-context-choice
524 '(choice
525 (const :tag "Always" t)
526 (const :tag "Never" nil)
527 (repeat :greedy t :tag "Individual contexts"
528 (cons
529 (choice :tag "Context"
530 (const agenda)
531 (const org-goto)
532 (const occur-tree)
533 (const tags-tree)
534 (const link-search)
535 (const mark-goto)
536 (const bookmark-jump)
537 (const isearch)
538 (const default))
539 (boolean))))
540 "Contexts for the reveal options.")
542 (defcustom org-show-hierarchy-above '((default . t))
543 "Non-nil means show full hierarchy when revealing a location.
544 Org-mode often shows locations in an org-mode file which might have
545 been invisible before. When this is set, the hierarchy of headings
546 above the exposed location is shown.
547 Turning this off for example for sparse trees makes them very compact.
548 Instead of t, this can also be an alist specifying this option for different
549 contexts. Valid contexts are
550 agenda when exposing an entry from the agenda
551 org-goto when using the command `org-goto' on key C-c C-j
552 occur-tree when using the command `org-occur' on key C-c /
553 tags-tree when constructing a sparse tree based on tags matches
554 link-search when exposing search matches associated with a link
555 mark-goto when exposing the jump goal of a mark
556 bookmark-jump when exposing a bookmark location
557 isearch when exiting from an incremental search
558 default default for all contexts not set explicitly"
559 :group 'org-reveal-location
560 :type org-context-choice)
562 (defcustom org-show-following-heading '((default . nil))
563 "Non-nil means show following heading when revealing a location.
564 Org-mode often shows locations in an org-mode file which might have
565 been invisible before. When this is set, the heading following the
566 match is shown.
567 Turning this off for example for sparse trees makes them very compact,
568 but makes it harder to edit the location of the match. In such a case,
569 use the command \\[org-reveal] to show more context.
570 Instead of t, this can also be an alist specifying this option for different
571 contexts. See `org-show-hierarchy-above' for valid contexts."
572 :group 'org-reveal-location
573 :type org-context-choice)
575 (defcustom org-show-siblings '((default . nil) (isearch t))
576 "Non-nil means show all sibling heading when revealing a location.
577 Org-mode often shows locations in an org-mode file which might have
578 been invisible before. When this is set, the sibling of the current entry
579 heading are all made visible. If `org-show-hierarchy-above' is t,
580 the same happens on each level of the hierarchy above the current entry.
582 By default this is on for the isearch context, off for all other contexts.
583 Turning this off for example for sparse trees makes them very compact,
584 but makes it harder to edit the location of the match. In such a case,
585 use the command \\[org-reveal] to show more context.
586 Instead of t, this can also be an alist specifying this option for different
587 contexts. See `org-show-hierarchy-above' for valid contexts."
588 :group 'org-reveal-location
589 :type org-context-choice)
591 (defcustom org-show-entry-below '((default . nil))
592 "Non-nil means show the entry below a headline when revealing a location.
593 Org-mode often shows locations in an org-mode file which might have
594 been invisible before. When this is set, the text below the headline that is
595 exposed is also shown.
597 By default this is off for all contexts.
598 Instead of t, this can also be an alist specifying this option for different
599 contexts. See `org-show-hierarchy-above' for valid contexts."
600 :group 'org-reveal-location
601 :type org-context-choice)
603 (defcustom org-indirect-buffer-display 'other-window
604 "How should indirect tree buffers be displayed?
605 This applies to indirect buffers created with the commands
606 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
607 Valid values are:
608 current-window Display in the current window
609 other-window Just display in another window.
610 dedicated-frame Create one new frame, and re-use it each time.
611 new-frame Make a new frame each time. Note that in this case
612 previously-made indirect buffers are kept, and you need to
613 kill these buffers yourself."
614 :group 'org-structure
615 :group 'org-agenda-windows
616 :type '(choice
617 (const :tag "In current window" current-window)
618 (const :tag "In current frame, other window" other-window)
619 (const :tag "Each time a new frame" new-frame)
620 (const :tag "One dedicated frame" dedicated-frame)))
622 (defcustom org-use-speed-commands nil
623 "Non-nil means activate single letter commands at beginning of a headline.
624 This may also be a function to test for appropriate locations where speed
625 commands should be active."
626 :group 'org-structure
627 :type '(choice
628 (const :tag "Never" nil)
629 (const :tag "At beginning of headline stars" t)
630 (function)))
632 (defcustom org-speed-commands-user nil
633 "Alist of additional speed commands.
634 This list will be checked before `org-speed-commands-default'
635 when the variable `org-use-speed-commands' is non-nil
636 and when the cursor is at the beginning of a headline.
637 The car if each entry is a string with a single letter, which must
638 be assigned to `self-insert-command' in the global map.
639 The cdr is either a command to be called interactively, a function
640 to be called, or a form to be evaluated.
641 An entry that is just a list with a single string will be interpreted
642 as a descriptive headline that will be added when listing the speed
643 copmmands in the Help buffer using the `?' speed command."
644 :group 'org-structure
645 :type '(repeat :value ("k" . ignore)
646 (choice :value ("k" . ignore)
647 (list :tag "Descriptive Headline" (string :tag "Headline"))
648 (cons :tag "Letter and Command"
649 (string :tag "Command letter")
650 (choice
651 (function)
652 (sexp))))))
654 (defgroup org-cycle nil
655 "Options concerning visibility cycling in Org-mode."
656 :tag "Org Cycle"
657 :group 'org-structure)
659 (defcustom org-cycle-skip-children-state-if-no-children t
660 "Non-nil means skip CHILDREN state in entries that don't have any."
661 :group 'org-cycle
662 :type 'boolean)
664 (defcustom org-cycle-max-level nil
665 "Maximum level which should still be subject to visibility cycling.
666 Levels higher than this will, for cycling, be treated as text, not a headline.
667 When `org-odd-levels-only' is set, a value of N in this variable actually
668 means 2N-1 stars as the limiting headline.
669 When nil, cycle all levels.
670 Note that the limiting level of cycling is also influenced by
671 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
672 `org-inlinetask-min-level' is, cycling will be limited to levels one less
673 than its value."
674 :group 'org-cycle
675 :type '(choice
676 (const :tag "No limit" nil)
677 (integer :tag "Maximum level")))
679 (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
680 "Names of drawers. Drawers are not opened by cycling on the headline above.
681 Drawers only open with a TAB on the drawer line itself. A drawer looks like
682 this:
683 :DRAWERNAME:
684 .....
685 :END:
686 The drawer \"PROPERTIES\" is special for capturing properties through
687 the property API.
689 Drawers can be defined on the per-file basis with a line like:
691 #+DRAWERS: HIDDEN STATE PROPERTIES"
692 :group 'org-structure
693 :group 'org-cycle
694 :type '(repeat (string :tag "Drawer Name")))
696 (defcustom org-hide-block-startup nil
697 "Non-nil means entering Org-mode will fold all blocks.
698 This can also be set in on a per-file basis with
700 #+STARTUP: hideblocks
701 #+STARTUP: showblocks"
702 :group 'org-startup
703 :group 'org-cycle
704 :type 'boolean)
706 (defcustom org-cycle-global-at-bob nil
707 "Cycle globally if cursor is at beginning of buffer and not at a headline.
708 This makes it possible to do global cycling without having to use S-TAB or
709 C-u TAB. For this special case to work, the first line of the buffer
710 must not be a headline - it may be empty or some other text. When used in
711 this way, `org-cycle-hook' is disables temporarily, to make sure the
712 cursor stays at the beginning of the buffer.
713 When this option is nil, don't do anything special at the beginning
714 of the buffer."
715 :group 'org-cycle
716 :type 'boolean)
718 (defcustom org-cycle-level-after-item/entry-creation t
719 "Non-nil means cycle entry level or item indentation in new empty entries.
721 When the cursor is at the end of an empty headline, i.e with only stars
722 and maybe a TODO keyword, TAB will then switch the entry to become a child,
723 and then all possible anchestor states, before returning to the original state.
724 This makes data entry extremely fast: M-RET to create a new headline,
725 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
727 When the cursor is at the end of an empty plain list item, one TAB will
728 make it a subitem, two or more tabs will back up to make this an item
729 higher up in the item hierarchy."
730 :group 'org-cycle
731 :type 'boolean)
733 (defcustom org-cycle-emulate-tab t
734 "Where should `org-cycle' emulate TAB.
735 nil Never
736 white Only in completely white lines
737 whitestart Only at the beginning of lines, before the first non-white char
738 t Everywhere except in headlines
739 exc-hl-bol Everywhere except at the start of a headline
740 If TAB is used in a place where it does not emulate TAB, the current subtree
741 visibility is cycled."
742 :group 'org-cycle
743 :type '(choice (const :tag "Never" nil)
744 (const :tag "Only in completely white lines" white)
745 (const :tag "Before first char in a line" whitestart)
746 (const :tag "Everywhere except in headlines" t)
747 (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
750 (defcustom org-cycle-separator-lines 2
751 "Number of empty lines needed to keep an empty line between collapsed trees.
752 If you leave an empty line between the end of a subtree and the following
753 headline, this empty line is hidden when the subtree is folded.
754 Org-mode will leave (exactly) one empty line visible if the number of
755 empty lines is equal or larger to the number given in this variable.
756 So the default 2 means at least 2 empty lines after the end of a subtree
757 are needed to produce free space between a collapsed subtree and the
758 following headline.
760 If the number is negative, and the number of empty lines is at least -N,
761 all empty lines are shown.
763 Special case: when 0, never leave empty lines in collapsed view."
764 :group 'org-cycle
765 :type 'integer)
766 (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
768 (defcustom org-pre-cycle-hook nil
769 "Hook that is run before visibility cycling is happening.
770 The function(s) in this hook must accept a single argument which indicates
771 the new state that will be set right after running this hook. The
772 argument is a symbol. Before a global state change, it can have the values
773 `overview', `content', or `all'. Before a local state change, it can have
774 the values `folded', `children', or `subtree'."
775 :group 'org-cycle
776 :type 'hook)
778 (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
779 org-cycle-hide-drawers
780 org-cycle-show-empty-lines
781 org-optimize-window-after-visibility-change)
782 "Hook that is run after `org-cycle' has changed the buffer visibility.
783 The function(s) in this hook must accept a single argument which indicates
784 the new state that was set by the most recent `org-cycle' command. The
785 argument is a symbol. After a global state change, it can have the values
786 `overview', `content', or `all'. After a local state change, it can have
787 the values `folded', `children', or `subtree'."
788 :group 'org-cycle
789 :type 'hook)
791 (defgroup org-edit-structure nil
792 "Options concerning structure editing in Org-mode."
793 :tag "Org Edit Structure"
794 :group 'org-structure)
796 (defcustom org-odd-levels-only nil
797 "Non-nil means skip even levels and only use odd levels for the outline.
798 This has the effect that two stars are being added/taken away in
799 promotion/demotion commands. It also influences how levels are
800 handled by the exporters.
801 Changing it requires restart of `font-lock-mode' to become effective
802 for fontification also in regions already fontified.
803 You may also set this on a per-file basis by adding one of the following
804 lines to the buffer:
806 #+STARTUP: odd
807 #+STARTUP: oddeven"
808 :group 'org-edit-structure
809 :group 'org-appearance
810 :type 'boolean)
812 (defcustom org-adapt-indentation t
813 "Non-nil means adapt indentation to outline node level.
815 When this variable is set, Org assumes that you write outlines by
816 indenting text in each node to align with the headline (after the stars).
817 The following issues are influenced by this variable:
819 - When this is set and the *entire* text in an entry is indented, the
820 indentation is increased by one space in a demotion command, and
821 decreased by one in a promotion command. If any line in the entry
822 body starts with text at column 0, indentation is not changed at all.
824 - Property drawers and planning information is inserted indented when
825 this variable s set. When nil, they will not be indented.
827 - TAB indents a line relative to context. The lines below a headline
828 will be indented when this variable is set.
830 Note that this is all about true indentation, by adding and removing
831 space characters. See also `org-indent.el' which does level-dependent
832 indentation in a virtual way, i.e. at display time in Emacs."
833 :group 'org-edit-structure
834 :type 'boolean)
836 (defcustom org-special-ctrl-a/e nil
837 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
839 When t, `C-a' will bring back the cursor to the beginning of the
840 headline text, i.e. after the stars and after a possible TODO keyword.
841 In an item, this will be the position after the bullet.
842 When the cursor is already at that position, another `C-a' will bring
843 it to the beginning of the line.
845 `C-e' will jump to the end of the headline, ignoring the presence of tags
846 in the headline. A second `C-e' will then jump to the true end of the
847 line, after any tags. This also means that, when this variable is
848 non-nil, `C-e' also will never jump beyond the end of the heading of a
849 folded section, i.e. not after the ellipses.
851 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
852 going to the true line boundary first. Only a directly following, identical
853 keypress will bring the cursor to the special positions.
855 This may also be a cons cell where the behavior for `C-a' and `C-e' is
856 set separately."
857 :group 'org-edit-structure
858 :type '(choice
859 (const :tag "off" nil)
860 (const :tag "on: after stars/bullet and before tags first" t)
861 (const :tag "reversed: true line boundary first" reversed)
862 (cons :tag "Set C-a and C-e separately"
863 (choice :tag "Special C-a"
864 (const :tag "off" nil)
865 (const :tag "on: after stars/bullet first" t)
866 (const :tag "reversed: before stars/bullet first" reversed))
867 (choice :tag "Special C-e"
868 (const :tag "off" nil)
869 (const :tag "on: before tags first" t)
870 (const :tag "reversed: after tags first" reversed)))))
871 (if (fboundp 'defvaralias)
872 (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
874 (defcustom org-special-ctrl-k nil
875 "Non-nil means `C-k' will behave specially in headlines.
876 When nil, `C-k' will call the default `kill-line' command.
877 When t, the following will happen while the cursor is in the headline:
879 - When the cursor is at the beginning of a headline, kill the entire
880 line and possible the folded subtree below the line.
881 - When in the middle of the headline text, kill the headline up to the tags.
882 - When after the headline text, kill the tags."
883 :group 'org-edit-structure
884 :type 'boolean)
886 (defcustom org-yank-folded-subtrees t
887 "Non-nil means when yanking subtrees, fold them.
888 If the kill is a single subtree, or a sequence of subtrees, i.e. if
889 it starts with a heading and all other headings in it are either children
890 or siblings, then fold all the subtrees. However, do this only if no
891 text after the yank would be swallowed into a folded tree by this action."
892 :group 'org-edit-structure
893 :type 'boolean)
895 (defcustom org-yank-adjusted-subtrees nil
896 "Non-nil means when yanking subtrees, adjust the level.
897 With this setting, `org-paste-subtree' is used to insert the subtree, see
898 this function for details."
899 :group 'org-edit-structure
900 :type 'boolean)
902 (defcustom org-M-RET-may-split-line '((default . t))
903 "Non-nil means M-RET will split the line at the cursor position.
904 When nil, it will go to the end of the line before making a
905 new line.
906 You may also set this option in a different way for different
907 contexts. Valid contexts are:
909 headline when creating a new headline
910 item when creating a new item
911 table in a table field
912 default the value to be used for all contexts not explicitly
913 customized"
914 :group 'org-structure
915 :group 'org-table
916 :type '(choice
917 (const :tag "Always" t)
918 (const :tag "Never" nil)
919 (repeat :greedy t :tag "Individual contexts"
920 (cons
921 (choice :tag "Context"
922 (const headline)
923 (const item)
924 (const table)
925 (const default))
926 (boolean)))))
929 (defcustom org-insert-heading-respect-content nil
930 "Non-nil means insert new headings after the current subtree.
931 When nil, the new heading is created directly after the current line.
932 The commands \\[org-insert-heading-respect-content] and
933 \\[org-insert-todo-heading-respect-content] turn this variable on
934 for the duration of the command."
935 :group 'org-structure
936 :type 'boolean)
938 (defcustom org-blank-before-new-entry '((heading . auto)
939 (plain-list-item . auto))
940 "Should `org-insert-heading' leave a blank line before new heading/item?
941 The value is an alist, with `heading' and `plain-list-item' as car,
942 and a boolean flag as cdr. For plain lists, if the variable
943 `org-empty-line-terminates-plain-lists' is set, the setting here
944 is ignored and no empty line is inserted, to keep the list in tact."
945 :group 'org-edit-structure
946 :type '(list
947 (cons (const heading)
948 (choice (const :tag "Never" nil)
949 (const :tag "Always" t)
950 (const :tag "Auto" auto)))
951 (cons (const plain-list-item)
952 (choice (const :tag "Never" nil)
953 (const :tag "Always" t)
954 (const :tag "Auto" auto)))))
956 (defcustom org-insert-heading-hook nil
957 "Hook being run after inserting a new heading."
958 :group 'org-edit-structure
959 :type 'hook)
961 (defcustom org-enable-fixed-width-editor t
962 "Non-nil means lines starting with \":\" are treated as fixed-width.
963 This currently only means they are never auto-wrapped.
964 When nil, such lines will be treated like ordinary lines.
965 See also the QUOTE keyword."
966 :group 'org-edit-structure
967 :type 'boolean)
970 (defcustom org-goto-auto-isearch t
971 "Non-nil means typing characters in org-goto starts incremental search."
972 :group 'org-edit-structure
973 :type 'boolean)
975 (defgroup org-sparse-trees nil
976 "Options concerning sparse trees in Org-mode."
977 :tag "Org Sparse Trees"
978 :group 'org-structure)
980 (defcustom org-highlight-sparse-tree-matches t
981 "Non-nil means highlight all matches that define a sparse tree.
982 The highlights will automatically disappear the next time the buffer is
983 changed by an edit command."
984 :group 'org-sparse-trees
985 :type 'boolean)
987 (defcustom org-remove-highlights-with-change t
988 "Non-nil means any change to the buffer will remove temporary highlights.
989 Such highlights are created by `org-occur' and `org-clock-display'.
990 When nil, `C-c C-c needs to be used to get rid of the highlights.
991 The highlights created by `org-preview-latex-fragment' always need
992 `C-c C-c' to be removed."
993 :group 'org-sparse-trees
994 :group 'org-time
995 :type 'boolean)
998 (defcustom org-occur-hook '(org-first-headline-recenter)
999 "Hook that is run after `org-occur' has constructed a sparse tree.
1000 This can be used to recenter the window to show as much of the structure
1001 as possible."
1002 :group 'org-sparse-trees
1003 :type 'hook)
1005 (defgroup org-imenu-and-speedbar nil
1006 "Options concerning imenu and speedbar in Org-mode."
1007 :tag "Org Imenu and Speedbar"
1008 :group 'org-structure)
1010 (defcustom org-imenu-depth 2
1011 "The maximum level for Imenu access to Org-mode headlines.
1012 This also applied for speedbar access."
1013 :group 'org-imenu-and-speedbar
1014 :type 'integer)
1016 (defgroup org-table nil
1017 "Options concerning tables in Org-mode."
1018 :tag "Org Table"
1019 :group 'org)
1021 (defcustom org-enable-table-editor 'optimized
1022 "Non-nil means lines starting with \"|\" are handled by the table editor.
1023 When nil, such lines will be treated like ordinary lines.
1025 When equal to the symbol `optimized', the table editor will be optimized to
1026 do the following:
1027 - Automatic overwrite mode in front of whitespace in table fields.
1028 This makes the structure of the table stay in tact as long as the edited
1029 field does not exceed the column width.
1030 - Minimize the number of realigns. Normally, the table is aligned each time
1031 TAB or RET are pressed to move to another field. With optimization this
1032 happens only if changes to a field might have changed the column width.
1033 Optimization requires replacing the functions `self-insert-command',
1034 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1035 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1036 very good at guessing when a re-align will be necessary, but you can always
1037 force one with \\[org-ctrl-c-ctrl-c].
1039 If you would like to use the optimized version in Org-mode, but the
1040 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1042 This variable can be used to turn on and off the table editor during a session,
1043 but in order to toggle optimization, a restart is required.
1045 See also the variable `org-table-auto-blank-field'."
1046 :group 'org-table
1047 :type '(choice
1048 (const :tag "off" nil)
1049 (const :tag "on" t)
1050 (const :tag "on, optimized" optimized)))
1052 (defcustom org-self-insert-cluster-for-undo t
1053 "Non-nil means cluster self-insert commands for undo when possible.
1054 If this is set, then, like in the Emacs command loop, 20 consecutive
1055 characters will be undone together.
1056 This is configurable, because there is some impact on typing performance."
1057 :group 'org-table
1058 :type 'boolean)
1060 (defcustom org-table-tab-recognizes-table.el t
1061 "Non-nil means TAB will automatically notice a table.el table.
1062 When it sees such a table, it moves point into it and - if necessary -
1063 calls `table-recognize-table'."
1064 :group 'org-table-editing
1065 :type 'boolean)
1067 (defgroup org-link nil
1068 "Options concerning links in Org-mode."
1069 :tag "Org Link"
1070 :group 'org)
1072 (defvar org-link-abbrev-alist-local nil
1073 "Buffer-local version of `org-link-abbrev-alist', which see.
1074 The value of this is taken from the #+LINK lines.")
1075 (make-variable-buffer-local 'org-link-abbrev-alist-local)
1077 (defcustom org-link-abbrev-alist nil
1078 "Alist of link abbreviations.
1079 The car of each element is a string, to be replaced at the start of a link.
1080 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1081 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1083 [[linkkey:tag][description]]
1085 The 'linkkey' must be a word word, starting with a letter, followed
1086 by letters, numbers, '-' or '_'.
1088 If REPLACE is a string, the tag will simply be appended to create the link.
1089 If the string contains \"%s\", the tag will be inserted there. Alternatively,
1090 the placeholder \"%h\" will cause a url-encoded version of the tag to
1091 be inserted at that point (see the function `url-hexify-string').
1093 REPLACE may also be a function that will be called with the tag as the
1094 only argument to create the link, which should be returned as a string.
1096 See the manual for examples."
1097 :group 'org-link
1098 :type '(repeat
1099 (cons
1100 (string :tag "Protocol")
1101 (choice
1102 (string :tag "Format")
1103 (function)))))
1105 (defcustom org-descriptive-links t
1106 "Non-nil means hide link part and only show description of bracket links.
1107 Bracket links are like [[link][description]]. This variable sets the initial
1108 state in new org-mode buffers. The setting can then be toggled on a
1109 per-buffer basis from the Org->Hyperlinks menu."
1110 :group 'org-link
1111 :type 'boolean)
1113 (defcustom org-link-file-path-type 'adaptive
1114 "How the path name in file links should be stored.
1115 Valid values are:
1117 relative Relative to the current directory, i.e. the directory of the file
1118 into which the link is being inserted.
1119 absolute Absolute path, if possible with ~ for home directory.
1120 noabbrev Absolute path, no abbreviation of home directory.
1121 adaptive Use relative path for files in the current directory and sub-
1122 directories of it. For other files, use an absolute path."
1123 :group 'org-link
1124 :type '(choice
1125 (const relative)
1126 (const absolute)
1127 (const noabbrev)
1128 (const adaptive)))
1130 (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
1131 "Types of links that should be activated in Org-mode files.
1132 This is a list of symbols, each leading to the activation of a certain link
1133 type. In principle, it does not hurt to turn on most link types - there may
1134 be a small gain when turning off unused link types. The types are:
1136 bracket The recommended [[link][description]] or [[link]] links with hiding.
1137 angular Links in angular brackets that may contain whitespace like
1138 <bbdb:Carsten Dominik>.
1139 plain Plain links in normal text, no whitespace, like http://google.com.
1140 radio Text that is matched by a radio target, see manual for details.
1141 tag Tag settings in a headline (link to tag search).
1142 date Time stamps (link to calendar).
1143 footnote Footnote labels.
1145 Changing this variable requires a restart of Emacs to become effective."
1146 :group 'org-link
1147 :type '(set :greedy t
1148 (const :tag "Double bracket links (new style)" bracket)
1149 (const :tag "Angular bracket links (old style)" angular)
1150 (const :tag "Plain text links" plain)
1151 (const :tag "Radio target matches" radio)
1152 (const :tag "Tags" tag)
1153 (const :tag "Timestamps" date)
1154 (const :tag "Footnotes" footnote)))
1156 (defcustom org-make-link-description-function nil
1157 "Function to use to generate link descriptions from links. If
1158 nil the link location will be used. This function must take two
1159 parameters; the first is the link and the second the description
1160 org-insert-link has generated, and should return the description
1161 to use."
1162 :group 'org-link
1163 :type 'function)
1165 (defgroup org-link-store nil
1166 "Options concerning storing links in Org-mode."
1167 :tag "Org Store Link"
1168 :group 'org-link)
1170 (defcustom org-email-link-description-format "Email %c: %.30s"
1171 "Format of the description part of a link to an email or usenet message.
1172 The following %-escapes will be replaced by corresponding information:
1174 %F full \"From\" field
1175 %f name, taken from \"From\" field, address if no name
1176 %T full \"To\" field
1177 %t first name in \"To\" field, address if no name
1178 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1179 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1180 %s subject
1181 %m message-id.
1183 You may use normal field width specification between the % and the letter.
1184 This is for example useful to limit the length of the subject.
1186 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1187 :group 'org-link-store
1188 :type 'string)
1190 (defcustom org-from-is-user-regexp
1191 (let (r1 r2)
1192 (when (and user-mail-address (not (string= user-mail-address "")))
1193 (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
1194 (when (and user-full-name (not (string= user-full-name "")))
1195 (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
1196 (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
1197 "Regexp matched against the \"From:\" header of an email or usenet message.
1198 It should match if the message is from the user him/herself."
1199 :group 'org-link-store
1200 :type 'regexp)
1202 (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
1203 "Non-nil means storing a link to an Org file will use entry IDs.
1205 Note that before this variable is even considered, org-id must be loaded,
1206 so please customize `org-modules' and turn it on.
1208 The variable can have the following values:
1210 t Create an ID if needed to make a link to the current entry.
1212 create-if-interactive
1213 If `org-store-link' is called directly (interactively, as a user
1214 command), do create an ID to support the link. But when doing the
1215 job for remember, only use the ID if it already exists. The
1216 purpose of this setting is to avoid proliferation of unwanted
1217 IDs, just because you happen to be in an Org file when you
1218 call `org-remember' that automatically and preemptively
1219 creates a link. If you do want to get an ID link in a remember
1220 template to an entry not having an ID, create it first by
1221 explicitly creating a link to it, using `C-c C-l' first.
1223 create-if-interactive-and-no-custom-id
1224 Like create-if-interactive, but do not create an ID if there is
1225 a CUSTOM_ID property defined in the entry. This is the default.
1227 use-existing
1228 Use existing ID, do not create one.
1230 nil Never use an ID to make a link, instead link using a text search for
1231 the headline text."
1232 :group 'org-link-store
1233 :type '(choice
1234 (const :tag "Create ID to make link" t)
1235 (const :tag "Create if storing link interactively"
1236 create-if-interactive)
1237 (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
1238 create-if-interactive-and-no-custom-id)
1239 (const :tag "Only use existing" use-existing)
1240 (const :tag "Do not use ID to create link" nil)))
1242 (defcustom org-context-in-file-links t
1243 "Non-nil means file links from `org-store-link' contain context.
1244 A search string will be added to the file name with :: as separator and
1245 used to find the context when the link is activated by the command
1246 `org-open-at-point'.
1247 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1248 negates this setting for the duration of the command."
1249 :group 'org-link-store
1250 :type 'boolean)
1252 (defcustom org-keep-stored-link-after-insertion nil
1253 "Non-nil means keep link in list for entire session.
1255 The command `org-store-link' adds a link pointing to the current
1256 location to an internal list. These links accumulate during a session.
1257 The command `org-insert-link' can be used to insert links into any
1258 Org-mode file (offering completion for all stored links). When this
1259 option is nil, every link which has been inserted once using \\[org-insert-link]
1260 will be removed from the list, to make completing the unused links
1261 more efficient."
1262 :group 'org-link-store
1263 :type 'boolean)
1265 (defgroup org-link-follow nil
1266 "Options concerning following links in Org-mode."
1267 :tag "Org Follow Link"
1268 :group 'org-link)
1270 (defcustom org-link-translation-function nil
1271 "Function to translate links with different syntax to Org syntax.
1272 This can be used to translate links created for example by the Planner
1273 or emacs-wiki packages to Org syntax.
1274 The function must accept two parameters, a TYPE containing the link
1275 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1276 which is everything after the link protocol. It should return a cons
1277 with possibly modified values of type and path.
1278 Org contains a function for this, so if you set this variable to
1279 `org-translate-link-from-planner', you should be able follow many
1280 links created by planner."
1281 :group 'org-link-follow
1282 :type 'function)
1284 (defcustom org-follow-link-hook nil
1285 "Hook that is run after a link has been followed."
1286 :group 'org-link-follow
1287 :type 'hook)
1289 (defcustom org-tab-follows-link nil
1290 "Non-nil means on links TAB will follow the link.
1291 Needs to be set before org.el is loaded.
1292 This really should not be used, it does not make sense, and the
1293 implementation is bad."
1294 :group 'org-link-follow
1295 :type 'boolean)
1297 (defcustom org-return-follows-link nil
1298 "Non-nil means on links RET will follow the link.
1299 Needs to be set before org.el is loaded."
1300 :group 'org-link-follow
1301 :type 'boolean)
1303 (defcustom org-mouse-1-follows-link
1304 (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
1305 "Non-nil means mouse-1 on a link will follow the link.
1306 A longer mouse click will still set point. Does not work on XEmacs.
1307 Needs to be set before org.el is loaded."
1308 :group 'org-link-follow
1309 :type 'boolean)
1311 (defcustom org-mark-ring-length 4
1312 "Number of different positions to be recorded in the ring
1313 Changing this requires a restart of Emacs to work correctly."
1314 :group 'org-link-follow
1315 :type 'integer)
1317 (defcustom org-link-frame-setup
1318 '((vm . vm-visit-folder-other-frame)
1319 (gnus . gnus-other-frame)
1320 (file . find-file-other-window))
1321 "Setup the frame configuration for following links.
1322 When following a link with Emacs, it may often be useful to display
1323 this link in another window or frame. This variable can be used to
1324 set this up for the different types of links.
1325 For VM, use any of
1326 `vm-visit-folder'
1327 `vm-visit-folder-other-frame'
1328 For Gnus, use any of
1329 `gnus'
1330 `gnus-other-frame'
1331 `org-gnus-no-new-news'
1332 For FILE, use any of
1333 `find-file'
1334 `find-file-other-window'
1335 `find-file-other-frame'
1336 For the calendar, use the variable `calendar-setup'.
1337 For BBDB, it is currently only possible to display the matches in
1338 another window."
1339 :group 'org-link-follow
1340 :type '(list
1341 (cons (const vm)
1342 (choice
1343 (const vm-visit-folder)
1344 (const vm-visit-folder-other-window)
1345 (const vm-visit-folder-other-frame)))
1346 (cons (const gnus)
1347 (choice
1348 (const gnus)
1349 (const gnus-other-frame)
1350 (const org-gnus-no-new-news)))
1351 (cons (const file)
1352 (choice
1353 (const find-file)
1354 (const find-file-other-window)
1355 (const find-file-other-frame)))))
1357 (defcustom org-display-internal-link-with-indirect-buffer nil
1358 "Non-nil means use indirect buffer to display infile links.
1359 Activating internal links (from one location in a file to another location
1360 in the same file) normally just jumps to the location. When the link is
1361 activated with a C-u prefix (or with mouse-3), the link is displayed in
1362 another window. When this option is set, the other window actually displays
1363 an indirect buffer clone of the current buffer, to avoid any visibility
1364 changes to the current buffer."
1365 :group 'org-link-follow
1366 :type 'boolean)
1368 (defcustom org-open-non-existing-files nil
1369 "Non-nil means `org-open-file' will open non-existing files.
1370 When nil, an error will be generated.
1371 This variable applies only to external applications because they
1372 might choke on non-existing files. If the link is to a file that
1373 will be opened in Emacs, the variable is ignored."
1374 :group 'org-link-follow
1375 :type 'boolean)
1377 (defcustom org-open-directory-means-index-dot-org nil
1378 "Non-nil means a link to a directory really means to index.org.
1379 When nil, following a directory link will run dired or open a finder/explorer
1380 window on that directory."
1381 :group 'org-link-follow
1382 :type 'boolean)
1384 (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
1385 "Function and arguments to call for following mailto links.
1386 This is a list with the first element being a lisp function, and the
1387 remaining elements being arguments to the function. In string arguments,
1388 %a will be replaced by the address, and %s will be replaced by the subject
1389 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1390 :group 'org-link-follow
1391 :type '(choice
1392 (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1393 (const :tag "compose-mail" (compose-mail "%a" "%s"))
1394 (const :tag "message-mail" (message-mail "%a" "%s"))
1395 (cons :tag "other" (function) (repeat :tag "argument" sexp))))
1397 (defcustom org-confirm-shell-link-function 'yes-or-no-p
1398 "Non-nil means ask for confirmation before executing shell links.
1399 Shell links can be dangerous: just think about a link
1401 [[shell:rm -rf ~/*][Google Search]]
1403 This link would show up in your Org-mode document as \"Google Search\",
1404 but really it would remove your entire home directory.
1405 Therefore we advise against setting this variable to nil.
1406 Just change it to `y-or-n-p' if you want to confirm with a
1407 single keystroke rather than having to type \"yes\"."
1408 :group 'org-link-follow
1409 :type '(choice
1410 (const :tag "with yes-or-no (safer)" yes-or-no-p)
1411 (const :tag "with y-or-n (faster)" y-or-n-p)
1412 (const :tag "no confirmation (dangerous)" nil)))
1414 (defcustom org-confirm-elisp-link-function 'yes-or-no-p
1415 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1416 Elisp links can be dangerous: just think about a link
1418 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1420 This link would show up in your Org-mode document as \"Google Search\",
1421 but really it would remove your entire home directory.
1422 Therefore we advise against setting this variable to nil.
1423 Just change it to `y-or-n-p' if you want to confirm with a
1424 single keystroke rather than having to type \"yes\"."
1425 :group 'org-link-follow
1426 :type '(choice
1427 (const :tag "with yes-or-no (safer)" yes-or-no-p)
1428 (const :tag "with y-or-n (faster)" y-or-n-p)
1429 (const :tag "no confirmation (dangerous)" nil)))
1431 (defconst org-file-apps-defaults-gnu
1432 '((remote . emacs)
1433 (system . mailcap)
1434 (t . mailcap))
1435 "Default file applications on a UNIX or GNU/Linux system.
1436 See `org-file-apps'.")
1438 (defconst org-file-apps-defaults-macosx
1439 '((remote . emacs)
1440 (t . "open %s")
1441 (system . "open %s")
1442 ("ps.gz" . "gv %s")
1443 ("eps.gz" . "gv %s")
1444 ("dvi" . "xdvi %s")
1445 ("fig" . "xfig %s"))
1446 "Default file applications on a MacOS X system.
1447 The system \"open\" is known as a default, but we use X11 applications
1448 for some files for which the OS does not have a good default.
1449 See `org-file-apps'.")
1451 (defconst org-file-apps-defaults-windowsnt
1452 (list
1453 '(remote . emacs)
1454 (cons t
1455 (list (if (featurep 'xemacs)
1456 'mswindows-shell-execute
1457 'w32-shell-execute)
1458 "open" 'file))
1459 (cons 'system
1460 (list (if (featurep 'xemacs)
1461 'mswindows-shell-execute
1462 'w32-shell-execute)
1463 "open" 'file)))
1464 "Default file applications on a Windows NT system.
1465 The system \"open\" is used for most files.
1466 See `org-file-apps'.")
1468 (defcustom org-file-apps
1470 (auto-mode . emacs)
1471 ("\\.mm\\'" . default)
1472 ("\\.x?html?\\'" . default)
1473 ("\\.pdf\\'" . default)
1475 "External applications for opening `file:path' items in a document.
1476 Org-mode uses system defaults for different file types, but
1477 you can use this variable to set the application for a given file
1478 extension. The entries in this list are cons cells where the car identifies
1479 files and the cdr the corresponding command. Possible values for the
1480 file identifier are
1481 \"regex\" Regular expression matched against the file name. For backward
1482 compatibility, this can also be a string with only alphanumeric
1483 characters, which is then interpreted as an extension.
1484 `directory' Matches a directory
1485 `remote' Matches a remote file, accessible through tramp or efs.
1486 Remote files most likely should be visited through Emacs
1487 because external applications cannot handle such paths.
1488 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1489 so all files Emacs knows how to handle. Using this with
1490 command `emacs' will open most files in Emacs. Beware that this
1491 will also open html files inside Emacs, unless you add
1492 (\"html\" . default) to the list as well.
1493 t Default for files not matched by any of the other options.
1494 `system' The system command to open files, like `open' on Windows
1495 and Mac OS X, and mailcap under GNU/Linux. This is the command
1496 that will be selected if you call `C-c C-o' with a double
1497 `C-u C-u' prefix.
1499 Possible values for the command are:
1500 `emacs' The file will be visited by the current Emacs process.
1501 `default' Use the default application for this file type, which is the
1502 association for t in the list, most likely in the system-specific
1503 part.
1504 This can be used to overrule an unwanted setting in the
1505 system-specific variable.
1506 `system' Use the system command for opening files, like \"open\".
1507 This command is specified by the entry whose car is `system'.
1508 Most likely, the system-specific version of this variable
1509 does define this command, but you can overrule/replace it
1510 here.
1511 string A command to be executed by a shell; %s will be replaced
1512 by the path to the file.
1513 sexp A Lisp form which will be evaluated. The file path will
1514 be available in the Lisp variable `file'.
1515 For more examples, see the system specific constants
1516 `org-file-apps-defaults-macosx'
1517 `org-file-apps-defaults-windowsnt'
1518 `org-file-apps-defaults-gnu'."
1519 :group 'org-link-follow
1520 :type '(repeat
1521 (cons (choice :value ""
1522 (string :tag "Extension")
1523 (const :tag "System command to open files" system)
1524 (const :tag "Default for unrecognized files" t)
1525 (const :tag "Remote file" remote)
1526 (const :tag "Links to a directory" directory)
1527 (const :tag "Any files that have Emacs modes"
1528 auto-mode))
1529 (choice :value ""
1530 (const :tag "Visit with Emacs" emacs)
1531 (const :tag "Use default" default)
1532 (const :tag "Use the system command" system)
1533 (string :tag "Command")
1534 (sexp :tag "Lisp form")))))
1538 (defgroup org-refile nil
1539 "Options concerning refiling entries in Org-mode."
1540 :tag "Org Refile"
1541 :group 'org)
1543 (defcustom org-directory "~/org"
1544 "Directory with org files.
1545 This is just a default location to look for Org files. There is no need
1546 at all to put your files into this directory. It is only used in the
1547 following situations:
1549 1. When a remember template specifies a target file that is not an
1550 absolute path. The path will then be interpreted relative to
1551 `org-directory'
1552 2. When a remember note is filed away in an interactive way (when exiting the
1553 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1554 with `org-directory' as the default path."
1555 :group 'org-refile
1556 :group 'org-remember
1557 :type 'directory)
1559 (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
1560 "Default target for storing notes.
1561 Used by the hooks for remember.el. This can be a string, or nil to mean
1562 the value of `remember-data-file'.
1563 You can set this on a per-template basis with the variable
1564 `org-remember-templates'."
1565 :group 'org-refile
1566 :group 'org-remember
1567 :type '(choice
1568 (const :tag "Default from remember-data-file" nil)
1569 file))
1571 (defcustom org-goto-interface 'outline
1572 "The default interface to be used for `org-goto'.
1573 Allowed values are:
1574 outline The interface shows an outline of the relevant file
1575 and the correct heading is found by moving through
1576 the outline or by searching with incremental search.
1577 outline-path-completion Headlines in the current buffer are offered via
1578 completion. This is the interface also used by
1579 the refile command."
1580 :group 'org-refile
1581 :type '(choice
1582 (const :tag "Outline" outline)
1583 (const :tag "Outline-path-completion" outline-path-completion)))
1585 (defcustom org-goto-max-level 5
1586 "Maximum level to be considered when running org-goto with refile interface."
1587 :group 'org-refile
1588 :type 'integer)
1590 (defcustom org-reverse-note-order nil
1591 "Non-nil means store new notes at the beginning of a file or entry.
1592 When nil, new notes will be filed to the end of a file or entry.
1593 This can also be a list with cons cells of regular expressions that
1594 are matched against file names, and values."
1595 :group 'org-remember
1596 :group 'org-refile
1597 :type '(choice
1598 (const :tag "Reverse always" t)
1599 (const :tag "Reverse never" nil)
1600 (repeat :tag "By file name regexp"
1601 (cons regexp boolean))))
1603 (defcustom org-log-refile nil
1604 "Information to record when a task is refiled.
1606 Possible values are:
1608 nil Don't add anything
1609 time Add a time stamp to the task
1610 note Prompt for a note and add it with template `org-log-note-headings'
1612 This option can also be set with on a per-file-basis with
1614 #+STARTUP: nologrefile
1615 #+STARTUP: logrefile
1616 #+STARTUP: lognoterefile
1618 You can have local logging settings for a subtree by setting the LOGGING
1619 property to one or more of these keywords.
1621 When bulk-refiling from the agenda, the value `note' is forbidden and
1622 will temporarily be changed to `time'."
1623 :group 'org-refile
1624 :group 'org-progress
1625 :type '(choice
1626 (const :tag "No logging" nil)
1627 (const :tag "Record timestamp" time)
1628 (const :tag "Record timestamp with note." note)))
1630 (defcustom org-refile-targets nil
1631 "Targets for refiling entries with \\[org-refile].
1632 This is list of cons cells. Each cell contains:
1633 - a specification of the files to be considered, either a list of files,
1634 or a symbol whose function or variable value will be used to retrieve
1635 a file name or a list of file names. If you use `org-agenda-files' for
1636 that, all agenda files will be scanned for targets. Nil means consider
1637 headings in the current buffer.
1638 - A specification of how to find candidate refile targets. This may be
1639 any of:
1640 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1641 This tag has to be present in all target headlines, inheritance will
1642 not be considered.
1643 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1644 todo keyword.
1645 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1646 headlines that are refiling targets.
1647 - a cons cell (:level . N). Any headline of level N is considered a target.
1648 Note that, when `org-odd-levels-only' is set, level corresponds to
1649 order in hierarchy, not to the number of stars.
1650 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1651 Note that, when `org-odd-levels-only' is set, level corresponds to
1652 order in hierarchy, not to the number of stars.
1654 You can set the variable `org-refile-target-verify-function' to a function
1655 to verify each headline found by the simple critery above.
1657 When this variable is nil, all top-level headlines in the current buffer
1658 are used, equivalent to the value `((nil . (:level . 1))'."
1659 :group 'org-refile
1660 :type '(repeat
1661 (cons
1662 (choice :value org-agenda-files
1663 (const :tag "All agenda files" org-agenda-files)
1664 (const :tag "Current buffer" nil)
1665 (function) (variable) (file))
1666 (choice :tag "Identify target headline by"
1667 (cons :tag "Specific tag" (const :value :tag) (string))
1668 (cons :tag "TODO keyword" (const :value :todo) (string))
1669 (cons :tag "Regular expression" (const :value :regexp) (regexp))
1670 (cons :tag "Level number" (const :value :level) (integer))
1671 (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
1673 (defcustom org-refile-target-verify-function nil
1674 "Function to verify if the headline at point should be a refile target.
1675 The function will be called without arguments, with point at the
1676 beginning of the headline. It should return t and leave point
1677 where it is if the headline is a valid target for refiling.
1679 If the target should not be selected, the function must return nil.
1680 In addition to this, it may move point to a place from where the search
1681 should be continued. For example, the function may decide that the entire
1682 subtree of the current entry should be excluded and move point to the end
1683 of the subtree."
1684 :group 'org-refile
1685 :type 'function)
1687 (defcustom org-refile-use-outline-path nil
1688 "Non-nil means provide refile targets as paths.
1689 So a level 3 headline will be available as level1/level2/level3.
1691 When the value is `file', also include the file name (without directory)
1692 into the path. In this case, you can also stop the completion after
1693 the file name, to get entries inserted as top level in the file.
1695 When `full-file-path', include the full file path."
1696 :group 'org-refile
1697 :type '(choice
1698 (const :tag "Not" nil)
1699 (const :tag "Yes" t)
1700 (const :tag "Start with file name" file)
1701 (const :tag "Start with full file path" full-file-path)))
1703 (defcustom org-outline-path-complete-in-steps t
1704 "Non-nil means complete the outline path in hierarchical steps.
1705 When Org-mode uses the refile interface to select an outline path
1706 \(see variable `org-refile-use-outline-path'), the completion of
1707 the path can be done is a single go, or if can be done in steps down
1708 the headline hierarchy. Going in steps is probably the best if you
1709 do not use a special completion package like `ido' or `icicles'.
1710 However, when using these packages, going in one step can be very
1711 fast, while still showing the whole path to the entry."
1712 :group 'org-refile
1713 :type 'boolean)
1715 (defcustom org-refile-allow-creating-parent-nodes nil
1716 "Non-nil means allow to create new nodes as refile targets.
1717 New nodes are then created by adding \"/new node name\" to the completion
1718 of an existing node. When the value of this variable is `confirm',
1719 new node creation must be confirmed by the user (recommended)
1720 When nil, the completion must match an existing entry.
1722 Note that, if the new heading is not seen by the criteria
1723 listed in `org-refile-targets', multiple instances of the same
1724 heading would be created by trying again to file under the new
1725 heading."
1726 :group 'org-refile
1727 :type '(choice
1728 (const :tag "Never" nil)
1729 (const :tag "Always" t)
1730 (const :tag "Prompt for confirmation" confirm)))
1732 (defgroup org-todo nil
1733 "Options concerning TODO items in Org-mode."
1734 :tag "Org TODO"
1735 :group 'org)
1737 (defgroup org-progress nil
1738 "Options concerning Progress logging in Org-mode."
1739 :tag "Org Progress"
1740 :group 'org-time)
1742 (defvar org-todo-interpretation-widgets
1744 (:tag "Sequence (cycling hits every state)" sequence)
1745 (:tag "Type (cycling directly to DONE)" type))
1746 "The available interpretation symbols for customizing
1747 `org-todo-keywords'.
1748 Interested libraries should add to this list.")
1750 (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
1751 "List of TODO entry keyword sequences and their interpretation.
1752 \\<org-mode-map>This is a list of sequences.
1754 Each sequence starts with a symbol, either `sequence' or `type',
1755 indicating if the keywords should be interpreted as a sequence of
1756 action steps, or as different types of TODO items. The first
1757 keywords are states requiring action - these states will select a headline
1758 for inclusion into the global TODO list Org-mode produces. If one of
1759 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1760 signify that no further action is necessary. If \"|\" is not found,
1761 the last keyword is treated as the only DONE state of the sequence.
1763 The command \\[org-todo] cycles an entry through these states, and one
1764 additional state where no keyword is present. For details about this
1765 cycling, see the manual.
1767 TODO keywords and interpretation can also be set on a per-file basis with
1768 the special #+SEQ_TODO and #+TYP_TODO lines.
1770 Each keyword can optionally specify a character for fast state selection
1771 \(in combination with the variable `org-use-fast-todo-selection')
1772 and specifiers for state change logging, using the same syntax
1773 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1774 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1775 indicates to record a time stamp each time this state is selected.
1777 Each keyword may also specify if a timestamp or a note should be
1778 recorded when entering or leaving the state, by adding additional
1779 characters in the parenthesis after the keyword. This looks like this:
1780 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1781 record only the time of the state change. With X and Y being either
1782 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1783 Y when leaving the state if and only if the *target* state does not
1784 define X. You may omit any of the fast-selection key or X or /Y,
1785 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1787 For backward compatibility, this variable may also be just a list
1788 of keywords - in this case the interpretation (sequence or type) will be
1789 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1790 :group 'org-todo
1791 :group 'org-keywords
1792 :type '(choice
1793 (repeat :tag "Old syntax, just keywords"
1794 (string :tag "Keyword"))
1795 (repeat :tag "New syntax"
1796 (cons
1797 (choice
1798 :tag "Interpretation"
1799 ;;Quick and dirty way to see
1800 ;;`org-todo-interpretations'. This takes the
1801 ;;place of item arguments
1802 :convert-widget
1803 (lambda (widget)
1804 (widget-put widget
1805 :args (mapcar
1806 #'(lambda (x)
1807 (widget-convert
1808 (cons 'const x)))
1809 org-todo-interpretation-widgets))
1810 widget))
1811 (repeat
1812 (string :tag "Keyword"))))))
1814 (defvar org-todo-keywords-1 nil
1815 "All TODO and DONE keywords active in a buffer.")
1816 (make-variable-buffer-local 'org-todo-keywords-1)
1817 (defvar org-todo-keywords-for-agenda nil)
1818 (defvar org-done-keywords-for-agenda nil)
1819 (defvar org-drawers-for-agenda nil)
1820 (defvar org-todo-keyword-alist-for-agenda nil)
1821 (defvar org-tag-alist-for-agenda nil)
1822 (defvar org-agenda-contributing-files nil)
1823 (defvar org-not-done-keywords nil)
1824 (make-variable-buffer-local 'org-not-done-keywords)
1825 (defvar org-done-keywords nil)
1826 (make-variable-buffer-local 'org-done-keywords)
1827 (defvar org-todo-heads nil)
1828 (make-variable-buffer-local 'org-todo-heads)
1829 (defvar org-todo-sets nil)
1830 (make-variable-buffer-local 'org-todo-sets)
1831 (defvar org-todo-log-states nil)
1832 (make-variable-buffer-local 'org-todo-log-states)
1833 (defvar org-todo-kwd-alist nil)
1834 (make-variable-buffer-local 'org-todo-kwd-alist)
1835 (defvar org-todo-key-alist nil)
1836 (make-variable-buffer-local 'org-todo-key-alist)
1837 (defvar org-todo-key-trigger nil)
1838 (make-variable-buffer-local 'org-todo-key-trigger)
1840 (defcustom org-todo-interpretation 'sequence
1841 "Controls how TODO keywords are interpreted.
1842 This variable is in principle obsolete and is only used for
1843 backward compatibility, if the interpretation of todo keywords is
1844 not given already in `org-todo-keywords'. See that variable for
1845 more information."
1846 :group 'org-todo
1847 :group 'org-keywords
1848 :type '(choice (const sequence)
1849 (const type)))
1851 (defcustom org-use-fast-todo-selection t
1852 "Non-nil means use the fast todo selection scheme with C-c C-t.
1853 This variable describes if and under what circumstances the cycling
1854 mechanism for TODO keywords will be replaced by a single-key, direct
1855 selection scheme.
1857 When nil, fast selection is never used.
1859 When the symbol `prefix', it will be used when `org-todo' is called with
1860 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1861 in an agenda buffer.
1863 When t, fast selection is used by default. In this case, the prefix
1864 argument forces cycling instead.
1866 In all cases, the special interface is only used if access keys have actually
1867 been assigned by the user, i.e. if keywords in the configuration are followed
1868 by a letter in parenthesis, like TODO(t)."
1869 :group 'org-todo
1870 :type '(choice
1871 (const :tag "Never" nil)
1872 (const :tag "By default" t)
1873 (const :tag "Only with C-u C-c C-t" prefix)))
1875 (defcustom org-provide-todo-statistics t
1876 "Non-nil means update todo statistics after insert and toggle.
1877 ALL-HEADLINES means update todo statistics by including headlines
1878 with no TODO keyword as well, counting them as not done.
1879 A list of TODO keywords means the same, but skip keywords that are
1880 not in this list.
1882 When this is set, todo statistics is updated in the parent of the
1883 current entry each time a todo state is changed."
1884 :group 'org-todo
1885 :type '(choice
1886 (const :tag "Yes, only for TODO entries" t)
1887 (const :tag "Yes, including all entries" 'all-headlines)
1888 (repeat :tag "Yes, for TODOs in this list"
1889 (string :tag "TODO keyword"))
1890 (other :tag "No TODO statistics" nil)))
1892 (defcustom org-hierarchical-todo-statistics t
1893 "Non-nil means TODO statistics covers just direct children.
1894 When nil, all entries in the subtree are considered.
1895 This has only an effect if `org-provide-todo-statistics' is set.
1896 To set this to nil for only a single subtree, use a COOKIE_DATA
1897 property and include the word \"recursive\" into the value."
1898 :group 'org-todo
1899 :type 'boolean)
1901 (defcustom org-after-todo-state-change-hook nil
1902 "Hook which is run after the state of a TODO item was changed.
1903 The new state (a string with a TODO keyword, or nil) is available in the
1904 Lisp variable `state'."
1905 :group 'org-todo
1906 :type 'hook)
1908 (defvar org-blocker-hook nil
1909 "Hook for functions that are allowed to block a state change.
1911 Each function gets as its single argument a property list, see
1912 `org-trigger-hook' for more information about this list.
1914 If any of the functions in this hook returns nil, the state change
1915 is blocked.")
1917 (defvar org-trigger-hook nil
1918 "Hook for functions that are triggered by a state change.
1920 Each function gets as its single argument a property list with at least
1921 the following elements:
1923 (:type type-of-change :position pos-at-entry-start
1924 :from old-state :to new-state)
1926 Depending on the type, more properties may be present.
1928 This mechanism is currently implemented for:
1930 TODO state changes
1931 ------------------
1932 :type todo-state-change
1933 :from previous state (keyword as a string), or nil, or a symbol
1934 'todo' or 'done', to indicate the general type of state.
1935 :to new state, like in :from")
1937 (defcustom org-enforce-todo-dependencies nil
1938 "Non-nil means undone TODO entries will block switching the parent to DONE.
1939 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
1940 be blocked if any prior sibling is not yet done.
1941 Finally, if the parent is blocked because of ordered siblings of its own,
1942 the child will also be blocked.
1943 This variable needs to be set before org.el is loaded, and you need to
1944 restart Emacs after a change to make the change effective. The only way
1945 to change is while Emacs is running is through the customize interface."
1946 :set (lambda (var val)
1947 (set var val)
1948 (if val
1949 (add-hook 'org-blocker-hook
1950 'org-block-todo-from-children-or-siblings-or-parent)
1951 (remove-hook 'org-blocker-hook
1952 'org-block-todo-from-children-or-siblings-or-parent)))
1953 :group 'org-todo
1954 :type 'boolean)
1956 (defcustom org-enforce-todo-checkbox-dependencies nil
1957 "Non-nil means unchecked boxes will block switching the parent to DONE.
1958 When this is nil, checkboxes have no influence on switching TODO states.
1959 When non-nil, you first need to check off all check boxes before the TODO
1960 entry can be switched to DONE.
1961 This variable needs to be set before org.el is loaded, and you need to
1962 restart Emacs after a change to make the change effective. The only way
1963 to change is while Emacs is running is through the customize interface."
1964 :set (lambda (var val)
1965 (set var val)
1966 (if val
1967 (add-hook 'org-blocker-hook
1968 'org-block-todo-from-checkboxes)
1969 (remove-hook 'org-blocker-hook
1970 'org-block-todo-from-checkboxes)))
1971 :group 'org-todo
1972 :type 'boolean)
1974 (defcustom org-treat-insert-todo-heading-as-state-change nil
1975 "Non-nil means inserting a TODO heading is treated as state change.
1976 So when the command \\[org-insert-todo-heading] is used, state change
1977 logging will apply if appropriate. When nil, the new TODO item will
1978 be inserted directly, and no logging will take place."
1979 :group 'org-todo
1980 :type 'boolean)
1982 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
1983 "Non-nil means switching TODO states with S-cursor counts as state change.
1984 This is the default behavior. However, setting this to nil allows a
1985 convenient way to select a TODO state and bypass any logging associated
1986 with that."
1987 :group 'org-todo
1988 :type 'boolean)
1990 (defcustom org-todo-state-tags-triggers nil
1991 "Tag changes that should be triggered by TODO state changes.
1992 This is a list. Each entry is
1994 (state-change (tag . flag) .......)
1996 State-change can be a string with a state, and empty string to indicate the
1997 state that has no TODO keyword, or it can be one of the symbols `todo'
1998 or `done', meaning any not-done or done state, respectively."
1999 :group 'org-todo
2000 :group 'org-tags
2001 :type '(repeat
2002 (cons (choice :tag "When changing to"
2003 (const :tag "Not-done state" todo)
2004 (const :tag "Done state" done)
2005 (string :tag "State"))
2006 (repeat
2007 (cons :tag "Tag action"
2008 (string :tag "Tag")
2009 (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
2011 (defcustom org-log-done nil
2012 "Information to record when a task moves to the DONE state.
2014 Possible values are:
2016 nil Don't add anything, just change the keyword
2017 time Add a time stamp to the task
2018 note Prompt for a note and add it with template `org-log-note-headings'
2020 This option can also be set with on a per-file-basis with
2022 #+STARTUP: nologdone
2023 #+STARTUP: logdone
2024 #+STARTUP: lognotedone
2026 You can have local logging settings for a subtree by setting the LOGGING
2027 property to one or more of these keywords."
2028 :group 'org-todo
2029 :group 'org-progress
2030 :type '(choice
2031 (const :tag "No logging" nil)
2032 (const :tag "Record CLOSED timestamp" time)
2033 (const :tag "Record CLOSED timestamp with note." note)))
2035 ;; Normalize old uses of org-log-done.
2036 (cond
2037 ((eq org-log-done t) (setq org-log-done 'time))
2038 ((and (listp org-log-done) (memq 'done org-log-done))
2039 (setq org-log-done 'note)))
2041 (defcustom org-log-reschedule nil
2042 "Information to record when the scheduling date of a tasks is modified.
2044 Possible values are:
2046 nil Don't add anything, just change the date
2047 time Add a time stamp to the task
2048 note Prompt for a note and add it with template `org-log-note-headings'
2050 This option can also be set with on a per-file-basis with
2052 #+STARTUP: nologreschedule
2053 #+STARTUP: logreschedule
2054 #+STARTUP: lognotereschedule"
2055 :group 'org-todo
2056 :group 'org-progress
2057 :type '(choice
2058 (const :tag "No logging" nil)
2059 (const :tag "Record timestamp" time)
2060 (const :tag "Record timestamp with note." note)))
2062 (defcustom org-log-redeadline nil
2063 "Information to record when the deadline date of a tasks is modified.
2065 Possible values are:
2067 nil Don't add anything, just change the date
2068 time Add a time stamp to the task
2069 note Prompt for a note and add it with template `org-log-note-headings'
2071 This option can also be set with on a per-file-basis with
2073 #+STARTUP: nologredeadline
2074 #+STARTUP: logredeadline
2075 #+STARTUP: lognoteredeadline
2077 You can have local logging settings for a subtree by setting the LOGGING
2078 property to one or more of these keywords."
2079 :group 'org-todo
2080 :group 'org-progress
2081 :type '(choice
2082 (const :tag "No logging" nil)
2083 (const :tag "Record timestamp" time)
2084 (const :tag "Record timestamp with note." note)))
2086 (defcustom org-log-note-clock-out nil
2087 "Non-nil means record a note when clocking out of an item.
2088 This can also be configured on a per-file basis by adding one of
2089 the following lines anywhere in the buffer:
2091 #+STARTUP: lognoteclock-out
2092 #+STARTUP: nolognoteclock-out"
2093 :group 'org-todo
2094 :group 'org-progress
2095 :type 'boolean)
2097 (defcustom org-log-done-with-time t
2098 "Non-nil means the CLOSED time stamp will contain date and time.
2099 When nil, only the date will be recorded."
2100 :group 'org-progress
2101 :type 'boolean)
2103 (defcustom org-log-note-headings
2104 '((done . "CLOSING NOTE %t")
2105 (state . "State %-12s from %-12S %t")
2106 (note . "Note taken on %t")
2107 (reschedule . "Rescheduled from %S on %t")
2108 (delschedule . "Not scheduled, was %S on %t")
2109 (redeadline . "New deadline from %S on %t")
2110 (deldeadline . "Removed deadline, was %S on %t")
2111 (refile . "Refiled on %t")
2112 (clock-out . ""))
2113 "Headings for notes added to entries.
2114 The value is an alist, with the car being a symbol indicating the note
2115 context, and the cdr is the heading to be used. The heading may also be the
2116 empty string.
2117 %t in the heading will be replaced by a time stamp.
2118 %s will be replaced by the new TODO state, in double quotes.
2119 %S will be replaced by the old TODO state, in double quotes.
2120 %u will be replaced by the user name.
2121 %U will be replaced by the full user name.
2123 In fact, it is not a good idea to change the `state' entry, because
2124 agenda log mode depends on the format of these entries."
2125 :group 'org-todo
2126 :group 'org-progress
2127 :type '(list :greedy t
2128 (cons (const :tag "Heading when closing an item" done) string)
2129 (cons (const :tag
2130 "Heading when changing todo state (todo sequence only)"
2131 state) string)
2132 (cons (const :tag "Heading when just taking a note" note) string)
2133 (cons (const :tag "Heading when clocking out" clock-out) string)
2134 (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
2135 (cons (const :tag "Heading when rescheduling" reschedule) string)
2136 (cons (const :tag "Heading when changing deadline" redeadline) string)
2137 (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
2138 (cons (const :tag "Heading when refiling" refile) string)))
2140 (unless (assq 'note org-log-note-headings)
2141 (push '(note . "%t") org-log-note-headings))
2143 (defcustom org-log-into-drawer nil
2144 "Non-nil means insert state change notes and time stamps into a drawer.
2145 When nil, state changes notes will be inserted after the headline and
2146 any scheduling and clock lines, but not inside a drawer.
2148 The value of this variable should be the name of the drawer to use.
2149 LOGBOOK is proposed at the default drawer for this purpose, you can
2150 also set this to a string to define the drawer of your choice.
2152 A value of t is also allowed, representing \"LOGBOOK\".
2154 If this variable is set, `org-log-state-notes-insert-after-drawers'
2155 will be ignored.
2157 You can set the property LOG_INTO_DRAWER to overrule this setting for
2158 a subtree."
2159 :group 'org-todo
2160 :group 'org-progress
2161 :type '(choice
2162 (const :tag "Not into a drawer" nil)
2163 (const :tag "LOGBOOK" t)
2164 (string :tag "Other")))
2166 (if (fboundp 'defvaralias)
2167 (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
2169 (defun org-log-into-drawer ()
2170 "Return the value of `org-log-into-drawer', but let properties overrule.
2171 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2172 used instead of the default value."
2173 (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
2174 (cond
2175 ((or (not p) (equal p "nil")) org-log-into-drawer)
2176 ((equal p "t") "LOGBOOK")
2177 (t p))))
2179 (defcustom org-log-state-notes-insert-after-drawers nil
2180 "Non-nil means insert state change notes after any drawers in entry.
2181 Only the drawers that *immediately* follow the headline and the
2182 deadline/scheduled line are skipped.
2183 When nil, insert notes right after the heading and perhaps the line
2184 with deadline/scheduling if present.
2186 This variable will have no effect if `org-log-into-drawer' is
2187 set."
2188 :group 'org-todo
2189 :group 'org-progress
2190 :type 'boolean)
2192 (defcustom org-log-states-order-reversed t
2193 "Non-nil means the latest state note will be directly after heading.
2194 When nil, the state change notes will be ordered according to time."
2195 :group 'org-todo
2196 :group 'org-progress
2197 :type 'boolean)
2199 (defcustom org-todo-repeat-to-state nil
2200 "The TODO state to which a repeater should return the repeating task.
2201 By default this is the first task in a TODO sequence, or the previous state
2202 in a TODO_TYP set. But you can specify another task here.
2203 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2204 :group 'org-todo
2205 :type '(choice (const :tag "Head of sequence" nil)
2206 (string :tag "Specific state")))
2208 (defcustom org-log-repeat 'time
2209 "Non-nil means record moving through the DONE state when triggering repeat.
2210 An auto-repeating task is immediately switched back to TODO when
2211 marked DONE. If you are not logging state changes (by adding \"@\"
2212 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2213 record a closing note, there will be no record of the task moving
2214 through DONE. This variable forces taking a note anyway.
2216 nil Don't force a record
2217 time Record a time stamp
2218 note Record a note
2220 This option can also be set with on a per-file-basis with
2222 #+STARTUP: logrepeat
2223 #+STARTUP: lognoterepeat
2224 #+STARTUP: nologrepeat
2226 You can have local logging settings for a subtree by setting the LOGGING
2227 property to one or more of these keywords."
2228 :group 'org-todo
2229 :group 'org-progress
2230 :type '(choice
2231 (const :tag "Don't force a record" nil)
2232 (const :tag "Force recording the DONE state" time)
2233 (const :tag "Force recording a note with the DONE state" note)))
2236 (defgroup org-priorities nil
2237 "Priorities in Org-mode."
2238 :tag "Org Priorities"
2239 :group 'org-todo)
2241 (defcustom org-enable-priority-commands t
2242 "Non-nil means priority commands are active.
2243 When nil, these commands will be disabled, so that you never accidentally
2244 set a priority."
2245 :group 'org-priorities
2246 :type 'boolean)
2248 (defcustom org-highest-priority ?A
2249 "The highest priority of TODO items. A character like ?A, ?B etc.
2250 Must have a smaller ASCII number than `org-lowest-priority'."
2251 :group 'org-priorities
2252 :type 'character)
2254 (defcustom org-lowest-priority ?C
2255 "The lowest priority of TODO items. A character like ?A, ?B etc.
2256 Must have a larger ASCII number than `org-highest-priority'."
2257 :group 'org-priorities
2258 :type 'character)
2260 (defcustom org-default-priority ?B
2261 "The default priority of TODO items.
2262 This is the priority an item get if no explicit priority is given."
2263 :group 'org-priorities
2264 :type 'character)
2266 (defcustom org-priority-start-cycle-with-default t
2267 "Non-nil means start with default priority when starting to cycle.
2268 When this is nil, the first step in the cycle will be (depending on the
2269 command used) one higher or lower that the default priority."
2270 :group 'org-priorities
2271 :type 'boolean)
2273 (defgroup org-time nil
2274 "Options concerning time stamps and deadlines in Org-mode."
2275 :tag "Org Time"
2276 :group 'org)
2278 (defcustom org-insert-labeled-timestamps-at-point nil
2279 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2280 When nil, these labeled time stamps are forces into the second line of an
2281 entry, just after the headline. When scheduling from the global TODO list,
2282 the time stamp will always be forced into the second line."
2283 :group 'org-time
2284 :type 'boolean)
2286 (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
2287 "Formats for `format-time-string' which are used for time stamps.
2288 It is not recommended to change this constant.")
2290 (defcustom org-time-stamp-rounding-minutes '(0 5)
2291 "Number of minutes to round time stamps to.
2292 These are two values, the first applies when first creating a time stamp.
2293 The second applies when changing it with the commands `S-up' and `S-down'.
2294 When changing the time stamp, this means that it will change in steps
2295 of N minutes, as given by the second value.
2297 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2298 numbers should be factors of 60, so for example 5, 10, 15.
2300 When this is larger than 1, you can still force an exact time-stamp by using
2301 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
2302 and by using a prefix arg to `S-up/down' to specify the exact number
2303 of minutes to shift."
2304 :group 'org-time
2305 :get '(lambda (var) ; Make sure all entries have 5 elements
2306 (if (integerp (default-value var))
2307 (list (default-value var) 5)
2308 (default-value var)))
2309 :type '(list
2310 (integer :tag "when inserting times")
2311 (integer :tag "when modifying times")))
2313 ;; Normalize old customizations of this variable.
2314 (when (integerp org-time-stamp-rounding-minutes)
2315 (setq org-time-stamp-rounding-minutes
2316 (list org-time-stamp-rounding-minutes
2317 org-time-stamp-rounding-minutes)))
2319 (defcustom org-display-custom-times nil
2320 "Non-nil means overlay custom formats over all time stamps.
2321 The formats are defined through the variable `org-time-stamp-custom-formats'.
2322 To turn this on on a per-file basis, insert anywhere in the file:
2323 #+STARTUP: customtime"
2324 :group 'org-time
2325 :set 'set-default
2326 :type 'sexp)
2327 (make-variable-buffer-local 'org-display-custom-times)
2329 (defcustom org-time-stamp-custom-formats
2330 '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
2331 "Custom formats for time stamps. See `format-time-string' for the syntax.
2332 These are overlayed over the default ISO format if the variable
2333 `org-display-custom-times' is set. Time like %H:%M should be at the
2334 end of the second format. The custom formats are also honored by export
2335 commands, if custom time display is turned on at the time of export."
2336 :group 'org-time
2337 :type 'sexp)
2339 (defun org-time-stamp-format (&optional long inactive)
2340 "Get the right format for a time string."
2341 (let ((f (if long (cdr org-time-stamp-formats)
2342 (car org-time-stamp-formats))))
2343 (if inactive
2344 (concat "[" (substring f 1 -1) "]")
2345 f)))
2347 (defcustom org-time-clocksum-format "%d:%02d"
2348 "The format string used when creating CLOCKSUM lines, or when
2349 org-mode generates a time duration."
2350 :group 'org-time
2351 :type 'string)
2353 (defcustom org-time-clocksum-use-fractional nil
2354 "If non-nil, \\[org-clock-display] uses fractional times.
2355 org-mode generates a time duration."
2356 :group 'org-time
2357 :type 'boolean)
2359 (defcustom org-time-clocksum-fractional-format "%.2f"
2360 "The format string used when creating CLOCKSUM lines, or when
2361 org-mode generates a time duration."
2362 :group 'org-time
2363 :type 'string)
2365 (defcustom org-deadline-warning-days 14
2366 "No. of days before expiration during which a deadline becomes active.
2367 This variable governs the display in sparse trees and in the agenda.
2368 When 0 or negative, it means use this number (the absolute value of it)
2369 even if a deadline has a different individual lead time specified.
2371 Custom commands can set this variable in the options section."
2372 :group 'org-time
2373 :group 'org-agenda-daily/weekly
2374 :type 'integer)
2376 (defcustom org-read-date-prefer-future t
2377 "Non-nil means assume future for incomplete date input from user.
2378 This affects the following situations:
2379 1. The user gives a month but not a year.
2380 For example, if it is april and you enter \"feb 2\", this will be read
2381 as feb 2, *next* year. \"May 5\", however, will be this year.
2382 2. The user gives a day, but no month.
2383 For example, if today is the 15th, and you enter \"3\", Org-mode will
2384 read this as the third of *next* month. However, if you enter \"17\",
2385 it will be considered as *this* month.
2387 If you set this variable to the symbol `time', then also the following
2388 will work:
2390 3. If the user gives a time, but no day. If the time is before now,
2391 to will be interpreted as tomorrow.
2393 Currently none of this works for ISO week specifications.
2395 When this option is nil, the current day, month and year will always be
2396 used as defaults."
2397 :group 'org-time
2398 :type '(choice
2399 (const :tag "Never" nil)
2400 (const :tag "Check month and day" t)
2401 (const :tag "Check month, day, and time" time)))
2403 (defcustom org-read-date-display-live t
2404 "Non-nil means display current interpretation of date prompt live.
2405 This display will be in an overlay, in the minibuffer."
2406 :group 'org-time
2407 :type 'boolean)
2409 (defcustom org-read-date-popup-calendar t
2410 "Non-nil means pop up a calendar when prompting for a date.
2411 In the calendar, the date can be selected with mouse-1. However, the
2412 minibuffer will also be active, and you can simply enter the date as well.
2413 When nil, only the minibuffer will be available."
2414 :group 'org-time
2415 :type 'boolean)
2416 (if (fboundp 'defvaralias)
2417 (defvaralias 'org-popup-calendar-for-date-prompt
2418 'org-read-date-popup-calendar))
2420 (defcustom org-read-date-minibuffer-setup-hook nil
2421 "Hook to be used to set up keys for the date/time interface.
2422 Add key definitions to `minibuffer-local-map', which will be a temporary
2423 copy."
2424 :group 'org-time
2425 :type 'hook)
2427 (defcustom org-extend-today-until 0
2428 "The hour when your day really ends. Must be an integer.
2429 This has influence for the following applications:
2430 - When switching the agenda to \"today\". It it is still earlier than
2431 the time given here, the day recognized as TODAY is actually yesterday.
2432 - When a date is read from the user and it is still before the time given
2433 here, the current date and time will be assumed to be yesterday, 23:59.
2434 Also, timestamps inserted in remember templates follow this rule.
2436 IMPORTANT: This is a feature whose implementation is and likely will
2437 remain incomplete. Really, it is only here because past midnight seems to
2438 be the favorite working time of John Wiegley :-)"
2439 :group 'org-time
2440 :type 'integer)
2442 (defcustom org-edit-timestamp-down-means-later nil
2443 "Non-nil means S-down will increase the time in a time stamp.
2444 When nil, S-up will increase."
2445 :group 'org-time
2446 :type 'boolean)
2448 (defcustom org-calendar-follow-timestamp-change t
2449 "Non-nil means make the calendar window follow timestamp changes.
2450 When a timestamp is modified and the calendar window is visible, it will be
2451 moved to the new date."
2452 :group 'org-time
2453 :type 'boolean)
2455 (defgroup org-tags nil
2456 "Options concerning tags in Org-mode."
2457 :tag "Org Tags"
2458 :group 'org)
2460 (defcustom org-tag-alist nil
2461 "List of tags allowed in Org-mode files.
2462 When this list is nil, Org-mode will base TAG input on what is already in the
2463 buffer.
2464 The value of this variable is an alist, the car of each entry must be a
2465 keyword as a string, the cdr may be a character that is used to select
2466 that tag through the fast-tag-selection interface.
2467 See the manual for details."
2468 :group 'org-tags
2469 :type '(repeat
2470 (choice
2471 (cons (string :tag "Tag name")
2472 (character :tag "Access char"))
2473 (list :tag "Start radio group"
2474 (const :startgroup)
2475 (option (string :tag "Group description")))
2476 (list :tag "End radio group"
2477 (const :endgroup)
2478 (option (string :tag "Group description")))
2479 (const :tag "New line" (:newline)))))
2481 (defcustom org-tag-persistent-alist nil
2482 "List of tags that will always appear in all Org-mode files.
2483 This is in addition to any in buffer settings or customizations
2484 of `org-tag-alist'.
2485 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2486 The value of this variable is an alist, the car of each entry must be a
2487 keyword as a string, the cdr may be a character that is used to select
2488 that tag through the fast-tag-selection interface.
2489 See the manual for details.
2490 To disable these tags on a per-file basis, insert anywhere in the file:
2491 #+STARTUP: noptag"
2492 :group 'org-tags
2493 :type '(repeat
2494 (choice
2495 (cons (string :tag "Tag name")
2496 (character :tag "Access char"))
2497 (const :tag "Start radio group" (:startgroup))
2498 (const :tag "End radio group" (:endgroup))
2499 (const :tag "New line" (:newline)))))
2501 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2502 "If non-nil, always offer completion for all tags of all agenda files.
2503 Instead of customizing this variable directly, you might want to
2504 set it locally for remember buffers, because there no list of
2505 tags in that file can be created dynamically (there are none).
2507 (add-hook 'org-remember-mode-hook
2508 (lambda ()
2509 (set (make-local-variable
2510 'org-complete-tags-always-offer-all-agenda-tags)
2511 t)))"
2512 :group 'org-tags
2513 :type 'boolean)
2515 (defvar org-file-tags nil
2516 "List of tags that can be inherited by all entries in the file.
2517 The tags will be inherited if the variable `org-use-tag-inheritance'
2518 says they should be.
2519 This variable is populated from #+FILETAGS lines.")
2521 (defcustom org-use-fast-tag-selection 'auto
2522 "Non-nil means use fast tag selection scheme.
2523 This is a special interface to select and deselect tags with single keys.
2524 When nil, fast selection is never used.
2525 When the symbol `auto', fast selection is used if and only if selection
2526 characters for tags have been configured, either through the variable
2527 `org-tag-alist' or through a #+TAGS line in the buffer.
2528 When t, fast selection is always used and selection keys are assigned
2529 automatically if necessary."
2530 :group 'org-tags
2531 :type '(choice
2532 (const :tag "Always" t)
2533 (const :tag "Never" nil)
2534 (const :tag "When selection characters are configured" 'auto)))
2536 (defcustom org-fast-tag-selection-single-key nil
2537 "Non-nil means fast tag selection exits after first change.
2538 When nil, you have to press RET to exit it.
2539 During fast tag selection, you can toggle this flag with `C-c'.
2540 This variable can also have the value `expert'. In this case, the window
2541 displaying the tags menu is not even shown, until you press C-c again."
2542 :group 'org-tags
2543 :type '(choice
2544 (const :tag "No" nil)
2545 (const :tag "Yes" t)
2546 (const :tag "Expert" expert)))
2548 (defvar org-fast-tag-selection-include-todo nil
2549 "Non-nil means fast tags selection interface will also offer TODO states.
2550 This is an undocumented feature, you should not rely on it.")
2552 (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
2553 "The column to which tags should be indented in a headline.
2554 If this number is positive, it specifies the column. If it is negative,
2555 it means that the tags should be flushright to that column. For example,
2556 -80 works well for a normal 80 character screen."
2557 :group 'org-tags
2558 :type 'integer)
2560 (defcustom org-auto-align-tags t
2561 "Non-nil means realign tags after pro/demotion of TODO state change.
2562 These operations change the length of a headline and therefore shift
2563 the tags around. With this options turned on, after each such operation
2564 the tags are again aligned to `org-tags-column'."
2565 :group 'org-tags
2566 :type 'boolean)
2568 (defcustom org-use-tag-inheritance t
2569 "Non-nil means tags in levels apply also for sublevels.
2570 When nil, only the tags directly given in a specific line apply there.
2571 This may also be a list of tags that should be inherited, or a regexp that
2572 matches tags that should be inherited. Additional control is possible
2573 with the variable `org-tags-exclude-from-inheritance' which gives an
2574 explicit list of tags to be excluded from inheritance., even if the value of
2575 `org-use-tag-inheritance' would select it for inheritance.
2577 If this option is t, a match early-on in a tree can lead to a large
2578 number of matches in the subtree when constructing the agenda or creating
2579 a sparse tree. If you only want to see the first match in a tree during
2580 a search, check out the variable `org-tags-match-list-sublevels'."
2581 :group 'org-tags
2582 :type '(choice
2583 (const :tag "Not" nil)
2584 (const :tag "Always" t)
2585 (repeat :tag "Specific tags" (string :tag "Tag"))
2586 (regexp :tag "Tags matched by regexp")))
2588 (defcustom org-tags-exclude-from-inheritance nil
2589 "List of tags that should never be inherited.
2590 This is a way to exclude a few tags from inheritance. For way to do
2591 the opposite, to actively allow inheritance for selected tags,
2592 see the variable `org-use-tag-inheritance'."
2593 :group 'org-tags
2594 :type '(repeat (string :tag "Tag")))
2596 (defun org-tag-inherit-p (tag)
2597 "Check if TAG is one that should be inherited."
2598 (cond
2599 ((member tag org-tags-exclude-from-inheritance) nil)
2600 ((eq org-use-tag-inheritance t) t)
2601 ((not org-use-tag-inheritance) nil)
2602 ((stringp org-use-tag-inheritance)
2603 (string-match org-use-tag-inheritance tag))
2604 ((listp org-use-tag-inheritance)
2605 (member tag org-use-tag-inheritance))
2606 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
2608 (defcustom org-tags-match-list-sublevels t
2609 "Non-nil means list also sublevels of headlines matching a search.
2610 This variable applies to tags/property searches, and also to stuck
2611 projects because this search is based on a tags match as well.
2613 When set to the symbol `indented', sublevels are indented with
2614 leading dots.
2616 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2617 the sublevels of a headline matching a tag search often also match
2618 the same search. Listing all of them can create very long lists.
2619 Setting this variable to nil causes subtrees of a match to be skipped.
2621 This variable is semi-obsolete and probably should always be true. It
2622 is better to limit inheritance to certain tags using the variables
2623 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
2624 :group 'org-tags
2625 :type '(choice
2626 (const :tag "No, don't list them" nil)
2627 (const :tag "Yes, do list them" t)
2628 (const :tag "List them, indented with leading dots" indented)))
2630 (defcustom org-tags-sort-function nil
2631 "When set, tags are sorted using this function as a comparator"
2632 :group 'org-tags
2633 :type '(choice
2634 (const :tag "No sorting" nil)
2635 (const :tag "Alphabetical" string<)
2636 (const :tag "Reverse alphabetical" string>)
2637 (function :tag "Custom function" nil)))
2639 (defvar org-tags-history nil
2640 "History of minibuffer reads for tags.")
2641 (defvar org-last-tags-completion-table nil
2642 "The last used completion table for tags.")
2643 (defvar org-after-tags-change-hook nil
2644 "Hook that is run after the tags in a line have changed.")
2646 (defgroup org-properties nil
2647 "Options concerning properties in Org-mode."
2648 :tag "Org Properties"
2649 :group 'org)
2651 (defcustom org-property-format "%-10s %s"
2652 "How property key/value pairs should be formatted by `indent-line'.
2653 When `indent-line' hits a property definition, it will format the line
2654 according to this format, mainly to make sure that the values are
2655 lined-up with respect to each other."
2656 :group 'org-properties
2657 :type 'string)
2659 (defcustom org-use-property-inheritance nil
2660 "Non-nil means properties apply also for sublevels.
2662 This setting is chiefly used during property searches. Turning it on can
2663 cause significant overhead when doing a search, which is why it is not
2664 on by default.
2666 When nil, only the properties directly given in the current entry count.
2667 When t, every property is inherited. The value may also be a list of
2668 properties that should have inheritance, or a regular expression matching
2669 properties that should be inherited.
2671 However, note that some special properties use inheritance under special
2672 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2673 and the properties ending in \"_ALL\" when they are used as descriptor
2674 for valid values of a property.
2676 Note for programmers:
2677 When querying an entry with `org-entry-get', you can control if inheritance
2678 should be used. By default, `org-entry-get' looks only at the local
2679 properties. You can request inheritance by setting the inherit argument
2680 to t (to force inheritance) or to `selective' (to respect the setting
2681 in this variable)."
2682 :group 'org-properties
2683 :type '(choice
2684 (const :tag "Not" nil)
2685 (const :tag "Always" t)
2686 (repeat :tag "Specific properties" (string :tag "Property"))
2687 (regexp :tag "Properties matched by regexp")))
2689 (defun org-property-inherit-p (property)
2690 "Check if PROPERTY is one that should be inherited."
2691 (cond
2692 ((eq org-use-property-inheritance t) t)
2693 ((not org-use-property-inheritance) nil)
2694 ((stringp org-use-property-inheritance)
2695 (string-match org-use-property-inheritance property))
2696 ((listp org-use-property-inheritance)
2697 (member property org-use-property-inheritance))
2698 (t (error "Invalid setting of `org-use-property-inheritance'"))))
2700 (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
2701 "The default column format, if no other format has been defined.
2702 This variable can be set on the per-file basis by inserting a line
2704 #+COLUMNS: %25ITEM ....."
2705 :group 'org-properties
2706 :type 'string)
2708 (defcustom org-columns-ellipses ".."
2709 "The ellipses to be used when a field in column view is truncated.
2710 When this is the empty string, as many characters as possible are shown,
2711 but then there will be no visual indication that the field has been truncated.
2712 When this is a string of length N, the last N characters of a truncated
2713 field are replaced by this string. If the column is narrower than the
2714 ellipses string, only part of the ellipses string will be shown."
2715 :group 'org-properties
2716 :type 'string)
2718 (defcustom org-columns-modify-value-for-display-function nil
2719 "Function that modifies values for display in column view.
2720 For example, it can be used to cut out a certain part from a time stamp.
2721 The function must take 2 arguments:
2723 column-title The title of the column (*not* the property name)
2724 value The value that should be modified.
2726 The function should return the value that should be displayed,
2727 or nil if the normal value should be used."
2728 :group 'org-properties
2729 :type 'function)
2731 (defcustom org-effort-property "Effort"
2732 "The property that is being used to keep track of effort estimates.
2733 Effort estimates given in this property need to have the format H:MM."
2734 :group 'org-properties
2735 :group 'org-progress
2736 :type '(string :tag "Property"))
2738 (defconst org-global-properties-fixed
2739 '(("VISIBILITY_ALL" . "folded children content all")
2740 ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
2741 "List of property/value pairs that can be inherited by any entry.
2743 These are fixed values, for the preset properties. The user variable
2744 that can be used to add to this list is `org-global-properties'.
2746 The entries in this list are cons cells where the car is a property
2747 name and cdr is a string with the value. If the value represents
2748 multiple items like an \"_ALL\" property, separate the items by
2749 spaces.")
2751 (defcustom org-global-properties nil
2752 "List of property/value pairs that can be inherited by any entry.
2754 This list will be combined with the constant `org-global-properties-fixed'.
2756 The entries in this list are cons cells where the car is a property
2757 name and cdr is a string with the value.
2759 You can set buffer-local values for the same purpose in the variable
2760 `org-file-properties' this by adding lines like
2762 #+PROPERTY: NAME VALUE"
2763 :group 'org-properties
2764 :type '(repeat
2765 (cons (string :tag "Property")
2766 (string :tag "Value"))))
2768 (defvar org-file-properties nil
2769 "List of property/value pairs that can be inherited by any entry.
2770 Valid for the current buffer.
2771 This variable is populated from #+PROPERTY lines.")
2772 (make-variable-buffer-local 'org-file-properties)
2774 (defgroup org-agenda nil
2775 "Options concerning agenda views in Org-mode."
2776 :tag "Org Agenda"
2777 :group 'org)
2779 (defvar org-category nil
2780 "Variable used by org files to set a category for agenda display.
2781 Such files should use a file variable to set it, for example
2783 # -*- mode: org; org-category: \"ELisp\"
2785 or contain a special line
2787 #+CATEGORY: ELisp
2789 If the file does not specify a category, then file's base name
2790 is used instead.")
2791 (make-variable-buffer-local 'org-category)
2792 (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
2794 (defcustom org-agenda-files nil
2795 "The files to be used for agenda display.
2796 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2797 \\[org-remove-file]. You can also use customize to edit the list.
2799 If an entry is a directory, all files in that directory that are matched by
2800 `org-agenda-file-regexp' will be part of the file list.
2802 If the value of the variable is not a list but a single file name, then
2803 the list of agenda files is actually stored and maintained in that file, one
2804 agenda file per line. In this file paths can be given relative to
2805 `org-directory'. Tilde expansion and environment variable substitution
2806 are also made."
2807 :group 'org-agenda
2808 :type '(choice
2809 (repeat :tag "List of files and directories" file)
2810 (file :tag "Store list in a file\n" :value "~/.agenda_files")))
2812 (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
2813 "Regular expression to match files for `org-agenda-files'.
2814 If any element in the list in that variable contains a directory instead
2815 of a normal file, all files in that directory that are matched by this
2816 regular expression will be included."
2817 :group 'org-agenda
2818 :type 'regexp)
2820 (defcustom org-agenda-text-search-extra-files nil
2821 "List of extra files to be searched by text search commands.
2822 These files will be search in addition to the agenda files by the
2823 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
2824 Note that these files will only be searched for text search commands,
2825 not for the other agenda views like todo lists, tag searches or the weekly
2826 agenda. This variable is intended to list notes and possibly archive files
2827 that should also be searched by these two commands.
2828 In fact, if the first element in the list is the symbol `agenda-archives',
2829 than all archive files of all agenda files will be added to the search
2830 scope."
2831 :group 'org-agenda
2832 :type '(set :greedy t
2833 (const :tag "Agenda Archives" agenda-archives)
2834 (repeat :inline t (file))))
2836 (if (fboundp 'defvaralias)
2837 (defvaralias 'org-agenda-multi-occur-extra-files
2838 'org-agenda-text-search-extra-files))
2840 (defcustom org-agenda-skip-unavailable-files nil
2841 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
2842 A nil value means to remove them, after a query, from the list."
2843 :group 'org-agenda
2844 :type 'boolean)
2846 (defcustom org-calendar-to-agenda-key [?c]
2847 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2848 The command `org-calendar-goto-agenda' will be bound to this key. The
2849 default is the character `c' because then `c' can be used to switch back and
2850 forth between agenda and calendar."
2851 :group 'org-agenda
2852 :type 'sexp)
2854 (defcustom org-calendar-agenda-action-key [?k]
2855 "The key to be installed in `calendar-mode-map' for agenda-action.
2856 The command `org-agenda-action' will be bound to this key. The
2857 default is the character `k' because we use the same key in the agenda."
2858 :group 'org-agenda
2859 :type 'sexp)
2861 (defcustom org-calendar-insert-diary-entry-key [?i]
2862 "The key to be installed in `calendar-mode-map' for adding diary entries.
2863 This option is irrelevant until `org-agenda-diary-file' has been configured
2864 to point to an Org-mode file. When that is the case, the command
2865 `org-agenda-diary-entry' will be bound to the key given here, by default
2866 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
2867 if you want to continue doing this, you need to change this to a different
2868 key."
2869 :group 'org-agenda
2870 :type 'sexp)
2872 (defcustom org-agenda-diary-file 'diary-file
2873 "File to which to add new entries with the `i' key in agenda and calendar.
2874 When this is the symbol `diary-file', the functionality in the Emacs
2875 calendar will be used to add entries to the `diary-file'. But when this
2876 points to a file, `org-agenda-diary-entry' will be used instead."
2877 :group 'org-agenda
2878 :type '(choice
2879 (const :tag "The standard Emacs diary file" diary-file)
2880 (file :tag "Special Org file diary entries")))
2882 (eval-after-load "calendar"
2883 '(progn
2884 (org-defkey calendar-mode-map org-calendar-to-agenda-key
2885 'org-calendar-goto-agenda)
2886 (org-defkey calendar-mode-map org-calendar-agenda-action-key
2887 'org-agenda-action)
2888 (add-hook 'calendar-mode-hook
2889 (lambda ()
2890 (unless (eq org-agenda-diary-file 'diary-file)
2891 (define-key calendar-mode-map
2892 org-calendar-insert-diary-entry-key
2893 'org-agenda-diary-entry))))))
2895 (defgroup org-latex nil
2896 "Options for embedding LaTeX code into Org-mode."
2897 :tag "Org LaTeX"
2898 :group 'org)
2900 (defcustom org-format-latex-options
2901 '(:foreground default :background default :scale 1.0
2902 :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
2903 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
2904 "Options for creating images from LaTeX fragments.
2905 This is a property list with the following properties:
2906 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
2907 `default' means use the foreground of the default face.
2908 :background the background color, or \"Transparent\".
2909 `default' means use the background of the default face.
2910 :scale a scaling factor for the size of the images.
2911 :html-foreground, :html-background, :html-scale
2912 the same numbers for HTML export.
2913 :matchers a list indicating which matchers should be used to
2914 find LaTeX fragments. Valid members of this list are:
2915 \"begin\" find environments
2916 \"$1\" find single characters surrounded by $.$
2917 \"$\" find math expressions surrounded by $...$
2918 \"$$\" find math expressions surrounded by $$....$$
2919 \"\\(\" find math expressions surrounded by \\(...\\)
2920 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2921 :group 'org-latex
2922 :type 'plist)
2924 (defcustom org-format-latex-signal-error t
2925 "Non-nil means signal an error when image creation of LaTeX snippets fails.
2926 When nil, just push out a message."
2927 :group 'org-latex
2928 :type 'boolean)
2930 (defcustom org-format-latex-header "\\documentclass{article}
2931 \\usepackage[usenames]{color}
2932 \\usepackage{amsmath}
2933 \\usepackage[mathscr]{eucal}
2934 \\pagestyle{empty} % do not remove
2935 \[PACKAGES]
2936 \[DEFAULT-PACKAGES]
2937 % The settings below are copied from fullpage.sty
2938 \\setlength{\\textwidth}{\\paperwidth}
2939 \\addtolength{\\textwidth}{-3cm}
2940 \\setlength{\\oddsidemargin}{1.5cm}
2941 \\addtolength{\\oddsidemargin}{-2.54cm}
2942 \\setlength{\\evensidemargin}{\\oddsidemargin}
2943 \\setlength{\\textheight}{\\paperheight}
2944 \\addtolength{\\textheight}{-\\headheight}
2945 \\addtolength{\\textheight}{-\\headsep}
2946 \\addtolength{\\textheight}{-\\footskip}
2947 \\addtolength{\\textheight}{-3cm}
2948 \\setlength{\\topmargin}{1.5cm}
2949 \\addtolength{\\topmargin}{-2.54cm}"
2950 "The document header used for processing LaTeX fragments.
2951 It is imperative that this header make sure that no page number
2952 appears on the page. The package defined in the variables
2953 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
2954 will either replace the placeholder \"[PACKAGES]\" in this header, or they
2955 will be appended."
2956 :group 'org-latex
2957 :type 'string)
2959 (defvar org-format-latex-header-extra nil)
2961 ;; The following variables are defined here because is it also used
2962 ;; when formatting latex fragments. Originally it was part of the
2963 ;; LaTeX exporter, which is why the name includes "export".
2964 (defcustom org-export-latex-default-packages-alist
2965 '(("AUTO" "inputenc")
2966 ("T1" "fontenc")
2967 ("" "fixltx2e")
2968 ("" "graphicx")
2969 ("" "longtable")
2970 ("" "float")
2971 ("" "wrapfig")
2972 ("" "soul")
2973 ("" "t1enc")
2974 ("" "textcomp")
2975 ("" "marvosym")
2976 ("" "wasysym")
2977 ("" "latexsym")
2978 ("" "amssymb")
2979 ("" "hyperref")
2980 "\\tolerance=1000"
2982 "Alist of default packages to be inserted in the header.
2983 Change this only if one of the packages here causes an incompatibility
2984 with another package you are using.
2985 The packages in this list are needed by one part or another of Org-mode
2986 to function properly.
2988 - inputenc, fontenc, t1enc: for basic font and character selection
2989 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
2990 for interpreting the entities in `org-entities'. You can skip some of these
2991 packages if you don't use any of the symbols in it.
2992 - graphicx: for including images
2993 - float, wrapfig: for figure placement
2994 - longtable: for long tables
2995 - hyperref: for cross references
2997 Therefore you should not modify this variable unless you know what you
2998 are doing. The one reason to change it anyway is that you might be loading
2999 some other package that conflicts with one of the default packages.
3000 Each cell is of the format \( \"options\" \"package\" \)."
3001 :group 'org-export-latex
3002 :type '(repeat
3003 (choice
3004 (string :tag "A line of LaTeX")
3005 (list :tag "options/package pair"
3006 (string :tag "options")
3007 (string :tag "package")))))
3009 (defcustom org-export-latex-packages-alist nil
3010 "Alist of packages to be inserted in every LaTeX the header.
3011 These will be inserted after `org-export-latex-default-packages-alist'.
3012 Each cell is of the format \( \"options\" \"package\" \).
3013 Make sure that you only lis packages here which:
3014 - you want in every file
3015 - do not conflict with the default packages in
3016 `org-export-latex-default-packages-alist'
3017 - do not conflict with the setup in `org-format-latex-header'."
3018 :group 'org-export-latex
3019 :type '(repeat
3020 (choice
3021 (string :tag "A line of LaTeX")
3022 (list :tag "options/package pair"
3023 (string :tag "options")
3024 (string :tag "package")))))
3026 (defgroup org-appearance nil
3027 "Settings for Org-mode appearance."
3028 :tag "Org Appearance"
3029 :group 'org)
3031 (defcustom org-level-color-stars-only nil
3032 "Non-nil means fontify only the stars in each headline.
3033 When nil, the entire headline is fontified.
3034 Changing it requires restart of `font-lock-mode' to become effective
3035 also in regions already fontified."
3036 :group 'org-appearance
3037 :type 'boolean)
3039 (defcustom org-hide-leading-stars nil
3040 "Non-nil means hide the first N-1 stars in a headline.
3041 This works by using the face `org-hide' for these stars. This
3042 face is white for a light background, and black for a dark
3043 background. You may have to customize the face `org-hide' to
3044 make this work.
3045 Changing it requires restart of `font-lock-mode' to become effective
3046 also in regions already fontified.
3047 You may also set this on a per-file basis by adding one of the following
3048 lines to the buffer:
3050 #+STARTUP: hidestars
3051 #+STARTUP: showstars"
3052 :group 'org-appearance
3053 :type 'boolean)
3055 (defcustom org-hidden-keywords nil
3056 "List of keywords that should be hidden when typed in the org buffer.
3057 For example, add #+TITLE to this list in order to make the
3058 document title appear in the buffer without the initial #+TITLE:
3059 keyword."
3060 :group 'org-appearance
3061 :type '(set (const :tag "#+AUTHOR" author)
3062 (const :tag "#+DATE" date)
3063 (const :tag "#+EMAIL" email)
3064 (const :tag "#+TITLE" title)))
3066 (defcustom org-fontify-done-headline nil
3067 "Non-nil means change the face of a headline if it is marked DONE.
3068 Normally, only the TODO/DONE keyword indicates the state of a headline.
3069 When this is non-nil, the headline after the keyword is set to the
3070 `org-headline-done' as an additional indication."
3071 :group 'org-appearance
3072 :type 'boolean)
3074 (defcustom org-fontify-emphasized-text t
3075 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3076 Changing this variable requires a restart of Emacs to take effect."
3077 :group 'org-appearance
3078 :type 'boolean)
3080 (defcustom org-fontify-whole-heading-line nil
3081 "Non-nil means fontify the whole line for headings.
3082 This is useful when setting a background color for the
3083 org-level-* faces."
3084 :group 'org-appearance
3085 :type 'boolean)
3087 (defcustom org-highlight-latex-fragments-and-specials nil
3088 "Non-nil means fontify what is treated specially by the exporters."
3089 :group 'org-appearance
3090 :type 'boolean)
3092 (defcustom org-hide-emphasis-markers nil
3093 "Non-nil mean font-lock should hide the emphasis marker characters."
3094 :group 'org-appearance
3095 :type 'boolean)
3097 (defvar org-emph-re nil
3098 "Regular expression for matching emphasis.")
3099 (defvar org-verbatim-re nil
3100 "Regular expression for matching verbatim text.")
3101 (defvar org-emphasis-regexp-components) ; defined just below
3102 (defvar org-emphasis-alist) ; defined just below
3103 (defun org-set-emph-re (var val)
3104 "Set variable and compute the emphasis regular expression."
3105 (set var val)
3106 (when (and (boundp 'org-emphasis-alist)
3107 (boundp 'org-emphasis-regexp-components)
3108 org-emphasis-alist org-emphasis-regexp-components)
3109 (let* ((e org-emphasis-regexp-components)
3110 (pre (car e))
3111 (post (nth 1 e))
3112 (border (nth 2 e))
3113 (body (nth 3 e))
3114 (nl (nth 4 e))
3115 (body1 (concat body "*?"))
3116 (markers (mapconcat 'car org-emphasis-alist ""))
3117 (vmarkers (mapconcat
3118 (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
3119 org-emphasis-alist "")))
3120 ;; make sure special characters appear at the right position in the class
3121 (if (string-match "\\^" markers)
3122 (setq markers (concat (replace-match "" t t markers) "^")))
3123 (if (string-match "-" markers)
3124 (setq markers (concat (replace-match "" t t markers) "-")))
3125 (if (string-match "\\^" vmarkers)
3126 (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
3127 (if (string-match "-" vmarkers)
3128 (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
3129 (if (> nl 0)
3130 (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
3131 (int-to-string nl) "\\}")))
3132 ;; Make the regexp
3133 (setq org-emph-re
3134 (concat "\\([" pre "]\\|^\\)"
3135 "\\("
3136 "\\([" markers "]\\)"
3137 "\\("
3138 "[^" border "]\\|"
3139 "[^" border "]"
3140 body1
3141 "[^" border "]"
3142 "\\)"
3143 "\\3\\)"
3144 "\\([" post "]\\|$\\)"))
3145 (setq org-verbatim-re
3146 (concat "\\([" pre "]\\|^\\)"
3147 "\\("
3148 "\\([" vmarkers "]\\)"
3149 "\\("
3150 "[^" border "]\\|"
3151 "[^" border "]"
3152 body1
3153 "[^" border "]"
3154 "\\)"
3155 "\\3\\)"
3156 "\\([" post "]\\|$\\)")))))
3158 (defcustom org-emphasis-regexp-components
3159 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3160 "Components used to build the regular expression for emphasis.
3161 This is a list with 6 entries. Terminology: In an emphasis string
3162 like \" *strong word* \", we call the initial space PREMATCH, the final
3163 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3164 and \"trong wor\" is the body. The different components in this variable
3165 specify what is allowed/forbidden in each part:
3167 pre Chars allowed as prematch. Beginning of line will be allowed too.
3168 post Chars allowed as postmatch. End of line will be allowed too.
3169 border The chars *forbidden* as border characters.
3170 body-regexp A regexp like \".\" to match a body character. Don't use
3171 non-shy groups here, and don't allow newline here.
3172 newline The maximum number of newlines allowed in an emphasis exp.
3174 Use customize to modify this, or restart Emacs after changing it."
3175 :group 'org-appearance
3176 :set 'org-set-emph-re
3177 :type '(list
3178 (sexp :tag "Allowed chars in pre ")
3179 (sexp :tag "Allowed chars in post ")
3180 (sexp :tag "Forbidden chars in border ")
3181 (sexp :tag "Regexp for body ")
3182 (integer :tag "number of newlines allowed")
3183 (option (boolean :tag "Please ignore this button"))))
3185 (defcustom org-emphasis-alist
3186 `(("*" bold "<b>" "</b>")
3187 ("/" italic "<i>" "</i>")
3188 ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
3189 ("=" org-code "<code>" "</code>" verbatim)
3190 ("~" org-verbatim "<code>" "</code>" verbatim)
3191 ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
3192 "<del>" "</del>")
3194 "Special syntax for emphasized text.
3195 Text starting and ending with a special character will be emphasized, for
3196 example *bold*, _underlined_ and /italic/. This variable sets the marker
3197 characters, the face to be used by font-lock for highlighting in Org-mode
3198 Emacs buffers, and the HTML tags to be used for this.
3199 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3200 Use customize to modify this, or restart Emacs after changing it."
3201 :group 'org-appearance
3202 :set 'org-set-emph-re
3203 :type '(repeat
3204 (list
3205 (string :tag "Marker character")
3206 (choice
3207 (face :tag "Font-lock-face")
3208 (plist :tag "Face property list"))
3209 (string :tag "HTML start tag")
3210 (string :tag "HTML end tag")
3211 (option (const verbatim)))))
3213 (defvar org-protecting-blocks
3214 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3215 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3216 This is needed for font-lock setup.")
3218 ;;; Miscellaneous options
3220 (defgroup org-completion nil
3221 "Completion in Org-mode."
3222 :tag "Org Completion"
3223 :group 'org)
3225 (defcustom org-completion-use-ido nil
3226 "Non-nil means use ido completion wherever possible.
3227 Note that `ido-mode' must be active for this variable to be relevant.
3228 If you decide to turn this variable on, you might well want to turn off
3229 `org-outline-path-complete-in-steps'.
3230 See also `org-completion-use-iswitchb'."
3231 :group 'org-completion
3232 :type 'boolean)
3234 (defcustom org-completion-use-iswitchb nil
3235 "Non-nil means use iswitchb completion wherever possible.
3236 Note that `iswitchb-mode' must be active for this variable to be relevant.
3237 If you decide to turn this variable on, you might well want to turn off
3238 `org-outline-path-complete-in-steps'.
3239 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3240 :group 'org-completion
3241 :type 'boolean)
3243 (defcustom org-completion-fallback-command 'hippie-expand
3244 "The expansion command called by \\[org-complete] in normal context.
3245 Normal means no org-mode-specific context."
3246 :group 'org-completion
3247 :type 'function)
3249 ;;; Functions and variables from their packages
3250 ;; Declared here to avoid compiler warnings
3252 ;; XEmacs only
3253 (defvar outline-mode-menu-heading)
3254 (defvar outline-mode-menu-show)
3255 (defvar outline-mode-menu-hide)
3256 (defvar zmacs-regions) ; XEmacs regions
3258 ;; Emacs only
3259 (defvar mark-active)
3261 ;; Various packages
3262 (declare-function calendar-absolute-from-iso "cal-iso" (date))
3263 (declare-function calendar-forward-day "cal-move" (arg))
3264 (declare-function calendar-goto-date "cal-move" (date))
3265 (declare-function calendar-goto-today "cal-move" ())
3266 (declare-function calendar-iso-from-absolute "cal-iso" (date))
3267 (defvar calc-embedded-close-formula)
3268 (defvar calc-embedded-open-formula)
3269 (declare-function cdlatex-tab "ext:cdlatex" ())
3270 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
3271 (defvar font-lock-unfontify-region-function)
3272 (declare-function iswitchb-read-buffer "iswitchb"
3273 (prompt &optional default require-match start matches-set))
3274 (defvar iswitchb-temp-buflist)
3275 (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
3276 (defvar org-agenda-tags-todo-honor-ignore-options)
3277 (declare-function org-agenda-skip "org-agenda" ())
3278 (declare-function
3279 org-format-agenda-item "org-agenda"
3280 (extra txt &optional category tags dotime noprefix remove-re habitp))
3281 (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
3282 (declare-function org-agenda-change-all-lines "org-agenda"
3283 (newhead hdmarker &optional fixface just-this))
3284 (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
3285 (declare-function org-agenda-maybe-redo "org-agenda" ())
3286 (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
3287 (beg end))
3288 (declare-function org-agenda-copy-local-variable "org-agenda" (var))
3289 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3290 "org-agenda" (&optional end))
3291 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
3292 (declare-function org-indent-mode "org-indent" (&optional arg))
3293 (declare-function parse-time-string "parse-time" (string))
3294 (declare-function org-attach-reveal "org-attach" (&optional if-exists))
3295 (declare-function org-export-latex-fix-inputenc "org-latex" ())
3296 (defvar remember-data-file)
3297 (defvar texmathp-why)
3298 (declare-function speedbar-line-directory "speedbar" (&optional depth))
3299 (declare-function table--at-cell-p "table" (position &optional object at-column))
3301 (defvar w3m-current-url)
3302 (defvar w3m-current-title)
3304 (defvar org-latex-regexps)
3306 ;;; Autoload and prepare some org modules
3308 ;; Some table stuff that needs to be defined here, because it is used
3309 ;; by the functions setting up org-mode or checking for table context.
3311 (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
3312 "Detects an org-type or table-type table.")
3313 (defconst org-table-line-regexp "^[ \t]*|"
3314 "Detects an org-type table line.")
3315 (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
3316 "Detects an org-type table line.")
3317 (defconst org-table-hline-regexp "^[ \t]*|-"
3318 "Detects an org-type table hline.")
3319 (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
3320 "Detects a table-type table hline.")
3321 (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
3322 "Searching from within a table (any type) this finds the first line
3323 outside the table.")
3325 ;; Autoload the functions in org-table.el that are needed by functions here.
3327 (eval-and-compile
3328 (org-autoload "org-table"
3329 '(org-table-align org-table-begin org-table-blank-field
3330 org-table-convert org-table-convert-region org-table-copy-down
3331 org-table-copy-region org-table-create
3332 org-table-create-or-convert-from-region
3333 org-table-create-with-table.el org-table-current-dline
3334 org-table-cut-region org-table-delete-column org-table-edit-field
3335 org-table-edit-formulas org-table-end org-table-eval-formula
3336 org-table-export org-table-field-info
3337 org-table-get-stored-formulas org-table-goto-column
3338 org-table-hline-and-move org-table-import org-table-insert-column
3339 org-table-insert-hline org-table-insert-row org-table-iterate
3340 org-table-justify-field-maybe org-table-kill-row
3341 org-table-maybe-eval-formula org-table-maybe-recalculate-line
3342 org-table-move-column org-table-move-column-left
3343 org-table-move-column-right org-table-move-row
3344 org-table-move-row-down org-table-move-row-up
3345 org-table-next-field org-table-next-row org-table-paste-rectangle
3346 org-table-previous-field org-table-recalculate
3347 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
3348 org-table-toggle-coordinate-overlays
3349 org-table-toggle-formula-debugger org-table-wrap-region
3350 orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
3352 (defun org-at-table-p (&optional table-type)
3353 "Return t if the cursor is inside an org-type table.
3354 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3355 (if org-enable-table-editor
3356 (save-excursion
3357 (beginning-of-line 1)
3358 (looking-at (if table-type org-table-any-line-regexp
3359 org-table-line-regexp)))
3360 nil))
3361 (defsubst org-table-p () (org-at-table-p))
3363 (defun org-at-table.el-p ()
3364 "Return t if and only if we are at a table.el table."
3365 (and (org-at-table-p 'any)
3366 (save-excursion
3367 (goto-char (org-table-begin 'any))
3368 (looking-at org-table1-hline-regexp))))
3369 (defun org-table-recognize-table.el ()
3370 "If there is a table.el table nearby, recognize it and move into it."
3371 (if org-table-tab-recognizes-table.el
3372 (if (org-at-table.el-p)
3373 (progn
3374 (beginning-of-line 1)
3375 (if (looking-at org-table-dataline-regexp)
3377 (if (looking-at org-table1-hline-regexp)
3378 (progn
3379 (beginning-of-line 2)
3380 (if (looking-at org-table-any-border-regexp)
3381 (beginning-of-line -1)))))
3382 (if (re-search-forward "|" (org-table-end t) t)
3383 (progn
3384 (require 'table)
3385 (if (table--at-cell-p (point))
3387 (message "recognizing table.el table...")
3388 (table-recognize-table)
3389 (message "recognizing table.el table...done")))
3390 (error "This should not happen..."))
3392 nil)
3393 nil))
3395 (defun org-at-table-hline-p ()
3396 "Return t if the cursor is inside a hline in a table."
3397 (if org-enable-table-editor
3398 (save-excursion
3399 (beginning-of-line 1)
3400 (looking-at org-table-hline-regexp))
3401 nil))
3403 (defvar org-table-clean-did-remove-column nil)
3405 (defun org-table-map-tables (function)
3406 "Apply FUNCTION to the start of all tables in the buffer."
3407 (save-excursion
3408 (save-restriction
3409 (widen)
3410 (goto-char (point-min))
3411 (while (re-search-forward org-table-any-line-regexp nil t)
3412 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
3413 (beginning-of-line 1)
3414 (when (looking-at org-table-line-regexp)
3415 (save-excursion (funcall function))
3416 (or (looking-at org-table-line-regexp)
3417 (forward-char 1)))
3418 (re-search-forward org-table-any-border-regexp nil 1))))
3419 (message "Mapping tables: done"))
3421 ;; Declare and autoload functions from org-exp.el & Co
3423 (declare-function org-default-export-plist "org-exp")
3424 (declare-function org-infile-export-plist "org-exp")
3425 (declare-function org-get-current-options "org-exp")
3426 (eval-and-compile
3427 (org-autoload "org-exp"
3428 '(org-export org-export-visible
3429 org-insert-export-options-template
3430 org-table-clean-before-export))
3431 (org-autoload "org-ascii"
3432 '(org-export-as-ascii org-export-ascii-preprocess
3433 org-export-as-ascii-to-buffer org-replace-region-by-ascii
3434 org-export-region-as-ascii))
3435 (org-autoload "org-latex"
3436 '(org-export-as-latex-batch org-export-as-latex-to-buffer
3437 org-replace-region-by-latex org-export-region-as-latex
3438 org-export-as-latex org-export-as-pdf
3439 org-export-as-pdf-and-open))
3440 (org-autoload "org-html"
3441 '(org-export-as-html-and-open
3442 org-export-as-html-batch org-export-as-html-to-buffer
3443 org-replace-region-by-html org-export-region-as-html
3444 org-export-as-html))
3445 (org-autoload "org-docbook"
3446 '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
3447 org-replace-region-by-docbook org-export-region-as-docbook
3448 org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
3449 org-export-as-docbook))
3450 (org-autoload "org-icalendar"
3451 '(org-export-icalendar-this-file
3452 org-export-icalendar-all-agenda-files
3453 org-export-icalendar-combine-agenda-files))
3454 (org-autoload "org-xoxo" '(org-export-as-xoxo))
3455 (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
3457 ;; Declare and autoload functions from org-agenda.el
3459 (eval-and-compile
3460 (org-autoload "org-agenda"
3461 '(org-agenda org-agenda-list org-search-view
3462 org-todo-list org-tags-view org-agenda-list-stuck-projects
3463 org-diary org-agenda-to-appt
3464 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
3466 ;; Autoload org-remember
3468 (eval-and-compile
3469 (org-autoload "org-remember"
3470 '(org-remember-insinuate org-remember-annotation
3471 org-remember-apply-template org-remember org-remember-handler)))
3473 ;; Autoload org-clock.el
3476 (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
3477 (beg end))
3478 (declare-function org-clock-update-mode-line "org-clock" ())
3479 (declare-function org-resolve-clocks "org-clock"
3480 (&optional also-non-dangling-p prompt last-valid))
3481 (defvar org-clock-start-time)
3482 (defvar org-clock-marker (make-marker)
3483 "Marker recording the last clock-in.")
3484 (defvar org-clock-hd-marker (make-marker)
3485 "Marker recording the last clock-in, but the headline position.")
3486 (defvar org-clock-heading ""
3487 "The heading of the current clock entry.")
3488 (defun org-clock-is-active ()
3489 "Return non-nil if clock is currently running.
3490 The return value is actually the clock marker."
3491 (marker-buffer org-clock-marker))
3493 (eval-and-compile
3494 (org-autoload
3495 "org-clock"
3496 '(org-clock-in org-clock-out org-clock-cancel
3497 org-clock-goto org-clock-sum org-clock-display
3498 org-clock-remove-overlays org-clock-report
3499 org-clocktable-shift org-dblock-write:clocktable
3500 org-get-clocktable org-resolve-clocks)))
3502 (defun org-clock-update-time-maybe ()
3503 "If this is a CLOCK line, update it and return t.
3504 Otherwise, return nil."
3505 (interactive)
3506 (save-excursion
3507 (beginning-of-line 1)
3508 (skip-chars-forward " \t")
3509 (when (looking-at org-clock-string)
3510 (let ((re (concat "[ \t]*" org-clock-string
3511 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
3512 "\\([ \t]*=>.*\\)?\\)?"))
3513 ts te h m s neg)
3514 (cond
3515 ((not (looking-at re))
3516 nil)
3517 ((not (match-end 2))
3518 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
3519 (> org-clock-marker (point))
3520 (<= org-clock-marker (point-at-eol)))
3521 ;; The clock is running here
3522 (setq org-clock-start-time
3523 (apply 'encode-time
3524 (org-parse-time-string (match-string 1))))
3525 (org-clock-update-mode-line)))
3527 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
3528 (end-of-line 1)
3529 (setq ts (match-string 1)
3530 te (match-string 3))
3531 (setq s (- (org-float-time
3532 (apply 'encode-time (org-parse-time-string te)))
3533 (org-float-time
3534 (apply 'encode-time (org-parse-time-string ts))))
3535 neg (< s 0)
3536 s (abs s)
3537 h (floor (/ s 3600))
3538 s (- s (* 3600 h))
3539 m (floor (/ s 60))
3540 s (- s (* 60 s)))
3541 (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
3542 t))))))
3544 (defun org-check-running-clock ()
3545 "Check if the current buffer contains the running clock.
3546 If yes, offer to stop it and to save the buffer with the changes."
3547 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
3548 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
3549 (buffer-name))))
3550 (org-clock-out)
3551 (when (y-or-n-p "Save changed buffer?")
3552 (save-buffer))))
3554 (defun org-clocktable-try-shift (dir n)
3555 "Check if this line starts a clock table, if yes, shift the time block."
3556 (when (org-match-line "#\\+BEGIN: clocktable\\>")
3557 (org-clocktable-shift dir n)))
3559 ;; Autoload org-timer.el
3561 (eval-and-compile
3562 (org-autoload
3563 "org-timer"
3564 '(org-timer-start org-timer org-timer-item
3565 org-timer-change-times-in-region
3566 org-timer-set-timer
3567 org-timer-reset-timers
3568 org-timer-show-remaining-time)))
3570 ;; Autoload org-feed.el
3572 (eval-and-compile
3573 (org-autoload
3574 "org-feed"
3575 '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
3578 ;; Autoload org-indent.el
3580 ;; Define the variable already here, to make sure we have it.
3581 (defvar org-indent-mode nil
3582 "Non-nil if Org-Indent mode is enabled.
3583 Use the command `org-indent-mode' to change this variable.")
3585 (eval-and-compile
3586 (org-autoload
3587 "org-indent"
3588 '(org-indent-mode)))
3590 ;; Autoload org-mobile.el
3592 (eval-and-compile
3593 (org-autoload
3594 "org-mobile"
3595 '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
3597 ;; Autoload archiving code
3598 ;; The stuff that is needed for cycling and tags has to be defined here.
3600 (defgroup org-archive nil
3601 "Options concerning archiving in Org-mode."
3602 :tag "Org Archive"
3603 :group 'org-structure)
3605 (defcustom org-archive-location "%s_archive::"
3606 "The location where subtrees should be archived.
3608 The value of this variable is a string, consisting of two parts,
3609 separated by a double-colon. The first part is a filename and
3610 the second part is a headline.
3612 When the filename is omitted, archiving happens in the same file.
3613 %s in the filename will be replaced by the current file
3614 name (without the directory part). Archiving to a different file
3615 is useful to keep archived entries from contributing to the
3616 Org-mode Agenda.
3618 The archived entries will be filed as subtrees of the specified
3619 headline. When the headline is omitted, the subtrees are simply
3620 filed away at the end of the file, as top-level entries. Also in
3621 the heading you can use %s to represent the file name, this can be
3622 useful when using the same archive for a number of different files.
3624 Here are a few examples:
3625 \"%s_archive::\"
3626 If the current file is Projects.org, archive in file
3627 Projects.org_archive, as top-level trees. This is the default.
3629 \"::* Archived Tasks\"
3630 Archive in the current file, under the top-level headline
3631 \"* Archived Tasks\".
3633 \"~/org/archive.org::\"
3634 Archive in file ~/org/archive.org (absolute path), as top-level trees.
3636 \"~/org/archive.org::From %s\"
3637 Archive in file ~/org/archive.org (absolute path), under headlines
3638 \"From FILENAME\" where file name is the current file name.
3640 \"basement::** Finished Tasks\"
3641 Archive in file ./basement (relative path), as level 3 trees
3642 below the level 2 heading \"** Finished Tasks\".
3644 You may set this option on a per-file basis by adding to the buffer a
3645 line like
3647 #+ARCHIVE: basement::** Finished Tasks
3649 You may also define it locally for a subtree by setting an ARCHIVE property
3650 in the entry. If such a property is found in an entry, or anywhere up
3651 the hierarchy, it will be used."
3652 :group 'org-archive
3653 :type 'string)
3655 (defcustom org-archive-tag "ARCHIVE"
3656 "The tag that marks a subtree as archived.
3657 An archived subtree does not open during visibility cycling, and does
3658 not contribute to the agenda listings.
3659 After changing this, font-lock must be restarted in the relevant buffers to
3660 get the proper fontification."
3661 :group 'org-archive
3662 :group 'org-keywords
3663 :type 'string)
3665 (defcustom org-agenda-skip-archived-trees t
3666 "Non-nil means the agenda will skip any items located in archived trees.
3667 An archived tree is a tree marked with the tag ARCHIVE. The use of this
3668 variable is no longer recommended, you should leave it at the value t.
3669 Instead, use the key `v' to cycle the archives-mode in the agenda."
3670 :group 'org-archive
3671 :group 'org-agenda-skip
3672 :type 'boolean)
3674 (defcustom org-columns-skip-archived-trees t
3675 "Non-nil means ignore archived trees when creating column view."
3676 :group 'org-archive
3677 :group 'org-properties
3678 :type 'boolean)
3680 (defcustom org-cycle-open-archived-trees nil
3681 "Non-nil means `org-cycle' will open archived trees.
3682 An archived tree is a tree marked with the tag ARCHIVE.
3683 When nil, archived trees will stay folded. You can still open them with
3684 normal outline commands like `show-all', but not with the cycling commands."
3685 :group 'org-archive
3686 :group 'org-cycle
3687 :type 'boolean)
3689 (defcustom org-sparse-tree-open-archived-trees nil
3690 "Non-nil means sparse tree construction shows matches in archived trees.
3691 When nil, matches in these trees are highlighted, but the trees are kept in
3692 collapsed state."
3693 :group 'org-archive
3694 :group 'org-sparse-trees
3695 :type 'boolean)
3697 (defun org-cycle-hide-archived-subtrees (state)
3698 "Re-hide all archived subtrees after a visibility state change."
3699 (when (and (not org-cycle-open-archived-trees)
3700 (not (memq state '(overview folded))))
3701 (save-excursion
3702 (let* ((globalp (memq state '(contents all)))
3703 (beg (if globalp (point-min) (point)))
3704 (end (if globalp (point-max) (org-end-of-subtree t))))
3705 (org-hide-archived-subtrees beg end)
3706 (goto-char beg)
3707 (if (looking-at (concat ".*:" org-archive-tag ":"))
3708 (message "%s" (substitute-command-keys
3709 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
3711 (defun org-force-cycle-archived ()
3712 "Cycle subtree even if it is archived."
3713 (interactive)
3714 (setq this-command 'org-cycle)
3715 (let ((org-cycle-open-archived-trees t))
3716 (call-interactively 'org-cycle)))
3718 (defun org-hide-archived-subtrees (beg end)
3719 "Re-hide all archived subtrees after a visibility state change."
3720 (save-excursion
3721 (let* ((re (concat ":" org-archive-tag ":")))
3722 (goto-char beg)
3723 (while (re-search-forward re end t)
3724 (when (org-on-heading-p)
3725 (org-flag-subtree t)
3726 (org-end-of-subtree t))))))
3728 (defun org-flag-subtree (flag)
3729 (save-excursion
3730 (org-back-to-heading t)
3731 (outline-end-of-heading)
3732 (outline-flag-region (point)
3733 (progn (org-end-of-subtree t) (point))
3734 flag)))
3736 (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
3738 (eval-and-compile
3739 (org-autoload "org-archive"
3740 '(org-add-archive-files org-archive-subtree
3741 org-archive-to-archive-sibling org-toggle-archive-tag
3742 org-archive-subtree-default
3743 org-archive-subtree-default-with-confirmation)))
3745 ;; Autoload Column View Code
3747 (declare-function org-columns-number-to-string "org-colview")
3748 (declare-function org-columns-get-format-and-top-level "org-colview")
3749 (declare-function org-columns-compute "org-colview")
3751 (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
3752 '(org-columns-number-to-string org-columns-get-format-and-top-level
3753 org-columns-compute org-agenda-columns org-columns-remove-overlays
3754 org-columns org-insert-columns-dblock org-dblock-write:columnview))
3756 ;; Autoload ID code
3758 (declare-function org-id-store-link "org-id")
3759 (declare-function org-id-locations-load "org-id")
3760 (declare-function org-id-locations-save "org-id")
3761 (defvar org-id-track-globally)
3762 (org-autoload "org-id"
3763 '(org-id-get-create org-id-new org-id-copy org-id-get
3764 org-id-get-with-outline-path-completion
3765 org-id-get-with-outline-drilling
3766 org-id-goto org-id-find org-id-store-link))
3768 ;; Autoload Plotting Code
3770 (org-autoload "org-plot"
3771 '(org-plot/gnuplot))
3773 ;;; Variables for pre-computed regular expressions, all buffer local
3775 (defvar org-drawer-regexp nil
3776 "Matches first line of a hidden block.")
3777 (make-variable-buffer-local 'org-drawer-regexp)
3778 (defvar org-todo-regexp nil
3779 "Matches any of the TODO state keywords.")
3780 (make-variable-buffer-local 'org-todo-regexp)
3781 (defvar org-not-done-regexp nil
3782 "Matches any of the TODO state keywords except the last one.")
3783 (make-variable-buffer-local 'org-not-done-regexp)
3784 (defvar org-not-done-heading-regexp nil
3785 "Matches a TODO headline that is not done.")
3786 (make-variable-buffer-local 'org-not-done-regexp)
3787 (defvar org-todo-line-regexp nil
3788 "Matches a headline and puts TODO state into group 2 if present.")
3789 (make-variable-buffer-local 'org-todo-line-regexp)
3790 (defvar org-complex-heading-regexp nil
3791 "Matches a headline and puts everything into groups:
3792 group 1: the stars
3793 group 2: The todo keyword, maybe
3794 group 3: Priority cookie
3795 group 4: True headline
3796 group 5: Tags")
3797 (make-variable-buffer-local 'org-complex-heading-regexp)
3798 (defvar org-complex-heading-regexp-format nil)
3799 (make-variable-buffer-local 'org-complex-heading-regexp-format)
3800 (defvar org-todo-line-tags-regexp nil
3801 "Matches a headline and puts TODO state into group 2 if present.
3802 Also put tags into group 4 if tags are present.")
3803 (make-variable-buffer-local 'org-todo-line-tags-regexp)
3804 (defvar org-nl-done-regexp nil
3805 "Matches newline followed by a headline with the DONE keyword.")
3806 (make-variable-buffer-local 'org-nl-done-regexp)
3807 (defvar org-looking-at-done-regexp nil
3808 "Matches the DONE keyword a point.")
3809 (make-variable-buffer-local 'org-looking-at-done-regexp)
3810 (defvar org-ds-keyword-length 12
3811 "Maximum length of the Deadline and SCHEDULED keywords.")
3812 (make-variable-buffer-local 'org-ds-keyword-length)
3813 (defvar org-deadline-regexp nil
3814 "Matches the DEADLINE keyword.")
3815 (make-variable-buffer-local 'org-deadline-regexp)
3816 (defvar org-deadline-time-regexp nil
3817 "Matches the DEADLINE keyword together with a time stamp.")
3818 (make-variable-buffer-local 'org-deadline-time-regexp)
3819 (defvar org-deadline-line-regexp nil
3820 "Matches the DEADLINE keyword and the rest of the line.")
3821 (make-variable-buffer-local 'org-deadline-line-regexp)
3822 (defvar org-scheduled-regexp nil
3823 "Matches the SCHEDULED keyword.")
3824 (make-variable-buffer-local 'org-scheduled-regexp)
3825 (defvar org-scheduled-time-regexp nil
3826 "Matches the SCHEDULED keyword together with a time stamp.")
3827 (make-variable-buffer-local 'org-scheduled-time-regexp)
3828 (defvar org-closed-time-regexp nil
3829 "Matches the CLOSED keyword together with a time stamp.")
3830 (make-variable-buffer-local 'org-closed-time-regexp)
3832 (defvar org-keyword-time-regexp nil
3833 "Matches any of the 4 keywords, together with the time stamp.")
3834 (make-variable-buffer-local 'org-keyword-time-regexp)
3835 (defvar org-keyword-time-not-clock-regexp nil
3836 "Matches any of the 3 keywords, together with the time stamp.")
3837 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
3838 (defvar org-maybe-keyword-time-regexp nil
3839 "Matches a timestamp, possibly preceeded by a keyword.")
3840 (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
3841 (defvar org-planning-or-clock-line-re nil
3842 "Matches a line with planning or clock info.")
3843 (make-variable-buffer-local 'org-planning-or-clock-line-re)
3844 (defvar org-all-time-keywords nil
3845 "List of time keywords.")
3846 (make-variable-buffer-local 'org-all-time-keywords)
3848 (defconst org-plain-time-of-day-regexp
3849 (concat
3850 "\\(\\<[012]?[0-9]"
3851 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3852 "\\(--?"
3853 "\\(\\<[012]?[0-9]"
3854 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3855 "\\)?")
3856 "Regular expression to match a plain time or time range.
3857 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3858 groups carry important information:
3859 0 the full match
3860 1 the first time, range or not
3861 8 the second time, if it is a range.")
3863 (defconst org-plain-time-extension-regexp
3864 (concat
3865 "\\(\\<[012]?[0-9]"
3866 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3867 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
3868 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
3869 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3870 groups carry important information:
3871 0 the full match
3872 7 hours of duration
3873 9 minutes of duration")
3875 (defconst org-stamp-time-of-day-regexp
3876 (concat
3877 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
3878 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
3879 "\\(--?"
3880 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
3881 "Regular expression to match a timestamp time or time range.
3882 After a match, the following groups carry important information:
3883 0 the full match
3884 1 date plus weekday, for back referencing to make sure both times are on the same day
3885 2 the first time, range or not
3886 4 the second time, if it is a range.")
3888 (defconst org-startup-options
3889 '(("fold" org-startup-folded t)
3890 ("overview" org-startup-folded t)
3891 ("nofold" org-startup-folded nil)
3892 ("showall" org-startup-folded nil)
3893 ("showeverything" org-startup-folded showeverything)
3894 ("content" org-startup-folded content)
3895 ("indent" org-startup-indented t)
3896 ("noindent" org-startup-indented nil)
3897 ("hidestars" org-hide-leading-stars t)
3898 ("showstars" org-hide-leading-stars nil)
3899 ("odd" org-odd-levels-only t)
3900 ("oddeven" org-odd-levels-only nil)
3901 ("align" org-startup-align-all-tables t)
3902 ("noalign" org-startup-align-all-tables nil)
3903 ("customtime" org-display-custom-times t)
3904 ("logdone" org-log-done time)
3905 ("lognotedone" org-log-done note)
3906 ("nologdone" org-log-done nil)
3907 ("lognoteclock-out" org-log-note-clock-out t)
3908 ("nolognoteclock-out" org-log-note-clock-out nil)
3909 ("logrepeat" org-log-repeat state)
3910 ("lognoterepeat" org-log-repeat note)
3911 ("nologrepeat" org-log-repeat nil)
3912 ("logreschedule" org-log-reschedule time)
3913 ("lognotereschedule" org-log-reschedule note)
3914 ("nologreschedule" org-log-reschedule nil)
3915 ("logredeadline" org-log-redeadline time)
3916 ("lognoteredeadline" org-log-redeadline note)
3917 ("nologredeadline" org-log-redeadline nil)
3918 ("logrefile" org-log-refile time)
3919 ("lognoterefile" org-log-refile note)
3920 ("nologrefile" org-log-refile nil)
3921 ("fninline" org-footnote-define-inline t)
3922 ("nofninline" org-footnote-define-inline nil)
3923 ("fnlocal" org-footnote-section nil)
3924 ("fnauto" org-footnote-auto-label t)
3925 ("fnprompt" org-footnote-auto-label nil)
3926 ("fnconfirm" org-footnote-auto-label confirm)
3927 ("fnplain" org-footnote-auto-label plain)
3928 ("fnadjust" org-footnote-auto-adjust t)
3929 ("nofnadjust" org-footnote-auto-adjust nil)
3930 ("constcgs" constants-unit-system cgs)
3931 ("constSI" constants-unit-system SI)
3932 ("noptag" org-tag-persistent-alist nil)
3933 ("hideblocks" org-hide-block-startup t)
3934 ("nohideblocks" org-hide-block-startup nil)
3935 ("beamer" org-startup-with-beamer-mode t))
3936 "Variable associated with STARTUP options for org-mode.
3937 Each element is a list of three items: The startup options as written
3938 in the #+STARTUP line, the corresponding variable, and the value to
3939 set this variable to if the option is found. An optional forth element PUSH
3940 means to push this value onto the list in the variable.")
3942 (defun org-set-regexps-and-options ()
3943 "Precompute regular expressions for current buffer."
3944 (when (org-mode-p)
3945 (org-set-local 'org-todo-kwd-alist nil)
3946 (org-set-local 'org-todo-key-alist nil)
3947 (org-set-local 'org-todo-key-trigger nil)
3948 (org-set-local 'org-todo-keywords-1 nil)
3949 (org-set-local 'org-done-keywords nil)
3950 (org-set-local 'org-todo-heads nil)
3951 (org-set-local 'org-todo-sets nil)
3952 (org-set-local 'org-todo-log-states nil)
3953 (org-set-local 'org-file-properties nil)
3954 (org-set-local 'org-file-tags nil)
3955 (let ((re (org-make-options-regexp
3956 '("CATEGORY" "TODO" "COLUMNS"
3957 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
3958 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
3959 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
3960 (splitre "[ \t]+")
3961 kwds kws0 kwsa key log value cat arch tags const links hw dws
3962 tail sep kws1 prio props ftags drawers beamer-p
3963 ext-setup-or-nil setup-contents (start 0))
3964 (save-excursion
3965 (save-restriction
3966 (widen)
3967 (goto-char (point-min))
3968 (while (or (and ext-setup-or-nil
3969 (string-match re ext-setup-or-nil start)
3970 (setq start (match-end 0)))
3971 (and (setq ext-setup-or-nil nil start 0)
3972 (re-search-forward re nil t)))
3973 (setq key (upcase (match-string 1 ext-setup-or-nil))
3974 value (org-match-string-no-properties 2 ext-setup-or-nil))
3975 (cond
3976 ((equal key "CATEGORY")
3977 (if (string-match "[ \t]+$" value)
3978 (setq value (replace-match "" t t value)))
3979 (setq cat value))
3980 ((member key '("SEQ_TODO" "TODO"))
3981 (push (cons 'sequence (org-split-string value splitre)) kwds))
3982 ((equal key "TYP_TODO")
3983 (push (cons 'type (org-split-string value splitre)) kwds))
3984 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
3985 ;; general TODO-like setup
3986 (push (cons (intern (downcase (match-string 1 key)))
3987 (org-split-string value splitre)) kwds))
3988 ((equal key "TAGS")
3989 (setq tags (append tags (if tags '("\\n") nil)
3990 (org-split-string value splitre))))
3991 ((equal key "COLUMNS")
3992 (org-set-local 'org-columns-default-format value))
3993 ((equal key "LINK")
3994 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
3995 (push (cons (match-string 1 value)
3996 (org-trim (match-string 2 value)))
3997 links)))
3998 ((equal key "PRIORITIES")
3999 (setq prio (org-split-string value " +")))
4000 ((equal key "PROPERTY")
4001 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
4002 (push (cons (match-string 1 value) (match-string 2 value))
4003 props)))
4004 ((equal key "FILETAGS")
4005 (when (string-match "\\S-" value)
4006 (setq ftags
4007 (append
4008 ftags
4009 (apply 'append
4010 (mapcar (lambda (x) (org-split-string x ":"))
4011 (org-split-string value)))))))
4012 ((equal key "DRAWERS")
4013 (setq drawers (org-split-string value splitre)))
4014 ((equal key "CONSTANTS")
4015 (setq const (append const (org-split-string value splitre))))
4016 ((equal key "STARTUP")
4017 (let ((opts (org-split-string value splitre))
4018 l var val)
4019 (while (setq l (pop opts))
4020 (when (setq l (assoc l org-startup-options))
4021 (setq var (nth 1 l) val (nth 2 l))
4022 (if (not (nth 3 l))
4023 (set (make-local-variable var) val)
4024 (if (not (listp (symbol-value var)))
4025 (set (make-local-variable var) nil))
4026 (set (make-local-variable var) (symbol-value var))
4027 (add-to-list var val))))))
4028 ((equal key "ARCHIVE")
4029 (string-match " *$" value)
4030 (setq arch (replace-match "" t t value))
4031 (remove-text-properties 0 (length arch)
4032 '(face t fontified t) arch))
4033 ((equal key "LATEX_CLASS")
4034 (setq beamer-p (equal value "beamer")))
4035 ((equal key "SETUPFILE")
4036 (setq setup-contents (org-file-contents
4037 (expand-file-name
4038 (org-remove-double-quotes value))
4039 'noerror))
4040 (if (not ext-setup-or-nil)
4041 (setq ext-setup-or-nil setup-contents start 0)
4042 (setq ext-setup-or-nil
4043 (concat (substring ext-setup-or-nil 0 start)
4044 "\n" setup-contents "\n"
4045 (substring ext-setup-or-nil start)))))
4046 ))))
4047 (when cat
4048 (org-set-local 'org-category (intern cat))
4049 (push (cons "CATEGORY" cat) props))
4050 (when prio
4051 (if (< (length prio) 3) (setq prio '("A" "C" "B")))
4052 (setq prio (mapcar 'string-to-char prio))
4053 (org-set-local 'org-highest-priority (nth 0 prio))
4054 (org-set-local 'org-lowest-priority (nth 1 prio))
4055 (org-set-local 'org-default-priority (nth 2 prio)))
4056 (and props (org-set-local 'org-file-properties (nreverse props)))
4057 (and ftags (org-set-local 'org-file-tags
4058 (mapcar 'org-add-prop-inherited ftags)))
4059 (and drawers (org-set-local 'org-drawers drawers))
4060 (and arch (org-set-local 'org-archive-location arch))
4061 (and links (setq org-link-abbrev-alist-local (nreverse links)))
4062 ;; Process the TODO keywords
4063 (unless kwds
4064 ;; Use the global values as if they had been given locally.
4065 (setq kwds (default-value 'org-todo-keywords))
4066 (if (stringp (car kwds))
4067 (setq kwds (list (cons org-todo-interpretation
4068 (default-value 'org-todo-keywords)))))
4069 (setq kwds (reverse kwds)))
4070 (setq kwds (nreverse kwds))
4071 (let (inter kws kw)
4072 (while (setq kws (pop kwds))
4073 (let ((kws (or
4074 (run-hook-with-args-until-success
4075 'org-todo-setup-filter-hook kws)
4076 kws)))
4077 (setq inter (pop kws) sep (member "|" kws)
4078 kws0 (delete "|" (copy-sequence kws))
4079 kwsa nil
4080 kws1 (mapcar
4081 (lambda (x)
4082 ;; 1 2
4083 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
4084 (progn
4085 (setq kw (match-string 1 x)
4086 key (and (match-end 2) (match-string 2 x))
4087 log (org-extract-log-state-settings x))
4088 (push (cons kw (and key (string-to-char key))) kwsa)
4089 (and log (push log org-todo-log-states))
4091 (error "Invalid TODO keyword %s" x)))
4092 kws0)
4093 kwsa (if kwsa (append '((:startgroup))
4094 (nreverse kwsa)
4095 '((:endgroup))))
4096 hw (car kws1)
4097 dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
4098 tail (list inter hw (car dws) (org-last dws))))
4099 (add-to-list 'org-todo-heads hw 'append)
4100 (push kws1 org-todo-sets)
4101 (setq org-done-keywords (append org-done-keywords dws nil))
4102 (setq org-todo-key-alist (append org-todo-key-alist kwsa))
4103 (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
4104 (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
4105 (setq org-todo-sets (nreverse org-todo-sets)
4106 org-todo-kwd-alist (nreverse org-todo-kwd-alist)
4107 org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
4108 org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
4109 ;; Process the constants
4110 (when const
4111 (let (e cst)
4112 (while (setq e (pop const))
4113 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
4114 (push (cons (match-string 1 e) (match-string 2 e)) cst)))
4115 (setq org-table-formula-constants-local cst)))
4117 ;; Process the tags.
4118 (when tags
4119 (let (e tgs)
4120 (while (setq e (pop tags))
4121 (cond
4122 ((equal e "{") (push '(:startgroup) tgs))
4123 ((equal e "}") (push '(:endgroup) tgs))
4124 ((equal e "\\n") (push '(:newline) tgs))
4125 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
4126 (push (cons (match-string 1 e)
4127 (string-to-char (match-string 2 e)))
4128 tgs))
4129 (t (push (list e) tgs))))
4130 (org-set-local 'org-tag-alist nil)
4131 (while (setq e (pop tgs))
4132 (or (and (stringp (car e))
4133 (assoc (car e) org-tag-alist))
4134 (push e org-tag-alist)))))
4136 ;; Compute the regular expressions and other local variables
4137 (if (not org-done-keywords)
4138 (setq org-done-keywords (and org-todo-keywords-1
4139 (list (org-last org-todo-keywords-1)))))
4140 (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
4141 (length org-scheduled-string)
4142 (length org-clock-string)
4143 (length org-closed-string)))
4144 org-drawer-regexp
4145 (concat "^[ \t]*:\\("
4146 (mapconcat 'regexp-quote org-drawers "\\|")
4147 "\\):[ \t]*$")
4148 org-not-done-keywords
4149 (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
4150 org-todo-regexp
4151 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
4152 "\\|") "\\)\\>")
4153 org-not-done-regexp
4154 (concat "\\<\\("
4155 (mapconcat 'regexp-quote org-not-done-keywords "\\|")
4156 "\\)\\>")
4157 org-not-done-heading-regexp
4158 (concat "^\\(\\*+\\)[ \t]+\\("
4159 (mapconcat 'regexp-quote org-not-done-keywords "\\|")
4160 "\\)\\>")
4161 org-todo-line-regexp
4162 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4163 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
4164 "\\)\\>\\)?[ \t]*\\(.*\\)")
4165 org-complex-heading-regexp
4166 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4167 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
4168 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
4169 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4170 org-complex-heading-regexp-format
4171 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4172 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
4173 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
4174 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4175 org-nl-done-regexp
4176 (concat "\n\\*+[ \t]+"
4177 "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
4178 "\\)" "\\>")
4179 org-todo-line-tags-regexp
4180 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4181 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
4182 (org-re
4183 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
4184 org-looking-at-done-regexp
4185 (concat "^" "\\(?:"
4186 (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
4187 "\\>")
4188 org-deadline-regexp (concat "\\<" org-deadline-string)
4189 org-deadline-time-regexp
4190 (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
4191 org-deadline-line-regexp
4192 (concat "\\<\\(" org-deadline-string "\\).*")
4193 org-scheduled-regexp
4194 (concat "\\<" org-scheduled-string)
4195 org-scheduled-time-regexp
4196 (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
4197 org-closed-time-regexp
4198 (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
4199 org-keyword-time-regexp
4200 (concat "\\<\\(" org-scheduled-string
4201 "\\|" org-deadline-string
4202 "\\|" org-closed-string
4203 "\\|" org-clock-string "\\)"
4204 " *[[<]\\([^]>]+\\)[]>]")
4205 org-keyword-time-not-clock-regexp
4206 (concat "\\<\\(" org-scheduled-string
4207 "\\|" org-deadline-string
4208 "\\|" org-closed-string
4209 "\\)"
4210 " *[[<]\\([^]>]+\\)[]>]")
4211 org-maybe-keyword-time-regexp
4212 (concat "\\(\\<\\(" org-scheduled-string
4213 "\\|" org-deadline-string
4214 "\\|" org-closed-string
4215 "\\|" org-clock-string "\\)\\)?"
4216 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4217 org-planning-or-clock-line-re
4218 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
4219 "\\|" org-deadline-string
4220 "\\|" org-closed-string "\\|" org-clock-string
4221 "\\)\\>\\)")
4222 org-all-time-keywords
4223 (mapcar (lambda (w) (substring w 0 -1))
4224 (list org-scheduled-string org-deadline-string
4225 org-clock-string org-closed-string))
4227 (org-compute-latex-and-specials-regexp)
4228 (org-set-font-lock-defaults))))
4230 (defun org-file-contents (file &optional noerror)
4231 "Return the contents of FILE, as a string."
4232 (if (or (not file)
4233 (not (file-readable-p file)))
4234 (if noerror
4235 (progn
4236 (message "Cannot read file %s" file)
4237 (ding) (sit-for 2)
4239 (error "Cannot read file %s" file))
4240 (with-temp-buffer
4241 (insert-file-contents file)
4242 (buffer-string))))
4244 (defun org-extract-log-state-settings (x)
4245 "Extract the log state setting from a TODO keyword string.
4246 This will extract info from a string like \"WAIT(w@/!)\"."
4247 (let (kw key log1 log2)
4248 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
4249 (setq kw (match-string 1 x)
4250 key (and (match-end 2) (match-string 2 x))
4251 log1 (and (match-end 3) (match-string 3 x))
4252 log2 (and (match-end 4) (match-string 4 x)))
4253 (and (or log1 log2)
4254 (list kw
4255 (and log1 (if (equal log1 "!") 'time 'note))
4256 (and log2 (if (equal log2 "!") 'time 'note)))))))
4258 (defun org-remove-keyword-keys (list)
4259 "Remove a pair of parenthesis at the end of each string in LIST."
4260 (mapcar (lambda (x)
4261 (if (string-match "(.*)$" x)
4262 (substring x 0 (match-beginning 0))
4264 list))
4266 (defun org-assign-fast-keys (alist)
4267 "Assign fast keys to a keyword-key alist.
4268 Respect keys that are already there."
4269 (let (new e (alt ?0))
4270 (while (setq e (pop alist))
4271 (if (or (memq (car e) '(:newline :endgroup :startgroup))
4272 (cdr e)) ;; Key already assigned.
4273 (push e new)
4274 (let ((clist (string-to-list (downcase (car e))))
4275 (used (append new alist)))
4276 (when (= (car clist) ?@)
4277 (pop clist))
4278 (while (and clist (rassoc (car clist) used))
4279 (pop clist))
4280 (unless clist
4281 (while (rassoc alt used)
4282 (incf alt)))
4283 (push (cons (car e) (or (car clist) alt)) new))))
4284 (nreverse new)))
4286 ;;; Some variables used in various places
4288 (defvar org-window-configuration nil
4289 "Used in various places to store a window configuration.")
4290 (defvar org-selected-window nil
4291 "Used in various places to store a window configuration.")
4292 (defvar org-finish-function nil
4293 "Function to be called when `C-c C-c' is used.
4294 This is for getting out of special buffers like remember.")
4297 ;; FIXME: Occasionally check by commenting these, to make sure
4298 ;; no other functions uses these, forgetting to let-bind them.
4299 (defvar entry)
4300 (defvar last-state)
4301 (defvar date)
4303 ;; Defined somewhere in this file, but used before definition.
4304 (defvar org-entities) ;; defined in org-entities.el
4305 (defvar org-struct-menu)
4306 (defvar org-org-menu)
4307 (defvar org-tbl-menu)
4309 ;;;; Define the Org-mode
4311 (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
4312 (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."))
4315 ;; We use a before-change function to check if a table might need
4316 ;; an update.
4317 (defvar org-table-may-need-update t
4318 "Indicates that a table might need an update.
4319 This variable is set by `org-before-change-function'.
4320 `org-table-align' sets it back to nil.")
4321 (defun org-before-change-function (beg end)
4322 "Every change indicates that a table might need an update."
4323 (setq org-table-may-need-update t))
4324 (defvar org-mode-map)
4325 (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
4326 (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
4327 (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
4328 (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
4329 (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
4330 (defvar org-table-buffer-is-an nil)
4331 (defconst org-outline-regexp "\\*+ ")
4333 ;;;###autoload
4334 (define-derived-mode org-mode outline-mode "Org"
4335 "Outline-based notes management and organizer, alias
4336 \"Carsten's outline-mode for keeping track of everything.\"
4338 Org-mode develops organizational tasks around a NOTES file which
4339 contains information about projects as plain text. Org-mode is
4340 implemented on top of outline-mode, which is ideal to keep the content
4341 of large files well structured. It supports ToDo items, deadlines and
4342 time stamps, which magically appear in the diary listing of the Emacs
4343 calendar. Tables are easily created with a built-in table editor.
4344 Plain text URL-like links connect to websites, emails (VM), Usenet
4345 messages (Gnus), BBDB entries, and any files related to the project.
4346 For printing and sharing of notes, an Org-mode file (or a part of it)
4347 can be exported as a structured ASCII or HTML file.
4349 The following commands are available:
4351 \\{org-mode-map}"
4353 ;; Get rid of Outline menus, they are not needed
4354 ;; Need to do this here because define-derived-mode sets up
4355 ;; the keymap so late. Still, it is a waste to call this each time
4356 ;; we switch another buffer into org-mode.
4357 (if (featurep 'xemacs)
4358 (when (boundp 'outline-mode-menu-heading)
4359 ;; Assume this is Greg's port, it uses easymenu
4360 (easy-menu-remove outline-mode-menu-heading)
4361 (easy-menu-remove outline-mode-menu-show)
4362 (easy-menu-remove outline-mode-menu-hide))
4363 (define-key org-mode-map [menu-bar headings] 'undefined)
4364 (define-key org-mode-map [menu-bar hide] 'undefined)
4365 (define-key org-mode-map [menu-bar show] 'undefined))
4367 (org-load-modules-maybe)
4368 (easy-menu-add org-org-menu)
4369 (easy-menu-add org-tbl-menu)
4370 (org-install-agenda-files-menu)
4371 (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
4372 (add-to-invisibility-spec '(org-cwidth))
4373 (add-to-invisibility-spec '(org-hide-block . t))
4374 (when (featurep 'xemacs)
4375 (org-set-local 'line-move-ignore-invisible t))
4376 (org-set-local 'outline-regexp org-outline-regexp)
4377 (org-set-local 'outline-level 'org-outline-level)
4378 (when (and org-ellipsis
4379 (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
4380 (fboundp 'make-glyph-code))
4381 (unless org-display-table
4382 (setq org-display-table (make-display-table)))
4383 (set-display-table-slot
4384 org-display-table 4
4385 (vconcat (mapcar
4386 (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
4387 org-ellipsis)))
4388 (if (stringp org-ellipsis) org-ellipsis "..."))))
4389 (setq buffer-display-table org-display-table))
4390 (org-set-regexps-and-options)
4391 (when (and org-tag-faces (not org-tags-special-faces-re))
4392 ;; tag faces set outside customize.... force initialization.
4393 (org-set-tag-faces 'org-tag-faces org-tag-faces))
4394 ;; Calc embedded
4395 (org-set-local 'calc-embedded-open-mode "# ")
4396 (modify-syntax-entry ?# "<")
4397 (modify-syntax-entry ?@ "w")
4398 (if org-startup-truncated (setq truncate-lines t))
4399 (org-set-local 'font-lock-unfontify-region-function
4400 'org-unfontify-region)
4401 ;; Activate before-change-function
4402 (org-set-local 'org-table-may-need-update t)
4403 (org-add-hook 'before-change-functions 'org-before-change-function nil
4404 'local)
4405 ;; Check for running clock before killing a buffer
4406 (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
4407 ;; Paragraphs and auto-filling
4408 (org-set-autofill-regexps)
4409 (setq indent-line-function 'org-indent-line-function)
4410 (org-update-radio-target-regexp)
4411 ;; Make sure dependence stuff works reliably, even for users who set it
4412 ;; too late :-(
4413 (if org-enforce-todo-dependencies
4414 (add-hook 'org-blocker-hook
4415 'org-block-todo-from-children-or-siblings-or-parent)
4416 (remove-hook 'org-blocker-hook
4417 'org-block-todo-from-children-or-siblings-or-parent))
4418 (if org-enforce-todo-checkbox-dependencies
4419 (add-hook 'org-blocker-hook
4420 'org-block-todo-from-checkboxes)
4421 (remove-hook 'org-blocker-hook
4422 'org-block-todo-from-checkboxes))
4424 ;; Comment characters
4425 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
4426 (org-set-local 'comment-padding " ")
4428 ;; Align options lines
4429 (org-set-local
4430 'align-mode-rules-list
4431 '((org-in-buffer-settings
4432 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
4433 (modes . '(org-mode)))))
4435 ;; Imenu
4436 (org-set-local 'imenu-create-index-function
4437 'org-imenu-get-tree)
4439 ;; Make isearch reveal context
4440 (if (or (featurep 'xemacs)
4441 (not (boundp 'outline-isearch-open-invisible-function)))
4442 ;; Emacs 21 and XEmacs make use of the hook
4443 (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
4444 ;; Emacs 22 deals with this through a special variable
4445 (org-set-local 'outline-isearch-open-invisible-function
4446 (lambda (&rest ignore) (org-show-context 'isearch))))
4448 ;; Turn on org-beamer-mode?
4449 (and org-startup-with-beamer-mode (org-beamer-mode 1))
4451 ;; If empty file that did not turn on org-mode automatically, make it to.
4452 (if (and org-insert-mode-line-in-empty-file
4453 (interactive-p)
4454 (= (point-min) (point-max)))
4455 (insert "# -*- mode: org -*-\n\n"))
4456 (unless org-inhibit-startup
4457 (when org-startup-align-all-tables
4458 (let ((bmp (buffer-modified-p)))
4459 (org-table-map-tables 'org-table-align)
4460 (set-buffer-modified-p bmp)))
4461 (when org-startup-indented
4462 (require 'org-indent)
4463 (org-indent-mode 1))
4464 (unless org-inhibit-startup-visibility-stuff
4465 (org-set-startup-visibility))))
4467 (when (fboundp 'abbrev-table-put)
4468 (abbrev-table-put org-mode-abbrev-table
4469 :parents (list text-mode-abbrev-table)))
4471 (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
4473 (defun org-current-time ()
4474 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
4475 (if (> (car org-time-stamp-rounding-minutes) 1)
4476 (let ((r (car org-time-stamp-rounding-minutes))
4477 (time (decode-time)))
4478 (apply 'encode-time
4479 (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
4480 (nthcdr 2 time))))
4481 (current-time)))
4483 ;;;; Font-Lock stuff, including the activators
4485 (defvar org-mouse-map (make-sparse-keymap))
4486 (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
4487 (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
4488 (when org-mouse-1-follows-link
4489 (org-defkey org-mouse-map [follow-link] 'mouse-face))
4490 (when org-tab-follows-link
4491 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
4492 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
4494 (require 'font-lock)
4496 (defconst org-non-link-chars "]\t\n\r<>")
4497 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
4498 "shell" "elisp"))
4499 (defvar org-link-types-re nil
4500 "Matches a link that has a url-like prefix like \"http:\"")
4501 (defvar org-link-re-with-space nil
4502 "Matches a link with spaces, optional angular brackets around it.")
4503 (defvar org-link-re-with-space2 nil
4504 "Matches a link with spaces, optional angular brackets around it.")
4505 (defvar org-link-re-with-space3 nil
4506 "Matches a link with spaces, only for internal part in bracket links.")
4507 (defvar org-angle-link-re nil
4508 "Matches link with angular brackets, spaces are allowed.")
4509 (defvar org-plain-link-re nil
4510 "Matches plain link, without spaces.")
4511 (defvar org-bracket-link-regexp nil
4512 "Matches a link in double brackets.")
4513 (defvar org-bracket-link-analytic-regexp nil
4514 "Regular expression used to analyze links.
4515 Here is what the match groups contain after a match:
4516 1: http:
4517 2: http
4518 3: path
4519 4: [desc]
4520 5: desc")
4521 (defvar org-bracket-link-analytic-regexp++ nil
4522 "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
4523 (defvar org-any-link-re nil
4524 "Regular expression matching any link.")
4526 (defun org-make-link-regexps ()
4527 "Update the link regular expressions.
4528 This should be called after the variable `org-link-types' has changed."
4529 (setq org-link-types-re
4530 (concat
4531 "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
4532 org-link-re-with-space
4533 (concat
4534 "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4535 "\\([^" org-non-link-chars " ]"
4536 "[^" org-non-link-chars "]*"
4537 "[^" org-non-link-chars " ]\\)>?")
4538 org-link-re-with-space2
4539 (concat
4540 "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4541 "\\([^" org-non-link-chars " ]"
4542 "[^\t\n\r]*"
4543 "[^" org-non-link-chars " ]\\)>?")
4544 org-link-re-with-space3
4545 (concat
4546 "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4547 "\\([^" org-non-link-chars " ]"
4548 "[^\t\n\r]*\\)")
4549 org-angle-link-re
4550 (concat
4551 "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4552 "\\([^" org-non-link-chars " ]"
4553 "[^" org-non-link-chars "]*"
4554 "\\)>")
4555 org-plain-link-re
4556 (concat
4557 "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4558 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
4559 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
4560 org-bracket-link-regexp
4561 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
4562 org-bracket-link-analytic-regexp
4563 (concat
4564 "\\[\\["
4565 "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
4566 "\\([^]]+\\)"
4567 "\\]"
4568 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4569 "\\]")
4570 org-bracket-link-analytic-regexp++
4571 (concat
4572 "\\[\\["
4573 "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
4574 "\\([^]]+\\)"
4575 "\\]"
4576 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4577 "\\]")
4578 org-any-link-re
4579 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
4580 org-angle-link-re "\\)\\|\\("
4581 org-plain-link-re "\\)")))
4583 (org-make-link-regexps)
4585 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
4586 "Regular expression for fast time stamp matching.")
4587 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
4588 "Regular expression for fast time stamp matching.")
4589 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4590 "Regular expression matching time strings for analysis.
4591 This one does not require the space after the date, so it can be used
4592 on a string that terminates immediately after the date.")
4593 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4594 "Regular expression matching time strings for analysis.")
4595 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
4596 "Regular expression matching time stamps, with groups.")
4597 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
4598 "Regular expression matching time stamps (also [..]), with groups.")
4599 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
4600 "Regular expression matching a time stamp range.")
4601 (defconst org-tr-regexp-both
4602 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
4603 "Regular expression matching a time stamp range.")
4604 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
4605 org-ts-regexp "\\)?")
4606 "Regular expression matching a time stamp or time stamp range.")
4607 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
4608 org-ts-regexp-both "\\)?")
4609 "Regular expression matching a time stamp or time stamp range.
4610 The time stamps may be either active or inactive.")
4612 (defvar org-emph-face nil)
4614 (defun org-do-emphasis-faces (limit)
4615 "Run through the buffer and add overlays to links."
4616 (let (rtn a)
4617 (while (and (not rtn) (re-search-forward org-emph-re limit t))
4618 (if (not (= (char-after (match-beginning 3))
4619 (char-after (match-beginning 4))))
4620 (progn
4621 (setq rtn t)
4622 (setq a (assoc (match-string 3) org-emphasis-alist))
4623 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
4624 'face
4625 (nth 1 a))
4626 (and (nth 4 a)
4627 (org-remove-flyspell-overlays-in
4628 (match-beginning 0) (match-end 0)))
4629 (add-text-properties (match-beginning 2) (match-end 2)
4630 '(font-lock-multiline t))
4631 (when org-hide-emphasis-markers
4632 (add-text-properties (match-end 4) (match-beginning 5)
4633 '(invisible org-link))
4634 (add-text-properties (match-beginning 3) (match-end 3)
4635 '(invisible org-link)))))
4636 (backward-char 1))
4637 rtn))
4639 (defun org-emphasize (&optional char)
4640 "Insert or change an emphasis, i.e. a font like bold or italic.
4641 If there is an active region, change that region to a new emphasis.
4642 If there is no region, just insert the marker characters and position
4643 the cursor between them.
4644 CHAR should be either the marker character, or the first character of the
4645 HTML tag associated with that emphasis. If CHAR is a space, the means
4646 to remove the emphasis of the selected region.
4647 If char is not given (for example in an interactive call) it
4648 will be prompted for."
4649 (interactive)
4650 (let ((eal org-emphasis-alist) e det
4651 (erc org-emphasis-regexp-components)
4652 (prompt "")
4653 (string "") beg end move tag c s)
4654 (if (org-region-active-p)
4655 (setq beg (region-beginning) end (region-end)
4656 string (buffer-substring beg end))
4657 (setq move t))
4659 (while (setq e (pop eal))
4660 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
4661 c (aref tag 0))
4662 (push (cons c (string-to-char (car e))) det)
4663 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
4664 (substring tag 1)))))
4665 (setq det (nreverse det))
4666 (unless char
4667 (message "%s" (concat "Emphasis marker or tag:" prompt))
4668 (setq char (read-char-exclusive)))
4669 (setq char (or (cdr (assoc char det)) char))
4670 (if (equal char ?\ )
4671 (setq s "" move nil)
4672 (unless (assoc (char-to-string char) org-emphasis-alist)
4673 (error "No such emphasis marker: \"%c\"" char))
4674 (setq s (char-to-string char)))
4675 (while (and (> (length string) 1)
4676 (equal (substring string 0 1) (substring string -1))
4677 (assoc (substring string 0 1) org-emphasis-alist))
4678 (setq string (substring string 1 -1)))
4679 (setq string (concat s string s))
4680 (if beg (delete-region beg end))
4681 (unless (or (bolp)
4682 (string-match (concat "[" (nth 0 erc) "\n]")
4683 (char-to-string (char-before (point)))))
4684 (insert " "))
4685 (unless (or (eobp)
4686 (string-match (concat "[" (nth 1 erc) "\n]")
4687 (char-to-string (char-after (point)))))
4688 (insert " ") (backward-char 1))
4689 (insert string)
4690 (and move (backward-char 1))))
4692 (defconst org-nonsticky-props
4693 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
4695 (defsubst org-rear-nonsticky-at (pos)
4696 (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
4698 (defun org-activate-plain-links (limit)
4699 "Run through the buffer and add overlays to links."
4700 (catch 'exit
4701 (let (f)
4702 (if (re-search-forward org-plain-link-re limit t)
4703 (progn
4704 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4705 (setq f (get-text-property (match-beginning 0) 'face))
4706 (if (or (eq f 'org-tag)
4707 (and (listp f) (memq 'org-tag f)))
4709 (add-text-properties (match-beginning 0) (match-end 0)
4710 (list 'mouse-face 'highlight
4711 'face 'org-link
4712 'keymap org-mouse-map))
4713 (org-rear-nonsticky-at (match-end 0)))
4714 t)))))
4716 (defun org-activate-code (limit)
4717 (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
4718 (progn
4719 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4720 (remove-text-properties (match-beginning 0) (match-end 0)
4721 '(display t invisible t intangible t))
4722 t)))
4724 (defun org-fontify-meta-lines-and-blocks (limit)
4725 "Fontify #+ lines and blocks, in the correct ways."
4726 (let ((case-fold-search t))
4727 (if (re-search-forward
4728 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
4729 limit t)
4730 (let ((beg (match-beginning 0))
4731 (beg1 (line-beginning-position 2))
4732 (dc1 (downcase (match-string 2)))
4733 (dc3 (downcase (match-string 3)))
4734 end end1 quoting block-type)
4735 (cond
4736 ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
4737 ;; a single line of backend-specific content
4738 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4739 (remove-text-properties (match-beginning 0) (match-end 0)
4740 '(display t invisible t intangible t))
4741 (add-text-properties (match-beginning 1) (match-end 3)
4742 '(font-lock-fontified t face org-meta-line))
4743 (add-text-properties (match-beginning 6) (match-end 6)
4744 '(font-lock-fontified t face org-block))
4746 ((and (match-end 4) (equal dc3 "begin"))
4747 ;; Truely a block
4748 (setq block-type (downcase (match-string 5))
4749 quoting (member block-type org-protecting-blocks))
4750 (when (re-search-forward
4751 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
4752 nil t) ;; on purpose, we look further than LIMIT
4753 (setq end (match-end 0) end1 (1- (match-beginning 0)))
4754 (when quoting
4755 (remove-text-properties beg end
4756 '(display t invisible t intangible t)))
4757 (add-text-properties
4758 beg end
4759 '(font-lock-fontified t font-lock-multiline t))
4760 (add-text-properties beg beg1 '(face org-meta-line))
4761 (add-text-properties end1 end '(face org-meta-line))
4762 (cond
4763 (quoting
4764 (add-text-properties beg1 end1 '(face org-block)))
4765 ((not org-fontify-quote-and-verse-blocks))
4766 ((string= block-type "quote")
4767 (add-text-properties beg1 end1 '(face org-quote)))
4768 ((string= block-type "verse")
4769 (add-text-properties beg1 end1 '(face org-verse))))
4771 ((member dc1 '("title:" "author:" "email:" "date:"))
4772 (add-text-properties
4773 beg (match-end 3)
4774 (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
4775 '(font-lock-fontified t invisible t)
4776 '(font-lock-fontified t face org-document-info-keyword)))
4777 (add-text-properties
4778 (match-beginning 6) (match-end 6)
4779 (if (string-equal dc1 "title:")
4780 '(font-lock-fontified t face org-document-title)
4781 '(font-lock-fontified t face org-document-info))))
4782 ((not (member (char-after beg) '(?\ ?\t)))
4783 ;; just any other in-buffer setting, but not indented
4784 (add-text-properties
4785 beg (match-end 0)
4786 '(font-lock-fontified t face org-meta-line))
4788 ((or (member dc1 '("begin:" "end:" "caption:" "label:"
4789 "orgtbl:" "tblfm:" "tblname:"))
4790 (and (match-end 4) (equal dc3 "attr")))
4791 (add-text-properties
4792 beg (match-end 0)
4793 '(font-lock-fontified t face org-meta-line))
4795 ((member dc3 '(" " ""))
4796 (add-text-properties
4797 beg (match-end 0)
4798 '(font-lock-fontified t face font-lock-comment-face)))
4799 (t nil))))))
4801 (defun org-activate-angle-links (limit)
4802 "Run through the buffer and add overlays to links."
4803 (if (re-search-forward org-angle-link-re limit t)
4804 (progn
4805 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4806 (add-text-properties (match-beginning 0) (match-end 0)
4807 (list 'mouse-face 'highlight
4808 'keymap org-mouse-map))
4809 (org-rear-nonsticky-at (match-end 0))
4810 t)))
4812 (defun org-activate-footnote-links (limit)
4813 "Run through the buffer and add overlays to links."
4814 (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
4815 limit t)
4816 (progn
4817 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4818 (add-text-properties (match-beginning 2) (match-end 2)
4819 (list 'mouse-face 'highlight
4820 'keymap org-mouse-map
4821 'help-echo
4822 (if (= (point-at-bol) (match-beginning 2))
4823 "Footnote definition"
4824 "Footnote reference")
4826 (org-rear-nonsticky-at (match-end 2))
4827 t)))
4829 (defun org-activate-bracket-links (limit)
4830 "Run through the buffer and add overlays to bracketed links."
4831 (if (re-search-forward org-bracket-link-regexp limit t)
4832 (let* ((help (concat "LINK: "
4833 (org-match-string-no-properties 1)))
4834 ;; FIXME: above we should remove the escapes.
4835 ;; but that requires another match, protecting match data,
4836 ;; a lot of overhead for font-lock.
4837 (ip (org-maybe-intangible
4838 (list 'invisible 'org-link
4839 'keymap org-mouse-map 'mouse-face 'highlight
4840 'font-lock-multiline t 'help-echo help)))
4841 (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
4842 'font-lock-multiline t 'help-echo help)))
4843 ;; We need to remove the invisible property here. Table narrowing
4844 ;; may have made some of this invisible.
4845 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4846 (remove-text-properties (match-beginning 0) (match-end 0)
4847 '(invisible nil))
4848 (if (match-end 3)
4849 (progn
4850 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
4851 (org-rear-nonsticky-at (match-beginning 3))
4852 (add-text-properties (match-beginning 3) (match-end 3) vp)
4853 (org-rear-nonsticky-at (match-end 3))
4854 (add-text-properties (match-end 3) (match-end 0) ip)
4855 (org-rear-nonsticky-at (match-end 0)))
4856 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
4857 (org-rear-nonsticky-at (match-beginning 1))
4858 (add-text-properties (match-beginning 1) (match-end 1) vp)
4859 (org-rear-nonsticky-at (match-end 1))
4860 (add-text-properties (match-end 1) (match-end 0) ip)
4861 (org-rear-nonsticky-at (match-end 0)))
4862 t)))
4864 (defun org-activate-dates (limit)
4865 "Run through the buffer and add overlays to dates."
4866 (if (re-search-forward org-tsr-regexp-both 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 (org-rear-nonsticky-at (match-end 0))
4873 (when org-display-custom-times
4874 (if (match-end 3)
4875 (org-display-custom-time (match-beginning 3) (match-end 3)))
4876 (org-display-custom-time (match-beginning 1) (match-end 1)))
4877 t)))
4879 (defvar org-target-link-regexp nil
4880 "Regular expression matching radio targets in plain text.")
4881 (make-variable-buffer-local 'org-target-link-regexp)
4882 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
4883 "Regular expression matching a link target.")
4884 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
4885 "Regular expression matching a radio target.")
4886 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
4887 "Regular expression matching any target.")
4889 (defun org-activate-target-links (limit)
4890 "Run through the buffer and add overlays to target matches."
4891 (when org-target-link-regexp
4892 (let ((case-fold-search t))
4893 (if (re-search-forward org-target-link-regexp limit t)
4894 (progn
4895 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4896 (add-text-properties (match-beginning 0) (match-end 0)
4897 (list 'mouse-face 'highlight
4898 'keymap org-mouse-map
4899 'help-echo "Radio target link"
4900 'org-linked-text t))
4901 (org-rear-nonsticky-at (match-end 0))
4902 t)))))
4904 (defun org-update-radio-target-regexp ()
4905 "Find all radio targets in this file and update the regular expression."
4906 (interactive)
4907 (when (memq 'radio org-activate-links)
4908 (setq org-target-link-regexp
4909 (org-make-target-link-regexp (org-all-targets 'radio)))
4910 (org-restart-font-lock)))
4912 (defun org-hide-wide-columns (limit)
4913 (let (s e)
4914 (setq s (text-property-any (point) (or limit (point-max))
4915 'org-cwidth t))
4916 (when s
4917 (setq e (next-single-property-change s 'org-cwidth))
4918 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
4919 (goto-char e)
4920 t)))
4922 (defvar org-latex-and-specials-regexp nil
4923 "Regular expression for highlighting export special stuff.")
4924 (defvar org-match-substring-regexp)
4925 (defvar org-match-substring-with-braces-regexp)
4927 ;; This should be with the exporter code, but we also use if for font-locking
4928 (defconst org-export-html-special-string-regexps
4929 '(("\\\\-" . "&shy;")
4930 ("---\\([^-]\\)" . "&mdash;\\1")
4931 ("--\\([^-]\\)" . "&ndash;\\1")
4932 ("\\.\\.\\." . "&hellip;"))
4933 "Regular expressions for special string conversion.")
4936 (defun org-compute-latex-and-specials-regexp ()
4937 "Compute regular expression for stuff treated specially by exporters."
4938 (if (not org-highlight-latex-fragments-and-specials)
4939 (org-set-local 'org-latex-and-specials-regexp nil)
4940 (require 'org-exp)
4941 (let*
4942 ((matchers (plist-get org-format-latex-options :matchers))
4943 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
4944 org-latex-regexps)))
4945 (org-export-allow-BIND nil)
4946 (options (org-combine-plists (org-default-export-plist)
4947 (org-infile-export-plist)))
4948 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
4949 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
4950 (org-export-with-TeX-macros (plist-get options :TeX-macros))
4951 (org-export-html-expand (plist-get options :expand-quoted-html))
4952 (org-export-with-special-strings (plist-get options :special-strings))
4953 (re-sub
4954 (cond
4955 ((equal org-export-with-sub-superscripts '{})
4956 (list org-match-substring-with-braces-regexp))
4957 (org-export-with-sub-superscripts
4958 (list org-match-substring-regexp))
4959 (t nil)))
4960 (re-latex
4961 (if org-export-with-LaTeX-fragments
4962 (mapcar (lambda (x) (nth 1 x)) latexs)))
4963 (re-macros
4964 (if org-export-with-TeX-macros
4965 (list (concat "\\\\"
4966 (regexp-opt
4967 (append (mapcar 'car (append org-entities-user
4968 org-entities))
4969 (if (boundp 'org-latex-entities)
4970 (mapcar (lambda (x)
4971 (or (car-safe x) x))
4972 org-latex-entities)
4973 nil))
4974 'words))) ; FIXME
4976 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
4977 (re-special (if org-export-with-special-strings
4978 (mapcar (lambda (x) (car x))
4979 org-export-html-special-string-regexps)))
4980 (re-rest
4981 (delq nil
4982 (list
4983 (if org-export-html-expand "@<[^>\n]+>")
4984 ))))
4985 (org-set-local
4986 'org-latex-and-specials-regexp
4987 (mapconcat 'identity (append re-latex re-sub re-macros re-special
4988 re-rest) "\\|")))))
4990 (defun org-do-latex-and-special-faces (limit)
4991 "Run through the buffer and add overlays to links."
4992 (when org-latex-and-specials-regexp
4993 (let (rtn d)
4994 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
4995 limit t))
4996 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
4997 'face))
4998 '(org-code org-verbatim underline)))
4999 (progn
5000 (setq rtn t
5001 d (cond ((member (char-after (1+ (match-beginning 0)))
5002 '(?_ ?^)) 1)
5003 (t 0)))
5004 (font-lock-prepend-text-property
5005 (+ d (match-beginning 0)) (match-end 0)
5006 'face 'org-latex-and-export-specials)
5007 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
5008 '(font-lock-multiline t)))))
5009 rtn)))
5011 (defun org-restart-font-lock ()
5012 "Restart font-lock-mode, to force refontification."
5013 (when (and (boundp 'font-lock-mode) font-lock-mode)
5014 (font-lock-mode -1)
5015 (font-lock-mode 1)))
5017 (defun org-all-targets (&optional radio)
5018 "Return a list of all targets in this file.
5019 With optional argument RADIO, only find radio targets."
5020 (let ((re (if radio org-radio-target-regexp org-target-regexp))
5021 rtn)
5022 (save-excursion
5023 (goto-char (point-min))
5024 (while (re-search-forward re nil t)
5025 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
5026 rtn)))
5028 (defun org-make-target-link-regexp (targets)
5029 "Make regular expression matching all strings in TARGETS.
5030 The regular expression finds the targets also if there is a line break
5031 between words."
5032 (and targets
5033 (concat
5034 "\\<\\("
5035 (mapconcat
5036 (lambda (x)
5037 (while (string-match " +" x)
5038 (setq x (replace-match "\\s-+" t t x)))
5040 targets
5041 "\\|")
5042 "\\)\\>")))
5044 (defun org-activate-tags (limit)
5045 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
5046 (progn
5047 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5048 (add-text-properties (match-beginning 1) (match-end 1)
5049 (list 'mouse-face 'highlight
5050 'keymap org-mouse-map))
5051 (org-rear-nonsticky-at (match-end 1))
5052 t)))
5054 (defun org-outline-level ()
5055 "Compute the outline level of the heading at point.
5056 This function assumes that the cursor is at the beginning of a line matched
5057 by outline-regexp. Otherwise it returns garbage.
5058 If this is called at a normal headline, the level is the number of stars.
5059 Use `org-reduced-level' to remove the effect of `org-odd-levels'.
5060 For plain list items, if they are matched by `outline-regexp', this returns
5061 1000 plus the line indentation."
5062 (save-excursion
5063 (looking-at outline-regexp)
5064 (if (match-beginning 1)
5065 (+ (org-get-string-indentation (match-string 1)) 1000)
5066 (1- (- (match-end 0) (match-beginning 0))))))
5068 (defvar org-font-lock-keywords nil)
5070 (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5071 "Regular expression matching a property line.")
5073 (defvar org-font-lock-hook nil
5074 "Functions to be called for special font lock stuff.")
5076 (defun org-font-lock-hook (limit)
5077 (run-hook-with-args 'org-font-lock-hook limit))
5079 (defun org-set-font-lock-defaults ()
5080 (let* ((em org-fontify-emphasized-text)
5081 (lk org-activate-links)
5082 (org-font-lock-extra-keywords
5083 (list
5084 ;; Call the hook
5085 '(org-font-lock-hook)
5086 ;; Headlines
5087 `(,(if org-fontify-whole-heading-line
5088 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5089 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5090 (1 (org-get-level-face 1))
5091 (2 (org-get-level-face 2))
5092 (3 (org-get-level-face 3)))
5093 ;; Table lines
5094 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5095 (1 'org-table t))
5096 ;; Table internals
5097 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
5098 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
5099 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
5100 '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
5101 ;; Drawers
5102 (list org-drawer-regexp '(0 'org-special-keyword t))
5103 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
5104 ;; Properties
5105 (list org-property-re
5106 '(1 'org-special-keyword t)
5107 '(3 'org-property-value t))
5108 ;; Links
5109 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
5110 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
5111 (if (memq 'plain lk) '(org-activate-plain-links))
5112 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
5113 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
5114 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
5115 (if (memq 'footnote lk) '(org-activate-footnote-links
5116 (2 'org-footnote t)))
5117 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
5118 '(org-hide-wide-columns (0 nil append))
5119 ;; TODO lines
5120 (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
5121 '(1 (org-get-todo-face 1) t))
5122 ;; DONE
5123 (if org-fontify-done-headline
5124 (list (concat "^[*]+ +\\<\\("
5125 (mapconcat 'regexp-quote org-done-keywords "\\|")
5126 "\\)\\(.*\\)")
5127 '(2 'org-headline-done t))
5128 nil)
5129 ;; Priorities
5130 '(org-font-lock-add-priority-faces)
5131 ;; Tags
5132 '(org-font-lock-add-tag-faces)
5133 ;; Special keywords
5134 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
5135 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
5136 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
5137 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
5138 ;; Emphasis
5139 (if em
5140 (if (featurep 'xemacs)
5141 '(org-do-emphasis-faces (0 nil append))
5142 '(org-do-emphasis-faces)))
5143 ;; Checkboxes
5144 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
5145 2 'org-checkbox prepend)
5146 (if org-provide-checkbox-statistics
5147 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5148 (0 (org-get-checkbox-statistics-face) t)))
5149 ;; Description list items
5150 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
5151 2 'bold prepend)
5152 ;; ARCHIVEd headings
5153 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
5154 '(1 'org-archived prepend))
5155 ;; Specials
5156 '(org-do-latex-and-special-faces)
5157 ;; Code
5158 '(org-activate-code (1 'org-code t))
5159 ;; COMMENT
5160 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
5161 "\\|" org-quote-string "\\)\\>")
5162 '(1 'org-special-keyword t))
5163 '("^#.*" (0 'font-lock-comment-face t))
5164 ;; Blocks and meta lines
5165 '(org-fontify-meta-lines-and-blocks)
5167 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
5168 ;; Now set the full font-lock-keywords
5169 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
5170 (org-set-local 'font-lock-defaults
5171 '(org-font-lock-keywords t nil nil backward-paragraph))
5172 (kill-local-variable 'font-lock-keywords) nil))
5174 (defun org-fontify-like-in-org-mode (s &optional odd-levels)
5175 "Fontify string S like in Org-mode"
5176 (with-temp-buffer
5177 (insert s)
5178 (let ((org-odd-levels-only odd-levels))
5179 (org-mode)
5180 (font-lock-fontify-buffer)
5181 (buffer-string))))
5183 (defvar org-m nil)
5184 (defvar org-l nil)
5185 (defvar org-f nil)
5186 (defun org-get-level-face (n)
5187 "Get the right face for match N in font-lock matching of headlines."
5188 (setq org-l (- (match-end 2) (match-beginning 1) 1))
5189 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
5190 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
5191 (cond
5192 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
5193 ((eq n 2) org-f)
5194 (t (if org-level-color-stars-only nil org-f))))
5196 (defun org-get-todo-face (kwd)
5197 "Get the right face for a TODO keyword KWD.
5198 If KWD is a number, get the corresponding match group."
5199 (if (numberp kwd) (setq kwd (match-string kwd)))
5200 (or (org-face-from-face-or-color
5201 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
5202 (and (member kwd org-done-keywords) 'org-done)
5203 'org-todo))
5205 (defun org-face-from-face-or-color (context inherit face-or-color)
5206 "Create a face list that inherits INHERIT, but sets the foreground color.
5207 When FACE-OR-COLOR is not a string, just return it."
5208 (if (stringp face-or-color)
5209 (list :inherit inherit
5210 (cdr (assoc context org-faces-easy-properties))
5211 face-or-color)
5212 face-or-color))
5214 (defun org-font-lock-add-tag-faces (limit)
5215 "Add the special tag faces."
5216 (when (and org-tag-faces org-tags-special-faces-re)
5217 (while (re-search-forward org-tags-special-faces-re limit t)
5218 (add-text-properties (match-beginning 1) (match-end 1)
5219 (list 'face (org-get-tag-face 1)
5220 'font-lock-fontified t))
5221 (backward-char 1))))
5223 (defun org-font-lock-add-priority-faces (limit)
5224 "Add the special priority faces."
5225 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
5226 (add-text-properties
5227 (match-beginning 0) (match-end 0)
5228 (list 'face (or (org-face-from-face-or-color
5229 'priority 'org-special-keyword
5230 (cdr (assoc (char-after (match-beginning 1))
5231 org-priority-faces)))
5232 'org-special-keyword)
5233 'font-lock-fontified t))))
5235 (defun org-get-tag-face (kwd)
5236 "Get the right face for a TODO keyword KWD.
5237 If KWD is a number, get the corresponding match group."
5238 (if (numberp kwd) (setq kwd (match-string kwd)))
5239 (or (org-face-from-face-or-color
5240 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
5241 'org-tag))
5243 (defun org-unfontify-region (beg end &optional maybe_loudly)
5244 "Remove fontification and activation overlays from links."
5245 (font-lock-default-unfontify-region beg end)
5246 (let* ((buffer-undo-list t)
5247 (inhibit-read-only t) (inhibit-point-motion-hooks t)
5248 (inhibit-modification-hooks t)
5249 deactivate-mark buffer-file-name buffer-file-truename)
5250 (remove-text-properties
5251 beg end
5252 (if org-indent-mode
5253 ;; also remove line-prefix and wrap-prefix properties
5254 '(mouse-face t keymap t org-linked-text t
5255 invisible t intangible t
5256 line-prefix t wrap-prefix t
5257 org-no-flyspell t)
5258 '(mouse-face t keymap t org-linked-text t
5259 invisible t intangible t
5260 org-no-flyspell t)))))
5262 ;;;; Visibility cycling, including org-goto and indirect buffer
5264 ;;; Cycling
5266 (defvar org-cycle-global-status nil)
5267 (make-variable-buffer-local 'org-cycle-global-status)
5268 (defvar org-cycle-subtree-status nil)
5269 (make-variable-buffer-local 'org-cycle-subtree-status)
5271 ;;;###autoload
5273 (defvar org-inlinetask-min-level)
5275 (defun org-cycle (&optional arg)
5276 "TAB-action and visibility cycling for Org-mode.
5278 This is the command invoked in Org-mode by the TAB key. Its main purpose
5279 is outline visibility cycling, but it also invokes other actions
5280 in special contexts.
5282 - When this function is called with a prefix argument, rotate the entire
5283 buffer through 3 states (global cycling)
5284 1. OVERVIEW: Show only top-level headlines.
5285 2. CONTENTS: Show all headlines of all levels, but no body text.
5286 3. SHOW ALL: Show everything.
5287 When called with two `C-u C-u' prefixes, switch to the startup visibility,
5288 determined by the variable `org-startup-folded', and by any VISIBILITY
5289 properties in the buffer.
5290 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
5291 including any drawers.
5293 - When inside a table, re-align the table and move to the next field.
5295 - When point is at the beginning of a headline, rotate the subtree started
5296 by this line through 3 different states (local cycling)
5297 1. FOLDED: Only the main headline is shown.
5298 2. CHILDREN: The main headline and the direct children are shown.
5299 From this state, you can move to one of the children
5300 and zoom in further.
5301 3. SUBTREE: Show the entire subtree, including body text.
5302 If there is no subtree, switch directly from CHILDREN to FOLDED.
5304 - When point is at the beginning of an empty headline and the variable
5305 `org-cycle-level-after-item/entry-creation' is set, cycle the level
5306 of the headline by demoting and promoting it to likely levels. This
5307 speeds up creation document structure by presing TAB once or several
5308 times right after creating a new headline.
5310 - When there is a numeric prefix, go up to a heading with level ARG, do
5311 a `show-subtree' and return to the previous cursor position. If ARG
5312 is negative, go up that many levels.
5314 - When point is not at the beginning of a headline, execute the global
5315 binding for TAB, which is re-indenting the line. See the option
5316 `org-cycle-emulate-tab' for details.
5318 - Special case: if point is at the beginning of the buffer and there is
5319 no headline in line 1, this function will act as if called with prefix arg.
5320 But only if also the variable `org-cycle-global-at-bob' is t."
5321 (interactive "P")
5322 (org-load-modules-maybe)
5323 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
5324 (and org-cycle-level-after-item/entry-creation
5325 (or (org-cycle-level)
5326 (org-cycle-item-indentation))))
5327 (let* ((limit-level
5328 (or org-cycle-max-level
5329 (and (boundp 'org-inlinetask-min-level)
5330 org-inlinetask-min-level
5331 (1- org-inlinetask-min-level))))
5332 (nstars (and limit-level
5333 (if org-odd-levels-only
5334 (and limit-level (1- (* limit-level 2)))
5335 limit-level)))
5336 (outline-regexp
5337 (cond
5338 ((not (org-mode-p)) outline-regexp)
5339 ((or (eq org-cycle-include-plain-lists 'integrate)
5340 (and org-cycle-include-plain-lists (org-at-item-p)))
5341 (concat "\\(?:\\*"
5342 (if nstars (format "\\{1,%d\\}" nstars) "+")
5343 " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
5344 (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
5345 (bob-special (and org-cycle-global-at-bob (bobp)
5346 (not (looking-at outline-regexp))))
5347 (org-cycle-hook
5348 (if bob-special
5349 (delq 'org-optimize-window-after-visibility-change
5350 (copy-sequence org-cycle-hook))
5351 org-cycle-hook))
5352 (pos (point)))
5354 (if (or bob-special (equal arg '(4)))
5355 ;; special case: use global cycling
5356 (setq arg t))
5358 (cond
5360 ((equal arg '(16))
5361 (org-set-startup-visibility)
5362 (message "Startup visibility, plus VISIBILITY properties"))
5364 ((equal arg '(64))
5365 (show-all)
5366 (message "Entire buffer visible, including drawers"))
5368 ((org-at-table-p 'any)
5369 ;; Enter the table or move to the next field in the table
5370 (if (org-at-table.el-p)
5371 (message "Use C-c ' to edit table.el tables")
5372 (if arg (org-table-edit-field t)
5373 (org-table-justify-field-maybe)
5374 (call-interactively 'org-table-next-field))))
5376 ((run-hook-with-args-until-success
5377 'org-tab-after-check-for-table-hook))
5379 ((eq arg t) ;; Global cycling
5380 (org-cycle-internal-global))
5382 ((and org-drawers org-drawer-regexp
5383 (save-excursion
5384 (beginning-of-line 1)
5385 (looking-at org-drawer-regexp)))
5386 ;; Toggle block visibility
5387 (org-flag-drawer
5388 (not (get-char-property (match-end 0) 'invisible))))
5390 ((integerp arg)
5391 ;; Show-subtree, ARG levels up from here.
5392 (save-excursion
5393 (org-back-to-heading)
5394 (outline-up-heading (if (< arg 0) (- arg)
5395 (- (funcall outline-level) arg)))
5396 (org-show-subtree)))
5398 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
5399 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
5401 (org-cycle-internal-local))
5403 ;; TAB emulation and template completion
5404 (buffer-read-only (org-back-to-heading))
5406 ((run-hook-with-args-until-success
5407 'org-tab-after-check-for-cycling-hook))
5409 ((org-try-structure-completion))
5411 ((org-try-cdlatex-tab))
5413 ((run-hook-with-args-until-success
5414 'org-tab-before-tab-emulation-hook))
5416 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
5417 (or (not (bolp))
5418 (not (looking-at outline-regexp))))
5419 (call-interactively (global-key-binding "\t")))
5421 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
5422 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
5423 (or (and (eq org-cycle-emulate-tab 'white)
5424 (= (match-end 0) (point-at-eol)))
5425 (and (eq org-cycle-emulate-tab 'whitestart)
5426 (>= (match-end 0) pos))))
5428 (eq org-cycle-emulate-tab t))
5429 (call-interactively (global-key-binding "\t")))
5431 (t (save-excursion
5432 (org-back-to-heading)
5433 (org-cycle)))))))
5435 (defun org-cycle-internal-global ()
5436 "Do the global cycling action."
5437 (cond
5438 ((and (eq last-command this-command)
5439 (eq org-cycle-global-status 'overview))
5440 ;; We just created the overview - now do table of contents
5441 ;; This can be slow in very large buffers, so indicate action
5442 (run-hook-with-args 'org-pre-cycle-hook 'contents)
5443 (message "CONTENTS...")
5444 (org-content)
5445 (message "CONTENTS...done")
5446 (setq org-cycle-global-status 'contents)
5447 (run-hook-with-args 'org-cycle-hook 'contents))
5449 ((and (eq last-command this-command)
5450 (eq org-cycle-global-status 'contents))
5451 ;; We just showed the table of contents - now show everything
5452 (run-hook-with-args 'org-pre-cycle-hook 'all)
5453 (show-all)
5454 (message "SHOW ALL")
5455 (setq org-cycle-global-status 'all)
5456 (run-hook-with-args 'org-cycle-hook 'all))
5459 ;; Default action: go to overview
5460 (run-hook-with-args 'org-pre-cycle-hook 'overview)
5461 (org-overview)
5462 (message "OVERVIEW")
5463 (setq org-cycle-global-status 'overview)
5464 (run-hook-with-args 'org-cycle-hook 'overview))))
5466 (defun org-cycle-internal-local ()
5467 "Do the local cycling action."
5468 (org-back-to-heading)
5469 (let ((goal-column 0) eoh eol eos level has-children children-skipped)
5470 ;; First, some boundaries
5471 (save-excursion
5472 (org-back-to-heading)
5473 (setq level (funcall outline-level))
5474 (save-excursion
5475 (beginning-of-line 2)
5476 (if (or (featurep 'xemacs) (<= emacs-major-version 21))
5477 ; XEmacs does not have `next-single-char-property-change'
5478 ; I'm not sure about Emacs 21.
5479 (while (and (not (eobp)) ;; this is like `next-line'
5480 (get-char-property (1- (point)) 'invisible))
5481 (beginning-of-line 2))
5482 (while (and (not (eobp)) ;; this is like `next-line'
5483 (get-char-property (1- (point)) 'invisible))
5484 (goto-char (next-single-char-property-change (point) 'invisible))
5485 (and (eolp) (beginning-of-line 2))))
5486 (setq eol (point)))
5487 (outline-end-of-heading) (setq eoh (point))
5488 (save-excursion
5489 (outline-next-heading)
5490 (setq has-children (and (org-at-heading-p t)
5491 (> (funcall outline-level) level))))
5492 (org-end-of-subtree t)
5493 (unless (eobp)
5494 (skip-chars-forward " \t\n")
5495 (beginning-of-line 1) ; in case this is an item
5497 (setq eos (if (eobp) (point) (1- (point)))))
5498 ;; Find out what to do next and set `this-command'
5499 (cond
5500 ((= eos eoh)
5501 ;; Nothing is hidden behind this heading
5502 (run-hook-with-args 'org-pre-cycle-hook 'empty)
5503 (message "EMPTY ENTRY")
5504 (setq org-cycle-subtree-status nil)
5505 (save-excursion
5506 (goto-char eos)
5507 (outline-next-heading)
5508 (if (org-invisible-p) (org-flag-heading nil))))
5509 ((and (or (>= eol eos)
5510 (not (string-match "\\S-" (buffer-substring eol eos))))
5511 (or has-children
5512 (not (setq children-skipped
5513 org-cycle-skip-children-state-if-no-children))))
5514 ;; Entire subtree is hidden in one line: children view
5515 (run-hook-with-args 'org-pre-cycle-hook 'children)
5516 (org-show-entry)
5517 (show-children)
5518 (message "CHILDREN")
5519 (save-excursion
5520 (goto-char eos)
5521 (outline-next-heading)
5522 (if (org-invisible-p) (org-flag-heading nil)))
5523 (setq org-cycle-subtree-status 'children)
5524 (run-hook-with-args 'org-cycle-hook 'children))
5525 ((or children-skipped
5526 (and (eq last-command this-command)
5527 (eq org-cycle-subtree-status 'children)))
5528 ;; We just showed the children, or no children are there,
5529 ;; now show everything.
5530 (run-hook-with-args 'org-pre-cycle-hook 'subtree)
5531 (org-show-subtree)
5532 (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
5533 (setq org-cycle-subtree-status 'subtree)
5534 (run-hook-with-args 'org-cycle-hook 'subtree))
5536 ;; Default action: hide the subtree.
5537 (run-hook-with-args 'org-pre-cycle-hook 'folded)
5538 (hide-subtree)
5539 (message "FOLDED")
5540 (setq org-cycle-subtree-status 'folded)
5541 (run-hook-with-args 'org-cycle-hook 'folded)))))
5543 ;;;###autoload
5544 (defun org-global-cycle (&optional arg)
5545 "Cycle the global visibility. For details see `org-cycle'.
5546 With C-u prefix arg, switch to startup visibility.
5547 With a numeric prefix, show all headlines up to that level."
5548 (interactive "P")
5549 (let ((org-cycle-include-plain-lists
5550 (if (org-mode-p) org-cycle-include-plain-lists nil)))
5551 (cond
5552 ((integerp arg)
5553 (show-all)
5554 (hide-sublevels arg)
5555 (setq org-cycle-global-status 'contents))
5556 ((equal arg '(4))
5557 (org-set-startup-visibility)
5558 (message "Startup visibility, plus VISIBILITY properties."))
5560 (org-cycle '(4))))))
5562 (defun org-set-startup-visibility ()
5563 "Set the visibility required by startup options and properties."
5564 (cond
5565 ((eq org-startup-folded t)
5566 (org-cycle '(4)))
5567 ((eq org-startup-folded 'content)
5568 (let ((this-command 'org-cycle) (last-command 'org-cycle))
5569 (org-cycle '(4)) (org-cycle '(4)))))
5570 (unless (eq org-startup-folded 'showeverything)
5571 (if org-hide-block-startup (org-hide-block-all))
5572 (org-set-visibility-according-to-property 'no-cleanup)
5573 (org-cycle-hide-archived-subtrees 'all)
5574 (org-cycle-hide-drawers 'all)
5575 (org-cycle-show-empty-lines 'all)))
5577 (defun org-set-visibility-according-to-property (&optional no-cleanup)
5578 "Switch subtree visibilities according to :VISIBILITY: property."
5579 (interactive)
5580 (let (org-show-entry-below state)
5581 (save-excursion
5582 (goto-char (point-min))
5583 (while (re-search-forward
5584 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
5585 nil t)
5586 (setq state (match-string 1))
5587 (save-excursion
5588 (org-back-to-heading t)
5589 (hide-subtree)
5590 (org-reveal)
5591 (cond
5592 ((equal state '("fold" "folded"))
5593 (hide-subtree))
5594 ((equal state "children")
5595 (org-show-hidden-entry)
5596 (show-children))
5597 ((equal state "content")
5598 (save-excursion
5599 (save-restriction
5600 (org-narrow-to-subtree)
5601 (org-content))))
5602 ((member state '("all" "showall"))
5603 (show-subtree)))))
5604 (unless no-cleanup
5605 (org-cycle-hide-archived-subtrees 'all)
5606 (org-cycle-hide-drawers 'all)
5607 (org-cycle-show-empty-lines 'all)))))
5609 (defun org-overview ()
5610 "Switch to overview mode, showing only top-level headlines.
5611 Really, this shows all headlines with level equal or greater than the level
5612 of the first headline in the buffer. This is important, because if the
5613 first headline is not level one, then (hide-sublevels 1) gives confusing
5614 results."
5615 (interactive)
5616 (let ((level (save-excursion
5617 (goto-char (point-min))
5618 (if (re-search-forward (concat "^" outline-regexp) nil t)
5619 (progn
5620 (goto-char (match-beginning 0))
5621 (funcall outline-level))))))
5622 (and level (hide-sublevels level))))
5624 (defun org-content (&optional arg)
5625 "Show all headlines in the buffer, like a table of contents.
5626 With numerical argument N, show content up to level N."
5627 (interactive "P")
5628 (save-excursion
5629 ;; Visit all headings and show their offspring
5630 (and (integerp arg) (org-overview))
5631 (goto-char (point-max))
5632 (catch 'exit
5633 (while (and (progn (condition-case nil
5634 (outline-previous-visible-heading 1)
5635 (error (goto-char (point-min))))
5637 (looking-at outline-regexp))
5638 (if (integerp arg)
5639 (show-children (1- arg))
5640 (show-branches))
5641 (if (bobp) (throw 'exit nil))))))
5644 (defun org-optimize-window-after-visibility-change (state)
5645 "Adjust the window after a change in outline visibility.
5646 This function is the default value of the hook `org-cycle-hook'."
5647 (when (get-buffer-window (current-buffer))
5648 (cond
5649 ((eq state 'content) nil)
5650 ((eq state 'all) nil)
5651 ((eq state 'folded) nil)
5652 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
5653 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
5655 (defun org-remove-empty-overlays-at (pos)
5656 "Remove outline overlays that do not contain non-white stuff."
5657 (mapc
5658 (lambda (o)
5659 (and (eq 'outline (overlay-get o 'invisible))
5660 (not (string-match "\\S-" (buffer-substring (overlay-start o)
5661 (overlay-end o))))
5662 (delete-overlay o)))
5663 (overlays-at pos)))
5665 (defun org-clean-visibility-after-subtree-move ()
5666 "Fix visibility issues after moving a subtree."
5667 ;; First, find a reasonable region to look at:
5668 ;; Start two siblings above, end three below
5669 (let* ((beg (save-excursion
5670 (and (org-get-last-sibling)
5671 (org-get-last-sibling))
5672 (point)))
5673 (end (save-excursion
5674 (and (org-get-next-sibling)
5675 (org-get-next-sibling)
5676 (org-get-next-sibling))
5677 (if (org-at-heading-p)
5678 (point-at-eol)
5679 (point))))
5680 (level (looking-at "\\*+"))
5681 (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
5682 (save-excursion
5683 (save-restriction
5684 (narrow-to-region beg end)
5685 (when re
5686 ;; Properly fold already folded siblings
5687 (goto-char (point-min))
5688 (while (re-search-forward re nil t)
5689 (if (and (not (org-invisible-p))
5690 (save-excursion
5691 (goto-char (point-at-eol)) (org-invisible-p)))
5692 (hide-entry))))
5693 (org-cycle-show-empty-lines 'overview)
5694 (org-cycle-hide-drawers 'overview)))))
5696 (defun org-cycle-show-empty-lines (state)
5697 "Show empty lines above all visible headlines.
5698 The region to be covered depends on STATE when called through
5699 `org-cycle-hook'. Lisp program can use t for STATE to get the
5700 entire buffer covered. Note that an empty line is only shown if there
5701 are at least `org-cycle-separator-lines' empty lines before the headline."
5702 (when (not (= org-cycle-separator-lines 0))
5703 (save-excursion
5704 (let* ((n (abs org-cycle-separator-lines))
5705 (re (cond
5706 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
5707 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
5708 (t (let ((ns (number-to-string (- n 2))))
5709 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
5710 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
5711 beg end b e)
5712 (cond
5713 ((memq state '(overview contents t))
5714 (setq beg (point-min) end (point-max)))
5715 ((memq state '(children folded))
5716 (setq beg (point) end (progn (org-end-of-subtree t t)
5717 (beginning-of-line 2)
5718 (point)))))
5719 (when beg
5720 (goto-char beg)
5721 (while (re-search-forward re end t)
5722 (unless (get-char-property (match-end 1) 'invisible)
5723 (setq e (match-end 1))
5724 (if (< org-cycle-separator-lines 0)
5725 (setq b (save-excursion
5726 (goto-char (match-beginning 0))
5727 (org-back-over-empty-lines)
5728 (if (save-excursion
5729 (goto-char (max (point-min) (1- (point))))
5730 (org-on-heading-p))
5731 (1- (point))
5732 (point))))
5733 (setq b (match-beginning 1)))
5734 (outline-flag-region b e nil)))))))
5735 ;; Never hide empty lines at the end of the file.
5736 (save-excursion
5737 (goto-char (point-max))
5738 (outline-previous-heading)
5739 (outline-end-of-heading)
5740 (if (and (looking-at "[ \t\n]+")
5741 (= (match-end 0) (point-max)))
5742 (outline-flag-region (point) (match-end 0) nil))))
5744 (defun org-show-empty-lines-in-parent ()
5745 "Move to the parent and re-show empty lines before visible headlines."
5746 (save-excursion
5747 (let ((context (if (org-up-heading-safe) 'children 'overview)))
5748 (org-cycle-show-empty-lines context))))
5750 (defun org-files-list ()
5751 "Return `org-agenda-files' list, plus all open org-mode files.
5752 This is useful for operations that need to scan all of a user's
5753 open and agenda-wise Org files."
5754 (let ((files (mapcar 'expand-file-name (org-agenda-files))))
5755 (dolist (buf (buffer-list))
5756 (with-current-buffer buf
5757 (if (and (eq major-mode 'org-mode) (buffer-file-name))
5758 (let ((file (expand-file-name (buffer-file-name))))
5759 (unless (member file files)
5760 (push file files))))))
5761 files))
5763 (defsubst org-entry-beginning-position ()
5764 "Return the beginning position of the current entry."
5765 (save-excursion (outline-back-to-heading t) (point)))
5767 (defsubst org-entry-end-position ()
5768 "Return the end position of the current entry."
5769 (save-excursion (outline-next-heading) (point)))
5771 (defun org-cycle-hide-drawers (state)
5772 "Re-hide all drawers after a visibility state change."
5773 (when (and (org-mode-p)
5774 (not (memq state '(overview folded contents))))
5775 (save-excursion
5776 (let* ((globalp (memq state '(contents all)))
5777 (beg (if globalp (point-min) (point)))
5778 (end (if globalp (point-max)
5779 (if (eq state 'children)
5780 (save-excursion (outline-next-heading) (point))
5781 (org-end-of-subtree t)))))
5782 (goto-char beg)
5783 (while (re-search-forward org-drawer-regexp end t)
5784 (org-flag-drawer t))))))
5786 (defun org-flag-drawer (flag)
5787 (save-excursion
5788 (beginning-of-line 1)
5789 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
5790 (let ((b (match-end 0))
5791 (outline-regexp org-outline-regexp))
5792 (if (re-search-forward
5793 "^[ \t]*:END:"
5794 (save-excursion (outline-next-heading) (point)) t)
5795 (outline-flag-region b (point-at-eol) flag)
5796 (error ":END: line missing at position %s" b))))))
5798 (defun org-subtree-end-visible-p ()
5799 "Is the end of the current subtree visible?"
5800 (pos-visible-in-window-p
5801 (save-excursion (org-end-of-subtree t) (point))))
5803 (defun org-first-headline-recenter (&optional N)
5804 "Move cursor to the first headline and recenter the headline.
5805 Optional argument N means put the headline into the Nth line of the window."
5806 (goto-char (point-min))
5807 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
5808 (beginning-of-line)
5809 (recenter (prefix-numeric-value N))))
5811 ;;; Saving and restoring visibility
5813 (defun org-outline-overlay-data (&optional use-markers)
5814 "Return a list of the locations of all outline overlays.
5815 The are overlays with the `invisible' property value `outline'.
5816 The return valus is a list of cons cells, with start and stop
5817 positions for each overlay.
5818 If USE-MARKERS is set, return the positions as markers."
5819 (let (beg end)
5820 (save-excursion
5821 (save-restriction
5822 (widen)
5823 (delq nil
5824 (mapcar (lambda (o)
5825 (when (eq (overlay-get o 'invisible) 'outline)
5826 (setq beg (overlay-start o)
5827 end (overlay-end o))
5828 (and beg end (> end beg)
5829 (if use-markers
5830 (cons (move-marker (make-marker) beg)
5831 (move-marker (make-marker) end))
5832 (cons beg end)))))
5833 (overlays-in (point-min) (point-max))))))))
5835 (defun org-set-outline-overlay-data (data)
5836 "Create visibility overlays for all positions in DATA.
5837 DATA should have been made by `org-outline-overlay-data'."
5838 (let (o)
5839 (save-excursion
5840 (save-restriction
5841 (widen)
5842 (show-all)
5843 (mapc (lambda (c)
5844 (setq o (make-overlay (car c) (cdr c)))
5845 (overlay-put o 'invisible 'outline))
5846 data)))))
5848 (defmacro org-save-outline-visibility (use-markers &rest body)
5849 "Save and restore outline visibility around BODY.
5850 If USE-MARKERS is non-nil, use markers for the positions.
5851 This means that the buffer may change while running BODY,
5852 but it also means that the buffer should stay alive
5853 during the operation, because otherwise all these markers will
5854 point nowhere."
5855 `(let ((data (org-outline-overlay-data ,use-markers)))
5856 (unwind-protect
5857 (progn
5858 ,@body
5859 (org-set-outline-overlay-data data))
5860 (when ,use-markers
5861 (mapc (lambda (c)
5862 (and (markerp (car c)) (move-marker (car c) nil))
5863 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
5864 data)))))
5867 ;;; Folding of blocks
5869 (defconst org-block-regexp
5871 "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
5872 "Regular expression for hiding blocks.")
5874 (defvar org-hide-block-overlays nil
5875 "Overlays hiding blocks.")
5876 (make-variable-buffer-local 'org-hide-block-overlays)
5878 (defun org-block-map (function &optional start end)
5879 "Call func at the head of all source blocks in the current
5880 buffer. Optional arguments START and END can be used to limit
5881 the range."
5882 (let ((start (or start (point-min)))
5883 (end (or end (point-max))))
5884 (save-excursion
5885 (goto-char start)
5886 (while (and (< (point) end) (re-search-forward org-block-regexp end t))
5887 (save-excursion
5888 (save-match-data
5889 (goto-char (match-beginning 0))
5890 (funcall function)))))))
5892 (defun org-hide-block-toggle-all ()
5893 "Toggle the visibility of all blocks in the current buffer."
5894 (org-block-map #'org-hide-block-toggle))
5896 (defun org-hide-block-all ()
5897 "Fold all blocks in the current buffer."
5898 (interactive)
5899 (org-show-block-all)
5900 (org-block-map #'org-hide-block-toggle-maybe))
5902 (defun org-show-block-all ()
5903 "Unfold all blocks in the current buffer."
5904 (mapc 'delete-overlay org-hide-block-overlays)
5905 (setq org-hide-block-overlays nil))
5907 (defun org-hide-block-toggle-maybe ()
5908 "Toggle visibility of block at point."
5909 (interactive)
5910 (let ((case-fold-search t))
5911 (if (save-excursion
5912 (beginning-of-line 1)
5913 (looking-at org-block-regexp))
5914 (progn (org-hide-block-toggle)
5915 t) ;; to signal that we took action
5916 nil))) ;; to signal that we did not
5918 (defun org-hide-block-toggle (&optional force)
5919 "Toggle the visibility of the current block."
5920 (interactive)
5921 (save-excursion
5922 (beginning-of-line)
5923 (if (re-search-forward org-block-regexp nil t)
5924 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
5925 (end (match-end 0)) ;; end of entire body
5927 (if (memq t (mapcar (lambda (overlay)
5928 (eq (overlay-get overlay 'invisible)
5929 'org-hide-block))
5930 (overlays-at start)))
5931 (if (or (not force) (eq force 'off))
5932 (mapc (lambda (ov)
5933 (when (member ov org-hide-block-overlays)
5934 (setq org-hide-block-overlays
5935 (delq ov org-hide-block-overlays)))
5936 (when (eq (overlay-get ov 'invisible)
5937 'org-hide-block)
5938 (delete-overlay ov)))
5939 (overlays-at start)))
5940 (setq ov (make-overlay start end))
5941 (overlay-put ov 'invisible 'org-hide-block)
5942 ;; make the block accessible to isearch
5943 (overlay-put
5944 ov 'isearch-open-invisible
5945 (lambda (ov)
5946 (when (member ov org-hide-block-overlays)
5947 (setq org-hide-block-overlays
5948 (delq ov org-hide-block-overlays)))
5949 (when (eq (overlay-get ov 'invisible)
5950 'org-hide-block)
5951 (delete-overlay ov))))
5952 (push ov org-hide-block-overlays)))
5953 (error "Not looking at a source block"))))
5955 ;; org-tab-after-check-for-cycling-hook
5956 (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
5957 ;; Remove overlays when changing major mode
5958 (add-hook 'org-mode-hook
5959 (lambda () (org-add-hook 'change-major-mode-hook
5960 'org-show-block-all 'append 'local)))
5962 ;;; Org-goto
5964 (defvar org-goto-window-configuration nil)
5965 (defvar org-goto-marker nil)
5966 (defvar org-goto-map
5967 (let ((map (make-sparse-keymap)))
5968 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
5969 (while (setq cmd (pop cmds))
5970 (substitute-key-definition cmd cmd map global-map)))
5971 (suppress-keymap map)
5972 (org-defkey map "\C-m" 'org-goto-ret)
5973 (org-defkey map [(return)] 'org-goto-ret)
5974 (org-defkey map [(left)] 'org-goto-left)
5975 (org-defkey map [(right)] 'org-goto-right)
5976 (org-defkey map [(control ?g)] 'org-goto-quit)
5977 (org-defkey map "\C-i" 'org-cycle)
5978 (org-defkey map [(tab)] 'org-cycle)
5979 (org-defkey map [(down)] 'outline-next-visible-heading)
5980 (org-defkey map [(up)] 'outline-previous-visible-heading)
5981 (if org-goto-auto-isearch
5982 (if (fboundp 'define-key-after)
5983 (define-key-after map [t] 'org-goto-local-auto-isearch)
5984 nil)
5985 (org-defkey map "q" 'org-goto-quit)
5986 (org-defkey map "n" 'outline-next-visible-heading)
5987 (org-defkey map "p" 'outline-previous-visible-heading)
5988 (org-defkey map "f" 'outline-forward-same-level)
5989 (org-defkey map "b" 'outline-backward-same-level)
5990 (org-defkey map "u" 'outline-up-heading))
5991 (org-defkey map "/" 'org-occur)
5992 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
5993 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
5994 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
5995 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
5996 (org-defkey map "\C-c\C-u" 'outline-up-heading)
5997 map))
5999 (defconst org-goto-help
6000 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
6001 RET=jump to location [Q]uit and return to previous location
6002 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6004 (defvar org-goto-start-pos) ; dynamically scoped parameter
6006 ;; FIXME: Docstring does not mention both interfaces
6007 (defun org-goto (&optional alternative-interface)
6008 "Look up a different location in the current file, keeping current visibility.
6010 When you want look-up or go to a different location in a document, the
6011 fastest way is often to fold the entire buffer and then dive into the tree.
6012 This method has the disadvantage, that the previous location will be folded,
6013 which may not be what you want.
6015 This command works around this by showing a copy of the current buffer
6016 in an indirect buffer, in overview mode. You can dive into the tree in
6017 that copy, use org-occur and incremental search to find a location.
6018 When pressing RET or `Q', the command returns to the original buffer in
6019 which the visibility is still unchanged. After RET is will also jump to
6020 the location selected in the indirect buffer and expose the
6021 the headline hierarchy above."
6022 (interactive "P")
6023 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6024 (org-refile-use-outline-path t)
6025 (org-refile-target-verify-function nil)
6026 (interface
6027 (if (not alternative-interface)
6028 org-goto-interface
6029 (if (eq org-goto-interface 'outline)
6030 'outline-path-completion
6031 'outline)))
6032 (org-goto-start-pos (point))
6033 (selected-point
6034 (if (eq interface 'outline)
6035 (car (org-get-location (current-buffer) org-goto-help))
6036 (nth 3 (org-refile-get-location "Goto: ")))))
6037 (if selected-point
6038 (progn
6039 (org-mark-ring-push org-goto-start-pos)
6040 (goto-char selected-point)
6041 (if (or (org-invisible-p) (org-invisible-p2))
6042 (org-show-context 'org-goto)))
6043 (message "Quit"))))
6045 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6046 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6047 (defvar org-goto-local-auto-isearch-map) ; defined below
6049 (defun org-get-location (buf help)
6050 "Let the user select a location in the Org-mode buffer BUF.
6051 This function uses a recursive edit. It returns the selected position
6052 or nil."
6053 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6054 (isearch-hide-immediately nil)
6055 (isearch-search-fun-function
6056 (lambda () 'org-goto-local-search-headings))
6057 (org-goto-selected-point org-goto-exit-command)
6058 (pop-up-frames nil)
6059 (special-display-buffer-names nil)
6060 (special-display-regexps nil)
6061 (special-display-function nil))
6062 (save-excursion
6063 (save-window-excursion
6064 (delete-other-windows)
6065 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6066 (switch-to-buffer
6067 (condition-case nil
6068 (make-indirect-buffer (current-buffer) "*org-goto*")
6069 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6070 (with-output-to-temp-buffer "*Help*"
6071 (princ help))
6072 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
6073 (setq buffer-read-only nil)
6074 (let ((org-startup-truncated t)
6075 (org-startup-folded nil)
6076 (org-startup-align-all-tables nil))
6077 (org-mode)
6078 (org-overview))
6079 (setq buffer-read-only t)
6080 (if (and (boundp 'org-goto-start-pos)
6081 (integer-or-marker-p org-goto-start-pos))
6082 (let ((org-show-hierarchy-above t)
6083 (org-show-siblings t)
6084 (org-show-following-heading t))
6085 (goto-char org-goto-start-pos)
6086 (and (org-invisible-p) (org-show-context)))
6087 (goto-char (point-min)))
6088 (let (org-special-ctrl-a/e) (org-beginning-of-line))
6089 (message "Select location and press RET")
6090 (use-local-map org-goto-map)
6091 (recursive-edit)
6093 (kill-buffer "*org-goto*")
6094 (cons org-goto-selected-point org-goto-exit-command)))
6096 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6097 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6098 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6099 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6101 (defun org-goto-local-search-headings (string bound noerror)
6102 "Search and make sure that any matches are in headlines."
6103 (catch 'return
6104 (while (if isearch-forward
6105 (search-forward string bound noerror)
6106 (search-backward string bound noerror))
6107 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6108 (and (member :headline context)
6109 (not (member :tags context))))
6110 (throw 'return (point))))))
6112 (defun org-goto-local-auto-isearch ()
6113 "Start isearch."
6114 (interactive)
6115 (goto-char (point-min))
6116 (let ((keys (this-command-keys)))
6117 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6118 (isearch-mode t)
6119 (isearch-process-search-char (string-to-char keys)))))
6121 (defun org-goto-ret (&optional arg)
6122 "Finish `org-goto' by going to the new location."
6123 (interactive "P")
6124 (setq org-goto-selected-point (point)
6125 org-goto-exit-command 'return)
6126 (throw 'exit nil))
6128 (defun org-goto-left ()
6129 "Finish `org-goto' by going to the new location."
6130 (interactive)
6131 (if (org-on-heading-p)
6132 (progn
6133 (beginning-of-line 1)
6134 (setq org-goto-selected-point (point)
6135 org-goto-exit-command 'left)
6136 (throw 'exit nil))
6137 (error "Not on a heading")))
6139 (defun org-goto-right ()
6140 "Finish `org-goto' by going to the new location."
6141 (interactive)
6142 (if (org-on-heading-p)
6143 (progn
6144 (setq org-goto-selected-point (point)
6145 org-goto-exit-command 'right)
6146 (throw 'exit nil))
6147 (error "Not on a heading")))
6149 (defun org-goto-quit ()
6150 "Finish `org-goto' without cursor motion."
6151 (interactive)
6152 (setq org-goto-selected-point nil)
6153 (setq org-goto-exit-command 'quit)
6154 (throw 'exit nil))
6156 ;;; Indirect buffer display of subtrees
6158 (defvar org-indirect-dedicated-frame nil
6159 "This is the frame being used for indirect tree display.")
6160 (defvar org-last-indirect-buffer nil)
6162 (defun org-tree-to-indirect-buffer (&optional arg)
6163 "Create indirect buffer and narrow it to current subtree.
6164 With numerical prefix ARG, go up to this level and then take that tree.
6165 If ARG is negative, go up that many levels.
6166 If `org-indirect-buffer-display' is not `new-frame', the command removes the
6167 indirect buffer previously made with this command, to avoid proliferation of
6168 indirect buffers. However, when you call the command with a `C-u' prefix, or
6169 when `org-indirect-buffer-display' is `new-frame', the last buffer
6170 is kept so that you can work with several indirect buffers at the same time.
6171 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
6172 requests that a new frame be made for the new buffer, so that the dedicated
6173 frame is not changed."
6174 (interactive "P")
6175 (let ((cbuf (current-buffer))
6176 (cwin (selected-window))
6177 (pos (point))
6178 beg end level heading ibuf)
6179 (save-excursion
6180 (org-back-to-heading t)
6181 (when (numberp arg)
6182 (setq level (org-outline-level))
6183 (if (< arg 0) (setq arg (+ level arg)))
6184 (while (> (setq level (org-outline-level)) arg)
6185 (outline-up-heading 1 t)))
6186 (setq beg (point)
6187 heading (org-get-heading))
6188 (org-end-of-subtree t t)
6189 (if (org-on-heading-p) (backward-char 1))
6190 (setq end (point)))
6191 (if (and (buffer-live-p org-last-indirect-buffer)
6192 (not (eq org-indirect-buffer-display 'new-frame))
6193 (not arg))
6194 (kill-buffer org-last-indirect-buffer))
6195 (setq ibuf (org-get-indirect-buffer cbuf)
6196 org-last-indirect-buffer ibuf)
6197 (cond
6198 ((or (eq org-indirect-buffer-display 'new-frame)
6199 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
6200 (select-frame (make-frame))
6201 (delete-other-windows)
6202 (switch-to-buffer ibuf)
6203 (org-set-frame-title heading))
6204 ((eq org-indirect-buffer-display 'dedicated-frame)
6205 (raise-frame
6206 (select-frame (or (and org-indirect-dedicated-frame
6207 (frame-live-p org-indirect-dedicated-frame)
6208 org-indirect-dedicated-frame)
6209 (setq org-indirect-dedicated-frame (make-frame)))))
6210 (delete-other-windows)
6211 (switch-to-buffer ibuf)
6212 (org-set-frame-title (concat "Indirect: " heading)))
6213 ((eq org-indirect-buffer-display 'current-window)
6214 (switch-to-buffer ibuf))
6215 ((eq org-indirect-buffer-display 'other-window)
6216 (pop-to-buffer ibuf))
6217 (t (error "Invalid value")))
6218 (if (featurep 'xemacs)
6219 (save-excursion (org-mode) (turn-on-font-lock)))
6220 (narrow-to-region beg end)
6221 (show-all)
6222 (goto-char pos)
6223 (and (window-live-p cwin) (select-window cwin))))
6225 (defun org-get-indirect-buffer (&optional buffer)
6226 (setq buffer (or buffer (current-buffer)))
6227 (let ((n 1) (base (buffer-name buffer)) bname)
6228 (while (buffer-live-p
6229 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
6230 (setq n (1+ n)))
6231 (condition-case nil
6232 (make-indirect-buffer buffer bname 'clone)
6233 (error (make-indirect-buffer buffer bname)))))
6235 (defun org-set-frame-title (title)
6236 "Set the title of the current frame to the string TITLE."
6237 ;; FIXME: how to name a single frame in XEmacs???
6238 (unless (featurep 'xemacs)
6239 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
6241 ;;;; Structure editing
6243 ;;; Inserting headlines
6245 (defun org-previous-line-empty-p ()
6246 (save-excursion
6247 (and (not (bobp))
6248 (or (beginning-of-line 0) t)
6249 (save-match-data
6250 (looking-at "[ \t]*$")))))
6252 (defun org-insert-heading (&optional force-heading invisible-ok)
6253 "Insert a new heading or item with same depth at point.
6254 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
6255 If point is at the beginning of a headline, insert a sibling before the
6256 current headline. If point is not at the beginning, do not split the line,
6257 but create the new headline after the current line.
6258 When INVISIBLE-OK is set, stop at invisible headlines when going back.
6259 This is important for non-interactive uses of the command."
6260 (interactive "P")
6261 (if (or (= (buffer-size) 0)
6262 (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
6263 (org-on-heading-p))))
6264 (not (org-in-item-p))))
6265 (insert "\n* ")
6266 (when (or force-heading (not (org-insert-item)))
6267 (let* ((empty-line-p nil)
6268 (head (save-excursion
6269 (condition-case nil
6270 (progn
6271 (org-back-to-heading invisible-ok)
6272 (setq empty-line-p (org-previous-line-empty-p))
6273 (match-string 0))
6274 (error "*"))))
6275 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
6276 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
6277 pos hide-previous previous-pos)
6278 (cond
6279 ((and (org-on-heading-p) (bolp)
6280 (or (bobp)
6281 (save-excursion (backward-char 1) (not (org-invisible-p)))))
6282 ;; insert before the current line
6283 (open-line (if blank 2 1)))
6284 ((and (bolp)
6285 (not org-insert-heading-respect-content)
6286 (or (bobp)
6287 (save-excursion
6288 (backward-char 1) (not (org-invisible-p)))))
6289 ;; insert right here
6290 nil)
6292 ;; somewhere in the line
6293 (save-excursion
6294 (setq previous-pos (point-at-bol))
6295 (end-of-line)
6296 (setq hide-previous (org-invisible-p)))
6297 (and org-insert-heading-respect-content (org-show-subtree))
6298 (let ((split
6299 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
6300 (save-excursion
6301 (let ((p (point)))
6302 (goto-char (point-at-bol))
6303 (and (looking-at org-complex-heading-regexp)
6304 (> p (match-beginning 4)))))))
6305 tags pos)
6306 (cond
6307 (org-insert-heading-respect-content
6308 (org-end-of-subtree nil t)
6309 (or (bolp) (newline))
6310 (or (org-previous-line-empty-p)
6311 (and blank (newline)))
6312 (open-line 1))
6313 ((org-on-heading-p)
6314 (when hide-previous
6315 (show-children)
6316 (org-show-entry))
6317 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
6318 (setq tags (and (match-end 2) (match-string 2)))
6319 (and (match-end 1)
6320 (delete-region (match-beginning 1) (match-end 1)))
6321 (setq pos (point-at-bol))
6322 (or split (end-of-line 1))
6323 (delete-horizontal-space)
6324 (if (string-match "\\`\\*+\\'"
6325 (buffer-substring (point-at-bol) (point)))
6326 (insert " "))
6327 (newline (if blank 2 1))
6328 (when tags
6329 (save-excursion
6330 (goto-char pos)
6331 (end-of-line 1)
6332 (insert " " tags)
6333 (org-set-tags nil 'align))))
6335 (or split (end-of-line 1))
6336 (newline (if blank 2 1)))))))
6337 (insert head) (just-one-space)
6338 (setq pos (point))
6339 (end-of-line 1)
6340 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
6341 (when (and org-insert-heading-respect-content hide-previous)
6342 (save-excursion
6343 (goto-char previous-pos)
6344 (hide-subtree)))
6345 (run-hooks 'org-insert-heading-hook)))))
6347 (defun org-get-heading (&optional no-tags)
6348 "Return the heading of the current entry, without the stars."
6349 (save-excursion
6350 (org-back-to-heading t)
6351 (if (looking-at
6352 (if no-tags
6353 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
6354 "\\*+[ \t]+\\([^\r\n]*\\)"))
6355 (match-string 1) "")))
6357 (defun org-heading-components ()
6358 "Return the components of the current heading.
6359 This is a list with the following elements:
6360 - the level as an integer
6361 - the reduced level, different if `org-odd-levels-only' is set.
6362 - the TODO keyword, or nil
6363 - the priority character, like ?A, or nil if no priority is given
6364 - the headline text itself, or the tags string if no headline text
6365 - the tags string, or nil."
6366 (save-excursion
6367 (org-back-to-heading t)
6368 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
6369 (list (length (match-string 1))
6370 (org-reduced-level (length (match-string 1)))
6371 (org-match-string-no-properties 2)
6372 (and (match-end 3) (aref (match-string 3) 2))
6373 (org-match-string-no-properties 4)
6374 (org-match-string-no-properties 5)))))
6376 (defun org-get-entry ()
6377 "Get the entry text, after heading, entire subtree."
6378 (save-excursion
6379 (org-back-to-heading t)
6380 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
6382 (defun org-insert-heading-after-current ()
6383 "Insert a new heading with same level as current, after current subtree."
6384 (interactive)
6385 (org-back-to-heading)
6386 (org-insert-heading)
6387 (org-move-subtree-down)
6388 (end-of-line 1))
6390 (defun org-insert-heading-respect-content ()
6391 (interactive)
6392 (let ((org-insert-heading-respect-content t))
6393 (org-insert-heading t)))
6395 (defun org-insert-todo-heading-respect-content (&optional force-state)
6396 (interactive "P")
6397 (let ((org-insert-heading-respect-content t))
6398 (org-insert-todo-heading force-state t)))
6400 (defun org-insert-todo-heading (arg &optional force-heading)
6401 "Insert a new heading with the same level and TODO state as current heading.
6402 If the heading has no TODO state, or if the state is DONE, use the first
6403 state (TODO by default). Also with prefix arg, force first state."
6404 (interactive "P")
6405 (when (or force-heading (not (org-insert-item 'checkbox)))
6406 (org-insert-heading force-heading)
6407 (save-excursion
6408 (org-back-to-heading)
6409 (outline-previous-heading)
6410 (looking-at org-todo-line-regexp))
6411 (let*
6412 ((new-mark-x
6413 (if (or arg
6414 (not (match-beginning 2))
6415 (member (match-string 2) org-done-keywords))
6416 (car org-todo-keywords-1)
6417 (match-string 2)))
6418 (new-mark
6420 (run-hook-with-args-until-success
6421 'org-todo-get-default-hook new-mark-x nil)
6422 new-mark-x)))
6423 (beginning-of-line 1)
6424 (and (looking-at "\\*+ ") (goto-char (match-end 0))
6425 (if org-treat-insert-todo-heading-as-state-change
6426 (org-todo new-mark)
6427 (insert new-mark " "))))
6428 (when org-provide-todo-statistics
6429 (org-update-parent-todo-statistics))))
6431 (defun org-insert-subheading (arg)
6432 "Insert a new subheading and demote it.
6433 Works for outline headings and for plain lists alike."
6434 (interactive "P")
6435 (org-insert-heading arg)
6436 (cond
6437 ((org-on-heading-p) (org-do-demote))
6438 ((org-at-item-p) (org-indent-item 1))))
6440 (defun org-insert-todo-subheading (arg)
6441 "Insert a new subheading with TODO keyword or checkbox and demote it.
6442 Works for outline headings and for plain lists alike."
6443 (interactive "P")
6444 (org-insert-todo-heading arg)
6445 (cond
6446 ((org-on-heading-p) (org-do-demote))
6447 ((org-at-item-p) (org-indent-item 1))))
6449 ;;; Promotion and Demotion
6451 (defvar org-after-demote-entry-hook nil
6452 "Hook run after an entry has been demoted.
6453 The cursor will be at the beginning of the entry.
6454 When a subtree is being demoted, the hook will be called for each node.")
6456 (defvar org-after-promote-entry-hook nil
6457 "Hook run after an entry has been promoted.
6458 The cursor will be at the beginning of the entry.
6459 When a subtree is being promoted, the hook will be called for each node.")
6461 (defun org-promote-subtree ()
6462 "Promote the entire subtree.
6463 See also `org-promote'."
6464 (interactive)
6465 (save-excursion
6466 (org-map-tree 'org-promote))
6467 (org-fix-position-after-promote))
6469 (defun org-demote-subtree ()
6470 "Demote the entire subtree. See `org-demote'.
6471 See also `org-promote'."
6472 (interactive)
6473 (save-excursion
6474 (org-map-tree 'org-demote))
6475 (org-fix-position-after-promote))
6478 (defun org-do-promote ()
6479 "Promote the current heading higher up the tree.
6480 If the region is active in `transient-mark-mode', promote all headings
6481 in the region."
6482 (interactive)
6483 (save-excursion
6484 (if (org-region-active-p)
6485 (org-map-region 'org-promote (region-beginning) (region-end))
6486 (org-promote)))
6487 (org-fix-position-after-promote))
6489 (defun org-do-demote ()
6490 "Demote the current heading lower down the tree.
6491 If the region is active in `transient-mark-mode', demote all headings
6492 in the region."
6493 (interactive)
6494 (save-excursion
6495 (if (org-region-active-p)
6496 (org-map-region 'org-demote (region-beginning) (region-end))
6497 (org-demote)))
6498 (org-fix-position-after-promote))
6500 (defun org-fix-position-after-promote ()
6501 "Make sure that after pro/demotion cursor position is right."
6502 (let ((pos (point)))
6503 (when (save-excursion
6504 (beginning-of-line 1)
6505 (looking-at org-todo-line-regexp)
6506 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
6507 (cond ((eobp) (insert " "))
6508 ((eolp) (insert " "))
6509 ((equal (char-after) ?\ ) (forward-char 1))))))
6511 (defun org-current-level ()
6512 "Return the level of the current entry, or nil if before the first headline.
6513 The level is the number of stars at the beginning of the headline."
6514 (save-excursion
6515 (condition-case nil
6516 (progn
6517 (org-back-to-heading t)
6518 (funcall outline-level))
6519 (error nil))))
6521 (defun org-get-previous-line-level ()
6522 "Return the outline depth of the last headline before the current line.
6523 Returns 0 for the first headline in the buffer, and nil if before the
6524 first headline."
6525 (let ((current-level (org-current-level))
6526 (prev-level (when (> (line-number-at-pos) 1)
6527 (save-excursion
6528 (beginning-of-line 0)
6529 (org-current-level)))))
6530 (cond ((null current-level) nil) ; Before first headline
6531 ((null prev-level) 0) ; At first headline
6532 (prev-level))))
6534 (defun org-reduced-level (l)
6535 "Compute the effective level of a heading.
6536 This takes into account the setting of `org-odd-levels-only'."
6537 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
6539 (defun org-level-increment ()
6540 "Return the number of stars that will be added or removed at a
6541 time to headlines when structure editing, based on the value of
6542 `org-odd-levels-only'."
6543 (if org-odd-levels-only 2 1))
6545 (defun org-get-valid-level (level &optional change)
6546 "Rectify a level change under the influence of `org-odd-levels-only'
6547 LEVEL is a current level, CHANGE is by how much the level should be
6548 modified. Even if CHANGE is nil, LEVEL may be returned modified because
6549 even level numbers will become the next higher odd number."
6550 (if org-odd-levels-only
6551 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
6552 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
6553 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
6554 (max 1 (+ level (or change 0)))))
6556 (if (boundp 'define-obsolete-function-alias)
6557 (if (or (featurep 'xemacs) (< emacs-major-version 23))
6558 (define-obsolete-function-alias 'org-get-legal-level
6559 'org-get-valid-level)
6560 (define-obsolete-function-alias 'org-get-legal-level
6561 'org-get-valid-level "23.1")))
6563 (defun org-promote ()
6564 "Promote the current heading higher up the tree.
6565 If the region is active in `transient-mark-mode', promote all headings
6566 in the region."
6567 (org-back-to-heading t)
6568 (let* ((level (save-match-data (funcall outline-level)))
6569 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
6570 (diff (abs (- level (length up-head) -1))))
6571 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
6572 (replace-match up-head nil t)
6573 ;; Fixup tag positioning
6574 (and org-auto-align-tags (org-set-tags nil t))
6575 (if org-adapt-indentation (org-fixup-indentation (- diff)))
6576 (run-hooks 'org-after-promote-entry-hook)))
6578 (defun org-demote ()
6579 "Demote the current heading lower down the tree.
6580 If the region is active in `transient-mark-mode', demote all headings
6581 in the region."
6582 (org-back-to-heading t)
6583 (let* ((level (save-match-data (funcall outline-level)))
6584 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
6585 (diff (abs (- level (length down-head) -1))))
6586 (replace-match down-head nil t)
6587 ;; Fixup tag positioning
6588 (and org-auto-align-tags (org-set-tags nil t))
6589 (if org-adapt-indentation (org-fixup-indentation diff))
6590 (run-hooks 'org-after-demote-entry-hook)))
6592 (defun org-cycle-level ()
6593 "Cycle the level of an empty headline through possible states.
6594 This goes first to child, then to parent, level, then up the hierarchy.
6595 After top level, it switches back to sibling level."
6596 (interactive)
6597 (let ((org-adapt-indentation nil))
6598 (when (org-point-at-end-of-empty-headline)
6599 (setq this-command 'org-cycle-level) ; Only needed for caching
6600 (let ((cur-level (org-current-level))
6601 (prev-level (org-get-previous-line-level)))
6602 (cond
6603 ;; If first headline in file, promote to top-level.
6604 ((= prev-level 0)
6605 (loop repeat (/ (- cur-level 1) (org-level-increment))
6606 do (org-do-promote)))
6607 ;; If same level as prev, demote one.
6608 ((= prev-level cur-level)
6609 (org-do-demote))
6610 ;; If parent is top-level, promote to top level if not already.
6611 ((= prev-level 1)
6612 (loop repeat (/ (- cur-level 1) (org-level-increment))
6613 do (org-do-promote)))
6614 ;; If top-level, return to prev-level.
6615 ((= cur-level 1)
6616 (loop repeat (/ (- prev-level 1) (org-level-increment))
6617 do (org-do-demote)))
6618 ;; If less than prev-level, promote one.
6619 ((< cur-level prev-level)
6620 (org-do-promote))
6621 ;; If deeper than prev-level, promote until higher than
6622 ;; prev-level.
6623 ((> cur-level prev-level)
6624 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
6625 do (org-do-promote))))
6626 t))))
6628 (defun org-map-tree (fun)
6629 "Call FUN for every heading underneath the current one."
6630 (org-back-to-heading)
6631 (let ((level (funcall outline-level)))
6632 (save-excursion
6633 (funcall fun)
6634 (while (and (progn
6635 (outline-next-heading)
6636 (> (funcall outline-level) level))
6637 (not (eobp)))
6638 (funcall fun)))))
6640 (defun org-map-region (fun beg end)
6641 "Call FUN for every heading between BEG and END."
6642 (let ((org-ignore-region t))
6643 (save-excursion
6644 (setq end (copy-marker end))
6645 (goto-char beg)
6646 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
6647 (< (point) end))
6648 (funcall fun))
6649 (while (and (progn
6650 (outline-next-heading)
6651 (< (point) end))
6652 (not (eobp)))
6653 (funcall fun)))))
6655 (defun org-fixup-indentation (diff)
6656 "Change the indentation in the current entry by DIFF
6657 However, if any line in the current entry has no indentation, or if it
6658 would end up with no indentation after the change, nothing at all is done."
6659 (save-excursion
6660 (let ((end (save-excursion (outline-next-heading)
6661 (point-marker)))
6662 (prohibit (if (> diff 0)
6663 "^\\S-"
6664 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
6665 col)
6666 (unless (save-excursion (end-of-line 1)
6667 (re-search-forward prohibit end t))
6668 (while (and (< (point) end)
6669 (re-search-forward "^[ \t]+" end t))
6670 (goto-char (match-end 0))
6671 (setq col (current-column))
6672 (if (< diff 0) (replace-match ""))
6673 (org-indent-to-column (+ diff col))))
6674 (move-marker end nil))))
6676 (defun org-convert-to-odd-levels ()
6677 "Convert an org-mode file with all levels allowed to one with odd levels.
6678 This will leave level 1 alone, convert level 2 to level 3, level 3 to
6679 level 5 etc."
6680 (interactive)
6681 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
6682 (let ((outline-regexp org-outline-regexp)
6683 (outline-level 'org-outline-level)
6684 (org-odd-levels-only nil) n)
6685 (save-excursion
6686 (goto-char (point-min))
6687 (while (re-search-forward "^\\*\\*+ " nil t)
6688 (setq n (- (length (match-string 0)) 2))
6689 (while (>= (setq n (1- n)) 0)
6690 (org-demote))
6691 (end-of-line 1))))))
6693 (defun org-convert-to-oddeven-levels ()
6694 "Convert an org-mode file with only odd levels to one with odd and even levels.
6695 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
6696 section with an even level, conversion would destroy the structure of the file. An error
6697 is signaled in this case."
6698 (interactive)
6699 (goto-char (point-min))
6700 ;; First check if there are no even levels
6701 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
6702 (org-show-context t)
6703 (error "Not all levels are odd in this file. Conversion not possible"))
6704 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
6705 (let ((outline-regexp org-outline-regexp)
6706 (outline-level 'org-outline-level)
6707 (org-odd-levels-only nil) n)
6708 (save-excursion
6709 (goto-char (point-min))
6710 (while (re-search-forward "^\\*\\*+ " nil t)
6711 (setq n (/ (1- (length (match-string 0))) 2))
6712 (while (>= (setq n (1- n)) 0)
6713 (org-promote))
6714 (end-of-line 1))))))
6716 (defun org-tr-level (n)
6717 "Make N odd if required."
6718 (if org-odd-levels-only (1+ (/ n 2)) n))
6720 ;;; Vertical tree motion, cutting and pasting of subtrees
6722 (defun org-move-subtree-up (&optional arg)
6723 "Move the current subtree up past ARG headlines of the same level."
6724 (interactive "p")
6725 (org-move-subtree-down (- (prefix-numeric-value arg))))
6727 (defun org-move-subtree-down (&optional arg)
6728 "Move the current subtree down past ARG headlines of the same level."
6729 (interactive "p")
6730 (setq arg (prefix-numeric-value arg))
6731 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
6732 'org-get-last-sibling))
6733 (ins-point (make-marker))
6734 (cnt (abs arg))
6735 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
6736 ;; Select the tree
6737 (org-back-to-heading)
6738 (setq beg0 (point))
6739 (save-excursion
6740 (setq ne-beg (org-back-over-empty-lines))
6741 (setq beg (point)))
6742 (save-match-data
6743 (save-excursion (outline-end-of-heading)
6744 (setq folded (org-invisible-p)))
6745 (outline-end-of-subtree))
6746 (outline-next-heading)
6747 (setq ne-end (org-back-over-empty-lines))
6748 (setq end (point))
6749 (goto-char beg0)
6750 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
6751 ;; include less whitespace
6752 (save-excursion
6753 (goto-char beg)
6754 (forward-line (- ne-beg ne-end))
6755 (setq beg (point))))
6756 ;; Find insertion point, with error handling
6757 (while (> cnt 0)
6758 (or (and (funcall movfunc) (looking-at outline-regexp))
6759 (progn (goto-char beg0)
6760 (error "Cannot move past superior level or buffer limit")))
6761 (setq cnt (1- cnt)))
6762 (if (> arg 0)
6763 ;; Moving forward - still need to move over subtree
6764 (progn (org-end-of-subtree t t)
6765 (save-excursion
6766 (org-back-over-empty-lines)
6767 (or (bolp) (newline)))))
6768 (setq ne-ins (org-back-over-empty-lines))
6769 (move-marker ins-point (point))
6770 (setq txt (buffer-substring beg end))
6771 (org-save-markers-in-region beg end)
6772 (delete-region beg end)
6773 (org-remove-empty-overlays-at beg)
6774 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
6775 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
6776 (and (not (bolp)) (looking-at "\n") (forward-char 1))
6777 (let ((bbb (point)))
6778 (insert-before-markers txt)
6779 (org-reinstall-markers-in-region bbb)
6780 (move-marker ins-point bbb))
6781 (or (bolp) (insert "\n"))
6782 (setq ins-end (point))
6783 (goto-char ins-point)
6784 (org-skip-whitespace)
6785 (when (and (< arg 0)
6786 (org-first-sibling-p)
6787 (> ne-ins ne-beg))
6788 ;; Move whitespace back to beginning
6789 (save-excursion
6790 (goto-char ins-end)
6791 (let ((kill-whole-line t))
6792 (kill-line (- ne-ins ne-beg)) (point)))
6793 (insert (make-string (- ne-ins ne-beg) ?\n)))
6794 (move-marker ins-point nil)
6795 (if folded
6796 (hide-subtree)
6797 (org-show-entry)
6798 (show-children)
6799 (org-cycle-hide-drawers 'children))
6800 (org-clean-visibility-after-subtree-move)))
6802 (defvar org-subtree-clip ""
6803 "Clipboard for cut and paste of subtrees.
6804 This is actually only a copy of the kill, because we use the normal kill
6805 ring. We need it to check if the kill was created by `org-copy-subtree'.")
6807 (defvar org-subtree-clip-folded nil
6808 "Was the last copied subtree folded?
6809 This is used to fold the tree back after pasting.")
6811 (defun org-cut-subtree (&optional n)
6812 "Cut the current subtree into the clipboard.
6813 With prefix arg N, cut this many sequential subtrees.
6814 This is a short-hand for marking the subtree and then cutting it."
6815 (interactive "p")
6816 (org-copy-subtree n 'cut))
6818 (defun org-copy-subtree (&optional n cut force-store-markers)
6819 "Cut the current subtree into the clipboard.
6820 With prefix arg N, cut this many sequential subtrees.
6821 This is a short-hand for marking the subtree and then copying it.
6822 If CUT is non-nil, actually cut the subtree.
6823 If FORCE-STORE-MARKERS is non-nil, store the relative locations
6824 of some markers in the region, even if CUT is non-nil. This is
6825 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
6826 (interactive "p")
6827 (let (beg end folded (beg0 (point)))
6828 (if (interactive-p)
6829 (org-back-to-heading nil) ; take what looks like a subtree
6830 (org-back-to-heading t)) ; take what is really there
6831 (org-back-over-empty-lines)
6832 (setq beg (point))
6833 (skip-chars-forward " \t\r\n")
6834 (save-match-data
6835 (save-excursion (outline-end-of-heading)
6836 (setq folded (org-invisible-p)))
6837 (condition-case nil
6838 (org-forward-same-level (1- n) t)
6839 (error nil))
6840 (org-end-of-subtree t t))
6841 (org-back-over-empty-lines)
6842 (setq end (point))
6843 (goto-char beg0)
6844 (when (> end beg)
6845 (setq org-subtree-clip-folded folded)
6846 (when (or cut force-store-markers)
6847 (org-save-markers-in-region beg end))
6848 (if cut (kill-region beg end) (copy-region-as-kill beg end))
6849 (setq org-subtree-clip (current-kill 0))
6850 (message "%s: Subtree(s) with %d characters"
6851 (if cut "Cut" "Copied")
6852 (length org-subtree-clip)))))
6854 (defun org-paste-subtree (&optional level tree for-yank)
6855 "Paste the clipboard as a subtree, with modification of headline level.
6856 The entire subtree is promoted or demoted in order to match a new headline
6857 level.
6859 If the cursor is at the beginning of a headline, the same level as
6860 that headline is used to paste the tree
6862 If not, the new level is derived from the *visible* headings
6863 before and after the insertion point, and taken to be the inferior headline
6864 level of the two. So if the previous visible heading is level 3 and the
6865 next is level 4 (or vice versa), level 4 will be used for insertion.
6866 This makes sure that the subtree remains an independent subtree and does
6867 not swallow low level entries.
6869 You can also force a different level, either by using a numeric prefix
6870 argument, or by inserting the heading marker by hand. For example, if the
6871 cursor is after \"*****\", then the tree will be shifted to level 5.
6873 If optional TREE is given, use this text instead of the kill ring.
6875 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
6876 move back over whitespace before inserting, and move point to the end of
6877 the inserted text when done."
6878 (interactive "P")
6879 (setq tree (or tree (and kill-ring (current-kill 0))))
6880 (unless (org-kill-is-subtree-p tree)
6881 (error "%s"
6882 (substitute-command-keys
6883 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
6884 (let* ((visp (not (org-invisible-p)))
6885 (txt tree)
6886 (^re (concat "^\\(" outline-regexp "\\)"))
6887 (re (concat "\\(" outline-regexp "\\)"))
6888 (^re_ (concat "\\(\\*+\\)[ \t]*"))
6890 (old-level (if (string-match ^re txt)
6891 (- (match-end 0) (match-beginning 0) 1)
6892 -1))
6893 (force-level (cond (level (prefix-numeric-value level))
6894 ((and (looking-at "[ \t]*$")
6895 (string-match
6896 ^re_ (buffer-substring
6897 (point-at-bol) (point))))
6898 (- (match-end 1) (match-beginning 1)))
6899 ((and (bolp)
6900 (looking-at org-outline-regexp))
6901 (- (match-end 0) (point) 1))
6902 (t nil)))
6903 (previous-level (save-excursion
6904 (condition-case nil
6905 (progn
6906 (outline-previous-visible-heading 1)
6907 (if (looking-at re)
6908 (- (match-end 0) (match-beginning 0) 1)
6910 (error 1))))
6911 (next-level (save-excursion
6912 (condition-case nil
6913 (progn
6914 (or (looking-at outline-regexp)
6915 (outline-next-visible-heading 1))
6916 (if (looking-at re)
6917 (- (match-end 0) (match-beginning 0) 1)
6919 (error 1))))
6920 (new-level (or force-level (max previous-level next-level)))
6921 (shift (if (or (= old-level -1)
6922 (= new-level -1)
6923 (= old-level new-level))
6925 (- new-level old-level)))
6926 (delta (if (> shift 0) -1 1))
6927 (func (if (> shift 0) 'org-demote 'org-promote))
6928 (org-odd-levels-only nil)
6929 beg end newend)
6930 ;; Remove the forced level indicator
6931 (if force-level
6932 (delete-region (point-at-bol) (point)))
6933 ;; Paste
6934 (beginning-of-line 1)
6935 (unless for-yank (org-back-over-empty-lines))
6936 (setq beg (point))
6937 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
6938 (insert-before-markers txt)
6939 (unless (string-match "\n\\'" txt) (insert "\n"))
6940 (setq newend (point))
6941 (org-reinstall-markers-in-region beg)
6942 (setq end (point))
6943 (goto-char beg)
6944 (skip-chars-forward " \t\n\r")
6945 (setq beg (point))
6946 (if (and (org-invisible-p) visp)
6947 (save-excursion (outline-show-heading)))
6948 ;; Shift if necessary
6949 (unless (= shift 0)
6950 (save-restriction
6951 (narrow-to-region beg end)
6952 (while (not (= shift 0))
6953 (org-map-region func (point-min) (point-max))
6954 (setq shift (+ delta shift)))
6955 (goto-char (point-min))
6956 (setq newend (point-max))))
6957 (when (or (interactive-p) for-yank)
6958 (message "Clipboard pasted as level %d subtree" new-level))
6959 (if (and (not for-yank) ; in this case, org-yank will decide about folding
6960 kill-ring
6961 (eq org-subtree-clip (current-kill 0))
6962 org-subtree-clip-folded)
6963 ;; The tree was folded before it was killed/copied
6964 (hide-subtree))
6965 (and for-yank (goto-char newend))))
6967 (defun org-kill-is-subtree-p (&optional txt)
6968 "Check if the current kill is an outline subtree, or a set of trees.
6969 Returns nil if kill does not start with a headline, or if the first
6970 headline level is not the largest headline level in the tree.
6971 So this will actually accept several entries of equal levels as well,
6972 which is OK for `org-paste-subtree'.
6973 If optional TXT is given, check this string instead of the current kill."
6974 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
6975 (start-level (and kill
6976 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
6977 org-outline-regexp "\\)")
6978 kill)
6979 (- (match-end 2) (match-beginning 2) 1)))
6980 (re (concat "^" org-outline-regexp))
6981 (start (1+ (or (match-beginning 2) -1))))
6982 (if (not start-level)
6983 (progn
6984 nil) ;; does not even start with a heading
6985 (catch 'exit
6986 (while (setq start (string-match re kill (1+ start)))
6987 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
6988 (throw 'exit nil)))
6989 t))))
6991 (defvar org-markers-to-move nil
6992 "Markers that should be moved with a cut-and-paste operation.
6993 Those markers are stored together with their positions relative to
6994 the start of the region.")
6996 (defun org-save-markers-in-region (beg end)
6997 "Check markers in region.
6998 If these markers are between BEG and END, record their position relative
6999 to BEG, so that after moving the block of text, we can put the markers back
7000 into place.
7001 This function gets called just before an entry or tree gets cut from the
7002 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
7003 called immediately, to move the markers with the entries."
7004 (setq org-markers-to-move nil)
7005 (when (featurep 'org-clock)
7006 (org-clock-save-markers-for-cut-and-paste beg end))
7007 (when (featurep 'org-agenda)
7008 (org-agenda-save-markers-for-cut-and-paste beg end)))
7010 (defun org-check-and-save-marker (marker beg end)
7011 "Check if MARKER is between BEG and END.
7012 If yes, remember the marker and the distance to BEG."
7013 (when (and (marker-buffer marker)
7014 (equal (marker-buffer marker) (current-buffer)))
7015 (if (and (>= marker beg) (< marker end))
7016 (push (cons marker (- marker beg)) org-markers-to-move))))
7018 (defun org-reinstall-markers-in-region (beg)
7019 "Move all remembered markers to their position relative to BEG."
7020 (mapc (lambda (x)
7021 (move-marker (car x) (+ beg (cdr x))))
7022 org-markers-to-move)
7023 (setq org-markers-to-move nil))
7025 (defun org-narrow-to-subtree ()
7026 "Narrow buffer to the current subtree."
7027 (interactive)
7028 (save-excursion
7029 (save-match-data
7030 (narrow-to-region
7031 (progn (org-back-to-heading t) (point))
7032 (progn (org-end-of-subtree t t)
7033 (if (org-on-heading-p) (backward-char 1))
7034 (point))))))
7036 (defun org-clone-subtree-with-time-shift (n &optional shift)
7037 "Clone the task (subtree) at point N times.
7038 The clones will be inserted as siblings.
7040 In interactive use, the user will be prompted for the number of clones
7041 to be produced, and for a time SHIFT, which may be a repeater as used
7042 in time stamps, for example `+3d'.
7044 When a valid repeater is given and the entry contains any time stamps,
7045 the clones will become a sequence in time, with time stamps in the
7046 subtree shifted for each clone produced. If SHIFT is nil or the
7047 empty string, time stamps will be left alone.
7049 If the original subtree did contain time stamps with a repeater,
7050 the following will happen:
7051 - the repeater will be removed in each clone
7052 - an additional clone will be produced, with the current, unshifted
7053 date(s) in the entry.
7054 - the original entry will be placed *after* all the clones, with
7055 repeater intact.
7056 - the start days in the repeater in the original entry will be shifted
7057 to past the last clone.
7058 I this way you can spell out a number of instances of a repeating task,
7059 and still retain the repeater to cover future instances of the task."
7060 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
7061 (let (beg end template task
7062 shift-n shift-what doshift nmin nmax (n-no-remove -1))
7063 (if (not (and (integerp n) (> n 0)))
7064 (error "Invalid number of replications %s" n))
7065 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
7066 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
7067 shift)))
7068 (error "Invalid shift specification %s" shift))
7069 (when doshift
7070 (setq shift-n (string-to-number (match-string 1 shift))
7071 shift-what (cdr (assoc (match-string 2 shift)
7072 '(("d" . day) ("w" . week)
7073 ("m" . month) ("y" . year))))))
7074 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
7075 (setq nmin 1 nmax n)
7076 (org-back-to-heading t)
7077 (setq beg (point))
7078 (org-end-of-subtree t t)
7079 (or (bolp) (insert "\n"))
7080 (setq end (point))
7081 (setq template (buffer-substring beg end))
7082 (when (and doshift
7083 (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
7084 (delete-region beg end)
7085 (setq end beg)
7086 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
7087 (goto-char end)
7088 (loop for n from nmin to nmax do
7089 (if (not doshift)
7090 (setq task template)
7091 (with-temp-buffer
7092 (insert template)
7093 (org-mode)
7094 (goto-char (point-min))
7095 (while (re-search-forward org-ts-regexp-both nil t)
7096 (org-timestamp-change (* n shift-n) shift-what))
7097 (unless (= n n-no-remove)
7098 (goto-char (point-min))
7099 (while (re-search-forward org-ts-regexp nil t)
7100 (save-excursion
7101 (goto-char (match-beginning 0))
7102 (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
7103 (delete-region (match-beginning 1) (match-end 1))))))
7104 (setq task (buffer-string))))
7105 (insert task))
7106 (goto-char beg)))
7108 ;;; Outline Sorting
7110 (defun org-sort (with-case)
7111 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
7112 Optional argument WITH-CASE means sort case-sensitively.
7113 With a double prefix argument, also remove duplicate entries."
7114 (interactive "P")
7115 (if (org-at-table-p)
7116 (org-call-with-arg 'org-table-sort-lines with-case)
7117 (org-call-with-arg 'org-sort-entries-or-items with-case)))
7119 (defun org-sort-remove-invisible (s)
7120 (remove-text-properties 0 (length s) org-rm-props s)
7121 (while (string-match org-bracket-link-regexp s)
7122 (setq s (replace-match (if (match-end 2)
7123 (match-string 3 s)
7124 (match-string 1 s)) t t s)))
7127 (defvar org-priority-regexp) ; defined later in the file
7129 (defvar org-after-sorting-entries-or-items-hook nil
7130 "Hook that is run after a bunch of entries or items have been sorted.
7131 When children are sorted, the cursor is in the parent line when this
7132 hook gets called. When a region or a plain list is sorted, the cursor
7133 will be in the first entry of the sorted region/list.")
7135 (defun org-sort-entries-or-items
7136 (&optional with-case sorting-type getkey-func compare-func property)
7137 "Sort entries on a certain level of an outline tree, or plain list items.
7138 If there is an active region, the entries in the region are sorted.
7139 Else, if the cursor is before the first entry, sort the top-level items.
7140 Else, the children of the entry at point are sorted.
7141 If the cursor is at the first item in a plain list, the list items will be
7142 sorted.
7144 Sorting can be alphabetically, numerically, by date/time as given by
7145 a time stamp, by a property or by priority.
7147 The command prompts for the sorting type unless it has been given to the
7148 function through the SORTING-TYPE argument, which needs to a character,
7149 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
7150 precise meaning of each character:
7152 n Numerically, by converting the beginning of the entry/item to a number.
7153 a Alphabetically, ignoring the TODO keyword and the priority, if any.
7154 t By date/time, either the first active time stamp in the entry, or, if
7155 none exist, by the first inactive one.
7156 In items, only the first line will be checked.
7157 s By the scheduled date/time.
7158 d By deadline date/time.
7159 c By creation time, which is assumed to be the first inactive time stamp
7160 at the beginning of a line.
7161 p By priority according to the cookie.
7162 r By the value of a property.
7164 Capital letters will reverse the sort order.
7166 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
7167 called with point at the beginning of the record. It must return either
7168 a string or a number that should serve as the sorting key for that record.
7170 Comparing entries ignores case by default. However, with an optional argument
7171 WITH-CASE, the sorting considers case as well."
7172 (interactive "P")
7173 (let ((case-func (if with-case 'identity 'downcase))
7174 start beg end stars re re2
7175 txt what tmp plain-list-p)
7176 ;; Find beginning and end of region to sort
7177 (cond
7178 ((org-region-active-p)
7179 ;; we will sort the region
7180 (setq end (region-end)
7181 what "region")
7182 (goto-char (region-beginning))
7183 (if (not (org-on-heading-p)) (outline-next-heading))
7184 (setq start (point)))
7185 ((org-at-item-p)
7186 ;; we will sort this plain list
7187 (org-beginning-of-item-list) (setq start (point))
7188 (org-end-of-item-list)
7189 (or (bolp) (insert "\n"))
7190 (setq end (point))
7191 (goto-char start)
7192 (setq plain-list-p t
7193 what "plain list"))
7194 ((or (org-on-heading-p)
7195 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
7196 ;; we will sort the children of the current headline
7197 (org-back-to-heading)
7198 (setq start (point)
7199 end (progn (org-end-of-subtree t t)
7200 (or (bolp) (insert "\n"))
7201 (org-back-over-empty-lines)
7202 (point))
7203 what "children")
7204 (goto-char start)
7205 (show-subtree)
7206 (outline-next-heading))
7208 ;; we will sort the top-level entries in this file
7209 (goto-char (point-min))
7210 (or (org-on-heading-p) (outline-next-heading))
7211 (setq start (point))
7212 (goto-char (point-max))
7213 (beginning-of-line 1)
7214 (when (looking-at ".*?\\S-")
7215 ;; File ends in a non-white line
7216 (end-of-line 1)
7217 (insert "\n"))
7218 (setq end (point-max))
7219 (setq what "top-level")
7220 (goto-char start)
7221 (show-all)))
7223 (setq beg (point))
7224 (if (>= beg end) (error "Nothing to sort"))
7226 (unless plain-list-p
7227 (looking-at "\\(\\*+\\)")
7228 (setq stars (match-string 1)
7229 re (concat "^" (regexp-quote stars) " +")
7230 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
7231 txt (buffer-substring beg end))
7232 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
7233 (if (and (not (equal stars "*")) (string-match re2 txt))
7234 (error "Region to sort contains a level above the first entry")))
7236 (unless sorting-type
7237 (message
7238 (if plain-list-p
7239 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
7240 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
7241 [t]ime [s]cheduled [d]eadline [c]reated
7242 A/N/T/S/D/C/P/O/F means reversed:")
7243 what)
7244 (setq sorting-type (read-char-exclusive))
7246 (and (= (downcase sorting-type) ?f)
7247 (setq getkey-func
7248 (org-icompleting-read "Sort using function: "
7249 obarray 'fboundp t nil nil))
7250 (setq getkey-func (intern getkey-func)))
7252 (and (= (downcase sorting-type) ?r)
7253 (setq property
7254 (org-icompleting-read "Property: "
7255 (mapcar 'list (org-buffer-property-keys t))
7256 nil t))))
7258 (message "Sorting entries...")
7260 (save-restriction
7261 (narrow-to-region start end)
7263 (let ((dcst (downcase sorting-type))
7264 (case-fold-search nil)
7265 (now (current-time)))
7266 (sort-subr
7267 (/= dcst sorting-type)
7268 ;; This function moves to the beginning character of the "record" to
7269 ;; be sorted.
7270 (if plain-list-p
7271 (lambda nil
7272 (if (org-at-item-p) t (goto-char (point-max))))
7273 (lambda nil
7274 (if (re-search-forward re nil t)
7275 (goto-char (match-beginning 0))
7276 (goto-char (point-max)))))
7277 ;; This function moves to the last character of the "record" being
7278 ;; sorted.
7279 (if plain-list-p
7280 'org-end-of-item
7281 (lambda nil
7282 (save-match-data
7283 (condition-case nil
7284 (outline-forward-same-level 1)
7285 (error
7286 (goto-char (point-max)))))))
7288 ;; This function returns the value that gets sorted against.
7289 (if plain-list-p
7290 (lambda nil
7291 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
7292 (cond
7293 ((= dcst ?n)
7294 (string-to-number (buffer-substring (match-end 0)
7295 (point-at-eol))))
7296 ((= dcst ?a)
7297 (buffer-substring (match-end 0) (point-at-eol)))
7298 ((= dcst ?t)
7299 (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
7300 (re-search-forward org-ts-regexp-both
7301 (point-at-eol) t))
7302 (org-time-string-to-seconds (match-string 0))
7303 (org-float-time now)))
7304 ((= dcst ?f)
7305 (if getkey-func
7306 (progn
7307 (setq tmp (funcall getkey-func))
7308 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
7309 tmp)
7310 (error "Invalid key function `%s'" getkey-func)))
7311 (t (error "Invalid sorting type `%c'" sorting-type)))))
7312 (lambda nil
7313 (cond
7314 ((= dcst ?n)
7315 (if (looking-at org-complex-heading-regexp)
7316 (string-to-number (match-string 4))
7317 nil))
7318 ((= dcst ?a)
7319 (if (looking-at org-complex-heading-regexp)
7320 (funcall case-func (match-string 4))
7321 nil))
7322 ((= dcst ?t)
7323 (let ((end (save-excursion (outline-next-heading) (point))))
7324 (if (or (re-search-forward org-ts-regexp end t)
7325 (re-search-forward org-ts-regexp-both end t))
7326 (org-time-string-to-seconds (match-string 0))
7327 (org-float-time now))))
7328 ((= dcst ?c)
7329 (let ((end (save-excursion (outline-next-heading) (point))))
7330 (if (re-search-forward
7331 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
7332 end t)
7333 (org-time-string-to-seconds (match-string 0))
7334 (org-float-time now))))
7335 ((= dcst ?s)
7336 (let ((end (save-excursion (outline-next-heading) (point))))
7337 (if (re-search-forward org-scheduled-time-regexp end t)
7338 (org-time-string-to-seconds (match-string 1))
7339 (org-float-time now))))
7340 ((= dcst ?d)
7341 (let ((end (save-excursion (outline-next-heading) (point))))
7342 (if (re-search-forward org-deadline-time-regexp end t)
7343 (org-time-string-to-seconds (match-string 1))
7344 (org-float-time now))))
7345 ((= dcst ?p)
7346 (if (re-search-forward org-priority-regexp (point-at-eol) t)
7347 (string-to-char (match-string 2))
7348 org-default-priority))
7349 ((= dcst ?r)
7350 (or (org-entry-get nil property) ""))
7351 ((= dcst ?o)
7352 (if (looking-at org-complex-heading-regexp)
7353 (- 9999 (length (member (match-string 2)
7354 org-todo-keywords-1)))))
7355 ((= dcst ?f)
7356 (if getkey-func
7357 (progn
7358 (setq tmp (funcall getkey-func))
7359 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
7360 tmp)
7361 (error "Invalid key function `%s'" getkey-func)))
7362 (t (error "Invalid sorting type `%c'" sorting-type)))))
7364 (cond
7365 ((= dcst ?a) 'string<)
7366 ((= dcst ?f) compare-func)
7367 ((member dcst '(?p ?t ?s ?d ?c)) '<)
7368 (t nil)))))
7369 (run-hooks 'org-after-sorting-entries-or-items-hook)
7370 (message "Sorting entries...done")))
7372 (defun org-do-sort (table what &optional with-case sorting-type)
7373 "Sort TABLE of WHAT according to SORTING-TYPE.
7374 The user will be prompted for the SORTING-TYPE if the call to this
7375 function does not specify it. WHAT is only for the prompt, to indicate
7376 what is being sorted. The sorting key will be extracted from
7377 the car of the elements of the table.
7378 If WITH-CASE is non-nil, the sorting will be case-sensitive."
7379 (unless sorting-type
7380 (message
7381 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
7382 what)
7383 (setq sorting-type (read-char-exclusive)))
7384 (let ((dcst (downcase sorting-type))
7385 extractfun comparefun)
7386 ;; Define the appropriate functions
7387 (cond
7388 ((= dcst ?n)
7389 (setq extractfun 'string-to-number
7390 comparefun (if (= dcst sorting-type) '< '>)))
7391 ((= dcst ?a)
7392 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
7393 (lambda(x) (downcase (org-sort-remove-invisible x))))
7394 comparefun (if (= dcst sorting-type)
7395 'string<
7396 (lambda (a b) (and (not (string< a b))
7397 (not (string= a b)))))))
7398 ((= dcst ?t)
7399 (setq extractfun
7400 (lambda (x)
7401 (if (or (string-match org-ts-regexp x)
7402 (string-match org-ts-regexp-both x))
7403 (org-float-time
7404 (org-time-string-to-time (match-string 0 x)))
7406 comparefun (if (= dcst sorting-type) '< '>)))
7407 (t (error "Invalid sorting type `%c'" sorting-type)))
7409 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
7410 table)
7411 (lambda (a b) (funcall comparefun (car a) (car b))))))
7414 ;;; The orgstruct minor mode
7416 ;; Define a minor mode which can be used in other modes in order to
7417 ;; integrate the org-mode structure editing commands.
7419 ;; This is really a hack, because the org-mode structure commands use
7420 ;; keys which normally belong to the major mode. Here is how it
7421 ;; works: The minor mode defines all the keys necessary to operate the
7422 ;; structure commands, but wraps the commands into a function which
7423 ;; tests if the cursor is currently at a headline or a plain list
7424 ;; item. If that is the case, the structure command is used,
7425 ;; temporarily setting many Org-mode variables like regular
7426 ;; expressions for filling etc. However, when any of those keys is
7427 ;; used at a different location, function uses `key-binding' to look
7428 ;; up if the key has an associated command in another currently active
7429 ;; keymap (minor modes, major mode, global), and executes that
7430 ;; command. There might be problems if any of the keys is otherwise
7431 ;; used as a prefix key.
7433 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
7434 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
7435 ;; addresses this by checking explicitly for both bindings.
7437 (defvar orgstruct-mode-map (make-sparse-keymap)
7438 "Keymap for the minor `orgstruct-mode'.")
7440 (defvar org-local-vars nil
7441 "List of local variables, for use by `orgstruct-mode'")
7443 ;;;###autoload
7444 (define-minor-mode orgstruct-mode
7445 "Toggle the minor more `orgstruct-mode'.
7446 This mode is for using Org-mode structure commands in other modes.
7447 The following key behave as if Org-mode was active, if the cursor
7448 is on a headline, or on a plain list item (both in the definition
7449 of Org-mode).
7451 M-up Move entry/item up
7452 M-down Move entry/item down
7453 M-left Promote
7454 M-right Demote
7455 M-S-up Move entry/item up
7456 M-S-down Move entry/item down
7457 M-S-left Promote subtree
7458 M-S-right Demote subtree
7459 M-q Fill paragraph and items like in Org-mode
7460 C-c ^ Sort entries
7461 C-c - Cycle list bullet
7462 TAB Cycle item visibility
7463 M-RET Insert new heading/item
7464 S-M-RET Insert new TODO heading / Checkbox item
7465 C-c C-c Set tags / toggle checkbox"
7466 nil " OrgStruct" nil
7467 (org-load-modules-maybe)
7468 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
7470 ;;;###autoload
7471 (defun turn-on-orgstruct ()
7472 "Unconditionally turn on `orgstruct-mode'."
7473 (orgstruct-mode 1))
7475 (defun orgstruct++-mode (&optional arg)
7476 "Toggle `orgstruct-mode', the enhanced version of it.
7477 In addition to setting orgstruct-mode, this also exports all indentation
7478 and autofilling variables from org-mode into the buffer. It will also
7479 recognize item context in multiline items.
7480 Note that turning off orgstruct-mode will *not* remove the
7481 indentation/paragraph settings. This can only be done by refreshing the
7482 major mode, for example with \\[normal-mode]."
7483 (interactive "P")
7484 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
7485 (if (< arg 1)
7486 (orgstruct-mode -1)
7487 (orgstruct-mode 1)
7488 (let (var val)
7489 (mapc
7490 (lambda (x)
7491 (when (string-match
7492 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7493 (symbol-name (car x)))
7494 (setq var (car x) val (nth 1 x))
7495 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
7496 org-local-vars)
7497 (org-set-local 'orgstruct-is-++ t))))
7499 (defvar orgstruct-is-++ nil
7500 "Is orgstruct-mode in ++ version in the current-buffer?")
7501 (make-variable-buffer-local 'orgstruct-is-++)
7503 ;;;###autoload
7504 (defun turn-on-orgstruct++ ()
7505 "Unconditionally turn on `orgstruct++-mode'."
7506 (orgstruct++-mode 1))
7508 (defun orgstruct-error ()
7509 "Error when there is no default binding for a structure key."
7510 (interactive)
7511 (error "This key has no function outside structure elements"))
7513 (defun orgstruct-setup ()
7514 "Setup orgstruct keymaps."
7515 (let ((nfunc 0)
7516 (bindings
7517 (list
7518 '([(meta up)] org-metaup)
7519 '([(meta down)] org-metadown)
7520 '([(meta left)] org-metaleft)
7521 '([(meta right)] org-metaright)
7522 '([(meta shift up)] org-shiftmetaup)
7523 '([(meta shift down)] org-shiftmetadown)
7524 '([(meta shift left)] org-shiftmetaleft)
7525 '([(meta shift right)] org-shiftmetaright)
7526 '([?\e (up)] org-metaup)
7527 '([?\e (down)] org-metadown)
7528 '([?\e (left)] org-metaleft)
7529 '([?\e (right)] org-metaright)
7530 '([?\e (shift up)] org-shiftmetaup)
7531 '([?\e (shift down)] org-shiftmetadown)
7532 '([?\e (shift left)] org-shiftmetaleft)
7533 '([?\e (shift right)] org-shiftmetaright)
7534 '([(shift up)] org-shiftup)
7535 '([(shift down)] org-shiftdown)
7536 '([(shift left)] org-shiftleft)
7537 '([(shift right)] org-shiftright)
7538 '("\C-c\C-c" org-ctrl-c-ctrl-c)
7539 '("\M-q" fill-paragraph)
7540 '("\C-c^" org-sort)
7541 '("\C-c-" org-cycle-list-bullet)))
7542 elt key fun cmd)
7543 (while (setq elt (pop bindings))
7544 (setq nfunc (1+ nfunc))
7545 (setq key (org-key (car elt))
7546 fun (nth 1 elt)
7547 cmd (orgstruct-make-binding fun nfunc key))
7548 (org-defkey orgstruct-mode-map key cmd))
7550 ;; Special treatment needed for TAB and RET
7551 (org-defkey orgstruct-mode-map [(tab)]
7552 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
7553 (org-defkey orgstruct-mode-map "\C-i"
7554 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
7556 (org-defkey orgstruct-mode-map "\M-\C-m"
7557 (orgstruct-make-binding 'org-insert-heading 105
7558 "\M-\C-m" [(meta return)]))
7559 (org-defkey orgstruct-mode-map [(meta return)]
7560 (orgstruct-make-binding 'org-insert-heading 106
7561 [(meta return)] "\M-\C-m"))
7563 (org-defkey orgstruct-mode-map [(shift meta return)]
7564 (orgstruct-make-binding 'org-insert-todo-heading 107
7565 [(meta return)] "\M-\C-m"))
7567 (org-defkey orgstruct-mode-map "\e\C-m"
7568 (orgstruct-make-binding 'org-insert-heading 108
7569 "\e\C-m" [?\e (return)]))
7570 (org-defkey orgstruct-mode-map [?\e (return)]
7571 (orgstruct-make-binding 'org-insert-heading 109
7572 [?\e (return)] "\e\C-m"))
7573 (org-defkey orgstruct-mode-map [?\e (shift return)]
7574 (orgstruct-make-binding 'org-insert-todo-heading 110
7575 [?\e (return)] "\e\C-m"))
7577 (unless org-local-vars
7578 (setq org-local-vars (org-get-local-variables)))
7582 (defun orgstruct-make-binding (fun n &rest keys)
7583 "Create a function for binding in the structure minor mode.
7584 FUN is the command to call inside a table. N is used to create a unique
7585 command name. KEYS are keys that should be checked in for a command
7586 to execute outside of tables."
7587 (eval
7588 (list 'defun
7589 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
7590 '(arg)
7591 (concat "In Structure, run `" (symbol-name fun) "'.\n"
7592 "Outside of structure, run the binding of `"
7593 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
7594 "'.")
7595 '(interactive "p")
7596 (list 'if
7597 `(org-context-p 'headline 'item
7598 (and orgstruct-is-++
7599 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
7600 'item-body))
7601 (list 'org-run-like-in-org-mode (list 'quote fun))
7602 (list 'let '(orgstruct-mode)
7603 (list 'call-interactively
7604 (append '(or)
7605 (mapcar (lambda (k)
7606 (list 'key-binding k))
7607 keys)
7608 '('orgstruct-error))))))))
7610 (defun org-context-p (&rest contexts)
7611 "Check if local context is any of CONTEXTS.
7612 Possible values in the list of contexts are `table', `headline', and `item'."
7613 (let ((pos (point)))
7614 (goto-char (point-at-bol))
7615 (prog1 (or (and (memq 'table contexts)
7616 (looking-at "[ \t]*|"))
7617 (and (memq 'headline contexts)
7618 ;;????????? (looking-at "\\*+"))
7619 (looking-at outline-regexp))
7620 (and (memq 'item contexts)
7621 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
7622 (and (memq 'item-body contexts)
7623 (org-in-item-p)))
7624 (goto-char pos))))
7626 (defun org-get-local-variables ()
7627 "Return a list of all local variables in an org-mode buffer."
7628 (let (varlist)
7629 (with-current-buffer (get-buffer-create "*Org tmp*")
7630 (erase-buffer)
7631 (org-mode)
7632 (setq varlist (buffer-local-variables)))
7633 (kill-buffer "*Org tmp*")
7634 (delq nil
7635 (mapcar
7636 (lambda (x)
7637 (setq x
7638 (if (symbolp x)
7639 (list x)
7640 (list (car x) (list 'quote (cdr x)))))
7641 (if (string-match
7642 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7643 (symbol-name (car x)))
7644 x nil))
7645 varlist))))
7647 ;;;###autoload
7648 (defun org-run-like-in-org-mode (cmd)
7649 "Run a command, pretending that the current buffer is in Org-mode.
7650 This will temporarily bind local variables that are typically bound in
7651 Org-mode to the values they have in Org-mode, and then interactively
7652 call CMD."
7653 (org-load-modules-maybe)
7654 (unless org-local-vars
7655 (setq org-local-vars (org-get-local-variables)))
7656 (eval (list 'let org-local-vars
7657 (list 'call-interactively (list 'quote cmd)))))
7659 ;;;; Archiving
7661 (defun org-get-category (&optional pos)
7662 "Get the category applying to position POS."
7663 (get-text-property (or pos (point)) 'org-category))
7665 (defun org-refresh-category-properties ()
7666 "Refresh category text properties in the buffer."
7667 (let ((def-cat (cond
7668 ((null org-category)
7669 (if buffer-file-name
7670 (file-name-sans-extension
7671 (file-name-nondirectory buffer-file-name))
7672 "???"))
7673 ((symbolp org-category) (symbol-name org-category))
7674 (t org-category)))
7675 beg end cat pos optionp)
7676 (org-unmodified
7677 (save-excursion
7678 (save-restriction
7679 (widen)
7680 (goto-char (point-min))
7681 (put-text-property (point) (point-max) 'org-category def-cat)
7682 (while (re-search-forward
7683 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
7684 (setq pos (match-end 0)
7685 optionp (equal (char-after (match-beginning 0)) ?#)
7686 cat (org-trim (match-string 2)))
7687 (if optionp
7688 (setq beg (point-at-bol) end (point-max))
7689 (org-back-to-heading t)
7690 (setq beg (point) end (org-end-of-subtree t t)))
7691 (put-text-property beg end 'org-category cat)
7692 (goto-char pos)))))))
7695 ;;;; Link Stuff
7697 ;;; Link abbreviations
7699 (defun org-link-expand-abbrev (link)
7700 "Apply replacements as defined in `org-link-abbrev-alist."
7701 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
7702 (let* ((key (match-string 1 link))
7703 (as (or (assoc key org-link-abbrev-alist-local)
7704 (assoc key org-link-abbrev-alist)))
7705 (tag (and (match-end 2) (match-string 3 link)))
7706 rpl)
7707 (if (not as)
7708 link
7709 (setq rpl (cdr as))
7710 (cond
7711 ((symbolp rpl) (funcall rpl tag))
7712 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
7713 ((string-match "%h" rpl)
7714 (replace-match (url-hexify-string (or tag "")) t t rpl))
7715 (t (concat rpl tag)))))
7716 link))
7718 ;;; Storing and inserting links
7720 (defvar org-insert-link-history nil
7721 "Minibuffer history for links inserted with `org-insert-link'.")
7723 (defvar org-stored-links nil
7724 "Contains the links stored with `org-store-link'.")
7726 (defvar org-store-link-plist nil
7727 "Plist with info about the most recently link created with `org-store-link'.")
7729 (defvar org-link-protocols nil
7730 "Link protocols added to Org-mode using `org-add-link-type'.")
7732 (defvar org-store-link-functions nil
7733 "List of functions that are called to create and store a link.
7734 Each function will be called in turn until one returns a non-nil
7735 value. Each function should check if it is responsible for creating
7736 this link (for example by looking at the major mode).
7737 If not, it must exit and return nil.
7738 If yes, it should return a non-nil value after a calling
7739 `org-store-link-props' with a list of properties and values.
7740 Special properties are:
7742 :type The link prefix. like \"http\". This must be given.
7743 :link The link, like \"http://www.astro.uva.nl/~dominik\".
7744 This is obligatory as well.
7745 :description Optional default description for the second pair
7746 of brackets in an Org-mode link. The user can still change
7747 this when inserting this link into an Org-mode buffer.
7749 In addition to these, any additional properties can be specified
7750 and then used in remember templates.")
7752 (defun org-add-link-type (type &optional follow export)
7753 "Add TYPE to the list of `org-link-types'.
7754 Re-compute all regular expressions depending on `org-link-types'
7756 FOLLOW and EXPORT are two functions.
7758 FOLLOW should take the link path as the single argument and do whatever
7759 is necessary to follow the link, for example find a file or display
7760 a mail message.
7762 EXPORT should format the link path for export to one of the export formats.
7763 It should be a function accepting three arguments:
7765 path the path of the link, the text after the prefix (like \"http:\")
7766 desc the description of the link, if any, nil if there was no description
7767 format the export format, a symbol like `html' or `latex'.
7769 The function may use the FORMAT information to return different values
7770 depending on the format. The return value will be put literally into
7771 the exported file.
7772 Org-mode has a built-in default for exporting links. If you are happy with
7773 this default, there is no need to define an export function for the link
7774 type. For a simple example of an export function, see `org-bbdb.el'."
7775 (add-to-list 'org-link-types type t)
7776 (org-make-link-regexps)
7777 (if (assoc type org-link-protocols)
7778 (setcdr (assoc type org-link-protocols) (list follow export))
7779 (push (list type follow export) org-link-protocols)))
7781 (defvar org-agenda-buffer-name)
7783 ;;;###autoload
7784 (defun org-store-link (arg)
7785 "\\<org-mode-map>Store an org-link to the current location.
7786 This link is added to `org-stored-links' and can later be inserted
7787 into an org-buffer with \\[org-insert-link].
7789 For some link types, a prefix arg is interpreted:
7790 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
7791 For file links, arg negates `org-context-in-file-links'."
7792 (interactive "P")
7793 (org-load-modules-maybe)
7794 (setq org-store-link-plist nil) ; reset
7795 (let ((outline-regexp (org-get-limited-outline-regexp))
7796 link cpltxt desc description search txt custom-id)
7797 (cond
7799 ((run-hook-with-args-until-success 'org-store-link-functions)
7800 (setq link (plist-get org-store-link-plist :link)
7801 desc (or (plist-get org-store-link-plist :description) link)))
7803 ((equal (buffer-name) "*Org Edit Src Example*")
7804 (let (label gc)
7805 (while (or (not label)
7806 (save-excursion
7807 (save-restriction
7808 (widen)
7809 (goto-char (point-min))
7810 (re-search-forward
7811 (regexp-quote (format org-coderef-label-format label))
7812 nil t))))
7813 (when label (message "Label exists already") (sit-for 2))
7814 (setq label (read-string "Code line label: " label)))
7815 (end-of-line 1)
7816 (setq link (format org-coderef-label-format label))
7817 (setq gc (- 79 (length link)))
7818 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
7819 (insert link)
7820 (setq link (concat "(" label ")") desc nil)))
7822 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
7823 ;; We are in the agenda, link to referenced location
7824 (let ((m (or (get-text-property (point) 'org-hd-marker)
7825 (get-text-property (point) 'org-marker))))
7826 (when m
7827 (org-with-point-at m
7828 (call-interactively 'org-store-link)))))
7830 ((eq major-mode 'calendar-mode)
7831 (let ((cd (calendar-cursor-to-date)))
7832 (setq link
7833 (format-time-string
7834 (car org-time-stamp-formats)
7835 (apply 'encode-time
7836 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
7837 nil nil nil))))
7838 (org-store-link-props :type "calendar" :date cd)))
7840 ((eq major-mode 'w3-mode)
7841 (setq cpltxt (if (and (buffer-name)
7842 (not (string-match "Untitled" (buffer-name))))
7843 (buffer-name)
7844 (url-view-url t))
7845 link (org-make-link (url-view-url t)))
7846 (org-store-link-props :type "w3" :url (url-view-url t)))
7848 ((eq major-mode 'w3m-mode)
7849 (setq cpltxt (or w3m-current-title w3m-current-url)
7850 link (org-make-link w3m-current-url))
7851 (org-store-link-props :type "w3m" :url (url-view-url t)))
7853 ((setq search (run-hook-with-args-until-success
7854 'org-create-file-search-functions))
7855 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
7856 "::" search))
7857 (setq cpltxt (or description link)))
7859 ((eq major-mode 'image-mode)
7860 (setq cpltxt (concat "file:"
7861 (abbreviate-file-name buffer-file-name))
7862 link (org-make-link cpltxt))
7863 (org-store-link-props :type "image" :file buffer-file-name))
7865 ((eq major-mode 'dired-mode)
7866 ;; link to the file in the current line
7867 (let ((file (dired-get-filename nil t)))
7868 (setq file (if file
7869 (abbreviate-file-name
7870 (expand-file-name (dired-get-filename nil t)))
7871 ;; otherwise, no file so use current directory.
7872 default-directory))
7873 (setq cpltxt (concat "file:" file)
7874 link (org-make-link cpltxt))))
7876 ((and buffer-file-name (org-mode-p))
7877 (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
7878 (cond
7879 ((org-in-regexp "<<\\(.*?\\)>>")
7880 (setq cpltxt
7881 (concat "file:"
7882 (abbreviate-file-name buffer-file-name)
7883 "::" (match-string 1))
7884 link (org-make-link cpltxt)))
7885 ((and (featurep 'org-id)
7886 (or (eq org-link-to-org-use-id t)
7887 (and (eq org-link-to-org-use-id 'create-if-interactive)
7888 (interactive-p))
7889 (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
7890 (interactive-p)
7891 (not custom-id))
7892 (and org-link-to-org-use-id
7893 (condition-case nil
7894 (org-entry-get nil "ID")
7895 (error nil)))))
7896 ;; We can make a link using the ID.
7897 (setq link (condition-case nil
7898 (prog1 (org-id-store-link)
7899 (setq desc (plist-get org-store-link-plist
7900 :description)))
7901 (error
7902 ;; probably before first headline, link to file only
7903 (concat "file:"
7904 (abbreviate-file-name buffer-file-name))))))
7906 ;; Just link to current headline
7907 (setq cpltxt (concat "file:"
7908 (abbreviate-file-name buffer-file-name)))
7909 ;; Add a context search string
7910 (when (org-xor org-context-in-file-links arg)
7911 (setq txt (cond
7912 ((org-on-heading-p) nil)
7913 ((org-region-active-p)
7914 (buffer-substring (region-beginning) (region-end)))
7915 (t nil)))
7916 (when (or (null txt) (string-match "\\S-" txt))
7917 (setq cpltxt
7918 (concat cpltxt "::"
7919 (condition-case nil
7920 (org-make-org-heading-search-string txt)
7921 (error "")))
7922 desc (or (nth 4 (ignore-errors
7923 (org-heading-components))) "NONE"))))
7924 (if (string-match "::\\'" cpltxt)
7925 (setq cpltxt (substring cpltxt 0 -2)))
7926 (setq link (org-make-link cpltxt)))))
7928 ((buffer-file-name (buffer-base-buffer))
7929 ;; Just link to this file here.
7930 (setq cpltxt (concat "file:"
7931 (abbreviate-file-name
7932 (buffer-file-name (buffer-base-buffer)))))
7933 ;; Add a context string
7934 (when (org-xor org-context-in-file-links arg)
7935 (setq txt (if (org-region-active-p)
7936 (buffer-substring (region-beginning) (region-end))
7937 (buffer-substring (point-at-bol) (point-at-eol))))
7938 ;; Only use search option if there is some text.
7939 (when (string-match "\\S-" txt)
7940 (setq cpltxt
7941 (concat cpltxt "::" (org-make-org-heading-search-string txt))
7942 desc "NONE")))
7943 (setq link (org-make-link cpltxt)))
7945 ((interactive-p)
7946 (error "Cannot link to a buffer which is not visiting a file"))
7948 (t (setq link nil)))
7950 (if (consp link) (setq cpltxt (car link) link (cdr link)))
7951 (setq link (or link cpltxt)
7952 desc (or desc cpltxt))
7953 (if (equal desc "NONE") (setq desc nil))
7955 (if (and (or (interactive-p) executing-kbd-macro) link)
7956 (progn
7957 (setq org-stored-links
7958 (cons (list link desc) org-stored-links))
7959 (message "Stored: %s" (or desc link))
7960 (when custom-id
7961 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
7962 "::#" custom-id))
7963 (setq org-stored-links
7964 (cons (list link desc) org-stored-links))))
7965 (and link (org-make-link-string link desc)))))
7967 (defun org-store-link-props (&rest plist)
7968 "Store link properties, extract names and addresses."
7969 (let (x adr)
7970 (when (setq x (plist-get plist :from))
7971 (setq adr (mail-extract-address-components x))
7972 (setq plist (plist-put plist :fromname (car adr)))
7973 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
7974 (when (setq x (plist-get plist :to))
7975 (setq adr (mail-extract-address-components x))
7976 (setq plist (plist-put plist :toname (car adr)))
7977 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
7978 (let ((from (plist-get plist :from))
7979 (to (plist-get plist :to)))
7980 (when (and from to org-from-is-user-regexp)
7981 (setq plist
7982 (plist-put plist :fromto
7983 (if (string-match org-from-is-user-regexp from)
7984 (concat "to %t")
7985 (concat "from %f"))))))
7986 (setq org-store-link-plist plist))
7988 (defun org-add-link-props (&rest plist)
7989 "Add these properties to the link property list."
7990 (let (key value)
7991 (while plist
7992 (setq key (pop plist) value (pop plist))
7993 (setq org-store-link-plist
7994 (plist-put org-store-link-plist key value)))))
7996 (defun org-email-link-description (&optional fmt)
7997 "Return the description part of an email link.
7998 This takes information from `org-store-link-plist' and formats it
7999 according to FMT (default from `org-email-link-description-format')."
8000 (setq fmt (or fmt org-email-link-description-format))
8001 (let* ((p org-store-link-plist)
8002 (to (plist-get p :toaddress))
8003 (from (plist-get p :fromaddress))
8004 (table
8005 (list
8006 (cons "%c" (plist-get p :fromto))
8007 (cons "%F" (plist-get p :from))
8008 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
8009 (cons "%T" (plist-get p :to))
8010 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
8011 (cons "%s" (plist-get p :subject))
8012 (cons "%m" (plist-get p :message-id)))))
8013 (when (string-match "%c" fmt)
8014 ;; Check if the user wrote this message
8015 (if (and org-from-is-user-regexp from to
8016 (save-match-data (string-match org-from-is-user-regexp from)))
8017 (setq fmt (replace-match "to %t" t t fmt))
8018 (setq fmt (replace-match "from %f" t t fmt))))
8019 (org-replace-escapes fmt table)))
8021 (defun org-make-org-heading-search-string (&optional string heading)
8022 "Make search string for STRING or current headline."
8023 (interactive)
8024 (let ((s (or string (org-get-heading))))
8025 (unless (and string (not heading))
8026 ;; We are using a headline, clean up garbage in there.
8027 (if (string-match org-todo-regexp s)
8028 (setq s (replace-match "" t t s)))
8029 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
8030 (setq s (replace-match "" t t s)))
8031 (setq s (org-trim s))
8032 (if (string-match (concat "^\\(" org-quote-string "\\|"
8033 org-comment-string "\\)") s)
8034 (setq s (replace-match "" t t s)))
8035 (while (string-match org-ts-regexp s)
8036 (setq s (replace-match "" t t s))))
8037 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
8038 (setq s (replace-match " " t t s)))
8039 (or string (setq s (concat "*" s))) ; Add * for headlines
8040 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
8042 (defun org-make-link (&rest strings)
8043 "Concatenate STRINGS."
8044 (apply 'concat strings))
8046 (defun org-make-link-string (link &optional description)
8047 "Make a link with brackets, consisting of LINK and DESCRIPTION."
8048 (unless (string-match "\\S-" link)
8049 (error "Empty link"))
8050 (when (and description
8051 (stringp description)
8052 (not (string-match "\\S-" description)))
8053 (setq description nil))
8054 (when (stringp description)
8055 ;; Remove brackets from the description, they are fatal.
8056 (while (string-match "\\[" description)
8057 (setq description (replace-match "{" t t description)))
8058 (while (string-match "\\]" description)
8059 (setq description (replace-match "}" t t description))))
8060 (when (equal (org-link-escape link) description)
8061 ;; No description needed, it is identical
8062 (setq description nil))
8063 (when (and (not description)
8064 (not (equal link (org-link-escape link))))
8065 (setq description (org-extract-attributes link)))
8066 (concat "[[" (org-link-escape link) "]"
8067 (if description (concat "[" description "]") "")
8068 "]"))
8070 (defconst org-link-escape-chars
8071 '((?\ . "%20")
8072 (?\[ . "%5B")
8073 (?\] . "%5D")
8074 (?\340 . "%E0") ; `a
8075 (?\342 . "%E2") ; ^a
8076 (?\347 . "%E7") ; ,c
8077 (?\350 . "%E8") ; `e
8078 (?\351 . "%E9") ; 'e
8079 (?\352 . "%EA") ; ^e
8080 (?\356 . "%EE") ; ^i
8081 (?\364 . "%F4") ; ^o
8082 (?\371 . "%F9") ; `u
8083 (?\373 . "%FB") ; ^u
8084 (?\; . "%3B")
8085 ;; (?? . "%3F")
8086 (?= . "%3D")
8087 (?+ . "%2B")
8089 "Association list of escapes for some characters problematic in links.
8090 This is the list that is used for internal purposes.")
8092 (defvar org-url-encoding-use-url-hexify nil)
8094 (defconst org-link-escape-chars-browser
8095 '((?\ . "%20")) ; 32 for the SPC char
8096 "Association list of escapes for some characters problematic in links.
8097 This is the list that is used before handing over to the browser.")
8099 (defun org-link-escape (text &optional table)
8100 "Escape characters in TEXT that are problematic for links."
8101 (if (and org-url-encoding-use-url-hexify (not table))
8102 (url-hexify-string text)
8103 (setq table (or table org-link-escape-chars))
8104 (when text
8105 (let ((re (mapconcat (lambda (x) (regexp-quote
8106 (char-to-string (car x))))
8107 table "\\|")))
8108 (while (string-match re text)
8109 (setq text
8110 (replace-match
8111 (cdr (assoc (string-to-char (match-string 0 text))
8112 table))
8113 t t text)))
8114 text))))
8116 (defun org-link-unescape (text &optional table)
8117 "Reverse the action of `org-link-escape'."
8118 (if (and org-url-encoding-use-url-hexify (not table))
8119 (url-unhex-string text)
8120 (setq table (or table org-link-escape-chars))
8121 (when text
8122 (let ((case-fold-search t)
8123 (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
8124 table "\\|")))
8125 (while (string-match re text)
8126 (setq text
8127 (replace-match
8128 (char-to-string (car (rassoc (upcase (match-string 0 text))
8129 table)))
8130 t t text)))
8131 text))))
8133 (defun org-xor (a b)
8134 "Exclusive or."
8135 (if a (not b) b))
8137 (defun org-fixup-message-id-for-http (s)
8138 "Replace special characters in a message id, so it can be used in an http query."
8139 (while (string-match "<" s)
8140 (setq s (replace-match "%3C" t t s)))
8141 (while (string-match ">" s)
8142 (setq s (replace-match "%3E" t t s)))
8143 (while (string-match "@" s)
8144 (setq s (replace-match "%40" t t s)))
8147 ;;;###autoload
8148 (defun org-insert-link-global ()
8149 "Insert a link like Org-mode does.
8150 This command can be called in any mode to insert a link in Org-mode syntax."
8151 (interactive)
8152 (org-load-modules-maybe)
8153 (org-run-like-in-org-mode 'org-insert-link))
8155 (defun org-insert-link (&optional complete-file link-location)
8156 "Insert a link. At the prompt, enter the link.
8158 Completion can be used to insert any of the link protocol prefixes like
8159 http or ftp in use.
8161 The history can be used to select a link previously stored with
8162 `org-store-link'. When the empty string is entered (i.e. if you just
8163 press RET at the prompt), the link defaults to the most recently
8164 stored link. As SPC triggers completion in the minibuffer, you need to
8165 use M-SPC or C-q SPC to force the insertion of a space character.
8167 You will also be prompted for a description, and if one is given, it will
8168 be displayed in the buffer instead of the link.
8170 If there is already a link at point, this command will allow you to edit link
8171 and description parts.
8173 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
8174 be selected using completion. The path to the file will be relative to the
8175 current directory if the file is in the current directory or a subdirectory.
8176 Otherwise, the link will be the absolute path as completed in the minibuffer
8177 \(i.e. normally ~/path/to/file). You can configure this behavior using the
8178 option `org-link-file-path-type'.
8180 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
8181 the current directory or below.
8183 With three \\[universal-argument] prefixes, negate the meaning of
8184 `org-keep-stored-link-after-insertion'.
8186 If `org-make-link-description-function' is non-nil, this function will be
8187 called with the link target, and the result will be the default
8188 link description.
8190 If the LINK-LOCATION parameter is non-nil, this value will be
8191 used as the link location instead of reading one interactively."
8192 (interactive "P")
8193 (let* ((wcf (current-window-configuration))
8194 (region (if (org-region-active-p)
8195 (buffer-substring (region-beginning) (region-end))))
8196 (remove (and region (list (region-beginning) (region-end))))
8197 (desc region)
8198 tmphist ; byte-compile incorrectly complains about this
8199 (link link-location)
8200 entry file all-prefixes)
8201 (cond
8202 (link-location) ; specified by arg, just use it.
8203 ((org-in-regexp org-bracket-link-regexp 1)
8204 ;; We do have a link at point, and we are going to edit it.
8205 (setq remove (list (match-beginning 0) (match-end 0)))
8206 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
8207 (setq link (read-string "Link: "
8208 (org-link-unescape
8209 (org-match-string-no-properties 1)))))
8210 ((or (org-in-regexp org-angle-link-re)
8211 (org-in-regexp org-plain-link-re))
8212 ;; Convert to bracket link
8213 (setq remove (list (match-beginning 0) (match-end 0))
8214 link (read-string "Link: "
8215 (org-remove-angle-brackets (match-string 0)))))
8216 ((member complete-file '((4) (16)))
8217 ;; Completing read for file names.
8218 (setq link (org-file-complete-link complete-file)))
8220 ;; Read link, with completion for stored links.
8221 (with-output-to-temp-buffer "*Org Links*"
8222 (princ "Insert a link.
8223 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
8224 (when org-stored-links
8225 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
8226 (princ (mapconcat
8227 (lambda (x)
8228 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
8229 (reverse org-stored-links) "\n"))))
8230 (let ((cw (selected-window)))
8231 (select-window (get-buffer-window "*Org Links*" 'visible))
8232 (setq truncate-lines t)
8233 (unless (pos-visible-in-window-p (point-max))
8234 (org-fit-window-to-buffer))
8235 (and (window-live-p cw) (select-window cw)))
8236 ;; Fake a link history, containing the stored links.
8237 (setq tmphist (append (mapcar 'car org-stored-links)
8238 org-insert-link-history))
8239 (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
8240 (mapcar 'car org-link-abbrev-alist)
8241 org-link-types))
8242 (unwind-protect
8243 (progn
8244 (setq link
8245 (let ((org-completion-use-ido nil)
8246 (org-completion-use-iswitchb nil))
8247 (org-completing-read
8248 "Link: "
8249 (append
8250 (mapcar (lambda (x) (list (concat x ":")))
8251 all-prefixes)
8252 (mapcar 'car org-stored-links))
8253 nil nil nil
8254 'tmphist
8255 (car (car org-stored-links)))))
8256 (if (not (string-match "\\S-" link))
8257 (error "No link selected"))
8258 (if (or (member link all-prefixes)
8259 (and (equal ":" (substring link -1))
8260 (member (substring link 0 -1) all-prefixes)
8261 (setq link (substring link 0 -1))))
8262 (setq link (org-link-try-special-completion link))))
8263 (set-window-configuration wcf)
8264 (kill-buffer "*Org Links*"))
8265 (setq entry (assoc link org-stored-links))
8266 (or entry (push link org-insert-link-history))
8267 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
8268 (not org-keep-stored-link-after-insertion))
8269 (setq org-stored-links (delq (assoc link org-stored-links)
8270 org-stored-links)))
8271 (setq desc (or desc (nth 1 entry)))))
8273 (if (string-match org-plain-link-re link)
8274 ;; URL-like link, normalize the use of angular brackets.
8275 (setq link (org-make-link (org-remove-angle-brackets link))))
8277 ;; Check if we are linking to the current file with a search option
8278 ;; If yes, simplify the link by using only the search option.
8279 (when (and buffer-file-name
8280 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
8281 (let* ((path (match-string 1 link))
8282 (case-fold-search nil)
8283 (search (match-string 2 link)))
8284 (save-match-data
8285 (if (equal (file-truename buffer-file-name) (file-truename path))
8286 ;; We are linking to this same file, with a search option
8287 (setq link search)))))
8289 ;; Check if we can/should use a relative path. If yes, simplify the link
8290 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
8291 (let* ((type (match-string 1 link))
8292 (path (match-string 2 link))
8293 (origpath path)
8294 (case-fold-search nil))
8295 (cond
8296 ((or (eq org-link-file-path-type 'absolute)
8297 (equal complete-file '(16)))
8298 (setq path (abbreviate-file-name (expand-file-name path))))
8299 ((eq org-link-file-path-type 'noabbrev)
8300 (setq path (expand-file-name path)))
8301 ((eq org-link-file-path-type 'relative)
8302 (setq path (file-relative-name path)))
8304 (save-match-data
8305 (if (string-match (concat "^" (regexp-quote
8306 (file-name-as-directory
8307 (expand-file-name "."))))
8308 (expand-file-name path))
8309 ;; We are linking a file with relative path name.
8310 (setq path (substring (expand-file-name path)
8311 (match-end 0)))
8312 (setq path (abbreviate-file-name (expand-file-name path)))))))
8313 (setq link (concat type path))
8314 (if (equal desc origpath)
8315 (setq desc path))))
8317 (if org-make-link-description-function
8318 (setq desc (funcall org-make-link-description-function link desc)))
8320 (setq desc (read-string "Description: " desc))
8321 (unless (string-match "\\S-" desc) (setq desc nil))
8322 (if remove (apply 'delete-region remove))
8323 (insert (org-make-link-string link desc))))
8325 (defun org-link-try-special-completion (type)
8326 "If there is completion support for link type TYPE, offer it."
8327 (let ((fun (intern (concat "org-" type "-complete-link"))))
8328 (if (functionp fun)
8329 (funcall fun)
8330 (read-string "Link (no completion support): " (concat type ":")))))
8332 (defun org-file-complete-link (&optional arg)
8333 "Create a file link using completion."
8334 (let (file link)
8335 (setq file (read-file-name "File: "))
8336 (let ((pwd (file-name-as-directory (expand-file-name ".")))
8337 (pwd1 (file-name-as-directory (abbreviate-file-name
8338 (expand-file-name ".")))))
8339 (cond
8340 ((equal arg '(16))
8341 (setq link (org-make-link
8342 "file:"
8343 (abbreviate-file-name (expand-file-name file)))))
8344 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
8345 (setq link (org-make-link "file:" (match-string 1 file))))
8346 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
8347 (expand-file-name file))
8348 (setq link (org-make-link
8349 "file:" (match-string 1 (expand-file-name file)))))
8350 (t (setq link (org-make-link "file:" file)))))
8351 link))
8353 (defun org-completing-read (&rest args)
8354 "Completing-read with SPACE being a normal character."
8355 (let ((minibuffer-local-completion-map
8356 (copy-keymap minibuffer-local-completion-map)))
8357 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
8358 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
8359 (apply 'org-icompleting-read args)))
8361 (defun org-completing-read-no-i (&rest args)
8362 (let (org-completion-use-ido org-completion-use-iswitchb)
8363 (apply 'org-completing-read args)))
8365 (defun org-iswitchb-completing-read (prompt choices &rest args)
8366 "Use iswitch as a completing-read replacement to choose from choices.
8367 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
8368 from."
8369 (let* ((iswitchb-use-virtual-buffers nil)
8370 (iswitchb-make-buflist-hook
8371 (lambda ()
8372 (setq iswitchb-temp-buflist choices))))
8373 (iswitchb-read-buffer prompt)))
8375 (defun org-icompleting-read (&rest args)
8376 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
8377 (org-without-partial-completion
8378 (if (and org-completion-use-ido
8379 (fboundp 'ido-completing-read)
8380 (boundp 'ido-mode) ido-mode
8381 (listp (second args)))
8382 (let ((ido-enter-matching-directory nil))
8383 (apply 'ido-completing-read (concat (car args))
8384 (if (consp (car (nth 1 args)))
8385 (mapcar (lambda (x) (car x)) (nth 1 args))
8386 (nth 1 args))
8387 (cddr args)))
8388 (if (and org-completion-use-iswitchb
8389 (boundp 'iswitchb-mode) iswitchb-mode
8390 (listp (second args)))
8391 (apply 'org-iswitchb-completing-read (concat (car args))
8392 (if (consp (car (nth 1 args)))
8393 (mapcar (lambda (x) (car x)) (nth 1 args))
8394 (nth 1 args))
8395 (cddr args))
8396 (apply 'completing-read args)))))
8398 (defun org-extract-attributes (s)
8399 "Extract the attributes cookie from a string and set as text property."
8400 (let (a attr (start 0) key value)
8401 (save-match-data
8402 (when (string-match "{{\\([^}]+\\)}}$" s)
8403 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
8404 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
8405 (setq key (match-string 1 a) value (match-string 2 a)
8406 start (match-end 0)
8407 attr (plist-put attr (intern key) value))))
8408 (org-add-props s nil 'org-attr attr))
8411 (defun org-extract-attributes-from-string (tag)
8412 (let (key value attr)
8413 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
8414 (setq key (match-string 1 tag) value (match-string 2 tag)
8415 tag (replace-match "" t t tag)
8416 attr (plist-put attr (intern key) value)))
8417 (cons tag attr)))
8419 (defun org-attributes-to-string (plist)
8420 "Format a property list into an HTML attribute list."
8421 (let ((s "") key value)
8422 (while plist
8423 (setq key (pop plist) value (pop plist))
8424 (and value
8425 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
8428 ;;; Opening/following a link
8430 (defvar org-link-search-failed nil)
8432 (defvar org-open-link-functions nil
8433 "Hook for functions finding a plain text link.
8434 These functions must take a single argument, the link content.
8435 They will be called for links that look like [[link text][description]]
8436 when LINK TEXT does not have a protocol like \"http:\" and does not look
8437 like a filename (e.g. \"./blue.png\").
8439 These functions will be called *before* Org attempts to resolve the
8440 link by doing text searches in the current buffer - so if you want a
8441 link \"[[target]]\" to still find \"<<target>>\", your function should
8442 handle this as a special case.
8444 When the function does handle the link, it must return a non-nil value.
8445 If it decides that it is not responsible for this link, it must return
8446 nil to indicate that that Org-mode can continue with other options
8447 like exact and fuzzy text search.")
8449 (defun org-next-link ()
8450 "Move forward to the next link.
8451 If the link is in hidden text, expose it."
8452 (interactive)
8453 (when (and org-link-search-failed (eq this-command last-command))
8454 (goto-char (point-min))
8455 (message "Link search wrapped back to beginning of buffer"))
8456 (setq org-link-search-failed nil)
8457 (let* ((pos (point))
8458 (ct (org-context))
8459 (a (assoc :link ct)))
8460 (if a (goto-char (nth 2 a)))
8461 (if (re-search-forward org-any-link-re nil t)
8462 (progn
8463 (goto-char (match-beginning 0))
8464 (if (org-invisible-p) (org-show-context)))
8465 (goto-char pos)
8466 (setq org-link-search-failed t)
8467 (error "No further link found"))))
8469 (defun org-previous-link ()
8470 "Move backward to the previous link.
8471 If the link is in hidden text, expose it."
8472 (interactive)
8473 (when (and org-link-search-failed (eq this-command last-command))
8474 (goto-char (point-max))
8475 (message "Link search wrapped back to end of buffer"))
8476 (setq org-link-search-failed nil)
8477 (let* ((pos (point))
8478 (ct (org-context))
8479 (a (assoc :link ct)))
8480 (if a (goto-char (nth 1 a)))
8481 (if (re-search-backward org-any-link-re nil t)
8482 (progn
8483 (goto-char (match-beginning 0))
8484 (if (org-invisible-p) (org-show-context)))
8485 (goto-char pos)
8486 (setq org-link-search-failed t)
8487 (error "No further link found"))))
8489 (defun org-translate-link (s)
8490 "Translate a link string if a translation function has been defined."
8491 (if (and org-link-translation-function
8492 (fboundp org-link-translation-function)
8493 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
8494 (progn
8495 (setq s (funcall org-link-translation-function
8496 (match-string 1) (match-string 2)))
8497 (concat (car s) ":" (cdr s)))
8500 (defun org-translate-link-from-planner (type path)
8501 "Translate a link from Emacs Planner syntax so that Org can follow it.
8502 This is still an experimental function, your mileage may vary."
8503 (cond
8504 ((member type '("http" "https" "news" "ftp"))
8505 ;; standard Internet links are the same.
8506 nil)
8507 ((and (equal type "irc") (string-match "^//" path))
8508 ;; Planner has two / at the beginning of an irc link, we have 1.
8509 ;; We should have zero, actually....
8510 (setq path (substring path 1)))
8511 ((and (equal type "lisp") (string-match "^/" path))
8512 ;; Planner has a slash, we do not.
8513 (setq type "elisp" path (substring path 1)))
8514 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
8515 ;; A typical message link. Planner has the id after the final slash,
8516 ;; we separate it with a hash mark
8517 (setq path (concat (match-string 1 path) "#"
8518 (org-remove-angle-brackets (match-string 2 path)))))
8520 (cons type path))
8522 (defun org-find-file-at-mouse (ev)
8523 "Open file link or URL at mouse."
8524 (interactive "e")
8525 (mouse-set-point ev)
8526 (org-open-at-point 'in-emacs))
8528 (defun org-open-at-mouse (ev)
8529 "Open file link or URL at mouse."
8530 (interactive "e")
8531 (mouse-set-point ev)
8532 (if (eq major-mode 'org-agenda-mode)
8533 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
8534 (org-open-at-point))
8536 (defvar org-window-config-before-follow-link nil
8537 "The window configuration before following a link.
8538 This is saved in case the need arises to restore it.")
8540 (defvar org-open-link-marker (make-marker)
8541 "Marker pointing to the location where `org-open-at-point; was called.")
8543 ;;;###autoload
8544 (defun org-open-at-point-global ()
8545 "Follow a link like Org-mode does.
8546 This command can be called in any mode to follow a link that has
8547 Org-mode syntax."
8548 (interactive)
8549 (org-run-like-in-org-mode 'org-open-at-point))
8551 ;;;###autoload
8552 (defun org-open-link-from-string (s &optional arg reference-buffer)
8553 "Open a link in the string S, as if it was in Org-mode."
8554 (interactive "sLink: \nP")
8555 (let ((reference-buffer (or reference-buffer (current-buffer))))
8556 (with-temp-buffer
8557 (let ((org-inhibit-startup t))
8558 (org-mode)
8559 (insert s)
8560 (goto-char (point-min))
8561 (when reference-buffer
8562 (setq org-link-abbrev-alist-local
8563 (with-current-buffer reference-buffer
8564 org-link-abbrev-alist-local)))
8565 (org-open-at-point arg reference-buffer)))))
8567 (defun org-open-at-point (&optional in-emacs reference-buffer)
8568 "Open link at or after point.
8569 If there is no link at point, this function will search forward up to
8570 the end of the current line.
8571 Normally, files will be opened by an appropriate application. If the
8572 optional argument IN-EMACS is non-nil, Emacs will visit the file.
8573 With a double prefix argument, try to open outside of Emacs, in the
8574 application the system uses for this file type."
8575 (interactive "P")
8576 (org-load-modules-maybe)
8577 (move-marker org-open-link-marker (point))
8578 (setq org-window-config-before-follow-link (current-window-configuration))
8579 (org-remove-occur-highlights nil nil t)
8580 (cond
8581 ((and (org-on-heading-p)
8582 (not (org-in-regexp
8583 (concat org-plain-link-re "\\|"
8584 org-bracket-link-regexp "\\|"
8585 org-angle-link-re "\\|"
8586 "[ \t]:[^ \t\n]+:[ \t]*$")))
8587 (not (get-text-property (point) 'org-linked-text)))
8588 (or (org-offer-links-in-entry in-emacs)
8589 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
8590 ((org-at-timestamp-p t) (org-follow-timestamp-link))
8591 ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
8592 (org-footnote-action))
8594 (let (type path link line search (pos (point)))
8595 (catch 'match
8596 (save-excursion
8597 (skip-chars-forward "^]\n\r")
8598 (when (org-in-regexp org-bracket-link-regexp 1)
8599 (setq link (org-extract-attributes
8600 (org-link-unescape (org-match-string-no-properties 1))))
8601 (while (string-match " *\n *" link)
8602 (setq link (replace-match " " t t link)))
8603 (setq link (org-link-expand-abbrev link))
8604 (cond
8605 ((or (file-name-absolute-p link)
8606 (string-match "^\\.\\.?/" link))
8607 (setq type "file" path link))
8608 ((string-match org-link-re-with-space3 link)
8609 (setq type (match-string 1 link) path (match-string 2 link)))
8610 (t (setq type "thisfile" path link)))
8611 (throw 'match t)))
8613 (when (get-text-property (point) 'org-linked-text)
8614 (setq type "thisfile"
8615 pos (if (get-text-property (1+ (point)) 'org-linked-text)
8616 (1+ (point)) (point))
8617 path (buffer-substring
8618 (previous-single-property-change pos 'org-linked-text)
8619 (next-single-property-change pos 'org-linked-text)))
8620 (throw 'match t))
8622 (save-excursion
8623 (when (or (org-in-regexp org-angle-link-re)
8624 (org-in-regexp org-plain-link-re))
8625 (setq type (match-string 1) path (match-string 2))
8626 (throw 'match t)))
8627 (save-excursion
8628 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
8629 (setq type "tags"
8630 path (match-string 1))
8631 (while (string-match ":" path)
8632 (setq path (replace-match "+" t t path)))
8633 (throw 'match t)))
8634 (when (org-in-regexp "<\\([^><\n]+\\)>")
8635 (setq type "tree-match"
8636 path (match-string 1))
8637 (throw 'match t)))
8638 (unless path
8639 (error "No link found"))
8641 ;; switch back to reference buffer
8642 ;; needed when if called in a temporary buffer through
8643 ;; org-open-link-from-string
8644 (with-current-buffer (or reference-buffer (current-buffer))
8646 ;; Remove any trailing spaces in path
8647 (if (string-match " +\\'" path)
8648 (setq path (replace-match "" t t path)))
8649 (if (and org-link-translation-function
8650 (fboundp org-link-translation-function))
8651 ;; Check if we need to translate the link
8652 (let ((tmp (funcall org-link-translation-function type path)))
8653 (setq type (car tmp) path (cdr tmp))))
8655 (cond
8657 ((assoc type org-link-protocols)
8658 (funcall (nth 1 (assoc type org-link-protocols)) path))
8660 ((equal type "mailto")
8661 (let ((cmd (car org-link-mailto-program))
8662 (args (cdr org-link-mailto-program)) args1
8663 (address path) (subject "") a)
8664 (if (string-match "\\(.*\\)::\\(.*\\)" path)
8665 (setq address (match-string 1 path)
8666 subject (org-link-escape (match-string 2 path))))
8667 (while args
8668 (cond
8669 ((not (stringp (car args))) (push (pop args) args1))
8670 (t (setq a (pop args))
8671 (if (string-match "%a" a)
8672 (setq a (replace-match address t t a)))
8673 (if (string-match "%s" a)
8674 (setq a (replace-match subject t t a)))
8675 (push a args1))))
8676 (apply cmd (nreverse args1))))
8678 ((member type '("http" "https" "ftp" "news"))
8679 (browse-url (concat type ":" (org-link-escape
8680 path org-link-escape-chars-browser))))
8682 ((member type '("message"))
8683 (browse-url (concat type ":" path)))
8685 ((string= type "tags")
8686 (org-tags-view in-emacs path))
8688 ((string= type "tree-match")
8689 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
8691 ((string= type "file")
8692 (if (string-match "::\\([0-9]+\\)\\'" path)
8693 (setq line (string-to-number (match-string 1 path))
8694 path (substring path 0 (match-beginning 0)))
8695 (if (string-match "::\\(.+\\)\\'" path)
8696 (setq search (match-string 1 path)
8697 path (substring path 0 (match-beginning 0)))))
8698 (if (string-match "[*?{]" (file-name-nondirectory path))
8699 (dired path)
8700 (org-open-file path in-emacs line search)))
8702 ((string= type "news")
8703 (require 'org-gnus)
8704 (org-gnus-follow-link path))
8706 ((string= type "shell")
8707 (let ((cmd path))
8708 (if (or (not org-confirm-shell-link-function)
8709 (funcall org-confirm-shell-link-function
8710 (format "Execute \"%s\" in shell? "
8711 (org-add-props cmd nil
8712 'face 'org-warning))))
8713 (progn
8714 (message "Executing %s" cmd)
8715 (shell-command cmd))
8716 (error "Abort"))))
8718 ((string= type "elisp")
8719 (let ((cmd path))
8720 (if (or (not org-confirm-elisp-link-function)
8721 (funcall org-confirm-elisp-link-function
8722 (format "Execute \"%s\" as elisp? "
8723 (org-add-props cmd nil
8724 'face 'org-warning))))
8725 (message "%s => %s" cmd
8726 (if (equal (string-to-char cmd) ?\()
8727 (eval (read cmd))
8728 (call-interactively (read cmd))))
8729 (error "Abort"))))
8731 ((and (string= type "thisfile")
8732 (run-hook-with-args-until-success
8733 'org-open-link-functions path)))
8735 ((string= type "thisfile")
8736 (if in-emacs
8737 (switch-to-buffer-other-window
8738 (org-get-buffer-for-internal-link (current-buffer)))
8739 (org-mark-ring-push))
8740 (let ((cmd `(org-link-search
8741 ,path
8742 ,(cond ((equal in-emacs '(4)) 'occur)
8743 ((equal in-emacs '(16)) 'org-occur)
8744 (t nil))
8745 ,pos)))
8746 (condition-case nil (eval cmd)
8747 (error (progn (widen) (eval cmd))))))
8750 (browse-url-at-point)))))))
8751 (move-marker org-open-link-marker nil)
8752 (run-hook-with-args 'org-follow-link-hook))
8754 (defun org-offer-links-in-entry (&optional nth zero)
8755 "Offer links in the current entry and follow the selected link.
8756 If there is only one link, follow it immediately as well.
8757 If NTH is an integer, immediately pick the NTH link found.
8758 If ZERO is a string, check also this string for a link, and if
8759 there is one, offer it as link number zero."
8760 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
8761 "\\(" org-angle-link-re "\\)\\|"
8762 "\\(" org-plain-link-re "\\)"))
8763 (cnt ?0)
8764 (in-emacs (if (integerp nth) nil nth))
8765 have-zero end links link c)
8766 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
8767 (push (match-string 0 zero) links)
8768 (setq cnt (1- cnt) have-zero t))
8769 (save-excursion
8770 (org-back-to-heading t)
8771 (setq end (save-excursion (outline-next-heading) (point)))
8772 (while (re-search-forward re end t)
8773 (push (match-string 0) links))
8774 (setq links (org-uniquify (reverse links))))
8776 (cond
8777 ((null links)
8778 (message "No links"))
8779 ((equal (length links) 1)
8780 (setq link (list (car links))))
8781 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
8782 (setq link (nth (if have-zero nth (1- nth)) links)))
8783 (t ; we have to select a link
8784 (save-excursion
8785 (save-window-excursion
8786 (delete-other-windows)
8787 (with-output-to-temp-buffer "*Select Link*"
8788 (mapc (lambda (l)
8789 (if (not (string-match org-bracket-link-regexp l))
8790 (princ (format "[%c] %s\n" (incf cnt)
8791 (org-remove-angle-brackets l)))
8792 (if (match-end 3)
8793 (princ (format "[%c] %s (%s)\n" (incf cnt)
8794 (match-string 3 l) (match-string 1 l)))
8795 (princ (format "[%c] %s\n" (incf cnt)
8796 (match-string 1 l))))))
8797 links))
8798 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
8799 (message "Select link to open, RET to open all:")
8800 (setq c (read-char-exclusive))
8801 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
8802 (when (equal c ?q) (error "Abort"))
8803 (if (equal c ?\C-m)
8804 (setq link links)
8805 (setq nth (- c ?0))
8806 (if have-zero (setq nth (1+ nth)))
8807 (unless (and (integerp nth) (>= (length links) nth))
8808 (error "Invalid link selection"))
8809 (setq link (list (nth (1- nth) links))))))
8810 (if link
8811 (let ((buf (current-buffer)))
8812 (dolist (l link)
8813 (org-open-link-from-string l in-emacs buf))
8815 nil)))
8817 ;; Add special file links that specify the way of opening
8819 (org-add-link-type "file+sys" 'org-open-file-with-system)
8820 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
8821 (defun org-open-file-with-system (path)
8822 "Open file at PATH using the system way of opeing it."
8823 (org-open-file path 'system))
8824 (defun org-open-file-with-emacs (path)
8825 "Open file at PATH in emacs."
8826 (org-open-file path 'emacs))
8827 (defun org-remove-file-link-modifiers ()
8828 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
8829 (goto-char (point-min))
8830 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
8831 (org-if-unprotected
8832 (replace-match "file:" t t))))
8833 (eval-after-load "org-exp"
8834 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
8835 'org-remove-file-link-modifiers))
8837 ;;;; Time estimates
8839 (defun org-get-effort (&optional pom)
8840 "Get the effort estimate for the current entry."
8841 (org-entry-get pom org-effort-property))
8843 ;;; File search
8845 (defvar org-create-file-search-functions nil
8846 "List of functions to construct the right search string for a file link.
8847 These functions are called in turn with point at the location to
8848 which the link should point.
8850 A function in the hook should first test if it would like to
8851 handle this file type, for example by checking the major-mode or
8852 the file extension. If it decides not to handle this file, it
8853 should just return nil to give other functions a chance. If it
8854 does handle the file, it must return the search string to be used
8855 when following the link. The search string will be part of the
8856 file link, given after a double colon, and `org-open-at-point'
8857 will automatically search for it. If special measures must be
8858 taken to make the search successful, another function should be
8859 added to the companion hook `org-execute-file-search-functions',
8860 which see.
8862 A function in this hook may also use `setq' to set the variable
8863 `description' to provide a suggestion for the descriptive text to
8864 be used for this link when it gets inserted into an Org-mode
8865 buffer with \\[org-insert-link].")
8867 (defvar org-execute-file-search-functions nil
8868 "List of functions to execute a file search triggered by a link.
8870 Functions added to this hook must accept a single argument, the
8871 search string that was part of the file link, the part after the
8872 double colon. The function must first check if it would like to
8873 handle this search, for example by checking the major-mode or the
8874 file extension. If it decides not to handle this search, it
8875 should just return nil to give other functions a chance. If it
8876 does handle the search, it must return a non-nil value to keep
8877 other functions from trying.
8879 Each function can access the current prefix argument through the
8880 variable `current-prefix-argument'. Note that a single prefix is
8881 used to force opening a link in Emacs, so it may be good to only
8882 use a numeric or double prefix to guide the search function.
8884 In case this is needed, a function in this hook can also restore
8885 the window configuration before `org-open-at-point' was called using:
8887 (set-window-configuration org-window-config-before-follow-link)")
8889 (defun org-link-search (s &optional type avoid-pos)
8890 "Search for a link search option.
8891 If S is surrounded by forward slashes, it is interpreted as a
8892 regular expression. In org-mode files, this will create an `org-occur'
8893 sparse tree. In ordinary files, `occur' will be used to list matches.
8894 If the current buffer is in `dired-mode', grep will be used to search
8895 in all files. If AVOID-POS is given, ignore matches near that position."
8896 (let ((case-fold-search t)
8897 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
8898 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
8899 (append '(("") (" ") ("\t") ("\n"))
8900 org-emphasis-alist)
8901 "\\|") "\\)"))
8902 (pos (point))
8903 (pre nil) (post nil)
8904 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
8905 (cond
8906 ;; First check if there are any special
8907 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
8908 ;; Now try the builtin stuff
8909 ((and (equal (string-to-char s0) ?#)
8910 (> (length s0) 1)
8911 (save-excursion
8912 (goto-char (point-min))
8913 (and
8914 (re-search-forward
8915 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
8916 (setq type 'dedicated
8917 pos (match-beginning 0))))
8918 ;; There is an exact target for this
8919 (goto-char pos)
8920 (org-back-to-heading t)))
8921 ((save-excursion
8922 (goto-char (point-min))
8923 (and
8924 (re-search-forward
8925 (concat "<<" (regexp-quote s0) ">>") nil t)
8926 (setq type 'dedicated
8927 pos (match-beginning 0))))
8928 ;; There is an exact target for this
8929 (goto-char pos))
8930 ((and (string-match "^(\\(.*\\))$" s0)
8931 (save-excursion
8932 (goto-char (point-min))
8933 (and
8934 (re-search-forward
8935 (concat "[^[]" (regexp-quote
8936 (format org-coderef-label-format
8937 (match-string 1 s0))))
8938 nil t)
8939 (setq type 'dedicated
8940 pos (1+ (match-beginning 0))))))
8941 ;; There is a coderef target for this
8942 (goto-char pos))
8943 ((string-match "^/\\(.*\\)/$" s)
8944 ;; A regular expression
8945 (cond
8946 ((org-mode-p)
8947 (org-occur (match-string 1 s)))
8948 ;;((eq major-mode 'dired-mode)
8949 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
8950 (t (org-do-occur (match-string 1 s)))))
8952 ;; A normal search strings
8953 (when (equal (string-to-char s) ?*)
8954 ;; Anchor on headlines, post may include tags.
8955 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
8956 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
8957 s (substring s 1)))
8958 (remove-text-properties
8959 0 (length s)
8960 '(face nil mouse-face nil keymap nil fontified nil) s)
8961 ;; Make a series of regular expressions to find a match
8962 (setq words (org-split-string s "[ \n\r\t]+")
8964 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
8965 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
8966 "\\)" markers)
8967 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
8968 re2a (concat "[ \t\r\n]" re2a_)
8969 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
8970 re4 (concat "[^a-zA-Z_]" re4_)
8972 re1 (concat pre re2 post)
8973 re3 (concat pre (if pre re4_ re4) post)
8974 re5 (concat pre ".*" re4)
8975 re2 (concat pre re2)
8976 re2a (concat pre (if pre re2a_ re2a))
8977 re4 (concat pre (if pre re4_ re4))
8978 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
8979 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
8980 re5 "\\)"
8982 (cond
8983 ((eq type 'org-occur) (org-occur reall))
8984 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
8985 (t (goto-char (point-min))
8986 (setq type 'fuzzy)
8987 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
8988 (org-search-not-self 1 re1 nil t)
8989 (org-search-not-self 1 re2 nil t)
8990 (org-search-not-self 1 re2a nil t)
8991 (org-search-not-self 1 re3 nil t)
8992 (org-search-not-self 1 re4 nil t)
8993 (org-search-not-self 1 re5 nil t)
8995 (goto-char (match-beginning 1))
8996 (goto-char pos)
8997 (error "No match")))))
8999 ;; Normal string-search
9000 (goto-char (point-min))
9001 (if (search-forward s nil t)
9002 (goto-char (match-beginning 0))
9003 (error "No match"))))
9004 (and (org-mode-p) (org-show-context 'link-search))
9005 type))
9007 (defun org-search-not-self (group &rest args)
9008 "Execute `re-search-forward', but only accept matches that do not
9009 enclose the position of `org-open-link-marker'."
9010 (let ((m org-open-link-marker))
9011 (catch 'exit
9012 (while (apply 're-search-forward args)
9013 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
9014 (goto-char (match-end group))
9015 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
9016 (> (match-beginning 0) (marker-position m))
9017 (< (match-end 0) (marker-position m)))
9018 (save-match-data
9019 (or (not (org-in-regexp
9020 org-bracket-link-analytic-regexp 1))
9021 (not (match-end 4)) ; no description
9022 (and (<= (match-beginning 4) (point))
9023 (>= (match-end 4) (point))))))
9024 (throw 'exit (point))))))))
9026 (defun org-get-buffer-for-internal-link (buffer)
9027 "Return a buffer to be used for displaying the link target of internal links."
9028 (cond
9029 ((not org-display-internal-link-with-indirect-buffer)
9030 buffer)
9031 ((string-match "(Clone)$" (buffer-name buffer))
9032 (message "Buffer is already a clone, not making another one")
9033 ;; we also do not modify visibility in this case
9034 buffer)
9035 (t ; make a new indirect buffer for displaying the link
9036 (let* ((bn (buffer-name buffer))
9037 (ibn (concat bn "(Clone)"))
9038 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
9039 (with-current-buffer ib (org-overview))
9040 ib))))
9042 (defun org-do-occur (regexp &optional cleanup)
9043 "Call the Emacs command `occur'.
9044 If CLEANUP is non-nil, remove the printout of the regular expression
9045 in the *Occur* buffer. This is useful if the regex is long and not useful
9046 to read."
9047 (occur regexp)
9048 (when cleanup
9049 (let ((cwin (selected-window)) win beg end)
9050 (when (setq win (get-buffer-window "*Occur*"))
9051 (select-window win))
9052 (goto-char (point-min))
9053 (when (re-search-forward "match[a-z]+" nil t)
9054 (setq beg (match-end 0))
9055 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
9056 (setq end (1- (match-beginning 0)))))
9057 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
9058 (goto-char (point-min))
9059 (select-window cwin))))
9061 ;;; The mark ring for links jumps
9063 (defvar org-mark-ring nil
9064 "Mark ring for positions before jumps in Org-mode.")
9065 (defvar org-mark-ring-last-goto nil
9066 "Last position in the mark ring used to go back.")
9067 ;; Fill and close the ring
9068 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
9069 (loop for i from 1 to org-mark-ring-length do
9070 (push (make-marker) org-mark-ring))
9071 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
9072 org-mark-ring)
9074 (defun org-mark-ring-push (&optional pos buffer)
9075 "Put the current position or POS into the mark ring and rotate it."
9076 (interactive)
9077 (setq pos (or pos (point)))
9078 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
9079 (move-marker (car org-mark-ring)
9080 (or pos (point))
9081 (or buffer (current-buffer)))
9082 (message "%s"
9083 (substitute-command-keys
9084 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
9086 (defun org-mark-ring-goto (&optional n)
9087 "Jump to the previous position in the mark ring.
9088 With prefix arg N, jump back that many stored positions. When
9089 called several times in succession, walk through the entire ring.
9090 Org-mode commands jumping to a different position in the current file,
9091 or to another Org-mode file, automatically push the old position
9092 onto the ring."
9093 (interactive "p")
9094 (let (p m)
9095 (if (eq last-command this-command)
9096 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
9097 (setq p org-mark-ring))
9098 (setq org-mark-ring-last-goto p)
9099 (setq m (car p))
9100 (switch-to-buffer (marker-buffer m))
9101 (goto-char m)
9102 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
9104 (defun org-remove-angle-brackets (s)
9105 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
9106 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
9108 (defun org-add-angle-brackets (s)
9109 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
9110 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
9112 (defun org-remove-double-quotes (s)
9113 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
9114 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
9117 ;;; Following specific links
9119 (defun org-follow-timestamp-link ()
9120 (cond
9121 ((org-at-date-range-p t)
9122 (let ((org-agenda-start-on-weekday)
9123 (t1 (match-string 1))
9124 (t2 (match-string 2)))
9125 (setq t1 (time-to-days (org-time-string-to-time t1))
9126 t2 (time-to-days (org-time-string-to-time t2)))
9127 (org-agenda-list nil t1 (1+ (- t2 t1)))))
9128 ((org-at-timestamp-p t)
9129 (org-agenda-list nil (time-to-days (org-time-string-to-time
9130 (substring (match-string 1) 0 10)))
9132 (t (error "This should not happen"))))
9135 ;;; Following file links
9136 (defvar org-wait nil)
9137 (defun org-open-file (path &optional in-emacs line search)
9138 "Open the file at PATH.
9139 First, this expands any special file name abbreviations. Then the
9140 configuration variable `org-file-apps' is checked if it contains an
9141 entry for this file type, and if yes, the corresponding command is launched.
9143 If no application is found, Emacs simply visits the file.
9145 With optional prefix argument IN-EMACS, Emacs will visit the file.
9146 With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
9147 and to use an external application to visit the file.
9149 Optional LINE specifies a line to go to, optional SEARCH a string
9150 to search for. If LINE or SEARCH is given, the file will be
9151 opened in Emacs, unless an entry from org-file-apps that makes
9152 use of groups in a regexp matches.
9153 If the file does not exist, an error is thrown."
9154 (let* ((file (if (equal path "")
9155 buffer-file-name
9156 (substitute-in-file-name (expand-file-name path))))
9157 (file-apps (append org-file-apps (org-default-apps)))
9158 (apps (org-remove-if
9159 'org-file-apps-entry-match-against-dlink-p file-apps))
9160 (apps-dlink (org-remove-if-not
9161 'org-file-apps-entry-match-against-dlink-p file-apps))
9162 (remp (and (assq 'remote apps) (org-file-remote-p file)))
9163 (dirp (if remp nil (file-directory-p file)))
9164 (file (if (and dirp org-open-directory-means-index-dot-org)
9165 (concat (file-name-as-directory file) "index.org")
9166 file))
9167 (a-m-a-p (assq 'auto-mode apps))
9168 (dfile (downcase file))
9169 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
9170 (link (cond ((and (eq line nil)
9171 (eq search nil))
9172 file)
9173 (line
9174 (concat file "::" (number-to-string line)))
9175 (search
9176 (concat file "::" search))))
9177 (dlink (downcase link))
9178 (old-buffer (current-buffer))
9179 (old-pos (point))
9180 (old-mode major-mode)
9181 ext cmd link-match-data)
9182 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
9183 (setq ext (match-string 1 dfile))
9184 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
9185 (setq ext (match-string 1 dfile))))
9186 (cond
9187 ((member in-emacs '((16) system))
9188 (setq cmd (cdr (assoc 'system apps))))
9189 (in-emacs (setq cmd 'emacs))
9191 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
9192 (and dirp (cdr (assoc 'directory apps)))
9193 ; first, try matching against apps-dlink
9194 ; if we get a match here, store the match data for later
9195 (let ((match (assoc-default dlink apps-dlink
9196 'string-match)))
9197 (if match
9198 (progn (setq link-match-data (match-data))
9199 match)
9200 (progn (setq in-emacs (or in-emacs line search))
9201 nil))) ; if we have no match in apps-dlink,
9202 ; always open the file in emacs if line or search
9203 ; is given (for backwards compatibility)
9204 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
9205 'string-match)
9206 (cdr (assoc ext apps))
9207 (cdr (assoc t apps))))))
9208 (when (eq cmd 'system)
9209 (setq cmd (cdr (assoc 'system apps))))
9210 (when (eq cmd 'default)
9211 (setq cmd (cdr (assoc t apps))))
9212 (when (eq cmd 'mailcap)
9213 (require 'mailcap)
9214 (mailcap-parse-mailcaps)
9215 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
9216 (command (mailcap-mime-info mime-type)))
9217 (if (stringp command)
9218 (setq cmd command)
9219 (setq cmd 'emacs))))
9220 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
9221 (not (file-exists-p file))
9222 (not org-open-non-existing-files))
9223 (error "No such file: %s" file))
9224 (cond
9225 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
9226 ;; Remove quotes around the file name - we'll use shell-quote-argument.
9227 (while (string-match "['\"]%s['\"]" cmd)
9228 (setq cmd (replace-match "%s" t t cmd)))
9229 (while (string-match "%s" cmd)
9230 (setq cmd (replace-match
9231 (save-match-data
9232 (shell-quote-argument
9233 (convert-standard-filename file)))
9234 t t cmd)))
9236 ;; Replace "%1", "%2" etc. in command with group matches from regex
9237 (save-match-data
9238 (let ((match-index 1)
9239 (number-of-groups (- (/ (length link-match-data) 2) 1)))
9240 (set-match-data link-match-data)
9241 (while (<= match-index number-of-groups)
9242 (let ((regex (concat "%" (number-to-string match-index)))
9243 (replace-with (match-string match-index dlink)))
9244 (while (string-match regex cmd)
9245 (setq cmd (replace-match replace-with t t cmd))))
9246 (setq match-index (+ match-index 1)))))
9248 (save-window-excursion
9249 (start-process-shell-command cmd nil cmd)
9250 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
9252 ((or (stringp cmd)
9253 (eq cmd 'emacs))
9254 (funcall (cdr (assq 'file org-link-frame-setup)) file)
9255 (widen)
9256 (if line (org-goto-line line)
9257 (if search (org-link-search search))))
9258 ((consp cmd)
9259 (let ((file (convert-standard-filename file)))
9260 (save-match-data
9261 (set-match-data link-match-data)
9262 (eval cmd))))
9263 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
9264 (and (org-mode-p) (eq old-mode 'org-mode)
9265 (or (not (equal old-buffer (current-buffer)))
9266 (not (equal old-pos (point))))
9267 (org-mark-ring-push old-pos old-buffer))))
9269 (defun org-file-apps-entry-match-against-dlink-p (entry)
9270 "This function returns non-nil if `entry' uses a regular
9271 expression which should be matched against the whole link by
9272 org-open-file.
9274 It assumes that is the case when the entry uses a regular
9275 expression which has at least one grouping construct and the
9276 action is either a lisp form or a command string containing
9277 '%1', i.e. using at least one subexpression match as a
9278 parameter."
9279 (let ((selector (car entry))
9280 (action (cdr entry)))
9281 (if (stringp selector)
9282 (and (> (regexp-opt-depth selector) 0)
9283 (or (and (stringp action)
9284 (string-match "%[0-9]" action))
9285 (consp action)))
9286 nil)))
9288 (defun org-default-apps ()
9289 "Return the default applications for this operating system."
9290 (cond
9291 ((eq system-type 'darwin)
9292 org-file-apps-defaults-macosx)
9293 ((eq system-type 'windows-nt)
9294 org-file-apps-defaults-windowsnt)
9295 (t org-file-apps-defaults-gnu)))
9297 (defun org-apps-regexp-alist (list &optional add-auto-mode)
9298 "Convert extensions to regular expressions in the cars of LIST.
9299 Also, weed out any non-string entries, because the return value is used
9300 only for regexp matching.
9301 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
9302 point to the symbol `emacs', indicating that the file should
9303 be opened in Emacs."
9304 (append
9305 (delq nil
9306 (mapcar (lambda (x)
9307 (if (not (stringp (car x)))
9309 (if (string-match "\\W" (car x))
9311 (cons (concat "\\." (car x) "\\'") (cdr x)))))
9312 list))
9313 (if add-auto-mode
9314 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
9316 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
9317 (defun org-file-remote-p (file)
9318 "Test whether FILE specifies a location on a remote system.
9319 Return non-nil if the location is indeed remote.
9321 For example, the filename \"/user@host:/foo\" specifies a location
9322 on the system \"/user@host:\"."
9323 (cond ((fboundp 'file-remote-p)
9324 (file-remote-p file))
9325 ((fboundp 'tramp-handle-file-remote-p)
9326 (tramp-handle-file-remote-p file))
9327 ((and (boundp 'ange-ftp-name-format)
9328 (string-match (car ange-ftp-name-format) file))
9330 (t nil)))
9333 ;;;; Refiling
9335 (defun org-get-org-file ()
9336 "Read a filename, with default directory `org-directory'."
9337 (let ((default (or org-default-notes-file remember-data-file)))
9338 (read-file-name (format "File name [%s]: " default)
9339 (file-name-as-directory org-directory)
9340 default)))
9342 (defun org-notes-order-reversed-p ()
9343 "Check if the current file should receive notes in reversed order."
9344 (cond
9345 ((not org-reverse-note-order) nil)
9346 ((eq t org-reverse-note-order) t)
9347 ((not (listp org-reverse-note-order)) nil)
9348 (t (catch 'exit
9349 (let ((all org-reverse-note-order)
9350 entry)
9351 (while (setq entry (pop all))
9352 (if (string-match (car entry) buffer-file-name)
9353 (throw 'exit (cdr entry))))
9354 nil)))))
9356 (defvar org-refile-target-table nil
9357 "The list of refile targets, created by `org-refile'.")
9359 (defvar org-agenda-new-buffers nil
9360 "Buffers created to visit agenda files.")
9362 (defun org-get-refile-targets (&optional default-buffer)
9363 "Produce a table with refile targets."
9364 (let ((case-fold-search nil)
9365 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
9366 (entries (or org-refile-targets '((nil . (:level . 1)))))
9367 targets txt re files f desc descre fast-path-p level pos0)
9368 (message "Getting targets...")
9369 (with-current-buffer (or default-buffer (current-buffer))
9370 (while (setq entry (pop entries))
9371 (setq files (car entry) desc (cdr entry))
9372 (setq fast-path-p nil)
9373 (cond
9374 ((null files) (setq files (list (current-buffer))))
9375 ((eq files 'org-agenda-files)
9376 (setq files (org-agenda-files 'unrestricted)))
9377 ((and (symbolp files) (fboundp files))
9378 (setq files (funcall files)))
9379 ((and (symbolp files) (boundp files))
9380 (setq files (symbol-value files))))
9381 (if (stringp files) (setq files (list files)))
9382 (cond
9383 ((eq (car desc) :tag)
9384 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
9385 ((eq (car desc) :todo)
9386 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
9387 ((eq (car desc) :regexp)
9388 (setq descre (cdr desc)))
9389 ((eq (car desc) :level)
9390 (setq descre (concat "^\\*\\{" (number-to-string
9391 (if org-odd-levels-only
9392 (1- (* 2 (cdr desc)))
9393 (cdr desc)))
9394 "\\}[ \t]")))
9395 ((eq (car desc) :maxlevel)
9396 (setq fast-path-p t)
9397 (setq descre (concat "^\\*\\{1," (number-to-string
9398 (if org-odd-levels-only
9399 (1- (* 2 (cdr desc)))
9400 (cdr desc)))
9401 "\\}[ \t]")))
9402 (t (error "Bad refiling target description %s" desc)))
9403 (while (setq f (pop files))
9404 (with-current-buffer
9405 (if (bufferp f) f (org-get-agenda-file-buffer f))
9406 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
9407 (setq f (and f (expand-file-name f)))
9408 (if (eq org-refile-use-outline-path 'file)
9409 (push (list (file-name-nondirectory f) f nil nil) targets))
9410 (save-excursion
9411 (save-restriction
9412 (widen)
9413 (goto-char (point-min))
9414 (while (re-search-forward descre nil t)
9415 (goto-char (setq pos0 (point-at-bol)))
9416 (catch 'next
9417 (when org-refile-target-verify-function
9418 (save-match-data
9419 (or (funcall org-refile-target-verify-function)
9420 (throw 'next t))))
9421 (when (looking-at org-complex-heading-regexp)
9422 (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
9423 txt (org-link-display-format (match-string 4))
9424 re (concat "^" (regexp-quote
9425 (buffer-substring (match-beginning 1)
9426 (match-end 4)))))
9427 (if (match-end 5) (setq re (concat re "[ \t]+"
9428 (regexp-quote
9429 (match-string 5)))))
9430 (setq re (concat re "[ \t]*$"))
9431 (when org-refile-use-outline-path
9432 (setq txt (mapconcat 'org-protect-slash
9433 (append
9434 (if (eq org-refile-use-outline-path 'file)
9435 (list (file-name-nondirectory
9436 (buffer-file-name (buffer-base-buffer))))
9437 (if (eq org-refile-use-outline-path 'full-file-path)
9438 (list (buffer-file-name (buffer-base-buffer)))))
9439 (org-get-outline-path fast-path-p level txt)
9440 (list txt))
9441 "/")))
9442 (push (list txt f re (point)) targets)))
9443 (when (= (point) pos0)
9444 ;; verification function has not moved point
9445 (goto-char (point-at-eol))))))))))
9446 (message "Getting targets...done")
9447 (nreverse targets)))
9449 (defun org-protect-slash (s)
9450 (while (string-match "/" s)
9451 (setq s (replace-match "\\" t t s)))
9454 (defvar org-olpa (make-vector 20 nil))
9456 (defun org-get-outline-path (&optional fastp level heading)
9457 "Return the outline path to the current entry, as a list.
9458 The parameters FASTP, LEVEL, and HEADING are for use be a scanner
9459 routine which makes outline path derivations for an entire file,
9460 avoiding backtracing."
9461 (if fastp
9462 (progn
9463 (if (> level 19)
9464 (error "Outline path failure, more than 19 levels."))
9465 (loop for i from level upto 19 do
9466 (aset org-olpa i nil))
9467 (prog1
9468 (delq nil (append org-olpa nil))
9469 (aset org-olpa level heading)))
9470 (let (rtn case-fold-search)
9471 (save-excursion
9472 (save-restriction
9473 (widen)
9474 (while (org-up-heading-safe)
9475 (when (looking-at org-complex-heading-regexp)
9476 (push (org-match-string-no-properties 4) rtn)))
9477 rtn)))))
9479 (defun org-format-outline-path (path &optional width prefix)
9480 "Format the outlie path PATH for display.
9481 Width is the maximum number of characters that is available.
9482 Prefix is a prefix to be included in the returned string,
9483 such as the file name."
9484 (setq width (or width 79))
9485 (if prefix (setq width (- width (length prefix))))
9486 (if (not path)
9487 (or prefix "")
9488 (let* ((nsteps (length path))
9489 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
9490 (maxwidth (if (<= total-width width)
9491 10000 ;; everything fits
9492 ;; we need to shorten the level headings
9493 (/ (- width nsteps) nsteps)))
9494 (org-odd-levels-only nil)
9495 (n 0)
9496 (total (1+ (length prefix))))
9497 (setq maxwidth (max maxwidth 10))
9498 (concat prefix
9499 (mapconcat
9500 (lambda (h)
9501 (setq n (1+ n))
9502 (if (and (= n nsteps) (< maxwidth 10000))
9503 (setq maxwidth (- total-width total)))
9504 (if (< (length h) maxwidth)
9505 (progn (setq total (+ total (length h) 1)) h)
9506 (setq h (substring h 0 (- maxwidth 2))
9507 total (+ total maxwidth 1))
9508 (if (string-match "[ \t]+\\'" h)
9509 (setq h (substring h 0 (match-beginning 0))))
9510 (setq h (concat h "..")))
9511 (org-add-props h nil 'face
9512 (nth (% (1- n) org-n-level-faces)
9513 org-level-faces))
9515 path "/")))))
9517 (defun org-display-outline-path (&optional file current)
9518 "Display the current outline path in the echo area."
9519 (interactive "P")
9520 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
9521 (case-fold-search nil)
9522 (path (and (org-mode-p) (org-get-outline-path))))
9523 (if current (setq path (append path
9524 (save-excursion
9525 (org-back-to-heading t)
9526 (if (looking-at org-complex-heading-regexp)
9527 (list (match-string 4)))))))
9528 (message "%s"
9529 (org-format-outline-path
9530 path
9531 (1- (frame-width))
9532 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
9534 (defvar org-refile-history nil
9535 "History for refiling operations.")
9537 (defvar org-after-refile-insert-hook nil
9538 "Hook run after `org-refile' has inserted its stuff at the new location.
9539 Note that this is still *before* the stuff will be removed from
9540 the *old* location.")
9542 (defun org-refile (&optional goto default-buffer rfloc)
9543 "Move the entry at point to another heading.
9544 The list of target headings is compiled using the information in
9545 `org-refile-targets', which see. This list is created before each use
9546 and will therefore always be up-to-date.
9548 At the target location, the entry is filed as a subitem of the target heading.
9549 Depending on `org-reverse-note-order', the new subitem will either be the
9550 first or the last subitem.
9552 If there is an active region, all entries in that region will be moved.
9553 However, the region must fulfil the requirement that the first heading
9554 is the first one sets the top-level of the moved text - at most siblings
9555 below it are allowed.
9557 With prefix arg GOTO, the command will only visit the target location,
9558 not actually move anything.
9559 With a double prefix `C-u C-u', go to the location where the last refiling
9560 operation has put the subtree.
9561 With a prefix argument of `2', refile to the running clock.
9563 RFLOC can be a refile location obtained in a different way.
9565 See also `org-refile-use-outline-path' and `org-completion-use-ido'"
9566 (interactive "P")
9567 (let* ((cbuf (current-buffer))
9568 (regionp (org-region-active-p))
9569 (region-start (and regionp (region-beginning)))
9570 (region-end (and regionp (region-end)))
9571 (region-length (and regionp (- region-end region-start)))
9572 (filename (buffer-file-name (buffer-base-buffer cbuf)))
9573 pos it nbuf file re level reversed)
9574 (setq last-command nil)
9575 (when regionp
9576 (goto-char region-start)
9577 (or (bolp) (goto-char (point-at-bol)))
9578 (setq region-start (point))
9579 (unless (org-kill-is-subtree-p
9580 (buffer-substring region-start region-end))
9581 (error "The region is not a (sequence of) subtree(s)")))
9582 (if (equal goto '(16))
9583 (org-refile-goto-last-stored)
9584 (when (or
9585 (and (equal goto 2)
9586 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
9587 (prog1
9588 (setq it (list (or org-clock-heading "running clock")
9589 (buffer-file-name
9590 (marker-buffer org-clock-hd-marker))
9592 (marker-position org-clock-hd-marker)))
9593 (setq goto nil)))
9594 (setq it (or rfloc
9595 (save-excursion
9596 (org-refile-get-location
9597 (if goto "Goto: " "Refile to: ") default-buffer
9598 org-refile-allow-creating-parent-nodes)))))
9599 (setq file (nth 1 it)
9600 re (nth 2 it)
9601 pos (nth 3 it))
9602 (if (and (not goto)
9604 (equal (buffer-file-name) file)
9605 (if regionp
9606 (and (>= pos region-start)
9607 (<= pos region-end))
9608 (and (>= pos (point))
9609 (< pos (save-excursion
9610 (org-end-of-subtree t t))))))
9611 (error "Cannot refile to position inside the tree or region"))
9613 (setq nbuf (or (find-buffer-visiting file)
9614 (find-file-noselect file)))
9615 (if goto
9616 (progn
9617 (switch-to-buffer nbuf)
9618 (goto-char pos)
9619 (org-show-context 'org-goto))
9620 (if regionp
9621 (progn
9622 (org-kill-new (buffer-substring region-start region-end))
9623 (org-save-markers-in-region region-start region-end))
9624 (org-copy-subtree 1 nil t))
9625 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
9626 (find-file-noselect file)))
9627 (setq reversed (org-notes-order-reversed-p))
9628 (save-excursion
9629 (save-restriction
9630 (widen)
9631 (if pos
9632 (progn
9633 (goto-char pos)
9634 (looking-at outline-regexp)
9635 (setq level (org-get-valid-level (funcall outline-level) 1))
9636 (goto-char
9637 (if reversed
9638 (or (outline-next-heading) (point-max))
9639 (or (save-excursion (org-get-next-sibling))
9640 (org-end-of-subtree t t)
9641 (point-max)))))
9642 (setq level 1)
9643 (if (not reversed)
9644 (goto-char (point-max))
9645 (goto-char (point-min))
9646 (or (outline-next-heading) (goto-char (point-max)))))
9647 (if (not (bolp)) (newline))
9648 (org-paste-subtree level)
9649 (when org-log-refile
9650 (org-add-log-setup 'refile nil nil 'findpos
9651 org-log-refile)
9652 (unless (eq org-log-refile 'note)
9653 (save-excursion (org-add-log-note))))
9654 (and org-auto-align-tags (org-set-tags nil t))
9655 (bookmark-set "org-refile-last-stored")
9656 (if (fboundp 'deactivate-mark) (deactivate-mark))
9657 (run-hooks 'org-after-refile-insert-hook))))
9658 (if regionp
9659 (delete-region (point) (+ (point) region-length))
9660 (org-cut-subtree))
9661 (when (featurep 'org-inlinetask)
9662 (org-inlinetask-remove-END-maybe))
9663 (setq org-markers-to-move nil)
9664 (message "Refiled to \"%s\"" (car it)))))))
9666 (defun org-refile-goto-last-stored ()
9667 "Go to the location where the last refile was stored."
9668 (interactive)
9669 (bookmark-jump "org-refile-last-stored")
9670 (message "This is the location of the last refile"))
9672 (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
9673 "Prompt the user for a refile location, using PROMPT."
9674 (let ((org-refile-targets org-refile-targets)
9675 (org-refile-use-outline-path org-refile-use-outline-path))
9676 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
9677 (unless org-refile-target-table
9678 (error "No refile targets"))
9679 (let* ((cbuf (current-buffer))
9680 (partial-completion-mode nil)
9681 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
9682 (cfunc (if (and org-refile-use-outline-path
9683 org-outline-path-complete-in-steps)
9684 'org-olpath-completing-read
9685 'org-icompleting-read))
9686 (extra (if org-refile-use-outline-path "/" ""))
9687 (filename (and cfn (expand-file-name cfn)))
9688 (tbl (mapcar
9689 (lambda (x)
9690 (if (and (not (member org-refile-use-outline-path
9691 '(file full-file-path)))
9692 (not (equal filename (nth 1 x))))
9693 (cons (concat (car x) extra " ("
9694 (file-name-nondirectory (nth 1 x)) ")")
9695 (cdr x))
9696 (cons (concat (car x) extra) (cdr x))))
9697 org-refile-target-table))
9698 (completion-ignore-case t)
9699 pa answ parent-target child parent old-hist)
9700 (setq old-hist org-refile-history)
9701 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
9702 nil 'org-refile-history))
9703 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
9704 (if pa
9705 (progn
9706 (when (or (not org-refile-history)
9707 (not (eq old-hist org-refile-history))
9708 (not (equal (car pa) (car org-refile-history))))
9709 (setq org-refile-history
9710 (cons (car pa) (if (assoc (car org-refile-history) tbl)
9711 org-refile-history
9712 (cdr org-refile-history))))
9713 (if (equal (car org-refile-history) (nth 1 org-refile-history))
9714 (pop org-refile-history)))
9716 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
9717 (progn
9718 (setq parent (match-string 1 answ)
9719 child (match-string 2 answ))
9720 (setq parent-target (or (assoc parent tbl)
9721 (assoc (concat parent "/") tbl)))
9722 (when (and parent-target
9723 (or (eq new-nodes t)
9724 (and (eq new-nodes 'confirm)
9725 (y-or-n-p (format "Create new node \"%s\"? "
9726 child)))))
9727 (org-refile-new-child parent-target child)))
9728 (error "Invalid target location")))))
9730 (defun org-refile-new-child (parent-target child)
9731 "Use refile target PARENT-TARGET to add new CHILD below it."
9732 (unless parent-target
9733 (error "Cannot find parent for new node"))
9734 (let ((file (nth 1 parent-target))
9735 (pos (nth 3 parent-target))
9736 level)
9737 (with-current-buffer (or (find-buffer-visiting file)
9738 (find-file-noselect file))
9739 (save-excursion
9740 (save-restriction
9741 (widen)
9742 (if pos
9743 (goto-char pos)
9744 (goto-char (point-max))
9745 (if (not (bolp)) (newline)))
9746 (when (looking-at outline-regexp)
9747 (setq level (funcall outline-level))
9748 (org-end-of-subtree t t))
9749 (org-back-over-empty-lines)
9750 (insert "\n" (make-string
9751 (if pos (org-get-valid-level level 1) 1) ?*)
9752 " " child "\n")
9753 (beginning-of-line 0)
9754 (list (concat (car parent-target) "/" child) file "" (point)))))))
9756 (defun org-olpath-completing-read (prompt collection &rest args)
9757 "Read an outline path like a file name."
9758 (let ((thetable collection)
9759 (org-completion-use-ido nil) ; does not work with ido.
9760 (org-completion-use-iswitchb nil)) ; or iswitchb
9761 (apply
9762 'org-icompleting-read prompt
9763 (lambda (string predicate &optional flag)
9764 (let (rtn r f (l (length string)))
9765 (cond
9766 ((eq flag nil)
9767 ;; try completion
9768 (try-completion string thetable))
9769 ((eq flag t)
9770 ;; all-completions
9771 (setq rtn (all-completions string thetable predicate))
9772 (mapcar
9773 (lambda (x)
9774 (setq r (substring x l))
9775 (if (string-match " ([^)]*)$" x)
9776 (setq f (match-string 0 x))
9777 (setq f ""))
9778 (if (string-match "/" r)
9779 (concat string (substring r 0 (match-end 0)) f)
9781 rtn))
9782 ((eq flag 'lambda)
9783 ;; exact match?
9784 (assoc string thetable)))
9786 args)))
9788 ;;;; Dynamic blocks
9790 (defun org-find-dblock (name)
9791 "Find the first dynamic block with name NAME in the buffer.
9792 If not found, stay at current position and return nil."
9793 (let (pos)
9794 (save-excursion
9795 (goto-char (point-min))
9796 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
9797 nil t)
9798 (match-beginning 0))))
9799 (if pos (goto-char pos))
9800 pos))
9802 (defconst org-dblock-start-re
9803 "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
9804 "Matches the start line of a dynamic block, with parameters.")
9806 (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
9807 "Matches the end of a dynamic block.")
9809 (defun org-create-dblock (plist)
9810 "Create a dynamic block section, with parameters taken from PLIST.
9811 PLIST must contain a :name entry which is used as name of the block."
9812 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
9813 (end-of-line 1)
9814 (newline))
9815 (let ((col (current-column))
9816 (name (plist-get plist :name)))
9817 (insert "#+BEGIN: " name)
9818 (while plist
9819 (if (eq (car plist) :name)
9820 (setq plist (cddr plist))
9821 (insert " " (prin1-to-string (pop plist)))))
9822 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
9823 (beginning-of-line -2)))
9825 (defun org-prepare-dblock ()
9826 "Prepare dynamic block for refresh.
9827 This empties the block, puts the cursor at the insert position and returns
9828 the property list including an extra property :name with the block name."
9829 (unless (looking-at org-dblock-start-re)
9830 (error "Not at a dynamic block"))
9831 (let* ((begdel (1+ (match-end 0)))
9832 (name (org-no-properties (match-string 1)))
9833 (params (append (list :name name)
9834 (read (concat "(" (match-string 3) ")")))))
9835 (save-excursion
9836 (beginning-of-line 1)
9837 (skip-chars-forward " \t")
9838 (setq params (plist-put params :indentation-column (current-column))))
9839 (unless (re-search-forward org-dblock-end-re nil t)
9840 (error "Dynamic block not terminated"))
9841 (setq params
9842 (append params
9843 (list :content (buffer-substring
9844 begdel (match-beginning 0)))))
9845 (delete-region begdel (match-beginning 0))
9846 (goto-char begdel)
9847 (open-line 1)
9848 params))
9850 (defun org-map-dblocks (&optional command)
9851 "Apply COMMAND to all dynamic blocks in the current buffer.
9852 If COMMAND is not given, use `org-update-dblock'."
9853 (let ((cmd (or command 'org-update-dblock)))
9854 (save-excursion
9855 (goto-char (point-min))
9856 (while (re-search-forward org-dblock-start-re nil t)
9857 (goto-char (match-beginning 0))
9858 (save-excursion
9859 (condition-case nil
9860 (funcall cmd)
9861 (error (message "Error during update of dynamic block"))))
9862 (unless (re-search-forward org-dblock-end-re nil t)
9863 (error "Dynamic block not terminated"))))))
9865 (defun org-dblock-update (&optional arg)
9866 "User command for updating dynamic blocks.
9867 Update the dynamic block at point. With prefix ARG, update all dynamic
9868 blocks in the buffer."
9869 (interactive "P")
9870 (if arg
9871 (org-update-all-dblocks)
9872 (or (looking-at org-dblock-start-re)
9873 (org-beginning-of-dblock))
9874 (org-update-dblock)))
9876 (defun org-update-dblock ()
9877 "Update the dynamic block at point
9878 This means to empty the block, parse for parameters and then call
9879 the correct writing function."
9880 (save-window-excursion
9881 (let* ((pos (point))
9882 (line (org-current-line))
9883 (params (org-prepare-dblock))
9884 (name (plist-get params :name))
9885 (indent (plist-get params :indentation-column))
9886 (cmd (intern (concat "org-dblock-write:" name))))
9887 (message "Updating dynamic block `%s' at line %d..." name line)
9888 (funcall cmd params)
9889 (message "Updating dynamic block `%s' at line %d...done" name line)
9890 (goto-char pos)
9891 (when (and indent (> indent 0))
9892 (setq indent (make-string indent ?\ ))
9893 (save-excursion
9894 (org-beginning-of-dblock)
9895 (forward-line 1)
9896 (while (not (looking-at org-dblock-end-re))
9897 (insert indent)
9898 (beginning-of-line 2))
9899 (when (looking-at org-dblock-end-re)
9900 (and (looking-at "[ \t]+")
9901 (replace-match ""))
9902 (insert indent)))))))
9904 (defun org-beginning-of-dblock ()
9905 "Find the beginning of the dynamic block at point.
9906 Error if there is no such block at point."
9907 (let ((pos (point))
9908 beg)
9909 (end-of-line 1)
9910 (if (and (re-search-backward org-dblock-start-re nil t)
9911 (setq beg (match-beginning 0))
9912 (re-search-forward org-dblock-end-re nil t)
9913 (> (match-end 0) pos))
9914 (goto-char beg)
9915 (goto-char pos)
9916 (error "Not in a dynamic block"))))
9918 (defun org-update-all-dblocks ()
9919 "Update all dynamic blocks in the buffer.
9920 This function can be used in a hook."
9921 (when (org-mode-p)
9922 (org-map-dblocks 'org-update-dblock)))
9925 ;;;; Completion
9927 (defconst org-additional-option-like-keywords
9928 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
9929 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
9930 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
9931 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
9932 "BEGIN:" "END:"
9933 "ORGTBL" "TBLFM:" "TBLNAME:"
9934 "BEGIN_EXAMPLE" "END_EXAMPLE"
9935 "BEGIN_QUOTE" "END_QUOTE"
9936 "BEGIN_VERSE" "END_VERSE"
9937 "BEGIN_CENTER" "END_CENTER"
9938 "BEGIN_SRC" "END_SRC"
9939 "CATEGORY" "COLUMNS"
9940 "CAPTION" "LABEL"
9941 "SETUPFILE"
9942 "BIND"
9943 "MACRO"))
9945 (defcustom org-structure-template-alist
9947 ("s" "#+begin_src ?\n\n#+end_src"
9948 "<src lang=\"?\">\n\n</src>")
9949 ("e" "#+begin_example\n?\n#+end_example"
9950 "<example>\n?\n</example>")
9951 ("q" "#+begin_quote\n?\n#+end_quote"
9952 "<quote>\n?\n</quote>")
9953 ("v" "#+begin_verse\n?\n#+end_verse"
9954 "<verse>\n?\n/verse>")
9955 ("c" "#+begin_center\n?\n#+end_center"
9956 "<center>\n?\n/center>")
9957 ("l" "#+begin_latex\n?\n#+end_latex"
9958 "<literal style=\"latex\">\n?\n</literal>")
9959 ("L" "#+latex: "
9960 "<literal style=\"latex\">?</literal>")
9961 ("h" "#+begin_html\n?\n#+end_html"
9962 "<literal style=\"html\">\n?\n</literal>")
9963 ("H" "#+html: "
9964 "<literal style=\"html\">?</literal>")
9965 ("a" "#+begin_ascii\n?\n#+end_ascii")
9966 ("A" "#+ascii: ")
9967 ("i" "#+include %file ?"
9968 "<include file=%file markup=\"?\">")
9970 "Structure completion elements.
9971 This is a list of abbreviation keys and values. The value gets inserted
9972 if you type `<' followed by the key and then press the completion key,
9973 usually `M-TAB'. %file will be replaced by a file name after prompting
9974 for the file using completion.
9975 There are two templates for each key, the first uses the original Org syntax,
9976 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
9977 the default when the /org-mtags.el/ module has been loaded. See also the
9978 variable `org-mtags-prefer-muse-templates'.
9979 This is an experimental feature, it is undecided if it is going to stay in."
9980 :group 'org-completion
9981 :type '(repeat
9982 (string :tag "Key")
9983 (string :tag "Template")
9984 (string :tag "Muse Template")))
9986 (defun org-try-structure-completion ()
9987 "Try to complete a structure template before point.
9988 This looks for strings like \"<e\" on an otherwise empty line and
9989 expands them."
9990 (let ((l (buffer-substring (point-at-bol) (point)))
9992 (when (and (looking-at "[ \t]*$")
9993 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
9994 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
9995 (org-complete-expand-structure-template (+ -1 (point-at-bol)
9996 (match-beginning 1)) a)
9997 t)))
9999 (defun org-complete-expand-structure-template (start cell)
10000 "Expand a structure template."
10001 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
10002 (rpl (nth (if musep 2 1) cell))
10003 (ind ""))
10004 (delete-region start (point))
10005 (when (string-match "\\`#\\+" rpl)
10006 (cond
10007 ((bolp))
10008 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
10009 (setq ind (buffer-substring (point-at-bol) (point))))
10010 (t (newline))))
10011 (setq start (point))
10012 (if (string-match "%file" rpl)
10013 (setq rpl (replace-match
10014 (concat
10015 "\""
10016 (save-match-data
10017 (abbreviate-file-name (read-file-name "Include file: ")))
10018 "\"")
10019 t t rpl)))
10020 (setq rpl (mapconcat 'identity (split-string rpl "\n")
10021 (concat "\n" ind)))
10022 (insert rpl)
10023 (if (re-search-backward "\\?" start t) (delete-char 1))))
10026 (defun org-complete (&optional arg)
10027 "Perform completion on word at point.
10028 At the beginning of a headline, this completes TODO keywords as given in
10029 `org-todo-keywords'.
10030 If the current word is preceded by a backslash, completes the TeX symbols
10031 that are supported for HTML support.
10032 If the current word is preceded by \"#+\", completes special words for
10033 setting file options.
10034 In the line after \"#+STARTUP:, complete valid keywords.\"
10035 At all other locations, this simply calls the value of
10036 `org-completion-fallback-command'."
10037 (interactive "P")
10038 (org-without-partial-completion
10039 (catch 'exit
10040 (let* ((a nil)
10041 (end (point))
10042 (beg1 (save-excursion
10043 (skip-chars-backward (org-re "[:alnum:]_@"))
10044 (point)))
10045 (beg (save-excursion
10046 (skip-chars-backward "a-zA-Z0-9_:$")
10047 (point)))
10048 (confirm (lambda (x) (stringp (car x))))
10049 (searchhead (equal (char-before beg) ?*))
10050 (struct
10051 (when (and (member (char-before beg1) '(?. ?<))
10052 (setq a (assoc (buffer-substring beg1 (point))
10053 org-structure-template-alist)))
10054 (org-complete-expand-structure-template (1- beg1) a)
10055 (throw 'exit t)))
10056 (tag (and (equal (char-before beg1) ?:)
10057 (equal (char-after (point-at-bol)) ?*)))
10058 (prop (and (equal (char-before beg1) ?:)
10059 (not (equal (char-after (point-at-bol)) ?*))))
10060 (texp (equal (char-before beg) ?\\))
10061 (link (equal (char-before beg) ?\[))
10062 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
10063 beg)
10064 "#+"))
10065 (startup (string-match "^#\\+STARTUP:.*"
10066 (buffer-substring (point-at-bol) (point))))
10067 (completion-ignore-case opt)
10068 (type nil)
10069 (tbl nil)
10070 (table (cond
10071 (opt
10072 (setq type :opt)
10073 (require 'org-exp)
10074 (append
10075 (delq nil
10076 (mapcar
10077 (lambda (x)
10078 (if (string-match
10079 "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
10080 (cons (match-string 2 x)
10081 (match-string 1 x))))
10082 (org-split-string (org-get-current-options) "\n")))
10083 (mapcar 'list org-additional-option-like-keywords)))
10084 (startup
10085 (setq type :startup)
10086 org-startup-options)
10087 (link (append org-link-abbrev-alist-local
10088 org-link-abbrev-alist))
10089 (texp
10090 (setq type :tex)
10091 (append org-entities-user org-entities))
10092 ((string-match "\\`\\*+[ \t]+\\'"
10093 (buffer-substring (point-at-bol) beg))
10094 (setq type :todo)
10095 (mapcar 'list org-todo-keywords-1))
10096 (searchhead
10097 (setq type :searchhead)
10098 (save-excursion
10099 (goto-char (point-min))
10100 (while (re-search-forward org-todo-line-regexp nil t)
10101 (push (list
10102 (org-make-org-heading-search-string
10103 (match-string 3) t))
10104 tbl)))
10105 tbl)
10106 (tag (setq type :tag beg beg1)
10107 (or org-tag-alist (org-get-buffer-tags)))
10108 (prop (setq type :prop beg beg1)
10109 (mapcar 'list (org-buffer-property-keys nil t t)))
10110 (t (progn
10111 (call-interactively org-completion-fallback-command)
10112 (throw 'exit nil)))))
10113 (pattern (buffer-substring-no-properties beg end))
10114 (completion (try-completion pattern table confirm)))
10115 (cond ((eq completion t)
10116 (if (not (assoc (upcase pattern) table))
10117 (message "Already complete")
10118 (if (and (equal type :opt)
10119 (not (member (car (assoc (upcase pattern) table))
10120 org-additional-option-like-keywords)))
10121 (insert (substring (cdr (assoc (upcase pattern) table))
10122 (length pattern)))
10123 (if (memq type '(:tag :prop)) (insert ":")))))
10124 ((null completion)
10125 (message "Can't find completion for \"%s\"" pattern)
10126 (ding))
10127 ((not (string= pattern completion))
10128 (delete-region beg end)
10129 (if (string-match " +$" completion)
10130 (setq completion (replace-match "" t t completion)))
10131 (insert completion)
10132 (if (get-buffer-window "*Completions*")
10133 (delete-window (get-buffer-window "*Completions*")))
10134 (if (assoc completion table)
10135 (if (eq type :todo) (insert " ")
10136 (if (memq type '(:tag :prop)) (insert ":"))))
10137 (if (and (equal type :opt) (assoc completion table))
10138 (message "%s" (substitute-command-keys
10139 "Press \\[org-complete] again to insert example settings"))))
10141 (message "Making completion list...")
10142 (let ((list (sort (all-completions pattern table confirm)
10143 'string<)))
10144 (with-output-to-temp-buffer "*Completions*"
10145 (condition-case nil
10146 ;; Protection needed for XEmacs and emacs 21
10147 (display-completion-list list pattern)
10148 (error (display-completion-list list)))))
10149 (message "Making completion list...%s" "done")))))))
10151 ;;;; TODO, DEADLINE, Comments
10153 (defun org-toggle-comment ()
10154 "Change the COMMENT state of an entry."
10155 (interactive)
10156 (save-excursion
10157 (org-back-to-heading)
10158 (let (case-fold-search)
10159 (if (looking-at (concat outline-regexp
10160 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
10161 (replace-match "" t t nil 1)
10162 (if (looking-at outline-regexp)
10163 (progn
10164 (goto-char (match-end 0))
10165 (insert org-comment-string " ")))))))
10167 (defvar org-last-todo-state-is-todo nil
10168 "This is non-nil when the last TODO state change led to a TODO state.
10169 If the last change removed the TODO tag or switched to DONE, then
10170 this is nil.")
10172 (defvar org-setting-tags nil) ; dynamically skipped
10174 (defun org-parse-local-options (string var)
10175 "Parse STRING for startup setting relevant for variable VAR."
10176 (let ((rtn (symbol-value var))
10177 e opts)
10178 (save-match-data
10179 (if (or (not string) (not (string-match "\\S-" string)))
10181 (setq opts (delq nil (mapcar (lambda (x)
10182 (setq e (assoc x org-startup-options))
10183 (if (eq (nth 1 e) var) e nil))
10184 (org-split-string string "[ \t]+"))))
10185 (if (not opts)
10187 (setq rtn nil)
10188 (while (setq e (pop opts))
10189 (if (not (nth 3 e))
10190 (setq rtn (nth 2 e))
10191 (if (not (listp rtn)) (setq rtn nil))
10192 (push (nth 2 e) rtn)))
10193 rtn)))))
10195 (defvar org-todo-setup-filter-hook nil
10196 "Hook for functions that pre-filter todo specs.
10198 Each function takes a todo spec and returns either `nil' or the spec
10199 transformed into canonical form." )
10201 (defvar org-todo-get-default-hook nil
10202 "Hook for functions that get a default item for todo.
10204 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
10205 `nil' or a string to be used for the todo mark." )
10207 (defvar org-agenda-headline-snapshot-before-repeat)
10209 (defun org-todo (&optional arg)
10210 "Change the TODO state of an item.
10211 The state of an item is given by a keyword at the start of the heading,
10212 like
10213 *** TODO Write paper
10214 *** DONE Call mom
10216 The different keywords are specified in the variable `org-todo-keywords'.
10217 By default the available states are \"TODO\" and \"DONE\".
10218 So for this example: when the item starts with TODO, it is changed to DONE.
10219 When it starts with DONE, the DONE is removed. And when neither TODO nor
10220 DONE are present, add TODO at the beginning of the heading.
10222 With C-u prefix arg, use completion to determine the new state.
10223 With numeric prefix arg, switch to that state.
10224 With a double C-u prefix, switch to the next set of TODO keywords (nextset).
10225 With a triple C-u prefix, circumvent any state blocking.
10227 For calling through lisp, arg is also interpreted in the following way:
10228 'none -> empty state
10229 \"\"(empty string) -> switch to empty state
10230 'done -> switch to DONE
10231 'nextset -> switch to the next set of keywords
10232 'previousset -> switch to the previous set of keywords
10233 \"WAITING\" -> switch to the specified keyword, but only if it
10234 really is a member of `org-todo-keywords'."
10235 (interactive "P")
10236 (if (equal arg '(16)) (setq arg 'nextset))
10237 (let ((org-blocker-hook org-blocker-hook)
10238 (case-fold-search nil))
10239 (when (equal arg '(64))
10240 (setq arg nil org-blocker-hook nil))
10241 (when (and org-blocker-hook
10242 (or org-inhibit-blocking
10243 (org-entry-get nil "NOBLOCKING")))
10244 (setq org-blocker-hook nil))
10245 (save-excursion
10246 (catch 'exit
10247 (org-back-to-heading t)
10248 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
10249 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
10250 (looking-at " *"))
10251 (let* ((match-data (match-data))
10252 (startpos (point-at-bol))
10253 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
10254 (org-log-done org-log-done)
10255 (org-log-repeat org-log-repeat)
10256 (org-todo-log-states org-todo-log-states)
10257 (this (match-string 1))
10258 (hl-pos (match-beginning 0))
10259 (head (org-get-todo-sequence-head this))
10260 (ass (assoc head org-todo-kwd-alist))
10261 (interpret (nth 1 ass))
10262 (done-word (nth 3 ass))
10263 (final-done-word (nth 4 ass))
10264 (last-state (or this ""))
10265 (completion-ignore-case t)
10266 (member (member this org-todo-keywords-1))
10267 (tail (cdr member))
10268 (state (cond
10269 ((and org-todo-key-trigger
10270 (or (and (equal arg '(4))
10271 (eq org-use-fast-todo-selection 'prefix))
10272 (and (not arg) org-use-fast-todo-selection
10273 (not (eq org-use-fast-todo-selection
10274 'prefix)))))
10275 ;; Use fast selection
10276 (org-fast-todo-selection))
10277 ((and (equal arg '(4))
10278 (or (not org-use-fast-todo-selection)
10279 (not org-todo-key-trigger)))
10280 ;; Read a state with completion
10281 (org-icompleting-read
10282 "State: " (mapcar (lambda(x) (list x))
10283 org-todo-keywords-1)
10284 nil t))
10285 ((eq arg 'right)
10286 (if this
10287 (if tail (car tail) nil)
10288 (car org-todo-keywords-1)))
10289 ((eq arg 'left)
10290 (if (equal member org-todo-keywords-1)
10292 (if this
10293 (nth (- (length org-todo-keywords-1)
10294 (length tail) 2)
10295 org-todo-keywords-1)
10296 (org-last org-todo-keywords-1))))
10297 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
10298 (setq arg nil))) ; hack to fall back to cycling
10299 (arg
10300 ;; user or caller requests a specific state
10301 (cond
10302 ((equal arg "") nil)
10303 ((eq arg 'none) nil)
10304 ((eq arg 'done) (or done-word (car org-done-keywords)))
10305 ((eq arg 'nextset)
10306 (or (car (cdr (member head org-todo-heads)))
10307 (car org-todo-heads)))
10308 ((eq arg 'previousset)
10309 (let ((org-todo-heads (reverse org-todo-heads)))
10310 (or (car (cdr (member head org-todo-heads)))
10311 (car org-todo-heads))))
10312 ((car (member arg org-todo-keywords-1)))
10313 ((stringp arg)
10314 (error "State `%s' not valid in this file" arg))
10315 ((nth (1- (prefix-numeric-value arg))
10316 org-todo-keywords-1))))
10317 ((null member) (or head (car org-todo-keywords-1)))
10318 ((equal this final-done-word) nil) ;; -> make empty
10319 ((null tail) nil) ;; -> first entry
10320 ((memq interpret '(type priority))
10321 (if (eq this-command last-command)
10322 (car tail)
10323 (if (> (length tail) 0)
10324 (or done-word (car org-done-keywords))
10325 nil)))
10327 (car tail))))
10328 (state (or
10329 (run-hook-with-args-until-success
10330 'org-todo-get-default-hook state last-state)
10331 state))
10332 (next (if state (concat " " state " ") " "))
10333 (change-plist (list :type 'todo-state-change :from this :to state
10334 :position startpos))
10335 dolog now-done-p)
10336 (when org-blocker-hook
10337 (setq org-last-todo-state-is-todo
10338 (not (member this org-done-keywords)))
10339 (unless (save-excursion
10340 (save-match-data
10341 (run-hook-with-args-until-failure
10342 'org-blocker-hook change-plist)))
10343 (if (interactive-p)
10344 (error "TODO state change from %s to %s blocked" this state)
10345 ;; fail silently
10346 (message "TODO state change from %s to %s blocked" this state)
10347 (throw 'exit nil))))
10348 (store-match-data match-data)
10349 (replace-match next t t)
10350 (unless (pos-visible-in-window-p hl-pos)
10351 (message "TODO state changed to %s" (org-trim next)))
10352 (unless head
10353 (setq head (org-get-todo-sequence-head state)
10354 ass (assoc head org-todo-kwd-alist)
10355 interpret (nth 1 ass)
10356 done-word (nth 3 ass)
10357 final-done-word (nth 4 ass)))
10358 (when (memq arg '(nextset previousset))
10359 (message "Keyword-Set %d/%d: %s"
10360 (- (length org-todo-sets) -1
10361 (length (memq (assoc state org-todo-sets) org-todo-sets)))
10362 (length org-todo-sets)
10363 (mapconcat 'identity (assoc state org-todo-sets) " ")))
10364 (setq org-last-todo-state-is-todo
10365 (not (member state org-done-keywords)))
10366 (setq now-done-p (and (member state org-done-keywords)
10367 (not (member this org-done-keywords))))
10368 (and logging (org-local-logging logging))
10369 (when (and (or org-todo-log-states org-log-done)
10370 (not (eq org-inhibit-logging t))
10371 (not (memq arg '(nextset previousset))))
10372 ;; we need to look at recording a time and note
10373 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
10374 (nth 2 (assoc this org-todo-log-states))))
10375 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
10376 (setq dolog 'time))
10377 (when (and state
10378 (member state org-not-done-keywords)
10379 (not (member this org-not-done-keywords)))
10380 ;; This is now a todo state and was not one before
10381 ;; If there was a CLOSED time stamp, get rid of it.
10382 (org-add-planning-info nil nil 'closed))
10383 (when (and now-done-p org-log-done)
10384 ;; It is now done, and it was not done before
10385 (org-add-planning-info 'closed (org-current-time))
10386 (if (and (not dolog) (eq 'note org-log-done))
10387 (org-add-log-setup 'done state this 'findpos 'note)))
10388 (when (and state dolog)
10389 ;; This is a non-nil state, and we need to log it
10390 (org-add-log-setup 'state state this 'findpos dolog)))
10391 ;; Fixup tag positioning
10392 (org-todo-trigger-tag-changes state)
10393 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
10394 (when org-provide-todo-statistics
10395 (org-update-parent-todo-statistics))
10396 (run-hooks 'org-after-todo-state-change-hook)
10397 (if (and arg (not (member state org-done-keywords)))
10398 (setq head (org-get-todo-sequence-head state)))
10399 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
10400 ;; Do we need to trigger a repeat?
10401 (when now-done-p
10402 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
10403 ;; This is for the agenda, take a snapshot of the headline.
10404 (save-match-data
10405 (setq org-agenda-headline-snapshot-before-repeat
10406 (org-get-heading))))
10407 (org-auto-repeat-maybe state))
10408 ;; Fixup cursor location if close to the keyword
10409 (if (and (outline-on-heading-p)
10410 (not (bolp))
10411 (save-excursion (beginning-of-line 1)
10412 (looking-at org-todo-line-regexp))
10413 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
10414 (progn
10415 (goto-char (or (match-end 2) (match-end 1)))
10416 (and (looking-at " ") (just-one-space))))
10417 (when org-trigger-hook
10418 (save-excursion
10419 (run-hook-with-args 'org-trigger-hook change-plist))))))))
10421 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
10422 "Block turning an entry into a TODO, using the hierarchy.
10423 This checks whether the current task should be blocked from state
10424 changes. Such blocking occurs when:
10426 1. The task has children which are not all in a completed state.
10428 2. A task has a parent with the property :ORDERED:, and there
10429 are siblings prior to the current task with incomplete
10430 status.
10432 3. The parent of the task is blocked because it has siblings that should
10433 be done first, or is child of a block grandparent TODO entry."
10435 (if (not org-enforce-todo-dependencies)
10436 t ; if locally turned off don't block
10437 (catch 'dont-block
10438 ;; If this is not a todo state change, or if this entry is already DONE,
10439 ;; do not block
10440 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
10441 (member (plist-get change-plist :from)
10442 (cons 'done org-done-keywords))
10443 (member (plist-get change-plist :to)
10444 (cons 'todo org-not-done-keywords))
10445 (not (plist-get change-plist :to)))
10446 (throw 'dont-block t))
10447 ;; If this task has children, and any are undone, it's blocked
10448 (save-excursion
10449 (org-back-to-heading t)
10450 (let ((this-level (funcall outline-level)))
10451 (outline-next-heading)
10452 (let ((child-level (funcall outline-level)))
10453 (while (and (not (eobp))
10454 (> child-level this-level))
10455 ;; this todo has children, check whether they are all
10456 ;; completed
10457 (if (and (not (org-entry-is-done-p))
10458 (org-entry-is-todo-p))
10459 (throw 'dont-block nil))
10460 (outline-next-heading)
10461 (setq child-level (funcall outline-level))))))
10462 ;; Otherwise, if the task's parent has the :ORDERED: property, and
10463 ;; any previous siblings are undone, it's blocked
10464 (save-excursion
10465 (org-back-to-heading t)
10466 (let* ((pos (point))
10467 (parent-pos (and (org-up-heading-safe) (point))))
10468 (if (not parent-pos) (throw 'dont-block t)) ; no parent
10469 (when (and (org-entry-get (point) "ORDERED")
10470 (forward-line 1)
10471 (re-search-forward org-not-done-heading-regexp pos t))
10472 (throw 'dont-block nil)) ; block, there is an older sibling not done.
10473 ;; Search further up the hierarchy, to see if an anchestor is blocked
10474 (while t
10475 (goto-char parent-pos)
10476 (if (not (looking-at org-not-done-heading-regexp))
10477 (throw 'dont-block t)) ; do not block, parent is not a TODO
10478 (setq pos (point))
10479 (setq parent-pos (and (org-up-heading-safe) (point)))
10480 (if (not parent-pos) (throw 'dont-block t)) ; no parent
10481 (when (and (org-entry-get (point) "ORDERED")
10482 (forward-line 1)
10483 (re-search-forward org-not-done-heading-regexp pos t))
10484 (throw 'dont-block nil)))))))) ; block, older sibling not done.
10486 (defcustom org-track-ordered-property-with-tag nil
10487 "Should the ORDERED property also be shown as a tag?
10488 The ORDERED property decides if an entry should require subtasks to be
10489 completed in sequence. Since a property is not very visible, setting
10490 this option means that toggling the ORDERED property with the command
10491 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
10492 not relevant for the behavior, but it makes things more visible.
10494 Note that toggling the tag with tags commands will not change the property
10495 and therefore not influence behavior!
10497 This can be t, meaning the tag ORDERED should be used, It can also be a
10498 string to select a different tag for this task."
10499 :group 'org-todo
10500 :type '(choice
10501 (const :tag "No tracking" nil)
10502 (const :tag "Track with ORDERED tag" t)
10503 (string :tag "Use other tag")))
10505 (defun org-toggle-ordered-property ()
10506 "Toggle the ORDERED property of the current entry.
10507 For better visibility, you can track the value of this property with a tag.
10508 See variable `org-track-ordered-property-with-tag'."
10509 (interactive)
10510 (let* ((t1 org-track-ordered-property-with-tag)
10511 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
10512 (save-excursion
10513 (org-back-to-heading)
10514 (if (org-entry-get nil "ORDERED")
10515 (progn
10516 (org-delete-property "ORDERED")
10517 (and tag (org-toggle-tag tag 'off))
10518 (message "Subtasks can be completed in arbitrary order"))
10519 (org-entry-put nil "ORDERED" "t")
10520 (and tag (org-toggle-tag tag 'on))
10521 (message "Subtasks must be completed in sequence")))))
10523 (defvar org-blocked-by-checkboxes) ; dynamically scoped
10524 (defun org-block-todo-from-checkboxes (change-plist)
10525 "Block turning an entry into a TODO, using checkboxes.
10526 This checks whether the current task should be blocked from state
10527 changes because there are unchecked boxes in this entry."
10528 (if (not org-enforce-todo-checkbox-dependencies)
10529 t ; if locally turned off don't block
10530 (catch 'dont-block
10531 ;; If this is not a todo state change, or if this entry is already DONE,
10532 ;; do not block
10533 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
10534 (member (plist-get change-plist :from)
10535 (cons 'done org-done-keywords))
10536 (member (plist-get change-plist :to)
10537 (cons 'todo org-not-done-keywords))
10538 (not (plist-get change-plist :to)))
10539 (throw 'dont-block t))
10540 ;; If this task has checkboxes that are not checked, it's blocked
10541 (save-excursion
10542 (org-back-to-heading t)
10543 (let ((beg (point)) end)
10544 (outline-next-heading)
10545 (setq end (point))
10546 (goto-char beg)
10547 (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
10548 end t)
10549 (progn
10550 (if (boundp 'org-blocked-by-checkboxes)
10551 (setq org-blocked-by-checkboxes t))
10552 (throw 'dont-block nil)))))
10553 t))) ; do not block
10555 (defun org-entry-blocked-p ()
10556 "Is the current entry blocked?"
10557 (if (org-entry-get nil "NOBLOCKING")
10558 nil ;; Never block this entry
10559 (not
10560 (run-hook-with-args-until-failure
10561 'org-blocker-hook
10562 (list :type 'todo-state-change
10563 :position (point)
10564 :from 'todo
10565 :to 'done)))))
10567 (defun org-update-statistics-cookies (all)
10568 "Update the statistics cookie, either from TODO or from checkboxes.
10569 This should be called with the cursor in a line with a statistics cookie."
10570 (interactive "P")
10571 (if all
10572 (progn
10573 (org-update-checkbox-count 'all)
10574 (org-map-entries 'org-update-parent-todo-statistics))
10575 (if (not (org-on-heading-p))
10576 (org-update-checkbox-count)
10577 (let ((pos (move-marker (make-marker) (point)))
10578 end l1 l2)
10579 (ignore-errors (org-back-to-heading t))
10580 (if (not (org-on-heading-p))
10581 (org-update-checkbox-count)
10582 (setq l1 (org-outline-level))
10583 (setq end (save-excursion
10584 (outline-next-heading)
10585 (if (org-on-heading-p) (setq l2 (org-outline-level)))
10586 (point)))
10587 (if (and (save-excursion
10588 (re-search-forward
10589 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
10590 (not (save-excursion (re-search-forward
10591 ":COOKIE_DATA:.*\\<todo\\>" end t))))
10592 (org-update-checkbox-count)
10593 (if (and l2 (> l2 l1))
10594 (progn
10595 (goto-char end)
10596 (org-update-parent-todo-statistics))
10597 (goto-char pos)
10598 (beginning-of-line 1)
10599 (while (re-search-forward
10600 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
10601 (point-at-eol) t)
10602 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
10603 (goto-char pos)
10604 (move-marker pos nil)))))
10606 (defvar org-entry-property-inherited-from) ;; defined below
10607 (defun org-update-parent-todo-statistics ()
10608 "Update any statistics cookie in the parent of the current headline.
10609 When `org-hierarchical-todo-statistics' is nil, statistics will cover
10610 the entire subtree and this will travel up the hierarchy and update
10611 statistics everywhere."
10612 (interactive)
10613 (let* ((lim 0) prop
10614 (recursive (or (not org-hierarchical-todo-statistics)
10615 (string-match
10616 "\\<recursive\\>"
10617 (or (setq prop (org-entry-get
10618 nil "COOKIE_DATA" 'inherit)) ""))))
10619 (lim (or (and prop (marker-position
10620 org-entry-property-inherited-from))
10621 lim))
10622 (first t)
10623 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
10624 level ltoggle l1 new ndel
10625 (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
10626 (catch 'exit
10627 (save-excursion
10628 (beginning-of-line 1)
10629 (if (org-at-heading-p)
10630 (setq ltoggle (funcall outline-level))
10631 (error "This should not happen"))
10632 (while (and (setq level (org-up-heading-safe))
10633 (or recursive first)
10634 (>= (point) lim))
10635 (setq first nil cookie-present nil)
10636 (unless (and level
10637 (not (string-match
10638 "\\<checkbox\\>"
10639 (downcase
10640 (or (org-entry-get
10641 nil "COOKIE_DATA")
10642 "")))))
10643 (throw 'exit nil))
10644 (while (re-search-forward box-re (point-at-eol) t)
10645 (setq cnt-all 0 cnt-done 0 cookie-present t)
10646 (setq is-percent (match-end 2))
10647 (save-match-data
10648 (unless (outline-next-heading) (throw 'exit nil))
10649 (while (and (looking-at org-complex-heading-regexp)
10650 (> (setq l1 (length (match-string 1))) level))
10651 (setq kwd (and (or recursive (= l1 ltoggle))
10652 (match-string 2)))
10653 (if (or (eq org-provide-todo-statistics 'all-headlines)
10654 (and (listp org-provide-todo-statistics)
10655 (or (member kwd org-provide-todo-statistics)
10656 (member kwd org-done-keywords))))
10657 (setq cnt-all (1+ cnt-all))
10658 (if (eq org-provide-todo-statistics t)
10659 (and kwd (setq cnt-all (1+ cnt-all)))))
10660 (and (member kwd org-done-keywords)
10661 (setq cnt-done (1+ cnt-done)))
10662 (outline-next-heading)))
10663 (setq new
10664 (if is-percent
10665 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
10666 (format "[%d/%d]" cnt-done cnt-all))
10667 ndel (- (match-end 0) (match-beginning 0)))
10668 (goto-char (match-beginning 0))
10669 (insert new)
10670 (delete-region (point) (+ (point) ndel)))
10671 (when cookie-present
10672 (run-hook-with-args 'org-after-todo-statistics-hook
10673 cnt-done (- cnt-all cnt-done))))))
10674 (run-hooks 'org-todo-statistics-hook)))
10676 (defvar org-after-todo-statistics-hook nil
10677 "Hook that is called after a TODO statistics cookie has been updated.
10678 Each function is called with two arguments: the number of not-done entries
10679 and the number of done entries.
10681 For example, the following function, when added to this hook, will switch
10682 an entry to DONE when all children are done, and back to TODO when new
10683 entries are set to a TODO status. Note that this hook is only called
10684 when there is a statistics cookie in the headline!
10686 (defun org-summary-todo (n-done n-not-done)
10687 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
10688 (let (org-log-done org-log-states) ; turn off logging
10689 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
10692 (defvar org-todo-statistics-hook nil
10693 "Hook that is run whenever Org thinks TODO statistics should be updated.
10694 This hook runs even if there is no statistics cookie present, in which case
10695 `org-after-todo-statistics-hook' would not run.")
10697 (defun org-todo-trigger-tag-changes (state)
10698 "Apply the changes defined in `org-todo-state-tags-triggers'."
10699 (let ((l org-todo-state-tags-triggers)
10700 changes)
10701 (when (or (not state) (equal state ""))
10702 (setq changes (append changes (cdr (assoc "" l)))))
10703 (when (and (stringp state) (> (length state) 0))
10704 (setq changes (append changes (cdr (assoc state l)))))
10705 (when (member state org-not-done-keywords)
10706 (setq changes (append changes (cdr (assoc 'todo l)))))
10707 (when (member state org-done-keywords)
10708 (setq changes (append changes (cdr (assoc 'done l)))))
10709 (dolist (c changes)
10710 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
10712 (defun org-local-logging (value)
10713 "Get logging settings from a property VALUE."
10714 (let* (words w a)
10715 ;; directly set the variables, they are already local.
10716 (setq org-log-done nil
10717 org-log-repeat nil
10718 org-todo-log-states nil)
10719 (setq words (org-split-string value))
10720 (while (setq w (pop words))
10721 (cond
10722 ((setq a (assoc w org-startup-options))
10723 (and (member (nth 1 a) '(org-log-done org-log-repeat))
10724 (set (nth 1 a) (nth 2 a))))
10725 ((setq a (org-extract-log-state-settings w))
10726 (and (member (car a) org-todo-keywords-1)
10727 (push a org-todo-log-states)))))))
10729 (defun org-get-todo-sequence-head (kwd)
10730 "Return the head of the TODO sequence to which KWD belongs.
10731 If KWD is not set, check if there is a text property remembering the
10732 right sequence."
10733 (let (p)
10734 (cond
10735 ((not kwd)
10736 (or (get-text-property (point-at-bol) 'org-todo-head)
10737 (progn
10738 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
10739 nil (point-at-eol)))
10740 (get-text-property p 'org-todo-head))))
10741 ((not (member kwd org-todo-keywords-1))
10742 (car org-todo-keywords-1))
10743 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
10745 (defun org-fast-todo-selection ()
10746 "Fast TODO keyword selection with single keys.
10747 Returns the new TODO keyword, or nil if no state change should occur."
10748 (let* ((fulltable org-todo-key-alist)
10749 (done-keywords org-done-keywords) ;; needed for the faces.
10750 (maxlen (apply 'max (mapcar
10751 (lambda (x)
10752 (if (stringp (car x)) (string-width (car x)) 0))
10753 fulltable)))
10754 (expert nil)
10755 (fwidth (+ maxlen 3 1 3))
10756 (ncol (/ (- (window-width) 4) fwidth))
10757 tg cnt e c tbl
10758 groups ingroup)
10759 (save-excursion
10760 (save-window-excursion
10761 (if expert
10762 (set-buffer (get-buffer-create " *Org todo*"))
10763 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
10764 (erase-buffer)
10765 (org-set-local 'org-done-keywords done-keywords)
10766 (setq tbl fulltable cnt 0)
10767 (while (setq e (pop tbl))
10768 (cond
10769 ((equal e '(:startgroup))
10770 (push '() groups) (setq ingroup t)
10771 (when (not (= cnt 0))
10772 (setq cnt 0)
10773 (insert "\n"))
10774 (insert "{ "))
10775 ((equal e '(:endgroup))
10776 (setq ingroup nil cnt 0)
10777 (insert "}\n"))
10778 ((equal e '(:newline))
10779 (when (not (= cnt 0))
10780 (setq cnt 0)
10781 (insert "\n")
10782 (setq e (car tbl))
10783 (while (equal (car tbl) '(:newline))
10784 (insert "\n")
10785 (setq tbl (cdr tbl)))))
10787 (setq tg (car e) c (cdr e))
10788 (if ingroup (push tg (car groups)))
10789 (setq tg (org-add-props tg nil 'face
10790 (org-get-todo-face tg)))
10791 (if (and (= cnt 0) (not ingroup)) (insert " "))
10792 (insert "[" c "] " tg (make-string
10793 (- fwidth 4 (length tg)) ?\ ))
10794 (when (= (setq cnt (1+ cnt)) ncol)
10795 (insert "\n")
10796 (if ingroup (insert " "))
10797 (setq cnt 0)))))
10798 (insert "\n")
10799 (goto-char (point-min))
10800 (if (not expert) (org-fit-window-to-buffer))
10801 (message "[a-z..]:Set [SPC]:clear")
10802 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
10803 (cond
10804 ((or (= c ?\C-g)
10805 (and (= c ?q) (not (rassoc c fulltable))))
10806 (setq quit-flag t))
10807 ((= c ?\ ) nil)
10808 ((setq e (rassoc c fulltable) tg (car e))
10810 (t (setq quit-flag t)))))))
10812 (defun org-entry-is-todo-p ()
10813 (member (org-get-todo-state) org-not-done-keywords))
10815 (defun org-entry-is-done-p ()
10816 (member (org-get-todo-state) org-done-keywords))
10818 (defun org-get-todo-state ()
10819 (save-excursion
10820 (org-back-to-heading t)
10821 (and (looking-at org-todo-line-regexp)
10822 (match-end 2)
10823 (match-string 2))))
10825 (defun org-at-date-range-p (&optional inactive-ok)
10826 "Is the cursor inside a date range?"
10827 (interactive)
10828 (save-excursion
10829 (catch 'exit
10830 (let ((pos (point)))
10831 (skip-chars-backward "^[<\r\n")
10832 (skip-chars-backward "<[")
10833 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
10834 (>= (match-end 0) pos)
10835 (throw 'exit t))
10836 (skip-chars-backward "^<[\r\n")
10837 (skip-chars-backward "<[")
10838 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
10839 (>= (match-end 0) pos)
10840 (throw 'exit t)))
10841 nil)))
10843 (defun org-get-repeat (&optional tagline)
10844 "Check if there is a deadline/schedule with repeater in this entry."
10845 (save-match-data
10846 (save-excursion
10847 (org-back-to-heading t)
10848 (and (re-search-forward (if tagline
10849 (concat tagline "\\s-*" org-repeat-re)
10850 org-repeat-re)
10851 (org-entry-end-position) t)
10852 (match-string-no-properties 1)))))
10854 (defvar org-last-changed-timestamp)
10855 (defvar org-last-inserted-timestamp)
10856 (defvar org-log-post-message)
10857 (defvar org-log-note-purpose)
10858 (defvar org-log-note-how)
10859 (defvar org-log-note-extra)
10860 (defun org-auto-repeat-maybe (done-word)
10861 "Check if the current headline contains a repeated deadline/schedule.
10862 If yes, set TODO state back to what it was and change the base date
10863 of repeating deadline/scheduled time stamps to new date.
10864 This function is run automatically after each state change to a DONE state."
10865 ;; last-state is dynamically scoped into this function
10866 (let* ((repeat (org-get-repeat))
10867 (aa (assoc last-state org-todo-kwd-alist))
10868 (interpret (nth 1 aa))
10869 (head (nth 2 aa))
10870 (whata '(("d" . day) ("m" . month) ("y" . year)))
10871 (msg "Entry repeats: ")
10872 (org-log-done nil)
10873 (org-todo-log-states nil)
10874 (nshiftmax 10) (nshift 0)
10875 re type n what ts time to-state)
10876 (when repeat
10877 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
10878 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
10879 org-todo-repeat-to-state))
10880 (unless (and to-state (member to-state org-todo-keywords-1))
10881 (setq to-state (if (eq interpret 'type) last-state head)))
10882 (org-todo to-state)
10883 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
10884 (org-entry-put nil "LAST_REPEAT" (format-time-string
10885 (org-time-stamp-format t t))))
10886 (when org-log-repeat
10887 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
10888 (memq 'org-add-log-note post-command-hook))
10889 ;; OK, we are already setup for some record
10890 (if (eq org-log-repeat 'note)
10891 ;; make sure we take a note, not only a time stamp
10892 (setq org-log-note-how 'note))
10893 ;; Set up for taking a record
10894 (org-add-log-setup 'state (or done-word (car org-done-keywords))
10895 last-state
10896 'findpos org-log-repeat)))
10897 (org-back-to-heading t)
10898 (org-add-planning-info nil nil 'closed)
10899 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
10900 org-deadline-time-regexp "\\)\\|\\("
10901 org-ts-regexp "\\)"))
10902 (while (re-search-forward
10903 re (save-excursion (outline-next-heading) (point)) t)
10904 (setq type (if (match-end 1) org-scheduled-string
10905 (if (match-end 3) org-deadline-string "Plain:"))
10906 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
10907 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
10908 (setq n (string-to-number (match-string 2 ts))
10909 what (match-string 3 ts))
10910 (if (equal what "w") (setq n (* n 7) what "d"))
10911 ;; Preparation, see if we need to modify the start date for the change
10912 (when (match-end 1)
10913 (setq time (save-match-data (org-time-string-to-time ts)))
10914 (cond
10915 ((equal (match-string 1 ts) ".")
10916 ;; Shift starting date to today
10917 (org-timestamp-change
10918 (- (time-to-days (current-time)) (time-to-days time))
10919 'day))
10920 ((equal (match-string 1 ts) "+")
10921 (while (or (= nshift 0)
10922 (<= (time-to-days time) (time-to-days (current-time))))
10923 (when (= (incf nshift) nshiftmax)
10924 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
10925 (error "Abort")))
10926 (org-timestamp-change n (cdr (assoc what whata)))
10927 (org-at-timestamp-p t)
10928 (setq ts (match-string 1))
10929 (setq time (save-match-data (org-time-string-to-time ts))))
10930 (org-timestamp-change (- n) (cdr (assoc what whata)))
10931 ;; rematch, so that we have everything in place for the real shift
10932 (org-at-timestamp-p t)
10933 (setq ts (match-string 1))
10934 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
10935 (org-timestamp-change n (cdr (assoc what whata)))
10936 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
10937 (setq org-log-post-message msg)
10938 (message "%s" msg))))
10940 (defun org-show-todo-tree (arg)
10941 "Make a compact tree which shows all headlines marked with TODO.
10942 The tree will show the lines where the regexp matches, and all higher
10943 headlines above the match.
10944 With a \\[universal-argument] prefix, prompt for a regexp to match.
10945 With a numeric prefix N, construct a sparse tree for the Nth element
10946 of `org-todo-keywords-1'."
10947 (interactive "P")
10948 (let ((case-fold-search nil)
10949 (kwd-re
10950 (cond ((null arg) org-not-done-regexp)
10951 ((equal arg '(4))
10952 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
10953 (mapcar 'list org-todo-keywords-1))))
10954 (concat "\\("
10955 (mapconcat 'identity (org-split-string kwd "|") "\\|")
10956 "\\)\\>")))
10957 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
10958 (regexp-quote (nth (1- (prefix-numeric-value arg))
10959 org-todo-keywords-1)))
10960 (t (error "Invalid prefix argument: %s" arg)))))
10961 (message "%d TODO entries found"
10962 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
10964 (defun org-deadline (&optional remove time)
10965 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
10966 With argument REMOVE, remove any deadline from the item.
10967 When TIME is set, it should be an internal time specification, and the
10968 scheduling will use the corresponding date."
10969 (interactive "P")
10970 (let* ((old-date (org-entry-get nil "DEADLINE"))
10971 (repeater (and old-date
10972 (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
10973 (match-string 1 old-date))))
10974 (if remove
10975 (progn
10976 (when (and old-date org-log-redeadline)
10977 (org-add-log-setup 'deldeadline nil old-date 'findpos
10978 org-log-redeadline))
10979 (org-remove-timestamp-with-keyword org-deadline-string)
10980 (message "Item no longer has a deadline."))
10981 (org-add-planning-info 'deadline time 'closed)
10982 (when (and old-date org-log-redeadline
10983 (not (equal old-date
10984 (substring org-last-inserted-timestamp 1 -1))))
10985 (org-add-log-setup 'redeadline nil old-date 'findpos
10986 org-log-redeadline))
10987 (when repeater
10988 (save-excursion
10989 (org-back-to-heading t)
10990 (when (re-search-forward (concat org-deadline-string " "
10991 org-last-inserted-timestamp)
10992 (save-excursion
10993 (outline-next-heading) (point)) t)
10994 (goto-char (1- (match-end 0)))
10995 (insert " " repeater)
10996 (setq org-last-inserted-timestamp
10997 (concat (substring org-last-inserted-timestamp 0 -1)
10998 " " repeater
10999 (substring org-last-inserted-timestamp -1))))))
11000 (message "Deadline on %s" org-last-inserted-timestamp))))
11002 (defun org-schedule (&optional remove time)
11003 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
11004 With argument REMOVE, remove any scheduling date from the item.
11005 When TIME is set, it should be an internal time specification, and the
11006 scheduling will use the corresponding date."
11007 (interactive "P")
11008 (let* ((old-date (org-entry-get nil "SCHEDULED"))
11009 (repeater (and old-date
11010 (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
11011 (match-string 1 old-date))))
11012 (if remove
11013 (progn
11014 (when (and old-date org-log-reschedule)
11015 (org-add-log-setup 'delschedule nil old-date 'findpos
11016 org-log-reschedule))
11017 (org-remove-timestamp-with-keyword org-scheduled-string)
11018 (message "Item is no longer scheduled."))
11019 (org-add-planning-info 'scheduled time 'closed)
11020 (when (and old-date org-log-reschedule
11021 (not (equal old-date
11022 (substring org-last-inserted-timestamp 1 -1))))
11023 (org-add-log-setup 'reschedule nil old-date 'findpos
11024 org-log-reschedule))
11025 (when repeater
11026 (save-excursion
11027 (org-back-to-heading t)
11028 (when (re-search-forward (concat org-scheduled-string " "
11029 org-last-inserted-timestamp)
11030 (save-excursion
11031 (outline-next-heading) (point)) t)
11032 (goto-char (1- (match-end 0)))
11033 (insert " " repeater)
11034 (setq org-last-inserted-timestamp
11035 (concat (substring org-last-inserted-timestamp 0 -1)
11036 " " repeater
11037 (substring org-last-inserted-timestamp -1))))))
11038 (message "Scheduled to %s" org-last-inserted-timestamp))))
11040 (defun org-get-scheduled-time (pom &optional inherit)
11041 "Get the scheduled time as a time tuple, of a format suitable
11042 for calling org-schedule with, or if there is no scheduling,
11043 returns nil."
11044 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
11045 (when time
11046 (apply 'encode-time (org-parse-time-string time)))))
11048 (defun org-get-deadline-time (pom &optional inherit)
11049 "Get the deadine as a time tuple, of a format suitable for
11050 calling org-deadline with, or if there is no scheduling, returns
11051 nil."
11052 (let ((time (org-entry-get pom "DEADLINE" inherit)))
11053 (when time
11054 (apply 'encode-time (org-parse-time-string time)))))
11056 (defun org-remove-timestamp-with-keyword (keyword)
11057 "Remove all time stamps with KEYWORD in the current entry."
11058 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
11059 beg)
11060 (save-excursion
11061 (org-back-to-heading t)
11062 (setq beg (point))
11063 (outline-next-heading)
11064 (while (re-search-backward re beg t)
11065 (replace-match "")
11066 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
11067 (equal (char-before) ?\ ))
11068 (backward-delete-char 1)
11069 (if (string-match "^[ \t]*$" (buffer-substring
11070 (point-at-bol) (point-at-eol)))
11071 (delete-region (point-at-bol)
11072 (min (point-max) (1+ (point-at-eol))))))))))
11074 (defun org-add-planning-info (what &optional time &rest remove)
11075 "Insert new timestamp with keyword in the line directly after the headline.
11076 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
11077 If non is given, the user is prompted for a date.
11078 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
11079 be removed."
11080 (interactive)
11081 (let (org-time-was-given org-end-time-was-given ts
11082 end default-time default-input)
11084 (catch 'exit
11085 (when (and (not time) (memq what '(scheduled deadline)))
11086 ;; Try to get a default date/time from existing timestamp
11087 (save-excursion
11088 (org-back-to-heading t)
11089 (setq end (save-excursion (outline-next-heading) (point)))
11090 (when (re-search-forward (if (eq what 'scheduled)
11091 org-scheduled-time-regexp
11092 org-deadline-time-regexp)
11093 end t)
11094 (setq ts (match-string 1)
11095 default-time
11096 (apply 'encode-time (org-parse-time-string ts))
11097 default-input (and ts (org-get-compact-tod ts))))))
11098 (when what
11099 ;; If necessary, get the time from the user
11100 (setq time (or time (org-read-date nil 'to-time nil nil
11101 default-time default-input))))
11103 (when (and org-insert-labeled-timestamps-at-point
11104 (member what '(scheduled deadline)))
11105 (insert
11106 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
11107 (org-insert-time-stamp time org-time-was-given
11108 nil nil nil (list org-end-time-was-given))
11109 (setq what nil))
11110 (save-excursion
11111 (save-restriction
11112 (let (col list elt ts buffer-invisibility-spec)
11113 (org-back-to-heading t)
11114 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
11115 (goto-char (match-end 1))
11116 (setq col (current-column))
11117 (goto-char (match-end 0))
11118 (if (eobp) (insert "\n") (forward-char 1))
11119 (when (and (not what)
11120 (not (looking-at
11121 (concat "[ \t]*"
11122 org-keyword-time-not-clock-regexp))))
11123 ;; Nothing to add, nothing to remove...... :-)
11124 (throw 'exit nil))
11125 (if (and (not (looking-at outline-regexp))
11126 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
11127 "[^\r\n]*"))
11128 (not (equal (match-string 1) org-clock-string)))
11129 (narrow-to-region (match-beginning 0) (match-end 0))
11130 (insert-before-markers "\n")
11131 (backward-char 1)
11132 (narrow-to-region (point) (point))
11133 (and org-adapt-indentation (org-indent-to-column col)))
11134 ;; Check if we have to remove something.
11135 (setq list (cons what remove))
11136 (while list
11137 (setq elt (pop list))
11138 (goto-char (point-min))
11139 (when (or (and (eq elt 'scheduled)
11140 (re-search-forward org-scheduled-time-regexp nil t))
11141 (and (eq elt 'deadline)
11142 (re-search-forward org-deadline-time-regexp nil t))
11143 (and (eq elt 'closed)
11144 (re-search-forward org-closed-time-regexp nil t)))
11145 (replace-match "")
11146 (if (looking-at "--+<[^>]+>") (replace-match ""))
11147 (skip-chars-backward " ")
11148 (if (looking-at " +") (replace-match ""))))
11149 (goto-char (point-max))
11150 (and org-adapt-indentation (bolp) (org-indent-to-column col))
11151 (when what
11152 (insert
11153 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
11154 (cond ((eq what 'scheduled) org-scheduled-string)
11155 ((eq what 'deadline) org-deadline-string)
11156 ((eq what 'closed) org-closed-string))
11157 " ")
11158 (setq ts (org-insert-time-stamp
11159 time
11160 (or org-time-was-given
11161 (and (eq what 'closed) org-log-done-with-time))
11162 (eq what 'closed)
11163 nil nil (list org-end-time-was-given)))
11164 (end-of-line 1))
11165 (goto-char (point-min))
11166 (widen)
11167 (if (and (looking-at "[ \t]+\n")
11168 (equal (char-before) ?\n))
11169 (delete-region (1- (point)) (point-at-eol)))
11170 ts))))))
11172 (defvar org-log-note-marker (make-marker))
11173 (defvar org-log-note-purpose nil)
11174 (defvar org-log-note-state nil)
11175 (defvar org-log-note-previous-state nil)
11176 (defvar org-log-note-how nil)
11177 (defvar org-log-note-extra nil)
11178 (defvar org-log-note-window-configuration nil)
11179 (defvar org-log-note-return-to (make-marker))
11180 (defvar org-log-post-message nil
11181 "Message to be displayed after a log note has been stored.
11182 The auto-repeater uses this.")
11184 (defun org-add-note ()
11185 "Add a note to the current entry.
11186 This is done in the same way as adding a state change note."
11187 (interactive)
11188 (org-add-log-setup 'note nil nil 'findpos nil))
11190 (defvar org-property-end-re)
11191 (defun org-add-log-setup (&optional purpose state prev-state
11192 findpos how &optional extra)
11193 "Set up the post command hook to take a note.
11194 If this is about to TODO state change, the new state is expected in STATE.
11195 When FINDPOS is non-nil, find the correct position for the note in
11196 the current entry. If not, assume that it can be inserted at point.
11197 HOW is an indicator what kind of note should be created.
11198 EXTRA is additional text that will be inserted into the notes buffer."
11199 (let* ((org-log-into-drawer (org-log-into-drawer))
11200 (drawer (cond ((stringp org-log-into-drawer)
11201 org-log-into-drawer)
11202 (org-log-into-drawer "LOGBOOK")
11203 (t nil))))
11204 (save-restriction
11205 (save-excursion
11206 (when findpos
11207 (org-back-to-heading t)
11208 (narrow-to-region (point) (save-excursion
11209 (outline-next-heading) (point)))
11210 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
11211 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
11212 "[^\r\n]*\\)?"))
11213 (goto-char (match-end 0))
11214 (cond
11215 (drawer
11216 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
11217 nil t)
11218 (progn
11219 (goto-char (match-end 0))
11220 (or org-log-states-order-reversed
11221 (and (re-search-forward org-property-end-re nil t)
11222 (goto-char (1- (match-beginning 0))))))
11223 (insert "\n:" drawer ":\n:END:")
11224 (beginning-of-line 0)
11225 (org-indent-line-function)
11226 (beginning-of-line 2)
11227 (org-indent-line-function)
11228 (end-of-line 0)))
11229 ((and org-log-state-notes-insert-after-drawers
11230 (save-excursion
11231 (forward-line) (looking-at org-drawer-regexp)))
11232 (forward-line)
11233 (while (looking-at org-drawer-regexp)
11234 (goto-char (match-end 0))
11235 (re-search-forward org-property-end-re (point-max) t)
11236 (forward-line))
11237 (forward-line -1)))
11238 (unless org-log-states-order-reversed
11239 (and (= (char-after) ?\n) (forward-char 1))
11240 (org-skip-over-state-notes)
11241 (skip-chars-backward " \t\n\r")))
11242 (move-marker org-log-note-marker (point))
11243 (setq org-log-note-purpose purpose
11244 org-log-note-state state
11245 org-log-note-previous-state prev-state
11246 org-log-note-how how
11247 org-log-note-extra extra)
11248 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
11250 (defun org-skip-over-state-notes ()
11251 "Skip past the list of State notes in an entry."
11252 (if (looking-at "\n[ \t]*- State") (forward-char 1))
11253 (while (looking-at "[ \t]*- State")
11254 (condition-case nil
11255 (org-next-item)
11256 (error (org-end-of-item)))))
11258 (defun org-add-log-note (&optional purpose)
11259 "Pop up a window for taking a note, and add this note later at point."
11260 (remove-hook 'post-command-hook 'org-add-log-note)
11261 (setq org-log-note-window-configuration (current-window-configuration))
11262 (delete-other-windows)
11263 (move-marker org-log-note-return-to (point))
11264 (switch-to-buffer (marker-buffer org-log-note-marker))
11265 (goto-char org-log-note-marker)
11266 (org-switch-to-buffer-other-window "*Org Note*")
11267 (erase-buffer)
11268 (if (memq org-log-note-how '(time state))
11269 (let (current-prefix-arg) (org-store-log-note))
11270 (let ((org-inhibit-startup t)) (org-mode))
11271 (insert (format "# Insert note for %s.
11272 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
11273 (cond
11274 ((eq org-log-note-purpose 'clock-out) "stopped clock")
11275 ((eq org-log-note-purpose 'done) "closed todo item")
11276 ((eq org-log-note-purpose 'state)
11277 (format "state change from \"%s\" to \"%s\""
11278 (or org-log-note-previous-state "")
11279 (or org-log-note-state "")))
11280 ((eq org-log-note-purpose 'reschedule)
11281 "rescheduling")
11282 ((eq org-log-note-purpose 'delschedule)
11283 "no longer scheduled")
11284 ((eq org-log-note-purpose 'redeadline)
11285 "changing deadline")
11286 ((eq org-log-note-purpose 'deldeadline)
11287 "removing deadline")
11288 ((eq org-log-note-purpose 'refile)
11289 "refiling")
11290 ((eq org-log-note-purpose 'note)
11291 "this entry")
11292 (t (error "This should not happen")))))
11293 (if org-log-note-extra (insert org-log-note-extra))
11294 (org-set-local 'org-finish-function 'org-store-log-note)))
11296 (defvar org-note-abort nil) ; dynamically scoped
11297 (defun org-store-log-note ()
11298 "Finish taking a log note, and insert it to where it belongs."
11299 (let ((txt (buffer-string))
11300 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
11301 lines ind)
11302 (kill-buffer (current-buffer))
11303 (while (string-match "\\`#.*\n[ \t\n]*" txt)
11304 (setq txt (replace-match "" t t txt)))
11305 (if (string-match "\\s-+\\'" txt)
11306 (setq txt (replace-match "" t t txt)))
11307 (setq lines (org-split-string txt "\n"))
11308 (when (and note (string-match "\\S-" note))
11309 (setq note
11310 (org-replace-escapes
11311 note
11312 (list (cons "%u" (user-login-name))
11313 (cons "%U" user-full-name)
11314 (cons "%t" (format-time-string
11315 (org-time-stamp-format 'long 'inactive)
11316 (current-time)))
11317 (cons "%s" (if org-log-note-state
11318 (concat "\"" org-log-note-state "\"")
11319 ""))
11320 (cons "%S" (if org-log-note-previous-state
11321 (concat "\"" org-log-note-previous-state "\"")
11322 "\"\"")))))
11323 (if lines (setq note (concat note " \\\\")))
11324 (push note lines))
11325 (when (or current-prefix-arg org-note-abort)
11326 (when org-log-into-drawer
11327 (org-remove-empty-drawer-at
11328 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
11329 org-log-note-marker))
11330 (setq lines nil))
11331 (when lines
11332 (with-current-buffer (marker-buffer org-log-note-marker)
11333 (save-excursion
11334 (goto-char org-log-note-marker)
11335 (move-marker org-log-note-marker nil)
11336 (end-of-line 1)
11337 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
11338 (insert "- " (pop lines))
11339 (org-indent-line-function)
11340 (beginning-of-line 1)
11341 (looking-at "[ \t]*")
11342 (setq ind (concat (match-string 0) " "))
11343 (end-of-line 1)
11344 (while lines (insert "\n" ind (pop lines)))
11345 (message "Note stored")
11346 (org-back-to-heading t)
11347 (org-cycle-hide-drawers 'children)))))
11348 (set-window-configuration org-log-note-window-configuration)
11349 (with-current-buffer (marker-buffer org-log-note-return-to)
11350 (goto-char org-log-note-return-to))
11351 (move-marker org-log-note-return-to nil)
11352 (and org-log-post-message (message "%s" org-log-post-message)))
11354 (defun org-remove-empty-drawer-at (drawer pos)
11355 "Remove an empty drawer DRAWER at position POS.
11356 POS may also be a marker."
11357 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
11358 (save-excursion
11359 (save-restriction
11360 (widen)
11361 (goto-char pos)
11362 (if (org-in-regexp
11363 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
11364 (replace-match ""))))))
11366 (defun org-sparse-tree (&optional arg)
11367 "Create a sparse tree, prompt for the details.
11368 This command can create sparse trees. You first need to select the type
11369 of match used to create the tree:
11371 t Show entries with a specific TODO keyword.
11372 m Show entries selected by a tags/property match.
11373 p Enter a property name and its value (both with completion on existing
11374 names/values) and show entries with that property.
11375 / Show entries matching a regular expression (`r' can be used as well)
11376 d Show deadlines due within `org-deadline-warning-days'.
11377 b Show deadlines and scheduled items before a date.
11378 a Show deadlines and scheduled items after a date."
11379 (interactive "P")
11380 (let (ans kwd value)
11381 (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
11382 (setq ans (read-char-exclusive))
11383 (cond
11384 ((equal ans ?d)
11385 (call-interactively 'org-check-deadlines))
11386 ((equal ans ?b)
11387 (call-interactively 'org-check-before-date))
11388 ((equal ans ?a)
11389 (call-interactively 'org-check-after-date))
11390 ((equal ans ?t)
11391 (org-show-todo-tree '(4)))
11392 ((member ans '(?T ?m))
11393 (call-interactively 'org-match-sparse-tree))
11394 ((member ans '(?p ?P))
11395 (setq kwd (org-icompleting-read "Property: "
11396 (mapcar 'list (org-buffer-property-keys))))
11397 (setq value (org-icompleting-read "Value: "
11398 (mapcar 'list (org-property-values kwd))))
11399 (unless (string-match "\\`{.*}\\'" value)
11400 (setq value (concat "\"" value "\"")))
11401 (org-match-sparse-tree arg (concat kwd "=" value)))
11402 ((member ans '(?r ?R ?/))
11403 (call-interactively 'org-occur))
11404 (t (error "No such sparse tree command \"%c\"" ans)))))
11406 (defvar org-occur-highlights nil
11407 "List of overlays used for occur matches.")
11408 (make-variable-buffer-local 'org-occur-highlights)
11409 (defvar org-occur-parameters nil
11410 "Parameters of the active org-occur calls.
11411 This is a list, each call to org-occur pushes as cons cell,
11412 containing the regular expression and the callback, onto the list.
11413 The list can contain several entries if `org-occur' has been called
11414 several time with the KEEP-PREVIOUS argument. Otherwise, this list
11415 will only contain one set of parameters. When the highlights are
11416 removed (for example with `C-c C-c', or with the next edit (depending
11417 on `org-remove-highlights-with-change'), this variable is emptied
11418 as well.")
11419 (make-variable-buffer-local 'org-occur-parameters)
11421 (defun org-occur (regexp &optional keep-previous callback)
11422 "Make a compact tree which shows all matches of REGEXP.
11423 The tree will show the lines where the regexp matches, and all higher
11424 headlines above the match. It will also show the heading after the match,
11425 to make sure editing the matching entry is easy.
11426 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
11427 call to `org-occur' will be kept, to allow stacking of calls to this
11428 command.
11429 If CALLBACK is non-nil, it is a function which is called to confirm
11430 that the match should indeed be shown."
11431 (interactive "sRegexp: \nP")
11432 (when (equal regexp "")
11433 (error "Regexp cannot be empty"))
11434 (unless keep-previous
11435 (org-remove-occur-highlights nil nil t))
11436 (push (cons regexp callback) org-occur-parameters)
11437 (let ((cnt 0))
11438 (save-excursion
11439 (goto-char (point-min))
11440 (if (or (not keep-previous) ; do not want to keep
11441 (not org-occur-highlights)) ; no previous matches
11442 ;; hide everything
11443 (org-overview))
11444 (while (re-search-forward regexp nil t)
11445 (when (or (not callback)
11446 (save-match-data (funcall callback)))
11447 (setq cnt (1+ cnt))
11448 (when org-highlight-sparse-tree-matches
11449 (org-highlight-new-match (match-beginning 0) (match-end 0)))
11450 (org-show-context 'occur-tree))))
11451 (when org-remove-highlights-with-change
11452 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
11453 nil 'local))
11454 (unless org-sparse-tree-open-archived-trees
11455 (org-hide-archived-subtrees (point-min) (point-max)))
11456 (run-hooks 'org-occur-hook)
11457 (if (interactive-p)
11458 (message "%d match(es) for regexp %s" cnt regexp))
11459 cnt))
11461 (defun org-show-context (&optional key)
11462 "Make sure point and context and visible.
11463 How much context is shown depends upon the variables
11464 `org-show-hierarchy-above', `org-show-following-heading'. and
11465 `org-show-siblings'."
11466 (let ((heading-p (org-on-heading-p t))
11467 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
11468 (following-p (org-get-alist-option org-show-following-heading key))
11469 (entry-p (org-get-alist-option org-show-entry-below key))
11470 (siblings-p (org-get-alist-option org-show-siblings key)))
11471 (catch 'exit
11472 ;; Show heading or entry text
11473 (if (and heading-p (not entry-p))
11474 (org-flag-heading nil) ; only show the heading
11475 (and (or entry-p (org-invisible-p) (org-invisible-p2))
11476 (org-show-hidden-entry))) ; show entire entry
11477 (when following-p
11478 ;; Show next sibling, or heading below text
11479 (save-excursion
11480 (and (if heading-p (org-goto-sibling) (outline-next-heading))
11481 (org-flag-heading nil))))
11482 (when siblings-p (org-show-siblings))
11483 (when hierarchy-p
11484 ;; show all higher headings, possibly with siblings
11485 (save-excursion
11486 (while (and (condition-case nil
11487 (progn (org-up-heading-all 1) t)
11488 (error nil))
11489 (not (bobp)))
11490 (org-flag-heading nil)
11491 (when siblings-p (org-show-siblings))))))))
11493 (defvar org-reveal-start-hook nil
11494 "Hook run before revealing a location.")
11496 (defun org-reveal (&optional siblings)
11497 "Show current entry, hierarchy above it, and the following headline.
11498 This can be used to show a consistent set of context around locations
11499 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
11500 not t for the search context.
11502 With optional argument SIBLINGS, on each level of the hierarchy all
11503 siblings are shown. This repairs the tree structure to what it would
11504 look like when opened with hierarchical calls to `org-cycle'.
11505 With double optional argument `C-u C-u', go to the parent and show the
11506 entire tree."
11507 (interactive "P")
11508 (run-hooks 'org-reveal-start-hook)
11509 (let ((org-show-hierarchy-above t)
11510 (org-show-following-heading t)
11511 (org-show-siblings (if siblings t org-show-siblings)))
11512 (org-show-context nil))
11513 (when (equal siblings '(16))
11514 (save-excursion
11515 (when (org-up-heading-safe)
11516 (org-show-subtree)
11517 (run-hook-with-args 'org-cycle-hook 'subtree)))))
11519 (defun org-highlight-new-match (beg end)
11520 "Highlight from BEG to END and mark the highlight is an occur headline."
11521 (let ((ov (make-overlay beg end)))
11522 (overlay-put ov 'face 'secondary-selection)
11523 (push ov org-occur-highlights)))
11525 (defun org-remove-occur-highlights (&optional beg end noremove)
11526 "Remove the occur highlights from the buffer.
11527 BEG and END are ignored. If NOREMOVE is nil, remove this function
11528 from the `before-change-functions' in the current buffer."
11529 (interactive)
11530 (unless org-inhibit-highlight-removal
11531 (mapc 'delete-overlay org-occur-highlights)
11532 (setq org-occur-highlights nil)
11533 (setq org-occur-parameters nil)
11534 (unless noremove
11535 (remove-hook 'before-change-functions
11536 'org-remove-occur-highlights 'local))))
11538 ;;;; Priorities
11540 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
11541 "Regular expression matching the priority indicator.")
11543 (defvar org-remove-priority-next-time nil)
11545 (defun org-priority-up ()
11546 "Increase the priority of the current item."
11547 (interactive)
11548 (org-priority 'up))
11550 (defun org-priority-down ()
11551 "Decrease the priority of the current item."
11552 (interactive)
11553 (org-priority 'down))
11555 (defun org-priority (&optional action)
11556 "Change the priority of an item by ARG.
11557 ACTION can be `set', `up', `down', or a character."
11558 (interactive)
11559 (unless org-enable-priority-commands
11560 (error "Priority commands are disabled"))
11561 (setq action (or action 'set))
11562 (let (current new news have remove)
11563 (save-excursion
11564 (org-back-to-heading t)
11565 (if (looking-at org-priority-regexp)
11566 (setq current (string-to-char (match-string 2))
11567 have t)
11568 (setq current org-default-priority))
11569 (cond
11570 ((eq action 'remove)
11571 (setq remove t new ?\ ))
11572 ((or (eq action 'set)
11573 (if (featurep 'xemacs) (characterp action) (integerp action)))
11574 (if (not (eq action 'set))
11575 (setq new action)
11576 (message "Priority %c-%c, SPC to remove: "
11577 org-highest-priority org-lowest-priority)
11578 (setq new (read-char-exclusive)))
11579 (if (and (= (upcase org-highest-priority) org-highest-priority)
11580 (= (upcase org-lowest-priority) org-lowest-priority))
11581 (setq new (upcase new)))
11582 (cond ((equal new ?\ ) (setq remove t))
11583 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
11584 (error "Priority must be between `%c' and `%c'"
11585 org-highest-priority org-lowest-priority))))
11586 ((eq action 'up)
11587 (if (and (not have) (eq last-command this-command))
11588 (setq new org-lowest-priority)
11589 (setq new (if (and org-priority-start-cycle-with-default (not have))
11590 org-default-priority (1- current)))))
11591 ((eq action 'down)
11592 (if (and (not have) (eq last-command this-command))
11593 (setq new org-highest-priority)
11594 (setq new (if (and org-priority-start-cycle-with-default (not have))
11595 org-default-priority (1+ current)))))
11596 (t (error "Invalid action")))
11597 (if (or (< (upcase new) org-highest-priority)
11598 (> (upcase new) org-lowest-priority))
11599 (setq remove t))
11600 (setq news (format "%c" new))
11601 (if have
11602 (if remove
11603 (replace-match "" t t nil 1)
11604 (replace-match news t t nil 2))
11605 (if remove
11606 (error "No priority cookie found in line")
11607 (let ((case-fold-search nil))
11608 (looking-at org-todo-line-regexp))
11609 (if (match-end 2)
11610 (progn
11611 (goto-char (match-end 2))
11612 (insert " [#" news "]"))
11613 (goto-char (match-beginning 3))
11614 (insert "[#" news "] "))))
11615 (org-preserve-lc (org-set-tags nil 'align)))
11616 (if remove
11617 (message "Priority removed")
11618 (message "Priority of current item set to %s" news))))
11620 (defun org-get-priority (s)
11621 "Find priority cookie and return priority."
11622 (save-match-data
11623 (if (not (string-match org-priority-regexp s))
11624 (* 1000 (- org-lowest-priority org-default-priority))
11625 (* 1000 (- org-lowest-priority
11626 (string-to-char (match-string 2 s)))))))
11628 ;;;; Tags
11630 (defvar org-agenda-archives-mode)
11631 (defvar org-map-continue-from nil
11632 "Position from where mapping should continue.
11633 Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
11635 (defvar org-scanner-tags nil
11636 "The current tag list while the tags scanner is running.")
11637 (defvar org-trust-scanner-tags nil
11638 "Should `org-get-tags-at' use the tags fro the scanner.
11639 This is for internal dynamical scoping only.
11640 When this is non-nil, the function `org-get-tags-at' will return the value
11641 of `org-scanner-tags' instead of building the list by itself. This
11642 can lead to large speed-ups when the tags scanner is used in a file with
11643 many entries, and when the list of tags is retrieved, for example to
11644 obtain a list of properties. Building the tags list for each entry in such
11645 a file becomes an N^2 operation - but with this variable set, it scales
11646 as N.")
11648 (defun org-scan-tags (action matcher &optional todo-only)
11649 "Scan headline tags with inheritance and produce output ACTION.
11651 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
11652 or `agenda' to produce an entry list for an agenda view. It can also be
11653 a Lisp form or a function that should be called at each matched headline, in
11654 this case the return value is a list of all return values from these calls.
11656 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
11657 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
11658 only lines with a TODO keyword are included in the output."
11659 (require 'org-agenda)
11660 (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
11661 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
11662 (org-re
11663 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
11664 (props (list 'face 'default
11665 'done-face 'org-agenda-done
11666 'undone-face 'default
11667 'mouse-face 'highlight
11668 'org-not-done-regexp org-not-done-regexp
11669 'org-todo-regexp org-todo-regexp
11670 'help-echo
11671 (format "mouse-2 or RET jump to org file %s"
11672 (abbreviate-file-name
11673 (or (buffer-file-name (buffer-base-buffer))
11674 (buffer-name (buffer-base-buffer)))))))
11675 (case-fold-search nil)
11676 (org-map-continue-from nil)
11677 lspos tags tags-list
11678 (tags-alist (list (cons 0 org-file-tags)))
11679 (llast 0) rtn rtn1 level category i txt
11680 todo marker entry priority)
11681 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
11682 (setq action (list 'lambda nil action)))
11683 (save-excursion
11684 (goto-char (point-min))
11685 (when (eq action 'sparse-tree)
11686 (org-overview)
11687 (org-remove-occur-highlights))
11688 (while (re-search-forward re nil t)
11689 (catch :skip
11690 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
11691 tags (if (match-end 4) (org-match-string-no-properties 4)))
11692 (goto-char (setq lspos (match-beginning 0)))
11693 (setq level (org-reduced-level (funcall outline-level))
11694 category (org-get-category))
11695 (setq i llast llast level)
11696 ;; remove tag lists from same and sublevels
11697 (while (>= i level)
11698 (when (setq entry (assoc i tags-alist))
11699 (setq tags-alist (delete entry tags-alist)))
11700 (setq i (1- i)))
11701 ;; add the next tags
11702 (when tags
11703 (setq tags (org-split-string tags ":")
11704 tags-alist
11705 (cons (cons level tags) tags-alist)))
11706 ;; compile tags for current headline
11707 (setq tags-list
11708 (if org-use-tag-inheritance
11709 (apply 'append (mapcar 'cdr (reverse tags-alist)))
11710 tags)
11711 org-scanner-tags tags-list)
11712 (when org-use-tag-inheritance
11713 (setcdr (car tags-alist)
11714 (mapcar (lambda (x)
11715 (setq x (copy-sequence x))
11716 (org-add-prop-inherited x))
11717 (cdar tags-alist))))
11718 (when (and tags org-use-tag-inheritance
11719 (or (not (eq t org-use-tag-inheritance))
11720 org-tags-exclude-from-inheritance))
11721 ;; selective inheritance, remove uninherited ones
11722 (setcdr (car tags-alist)
11723 (org-remove-uniherited-tags (cdar tags-alist))))
11724 (when (and (or (not todo-only)
11725 (and (member todo org-not-done-keywords)
11726 (or (not org-agenda-tags-todo-honor-ignore-options)
11727 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
11728 (let ((case-fold-search t)) (eval matcher))
11730 (not (member org-archive-tag tags-list))
11731 ;; we have an archive tag, should we use this anyway?
11732 (or (not org-agenda-skip-archived-trees)
11733 (and (eq action 'agenda) org-agenda-archives-mode))))
11734 (unless (eq action 'sparse-tree) (org-agenda-skip))
11736 ;; select this headline
11738 (cond
11739 ((eq action 'sparse-tree)
11740 (and org-highlight-sparse-tree-matches
11741 (org-get-heading) (match-end 0)
11742 (org-highlight-new-match
11743 (match-beginning 0) (match-beginning 1)))
11744 (org-show-context 'tags-tree))
11745 ((eq action 'agenda)
11746 (setq txt (org-format-agenda-item
11748 (concat
11749 (if (eq org-tags-match-list-sublevels 'indented)
11750 (make-string (1- level) ?.) "")
11751 (org-get-heading))
11752 category
11753 tags-list
11755 priority (org-get-priority txt))
11756 (goto-char lspos)
11757 (setq marker (org-agenda-new-marker))
11758 (org-add-props txt props
11759 'org-marker marker 'org-hd-marker marker 'org-category category
11760 'todo-state todo
11761 'priority priority 'type "tagsmatch")
11762 (push txt rtn))
11763 ((functionp action)
11764 (setq org-map-continue-from nil)
11765 (save-excursion
11766 (setq rtn1 (funcall action))
11767 (push rtn1 rtn)))
11768 (t (error "Invalid action")))
11770 ;; if we are to skip sublevels, jump to end of subtree
11771 (unless org-tags-match-list-sublevels
11772 (org-end-of-subtree t)
11773 (backward-char 1))))
11774 ;; Get the correct position from where to continue
11775 (if org-map-continue-from
11776 (goto-char org-map-continue-from)
11777 (and (= (point) lspos) (end-of-line 1)))))
11778 (when (and (eq action 'sparse-tree)
11779 (not org-sparse-tree-open-archived-trees))
11780 (org-hide-archived-subtrees (point-min) (point-max)))
11781 (nreverse rtn)))
11783 (defun org-remove-uniherited-tags (tags)
11784 "Remove all tags that are not inherited from the list TAGS."
11785 (cond
11786 ((eq org-use-tag-inheritance t)
11787 (if org-tags-exclude-from-inheritance
11788 (org-delete-all org-tags-exclude-from-inheritance tags)
11789 tags))
11790 ((not org-use-tag-inheritance) nil)
11791 ((stringp org-use-tag-inheritance)
11792 (delq nil (mapcar
11793 (lambda (x)
11794 (if (and (string-match org-use-tag-inheritance x)
11795 (not (member x org-tags-exclude-from-inheritance)))
11796 x nil))
11797 tags)))
11798 ((listp org-use-tag-inheritance)
11799 (delq nil (mapcar
11800 (lambda (x)
11801 (if (member x org-use-tag-inheritance) x nil))
11802 tags)))))
11804 (defvar todo-only) ;; dynamically scoped
11806 (defun org-match-sparse-tree (&optional todo-only match)
11807 "Create a sparse tree according to tags string MATCH.
11808 MATCH can contain positive and negative selection of tags, like
11809 \"+WORK+URGENT-WITHBOSS\".
11810 If optional argument TODO-ONLY is non-nil, only select lines that are
11811 also TODO lines."
11812 (interactive "P")
11813 (org-prepare-agenda-buffers (list (current-buffer)))
11814 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
11816 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
11818 (defvar org-cached-props nil)
11819 (defun org-cached-entry-get (pom property)
11820 (if (or (eq t org-use-property-inheritance)
11821 (and (stringp org-use-property-inheritance)
11822 (string-match org-use-property-inheritance property))
11823 (and (listp org-use-property-inheritance)
11824 (member property org-use-property-inheritance)))
11825 ;; Caching is not possible, check it directly
11826 (org-entry-get pom property 'inherit)
11827 ;; Get all properties, so that we can do complicated checks easily
11828 (cdr (assoc property (or org-cached-props
11829 (setq org-cached-props
11830 (org-entry-properties pom)))))))
11832 (defun org-global-tags-completion-table (&optional files)
11833 "Return the list of all tags in all agenda buffer/files."
11834 (save-excursion
11835 (org-uniquify
11836 (delq nil
11837 (apply 'append
11838 (mapcar
11839 (lambda (file)
11840 (set-buffer (find-file-noselect file))
11841 (append (org-get-buffer-tags)
11842 (mapcar (lambda (x) (if (stringp (car-safe x))
11843 (list (car-safe x)) nil))
11844 org-tag-alist)))
11845 (if (and files (car files))
11846 files
11847 (org-agenda-files))))))))
11849 (defun org-make-tags-matcher (match)
11850 "Create the TAGS//TODO matcher form for the selection string MATCH."
11851 ;; todo-only is scoped dynamically into this function, and the function
11852 ;; may change it if the matcher asks for it.
11853 (unless match
11854 ;; Get a new match request, with completion
11855 (let ((org-last-tags-completion-table
11856 (org-global-tags-completion-table)))
11857 (setq match (org-completing-read-no-i
11858 "Match: " 'org-tags-completion-function nil nil nil
11859 'org-tags-history))))
11861 ;; Parse the string and create a lisp form
11862 (let ((match0 match)
11863 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
11864 minus tag mm
11865 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
11866 orterms term orlist re-p str-p level-p level-op time-p
11867 prop-p pn pv po cat-p gv rest)
11868 (if (string-match "/+" match)
11869 ;; match contains also a todo-matching request
11870 (progn
11871 (setq tagsmatch (substring match 0 (match-beginning 0))
11872 todomatch (substring match (match-end 0)))
11873 (if (string-match "^!" todomatch)
11874 (setq todo-only t todomatch (substring todomatch 1)))
11875 (if (string-match "^\\s-*$" todomatch)
11876 (setq todomatch nil)))
11877 ;; only matching tags
11878 (setq tagsmatch match todomatch nil))
11880 ;; Make the tags matcher
11881 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
11882 (setq tagsmatcher t)
11883 (setq orterms (org-split-string tagsmatch "|") orlist nil)
11884 (while (setq term (pop orterms))
11885 (while (and (equal (substring term -1) "\\") orterms)
11886 (setq term (concat term "|" (pop orterms)))) ; repair bad split
11887 (while (string-match re term)
11888 (setq rest (substring term (match-end 0))
11889 minus (and (match-end 1)
11890 (equal (match-string 1 term) "-"))
11891 tag (match-string 2 term)
11892 re-p (equal (string-to-char tag) ?{)
11893 level-p (match-end 4)
11894 prop-p (match-end 5)
11895 mm (cond
11896 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
11897 (level-p
11898 (setq level-op (org-op-to-function (match-string 3 term)))
11899 `(,level-op level ,(string-to-number
11900 (match-string 4 term))))
11901 (prop-p
11902 (setq pn (match-string 5 term)
11903 po (match-string 6 term)
11904 pv (match-string 7 term)
11905 cat-p (equal pn "CATEGORY")
11906 re-p (equal (string-to-char pv) ?{)
11907 str-p (equal (string-to-char pv) ?\")
11908 time-p (save-match-data
11909 (string-match "^\"[[<].*[]>]\"$" pv))
11910 pv (if (or re-p str-p) (substring pv 1 -1) pv))
11911 (if time-p (setq pv (org-matcher-time pv)))
11912 (setq po (org-op-to-function po (if time-p 'time str-p)))
11913 (cond
11914 ((equal pn "CATEGORY")
11915 (setq gv '(get-text-property (point) 'org-category)))
11916 ((equal pn "TODO")
11917 (setq gv 'todo))
11919 (setq gv `(org-cached-entry-get nil ,pn))))
11920 (if re-p
11921 (if (eq po 'org<>)
11922 `(not (string-match ,pv (or ,gv "")))
11923 `(string-match ,pv (or ,gv "")))
11924 (if str-p
11925 `(,po (or ,gv "") ,pv)
11926 `(,po (string-to-number (or ,gv ""))
11927 ,(string-to-number pv) ))))
11928 (t `(member ,tag tags-list)))
11929 mm (if minus (list 'not mm) mm)
11930 term rest)
11931 (push mm tagsmatcher))
11932 (push (if (> (length tagsmatcher) 1)
11933 (cons 'and tagsmatcher)
11934 (car tagsmatcher))
11935 orlist)
11936 (setq tagsmatcher nil))
11937 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
11938 (setq tagsmatcher
11939 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
11940 ;; Make the todo matcher
11941 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
11942 (setq todomatcher t)
11943 (setq orterms (org-split-string todomatch "|") orlist nil)
11944 (while (setq term (pop orterms))
11945 (while (string-match re term)
11946 (setq minus (and (match-end 1)
11947 (equal (match-string 1 term) "-"))
11948 kwd (match-string 2 term)
11949 re-p (equal (string-to-char kwd) ?{)
11950 term (substring term (match-end 0))
11951 mm (if re-p
11952 `(string-match ,(substring kwd 1 -1) todo)
11953 (list 'equal 'todo kwd))
11954 mm (if minus (list 'not mm) mm))
11955 (push mm todomatcher))
11956 (push (if (> (length todomatcher) 1)
11957 (cons 'and todomatcher)
11958 (car todomatcher))
11959 orlist)
11960 (setq todomatcher nil))
11961 (setq todomatcher (if (> (length orlist) 1)
11962 (cons 'or orlist) (car orlist))))
11964 ;; Return the string and lisp forms of the matcher
11965 (setq matcher (if todomatcher
11966 (list 'and tagsmatcher todomatcher)
11967 tagsmatcher))
11968 (cons match0 matcher)))
11970 (defun org-op-to-function (op &optional stringp)
11971 "Turn an operator into the appropriate function."
11972 (setq op
11973 (cond
11974 ((equal op "<" ) '(< string< org-time<))
11975 ((equal op ">" ) '(> org-string> org-time>))
11976 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
11977 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
11978 ((member op '("=" "==")) '(= string= org-time=))
11979 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
11980 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
11982 (defun org<> (a b) (not (= a b)))
11983 (defun org-string<= (a b) (or (string= a b) (string< a b)))
11984 (defun org-string>= (a b) (not (string< a b)))
11985 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
11986 (defun org-string<> (a b) (not (string= a b)))
11987 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
11988 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
11989 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
11990 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
11991 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
11992 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
11993 (defun org-2ft (s)
11994 "Convert S to a floating point time.
11995 If S is already a number, just return it. If it is a string, parse
11996 it as a time string and apply `float-time' to it. If S is nil, just return 0."
11997 (cond
11998 ((numberp s) s)
11999 ((stringp s)
12000 (condition-case nil
12001 (float-time (apply 'encode-time (org-parse-time-string s)))
12002 (error 0.)))
12003 (t 0.)))
12005 (defun org-time-today ()
12006 "Time in seconds today at 0:00.
12007 Returns the float number of seconds since the beginning of the
12008 epoch to the beginning of today (00:00)."
12009 (float-time (apply 'encode-time
12010 (append '(0 0 0) (nthcdr 3 (decode-time))))))
12012 (defun org-matcher-time (s)
12013 "Interpret a time comparison value."
12014 (save-match-data
12015 (cond
12016 ((string= s "<now>") (float-time))
12017 ((string= s "<today>") (org-time-today))
12018 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
12019 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
12020 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
12021 (+ (org-time-today)
12022 (* (string-to-number (match-string 1 s))
12023 (cdr (assoc (match-string 2 s)
12024 '(("d" . 86400.0) ("w" . 604800.0)
12025 ("m" . 2678400.0) ("y" . 31557600.0)))))))
12026 (t (org-2ft s)))))
12028 (defun org-match-any-p (re list)
12029 "Does re match any element of list?"
12030 (setq list (mapcar (lambda (x) (string-match re x)) list))
12031 (delq nil list))
12033 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
12034 (defvar org-tags-overlay (make-overlay 1 1))
12035 (org-detach-overlay org-tags-overlay)
12037 (defun org-get-local-tags-at (&optional pos)
12038 "Get a list of tags defined in the current headline."
12039 (org-get-tags-at pos 'local))
12041 (defun org-get-local-tags ()
12042 "Get a list of tags defined in the current headline."
12043 (org-get-tags-at nil 'local))
12045 (defun org-get-tags-at (&optional pos local)
12046 "Get a list of all headline tags applicable at POS.
12047 POS defaults to point. If tags are inherited, the list contains
12048 the targets in the same sequence as the headlines appear, i.e.
12049 the tags of the current headline come last.
12050 When LOCAL is non-nil, only return tags from the current headline,
12051 ignore inherited ones."
12052 (interactive)
12053 (if (and org-trust-scanner-tags
12054 (or (not pos) (equal pos (point)))
12055 (not local))
12056 org-scanner-tags
12057 (let (tags ltags lastpos parent)
12058 (save-excursion
12059 (save-restriction
12060 (widen)
12061 (goto-char (or pos (point)))
12062 (save-match-data
12063 (catch 'done
12064 (condition-case nil
12065 (progn
12066 (org-back-to-heading t)
12067 (while (not (equal lastpos (point)))
12068 (setq lastpos (point))
12069 (when (looking-at
12070 (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
12071 (setq ltags (org-split-string
12072 (org-match-string-no-properties 1) ":"))
12073 (when parent
12074 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
12075 (setq tags (append
12076 (if parent
12077 (org-remove-uniherited-tags ltags)
12078 ltags)
12079 tags)))
12080 (or org-use-tag-inheritance (throw 'done t))
12081 (if local (throw 'done t))
12082 (or (org-up-heading-safe) (error nil))
12083 (setq parent t)))
12084 (error nil)))))
12085 (append (org-remove-uniherited-tags org-file-tags) tags)))))
12087 (defun org-add-prop-inherited (s)
12088 (add-text-properties 0 (length s) '(inherited t) s)
12091 (defun org-toggle-tag (tag &optional onoff)
12092 "Toggle the tag TAG for the current line.
12093 If ONOFF is `on' or `off', don't toggle but set to this state."
12094 (let (res current)
12095 (save-excursion
12096 (org-back-to-heading t)
12097 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
12098 (point-at-eol) t)
12099 (progn
12100 (setq current (match-string 1))
12101 (replace-match ""))
12102 (setq current ""))
12103 (setq current (nreverse (org-split-string current ":")))
12104 (cond
12105 ((eq onoff 'on)
12106 (setq res t)
12107 (or (member tag current) (push tag current)))
12108 ((eq onoff 'off)
12109 (or (not (member tag current)) (setq current (delete tag current))))
12110 (t (if (member tag current)
12111 (setq current (delete tag current))
12112 (setq res t)
12113 (push tag current))))
12114 (end-of-line 1)
12115 (if current
12116 (progn
12117 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
12118 (org-set-tags nil t))
12119 (delete-horizontal-space))
12120 (run-hooks 'org-after-tags-change-hook))
12121 res))
12123 (defun org-align-tags-here (to-col)
12124 ;; Assumes that this is a headline
12125 (let ((pos (point)) (col (current-column)) ncol tags-l p)
12126 (beginning-of-line 1)
12127 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
12128 (< pos (match-beginning 2)))
12129 (progn
12130 (setq tags-l (- (match-end 2) (match-beginning 2)))
12131 (goto-char (match-beginning 1))
12132 (insert " ")
12133 (delete-region (point) (1+ (match-beginning 2)))
12134 (setq ncol (max (1+ (current-column))
12135 (1+ col)
12136 (if (> to-col 0)
12137 to-col
12138 (- (abs to-col) tags-l))))
12139 (setq p (point))
12140 (insert (make-string (- ncol (current-column)) ?\ ))
12141 (setq ncol (current-column))
12142 (when indent-tabs-mode (tabify p (point-at-eol)))
12143 (org-move-to-column (min ncol col) t))
12144 (goto-char pos))))
12146 (defun org-set-tags-command (&optional arg just-align)
12147 "Call the set-tags command for the current entry."
12148 (interactive "P")
12149 (if (org-on-heading-p)
12150 (org-set-tags arg just-align)
12151 (save-excursion
12152 (org-back-to-heading t)
12153 (org-set-tags arg just-align))))
12155 (defun org-set-tags-to (data)
12156 "Set the tags of the current entry to DATA, replacing the current tags.
12157 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
12158 If DATA is nil or the empty string, any tags will be removed."
12159 (interactive "sTags: ")
12160 (setq data
12161 (cond
12162 ((eq data nil) "")
12163 ((equal data "") "")
12164 ((stringp data)
12165 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
12166 ":"))
12167 ((listp data)
12168 (concat ":" (mapconcat 'identity data ":") ":"))
12169 (t nil)))
12170 (when data
12171 (save-excursion
12172 (org-back-to-heading t)
12173 (when (looking-at org-complex-heading-regexp)
12174 (if (match-end 5)
12175 (progn
12176 (goto-char (match-beginning 5))
12177 (insert data)
12178 (delete-region (point) (point-at-eol))
12179 (org-set-tags nil 'align))
12180 (goto-char (point-at-eol))
12181 (insert " " data)
12182 (org-set-tags nil 'align)))
12183 (beginning-of-line 1)
12184 (if (looking-at ".*?\\([ \t]+\\)$")
12185 (delete-region (match-beginning 1) (match-end 1))))))
12187 (defun org-align-all-tags ()
12188 "Align the tags i all headings."
12189 (interactive)
12190 (save-excursion
12191 (or (ignore-errors (org-back-to-heading t))
12192 (outline-next-heading))
12193 (if (org-on-heading-p)
12194 (org-set-tags t)
12195 (message "No headings"))))
12197 (defun org-set-tags (&optional arg just-align)
12198 "Set the tags for the current headline.
12199 With prefix ARG, realign all tags in headings in the current buffer."
12200 (interactive "P")
12201 (let* ((re (concat "^" outline-regexp))
12202 (current (org-get-tags-string))
12203 (col (current-column))
12204 (org-setting-tags t)
12205 table current-tags inherited-tags ; computed below when needed
12206 tags p0 c0 c1 rpl)
12207 (if arg
12208 (save-excursion
12209 (goto-char (point-min))
12210 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
12211 (while (re-search-forward re nil t)
12212 (org-set-tags nil t)
12213 (end-of-line 1)))
12214 (message "All tags realigned to column %d" org-tags-column))
12215 (if just-align
12216 (setq tags current)
12217 ;; Get a new set of tags from the user
12218 (save-excursion
12219 (setq table (append org-tag-persistent-alist
12220 (or org-tag-alist (org-get-buffer-tags))
12221 (and org-complete-tags-always-offer-all-agenda-tags
12222 (org-global-tags-completion-table (org-agenda-files))))
12223 org-last-tags-completion-table table
12224 current-tags (org-split-string current ":")
12225 inherited-tags (nreverse
12226 (nthcdr (length current-tags)
12227 (nreverse (org-get-tags-at))))
12228 tags
12229 (if (or (eq t org-use-fast-tag-selection)
12230 (and org-use-fast-tag-selection
12231 (delq nil (mapcar 'cdr table))))
12232 (org-fast-tag-selection
12233 current-tags inherited-tags table
12234 (if org-fast-tag-selection-include-todo org-todo-key-alist))
12235 (let ((org-add-colon-after-tag-completion t))
12236 (org-trim
12237 (org-without-partial-completion
12238 (org-icompleting-read "Tags: " 'org-tags-completion-function
12239 nil nil current 'org-tags-history)))))))
12240 (while (string-match "[-+&]+" tags)
12241 ;; No boolean logic, just a list
12242 (setq tags (replace-match ":" t t tags))))
12244 (if org-tags-sort-function
12245 (setq tags (mapconcat 'identity
12246 (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
12247 org-tags-sort-function) ":")))
12249 (if (string-match "\\`[\t ]*\\'" tags)
12250 (setq tags "")
12251 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
12252 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
12254 ;; Insert new tags at the correct column
12255 (beginning-of-line 1)
12256 (cond
12257 ((and (equal current "") (equal tags "")))
12258 ((re-search-forward
12259 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
12260 (point-at-eol) t)
12261 (if (equal tags "")
12262 (setq rpl "")
12263 (goto-char (match-beginning 0))
12264 (setq c0 (current-column) p0 (if (equal (char-before) ?*)
12265 (1+ (point)) (point))
12266 c1 (max (1+ c0) (if (> org-tags-column 0)
12267 org-tags-column
12268 (- (- org-tags-column) (length tags))))
12269 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
12270 (replace-match rpl t t)
12271 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
12272 tags)
12273 (t (error "Tags alignment failed")))
12274 (org-move-to-column col)
12275 (unless just-align
12276 (run-hooks 'org-after-tags-change-hook)))))
12278 (defun org-change-tag-in-region (beg end tag off)
12279 "Add or remove TAG for each entry in the region.
12280 This works in the agenda, and also in an org-mode buffer."
12281 (interactive
12282 (list (region-beginning) (region-end)
12283 (let ((org-last-tags-completion-table
12284 (if (org-mode-p)
12285 (org-get-buffer-tags)
12286 (org-global-tags-completion-table))))
12287 (org-icompleting-read
12288 "Tag: " 'org-tags-completion-function nil nil nil
12289 'org-tags-history))
12290 (progn
12291 (message "[s]et or [r]emove? ")
12292 (equal (read-char-exclusive) ?r))))
12293 (if (fboundp 'deactivate-mark) (deactivate-mark))
12294 (let ((agendap (equal major-mode 'org-agenda-mode))
12295 l1 l2 m buf pos newhead (cnt 0))
12296 (goto-char end)
12297 (setq l2 (1- (org-current-line)))
12298 (goto-char beg)
12299 (setq l1 (org-current-line))
12300 (loop for l from l1 to l2 do
12301 (org-goto-line l)
12302 (setq m (get-text-property (point) 'org-hd-marker))
12303 (when (or (and (org-mode-p) (org-on-heading-p))
12304 (and agendap m))
12305 (setq buf (if agendap (marker-buffer m) (current-buffer))
12306 pos (if agendap m (point)))
12307 (with-current-buffer buf
12308 (save-excursion
12309 (save-restriction
12310 (goto-char pos)
12311 (setq cnt (1+ cnt))
12312 (org-toggle-tag tag (if off 'off 'on))
12313 (setq newhead (org-get-heading)))))
12314 (and agendap (org-agenda-change-all-lines newhead m))))
12315 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
12317 (defun org-tags-completion-function (string predicate &optional flag)
12318 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
12319 (confirm (lambda (x) (stringp (car x)))))
12320 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
12321 (setq s1 (match-string 1 string)
12322 s2 (match-string 2 string))
12323 (setq s1 "" s2 string))
12324 (cond
12325 ((eq flag nil)
12326 ;; try completion
12327 (setq rtn (try-completion s2 ctable confirm))
12328 (if (stringp rtn)
12329 (setq rtn
12330 (concat s1 s2 (substring rtn (length s2))
12331 (if (and org-add-colon-after-tag-completion
12332 (assoc rtn ctable))
12333 ":" ""))))
12334 rtn)
12335 ((eq flag t)
12336 ;; all-completions
12337 (all-completions s2 ctable confirm)
12339 ((eq flag 'lambda)
12340 ;; exact match?
12341 (assoc s2 ctable)))
12344 (defun org-fast-tag-insert (kwd tags face &optional end)
12345 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
12346 (insert (format "%-12s" (concat kwd ":"))
12347 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
12348 (or end "")))
12350 (defun org-fast-tag-show-exit (flag)
12351 (save-excursion
12352 (org-goto-line 3)
12353 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
12354 (replace-match ""))
12355 (when flag
12356 (end-of-line 1)
12357 (org-move-to-column (- (window-width) 19) t)
12358 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
12360 (defun org-set-current-tags-overlay (current prefix)
12361 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
12362 (if (featurep 'xemacs)
12363 (org-overlay-display org-tags-overlay (concat prefix s)
12364 'secondary-selection)
12365 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
12366 (org-overlay-display org-tags-overlay (concat prefix s)))))
12368 (defvar org-last-tag-selection-key nil)
12369 (defun org-fast-tag-selection (current inherited table &optional todo-table)
12370 "Fast tag selection with single keys.
12371 CURRENT is the current list of tags in the headline, INHERITED is the
12372 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
12373 possibly with grouping information. TODO-TABLE is a similar table with
12374 TODO keywords, should these have keys assigned to them.
12375 If the keys are nil, a-z are automatically assigned.
12376 Returns the new tags string, or nil to not change the current settings."
12377 (let* ((fulltable (append table todo-table))
12378 (maxlen (apply 'max (mapcar
12379 (lambda (x)
12380 (if (stringp (car x)) (string-width (car x)) 0))
12381 fulltable)))
12382 (buf (current-buffer))
12383 (expert (eq org-fast-tag-selection-single-key 'expert))
12384 (buffer-tags nil)
12385 (fwidth (+ maxlen 3 1 3))
12386 (ncol (/ (- (window-width) 4) fwidth))
12387 (i-face 'org-done)
12388 (c-face 'org-todo)
12389 tg cnt e c char c1 c2 ntable tbl rtn
12390 ov-start ov-end ov-prefix
12391 (exit-after-next org-fast-tag-selection-single-key)
12392 (done-keywords org-done-keywords)
12393 groups ingroup)
12394 (save-excursion
12395 (beginning-of-line 1)
12396 (if (looking-at
12397 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
12398 (setq ov-start (match-beginning 1)
12399 ov-end (match-end 1)
12400 ov-prefix "")
12401 (setq ov-start (1- (point-at-eol))
12402 ov-end (1+ ov-start))
12403 (skip-chars-forward "^\n\r")
12404 (setq ov-prefix
12405 (concat
12406 (buffer-substring (1- (point)) (point))
12407 (if (> (current-column) org-tags-column)
12409 (make-string (- org-tags-column (current-column)) ?\ ))))))
12410 (move-overlay org-tags-overlay ov-start ov-end)
12411 (save-window-excursion
12412 (if expert
12413 (set-buffer (get-buffer-create " *Org tags*"))
12414 (delete-other-windows)
12415 (split-window-vertically)
12416 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
12417 (erase-buffer)
12418 (org-set-local 'org-done-keywords done-keywords)
12419 (org-fast-tag-insert "Inherited" inherited i-face "\n")
12420 (org-fast-tag-insert "Current" current c-face "\n\n")
12421 (org-fast-tag-show-exit exit-after-next)
12422 (org-set-current-tags-overlay current ov-prefix)
12423 (setq tbl fulltable char ?a cnt 0)
12424 (while (setq e (pop tbl))
12425 (cond
12426 ((equal (car e) :startgroup)
12427 (push '() groups) (setq ingroup t)
12428 (when (not (= cnt 0))
12429 (setq cnt 0)
12430 (insert "\n"))
12431 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
12432 ((equal (car e) :endgroup)
12433 (setq ingroup nil cnt 0)
12434 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
12435 ((equal e '(:newline))
12436 (when (not (= cnt 0))
12437 (setq cnt 0)
12438 (insert "\n")
12439 (setq e (car tbl))
12440 (while (equal (car tbl) '(:newline))
12441 (insert "\n")
12442 (setq tbl (cdr tbl)))))
12444 (setq tg (copy-sequence (car e)) c2 nil)
12445 (if (cdr e)
12446 (setq c (cdr e))
12447 ;; automatically assign a character.
12448 (setq c1 (string-to-char
12449 (downcase (substring
12450 tg (if (= (string-to-char tg) ?@) 1 0)))))
12451 (if (or (rassoc c1 ntable) (rassoc c1 table))
12452 (while (or (rassoc char ntable) (rassoc char table))
12453 (setq char (1+ char)))
12454 (setq c2 c1))
12455 (setq c (or c2 char)))
12456 (if ingroup (push tg (car groups)))
12457 (setq tg (org-add-props tg nil 'face
12458 (cond
12459 ((not (assoc tg table))
12460 (org-get-todo-face tg))
12461 ((member tg current) c-face)
12462 ((member tg inherited) i-face)
12463 (t nil))))
12464 (if (and (= cnt 0) (not ingroup)) (insert " "))
12465 (insert "[" c "] " tg (make-string
12466 (- fwidth 4 (length tg)) ?\ ))
12467 (push (cons tg c) ntable)
12468 (when (= (setq cnt (1+ cnt)) ncol)
12469 (insert "\n")
12470 (if ingroup (insert " "))
12471 (setq cnt 0)))))
12472 (setq ntable (nreverse ntable))
12473 (insert "\n")
12474 (goto-char (point-min))
12475 (if (not expert) (org-fit-window-to-buffer))
12476 (setq rtn
12477 (catch 'exit
12478 (while t
12479 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
12480 (if (not groups) "no " "")
12481 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
12482 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
12483 (setq org-last-tag-selection-key c)
12484 (cond
12485 ((= c ?\r) (throw 'exit t))
12486 ((= c ?!)
12487 (setq groups (not groups))
12488 (goto-char (point-min))
12489 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
12490 ((= c ?\C-c)
12491 (if (not expert)
12492 (org-fast-tag-show-exit
12493 (setq exit-after-next (not exit-after-next)))
12494 (setq expert nil)
12495 (delete-other-windows)
12496 (split-window-vertically)
12497 (org-switch-to-buffer-other-window " *Org tags*")
12498 (org-fit-window-to-buffer)))
12499 ((or (= c ?\C-g)
12500 (and (= c ?q) (not (rassoc c ntable))))
12501 (org-detach-overlay org-tags-overlay)
12502 (setq quit-flag t))
12503 ((= c ?\ )
12504 (setq current nil)
12505 (if exit-after-next (setq exit-after-next 'now)))
12506 ((= c ?\t)
12507 (condition-case nil
12508 (setq tg (org-icompleting-read
12509 "Tag: "
12510 (or buffer-tags
12511 (with-current-buffer buf
12512 (org-get-buffer-tags)))))
12513 (quit (setq tg "")))
12514 (when (string-match "\\S-" tg)
12515 (add-to-list 'buffer-tags (list tg))
12516 (if (member tg current)
12517 (setq current (delete tg current))
12518 (push tg current)))
12519 (if exit-after-next (setq exit-after-next 'now)))
12520 ((setq e (rassoc c todo-table) tg (car e))
12521 (with-current-buffer buf
12522 (save-excursion (org-todo tg)))
12523 (if exit-after-next (setq exit-after-next 'now)))
12524 ((setq e (rassoc c ntable) tg (car e))
12525 (if (member tg current)
12526 (setq current (delete tg current))
12527 (loop for g in groups do
12528 (if (member tg g)
12529 (mapc (lambda (x)
12530 (setq current (delete x current)))
12531 g)))
12532 (push tg current))
12533 (if exit-after-next (setq exit-after-next 'now))))
12535 ;; Create a sorted list
12536 (setq current
12537 (sort current
12538 (lambda (a b)
12539 (assoc b (cdr (memq (assoc a ntable) ntable))))))
12540 (if (eq exit-after-next 'now) (throw 'exit t))
12541 (goto-char (point-min))
12542 (beginning-of-line 2)
12543 (delete-region (point) (point-at-eol))
12544 (org-fast-tag-insert "Current" current c-face)
12545 (org-set-current-tags-overlay current ov-prefix)
12546 (while (re-search-forward
12547 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
12548 (setq tg (match-string 1))
12549 (add-text-properties
12550 (match-beginning 1) (match-end 1)
12551 (list 'face
12552 (cond
12553 ((member tg current) c-face)
12554 ((member tg inherited) i-face)
12555 (t (get-text-property (match-beginning 1) 'face))))))
12556 (goto-char (point-min)))))
12557 (org-detach-overlay org-tags-overlay)
12558 (if rtn
12559 (mapconcat 'identity current ":")
12560 nil))))
12562 (defun org-get-tags-string ()
12563 "Get the TAGS string in the current headline."
12564 (unless (org-on-heading-p t)
12565 (error "Not on a heading"))
12566 (save-excursion
12567 (beginning-of-line 1)
12568 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
12569 (org-match-string-no-properties 1)
12570 "")))
12572 (defun org-get-tags ()
12573 "Get the list of tags specified in the current headline."
12574 (org-split-string (org-get-tags-string) ":"))
12576 (defun org-get-buffer-tags ()
12577 "Get a table of all tags used in the buffer, for completion."
12578 (let (tags)
12579 (save-excursion
12580 (goto-char (point-min))
12581 (while (re-search-forward
12582 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
12583 (when (equal (char-after (point-at-bol 0)) ?*)
12584 (mapc (lambda (x) (add-to-list 'tags x))
12585 (org-split-string (org-match-string-no-properties 1) ":")))))
12586 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
12587 (mapcar 'list tags)))
12589 ;;;; The mapping API
12591 ;;;###autoload
12592 (defun org-map-entries (func &optional match scope &rest skip)
12593 "Call FUNC at each headline selected by MATCH in SCOPE.
12595 FUNC is a function or a lisp form. The function will be called without
12596 arguments, with the cursor positioned at the beginning of the headline.
12597 The return values of all calls to the function will be collected and
12598 returned as a list.
12600 The call to FUNC will be wrapped into a save-excursion form, so FUNC
12601 does not need to preserve point. After evaluation, the cursor will be
12602 moved to the end of the line (presumably of the headline of the
12603 processed entry) and search continues from there. Under some
12604 circumstances, this may not produce the wanted results. For example,
12605 if you have removed (e.g. archived) the current (sub)tree it could
12606 mean that the next entry will be skipped entirely. In such cases, you
12607 can specify the position from where search should continue by making
12608 FUNC set the variable `org-map-continue-from' to the desired buffer
12609 position.
12611 MATCH is a tags/property/todo match as it is used in the agenda tags view.
12612 Only headlines that are matched by this query will be considered during
12613 the iteration. When MATCH is nil or t, all headlines will be
12614 visited by the iteration.
12616 SCOPE determines the scope of this command. It can be any of:
12618 nil The current buffer, respecting the restriction if any
12619 tree The subtree started with the entry at point
12620 file The current buffer, without restriction
12621 file-with-archives
12622 The current buffer, and any archives associated with it
12623 agenda All agenda files
12624 agenda-with-archives
12625 All agenda files with any archive files associated with them
12626 \(file1 file2 ...)
12627 If this is a list, all files in the list will be scanned
12629 The remaining args are treated as settings for the skipping facilities of
12630 the scanner. The following items can be given here:
12632 archive skip trees with the archive tag.
12633 comment skip trees with the COMMENT keyword
12634 function or Emacs Lisp form:
12635 will be used as value for `org-agenda-skip-function', so whenever
12636 the function returns t, FUNC will not be called for that
12637 entry and search will continue from the point where the
12638 function leaves it.
12640 If your function needs to retrieve the tags including inherited tags
12641 at the *current* entry, you can use the value of the variable
12642 `org-scanner-tags' which will be much faster than getting the value
12643 with `org-get-tags-at'. If your function gets properties with
12644 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
12645 to t around the call to `org-entry-properties' to get the same speedup.
12646 Note that if your function moves around to retrieve tags and properties at
12647 a *different* entry, you cannot use these techniques."
12648 (let* ((org-agenda-archives-mode nil) ; just to make sure
12649 (org-agenda-skip-archived-trees (memq 'archive skip))
12650 (org-agenda-skip-comment-trees (memq 'comment skip))
12651 (org-agenda-skip-function
12652 (car (org-delete-all '(comment archive) skip)))
12653 (org-tags-match-list-sublevels t)
12654 matcher file res
12655 org-todo-keywords-for-agenda
12656 org-done-keywords-for-agenda
12657 org-todo-keyword-alist-for-agenda
12658 org-drawers-for-agenda
12659 org-tag-alist-for-agenda)
12661 (cond
12662 ((eq match t) (setq matcher t))
12663 ((eq match nil) (setq matcher t))
12664 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
12666 (save-excursion
12667 (save-restriction
12668 (when (eq scope 'tree)
12669 (org-back-to-heading t)
12670 (org-narrow-to-subtree)
12671 (setq scope nil))
12673 (if (not scope)
12674 (progn
12675 (org-prepare-agenda-buffers
12676 (list (buffer-file-name (current-buffer))))
12677 (setq res (org-scan-tags func matcher)))
12678 ;; Get the right scope
12679 (cond
12680 ((and scope (listp scope) (symbolp (car scope)))
12681 (setq scope (eval scope)))
12682 ((eq scope 'agenda)
12683 (setq scope (org-agenda-files t)))
12684 ((eq scope 'agenda-with-archives)
12685 (setq scope (org-agenda-files t))
12686 (setq scope (org-add-archive-files scope)))
12687 ((eq scope 'file)
12688 (setq scope (list (buffer-file-name))))
12689 ((eq scope 'file-with-archives)
12690 (setq scope (org-add-archive-files (list (buffer-file-name))))))
12691 (org-prepare-agenda-buffers scope)
12692 (while (setq file (pop scope))
12693 (with-current-buffer (org-find-base-buffer-visiting file)
12694 (save-excursion
12695 (save-restriction
12696 (widen)
12697 (goto-char (point-min))
12698 (setq res (append res (org-scan-tags func matcher))))))))))
12699 res))
12701 ;;;; Properties
12703 ;;; Setting and retrieving properties
12705 (defconst org-special-properties
12706 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
12707 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
12708 "The special properties valid in Org-mode.
12710 These are properties that are not defined in the property drawer,
12711 but in some other way.")
12713 (defconst org-default-properties
12714 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
12715 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
12716 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
12717 "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
12718 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
12719 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
12720 "Some properties that are used by Org-mode for various purposes.
12721 Being in this list makes sure that they are offered for completion.")
12723 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
12724 "Regular expression matching the first line of a property drawer.")
12726 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
12727 "Regular expression matching the last line of a property drawer.")
12729 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
12730 "Regular expression matching the first line of a property drawer.")
12732 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
12733 "Regular expression matching the first line of a property drawer.")
12735 (defconst org-property-drawer-re
12736 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
12737 org-property-end-re "\\)\n?")
12738 "Matches an entire property drawer.")
12740 (defconst org-clock-drawer-re
12741 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
12742 org-property-end-re "\\)\n?")
12743 "Matches an entire clock drawer.")
12745 (defun org-property-action ()
12746 "Do an action on properties."
12747 (interactive)
12748 (let (c)
12749 (org-at-property-p)
12750 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
12751 (setq c (read-char-exclusive))
12752 (cond
12753 ((equal c ?s)
12754 (call-interactively 'org-set-property))
12755 ((equal c ?d)
12756 (call-interactively 'org-delete-property))
12757 ((equal c ?D)
12758 (call-interactively 'org-delete-property-globally))
12759 ((equal c ?c)
12760 (call-interactively 'org-compute-property-at-point))
12761 (t (error "No such property action %c" c)))))
12763 (defun org-set-effort (&optional value)
12764 "Set the effort property of the current entry.
12765 With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
12766 allowed value."
12767 (interactive "P")
12768 (if (equal value 0) (setq value 10))
12769 (let* ((completion-ignore-case t)
12770 (prop org-effort-property)
12771 (cur (org-entry-get nil prop))
12772 (allowed (org-property-get-allowed-values nil prop 'table))
12773 (existing (mapcar 'list (org-property-values prop)))
12775 (val (cond
12776 ((stringp value) value)
12777 ((and allowed (integerp value))
12778 (or (car (nth (1- value) allowed))
12779 (car (org-last allowed))))
12780 (allowed
12781 (message "Select 1-9,0, [RET%s]: %s"
12782 (if cur (concat "=" cur) "")
12783 (mapconcat 'car allowed " "))
12784 (setq rpl (read-char-exclusive))
12785 (if (equal rpl ?\r)
12787 (setq rpl (- rpl ?0))
12788 (if (equal rpl 0) (setq rpl 10))
12789 (if (and (> rpl 0) (<= rpl (length allowed)))
12790 (car (nth (1- rpl) allowed))
12791 (org-completing-read "Effort: " allowed nil))))
12793 (let (org-completion-use-ido org-completion-use-iswitchb)
12794 (org-completing-read
12795 (concat "Effort " (if (and cur (string-match "\\S-" cur))
12796 (concat "[" cur "]") "")
12797 ": ")
12798 existing nil nil "" nil cur))))))
12799 (unless (equal (org-entry-get nil prop) val)
12800 (org-entry-put nil prop val))
12801 (message "%s is now %s" prop val)))
12803 (defun org-at-property-p ()
12804 "Is cursor inside a property drawer?"
12805 (save-excursion
12806 (beginning-of-line 1)
12807 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
12808 (let ((match (match-data)) ;; Keep match-data for use by calling
12809 (p (point)) ;; procedures.
12810 (range (unless (org-before-first-heading-p)
12811 (org-get-property-block))))
12812 (prog1 (and range (<= (car range) p) (< p (cdr range)))
12813 (set-match-data match))))))
12815 (defun org-get-property-block (&optional beg end force)
12816 "Return the (beg . end) range of the body of the property drawer.
12817 BEG and END can be beginning and end of subtree, if not given
12818 they will be found.
12819 If the drawer does not exist and FORCE is non-nil, create the drawer."
12820 (catch 'exit
12821 (save-excursion
12822 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
12823 (end (or end (progn (outline-next-heading) (point)))))
12824 (goto-char beg)
12825 (if (re-search-forward org-property-start-re end t)
12826 (setq beg (1+ (match-end 0)))
12827 (if force
12828 (save-excursion
12829 (org-insert-property-drawer)
12830 (setq end (progn (outline-next-heading) (point))))
12831 (throw 'exit nil))
12832 (goto-char beg)
12833 (if (re-search-forward org-property-start-re end t)
12834 (setq beg (1+ (match-end 0)))))
12835 (if (re-search-forward org-property-end-re end t)
12836 (setq end (match-beginning 0))
12837 (or force (throw 'exit nil))
12838 (goto-char beg)
12839 (setq end beg)
12840 (org-indent-line-function)
12841 (insert ":END:\n"))
12842 (cons beg end)))))
12844 (defun org-entry-properties (&optional pom which specific)
12845 "Get all properties of the entry at point-or-marker POM.
12846 This includes the TODO keyword, the tags, time strings for deadline,
12847 scheduled, and clocking, and any additional properties defined in the
12848 entry. The return value is an alist, keys may occur multiple times
12849 if the property key was used several times.
12850 POM may also be nil, in which case the current entry is used.
12851 If WHICH is nil or `all', get all properties. If WHICH is
12852 `special' or `standard', only get that subclass. If WHICH
12853 is a string only get exactly this property. Specific can be a string, the
12854 specific property we are interested in. Specifying it can speed
12855 things up because then unnecessary parsing is avoided."
12856 (setq which (or which 'all))
12857 (org-with-point-at pom
12858 (let ((clockstr (substring org-clock-string 0 -1))
12859 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
12860 (case-fold-search nil)
12861 beg end range props sum-props key value string clocksum)
12862 (save-excursion
12863 (when (condition-case nil
12864 (and (org-mode-p) (org-back-to-heading t))
12865 (error nil))
12866 (setq beg (point))
12867 (setq sum-props (get-text-property (point) 'org-summaries))
12868 (setq clocksum (get-text-property (point) :org-clock-minutes))
12869 (outline-next-heading)
12870 (setq end (point))
12871 (when (memq which '(all special))
12872 ;; Get the special properties, like TODO and tags
12873 (goto-char beg)
12874 (when (and (or (not specific) (string= specific "TODO"))
12875 (looking-at org-todo-line-regexp) (match-end 2))
12876 (push (cons "TODO" (org-match-string-no-properties 2)) props))
12877 (when (and (or (not specific) (string= specific "PRIORITY"))
12878 (looking-at org-priority-regexp))
12879 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
12880 (when (and (or (not specific) (string= specific "TAGS"))
12881 (setq value (org-get-tags-string))
12882 (string-match "\\S-" value))
12883 (push (cons "TAGS" value) props))
12884 (when (and (or (not specific) (string= specific "ALLTAGS"))
12885 (setq value (org-get-tags-at)))
12886 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
12887 ":"))
12888 props))
12889 (when (or (not specific) (string= specific "BLOCKED"))
12890 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
12891 (when (or (not specific)
12892 (member specific org-all-time-keywords)
12893 (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
12894 (while (re-search-forward org-maybe-keyword-time-regexp end t)
12895 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
12896 string (if (equal key clockstr)
12897 (org-no-properties
12898 (org-trim
12899 (buffer-substring
12900 (match-beginning 3) (goto-char (point-at-eol)))))
12901 (substring (org-match-string-no-properties 3) 1 -1)))
12902 (unless key
12903 (if (= (char-after (match-beginning 3)) ?\[)
12904 (setq key "TIMESTAMP_IA")
12905 (setq key "TIMESTAMP")))
12906 (when (or (equal key clockstr) (not (assoc key props)))
12907 (push (cons key string) props))))
12911 (when (memq which '(all standard))
12912 ;; Get the standard properties, like :PROP: ...
12913 (setq range (org-get-property-block beg end))
12914 (when range
12915 (goto-char (car range))
12916 (while (re-search-forward
12917 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
12918 (cdr range) t)
12919 (setq key (org-match-string-no-properties 1)
12920 value (org-trim (or (org-match-string-no-properties 2) "")))
12921 (unless (member key excluded)
12922 (push (cons key (or value "")) props)))))
12923 (if clocksum
12924 (push (cons "CLOCKSUM"
12925 (org-columns-number-to-string (/ (float clocksum) 60.)
12926 'add_times))
12927 props))
12928 (unless (assoc "CATEGORY" props)
12929 (setq value (or (org-get-category)
12930 (progn (org-refresh-category-properties)
12931 (org-get-category))))
12932 (push (cons "CATEGORY" value) props))
12933 (append sum-props (nreverse props)))))))
12935 (defun org-entry-get (pom property &optional inherit)
12936 "Get value of PROPERTY for entry at point-or-marker POM.
12937 If INHERIT is non-nil and the entry does not have the property,
12938 then also check higher levels of the hierarchy.
12939 If INHERIT is the symbol `selective', use inheritance only if the setting
12940 in `org-use-property-inheritance' selects PROPERTY for inheritance.
12941 If the property is present but empty, the return value is the empty string.
12942 If the property is not present at all, nil is returned."
12943 (org-with-point-at pom
12944 (if (and inherit (if (eq inherit 'selective)
12945 (org-property-inherit-p property)
12947 (org-entry-get-with-inheritance property)
12948 (if (member property org-special-properties)
12949 ;; We need a special property. Use `org-entry-properties' to
12950 ;; retrieve it, but specify the wanted property
12951 (cdr (assoc property (org-entry-properties nil 'special property)))
12952 (let ((range (org-get-property-block)))
12953 (if (and range
12954 (goto-char (car range))
12955 (re-search-forward
12956 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
12957 (cdr range) t))
12958 ;; Found the property, return it.
12959 (if (match-end 1)
12960 (org-match-string-no-properties 1)
12961 "")))))))
12963 (defun org-property-or-variable-value (var &optional inherit)
12964 "Check if there is a property fixing the value of VAR.
12965 If yes, return this value. If not, return the current value of the variable."
12966 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
12967 (if (and prop (stringp prop) (string-match "\\S-" prop))
12968 (read prop)
12969 (symbol-value var))))
12971 (defun org-entry-delete (pom property)
12972 "Delete the property PROPERTY from entry at point-or-marker POM."
12973 (org-with-point-at pom
12974 (if (member property org-special-properties)
12975 nil ; cannot delete these properties.
12976 (let ((range (org-get-property-block)))
12977 (if (and range
12978 (goto-char (car range))
12979 (re-search-forward
12980 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
12981 (cdr range) t))
12982 (progn
12983 (delete-region (match-beginning 0) (1+ (point-at-eol)))
12985 nil)))))
12987 ;; Multi-values properties are properties that contain multiple values
12988 ;; These values are assumed to be single words, separated by whitespace.
12989 (defun org-entry-add-to-multivalued-property (pom property value)
12990 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
12991 (let* ((old (org-entry-get pom property))
12992 (values (and old (org-split-string old "[ \t]"))))
12993 (setq value (org-entry-protect-space value))
12994 (unless (member value values)
12995 (setq values (cons value values))
12996 (org-entry-put pom property
12997 (mapconcat 'identity values " ")))))
12999 (defun org-entry-remove-from-multivalued-property (pom property value)
13000 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
13001 (let* ((old (org-entry-get pom property))
13002 (values (and old (org-split-string old "[ \t]"))))
13003 (setq value (org-entry-protect-space value))
13004 (when (member value values)
13005 (setq values (delete value values))
13006 (org-entry-put pom property
13007 (mapconcat 'identity values " ")))))
13009 (defun org-entry-member-in-multivalued-property (pom property value)
13010 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
13011 (let* ((old (org-entry-get pom property))
13012 (values (and old (org-split-string old "[ \t]"))))
13013 (setq value (org-entry-protect-space value))
13014 (member value values)))
13016 (defun org-entry-get-multivalued-property (pom property)
13017 "Return a list of values in a multivalued property."
13018 (let* ((value (org-entry-get pom property))
13019 (values (and value (org-split-string value "[ \t]"))))
13020 (mapcar 'org-entry-restore-space values)))
13022 (defun org-entry-put-multivalued-property (pom property &rest values)
13023 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
13024 VALUES should be a list of strings. Spaces will be protected."
13025 (org-entry-put pom property
13026 (mapconcat 'org-entry-protect-space values " "))
13027 (let* ((value (org-entry-get pom property))
13028 (values (and value (org-split-string value "[ \t]"))))
13029 (mapcar 'org-entry-restore-space values)))
13031 (defun org-entry-protect-space (s)
13032 "Protect spaces and newline in string S."
13033 (while (string-match " " s)
13034 (setq s (replace-match "%20" t t s)))
13035 (while (string-match "\n" s)
13036 (setq s (replace-match "%0A" t t s)))
13039 (defun org-entry-restore-space (s)
13040 "Restore spaces and newline in string S."
13041 (while (string-match "%20" s)
13042 (setq s (replace-match " " t t s)))
13043 (while (string-match "%0A" s)
13044 (setq s (replace-match "\n" t t s)))
13047 (defvar org-entry-property-inherited-from (make-marker)
13048 "Marker pointing to the entry from where a property was inherited.
13049 Each call to `org-entry-get-with-inheritance' will set this marker to the
13050 location of the entry where the inheritance search matched. If there was
13051 no match, the marker will point nowhere.
13052 Note that also `org-entry-get' calls this function, if the INHERIT flag
13053 is set.")
13055 (defun org-entry-get-with-inheritance (property)
13056 "Get entry property, and search higher levels if not present."
13057 (move-marker org-entry-property-inherited-from nil)
13058 (let (tmp)
13059 (save-excursion
13060 (save-restriction
13061 (widen)
13062 (catch 'ex
13063 (while t
13064 (when (setq tmp (org-entry-get nil property))
13065 (org-back-to-heading t)
13066 (move-marker org-entry-property-inherited-from (point))
13067 (throw 'ex tmp))
13068 (or (org-up-heading-safe) (throw 'ex nil)))))
13069 (or tmp
13070 (cdr (assoc property org-file-properties))
13071 (cdr (assoc property org-global-properties))
13072 (cdr (assoc property org-global-properties-fixed))))))
13074 (defvar org-property-changed-functions nil
13075 "Hook called when the value of a property has changed.
13076 Each hook function should accept two arguments, the name of the property
13077 and the new value.")
13079 (defun org-entry-put (pom property value)
13080 "Set PROPERTY to VALUE for entry at point-or-marker POM."
13081 (org-with-point-at pom
13082 (org-back-to-heading t)
13083 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
13084 range)
13085 (cond
13086 ((equal property "TODO")
13087 (when (and (stringp value) (string-match "\\S-" value)
13088 (not (member value org-todo-keywords-1)))
13089 (error "\"%s\" is not a valid TODO state" value))
13090 (if (or (not value)
13091 (not (string-match "\\S-" value)))
13092 (setq value 'none))
13093 (org-todo value)
13094 (org-set-tags nil 'align))
13095 ((equal property "PRIORITY")
13096 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
13097 (string-to-char value) ?\ ))
13098 (org-set-tags nil 'align))
13099 ((equal property "SCHEDULED")
13100 (if (re-search-forward org-scheduled-time-regexp end t)
13101 (cond
13102 ((eq value 'earlier) (org-timestamp-change -1 'day))
13103 ((eq value 'later) (org-timestamp-change 1 'day))
13104 (t (call-interactively 'org-schedule)))
13105 (call-interactively 'org-schedule)))
13106 ((equal property "DEADLINE")
13107 (if (re-search-forward org-deadline-time-regexp end t)
13108 (cond
13109 ((eq value 'earlier) (org-timestamp-change -1 'day))
13110 ((eq value 'later) (org-timestamp-change 1 'day))
13111 (t (call-interactively 'org-deadline)))
13112 (call-interactively 'org-deadline)))
13113 ((member property org-special-properties)
13114 (error "The %s property can not yet be set with `org-entry-put'"
13115 property))
13116 (t ; a non-special property
13117 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
13118 (setq range (org-get-property-block beg end 'force))
13119 (goto-char (car range))
13120 (if (re-search-forward
13121 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
13122 (progn
13123 (delete-region (match-beginning 1) (match-end 1))
13124 (goto-char (match-beginning 1)))
13125 (goto-char (cdr range))
13126 (insert "\n")
13127 (backward-char 1)
13128 (org-indent-line-function)
13129 (insert ":" property ":"))
13130 (and value (insert " " value))
13131 (org-indent-line-function)))))
13132 (run-hook-with-args 'org-property-changed-functions property value)))
13134 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
13135 "Get all property keys in the current buffer.
13136 With INCLUDE-SPECIALS, also list the special properties that reflect things
13137 like tags and TODO state.
13138 With INCLUDE-DEFAULTS, also include properties that has special meaning
13139 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
13140 With INCLUDE-COLUMNS, also include property names given in COLUMN
13141 formats in the current buffer."
13142 (let (rtn range cfmt s p)
13143 (save-excursion
13144 (save-restriction
13145 (widen)
13146 (goto-char (point-min))
13147 (while (re-search-forward org-property-start-re nil t)
13148 (setq range (org-get-property-block))
13149 (goto-char (car range))
13150 (while (re-search-forward
13151 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
13152 (cdr range) t)
13153 (add-to-list 'rtn (org-match-string-no-properties 1)))
13154 (outline-next-heading))))
13156 (when include-specials
13157 (setq rtn (append org-special-properties rtn)))
13159 (when include-defaults
13160 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
13161 (add-to-list 'rtn org-effort-property))
13163 (when include-columns
13164 (save-excursion
13165 (save-restriction
13166 (widen)
13167 (goto-char (point-min))
13168 (while (re-search-forward
13169 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
13170 nil t)
13171 (setq cfmt (match-string 2) s 0)
13172 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
13173 cfmt s)
13174 (setq s (match-end 0)
13175 p (match-string 1 cfmt))
13176 (unless (or (equal p "ITEM")
13177 (member p org-special-properties))
13178 (add-to-list 'rtn (match-string 1 cfmt))))))))
13180 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
13182 (defun org-property-values (key)
13183 "Return a list of all values of property KEY."
13184 (save-excursion
13185 (save-restriction
13186 (widen)
13187 (goto-char (point-min))
13188 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
13189 values)
13190 (while (re-search-forward re nil t)
13191 (add-to-list 'values (org-trim (match-string 1))))
13192 (delete "" values)))))
13194 (defun org-insert-property-drawer ()
13195 "Insert a property drawer into the current entry."
13196 (interactive)
13197 (org-back-to-heading t)
13198 (looking-at outline-regexp)
13199 (let ((indent (if org-adapt-indentation
13200 (- (match-end 0)(match-beginning 0))
13202 (beg (point))
13203 (re (concat "^[ \t]*" org-keyword-time-regexp))
13204 end hiddenp)
13205 (outline-next-heading)
13206 (setq end (point))
13207 (goto-char beg)
13208 (while (re-search-forward re end t))
13209 (setq hiddenp (org-invisible-p))
13210 (end-of-line 1)
13211 (and (equal (char-after) ?\n) (forward-char 1))
13212 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
13213 (if (member (match-string 1) '("CLOCK:" ":END:"))
13214 ;; just skip this line
13215 (beginning-of-line 2)
13216 ;; Drawer start, find the end
13217 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
13218 (beginning-of-line 1)))
13219 (org-skip-over-state-notes)
13220 (skip-chars-backward " \t\n\r")
13221 (if (eq (char-before) ?*) (forward-char 1))
13222 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
13223 (beginning-of-line 0)
13224 (org-indent-to-column indent)
13225 (beginning-of-line 2)
13226 (org-indent-to-column indent)
13227 (beginning-of-line 0)
13228 (if hiddenp
13229 (save-excursion
13230 (org-back-to-heading t)
13231 (hide-entry))
13232 (org-flag-drawer t))))
13234 (defun org-set-property (property value)
13235 "In the current entry, set PROPERTY to VALUE.
13236 When called interactively, this will prompt for a property name, offering
13237 completion on existing and default properties. And then it will prompt
13238 for a value, offering completion either on allowed values (via an inherited
13239 xxx_ALL property) or on existing values in other instances of this property
13240 in the current file."
13241 (interactive
13242 (let* ((completion-ignore-case t)
13243 (keys (org-buffer-property-keys nil t t))
13244 (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
13245 (prop (if (member prop0 keys)
13246 prop0
13247 (or (cdr (assoc (downcase prop0)
13248 (mapcar (lambda (x) (cons (downcase x) x))
13249 keys)))
13250 prop0)))
13251 (cur (org-entry-get nil prop))
13252 (prompt (concat prop " value"
13253 (if (and cur (string-match "\\S-" cur))
13254 (concat " [" cur "]") "") ": "))
13255 (allowed (org-property-get-allowed-values nil prop 'table))
13256 (existing (mapcar 'list (org-property-values prop)))
13257 (val (if allowed
13258 (org-completing-read prompt allowed nil
13259 (not (get-text-property 0 'org-unrestricted
13260 (caar allowed))))
13261 (let (org-completion-use-ido org-completion-use-iswitchb)
13262 (org-completing-read prompt existing nil nil "" nil cur)))))
13263 (list prop (if (equal val "") cur val))))
13264 (unless (equal (org-entry-get nil property) value)
13265 (org-entry-put nil property value)))
13267 (defun org-delete-property (property)
13268 "In the current entry, delete PROPERTY."
13269 (interactive
13270 (let* ((completion-ignore-case t)
13271 (prop (org-icompleting-read "Property: " (org-entry-properties nil 'standard))))
13272 (list prop)))
13273 (message "Property %s %s" property
13274 (if (org-entry-delete nil property)
13275 "deleted"
13276 "was not present in the entry")))
13278 (defun org-delete-property-globally (property)
13279 "Remove PROPERTY globally, from all entries."
13280 (interactive
13281 (let* ((completion-ignore-case t)
13282 (prop (org-icompleting-read
13283 "Globally remove property: "
13284 (mapcar 'list (org-buffer-property-keys)))))
13285 (list prop)))
13286 (save-excursion
13287 (save-restriction
13288 (widen)
13289 (goto-char (point-min))
13290 (let ((cnt 0))
13291 (while (re-search-forward
13292 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
13293 nil t)
13294 (setq cnt (1+ cnt))
13295 (replace-match ""))
13296 (message "Property \"%s\" removed from %d entries" property cnt)))))
13298 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
13300 (defun org-compute-property-at-point ()
13301 "Compute the property at point.
13302 This looks for an enclosing column format, extracts the operator and
13303 then applies it to the property in the column format's scope."
13304 (interactive)
13305 (unless (org-at-property-p)
13306 (error "Not at a property"))
13307 (let ((prop (org-match-string-no-properties 2)))
13308 (org-columns-get-format-and-top-level)
13309 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
13310 (error "No operator defined for property %s" prop))
13311 (org-columns-compute prop)))
13313 (defvar org-property-allowed-value-functions nil
13314 "Hook for functions supplying allowed values for a specific property.
13315 The functions must take a single argument, the name of the property, and
13316 return a flat list of allowed values. If \":ETC\" is one of
13317 the values, this means that these values are intended as defaults for
13318 completion, but that other values should be allowed too.
13319 The functions must return nil if they are not responsible for this
13320 property.")
13322 (defun org-property-get-allowed-values (pom property &optional table)
13323 "Get allowed values for the property PROPERTY.
13324 When TABLE is non-nil, return an alist that can directly be used for
13325 completion."
13326 (let (vals)
13327 (cond
13328 ((equal property "TODO")
13329 (setq vals (org-with-point-at pom
13330 (append org-todo-keywords-1 '("")))))
13331 ((equal property "PRIORITY")
13332 (let ((n org-lowest-priority))
13333 (while (>= n org-highest-priority)
13334 (push (char-to-string n) vals)
13335 (setq n (1- n)))))
13336 ((member property org-special-properties))
13337 ((setq vals (run-hook-with-args-until-success
13338 'org-property-allowed-value-functions property)))
13340 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
13341 (when (and vals (string-match "\\S-" vals))
13342 (setq vals (car (read-from-string (concat "(" vals ")"))))
13343 (setq vals (mapcar (lambda (x)
13344 (cond ((stringp x) x)
13345 ((numberp x) (number-to-string x))
13346 ((symbolp x) (symbol-name x))
13347 (t "???")))
13348 vals)))))
13349 (when (member ":ETC" vals)
13350 (setq vals (remove ":ETC" vals))
13351 (org-add-props (car vals) '(org-unrestricted t)))
13352 (if table (mapcar 'list vals) vals)))
13354 (defun org-property-previous-allowed-value (&optional previous)
13355 "Switch to the next allowed value for this property."
13356 (interactive)
13357 (org-property-next-allowed-value t))
13359 (defun org-property-next-allowed-value (&optional previous)
13360 "Switch to the next allowed value for this property."
13361 (interactive)
13362 (unless (org-at-property-p)
13363 (error "Not at a property"))
13364 (let* ((key (match-string 2))
13365 (value (match-string 3))
13366 (allowed (or (org-property-get-allowed-values (point) key)
13367 (and (member value '("[ ]" "[-]" "[X]"))
13368 '("[ ]" "[X]"))))
13369 nval)
13370 (unless allowed
13371 (error "Allowed values for this property have not been defined"))
13372 (if previous (setq allowed (reverse allowed)))
13373 (if (member value allowed)
13374 (setq nval (car (cdr (member value allowed)))))
13375 (setq nval (or nval (car allowed)))
13376 (if (equal nval value)
13377 (error "Only one allowed value for this property"))
13378 (org-at-property-p)
13379 (replace-match (concat " :" key ": " nval) t t)
13380 (org-indent-line-function)
13381 (beginning-of-line 1)
13382 (skip-chars-forward " \t")
13383 (run-hook-with-args 'org-property-changed-functions key nval)))
13385 (defun org-find-entry-with-id (ident)
13386 "Locate the entry that contains the ID property with exact value IDENT.
13387 IDENT can be a string, a symbol or a number, this function will search for
13388 the string representation of it.
13389 Return the position where this entry starts, or nil if there is no such entry."
13390 (interactive "sID: ")
13391 (let ((id (cond
13392 ((stringp ident) ident)
13393 ((symbol-name ident) (symbol-name ident))
13394 ((numberp ident) (number-to-string ident))
13395 (t (error "IDENT %s must be a string, symbol or number" ident))))
13396 (case-fold-search nil))
13397 (save-excursion
13398 (save-restriction
13399 (widen)
13400 (goto-char (point-min))
13401 (when (re-search-forward
13402 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
13403 nil t)
13404 (org-back-to-heading t)
13405 (point))))))
13407 ;;;; Timestamps
13409 (defvar org-last-changed-timestamp nil)
13410 (defvar org-last-inserted-timestamp nil
13411 "The last time stamp inserted with `org-insert-time-stamp'.")
13412 (defvar org-time-was-given) ; dynamically scoped parameter
13413 (defvar org-end-time-was-given) ; dynamically scoped parameter
13414 (defvar org-ts-what) ; dynamically scoped parameter
13416 (defun org-time-stamp (arg &optional inactive)
13417 "Prompt for a date/time and insert a time stamp.
13418 If the user specifies a time like HH:MM, or if this command is called
13419 with a prefix argument, the time stamp will contain date and time.
13420 Otherwise, only the date will be included. All parts of a date not
13421 specified by the user will be filled in from the current date/time.
13422 So if you press just return without typing anything, the time stamp
13423 will represent the current date/time. If there is already a timestamp
13424 at the cursor, it will be modified."
13425 (interactive "P")
13426 (let* ((ts nil)
13427 (default-time
13428 ;; Default time is either today, or, when entering a range,
13429 ;; the range start.
13430 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
13431 (save-excursion
13432 (re-search-backward
13433 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
13434 (- (point) 20) t)))
13435 (apply 'encode-time (org-parse-time-string (match-string 1)))
13436 (current-time)))
13437 (default-input (and ts (org-get-compact-tod ts)))
13438 org-time-was-given org-end-time-was-given time)
13439 (cond
13440 ((and (org-at-timestamp-p t)
13441 (memq last-command '(org-time-stamp org-time-stamp-inactive))
13442 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
13443 (insert "--")
13444 (setq time (let ((this-command this-command))
13445 (org-read-date arg 'totime nil nil
13446 default-time default-input)))
13447 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
13448 ((org-at-timestamp-p t)
13449 (setq time (let ((this-command this-command))
13450 (org-read-date arg 'totime nil nil default-time default-input)))
13451 (when (org-at-timestamp-p t) ; just to get the match data
13452 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
13453 (replace-match "")
13454 (setq org-last-changed-timestamp
13455 (org-insert-time-stamp
13456 time (or org-time-was-given arg)
13457 inactive nil nil (list org-end-time-was-given))))
13458 (message "Timestamp updated"))
13460 (setq time (let ((this-command this-command))
13461 (org-read-date arg 'totime nil nil default-time default-input)))
13462 (org-insert-time-stamp time (or org-time-was-given arg) inactive
13463 nil nil (list org-end-time-was-given))))))
13465 ;; FIXME: can we use this for something else, like computing time differences?
13466 (defun org-get-compact-tod (s)
13467 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
13468 (let* ((t1 (match-string 1 s))
13469 (h1 (string-to-number (match-string 2 s)))
13470 (m1 (string-to-number (match-string 3 s)))
13471 (t2 (and (match-end 4) (match-string 5 s)))
13472 (h2 (and t2 (string-to-number (match-string 6 s))))
13473 (m2 (and t2 (string-to-number (match-string 7 s))))
13474 dh dm)
13475 (if (not t2)
13477 (setq dh (- h2 h1) dm (- m2 m1))
13478 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
13479 (concat t1 "+" (number-to-string dh)
13480 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
13482 (defun org-time-stamp-inactive (&optional arg)
13483 "Insert an inactive time stamp.
13484 An inactive time stamp is enclosed in square brackets instead of angle
13485 brackets. It is inactive in the sense that it does not trigger agenda entries,
13486 does not link to the calendar and cannot be changed with the S-cursor keys.
13487 So these are more for recording a certain time/date."
13488 (interactive "P")
13489 (org-time-stamp arg 'inactive))
13491 (defvar org-date-ovl (make-overlay 1 1))
13492 (overlay-put org-date-ovl 'face 'org-warning)
13493 (org-detach-overlay org-date-ovl)
13495 (defvar org-ans1) ; dynamically scoped parameter
13496 (defvar org-ans2) ; dynamically scoped parameter
13498 (defvar org-plain-time-of-day-regexp) ; defined below
13500 (defvar org-overriding-default-time nil) ; dynamically scoped
13501 (defvar org-read-date-overlay nil)
13502 (defvar org-dcst nil) ; dynamically scoped
13503 (defvar org-read-date-history nil)
13504 (defvar org-read-date-final-answer nil)
13506 (defun org-read-date (&optional with-time to-time from-string prompt
13507 default-time default-input)
13508 "Read a date, possibly a time, and make things smooth for the user.
13509 The prompt will suggest to enter an ISO date, but you can also enter anything
13510 which will at least partially be understood by `parse-time-string'.
13511 Unrecognized parts of the date will default to the current day, month, year,
13512 hour and minute. If this command is called to replace a timestamp at point,
13513 of to enter the second timestamp of a range, the default time is taken from the
13514 existing stamp. For example,
13515 3-2-5 --> 2003-02-05
13516 feb 15 --> currentyear-02-15
13517 sep 12 9 --> 2009-09-12
13518 12:45 --> today 12:45
13519 22 sept 0:34 --> currentyear-09-22 0:34
13520 12 --> currentyear-currentmonth-12
13521 Fri --> nearest Friday (today or later)
13522 etc.
13524 Furthermore you can specify a relative date by giving, as the *first* thing
13525 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
13526 change in days weeks, months, years.
13527 With a single plus or minus, the date is relative to today. With a double
13528 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
13529 +4d --> four days from today
13530 +4 --> same as above
13531 +2w --> two weeks from today
13532 ++5 --> five days from default date
13534 The function understands only English month and weekday abbreviations,
13535 but this can be configured with the variables `parse-time-months' and
13536 `parse-time-weekdays'.
13538 While prompting, a calendar is popped up - you can also select the
13539 date with the mouse (button 1). The calendar shows a period of three
13540 months. To scroll it to other months, use the keys `>' and `<'.
13541 If you don't like the calendar, turn it off with
13542 \(setq org-read-date-popup-calendar nil)
13544 With optional argument TO-TIME, the date will immediately be converted
13545 to an internal time.
13546 With an optional argument WITH-TIME, the prompt will suggest to also
13547 insert a time. Note that when WITH-TIME is not set, you can still
13548 enter a time, and this function will inform the calling routine about
13549 this change. The calling routine may then choose to change the format
13550 used to insert the time stamp into the buffer to include the time.
13551 With optional argument FROM-STRING, read from this string instead from
13552 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
13553 the time/date that is used for everything that is not specified by the
13554 user."
13555 (require 'parse-time)
13556 (let* ((org-time-stamp-rounding-minutes
13557 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
13558 (org-dcst org-display-custom-times)
13559 (ct (org-current-time))
13560 (def (or org-overriding-default-time default-time ct))
13561 (defdecode (decode-time def))
13562 (dummy (progn
13563 (when (< (nth 2 defdecode) org-extend-today-until)
13564 (setcar (nthcdr 2 defdecode) -1)
13565 (setcar (nthcdr 1 defdecode) 59)
13566 (setq def (apply 'encode-time defdecode)
13567 defdecode (decode-time def)))))
13568 (calendar-frame-setup nil)
13569 (calendar-move-hook nil)
13570 (calendar-view-diary-initially-flag nil)
13571 (calendar-view-holidays-initially-flag nil)
13572 (timestr (format-time-string
13573 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
13574 (prompt (concat (if prompt (concat prompt " ") "")
13575 (format "Date+time [%s]: " timestr)))
13576 ans (org-ans0 "") org-ans1 org-ans2 final)
13578 (cond
13579 (from-string (setq ans from-string))
13580 (org-read-date-popup-calendar
13581 (save-excursion
13582 (save-window-excursion
13583 (calendar)
13584 (calendar-forward-day (- (time-to-days def)
13585 (calendar-absolute-from-gregorian
13586 (calendar-current-date))))
13587 (org-eval-in-calendar nil t)
13588 (let* ((old-map (current-local-map))
13589 (map (copy-keymap calendar-mode-map))
13590 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
13591 (org-defkey map (kbd "RET") 'org-calendar-select)
13592 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
13593 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
13594 (org-defkey minibuffer-local-map [(meta shift left)]
13595 (lambda () (interactive)
13596 (org-eval-in-calendar '(calendar-backward-month 1))))
13597 (org-defkey minibuffer-local-map [(meta shift right)]
13598 (lambda () (interactive)
13599 (org-eval-in-calendar '(calendar-forward-month 1))))
13600 (org-defkey minibuffer-local-map [(meta shift up)]
13601 (lambda () (interactive)
13602 (org-eval-in-calendar '(calendar-backward-year 1))))
13603 (org-defkey minibuffer-local-map [(meta shift down)]
13604 (lambda () (interactive)
13605 (org-eval-in-calendar '(calendar-forward-year 1))))
13606 (org-defkey minibuffer-local-map [?\e (shift left)]
13607 (lambda () (interactive)
13608 (org-eval-in-calendar '(calendar-backward-month 1))))
13609 (org-defkey minibuffer-local-map [?\e (shift right)]
13610 (lambda () (interactive)
13611 (org-eval-in-calendar '(calendar-forward-month 1))))
13612 (org-defkey minibuffer-local-map [?\e (shift up)]
13613 (lambda () (interactive)
13614 (org-eval-in-calendar '(calendar-backward-year 1))))
13615 (org-defkey minibuffer-local-map [?\e (shift down)]
13616 (lambda () (interactive)
13617 (org-eval-in-calendar '(calendar-forward-year 1))))
13618 (org-defkey minibuffer-local-map [(shift up)]
13619 (lambda () (interactive)
13620 (org-eval-in-calendar '(calendar-backward-week 1))))
13621 (org-defkey minibuffer-local-map [(shift down)]
13622 (lambda () (interactive)
13623 (org-eval-in-calendar '(calendar-forward-week 1))))
13624 (org-defkey minibuffer-local-map [(shift left)]
13625 (lambda () (interactive)
13626 (org-eval-in-calendar '(calendar-backward-day 1))))
13627 (org-defkey minibuffer-local-map [(shift right)]
13628 (lambda () (interactive)
13629 (org-eval-in-calendar '(calendar-forward-day 1))))
13630 (org-defkey minibuffer-local-map ">"
13631 (lambda () (interactive)
13632 (org-eval-in-calendar '(scroll-calendar-left 1))))
13633 (org-defkey minibuffer-local-map "<"
13634 (lambda () (interactive)
13635 (org-eval-in-calendar '(scroll-calendar-right 1))))
13636 (run-hooks 'org-read-date-minibuffer-setup-hook)
13637 (unwind-protect
13638 (progn
13639 (use-local-map map)
13640 (add-hook 'post-command-hook 'org-read-date-display)
13641 (setq org-ans0 (read-string prompt default-input
13642 'org-read-date-history nil))
13643 ;; org-ans0: from prompt
13644 ;; org-ans1: from mouse click
13645 ;; org-ans2: from calendar motion
13646 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
13647 (remove-hook 'post-command-hook 'org-read-date-display)
13648 (use-local-map old-map)
13649 (when org-read-date-overlay
13650 (delete-overlay org-read-date-overlay)
13651 (setq org-read-date-overlay nil)))))))
13653 (t ; Naked prompt only
13654 (unwind-protect
13655 (setq ans (read-string prompt default-input
13656 'org-read-date-history timestr))
13657 (when org-read-date-overlay
13658 (delete-overlay org-read-date-overlay)
13659 (setq org-read-date-overlay nil)))))
13661 (setq final (org-read-date-analyze ans def defdecode))
13662 (setq org-read-date-final-answer ans)
13664 (if to-time
13665 (apply 'encode-time final)
13666 (if (and (boundp 'org-time-was-given) org-time-was-given)
13667 (format "%04d-%02d-%02d %02d:%02d"
13668 (nth 5 final) (nth 4 final) (nth 3 final)
13669 (nth 2 final) (nth 1 final))
13670 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
13672 (defvar def)
13673 (defvar defdecode)
13674 (defvar with-time)
13675 (defvar org-read-date-analyze-futurep nil)
13676 (defun org-read-date-display ()
13677 "Display the current date prompt interpretation in the minibuffer."
13678 (when org-read-date-display-live
13679 (when org-read-date-overlay
13680 (delete-overlay org-read-date-overlay))
13681 (let ((p (point)))
13682 (end-of-line 1)
13683 (while (not (equal (buffer-substring
13684 (max (point-min) (- (point) 4)) (point))
13685 " "))
13686 (insert " "))
13687 (goto-char p))
13688 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
13689 " " (or org-ans1 org-ans2)))
13690 (org-end-time-was-given nil)
13691 (f (org-read-date-analyze ans def defdecode))
13692 (fmts (if org-dcst
13693 org-time-stamp-custom-formats
13694 org-time-stamp-formats))
13695 (fmt (if (or with-time
13696 (and (boundp 'org-time-was-given) org-time-was-given))
13697 (cdr fmts)
13698 (car fmts)))
13699 (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
13700 (when (and org-end-time-was-given
13701 (string-match org-plain-time-of-day-regexp txt))
13702 (setq txt (concat (substring txt 0 (match-end 0)) "-"
13703 org-end-time-was-given
13704 (substring txt (match-end 0)))))
13705 (when org-read-date-analyze-futurep
13706 (setq txt (concat txt " (=>F)")))
13707 (setq org-read-date-overlay
13708 (make-overlay (1- (point-at-eol)) (point-at-eol)))
13709 (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
13711 (defun org-read-date-analyze (ans def defdecode)
13712 "Analyse the combined answer of the date prompt."
13713 ;; FIXME: cleanup and comment
13714 (let ((nowdecode (decode-time (current-time)))
13715 delta deltan deltaw deltadef year month day
13716 hour minute second wday pm h2 m2 tl wday1
13717 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
13718 (setq org-read-date-analyze-futurep nil)
13719 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
13720 (setq ans "+0"))
13722 (when (setq delta (org-read-date-get-relative ans (current-time) def))
13723 (setq ans (replace-match "" t t ans)
13724 deltan (car delta)
13725 deltaw (nth 1 delta)
13726 deltadef (nth 2 delta)))
13728 ;; Check if there is an iso week date in there
13729 ;; If yes, store the info and postpone interpreting it until the rest
13730 ;; of the parsing is done
13731 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
13732 (setq iso-year (if (match-end 1)
13733 (org-small-year-to-year
13734 (string-to-number (match-string 1 ans))))
13735 iso-weekday (if (match-end 3)
13736 (string-to-number (match-string 3 ans)))
13737 iso-week (string-to-number (match-string 2 ans)))
13738 (setq ans (replace-match "" t t ans)))
13740 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
13741 (when (string-match
13742 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
13743 (setq year (if (match-end 2)
13744 (string-to-number (match-string 2 ans))
13745 (progn (setq kill-year t)
13746 (string-to-number (format-time-string "%Y"))))
13747 month (string-to-number (match-string 3 ans))
13748 day (string-to-number (match-string 4 ans)))
13749 (if (< year 100) (setq year (+ 2000 year)))
13750 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
13751 t nil ans)))
13752 ;; Help matching american dates, like 5/30 or 5/30/7
13753 (when (string-match
13754 "^ *\\([0-3]?[0-9]\\)/\\([0-1]?[0-9]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
13755 (setq year (if (match-end 4)
13756 (string-to-number (match-string 4 ans))
13757 (progn (setq kill-year t)
13758 (string-to-number (format-time-string "%Y"))))
13759 month (string-to-number (match-string 1 ans))
13760 day (string-to-number (match-string 2 ans)))
13761 (if (< year 100) (setq year (+ 2000 year)))
13762 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
13763 t nil ans)))
13764 ;; Help matching am/pm times, because `parse-time-string' does not do that.
13765 ;; If there is a time with am/pm, and *no* time without it, we convert
13766 ;; so that matching will be successful.
13767 (loop for i from 1 to 2 do ; twice, for end time as well
13768 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
13769 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
13770 (setq hour (string-to-number (match-string 1 ans))
13771 minute (if (match-end 3)
13772 (string-to-number (match-string 3 ans))
13774 pm (equal ?p
13775 (string-to-char (downcase (match-string 4 ans)))))
13776 (if (and (= hour 12) (not pm))
13777 (setq hour 0)
13778 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
13779 (setq ans (replace-match (format "%02d:%02d" hour minute)
13780 t t ans))))
13782 ;; Check if a time range is given as a duration
13783 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
13784 (setq hour (string-to-number (match-string 1 ans))
13785 h2 (+ hour (string-to-number (match-string 3 ans)))
13786 minute (string-to-number (match-string 2 ans))
13787 m2 (+ minute (if (match-end 5) (string-to-number
13788 (match-string 5 ans))0)))
13789 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
13790 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
13791 t t ans)))
13793 ;; Check if there is a time range
13794 (when (boundp 'org-end-time-was-given)
13795 (setq org-time-was-given nil)
13796 (when (and (string-match org-plain-time-of-day-regexp ans)
13797 (match-end 8))
13798 (setq org-end-time-was-given (match-string 8 ans))
13799 (setq ans (concat (substring ans 0 (match-beginning 7))
13800 (substring ans (match-end 7))))))
13802 (setq tl (parse-time-string ans)
13803 day (or (nth 3 tl) (nth 3 defdecode))
13804 month (or (nth 4 tl)
13805 (if (and org-read-date-prefer-future
13806 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
13807 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
13808 (nth 4 defdecode)))
13809 year (or (and (not kill-year) (nth 5 tl))
13810 (if (and org-read-date-prefer-future
13811 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
13812 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
13813 (nth 5 defdecode)))
13814 hour (or (nth 2 tl) (nth 2 defdecode))
13815 minute (or (nth 1 tl) (nth 1 defdecode))
13816 second (or (nth 0 tl) 0)
13817 wday (nth 6 tl))
13819 (when (and (eq org-read-date-prefer-future 'time)
13820 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
13821 (equal day (nth 3 nowdecode))
13822 (equal month (nth 4 nowdecode))
13823 (equal year (nth 5 nowdecode))
13824 (nth 2 tl)
13825 (or (< (nth 2 tl) (nth 2 nowdecode))
13826 (and (= (nth 2 tl) (nth 2 nowdecode))
13827 (nth 1 tl)
13828 (< (nth 1 tl) (nth 1 nowdecode)))))
13829 (setq day (1+ day)
13830 futurep t))
13832 ;; Special date definitions below
13833 (cond
13834 (iso-week
13835 ;; There was an iso week
13836 (require 'cal-iso)
13837 (setq futurep nil)
13838 (setq year (or iso-year year)
13839 day (or iso-weekday wday 1)
13840 wday nil ; to make sure that the trigger below does not match
13841 iso-date (calendar-gregorian-from-absolute
13842 (calendar-absolute-from-iso
13843 (list iso-week day year))))
13844 ; FIXME: Should we also push ISO weeks into the future?
13845 ; (when (and org-read-date-prefer-future
13846 ; (not iso-year)
13847 ; (< (calendar-absolute-from-gregorian iso-date)
13848 ; (time-to-days (current-time))))
13849 ; (setq year (1+ year)
13850 ; iso-date (calendar-gregorian-from-absolute
13851 ; (calendar-absolute-from-iso
13852 ; (list iso-week day year)))))
13853 (setq month (car iso-date)
13854 year (nth 2 iso-date)
13855 day (nth 1 iso-date)))
13856 (deltan
13857 (setq futurep nil)
13858 (unless deltadef
13859 (let ((now (decode-time (current-time))))
13860 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
13861 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
13862 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
13863 ((equal deltaw "m") (setq month (+ month deltan)))
13864 ((equal deltaw "y") (setq year (+ year deltan)))))
13865 ((and wday (not (nth 3 tl)))
13866 (setq futurep nil)
13867 ;; Weekday was given, but no day, so pick that day in the week
13868 ;; on or after the derived date.
13869 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
13870 (unless (equal wday wday1)
13871 (setq day (+ day (% (- wday wday1 -7) 7))))))
13872 (if (and (boundp 'org-time-was-given)
13873 (nth 2 tl))
13874 (setq org-time-was-given t))
13875 (if (< year 100) (setq year (+ 2000 year)))
13876 (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
13877 (setq org-read-date-analyze-futurep futurep)
13878 (list second minute hour day month year)))
13880 (defvar parse-time-weekdays)
13882 (defun org-read-date-get-relative (s today default)
13883 "Check string S for special relative date string.
13884 TODAY and DEFAULT are internal times, for today and for a default.
13885 Return shift list (N what def-flag)
13886 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
13887 N is the number of WHATs to shift.
13888 DEF-FLAG is t when a double ++ or -- indicates shift relative to
13889 the DEFAULT date rather than TODAY."
13890 (when (and
13891 (string-match
13892 (concat
13893 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
13894 "\\([0-9]+\\)?"
13895 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
13896 "\\([ \t]\\|$\\)") s)
13897 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
13898 (let* ((dir (if (> (match-end 1) (match-beginning 1))
13899 (string-to-char (substring (match-string 1 s) -1))
13900 ?+))
13901 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
13902 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
13903 (what (if (match-end 3) (match-string 3 s) "d"))
13904 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
13905 (date (if rel default today))
13906 (wday (nth 6 (decode-time date)))
13907 delta)
13908 (if wday1
13909 (progn
13910 (setq delta (mod (+ 7 (- wday1 wday)) 7))
13911 (if (= dir ?-) (setq delta (- delta 7)))
13912 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
13913 (list delta "d" rel))
13914 (list (* n (if (= dir ?-) -1 1)) what rel)))))
13916 (defun org-order-calendar-date-args (arg1 arg2 arg3)
13917 "Turn a user-specified date into the internal representation.
13918 The internal representation needed by the calendar is (month day year).
13919 This is a wrapper to handle the brain-dead convention in calendar that
13920 user function argument order change dependent on argument order."
13921 (if (boundp 'calendar-date-style)
13922 (cond
13923 ((eq calendar-date-style 'american)
13924 (list arg1 arg2 arg3))
13925 ((eq calendar-date-style 'european)
13926 (list arg2 arg1 arg3))
13927 ((eq calendar-date-style 'iso)
13928 (list arg2 arg3 arg1)))
13929 (if (org-bound-and-true-p european-calendar-style)
13930 (list arg2 arg1 arg3)
13931 (list arg1 arg2 arg3))))
13933 (defun org-eval-in-calendar (form &optional keepdate)
13934 "Eval FORM in the calendar window and return to current window.
13935 Also, store the cursor date in variable org-ans2."
13936 (let ((sf (selected-frame))
13937 (sw (selected-window)))
13938 (select-window (get-buffer-window "*Calendar*" t))
13939 (eval form)
13940 (when (and (not keepdate) (calendar-cursor-to-date))
13941 (let* ((date (calendar-cursor-to-date))
13942 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13943 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
13944 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
13945 (select-window sw)
13946 (org-select-frame-set-input-focus sf)))
13948 (defun org-calendar-select ()
13949 "Return to `org-read-date' with the date currently selected.
13950 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
13951 (interactive)
13952 (when (calendar-cursor-to-date)
13953 (let* ((date (calendar-cursor-to-date))
13954 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13955 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
13956 (if (active-minibuffer-window) (exit-minibuffer))))
13958 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
13959 "Insert a date stamp for the date given by the internal TIME.
13960 WITH-HM means use the stamp format that includes the time of the day.
13961 INACTIVE means use square brackets instead of angular ones, so that the
13962 stamp will not contribute to the agenda.
13963 PRE and POST are optional strings to be inserted before and after the
13964 stamp.
13965 The command returns the inserted time stamp."
13966 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
13967 stamp)
13968 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
13969 (insert-before-markers (or pre ""))
13970 (insert-before-markers (setq stamp (format-time-string fmt time)))
13971 (when (listp extra)
13972 (setq extra (car extra))
13973 (if (and (stringp extra)
13974 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
13975 (setq extra (format "-%02d:%02d"
13976 (string-to-number (match-string 1 extra))
13977 (string-to-number (match-string 2 extra))))
13978 (setq extra nil)))
13979 (when extra
13980 (backward-char 1)
13981 (insert-before-markers extra)
13982 (forward-char 1))
13983 (insert-before-markers (or post ""))
13984 (setq org-last-inserted-timestamp stamp)))
13986 (defun org-toggle-time-stamp-overlays ()
13987 "Toggle the use of custom time stamp formats."
13988 (interactive)
13989 (setq org-display-custom-times (not org-display-custom-times))
13990 (unless org-display-custom-times
13991 (let ((p (point-min)) (bmp (buffer-modified-p)))
13992 (while (setq p (next-single-property-change p 'display))
13993 (if (and (get-text-property p 'display)
13994 (eq (get-text-property p 'face) 'org-date))
13995 (remove-text-properties
13996 p (setq p (next-single-property-change p 'display))
13997 '(display t))))
13998 (set-buffer-modified-p bmp)))
13999 (if (featurep 'xemacs)
14000 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
14001 (org-restart-font-lock)
14002 (setq org-table-may-need-update t)
14003 (if org-display-custom-times
14004 (message "Time stamps are overlayed with custom format")
14005 (message "Time stamp overlays removed")))
14007 (defun org-display-custom-time (beg end)
14008 "Overlay modified time stamp format over timestamp between BEG and END."
14009 (let* ((ts (buffer-substring beg end))
14010 t1 w1 with-hm tf time str w2 (off 0))
14011 (save-match-data
14012 (setq t1 (org-parse-time-string ts t))
14013 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
14014 (setq off (- (match-end 0) (match-beginning 0)))))
14015 (setq end (- end off))
14016 (setq w1 (- end beg)
14017 with-hm (and (nth 1 t1) (nth 2 t1))
14018 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
14019 time (org-fix-decoded-time t1)
14020 str (org-add-props
14021 (format-time-string
14022 (substring tf 1 -1) (apply 'encode-time time))
14023 nil 'mouse-face 'highlight)
14024 w2 (length str))
14025 (if (not (= w2 w1))
14026 (add-text-properties (1+ beg) (+ 2 beg)
14027 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
14028 (if (featurep 'xemacs)
14029 (progn
14030 (put-text-property beg end 'invisible t)
14031 (put-text-property beg end 'end-glyph (make-glyph str)))
14032 (put-text-property beg end 'display str))))
14034 (defun org-translate-time (string)
14035 "Translate all timestamps in STRING to custom format.
14036 But do this only if the variable `org-display-custom-times' is set."
14037 (when org-display-custom-times
14038 (save-match-data
14039 (let* ((start 0)
14040 (re org-ts-regexp-both)
14041 t1 with-hm inactive tf time str beg end)
14042 (while (setq start (string-match re string start))
14043 (setq beg (match-beginning 0)
14044 end (match-end 0)
14045 t1 (save-match-data
14046 (org-parse-time-string (substring string beg end) t))
14047 with-hm (and (nth 1 t1) (nth 2 t1))
14048 inactive (equal (substring string beg (1+ beg)) "[")
14049 tf (funcall (if with-hm 'cdr 'car)
14050 org-time-stamp-custom-formats)
14051 time (org-fix-decoded-time t1)
14052 str (format-time-string
14053 (concat
14054 (if inactive "[" "<") (substring tf 1 -1)
14055 (if inactive "]" ">"))
14056 (apply 'encode-time time))
14057 string (replace-match str t t string)
14058 start (+ start (length str)))))))
14059 string)
14061 (defun org-fix-decoded-time (time)
14062 "Set 0 instead of nil for the first 6 elements of time.
14063 Don't touch the rest."
14064 (let ((n 0))
14065 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
14067 (defun org-days-to-time (timestamp-string)
14068 "Difference between TIMESTAMP-STRING and now in days."
14069 (- (time-to-days (org-time-string-to-time timestamp-string))
14070 (time-to-days (current-time))))
14072 (defun org-deadline-close (timestamp-string &optional ndays)
14073 "Is the time in TIMESTAMP-STRING close to the current date?"
14074 (setq ndays (or ndays (org-get-wdays timestamp-string)))
14075 (and (< (org-days-to-time timestamp-string) ndays)
14076 (not (org-entry-is-done-p))))
14078 (defun org-get-wdays (ts)
14079 "Get the deadline lead time appropriate for timestring TS."
14080 (cond
14081 ((<= org-deadline-warning-days 0)
14082 ;; 0 or negative, enforce this value no matter what
14083 (- org-deadline-warning-days))
14084 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
14085 ;; lead time is specified.
14086 (floor (* (string-to-number (match-string 1 ts))
14087 (cdr (assoc (match-string 2 ts)
14088 '(("d" . 1) ("w" . 7)
14089 ("m" . 30.4) ("y" . 365.25)))))))
14090 ;; go for the default.
14091 (t org-deadline-warning-days)))
14093 (defun org-calendar-select-mouse (ev)
14094 "Return to `org-read-date' with the date currently selected.
14095 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
14096 (interactive "e")
14097 (mouse-set-point ev)
14098 (when (calendar-cursor-to-date)
14099 (let* ((date (calendar-cursor-to-date))
14100 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
14101 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
14102 (if (active-minibuffer-window) (exit-minibuffer))))
14104 (defun org-check-deadlines (ndays)
14105 "Check if there are any deadlines due or past due.
14106 A deadline is considered due if it happens within `org-deadline-warning-days'
14107 days from today's date. If the deadline appears in an entry marked DONE,
14108 it is not shown. The prefix arg NDAYS can be used to test that many
14109 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
14110 (interactive "P")
14111 (let* ((org-warn-days
14112 (cond
14113 ((equal ndays '(4)) 100000)
14114 (ndays (prefix-numeric-value ndays))
14115 (t (abs org-deadline-warning-days))))
14116 (case-fold-search nil)
14117 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
14118 (callback
14119 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
14121 (message "%d deadlines past-due or due within %d days"
14122 (org-occur regexp nil callback)
14123 org-warn-days)))
14125 (defun org-check-before-date (date)
14126 "Check if there are deadlines or scheduled entries before DATE."
14127 (interactive (list (org-read-date)))
14128 (let ((case-fold-search nil)
14129 (regexp (concat "\\<\\(" org-deadline-string
14130 "\\|" org-scheduled-string
14131 "\\) *<\\([^>]+\\)>"))
14132 (callback
14133 (lambda () (time-less-p
14134 (org-time-string-to-time (match-string 2))
14135 (org-time-string-to-time date)))))
14136 (message "%d entries before %s"
14137 (org-occur regexp nil callback) date)))
14139 (defun org-check-after-date (date)
14140 "Check if there are deadlines or scheduled entries after DATE."
14141 (interactive (list (org-read-date)))
14142 (let ((case-fold-search nil)
14143 (regexp (concat "\\<\\(" org-deadline-string
14144 "\\|" org-scheduled-string
14145 "\\) *<\\([^>]+\\)>"))
14146 (callback
14147 (lambda () (not
14148 (time-less-p
14149 (org-time-string-to-time (match-string 2))
14150 (org-time-string-to-time date))))))
14151 (message "%d entries after %s"
14152 (org-occur regexp nil callback) date)))
14154 (defun org-evaluate-time-range (&optional to-buffer)
14155 "Evaluate a time range by computing the difference between start and end.
14156 Normally the result is just printed in the echo area, but with prefix arg
14157 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
14158 If the time range is actually in a table, the result is inserted into the
14159 next column.
14160 For time difference computation, a year is assumed to be exactly 365
14161 days in order to avoid rounding problems."
14162 (interactive "P")
14164 (org-clock-update-time-maybe)
14165 (save-excursion
14166 (unless (org-at-date-range-p t)
14167 (goto-char (point-at-bol))
14168 (re-search-forward org-tr-regexp-both (point-at-eol) t))
14169 (if (not (org-at-date-range-p t))
14170 (error "Not at a time-stamp range, and none found in current line")))
14171 (let* ((ts1 (match-string 1))
14172 (ts2 (match-string 2))
14173 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
14174 (match-end (match-end 0))
14175 (time1 (org-time-string-to-time ts1))
14176 (time2 (org-time-string-to-time ts2))
14177 (t1 (org-float-time time1))
14178 (t2 (org-float-time time2))
14179 (diff (abs (- t2 t1)))
14180 (negative (< (- t2 t1) 0))
14181 ;; (ys (floor (* 365 24 60 60)))
14182 (ds (* 24 60 60))
14183 (hs (* 60 60))
14184 (fy "%dy %dd %02d:%02d")
14185 (fy1 "%dy %dd")
14186 (fd "%dd %02d:%02d")
14187 (fd1 "%dd")
14188 (fh "%02d:%02d")
14189 y d h m align)
14190 (if havetime
14191 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
14193 d (floor (/ diff ds)) diff (mod diff ds)
14194 h (floor (/ diff hs)) diff (mod diff hs)
14195 m (floor (/ diff 60)))
14196 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
14198 d (floor (+ (/ diff ds) 0.5))
14199 h 0 m 0))
14200 (if (not to-buffer)
14201 (message "%s" (org-make-tdiff-string y d h m))
14202 (if (org-at-table-p)
14203 (progn
14204 (goto-char match-end)
14205 (setq align t)
14206 (and (looking-at " *|") (goto-char (match-end 0))))
14207 (goto-char match-end))
14208 (if (looking-at
14209 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
14210 (replace-match ""))
14211 (if negative (insert " -"))
14212 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
14213 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
14214 (insert " " (format fh h m))))
14215 (if align (org-table-align))
14216 (message "Time difference inserted")))))
14218 (defun org-make-tdiff-string (y d h m)
14219 (let ((fmt "")
14220 (l nil))
14221 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
14222 l (push y l)))
14223 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
14224 l (push d l)))
14225 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
14226 l (push h l)))
14227 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
14228 l (push m l)))
14229 (apply 'format fmt (nreverse l))))
14231 (defun org-time-string-to-time (s)
14232 (apply 'encode-time (org-parse-time-string s)))
14233 (defun org-time-string-to-seconds (s)
14234 (org-float-time (org-time-string-to-time s)))
14236 (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
14237 "Convert a time stamp to an absolute day number.
14238 If there is a specifyer for a cyclic time stamp, get the closest date to
14239 DAYNR.
14240 PREFER and SHOW-ALL are passed through to `org-closest-date'.
14241 the variable date is bound by the calendar when this is called."
14242 (cond
14243 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
14244 (if (org-diary-sexp-entry (match-string 1 s) "" date)
14245 daynr
14246 (+ daynr 1000)))
14247 ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
14248 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
14249 (time-to-days (current-time))) (match-string 0 s)
14250 prefer show-all))
14251 (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
14253 (defun org-days-to-iso-week (days)
14254 "Return the iso week number."
14255 (require 'cal-iso)
14256 (car (calendar-iso-from-absolute days)))
14258 (defun org-small-year-to-year (year)
14259 "Convert 2-digit years into 4-digit years.
14260 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
14261 The year 2000 cannot be abbreviated. Any year larger than 99
14262 is returned unchanged."
14263 (if (< year 38)
14264 (setq year (+ 2000 year))
14265 (if (< year 100)
14266 (setq year (+ 1900 year))))
14267 year)
14269 (defun org-time-from-absolute (d)
14270 "Return the time corresponding to date D.
14271 D may be an absolute day number, or a calendar-type list (month day year)."
14272 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
14273 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
14275 (defun org-calendar-holiday ()
14276 "List of holidays, for Diary display in Org-mode."
14277 (require 'holidays)
14278 (let ((hl (funcall
14279 (if (fboundp 'calendar-check-holidays)
14280 'calendar-check-holidays 'check-calendar-holidays) date)))
14281 (if hl (mapconcat 'identity hl "; "))))
14283 (defun org-diary-sexp-entry (sexp entry date)
14284 "Process a SEXP diary ENTRY for DATE."
14285 (require 'diary-lib)
14286 (let ((result (if calendar-debug-sexp
14287 (let ((stack-trace-on-error t))
14288 (eval (car (read-from-string sexp))))
14289 (condition-case nil
14290 (eval (car (read-from-string sexp)))
14291 (error
14292 (beep)
14293 (message "Bad sexp at line %d in %s: %s"
14294 (org-current-line)
14295 (buffer-file-name) sexp)
14296 (sleep-for 2))))))
14297 (cond ((stringp result) result)
14298 ((and (consp result)
14299 (stringp (cdr result))) (cdr result))
14300 (result entry)
14301 (t nil))))
14303 (defun org-diary-to-ical-string (frombuf)
14304 "Get iCalendar entries from diary entries in buffer FROMBUF.
14305 This uses the icalendar.el library."
14306 (let* ((tmpdir (if (featurep 'xemacs)
14307 (temp-directory)
14308 temporary-file-directory))
14309 (tmpfile (make-temp-name
14310 (expand-file-name "orgics" tmpdir)))
14311 buf rtn b e)
14312 (with-current-buffer frombuf
14313 (icalendar-export-region (point-min) (point-max) tmpfile)
14314 (setq buf (find-buffer-visiting tmpfile))
14315 (set-buffer buf)
14316 (goto-char (point-min))
14317 (if (re-search-forward "^BEGIN:VEVENT" nil t)
14318 (setq b (match-beginning 0)))
14319 (goto-char (point-max))
14320 (if (re-search-backward "^END:VEVENT" nil t)
14321 (setq e (match-end 0)))
14322 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
14323 (kill-buffer buf)
14324 (delete-file tmpfile)
14325 rtn))
14327 (defun org-closest-date (start current change prefer show-all)
14328 "Find the date closest to CURRENT that is consistent with START and CHANGE.
14329 When PREFER is `past' return a date that is either CURRENT or past.
14330 When PREFER is `future', return a date that is either CURRENT or future.
14331 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
14332 ;; Make the proper lists from the dates
14333 (catch 'exit
14334 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
14335 dn dw sday cday n1 n2 n0
14336 d m y y1 y2 date1 date2 nmonths nm ny m2)
14338 (setq start (org-date-to-gregorian start)
14339 current (org-date-to-gregorian
14340 (if show-all
14341 current
14342 (time-to-days (current-time))))
14343 sday (calendar-absolute-from-gregorian start)
14344 cday (calendar-absolute-from-gregorian current))
14346 (if (<= cday sday) (throw 'exit sday))
14348 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
14349 (setq dn (string-to-number (match-string 1 change))
14350 dw (cdr (assoc (match-string 2 change) a1)))
14351 (error "Invalid change specifyer: %s" change))
14352 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
14353 (cond
14354 ((eq dw 'day)
14355 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
14356 n2 (+ n1 dn)))
14357 ((eq dw 'year)
14358 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
14359 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
14360 (setq date1 (list m d y1)
14361 n1 (calendar-absolute-from-gregorian date1)
14362 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
14363 n2 (calendar-absolute-from-gregorian date2)))
14364 ((eq dw 'month)
14365 ;; approx number of month between the two dates
14366 (setq nmonths (floor (/ (- cday sday) 30.436875)))
14367 ;; How often does dn fit in there?
14368 (setq d (nth 1 start) m (car start) y (nth 2 start)
14369 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
14370 m (+ m nm)
14371 ny (floor (/ m 12))
14372 y (+ y ny)
14373 m (- m (* ny 12)))
14374 (while (> m 12) (setq m (- m 12) y (1+ y)))
14375 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
14376 (setq m2 (+ m dn) y2 y)
14377 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
14378 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
14379 (while (<= n2 cday)
14380 (setq n1 n2 m m2 y y2)
14381 (setq m2 (+ m dn) y2 y)
14382 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
14383 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
14384 ;; Make sure n1 is the earlier date
14385 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
14386 (if show-all
14387 (cond
14388 ((eq prefer 'past) (if (= cday n2) n2 n1))
14389 ((eq prefer 'future) (if (= cday n1) n1 n2))
14390 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
14391 (cond
14392 ((eq prefer 'past) (if (= cday n2) n2 n1))
14393 ((eq prefer 'future) (if (= cday n1) n1 n2))
14394 (t (if (= cday n1) n1 n2)))))))
14396 (defun org-date-to-gregorian (date)
14397 "Turn any specification of DATE into a gregorian date for the calendar."
14398 (cond ((integerp date) (calendar-gregorian-from-absolute date))
14399 ((and (listp date) (= (length date) 3)) date)
14400 ((stringp date)
14401 (setq date (org-parse-time-string date))
14402 (list (nth 4 date) (nth 3 date) (nth 5 date)))
14403 ((listp date)
14404 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
14406 (defun org-parse-time-string (s &optional nodefault)
14407 "Parse the standard Org-mode time string.
14408 This should be a lot faster than the normal `parse-time-string'.
14409 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
14410 hour and minute fields will be nil if not given."
14411 (if (string-match org-ts-regexp0 s)
14412 (list 0
14413 (if (or (match-beginning 8) (not nodefault))
14414 (string-to-number (or (match-string 8 s) "0")))
14415 (if (or (match-beginning 7) (not nodefault))
14416 (string-to-number (or (match-string 7 s) "0")))
14417 (string-to-number (match-string 4 s))
14418 (string-to-number (match-string 3 s))
14419 (string-to-number (match-string 2 s))
14420 nil nil nil)
14421 (error "Not a standard Org-mode time string: %s" s)))
14423 (defun org-timestamp-up (&optional arg)
14424 "Increase the date item at the cursor by one.
14425 If the cursor is on the year, change the year. If it is on the month or
14426 the day, change that.
14427 With prefix ARG, change by that many units."
14428 (interactive "p")
14429 (org-timestamp-change (prefix-numeric-value arg)))
14431 (defun org-timestamp-down (&optional arg)
14432 "Decrease the date item at the cursor by one.
14433 If the cursor is on the year, change the year. If it is on the month or
14434 the day, change that.
14435 With prefix ARG, change by that many units."
14436 (interactive "p")
14437 (org-timestamp-change (- (prefix-numeric-value arg))))
14439 (defun org-timestamp-up-day (&optional arg)
14440 "Increase the date in the time stamp by one day.
14441 With prefix ARG, change that many days."
14442 (interactive "p")
14443 (if (and (not (org-at-timestamp-p t))
14444 (org-on-heading-p))
14445 (org-todo 'up)
14446 (org-timestamp-change (prefix-numeric-value arg) 'day)))
14448 (defun org-timestamp-down-day (&optional arg)
14449 "Decrease the date in the time stamp by one day.
14450 With prefix ARG, change that many days."
14451 (interactive "p")
14452 (if (and (not (org-at-timestamp-p t))
14453 (org-on-heading-p))
14454 (org-todo 'down)
14455 (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
14457 (defun org-at-timestamp-p (&optional inactive-ok)
14458 "Determine if the cursor is in or at a timestamp."
14459 (interactive)
14460 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
14461 (pos (point))
14462 (ans (or (looking-at tsr)
14463 (save-excursion
14464 (skip-chars-backward "^[<\n\r\t")
14465 (if (> (point) (point-min)) (backward-char 1))
14466 (and (looking-at tsr)
14467 (> (- (match-end 0) pos) -1))))))
14468 (and ans
14469 (boundp 'org-ts-what)
14470 (setq org-ts-what
14471 (cond
14472 ((= pos (match-beginning 0)) 'bracket)
14473 ((= pos (1- (match-end 0))) 'bracket)
14474 ((org-pos-in-match-range pos 2) 'year)
14475 ((org-pos-in-match-range pos 3) 'month)
14476 ((org-pos-in-match-range pos 7) 'hour)
14477 ((org-pos-in-match-range pos 8) 'minute)
14478 ((or (org-pos-in-match-range pos 4)
14479 (org-pos-in-match-range pos 5)) 'day)
14480 ((and (> pos (or (match-end 8) (match-end 5)))
14481 (< pos (match-end 0)))
14482 (- pos (or (match-end 8) (match-end 5))))
14483 (t 'day))))
14484 ans))
14486 (defun org-toggle-timestamp-type ()
14487 "Toggle the type (<active> or [inactive]) of a time stamp."
14488 (interactive)
14489 (when (org-at-timestamp-p t)
14490 (let ((beg (match-beginning 0)) (end (match-end 0))
14491 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
14492 (save-excursion
14493 (goto-char beg)
14494 (while (re-search-forward "[][<>]" end t)
14495 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
14496 t t)))
14497 (message "Timestamp is now %sactive"
14498 (if (equal (char-after beg) ?<) "" "in")))))
14500 (defun org-timestamp-change (n &optional what)
14501 "Change the date in the time stamp at point.
14502 The date will be changed by N times WHAT. WHAT can be `day', `month',
14503 `year', `minute', `second'. If WHAT is not given, the cursor position
14504 in the timestamp determines what will be changed."
14505 (let ((pos (point))
14506 with-hm inactive
14507 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
14508 org-ts-what
14509 extra rem
14510 ts time time0)
14511 (if (not (org-at-timestamp-p t))
14512 (error "Not at a timestamp"))
14513 (if (and (not what) (eq org-ts-what 'bracket))
14514 (org-toggle-timestamp-type)
14515 (if (and (not what) (not (eq org-ts-what 'day))
14516 org-display-custom-times
14517 (get-text-property (point) 'display)
14518 (not (get-text-property (1- (point)) 'display)))
14519 (setq org-ts-what 'day))
14520 (setq org-ts-what (or what org-ts-what)
14521 inactive (= (char-after (match-beginning 0)) ?\[)
14522 ts (match-string 0))
14523 (replace-match "")
14524 (if (string-match
14525 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
14527 (setq extra (match-string 1 ts)))
14528 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
14529 (setq with-hm t))
14530 (setq time0 (org-parse-time-string ts))
14531 (when (and (eq org-ts-what 'minute)
14532 (eq current-prefix-arg nil))
14533 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
14534 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
14535 (setcar (cdr time0) (+ (nth 1 time0)
14536 (if (> n 0) (- rem) (- dm rem))))))
14537 (setq time
14538 (encode-time (or (car time0) 0)
14539 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
14540 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
14541 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
14542 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
14543 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
14544 (nthcdr 6 time0)))
14545 (when (and (member org-ts-what '(hour minute))
14546 extra
14547 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
14548 (setq extra (org-modify-ts-extra
14549 extra
14550 (if (eq org-ts-what 'hour) 2 5)
14551 n dm)))
14552 (when (integerp org-ts-what)
14553 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
14554 (if (eq what 'calendar)
14555 (let ((cal-date (org-get-date-from-calendar)))
14556 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
14557 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
14558 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
14559 (setcar time0 (or (car time0) 0))
14560 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
14561 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
14562 (setq time (apply 'encode-time time0))))
14563 (setq org-last-changed-timestamp
14564 (org-insert-time-stamp time with-hm inactive nil nil extra))
14565 (org-clock-update-time-maybe)
14566 (goto-char pos)
14567 ;; Try to recenter the calendar window, if any
14568 (if (and org-calendar-follow-timestamp-change
14569 (get-buffer-window "*Calendar*" t)
14570 (memq org-ts-what '(day month year)))
14571 (org-recenter-calendar (time-to-days time))))))
14573 (defun org-modify-ts-extra (s pos n dm)
14574 "Change the different parts of the lead-time and repeat fields in timestamp."
14575 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
14576 ng h m new rem)
14577 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
14578 (cond
14579 ((or (org-pos-in-match-range pos 2)
14580 (org-pos-in-match-range pos 3))
14581 (setq m (string-to-number (match-string 3 s))
14582 h (string-to-number (match-string 2 s)))
14583 (if (org-pos-in-match-range pos 2)
14584 (setq h (+ h n))
14585 (setq n (* dm (org-no-warnings (signum n))))
14586 (when (not (= 0 (setq rem (% m dm))))
14587 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
14588 (setq m (+ m n)))
14589 (if (< m 0) (setq m (+ m 60) h (1- h)))
14590 (if (> m 59) (setq m (- m 60) h (1+ h)))
14591 (setq h (min 24 (max 0 h)))
14592 (setq ng 1 new (format "-%02d:%02d" h m)))
14593 ((org-pos-in-match-range pos 6)
14594 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
14595 ((org-pos-in-match-range pos 5)
14596 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
14598 ((org-pos-in-match-range pos 9)
14599 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
14600 ((org-pos-in-match-range pos 8)
14601 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
14603 (when ng
14604 (setq s (concat
14605 (substring s 0 (match-beginning ng))
14607 (substring s (match-end ng))))))
14610 (defun org-recenter-calendar (date)
14611 "If the calendar is visible, recenter it to DATE."
14612 (let* ((win (selected-window))
14613 (cwin (get-buffer-window "*Calendar*" t))
14614 (calendar-move-hook nil))
14615 (when cwin
14616 (select-window cwin)
14617 (calendar-goto-date (if (listp date) date
14618 (calendar-gregorian-from-absolute date)))
14619 (select-window win))))
14621 (defun org-goto-calendar (&optional arg)
14622 "Go to the Emacs calendar at the current date.
14623 If there is a time stamp in the current line, go to that date.
14624 A prefix ARG can be used to force the current date."
14625 (interactive "P")
14626 (let ((tsr org-ts-regexp) diff
14627 (calendar-move-hook nil)
14628 (calendar-view-holidays-initially-flag nil)
14629 (calendar-view-diary-initially-flag nil))
14630 (if (or (org-at-timestamp-p)
14631 (save-excursion
14632 (beginning-of-line 1)
14633 (looking-at (concat ".*" tsr))))
14634 (let ((d1 (time-to-days (current-time)))
14635 (d2 (time-to-days
14636 (org-time-string-to-time (match-string 1)))))
14637 (setq diff (- d2 d1))))
14638 (calendar)
14639 (calendar-goto-today)
14640 (if (and diff (not arg)) (calendar-forward-day diff))))
14642 (defun org-get-date-from-calendar ()
14643 "Return a list (month day year) of date at point in calendar."
14644 (with-current-buffer "*Calendar*"
14645 (save-match-data
14646 (calendar-cursor-to-date))))
14648 (defun org-date-from-calendar ()
14649 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
14650 If there is already a time stamp at the cursor position, update it."
14651 (interactive)
14652 (if (org-at-timestamp-p t)
14653 (org-timestamp-change 0 'calendar)
14654 (let ((cal-date (org-get-date-from-calendar)))
14655 (org-insert-time-stamp
14656 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
14658 (defun org-minutes-to-hh:mm-string (m)
14659 "Compute H:MM from a number of minutes."
14660 (let ((h (/ m 60)))
14661 (setq m (- m (* 60 h)))
14662 (format org-time-clocksum-format h m)))
14664 (defun org-hh:mm-string-to-minutes (s)
14665 "Convert a string H:MM to a number of minutes.
14666 If the string is just a number, interpret it as minutes.
14667 In fact, the first hh:mm or number in the string will be taken,
14668 there can be extra stuff in the string.
14669 If no number is found, the return value is 0."
14670 (cond
14671 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
14672 (+ (* (string-to-number (match-string 1 s)) 60)
14673 (string-to-number (match-string 2 s))))
14674 ((string-match "\\([0-9]+\\)" s)
14675 (string-to-number (match-string 1 s)))
14676 (t 0)))
14678 ;;;; Files
14680 (defun org-save-all-org-buffers ()
14681 "Save all Org-mode buffers without user confirmation."
14682 (interactive)
14683 (message "Saving all Org-mode buffers...")
14684 (save-some-buffers t 'org-mode-p)
14685 (when (featurep 'org-id) (org-id-locations-save))
14686 (message "Saving all Org-mode buffers... done"))
14688 (defun org-revert-all-org-buffers ()
14689 "Revert all Org-mode buffers.
14690 Prompt for confirmation when there are unsaved changes.
14691 Be sure you know what you are doing before letting this function
14692 overwrite your changes.
14694 This function is useful in a setup where one tracks org files
14695 with a version control system, to revert on one machine after pulling
14696 changes from another. I believe the procedure must be like this:
14698 1. M-x org-save-all-org-buffers
14699 2. Pull changes from the other machine, resolve conflicts
14700 3. M-x org-revert-all-org-buffers"
14701 (interactive)
14702 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
14703 (error "Abort"))
14704 (save-excursion
14705 (save-window-excursion
14706 (mapc
14707 (lambda (b)
14708 (when (and (with-current-buffer b (org-mode-p))
14709 (with-current-buffer b buffer-file-name))
14710 (switch-to-buffer b)
14711 (revert-buffer t 'no-confirm)))
14712 (buffer-list))
14713 (when (and (featurep 'org-id) org-id-track-globally)
14714 (org-id-locations-load)))))
14716 ;;;; Agenda files
14718 ;;;###autoload
14719 (defun org-iswitchb (&optional arg)
14720 "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
14721 With a prefix argument, restrict available to files.
14722 With two prefix arguments, restrict available buffers to agenda files."
14723 (interactive "P")
14724 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
14725 ((equal arg '(16)) (org-buffer-list 'agenda))
14726 (t (org-buffer-list)))))
14727 (switch-to-buffer
14728 (org-icompleting-read "Org buffer: "
14729 (mapcar 'list (mapcar 'buffer-name blist))
14730 nil t))))
14732 ;;;###autoload
14733 (defalias 'org-ido-switchb 'org-iswitchb)
14735 (defun org-buffer-list (&optional predicate exclude-tmp)
14736 "Return a list of Org buffers.
14737 PREDICATE can be `export', `files' or `agenda'.
14739 export restrict the list to Export buffers.
14740 files restrict the list to buffers visiting Org files.
14741 agenda restrict the list to buffers visiting agenda files.
14743 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
14744 (let* ((bfn nil)
14745 (agenda-files (and (eq predicate 'agenda)
14746 (mapcar 'file-truename (org-agenda-files t))))
14747 (filter
14748 (cond
14749 ((eq predicate 'files)
14750 (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
14751 ((eq predicate 'export)
14752 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
14753 ((eq predicate 'agenda)
14754 (lambda (b)
14755 (with-current-buffer b
14756 (and (eq major-mode 'org-mode)
14757 (setq bfn (buffer-file-name b))
14758 (member (file-truename bfn) agenda-files)))))
14759 (t (lambda (b) (with-current-buffer b
14760 (or (eq major-mode 'org-mode)
14761 (string-match "\*Org .*Export"
14762 (buffer-name b)))))))))
14763 (delq nil
14764 (mapcar
14765 (lambda(b)
14766 (if (and (funcall filter b)
14767 (or (not exclude-tmp)
14768 (not (string-match "tmp" (buffer-name b)))))
14770 nil))
14771 (buffer-list)))))
14773 (defun org-agenda-files (&optional unrestricted archives)
14774 "Get the list of agenda files.
14775 Optional UNRESTRICTED means return the full list even if a restriction
14776 is currently in place.
14777 When ARCHIVES is t, include all archive files that are really being
14778 used by the agenda files. If ARCHIVE is `ifmode', do this only if
14779 `org-agenda-archives-mode' is t."
14780 (let ((files
14781 (cond
14782 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
14783 ((stringp org-agenda-files) (org-read-agenda-file-list))
14784 ((listp org-agenda-files) org-agenda-files)
14785 (t (error "Invalid value of `org-agenda-files'")))))
14786 (setq files (apply 'append
14787 (mapcar (lambda (f)
14788 (if (file-directory-p f)
14789 (directory-files
14790 f t org-agenda-file-regexp)
14791 (list f)))
14792 files)))
14793 (when org-agenda-skip-unavailable-files
14794 (setq files (delq nil
14795 (mapcar (function
14796 (lambda (file)
14797 (and (file-readable-p file) file)))
14798 files))))
14799 (when (or (eq archives t)
14800 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
14801 (setq files (org-add-archive-files files)))
14802 files))
14804 (defun org-edit-agenda-file-list ()
14805 "Edit the list of agenda files.
14806 Depending on setup, this either uses customize to edit the variable
14807 `org-agenda-files', or it visits the file that is holding the list. In the
14808 latter case, the buffer is set up in a way that saving it automatically kills
14809 the buffer and restores the previous window configuration."
14810 (interactive)
14811 (if (stringp org-agenda-files)
14812 (let ((cw (current-window-configuration)))
14813 (find-file org-agenda-files)
14814 (org-set-local 'org-window-configuration cw)
14815 (org-add-hook 'after-save-hook
14816 (lambda ()
14817 (set-window-configuration
14818 (prog1 org-window-configuration
14819 (kill-buffer (current-buffer))))
14820 (org-install-agenda-files-menu)
14821 (message "New agenda file list installed"))
14822 nil 'local)
14823 (message "%s" (substitute-command-keys
14824 "Edit list and finish with \\[save-buffer]")))
14825 (customize-variable 'org-agenda-files)))
14827 (defun org-store-new-agenda-file-list (list)
14828 "Set new value for the agenda file list and save it correctly."
14829 (if (stringp org-agenda-files)
14830 (let ((fe (org-read-agenda-file-list t)) b u)
14831 (while (setq b (find-buffer-visiting org-agenda-files))
14832 (kill-buffer b))
14833 (with-temp-file org-agenda-files
14834 (insert
14835 (mapconcat
14836 (lambda (f) ;; Keep un-expanded entries.
14837 (if (setq u (assoc f fe))
14838 (cdr u)
14840 list "\n")
14841 "\n")))
14842 (let ((org-mode-hook nil) (org-inhibit-startup t)
14843 (org-insert-mode-line-in-empty-file nil))
14844 (setq org-agenda-files list)
14845 (customize-save-variable 'org-agenda-files org-agenda-files))))
14847 (defun org-read-agenda-file-list (&optional pair-with-expansion)
14848 "Read the list of agenda files from a file.
14849 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
14850 filenames, used by `org-store-new-agenda-file-list' to write back
14851 un-expanded file names."
14852 (when (file-directory-p org-agenda-files)
14853 (error "`org-agenda-files' cannot be a single directory"))
14854 (when (stringp org-agenda-files)
14855 (with-temp-buffer
14856 (insert-file-contents org-agenda-files)
14857 (mapcar
14858 (lambda (f)
14859 (let ((e (expand-file-name (substitute-in-file-name f)
14860 org-directory)))
14861 (if pair-with-expansion
14862 (cons e f)
14863 e)))
14864 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
14866 ;;;###autoload
14867 (defun org-cycle-agenda-files ()
14868 "Cycle through the files in `org-agenda-files'.
14869 If the current buffer visits an agenda file, find the next one in the list.
14870 If the current buffer does not, find the first agenda file."
14871 (interactive)
14872 (let* ((fs (org-agenda-files t))
14873 (files (append fs (list (car fs))))
14874 (tcf (if buffer-file-name (file-truename buffer-file-name)))
14875 file)
14876 (unless files (error "No agenda files"))
14877 (catch 'exit
14878 (while (setq file (pop files))
14879 (if (equal (file-truename file) tcf)
14880 (when (car files)
14881 (find-file (car files))
14882 (throw 'exit t))))
14883 (find-file (car fs)))
14884 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
14886 (defun org-agenda-file-to-front (&optional to-end)
14887 "Move/add the current file to the top of the agenda file list.
14888 If the file is not present in the list, it is added to the front. If it is
14889 present, it is moved there. With optional argument TO-END, add/move to the
14890 end of the list."
14891 (interactive "P")
14892 (let ((org-agenda-skip-unavailable-files nil)
14893 (file-alist (mapcar (lambda (x)
14894 (cons (file-truename x) x))
14895 (org-agenda-files t)))
14896 (ctf (file-truename buffer-file-name))
14897 x had)
14898 (setq x (assoc ctf file-alist) had x)
14900 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
14901 (if to-end
14902 (setq file-alist (append (delq x file-alist) (list x)))
14903 (setq file-alist (cons x (delq x file-alist))))
14904 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
14905 (org-install-agenda-files-menu)
14906 (message "File %s to %s of agenda file list"
14907 (if had "moved" "added") (if to-end "end" "front"))))
14909 (defun org-remove-file (&optional file)
14910 "Remove current file from the list of files in variable `org-agenda-files'.
14911 These are the files which are being checked for agenda entries.
14912 Optional argument FILE means use this file instead of the current."
14913 (interactive)
14914 (let* ((org-agenda-skip-unavailable-files nil)
14915 (file (or file buffer-file-name))
14916 (true-file (file-truename file))
14917 (afile (abbreviate-file-name file))
14918 (files (delq nil (mapcar
14919 (lambda (x)
14920 (if (equal true-file
14921 (file-truename x))
14922 nil x))
14923 (org-agenda-files t)))))
14924 (if (not (= (length files) (length (org-agenda-files t))))
14925 (progn
14926 (org-store-new-agenda-file-list files)
14927 (org-install-agenda-files-menu)
14928 (message "Removed file: %s" afile))
14929 (message "File was not in list: %s (not removed)" afile))))
14931 (defun org-file-menu-entry (file)
14932 (vector file (list 'find-file file) t))
14934 (defun org-check-agenda-file (file)
14935 "Make sure FILE exists. If not, ask user what to do."
14936 (when (not (file-exists-p file))
14937 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
14938 (abbreviate-file-name file))
14939 (let ((r (downcase (read-char-exclusive))))
14940 (cond
14941 ((equal r ?r)
14942 (org-remove-file file)
14943 (throw 'nextfile t))
14944 (t (error "Abort"))))))
14946 (defun org-get-agenda-file-buffer (file)
14947 "Get a buffer visiting FILE. If the buffer needs to be created, add
14948 it to the list of buffers which might be released later."
14949 (let ((buf (org-find-base-buffer-visiting file)))
14950 (if buf
14951 buf ; just return it
14952 ;; Make a new buffer and remember it
14953 (setq buf (find-file-noselect file))
14954 (if buf (push buf org-agenda-new-buffers))
14955 buf)))
14957 (defun org-release-buffers (blist)
14958 "Release all buffers in list, asking the user for confirmation when needed.
14959 When a buffer is unmodified, it is just killed. When modified, it is saved
14960 \(if the user agrees) and then killed."
14961 (let (buf file)
14962 (while (setq buf (pop blist))
14963 (setq file (buffer-file-name buf))
14964 (when (and (buffer-modified-p buf)
14965 file
14966 (y-or-n-p (format "Save file %s? " file)))
14967 (with-current-buffer buf (save-buffer)))
14968 (kill-buffer buf))))
14970 (defun org-prepare-agenda-buffers (files)
14971 "Create buffers for all agenda files, protect archived trees and comments."
14972 (interactive)
14973 (let ((pa '(:org-archived t))
14974 (pc '(:org-comment t))
14975 (pall '(:org-archived t :org-comment t))
14976 (inhibit-read-only t)
14977 (rea (concat ":" org-archive-tag ":"))
14978 bmp file re)
14979 (save-excursion
14980 (save-restriction
14981 (while (setq file (pop files))
14982 (catch 'nextfile
14983 (if (bufferp file)
14984 (set-buffer file)
14985 (org-check-agenda-file file)
14986 (set-buffer (org-get-agenda-file-buffer file)))
14987 (widen)
14988 (setq bmp (buffer-modified-p))
14989 (org-refresh-category-properties)
14990 (setq org-todo-keywords-for-agenda
14991 (append org-todo-keywords-for-agenda org-todo-keywords-1))
14992 (setq org-done-keywords-for-agenda
14993 (append org-done-keywords-for-agenda org-done-keywords))
14994 (setq org-todo-keyword-alist-for-agenda
14995 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
14996 (setq org-drawers-for-agenda
14997 (append org-drawers-for-agenda org-drawers))
14998 (setq org-tag-alist-for-agenda
14999 (append org-tag-alist-for-agenda org-tag-alist))
15001 (save-excursion
15002 (remove-text-properties (point-min) (point-max) pall)
15003 (when org-agenda-skip-archived-trees
15004 (goto-char (point-min))
15005 (while (re-search-forward rea nil t)
15006 (if (org-on-heading-p t)
15007 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
15008 (goto-char (point-min))
15009 (setq re (concat "^\\*+ +" org-comment-string "\\>"))
15010 (while (re-search-forward re nil t)
15011 (add-text-properties
15012 (match-beginning 0) (org-end-of-subtree t) pc)))
15013 (set-buffer-modified-p bmp)))))
15014 (setq org-todo-keywords-for-agenda
15015 (org-uniquify org-todo-keywords-for-agenda))
15016 (setq org-todo-keyword-alist-for-agenda
15017 (org-uniquify org-todo-keyword-alist-for-agenda)
15018 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
15020 ;;;; Embedded LaTeX
15022 (defvar org-cdlatex-mode-map (make-sparse-keymap)
15023 "Keymap for the minor `org-cdlatex-mode'.")
15025 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
15026 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
15027 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
15028 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
15029 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
15031 (defvar org-cdlatex-texmathp-advice-is-done nil
15032 "Flag remembering if we have applied the advice to texmathp already.")
15034 (define-minor-mode org-cdlatex-mode
15035 "Toggle the minor `org-cdlatex-mode'.
15036 This mode supports entering LaTeX environment and math in LaTeX fragments
15037 in Org-mode.
15038 \\{org-cdlatex-mode-map}"
15039 nil " OCDL" nil
15040 (when org-cdlatex-mode (require 'cdlatex))
15041 (unless org-cdlatex-texmathp-advice-is-done
15042 (setq org-cdlatex-texmathp-advice-is-done t)
15043 (defadvice texmathp (around org-math-always-on activate)
15044 "Always return t in org-mode buffers.
15045 This is because we want to insert math symbols without dollars even outside
15046 the LaTeX math segments. If Orgmode thinks that point is actually inside
15047 an embedded LaTeX fragment, let texmathp do its job.
15048 \\[org-cdlatex-mode-map]"
15049 (interactive)
15050 (let (p)
15051 (cond
15052 ((not (org-mode-p)) ad-do-it)
15053 ((eq this-command 'cdlatex-math-symbol)
15054 (setq ad-return-value t
15055 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
15057 (let ((p (org-inside-LaTeX-fragment-p)))
15058 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
15059 (setq ad-return-value t
15060 texmathp-why '("Org-mode embedded math" . 0))
15061 (if p ad-do-it)))))))))
15063 (defun turn-on-org-cdlatex ()
15064 "Unconditionally turn on `org-cdlatex-mode'."
15065 (org-cdlatex-mode 1))
15067 (defun org-inside-LaTeX-fragment-p ()
15068 "Test if point is inside a LaTeX fragment.
15069 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
15070 sequence appearing also before point.
15071 Even though the matchers for math are configurable, this function assumes
15072 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
15073 delimiters are skipped when they have been removed by customization.
15074 The return value is nil, or a cons cell with the delimiter and
15075 and the position of this delimiter.
15077 This function does a reasonably good job, but can locally be fooled by
15078 for example currency specifications. For example it will assume being in
15079 inline math after \"$22.34\". The LaTeX fragment formatter will only format
15080 fragments that are properly closed, but during editing, we have to live
15081 with the uncertainty caused by missing closing delimiters. This function
15082 looks only before point, not after."
15083 (catch 'exit
15084 (let ((pos (point))
15085 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
15086 (lim (progn
15087 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
15088 (point)))
15089 dd-on str (start 0) m re)
15090 (goto-char pos)
15091 (when dodollar
15092 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
15093 re (nth 1 (assoc "$" org-latex-regexps)))
15094 (while (string-match re str start)
15095 (cond
15096 ((= (match-end 0) (length str))
15097 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
15098 ((= (match-end 0) (- (length str) 5))
15099 (throw 'exit nil))
15100 (t (setq start (match-end 0))))))
15101 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
15102 (goto-char pos)
15103 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
15104 (and (match-beginning 2) (throw 'exit nil))
15105 ;; count $$
15106 (while (re-search-backward "\\$\\$" lim t)
15107 (setq dd-on (not dd-on)))
15108 (goto-char pos)
15109 (if dd-on (cons "$$" m))))))
15111 (defun org-inside-latex-macro-p ()
15112 "Is point inside a LaTeX macro or its arguments?"
15113 (save-match-data
15114 (org-in-regexp
15115 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
15117 (defun test ()
15118 (interactive)
15119 (message "%s" (org-inside-latex-macro-p)))
15121 (defun org-try-cdlatex-tab ()
15122 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
15123 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
15124 - inside a LaTeX fragment, or
15125 - after the first word in a line, where an abbreviation expansion could
15126 insert a LaTeX environment."
15127 (when org-cdlatex-mode
15128 (cond
15129 ((save-excursion
15130 (skip-chars-backward "a-zA-Z0-9*")
15131 (skip-chars-backward " \t")
15132 (bolp))
15133 (cdlatex-tab) t)
15134 ((org-inside-LaTeX-fragment-p)
15135 (cdlatex-tab) t)
15136 (t nil))))
15138 (defun org-cdlatex-underscore-caret (&optional arg)
15139 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
15140 Revert to the normal definition outside of these fragments."
15141 (interactive "P")
15142 (if (org-inside-LaTeX-fragment-p)
15143 (call-interactively 'cdlatex-sub-superscript)
15144 (let (org-cdlatex-mode)
15145 (call-interactively (key-binding (vector last-input-event))))))
15147 (defun org-cdlatex-math-modify (&optional arg)
15148 "Execute `cdlatex-math-modify' in LaTeX fragments.
15149 Revert to the normal definition outside of these fragments."
15150 (interactive "P")
15151 (if (org-inside-LaTeX-fragment-p)
15152 (call-interactively 'cdlatex-math-modify)
15153 (let (org-cdlatex-mode)
15154 (call-interactively (key-binding (vector last-input-event))))))
15156 (defvar org-latex-fragment-image-overlays nil
15157 "List of overlays carrying the images of latex fragments.")
15158 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
15160 (defun org-remove-latex-fragment-image-overlays ()
15161 "Remove all overlays with LaTeX fragment images in current buffer."
15162 (mapc 'delete-overlay org-latex-fragment-image-overlays)
15163 (setq org-latex-fragment-image-overlays nil))
15165 (defun org-preview-latex-fragment (&optional subtree)
15166 "Preview the LaTeX fragment at point, or all locally or globally.
15167 If the cursor is in a LaTeX fragment, create the image and overlay
15168 it over the source code. If there is no fragment at point, display
15169 all fragments in the current text, from one headline to the next. With
15170 prefix SUBTREE, display all fragments in the current subtree. With a
15171 double prefix `C-u C-u', or when the cursor is before the first headline,
15172 display all fragments in the buffer.
15173 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
15174 (interactive "P")
15175 (org-remove-latex-fragment-image-overlays)
15176 (save-excursion
15177 (save-restriction
15178 (let (beg end at msg)
15179 (cond
15180 ((or (equal subtree '(16))
15181 (not (save-excursion
15182 (re-search-backward (concat "^" outline-regexp) nil t))))
15183 (setq beg (point-min) end (point-max)
15184 msg "Creating images for buffer...%s"))
15185 ((equal subtree '(4))
15186 (org-back-to-heading)
15187 (setq beg (point) end (org-end-of-subtree t)
15188 msg "Creating images for subtree...%s"))
15190 (if (setq at (org-inside-LaTeX-fragment-p))
15191 (goto-char (max (point-min) (- (cdr at) 2)))
15192 (org-back-to-heading))
15193 (setq beg (point) end (progn (outline-next-heading) (point))
15194 msg (if at "Creating image...%s"
15195 "Creating images for entry...%s"))))
15196 (message msg "")
15197 (narrow-to-region beg end)
15198 (goto-char beg)
15199 (org-format-latex
15200 (concat "ltxpng/" (file-name-sans-extension
15201 (file-name-nondirectory
15202 buffer-file-name)))
15203 default-directory 'overlays msg at 'forbuffer)
15204 (message msg "done. Use `C-c C-c' to remove images.")))))
15206 (defvar org-latex-regexps
15207 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
15208 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
15209 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
15210 ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
15211 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
15212 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
15213 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
15214 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
15215 "Regular expressions for matching embedded LaTeX.")
15217 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
15218 "Replace LaTeX fragments with links to an image, and produce images.
15219 Some of the options can be changed using the variable
15220 `org-format-latex-options'."
15221 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
15222 (let* ((prefixnodir (file-name-nondirectory prefix))
15223 (absprefix (expand-file-name prefix dir))
15224 (todir (file-name-directory absprefix))
15225 (opt org-format-latex-options)
15226 (matchers (plist-get opt :matchers))
15227 (re-list org-latex-regexps)
15228 (org-format-latex-header-extra
15229 (plist-get (org-infile-export-plist) :latex-header-extra))
15230 (cnt 0) txt hash link beg end re e checkdir
15231 executables-checked
15232 m n block linkfile movefile ov)
15233 ;; Check the different regular expressions
15234 (while (setq e (pop re-list))
15235 (setq m (car e) re (nth 1 e) n (nth 2 e)
15236 block (if (nth 3 e) "\n\n" ""))
15237 (when (member m matchers)
15238 (goto-char (point-min))
15239 (while (re-search-forward re nil t)
15240 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
15241 (not (get-text-property (match-beginning n)
15242 'org-protected))
15243 (or (not overlays)
15244 (not (eq (get-char-property (match-beginning n)
15245 'org-overlay-type)
15246 'org-latex-overlay))))
15247 (setq txt (match-string n)
15248 beg (match-beginning n) end (match-end n)
15249 cnt (1+ cnt))
15250 (let (print-length print-level) ; make sure full list is printed
15251 (setq hash (sha1 (prin1-to-string
15252 (list org-format-latex-header
15253 org-format-latex-header-extra
15254 org-export-latex-default-packages-alist
15255 org-export-latex-packages-alist
15256 org-format-latex-options
15257 forbuffer txt)))
15258 linkfile (format "%s_%s.png" prefix hash)
15259 movefile (format "%s_%s.png" absprefix hash)))
15260 (setq link (concat block "[[file:" linkfile "]]" block))
15261 (if msg (message msg cnt))
15262 (goto-char beg)
15263 (unless checkdir ; make sure the directory exists
15264 (setq checkdir t)
15265 (or (file-directory-p todir) (make-directory todir)))
15267 (unless executables-checked
15268 (org-check-external-command
15269 "latex" "needed to convert LaTeX fragments to images")
15270 (org-check-external-command
15271 "dvipng" "needed to convert LaTeX fragments to images")
15272 (setq executables-checked t))
15274 (unless (file-exists-p movefile)
15275 (org-create-formula-image
15276 txt movefile opt forbuffer))
15277 (if overlays
15278 (progn
15279 (mapc (lambda (o)
15280 (if (eq (overlay-get o 'org-overlay-type)
15281 'org-latex-overlay)
15282 (delete-overlay o)))
15283 (overlays-in beg end))
15284 (setq ov (make-overlay beg end))
15285 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
15286 (if (featurep 'xemacs)
15287 (progn
15288 (overlay-put ov 'invisible t)
15289 (overlay-put
15290 ov 'end-glyph
15291 (make-glyph (vector 'png :file movefile))))
15292 (overlay-put
15293 ov 'display
15294 (list 'image :type 'png :file movefile :ascent 'center)))
15295 (push ov org-latex-fragment-image-overlays)
15296 (goto-char end))
15297 (delete-region beg end)
15298 (insert (org-add-props link
15299 (list 'org-latex-src
15300 (replace-regexp-in-string "\"" "" txt)))))))))))
15302 ;; This function borrows from Ganesh Swami's latex2png.el
15303 (defun org-create-formula-image (string tofile options buffer)
15304 "This calls dvipng."
15305 (require 'org-latex)
15306 (let* ((tmpdir (if (featurep 'xemacs)
15307 (temp-directory)
15308 temporary-file-directory))
15309 (texfilebase (make-temp-name
15310 (expand-file-name "orgtex" tmpdir)))
15311 (texfile (concat texfilebase ".tex"))
15312 (dvifile (concat texfilebase ".dvi"))
15313 (pngfile (concat texfilebase ".png"))
15314 (fnh (if (featurep 'xemacs)
15315 (font-height (get-face-font 'default))
15316 (face-attribute 'default :height nil)))
15317 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
15318 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
15319 (fg (or (plist-get options (if buffer :foreground :html-foreground))
15320 "Black"))
15321 (bg (or (plist-get options (if buffer :background :html-background))
15322 "Transparent")))
15323 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
15324 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
15325 (with-temp-file texfile
15326 (insert (org-splice-latex-header
15327 org-format-latex-header
15328 org-export-latex-default-packages-alist
15329 org-export-latex-packages-alist
15330 org-format-latex-header-extra))
15331 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
15332 (require 'org-latex)
15333 (org-export-latex-fix-inputenc))
15334 (let ((dir default-directory))
15335 (condition-case nil
15336 (progn
15337 (cd tmpdir)
15338 (call-process "latex" nil nil nil texfile))
15339 (error nil))
15340 (cd dir))
15341 (if (not (file-exists-p dvifile))
15342 (progn (message "Failed to create dvi file from %s" texfile) nil)
15343 (condition-case nil
15344 (call-process "dvipng" nil nil nil
15345 "-fg" fg "-bg" bg
15346 "-D" dpi
15347 ;;"-x" scale "-y" scale
15348 "-T" "tight"
15349 "-o" pngfile
15350 dvifile)
15351 (error nil))
15352 (if (not (file-exists-p pngfile))
15353 (if org-format-latex-signal-error
15354 (error "Failed to create png file from %s" texfile)
15355 (message "Failed to create png file from %s" texfile)
15356 nil)
15357 ;; Use the requested file name and clean up
15358 (copy-file pngfile tofile 'replace)
15359 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
15360 (delete-file (concat texfilebase e)))
15361 pngfile))))
15363 (defun org-splice-latex-header (tpl def-pkg pkg &optional extra)
15364 "Fill a LaTeX header template TPL.
15365 In the template, the following place holders will be recognized:
15367 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
15368 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
15369 [PACKAGES] \\usepackage statements for PKG
15370 [NO-PACKAGES] do not include PKG
15371 [EXTRA] the string EXTRA
15372 [NO-EXTRA] do not include EXTRA
15374 For backward compatibility, if both the positive and the negative place
15375 holder is missing, the positive one (without the \"NO-\") will be
15376 assumed to be present at the end of the template.
15377 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
15378 EXTRA is a string."
15379 (let (rpl (end ""))
15380 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
15381 (setq rpl (if (or (match-end 1) (not def-pkg))
15382 "" (org-latex-packages-to-string def-pkg t))
15383 tpl (replace-match rpl t t tpl))
15384 (if def-pkg (setq end (org-latex-packages-to-string def-pkg))))
15386 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
15387 (setq rpl (if (or (match-end 1) (not pkg))
15388 "" (org-latex-packages-to-string pkg t))
15389 tpl (replace-match rpl t t tpl))
15390 (if pkg (setq end (concat end "\n" (org-latex-packages-to-string pkg)))))
15392 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
15393 (setq rpl (if (or (match-end 1) (not extra))
15394 "" (concat extra "\n"))
15395 tpl (replace-match rpl t t tpl))
15396 (if (and extra (string-match "\\S-" extra))
15397 (setq end (concat end "\n" extra))))
15399 (if (string-match "\\S-" end)
15400 (concat tpl "\n" end)
15401 tpl)))
15403 (defun org-latex-packages-to-string (pkg &optional newline)
15404 "Turn an alist of packages into a string with the \\usepackage macros."
15405 (setq pkg (mapconcat (lambda(p)
15406 (cond
15407 ((stringp p) p)
15408 ((equal "" (car p))
15409 (format "\\usepackage{%s}" (cadr p)))
15411 (format "\\usepackage[%s]{%s}"
15412 (car p) (cadr p)))))
15414 "\n"))
15415 (if newline (concat pkg "\n") pkg))
15417 (defun org-dvipng-color (attr)
15418 "Return an rgb color specification for dvipng."
15419 (apply 'format "rgb %s %s %s"
15420 (mapcar 'org-normalize-color
15421 (color-values (face-attribute 'default attr nil)))))
15423 (defun org-normalize-color (value)
15424 "Return string to be used as color value for an RGB component."
15425 (format "%g" (/ value 65535.0)))
15427 ;;;; Key bindings
15429 ;; Make `C-c C-x' a prefix key
15430 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
15432 ;; TAB key with modifiers
15433 (org-defkey org-mode-map "\C-i" 'org-cycle)
15434 (org-defkey org-mode-map [(tab)] 'org-cycle)
15435 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
15436 (org-defkey org-mode-map [(meta tab)] 'org-complete)
15437 (org-defkey org-mode-map "\M-\t" 'org-complete)
15438 (org-defkey org-mode-map "\M-\C-i" 'org-complete)
15439 ;; The following line is necessary under Suse GNU/Linux
15440 (unless (featurep 'xemacs)
15441 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
15442 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
15443 (define-key org-mode-map [backtab] 'org-shifttab)
15445 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
15446 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
15447 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
15449 ;; Cursor keys with modifiers
15450 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
15451 (org-defkey org-mode-map [(meta right)] 'org-metaright)
15452 (org-defkey org-mode-map [(meta up)] 'org-metaup)
15453 (org-defkey org-mode-map [(meta down)] 'org-metadown)
15455 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
15456 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
15457 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
15458 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
15460 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
15461 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
15462 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
15463 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
15465 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
15466 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
15468 ;;; Extra keys for tty access.
15469 ;; We only set them when really needed because otherwise the
15470 ;; menus don't show the simple keys
15472 (when (or org-use-extra-keys
15473 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
15474 (not window-system))
15475 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
15476 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
15477 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
15478 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
15479 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
15480 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
15481 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
15482 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
15483 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
15484 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
15485 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
15486 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
15487 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
15488 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
15489 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
15490 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
15491 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
15492 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
15493 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
15494 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
15495 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
15496 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
15497 (org-defkey org-mode-map [?\e (tab)] 'org-complete)
15498 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
15499 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
15500 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
15501 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
15502 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
15504 ;; All the other keys
15506 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
15507 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
15508 (if (boundp 'narrow-map)
15509 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
15510 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
15511 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
15512 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
15513 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
15514 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
15515 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
15516 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
15517 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
15518 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
15519 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
15520 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
15521 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
15522 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
15523 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
15524 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
15525 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
15526 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
15527 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
15528 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
15529 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
15530 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
15531 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
15532 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
15533 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
15534 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
15535 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
15536 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
15537 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
15538 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
15539 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
15540 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
15541 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
15542 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
15543 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
15544 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
15545 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
15546 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
15547 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
15548 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
15549 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
15550 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
15551 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
15552 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
15553 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
15554 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
15555 (org-defkey org-mode-map "\C-c^" 'org-sort)
15556 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
15557 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
15558 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
15559 (org-defkey org-mode-map "\C-m" 'org-return)
15560 (org-defkey org-mode-map "\C-j" 'org-return-indent)
15561 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
15562 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
15563 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
15564 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
15565 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
15566 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
15567 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
15568 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
15569 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
15570 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
15571 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
15572 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
15573 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
15574 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
15575 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
15576 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
15577 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
15578 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
15579 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
15580 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
15582 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
15583 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
15584 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
15585 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
15587 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
15588 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
15589 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
15590 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
15591 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
15592 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
15593 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
15594 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
15595 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
15596 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
15597 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
15598 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
15599 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
15600 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
15601 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
15603 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
15604 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
15605 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
15606 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
15608 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
15610 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
15612 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
15613 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
15615 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
15618 (when (featurep 'xemacs)
15619 (org-defkey org-mode-map 'button3 'popup-mode-menu))
15622 (defconst org-speed-commands-default
15624 ("Outline Navigation")
15625 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
15626 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
15627 ("f" . (org-speed-move-safe 'org-forward-same-level))
15628 ("b" . (org-speed-move-safe 'org-backward-same-level))
15629 ("u" . (org-speed-move-safe 'outline-up-heading))
15630 ("j" . org-goto)
15631 ("g" . (org-refile t))
15632 ("Outline Visibility")
15633 ("c" . org-cycle)
15634 ("C" . org-shifttab)
15635 (" " . org-display-outline-path)
15636 ("Outline Structure Editing")
15637 ("U" . org-shiftmetaup)
15638 ("D" . org-shiftmetadown)
15639 ("r" . org-metaright)
15640 ("l" . org-metaleft)
15641 ("R" . org-shiftmetaright)
15642 ("L" . org-shiftmetaleft)
15643 ("i" . (progn (forward-char 1) (call-interactively
15644 'org-insert-heading-respect-content)))
15645 ("^" . org-sort)
15646 ("w" . org-refile)
15647 ("a" . org-archive-subtree-default-with-confirmation)
15648 ("." . outline-mark-subtree)
15649 ("Clock Commands")
15650 ("I" . org-clock-in)
15651 ("O" . org-clock-out)
15652 ("Meta Data Editing")
15653 ("t" . org-todo)
15654 ("0" . (org-priority ?\ ))
15655 ("1" . (org-priority ?A))
15656 ("2" . (org-priority ?B))
15657 ("3" . (org-priority ?C))
15658 (";" . org-set-tags-command)
15659 ("e" . org-set-effort)
15660 ("Agenda Views etc")
15661 ("v" . org-agenda)
15662 ("/" . org-sparse-tree)
15663 ("Misc")
15664 ("o" . org-open-at-point)
15665 ("?" . org-speed-command-help)
15667 "The default speed commands.")
15669 (defun org-print-speed-command (e)
15670 (if (> (length (car e)) 1)
15671 (progn
15672 (princ "\n")
15673 (princ (car e))
15674 (princ "\n")
15675 (princ (make-string (length (car e)) ?-))
15676 (princ "\n"))
15677 (princ (car e))
15678 (princ " ")
15679 (if (symbolp (cdr e))
15680 (princ (symbol-name (cdr e)))
15681 (prin1 (cdr e)))
15682 (princ "\n")))
15684 (defun org-speed-command-help ()
15685 "Show the available speed commands."
15686 (interactive)
15687 (if (not org-use-speed-commands)
15688 (error "Speed commands are not activated, customize `org-use-speed-commands'.")
15689 (with-output-to-temp-buffer "*Help*"
15690 (princ "User-defined Speed commands\n===========================\n")
15691 (mapc 'org-print-speed-command org-speed-commands-user)
15692 (princ "\n")
15693 (princ "Built-in Speed commands\n=======================\n")
15694 (mapc 'org-print-speed-command org-speed-commands-default))
15695 (with-current-buffer "*Help*"
15696 (setq truncate-lines t))))
15698 (defun org-speed-move-safe (cmd)
15699 "Execute CMD, but make sure that the cursor always ends up in a headline.
15700 If not, return to the original position and throw an error."
15701 (interactive)
15702 (let ((pos (point)))
15703 (call-interactively cmd)
15704 (unless (and (bolp) (org-on-heading-p))
15705 (goto-char pos)
15706 (error "Boundary reached while executing %s" cmd))))
15708 (defvar org-self-insert-command-undo-counter 0)
15710 (defvar org-table-auto-blank-field) ; defined in org-table.el
15711 (defvar org-speed-command nil)
15712 (defun org-self-insert-command (N)
15713 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
15714 If the cursor is in a table looking at whitespace, the whitespace is
15715 overwritten, and the table is not marked as requiring realignment."
15716 (interactive "p")
15717 (cond
15718 ((and org-use-speed-commands
15719 (or (and (bolp) (looking-at outline-regexp))
15720 (and (functionp org-use-speed-commands)
15721 (funcall org-use-speed-commands)))
15722 (setq
15723 org-speed-command
15724 (or (cdr (assoc (this-command-keys) org-speed-commands-user))
15725 (cdr (assoc (this-command-keys) org-speed-commands-default)))))
15726 (cond
15727 ((commandp org-speed-command)
15728 (setq this-command org-speed-command)
15729 (call-interactively org-speed-command))
15730 ((functionp org-speed-command)
15731 (funcall org-speed-command))
15732 ((and org-speed-command (listp org-speed-command))
15733 (eval org-speed-command))
15734 (t (let (org-use-speed-commands)
15735 (call-interactively 'org-self-insert-command)))))
15736 ((and
15737 (org-table-p)
15738 (progn
15739 ;; check if we blank the field, and if that triggers align
15740 (and (featurep 'org-table) org-table-auto-blank-field
15741 (member last-command
15742 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
15743 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
15744 ;; got extra space, this field does not determine column width
15745 (let (org-table-may-need-update) (org-table-blank-field))
15746 ;; no extra space, this field may determine column width
15747 (org-table-blank-field)))
15749 (eq N 1)
15750 (looking-at "[^|\n]* |"))
15751 (let (org-table-may-need-update)
15752 (goto-char (1- (match-end 0)))
15753 (delete-backward-char 1)
15754 (goto-char (match-beginning 0))
15755 (self-insert-command N)))
15757 (setq org-table-may-need-update t)
15758 (self-insert-command N)
15759 (org-fix-tags-on-the-fly)
15760 (if org-self-insert-cluster-for-undo
15761 (if (not (eq last-command 'org-self-insert-command))
15762 (setq org-self-insert-command-undo-counter 1)
15763 (if (>= org-self-insert-command-undo-counter 20)
15764 (setq org-self-insert-command-undo-counter 1)
15765 (and (> org-self-insert-command-undo-counter 0)
15766 buffer-undo-list
15767 (not (cadr buffer-undo-list)) ; remove nil entry
15768 (setcdr buffer-undo-list (cddr buffer-undo-list)))
15769 (setq org-self-insert-command-undo-counter
15770 (1+ org-self-insert-command-undo-counter))))))))
15772 (defun org-fix-tags-on-the-fly ()
15773 (when (and (equal (char-after (point-at-bol)) ?*)
15774 (org-on-heading-p))
15775 (org-align-tags-here org-tags-column)))
15777 (defun org-delete-backward-char (N)
15778 "Like `delete-backward-char', insert whitespace at field end in tables.
15779 When deleting backwards, in tables this function will insert whitespace in
15780 front of the next \"|\" separator, to keep the table aligned. The table will
15781 still be marked for re-alignment if the field did fill the entire column,
15782 because, in this case the deletion might narrow the column."
15783 (interactive "p")
15784 (if (and (org-table-p)
15785 (eq N 1)
15786 (string-match "|" (buffer-substring (point-at-bol) (point)))
15787 (looking-at ".*?|"))
15788 (let ((pos (point))
15789 (noalign (looking-at "[^|\n\r]* |"))
15790 (c org-table-may-need-update))
15791 (backward-delete-char N)
15792 (skip-chars-forward "^|")
15793 (insert " ")
15794 (goto-char (1- pos))
15795 ;; noalign: if there were two spaces at the end, this field
15796 ;; does not determine the width of the column.
15797 (if noalign (setq org-table-may-need-update c)))
15798 (backward-delete-char N)
15799 (org-fix-tags-on-the-fly)))
15801 (defun org-delete-char (N)
15802 "Like `delete-char', but insert whitespace at field end in tables.
15803 When deleting characters, in tables this function will insert whitespace in
15804 front of the next \"|\" separator, to keep the table aligned. The table will
15805 still be marked for re-alignment if the field did fill the entire column,
15806 because, in this case the deletion might narrow the column."
15807 (interactive "p")
15808 (if (and (org-table-p)
15809 (not (bolp))
15810 (not (= (char-after) ?|))
15811 (eq N 1))
15812 (if (looking-at ".*?|")
15813 (let ((pos (point))
15814 (noalign (looking-at "[^|\n\r]* |"))
15815 (c org-table-may-need-update))
15816 (replace-match (concat
15817 (substring (match-string 0) 1 -1)
15818 " |"))
15819 (goto-char pos)
15820 ;; noalign: if there were two spaces at the end, this field
15821 ;; does not determine the width of the column.
15822 (if noalign (setq org-table-may-need-update c)))
15823 (delete-char N))
15824 (delete-char N)
15825 (org-fix-tags-on-the-fly)))
15827 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
15828 (put 'org-self-insert-command 'delete-selection t)
15829 (put 'orgtbl-self-insert-command 'delete-selection t)
15830 (put 'org-delete-char 'delete-selection 'supersede)
15831 (put 'org-delete-backward-char 'delete-selection 'supersede)
15832 (put 'org-yank 'delete-selection 'yank)
15834 ;; Make `flyspell-mode' delay after some commands
15835 (put 'org-self-insert-command 'flyspell-delayed t)
15836 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
15837 (put 'org-delete-char 'flyspell-delayed t)
15838 (put 'org-delete-backward-char 'flyspell-delayed t)
15840 ;; Make pabbrev-mode expand after org-mode commands
15841 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
15842 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
15844 ;; How to do this: Measure non-white length of current string
15845 ;; If equal to column width, we should realign.
15847 (defun org-remap (map &rest commands)
15848 "In MAP, remap the functions given in COMMANDS.
15849 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
15850 (let (new old)
15851 (while commands
15852 (setq old (pop commands) new (pop commands))
15853 (if (fboundp 'command-remapping)
15854 (org-defkey map (vector 'remap old) new)
15855 (substitute-key-definition old new map global-map)))))
15857 (when (eq org-enable-table-editor 'optimized)
15858 ;; If the user wants maximum table support, we need to hijack
15859 ;; some standard editing functions
15860 (org-remap org-mode-map
15861 'self-insert-command 'org-self-insert-command
15862 'delete-char 'org-delete-char
15863 'delete-backward-char 'org-delete-backward-char)
15864 (org-defkey org-mode-map "|" 'org-force-self-insert))
15866 (defvar org-ctrl-c-ctrl-c-hook nil
15867 "Hook for functions attaching themselves to `C-c C-c'.
15868 This can be used to add additional functionality to the C-c C-c key which
15869 executes context-dependent commands.
15870 Each function will be called with no arguments. The function must check
15871 if the context is appropriate for it to act. If yes, it should do its
15872 thing and then return a non-nil value. If the context is wrong,
15873 just do nothing and return nil.")
15875 (defvar org-tab-first-hook nil
15876 "Hook for functions to attach themselves to TAB.
15877 See `org-ctrl-c-ctrl-c-hook' for more information.
15878 This hook runs as the first action when TAB is pressed, even before
15879 `org-cycle' messes around with the `outline-regexp' to cater for
15880 inline tasks and plain list item folding.
15881 If any function in this hook returns t, not other actions like table
15882 field motion visibility cycling will be done.")
15884 (defvar org-tab-after-check-for-table-hook nil
15885 "Hook for functions to attach themselves to TAB.
15886 See `org-ctrl-c-ctrl-c-hook' for more information.
15887 This hook runs after it has been established that the cursor is not in a
15888 table, but before checking if the cursor is in a headline or if global cycling
15889 should be done.
15890 If any function in this hook returns t, not other actions like visibility
15891 cycling will be done.")
15893 (defvar org-tab-after-check-for-cycling-hook nil
15894 "Hook for functions to attach themselves to TAB.
15895 See `org-ctrl-c-ctrl-c-hook' for more information.
15896 This hook runs after it has been established that not table field motion and
15897 not visibility should be done because of current context. This is probably
15898 the place where a package like yasnippets can hook in.")
15900 (defvar org-tab-before-tab-emulation-hook nil
15901 "Hook for functions to attach themselves to TAB.
15902 See `org-ctrl-c-ctrl-c-hook' for more information.
15903 This hook runs after every other options for TAB have been exhausted, but
15904 before indentation and \t insertion takes place.")
15906 (defvar org-metaleft-hook nil
15907 "Hook for functions attaching themselves to `M-left'.
15908 See `org-ctrl-c-ctrl-c-hook' for more information.")
15909 (defvar org-metaright-hook nil
15910 "Hook for functions attaching themselves to `M-right'.
15911 See `org-ctrl-c-ctrl-c-hook' for more information.")
15912 (defvar org-metaup-hook nil
15913 "Hook for functions attaching themselves to `M-up'.
15914 See `org-ctrl-c-ctrl-c-hook' for more information.")
15915 (defvar org-metadown-hook nil
15916 "Hook for functions attaching themselves to `M-down'.
15917 See `org-ctrl-c-ctrl-c-hook' for more information.")
15918 (defvar org-shiftmetaleft-hook nil
15919 "Hook for functions attaching themselves to `M-S-left'.
15920 See `org-ctrl-c-ctrl-c-hook' for more information.")
15921 (defvar org-shiftmetaright-hook nil
15922 "Hook for functions attaching themselves to `M-S-right'.
15923 See `org-ctrl-c-ctrl-c-hook' for more information.")
15924 (defvar org-shiftmetaup-hook nil
15925 "Hook for functions attaching themselves to `M-S-up'.
15926 See `org-ctrl-c-ctrl-c-hook' for more information.")
15927 (defvar org-shiftmetadown-hook nil
15928 "Hook for functions attaching themselves to `M-S-down'.
15929 See `org-ctrl-c-ctrl-c-hook' for more information.")
15930 (defvar org-metareturn-hook nil
15931 "Hook for functions attaching themselves to `M-RET'.
15932 See `org-ctrl-c-ctrl-c-hook' for more information.")
15933 (defvar org-shiftup-hook nil
15934 "Hook for functions attaching themselves to `S-up'.
15935 See `org-ctrl-c-ctrl-c-hook' for more information.")
15936 (defvar org-shiftdown-hook nil
15937 "Hook for functions attaching themselves to `S-down'.
15938 See `org-ctrl-c-ctrl-c-hook' for more information.")
15939 (defvar org-shiftleft-hook nil
15940 "Hook for functions attaching themselves to `S-left'.
15941 See `org-ctrl-c-ctrl-c-hook' for more information.")
15942 (defvar org-shiftright-hook nil
15943 "Hook for functions attaching themselves to `S-right'.
15944 See `org-ctrl-c-ctrl-c-hook' for more information.")
15946 (defun org-modifier-cursor-error ()
15947 "Throw an error, a modified cursor command was applied in wrong context."
15948 (error "This command is active in special context like tables, headlines or items"))
15950 (defun org-shiftselect-error ()
15951 "Throw an error because Shift-Cursor command was applied in wrong context."
15952 (if (and (boundp 'shift-select-mode) shift-select-mode)
15953 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
15954 (error "This command works only in special context like headlines or timestamps")))
15956 (defun org-call-for-shift-select (cmd)
15957 (let ((this-command-keys-shift-translated t))
15958 (call-interactively cmd)))
15960 (defun org-shifttab (&optional arg)
15961 "Global visibility cycling or move to previous table field.
15962 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
15963 on context.
15964 See the individual commands for more information."
15965 (interactive "P")
15966 (cond
15967 ((org-at-table-p) (call-interactively 'org-table-previous-field))
15968 ((integerp arg)
15969 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
15970 (message "Content view to level: %d" arg)
15971 (org-content (prefix-numeric-value arg2))
15972 (setq org-cycle-global-status 'overview)))
15973 (t (call-interactively 'org-global-cycle))))
15975 (defun org-shiftmetaleft ()
15976 "Promote subtree or delete table column.
15977 Calls `org-promote-subtree', `org-outdent-item',
15978 or `org-table-delete-column', depending on context.
15979 See the individual commands for more information."
15980 (interactive)
15981 (cond
15982 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
15983 ((org-at-table-p) (call-interactively 'org-table-delete-column))
15984 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
15985 ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
15986 (t (org-modifier-cursor-error))))
15988 (defun org-shiftmetaright ()
15989 "Demote subtree or insert table column.
15990 Calls `org-demote-subtree', `org-indent-item',
15991 or `org-table-insert-column', depending on context.
15992 See the individual commands for more information."
15993 (interactive)
15994 (cond
15995 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
15996 ((org-at-table-p) (call-interactively 'org-table-insert-column))
15997 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
15998 ((org-at-item-p) (call-interactively 'org-indent-item-tree))
15999 (t (org-modifier-cursor-error))))
16001 (defun org-shiftmetaup (&optional arg)
16002 "Move subtree up or kill table row.
16003 Calls `org-move-subtree-up' or `org-table-kill-row' or
16004 `org-move-item-up' depending on context. See the individual commands
16005 for more information."
16006 (interactive "P")
16007 (cond
16008 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
16009 ((org-at-table-p) (call-interactively 'org-table-kill-row))
16010 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
16011 ((org-at-item-p) (call-interactively 'org-move-item-up))
16012 (t (org-modifier-cursor-error))))
16014 (defun org-shiftmetadown (&optional arg)
16015 "Move subtree down or insert table row.
16016 Calls `org-move-subtree-down' or `org-table-insert-row' or
16017 `org-move-item-down', depending on context. See the individual
16018 commands for more information."
16019 (interactive "P")
16020 (cond
16021 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
16022 ((org-at-table-p) (call-interactively 'org-table-insert-row))
16023 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
16024 ((org-at-item-p) (call-interactively 'org-move-item-down))
16025 (t (org-modifier-cursor-error))))
16027 (defsubst org-hidden-tree-error ()
16028 (error
16029 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
16031 (defun org-metaleft (&optional arg)
16032 "Promote heading or move table column to left.
16033 Calls `org-do-promote' or `org-table-move-column', depending on context.
16034 With no specific context, calls the Emacs default `backward-word'.
16035 See the individual commands for more information."
16036 (interactive "P")
16037 (cond
16038 ((run-hook-with-args-until-success 'org-metaleft-hook))
16039 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
16040 ((or (org-on-heading-p)
16041 (and (org-region-active-p)
16042 (save-excursion
16043 (goto-char (region-beginning))
16044 (org-on-heading-p))))
16045 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
16046 (call-interactively 'org-do-promote))
16047 ((or (org-at-item-p)
16048 (and (org-region-active-p)
16049 (save-excursion
16050 (goto-char (region-beginning))
16051 (org-at-item-p))))
16052 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
16053 (call-interactively 'org-outdent-item))
16054 (t (call-interactively 'backward-word))))
16056 (defun org-metaright (&optional arg)
16057 "Demote subtree or move table column to right.
16058 Calls `org-do-demote' or `org-table-move-column', depending on context.
16059 With no specific context, calls the Emacs default `forward-word'.
16060 See the individual commands for more information."
16061 (interactive "P")
16062 (cond
16063 ((run-hook-with-args-until-success 'org-metaright-hook))
16064 ((org-at-table-p) (call-interactively 'org-table-move-column))
16065 ((or (org-on-heading-p)
16066 (and (org-region-active-p)
16067 (save-excursion
16068 (goto-char (region-beginning))
16069 (org-on-heading-p))))
16070 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
16071 (call-interactively 'org-do-demote))
16072 ((or (org-at-item-p)
16073 (and (org-region-active-p)
16074 (save-excursion
16075 (goto-char (region-beginning))
16076 (org-at-item-p))))
16077 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
16078 (call-interactively 'org-indent-item))
16079 (t (call-interactively 'forward-word))))
16081 (defun org-check-for-hidden (what)
16082 "Check if there are hidden headlines/items in the current visual line.
16083 WHAT can be either `headlines' or `items'. If the current line is
16084 an outline or item heading and it has a folded subtree below it,
16085 this fucntion returns t, nil otherwise."
16086 (let ((re (cond
16087 ((eq what 'headlines) (concat "^" org-outline-regexp))
16088 ((eq what 'items) (concat "^" (org-item-re t)))
16089 (t (error "This should not happen"))))
16090 beg end)
16091 (save-excursion
16092 (catch 'exit
16093 (if (org-region-active-p)
16094 (setq beg (region-beginning) end (region-end))
16095 (setq beg (point-at-bol))
16096 (beginning-of-line 2)
16097 (while (and (not (eobp)) ;; this is like `next-line'
16098 (get-char-property (1- (point)) 'invisible))
16099 (beginning-of-line 2))
16100 (setq end (point)))
16101 (goto-char beg)
16102 (goto-char (point-at-eol))
16103 (setq end (max end (point)))
16104 (while (re-search-forward re end t)
16105 (if (get-char-property (match-beginning 0) 'invisible)
16106 (throw 'exit t)))
16107 nil))))
16109 (defun org-metaup (&optional arg)
16110 "Move subtree up or move table row up.
16111 Calls `org-move-subtree-up' or `org-table-move-row' or
16112 `org-move-item-up', depending on context. See the individual commands
16113 for more information."
16114 (interactive "P")
16115 (cond
16116 ((run-hook-with-args-until-success 'org-metaup-hook))
16117 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
16118 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
16119 ((org-at-item-p) (call-interactively 'org-move-item-up))
16120 (t (transpose-lines 1) (beginning-of-line -1))))
16122 (defun org-metadown (&optional arg)
16123 "Move subtree down or move table row down.
16124 Calls `org-move-subtree-down' or `org-table-move-row' or
16125 `org-move-item-down', depending on context. See the individual
16126 commands for more information."
16127 (interactive "P")
16128 (cond
16129 ((run-hook-with-args-until-success 'org-metadown-hook))
16130 ((org-at-table-p) (call-interactively 'org-table-move-row))
16131 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
16132 ((org-at-item-p) (call-interactively 'org-move-item-down))
16133 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
16135 (defun org-shiftup (&optional arg)
16136 "Increase item in timestamp or increase priority of current headline.
16137 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
16138 depending on context. See the individual commands for more information."
16139 (interactive "P")
16140 (cond
16141 ((run-hook-with-args-until-success 'org-shiftup-hook))
16142 ((and org-support-shift-select (org-region-active-p))
16143 (org-call-for-shift-select 'previous-line))
16144 ((org-at-timestamp-p t)
16145 (call-interactively (if org-edit-timestamp-down-means-later
16146 'org-timestamp-down 'org-timestamp-up)))
16147 ((and (not (eq org-support-shift-select 'always))
16148 org-enable-priority-commands
16149 (org-on-heading-p))
16150 (call-interactively 'org-priority-up))
16151 ((and (not org-support-shift-select) (org-at-item-p))
16152 (call-interactively 'org-previous-item))
16153 ((org-clocktable-try-shift 'up arg))
16154 (org-support-shift-select
16155 (org-call-for-shift-select 'previous-line))
16156 (t (org-shiftselect-error))))
16158 (defun org-shiftdown (&optional arg)
16159 "Decrease item in timestamp or decrease priority of current headline.
16160 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
16161 depending on context. See the individual commands for more information."
16162 (interactive "P")
16163 (cond
16164 ((run-hook-with-args-until-success 'org-shiftdown-hook))
16165 ((and org-support-shift-select (org-region-active-p))
16166 (org-call-for-shift-select 'next-line))
16167 ((org-at-timestamp-p t)
16168 (call-interactively (if org-edit-timestamp-down-means-later
16169 'org-timestamp-up 'org-timestamp-down)))
16170 ((and (not (eq org-support-shift-select 'always))
16171 org-enable-priority-commands
16172 (org-on-heading-p))
16173 (call-interactively 'org-priority-down))
16174 ((and (not org-support-shift-select) (org-at-item-p))
16175 (call-interactively 'org-next-item))
16176 ((org-clocktable-try-shift 'down arg))
16177 (org-support-shift-select
16178 (org-call-for-shift-select 'next-line))
16179 (t (org-shiftselect-error))))
16181 (defun org-shiftright (&optional arg)
16182 "Cycle the thing at point or in the current line, depending on context.
16183 Depending on context, this does one of the following:
16185 - switch a timestamp at point one day into the future
16186 - on a headline, switch to the next TODO keyword.
16187 - on an item, switch entire list to the next bullet type
16188 - on a property line, switch to the next allowed value
16189 - on a clocktable definition line, move time block into the future"
16190 (interactive "P")
16191 (cond
16192 ((run-hook-with-args-until-success 'org-shiftright-hook))
16193 ((and org-support-shift-select (org-region-active-p))
16194 (org-call-for-shift-select 'forward-char))
16195 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
16196 ((and (not (eq org-support-shift-select 'always))
16197 (org-on-heading-p))
16198 (let ((org-inhibit-logging
16199 (not org-treat-S-cursor-todo-selection-as-state-change))
16200 (org-inhibit-blocking
16201 (not org-treat-S-cursor-todo-selection-as-state-change)))
16202 (org-call-with-arg 'org-todo 'right)))
16203 ((or (and org-support-shift-select
16204 (not (eq org-support-shift-select 'always))
16205 (org-at-item-bullet-p))
16206 (and (not org-support-shift-select) (org-at-item-p)))
16207 (org-call-with-arg 'org-cycle-list-bullet nil))
16208 ((and (not (eq org-support-shift-select 'always))
16209 (org-at-property-p))
16210 (call-interactively 'org-property-next-allowed-value))
16211 ((org-clocktable-try-shift 'right arg))
16212 (org-support-shift-select
16213 (org-call-for-shift-select 'forward-char))
16214 (t (org-shiftselect-error))))
16216 (defun org-shiftleft (&optional arg)
16217 "Cycle the thing at point or in the current line, depending on context.
16218 Depending on context, this does one of the following:
16220 - switch a timestamp at point one day into the past
16221 - on a headline, switch to the previous TODO keyword.
16222 - on an item, switch entire list to the previous bullet type
16223 - on a property line, switch to the previous allowed value
16224 - on a clocktable definition line, move time block into the past"
16225 (interactive "P")
16226 (cond
16227 ((run-hook-with-args-until-success 'org-shiftleft-hook))
16228 ((and org-support-shift-select (org-region-active-p))
16229 (org-call-for-shift-select 'backward-char))
16230 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
16231 ((and (not (eq org-support-shift-select 'always))
16232 (org-on-heading-p))
16233 (let ((org-inhibit-logging
16234 (not org-treat-S-cursor-todo-selection-as-state-change))
16235 (org-inhibit-blocking
16236 (not org-treat-S-cursor-todo-selection-as-state-change)))
16237 (org-call-with-arg 'org-todo 'left)))
16238 ((or (and org-support-shift-select
16239 (not (eq org-support-shift-select 'always))
16240 (org-at-item-bullet-p))
16241 (and (not org-support-shift-select) (org-at-item-p)))
16242 (org-call-with-arg 'org-cycle-list-bullet 'previous))
16243 ((and (not (eq org-support-shift-select 'always))
16244 (org-at-property-p))
16245 (call-interactively 'org-property-previous-allowed-value))
16246 ((org-clocktable-try-shift 'left arg))
16247 (org-support-shift-select
16248 (org-call-for-shift-select 'backward-char))
16249 (t (org-shiftselect-error))))
16251 (defun org-shiftcontrolright ()
16252 "Switch to next TODO set."
16253 (interactive)
16254 (cond
16255 ((and org-support-shift-select (org-region-active-p))
16256 (org-call-for-shift-select 'forward-word))
16257 ((and (not (eq org-support-shift-select 'always))
16258 (org-on-heading-p))
16259 (org-call-with-arg 'org-todo 'nextset))
16260 (org-support-shift-select
16261 (org-call-for-shift-select 'forward-word))
16262 (t (org-shiftselect-error))))
16264 (defun org-shiftcontrolleft ()
16265 "Switch to previous TODO set."
16266 (interactive)
16267 (cond
16268 ((and org-support-shift-select (org-region-active-p))
16269 (org-call-for-shift-select 'backward-word))
16270 ((and (not (eq org-support-shift-select 'always))
16271 (org-on-heading-p))
16272 (org-call-with-arg 'org-todo 'previousset))
16273 (org-support-shift-select
16274 (org-call-for-shift-select 'backward-word))
16275 (t (org-shiftselect-error))))
16277 (defun org-ctrl-c-ret ()
16278 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
16279 (interactive)
16280 (cond
16281 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
16282 (t (call-interactively 'org-insert-heading))))
16284 (defun org-copy-special ()
16285 "Copy region in table or copy current subtree.
16286 Calls `org-table-copy' or `org-copy-subtree', depending on context.
16287 See the individual commands for more information."
16288 (interactive)
16289 (call-interactively
16290 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
16292 (defun org-cut-special ()
16293 "Cut region in table or cut current subtree.
16294 Calls `org-table-copy' or `org-cut-subtree', depending on context.
16295 See the individual commands for more information."
16296 (interactive)
16297 (call-interactively
16298 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
16300 (defun org-paste-special (arg)
16301 "Paste rectangular region into table, or past subtree relative to level.
16302 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
16303 See the individual commands for more information."
16304 (interactive "P")
16305 (if (org-at-table-p)
16306 (org-table-paste-rectangle)
16307 (org-paste-subtree arg)))
16309 (defun org-edit-special ()
16310 "Call a special editor for the stuff at point.
16311 When at a table, call the formula editor with `org-table-edit-formulas'.
16312 When at the first line of an src example, call `org-edit-src-code'.
16313 When in an #+include line, visit the include file. Otherwise call
16314 `ffap' to visit the file at point."
16315 (interactive)
16316 (cond
16317 ((org-at-table.el-p)
16318 (org-edit-src-code))
16319 ((org-at-table-p)
16320 (call-interactively 'org-table-edit-formulas))
16321 ((save-excursion
16322 (beginning-of-line 1)
16323 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
16324 (find-file (org-trim (match-string 1))))
16325 ((org-edit-src-code))
16326 ((org-edit-fixed-width-region))
16327 (t (call-interactively 'ffap))))
16330 (defun org-ctrl-c-ctrl-c (&optional arg)
16331 "Set tags in headline, or update according to changed information at point.
16333 This command does many different things, depending on context:
16335 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
16336 this is what we do.
16338 - If the cursor is on a statistics cookie, update it.
16340 - If the cursor is in a headline, prompt for tags and insert them
16341 into the current line, aligned to `org-tags-column'. When called
16342 with prefix arg, realign all tags in the current buffer.
16344 - If the cursor is in one of the special #+KEYWORD lines, this
16345 triggers scanning the buffer for these lines and updating the
16346 information.
16348 - If the cursor is inside a table, realign the table. This command
16349 works even if the automatic table editor has been turned off.
16351 - If the cursor is on a #+TBLFM line, re-apply the formulas to
16352 the entire table.
16354 - If the cursor is at a footnote reference or definition, jump to
16355 the corresponding definition or references, respectively.
16357 - If the cursor is a the beginning of a dynamic block, update it.
16359 - If the current buffer is a remember buffer, close note and file
16360 it. A prefix argument of 1 files to the default location
16361 without further interaction. A prefix argument of 2 files to
16362 the currently clocking task.
16364 - If the cursor is on a <<<target>>>, update radio targets and corresponding
16365 links in this buffer.
16367 - If the cursor is on a numbered item in a plain list, renumber the
16368 ordered list.
16370 - If the cursor is on a checkbox, toggle it."
16371 (interactive "P")
16372 (let ((org-enable-table-editor t))
16373 (cond
16374 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
16375 org-occur-highlights
16376 org-latex-fragment-image-overlays)
16377 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
16378 (org-remove-occur-highlights)
16379 (org-remove-latex-fragment-image-overlays)
16380 (message "Temporary highlights/overlays removed from current buffer"))
16381 ((and (local-variable-p 'org-finish-function (current-buffer))
16382 (fboundp org-finish-function))
16383 (funcall org-finish-function))
16384 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
16385 ((or (looking-at org-property-start-re)
16386 (org-at-property-p))
16387 (call-interactively 'org-property-action))
16388 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
16389 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
16390 (or (org-on-heading-p) (org-at-item-p)))
16391 (call-interactively 'org-update-statistics-cookies))
16392 ((org-on-heading-p) (call-interactively 'org-set-tags))
16393 ((org-at-table.el-p)
16394 (message "Use C-c ' to edit table.el tables"))
16395 ((org-at-table-p)
16396 (org-table-maybe-eval-formula)
16397 (if arg
16398 (call-interactively 'org-table-recalculate)
16399 (org-table-maybe-recalculate-line))
16400 (call-interactively 'org-table-align))
16401 ((or (org-footnote-at-reference-p)
16402 (org-footnote-at-definition-p))
16403 (call-interactively 'org-footnote-action))
16404 ((org-at-item-checkbox-p)
16405 (call-interactively 'org-toggle-checkbox))
16406 ((org-at-item-p)
16407 (if arg
16408 (call-interactively 'org-toggle-checkbox)
16409 (call-interactively 'org-maybe-renumber-ordered-list)))
16410 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
16411 ;; Dynamic block
16412 (beginning-of-line 1)
16413 (save-excursion (org-update-dblock)))
16414 ((save-excursion
16415 (beginning-of-line 1)
16416 (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
16417 (cond
16418 ((equal (match-string 1) "TBLFM")
16419 ;; Recalculate the table before this line
16420 (save-excursion
16421 (beginning-of-line 1)
16422 (skip-chars-backward " \r\n\t")
16423 (if (org-at-table-p)
16424 (org-call-with-arg 'org-table-recalculate (or arg t)))))
16426 (let ((org-inhibit-startup-visibility-stuff t)
16427 (org-startup-align-all-tables nil))
16428 (org-save-outline-visibility 'use-markers (org-mode-restart)))
16429 (message "Local setup has been refreshed"))))
16430 ((org-clock-update-time-maybe))
16431 (t (error "C-c C-c can do nothing useful at this location")))))
16433 (defun org-mode-restart ()
16434 "Restart Org-mode, to scan again for special lines.
16435 Also updates the keyword regular expressions."
16436 (interactive)
16437 (org-mode)
16438 (message "Org-mode restarted"))
16440 (defun org-kill-note-or-show-branches ()
16441 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
16442 (interactive)
16443 (if (not org-finish-function)
16444 (call-interactively 'show-branches)
16445 (let ((org-note-abort t))
16446 (funcall org-finish-function))))
16448 (defun org-return (&optional indent)
16449 "Goto next table row or insert a newline.
16450 Calls `org-table-next-row' or `newline', depending on context.
16451 See the individual commands for more information."
16452 (interactive)
16453 (cond
16454 ((bobp) (if indent (newline-and-indent) (newline)))
16455 ((org-at-table-p)
16456 (org-table-justify-field-maybe)
16457 (call-interactively 'org-table-next-row))
16458 ((and org-return-follows-link
16459 (eq (get-text-property (point) 'face) 'org-link))
16460 (call-interactively 'org-open-at-point))
16461 ((and (org-at-heading-p)
16462 (looking-at
16463 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
16464 (org-show-entry)
16465 (end-of-line 1)
16466 (newline))
16467 (t (if indent (newline-and-indent) (newline)))))
16469 (defun org-return-indent ()
16470 "Goto next table row or insert a newline and indent.
16471 Calls `org-table-next-row' or `newline-and-indent', depending on
16472 context. See the individual commands for more information."
16473 (interactive)
16474 (org-return t))
16476 (defun org-ctrl-c-star ()
16477 "Compute table, or change heading status of lines.
16478 Calls `org-table-recalculate' or `org-toggle-heading',
16479 depending on context."
16480 (interactive)
16481 (cond
16482 ((org-at-table-p)
16483 (call-interactively 'org-table-recalculate))
16485 ;; Convert all lines in region to list items
16486 (call-interactively 'org-toggle-heading))))
16488 (defun org-ctrl-c-minus ()
16489 "Insert separator line in table or modify bullet status of line.
16490 Also turns a plain line or a region of lines into list items.
16491 Calls `org-table-insert-hline', `org-toggle-item', or
16492 `org-cycle-list-bullet', depending on context."
16493 (interactive)
16494 (cond
16495 ((org-at-table-p)
16496 (call-interactively 'org-table-insert-hline))
16497 ((org-region-active-p)
16498 (call-interactively 'org-toggle-item))
16499 ((org-in-item-p)
16500 (call-interactively 'org-cycle-list-bullet))
16502 (call-interactively 'org-toggle-item))))
16504 (defun org-toggle-item ()
16505 "Convert headings or normal lines to items, items to normal lines.
16506 If there is no active region, only the current line is considered.
16508 If the first line in the region is a headline, convert all headlines to items.
16510 If the first line in the region is an item, convert all items to normal lines.
16512 If the first line is normal text, add an item bullet to each line."
16513 (interactive)
16514 (let (l2 l beg end)
16515 (if (org-region-active-p)
16516 (setq beg (region-beginning) end (region-end))
16517 (setq beg (point-at-bol)
16518 end (min (1+ (point-at-eol)) (point-max))))
16519 (save-excursion
16520 (goto-char end)
16521 (setq l2 (org-current-line))
16522 (goto-char beg)
16523 (beginning-of-line 1)
16524 (setq l (1- (org-current-line)))
16525 (if (org-at-item-p)
16526 ;; We already have items, de-itemize
16527 (while (< (setq l (1+ l)) l2)
16528 (when (org-at-item-p)
16529 (goto-char (match-beginning 2))
16530 (delete-region (match-beginning 2) (match-end 2))
16531 (and (looking-at "[ \t]+") (replace-match "")))
16532 (beginning-of-line 2))
16533 (if (org-on-heading-p)
16534 ;; Headings, convert to items
16535 (while (< (setq l (1+ l)) l2)
16536 (if (looking-at org-outline-regexp)
16537 (replace-match "- " t t))
16538 (beginning-of-line 2))
16539 ;; normal lines, turn them into items
16540 (while (< (setq l (1+ l)) l2)
16541 (unless (org-at-item-p)
16542 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
16543 (replace-match "\\1- \\2")))
16544 (beginning-of-line 2)))))))
16546 (defun org-toggle-heading (&optional nstars)
16547 "Convert headings to normal text, or items or text to headings.
16548 If there is no active region, only the current line is considered.
16550 If the first line is a heading, remove the stars from all headlines
16551 in the region.
16553 If the first line is a plain list item, turn all plain list items
16554 into headings.
16556 If the first line is a normal line, turn each and every line in the
16557 region into a heading.
16559 When converting a line into a heading, the number of stars is chosen
16560 such that the lines become children of the current entry. However,
16561 when a prefix argument is given, its value determines the number of
16562 stars to add."
16563 (interactive "P")
16564 (let (l2 l itemp beg end)
16565 (if (org-region-active-p)
16566 (setq beg (region-beginning) end (region-end))
16567 (setq beg (point-at-bol)
16568 end (min (1+ (point-at-eol)) (point-max))))
16569 (save-excursion
16570 (goto-char end)
16571 (setq l2 (org-current-line))
16572 (goto-char beg)
16573 (beginning-of-line 1)
16574 (setq l (1- (org-current-line)))
16575 (if (org-on-heading-p)
16576 ;; We already have headlines, de-star them
16577 (while (< (setq l (1+ l)) l2)
16578 (when (org-on-heading-p t)
16579 (and (looking-at outline-regexp) (replace-match "")))
16580 (beginning-of-line 2))
16581 (setq itemp (org-at-item-p))
16582 (let* ((stars
16583 (if nstars
16584 (make-string (prefix-numeric-value current-prefix-arg)
16586 (save-excursion
16587 (if (re-search-backward org-complex-heading-regexp nil t)
16588 (match-string 1) ""))))
16589 (add-stars (cond (nstars "")
16590 ((equal stars "") "*")
16591 (org-odd-levels-only "**")
16592 (t "*")))
16593 (rpl (concat stars add-stars " ")))
16594 (while (< (setq l (1+ l)) l2)
16595 (if itemp
16596 (and (org-at-item-p) (replace-match rpl t t))
16597 (unless (org-on-heading-p)
16598 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
16599 (replace-match (concat rpl (match-string 2))))))
16600 (beginning-of-line 2)))))))
16602 (defun org-meta-return (&optional arg)
16603 "Insert a new heading or wrap a region in a table.
16604 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
16605 See the individual commands for more information."
16606 (interactive "P")
16607 (cond
16608 ((run-hook-with-args-until-success 'org-metareturn-hook))
16609 ((org-at-table-p)
16610 (call-interactively 'org-table-wrap-region))
16611 (t (call-interactively 'org-insert-heading))))
16613 ;;; Menu entries
16615 ;; Define the Org-mode menus
16616 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
16617 '("Tbl"
16618 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
16619 ["Next Field" org-cycle (org-at-table-p)]
16620 ["Previous Field" org-shifttab (org-at-table-p)]
16621 ["Next Row" org-return (org-at-table-p)]
16622 "--"
16623 ["Blank Field" org-table-blank-field (org-at-table-p)]
16624 ["Edit Field" org-table-edit-field (org-at-table-p)]
16625 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
16626 "--"
16627 ("Column"
16628 ["Move Column Left" org-metaleft (org-at-table-p)]
16629 ["Move Column Right" org-metaright (org-at-table-p)]
16630 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
16631 ["Insert Column" org-shiftmetaright (org-at-table-p)])
16632 ("Row"
16633 ["Move Row Up" org-metaup (org-at-table-p)]
16634 ["Move Row Down" org-metadown (org-at-table-p)]
16635 ["Delete Row" org-shiftmetaup (org-at-table-p)]
16636 ["Insert Row" org-shiftmetadown (org-at-table-p)]
16637 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
16638 "--"
16639 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
16640 ("Rectangle"
16641 ["Copy Rectangle" org-copy-special (org-at-table-p)]
16642 ["Cut Rectangle" org-cut-special (org-at-table-p)]
16643 ["Paste Rectangle" org-paste-special (org-at-table-p)]
16644 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
16645 "--"
16646 ("Calculate"
16647 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
16648 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
16649 ["Edit Formulas" org-edit-special (org-at-table-p)]
16650 "--"
16651 ["Recalculate line" org-table-recalculate (org-at-table-p)]
16652 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
16653 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
16654 "--"
16655 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
16656 "--"
16657 ["Sum Column/Rectangle" org-table-sum
16658 (or (org-at-table-p) (org-region-active-p))]
16659 ["Which Column?" org-table-current-column (org-at-table-p)])
16660 ["Debug Formulas"
16661 org-table-toggle-formula-debugger
16662 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
16663 ["Show Col/Row Numbers"
16664 org-table-toggle-coordinate-overlays
16665 :style toggle
16666 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
16667 "--"
16668 ["Create" org-table-create (and (not (org-at-table-p))
16669 org-enable-table-editor)]
16670 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
16671 ["Import from File" org-table-import (not (org-at-table-p))]
16672 ["Export to File" org-table-export (org-at-table-p)]
16673 "--"
16674 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
16676 (easy-menu-define org-org-menu org-mode-map "Org menu"
16677 '("Org"
16678 ("Show/Hide"
16679 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
16680 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
16681 ["Sparse Tree..." org-sparse-tree t]
16682 ["Reveal Context" org-reveal t]
16683 ["Show All" show-all t]
16684 "--"
16685 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
16686 "--"
16687 ["New Heading" org-insert-heading t]
16688 ("Navigate Headings"
16689 ["Up" outline-up-heading t]
16690 ["Next" outline-next-visible-heading t]
16691 ["Previous" outline-previous-visible-heading t]
16692 ["Next Same Level" outline-forward-same-level t]
16693 ["Previous Same Level" outline-backward-same-level t]
16694 "--"
16695 ["Jump" org-goto t])
16696 ("Edit Structure"
16697 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
16698 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
16699 "--"
16700 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
16701 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
16702 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
16703 "--"
16704 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
16705 "--"
16706 ["Promote Heading" org-metaleft (not (org-at-table-p))]
16707 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
16708 ["Demote Heading" org-metaright (not (org-at-table-p))]
16709 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
16710 "--"
16711 ["Sort Region/Children" org-sort (not (org-at-table-p))]
16712 "--"
16713 ["Convert to odd levels" org-convert-to-odd-levels t]
16714 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
16715 ("Editing"
16716 ["Emphasis..." org-emphasize t]
16717 ["Edit Source Example" org-edit-special t]
16718 "--"
16719 ["Footnote new/jump" org-footnote-action t]
16720 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
16721 ("Archive"
16722 ["Archive (default method)" org-archive-subtree-default t]
16723 "--"
16724 ["Move Subtree to Archive file" org-advertized-archive-subtree t]
16725 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
16726 ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
16728 "--"
16729 ("Hyperlinks"
16730 ["Store Link (Global)" org-store-link t]
16731 ["Find existing link to here" org-occur-link-in-agenda-files t]
16732 ["Insert Link" org-insert-link t]
16733 ["Follow Link" org-open-at-point t]
16734 "--"
16735 ["Next link" org-next-link t]
16736 ["Previous link" org-previous-link t]
16737 "--"
16738 ["Descriptive Links"
16739 (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
16740 :style radio
16741 :selected (member '(org-link) buffer-invisibility-spec)]
16742 ["Literal Links"
16743 (progn
16744 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
16745 :style radio
16746 :selected (not (member '(org-link) buffer-invisibility-spec))])
16747 "--"
16748 ("TODO Lists"
16749 ["TODO/DONE/-" org-todo t]
16750 ("Select keyword"
16751 ["Next keyword" org-shiftright (org-on-heading-p)]
16752 ["Previous keyword" org-shiftleft (org-on-heading-p)]
16753 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
16754 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
16755 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
16756 ["Show TODO Tree" org-show-todo-tree t]
16757 ["Global TODO list" org-todo-list t]
16758 "--"
16759 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
16760 :selected org-enforce-todo-dependencies :style toggle :active t]
16761 "Settings for tree at point"
16762 ["Do Children sequentially" org-toggle-ordered-property :style radio
16763 :selected (ignore-errors (org-entry-get nil "ORDERED"))
16764 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
16765 ["Do Children parallel" org-toggle-ordered-property :style radio
16766 :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
16767 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
16768 "--"
16769 ["Set Priority" org-priority t]
16770 ["Priority Up" org-shiftup t]
16771 ["Priority Down" org-shiftdown t]
16772 "--"
16773 ["Get news from all feeds" org-feed-update-all t]
16774 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
16775 ["Customize feeds" (customize-variable 'org-feed-alist) t])
16776 ("TAGS and Properties"
16777 ["Set Tags" org-set-tags-command t]
16778 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
16779 "--"
16780 ["Set property" org-set-property t]
16781 ["Column view of properties" org-columns t]
16782 ["Insert Column View DBlock" org-insert-columns-dblock t])
16783 ("Dates and Scheduling"
16784 ["Timestamp" org-time-stamp t]
16785 ["Timestamp (inactive)" org-time-stamp-inactive t]
16786 ("Change Date"
16787 ["1 Day Later" org-shiftright t]
16788 ["1 Day Earlier" org-shiftleft t]
16789 ["1 ... Later" org-shiftup t]
16790 ["1 ... Earlier" org-shiftdown t])
16791 ["Compute Time Range" org-evaluate-time-range t]
16792 ["Schedule Item" org-schedule t]
16793 ["Deadline" org-deadline t]
16794 "--"
16795 ["Custom time format" org-toggle-time-stamp-overlays
16796 :style radio :selected org-display-custom-times]
16797 "--"
16798 ["Goto Calendar" org-goto-calendar t]
16799 ["Date from Calendar" org-date-from-calendar t]
16800 "--"
16801 ["Start/Restart Timer" org-timer-start t]
16802 ["Pause/Continue Timer" org-timer-pause-or-continue t]
16803 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
16804 ["Insert Timer String" org-timer t]
16805 ["Insert Timer Item" org-timer-item t])
16806 ("Logging work"
16807 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
16808 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
16809 ["Clock out" org-clock-out t]
16810 ["Clock cancel" org-clock-cancel t]
16811 "--"
16812 ["Mark as default task" org-clock-mark-default-task t]
16813 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
16814 ["Goto running clock" org-clock-goto t]
16815 "--"
16816 ["Display times" org-clock-display t]
16817 ["Create clock table" org-clock-report t]
16818 "--"
16819 ["Record DONE time"
16820 (progn (setq org-log-done (not org-log-done))
16821 (message "Switching to %s will %s record a timestamp"
16822 (car org-done-keywords)
16823 (if org-log-done "automatically" "not")))
16824 :style toggle :selected org-log-done])
16825 "--"
16826 ["Agenda Command..." org-agenda t]
16827 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
16828 ("File List for Agenda")
16829 ("Special views current file"
16830 ["TODO Tree" org-show-todo-tree t]
16831 ["Check Deadlines" org-check-deadlines t]
16832 ["Timeline" org-timeline t]
16833 ["Tags/Property tree" org-match-sparse-tree t])
16834 "--"
16835 ["Export/Publish..." org-export t]
16836 ("LaTeX"
16837 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
16838 :selected org-cdlatex-mode]
16839 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
16840 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
16841 ["Modify math symbol" org-cdlatex-math-modify
16842 (org-inside-LaTeX-fragment-p)]
16843 ["Insert citation" org-reftex-citation t]
16844 "--"
16845 ["Export LaTeX fragments as images"
16846 (if (featurep 'org-exp)
16847 (setq org-export-with-LaTeX-fragments
16848 (not org-export-with-LaTeX-fragments))
16849 (require 'org-exp))
16850 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
16851 org-export-with-LaTeX-fragments)]
16852 "--"
16853 ["Template for BEAMER" org-beamer-settings-template t])
16854 "--"
16855 ("MobileOrg"
16856 ["Push Files and Views" org-mobile-push t]
16857 ["Get Captured and Flagged" org-mobile-pull t]
16858 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
16859 "--"
16860 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
16861 "--"
16862 ("Documentation"
16863 ["Show Version" org-version t]
16864 ["Info Documentation" org-info t])
16865 ("Customize"
16866 ["Browse Org Group" org-customize t]
16867 "--"
16868 ["Expand This Menu" org-create-customize-menu
16869 (fboundp 'customize-menu-create)])
16870 ["Send bug report" org-submit-bug-report t]
16871 "--"
16872 ("Refresh/Reload"
16873 ["Refresh setup current buffer" org-mode-restart t]
16874 ["Reload Org (after update)" org-reload t]
16875 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
16878 (defun org-info (&optional node)
16879 "Read documentation for Org-mode in the info system.
16880 With optional NODE, go directly to that node."
16881 (interactive)
16882 (info (format "(org)%s" (or node ""))))
16884 ;;;###autoload
16885 (defun org-submit-bug-report ()
16886 "Submit a bug report on Org-mode via mail.
16888 Don't hesitate to report any problems or inaccurate documentation.
16890 If you don't have setup sending mail from (X)Emacs, please copy the
16891 output buffer into your mail program, as it gives us important
16892 information about your Org-mode version and configuration."
16893 (interactive)
16894 (require 'reporter)
16895 (org-load-modules-maybe)
16896 (org-require-autoloaded-modules)
16897 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
16898 (reporter-submit-bug-report
16899 "emacs-orgmode@gnu.org"
16900 (org-version)
16901 (let (list)
16902 (save-window-excursion
16903 (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
16904 (delete-other-windows)
16905 (erase-buffer)
16906 (insert "You are about to submit a bug report to the Org-mode mailing list.
16908 We would like to add your full Org-mode and Outline configuration to the
16909 bug report. This greatly simplifies the work of the maintainer and
16910 other experts on the mailing list.
16912 HOWEVER, some variables you have customized may contain private
16913 information. The names of customers, colleagues, or friends, might
16914 appear in the form of file names, tags, todo states, or search strings.
16915 If you answer yes to the prompt, you might want to check and remove
16916 such private information before sending the email.")
16917 (add-text-properties (point-min) (point-max) '(face org-warning))
16918 (when (yes-or-no-p "Include your Org-mode configuration ")
16919 (mapatoms
16920 (lambda (v)
16921 (and (boundp v)
16922 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
16923 (or (and (symbol-value v)
16924 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
16925 (and
16926 (get v 'custom-type) (get v 'standard-value)
16927 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
16928 (push v list)))))
16929 (kill-buffer (get-buffer "*Warn about privacy*"))
16930 list))
16931 nil nil
16932 "Remember to cover the basics, that is, what you expected to happen and
16933 what in fact did happen. You don't know how to make a good report? See
16935 http://orgmode.org/manual/Feedback.html#Feedback
16937 Your bug report will be posted to the Org-mode mailing list.
16938 ------------------------------------------------------------------------")
16939 (save-excursion
16940 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
16941 (replace-match "\\1Bug: \\3 [\\2]")))))
16944 (defun org-install-agenda-files-menu ()
16945 (let ((bl (buffer-list)))
16946 (save-excursion
16947 (while bl
16948 (set-buffer (pop bl))
16949 (if (org-mode-p) (setq bl nil)))
16950 (when (org-mode-p)
16951 (easy-menu-change
16952 '("Org") "File List for Agenda"
16953 (append
16954 (list
16955 ["Edit File List" (org-edit-agenda-file-list) t]
16956 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
16957 ["Remove Current File from List" org-remove-file t]
16958 ["Cycle through agenda files" org-cycle-agenda-files t]
16959 ["Occur in all agenda files" org-occur-in-agenda-files t]
16960 "--")
16961 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
16963 ;;;; Documentation
16965 ;;;###autoload
16966 (defun org-require-autoloaded-modules ()
16967 (interactive)
16968 (mapc 'require
16969 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
16970 org-docbook org-exp org-html org-icalendar
16971 org-id org-latex
16972 org-publish org-remember org-table
16973 org-timer org-xoxo)))
16975 ;;;###autoload
16976 (defun org-reload (&optional uncompiled)
16977 "Reload all org lisp files.
16978 With prefix arg UNCOMPILED, load the uncompiled versions."
16979 (interactive "P")
16980 (require 'find-func)
16981 (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
16982 (dir-org (file-name-directory (org-find-library-name "org")))
16983 (dir-org-contrib (ignore-errors
16984 (file-name-directory
16985 (org-find-library-name "org-contribdir"))))
16986 (files
16987 (append (directory-files dir-org t file-re)
16988 (and dir-org-contrib
16989 (directory-files dir-org-contrib t file-re))))
16990 (remove-re (concat (if (featurep 'xemacs)
16991 "org-colview" "org-colview-xemacs")
16992 "\\'")))
16993 (setq files (mapcar 'file-name-sans-extension files))
16994 (setq files (mapcar
16995 (lambda (x) (if (string-match remove-re x) nil x))
16996 files))
16997 (setq files (delq nil files))
16998 (mapc
16999 (lambda (f)
17000 (when (featurep (intern (file-name-nondirectory f)))
17001 (if (and (not uncompiled)
17002 (file-exists-p (concat f ".elc")))
17003 (load (concat f ".elc") nil nil t)
17004 (load (concat f ".el") nil nil t))))
17005 files))
17006 (org-version))
17008 ;;;###autoload
17009 (defun org-customize ()
17010 "Call the customize function with org as argument."
17011 (interactive)
17012 (org-load-modules-maybe)
17013 (org-require-autoloaded-modules)
17014 (customize-browse 'org))
17016 (defun org-create-customize-menu ()
17017 "Create a full customization menu for Org-mode, insert it into the menu."
17018 (interactive)
17019 (org-load-modules-maybe)
17020 (org-require-autoloaded-modules)
17021 (if (fboundp 'customize-menu-create)
17022 (progn
17023 (easy-menu-change
17024 '("Org") "Customize"
17025 `(["Browse Org group" org-customize t]
17026 "--"
17027 ,(customize-menu-create 'org)
17028 ["Set" Custom-set t]
17029 ["Save" Custom-save t]
17030 ["Reset to Current" Custom-reset-current t]
17031 ["Reset to Saved" Custom-reset-saved t]
17032 ["Reset to Standard Settings" Custom-reset-standard t]))
17033 (message "\"Org\"-menu now contains full customization menu"))
17034 (error "Cannot expand menu (outdated version of cus-edit.el)")))
17036 ;;;; Miscellaneous stuff
17038 ;;; Generally useful functions
17040 (defun org-get-at-bol (property)
17041 "Get text property PROPERTY at beginning of line."
17042 (get-text-property (point-at-bol) property))
17044 (defun org-find-text-property-in-string (prop s)
17045 "Return the first non-nil value of property PROP in string S."
17046 (or (get-text-property 0 prop s)
17047 (get-text-property (or (next-single-property-change 0 prop s) 0)
17048 prop s)))
17050 (defun org-display-warning (message) ;; Copied from Emacs-Muse
17051 "Display the given MESSAGE as a warning."
17052 (if (fboundp 'display-warning)
17053 (display-warning 'org message
17054 (if (featurep 'xemacs) 'warning :warning))
17055 (let ((buf (get-buffer-create "*Org warnings*")))
17056 (with-current-buffer buf
17057 (goto-char (point-max))
17058 (insert "Warning (Org): " message)
17059 (unless (bolp)
17060 (newline)))
17061 (display-buffer buf)
17062 (sit-for 0))))
17064 (defun org-in-commented-line ()
17065 "Is point in a line starting with `#'?"
17066 (equal (char-after (point-at-bol)) ?#))
17068 (defun org-in-verbatim-emphasis ()
17069 (save-match-data
17070 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
17072 (defun org-goto-marker-or-bmk (marker &optional bookmark)
17073 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
17074 (if (and marker (marker-buffer marker)
17075 (buffer-live-p (marker-buffer marker)))
17076 (progn
17077 (switch-to-buffer (marker-buffer marker))
17078 (if (or (> marker (point-max)) (< marker (point-min)))
17079 (widen))
17080 (goto-char marker)
17081 (org-show-context 'org-goto))
17082 (if bookmark
17083 (bookmark-jump bookmark)
17084 (error "Cannot find location"))))
17086 (defun org-quote-csv-field (s)
17087 "Quote field for inclusion in CSV material."
17088 (if (string-match "[\",]" s)
17089 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
17092 (defun org-plist-delete (plist property)
17093 "Delete PROPERTY from PLIST.
17094 This is in contrast to merely setting it to 0."
17095 (let (p)
17096 (while plist
17097 (if (not (eq property (car plist)))
17098 (setq p (plist-put p (car plist) (nth 1 plist))))
17099 (setq plist (cddr plist)))
17102 (defun org-force-self-insert (N)
17103 "Needed to enforce self-insert under remapping."
17104 (interactive "p")
17105 (self-insert-command N))
17107 (defun org-string-width (s)
17108 "Compute width of string, ignoring invisible characters.
17109 This ignores character with invisibility property `org-link', and also
17110 characters with property `org-cwidth', because these will become invisible
17111 upon the next fontification round."
17112 (let (b l)
17113 (when (or (eq t buffer-invisibility-spec)
17114 (assq 'org-link buffer-invisibility-spec))
17115 (while (setq b (text-property-any 0 (length s)
17116 'invisible 'org-link s))
17117 (setq s (concat (substring s 0 b)
17118 (substring s (or (next-single-property-change
17119 b 'invisible s) (length s)))))))
17120 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
17121 (setq s (concat (substring s 0 b)
17122 (substring s (or (next-single-property-change
17123 b 'org-cwidth s) (length s))))))
17124 (setq l (string-width s) b -1)
17125 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
17126 (setq l (- l (get-text-property b 'org-dwidth-n s))))
17129 (defun org-get-indentation (&optional line)
17130 "Get the indentation of the current line, interpreting tabs.
17131 When LINE is given, assume it represents a line and compute its indentation."
17132 (if line
17133 (if (string-match "^ *" (org-remove-tabs line))
17134 (match-end 0))
17135 (save-excursion
17136 (beginning-of-line 1)
17137 (skip-chars-forward " \t")
17138 (current-column))))
17140 (defun org-remove-tabs (s &optional width)
17141 "Replace tabulators in S with spaces.
17142 Assumes that s is a single line, starting in column 0."
17143 (setq width (or width tab-width))
17144 (while (string-match "\t" s)
17145 (setq s (replace-match
17146 (make-string
17147 (- (* width (/ (+ (match-beginning 0) width) width))
17148 (match-beginning 0)) ?\ )
17149 t t s)))
17152 (defun org-fix-indentation (line ind)
17153 "Fix indentation in LINE.
17154 IND is a cons cell with target and minimum indentation.
17155 If the current indentation in LINE is smaller than the minimum,
17156 leave it alone. If it is larger than ind, set it to the target."
17157 (let* ((l (org-remove-tabs line))
17158 (i (org-get-indentation l))
17159 (i1 (car ind)) (i2 (cdr ind)))
17160 (if (>= i i2) (setq l (substring line i2)))
17161 (if (> i1 0)
17162 (concat (make-string i1 ?\ ) l)
17163 l)))
17165 (defun org-remove-indentation (code &optional n)
17166 "Remove the maximum common indentation from the lines in CODE.
17167 N may optionally be the number of spaces to remove."
17168 (with-temp-buffer
17169 (insert code)
17170 (org-do-remove-indentation n)
17171 (buffer-string)))
17173 (defun org-do-remove-indentation (&optional n)
17174 "Remove the maximum common indentation from the buffer."
17175 (untabify (point-min) (point-max))
17176 (let ((min 10000) re)
17177 (if n
17178 (setq min n)
17179 (goto-char (point-min))
17180 (while (re-search-forward "^ *[^ \n]" nil t)
17181 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
17182 (unless (or (= min 0) (= min 10000))
17183 (setq re (format "^ \\{%d\\}" min))
17184 (goto-char (point-min))
17185 (while (re-search-forward re nil t)
17186 (replace-match "")
17187 (end-of-line 1))
17188 min)))
17190 (defun org-fill-template (template alist)
17191 "Find each %key of ALIST in TEMPLATE and replace it."
17192 (let ((case-fold-search nil)
17193 entry key value)
17194 (setq alist (sort (copy-sequence alist)
17195 (lambda (a b) (< (length (car a)) (length (car b))))))
17196 (while (setq entry (pop alist))
17197 (setq template
17198 (replace-regexp-in-string
17199 (concat "%" (regexp-quote (car entry)))
17200 (cdr entry) template t t)))
17201 template))
17203 (defun org-base-buffer (buffer)
17204 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
17205 (if (not buffer)
17206 buffer
17207 (or (buffer-base-buffer buffer)
17208 buffer)))
17210 (defun org-trim (s)
17211 "Remove whitespace at beginning and end of string."
17212 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
17213 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
17216 (defun org-wrap (string &optional width lines)
17217 "Wrap string to either a number of lines, or a width in characters.
17218 If WIDTH is non-nil, the string is wrapped to that width, however many lines
17219 that costs. If there is a word longer than WIDTH, the text is actually
17220 wrapped to the length of that word.
17221 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
17222 many lines, whatever width that takes.
17223 The return value is a list of lines, without newlines at the end."
17224 (let* ((words (org-split-string string "[ \t\n]+"))
17225 (maxword (apply 'max (mapcar 'org-string-width words)))
17226 w ll)
17227 (cond (width
17228 (org-do-wrap words (max maxword width)))
17229 (lines
17230 (setq w maxword)
17231 (setq ll (org-do-wrap words maxword))
17232 (if (<= (length ll) lines)
17234 (setq ll words)
17235 (while (> (length ll) lines)
17236 (setq w (1+ w))
17237 (setq ll (org-do-wrap words w)))
17238 ll))
17239 (t (error "Cannot wrap this")))))
17241 (defun org-do-wrap (words width)
17242 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
17243 (let (lines line)
17244 (while words
17245 (setq line (pop words))
17246 (while (and words (< (+ (length line) (length (car words))) width))
17247 (setq line (concat line " " (pop words))))
17248 (setq lines (push line lines)))
17249 (nreverse lines)))
17251 (defun org-split-string (string &optional separators)
17252 "Splits STRING into substrings at SEPARATORS.
17253 No empty strings are returned if there are matches at the beginning
17254 and end of string."
17255 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
17256 (start 0)
17257 notfirst
17258 (list nil))
17259 (while (and (string-match rexp string
17260 (if (and notfirst
17261 (= start (match-beginning 0))
17262 (< start (length string)))
17263 (1+ start) start))
17264 (< (match-beginning 0) (length string)))
17265 (setq notfirst t)
17266 (or (eq (match-beginning 0) 0)
17267 (and (eq (match-beginning 0) (match-end 0))
17268 (eq (match-beginning 0) start))
17269 (setq list
17270 (cons (substring string start (match-beginning 0))
17271 list)))
17272 (setq start (match-end 0)))
17273 (or (eq start (length string))
17274 (setq list
17275 (cons (substring string start)
17276 list)))
17277 (nreverse list)))
17279 (defun org-quote-vert (s)
17280 "Replace \"|\" with \"\\vert\"."
17281 (while (string-match "|" s)
17282 (setq s (replace-match "\\vert" t t s)))
17285 (defun org-uuidgen-p (s)
17286 "Is S an ID created by UUIDGEN?"
17287 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
17289 (defun org-context ()
17290 "Return a list of contexts of the current cursor position.
17291 If several contexts apply, all are returned.
17292 Each context entry is a list with a symbol naming the context, and
17293 two positions indicating start and end of the context. Possible
17294 contexts are:
17296 :headline anywhere in a headline
17297 :headline-stars on the leading stars in a headline
17298 :todo-keyword on a TODO keyword (including DONE) in a headline
17299 :tags on the TAGS in a headline
17300 :priority on the priority cookie in a headline
17301 :item on the first line of a plain list item
17302 :item-bullet on the bullet/number of a plain list item
17303 :checkbox on the checkbox in a plain list item
17304 :table in an org-mode table
17305 :table-special on a special filed in a table
17306 :table-table in a table.el table
17307 :link on a hyperlink
17308 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
17309 :target on a <<target>>
17310 :radio-target on a <<<radio-target>>>
17311 :latex-fragment on a LaTeX fragment
17312 :latex-preview on a LaTeX fragment with overlayed preview image
17314 This function expects the position to be visible because it uses font-lock
17315 faces as a help to recognize the following contexts: :table-special, :link,
17316 and :keyword."
17317 (let* ((f (get-text-property (point) 'face))
17318 (faces (if (listp f) f (list f)))
17319 (p (point)) clist o)
17320 ;; First the large context
17321 (cond
17322 ((org-on-heading-p t)
17323 (push (list :headline (point-at-bol) (point-at-eol)) clist)
17324 (when (progn
17325 (beginning-of-line 1)
17326 (looking-at org-todo-line-tags-regexp))
17327 (push (org-point-in-group p 1 :headline-stars) clist)
17328 (push (org-point-in-group p 2 :todo-keyword) clist)
17329 (push (org-point-in-group p 4 :tags) clist))
17330 (goto-char p)
17331 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
17332 (if (looking-at "\\[#[A-Z0-9]\\]")
17333 (push (org-point-in-group p 0 :priority) clist)))
17335 ((org-at-item-p)
17336 (push (org-point-in-group p 2 :item-bullet) clist)
17337 (push (list :item (point-at-bol)
17338 (save-excursion (org-end-of-item) (point)))
17339 clist)
17340 (and (org-at-item-checkbox-p)
17341 (push (org-point-in-group p 0 :checkbox) clist)))
17343 ((org-at-table-p)
17344 (push (list :table (org-table-begin) (org-table-end)) clist)
17345 (if (memq 'org-formula faces)
17346 (push (list :table-special
17347 (previous-single-property-change p 'face)
17348 (next-single-property-change p 'face)) clist)))
17349 ((org-at-table-p 'any)
17350 (push (list :table-table) clist)))
17351 (goto-char p)
17353 ;; Now the small context
17354 (cond
17355 ((org-at-timestamp-p)
17356 (push (org-point-in-group p 0 :timestamp) clist))
17357 ((memq 'org-link faces)
17358 (push (list :link
17359 (previous-single-property-change p 'face)
17360 (next-single-property-change p 'face)) clist))
17361 ((memq 'org-special-keyword faces)
17362 (push (list :keyword
17363 (previous-single-property-change p 'face)
17364 (next-single-property-change p 'face)) clist))
17365 ((org-on-target-p)
17366 (push (org-point-in-group p 0 :target) clist)
17367 (goto-char (1- (match-beginning 0)))
17368 (if (looking-at org-radio-target-regexp)
17369 (push (org-point-in-group p 0 :radio-target) clist))
17370 (goto-char p))
17371 ((setq o (car (delq nil
17372 (mapcar
17373 (lambda (x)
17374 (if (memq x org-latex-fragment-image-overlays) x))
17375 (overlays-at (point))))))
17376 (push (list :latex-fragment
17377 (overlay-start o) (overlay-end o)) clist)
17378 (push (list :latex-preview
17379 (overlay-start o) (overlay-end o)) clist))
17380 ((org-inside-LaTeX-fragment-p)
17381 ;; FIXME: positions wrong.
17382 (push (list :latex-fragment (point) (point)) clist)))
17384 (setq clist (nreverse (delq nil clist)))
17385 clist))
17387 ;; FIXME: Compare with at-regexp-p Do we need both?
17388 (defun org-in-regexp (re &optional nlines visually)
17389 "Check if point is inside a match of regexp.
17390 Normally only the current line is checked, but you can include NLINES extra
17391 lines both before and after point into the search.
17392 If VISUALLY is set, require that the cursor is not after the match but
17393 really on, so that the block visually is on the match."
17394 (catch 'exit
17395 (let ((pos (point))
17396 (eol (point-at-eol (+ 1 (or nlines 0))))
17397 (inc (if visually 1 0)))
17398 (save-excursion
17399 (beginning-of-line (- 1 (or nlines 0)))
17400 (while (re-search-forward re eol t)
17401 (if (and (<= (match-beginning 0) pos)
17402 (>= (+ inc (match-end 0)) pos))
17403 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
17405 (defun org-at-regexp-p (regexp)
17406 "Is point inside a match of REGEXP in the current line?"
17407 (catch 'exit
17408 (save-excursion
17409 (let ((pos (point)) (end (point-at-eol)))
17410 (beginning-of-line 1)
17411 (while (re-search-forward regexp end t)
17412 (if (and (<= (match-beginning 0) pos)
17413 (>= (match-end 0) pos))
17414 (throw 'exit t)))
17415 nil))))
17417 (defun org-in-regexps-block-p (start-re end-re)
17418 "Returns t if the current point is between matches of START-RE and END-RE.
17419 This will also return to if point is on one of the two matches."
17420 (interactive)
17421 (let ((p (point)))
17422 (save-excursion
17423 (and (or (org-at-regexp-p start-re)
17424 (re-search-backward start-re nil t))
17425 (re-search-forward end-re nil t)
17426 (>= (point) p)))))
17428 (defun org-occur-in-agenda-files (regexp &optional nlines)
17429 "Call `multi-occur' with buffers for all agenda files."
17430 (interactive "sOrg-files matching: \np")
17431 (let* ((files (org-agenda-files))
17432 (tnames (mapcar 'file-truename files))
17433 (extra org-agenda-text-search-extra-files)
17435 (when (eq (car extra) 'agenda-archives)
17436 (setq extra (cdr extra))
17437 (setq files (org-add-archive-files files)))
17438 (while (setq f (pop extra))
17439 (unless (member (file-truename f) tnames)
17440 (add-to-list 'files f 'append)
17441 (add-to-list 'tnames (file-truename f) 'append)))
17442 (multi-occur
17443 (mapcar (lambda (x)
17444 (with-current-buffer
17445 (or (get-file-buffer x) (find-file-noselect x))
17446 (widen)
17447 (current-buffer)))
17448 files)
17449 regexp)))
17451 (if (boundp 'occur-mode-find-occurrence-hook)
17452 ;; Emacs 23
17453 (add-hook 'occur-mode-find-occurrence-hook
17454 (lambda ()
17455 (when (org-mode-p)
17456 (org-reveal))))
17457 ;; Emacs 22
17458 (defadvice occur-mode-goto-occurrence
17459 (after org-occur-reveal activate)
17460 (and (org-mode-p) (org-reveal)))
17461 (defadvice occur-mode-goto-occurrence-other-window
17462 (after org-occur-reveal activate)
17463 (and (org-mode-p) (org-reveal)))
17464 (defadvice occur-mode-display-occurrence
17465 (after org-occur-reveal activate)
17466 (when (org-mode-p)
17467 (let ((pos (occur-mode-find-occurrence)))
17468 (with-current-buffer (marker-buffer pos)
17469 (save-excursion
17470 (goto-char pos)
17471 (org-reveal)))))))
17473 (defun org-occur-link-in-agenda-files ()
17474 "Create a link and search for it in the agendas.
17475 The link is not stored in `org-stored-links', it is just created
17476 for the search purpose."
17477 (interactive)
17478 (let ((link (condition-case nil
17479 (org-store-link nil)
17480 (error "Unable to create a link to here"))))
17481 (org-occur-in-agenda-files (regexp-quote link))))
17483 (defun org-uniquify (list)
17484 "Remove duplicate elements from LIST."
17485 (let (res)
17486 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
17487 res))
17489 (defun org-delete-all (elts list)
17490 "Remove all elements in ELTS from LIST."
17491 (while elts
17492 (setq list (delete (pop elts) list)))
17493 list)
17495 (defun org-remove-if (predicate seq)
17496 "Remove everything from SEQ that fulfills PREDICATE."
17497 (let (res e)
17498 (while seq
17499 (setq e (pop seq))
17500 (if (not (funcall predicate e)) (push e res)))
17501 (nreverse res)))
17503 (defun org-remove-if-not (predicate seq)
17504 "Remove everything from SEQ that does not fulfill PREDICATE."
17505 (let (res e)
17506 (while seq
17507 (setq e (pop seq))
17508 (if (funcall predicate e) (push e res)))
17509 (nreverse res)))
17511 (defun org-back-over-empty-lines ()
17512 "Move backwards over whitespace, to the beginning of the first empty line.
17513 Returns the number of empty lines passed."
17514 (let ((pos (point)))
17515 (skip-chars-backward " \t\n\r")
17516 (beginning-of-line 2)
17517 (goto-char (min (point) pos))
17518 (count-lines (point) pos)))
17520 (defun org-skip-whitespace ()
17521 (skip-chars-forward " \t\n\r"))
17523 (defun org-point-in-group (point group &optional context)
17524 "Check if POINT is in match-group GROUP.
17525 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
17526 match. If the match group does ot exist or point is not inside it,
17527 return nil."
17528 (and (match-beginning group)
17529 (>= point (match-beginning group))
17530 (<= point (match-end group))
17531 (if context
17532 (list context (match-beginning group) (match-end group))
17533 t)))
17535 (defun org-switch-to-buffer-other-window (&rest args)
17536 "Switch to buffer in a second window on the current frame.
17537 In particular, do not allow pop-up frames."
17538 (let (pop-up-frames special-display-buffer-names special-display-regexps
17539 special-display-function)
17540 (apply 'switch-to-buffer-other-window args)))
17542 (defun org-combine-plists (&rest plists)
17543 "Create a single property list from all plists in PLISTS.
17544 The process starts by copying the first list, and then setting properties
17545 from the other lists. Settings in the last list are the most significant
17546 ones and overrule settings in the other lists."
17547 (let ((rtn (copy-sequence (pop plists)))
17548 p v ls)
17549 (while plists
17550 (setq ls (pop plists))
17551 (while ls
17552 (setq p (pop ls) v (pop ls))
17553 (setq rtn (plist-put rtn p v))))
17554 rtn))
17556 (defun org-move-line-down (arg)
17557 "Move the current line down. With prefix argument, move it past ARG lines."
17558 (interactive "p")
17559 (let ((col (current-column))
17560 beg end pos)
17561 (beginning-of-line 1) (setq beg (point))
17562 (beginning-of-line 2) (setq end (point))
17563 (beginning-of-line (+ 1 arg))
17564 (setq pos (move-marker (make-marker) (point)))
17565 (insert (delete-and-extract-region beg end))
17566 (goto-char pos)
17567 (org-move-to-column col)))
17569 (defun org-move-line-up (arg)
17570 "Move the current line up. With prefix argument, move it past ARG lines."
17571 (interactive "p")
17572 (let ((col (current-column))
17573 beg end pos)
17574 (beginning-of-line 1) (setq beg (point))
17575 (beginning-of-line 2) (setq end (point))
17576 (beginning-of-line (- arg))
17577 (setq pos (move-marker (make-marker) (point)))
17578 (insert (delete-and-extract-region beg end))
17579 (goto-char pos)
17580 (org-move-to-column col)))
17582 (defun org-replace-escapes (string table)
17583 "Replace %-escapes in STRING with values in TABLE.
17584 TABLE is an association list with keys like \"%a\" and string values.
17585 The sequences in STRING may contain normal field width and padding information,
17586 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
17587 so values can contain further %-escapes if they are define later in TABLE."
17588 (let ((case-fold-search nil)
17589 e re rpl)
17590 (while (setq e (pop table))
17591 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
17592 (while (string-match re string)
17593 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
17594 (cdr e)))
17595 (setq string (replace-match rpl t t string))))
17596 string))
17599 (defun org-sublist (list start end)
17600 "Return a section of LIST, from START to END.
17601 Counting starts at 1."
17602 (let (rtn (c start))
17603 (setq list (nthcdr (1- start) list))
17604 (while (and list (<= c end))
17605 (push (pop list) rtn)
17606 (setq c (1+ c)))
17607 (nreverse rtn)))
17609 (defun org-find-base-buffer-visiting (file)
17610 "Like `find-buffer-visiting' but always return the base buffer and
17611 not an indirect buffer."
17612 (let ((buf (or (get-file-buffer file)
17613 (find-buffer-visiting file))))
17614 (if buf
17615 (or (buffer-base-buffer buf) buf)
17616 nil)))
17618 (defun org-image-file-name-regexp (&optional extensions)
17619 "Return regexp matching the file names of images.
17620 If EXTENSIONS is given, only match these."
17621 (if (and (not extensions) (fboundp 'image-file-name-regexp))
17622 (image-file-name-regexp)
17623 (let ((image-file-name-extensions
17624 (or extensions
17625 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
17626 "xbm" "xpm" "pbm" "pgm" "ppm"))))
17627 (concat "\\."
17628 (regexp-opt (nconc (mapcar 'upcase
17629 image-file-name-extensions)
17630 image-file-name-extensions)
17632 "\\'"))))
17634 (defun org-file-image-p (file &optional extensions)
17635 "Return non-nil if FILE is an image."
17636 (save-match-data
17637 (string-match (org-image-file-name-regexp extensions) file)))
17639 (defun org-get-cursor-date ()
17640 "Return the date at cursor in as a time.
17641 This works in the calendar and in the agenda, anywhere else it just
17642 returns the current time."
17643 (let (date day defd)
17644 (cond
17645 ((eq major-mode 'calendar-mode)
17646 (setq date (calendar-cursor-to-date)
17647 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
17648 ((eq major-mode 'org-agenda-mode)
17649 (setq day (get-text-property (point) 'day))
17650 (if day
17651 (setq date (calendar-gregorian-from-absolute day)
17652 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
17653 (nth 2 date))))))
17654 (or defd (current-time))))
17656 (defvar org-agenda-action-marker (make-marker)
17657 "Marker pointing to the entry for the next agenda action.")
17659 (defun org-mark-entry-for-agenda-action ()
17660 "Mark the current entry as target of an agenda action.
17661 Agenda actions are actions executed from the agenda with the key `k',
17662 which make use of the date at the cursor."
17663 (interactive)
17664 (move-marker org-agenda-action-marker
17665 (save-excursion (org-back-to-heading t) (point))
17666 (current-buffer))
17667 (message
17668 "Entry marked for action; press `k' at desired date in agenda or calendar"))
17670 ;;; Paragraph filling stuff.
17671 ;; We want this to be just right, so use the full arsenal.
17673 (defun org-indent-line-function ()
17674 "Indent line like previous, but further if previous was headline or item."
17675 (interactive)
17676 (let* ((pos (point))
17677 (itemp (org-at-item-p))
17678 (case-fold-search t)
17679 (org-drawer-regexp (or org-drawer-regexp "\000"))
17680 column bpos bcol tpos tcol bullet btype bullet-type)
17681 ;; Find the previous relevant line
17682 (beginning-of-line 1)
17683 (cond
17684 ((looking-at "#") (setq column 0))
17685 ((looking-at "\\*+ ") (setq column 0))
17686 ((and (looking-at "[ \t]*:END:")
17687 (save-excursion (re-search-backward org-drawer-regexp nil t)))
17688 (save-excursion
17689 (goto-char (1- (match-beginning 1)))
17690 (setq column (current-column))))
17691 ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
17692 (save-excursion
17693 (re-search-backward
17694 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
17695 (setq column (org-get-indentation (match-string 0))))
17697 (beginning-of-line 0)
17698 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
17699 (not (looking-at "[ \t]*:END:"))
17700 (not (looking-at org-drawer-regexp)))
17701 (beginning-of-line 0))
17702 (cond
17703 ((looking-at "\\*+[ \t]+")
17704 (if (not org-adapt-indentation)
17705 (setq column 0)
17706 (goto-char (match-end 0))
17707 (setq column (current-column))))
17708 ((looking-at org-drawer-regexp)
17709 (goto-char (1- (match-beginning 1)))
17710 (setq column (current-column)))
17711 ((looking-at "\\([ \t]*\\):END:")
17712 (goto-char (match-end 1))
17713 (setq column (current-column)))
17714 ((org-in-item-p)
17715 (org-beginning-of-item)
17716 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
17717 (setq bpos (match-beginning 1) tpos (match-end 0)
17718 bcol (progn (goto-char bpos) (current-column))
17719 tcol (progn (goto-char tpos) (current-column))
17720 bullet (match-string 1)
17721 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
17722 (if (> tcol (+ bcol org-description-max-indent))
17723 (setq tcol (+ bcol 5)))
17724 (if (not itemp)
17725 (setq column tcol)
17726 (goto-char pos)
17727 (beginning-of-line 1)
17728 (if (looking-at "\\S-")
17729 (progn
17730 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
17731 (setq bullet (match-string 1)
17732 btype (if (string-match "[0-9]" bullet) "n" bullet))
17733 (setq column (if (equal btype bullet-type) bcol tcol)))
17734 (setq column (org-get-indentation)))))
17735 (t (setq column (org-get-indentation))))))
17736 (goto-char pos)
17737 (if (<= (current-column) (current-indentation))
17738 (org-indent-line-to column)
17739 (save-excursion (org-indent-line-to column)))
17740 (setq column (current-column))
17741 (beginning-of-line 1)
17742 (if (looking-at
17743 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
17744 (replace-match (concat (match-string 1)
17745 (format org-property-format
17746 (match-string 2) (match-string 3)))
17747 t t))
17748 (org-move-to-column column)))
17750 (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
17751 "Variable to store copy of `adaptive-fill-regexp'.
17752 Since `adaptive-fill-regexp' is set to never match, we need to
17753 store a backup of its value before entering `org-mode' so that
17754 the functionality can be provided as a fall-back.")
17756 (defun org-set-autofill-regexps ()
17757 (interactive)
17758 ;; In the paragraph separator we include headlines, because filling
17759 ;; text in a line directly attached to a headline would otherwise
17760 ;; fill the headline as well.
17761 (org-set-local 'comment-start-skip "^#+[ \t]*")
17762 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
17763 ;; The paragraph starter includes hand-formatted lists.
17764 (org-set-local
17765 'paragraph-start
17766 (concat
17767 "\f" "\\|"
17768 "[ ]*$" "\\|"
17769 "\\*+ " "\\|"
17770 "[ \t]*#" "\\|"
17771 "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
17772 "[ \t]*[:|]" "\\|"
17773 "\\$\\$" "\\|"
17774 "\\\\\\(begin\\|end\\|[][]\\)"))
17775 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
17776 ;; But only if the user has not turned off tables or fixed-width regions
17777 (org-set-local
17778 'auto-fill-inhibit-regexp
17779 (concat "\\*+ \\|#\\+"
17780 "\\|[ \t]*" org-keyword-time-regexp
17781 (if (or org-enable-table-editor org-enable-fixed-width-editor)
17782 (concat
17783 "\\|[ \t]*["
17784 (if org-enable-table-editor "|" "")
17785 (if org-enable-fixed-width-editor ":" "")
17786 "]"))))
17787 ;; We use our own fill-paragraph function, to make sure that tables
17788 ;; and fixed-width regions are not wrapped. That function will pass
17789 ;; through to `fill-paragraph' when appropriate.
17790 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
17791 ;; Adaptive filling: To get full control, first make sure that
17792 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
17793 (unless (local-variable-p 'adaptive-fill-regexp)
17794 (org-set-local 'org-adaptive-fill-regexp-backup
17795 adaptive-fill-regexp))
17796 (org-set-local 'adaptive-fill-regexp "\000")
17797 (org-set-local 'adaptive-fill-function
17798 'org-adaptive-fill-function)
17799 (org-set-local
17800 'align-mode-rules-list
17801 '((org-in-buffer-settings
17802 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
17803 (modes . '(org-mode))))))
17805 (defun org-fill-paragraph (&optional justify)
17806 "Re-align a table, pass through to fill-paragraph if no table."
17807 (let ((table-p (org-at-table-p))
17808 (table.el-p (org-at-table.el-p)))
17809 (cond ((and (equal (char-after (point-at-bol)) ?*)
17810 (save-excursion (goto-char (point-at-bol))
17811 (looking-at outline-regexp)))
17812 t) ; skip headlines
17813 (table.el-p t) ; skip table.el tables
17814 (table-p (org-table-align) t) ; align org-mode tables
17815 (t nil)))) ; call paragraph-fill
17817 ;; For reference, this is the default value of adaptive-fill-regexp
17818 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
17820 (defun org-adaptive-fill-function ()
17821 "Return a fill prefix for org-mode files.
17822 In particular, this makes sure hanging paragraphs for hand-formatted lists
17823 work correctly."
17824 (cond
17825 ;; Comment line
17826 ((looking-at "#[ \t]+")
17827 (match-string-no-properties 0))
17828 ;; Description list
17829 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
17830 (save-excursion
17831 (if (> (match-end 1) (+ (match-beginning 1)
17832 org-description-max-indent))
17833 (goto-char (+ (match-beginning 1) 5))
17834 (goto-char (match-end 0)))
17835 (make-string (current-column) ?\ )))
17836 ;; Ordered or unordered list
17837 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
17838 (save-excursion
17839 (goto-char (match-end 0))
17840 (make-string (current-column) ?\ )))
17841 ;; Other text
17842 ((looking-at org-adaptive-fill-regexp-backup)
17843 (match-string-no-properties 0))))
17845 ;;; Other stuff.
17847 (defun org-toggle-fixed-width-section (arg)
17848 "Toggle the fixed-width export.
17849 If there is no active region, the QUOTE keyword at the current headline is
17850 inserted or removed. When present, it causes the text between this headline
17851 and the next to be exported as fixed-width text, and unmodified.
17852 If there is an active region, this command adds or removes a colon as the
17853 first character of this line. If the first character of a line is a colon,
17854 this line is also exported in fixed-width font."
17855 (interactive "P")
17856 (let* ((cc 0)
17857 (regionp (org-region-active-p))
17858 (beg (if regionp (region-beginning) (point)))
17859 (end (if regionp (region-end)))
17860 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
17861 (case-fold-search nil)
17862 (re "[ \t]*\\(: \\)")
17863 off)
17864 (if regionp
17865 (save-excursion
17866 (goto-char beg)
17867 (setq cc (current-column))
17868 (beginning-of-line 1)
17869 (setq off (looking-at re))
17870 (while (> nlines 0)
17871 (setq nlines (1- nlines))
17872 (beginning-of-line 1)
17873 (cond
17874 (arg
17875 (org-move-to-column cc t)
17876 (insert ": \n")
17877 (forward-line -1))
17878 ((and off (looking-at re))
17879 (replace-match "" t t nil 1))
17880 ((not off) (org-move-to-column cc t) (insert ": ")))
17881 (forward-line 1)))
17882 (save-excursion
17883 (org-back-to-heading)
17884 (if (looking-at (concat outline-regexp
17885 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
17886 (replace-match "" t t nil 1)
17887 (if (looking-at outline-regexp)
17888 (progn
17889 (goto-char (match-end 0))
17890 (insert org-quote-string " "))))))))
17892 (defun org-reftex-citation ()
17893 "Use reftex-citation to insert a citation into the buffer.
17894 This looks for a line like
17896 #+BIBLIOGRAPHY: foo plain option:-d
17898 and derives from it that foo.bib is the bibliography file relevant
17899 for this document. It then installs the necessary environment for RefTeX
17900 to work in this buffer and calls `reftex-citation' to insert a citation
17901 into the buffer.
17903 Export of such citations to both LaTeX and HTML is handled by the contributed
17904 package org-exp-bibtex by Taru Karttunen."
17905 (interactive)
17906 (let ((reftex-docstruct-symbol 'rds)
17907 (reftex-cite-format "\\cite{%l}")
17908 rds bib)
17909 (save-excursion
17910 (save-restriction
17911 (widen)
17912 (let ((case-fold-search t)
17913 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
17914 (if (not (save-excursion
17915 (or (re-search-forward re nil t)
17916 (re-search-backward re nil t))))
17917 (error "No bibliography defined in file")
17918 (setq bib (concat (match-string 1) ".bib")
17919 rds (list (list 'bib bib)))))))
17920 (call-interactively 'reftex-citation)))
17922 ;;;; Functions extending outline functionality
17924 (defun org-beginning-of-line (&optional arg)
17925 "Go to the beginning of the current line. If that is invisible, continue
17926 to a visible line beginning. This makes the function of C-a more intuitive.
17927 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
17928 first attempt, and only move to after the tags when the cursor is already
17929 beyond the end of the headline."
17930 (interactive "P")
17931 (let ((pos (point))
17932 (special (if (consp org-special-ctrl-a/e)
17933 (car org-special-ctrl-a/e)
17934 org-special-ctrl-a/e))
17935 refpos)
17936 (if (org-bound-and-true-p line-move-visual)
17937 (beginning-of-visual-line 1)
17938 (beginning-of-line 1))
17939 (if (and arg (fboundp 'move-beginning-of-line))
17940 (call-interactively 'move-beginning-of-line)
17941 (if (bobp)
17943 (backward-char 1)
17944 (if (org-invisible-p)
17945 (while (and (not (bobp)) (org-invisible-p))
17946 (backward-char 1)
17947 (beginning-of-line 1))
17948 (forward-char 1))))
17949 (when special
17950 (cond
17951 ((and (looking-at org-complex-heading-regexp)
17952 (= (char-after (match-end 1)) ?\ ))
17953 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
17954 (point-at-eol)))
17955 (goto-char
17956 (if (eq special t)
17957 (cond ((> pos refpos) refpos)
17958 ((= pos (point)) refpos)
17959 (t (point)))
17960 (cond ((> pos (point)) (point))
17961 ((not (eq last-command this-command)) (point))
17962 (t refpos)))))
17963 ((org-at-item-p)
17964 (goto-char
17965 (if (eq special t)
17966 (cond ((> pos (match-end 4)) (match-end 4))
17967 ((= pos (point)) (match-end 4))
17968 (t (point)))
17969 (cond ((> pos (point)) (point))
17970 ((not (eq last-command this-command)) (point))
17971 (t (match-end 4))))))))
17972 (org-no-warnings
17973 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
17975 (defun org-end-of-line (&optional arg)
17976 "Go to the end of the line.
17977 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
17978 first attempt, and only move to after the tags when the cursor is already
17979 beyond the end of the headline."
17980 (interactive "P")
17981 (let ((special (if (consp org-special-ctrl-a/e)
17982 (cdr org-special-ctrl-a/e)
17983 org-special-ctrl-a/e)))
17984 (if (or (not special)
17985 (not (org-on-heading-p))
17986 arg)
17987 (call-interactively
17988 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
17989 ((fboundp 'move-end-of-line) 'move-end-of-line)
17990 (t 'end-of-line)))
17991 (let ((pos (point)))
17992 (beginning-of-line 1)
17993 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
17994 (if (eq special t)
17995 (if (or (< pos (match-beginning 1))
17996 (= pos (match-end 0)))
17997 (goto-char (match-beginning 1))
17998 (goto-char (match-end 0)))
17999 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
18000 (goto-char (match-end 0))
18001 (goto-char (match-beginning 1))))
18002 (call-interactively (if (fboundp 'move-end-of-line)
18003 'move-end-of-line
18004 'end-of-line)))))
18005 (org-no-warnings
18006 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
18008 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
18009 (define-key org-mode-map "\C-e" 'org-end-of-line)
18010 (define-key org-mode-map [home] 'org-beginning-of-line)
18011 (define-key org-mode-map [end] 'org-end-of-line)
18013 (defun org-backward-sentence (&optional arg)
18014 "Go to beginning of sentence, or beginning of table field.
18015 This will call `backward-sentence' or `org-table-beginning-of-field',
18016 depending on context."
18017 (interactive "P")
18018 (cond
18019 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
18020 (t (call-interactively 'backward-sentence))))
18022 (defun org-forward-sentence (&optional arg)
18023 "Go to end of sentence, or end of table field.
18024 This will call `forward-sentence' or `org-table-end-of-field',
18025 depending on context."
18026 (interactive "P")
18027 (cond
18028 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
18029 (t (call-interactively 'forward-sentence))))
18031 (define-key org-mode-map "\M-a" 'org-backward-sentence)
18032 (define-key org-mode-map "\M-e" 'org-forward-sentence)
18034 (defun org-kill-line (&optional arg)
18035 "Kill line, to tags or end of line."
18036 (interactive "P")
18037 (cond
18038 ((or (not org-special-ctrl-k)
18039 (bolp)
18040 (not (org-on-heading-p)))
18041 (call-interactively 'kill-line))
18042 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
18043 (kill-region (point) (match-beginning 1))
18044 (org-set-tags nil t))
18045 (t (kill-region (point) (point-at-eol)))))
18047 (define-key org-mode-map "\C-k" 'org-kill-line)
18049 (defun org-yank (&optional arg)
18050 "Yank. If the kill is a subtree, treat it specially.
18051 This command will look at the current kill and check if is a single
18052 subtree, or a series of subtrees[1]. If it passes the test, and if the
18053 cursor is at the beginning of a line or after the stars of a currently
18054 empty headline, then the yank is handled specially. How exactly depends
18055 on the value of the following variables, both set by default.
18057 org-yank-folded-subtrees
18058 When set, the subtree(s) will be folded after insertion, but only
18059 if doing so would now swallow text after the yanked text.
18061 org-yank-adjusted-subtrees
18062 When set, the subtree will be promoted or demoted in order to
18063 fit into the local outline tree structure, which means that the level
18064 will be adjusted so that it becomes the smaller one of the two
18065 *visible* surrounding headings.
18067 Any prefix to this command will cause `yank' to be called directly with
18068 no special treatment. In particular, a simple `C-u' prefix will just
18069 plainly yank the text as it is.
18071 \[1] The test checks if the first non-white line is a heading
18072 and if there are no other headings with fewer stars."
18073 (interactive "P")
18074 (org-yank-generic 'yank arg))
18076 (defun org-yank-generic (command arg)
18077 "Perform some yank-like command.
18079 This function implements the behavior described in the `org-yank'
18080 documentation. However, it has been generalized to work for any
18081 interactive command with similar behavior."
18083 ;; pretend to be command COMMAND
18084 (setq this-command command)
18086 (if arg
18087 (call-interactively command)
18089 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
18090 (and (org-kill-is-subtree-p)
18091 (or (bolp)
18092 (and (looking-at "[ \t]*$")
18093 (string-match
18094 "\\`\\*+\\'"
18095 (buffer-substring (point-at-bol) (point)))))))
18096 swallowp)
18097 (cond
18098 ((and subtreep org-yank-folded-subtrees)
18099 (let ((beg (point))
18100 end)
18101 (if (and subtreep org-yank-adjusted-subtrees)
18102 (org-paste-subtree nil nil 'for-yank)
18103 (call-interactively command))
18105 (setq end (point))
18106 (goto-char beg)
18107 (when (and (bolp) subtreep
18108 (not (setq swallowp
18109 (org-yank-folding-would-swallow-text beg end))))
18110 (or (looking-at outline-regexp)
18111 (re-search-forward (concat "^" outline-regexp) end t))
18112 (while (and (< (point) end) (looking-at outline-regexp))
18113 (hide-subtree)
18114 (org-cycle-show-empty-lines 'folded)
18115 (condition-case nil
18116 (outline-forward-same-level 1)
18117 (error (goto-char end)))))
18118 (when swallowp
18119 (message
18120 "Inserted text not folded because that would swallow text"))
18122 (goto-char end)
18123 (skip-chars-forward " \t\n\r")
18124 (beginning-of-line 1)
18125 (push-mark beg 'nomsg)))
18126 ((and subtreep org-yank-adjusted-subtrees)
18127 (let ((beg (point-at-bol)))
18128 (org-paste-subtree nil nil 'for-yank)
18129 (push-mark beg 'nomsg)))
18131 (call-interactively command))))))
18133 (defun org-yank-folding-would-swallow-text (beg end)
18134 "Would hide-subtree at BEG swallow any text after END?"
18135 (let (level)
18136 (save-excursion
18137 (goto-char beg)
18138 (when (or (looking-at outline-regexp)
18139 (re-search-forward (concat "^" outline-regexp) end t))
18140 (setq level (org-outline-level)))
18141 (goto-char end)
18142 (skip-chars-forward " \t\r\n\v\f")
18143 (if (or (eobp)
18144 (and (bolp) (looking-at org-outline-regexp)
18145 (<= (org-outline-level) level)))
18146 nil ; Nothing would be swallowed
18147 t)))) ; something would swallow
18149 (define-key org-mode-map "\C-y" 'org-yank)
18151 (defun org-invisible-p ()
18152 "Check if point is at a character currently not visible."
18153 ;; Early versions of noutline don't have `outline-invisible-p'.
18154 (if (fboundp 'outline-invisible-p)
18155 (outline-invisible-p)
18156 (get-char-property (point) 'invisible)))
18158 (defun org-invisible-p2 ()
18159 "Check if point is at a character currently not visible."
18160 (save-excursion
18161 (if (and (eolp) (not (bobp))) (backward-char 1))
18162 ;; Early versions of noutline don't have `outline-invisible-p'.
18163 (if (fboundp 'outline-invisible-p)
18164 (outline-invisible-p)
18165 (get-char-property (point) 'invisible))))
18167 (defun org-back-to-heading (&optional invisible-ok)
18168 "Call `outline-back-to-heading', but provide a better error message."
18169 (condition-case nil
18170 (outline-back-to-heading invisible-ok)
18171 (error (error "Before first headline at position %d in buffer %s"
18172 (point) (current-buffer)))))
18174 (defun org-before-first-heading-p ()
18175 "Before first heading?"
18176 (save-excursion
18177 (null (re-search-backward "^\\*+ " nil t))))
18179 (defun org-on-heading-p (&optional ignored)
18180 (outline-on-heading-p t))
18181 (defun org-at-heading-p (&optional ignored)
18182 (outline-on-heading-p t))
18184 (defun org-point-at-end-of-empty-headline ()
18185 "If point is at the end of an empty headline, return t, else nil.
18186 If the heading only contains a TODO keyword, it is still still considered
18187 empty."
18188 (and (looking-at "[ \t]*$")
18189 (save-excursion
18190 (beginning-of-line 1)
18191 (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
18192 "\\)?[ \t]*$")))))
18193 (defun org-at-heading-or-item-p ()
18194 (or (org-on-heading-p) (org-at-item-p)))
18196 (defun org-on-target-p ()
18197 (or (org-in-regexp org-radio-target-regexp)
18198 (org-in-regexp org-target-regexp)))
18200 (defun org-up-heading-all (arg)
18201 "Move to the heading line of which the present line is a subheading.
18202 This function considers both visible and invisible heading lines.
18203 With argument, move up ARG levels."
18204 (if (fboundp 'outline-up-heading-all)
18205 (outline-up-heading-all arg) ; emacs 21 version of outline.el
18206 (outline-up-heading arg t))) ; emacs 22 version of outline.el
18208 (defun org-up-heading-safe ()
18209 "Move to the heading line of which the present line is a subheading.
18210 This version will not throw an error. It will return the level of the
18211 headline found, or nil if no higher level is found.
18213 Also, this function will be a lot faster than `outline-up-heading',
18214 because it relies on stars being the outline starters. This can really
18215 make a significant difference in outlines with very many siblings."
18216 (let (start-level re)
18217 (org-back-to-heading t)
18218 (setq start-level (funcall outline-level))
18219 (if (equal start-level 1)
18221 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
18222 (if (re-search-backward re nil t)
18223 (funcall outline-level)))))
18225 (defun org-first-sibling-p ()
18226 "Is this heading the first child of its parents?"
18227 (interactive)
18228 (let ((re (concat "^" outline-regexp))
18229 level l)
18230 (unless (org-at-heading-p t)
18231 (error "Not at a heading"))
18232 (setq level (funcall outline-level))
18233 (save-excursion
18234 (if (not (re-search-backward re nil t))
18236 (setq l (funcall outline-level))
18237 (< l level)))))
18239 (defun org-goto-sibling (&optional previous)
18240 "Goto the next sibling, even if it is invisible.
18241 When PREVIOUS is set, go to the previous sibling instead. Returns t
18242 when a sibling was found. When none is found, return nil and don't
18243 move point."
18244 (let ((fun (if previous 're-search-backward 're-search-forward))
18245 (pos (point))
18246 (re (concat "^" outline-regexp))
18247 level l)
18248 (when (condition-case nil (org-back-to-heading t) (error nil))
18249 (setq level (funcall outline-level))
18250 (catch 'exit
18251 (or previous (forward-char 1))
18252 (while (funcall fun re nil t)
18253 (setq l (funcall outline-level))
18254 (when (< l level) (goto-char pos) (throw 'exit nil))
18255 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
18256 (goto-char pos)
18257 nil))))
18259 (defun org-show-siblings ()
18260 "Show all siblings of the current headline."
18261 (save-excursion
18262 (while (org-goto-sibling) (org-flag-heading nil)))
18263 (save-excursion
18264 (while (org-goto-sibling 'previous)
18265 (org-flag-heading nil))))
18267 (defun org-show-hidden-entry ()
18268 "Show an entry where even the heading is hidden."
18269 (save-excursion
18270 (org-show-entry)))
18272 (defun org-flag-heading (flag &optional entry)
18273 "Flag the current heading. FLAG non-nil means make invisible.
18274 When ENTRY is non-nil, show the entire entry."
18275 (save-excursion
18276 (org-back-to-heading t)
18277 ;; Check if we should show the entire entry
18278 (if entry
18279 (progn
18280 (org-show-entry)
18281 (save-excursion
18282 (and (outline-next-heading)
18283 (org-flag-heading nil))))
18284 (outline-flag-region (max (point-min) (1- (point)))
18285 (save-excursion (outline-end-of-heading) (point))
18286 flag))))
18288 (defun org-get-next-sibling ()
18289 "Move to next heading of the same level, and return point.
18290 If there is no such heading, return nil.
18291 This is like outline-next-sibling, but invisible headings are ok."
18292 (let ((level (funcall outline-level)))
18293 (outline-next-heading)
18294 (while (and (not (eobp)) (> (funcall outline-level) level))
18295 (outline-next-heading))
18296 (if (or (eobp) (< (funcall outline-level) level))
18298 (point))))
18300 (defun org-get-last-sibling ()
18301 "Move to previous heading of the same level, and return point.
18302 If there is no such heading, return nil."
18303 (let ((opoint (point))
18304 (level (funcall outline-level)))
18305 (outline-previous-heading)
18306 (when (and (/= (point) opoint) (outline-on-heading-p t))
18307 (while (and (> (funcall outline-level) level)
18308 (not (bobp)))
18309 (outline-previous-heading))
18310 (if (< (funcall outline-level) level)
18312 (point)))))
18314 (defun org-end-of-subtree (&optional invisible-OK to-heading)
18315 ;; This contains an exact copy of the original function, but it uses
18316 ;; `org-back-to-heading', to make it work also in invisible
18317 ;; trees. And is uses an invisible-OK argument.
18318 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
18319 ;; Furthermore, when used inside Org, finding the end of a large subtree
18320 ;; with many children and grandchildren etc, this can be much faster
18321 ;; than the outline version.
18322 (org-back-to-heading invisible-OK)
18323 (let ((first t)
18324 (level (funcall outline-level)))
18325 (if (and (org-mode-p) (< level 1000))
18326 ;; A true heading (not a plain list item), in Org-mode
18327 ;; This means we can easily find the end by looking
18328 ;; only for the right number of stars. Using a regexp to do
18329 ;; this is so much faster than using a Lisp loop.
18330 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
18331 (forward-char 1)
18332 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
18333 ;; something else, do it the slow way
18334 (while (and (not (eobp))
18335 (or first (> (funcall outline-level) level)))
18336 (setq first nil)
18337 (outline-next-heading)))
18338 (unless to-heading
18339 (if (memq (preceding-char) '(?\n ?\^M))
18340 (progn
18341 ;; Go to end of line before heading
18342 (forward-char -1)
18343 (if (memq (preceding-char) '(?\n ?\^M))
18344 ;; leave blank line before heading
18345 (forward-char -1))))))
18346 (point))
18348 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
18349 "Use Org version in org-mode, for dramatic speed-up."
18350 (if (eq major-mode 'org-mode)
18351 (progn
18352 (org-end-of-subtree nil t)
18353 (unless (eobp) (backward-char 1)))
18354 ad-do-it))
18356 (defun org-forward-same-level (arg &optional invisible-ok)
18357 "Move forward to the arg'th subheading at same level as this one.
18358 Stop at the first and last subheadings of a superior heading."
18359 (interactive "p")
18360 (org-back-to-heading invisible-ok)
18361 (org-on-heading-p)
18362 (let* ((level (- (match-end 0) (match-beginning 0) 1))
18363 (re (format "^\\*\\{1,%d\\} " level))
18365 (forward-char 1)
18366 (while (> arg 0)
18367 (while (and (re-search-forward re nil 'move)
18368 (setq l (- (match-end 0) (match-beginning 0) 1))
18369 (= l level)
18370 (not invisible-ok)
18371 (progn (backward-char 1) (org-invisible-p)))
18372 (if (< l level) (setq arg 1)))
18373 (setq arg (1- arg)))
18374 (beginning-of-line 1)))
18376 (defun org-backward-same-level (arg &optional invisible-ok)
18377 "Move backward to the arg'th subheading at same level as this one.
18378 Stop at the first and last subheadings of a superior heading."
18379 (interactive "p")
18380 (org-back-to-heading)
18381 (org-on-heading-p)
18382 (let* ((level (- (match-end 0) (match-beginning 0) 1))
18383 (re (format "^\\*\\{1,%d\\} " level))
18385 (while (> arg 0)
18386 (while (and (re-search-backward re nil 'move)
18387 (setq l (- (match-end 0) (match-beginning 0) 1))
18388 (= l level)
18389 (not invisible-ok)
18390 (org-invisible-p))
18391 (if (< l level) (setq arg 1)))
18392 (setq arg (1- arg)))))
18394 (defun org-show-subtree ()
18395 "Show everything after this heading at deeper levels."
18396 (outline-flag-region
18397 (point)
18398 (save-excursion
18399 (org-end-of-subtree t t))
18400 nil))
18402 (defun org-show-entry ()
18403 "Show the body directly following this heading.
18404 Show the heading too, if it is currently invisible."
18405 (interactive)
18406 (save-excursion
18407 (condition-case nil
18408 (progn
18409 (org-back-to-heading t)
18410 (outline-flag-region
18411 (max (point-min) (1- (point)))
18412 (save-excursion
18413 (if (re-search-forward
18414 (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
18415 (match-beginning 1)
18416 (point-max)))
18417 nil)
18418 (org-cycle-hide-drawers 'children))
18419 (error nil))))
18421 (defun org-make-options-regexp (kwds &optional extra)
18422 "Make a regular expression for keyword lines."
18423 (concat
18425 "#?[ \t]*\\+\\("
18426 (mapconcat 'regexp-quote kwds "\\|")
18427 (if extra (concat "\\|" extra))
18428 "\\):[ \t]*"
18429 "\\(.*\\)"))
18431 ;; Make isearch reveal the necessary context
18432 (defun org-isearch-end ()
18433 "Reveal context after isearch exits."
18434 (when isearch-success ; only if search was successful
18435 (if (featurep 'xemacs)
18436 ;; Under XEmacs, the hook is run in the correct place,
18437 ;; we directly show the context.
18438 (org-show-context 'isearch)
18439 ;; In Emacs the hook runs *before* restoring the overlays.
18440 ;; So we have to use a one-time post-command-hook to do this.
18441 ;; (Emacs 22 has a special variable, see function `org-mode')
18442 (unless (and (boundp 'isearch-mode-end-hook-quit)
18443 isearch-mode-end-hook-quit)
18444 ;; Only when the isearch was not quitted.
18445 (org-add-hook 'post-command-hook 'org-isearch-post-command
18446 'append 'local)))))
18448 (defun org-isearch-post-command ()
18449 "Remove self from hook, and show context."
18450 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
18451 (org-show-context 'isearch))
18454 ;;;; Integration with and fixes for other packages
18456 ;;; Imenu support
18458 (defvar org-imenu-markers nil
18459 "All markers currently used by Imenu.")
18460 (make-variable-buffer-local 'org-imenu-markers)
18462 (defun org-imenu-new-marker (&optional pos)
18463 "Return a new marker for use by Imenu, and remember the marker."
18464 (let ((m (make-marker)))
18465 (move-marker m (or pos (point)))
18466 (push m org-imenu-markers)
18469 (defun org-imenu-get-tree ()
18470 "Produce the index for Imenu."
18471 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
18472 (setq org-imenu-markers nil)
18473 (let* ((n org-imenu-depth)
18474 (re (concat "^" outline-regexp))
18475 (subs (make-vector (1+ n) nil))
18476 (last-level 0)
18477 m level head)
18478 (save-excursion
18479 (save-restriction
18480 (widen)
18481 (goto-char (point-max))
18482 (while (re-search-backward re nil t)
18483 (setq level (org-reduced-level (funcall outline-level)))
18484 (when (<= level n)
18485 (looking-at org-complex-heading-regexp)
18486 (setq head (org-link-display-format
18487 (org-match-string-no-properties 4))
18488 m (org-imenu-new-marker))
18489 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
18490 (if (>= level last-level)
18491 (push (cons head m) (aref subs level))
18492 (push (cons head (aref subs (1+ level))) (aref subs level))
18493 (loop for i from (1+ level) to n do (aset subs i nil)))
18494 (setq last-level level)))))
18495 (aref subs 1)))
18497 (eval-after-load "imenu"
18498 '(progn
18499 (add-hook 'imenu-after-jump-hook
18500 (lambda ()
18501 (if (eq major-mode 'org-mode)
18502 (org-show-context 'org-goto))))))
18504 (defun org-link-display-format (link)
18505 "Replace a link with either the description, or the link target
18506 if no description is present"
18507 (save-match-data
18508 (if (string-match org-bracket-link-analytic-regexp link)
18509 (replace-match (if (match-end 5)
18510 (match-string 5 link)
18511 (concat (match-string 1 link)
18512 (match-string 3 link)))
18513 nil t link)
18514 link)))
18516 ;; Speedbar support
18518 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
18519 "Overlay marking the agenda restriction line in speedbar.")
18520 (overlay-put org-speedbar-restriction-lock-overlay
18521 'face 'org-agenda-restriction-lock)
18522 (overlay-put org-speedbar-restriction-lock-overlay
18523 'help-echo "Agendas are currently limited to this item.")
18524 (org-detach-overlay org-speedbar-restriction-lock-overlay)
18526 (defun org-speedbar-set-agenda-restriction ()
18527 "Restrict future agenda commands to the location at point in speedbar.
18528 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
18529 (interactive)
18530 (require 'org-agenda)
18531 (let (p m tp np dir txt)
18532 (cond
18533 ((setq p (text-property-any (point-at-bol) (point-at-eol)
18534 'org-imenu t))
18535 (setq m (get-text-property p 'org-imenu-marker))
18536 (with-current-buffer (marker-buffer m)
18537 (goto-char m)
18538 (org-agenda-set-restriction-lock 'subtree)))
18539 ((setq p (text-property-any (point-at-bol) (point-at-eol)
18540 'speedbar-function 'speedbar-find-file))
18541 (setq tp (previous-single-property-change
18542 (1+ p) 'speedbar-function)
18543 np (next-single-property-change
18544 tp 'speedbar-function)
18545 dir (speedbar-line-directory)
18546 txt (buffer-substring-no-properties (or tp (point-min))
18547 (or np (point-max))))
18548 (with-current-buffer (find-file-noselect
18549 (let ((default-directory dir))
18550 (expand-file-name txt)))
18551 (unless (org-mode-p)
18552 (error "Cannot restrict to non-Org-mode file"))
18553 (org-agenda-set-restriction-lock 'file)))
18554 (t (error "Don't know how to restrict Org-mode's agenda")))
18555 (move-overlay org-speedbar-restriction-lock-overlay
18556 (point-at-bol) (point-at-eol))
18557 (setq current-prefix-arg nil)
18558 (org-agenda-maybe-redo)))
18560 (eval-after-load "speedbar"
18561 '(progn
18562 (speedbar-add-supported-extension ".org")
18563 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
18564 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
18565 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
18566 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
18567 (add-hook 'speedbar-visiting-tag-hook
18568 (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
18571 ;;; Fixes and Hacks for problems with other packages
18573 ;; Make flyspell not check words in links, to not mess up our keymap
18574 (defun org-mode-flyspell-verify ()
18575 "Don't let flyspell put overlays at active buttons."
18576 (and (not (get-text-property (point) 'keymap))
18577 (not (get-text-property (point) 'org-no-flyspell))))
18579 (defun org-remove-flyspell-overlays-in (beg end)
18580 "Remove flyspell overlays in region."
18581 (and (org-bound-and-true-p flyspell-mode)
18582 (fboundp 'flyspell-delete-region-overlays)
18583 (flyspell-delete-region-overlays beg end))
18584 (add-text-properties beg end '(org-no-flyspell t)))
18586 ;; Make `bookmark-jump' shows the jump location if it was hidden.
18587 (eval-after-load "bookmark"
18588 '(if (boundp 'bookmark-after-jump-hook)
18589 ;; We can use the hook
18590 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
18591 ;; Hook not available, use advice
18592 (defadvice bookmark-jump (after org-make-visible activate)
18593 "Make the position visible."
18594 (org-bookmark-jump-unhide))))
18596 ;; Make sure saveplace shows the location if it was hidden
18597 (eval-after-load "saveplace"
18598 '(defadvice save-place-find-file-hook (after org-make-visible activate)
18599 "Make the position visible."
18600 (org-bookmark-jump-unhide)))
18602 ;; Make sure ecb shows the location if it was hidden
18603 (eval-after-load "ecb"
18604 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
18605 "Make hierarchy visible when jumping into location from ECB tree buffer."
18606 (if (eq major-mode 'org-mode)
18607 (org-show-context))))
18609 (defun org-bookmark-jump-unhide ()
18610 "Unhide the current position, to show the bookmark location."
18611 (and (org-mode-p)
18612 (or (org-invisible-p)
18613 (save-excursion (goto-char (max (point-min) (1- (point))))
18614 (org-invisible-p)))
18615 (org-show-context 'bookmark-jump)))
18617 ;; Make session.el ignore our circular variable
18618 (eval-after-load "session"
18619 '(add-to-list 'session-globals-exclude 'org-mark-ring))
18621 ;;;; Experimental code
18623 (defun org-closed-in-range ()
18624 "Sparse tree of items closed in a certain time range.
18625 Still experimental, may disappear in the future."
18626 (interactive)
18627 ;; Get the time interval from the user.
18628 (let* ((time1 (org-float-time
18629 (org-read-date nil 'to-time nil "Starting date: ")))
18630 (time2 (org-float-time
18631 (org-read-date nil 'to-time nil "End date:")))
18632 ;; callback function
18633 (callback (lambda ()
18634 (let ((time
18635 (org-float-time
18636 (apply 'encode-time
18637 (org-parse-time-string
18638 (match-string 1))))))
18639 ;; check if time in interval
18640 (and (>= time time1) (<= time time2))))))
18641 ;; make tree, check each match with the callback
18642 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
18644 ;;;; Finish up
18646 (provide 'org)
18648 (run-hooks 'org-load-hook)
18650 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
18652 ;;; org.el ends here