No linger bind obsolete calendar variables
[org-mode.git] / lisp / org.el
blobba1fd0788e7bc383707cbfcfedcb1f075380f9be
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 (if org-replace-disputed-keys
422 (let* ((nkey (key-description key))
423 (x (org-find-if (lambda (x)
424 (equal (key-description (car x)) nkey))
425 org-disputed-keys)))
426 (if x (cdr x) key))
427 key))
429 (defun org-find-if (predicate seq)
430 (catch 'exit
431 (while seq
432 (if (funcall predicate (car seq))
433 (throw 'exit (car seq))
434 (pop seq)))))
436 (defun org-defkey (keymap key def)
437 "Define a key, possibly translated, as returned by `org-key'."
438 (define-key keymap (org-key key) def))
440 (defcustom org-ellipsis nil
441 "The ellipsis to use in the Org-mode outline.
442 When nil, just use the standard three dots. When a string, use that instead,
443 When a face, use the standard 3 dots, but with the specified face.
444 The change affects only Org-mode (which will then use its own display table).
445 Changing this requires executing `M-x org-mode' in a buffer to become
446 effective."
447 :group 'org-startup
448 :type '(choice (const :tag "Default" nil)
449 (face :tag "Face" :value org-warning)
450 (string :tag "String" :value "...#")))
452 (defvar org-display-table nil
453 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
455 (defgroup org-keywords nil
456 "Keywords in Org-mode."
457 :tag "Org Keywords"
458 :group 'org)
460 (defcustom org-deadline-string "DEADLINE:"
461 "String to mark deadline entries.
462 A deadline is this string, followed by a time stamp. Should be a word,
463 terminated by a colon. You can insert a schedule keyword and
464 a timestamp with \\[org-deadline].
465 Changes become only effective after restarting Emacs."
466 :group 'org-keywords
467 :type 'string)
469 (defcustom org-scheduled-string "SCHEDULED:"
470 "String to mark scheduled TODO entries.
471 A schedule is this string, followed by a time stamp. Should be a word,
472 terminated by a colon. You can insert a schedule keyword and
473 a timestamp with \\[org-schedule].
474 Changes become only effective after restarting Emacs."
475 :group 'org-keywords
476 :type 'string)
478 (defcustom org-closed-string "CLOSED:"
479 "String used as the prefix for timestamps logging closing a TODO entry."
480 :group 'org-keywords
481 :type 'string)
483 (defcustom org-clock-string "CLOCK:"
484 "String used as prefix for timestamps clocking work hours on an item."
485 :group 'org-keywords
486 :type 'string)
488 (defcustom org-comment-string "COMMENT"
489 "Entries starting with this keyword will never be exported.
490 An entry can be toggled between COMMENT and normal with
491 \\[org-toggle-comment].
492 Changes become only effective after restarting Emacs."
493 :group 'org-keywords
494 :type 'string)
496 (defcustom org-quote-string "QUOTE"
497 "Entries starting with this keyword will be exported in fixed-width font.
498 Quoting applies only to the text in the entry following the headline, and does
499 not extend beyond the next headline, even if that is lower level.
500 An entry can be toggled between QUOTE and normal with
501 \\[org-toggle-fixed-width-section]."
502 :group 'org-keywords
503 :type 'string)
505 (defconst org-repeat-re
506 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
507 "Regular expression for specifying repeated events.
508 After a match, group 1 contains the repeat expression.")
510 (defgroup org-structure nil
511 "Options concerning the general structure of Org-mode files."
512 :tag "Org Structure"
513 :group 'org)
515 (defgroup org-reveal-location nil
516 "Options about how to make context of a location visible."
517 :tag "Org Reveal Location"
518 :group 'org-structure)
520 (defconst org-context-choice
521 '(choice
522 (const :tag "Always" t)
523 (const :tag "Never" nil)
524 (repeat :greedy t :tag "Individual contexts"
525 (cons
526 (choice :tag "Context"
527 (const agenda)
528 (const org-goto)
529 (const occur-tree)
530 (const tags-tree)
531 (const link-search)
532 (const mark-goto)
533 (const bookmark-jump)
534 (const isearch)
535 (const default))
536 (boolean))))
537 "Contexts for the reveal options.")
539 (defcustom org-show-hierarchy-above '((default . t))
540 "Non-nil means show full hierarchy when revealing a location.
541 Org-mode often shows locations in an org-mode file which might have
542 been invisible before. When this is set, the hierarchy of headings
543 above the exposed location is shown.
544 Turning this off for example for sparse trees makes them very compact.
545 Instead of t, this can also be an alist specifying this option for different
546 contexts. Valid contexts are
547 agenda when exposing an entry from the agenda
548 org-goto when using the command `org-goto' on key C-c C-j
549 occur-tree when using the command `org-occur' on key C-c /
550 tags-tree when constructing a sparse tree based on tags matches
551 link-search when exposing search matches associated with a link
552 mark-goto when exposing the jump goal of a mark
553 bookmark-jump when exposing a bookmark location
554 isearch when exiting from an incremental search
555 default default for all contexts not set explicitly"
556 :group 'org-reveal-location
557 :type org-context-choice)
559 (defcustom org-show-following-heading '((default . nil))
560 "Non-nil means show following heading when revealing a location.
561 Org-mode often shows locations in an org-mode file which might have
562 been invisible before. When this is set, the heading following the
563 match is shown.
564 Turning this off for example for sparse trees makes them very compact,
565 but makes it harder to edit the location of the match. In such a case,
566 use the command \\[org-reveal] to show more context.
567 Instead of t, this can also be an alist specifying this option for different
568 contexts. See `org-show-hierarchy-above' for valid contexts."
569 :group 'org-reveal-location
570 :type org-context-choice)
572 (defcustom org-show-siblings '((default . nil) (isearch t))
573 "Non-nil means show all sibling heading when revealing a location.
574 Org-mode often shows locations in an org-mode file which might have
575 been invisible before. When this is set, the sibling of the current entry
576 heading are all made visible. If `org-show-hierarchy-above' is t,
577 the same happens on each level of the hierarchy above the current entry.
579 By default this is on for the isearch context, off for all other contexts.
580 Turning this off for example for sparse trees makes them very compact,
581 but makes it harder to edit the location of the match. In such a case,
582 use the command \\[org-reveal] to show more context.
583 Instead of t, this can also be an alist specifying this option for different
584 contexts. See `org-show-hierarchy-above' for valid contexts."
585 :group 'org-reveal-location
586 :type org-context-choice)
588 (defcustom org-show-entry-below '((default . nil))
589 "Non-nil means show the entry below a headline when revealing a location.
590 Org-mode often shows locations in an org-mode file which might have
591 been invisible before. When this is set, the text below the headline that is
592 exposed is also shown.
594 By default this is off for all contexts.
595 Instead of t, this can also be an alist specifying this option for different
596 contexts. See `org-show-hierarchy-above' for valid contexts."
597 :group 'org-reveal-location
598 :type org-context-choice)
600 (defcustom org-indirect-buffer-display 'other-window
601 "How should indirect tree buffers be displayed?
602 This applies to indirect buffers created with the commands
603 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
604 Valid values are:
605 current-window Display in the current window
606 other-window Just display in another window.
607 dedicated-frame Create one new frame, and re-use it each time.
608 new-frame Make a new frame each time. Note that in this case
609 previously-made indirect buffers are kept, and you need to
610 kill these buffers yourself."
611 :group 'org-structure
612 :group 'org-agenda-windows
613 :type '(choice
614 (const :tag "In current window" current-window)
615 (const :tag "In current frame, other window" other-window)
616 (const :tag "Each time a new frame" new-frame)
617 (const :tag "One dedicated frame" dedicated-frame)))
619 (defcustom org-use-speed-commands nil
620 "Non-nil means activate single letter commands at beginning of a headline.
621 This may also be a function to test for appropriate locations where speed
622 commands should be active."
623 :group 'org-structure
624 :type '(choice
625 (const :tag "Never" nil)
626 (const :tag "At beginning of headline stars" t)
627 (function)))
629 (defcustom org-speed-commands-user nil
630 "Alist of additional speed commands.
631 This list will be checked before `org-speed-commands-default'
632 when the variable `org-use-speed-commands' is non-nil
633 and when the cursor is at the beginning of a headline.
634 The car if each entry is a string with a single letter, which must
635 be assigned to `self-insert-command' in the global map.
636 The cdr is either a command to be called interactively, a function
637 to be called, or a form to be evaluated.
638 An entry that is just a list with a single string will be interpreted
639 as a descriptive headline that will be added when listing the speed
640 copmmands in the Help buffer using the `?' speed command."
641 :group 'org-structure
642 :type '(repeat :value ("k" . ignore)
643 (choice :value ("k" . ignore)
644 (list :tag "Descriptive Headline" (string :tag "Headline"))
645 (cons :tag "Letter and Command"
646 (string :tag "Command letter")
647 (choice
648 (function)
649 (sexp))))))
651 (defgroup org-cycle nil
652 "Options concerning visibility cycling in Org-mode."
653 :tag "Org Cycle"
654 :group 'org-structure)
656 (defcustom org-cycle-skip-children-state-if-no-children t
657 "Non-nil means skip CHILDREN state in entries that don't have any."
658 :group 'org-cycle
659 :type 'boolean)
661 (defcustom org-cycle-max-level nil
662 "Maximum level which should still be subject to visibility cycling.
663 Levels higher than this will, for cycling, be treated as text, not a headline.
664 When `org-odd-levels-only' is set, a value of N in this variable actually
665 means 2N-1 stars as the limiting headline.
666 When nil, cycle all levels.
667 Note that the limiting level of cycling is also influenced by
668 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
669 `org-inlinetask-min-level' is, cycling will be limited to levels one less
670 than its value."
671 :group 'org-cycle
672 :type '(choice
673 (const :tag "No limit" nil)
674 (integer :tag "Maximum level")))
676 (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
677 "Names of drawers. Drawers are not opened by cycling on the headline above.
678 Drawers only open with a TAB on the drawer line itself. A drawer looks like
679 this:
680 :DRAWERNAME:
681 .....
682 :END:
683 The drawer \"PROPERTIES\" is special for capturing properties through
684 the property API.
686 Drawers can be defined on the per-file basis with a line like:
688 #+DRAWERS: HIDDEN STATE PROPERTIES"
689 :group 'org-structure
690 :group 'org-cycle
691 :type '(repeat (string :tag "Drawer Name")))
693 (defcustom org-hide-block-startup nil
694 "Non-nil means entering Org-mode will fold all blocks.
695 This can also be set in on a per-file basis with
697 #+STARTUP: hideblocks
698 #+STARTUP: showblocks"
699 :group 'org-startup
700 :group 'org-cycle
701 :type 'boolean)
703 (defcustom org-cycle-global-at-bob nil
704 "Cycle globally if cursor is at beginning of buffer and not at a headline.
705 This makes it possible to do global cycling without having to use S-TAB or
706 C-u TAB. For this special case to work, the first line of the buffer
707 must not be a headline - it may be empty or some other text. When used in
708 this way, `org-cycle-hook' is disables temporarily, to make sure the
709 cursor stays at the beginning of the buffer.
710 When this option is nil, don't do anything special at the beginning
711 of the buffer."
712 :group 'org-cycle
713 :type 'boolean)
715 (defcustom org-cycle-level-after-item/entry-creation t
716 "Non-nil means cycle entry level or item indentation in new empty entries.
718 When the cursor is at the end of an empty headline, i.e with only stars
719 and maybe a TODO keyword, TAB will then switch the entry to become a child,
720 and then all possible anchestor states, before returning to the original state.
721 This makes data entry extremely fast: M-RET to create a new headline,
722 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
724 When the cursor is at the end of an empty plain list item, one TAB will
725 make it a subitem, two or more tabs will back up to make this an item
726 higher up in the item hierarchy."
727 :group 'org-cycle
728 :type 'boolean)
730 (defcustom org-cycle-emulate-tab t
731 "Where should `org-cycle' emulate TAB.
732 nil Never
733 white Only in completely white lines
734 whitestart Only at the beginning of lines, before the first non-white char
735 t Everywhere except in headlines
736 exc-hl-bol Everywhere except at the start of a headline
737 If TAB is used in a place where it does not emulate TAB, the current subtree
738 visibility is cycled."
739 :group 'org-cycle
740 :type '(choice (const :tag "Never" nil)
741 (const :tag "Only in completely white lines" white)
742 (const :tag "Before first char in a line" whitestart)
743 (const :tag "Everywhere except in headlines" t)
744 (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
747 (defcustom org-cycle-separator-lines 2
748 "Number of empty lines needed to keep an empty line between collapsed trees.
749 If you leave an empty line between the end of a subtree and the following
750 headline, this empty line is hidden when the subtree is folded.
751 Org-mode will leave (exactly) one empty line visible if the number of
752 empty lines is equal or larger to the number given in this variable.
753 So the default 2 means at least 2 empty lines after the end of a subtree
754 are needed to produce free space between a collapsed subtree and the
755 following headline.
757 If the number is negative, and the number of empty lines is at least -N,
758 all empty lines are shown.
760 Special case: when 0, never leave empty lines in collapsed view."
761 :group 'org-cycle
762 :type 'integer)
763 (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
765 (defcustom org-pre-cycle-hook nil
766 "Hook that is run before visibility cycling is happening.
767 The function(s) in this hook must accept a single argument which indicates
768 the new state that will be set right after running this hook. The
769 argument is a symbol. Before a global state change, it can have the values
770 `overview', `content', or `all'. Before a local state change, it can have
771 the values `folded', `children', or `subtree'."
772 :group 'org-cycle
773 :type 'hook)
775 (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
776 org-cycle-hide-drawers
777 org-cycle-show-empty-lines
778 org-optimize-window-after-visibility-change)
779 "Hook that is run after `org-cycle' has changed the buffer visibility.
780 The function(s) in this hook must accept a single argument which indicates
781 the new state that was set by the most recent `org-cycle' command. The
782 argument is a symbol. After a global state change, it can have the values
783 `overview', `content', or `all'. After a local state change, it can have
784 the values `folded', `children', or `subtree'."
785 :group 'org-cycle
786 :type 'hook)
788 (defgroup org-edit-structure nil
789 "Options concerning structure editing in Org-mode."
790 :tag "Org Edit Structure"
791 :group 'org-structure)
793 (defcustom org-odd-levels-only nil
794 "Non-nil means skip even levels and only use odd levels for the outline.
795 This has the effect that two stars are being added/taken away in
796 promotion/demotion commands. It also influences how levels are
797 handled by the exporters.
798 Changing it requires restart of `font-lock-mode' to become effective
799 for fontification also in regions already fontified.
800 You may also set this on a per-file basis by adding one of the following
801 lines to the buffer:
803 #+STARTUP: odd
804 #+STARTUP: oddeven"
805 :group 'org-edit-structure
806 :group 'org-appearance
807 :type 'boolean)
809 (defcustom org-adapt-indentation t
810 "Non-nil means adapt indentation to outline node level.
812 When this variable is set, Org assumes that you write outlines by
813 indenting text in each node to align with the headline (after the stars).
814 The following issues are influenced by this variable:
816 - When this is set and the *entire* text in an entry is indented, the
817 indentation is increased by one space in a demotion command, and
818 decreased by one in a promotion command. If any line in the entry
819 body starts with text at column 0, indentation is not changed at all.
821 - Property drawers and planning information is inserted indented when
822 this variable s set. When nil, they will not be indented.
824 - TAB indents a line relative to context. The lines below a headline
825 will be indented when this variable is set.
827 Note that this is all about true indentation, by adding and removing
828 space characters. See also `org-indent.el' which does level-dependent
829 indentation in a virtual way, i.e. at display time in Emacs."
830 :group 'org-edit-structure
831 :type 'boolean)
833 (defcustom org-special-ctrl-a/e nil
834 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
836 When t, `C-a' will bring back the cursor to the beginning of the
837 headline text, i.e. after the stars and after a possible TODO keyword.
838 In an item, this will be the position after the bullet.
839 When the cursor is already at that position, another `C-a' will bring
840 it to the beginning of the line.
842 `C-e' will jump to the end of the headline, ignoring the presence of tags
843 in the headline. A second `C-e' will then jump to the true end of the
844 line, after any tags. This also means that, when this variable is
845 non-nil, `C-e' also will never jump beyond the end of the heading of a
846 folded section, i.e. not after the ellipses.
848 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
849 going to the true line boundary first. Only a directly following, identical
850 keypress will bring the cursor to the special positions.
852 This may also be a cons cell where the behavior for `C-a' and `C-e' is
853 set separately."
854 :group 'org-edit-structure
855 :type '(choice
856 (const :tag "off" nil)
857 (const :tag "on: after stars/bullet and before tags first" t)
858 (const :tag "reversed: true line boundary first" reversed)
859 (cons :tag "Set C-a and C-e separately"
860 (choice :tag "Special C-a"
861 (const :tag "off" nil)
862 (const :tag "on: after stars/bullet first" t)
863 (const :tag "reversed: before stars/bullet first" reversed))
864 (choice :tag "Special C-e"
865 (const :tag "off" nil)
866 (const :tag "on: before tags first" t)
867 (const :tag "reversed: after tags first" reversed)))))
868 (if (fboundp 'defvaralias)
869 (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
871 (defcustom org-special-ctrl-k nil
872 "Non-nil means `C-k' will behave specially in headlines.
873 When nil, `C-k' will call the default `kill-line' command.
874 When t, the following will happen while the cursor is in the headline:
876 - When the cursor is at the beginning of a headline, kill the entire
877 line and possible the folded subtree below the line.
878 - When in the middle of the headline text, kill the headline up to the tags.
879 - When after the headline text, kill the tags."
880 :group 'org-edit-structure
881 :type 'boolean)
883 (defcustom org-yank-folded-subtrees t
884 "Non-nil means when yanking subtrees, fold them.
885 If the kill is a single subtree, or a sequence of subtrees, i.e. if
886 it starts with a heading and all other headings in it are either children
887 or siblings, then fold all the subtrees. However, do this only if no
888 text after the yank would be swallowed into a folded tree by this action."
889 :group 'org-edit-structure
890 :type 'boolean)
892 (defcustom org-yank-adjusted-subtrees nil
893 "Non-nil means when yanking subtrees, adjust the level.
894 With this setting, `org-paste-subtree' is used to insert the subtree, see
895 this function for details."
896 :group 'org-edit-structure
897 :type 'boolean)
899 (defcustom org-M-RET-may-split-line '((default . t))
900 "Non-nil means M-RET will split the line at the cursor position.
901 When nil, it will go to the end of the line before making a
902 new line.
903 You may also set this option in a different way for different
904 contexts. Valid contexts are:
906 headline when creating a new headline
907 item when creating a new item
908 table in a table field
909 default the value to be used for all contexts not explicitly
910 customized"
911 :group 'org-structure
912 :group 'org-table
913 :type '(choice
914 (const :tag "Always" t)
915 (const :tag "Never" nil)
916 (repeat :greedy t :tag "Individual contexts"
917 (cons
918 (choice :tag "Context"
919 (const headline)
920 (const item)
921 (const table)
922 (const default))
923 (boolean)))))
926 (defcustom org-insert-heading-respect-content nil
927 "Non-nil means insert new headings after the current subtree.
928 When nil, the new heading is created directly after the current line.
929 The commands \\[org-insert-heading-respect-content] and
930 \\[org-insert-todo-heading-respect-content] turn this variable on
931 for the duration of the command."
932 :group 'org-structure
933 :type 'boolean)
935 (defcustom org-blank-before-new-entry '((heading . auto)
936 (plain-list-item . auto))
937 "Should `org-insert-heading' leave a blank line before new heading/item?
938 The value is an alist, with `heading' and `plain-list-item' as car,
939 and a boolean flag as cdr. For plain lists, if the variable
940 `org-empty-line-terminates-plain-lists' is set, the setting here
941 is ignored and no empty line is inserted, to keep the list in tact."
942 :group 'org-edit-structure
943 :type '(list
944 (cons (const heading)
945 (choice (const :tag "Never" nil)
946 (const :tag "Always" t)
947 (const :tag "Auto" auto)))
948 (cons (const plain-list-item)
949 (choice (const :tag "Never" nil)
950 (const :tag "Always" t)
951 (const :tag "Auto" auto)))))
953 (defcustom org-insert-heading-hook nil
954 "Hook being run after inserting a new heading."
955 :group 'org-edit-structure
956 :type 'hook)
958 (defcustom org-enable-fixed-width-editor t
959 "Non-nil means lines starting with \":\" are treated as fixed-width.
960 This currently only means they are never auto-wrapped.
961 When nil, such lines will be treated like ordinary lines.
962 See also the QUOTE keyword."
963 :group 'org-edit-structure
964 :type 'boolean)
967 (defcustom org-goto-auto-isearch t
968 "Non-nil means typing characters in org-goto starts incremental search."
969 :group 'org-edit-structure
970 :type 'boolean)
972 (defgroup org-sparse-trees nil
973 "Options concerning sparse trees in Org-mode."
974 :tag "Org Sparse Trees"
975 :group 'org-structure)
977 (defcustom org-highlight-sparse-tree-matches t
978 "Non-nil means highlight all matches that define a sparse tree.
979 The highlights will automatically disappear the next time the buffer is
980 changed by an edit command."
981 :group 'org-sparse-trees
982 :type 'boolean)
984 (defcustom org-remove-highlights-with-change t
985 "Non-nil means any change to the buffer will remove temporary highlights.
986 Such highlights are created by `org-occur' and `org-clock-display'.
987 When nil, `C-c C-c needs to be used to get rid of the highlights.
988 The highlights created by `org-preview-latex-fragment' always need
989 `C-c C-c' to be removed."
990 :group 'org-sparse-trees
991 :group 'org-time
992 :type 'boolean)
995 (defcustom org-occur-hook '(org-first-headline-recenter)
996 "Hook that is run after `org-occur' has constructed a sparse tree.
997 This can be used to recenter the window to show as much of the structure
998 as possible."
999 :group 'org-sparse-trees
1000 :type 'hook)
1002 (defgroup org-imenu-and-speedbar nil
1003 "Options concerning imenu and speedbar in Org-mode."
1004 :tag "Org Imenu and Speedbar"
1005 :group 'org-structure)
1007 (defcustom org-imenu-depth 2
1008 "The maximum level for Imenu access to Org-mode headlines.
1009 This also applied for speedbar access."
1010 :group 'org-imenu-and-speedbar
1011 :type 'integer)
1013 (defgroup org-table nil
1014 "Options concerning tables in Org-mode."
1015 :tag "Org Table"
1016 :group 'org)
1018 (defcustom org-enable-table-editor 'optimized
1019 "Non-nil means lines starting with \"|\" are handled by the table editor.
1020 When nil, such lines will be treated like ordinary lines.
1022 When equal to the symbol `optimized', the table editor will be optimized to
1023 do the following:
1024 - Automatic overwrite mode in front of whitespace in table fields.
1025 This makes the structure of the table stay in tact as long as the edited
1026 field does not exceed the column width.
1027 - Minimize the number of realigns. Normally, the table is aligned each time
1028 TAB or RET are pressed to move to another field. With optimization this
1029 happens only if changes to a field might have changed the column width.
1030 Optimization requires replacing the functions `self-insert-command',
1031 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1032 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1033 very good at guessing when a re-align will be necessary, but you can always
1034 force one with \\[org-ctrl-c-ctrl-c].
1036 If you would like to use the optimized version in Org-mode, but the
1037 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1039 This variable can be used to turn on and off the table editor during a session,
1040 but in order to toggle optimization, a restart is required.
1042 See also the variable `org-table-auto-blank-field'."
1043 :group 'org-table
1044 :type '(choice
1045 (const :tag "off" nil)
1046 (const :tag "on" t)
1047 (const :tag "on, optimized" optimized)))
1049 (defcustom org-self-insert-cluster-for-undo t
1050 "Non-nil means cluster self-insert commands for undo when possible.
1051 If this is set, then, like in the Emacs command loop, 20 consecutive
1052 characters will be undone together.
1053 This is configurable, because there is some impact on typing performance."
1054 :group 'org-table
1055 :type 'boolean)
1057 (defcustom org-table-tab-recognizes-table.el t
1058 "Non-nil means TAB will automatically notice a table.el table.
1059 When it sees such a table, it moves point into it and - if necessary -
1060 calls `table-recognize-table'."
1061 :group 'org-table-editing
1062 :type 'boolean)
1064 (defgroup org-link nil
1065 "Options concerning links in Org-mode."
1066 :tag "Org Link"
1067 :group 'org)
1069 (defvar org-link-abbrev-alist-local nil
1070 "Buffer-local version of `org-link-abbrev-alist', which see.
1071 The value of this is taken from the #+LINK lines.")
1072 (make-variable-buffer-local 'org-link-abbrev-alist-local)
1074 (defcustom org-link-abbrev-alist nil
1075 "Alist of link abbreviations.
1076 The car of each element is a string, to be replaced at the start of a link.
1077 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1078 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1080 [[linkkey:tag][description]]
1082 The 'linkkey' must be a word word, starting with a letter, followed
1083 by letters, numbers, '-' or '_'.
1085 If REPLACE is a string, the tag will simply be appended to create the link.
1086 If the string contains \"%s\", the tag will be inserted there. Alternatively,
1087 the placeholder \"%h\" will cause a url-encoded version of the tag to
1088 be inserted at that point (see the function `url-hexify-string').
1090 REPLACE may also be a function that will be called with the tag as the
1091 only argument to create the link, which should be returned as a string.
1093 See the manual for examples."
1094 :group 'org-link
1095 :type '(repeat
1096 (cons
1097 (string :tag "Protocol")
1098 (choice
1099 (string :tag "Format")
1100 (function)))))
1102 (defcustom org-descriptive-links t
1103 "Non-nil means hide link part and only show description of bracket links.
1104 Bracket links are like [[link][description]]. This variable sets the initial
1105 state in new org-mode buffers. The setting can then be toggled on a
1106 per-buffer basis from the Org->Hyperlinks menu."
1107 :group 'org-link
1108 :type 'boolean)
1110 (defcustom org-link-file-path-type 'adaptive
1111 "How the path name in file links should be stored.
1112 Valid values are:
1114 relative Relative to the current directory, i.e. the directory of the file
1115 into which the link is being inserted.
1116 absolute Absolute path, if possible with ~ for home directory.
1117 noabbrev Absolute path, no abbreviation of home directory.
1118 adaptive Use relative path for files in the current directory and sub-
1119 directories of it. For other files, use an absolute path."
1120 :group 'org-link
1121 :type '(choice
1122 (const relative)
1123 (const absolute)
1124 (const noabbrev)
1125 (const adaptive)))
1127 (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
1128 "Types of links that should be activated in Org-mode files.
1129 This is a list of symbols, each leading to the activation of a certain link
1130 type. In principle, it does not hurt to turn on most link types - there may
1131 be a small gain when turning off unused link types. The types are:
1133 bracket The recommended [[link][description]] or [[link]] links with hiding.
1134 angular Links in angular brackets that may contain whitespace like
1135 <bbdb:Carsten Dominik>.
1136 plain Plain links in normal text, no whitespace, like http://google.com.
1137 radio Text that is matched by a radio target, see manual for details.
1138 tag Tag settings in a headline (link to tag search).
1139 date Time stamps (link to calendar).
1140 footnote Footnote labels.
1142 Changing this variable requires a restart of Emacs to become effective."
1143 :group 'org-link
1144 :type '(set :greedy t
1145 (const :tag "Double bracket links (new style)" bracket)
1146 (const :tag "Angular bracket links (old style)" angular)
1147 (const :tag "Plain text links" plain)
1148 (const :tag "Radio target matches" radio)
1149 (const :tag "Tags" tag)
1150 (const :tag "Timestamps" date)
1151 (const :tag "Footnotes" footnote)))
1153 (defcustom org-make-link-description-function nil
1154 "Function to use to generate link descriptions from links. If
1155 nil the link location will be used. This function must take two
1156 parameters; the first is the link and the second the description
1157 org-insert-link has generated, and should return the description
1158 to use."
1159 :group 'org-link
1160 :type 'function)
1162 (defgroup org-link-store nil
1163 "Options concerning storing links in Org-mode."
1164 :tag "Org Store Link"
1165 :group 'org-link)
1167 (defcustom org-email-link-description-format "Email %c: %.30s"
1168 "Format of the description part of a link to an email or usenet message.
1169 The following %-escapes will be replaced by corresponding information:
1171 %F full \"From\" field
1172 %f name, taken from \"From\" field, address if no name
1173 %T full \"To\" field
1174 %t first name in \"To\" field, address if no name
1175 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1176 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1177 %s subject
1178 %m message-id.
1180 You may use normal field width specification between the % and the letter.
1181 This is for example useful to limit the length of the subject.
1183 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1184 :group 'org-link-store
1185 :type 'string)
1187 (defcustom org-from-is-user-regexp
1188 (let (r1 r2)
1189 (when (and user-mail-address (not (string= user-mail-address "")))
1190 (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
1191 (when (and user-full-name (not (string= user-full-name "")))
1192 (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
1193 (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
1194 "Regexp matched against the \"From:\" header of an email or usenet message.
1195 It should match if the message is from the user him/herself."
1196 :group 'org-link-store
1197 :type 'regexp)
1199 (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
1200 "Non-nil means storing a link to an Org file will use entry IDs.
1202 Note that before this variable is even considered, org-id must be loaded,
1203 so please customize `org-modules' and turn it on.
1205 The variable can have the following values:
1207 t Create an ID if needed to make a link to the current entry.
1209 create-if-interactive
1210 If `org-store-link' is called directly (interactively, as a user
1211 command), do create an ID to support the link. But when doing the
1212 job for remember, only use the ID if it already exists. The
1213 purpose of this setting is to avoid proliferation of unwanted
1214 IDs, just because you happen to be in an Org file when you
1215 call `org-remember' that automatically and preemptively
1216 creates a link. If you do want to get an ID link in a remember
1217 template to an entry not having an ID, create it first by
1218 explicitly creating a link to it, using `C-c C-l' first.
1220 create-if-interactive-and-no-custom-id
1221 Like create-if-interactive, but do not create an ID if there is
1222 a CUSTOM_ID property defined in the entry. This is the default.
1224 use-existing
1225 Use existing ID, do not create one.
1227 nil Never use an ID to make a link, instead link using a text search for
1228 the headline text."
1229 :group 'org-link-store
1230 :type '(choice
1231 (const :tag "Create ID to make link" t)
1232 (const :tag "Create if storing link interactively"
1233 create-if-interactive)
1234 (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
1235 create-if-interactive-and-no-custom-id)
1236 (const :tag "Only use existing" use-existing)
1237 (const :tag "Do not use ID to create link" nil)))
1239 (defcustom org-context-in-file-links t
1240 "Non-nil means file links from `org-store-link' contain context.
1241 A search string will be added to the file name with :: as separator and
1242 used to find the context when the link is activated by the command
1243 `org-open-at-point'.
1244 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1245 negates this setting for the duration of the command."
1246 :group 'org-link-store
1247 :type 'boolean)
1249 (defcustom org-keep-stored-link-after-insertion nil
1250 "Non-nil means keep link in list for entire session.
1252 The command `org-store-link' adds a link pointing to the current
1253 location to an internal list. These links accumulate during a session.
1254 The command `org-insert-link' can be used to insert links into any
1255 Org-mode file (offering completion for all stored links). When this
1256 option is nil, every link which has been inserted once using \\[org-insert-link]
1257 will be removed from the list, to make completing the unused links
1258 more efficient."
1259 :group 'org-link-store
1260 :type 'boolean)
1262 (defgroup org-link-follow nil
1263 "Options concerning following links in Org-mode."
1264 :tag "Org Follow Link"
1265 :group 'org-link)
1267 (defcustom org-link-translation-function nil
1268 "Function to translate links with different syntax to Org syntax.
1269 This can be used to translate links created for example by the Planner
1270 or emacs-wiki packages to Org syntax.
1271 The function must accept two parameters, a TYPE containing the link
1272 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1273 which is everything after the link protocol. It should return a cons
1274 with possibly modified values of type and path.
1275 Org contains a function for this, so if you set this variable to
1276 `org-translate-link-from-planner', you should be able follow many
1277 links created by planner."
1278 :group 'org-link-follow
1279 :type 'function)
1281 (defcustom org-follow-link-hook nil
1282 "Hook that is run after a link has been followed."
1283 :group 'org-link-follow
1284 :type 'hook)
1286 (defcustom org-tab-follows-link nil
1287 "Non-nil means on links TAB will follow the link.
1288 Needs to be set before org.el is loaded.
1289 This really should not be used, it does not make sense, and the
1290 implementation is bad."
1291 :group 'org-link-follow
1292 :type 'boolean)
1294 (defcustom org-return-follows-link nil
1295 "Non-nil means on links RET will follow the link.
1296 Needs to be set before org.el is loaded."
1297 :group 'org-link-follow
1298 :type 'boolean)
1300 (defcustom org-mouse-1-follows-link
1301 (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
1302 "Non-nil means mouse-1 on a link will follow the link.
1303 A longer mouse click will still set point. Does not work on XEmacs.
1304 Needs to be set before org.el is loaded."
1305 :group 'org-link-follow
1306 :type 'boolean)
1308 (defcustom org-mark-ring-length 4
1309 "Number of different positions to be recorded in the ring
1310 Changing this requires a restart of Emacs to work correctly."
1311 :group 'org-link-follow
1312 :type 'integer)
1314 (defcustom org-link-frame-setup
1315 '((vm . vm-visit-folder-other-frame)
1316 (gnus . gnus-other-frame)
1317 (file . find-file-other-window))
1318 "Setup the frame configuration for following links.
1319 When following a link with Emacs, it may often be useful to display
1320 this link in another window or frame. This variable can be used to
1321 set this up for the different types of links.
1322 For VM, use any of
1323 `vm-visit-folder'
1324 `vm-visit-folder-other-frame'
1325 For Gnus, use any of
1326 `gnus'
1327 `gnus-other-frame'
1328 `org-gnus-no-new-news'
1329 For FILE, use any of
1330 `find-file'
1331 `find-file-other-window'
1332 `find-file-other-frame'
1333 For the calendar, use the variable `calendar-setup'.
1334 For BBDB, it is currently only possible to display the matches in
1335 another window."
1336 :group 'org-link-follow
1337 :type '(list
1338 (cons (const vm)
1339 (choice
1340 (const vm-visit-folder)
1341 (const vm-visit-folder-other-window)
1342 (const vm-visit-folder-other-frame)))
1343 (cons (const gnus)
1344 (choice
1345 (const gnus)
1346 (const gnus-other-frame)
1347 (const org-gnus-no-new-news)))
1348 (cons (const file)
1349 (choice
1350 (const find-file)
1351 (const find-file-other-window)
1352 (const find-file-other-frame)))))
1354 (defcustom org-display-internal-link-with-indirect-buffer nil
1355 "Non-nil means use indirect buffer to display infile links.
1356 Activating internal links (from one location in a file to another location
1357 in the same file) normally just jumps to the location. When the link is
1358 activated with a C-u prefix (or with mouse-3), the link is displayed in
1359 another window. When this option is set, the other window actually displays
1360 an indirect buffer clone of the current buffer, to avoid any visibility
1361 changes to the current buffer."
1362 :group 'org-link-follow
1363 :type 'boolean)
1365 (defcustom org-open-non-existing-files nil
1366 "Non-nil means `org-open-file' will open non-existing files.
1367 When nil, an error will be generated.
1368 This variable applies only to external applications because they
1369 might choke on non-existing files. If the link is to a file that
1370 will be opened in Emacs, the variable is ignored."
1371 :group 'org-link-follow
1372 :type 'boolean)
1374 (defcustom org-open-directory-means-index-dot-org nil
1375 "Non-nil means a link to a directory really means to index.org.
1376 When nil, following a directory link will run dired or open a finder/explorer
1377 window on that directory."
1378 :group 'org-link-follow
1379 :type 'boolean)
1381 (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
1382 "Function and arguments to call for following mailto links.
1383 This is a list with the first element being a lisp function, and the
1384 remaining elements being arguments to the function. In string arguments,
1385 %a will be replaced by the address, and %s will be replaced by the subject
1386 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1387 :group 'org-link-follow
1388 :type '(choice
1389 (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1390 (const :tag "compose-mail" (compose-mail "%a" "%s"))
1391 (const :tag "message-mail" (message-mail "%a" "%s"))
1392 (cons :tag "other" (function) (repeat :tag "argument" sexp))))
1394 (defcustom org-confirm-shell-link-function 'yes-or-no-p
1395 "Non-nil means ask for confirmation before executing shell links.
1396 Shell links can be dangerous: just think about a link
1398 [[shell:rm -rf ~/*][Google Search]]
1400 This link would show up in your Org-mode document as \"Google Search\",
1401 but really it would remove your entire home directory.
1402 Therefore we advise against setting this variable to nil.
1403 Just change it to `y-or-n-p' if you want to confirm with a
1404 single keystroke rather than having to type \"yes\"."
1405 :group 'org-link-follow
1406 :type '(choice
1407 (const :tag "with yes-or-no (safer)" yes-or-no-p)
1408 (const :tag "with y-or-n (faster)" y-or-n-p)
1409 (const :tag "no confirmation (dangerous)" nil)))
1411 (defcustom org-confirm-elisp-link-function 'yes-or-no-p
1412 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1413 Elisp links can be dangerous: just think about a link
1415 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1417 This link would show up in your Org-mode document as \"Google Search\",
1418 but really it would remove your entire home directory.
1419 Therefore we advise against setting this variable to nil.
1420 Just change it to `y-or-n-p' if you want to confirm with a
1421 single keystroke rather than having to type \"yes\"."
1422 :group 'org-link-follow
1423 :type '(choice
1424 (const :tag "with yes-or-no (safer)" yes-or-no-p)
1425 (const :tag "with y-or-n (faster)" y-or-n-p)
1426 (const :tag "no confirmation (dangerous)" nil)))
1428 (defconst org-file-apps-defaults-gnu
1429 '((remote . emacs)
1430 (system . mailcap)
1431 (t . mailcap))
1432 "Default file applications on a UNIX or GNU/Linux system.
1433 See `org-file-apps'.")
1435 (defconst org-file-apps-defaults-macosx
1436 '((remote . emacs)
1437 (t . "open %s")
1438 (system . "open %s")
1439 ("ps.gz" . "gv %s")
1440 ("eps.gz" . "gv %s")
1441 ("dvi" . "xdvi %s")
1442 ("fig" . "xfig %s"))
1443 "Default file applications on a MacOS X system.
1444 The system \"open\" is known as a default, but we use X11 applications
1445 for some files for which the OS does not have a good default.
1446 See `org-file-apps'.")
1448 (defconst org-file-apps-defaults-windowsnt
1449 (list
1450 '(remote . emacs)
1451 (cons t
1452 (list (if (featurep 'xemacs)
1453 'mswindows-shell-execute
1454 'w32-shell-execute)
1455 "open" 'file))
1456 (cons 'system
1457 (list (if (featurep 'xemacs)
1458 'mswindows-shell-execute
1459 'w32-shell-execute)
1460 "open" 'file)))
1461 "Default file applications on a Windows NT system.
1462 The system \"open\" is used for most files.
1463 See `org-file-apps'.")
1465 (defcustom org-file-apps
1467 (auto-mode . emacs)
1468 ("\\.mm\\'" . default)
1469 ("\\.x?html?\\'" . default)
1470 ("\\.pdf\\'" . default)
1472 "External applications for opening `file:path' items in a document.
1473 Org-mode uses system defaults for different file types, but
1474 you can use this variable to set the application for a given file
1475 extension. The entries in this list are cons cells where the car identifies
1476 files and the cdr the corresponding command. Possible values for the
1477 file identifier are
1478 \"regex\" Regular expression matched against the file name. For backward
1479 compatibility, this can also be a string with only alphanumeric
1480 characters, which is then interpreted as an extension.
1481 `directory' Matches a directory
1482 `remote' Matches a remote file, accessible through tramp or efs.
1483 Remote files most likely should be visited through Emacs
1484 because external applications cannot handle such paths.
1485 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1486 so all files Emacs knows how to handle. Using this with
1487 command `emacs' will open most files in Emacs. Beware that this
1488 will also open html files inside Emacs, unless you add
1489 (\"html\" . default) to the list as well.
1490 t Default for files not matched by any of the other options.
1491 `system' The system command to open files, like `open' on Windows
1492 and Mac OS X, and mailcap under GNU/Linux. This is the command
1493 that will be selected if you call `C-c C-o' with a double
1494 `C-u C-u' prefix.
1496 Possible values for the command are:
1497 `emacs' The file will be visited by the current Emacs process.
1498 `default' Use the default application for this file type, which is the
1499 association for t in the list, most likely in the system-specific
1500 part.
1501 This can be used to overrule an unwanted setting in the
1502 system-specific variable.
1503 `system' Use the system command for opening files, like \"open\".
1504 This command is specified by the entry whose car is `system'.
1505 Most likely, the system-specific version of this variable
1506 does define this command, but you can overrule/replace it
1507 here.
1508 string A command to be executed by a shell; %s will be replaced
1509 by the path to the file.
1510 sexp A Lisp form which will be evaluated. The file path will
1511 be available in the Lisp variable `file'.
1512 For more examples, see the system specific constants
1513 `org-file-apps-defaults-macosx'
1514 `org-file-apps-defaults-windowsnt'
1515 `org-file-apps-defaults-gnu'."
1516 :group 'org-link-follow
1517 :type '(repeat
1518 (cons (choice :value ""
1519 (string :tag "Extension")
1520 (const :tag "System command to open files" system)
1521 (const :tag "Default for unrecognized files" t)
1522 (const :tag "Remote file" remote)
1523 (const :tag "Links to a directory" directory)
1524 (const :tag "Any files that have Emacs modes"
1525 auto-mode))
1526 (choice :value ""
1527 (const :tag "Visit with Emacs" emacs)
1528 (const :tag "Use default" default)
1529 (const :tag "Use the system command" system)
1530 (string :tag "Command")
1531 (sexp :tag "Lisp form")))))
1535 (defgroup org-refile nil
1536 "Options concerning refiling entries in Org-mode."
1537 :tag "Org Refile"
1538 :group 'org)
1540 (defcustom org-directory "~/org"
1541 "Directory with org files.
1542 This is just a default location to look for Org files. There is no need
1543 at all to put your files into this directory. It is only used in the
1544 following situations:
1546 1. When a remember template specifies a target file that is not an
1547 absolute path. The path will then be interpreted relative to
1548 `org-directory'
1549 2. When a remember note is filed away in an interactive way (when exiting the
1550 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1551 with `org-directory' as the default path."
1552 :group 'org-refile
1553 :group 'org-remember
1554 :type 'directory)
1556 (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
1557 "Default target for storing notes.
1558 Used by the hooks for remember.el. This can be a string, or nil to mean
1559 the value of `remember-data-file'.
1560 You can set this on a per-template basis with the variable
1561 `org-remember-templates'."
1562 :group 'org-refile
1563 :group 'org-remember
1564 :type '(choice
1565 (const :tag "Default from remember-data-file" nil)
1566 file))
1568 (defcustom org-goto-interface 'outline
1569 "The default interface to be used for `org-goto'.
1570 Allowed values are:
1571 outline The interface shows an outline of the relevant file
1572 and the correct heading is found by moving through
1573 the outline or by searching with incremental search.
1574 outline-path-completion Headlines in the current buffer are offered via
1575 completion. This is the interface also used by
1576 the refile command."
1577 :group 'org-refile
1578 :type '(choice
1579 (const :tag "Outline" outline)
1580 (const :tag "Outline-path-completion" outline-path-completion)))
1582 (defcustom org-goto-max-level 5
1583 "Maximum level to be considered when running org-goto with refile interface."
1584 :group 'org-refile
1585 :type 'integer)
1587 (defcustom org-reverse-note-order nil
1588 "Non-nil means store new notes at the beginning of a file or entry.
1589 When nil, new notes will be filed to the end of a file or entry.
1590 This can also be a list with cons cells of regular expressions that
1591 are matched against file names, and values."
1592 :group 'org-remember
1593 :group 'org-refile
1594 :type '(choice
1595 (const :tag "Reverse always" t)
1596 (const :tag "Reverse never" nil)
1597 (repeat :tag "By file name regexp"
1598 (cons regexp boolean))))
1600 (defcustom org-log-refile nil
1601 "Information to record when a task is refiled.
1603 Possible values are:
1605 nil Don't add anything
1606 time Add a time stamp to the task
1607 note Prompt for a note and add it with template `org-log-note-headings'
1609 This option can also be set with on a per-file-basis with
1611 #+STARTUP: nologrefile
1612 #+STARTUP: logrefile
1613 #+STARTUP: lognoterefile
1615 You can have local logging settings for a subtree by setting the LOGGING
1616 property to one or more of these keywords.
1618 When bulk-refiling from the agenda, the value `note' is forbidden and
1619 will temporarily be changed to `time'."
1620 :group 'org-refile
1621 :group 'org-progress
1622 :type '(choice
1623 (const :tag "No logging" nil)
1624 (const :tag "Record timestamp" time)
1625 (const :tag "Record timestamp with note." note)))
1627 (defcustom org-refile-targets nil
1628 "Targets for refiling entries with \\[org-refile].
1629 This is list of cons cells. Each cell contains:
1630 - a specification of the files to be considered, either a list of files,
1631 or a symbol whose function or variable value will be used to retrieve
1632 a file name or a list of file names. If you use `org-agenda-files' for
1633 that, all agenda files will be scanned for targets. Nil means consider
1634 headings in the current buffer.
1635 - A specification of how to find candidate refile targets. This may be
1636 any of:
1637 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1638 This tag has to be present in all target headlines, inheritance will
1639 not be considered.
1640 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1641 todo keyword.
1642 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1643 headlines that are refiling targets.
1644 - a cons cell (:level . N). Any headline of level N is considered a target.
1645 Note that, when `org-odd-levels-only' is set, level corresponds to
1646 order in hierarchy, not to the number of stars.
1647 - a cons cell (:maxlevel . N). Any headline with level <= N is 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.
1651 You can set the variable `org-refile-target-verify-function' to a function
1652 to verify each headline found by the simple critery above.
1654 When this variable is nil, all top-level headlines in the current buffer
1655 are used, equivalent to the value `((nil . (:level . 1))'."
1656 :group 'org-refile
1657 :type '(repeat
1658 (cons
1659 (choice :value org-agenda-files
1660 (const :tag "All agenda files" org-agenda-files)
1661 (const :tag "Current buffer" nil)
1662 (function) (variable) (file))
1663 (choice :tag "Identify target headline by"
1664 (cons :tag "Specific tag" (const :value :tag) (string))
1665 (cons :tag "TODO keyword" (const :value :todo) (string))
1666 (cons :tag "Regular expression" (const :value :regexp) (regexp))
1667 (cons :tag "Level number" (const :value :level) (integer))
1668 (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
1670 (defcustom org-refile-target-verify-function nil
1671 "Function to verify if the headline at point should be a refile target.
1672 The function will be called without arguments, with point at the
1673 beginning of the headline. It should return t and leave point
1674 where it is if the headline is a valid target for refiling.
1676 If the target should not be selected, the function must return nil.
1677 In addition to this, it may move point to a place from where the search
1678 should be continued. For example, the function may decide that the entire
1679 subtree of the current entry should be excluded and move point to the end
1680 of the subtree."
1681 :group 'org-refile
1682 :type 'function)
1684 (defcustom org-refile-use-outline-path nil
1685 "Non-nil means provide refile targets as paths.
1686 So a level 3 headline will be available as level1/level2/level3.
1688 When the value is `file', also include the file name (without directory)
1689 into the path. In this case, you can also stop the completion after
1690 the file name, to get entries inserted as top level in the file.
1692 When `full-file-path', include the full file path."
1693 :group 'org-refile
1694 :type '(choice
1695 (const :tag "Not" nil)
1696 (const :tag "Yes" t)
1697 (const :tag "Start with file name" file)
1698 (const :tag "Start with full file path" full-file-path)))
1700 (defcustom org-outline-path-complete-in-steps t
1701 "Non-nil means complete the outline path in hierarchical steps.
1702 When Org-mode uses the refile interface to select an outline path
1703 \(see variable `org-refile-use-outline-path'), the completion of
1704 the path can be done is a single go, or if can be done in steps down
1705 the headline hierarchy. Going in steps is probably the best if you
1706 do not use a special completion package like `ido' or `icicles'.
1707 However, when using these packages, going in one step can be very
1708 fast, while still showing the whole path to the entry."
1709 :group 'org-refile
1710 :type 'boolean)
1712 (defcustom org-refile-allow-creating-parent-nodes nil
1713 "Non-nil means allow to create new nodes as refile targets.
1714 New nodes are then created by adding \"/new node name\" to the completion
1715 of an existing node. When the value of this variable is `confirm',
1716 new node creation must be confirmed by the user (recommended)
1717 When nil, the completion must match an existing entry.
1719 Note that, if the new heading is not seen by the criteria
1720 listed in `org-refile-targets', multiple instances of the same
1721 heading would be created by trying again to file under the new
1722 heading."
1723 :group 'org-refile
1724 :type '(choice
1725 (const :tag "Never" nil)
1726 (const :tag "Always" t)
1727 (const :tag "Prompt for confirmation" confirm)))
1729 (defgroup org-todo nil
1730 "Options concerning TODO items in Org-mode."
1731 :tag "Org TODO"
1732 :group 'org)
1734 (defgroup org-progress nil
1735 "Options concerning Progress logging in Org-mode."
1736 :tag "Org Progress"
1737 :group 'org-time)
1739 (defvar org-todo-interpretation-widgets
1741 (:tag "Sequence (cycling hits every state)" sequence)
1742 (:tag "Type (cycling directly to DONE)" type))
1743 "The available interpretation symbols for customizing
1744 `org-todo-keywords'.
1745 Interested libraries should add to this list.")
1747 (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
1748 "List of TODO entry keyword sequences and their interpretation.
1749 \\<org-mode-map>This is a list of sequences.
1751 Each sequence starts with a symbol, either `sequence' or `type',
1752 indicating if the keywords should be interpreted as a sequence of
1753 action steps, or as different types of TODO items. The first
1754 keywords are states requiring action - these states will select a headline
1755 for inclusion into the global TODO list Org-mode produces. If one of
1756 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1757 signify that no further action is necessary. If \"|\" is not found,
1758 the last keyword is treated as the only DONE state of the sequence.
1760 The command \\[org-todo] cycles an entry through these states, and one
1761 additional state where no keyword is present. For details about this
1762 cycling, see the manual.
1764 TODO keywords and interpretation can also be set on a per-file basis with
1765 the special #+SEQ_TODO and #+TYP_TODO lines.
1767 Each keyword can optionally specify a character for fast state selection
1768 \(in combination with the variable `org-use-fast-todo-selection')
1769 and specifiers for state change logging, using the same syntax
1770 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1771 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1772 indicates to record a time stamp each time this state is selected.
1774 Each keyword may also specify if a timestamp or a note should be
1775 recorded when entering or leaving the state, by adding additional
1776 characters in the parenthesis after the keyword. This looks like this:
1777 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1778 record only the time of the state change. With X and Y being either
1779 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1780 Y when leaving the state if and only if the *target* state does not
1781 define X. You may omit any of the fast-selection key or X or /Y,
1782 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1784 For backward compatibility, this variable may also be just a list
1785 of keywords - in this case the interpretation (sequence or type) will be
1786 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1787 :group 'org-todo
1788 :group 'org-keywords
1789 :type '(choice
1790 (repeat :tag "Old syntax, just keywords"
1791 (string :tag "Keyword"))
1792 (repeat :tag "New syntax"
1793 (cons
1794 (choice
1795 :tag "Interpretation"
1796 ;;Quick and dirty way to see
1797 ;;`org-todo-interpretations'. This takes the
1798 ;;place of item arguments
1799 :convert-widget
1800 (lambda (widget)
1801 (widget-put widget
1802 :args (mapcar
1803 #'(lambda (x)
1804 (widget-convert
1805 (cons 'const x)))
1806 org-todo-interpretation-widgets))
1807 widget))
1808 (repeat
1809 (string :tag "Keyword"))))))
1811 (defvar org-todo-keywords-1 nil
1812 "All TODO and DONE keywords active in a buffer.")
1813 (make-variable-buffer-local 'org-todo-keywords-1)
1814 (defvar org-todo-keywords-for-agenda nil)
1815 (defvar org-done-keywords-for-agenda nil)
1816 (defvar org-drawers-for-agenda nil)
1817 (defvar org-todo-keyword-alist-for-agenda nil)
1818 (defvar org-tag-alist-for-agenda nil)
1819 (defvar org-agenda-contributing-files nil)
1820 (defvar org-not-done-keywords nil)
1821 (make-variable-buffer-local 'org-not-done-keywords)
1822 (defvar org-done-keywords nil)
1823 (make-variable-buffer-local 'org-done-keywords)
1824 (defvar org-todo-heads nil)
1825 (make-variable-buffer-local 'org-todo-heads)
1826 (defvar org-todo-sets nil)
1827 (make-variable-buffer-local 'org-todo-sets)
1828 (defvar org-todo-log-states nil)
1829 (make-variable-buffer-local 'org-todo-log-states)
1830 (defvar org-todo-kwd-alist nil)
1831 (make-variable-buffer-local 'org-todo-kwd-alist)
1832 (defvar org-todo-key-alist nil)
1833 (make-variable-buffer-local 'org-todo-key-alist)
1834 (defvar org-todo-key-trigger nil)
1835 (make-variable-buffer-local 'org-todo-key-trigger)
1837 (defcustom org-todo-interpretation 'sequence
1838 "Controls how TODO keywords are interpreted.
1839 This variable is in principle obsolete and is only used for
1840 backward compatibility, if the interpretation of todo keywords is
1841 not given already in `org-todo-keywords'. See that variable for
1842 more information."
1843 :group 'org-todo
1844 :group 'org-keywords
1845 :type '(choice (const sequence)
1846 (const type)))
1848 (defcustom org-use-fast-todo-selection t
1849 "Non-nil means use the fast todo selection scheme with C-c C-t.
1850 This variable describes if and under what circumstances the cycling
1851 mechanism for TODO keywords will be replaced by a single-key, direct
1852 selection scheme.
1854 When nil, fast selection is never used.
1856 When the symbol `prefix', it will be used when `org-todo' is called with
1857 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1858 in an agenda buffer.
1860 When t, fast selection is used by default. In this case, the prefix
1861 argument forces cycling instead.
1863 In all cases, the special interface is only used if access keys have actually
1864 been assigned by the user, i.e. if keywords in the configuration are followed
1865 by a letter in parenthesis, like TODO(t)."
1866 :group 'org-todo
1867 :type '(choice
1868 (const :tag "Never" nil)
1869 (const :tag "By default" t)
1870 (const :tag "Only with C-u C-c C-t" prefix)))
1872 (defcustom org-provide-todo-statistics t
1873 "Non-nil means update todo statistics after insert and toggle.
1874 ALL-HEADLINES means update todo statistics by including headlines
1875 with no TODO keyword as well, counting them as not done.
1876 A list of TODO keywords means the same, but skip keywords that are
1877 not in this list.
1879 When this is set, todo statistics is updated in the parent of the
1880 current entry each time a todo state is changed."
1881 :group 'org-todo
1882 :type '(choice
1883 (const :tag "Yes, only for TODO entries" t)
1884 (const :tag "Yes, including all entries" 'all-headlines)
1885 (repeat :tag "Yes, for TODOs in this list"
1886 (string :tag "TODO keyword"))
1887 (other :tag "No TODO statistics" nil)))
1889 (defcustom org-hierarchical-todo-statistics t
1890 "Non-nil means TODO statistics covers just direct children.
1891 When nil, all entries in the subtree are considered.
1892 This has only an effect if `org-provide-todo-statistics' is set.
1893 To set this to nil for only a single subtree, use a COOKIE_DATA
1894 property and include the word \"recursive\" into the value."
1895 :group 'org-todo
1896 :type 'boolean)
1898 (defcustom org-after-todo-state-change-hook nil
1899 "Hook which is run after the state of a TODO item was changed.
1900 The new state (a string with a TODO keyword, or nil) is available in the
1901 Lisp variable `state'."
1902 :group 'org-todo
1903 :type 'hook)
1905 (defvar org-blocker-hook nil
1906 "Hook for functions that are allowed to block a state change.
1908 Each function gets as its single argument a property list, see
1909 `org-trigger-hook' for more information about this list.
1911 If any of the functions in this hook returns nil, the state change
1912 is blocked.")
1914 (defvar org-trigger-hook nil
1915 "Hook for functions that are triggered by a state change.
1917 Each function gets as its single argument a property list with at least
1918 the following elements:
1920 (:type type-of-change :position pos-at-entry-start
1921 :from old-state :to new-state)
1923 Depending on the type, more properties may be present.
1925 This mechanism is currently implemented for:
1927 TODO state changes
1928 ------------------
1929 :type todo-state-change
1930 :from previous state (keyword as a string), or nil, or a symbol
1931 'todo' or 'done', to indicate the general type of state.
1932 :to new state, like in :from")
1934 (defcustom org-enforce-todo-dependencies nil
1935 "Non-nil means undone TODO entries will block switching the parent to DONE.
1936 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
1937 be blocked if any prior sibling is not yet done.
1938 Finally, if the parent is blocked because of ordered siblings of its own,
1939 the child will also be blocked.
1940 This variable needs to be set before org.el is loaded, and you need to
1941 restart Emacs after a change to make the change effective. The only way
1942 to change is while Emacs is running is through the customize interface."
1943 :set (lambda (var val)
1944 (set var val)
1945 (if val
1946 (add-hook 'org-blocker-hook
1947 'org-block-todo-from-children-or-siblings-or-parent)
1948 (remove-hook 'org-blocker-hook
1949 'org-block-todo-from-children-or-siblings-or-parent)))
1950 :group 'org-todo
1951 :type 'boolean)
1953 (defcustom org-enforce-todo-checkbox-dependencies nil
1954 "Non-nil means unchecked boxes will block switching the parent to DONE.
1955 When this is nil, checkboxes have no influence on switching TODO states.
1956 When non-nil, you first need to check off all check boxes before the TODO
1957 entry can be switched to DONE.
1958 This variable needs to be set before org.el is loaded, and you need to
1959 restart Emacs after a change to make the change effective. The only way
1960 to change is while Emacs is running is through the customize interface."
1961 :set (lambda (var val)
1962 (set var val)
1963 (if val
1964 (add-hook 'org-blocker-hook
1965 'org-block-todo-from-checkboxes)
1966 (remove-hook 'org-blocker-hook
1967 'org-block-todo-from-checkboxes)))
1968 :group 'org-todo
1969 :type 'boolean)
1971 (defcustom org-treat-insert-todo-heading-as-state-change nil
1972 "Non-nil means inserting a TODO heading is treated as state change.
1973 So when the command \\[org-insert-todo-heading] is used, state change
1974 logging will apply if appropriate. When nil, the new TODO item will
1975 be inserted directly, and no logging will take place."
1976 :group 'org-todo
1977 :type 'boolean)
1979 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
1980 "Non-nil means switching TODO states with S-cursor counts as state change.
1981 This is the default behavior. However, setting this to nil allows a
1982 convenient way to select a TODO state and bypass any logging associated
1983 with that."
1984 :group 'org-todo
1985 :type 'boolean)
1987 (defcustom org-todo-state-tags-triggers nil
1988 "Tag changes that should be triggered by TODO state changes.
1989 This is a list. Each entry is
1991 (state-change (tag . flag) .......)
1993 State-change can be a string with a state, and empty string to indicate the
1994 state that has no TODO keyword, or it can be one of the symbols `todo'
1995 or `done', meaning any not-done or done state, respectively."
1996 :group 'org-todo
1997 :group 'org-tags
1998 :type '(repeat
1999 (cons (choice :tag "When changing to"
2000 (const :tag "Not-done state" todo)
2001 (const :tag "Done state" done)
2002 (string :tag "State"))
2003 (repeat
2004 (cons :tag "Tag action"
2005 (string :tag "Tag")
2006 (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
2008 (defcustom org-log-done nil
2009 "Information to record when a task moves to the DONE state.
2011 Possible values are:
2013 nil Don't add anything, just change the keyword
2014 time Add a time stamp to the task
2015 note Prompt for a note and add it with template `org-log-note-headings'
2017 This option can also be set with on a per-file-basis with
2019 #+STARTUP: nologdone
2020 #+STARTUP: logdone
2021 #+STARTUP: lognotedone
2023 You can have local logging settings for a subtree by setting the LOGGING
2024 property to one or more of these keywords."
2025 :group 'org-todo
2026 :group 'org-progress
2027 :type '(choice
2028 (const :tag "No logging" nil)
2029 (const :tag "Record CLOSED timestamp" time)
2030 (const :tag "Record CLOSED timestamp with note." note)))
2032 ;; Normalize old uses of org-log-done.
2033 (cond
2034 ((eq org-log-done t) (setq org-log-done 'time))
2035 ((and (listp org-log-done) (memq 'done org-log-done))
2036 (setq org-log-done 'note)))
2038 (defcustom org-log-reschedule nil
2039 "Information to record when the scheduling date of a tasks is modified.
2041 Possible values are:
2043 nil Don't add anything, just change the date
2044 time Add a time stamp to the task
2045 note Prompt for a note and add it with template `org-log-note-headings'
2047 This option can also be set with on a per-file-basis with
2049 #+STARTUP: nologreschedule
2050 #+STARTUP: logreschedule
2051 #+STARTUP: lognotereschedule"
2052 :group 'org-todo
2053 :group 'org-progress
2054 :type '(choice
2055 (const :tag "No logging" nil)
2056 (const :tag "Record timestamp" time)
2057 (const :tag "Record timestamp with note." note)))
2059 (defcustom org-log-redeadline nil
2060 "Information to record when the deadline date of a tasks is modified.
2062 Possible values are:
2064 nil Don't add anything, just change the date
2065 time Add a time stamp to the task
2066 note Prompt for a note and add it with template `org-log-note-headings'
2068 This option can also be set with on a per-file-basis with
2070 #+STARTUP: nologredeadline
2071 #+STARTUP: logredeadline
2072 #+STARTUP: lognoteredeadline
2074 You can have local logging settings for a subtree by setting the LOGGING
2075 property to one or more of these keywords."
2076 :group 'org-todo
2077 :group 'org-progress
2078 :type '(choice
2079 (const :tag "No logging" nil)
2080 (const :tag "Record timestamp" time)
2081 (const :tag "Record timestamp with note." note)))
2083 (defcustom org-log-note-clock-out nil
2084 "Non-nil means record a note when clocking out of an item.
2085 This can also be configured on a per-file basis by adding one of
2086 the following lines anywhere in the buffer:
2088 #+STARTUP: lognoteclock-out
2089 #+STARTUP: nolognoteclock-out"
2090 :group 'org-todo
2091 :group 'org-progress
2092 :type 'boolean)
2094 (defcustom org-log-done-with-time t
2095 "Non-nil means the CLOSED time stamp will contain date and time.
2096 When nil, only the date will be recorded."
2097 :group 'org-progress
2098 :type 'boolean)
2100 (defcustom org-log-note-headings
2101 '((done . "CLOSING NOTE %t")
2102 (state . "State %-12s from %-12S %t")
2103 (note . "Note taken on %t")
2104 (reschedule . "Rescheduled from %S on %t")
2105 (delschedule . "Not scheduled, was %S on %t")
2106 (redeadline . "New deadline from %S on %t")
2107 (deldeadline . "Removed deadline, was %S on %t")
2108 (refile . "Refiled on %t")
2109 (clock-out . ""))
2110 "Headings for notes added to entries.
2111 The value is an alist, with the car being a symbol indicating the note
2112 context, and the cdr is the heading to be used. The heading may also be the
2113 empty string.
2114 %t in the heading will be replaced by a time stamp.
2115 %s will be replaced by the new TODO state, in double quotes.
2116 %S will be replaced by the old TODO state, in double quotes.
2117 %u will be replaced by the user name.
2118 %U will be replaced by the full user name.
2120 In fact, it is not a good idea to change the `state' entry, because
2121 agenda log mode depends on the format of these entries."
2122 :group 'org-todo
2123 :group 'org-progress
2124 :type '(list :greedy t
2125 (cons (const :tag "Heading when closing an item" done) string)
2126 (cons (const :tag
2127 "Heading when changing todo state (todo sequence only)"
2128 state) string)
2129 (cons (const :tag "Heading when just taking a note" note) string)
2130 (cons (const :tag "Heading when clocking out" clock-out) string)
2131 (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
2132 (cons (const :tag "Heading when rescheduling" reschedule) string)
2133 (cons (const :tag "Heading when changing deadline" redeadline) string)
2134 (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
2135 (cons (const :tag "Heading when refiling" refile) string)))
2137 (unless (assq 'note org-log-note-headings)
2138 (push '(note . "%t") org-log-note-headings))
2140 (defcustom org-log-into-drawer nil
2141 "Non-nil means insert state change notes and time stamps into a drawer.
2142 When nil, state changes notes will be inserted after the headline and
2143 any scheduling and clock lines, but not inside a drawer.
2145 The value of this variable should be the name of the drawer to use.
2146 LOGBOOK is proposed at the default drawer for this purpose, you can
2147 also set this to a string to define the drawer of your choice.
2149 A value of t is also allowed, representing \"LOGBOOK\".
2151 If this variable is set, `org-log-state-notes-insert-after-drawers'
2152 will be ignored.
2154 You can set the property LOG_INTO_DRAWER to overrule this setting for
2155 a subtree."
2156 :group 'org-todo
2157 :group 'org-progress
2158 :type '(choice
2159 (const :tag "Not into a drawer" nil)
2160 (const :tag "LOGBOOK" t)
2161 (string :tag "Other")))
2163 (if (fboundp 'defvaralias)
2164 (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
2166 (defun org-log-into-drawer ()
2167 "Return the value of `org-log-into-drawer', but let properties overrule.
2168 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2169 used instead of the default value."
2170 (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
2171 (cond
2172 ((or (not p) (equal p "nil")) org-log-into-drawer)
2173 ((equal p "t") "LOGBOOK")
2174 (t p))))
2176 (defcustom org-log-state-notes-insert-after-drawers nil
2177 "Non-nil means insert state change notes after any drawers in entry.
2178 Only the drawers that *immediately* follow the headline and the
2179 deadline/scheduled line are skipped.
2180 When nil, insert notes right after the heading and perhaps the line
2181 with deadline/scheduling if present.
2183 This variable will have no effect if `org-log-into-drawer' is
2184 set."
2185 :group 'org-todo
2186 :group 'org-progress
2187 :type 'boolean)
2189 (defcustom org-log-states-order-reversed t
2190 "Non-nil means the latest state note will be directly after heading.
2191 When nil, the state change notes will be ordered according to time."
2192 :group 'org-todo
2193 :group 'org-progress
2194 :type 'boolean)
2196 (defcustom org-todo-repeat-to-state nil
2197 "The TODO state to which a repeater should return the repeating task.
2198 By default this is the first task in a TODO sequence, or the previous state
2199 in a TODO_TYP set. But you can specify another task here.
2200 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2201 :group 'org-todo
2202 :type '(choice (const :tag "Head of sequence" nil)
2203 (string :tag "Specific state")))
2205 (defcustom org-log-repeat 'time
2206 "Non-nil means record moving through the DONE state when triggering repeat.
2207 An auto-repeating task is immediately switched back to TODO when
2208 marked DONE. If you are not logging state changes (by adding \"@\"
2209 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2210 record a closing note, there will be no record of the task moving
2211 through DONE. This variable forces taking a note anyway.
2213 nil Don't force a record
2214 time Record a time stamp
2215 note Record a note
2217 This option can also be set with on a per-file-basis with
2219 #+STARTUP: logrepeat
2220 #+STARTUP: lognoterepeat
2221 #+STARTUP: nologrepeat
2223 You can have local logging settings for a subtree by setting the LOGGING
2224 property to one or more of these keywords."
2225 :group 'org-todo
2226 :group 'org-progress
2227 :type '(choice
2228 (const :tag "Don't force a record" nil)
2229 (const :tag "Force recording the DONE state" time)
2230 (const :tag "Force recording a note with the DONE state" note)))
2233 (defgroup org-priorities nil
2234 "Priorities in Org-mode."
2235 :tag "Org Priorities"
2236 :group 'org-todo)
2238 (defcustom org-enable-priority-commands t
2239 "Non-nil means priority commands are active.
2240 When nil, these commands will be disabled, so that you never accidentally
2241 set a priority."
2242 :group 'org-priorities
2243 :type 'boolean)
2245 (defcustom org-highest-priority ?A
2246 "The highest priority of TODO items. A character like ?A, ?B etc.
2247 Must have a smaller ASCII number than `org-lowest-priority'."
2248 :group 'org-priorities
2249 :type 'character)
2251 (defcustom org-lowest-priority ?C
2252 "The lowest priority of TODO items. A character like ?A, ?B etc.
2253 Must have a larger ASCII number than `org-highest-priority'."
2254 :group 'org-priorities
2255 :type 'character)
2257 (defcustom org-default-priority ?B
2258 "The default priority of TODO items.
2259 This is the priority an item get if no explicit priority is given."
2260 :group 'org-priorities
2261 :type 'character)
2263 (defcustom org-priority-start-cycle-with-default t
2264 "Non-nil means start with default priority when starting to cycle.
2265 When this is nil, the first step in the cycle will be (depending on the
2266 command used) one higher or lower that the default priority."
2267 :group 'org-priorities
2268 :type 'boolean)
2270 (defgroup org-time nil
2271 "Options concerning time stamps and deadlines in Org-mode."
2272 :tag "Org Time"
2273 :group 'org)
2275 (defcustom org-insert-labeled-timestamps-at-point nil
2276 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2277 When nil, these labeled time stamps are forces into the second line of an
2278 entry, just after the headline. When scheduling from the global TODO list,
2279 the time stamp will always be forced into the second line."
2280 :group 'org-time
2281 :type 'boolean)
2283 (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
2284 "Formats for `format-time-string' which are used for time stamps.
2285 It is not recommended to change this constant.")
2287 (defcustom org-time-stamp-rounding-minutes '(0 5)
2288 "Number of minutes to round time stamps to.
2289 These are two values, the first applies when first creating a time stamp.
2290 The second applies when changing it with the commands `S-up' and `S-down'.
2291 When changing the time stamp, this means that it will change in steps
2292 of N minutes, as given by the second value.
2294 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2295 numbers should be factors of 60, so for example 5, 10, 15.
2297 When this is larger than 1, you can still force an exact time-stamp by using
2298 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
2299 and by using a prefix arg to `S-up/down' to specify the exact number
2300 of minutes to shift."
2301 :group 'org-time
2302 :get '(lambda (var) ; Make sure all entries have 5 elements
2303 (if (integerp (default-value var))
2304 (list (default-value var) 5)
2305 (default-value var)))
2306 :type '(list
2307 (integer :tag "when inserting times")
2308 (integer :tag "when modifying times")))
2310 ;; Normalize old customizations of this variable.
2311 (when (integerp org-time-stamp-rounding-minutes)
2312 (setq org-time-stamp-rounding-minutes
2313 (list org-time-stamp-rounding-minutes
2314 org-time-stamp-rounding-minutes)))
2316 (defcustom org-display-custom-times nil
2317 "Non-nil means overlay custom formats over all time stamps.
2318 The formats are defined through the variable `org-time-stamp-custom-formats'.
2319 To turn this on on a per-file basis, insert anywhere in the file:
2320 #+STARTUP: customtime"
2321 :group 'org-time
2322 :set 'set-default
2323 :type 'sexp)
2324 (make-variable-buffer-local 'org-display-custom-times)
2326 (defcustom org-time-stamp-custom-formats
2327 '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
2328 "Custom formats for time stamps. See `format-time-string' for the syntax.
2329 These are overlayed over the default ISO format if the variable
2330 `org-display-custom-times' is set. Time like %H:%M should be at the
2331 end of the second format. The custom formats are also honored by export
2332 commands, if custom time display is turned on at the time of export."
2333 :group 'org-time
2334 :type 'sexp)
2336 (defun org-time-stamp-format (&optional long inactive)
2337 "Get the right format for a time string."
2338 (let ((f (if long (cdr org-time-stamp-formats)
2339 (car org-time-stamp-formats))))
2340 (if inactive
2341 (concat "[" (substring f 1 -1) "]")
2342 f)))
2344 (defcustom org-time-clocksum-format "%d:%02d"
2345 "The format string used when creating CLOCKSUM lines, or when
2346 org-mode generates a time duration."
2347 :group 'org-time
2348 :type 'string)
2350 (defcustom org-time-clocksum-use-fractional nil
2351 "If non-nil, \\[org-clock-display] uses fractional times.
2352 org-mode generates a time duration."
2353 :group 'org-time
2354 :type 'boolean)
2356 (defcustom org-time-clocksum-fractional-format "%.2f"
2357 "The format string used when creating CLOCKSUM lines, or when
2358 org-mode generates a time duration."
2359 :group 'org-time
2360 :type 'string)
2362 (defcustom org-deadline-warning-days 14
2363 "No. of days before expiration during which a deadline becomes active.
2364 This variable governs the display in sparse trees and in the agenda.
2365 When 0 or negative, it means use this number (the absolute value of it)
2366 even if a deadline has a different individual lead time specified.
2368 Custom commands can set this variable in the options section."
2369 :group 'org-time
2370 :group 'org-agenda-daily/weekly
2371 :type 'integer)
2373 (defcustom org-read-date-prefer-future t
2374 "Non-nil means assume future for incomplete date input from user.
2375 This affects the following situations:
2376 1. The user gives a month but not a year.
2377 For example, if it is april and you enter \"feb 2\", this will be read
2378 as feb 2, *next* year. \"May 5\", however, will be this year.
2379 2. The user gives a day, but no month.
2380 For example, if today is the 15th, and you enter \"3\", Org-mode will
2381 read this as the third of *next* month. However, if you enter \"17\",
2382 it will be considered as *this* month.
2384 If you set this variable to the symbol `time', then also the following
2385 will work:
2387 3. If the user gives a time, but no day. If the time is before now,
2388 to will be interpreted as tomorrow.
2390 Currently none of this works for ISO week specifications.
2392 When this option is nil, the current day, month and year will always be
2393 used as defaults."
2394 :group 'org-time
2395 :type '(choice
2396 (const :tag "Never" nil)
2397 (const :tag "Check month and day" t)
2398 (const :tag "Check month, day, and time" time)))
2400 (defcustom org-read-date-display-live t
2401 "Non-nil means display current interpretation of date prompt live.
2402 This display will be in an overlay, in the minibuffer."
2403 :group 'org-time
2404 :type 'boolean)
2406 (defcustom org-read-date-popup-calendar t
2407 "Non-nil means pop up a calendar when prompting for a date.
2408 In the calendar, the date can be selected with mouse-1. However, the
2409 minibuffer will also be active, and you can simply enter the date as well.
2410 When nil, only the minibuffer will be available."
2411 :group 'org-time
2412 :type 'boolean)
2413 (if (fboundp 'defvaralias)
2414 (defvaralias 'org-popup-calendar-for-date-prompt
2415 'org-read-date-popup-calendar))
2417 (defcustom org-read-date-minibuffer-setup-hook nil
2418 "Hook to be used to set up keys for the date/time interface.
2419 Add key definitions to `minibuffer-local-map', which will be a temporary
2420 copy."
2421 :group 'org-time
2422 :type 'hook)
2424 (defcustom org-extend-today-until 0
2425 "The hour when your day really ends. Must be an integer.
2426 This has influence for the following applications:
2427 - When switching the agenda to \"today\". It it is still earlier than
2428 the time given here, the day recognized as TODAY is actually yesterday.
2429 - When a date is read from the user and it is still before the time given
2430 here, the current date and time will be assumed to be yesterday, 23:59.
2431 Also, timestamps inserted in remember templates follow this rule.
2433 IMPORTANT: This is a feature whose implementation is and likely will
2434 remain incomplete. Really, it is only here because past midnight seems to
2435 be the favorite working time of John Wiegley :-)"
2436 :group 'org-time
2437 :type 'integer)
2439 (defcustom org-edit-timestamp-down-means-later nil
2440 "Non-nil means S-down will increase the time in a time stamp.
2441 When nil, S-up will increase."
2442 :group 'org-time
2443 :type 'boolean)
2445 (defcustom org-calendar-follow-timestamp-change t
2446 "Non-nil means make the calendar window follow timestamp changes.
2447 When a timestamp is modified and the calendar window is visible, it will be
2448 moved to the new date."
2449 :group 'org-time
2450 :type 'boolean)
2452 (defgroup org-tags nil
2453 "Options concerning tags in Org-mode."
2454 :tag "Org Tags"
2455 :group 'org)
2457 (defcustom org-tag-alist nil
2458 "List of tags allowed in Org-mode files.
2459 When this list is nil, Org-mode will base TAG input on what is already in the
2460 buffer.
2461 The value of this variable is an alist, the car of each entry must be a
2462 keyword as a string, the cdr may be a character that is used to select
2463 that tag through the fast-tag-selection interface.
2464 See the manual for details."
2465 :group 'org-tags
2466 :type '(repeat
2467 (choice
2468 (cons (string :tag "Tag name")
2469 (character :tag "Access char"))
2470 (list :tag "Start radio group"
2471 (const :startgroup)
2472 (option (string :tag "Group description")))
2473 (list :tag "End radio group"
2474 (const :endgroup)
2475 (option (string :tag "Group description")))
2476 (const :tag "New line" (:newline)))))
2478 (defcustom org-tag-persistent-alist nil
2479 "List of tags that will always appear in all Org-mode files.
2480 This is in addition to any in buffer settings or customizations
2481 of `org-tag-alist'.
2482 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2483 The value of this variable is an alist, the car of each entry must be a
2484 keyword as a string, the cdr may be a character that is used to select
2485 that tag through the fast-tag-selection interface.
2486 See the manual for details.
2487 To disable these tags on a per-file basis, insert anywhere in the file:
2488 #+STARTUP: noptag"
2489 :group 'org-tags
2490 :type '(repeat
2491 (choice
2492 (cons (string :tag "Tag name")
2493 (character :tag "Access char"))
2494 (const :tag "Start radio group" (:startgroup))
2495 (const :tag "End radio group" (:endgroup))
2496 (const :tag "New line" (:newline)))))
2498 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2499 "If non-nil, always offer completion for all tags of all agenda files.
2500 Instead of customizing this variable directly, you might want to
2501 set it locally for remember buffers, because there no list of
2502 tags in that file can be created dynamically (there are none).
2504 (add-hook 'org-remember-mode-hook
2505 (lambda ()
2506 (set (make-local-variable
2507 'org-complete-tags-always-offer-all-agenda-tags)
2508 t)))"
2509 :group 'org-tags
2510 :type 'boolean)
2512 (defvar org-file-tags nil
2513 "List of tags that can be inherited by all entries in the file.
2514 The tags will be inherited if the variable `org-use-tag-inheritance'
2515 says they should be.
2516 This variable is populated from #+FILETAGS lines.")
2518 (defcustom org-use-fast-tag-selection 'auto
2519 "Non-nil means use fast tag selection scheme.
2520 This is a special interface to select and deselect tags with single keys.
2521 When nil, fast selection is never used.
2522 When the symbol `auto', fast selection is used if and only if selection
2523 characters for tags have been configured, either through the variable
2524 `org-tag-alist' or through a #+TAGS line in the buffer.
2525 When t, fast selection is always used and selection keys are assigned
2526 automatically if necessary."
2527 :group 'org-tags
2528 :type '(choice
2529 (const :tag "Always" t)
2530 (const :tag "Never" nil)
2531 (const :tag "When selection characters are configured" 'auto)))
2533 (defcustom org-fast-tag-selection-single-key nil
2534 "Non-nil means fast tag selection exits after first change.
2535 When nil, you have to press RET to exit it.
2536 During fast tag selection, you can toggle this flag with `C-c'.
2537 This variable can also have the value `expert'. In this case, the window
2538 displaying the tags menu is not even shown, until you press C-c again."
2539 :group 'org-tags
2540 :type '(choice
2541 (const :tag "No" nil)
2542 (const :tag "Yes" t)
2543 (const :tag "Expert" expert)))
2545 (defvar org-fast-tag-selection-include-todo nil
2546 "Non-nil means fast tags selection interface will also offer TODO states.
2547 This is an undocumented feature, you should not rely on it.")
2549 (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
2550 "The column to which tags should be indented in a headline.
2551 If this number is positive, it specifies the column. If it is negative,
2552 it means that the tags should be flushright to that column. For example,
2553 -80 works well for a normal 80 character screen."
2554 :group 'org-tags
2555 :type 'integer)
2557 (defcustom org-auto-align-tags t
2558 "Non-nil means realign tags after pro/demotion of TODO state change.
2559 These operations change the length of a headline and therefore shift
2560 the tags around. With this options turned on, after each such operation
2561 the tags are again aligned to `org-tags-column'."
2562 :group 'org-tags
2563 :type 'boolean)
2565 (defcustom org-use-tag-inheritance t
2566 "Non-nil means tags in levels apply also for sublevels.
2567 When nil, only the tags directly given in a specific line apply there.
2568 This may also be a list of tags that should be inherited, or a regexp that
2569 matches tags that should be inherited. Additional control is possible
2570 with the variable `org-tags-exclude-from-inheritance' which gives an
2571 explicit list of tags to be excluded from inheritance., even if the value of
2572 `org-use-tag-inheritance' would select it for inheritance.
2574 If this option is t, a match early-on in a tree can lead to a large
2575 number of matches in the subtree when constructing the agenda or creating
2576 a sparse tree. If you only want to see the first match in a tree during
2577 a search, check out the variable `org-tags-match-list-sublevels'."
2578 :group 'org-tags
2579 :type '(choice
2580 (const :tag "Not" nil)
2581 (const :tag "Always" t)
2582 (repeat :tag "Specific tags" (string :tag "Tag"))
2583 (regexp :tag "Tags matched by regexp")))
2585 (defcustom org-tags-exclude-from-inheritance nil
2586 "List of tags that should never be inherited.
2587 This is a way to exclude a few tags from inheritance. For way to do
2588 the opposite, to actively allow inheritance for selected tags,
2589 see the variable `org-use-tag-inheritance'."
2590 :group 'org-tags
2591 :type '(repeat (string :tag "Tag")))
2593 (defun org-tag-inherit-p (tag)
2594 "Check if TAG is one that should be inherited."
2595 (cond
2596 ((member tag org-tags-exclude-from-inheritance) nil)
2597 ((eq org-use-tag-inheritance t) t)
2598 ((not org-use-tag-inheritance) nil)
2599 ((stringp org-use-tag-inheritance)
2600 (string-match org-use-tag-inheritance tag))
2601 ((listp org-use-tag-inheritance)
2602 (member tag org-use-tag-inheritance))
2603 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
2605 (defcustom org-tags-match-list-sublevels t
2606 "Non-nil means list also sublevels of headlines matching a search.
2607 This variable applies to tags/property searches, and also to stuck
2608 projects because this search is based on a tags match as well.
2610 When set to the symbol `indented', sublevels are indented with
2611 leading dots.
2613 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2614 the sublevels of a headline matching a tag search often also match
2615 the same search. Listing all of them can create very long lists.
2616 Setting this variable to nil causes subtrees of a match to be skipped.
2618 This variable is semi-obsolete and probably should always be true. It
2619 is better to limit inheritance to certain tags using the variables
2620 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
2621 :group 'org-tags
2622 :type '(choice
2623 (const :tag "No, don't list them" nil)
2624 (const :tag "Yes, do list them" t)
2625 (const :tag "List them, indented with leading dots" indented)))
2627 (defcustom org-tags-sort-function nil
2628 "When set, tags are sorted using this function as a comparator"
2629 :group 'org-tags
2630 :type '(choice
2631 (const :tag "No sorting" nil)
2632 (const :tag "Alphabetical" string<)
2633 (const :tag "Reverse alphabetical" string>)
2634 (function :tag "Custom function" nil)))
2636 (defvar org-tags-history nil
2637 "History of minibuffer reads for tags.")
2638 (defvar org-last-tags-completion-table nil
2639 "The last used completion table for tags.")
2640 (defvar org-after-tags-change-hook nil
2641 "Hook that is run after the tags in a line have changed.")
2643 (defgroup org-properties nil
2644 "Options concerning properties in Org-mode."
2645 :tag "Org Properties"
2646 :group 'org)
2648 (defcustom org-property-format "%-10s %s"
2649 "How property key/value pairs should be formatted by `indent-line'.
2650 When `indent-line' hits a property definition, it will format the line
2651 according to this format, mainly to make sure that the values are
2652 lined-up with respect to each other."
2653 :group 'org-properties
2654 :type 'string)
2656 (defcustom org-use-property-inheritance nil
2657 "Non-nil means properties apply also for sublevels.
2659 This setting is chiefly used during property searches. Turning it on can
2660 cause significant overhead when doing a search, which is why it is not
2661 on by default.
2663 When nil, only the properties directly given in the current entry count.
2664 When t, every property is inherited. The value may also be a list of
2665 properties that should have inheritance, or a regular expression matching
2666 properties that should be inherited.
2668 However, note that some special properties use inheritance under special
2669 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2670 and the properties ending in \"_ALL\" when they are used as descriptor
2671 for valid values of a property.
2673 Note for programmers:
2674 When querying an entry with `org-entry-get', you can control if inheritance
2675 should be used. By default, `org-entry-get' looks only at the local
2676 properties. You can request inheritance by setting the inherit argument
2677 to t (to force inheritance) or to `selective' (to respect the setting
2678 in this variable)."
2679 :group 'org-properties
2680 :type '(choice
2681 (const :tag "Not" nil)
2682 (const :tag "Always" t)
2683 (repeat :tag "Specific properties" (string :tag "Property"))
2684 (regexp :tag "Properties matched by regexp")))
2686 (defun org-property-inherit-p (property)
2687 "Check if PROPERTY is one that should be inherited."
2688 (cond
2689 ((eq org-use-property-inheritance t) t)
2690 ((not org-use-property-inheritance) nil)
2691 ((stringp org-use-property-inheritance)
2692 (string-match org-use-property-inheritance property))
2693 ((listp org-use-property-inheritance)
2694 (member property org-use-property-inheritance))
2695 (t (error "Invalid setting of `org-use-property-inheritance'"))))
2697 (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
2698 "The default column format, if no other format has been defined.
2699 This variable can be set on the per-file basis by inserting a line
2701 #+COLUMNS: %25ITEM ....."
2702 :group 'org-properties
2703 :type 'string)
2705 (defcustom org-columns-ellipses ".."
2706 "The ellipses to be used when a field in column view is truncated.
2707 When this is the empty string, as many characters as possible are shown,
2708 but then there will be no visual indication that the field has been truncated.
2709 When this is a string of length N, the last N characters of a truncated
2710 field are replaced by this string. If the column is narrower than the
2711 ellipses string, only part of the ellipses string will be shown."
2712 :group 'org-properties
2713 :type 'string)
2715 (defcustom org-columns-modify-value-for-display-function nil
2716 "Function that modifies values for display in column view.
2717 For example, it can be used to cut out a certain part from a time stamp.
2718 The function must take 2 arguments:
2720 column-title The title of the column (*not* the property name)
2721 value The value that should be modified.
2723 The function should return the value that should be displayed,
2724 or nil if the normal value should be used."
2725 :group 'org-properties
2726 :type 'function)
2728 (defcustom org-effort-property "Effort"
2729 "The property that is being used to keep track of effort estimates.
2730 Effort estimates given in this property need to have the format H:MM."
2731 :group 'org-properties
2732 :group 'org-progress
2733 :type '(string :tag "Property"))
2735 (defconst org-global-properties-fixed
2736 '(("VISIBILITY_ALL" . "folded children content all")
2737 ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
2738 "List of property/value pairs that can be inherited by any entry.
2740 These are fixed values, for the preset properties. The user variable
2741 that can be used to add to this list is `org-global-properties'.
2743 The entries in this list are cons cells where the car is a property
2744 name and cdr is a string with the value. If the value represents
2745 multiple items like an \"_ALL\" property, separate the items by
2746 spaces.")
2748 (defcustom org-global-properties nil
2749 "List of property/value pairs that can be inherited by any entry.
2751 This list will be combined with the constant `org-global-properties-fixed'.
2753 The entries in this list are cons cells where the car is a property
2754 name and cdr is a string with the value.
2756 You can set buffer-local values for the same purpose in the variable
2757 `org-file-properties' this by adding lines like
2759 #+PROPERTY: NAME VALUE"
2760 :group 'org-properties
2761 :type '(repeat
2762 (cons (string :tag "Property")
2763 (string :tag "Value"))))
2765 (defvar org-file-properties nil
2766 "List of property/value pairs that can be inherited by any entry.
2767 Valid for the current buffer.
2768 This variable is populated from #+PROPERTY lines.")
2769 (make-variable-buffer-local 'org-file-properties)
2771 (defgroup org-agenda nil
2772 "Options concerning agenda views in Org-mode."
2773 :tag "Org Agenda"
2774 :group 'org)
2776 (defvar org-category nil
2777 "Variable used by org files to set a category for agenda display.
2778 Such files should use a file variable to set it, for example
2780 # -*- mode: org; org-category: \"ELisp\"
2782 or contain a special line
2784 #+CATEGORY: ELisp
2786 If the file does not specify a category, then file's base name
2787 is used instead.")
2788 (make-variable-buffer-local 'org-category)
2789 (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
2791 (defcustom org-agenda-files nil
2792 "The files to be used for agenda display.
2793 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2794 \\[org-remove-file]. You can also use customize to edit the list.
2796 If an entry is a directory, all files in that directory that are matched by
2797 `org-agenda-file-regexp' will be part of the file list.
2799 If the value of the variable is not a list but a single file name, then
2800 the list of agenda files is actually stored and maintained in that file, one
2801 agenda file per line. In this file paths can be given relative to
2802 `org-directory'. Tilde expansion and environment variable substitution
2803 are also made."
2804 :group 'org-agenda
2805 :type '(choice
2806 (repeat :tag "List of files and directories" file)
2807 (file :tag "Store list in a file\n" :value "~/.agenda_files")))
2809 (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
2810 "Regular expression to match files for `org-agenda-files'.
2811 If any element in the list in that variable contains a directory instead
2812 of a normal file, all files in that directory that are matched by this
2813 regular expression will be included."
2814 :group 'org-agenda
2815 :type 'regexp)
2817 (defcustom org-agenda-text-search-extra-files nil
2818 "List of extra files to be searched by text search commands.
2819 These files will be search in addition to the agenda files by the
2820 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
2821 Note that these files will only be searched for text search commands,
2822 not for the other agenda views like todo lists, tag searches or the weekly
2823 agenda. This variable is intended to list notes and possibly archive files
2824 that should also be searched by these two commands.
2825 In fact, if the first element in the list is the symbol `agenda-archives',
2826 than all archive files of all agenda files will be added to the search
2827 scope."
2828 :group 'org-agenda
2829 :type '(set :greedy t
2830 (const :tag "Agenda Archives" agenda-archives)
2831 (repeat :inline t (file))))
2833 (if (fboundp 'defvaralias)
2834 (defvaralias 'org-agenda-multi-occur-extra-files
2835 'org-agenda-text-search-extra-files))
2837 (defcustom org-agenda-skip-unavailable-files nil
2838 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
2839 A nil value means to remove them, after a query, from the list."
2840 :group 'org-agenda
2841 :type 'boolean)
2843 (defcustom org-calendar-to-agenda-key [?c]
2844 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2845 The command `org-calendar-goto-agenda' will be bound to this key. The
2846 default is the character `c' because then `c' can be used to switch back and
2847 forth between agenda and calendar."
2848 :group 'org-agenda
2849 :type 'sexp)
2851 (defcustom org-calendar-agenda-action-key [?k]
2852 "The key to be installed in `calendar-mode-map' for agenda-action.
2853 The command `org-agenda-action' will be bound to this key. The
2854 default is the character `k' because we use the same key in the agenda."
2855 :group 'org-agenda
2856 :type 'sexp)
2858 (defcustom org-calendar-insert-diary-entry-key [?i]
2859 "The key to be installed in `calendar-mode-map' for adding diary entries.
2860 This option is irrelevant until `org-agenda-diary-file' has been configured
2861 to point to an Org-mode file. When that is the case, the command
2862 `org-agenda-diary-entry' will be bound to the key given here, by default
2863 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
2864 if you want to continue doing this, you need to change this to a different
2865 key."
2866 :group 'org-agenda
2867 :type 'sexp)
2869 (defcustom org-agenda-diary-file 'diary-file
2870 "File to which to add new entries with the `i' key in agenda and calendar.
2871 When this is the symbol `diary-file', the functionality in the Emacs
2872 calendar will be used to add entries to the `diary-file'. But when this
2873 points to a file, `org-agenda-diary-entry' will be used instead."
2874 :group 'org-agenda
2875 :type '(choice
2876 (const :tag "The standard Emacs diary file" diary-file)
2877 (file :tag "Special Org file diary entries")))
2879 (eval-after-load "calendar"
2880 '(progn
2881 (org-defkey calendar-mode-map org-calendar-to-agenda-key
2882 'org-calendar-goto-agenda)
2883 (org-defkey calendar-mode-map org-calendar-agenda-action-key
2884 'org-agenda-action)
2885 (add-hook 'calendar-mode-hook
2886 (lambda ()
2887 (unless (eq org-agenda-diary-file 'diary-file)
2888 (define-key calendar-mode-map
2889 org-calendar-insert-diary-entry-key
2890 'org-agenda-diary-entry))))))
2892 (defgroup org-latex nil
2893 "Options for embedding LaTeX code into Org-mode."
2894 :tag "Org LaTeX"
2895 :group 'org)
2897 (defcustom org-format-latex-options
2898 '(:foreground default :background default :scale 1.0
2899 :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
2900 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
2901 "Options for creating images from LaTeX fragments.
2902 This is a property list with the following properties:
2903 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
2904 `default' means use the foreground of the default face.
2905 :background the background color, or \"Transparent\".
2906 `default' means use the background of the default face.
2907 :scale a scaling factor for the size of the images.
2908 :html-foreground, :html-background, :html-scale
2909 the same numbers for HTML export.
2910 :matchers a list indicating which matchers should be used to
2911 find LaTeX fragments. Valid members of this list are:
2912 \"begin\" find environments
2913 \"$1\" find single characters surrounded by $.$
2914 \"$\" find math expressions surrounded by $...$
2915 \"$$\" find math expressions surrounded by $$....$$
2916 \"\\(\" find math expressions surrounded by \\(...\\)
2917 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2918 :group 'org-latex
2919 :type 'plist)
2921 (defcustom org-format-latex-signal-error t
2922 "Non-nil means signal an error when image creation of LaTeX snippets fails.
2923 When nil, just push out a message."
2924 :group 'org-latex
2925 :type 'boolean)
2927 (defcustom org-format-latex-header "\\documentclass{article}
2928 \\usepackage[usenames]{color}
2929 \\usepackage{amsmath}
2930 \\usepackage[mathscr]{eucal}
2931 \\pagestyle{empty} % do not remove
2932 \[PACKAGES]
2933 \[DEFAULT-PACKAGES]
2934 % The settings below are copied from fullpage.sty
2935 \\setlength{\\textwidth}{\\paperwidth}
2936 \\addtolength{\\textwidth}{-3cm}
2937 \\setlength{\\oddsidemargin}{1.5cm}
2938 \\addtolength{\\oddsidemargin}{-2.54cm}
2939 \\setlength{\\evensidemargin}{\\oddsidemargin}
2940 \\setlength{\\textheight}{\\paperheight}
2941 \\addtolength{\\textheight}{-\\headheight}
2942 \\addtolength{\\textheight}{-\\headsep}
2943 \\addtolength{\\textheight}{-\\footskip}
2944 \\addtolength{\\textheight}{-3cm}
2945 \\setlength{\\topmargin}{1.5cm}
2946 \\addtolength{\\topmargin}{-2.54cm}"
2947 "The document header used for processing LaTeX fragments.
2948 It is imperative that this header make sure that no page number
2949 appears on the page. The package defined in the variables
2950 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
2951 will either replace the placeholder \"[PACKAGES]\" in this header, or they
2952 will be appended."
2953 :group 'org-latex
2954 :type 'string)
2956 (defvar org-format-latex-header-extra nil)
2958 ;; The following variables are defined here because is it also used
2959 ;; when formatting latex fragments. Originally it was part of the
2960 ;; LaTeX exporter, which is why the name includes "export".
2961 (defcustom org-export-latex-default-packages-alist
2962 '(("AUTO" "inputenc")
2963 ("T1" "fontenc")
2964 ("" "fixltx2e")
2965 ("" "graphicx")
2966 ("" "longtable")
2967 ("" "float")
2968 ("" "wrapfig")
2969 ("" "soul")
2970 ("" "t1enc")
2971 ("" "textcomp")
2972 ("" "marvosym")
2973 ("" "wasysym")
2974 ("" "latexsym")
2975 ("" "amssymb")
2976 ("" "hyperref")
2977 "\\tolerance=1000"
2979 "Alist of default packages to be inserted in the header.
2980 Change this only if one of the packages here causes an incompatibility
2981 with another package you are using.
2982 The packages in this list are needed by one part or another of Org-mode
2983 to function properly.
2985 - inputenc, fontenc, t1enc: for basic font and character selection
2986 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
2987 for interpreting the entities in `org-entities'. You can skip some of these
2988 packages if you don't use any of the symbols in it.
2989 - graphicx: for including images
2990 - float, wrapfig: for figure placement
2991 - longtable: for long tables
2992 - hyperref: for cross references
2994 Therefore you should not modify this variable unless you know what you
2995 are doing. The one reason to change it anyway is that you might be loading
2996 some other package that conflicts with one of the default packages.
2997 Each cell is of the format \( \"options\" \"package\" \)."
2998 :group 'org-export-latex
2999 :type '(repeat
3000 (choice
3001 (string :tag "A line of LaTeX")
3002 (list :tag "options/package pair"
3003 (string :tag "options")
3004 (string :tag "package")))))
3006 (defcustom org-export-latex-packages-alist nil
3007 "Alist of packages to be inserted in every LaTeX the header.
3008 These will be inserted after `org-export-latex-default-packages-alist'.
3009 Each cell is of the format \( \"options\" \"package\" \).
3010 Make sure that you only lis packages here which:
3011 - you want in every file
3012 - do not conflict with the default packages in
3013 `org-export-latex-default-packages-alist'
3014 - do not conflict with the setup in `org-format-latex-header'."
3015 :group 'org-export-latex
3016 :type '(repeat
3017 (choice
3018 (string :tag "A line of LaTeX")
3019 (list :tag "options/package pair"
3020 (string :tag "options")
3021 (string :tag "package")))))
3023 (defgroup org-appearance nil
3024 "Settings for Org-mode appearance."
3025 :tag "Org Appearance"
3026 :group 'org)
3028 (defcustom org-level-color-stars-only nil
3029 "Non-nil means fontify only the stars in each headline.
3030 When nil, the entire headline is fontified.
3031 Changing it requires restart of `font-lock-mode' to become effective
3032 also in regions already fontified."
3033 :group 'org-appearance
3034 :type 'boolean)
3036 (defcustom org-hide-leading-stars nil
3037 "Non-nil means hide the first N-1 stars in a headline.
3038 This works by using the face `org-hide' for these stars. This
3039 face is white for a light background, and black for a dark
3040 background. You may have to customize the face `org-hide' to
3041 make this work.
3042 Changing it requires restart of `font-lock-mode' to become effective
3043 also in regions already fontified.
3044 You may also set this on a per-file basis by adding one of the following
3045 lines to the buffer:
3047 #+STARTUP: hidestars
3048 #+STARTUP: showstars"
3049 :group 'org-appearance
3050 :type 'boolean)
3052 (defcustom org-hidden-keywords nil
3053 "List of keywords that should be hidden when typed in the org buffer.
3054 For example, add #+TITLE to this list in order to make the
3055 document title appear in the buffer without the initial #+TITLE:
3056 keyword."
3057 :group 'org-appearance
3058 :type '(set (const :tag "#+AUTHOR" author)
3059 (const :tag "#+DATE" date)
3060 (const :tag "#+EMAIL" email)
3061 (const :tag "#+TITLE" title)))
3063 (defcustom org-fontify-done-headline nil
3064 "Non-nil means change the face of a headline if it is marked DONE.
3065 Normally, only the TODO/DONE keyword indicates the state of a headline.
3066 When this is non-nil, the headline after the keyword is set to the
3067 `org-headline-done' as an additional indication."
3068 :group 'org-appearance
3069 :type 'boolean)
3071 (defcustom org-fontify-emphasized-text t
3072 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3073 Changing this variable requires a restart of Emacs to take effect."
3074 :group 'org-appearance
3075 :type 'boolean)
3077 (defcustom org-fontify-whole-heading-line nil
3078 "Non-nil means fontify the whole line for headings.
3079 This is useful when setting a background color for the
3080 org-level-* faces."
3081 :group 'org-appearance
3082 :type 'boolean)
3084 (defcustom org-highlight-latex-fragments-and-specials nil
3085 "Non-nil means fontify what is treated specially by the exporters."
3086 :group 'org-appearance
3087 :type 'boolean)
3089 (defcustom org-hide-emphasis-markers nil
3090 "Non-nil mean font-lock should hide the emphasis marker characters."
3091 :group 'org-appearance
3092 :type 'boolean)
3094 (defvar org-emph-re nil
3095 "Regular expression for matching emphasis.")
3096 (defvar org-verbatim-re nil
3097 "Regular expression for matching verbatim text.")
3098 (defvar org-emphasis-regexp-components) ; defined just below
3099 (defvar org-emphasis-alist) ; defined just below
3100 (defun org-set-emph-re (var val)
3101 "Set variable and compute the emphasis regular expression."
3102 (set var val)
3103 (when (and (boundp 'org-emphasis-alist)
3104 (boundp 'org-emphasis-regexp-components)
3105 org-emphasis-alist org-emphasis-regexp-components)
3106 (let* ((e org-emphasis-regexp-components)
3107 (pre (car e))
3108 (post (nth 1 e))
3109 (border (nth 2 e))
3110 (body (nth 3 e))
3111 (nl (nth 4 e))
3112 (body1 (concat body "*?"))
3113 (markers (mapconcat 'car org-emphasis-alist ""))
3114 (vmarkers (mapconcat
3115 (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
3116 org-emphasis-alist "")))
3117 ;; make sure special characters appear at the right position in the class
3118 (if (string-match "\\^" markers)
3119 (setq markers (concat (replace-match "" t t markers) "^")))
3120 (if (string-match "-" markers)
3121 (setq markers (concat (replace-match "" t t markers) "-")))
3122 (if (string-match "\\^" vmarkers)
3123 (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
3124 (if (string-match "-" vmarkers)
3125 (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
3126 (if (> nl 0)
3127 (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
3128 (int-to-string nl) "\\}")))
3129 ;; Make the regexp
3130 (setq org-emph-re
3131 (concat "\\([" pre "]\\|^\\)"
3132 "\\("
3133 "\\([" markers "]\\)"
3134 "\\("
3135 "[^" border "]\\|"
3136 "[^" border "]"
3137 body1
3138 "[^" border "]"
3139 "\\)"
3140 "\\3\\)"
3141 "\\([" post "]\\|$\\)"))
3142 (setq org-verbatim-re
3143 (concat "\\([" pre "]\\|^\\)"
3144 "\\("
3145 "\\([" vmarkers "]\\)"
3146 "\\("
3147 "[^" border "]\\|"
3148 "[^" border "]"
3149 body1
3150 "[^" border "]"
3151 "\\)"
3152 "\\3\\)"
3153 "\\([" post "]\\|$\\)")))))
3155 (defcustom org-emphasis-regexp-components
3156 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3157 "Components used to build the regular expression for emphasis.
3158 This is a list with 6 entries. Terminology: In an emphasis string
3159 like \" *strong word* \", we call the initial space PREMATCH, the final
3160 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3161 and \"trong wor\" is the body. The different components in this variable
3162 specify what is allowed/forbidden in each part:
3164 pre Chars allowed as prematch. Beginning of line will be allowed too.
3165 post Chars allowed as postmatch. End of line will be allowed too.
3166 border The chars *forbidden* as border characters.
3167 body-regexp A regexp like \".\" to match a body character. Don't use
3168 non-shy groups here, and don't allow newline here.
3169 newline The maximum number of newlines allowed in an emphasis exp.
3171 Use customize to modify this, or restart Emacs after changing it."
3172 :group 'org-appearance
3173 :set 'org-set-emph-re
3174 :type '(list
3175 (sexp :tag "Allowed chars in pre ")
3176 (sexp :tag "Allowed chars in post ")
3177 (sexp :tag "Forbidden chars in border ")
3178 (sexp :tag "Regexp for body ")
3179 (integer :tag "number of newlines allowed")
3180 (option (boolean :tag "Please ignore this button"))))
3182 (defcustom org-emphasis-alist
3183 `(("*" bold "<b>" "</b>")
3184 ("/" italic "<i>" "</i>")
3185 ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
3186 ("=" org-code "<code>" "</code>" verbatim)
3187 ("~" org-verbatim "<code>" "</code>" verbatim)
3188 ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
3189 "<del>" "</del>")
3191 "Special syntax for emphasized text.
3192 Text starting and ending with a special character will be emphasized, for
3193 example *bold*, _underlined_ and /italic/. This variable sets the marker
3194 characters, the face to be used by font-lock for highlighting in Org-mode
3195 Emacs buffers, and the HTML tags to be used for this.
3196 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3197 Use customize to modify this, or restart Emacs after changing it."
3198 :group 'org-appearance
3199 :set 'org-set-emph-re
3200 :type '(repeat
3201 (list
3202 (string :tag "Marker character")
3203 (choice
3204 (face :tag "Font-lock-face")
3205 (plist :tag "Face property list"))
3206 (string :tag "HTML start tag")
3207 (string :tag "HTML end tag")
3208 (option (const verbatim)))))
3210 (defvar org-protecting-blocks
3211 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3212 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3213 This is needed for font-lock setup.")
3215 ;;; Miscellaneous options
3217 (defgroup org-completion nil
3218 "Completion in Org-mode."
3219 :tag "Org Completion"
3220 :group 'org)
3222 (defcustom org-completion-use-ido nil
3223 "Non-nil means use ido completion wherever possible.
3224 Note that `ido-mode' must be active for this variable to be relevant.
3225 If you decide to turn this variable on, you might well want to turn off
3226 `org-outline-path-complete-in-steps'.
3227 See also `org-completion-use-iswitchb'."
3228 :group 'org-completion
3229 :type 'boolean)
3231 (defcustom org-completion-use-iswitchb nil
3232 "Non-nil means use iswitchb completion wherever possible.
3233 Note that `iswitchb-mode' must be active for this variable to be relevant.
3234 If you decide to turn this variable on, you might well want to turn off
3235 `org-outline-path-complete-in-steps'.
3236 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3237 :group 'org-completion
3238 :type 'boolean)
3240 (defcustom org-completion-fallback-command 'hippie-expand
3241 "The expansion command called by \\[org-complete] in normal context.
3242 Normal means no org-mode-specific context."
3243 :group 'org-completion
3244 :type 'function)
3246 ;;; Functions and variables from their packages
3247 ;; Declared here to avoid compiler warnings
3249 ;; XEmacs only
3250 (defvar outline-mode-menu-heading)
3251 (defvar outline-mode-menu-show)
3252 (defvar outline-mode-menu-hide)
3253 (defvar zmacs-regions) ; XEmacs regions
3255 ;; Emacs only
3256 (defvar mark-active)
3258 ;; Various packages
3259 (declare-function calendar-absolute-from-iso "cal-iso" (date))
3260 (declare-function calendar-forward-day "cal-move" (arg))
3261 (declare-function calendar-goto-date "cal-move" (date))
3262 (declare-function calendar-goto-today "cal-move" ())
3263 (declare-function calendar-iso-from-absolute "cal-iso" (date))
3264 (defvar calc-embedded-close-formula)
3265 (defvar calc-embedded-open-formula)
3266 (declare-function cdlatex-tab "ext:cdlatex" ())
3267 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
3268 (defvar font-lock-unfontify-region-function)
3269 (declare-function iswitchb-read-buffer "iswitchb"
3270 (prompt &optional default require-match start matches-set))
3271 (defvar iswitchb-temp-buflist)
3272 (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
3273 (defvar org-agenda-tags-todo-honor-ignore-options)
3274 (declare-function org-agenda-skip "org-agenda" ())
3275 (declare-function
3276 org-format-agenda-item "org-agenda"
3277 (extra txt &optional category tags dotime noprefix remove-re habitp))
3278 (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
3279 (declare-function org-agenda-change-all-lines "org-agenda"
3280 (newhead hdmarker &optional fixface just-this))
3281 (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
3282 (declare-function org-agenda-maybe-redo "org-agenda" ())
3283 (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
3284 (beg end))
3285 (declare-function org-agenda-copy-local-variable "org-agenda" (var))
3286 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3287 "org-agenda" (&optional end))
3288 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
3289 (declare-function org-indent-mode "org-indent" (&optional arg))
3290 (declare-function parse-time-string "parse-time" (string))
3291 (declare-function org-attach-reveal "org-attach" (&optional if-exists))
3292 (defvar remember-data-file)
3293 (defvar texmathp-why)
3294 (declare-function speedbar-line-directory "speedbar" (&optional depth))
3295 (declare-function table--at-cell-p "table" (position &optional object at-column))
3297 (defvar w3m-current-url)
3298 (defvar w3m-current-title)
3300 (defvar org-latex-regexps)
3302 ;;; Autoload and prepare some org modules
3304 ;; Some table stuff that needs to be defined here, because it is used
3305 ;; by the functions setting up org-mode or checking for table context.
3307 (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
3308 "Detects an org-type or table-type table.")
3309 (defconst org-table-line-regexp "^[ \t]*|"
3310 "Detects an org-type table line.")
3311 (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
3312 "Detects an org-type table line.")
3313 (defconst org-table-hline-regexp "^[ \t]*|-"
3314 "Detects an org-type table hline.")
3315 (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
3316 "Detects a table-type table hline.")
3317 (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
3318 "Searching from within a table (any type) this finds the first line
3319 outside the table.")
3321 ;; Autoload the functions in org-table.el that are needed by functions here.
3323 (eval-and-compile
3324 (org-autoload "org-table"
3325 '(org-table-align org-table-begin org-table-blank-field
3326 org-table-convert org-table-convert-region org-table-copy-down
3327 org-table-copy-region org-table-create
3328 org-table-create-or-convert-from-region
3329 org-table-create-with-table.el org-table-current-dline
3330 org-table-cut-region org-table-delete-column org-table-edit-field
3331 org-table-edit-formulas org-table-end org-table-eval-formula
3332 org-table-export org-table-field-info
3333 org-table-get-stored-formulas org-table-goto-column
3334 org-table-hline-and-move org-table-import org-table-insert-column
3335 org-table-insert-hline org-table-insert-row org-table-iterate
3336 org-table-justify-field-maybe org-table-kill-row
3337 org-table-maybe-eval-formula org-table-maybe-recalculate-line
3338 org-table-move-column org-table-move-column-left
3339 org-table-move-column-right org-table-move-row
3340 org-table-move-row-down org-table-move-row-up
3341 org-table-next-field org-table-next-row org-table-paste-rectangle
3342 org-table-previous-field org-table-recalculate
3343 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
3344 org-table-toggle-coordinate-overlays
3345 org-table-toggle-formula-debugger org-table-wrap-region
3346 orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
3348 (defun org-at-table-p (&optional table-type)
3349 "Return t if the cursor is inside an org-type table.
3350 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3351 (if org-enable-table-editor
3352 (save-excursion
3353 (beginning-of-line 1)
3354 (looking-at (if table-type org-table-any-line-regexp
3355 org-table-line-regexp)))
3356 nil))
3357 (defsubst org-table-p () (org-at-table-p))
3359 (defun org-at-table.el-p ()
3360 "Return t if and only if we are at a table.el table."
3361 (and (org-at-table-p 'any)
3362 (save-excursion
3363 (goto-char (org-table-begin 'any))
3364 (looking-at org-table1-hline-regexp))))
3365 (defun org-table-recognize-table.el ()
3366 "If there is a table.el table nearby, recognize it and move into it."
3367 (if org-table-tab-recognizes-table.el
3368 (if (org-at-table.el-p)
3369 (progn
3370 (beginning-of-line 1)
3371 (if (looking-at org-table-dataline-regexp)
3373 (if (looking-at org-table1-hline-regexp)
3374 (progn
3375 (beginning-of-line 2)
3376 (if (looking-at org-table-any-border-regexp)
3377 (beginning-of-line -1)))))
3378 (if (re-search-forward "|" (org-table-end t) t)
3379 (progn
3380 (require 'table)
3381 (if (table--at-cell-p (point))
3383 (message "recognizing table.el table...")
3384 (table-recognize-table)
3385 (message "recognizing table.el table...done")))
3386 (error "This should not happen..."))
3388 nil)
3389 nil))
3391 (defun org-at-table-hline-p ()
3392 "Return t if the cursor is inside a hline in a table."
3393 (if org-enable-table-editor
3394 (save-excursion
3395 (beginning-of-line 1)
3396 (looking-at org-table-hline-regexp))
3397 nil))
3399 (defvar org-table-clean-did-remove-column nil)
3401 (defun org-table-map-tables (function)
3402 "Apply FUNCTION to the start of all tables in the buffer."
3403 (save-excursion
3404 (save-restriction
3405 (widen)
3406 (goto-char (point-min))
3407 (while (re-search-forward org-table-any-line-regexp nil t)
3408 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
3409 (beginning-of-line 1)
3410 (when (looking-at org-table-line-regexp)
3411 (save-excursion (funcall function))
3412 (or (looking-at org-table-line-regexp)
3413 (forward-char 1)))
3414 (re-search-forward org-table-any-border-regexp nil 1))))
3415 (message "Mapping tables: done"))
3417 ;; Declare and autoload functions from org-exp.el & Co
3419 (declare-function org-default-export-plist "org-exp")
3420 (declare-function org-infile-export-plist "org-exp")
3421 (declare-function org-get-current-options "org-exp")
3422 (eval-and-compile
3423 (org-autoload "org-exp"
3424 '(org-export org-export-visible
3425 org-insert-export-options-template
3426 org-table-clean-before-export))
3427 (org-autoload "org-ascii"
3428 '(org-export-as-ascii org-export-ascii-preprocess
3429 org-export-as-ascii-to-buffer org-replace-region-by-ascii
3430 org-export-region-as-ascii))
3431 (org-autoload "org-latex"
3432 '(org-export-as-latex-batch org-export-as-latex-to-buffer
3433 org-replace-region-by-latex org-export-region-as-latex
3434 org-export-as-latex org-export-as-pdf
3435 org-export-as-pdf-and-open))
3436 (org-autoload "org-html"
3437 '(org-export-as-html-and-open
3438 org-export-as-html-batch org-export-as-html-to-buffer
3439 org-replace-region-by-html org-export-region-as-html
3440 org-export-as-html))
3441 (org-autoload "org-docbook"
3442 '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
3443 org-replace-region-by-docbook org-export-region-as-docbook
3444 org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
3445 org-export-as-docbook))
3446 (org-autoload "org-icalendar"
3447 '(org-export-icalendar-this-file
3448 org-export-icalendar-all-agenda-files
3449 org-export-icalendar-combine-agenda-files))
3450 (org-autoload "org-xoxo" '(org-export-as-xoxo))
3451 (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
3453 ;; Declare and autoload functions from org-agenda.el
3455 (eval-and-compile
3456 (org-autoload "org-agenda"
3457 '(org-agenda org-agenda-list org-search-view
3458 org-todo-list org-tags-view org-agenda-list-stuck-projects
3459 org-diary org-agenda-to-appt
3460 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
3462 ;; Autoload org-remember
3464 (eval-and-compile
3465 (org-autoload "org-remember"
3466 '(org-remember-insinuate org-remember-annotation
3467 org-remember-apply-template org-remember org-remember-handler)))
3469 ;; Autoload org-clock.el
3472 (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
3473 (beg end))
3474 (declare-function org-clock-update-mode-line "org-clock" ())
3475 (declare-function org-resolve-clocks "org-clock"
3476 (&optional also-non-dangling-p prompt last-valid))
3477 (defvar org-clock-start-time)
3478 (defvar org-clock-marker (make-marker)
3479 "Marker recording the last clock-in.")
3480 (defvar org-clock-hd-marker (make-marker)
3481 "Marker recording the last clock-in, but the headline position.")
3482 (defvar org-clock-heading ""
3483 "The heading of the current clock entry.")
3484 (defun org-clock-is-active ()
3485 "Return non-nil if clock is currently running.
3486 The return value is actually the clock marker."
3487 (marker-buffer org-clock-marker))
3489 (eval-and-compile
3490 (org-autoload
3491 "org-clock"
3492 '(org-clock-in org-clock-out org-clock-cancel
3493 org-clock-goto org-clock-sum org-clock-display
3494 org-clock-remove-overlays org-clock-report
3495 org-clocktable-shift org-dblock-write:clocktable
3496 org-get-clocktable org-resolve-clocks)))
3498 (defun org-clock-update-time-maybe ()
3499 "If this is a CLOCK line, update it and return t.
3500 Otherwise, return nil."
3501 (interactive)
3502 (save-excursion
3503 (beginning-of-line 1)
3504 (skip-chars-forward " \t")
3505 (when (looking-at org-clock-string)
3506 (let ((re (concat "[ \t]*" org-clock-string
3507 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
3508 "\\([ \t]*=>.*\\)?\\)?"))
3509 ts te h m s neg)
3510 (cond
3511 ((not (looking-at re))
3512 nil)
3513 ((not (match-end 2))
3514 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
3515 (> org-clock-marker (point))
3516 (<= org-clock-marker (point-at-eol)))
3517 ;; The clock is running here
3518 (setq org-clock-start-time
3519 (apply 'encode-time
3520 (org-parse-time-string (match-string 1))))
3521 (org-clock-update-mode-line)))
3523 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
3524 (end-of-line 1)
3525 (setq ts (match-string 1)
3526 te (match-string 3))
3527 (setq s (- (org-float-time
3528 (apply 'encode-time (org-parse-time-string te)))
3529 (org-float-time
3530 (apply 'encode-time (org-parse-time-string ts))))
3531 neg (< s 0)
3532 s (abs s)
3533 h (floor (/ s 3600))
3534 s (- s (* 3600 h))
3535 m (floor (/ s 60))
3536 s (- s (* 60 s)))
3537 (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
3538 t))))))
3540 (defun org-check-running-clock ()
3541 "Check if the current buffer contains the running clock.
3542 If yes, offer to stop it and to save the buffer with the changes."
3543 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
3544 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
3545 (buffer-name))))
3546 (org-clock-out)
3547 (when (y-or-n-p "Save changed buffer?")
3548 (save-buffer))))
3550 (defun org-clocktable-try-shift (dir n)
3551 "Check if this line starts a clock table, if yes, shift the time block."
3552 (when (org-match-line "#\\+BEGIN: clocktable\\>")
3553 (org-clocktable-shift dir n)))
3555 ;; Autoload org-timer.el
3557 (eval-and-compile
3558 (org-autoload
3559 "org-timer"
3560 '(org-timer-start org-timer org-timer-item
3561 org-timer-change-times-in-region
3562 org-timer-set-timer
3563 org-timer-reset-timers
3564 org-timer-show-remaining-time)))
3566 ;; Autoload org-feed.el
3568 (eval-and-compile
3569 (org-autoload
3570 "org-feed"
3571 '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
3574 ;; Autoload org-indent.el
3576 ;; Define the variable already here, to make sure we have it.
3577 (defvar org-indent-mode nil
3578 "Non-nil if Org-Indent mode is enabled.
3579 Use the command `org-indent-mode' to change this variable.")
3581 (eval-and-compile
3582 (org-autoload
3583 "org-indent"
3584 '(org-indent-mode)))
3586 ;; Autoload org-mobile.el
3588 (eval-and-compile
3589 (org-autoload
3590 "org-mobile"
3591 '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
3593 ;; Autoload archiving code
3594 ;; The stuff that is needed for cycling and tags has to be defined here.
3596 (defgroup org-archive nil
3597 "Options concerning archiving in Org-mode."
3598 :tag "Org Archive"
3599 :group 'org-structure)
3601 (defcustom org-archive-location "%s_archive::"
3602 "The location where subtrees should be archived.
3604 The value of this variable is a string, consisting of two parts,
3605 separated by a double-colon. The first part is a filename and
3606 the second part is a headline.
3608 When the filename is omitted, archiving happens in the same file.
3609 %s in the filename will be replaced by the current file
3610 name (without the directory part). Archiving to a different file
3611 is useful to keep archived entries from contributing to the
3612 Org-mode Agenda.
3614 The archived entries will be filed as subtrees of the specified
3615 headline. When the headline is omitted, the subtrees are simply
3616 filed away at the end of the file, as top-level entries. Also in
3617 the heading you can use %s to represent the file name, this can be
3618 useful when using the same archive for a number of different files.
3620 Here are a few examples:
3621 \"%s_archive::\"
3622 If the current file is Projects.org, archive in file
3623 Projects.org_archive, as top-level trees. This is the default.
3625 \"::* Archived Tasks\"
3626 Archive in the current file, under the top-level headline
3627 \"* Archived Tasks\".
3629 \"~/org/archive.org::\"
3630 Archive in file ~/org/archive.org (absolute path), as top-level trees.
3632 \"~/org/archive.org::From %s\"
3633 Archive in file ~/org/archive.org (absolute path), under headlines
3634 \"From FILENAME\" where file name is the current file name.
3636 \"basement::** Finished Tasks\"
3637 Archive in file ./basement (relative path), as level 3 trees
3638 below the level 2 heading \"** Finished Tasks\".
3640 You may set this option on a per-file basis by adding to the buffer a
3641 line like
3643 #+ARCHIVE: basement::** Finished Tasks
3645 You may also define it locally for a subtree by setting an ARCHIVE property
3646 in the entry. If such a property is found in an entry, or anywhere up
3647 the hierarchy, it will be used."
3648 :group 'org-archive
3649 :type 'string)
3651 (defcustom org-archive-tag "ARCHIVE"
3652 "The tag that marks a subtree as archived.
3653 An archived subtree does not open during visibility cycling, and does
3654 not contribute to the agenda listings.
3655 After changing this, font-lock must be restarted in the relevant buffers to
3656 get the proper fontification."
3657 :group 'org-archive
3658 :group 'org-keywords
3659 :type 'string)
3661 (defcustom org-agenda-skip-archived-trees t
3662 "Non-nil means the agenda will skip any items located in archived trees.
3663 An archived tree is a tree marked with the tag ARCHIVE. The use of this
3664 variable is no longer recommended, you should leave it at the value t.
3665 Instead, use the key `v' to cycle the archives-mode in the agenda."
3666 :group 'org-archive
3667 :group 'org-agenda-skip
3668 :type 'boolean)
3670 (defcustom org-columns-skip-archived-trees t
3671 "Non-nil means ignore archived trees when creating column view."
3672 :group 'org-archive
3673 :group 'org-properties
3674 :type 'boolean)
3676 (defcustom org-cycle-open-archived-trees nil
3677 "Non-nil means `org-cycle' will open archived trees.
3678 An archived tree is a tree marked with the tag ARCHIVE.
3679 When nil, archived trees will stay folded. You can still open them with
3680 normal outline commands like `show-all', but not with the cycling commands."
3681 :group 'org-archive
3682 :group 'org-cycle
3683 :type 'boolean)
3685 (defcustom org-sparse-tree-open-archived-trees nil
3686 "Non-nil means sparse tree construction shows matches in archived trees.
3687 When nil, matches in these trees are highlighted, but the trees are kept in
3688 collapsed state."
3689 :group 'org-archive
3690 :group 'org-sparse-trees
3691 :type 'boolean)
3693 (defun org-cycle-hide-archived-subtrees (state)
3694 "Re-hide all archived subtrees after a visibility state change."
3695 (when (and (not org-cycle-open-archived-trees)
3696 (not (memq state '(overview folded))))
3697 (save-excursion
3698 (let* ((globalp (memq state '(contents all)))
3699 (beg (if globalp (point-min) (point)))
3700 (end (if globalp (point-max) (org-end-of-subtree t))))
3701 (org-hide-archived-subtrees beg end)
3702 (goto-char beg)
3703 (if (looking-at (concat ".*:" org-archive-tag ":"))
3704 (message "%s" (substitute-command-keys
3705 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
3707 (defun org-force-cycle-archived ()
3708 "Cycle subtree even if it is archived."
3709 (interactive)
3710 (setq this-command 'org-cycle)
3711 (let ((org-cycle-open-archived-trees t))
3712 (call-interactively 'org-cycle)))
3714 (defun org-hide-archived-subtrees (beg end)
3715 "Re-hide all archived subtrees after a visibility state change."
3716 (save-excursion
3717 (let* ((re (concat ":" org-archive-tag ":")))
3718 (goto-char beg)
3719 (while (re-search-forward re end t)
3720 (when (org-on-heading-p)
3721 (org-flag-subtree t)
3722 (org-end-of-subtree t))))))
3724 (defun org-flag-subtree (flag)
3725 (save-excursion
3726 (org-back-to-heading t)
3727 (outline-end-of-heading)
3728 (outline-flag-region (point)
3729 (progn (org-end-of-subtree t) (point))
3730 flag)))
3732 (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
3734 (eval-and-compile
3735 (org-autoload "org-archive"
3736 '(org-add-archive-files org-archive-subtree
3737 org-archive-to-archive-sibling org-toggle-archive-tag
3738 org-archive-subtree-default
3739 org-archive-subtree-default-with-confirmation)))
3741 ;; Autoload Column View Code
3743 (declare-function org-columns-number-to-string "org-colview")
3744 (declare-function org-columns-get-format-and-top-level "org-colview")
3745 (declare-function org-columns-compute "org-colview")
3747 (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
3748 '(org-columns-number-to-string org-columns-get-format-and-top-level
3749 org-columns-compute org-agenda-columns org-columns-remove-overlays
3750 org-columns org-insert-columns-dblock org-dblock-write:columnview))
3752 ;; Autoload ID code
3754 (declare-function org-id-store-link "org-id")
3755 (declare-function org-id-locations-load "org-id")
3756 (declare-function org-id-locations-save "org-id")
3757 (defvar org-id-track-globally)
3758 (org-autoload "org-id"
3759 '(org-id-get-create org-id-new org-id-copy org-id-get
3760 org-id-get-with-outline-path-completion
3761 org-id-get-with-outline-drilling
3762 org-id-goto org-id-find org-id-store-link))
3764 ;; Autoload Plotting Code
3766 (org-autoload "org-plot"
3767 '(org-plot/gnuplot))
3769 ;;; Variables for pre-computed regular expressions, all buffer local
3771 (defvar org-drawer-regexp nil
3772 "Matches first line of a hidden block.")
3773 (make-variable-buffer-local 'org-drawer-regexp)
3774 (defvar org-todo-regexp nil
3775 "Matches any of the TODO state keywords.")
3776 (make-variable-buffer-local 'org-todo-regexp)
3777 (defvar org-not-done-regexp nil
3778 "Matches any of the TODO state keywords except the last one.")
3779 (make-variable-buffer-local 'org-not-done-regexp)
3780 (defvar org-not-done-heading-regexp nil
3781 "Matches a TODO headline that is not done.")
3782 (make-variable-buffer-local 'org-not-done-regexp)
3783 (defvar org-todo-line-regexp nil
3784 "Matches a headline and puts TODO state into group 2 if present.")
3785 (make-variable-buffer-local 'org-todo-line-regexp)
3786 (defvar org-complex-heading-regexp nil
3787 "Matches a headline and puts everything into groups:
3788 group 1: the stars
3789 group 2: The todo keyword, maybe
3790 group 3: Priority cookie
3791 group 4: True headline
3792 group 5: Tags")
3793 (make-variable-buffer-local 'org-complex-heading-regexp)
3794 (defvar org-complex-heading-regexp-format nil)
3795 (make-variable-buffer-local 'org-complex-heading-regexp-format)
3796 (defvar org-todo-line-tags-regexp nil
3797 "Matches a headline and puts TODO state into group 2 if present.
3798 Also put tags into group 4 if tags are present.")
3799 (make-variable-buffer-local 'org-todo-line-tags-regexp)
3800 (defvar org-nl-done-regexp nil
3801 "Matches newline followed by a headline with the DONE keyword.")
3802 (make-variable-buffer-local 'org-nl-done-regexp)
3803 (defvar org-looking-at-done-regexp nil
3804 "Matches the DONE keyword a point.")
3805 (make-variable-buffer-local 'org-looking-at-done-regexp)
3806 (defvar org-ds-keyword-length 12
3807 "Maximum length of the Deadline and SCHEDULED keywords.")
3808 (make-variable-buffer-local 'org-ds-keyword-length)
3809 (defvar org-deadline-regexp nil
3810 "Matches the DEADLINE keyword.")
3811 (make-variable-buffer-local 'org-deadline-regexp)
3812 (defvar org-deadline-time-regexp nil
3813 "Matches the DEADLINE keyword together with a time stamp.")
3814 (make-variable-buffer-local 'org-deadline-time-regexp)
3815 (defvar org-deadline-line-regexp nil
3816 "Matches the DEADLINE keyword and the rest of the line.")
3817 (make-variable-buffer-local 'org-deadline-line-regexp)
3818 (defvar org-scheduled-regexp nil
3819 "Matches the SCHEDULED keyword.")
3820 (make-variable-buffer-local 'org-scheduled-regexp)
3821 (defvar org-scheduled-time-regexp nil
3822 "Matches the SCHEDULED keyword together with a time stamp.")
3823 (make-variable-buffer-local 'org-scheduled-time-regexp)
3824 (defvar org-closed-time-regexp nil
3825 "Matches the CLOSED keyword together with a time stamp.")
3826 (make-variable-buffer-local 'org-closed-time-regexp)
3828 (defvar org-keyword-time-regexp nil
3829 "Matches any of the 4 keywords, together with the time stamp.")
3830 (make-variable-buffer-local 'org-keyword-time-regexp)
3831 (defvar org-keyword-time-not-clock-regexp nil
3832 "Matches any of the 3 keywords, together with the time stamp.")
3833 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
3834 (defvar org-maybe-keyword-time-regexp nil
3835 "Matches a timestamp, possibly preceeded by a keyword.")
3836 (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
3837 (defvar org-planning-or-clock-line-re nil
3838 "Matches a line with planning or clock info.")
3839 (make-variable-buffer-local 'org-planning-or-clock-line-re)
3840 (defvar org-all-time-keywords nil
3841 "List of time keywords.")
3842 (make-variable-buffer-local 'org-all-time-keywords)
3844 (defconst org-plain-time-of-day-regexp
3845 (concat
3846 "\\(\\<[012]?[0-9]"
3847 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3848 "\\(--?"
3849 "\\(\\<[012]?[0-9]"
3850 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3851 "\\)?")
3852 "Regular expression to match a plain time or time range.
3853 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3854 groups carry important information:
3855 0 the full match
3856 1 the first time, range or not
3857 8 the second time, if it is a range.")
3859 (defconst org-plain-time-extension-regexp
3860 (concat
3861 "\\(\\<[012]?[0-9]"
3862 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3863 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
3864 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
3865 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3866 groups carry important information:
3867 0 the full match
3868 7 hours of duration
3869 9 minutes of duration")
3871 (defconst org-stamp-time-of-day-regexp
3872 (concat
3873 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
3874 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
3875 "\\(--?"
3876 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
3877 "Regular expression to match a timestamp time or time range.
3878 After a match, the following groups carry important information:
3879 0 the full match
3880 1 date plus weekday, for back referencing to make sure both times are on the same day
3881 2 the first time, range or not
3882 4 the second time, if it is a range.")
3884 (defconst org-startup-options
3885 '(("fold" org-startup-folded t)
3886 ("overview" org-startup-folded t)
3887 ("nofold" org-startup-folded nil)
3888 ("showall" org-startup-folded nil)
3889 ("showeverything" org-startup-folded showeverything)
3890 ("content" org-startup-folded content)
3891 ("indent" org-startup-indented t)
3892 ("noindent" org-startup-indented nil)
3893 ("hidestars" org-hide-leading-stars t)
3894 ("showstars" org-hide-leading-stars nil)
3895 ("odd" org-odd-levels-only t)
3896 ("oddeven" org-odd-levels-only nil)
3897 ("align" org-startup-align-all-tables t)
3898 ("noalign" org-startup-align-all-tables nil)
3899 ("customtime" org-display-custom-times t)
3900 ("logdone" org-log-done time)
3901 ("lognotedone" org-log-done note)
3902 ("nologdone" org-log-done nil)
3903 ("lognoteclock-out" org-log-note-clock-out t)
3904 ("nolognoteclock-out" org-log-note-clock-out nil)
3905 ("logrepeat" org-log-repeat state)
3906 ("lognoterepeat" org-log-repeat note)
3907 ("nologrepeat" org-log-repeat nil)
3908 ("logreschedule" org-log-reschedule time)
3909 ("lognotereschedule" org-log-reschedule note)
3910 ("nologreschedule" org-log-reschedule nil)
3911 ("logredeadline" org-log-redeadline time)
3912 ("lognoteredeadline" org-log-redeadline note)
3913 ("nologredeadline" org-log-redeadline nil)
3914 ("logrefile" org-log-refile time)
3915 ("lognoterefile" org-log-refile note)
3916 ("nologrefile" org-log-refile nil)
3917 ("fninline" org-footnote-define-inline t)
3918 ("nofninline" org-footnote-define-inline nil)
3919 ("fnlocal" org-footnote-section nil)
3920 ("fnauto" org-footnote-auto-label t)
3921 ("fnprompt" org-footnote-auto-label nil)
3922 ("fnconfirm" org-footnote-auto-label confirm)
3923 ("fnplain" org-footnote-auto-label plain)
3924 ("fnadjust" org-footnote-auto-adjust t)
3925 ("nofnadjust" org-footnote-auto-adjust nil)
3926 ("constcgs" constants-unit-system cgs)
3927 ("constSI" constants-unit-system SI)
3928 ("noptag" org-tag-persistent-alist nil)
3929 ("hideblocks" org-hide-block-startup t)
3930 ("nohideblocks" org-hide-block-startup nil)
3931 ("beamer" org-startup-with-beamer-mode t))
3932 "Variable associated with STARTUP options for org-mode.
3933 Each element is a list of three items: The startup options as written
3934 in the #+STARTUP line, the corresponding variable, and the value to
3935 set this variable to if the option is found. An optional forth element PUSH
3936 means to push this value onto the list in the variable.")
3938 (defun org-set-regexps-and-options ()
3939 "Precompute regular expressions for current buffer."
3940 (when (org-mode-p)
3941 (org-set-local 'org-todo-kwd-alist nil)
3942 (org-set-local 'org-todo-key-alist nil)
3943 (org-set-local 'org-todo-key-trigger nil)
3944 (org-set-local 'org-todo-keywords-1 nil)
3945 (org-set-local 'org-done-keywords nil)
3946 (org-set-local 'org-todo-heads nil)
3947 (org-set-local 'org-todo-sets nil)
3948 (org-set-local 'org-todo-log-states nil)
3949 (org-set-local 'org-file-properties nil)
3950 (org-set-local 'org-file-tags nil)
3951 (let ((re (org-make-options-regexp
3952 '("CATEGORY" "TODO" "COLUMNS"
3953 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
3954 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
3955 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
3956 (splitre "[ \t]+")
3957 kwds kws0 kwsa key log value cat arch tags const links hw dws
3958 tail sep kws1 prio props ftags drawers beamer-p
3959 ext-setup-or-nil setup-contents (start 0))
3960 (save-excursion
3961 (save-restriction
3962 (widen)
3963 (goto-char (point-min))
3964 (while (or (and ext-setup-or-nil
3965 (string-match re ext-setup-or-nil start)
3966 (setq start (match-end 0)))
3967 (and (setq ext-setup-or-nil nil start 0)
3968 (re-search-forward re nil t)))
3969 (setq key (upcase (match-string 1 ext-setup-or-nil))
3970 value (org-match-string-no-properties 2 ext-setup-or-nil))
3971 (cond
3972 ((equal key "CATEGORY")
3973 (if (string-match "[ \t]+$" value)
3974 (setq value (replace-match "" t t value)))
3975 (setq cat value))
3976 ((member key '("SEQ_TODO" "TODO"))
3977 (push (cons 'sequence (org-split-string value splitre)) kwds))
3978 ((equal key "TYP_TODO")
3979 (push (cons 'type (org-split-string value splitre)) kwds))
3980 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
3981 ;; general TODO-like setup
3982 (push (cons (intern (downcase (match-string 1 key)))
3983 (org-split-string value splitre)) kwds))
3984 ((equal key "TAGS")
3985 (setq tags (append tags (if tags '("\\n") nil)
3986 (org-split-string value splitre))))
3987 ((equal key "COLUMNS")
3988 (org-set-local 'org-columns-default-format value))
3989 ((equal key "LINK")
3990 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
3991 (push (cons (match-string 1 value)
3992 (org-trim (match-string 2 value)))
3993 links)))
3994 ((equal key "PRIORITIES")
3995 (setq prio (org-split-string value " +")))
3996 ((equal key "PROPERTY")
3997 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
3998 (push (cons (match-string 1 value) (match-string 2 value))
3999 props)))
4000 ((equal key "FILETAGS")
4001 (when (string-match "\\S-" value)
4002 (setq ftags
4003 (append
4004 ftags
4005 (apply 'append
4006 (mapcar (lambda (x) (org-split-string x ":"))
4007 (org-split-string value)))))))
4008 ((equal key "DRAWERS")
4009 (setq drawers (org-split-string value splitre)))
4010 ((equal key "CONSTANTS")
4011 (setq const (append const (org-split-string value splitre))))
4012 ((equal key "STARTUP")
4013 (let ((opts (org-split-string value splitre))
4014 l var val)
4015 (while (setq l (pop opts))
4016 (when (setq l (assoc l org-startup-options))
4017 (setq var (nth 1 l) val (nth 2 l))
4018 (if (not (nth 3 l))
4019 (set (make-local-variable var) val)
4020 (if (not (listp (symbol-value var)))
4021 (set (make-local-variable var) nil))
4022 (set (make-local-variable var) (symbol-value var))
4023 (add-to-list var val))))))
4024 ((equal key "ARCHIVE")
4025 (string-match " *$" value)
4026 (setq arch (replace-match "" t t value))
4027 (remove-text-properties 0 (length arch)
4028 '(face t fontified t) arch))
4029 ((equal key "LATEX_CLASS")
4030 (setq beamer-p (equal value "beamer")))
4031 ((equal key "SETUPFILE")
4032 (setq setup-contents (org-file-contents
4033 (expand-file-name
4034 (org-remove-double-quotes value))
4035 'noerror))
4036 (if (not ext-setup-or-nil)
4037 (setq ext-setup-or-nil setup-contents start 0)
4038 (setq ext-setup-or-nil
4039 (concat (substring ext-setup-or-nil 0 start)
4040 "\n" setup-contents "\n"
4041 (substring ext-setup-or-nil start)))))
4042 ))))
4043 (when cat
4044 (org-set-local 'org-category (intern cat))
4045 (push (cons "CATEGORY" cat) props))
4046 (when prio
4047 (if (< (length prio) 3) (setq prio '("A" "C" "B")))
4048 (setq prio (mapcar 'string-to-char prio))
4049 (org-set-local 'org-highest-priority (nth 0 prio))
4050 (org-set-local 'org-lowest-priority (nth 1 prio))
4051 (org-set-local 'org-default-priority (nth 2 prio)))
4052 (and props (org-set-local 'org-file-properties (nreverse props)))
4053 (and ftags (org-set-local 'org-file-tags
4054 (mapcar 'org-add-prop-inherited ftags)))
4055 (and drawers (org-set-local 'org-drawers drawers))
4056 (and arch (org-set-local 'org-archive-location arch))
4057 (and links (setq org-link-abbrev-alist-local (nreverse links)))
4058 ;; Process the TODO keywords
4059 (unless kwds
4060 ;; Use the global values as if they had been given locally.
4061 (setq kwds (default-value 'org-todo-keywords))
4062 (if (stringp (car kwds))
4063 (setq kwds (list (cons org-todo-interpretation
4064 (default-value 'org-todo-keywords)))))
4065 (setq kwds (reverse kwds)))
4066 (setq kwds (nreverse kwds))
4067 (let (inter kws kw)
4068 (while (setq kws (pop kwds))
4069 (let ((kws (or
4070 (run-hook-with-args-until-success
4071 'org-todo-setup-filter-hook kws)
4072 kws)))
4073 (setq inter (pop kws) sep (member "|" kws)
4074 kws0 (delete "|" (copy-sequence kws))
4075 kwsa nil
4076 kws1 (mapcar
4077 (lambda (x)
4078 ;; 1 2
4079 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
4080 (progn
4081 (setq kw (match-string 1 x)
4082 key (and (match-end 2) (match-string 2 x))
4083 log (org-extract-log-state-settings x))
4084 (push (cons kw (and key (string-to-char key))) kwsa)
4085 (and log (push log org-todo-log-states))
4087 (error "Invalid TODO keyword %s" x)))
4088 kws0)
4089 kwsa (if kwsa (append '((:startgroup))
4090 (nreverse kwsa)
4091 '((:endgroup))))
4092 hw (car kws1)
4093 dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
4094 tail (list inter hw (car dws) (org-last dws))))
4095 (add-to-list 'org-todo-heads hw 'append)
4096 (push kws1 org-todo-sets)
4097 (setq org-done-keywords (append org-done-keywords dws nil))
4098 (setq org-todo-key-alist (append org-todo-key-alist kwsa))
4099 (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
4100 (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
4101 (setq org-todo-sets (nreverse org-todo-sets)
4102 org-todo-kwd-alist (nreverse org-todo-kwd-alist)
4103 org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
4104 org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
4105 ;; Process the constants
4106 (when const
4107 (let (e cst)
4108 (while (setq e (pop const))
4109 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
4110 (push (cons (match-string 1 e) (match-string 2 e)) cst)))
4111 (setq org-table-formula-constants-local cst)))
4113 ;; Process the tags.
4114 (when tags
4115 (let (e tgs)
4116 (while (setq e (pop tags))
4117 (cond
4118 ((equal e "{") (push '(:startgroup) tgs))
4119 ((equal e "}") (push '(:endgroup) tgs))
4120 ((equal e "\\n") (push '(:newline) tgs))
4121 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
4122 (push (cons (match-string 1 e)
4123 (string-to-char (match-string 2 e)))
4124 tgs))
4125 (t (push (list e) tgs))))
4126 (org-set-local 'org-tag-alist nil)
4127 (while (setq e (pop tgs))
4128 (or (and (stringp (car e))
4129 (assoc (car e) org-tag-alist))
4130 (push e org-tag-alist)))))
4132 ;; Compute the regular expressions and other local variables
4133 (if (not org-done-keywords)
4134 (setq org-done-keywords (and org-todo-keywords-1
4135 (list (org-last org-todo-keywords-1)))))
4136 (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
4137 (length org-scheduled-string)
4138 (length org-clock-string)
4139 (length org-closed-string)))
4140 org-drawer-regexp
4141 (concat "^[ \t]*:\\("
4142 (mapconcat 'regexp-quote org-drawers "\\|")
4143 "\\):[ \t]*$")
4144 org-not-done-keywords
4145 (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
4146 org-todo-regexp
4147 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
4148 "\\|") "\\)\\>")
4149 org-not-done-regexp
4150 (concat "\\<\\("
4151 (mapconcat 'regexp-quote org-not-done-keywords "\\|")
4152 "\\)\\>")
4153 org-not-done-heading-regexp
4154 (concat "^\\(\\*+\\)[ \t]+\\("
4155 (mapconcat 'regexp-quote org-not-done-keywords "\\|")
4156 "\\)\\>")
4157 org-todo-line-regexp
4158 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4159 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
4160 "\\)\\>\\)?[ \t]*\\(.*\\)")
4161 org-complex-heading-regexp
4162 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4163 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
4164 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
4165 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4166 org-complex-heading-regexp-format
4167 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4168 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
4169 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
4170 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4171 org-nl-done-regexp
4172 (concat "\n\\*+[ \t]+"
4173 "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
4174 "\\)" "\\>")
4175 org-todo-line-tags-regexp
4176 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4177 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
4178 (org-re
4179 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
4180 org-looking-at-done-regexp
4181 (concat "^" "\\(?:"
4182 (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
4183 "\\>")
4184 org-deadline-regexp (concat "\\<" org-deadline-string)
4185 org-deadline-time-regexp
4186 (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
4187 org-deadline-line-regexp
4188 (concat "\\<\\(" org-deadline-string "\\).*")
4189 org-scheduled-regexp
4190 (concat "\\<" org-scheduled-string)
4191 org-scheduled-time-regexp
4192 (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
4193 org-closed-time-regexp
4194 (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
4195 org-keyword-time-regexp
4196 (concat "\\<\\(" org-scheduled-string
4197 "\\|" org-deadline-string
4198 "\\|" org-closed-string
4199 "\\|" org-clock-string "\\)"
4200 " *[[<]\\([^]>]+\\)[]>]")
4201 org-keyword-time-not-clock-regexp
4202 (concat "\\<\\(" org-scheduled-string
4203 "\\|" org-deadline-string
4204 "\\|" org-closed-string
4205 "\\)"
4206 " *[[<]\\([^]>]+\\)[]>]")
4207 org-maybe-keyword-time-regexp
4208 (concat "\\(\\<\\(" org-scheduled-string
4209 "\\|" org-deadline-string
4210 "\\|" org-closed-string
4211 "\\|" org-clock-string "\\)\\)?"
4212 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4213 org-planning-or-clock-line-re
4214 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
4215 "\\|" org-deadline-string
4216 "\\|" org-closed-string "\\|" org-clock-string
4217 "\\)\\>\\)")
4218 org-all-time-keywords
4219 (mapcar (lambda (w) (substring w 0 -1))
4220 (list org-scheduled-string org-deadline-string
4221 org-clock-string org-closed-string))
4223 (org-compute-latex-and-specials-regexp)
4224 (org-set-font-lock-defaults))))
4226 (defun org-file-contents (file &optional noerror)
4227 "Return the contents of FILE, as a string."
4228 (if (or (not file)
4229 (not (file-readable-p file)))
4230 (if noerror
4231 (progn
4232 (message "Cannot read file %s" file)
4233 (ding) (sit-for 2)
4235 (error "Cannot read file %s" file))
4236 (with-temp-buffer
4237 (insert-file-contents file)
4238 (buffer-string))))
4240 (defun org-extract-log-state-settings (x)
4241 "Extract the log state setting from a TODO keyword string.
4242 This will extract info from a string like \"WAIT(w@/!)\"."
4243 (let (kw key log1 log2)
4244 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
4245 (setq kw (match-string 1 x)
4246 key (and (match-end 2) (match-string 2 x))
4247 log1 (and (match-end 3) (match-string 3 x))
4248 log2 (and (match-end 4) (match-string 4 x)))
4249 (and (or log1 log2)
4250 (list kw
4251 (and log1 (if (equal log1 "!") 'time 'note))
4252 (and log2 (if (equal log2 "!") 'time 'note)))))))
4254 (defun org-remove-keyword-keys (list)
4255 "Remove a pair of parenthesis at the end of each string in LIST."
4256 (mapcar (lambda (x)
4257 (if (string-match "(.*)$" x)
4258 (substring x 0 (match-beginning 0))
4260 list))
4262 (defun org-assign-fast-keys (alist)
4263 "Assign fast keys to a keyword-key alist.
4264 Respect keys that are already there."
4265 (let (new e (alt ?0))
4266 (while (setq e (pop alist))
4267 (if (or (memq (car e) '(:newline :endgroup :startgroup))
4268 (cdr e)) ;; Key already assigned.
4269 (push e new)
4270 (let ((clist (string-to-list (downcase (car e))))
4271 (used (append new alist)))
4272 (when (= (car clist) ?@)
4273 (pop clist))
4274 (while (and clist (rassoc (car clist) used))
4275 (pop clist))
4276 (unless clist
4277 (while (rassoc alt used)
4278 (incf alt)))
4279 (push (cons (car e) (or (car clist) alt)) new))))
4280 (nreverse new)))
4282 ;;; Some variables used in various places
4284 (defvar org-window-configuration nil
4285 "Used in various places to store a window configuration.")
4286 (defvar org-selected-window nil
4287 "Used in various places to store a window configuration.")
4288 (defvar org-finish-function nil
4289 "Function to be called when `C-c C-c' is used.
4290 This is for getting out of special buffers like remember.")
4293 ;; FIXME: Occasionally check by commenting these, to make sure
4294 ;; no other functions uses these, forgetting to let-bind them.
4295 (defvar entry)
4296 (defvar last-state)
4297 (defvar date)
4299 ;; Defined somewhere in this file, but used before definition.
4300 (defvar org-entities) ;; defined in org-entities.el
4301 (defvar org-struct-menu)
4302 (defvar org-org-menu)
4303 (defvar org-tbl-menu)
4305 ;;;; Define the Org-mode
4307 (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
4308 (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."))
4311 ;; We use a before-change function to check if a table might need
4312 ;; an update.
4313 (defvar org-table-may-need-update t
4314 "Indicates that a table might need an update.
4315 This variable is set by `org-before-change-function'.
4316 `org-table-align' sets it back to nil.")
4317 (defun org-before-change-function (beg end)
4318 "Every change indicates that a table might need an update."
4319 (setq org-table-may-need-update t))
4320 (defvar org-mode-map)
4321 (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
4322 (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
4323 (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
4324 (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
4325 (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
4326 (defvar org-table-buffer-is-an nil)
4327 (defconst org-outline-regexp "\\*+ ")
4329 ;;;###autoload
4330 (define-derived-mode org-mode outline-mode "Org"
4331 "Outline-based notes management and organizer, alias
4332 \"Carsten's outline-mode for keeping track of everything.\"
4334 Org-mode develops organizational tasks around a NOTES file which
4335 contains information about projects as plain text. Org-mode is
4336 implemented on top of outline-mode, which is ideal to keep the content
4337 of large files well structured. It supports ToDo items, deadlines and
4338 time stamps, which magically appear in the diary listing of the Emacs
4339 calendar. Tables are easily created with a built-in table editor.
4340 Plain text URL-like links connect to websites, emails (VM), Usenet
4341 messages (Gnus), BBDB entries, and any files related to the project.
4342 For printing and sharing of notes, an Org-mode file (or a part of it)
4343 can be exported as a structured ASCII or HTML file.
4345 The following commands are available:
4347 \\{org-mode-map}"
4349 ;; Get rid of Outline menus, they are not needed
4350 ;; Need to do this here because define-derived-mode sets up
4351 ;; the keymap so late. Still, it is a waste to call this each time
4352 ;; we switch another buffer into org-mode.
4353 (if (featurep 'xemacs)
4354 (when (boundp 'outline-mode-menu-heading)
4355 ;; Assume this is Greg's port, it used easymenu
4356 (easy-menu-remove outline-mode-menu-heading)
4357 (easy-menu-remove outline-mode-menu-show)
4358 (easy-menu-remove outline-mode-menu-hide))
4359 (define-key org-mode-map [menu-bar headings] 'undefined)
4360 (define-key org-mode-map [menu-bar hide] 'undefined)
4361 (define-key org-mode-map [menu-bar show] 'undefined))
4363 (org-load-modules-maybe)
4364 (easy-menu-add org-org-menu)
4365 (easy-menu-add org-tbl-menu)
4366 (org-install-agenda-files-menu)
4367 (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
4368 (org-add-to-invisibility-spec '(org-cwidth))
4369 (org-add-to-invisibility-spec '(org-hide-block . t))
4370 (when (featurep 'xemacs)
4371 (org-set-local 'line-move-ignore-invisible t))
4372 (org-set-local 'outline-regexp org-outline-regexp)
4373 (org-set-local 'outline-level 'org-outline-level)
4374 (when (and org-ellipsis
4375 (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
4376 (fboundp 'make-glyph-code))
4377 (unless org-display-table
4378 (setq org-display-table (make-display-table)))
4379 (set-display-table-slot
4380 org-display-table 4
4381 (vconcat (mapcar
4382 (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
4383 org-ellipsis)))
4384 (if (stringp org-ellipsis) org-ellipsis "..."))))
4385 (setq buffer-display-table org-display-table))
4386 (org-set-regexps-and-options)
4387 (when (and org-tag-faces (not org-tags-special-faces-re))
4388 ;; tag faces set outside customize.... force initialization.
4389 (org-set-tag-faces 'org-tag-faces org-tag-faces))
4390 ;; Calc embedded
4391 (org-set-local 'calc-embedded-open-mode "# ")
4392 (modify-syntax-entry ?# "<")
4393 (modify-syntax-entry ?@ "w")
4394 (if org-startup-truncated (setq truncate-lines t))
4395 (org-set-local 'font-lock-unfontify-region-function
4396 'org-unfontify-region)
4397 ;; Activate before-change-function
4398 (org-set-local 'org-table-may-need-update t)
4399 (org-add-hook 'before-change-functions 'org-before-change-function nil
4400 'local)
4401 ;; Check for running clock before killing a buffer
4402 (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
4403 ;; Paragraphs and auto-filling
4404 (org-set-autofill-regexps)
4405 (setq indent-line-function 'org-indent-line-function)
4406 (org-update-radio-target-regexp)
4407 ;; Make sure dependence stuff works reliably, even for users who set it
4408 ;; too late :-(
4409 (if org-enforce-todo-dependencies
4410 (add-hook 'org-blocker-hook
4411 'org-block-todo-from-children-or-siblings-or-parent)
4412 (remove-hook 'org-blocker-hook
4413 'org-block-todo-from-children-or-siblings-or-parent))
4414 (if org-enforce-todo-checkbox-dependencies
4415 (add-hook 'org-blocker-hook
4416 'org-block-todo-from-checkboxes)
4417 (remove-hook 'org-blocker-hook
4418 'org-block-todo-from-checkboxes))
4420 ;; Comment characters
4421 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
4422 (org-set-local 'comment-padding " ")
4424 ;; Align options lines
4425 (org-set-local
4426 'align-mode-rules-list
4427 '((org-in-buffer-settings
4428 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
4429 (modes . '(org-mode)))))
4431 ;; Imenu
4432 (org-set-local 'imenu-create-index-function
4433 'org-imenu-get-tree)
4435 ;; Make isearch reveal context
4436 (if (or (featurep 'xemacs)
4437 (not (boundp 'outline-isearch-open-invisible-function)))
4438 ;; Emacs 21 and XEmacs make use of the hook
4439 (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
4440 ;; Emacs 22 deals with this through a special variable
4441 (org-set-local 'outline-isearch-open-invisible-function
4442 (lambda (&rest ignore) (org-show-context 'isearch))))
4444 ;; Turn on org-beamer-mode?
4445 (and org-startup-with-beamer-mode (org-beamer-mode 1))
4447 ;; If empty file that did not turn on org-mode automatically, make it to.
4448 (if (and org-insert-mode-line-in-empty-file
4449 (interactive-p)
4450 (= (point-min) (point-max)))
4451 (insert "# -*- mode: org -*-\n\n"))
4452 (unless org-inhibit-startup
4453 (when org-startup-align-all-tables
4454 (let ((bmp (buffer-modified-p)))
4455 (org-table-map-tables 'org-table-align)
4456 (set-buffer-modified-p bmp)))
4457 (when org-startup-indented
4458 (require 'org-indent)
4459 (org-indent-mode 1))
4460 (unless org-inhibit-startup-visibility-stuff
4461 (org-set-startup-visibility))))
4463 (when (fboundp 'abbrev-table-put)
4464 (abbrev-table-put org-mode-abbrev-table
4465 :parents (list text-mode-abbrev-table)))
4467 (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
4469 (defun org-current-time ()
4470 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
4471 (if (> (car org-time-stamp-rounding-minutes) 1)
4472 (let ((r (car org-time-stamp-rounding-minutes))
4473 (time (decode-time)))
4474 (apply 'encode-time
4475 (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
4476 (nthcdr 2 time))))
4477 (current-time)))
4479 ;;;; Font-Lock stuff, including the activators
4481 (defvar org-mouse-map (make-sparse-keymap))
4482 (org-defkey org-mouse-map
4483 (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
4484 (org-defkey org-mouse-map
4485 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
4486 (when org-mouse-1-follows-link
4487 (org-defkey org-mouse-map [follow-link] 'mouse-face))
4488 (when org-tab-follows-link
4489 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
4490 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
4492 (require 'font-lock)
4494 (defconst org-non-link-chars "]\t\n\r<>")
4495 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
4496 "shell" "elisp"))
4497 (defvar org-link-types-re nil
4498 "Matches a link that has a url-like prefix like \"http:\"")
4499 (defvar org-link-re-with-space nil
4500 "Matches a link with spaces, optional angular brackets around it.")
4501 (defvar org-link-re-with-space2 nil
4502 "Matches a link with spaces, optional angular brackets around it.")
4503 (defvar org-link-re-with-space3 nil
4504 "Matches a link with spaces, only for internal part in bracket links.")
4505 (defvar org-angle-link-re nil
4506 "Matches link with angular brackets, spaces are allowed.")
4507 (defvar org-plain-link-re nil
4508 "Matches plain link, without spaces.")
4509 (defvar org-bracket-link-regexp nil
4510 "Matches a link in double brackets.")
4511 (defvar org-bracket-link-analytic-regexp nil
4512 "Regular expression used to analyze links.
4513 Here is what the match groups contain after a match:
4514 1: http:
4515 2: http
4516 3: path
4517 4: [desc]
4518 5: desc")
4519 (defvar org-bracket-link-analytic-regexp++ nil
4520 "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
4521 (defvar org-any-link-re nil
4522 "Regular expression matching any link.")
4524 (defun org-make-link-regexps ()
4525 "Update the link regular expressions.
4526 This should be called after the variable `org-link-types' has changed."
4527 (setq org-link-types-re
4528 (concat
4529 "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
4530 org-link-re-with-space
4531 (concat
4532 "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4533 "\\([^" org-non-link-chars " ]"
4534 "[^" org-non-link-chars "]*"
4535 "[^" org-non-link-chars " ]\\)>?")
4536 org-link-re-with-space2
4537 (concat
4538 "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4539 "\\([^" org-non-link-chars " ]"
4540 "[^\t\n\r]*"
4541 "[^" org-non-link-chars " ]\\)>?")
4542 org-link-re-with-space3
4543 (concat
4544 "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4545 "\\([^" org-non-link-chars " ]"
4546 "[^\t\n\r]*\\)")
4547 org-angle-link-re
4548 (concat
4549 "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4550 "\\([^" org-non-link-chars " ]"
4551 "[^" org-non-link-chars "]*"
4552 "\\)>")
4553 org-plain-link-re
4554 (concat
4555 "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
4556 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
4557 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
4558 org-bracket-link-regexp
4559 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
4560 org-bracket-link-analytic-regexp
4561 (concat
4562 "\\[\\["
4563 "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
4564 "\\([^]]+\\)"
4565 "\\]"
4566 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4567 "\\]")
4568 org-bracket-link-analytic-regexp++
4569 (concat
4570 "\\[\\["
4571 "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
4572 "\\([^]]+\\)"
4573 "\\]"
4574 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4575 "\\]")
4576 org-any-link-re
4577 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
4578 org-angle-link-re "\\)\\|\\("
4579 org-plain-link-re "\\)")))
4581 (org-make-link-regexps)
4583 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
4584 "Regular expression for fast time stamp matching.")
4585 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
4586 "Regular expression for fast time stamp matching.")
4587 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4588 "Regular expression matching time strings for analysis.
4589 This one does not require the space after the date, so it can be used
4590 on a string that terminates immediately after the date.")
4591 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4592 "Regular expression matching time strings for analysis.")
4593 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
4594 "Regular expression matching time stamps, with groups.")
4595 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
4596 "Regular expression matching time stamps (also [..]), with groups.")
4597 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
4598 "Regular expression matching a time stamp range.")
4599 (defconst org-tr-regexp-both
4600 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
4601 "Regular expression matching a time stamp range.")
4602 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
4603 org-ts-regexp "\\)?")
4604 "Regular expression matching a time stamp or time stamp range.")
4605 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
4606 org-ts-regexp-both "\\)?")
4607 "Regular expression matching a time stamp or time stamp range.
4608 The time stamps may be either active or inactive.")
4610 (defvar org-emph-face nil)
4612 (defun org-do-emphasis-faces (limit)
4613 "Run through the buffer and add overlays to links."
4614 (let (rtn a)
4615 (while (and (not rtn) (re-search-forward org-emph-re limit t))
4616 (if (not (= (char-after (match-beginning 3))
4617 (char-after (match-beginning 4))))
4618 (progn
4619 (setq rtn t)
4620 (setq a (assoc (match-string 3) org-emphasis-alist))
4621 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
4622 'face
4623 (nth 1 a))
4624 (and (nth 4 a)
4625 (org-remove-flyspell-overlays-in
4626 (match-beginning 0) (match-end 0)))
4627 (add-text-properties (match-beginning 2) (match-end 2)
4628 '(font-lock-multiline t))
4629 (when org-hide-emphasis-markers
4630 (add-text-properties (match-end 4) (match-beginning 5)
4631 '(invisible org-link))
4632 (add-text-properties (match-beginning 3) (match-end 3)
4633 '(invisible org-link)))))
4634 (backward-char 1))
4635 rtn))
4637 (defun org-emphasize (&optional char)
4638 "Insert or change an emphasis, i.e. a font like bold or italic.
4639 If there is an active region, change that region to a new emphasis.
4640 If there is no region, just insert the marker characters and position
4641 the cursor between them.
4642 CHAR should be either the marker character, or the first character of the
4643 HTML tag associated with that emphasis. If CHAR is a space, the means
4644 to remove the emphasis of the selected region.
4645 If char is not given (for example in an interactive call) it
4646 will be prompted for."
4647 (interactive)
4648 (let ((eal org-emphasis-alist) e det
4649 (erc org-emphasis-regexp-components)
4650 (prompt "")
4651 (string "") beg end move tag c s)
4652 (if (org-region-active-p)
4653 (setq beg (region-beginning) end (region-end)
4654 string (buffer-substring beg end))
4655 (setq move t))
4657 (while (setq e (pop eal))
4658 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
4659 c (aref tag 0))
4660 (push (cons c (string-to-char (car e))) det)
4661 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
4662 (substring tag 1)))))
4663 (setq det (nreverse det))
4664 (unless char
4665 (message "%s" (concat "Emphasis marker or tag:" prompt))
4666 (setq char (read-char-exclusive)))
4667 (setq char (or (cdr (assoc char det)) char))
4668 (if (equal char ?\ )
4669 (setq s "" move nil)
4670 (unless (assoc (char-to-string char) org-emphasis-alist)
4671 (error "No such emphasis marker: \"%c\"" char))
4672 (setq s (char-to-string char)))
4673 (while (and (> (length string) 1)
4674 (equal (substring string 0 1) (substring string -1))
4675 (assoc (substring string 0 1) org-emphasis-alist))
4676 (setq string (substring string 1 -1)))
4677 (setq string (concat s string s))
4678 (if beg (delete-region beg end))
4679 (unless (or (bolp)
4680 (string-match (concat "[" (nth 0 erc) "\n]")
4681 (char-to-string (char-before (point)))))
4682 (insert " "))
4683 (unless (or (eobp)
4684 (string-match (concat "[" (nth 1 erc) "\n]")
4685 (char-to-string (char-after (point)))))
4686 (insert " ") (backward-char 1))
4687 (insert string)
4688 (and move (backward-char 1))))
4690 (defconst org-nonsticky-props
4691 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
4693 (defsubst org-rear-nonsticky-at (pos)
4694 (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
4696 (defun org-activate-plain-links (limit)
4697 "Run through the buffer and add overlays to links."
4698 (catch 'exit
4699 (let (f)
4700 (if (re-search-forward org-plain-link-re limit t)
4701 (progn
4702 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4703 (setq f (get-text-property (match-beginning 0) 'face))
4704 (if (or (eq f 'org-tag)
4705 (and (listp f) (memq 'org-tag f)))
4707 (add-text-properties (match-beginning 0) (match-end 0)
4708 (list 'mouse-face 'highlight
4709 'face 'org-link
4710 'keymap org-mouse-map))
4711 (org-rear-nonsticky-at (match-end 0)))
4712 t)))))
4714 (defun org-activate-code (limit)
4715 (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
4716 (progn
4717 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4718 (remove-text-properties (match-beginning 0) (match-end 0)
4719 '(display t invisible t intangible t))
4720 t)))
4722 (defun org-fontify-meta-lines-and-blocks (limit)
4723 "Fontify #+ lines and blocks, in the correct ways."
4724 (let ((case-fold-search t))
4725 (if (re-search-forward
4726 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
4727 limit t)
4728 (let ((beg (match-beginning 0))
4729 (beg1 (line-beginning-position 2))
4730 (dc1 (downcase (match-string 2)))
4731 (dc3 (downcase (match-string 3)))
4732 end end1 quoting block-type)
4733 (cond
4734 ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
4735 ;; a single line of backend-specific content
4736 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4737 (remove-text-properties (match-beginning 0) (match-end 0)
4738 '(display t invisible t intangible t))
4739 (add-text-properties (match-beginning 1) (match-end 3)
4740 '(font-lock-fontified t face org-meta-line))
4741 (add-text-properties (match-beginning 6) (match-end 6)
4742 '(font-lock-fontified t face org-block))
4744 ((and (match-end 4) (equal dc3 "begin"))
4745 ;; Truely a block
4746 (setq block-type (downcase (match-string 5))
4747 quoting (member block-type org-protecting-blocks))
4748 (when (re-search-forward
4749 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
4750 nil t) ;; on purpose, we look further than LIMIT
4751 (setq end (match-end 0) end1 (1- (match-beginning 0)))
4752 (when quoting
4753 (remove-text-properties beg end
4754 '(display t invisible t intangible t)))
4755 (add-text-properties
4756 beg end
4757 '(font-lock-fontified t font-lock-multiline t))
4758 (add-text-properties beg beg1 '(face org-meta-line))
4759 (add-text-properties end1 end '(face org-meta-line))
4760 (cond
4761 (quoting
4762 (add-text-properties beg1 end1 '(face org-block)))
4763 ((not org-fontify-quote-and-verse-blocks))
4764 ((string= block-type "quote")
4765 (add-text-properties beg1 end1 '(face org-quote)))
4766 ((string= block-type "verse")
4767 (add-text-properties beg1 end1 '(face org-verse))))
4769 ((member dc1 '("title:" "author:" "email:" "date:"))
4770 (add-text-properties
4771 beg (match-end 3)
4772 (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
4773 '(font-lock-fontified t invisible t)
4774 '(font-lock-fontified t face org-document-info-keyword)))
4775 (add-text-properties
4776 (match-beginning 6) (match-end 6)
4777 (if (string-equal dc1 "title:")
4778 '(font-lock-fontified t face org-document-title)
4779 '(font-lock-fontified t face org-document-info))))
4780 ((not (member (char-after beg) '(?\ ?\t)))
4781 ;; just any other in-buffer setting, but not indented
4782 (add-text-properties
4783 beg (match-end 0)
4784 '(font-lock-fontified t face org-meta-line))
4786 ((or (member dc1 '("begin:" "end:" "caption:" "label:"
4787 "orgtbl:" "tblfm:" "tblname:"))
4788 (and (match-end 4) (equal dc3 "attr")))
4789 (add-text-properties
4790 beg (match-end 0)
4791 '(font-lock-fontified t face org-meta-line))
4793 ((member dc3 '(" " ""))
4794 (add-text-properties
4795 beg (match-end 0)
4796 '(font-lock-fontified t face font-lock-comment-face)))
4797 (t nil))))))
4799 (defun org-activate-angle-links (limit)
4800 "Run through the buffer and add overlays to links."
4801 (if (re-search-forward org-angle-link-re limit t)
4802 (progn
4803 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4804 (add-text-properties (match-beginning 0) (match-end 0)
4805 (list 'mouse-face 'highlight
4806 'keymap org-mouse-map))
4807 (org-rear-nonsticky-at (match-end 0))
4808 t)))
4810 (defun org-activate-footnote-links (limit)
4811 "Run through the buffer and add overlays to links."
4812 (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
4813 limit t)
4814 (progn
4815 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4816 (add-text-properties (match-beginning 2) (match-end 2)
4817 (list 'mouse-face 'highlight
4818 'keymap org-mouse-map
4819 'help-echo
4820 (if (= (point-at-bol) (match-beginning 2))
4821 "Footnote definition"
4822 "Footnote reference")
4824 (org-rear-nonsticky-at (match-end 2))
4825 t)))
4827 (defun org-activate-bracket-links (limit)
4828 "Run through the buffer and add overlays to bracketed links."
4829 (if (re-search-forward org-bracket-link-regexp limit t)
4830 (let* ((help (concat "LINK: "
4831 (org-match-string-no-properties 1)))
4832 ;; FIXME: above we should remove the escapes.
4833 ;; but that requires another match, protecting match data,
4834 ;; a lot of overhead for font-lock.
4835 (ip (org-maybe-intangible
4836 (list 'invisible 'org-link
4837 'keymap org-mouse-map 'mouse-face 'highlight
4838 'font-lock-multiline t 'help-echo help)))
4839 (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
4840 'font-lock-multiline t 'help-echo help)))
4841 ;; We need to remove the invisible property here. Table narrowing
4842 ;; may have made some of this invisible.
4843 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4844 (remove-text-properties (match-beginning 0) (match-end 0)
4845 '(invisible nil))
4846 (if (match-end 3)
4847 (progn
4848 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
4849 (org-rear-nonsticky-at (match-beginning 3))
4850 (add-text-properties (match-beginning 3) (match-end 3) vp)
4851 (org-rear-nonsticky-at (match-end 3))
4852 (add-text-properties (match-end 3) (match-end 0) ip)
4853 (org-rear-nonsticky-at (match-end 0)))
4854 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
4855 (org-rear-nonsticky-at (match-beginning 1))
4856 (add-text-properties (match-beginning 1) (match-end 1) vp)
4857 (org-rear-nonsticky-at (match-end 1))
4858 (add-text-properties (match-end 1) (match-end 0) ip)
4859 (org-rear-nonsticky-at (match-end 0)))
4860 t)))
4862 (defun org-activate-dates (limit)
4863 "Run through the buffer and add overlays to dates."
4864 (if (re-search-forward org-tsr-regexp-both limit t)
4865 (progn
4866 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4867 (add-text-properties (match-beginning 0) (match-end 0)
4868 (list 'mouse-face 'highlight
4869 'keymap org-mouse-map))
4870 (org-rear-nonsticky-at (match-end 0))
4871 (when org-display-custom-times
4872 (if (match-end 3)
4873 (org-display-custom-time (match-beginning 3) (match-end 3)))
4874 (org-display-custom-time (match-beginning 1) (match-end 1)))
4875 t)))
4877 (defvar org-target-link-regexp nil
4878 "Regular expression matching radio targets in plain text.")
4879 (make-variable-buffer-local 'org-target-link-regexp)
4880 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
4881 "Regular expression matching a link target.")
4882 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
4883 "Regular expression matching a radio target.")
4884 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
4885 "Regular expression matching any target.")
4887 (defun org-activate-target-links (limit)
4888 "Run through the buffer and add overlays to target matches."
4889 (when org-target-link-regexp
4890 (let ((case-fold-search t))
4891 (if (re-search-forward org-target-link-regexp limit t)
4892 (progn
4893 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4894 (add-text-properties (match-beginning 0) (match-end 0)
4895 (list 'mouse-face 'highlight
4896 'keymap org-mouse-map
4897 'help-echo "Radio target link"
4898 'org-linked-text t))
4899 (org-rear-nonsticky-at (match-end 0))
4900 t)))))
4902 (defun org-update-radio-target-regexp ()
4903 "Find all radio targets in this file and update the regular expression."
4904 (interactive)
4905 (when (memq 'radio org-activate-links)
4906 (setq org-target-link-regexp
4907 (org-make-target-link-regexp (org-all-targets 'radio)))
4908 (org-restart-font-lock)))
4910 (defun org-hide-wide-columns (limit)
4911 (let (s e)
4912 (setq s (text-property-any (point) (or limit (point-max))
4913 'org-cwidth t))
4914 (when s
4915 (setq e (next-single-property-change s 'org-cwidth))
4916 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
4917 (goto-char e)
4918 t)))
4920 (defvar org-latex-and-specials-regexp nil
4921 "Regular expression for highlighting export special stuff.")
4922 (defvar org-match-substring-regexp)
4923 (defvar org-match-substring-with-braces-regexp)
4925 ;; This should be with the exporter code, but we also use if for font-locking
4926 (defconst org-export-html-special-string-regexps
4927 '(("\\\\-" . "&shy;")
4928 ("---\\([^-]\\)" . "&mdash;\\1")
4929 ("--\\([^-]\\)" . "&ndash;\\1")
4930 ("\\.\\.\\." . "&hellip;"))
4931 "Regular expressions for special string conversion.")
4934 (defun org-compute-latex-and-specials-regexp ()
4935 "Compute regular expression for stuff treated specially by exporters."
4936 (if (not org-highlight-latex-fragments-and-specials)
4937 (org-set-local 'org-latex-and-specials-regexp nil)
4938 (require 'org-exp)
4939 (let*
4940 ((matchers (plist-get org-format-latex-options :matchers))
4941 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
4942 org-latex-regexps)))
4943 (org-export-allow-BIND nil)
4944 (options (org-combine-plists (org-default-export-plist)
4945 (org-infile-export-plist)))
4946 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
4947 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
4948 (org-export-with-TeX-macros (plist-get options :TeX-macros))
4949 (org-export-html-expand (plist-get options :expand-quoted-html))
4950 (org-export-with-special-strings (plist-get options :special-strings))
4951 (re-sub
4952 (cond
4953 ((equal org-export-with-sub-superscripts '{})
4954 (list org-match-substring-with-braces-regexp))
4955 (org-export-with-sub-superscripts
4956 (list org-match-substring-regexp))
4957 (t nil)))
4958 (re-latex
4959 (if org-export-with-LaTeX-fragments
4960 (mapcar (lambda (x) (nth 1 x)) latexs)))
4961 (re-macros
4962 (if org-export-with-TeX-macros
4963 (list (concat "\\\\"
4964 (regexp-opt
4965 (append (mapcar 'car (append org-entities-user
4966 org-entities))
4967 (if (boundp 'org-latex-entities)
4968 (mapcar (lambda (x)
4969 (or (car-safe x) x))
4970 org-latex-entities)
4971 nil))
4972 'words))) ; FIXME
4974 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
4975 (re-special (if org-export-with-special-strings
4976 (mapcar (lambda (x) (car x))
4977 org-export-html-special-string-regexps)))
4978 (re-rest
4979 (delq nil
4980 (list
4981 (if org-export-html-expand "@<[^>\n]+>")
4982 ))))
4983 (org-set-local
4984 'org-latex-and-specials-regexp
4985 (mapconcat 'identity (append re-latex re-sub re-macros re-special
4986 re-rest) "\\|")))))
4988 (defun org-do-latex-and-special-faces (limit)
4989 "Run through the buffer and add overlays to links."
4990 (when org-latex-and-specials-regexp
4991 (let (rtn d)
4992 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
4993 limit t))
4994 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
4995 'face))
4996 '(org-code org-verbatim underline)))
4997 (progn
4998 (setq rtn t
4999 d (cond ((member (char-after (1+ (match-beginning 0)))
5000 '(?_ ?^)) 1)
5001 (t 0)))
5002 (font-lock-prepend-text-property
5003 (+ d (match-beginning 0)) (match-end 0)
5004 'face 'org-latex-and-export-specials)
5005 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
5006 '(font-lock-multiline t)))))
5007 rtn)))
5009 (defun org-restart-font-lock ()
5010 "Restart font-lock-mode, to force refontification."
5011 (when (and (boundp 'font-lock-mode) font-lock-mode)
5012 (font-lock-mode -1)
5013 (font-lock-mode 1)))
5015 (defun org-all-targets (&optional radio)
5016 "Return a list of all targets in this file.
5017 With optional argument RADIO, only find radio targets."
5018 (let ((re (if radio org-radio-target-regexp org-target-regexp))
5019 rtn)
5020 (save-excursion
5021 (goto-char (point-min))
5022 (while (re-search-forward re nil t)
5023 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
5024 rtn)))
5026 (defun org-make-target-link-regexp (targets)
5027 "Make regular expression matching all strings in TARGETS.
5028 The regular expression finds the targets also if there is a line break
5029 between words."
5030 (and targets
5031 (concat
5032 "\\<\\("
5033 (mapconcat
5034 (lambda (x)
5035 (while (string-match " +" x)
5036 (setq x (replace-match "\\s-+" t t x)))
5038 targets
5039 "\\|")
5040 "\\)\\>")))
5042 (defun org-activate-tags (limit)
5043 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
5044 (progn
5045 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5046 (add-text-properties (match-beginning 1) (match-end 1)
5047 (list 'mouse-face 'highlight
5048 'keymap org-mouse-map))
5049 (org-rear-nonsticky-at (match-end 1))
5050 t)))
5052 (defun org-outline-level ()
5053 "Compute the outline level of the heading at point.
5054 This function assumes that the cursor is at the beginning of a line matched
5055 by outline-regexp. Otherwise it returns garbage.
5056 If this is called at a normal headline, the level is the number of stars.
5057 Use `org-reduced-level' to remove the effect of `org-odd-levels'.
5058 For plain list items, if they are matched by `outline-regexp', this returns
5059 1000 plus the line indentation."
5060 (save-excursion
5061 (looking-at outline-regexp)
5062 (if (match-beginning 1)
5063 (+ (org-get-string-indentation (match-string 1)) 1000)
5064 (1- (- (match-end 0) (match-beginning 0))))))
5066 (defvar org-font-lock-keywords nil)
5068 (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5069 "Regular expression matching a property line.")
5071 (defvar org-font-lock-hook nil
5072 "Functions to be called for special font lock stuff.")
5074 (defun org-font-lock-hook (limit)
5075 (run-hook-with-args 'org-font-lock-hook limit))
5077 (defun org-set-font-lock-defaults ()
5078 (let* ((em org-fontify-emphasized-text)
5079 (lk org-activate-links)
5080 (org-font-lock-extra-keywords
5081 (list
5082 ;; Call the hook
5083 '(org-font-lock-hook)
5084 ;; Headlines
5085 `(,(if org-fontify-whole-heading-line
5086 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5087 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5088 (1 (org-get-level-face 1))
5089 (2 (org-get-level-face 2))
5090 (3 (org-get-level-face 3)))
5091 ;; Table lines
5092 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5093 (1 'org-table t))
5094 ;; Table internals
5095 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
5096 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
5097 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
5098 '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
5099 ;; Drawers
5100 (list org-drawer-regexp '(0 'org-special-keyword t))
5101 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
5102 ;; Properties
5103 (list org-property-re
5104 '(1 'org-special-keyword t)
5105 '(3 'org-property-value t))
5106 ;; Links
5107 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
5108 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
5109 (if (memq 'plain lk) '(org-activate-plain-links))
5110 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
5111 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
5112 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
5113 (if (memq 'footnote lk) '(org-activate-footnote-links
5114 (2 'org-footnote t)))
5115 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
5116 '(org-hide-wide-columns (0 nil append))
5117 ;; TODO lines
5118 (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
5119 '(1 (org-get-todo-face 1) t))
5120 ;; DONE
5121 (if org-fontify-done-headline
5122 (list (concat "^[*]+ +\\<\\("
5123 (mapconcat 'regexp-quote org-done-keywords "\\|")
5124 "\\)\\(.*\\)")
5125 '(2 'org-headline-done t))
5126 nil)
5127 ;; Priorities
5128 '(org-font-lock-add-priority-faces)
5129 ;; Tags
5130 '(org-font-lock-add-tag-faces)
5131 ;; Special keywords
5132 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
5133 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
5134 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
5135 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
5136 ;; Emphasis
5137 (if em
5138 (if (featurep 'xemacs)
5139 '(org-do-emphasis-faces (0 nil append))
5140 '(org-do-emphasis-faces)))
5141 ;; Checkboxes
5142 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
5143 2 'org-checkbox prepend)
5144 (if org-provide-checkbox-statistics
5145 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5146 (0 (org-get-checkbox-statistics-face) t)))
5147 ;; Description list items
5148 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
5149 2 'bold prepend)
5150 ;; ARCHIVEd headings
5151 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
5152 '(1 'org-archived prepend))
5153 ;; Specials
5154 '(org-do-latex-and-special-faces)
5155 ;; Code
5156 '(org-activate-code (1 'org-code t))
5157 ;; COMMENT
5158 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
5159 "\\|" org-quote-string "\\)\\>")
5160 '(1 'org-special-keyword t))
5161 '("^#.*" (0 'font-lock-comment-face t))
5162 ;; Blocks and meta lines
5163 '(org-fontify-meta-lines-and-blocks)
5165 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
5166 ;; Now set the full font-lock-keywords
5167 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
5168 (org-set-local 'font-lock-defaults
5169 '(org-font-lock-keywords t nil nil backward-paragraph))
5170 (kill-local-variable 'font-lock-keywords) nil))
5172 (defun org-fontify-like-in-org-mode (s &optional odd-levels)
5173 "Fontify string S like in Org-mode"
5174 (with-temp-buffer
5175 (insert s)
5176 (let ((org-odd-levels-only odd-levels))
5177 (org-mode)
5178 (font-lock-fontify-buffer)
5179 (buffer-string))))
5181 (defvar org-m nil)
5182 (defvar org-l nil)
5183 (defvar org-f nil)
5184 (defun org-get-level-face (n)
5185 "Get the right face for match N in font-lock matching of headlines."
5186 (setq org-l (- (match-end 2) (match-beginning 1) 1))
5187 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
5188 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
5189 (cond
5190 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
5191 ((eq n 2) org-f)
5192 (t (if org-level-color-stars-only nil org-f))))
5194 (defun org-get-todo-face (kwd)
5195 "Get the right face for a TODO keyword KWD.
5196 If KWD is a number, get the corresponding match group."
5197 (if (numberp kwd) (setq kwd (match-string kwd)))
5198 (or (org-face-from-face-or-color
5199 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
5200 (and (member kwd org-done-keywords) 'org-done)
5201 'org-todo))
5203 (defun org-face-from-face-or-color (context inherit face-or-color)
5204 "Create a face list that inherits INHERIT, but sets the foreground color.
5205 When FACE-OR-COLOR is not a string, just return it."
5206 (if (stringp face-or-color)
5207 (list :inherit inherit
5208 (cdr (assoc context org-faces-easy-properties))
5209 face-or-color)
5210 face-or-color))
5212 (defun org-font-lock-add-tag-faces (limit)
5213 "Add the special tag faces."
5214 (when (and org-tag-faces org-tags-special-faces-re)
5215 (while (re-search-forward org-tags-special-faces-re limit t)
5216 (add-text-properties (match-beginning 1) (match-end 1)
5217 (list 'face (org-get-tag-face 1)
5218 'font-lock-fontified t))
5219 (backward-char 1))))
5221 (defun org-font-lock-add-priority-faces (limit)
5222 "Add the special priority faces."
5223 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
5224 (add-text-properties
5225 (match-beginning 0) (match-end 0)
5226 (list 'face (or (org-face-from-face-or-color
5227 'priority 'org-special-keyword
5228 (cdr (assoc (char-after (match-beginning 1))
5229 org-priority-faces)))
5230 'org-special-keyword)
5231 'font-lock-fontified t))))
5233 (defun org-get-tag-face (kwd)
5234 "Get the right face for a TODO keyword KWD.
5235 If KWD is a number, get the corresponding match group."
5236 (if (numberp kwd) (setq kwd (match-string kwd)))
5237 (or (org-face-from-face-or-color
5238 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
5239 'org-tag))
5241 (defun org-unfontify-region (beg end &optional maybe_loudly)
5242 "Remove fontification and activation overlays from links."
5243 (font-lock-default-unfontify-region beg end)
5244 (let* ((buffer-undo-list t)
5245 (inhibit-read-only t) (inhibit-point-motion-hooks t)
5246 (inhibit-modification-hooks t)
5247 deactivate-mark buffer-file-name buffer-file-truename)
5248 (remove-text-properties
5249 beg end
5250 (if org-indent-mode
5251 ;; also remove line-prefix and wrap-prefix properties
5252 '(mouse-face t keymap t org-linked-text t
5253 invisible t intangible t
5254 line-prefix t wrap-prefix t
5255 org-no-flyspell t)
5256 '(mouse-face t keymap t org-linked-text t
5257 invisible t intangible t
5258 org-no-flyspell t)))))
5260 ;;;; Visibility cycling, including org-goto and indirect buffer
5262 ;;; Cycling
5264 (defvar org-cycle-global-status nil)
5265 (make-variable-buffer-local 'org-cycle-global-status)
5266 (defvar org-cycle-subtree-status nil)
5267 (make-variable-buffer-local 'org-cycle-subtree-status)
5269 ;;;###autoload
5271 (defvar org-inlinetask-min-level)
5273 (defun org-cycle (&optional arg)
5274 "TAB-action and visibility cycling for Org-mode.
5276 This is the command invoked in Org-mode by the TAB key. Its main purpose
5277 is outline visibility cycling, but it also invokes other actions
5278 in special contexts.
5280 - When this function is called with a prefix argument, rotate the entire
5281 buffer through 3 states (global cycling)
5282 1. OVERVIEW: Show only top-level headlines.
5283 2. CONTENTS: Show all headlines of all levels, but no body text.
5284 3. SHOW ALL: Show everything.
5285 When called with two `C-u C-u' prefixes, switch to the startup visibility,
5286 determined by the variable `org-startup-folded', and by any VISIBILITY
5287 properties in the buffer.
5288 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
5289 including any drawers.
5291 - When inside a table, re-align the table and move to the next field.
5293 - When point is at the beginning of a headline, rotate the subtree started
5294 by this line through 3 different states (local cycling)
5295 1. FOLDED: Only the main headline is shown.
5296 2. CHILDREN: The main headline and the direct children are shown.
5297 From this state, you can move to one of the children
5298 and zoom in further.
5299 3. SUBTREE: Show the entire subtree, including body text.
5300 If there is no subtree, switch directly from CHILDREN to FOLDED.
5302 - When point is at the beginning of an empty headline and the variable
5303 `org-cycle-level-after-item/entry-creation' is set, cycle the level
5304 of the headline by demoting and promoting it to likely levels. This
5305 speeds up creation document structure by presing TAB once or several
5306 times right after creating a new headline.
5308 - When there is a numeric prefix, go up to a heading with level ARG, do
5309 a `show-subtree' and return to the previous cursor position. If ARG
5310 is negative, go up that many levels.
5312 - When point is not at the beginning of a headline, execute the global
5313 binding for TAB, which is re-indenting the line. See the option
5314 `org-cycle-emulate-tab' for details.
5316 - Special case: if point is at the beginning of the buffer and there is
5317 no headline in line 1, this function will act as if called with prefix arg.
5318 But only if also the variable `org-cycle-global-at-bob' is t."
5319 (interactive "P")
5320 (org-load-modules-maybe)
5321 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
5322 (and org-cycle-level-after-item/entry-creation
5323 (or (org-cycle-level)
5324 (org-cycle-item-indentation))))
5325 (let* ((limit-level
5326 (or org-cycle-max-level
5327 (and (boundp 'org-inlinetask-min-level)
5328 org-inlinetask-min-level
5329 (1- org-inlinetask-min-level))))
5330 (nstars (and limit-level
5331 (if org-odd-levels-only
5332 (and limit-level (1- (* limit-level 2)))
5333 limit-level)))
5334 (outline-regexp
5335 (cond
5336 ((not (org-mode-p)) outline-regexp)
5337 ((or (eq org-cycle-include-plain-lists 'integrate)
5338 (and org-cycle-include-plain-lists (org-at-item-p)))
5339 (concat "\\(?:\\*"
5340 (if nstars (format "\\{1,%d\\}" nstars) "+")
5341 " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
5342 (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
5343 (bob-special (and org-cycle-global-at-bob (bobp)
5344 (not (looking-at outline-regexp))))
5345 (org-cycle-hook
5346 (if bob-special
5347 (delq 'org-optimize-window-after-visibility-change
5348 (copy-sequence org-cycle-hook))
5349 org-cycle-hook))
5350 (pos (point)))
5352 (if (or bob-special (equal arg '(4)))
5353 ;; special case: use global cycling
5354 (setq arg t))
5356 (cond
5358 ((equal arg '(16))
5359 (org-set-startup-visibility)
5360 (message "Startup visibility, plus VISIBILITY properties"))
5362 ((equal arg '(64))
5363 (show-all)
5364 (message "Entire buffer visible, including drawers"))
5366 ((org-at-table-p 'any)
5367 ;; Enter the table or move to the next field in the table
5368 (if (org-at-table.el-p)
5369 (message "Use C-c ' to edit table.el tables")
5370 (if arg (org-table-edit-field t)
5371 (org-table-justify-field-maybe)
5372 (call-interactively 'org-table-next-field))))
5374 ((run-hook-with-args-until-success
5375 'org-tab-after-check-for-table-hook))
5377 ((eq arg t) ;; Global cycling
5378 (org-cycle-internal-global))
5380 ((and org-drawers org-drawer-regexp
5381 (save-excursion
5382 (beginning-of-line 1)
5383 (looking-at org-drawer-regexp)))
5384 ;; Toggle block visibility
5385 (org-flag-drawer
5386 (not (get-char-property (match-end 0) 'invisible))))
5388 ((integerp arg)
5389 ;; Show-subtree, ARG levels up from here.
5390 (save-excursion
5391 (org-back-to-heading)
5392 (outline-up-heading (if (< arg 0) (- arg)
5393 (- (funcall outline-level) arg)))
5394 (org-show-subtree)))
5396 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
5397 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
5399 (org-cycle-internal-local))
5401 ;; TAB emulation and template completion
5402 (buffer-read-only (org-back-to-heading))
5404 ((run-hook-with-args-until-success
5405 'org-tab-after-check-for-cycling-hook))
5407 ((org-try-structure-completion))
5409 ((org-try-cdlatex-tab))
5411 ((run-hook-with-args-until-success
5412 'org-tab-before-tab-emulation-hook))
5414 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
5415 (or (not (bolp))
5416 (not (looking-at outline-regexp))))
5417 (call-interactively (global-key-binding "\t")))
5419 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
5420 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
5421 (or (and (eq org-cycle-emulate-tab 'white)
5422 (= (match-end 0) (point-at-eol)))
5423 (and (eq org-cycle-emulate-tab 'whitestart)
5424 (>= (match-end 0) pos))))
5426 (eq org-cycle-emulate-tab t))
5427 (call-interactively (global-key-binding "\t")))
5429 (t (save-excursion
5430 (org-back-to-heading)
5431 (org-cycle)))))))
5433 (defun org-cycle-internal-global ()
5434 "Do the global cycling action."
5435 (cond
5436 ((and (eq last-command this-command)
5437 (eq org-cycle-global-status 'overview))
5438 ;; We just created the overview - now do table of contents
5439 ;; This can be slow in very large buffers, so indicate action
5440 (run-hook-with-args 'org-pre-cycle-hook 'contents)
5441 (message "CONTENTS...")
5442 (org-content)
5443 (message "CONTENTS...done")
5444 (setq org-cycle-global-status 'contents)
5445 (run-hook-with-args 'org-cycle-hook 'contents))
5447 ((and (eq last-command this-command)
5448 (eq org-cycle-global-status 'contents))
5449 ;; We just showed the table of contents - now show everything
5450 (run-hook-with-args 'org-pre-cycle-hook 'all)
5451 (show-all)
5452 (message "SHOW ALL")
5453 (setq org-cycle-global-status 'all)
5454 (run-hook-with-args 'org-cycle-hook 'all))
5457 ;; Default action: go to overview
5458 (run-hook-with-args 'org-pre-cycle-hook 'overview)
5459 (org-overview)
5460 (message "OVERVIEW")
5461 (setq org-cycle-global-status 'overview)
5462 (run-hook-with-args 'org-cycle-hook 'overview))))
5464 (defun org-cycle-internal-local ()
5465 "Do the local cycling action."
5466 (org-back-to-heading)
5467 (let ((goal-column 0) eoh eol eos level has-children children-skipped)
5468 ;; First, some boundaries
5469 (save-excursion
5470 (org-back-to-heading)
5471 (setq level (funcall outline-level))
5472 (save-excursion
5473 (beginning-of-line 2)
5474 (if (or (featurep 'xemacs) (<= emacs-major-version 21))
5475 ; XEmacs does not have `next-single-char-property-change'
5476 ; I'm not sure about Emacs 21.
5477 (while (and (not (eobp)) ;; this is like `next-line'
5478 (get-char-property (1- (point)) 'invisible))
5479 (beginning-of-line 2))
5480 (while (and (not (eobp)) ;; this is like `next-line'
5481 (get-char-property (1- (point)) 'invisible))
5482 (goto-char (next-single-char-property-change (point) 'invisible))
5483 ;;;??? (or (bolp) (beginning-of-line 2))))
5484 (and (eolp) (beginning-of-line 2))))
5485 (setq eol (point)))
5486 (outline-end-of-heading) (setq eoh (point))
5487 (save-excursion
5488 (outline-next-heading)
5489 (setq has-children (and (org-at-heading-p t)
5490 (> (funcall outline-level) level))))
5491 (org-end-of-subtree t)
5492 (unless (eobp)
5493 (skip-chars-forward " \t\n")
5494 (beginning-of-line 1) ; in case this is an item
5496 (setq eos (if (eobp) (point) (1- (point)))))
5497 ;; Find out what to do next and set `this-command'
5498 (cond
5499 ((= eos eoh)
5500 ;; Nothing is hidden behind this heading
5501 (run-hook-with-args 'org-pre-cycle-hook 'empty)
5502 (message "EMPTY ENTRY")
5503 (setq org-cycle-subtree-status nil)
5504 (save-excursion
5505 (goto-char eos)
5506 (outline-next-heading)
5507 (if (org-invisible-p) (org-flag-heading nil))))
5508 ((and (or (>= eol eos)
5509 (not (string-match "\\S-" (buffer-substring eol eos))))
5510 (or has-children
5511 (not (setq children-skipped
5512 org-cycle-skip-children-state-if-no-children))))
5513 ;; Entire subtree is hidden in one line: children view
5514 (run-hook-with-args 'org-pre-cycle-hook 'children)
5515 (org-show-entry)
5516 (show-children)
5517 (message "CHILDREN")
5518 (save-excursion
5519 (goto-char eos)
5520 (outline-next-heading)
5521 (if (org-invisible-p) (org-flag-heading nil)))
5522 (setq org-cycle-subtree-status 'children)
5523 (run-hook-with-args 'org-cycle-hook 'children))
5524 ((or children-skipped
5525 (and (eq last-command this-command)
5526 (eq org-cycle-subtree-status 'children)))
5527 ;; We just showed the children, or no children are there,
5528 ;; now show everything.
5529 (run-hook-with-args 'org-pre-cycle-hook 'subtree)
5530 (org-show-subtree)
5531 (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
5532 (setq org-cycle-subtree-status 'subtree)
5533 (run-hook-with-args 'org-cycle-hook 'subtree))
5535 ;; Default action: hide the subtree.
5536 (run-hook-with-args 'org-pre-cycle-hook 'folded)
5537 (hide-subtree)
5538 (message "FOLDED")
5539 (setq org-cycle-subtree-status 'folded)
5540 (run-hook-with-args 'org-cycle-hook 'folded)))))
5542 ;;;###autoload
5543 (defun org-global-cycle (&optional arg)
5544 "Cycle the global visibility. For details see `org-cycle'.
5545 With C-u prefix arg, switch to startup visibility.
5546 With a numeric prefix, show all headlines up to that level."
5547 (interactive "P")
5548 (let ((org-cycle-include-plain-lists
5549 (if (org-mode-p) org-cycle-include-plain-lists nil)))
5550 (cond
5551 ((integerp arg)
5552 (show-all)
5553 (hide-sublevels arg)
5554 (setq org-cycle-global-status 'contents))
5555 ((equal arg '(4))
5556 (org-set-startup-visibility)
5557 (message "Startup visibility, plus VISIBILITY properties."))
5559 (org-cycle '(4))))))
5561 (defun org-set-startup-visibility ()
5562 "Set the visibility required by startup options and properties."
5563 (cond
5564 ((eq org-startup-folded t)
5565 (org-cycle '(4)))
5566 ((eq org-startup-folded 'content)
5567 (let ((this-command 'org-cycle) (last-command 'org-cycle))
5568 (org-cycle '(4)) (org-cycle '(4)))))
5569 (unless (eq org-startup-folded 'showeverything)
5570 (if org-hide-block-startup (org-hide-block-all))
5571 (org-set-visibility-according-to-property 'no-cleanup)
5572 (org-cycle-hide-archived-subtrees 'all)
5573 (org-cycle-hide-drawers 'all)
5574 (org-cycle-show-empty-lines 'all)))
5576 (defun org-set-visibility-according-to-property (&optional no-cleanup)
5577 "Switch subtree visibilities according to :VISIBILITY: property."
5578 (interactive)
5579 (let (org-show-entry-below state)
5580 (save-excursion
5581 (goto-char (point-min))
5582 (while (re-search-forward
5583 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
5584 nil t)
5585 (setq state (match-string 1))
5586 (save-excursion
5587 (org-back-to-heading t)
5588 (hide-subtree)
5589 (org-reveal)
5590 (cond
5591 ((equal state '("fold" "folded"))
5592 (hide-subtree))
5593 ((equal state "children")
5594 (org-show-hidden-entry)
5595 (show-children))
5596 ((equal state "content")
5597 (save-excursion
5598 (save-restriction
5599 (org-narrow-to-subtree)
5600 (org-content))))
5601 ((member state '("all" "showall"))
5602 (show-subtree)))))
5603 (unless no-cleanup
5604 (org-cycle-hide-archived-subtrees 'all)
5605 (org-cycle-hide-drawers 'all)
5606 (org-cycle-show-empty-lines 'all)))))
5608 (defun org-overview ()
5609 "Switch to overview mode, showing only top-level headlines.
5610 Really, this shows all headlines with level equal or greater than the level
5611 of the first headline in the buffer. This is important, because if the
5612 first headline is not level one, then (hide-sublevels 1) gives confusing
5613 results."
5614 (interactive)
5615 (let ((level (save-excursion
5616 (goto-char (point-min))
5617 (if (re-search-forward (concat "^" outline-regexp) nil t)
5618 (progn
5619 (goto-char (match-beginning 0))
5620 (funcall outline-level))))))
5621 (and level (hide-sublevels level))))
5623 (defun org-content (&optional arg)
5624 "Show all headlines in the buffer, like a table of contents.
5625 With numerical argument N, show content up to level N."
5626 (interactive "P")
5627 (save-excursion
5628 ;; Visit all headings and show their offspring
5629 (and (integerp arg) (org-overview))
5630 (goto-char (point-max))
5631 (catch 'exit
5632 (while (and (progn (condition-case nil
5633 (outline-previous-visible-heading 1)
5634 (error (goto-char (point-min))))
5636 (looking-at outline-regexp))
5637 (if (integerp arg)
5638 (show-children (1- arg))
5639 (show-branches))
5640 (if (bobp) (throw 'exit nil))))))
5643 (defun org-optimize-window-after-visibility-change (state)
5644 "Adjust the window after a change in outline visibility.
5645 This function is the default value of the hook `org-cycle-hook'."
5646 (when (get-buffer-window (current-buffer))
5647 (cond
5648 ((eq state 'content) nil)
5649 ((eq state 'all) nil)
5650 ((eq state 'folded) nil)
5651 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
5652 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
5654 (defun org-remove-empty-overlays-at (pos)
5655 "Remove outline overlays that do not contain non-white stuff."
5656 (mapc
5657 (lambda (o)
5658 (and (eq 'outline (overlay-get o 'invisible))
5659 (not (string-match "\\S-" (buffer-substring (overlay-start o)
5660 (overlay-end o))))
5661 (delete-overlay o)))
5662 (org-overlays-at pos)))
5664 (defun org-clean-visibility-after-subtree-move ()
5665 "Fix visibility issues after moving a subtree."
5666 ;; First, find a reasonable region to look at:
5667 ;; Start two siblings above, end three below
5668 (let* ((beg (save-excursion
5669 (and (org-get-last-sibling)
5670 (org-get-last-sibling))
5671 (point)))
5672 (end (save-excursion
5673 (and (org-get-next-sibling)
5674 (org-get-next-sibling)
5675 (org-get-next-sibling))
5676 (if (org-at-heading-p)
5677 (point-at-eol)
5678 (point))))
5679 (level (looking-at "\\*+"))
5680 (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
5681 (save-excursion
5682 (save-restriction
5683 (narrow-to-region beg end)
5684 (when re
5685 ;; Properly fold already folded siblings
5686 (goto-char (point-min))
5687 (while (re-search-forward re nil t)
5688 (if (and (not (org-invisible-p))
5689 (save-excursion
5690 (goto-char (point-at-eol)) (org-invisible-p)))
5691 (hide-entry))))
5692 (org-cycle-show-empty-lines 'overview)
5693 (org-cycle-hide-drawers 'overview)))))
5695 (defun org-cycle-show-empty-lines (state)
5696 "Show empty lines above all visible headlines.
5697 The region to be covered depends on STATE when called through
5698 `org-cycle-hook'. Lisp program can use t for STATE to get the
5699 entire buffer covered. Note that an empty line is only shown if there
5700 are at least `org-cycle-separator-lines' empty lines before the headline."
5701 (when (not (= org-cycle-separator-lines 0))
5702 (save-excursion
5703 (let* ((n (abs org-cycle-separator-lines))
5704 (re (cond
5705 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
5706 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
5707 (t (let ((ns (number-to-string (- n 2))))
5708 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
5709 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
5710 beg end b e)
5711 (cond
5712 ((memq state '(overview contents t))
5713 (setq beg (point-min) end (point-max)))
5714 ((memq state '(children folded))
5715 (setq beg (point) end (progn (org-end-of-subtree t t)
5716 (beginning-of-line 2)
5717 (point)))))
5718 (when beg
5719 (goto-char beg)
5720 (while (re-search-forward re end t)
5721 (unless (get-char-property (match-end 1) 'invisible)
5722 (setq e (match-end 1))
5723 (if (< org-cycle-separator-lines 0)
5724 (setq b (save-excursion
5725 (goto-char (match-beginning 0))
5726 (org-back-over-empty-lines)
5727 (if (save-excursion
5728 (goto-char (max (point-min) (1- (point))))
5729 (org-on-heading-p))
5730 (1- (point))
5731 (point))))
5732 (setq b (match-beginning 1)))
5733 (outline-flag-region b e nil)))))))
5734 ;; Never hide empty lines at the end of the file.
5735 (save-excursion
5736 (goto-char (point-max))
5737 (outline-previous-heading)
5738 (outline-end-of-heading)
5739 (if (and (looking-at "[ \t\n]+")
5740 (= (match-end 0) (point-max)))
5741 (outline-flag-region (point) (match-end 0) nil))))
5743 (defun org-show-empty-lines-in-parent ()
5744 "Move to the parent and re-show empty lines before visible headlines."
5745 (save-excursion
5746 (let ((context (if (org-up-heading-safe) 'children 'overview)))
5747 (org-cycle-show-empty-lines context))))
5749 (defun org-files-list ()
5750 "Return `org-agenda-files' list, plus all open org-mode files.
5751 This is useful for operations that need to scan all of a user's
5752 open and agenda-wise Org files."
5753 (let ((files (mapcar 'expand-file-name (org-agenda-files))))
5754 (dolist (buf (buffer-list))
5755 (with-current-buffer buf
5756 (if (and (eq major-mode 'org-mode) (buffer-file-name))
5757 (let ((file (expand-file-name (buffer-file-name))))
5758 (unless (member file files)
5759 (push file files))))))
5760 files))
5762 (defsubst org-entry-beginning-position ()
5763 "Return the beginning position of the current entry."
5764 (save-excursion (outline-back-to-heading t) (point)))
5766 (defsubst org-entry-end-position ()
5767 "Return the end position of the current entry."
5768 (save-excursion (outline-next-heading) (point)))
5770 (defun org-cycle-hide-drawers (state)
5771 "Re-hide all drawers after a visibility state change."
5772 (when (and (org-mode-p)
5773 (not (memq state '(overview folded contents))))
5774 (save-excursion
5775 (let* ((globalp (memq state '(contents all)))
5776 (beg (if globalp (point-min) (point)))
5777 (end (if globalp (point-max)
5778 (if (eq state 'children)
5779 (save-excursion (outline-next-heading) (point))
5780 (org-end-of-subtree t)))))
5781 (goto-char beg)
5782 (while (re-search-forward org-drawer-regexp end t)
5783 (org-flag-drawer t))))))
5785 (defun org-flag-drawer (flag)
5786 (save-excursion
5787 (beginning-of-line 1)
5788 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
5789 (let ((b (match-end 0))
5790 (outline-regexp org-outline-regexp))
5791 (if (re-search-forward
5792 "^[ \t]*:END:"
5793 (save-excursion (outline-next-heading) (point)) t)
5794 (outline-flag-region b (point-at-eol) flag)
5795 (error ":END: line missing at position %s" b))))))
5797 (defun org-subtree-end-visible-p ()
5798 "Is the end of the current subtree visible?"
5799 (pos-visible-in-window-p
5800 (save-excursion (org-end-of-subtree t) (point))))
5802 (defun org-first-headline-recenter (&optional N)
5803 "Move cursor to the first headline and recenter the headline.
5804 Optional argument N means put the headline into the Nth line of the window."
5805 (goto-char (point-min))
5806 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
5807 (beginning-of-line)
5808 (recenter (prefix-numeric-value N))))
5810 ;;; Saving and restoring visibility
5812 (defun org-outline-overlay-data (&optional use-markers)
5813 "Return a list of the locations of all outline overlays.
5814 The are overlays with the `invisible' property value `outline'.
5815 The return valus is a list of cons cells, with start and stop
5816 positions for each overlay.
5817 If USE-MARKERS is set, return the positions as markers."
5818 (let (beg end)
5819 (save-excursion
5820 (save-restriction
5821 (widen)
5822 (delq nil
5823 (mapcar (lambda (o)
5824 (when (eq (overlay-get o 'invisible) 'outline)
5825 (setq beg (overlay-start o)
5826 end (overlay-end o))
5827 (and beg end (> end beg)
5828 (if use-markers
5829 (cons (move-marker (make-marker) beg)
5830 (move-marker (make-marker) end))
5831 (cons beg end)))))
5832 (org-overlays-in (point-min) (point-max))))))))
5834 (defun org-set-outline-overlay-data (data)
5835 "Create visibility overlays for all positions in DATA.
5836 DATA should have been made by `org-outline-overlay-data'."
5837 (let (o)
5838 (save-excursion
5839 (save-restriction
5840 (widen)
5841 (show-all)
5842 (mapc (lambda (c)
5843 (setq o (make-overlay (car c) (cdr c)))
5844 (overlay-put o 'invisible 'outline))
5845 data)))))
5847 (defmacro org-save-outline-visibility (use-markers &rest body)
5848 "Save and restore outline visibility around BODY.
5849 If USE-MARKERS is non-nil, use markers for the positions.
5850 This means that the buffer may change while running BODY,
5851 but it also means that the buffer should stay alive
5852 during the operation, because otherwise all these markers will
5853 point nowhere."
5854 `(let ((data (org-outline-overlay-data ,use-markers)))
5855 (unwind-protect
5856 (progn
5857 ,@body
5858 (org-set-outline-overlay-data data))
5859 (when ,use-markers
5860 (mapc (lambda (c)
5861 (and (markerp (car c)) (move-marker (car c) nil))
5862 (and (markerp (cdr c)) (move-marker (cdr c) nil)))
5863 data)))))
5866 ;;; Folding of blocks
5868 (defconst org-block-regexp
5870 "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
5871 "Regular expression for hiding blocks.")
5873 (defvar org-hide-block-overlays nil
5874 "Overlays hiding blocks.")
5875 (make-variable-buffer-local 'org-hide-block-overlays)
5877 (defun org-block-map (function &optional start end)
5878 "Call func at the head of all source blocks in the current
5879 buffer. Optional arguments START and END can be used to limit
5880 the range."
5881 (let ((start (or start (point-min)))
5882 (end (or end (point-max))))
5883 (save-excursion
5884 (goto-char start)
5885 (while (and (< (point) end) (re-search-forward org-block-regexp end t))
5886 (save-excursion
5887 (save-match-data
5888 (goto-char (match-beginning 0))
5889 (funcall function)))))))
5891 (defun org-hide-block-toggle-all ()
5892 "Toggle the visibility of all blocks in the current buffer."
5893 (org-block-map #'org-hide-block-toggle))
5895 (defun org-hide-block-all ()
5896 "Fold all blocks in the current buffer."
5897 (interactive)
5898 (org-show-block-all)
5899 (org-block-map #'org-hide-block-toggle-maybe))
5901 (defun org-show-block-all ()
5902 "Unfold all blocks in the current buffer."
5903 (mapc 'delete-overlay org-hide-block-overlays)
5904 (setq org-hide-block-overlays nil))
5906 (defun org-hide-block-toggle-maybe ()
5907 "Toggle visibility of block at point."
5908 (interactive)
5909 (let ((case-fold-search t))
5910 (if (save-excursion
5911 (beginning-of-line 1)
5912 (looking-at org-block-regexp))
5913 (progn (org-hide-block-toggle)
5914 t) ;; to signal that we took action
5915 nil))) ;; to signal that we did not
5917 (defun org-hide-block-toggle (&optional force)
5918 "Toggle the visibility of the current block."
5919 (interactive)
5920 (save-excursion
5921 (beginning-of-line)
5922 (if (re-search-forward org-block-regexp nil t)
5923 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
5924 (end (match-end 0)) ;; end of entire body
5926 (if (memq t (mapcar (lambda (overlay)
5927 (eq (overlay-get overlay 'invisible)
5928 'org-hide-block))
5929 (org-overlays-at start)))
5930 (if (or (not force) (eq force 'off))
5931 (mapc (lambda (ov)
5932 (when (member ov org-hide-block-overlays)
5933 (setq org-hide-block-overlays
5934 (delq ov org-hide-block-overlays)))
5935 (when (eq (overlay-get ov 'invisible)
5936 'org-hide-block)
5937 (delete-overlay ov)))
5938 (org-overlays-at start)))
5939 (setq ov (make-overlay start end))
5940 (overlay-put ov 'invisible 'org-hide-block)
5941 ;; make the block accessible to isearch
5942 (overlay-put
5943 ov 'isearch-open-invisible
5944 (lambda (ov)
5945 (when (member ov org-hide-block-overlays)
5946 (setq org-hide-block-overlays
5947 (delq ov org-hide-block-overlays)))
5948 (when (eq (overlay-get ov 'invisible)
5949 'org-hide-block)
5950 (delete-overlay ov))))
5951 (push ov org-hide-block-overlays)))
5952 (error "Not looking at a source block"))))
5954 ;; org-tab-after-check-for-cycling-hook
5955 (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
5956 ;; Remove overlays when changing major mode
5957 (add-hook 'org-mode-hook
5958 (lambda () (org-add-hook 'change-major-mode-hook
5959 'org-show-block-all 'append 'local)))
5961 ;;; Org-goto
5963 (defvar org-goto-window-configuration nil)
5964 (defvar org-goto-marker nil)
5965 (defvar org-goto-map
5966 (let ((map (make-sparse-keymap)))
5967 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
5968 (while (setq cmd (pop cmds))
5969 (substitute-key-definition cmd cmd map global-map)))
5970 (suppress-keymap map)
5971 (org-defkey map "\C-m" 'org-goto-ret)
5972 (org-defkey map [(return)] 'org-goto-ret)
5973 (org-defkey map [(left)] 'org-goto-left)
5974 (org-defkey map [(right)] 'org-goto-right)
5975 (org-defkey map [(control ?g)] 'org-goto-quit)
5976 (org-defkey map "\C-i" 'org-cycle)
5977 (org-defkey map [(tab)] 'org-cycle)
5978 (org-defkey map [(down)] 'outline-next-visible-heading)
5979 (org-defkey map [(up)] 'outline-previous-visible-heading)
5980 (if org-goto-auto-isearch
5981 (if (fboundp 'define-key-after)
5982 (define-key-after map [t] 'org-goto-local-auto-isearch)
5983 nil)
5984 (org-defkey map "q" 'org-goto-quit)
5985 (org-defkey map "n" 'outline-next-visible-heading)
5986 (org-defkey map "p" 'outline-previous-visible-heading)
5987 (org-defkey map "f" 'outline-forward-same-level)
5988 (org-defkey map "b" 'outline-backward-same-level)
5989 (org-defkey map "u" 'outline-up-heading))
5990 (org-defkey map "/" 'org-occur)
5991 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
5992 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
5993 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
5994 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
5995 (org-defkey map "\C-c\C-u" 'outline-up-heading)
5996 map))
5998 (defconst org-goto-help
5999 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
6000 RET=jump to location [Q]uit and return to previous location
6001 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6003 (defvar org-goto-start-pos) ; dynamically scoped parameter
6005 ;; FIXME: Docstring does not mention both interfaces
6006 (defun org-goto (&optional alternative-interface)
6007 "Look up a different location in the current file, keeping current visibility.
6009 When you want look-up or go to a different location in a document, the
6010 fastest way is often to fold the entire buffer and then dive into the tree.
6011 This method has the disadvantage, that the previous location will be folded,
6012 which may not be what you want.
6014 This command works around this by showing a copy of the current buffer
6015 in an indirect buffer, in overview mode. You can dive into the tree in
6016 that copy, use org-occur and incremental search to find a location.
6017 When pressing RET or `Q', the command returns to the original buffer in
6018 which the visibility is still unchanged. After RET is will also jump to
6019 the location selected in the indirect buffer and expose the
6020 the headline hierarchy above."
6021 (interactive "P")
6022 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6023 (org-refile-use-outline-path t)
6024 (org-refile-target-verify-function nil)
6025 (interface
6026 (if (not alternative-interface)
6027 org-goto-interface
6028 (if (eq org-goto-interface 'outline)
6029 'outline-path-completion
6030 'outline)))
6031 (org-goto-start-pos (point))
6032 (selected-point
6033 (if (eq interface 'outline)
6034 (car (org-get-location (current-buffer) org-goto-help))
6035 (nth 3 (org-refile-get-location "Goto: ")))))
6036 (if selected-point
6037 (progn
6038 (org-mark-ring-push org-goto-start-pos)
6039 (goto-char selected-point)
6040 (if (or (org-invisible-p) (org-invisible-p2))
6041 (org-show-context 'org-goto)))
6042 (message "Quit"))))
6044 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6045 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6046 (defvar org-goto-local-auto-isearch-map) ; defined below
6048 (defun org-get-location (buf help)
6049 "Let the user select a location in the Org-mode buffer BUF.
6050 This function uses a recursive edit. It returns the selected position
6051 or nil."
6052 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6053 (isearch-hide-immediately nil)
6054 (isearch-search-fun-function
6055 (lambda () 'org-goto-local-search-headings))
6056 (org-goto-selected-point org-goto-exit-command)
6057 (pop-up-frames nil)
6058 (special-display-buffer-names nil)
6059 (special-display-regexps nil)
6060 (special-display-function nil))
6061 (save-excursion
6062 (save-window-excursion
6063 (delete-other-windows)
6064 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6065 (switch-to-buffer
6066 (condition-case nil
6067 (make-indirect-buffer (current-buffer) "*org-goto*")
6068 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6069 (with-output-to-temp-buffer "*Help*"
6070 (princ help))
6071 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
6072 (setq buffer-read-only nil)
6073 (let ((org-startup-truncated t)
6074 (org-startup-folded nil)
6075 (org-startup-align-all-tables nil))
6076 (org-mode)
6077 (org-overview))
6078 (setq buffer-read-only t)
6079 (if (and (boundp 'org-goto-start-pos)
6080 (integer-or-marker-p org-goto-start-pos))
6081 (let ((org-show-hierarchy-above t)
6082 (org-show-siblings t)
6083 (org-show-following-heading t))
6084 (goto-char org-goto-start-pos)
6085 (and (org-invisible-p) (org-show-context)))
6086 (goto-char (point-min)))
6087 (let (org-special-ctrl-a/e) (org-beginning-of-line))
6088 (message "Select location and press RET")
6089 (use-local-map org-goto-map)
6090 (recursive-edit)
6092 (kill-buffer "*org-goto*")
6093 (cons org-goto-selected-point org-goto-exit-command)))
6095 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6096 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6097 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6098 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6100 (defun org-goto-local-search-headings (string bound noerror)
6101 "Search and make sure that any matches are in headlines."
6102 (catch 'return
6103 (while (if isearch-forward
6104 (search-forward string bound noerror)
6105 (search-backward string bound noerror))
6106 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6107 (and (member :headline context)
6108 (not (member :tags context))))
6109 (throw 'return (point))))))
6111 (defun org-goto-local-auto-isearch ()
6112 "Start isearch."
6113 (interactive)
6114 (goto-char (point-min))
6115 (let ((keys (this-command-keys)))
6116 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6117 (isearch-mode t)
6118 (isearch-process-search-char (string-to-char keys)))))
6120 (defun org-goto-ret (&optional arg)
6121 "Finish `org-goto' by going to the new location."
6122 (interactive "P")
6123 (setq org-goto-selected-point (point)
6124 org-goto-exit-command 'return)
6125 (throw 'exit nil))
6127 (defun org-goto-left ()
6128 "Finish `org-goto' by going to the new location."
6129 (interactive)
6130 (if (org-on-heading-p)
6131 (progn
6132 (beginning-of-line 1)
6133 (setq org-goto-selected-point (point)
6134 org-goto-exit-command 'left)
6135 (throw 'exit nil))
6136 (error "Not on a heading")))
6138 (defun org-goto-right ()
6139 "Finish `org-goto' by going to the new location."
6140 (interactive)
6141 (if (org-on-heading-p)
6142 (progn
6143 (setq org-goto-selected-point (point)
6144 org-goto-exit-command 'right)
6145 (throw 'exit nil))
6146 (error "Not on a heading")))
6148 (defun org-goto-quit ()
6149 "Finish `org-goto' without cursor motion."
6150 (interactive)
6151 (setq org-goto-selected-point nil)
6152 (setq org-goto-exit-command 'quit)
6153 (throw 'exit nil))
6155 ;;; Indirect buffer display of subtrees
6157 (defvar org-indirect-dedicated-frame nil
6158 "This is the frame being used for indirect tree display.")
6159 (defvar org-last-indirect-buffer nil)
6161 (defun org-tree-to-indirect-buffer (&optional arg)
6162 "Create indirect buffer and narrow it to current subtree.
6163 With numerical prefix ARG, go up to this level and then take that tree.
6164 If ARG is negative, go up that many levels.
6165 If `org-indirect-buffer-display' is not `new-frame', the command removes the
6166 indirect buffer previously made with this command, to avoid proliferation of
6167 indirect buffers. However, when you call the command with a `C-u' prefix, or
6168 when `org-indirect-buffer-display' is `new-frame', the last buffer
6169 is kept so that you can work with several indirect buffers at the same time.
6170 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
6171 requests that a new frame be made for the new buffer, so that the dedicated
6172 frame is not changed."
6173 (interactive "P")
6174 (let ((cbuf (current-buffer))
6175 (cwin (selected-window))
6176 (pos (point))
6177 beg end level heading ibuf)
6178 (save-excursion
6179 (org-back-to-heading t)
6180 (when (numberp arg)
6181 (setq level (org-outline-level))
6182 (if (< arg 0) (setq arg (+ level arg)))
6183 (while (> (setq level (org-outline-level)) arg)
6184 (outline-up-heading 1 t)))
6185 (setq beg (point)
6186 heading (org-get-heading))
6187 (org-end-of-subtree t t)
6188 (if (org-on-heading-p) (backward-char 1))
6189 (setq end (point)))
6190 (if (and (buffer-live-p org-last-indirect-buffer)
6191 (not (eq org-indirect-buffer-display 'new-frame))
6192 (not arg))
6193 (kill-buffer org-last-indirect-buffer))
6194 (setq ibuf (org-get-indirect-buffer cbuf)
6195 org-last-indirect-buffer ibuf)
6196 (cond
6197 ((or (eq org-indirect-buffer-display 'new-frame)
6198 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
6199 (select-frame (make-frame))
6200 (delete-other-windows)
6201 (switch-to-buffer ibuf)
6202 (org-set-frame-title heading))
6203 ((eq org-indirect-buffer-display 'dedicated-frame)
6204 (raise-frame
6205 (select-frame (or (and org-indirect-dedicated-frame
6206 (frame-live-p org-indirect-dedicated-frame)
6207 org-indirect-dedicated-frame)
6208 (setq org-indirect-dedicated-frame (make-frame)))))
6209 (delete-other-windows)
6210 (switch-to-buffer ibuf)
6211 (org-set-frame-title (concat "Indirect: " heading)))
6212 ((eq org-indirect-buffer-display 'current-window)
6213 (switch-to-buffer ibuf))
6214 ((eq org-indirect-buffer-display 'other-window)
6215 (pop-to-buffer ibuf))
6216 (t (error "Invalid value")))
6217 (if (featurep 'xemacs)
6218 (save-excursion (org-mode) (turn-on-font-lock)))
6219 (narrow-to-region beg end)
6220 (show-all)
6221 (goto-char pos)
6222 (and (window-live-p cwin) (select-window cwin))))
6224 (defun org-get-indirect-buffer (&optional buffer)
6225 (setq buffer (or buffer (current-buffer)))
6226 (let ((n 1) (base (buffer-name buffer)) bname)
6227 (while (buffer-live-p
6228 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
6229 (setq n (1+ n)))
6230 (condition-case nil
6231 (make-indirect-buffer buffer bname 'clone)
6232 (error (make-indirect-buffer buffer bname)))))
6234 (defun org-set-frame-title (title)
6235 "Set the title of the current frame to the string TITLE."
6236 ;; FIXME: how to name a single frame in XEmacs???
6237 (unless (featurep 'xemacs)
6238 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
6240 ;;;; Structure editing
6242 ;;; Inserting headlines
6244 (defun org-previous-line-empty-p ()
6245 (save-excursion
6246 (and (not (bobp))
6247 (or (beginning-of-line 0) t)
6248 (save-match-data
6249 (looking-at "[ \t]*$")))))
6251 (defun org-insert-heading (&optional force-heading invisible-ok)
6252 "Insert a new heading or item with same depth at point.
6253 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
6254 If point is at the beginning of a headline, insert a sibling before the
6255 current headline. If point is not at the beginning, do not split the line,
6256 but create the new headline after the current line.
6257 When INVISIBLE-OK is set, stop at invisible headlines when going back.
6258 This is important for non-interactive uses of the command."
6259 (interactive "P")
6260 (if (or (= (buffer-size) 0)
6261 (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
6262 (org-on-heading-p))))
6263 (not (org-in-item-p))))
6264 (insert "\n* ")
6265 (when (or force-heading (not (org-insert-item)))
6266 (let* ((empty-line-p nil)
6267 (head (save-excursion
6268 (condition-case nil
6269 (progn
6270 (org-back-to-heading invisible-ok)
6271 (setq empty-line-p (org-previous-line-empty-p))
6272 (match-string 0))
6273 (error "*"))))
6274 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
6275 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
6276 pos hide-previous previous-pos)
6277 (cond
6278 ((and (org-on-heading-p) (bolp)
6279 (or (bobp)
6280 (save-excursion (backward-char 1) (not (org-invisible-p)))))
6281 ;; insert before the current line
6282 (open-line (if blank 2 1)))
6283 ((and (bolp)
6284 (not org-insert-heading-respect-content)
6285 (or (bobp)
6286 (save-excursion
6287 (backward-char 1) (not (org-invisible-p)))))
6288 ;; insert right here
6289 nil)
6291 ;; somewhere in the line
6292 (save-excursion
6293 (setq previous-pos (point-at-bol))
6294 (end-of-line)
6295 (setq hide-previous (org-invisible-p)))
6296 (and org-insert-heading-respect-content (org-show-subtree))
6297 (let ((split
6298 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
6299 (save-excursion
6300 (let ((p (point)))
6301 (goto-char (point-at-bol))
6302 (and (looking-at org-complex-heading-regexp)
6303 (> p (match-beginning 4)))))))
6304 tags pos)
6305 (cond
6306 (org-insert-heading-respect-content
6307 (org-end-of-subtree nil t)
6308 (or (bolp) (newline))
6309 (or (org-previous-line-empty-p)
6310 (and blank (newline)))
6311 (open-line 1))
6312 ((org-on-heading-p)
6313 (when hide-previous
6314 (show-children)
6315 (org-show-entry))
6316 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
6317 (setq tags (and (match-end 2) (match-string 2)))
6318 (and (match-end 1)
6319 (delete-region (match-beginning 1) (match-end 1)))
6320 (setq pos (point-at-bol))
6321 (or split (end-of-line 1))
6322 (delete-horizontal-space)
6323 (if (string-match "\\`\\*+\\'"
6324 (buffer-substring (point-at-bol) (point)))
6325 (insert " "))
6326 (newline (if blank 2 1))
6327 (when tags
6328 (save-excursion
6329 (goto-char pos)
6330 (end-of-line 1)
6331 (insert " " tags)
6332 (org-set-tags nil 'align))))
6334 (or split (end-of-line 1))
6335 (newline (if blank 2 1)))))))
6336 (insert head) (just-one-space)
6337 (setq pos (point))
6338 (end-of-line 1)
6339 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
6340 (when (and org-insert-heading-respect-content hide-previous)
6341 (save-excursion
6342 (goto-char previous-pos)
6343 (hide-subtree)))
6344 (run-hooks 'org-insert-heading-hook)))))
6346 (defun org-get-heading (&optional no-tags)
6347 "Return the heading of the current entry, without the stars."
6348 (save-excursion
6349 (org-back-to-heading t)
6350 (if (looking-at
6351 (if no-tags
6352 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
6353 "\\*+[ \t]+\\([^\r\n]*\\)"))
6354 (match-string 1) "")))
6356 (defun org-heading-components ()
6357 "Return the components of the current heading.
6358 This is a list with the following elements:
6359 - the level as an integer
6360 - the reduced level, different if `org-odd-levels-only' is set.
6361 - the TODO keyword, or nil
6362 - the priority character, like ?A, or nil if no priority is given
6363 - the headline text itself, or the tags string if no headline text
6364 - the tags string, or nil."
6365 (save-excursion
6366 (org-back-to-heading t)
6367 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
6368 (list (length (match-string 1))
6369 (org-reduced-level (length (match-string 1)))
6370 (org-match-string-no-properties 2)
6371 (and (match-end 3) (aref (match-string 3) 2))
6372 (org-match-string-no-properties 4)
6373 (org-match-string-no-properties 5)))))
6375 (defun org-get-entry ()
6376 "Get the entry text, after heading, entire subtree."
6377 (save-excursion
6378 (org-back-to-heading t)
6379 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
6381 (defun org-insert-heading-after-current ()
6382 "Insert a new heading with same level as current, after current subtree."
6383 (interactive)
6384 (org-back-to-heading)
6385 (org-insert-heading)
6386 (org-move-subtree-down)
6387 (end-of-line 1))
6389 (defun org-insert-heading-respect-content ()
6390 (interactive)
6391 (let ((org-insert-heading-respect-content t))
6392 (org-insert-heading t)))
6394 (defun org-insert-todo-heading-respect-content (&optional force-state)
6395 (interactive "P")
6396 (let ((org-insert-heading-respect-content t))
6397 (org-insert-todo-heading force-state t)))
6399 (defun org-insert-todo-heading (arg &optional force-heading)
6400 "Insert a new heading with the same level and TODO state as current heading.
6401 If the heading has no TODO state, or if the state is DONE, use the first
6402 state (TODO by default). Also with prefix arg, force first state."
6403 (interactive "P")
6404 (when (or force-heading (not (org-insert-item 'checkbox)))
6405 (org-insert-heading force-heading)
6406 (save-excursion
6407 (org-back-to-heading)
6408 (outline-previous-heading)
6409 (looking-at org-todo-line-regexp))
6410 (let*
6411 ((new-mark-x
6412 (if (or arg
6413 (not (match-beginning 2))
6414 (member (match-string 2) org-done-keywords))
6415 (car org-todo-keywords-1)
6416 (match-string 2)))
6417 (new-mark
6419 (run-hook-with-args-until-success
6420 'org-todo-get-default-hook new-mark-x nil)
6421 new-mark-x)))
6422 (beginning-of-line 1)
6423 (and (looking-at "\\*+ ") (goto-char (match-end 0))
6424 (if org-treat-insert-todo-heading-as-state-change
6425 (org-todo new-mark)
6426 (insert new-mark " "))))
6427 (when org-provide-todo-statistics
6428 (org-update-parent-todo-statistics))))
6430 (defun org-insert-subheading (arg)
6431 "Insert a new subheading and demote it.
6432 Works for outline headings and for plain lists alike."
6433 (interactive "P")
6434 (org-insert-heading arg)
6435 (cond
6436 ((org-on-heading-p) (org-do-demote))
6437 ((org-at-item-p) (org-indent-item 1))))
6439 (defun org-insert-todo-subheading (arg)
6440 "Insert a new subheading with TODO keyword or checkbox and demote it.
6441 Works for outline headings and for plain lists alike."
6442 (interactive "P")
6443 (org-insert-todo-heading arg)
6444 (cond
6445 ((org-on-heading-p) (org-do-demote))
6446 ((org-at-item-p) (org-indent-item 1))))
6448 ;;; Promotion and Demotion
6450 (defvar org-after-demote-entry-hook nil
6451 "Hook run after an entry has been demoted.
6452 The cursor will be at the beginning of the entry.
6453 When a subtree is being demoted, the hook will be called for each node.")
6455 (defvar org-after-promote-entry-hook nil
6456 "Hook run after an entry has been promoted.
6457 The cursor will be at the beginning of the entry.
6458 When a subtree is being promoted, the hook will be called for each node.")
6460 (defun org-promote-subtree ()
6461 "Promote the entire subtree.
6462 See also `org-promote'."
6463 (interactive)
6464 (save-excursion
6465 (org-map-tree 'org-promote))
6466 (org-fix-position-after-promote))
6468 (defun org-demote-subtree ()
6469 "Demote the entire subtree. See `org-demote'.
6470 See also `org-promote'."
6471 (interactive)
6472 (save-excursion
6473 (org-map-tree 'org-demote))
6474 (org-fix-position-after-promote))
6477 (defun org-do-promote ()
6478 "Promote the current heading higher up the tree.
6479 If the region is active in `transient-mark-mode', promote all headings
6480 in the region."
6481 (interactive)
6482 (save-excursion
6483 (if (org-region-active-p)
6484 (org-map-region 'org-promote (region-beginning) (region-end))
6485 (org-promote)))
6486 (org-fix-position-after-promote))
6488 (defun org-do-demote ()
6489 "Demote the current heading lower down the tree.
6490 If the region is active in `transient-mark-mode', demote all headings
6491 in the region."
6492 (interactive)
6493 (save-excursion
6494 (if (org-region-active-p)
6495 (org-map-region 'org-demote (region-beginning) (region-end))
6496 (org-demote)))
6497 (org-fix-position-after-promote))
6499 (defun org-fix-position-after-promote ()
6500 "Make sure that after pro/demotion cursor position is right."
6501 (let ((pos (point)))
6502 (when (save-excursion
6503 (beginning-of-line 1)
6504 (looking-at org-todo-line-regexp)
6505 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
6506 (cond ((eobp) (insert " "))
6507 ((eolp) (insert " "))
6508 ((equal (char-after) ?\ ) (forward-char 1))))))
6510 (defun org-current-level ()
6511 "Return the level of the current entry, or nil if before the first headline.
6512 The level is the number of stars at the beginning of the headline."
6513 (save-excursion
6514 (condition-case nil
6515 (progn
6516 (org-back-to-heading t)
6517 (funcall outline-level))
6518 (error nil))))
6520 (defun org-get-previous-line-level ()
6521 "Return the outline depth of the last headline before the current line.
6522 Returns 0 for the first headline in the buffer, and nil if before the
6523 first headline."
6524 (let ((current-level (org-current-level))
6525 (prev-level (when (> (line-number-at-pos) 1)
6526 (save-excursion
6527 (beginning-of-line 0)
6528 (org-current-level)))))
6529 (cond ((null current-level) nil) ; Before first headline
6530 ((null prev-level) 0) ; At first headline
6531 (prev-level))))
6533 (defun org-reduced-level (l)
6534 "Compute the effective level of a heading.
6535 This takes into account the setting of `org-odd-levels-only'."
6536 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
6538 (defun org-level-increment ()
6539 "Return the number of stars that will be added or removed at a
6540 time to headlines when structure editing, based on the value of
6541 `org-odd-levels-only'."
6542 (if org-odd-levels-only 2 1))
6544 (defun org-get-valid-level (level &optional change)
6545 "Rectify a level change under the influence of `org-odd-levels-only'
6546 LEVEL is a current level, CHANGE is by how much the level should be
6547 modified. Even if CHANGE is nil, LEVEL may be returned modified because
6548 even level numbers will become the next higher odd number."
6549 (if org-odd-levels-only
6550 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
6551 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
6552 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
6553 (max 1 (+ level (or change 0)))))
6555 (if (boundp 'define-obsolete-function-alias)
6556 (if (or (featurep 'xemacs) (< emacs-major-version 23))
6557 (define-obsolete-function-alias 'org-get-legal-level
6558 'org-get-valid-level)
6559 (define-obsolete-function-alias 'org-get-legal-level
6560 'org-get-valid-level "23.1")))
6562 (defun org-promote ()
6563 "Promote the current heading higher up the tree.
6564 If the region is active in `transient-mark-mode', promote all headings
6565 in the region."
6566 (org-back-to-heading t)
6567 (let* ((level (save-match-data (funcall outline-level)))
6568 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
6569 (diff (abs (- level (length up-head) -1))))
6570 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
6571 (replace-match up-head nil t)
6572 ;; Fixup tag positioning
6573 (and org-auto-align-tags (org-set-tags nil t))
6574 (if org-adapt-indentation (org-fixup-indentation (- diff)))
6575 (run-hooks 'org-after-promote-entry-hook)))
6577 (defun org-demote ()
6578 "Demote the current heading lower down the tree.
6579 If the region is active in `transient-mark-mode', demote all headings
6580 in the region."
6581 (org-back-to-heading t)
6582 (let* ((level (save-match-data (funcall outline-level)))
6583 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
6584 (diff (abs (- level (length down-head) -1))))
6585 (replace-match down-head nil t)
6586 ;; Fixup tag positioning
6587 (and org-auto-align-tags (org-set-tags nil t))
6588 (if org-adapt-indentation (org-fixup-indentation diff))
6589 (run-hooks 'org-after-demote-entry-hook)))
6591 (defun org-cycle-level ()
6592 "Cycle the level of an empty headline through possible states.
6593 This goes first to child, then to parent, level, then up the hierarchy.
6594 After top level, it switches back to sibling level."
6595 (interactive)
6596 (let ((org-adapt-indentation nil))
6597 (when (org-point-at-end-of-empty-headline)
6598 (setq this-command 'org-cycle-level) ; Only needed for caching
6599 (let ((cur-level (org-current-level))
6600 (prev-level (org-get-previous-line-level)))
6601 (cond
6602 ;; If first headline in file, promote to top-level.
6603 ((= prev-level 0)
6604 (loop repeat (/ (- cur-level 1) (org-level-increment))
6605 do (org-do-promote)))
6606 ;; If same level as prev, demote one.
6607 ((= prev-level cur-level)
6608 (org-do-demote))
6609 ;; If parent is top-level, promote to top level if not already.
6610 ((= prev-level 1)
6611 (loop repeat (/ (- cur-level 1) (org-level-increment))
6612 do (org-do-promote)))
6613 ;; If top-level, return to prev-level.
6614 ((= cur-level 1)
6615 (loop repeat (/ (- prev-level 1) (org-level-increment))
6616 do (org-do-demote)))
6617 ;; If less than prev-level, promote one.
6618 ((< cur-level prev-level)
6619 (org-do-promote))
6620 ;; If deeper than prev-level, promote until higher than
6621 ;; prev-level.
6622 ((> cur-level prev-level)
6623 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
6624 do (org-do-promote))))
6625 t))))
6627 (defun org-map-tree (fun)
6628 "Call FUN for every heading underneath the current one."
6629 (org-back-to-heading)
6630 (let ((level (funcall outline-level)))
6631 (save-excursion
6632 (funcall fun)
6633 (while (and (progn
6634 (outline-next-heading)
6635 (> (funcall outline-level) level))
6636 (not (eobp)))
6637 (funcall fun)))))
6639 (defun org-map-region (fun beg end)
6640 "Call FUN for every heading between BEG and END."
6641 (let ((org-ignore-region t))
6642 (save-excursion
6643 (setq end (copy-marker end))
6644 (goto-char beg)
6645 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
6646 (< (point) end))
6647 (funcall fun))
6648 (while (and (progn
6649 (outline-next-heading)
6650 (< (point) end))
6651 (not (eobp)))
6652 (funcall fun)))))
6654 (defun org-fixup-indentation (diff)
6655 "Change the indentation in the current entry by DIFF
6656 However, if any line in the current entry has no indentation, or if it
6657 would end up with no indentation after the change, nothing at all is done."
6658 (save-excursion
6659 (let ((end (save-excursion (outline-next-heading)
6660 (point-marker)))
6661 (prohibit (if (> diff 0)
6662 "^\\S-"
6663 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
6664 col)
6665 (unless (save-excursion (end-of-line 1)
6666 (re-search-forward prohibit end t))
6667 (while (and (< (point) end)
6668 (re-search-forward "^[ \t]+" end t))
6669 (goto-char (match-end 0))
6670 (setq col (current-column))
6671 (if (< diff 0) (replace-match ""))
6672 (org-indent-to-column (+ diff col))))
6673 (move-marker end nil))))
6675 (defun org-convert-to-odd-levels ()
6676 "Convert an org-mode file with all levels allowed to one with odd levels.
6677 This will leave level 1 alone, convert level 2 to level 3, level 3 to
6678 level 5 etc."
6679 (interactive)
6680 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
6681 (let ((outline-regexp org-outline-regexp)
6682 (outline-level 'org-outline-level)
6683 (org-odd-levels-only nil) n)
6684 (save-excursion
6685 (goto-char (point-min))
6686 (while (re-search-forward "^\\*\\*+ " nil t)
6687 (setq n (- (length (match-string 0)) 2))
6688 (while (>= (setq n (1- n)) 0)
6689 (org-demote))
6690 (end-of-line 1))))))
6692 (defun org-convert-to-oddeven-levels ()
6693 "Convert an org-mode file with only odd levels to one with odd and even levels.
6694 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
6695 section with an even level, conversion would destroy the structure of the file. An error
6696 is signaled in this case."
6697 (interactive)
6698 (goto-char (point-min))
6699 ;; First check if there are no even levels
6700 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
6701 (org-show-context t)
6702 (error "Not all levels are odd in this file. Conversion not possible"))
6703 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
6704 (let ((outline-regexp org-outline-regexp)
6705 (outline-level 'org-outline-level)
6706 (org-odd-levels-only nil) n)
6707 (save-excursion
6708 (goto-char (point-min))
6709 (while (re-search-forward "^\\*\\*+ " nil t)
6710 (setq n (/ (1- (length (match-string 0))) 2))
6711 (while (>= (setq n (1- n)) 0)
6712 (org-promote))
6713 (end-of-line 1))))))
6715 (defun org-tr-level (n)
6716 "Make N odd if required."
6717 (if org-odd-levels-only (1+ (/ n 2)) n))
6719 ;;; Vertical tree motion, cutting and pasting of subtrees
6721 (defun org-move-subtree-up (&optional arg)
6722 "Move the current subtree up past ARG headlines of the same level."
6723 (interactive "p")
6724 (org-move-subtree-down (- (prefix-numeric-value arg))))
6726 (defun org-move-subtree-down (&optional arg)
6727 "Move the current subtree down past ARG headlines of the same level."
6728 (interactive "p")
6729 (setq arg (prefix-numeric-value arg))
6730 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
6731 'org-get-last-sibling))
6732 (ins-point (make-marker))
6733 (cnt (abs arg))
6734 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
6735 ;; Select the tree
6736 (org-back-to-heading)
6737 (setq beg0 (point))
6738 (save-excursion
6739 (setq ne-beg (org-back-over-empty-lines))
6740 (setq beg (point)))
6741 (save-match-data
6742 (save-excursion (outline-end-of-heading)
6743 (setq folded (org-invisible-p)))
6744 (outline-end-of-subtree))
6745 (outline-next-heading)
6746 (setq ne-end (org-back-over-empty-lines))
6747 (setq end (point))
6748 (goto-char beg0)
6749 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
6750 ;; include less whitespace
6751 (save-excursion
6752 (goto-char beg)
6753 (forward-line (- ne-beg ne-end))
6754 (setq beg (point))))
6755 ;; Find insertion point, with error handling
6756 (while (> cnt 0)
6757 (or (and (funcall movfunc) (looking-at outline-regexp))
6758 (progn (goto-char beg0)
6759 (error "Cannot move past superior level or buffer limit")))
6760 (setq cnt (1- cnt)))
6761 (if (> arg 0)
6762 ;; Moving forward - still need to move over subtree
6763 (progn (org-end-of-subtree t t)
6764 (save-excursion
6765 (org-back-over-empty-lines)
6766 (or (bolp) (newline)))))
6767 (setq ne-ins (org-back-over-empty-lines))
6768 (move-marker ins-point (point))
6769 (setq txt (buffer-substring beg end))
6770 (org-save-markers-in-region beg end)
6771 (delete-region beg end)
6772 (org-remove-empty-overlays-at beg)
6773 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
6774 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
6775 (and (not (bolp)) (looking-at "\n") (forward-char 1))
6776 (let ((bbb (point)))
6777 (insert-before-markers txt)
6778 (org-reinstall-markers-in-region bbb)
6779 (move-marker ins-point bbb))
6780 (or (bolp) (insert "\n"))
6781 (setq ins-end (point))
6782 (goto-char ins-point)
6783 (org-skip-whitespace)
6784 (when (and (< arg 0)
6785 (org-first-sibling-p)
6786 (> ne-ins ne-beg))
6787 ;; Move whitespace back to beginning
6788 (save-excursion
6789 (goto-char ins-end)
6790 (let ((kill-whole-line t))
6791 (kill-line (- ne-ins ne-beg)) (point)))
6792 (insert (make-string (- ne-ins ne-beg) ?\n)))
6793 (move-marker ins-point nil)
6794 (if folded
6795 (hide-subtree)
6796 (org-show-entry)
6797 (show-children)
6798 (org-cycle-hide-drawers 'children))
6799 (org-clean-visibility-after-subtree-move)))
6801 (defvar org-subtree-clip ""
6802 "Clipboard for cut and paste of subtrees.
6803 This is actually only a copy of the kill, because we use the normal kill
6804 ring. We need it to check if the kill was created by `org-copy-subtree'.")
6806 (defvar org-subtree-clip-folded nil
6807 "Was the last copied subtree folded?
6808 This is used to fold the tree back after pasting.")
6810 (defun org-cut-subtree (&optional n)
6811 "Cut the current subtree into the clipboard.
6812 With prefix arg N, cut this many sequential subtrees.
6813 This is a short-hand for marking the subtree and then cutting it."
6814 (interactive "p")
6815 (org-copy-subtree n 'cut))
6817 (defun org-copy-subtree (&optional n cut force-store-markers)
6818 "Cut the current subtree into the clipboard.
6819 With prefix arg N, cut this many sequential subtrees.
6820 This is a short-hand for marking the subtree and then copying it.
6821 If CUT is non-nil, actually cut the subtree.
6822 If FORCE-STORE-MARKERS is non-nil, store the relative locations
6823 of some markers in the region, even if CUT is non-nil. This is
6824 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
6825 (interactive "p")
6826 (let (beg end folded (beg0 (point)))
6827 (if (interactive-p)
6828 (org-back-to-heading nil) ; take what looks like a subtree
6829 (org-back-to-heading t)) ; take what is really there
6830 (org-back-over-empty-lines)
6831 (setq beg (point))
6832 (skip-chars-forward " \t\r\n")
6833 (save-match-data
6834 (save-excursion (outline-end-of-heading)
6835 (setq folded (org-invisible-p)))
6836 (condition-case nil
6837 (org-forward-same-level (1- n) t)
6838 (error nil))
6839 (org-end-of-subtree t t))
6840 (org-back-over-empty-lines)
6841 (setq end (point))
6842 (goto-char beg0)
6843 (when (> end beg)
6844 (setq org-subtree-clip-folded folded)
6845 (when (or cut force-store-markers)
6846 (org-save-markers-in-region beg end))
6847 (if cut (kill-region beg end) (copy-region-as-kill beg end))
6848 (setq org-subtree-clip (current-kill 0))
6849 (message "%s: Subtree(s) with %d characters"
6850 (if cut "Cut" "Copied")
6851 (length org-subtree-clip)))))
6853 (defun org-paste-subtree (&optional level tree for-yank)
6854 "Paste the clipboard as a subtree, with modification of headline level.
6855 The entire subtree is promoted or demoted in order to match a new headline
6856 level.
6858 If the cursor is at the beginning of a headline, the same level as
6859 that headline is used to paste the tree
6861 If not, the new level is derived from the *visible* headings
6862 before and after the insertion point, and taken to be the inferior headline
6863 level of the two. So if the previous visible heading is level 3 and the
6864 next is level 4 (or vice versa), level 4 will be used for insertion.
6865 This makes sure that the subtree remains an independent subtree and does
6866 not swallow low level entries.
6868 You can also force a different level, either by using a numeric prefix
6869 argument, or by inserting the heading marker by hand. For example, if the
6870 cursor is after \"*****\", then the tree will be shifted to level 5.
6872 If optional TREE is given, use this text instead of the kill ring.
6874 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
6875 move back over whitespace before inserting, and move point to the end of
6876 the inserted text when done."
6877 (interactive "P")
6878 (setq tree (or tree (and kill-ring (current-kill 0))))
6879 (unless (org-kill-is-subtree-p tree)
6880 (error "%s"
6881 (substitute-command-keys
6882 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
6883 (let* ((visp (not (org-invisible-p)))
6884 (txt tree)
6885 (^re (concat "^\\(" outline-regexp "\\)"))
6886 (re (concat "\\(" outline-regexp "\\)"))
6887 (^re_ (concat "\\(\\*+\\)[ \t]*"))
6889 (old-level (if (string-match ^re txt)
6890 (- (match-end 0) (match-beginning 0) 1)
6891 -1))
6892 (force-level (cond (level (prefix-numeric-value level))
6893 ((and (looking-at "[ \t]*$")
6894 (string-match
6895 ^re_ (buffer-substring
6896 (point-at-bol) (point))))
6897 (- (match-end 1) (match-beginning 1)))
6898 ((and (bolp)
6899 (looking-at org-outline-regexp))
6900 (- (match-end 0) (point) 1))
6901 (t nil)))
6902 (previous-level (save-excursion
6903 (condition-case nil
6904 (progn
6905 (outline-previous-visible-heading 1)
6906 (if (looking-at re)
6907 (- (match-end 0) (match-beginning 0) 1)
6909 (error 1))))
6910 (next-level (save-excursion
6911 (condition-case nil
6912 (progn
6913 (or (looking-at outline-regexp)
6914 (outline-next-visible-heading 1))
6915 (if (looking-at re)
6916 (- (match-end 0) (match-beginning 0) 1)
6918 (error 1))))
6919 (new-level (or force-level (max previous-level next-level)))
6920 (shift (if (or (= old-level -1)
6921 (= new-level -1)
6922 (= old-level new-level))
6924 (- new-level old-level)))
6925 (delta (if (> shift 0) -1 1))
6926 (func (if (> shift 0) 'org-demote 'org-promote))
6927 (org-odd-levels-only nil)
6928 beg end newend)
6929 ;; Remove the forced level indicator
6930 (if force-level
6931 (delete-region (point-at-bol) (point)))
6932 ;; Paste
6933 (beginning-of-line 1)
6934 (unless for-yank (org-back-over-empty-lines))
6935 (setq beg (point))
6936 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
6937 (insert-before-markers txt)
6938 (unless (string-match "\n\\'" txt) (insert "\n"))
6939 (setq newend (point))
6940 (org-reinstall-markers-in-region beg)
6941 (setq end (point))
6942 (goto-char beg)
6943 (skip-chars-forward " \t\n\r")
6944 (setq beg (point))
6945 (if (and (org-invisible-p) visp)
6946 (save-excursion (outline-show-heading)))
6947 ;; Shift if necessary
6948 (unless (= shift 0)
6949 (save-restriction
6950 (narrow-to-region beg end)
6951 (while (not (= shift 0))
6952 (org-map-region func (point-min) (point-max))
6953 (setq shift (+ delta shift)))
6954 (goto-char (point-min))
6955 (setq newend (point-max))))
6956 (when (or (interactive-p) for-yank)
6957 (message "Clipboard pasted as level %d subtree" new-level))
6958 (if (and (not for-yank) ; in this case, org-yank will decide about folding
6959 kill-ring
6960 (eq org-subtree-clip (current-kill 0))
6961 org-subtree-clip-folded)
6962 ;; The tree was folded before it was killed/copied
6963 (hide-subtree))
6964 (and for-yank (goto-char newend))))
6966 (defun org-kill-is-subtree-p (&optional txt)
6967 "Check if the current kill is an outline subtree, or a set of trees.
6968 Returns nil if kill does not start with a headline, or if the first
6969 headline level is not the largest headline level in the tree.
6970 So this will actually accept several entries of equal levels as well,
6971 which is OK for `org-paste-subtree'.
6972 If optional TXT is given, check this string instead of the current kill."
6973 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
6974 (start-level (and kill
6975 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
6976 org-outline-regexp "\\)")
6977 kill)
6978 (- (match-end 2) (match-beginning 2) 1)))
6979 (re (concat "^" org-outline-regexp))
6980 (start (1+ (or (match-beginning 2) -1))))
6981 (if (not start-level)
6982 (progn
6983 nil) ;; does not even start with a heading
6984 (catch 'exit
6985 (while (setq start (string-match re kill (1+ start)))
6986 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
6987 (throw 'exit nil)))
6988 t))))
6990 (defvar org-markers-to-move nil
6991 "Markers that should be moved with a cut-and-paste operation.
6992 Those markers are stored together with their positions relative to
6993 the start of the region.")
6995 (defun org-save-markers-in-region (beg end)
6996 "Check markers in region.
6997 If these markers are between BEG and END, record their position relative
6998 to BEG, so that after moving the block of text, we can put the markers back
6999 into place.
7000 This function gets called just before an entry or tree gets cut from the
7001 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
7002 called immediately, to move the markers with the entries."
7003 (setq org-markers-to-move nil)
7004 (when (featurep 'org-clock)
7005 (org-clock-save-markers-for-cut-and-paste beg end))
7006 (when (featurep 'org-agenda)
7007 (org-agenda-save-markers-for-cut-and-paste beg end)))
7009 (defun org-check-and-save-marker (marker beg end)
7010 "Check if MARKER is between BEG and END.
7011 If yes, remember the marker and the distance to BEG."
7012 (when (and (marker-buffer marker)
7013 (equal (marker-buffer marker) (current-buffer)))
7014 (if (and (>= marker beg) (< marker end))
7015 (push (cons marker (- marker beg)) org-markers-to-move))))
7017 (defun org-reinstall-markers-in-region (beg)
7018 "Move all remembered markers to their position relative to BEG."
7019 (mapc (lambda (x)
7020 (move-marker (car x) (+ beg (cdr x))))
7021 org-markers-to-move)
7022 (setq org-markers-to-move nil))
7024 (defun org-narrow-to-subtree ()
7025 "Narrow buffer to the current subtree."
7026 (interactive)
7027 (save-excursion
7028 (save-match-data
7029 (narrow-to-region
7030 (progn (org-back-to-heading t) (point))
7031 (progn (org-end-of-subtree t t)
7032 (if (org-on-heading-p) (backward-char 1))
7033 (point))))))
7035 (defun org-clone-subtree-with-time-shift (n &optional shift)
7036 "Clone the task (subtree) at point N times.
7037 The clones will be inserted as siblings.
7039 In interactive use, the user will be prompted for the number of clones
7040 to be produced, and for a time SHIFT, which may be a repeater as used
7041 in time stamps, for example `+3d'.
7043 When a valid repeater is given and the entry contains any time stamps,
7044 the clones will become a sequence in time, with time stamps in the
7045 subtree shifted for each clone produced. If SHIFT is nil or the
7046 empty string, time stamps will be left alone.
7048 If the original subtree did contain time stamps with a repeater,
7049 the following will happen:
7050 - the repeater will be removed in each clone
7051 - an additional clone will be produced, with the current, unshifted
7052 date(s) in the entry.
7053 - the original entry will be placed *after* all the clones, with
7054 repeater intact.
7055 - the start days in the repeater in the original entry will be shifted
7056 to past the last clone.
7057 I this way you can spell out a number of instances of a repeating task,
7058 and still retain the repeater to cover future instances of the task."
7059 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
7060 (let (beg end template task
7061 shift-n shift-what doshift nmin nmax (n-no-remove -1))
7062 (if (not (and (integerp n) (> n 0)))
7063 (error "Invalid number of replications %s" n))
7064 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
7065 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
7066 shift)))
7067 (error "Invalid shift specification %s" shift))
7068 (when doshift
7069 (setq shift-n (string-to-number (match-string 1 shift))
7070 shift-what (cdr (assoc (match-string 2 shift)
7071 '(("d" . day) ("w" . week)
7072 ("m" . month) ("y" . year))))))
7073 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
7074 (setq nmin 1 nmax n)
7075 (org-back-to-heading t)
7076 (setq beg (point))
7077 (org-end-of-subtree t t)
7078 (or (bolp) (insert "\n"))
7079 (setq end (point))
7080 (setq template (buffer-substring beg end))
7081 (when (and doshift
7082 (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
7083 (delete-region beg end)
7084 (setq end beg)
7085 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
7086 (goto-char end)
7087 (loop for n from nmin to nmax do
7088 (if (not doshift)
7089 (setq task template)
7090 (with-temp-buffer
7091 (insert template)
7092 (org-mode)
7093 (goto-char (point-min))
7094 (while (re-search-forward org-ts-regexp-both nil t)
7095 (org-timestamp-change (* n shift-n) shift-what))
7096 (unless (= n n-no-remove)
7097 (goto-char (point-min))
7098 (while (re-search-forward org-ts-regexp nil t)
7099 (save-excursion
7100 (goto-char (match-beginning 0))
7101 (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
7102 (delete-region (match-beginning 1) (match-end 1))))))
7103 (setq task (buffer-string))))
7104 (insert task))
7105 (goto-char beg)))
7107 ;;; Outline Sorting
7109 (defun org-sort (with-case)
7110 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
7111 Optional argument WITH-CASE means sort case-sensitively.
7112 With a double prefix argument, also remove duplicate entries."
7113 (interactive "P")
7114 (if (org-at-table-p)
7115 (org-call-with-arg 'org-table-sort-lines with-case)
7116 (org-call-with-arg 'org-sort-entries-or-items with-case)))
7118 (defun org-sort-remove-invisible (s)
7119 (remove-text-properties 0 (length s) org-rm-props s)
7120 (while (string-match org-bracket-link-regexp s)
7121 (setq s (replace-match (if (match-end 2)
7122 (match-string 3 s)
7123 (match-string 1 s)) t t s)))
7126 (defvar org-priority-regexp) ; defined later in the file
7128 (defvar org-after-sorting-entries-or-items-hook nil
7129 "Hook that is run after a bunch of entries or items have been sorted.
7130 When children are sorted, the cursor is in the parent line when this
7131 hook gets called. When a region or a plain list is sorted, the cursor
7132 will be in the first entry of the sorted region/list.")
7134 (defun org-sort-entries-or-items
7135 (&optional with-case sorting-type getkey-func compare-func property)
7136 "Sort entries on a certain level of an outline tree, or plain list items.
7137 If there is an active region, the entries in the region are sorted.
7138 Else, if the cursor is before the first entry, sort the top-level items.
7139 Else, the children of the entry at point are sorted.
7140 If the cursor is at the first item in a plain list, the list items will be
7141 sorted.
7143 Sorting can be alphabetically, numerically, by date/time as given by
7144 a time stamp, by a property or by priority.
7146 The command prompts for the sorting type unless it has been given to the
7147 function through the SORTING-TYPE argument, which needs to a character,
7148 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
7149 precise meaning of each character:
7151 n Numerically, by converting the beginning of the entry/item to a number.
7152 a Alphabetically, ignoring the TODO keyword and the priority, if any.
7153 t By date/time, either the first active time stamp in the entry, or, if
7154 none exist, by the first inactive one.
7155 In items, only the first line will be checked.
7156 s By the scheduled date/time.
7157 d By deadline date/time.
7158 c By creation time, which is assumed to be the first inactive time stamp
7159 at the beginning of a line.
7160 p By priority according to the cookie.
7161 r By the value of a property.
7163 Capital letters will reverse the sort order.
7165 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
7166 called with point at the beginning of the record. It must return either
7167 a string or a number that should serve as the sorting key for that record.
7169 Comparing entries ignores case by default. However, with an optional argument
7170 WITH-CASE, the sorting considers case as well."
7171 (interactive "P")
7172 (let ((case-func (if with-case 'identity 'downcase))
7173 start beg end stars re re2
7174 txt what tmp plain-list-p)
7175 ;; Find beginning and end of region to sort
7176 (cond
7177 ((org-region-active-p)
7178 ;; we will sort the region
7179 (setq end (region-end)
7180 what "region")
7181 (goto-char (region-beginning))
7182 (if (not (org-on-heading-p)) (outline-next-heading))
7183 (setq start (point)))
7184 ((org-at-item-p)
7185 ;; we will sort this plain list
7186 (org-beginning-of-item-list) (setq start (point))
7187 (org-end-of-item-list)
7188 (or (bolp) (insert "\n"))
7189 (setq end (point))
7190 (goto-char start)
7191 (setq plain-list-p t
7192 what "plain list"))
7193 ((or (org-on-heading-p)
7194 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
7195 ;; we will sort the children of the current headline
7196 (org-back-to-heading)
7197 (setq start (point)
7198 end (progn (org-end-of-subtree t t)
7199 (or (bolp) (insert "\n"))
7200 (org-back-over-empty-lines)
7201 (point))
7202 what "children")
7203 (goto-char start)
7204 (show-subtree)
7205 (outline-next-heading))
7207 ;; we will sort the top-level entries in this file
7208 (goto-char (point-min))
7209 (or (org-on-heading-p) (outline-next-heading))
7210 (setq start (point))
7211 (goto-char (point-max))
7212 (beginning-of-line 1)
7213 (when (looking-at ".*?\\S-")
7214 ;; File ends in a non-white line
7215 (end-of-line 1)
7216 (insert "\n"))
7217 (setq end (point-max))
7218 (setq what "top-level")
7219 (goto-char start)
7220 (show-all)))
7222 (setq beg (point))
7223 (if (>= beg end) (error "Nothing to sort"))
7225 (unless plain-list-p
7226 (looking-at "\\(\\*+\\)")
7227 (setq stars (match-string 1)
7228 re (concat "^" (regexp-quote stars) " +")
7229 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
7230 txt (buffer-substring beg end))
7231 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
7232 (if (and (not (equal stars "*")) (string-match re2 txt))
7233 (error "Region to sort contains a level above the first entry")))
7235 (unless sorting-type
7236 (message
7237 (if plain-list-p
7238 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
7239 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
7240 [t]ime [s]cheduled [d]eadline [c]reated
7241 A/N/T/S/D/C/P/O/F means reversed:")
7242 what)
7243 (setq sorting-type (read-char-exclusive))
7245 (and (= (downcase sorting-type) ?f)
7246 (setq getkey-func
7247 (org-icompleting-read "Sort using function: "
7248 obarray 'fboundp t nil nil))
7249 (setq getkey-func (intern getkey-func)))
7251 (and (= (downcase sorting-type) ?r)
7252 (setq property
7253 (org-icompleting-read "Property: "
7254 (mapcar 'list (org-buffer-property-keys t))
7255 nil t))))
7257 (message "Sorting entries...")
7259 (save-restriction
7260 (narrow-to-region start end)
7262 (let ((dcst (downcase sorting-type))
7263 (case-fold-search nil)
7264 (now (current-time)))
7265 (sort-subr
7266 (/= dcst sorting-type)
7267 ;; This function moves to the beginning character of the "record" to
7268 ;; be sorted.
7269 (if plain-list-p
7270 (lambda nil
7271 (if (org-at-item-p) t (goto-char (point-max))))
7272 (lambda nil
7273 (if (re-search-forward re nil t)
7274 (goto-char (match-beginning 0))
7275 (goto-char (point-max)))))
7276 ;; This function moves to the last character of the "record" being
7277 ;; sorted.
7278 (if plain-list-p
7279 'org-end-of-item
7280 (lambda nil
7281 (save-match-data
7282 (condition-case nil
7283 (outline-forward-same-level 1)
7284 (error
7285 (goto-char (point-max)))))))
7287 ;; This function returns the value that gets sorted against.
7288 (if plain-list-p
7289 (lambda nil
7290 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
7291 (cond
7292 ((= dcst ?n)
7293 (string-to-number (buffer-substring (match-end 0)
7294 (point-at-eol))))
7295 ((= dcst ?a)
7296 (buffer-substring (match-end 0) (point-at-eol)))
7297 ((= dcst ?t)
7298 (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
7299 (re-search-forward org-ts-regexp-both
7300 (point-at-eol) t))
7301 (org-time-string-to-seconds (match-string 0))
7302 (org-float-time now)))
7303 ((= dcst ?f)
7304 (if getkey-func
7305 (progn
7306 (setq tmp (funcall getkey-func))
7307 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
7308 tmp)
7309 (error "Invalid key function `%s'" getkey-func)))
7310 (t (error "Invalid sorting type `%c'" sorting-type)))))
7311 (lambda nil
7312 (cond
7313 ((= dcst ?n)
7314 (if (looking-at org-complex-heading-regexp)
7315 (string-to-number (match-string 4))
7316 nil))
7317 ((= dcst ?a)
7318 (if (looking-at org-complex-heading-regexp)
7319 (funcall case-func (match-string 4))
7320 nil))
7321 ((= dcst ?t)
7322 (let ((end (save-excursion (outline-next-heading) (point))))
7323 (if (or (re-search-forward org-ts-regexp end t)
7324 (re-search-forward org-ts-regexp-both end t))
7325 (org-time-string-to-seconds (match-string 0))
7326 (org-float-time now))))
7327 ((= dcst ?c)
7328 (let ((end (save-excursion (outline-next-heading) (point))))
7329 (if (re-search-forward
7330 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
7331 end t)
7332 (org-time-string-to-seconds (match-string 0))
7333 (org-float-time now))))
7334 ((= dcst ?s)
7335 (let ((end (save-excursion (outline-next-heading) (point))))
7336 (if (re-search-forward org-scheduled-time-regexp end t)
7337 (org-time-string-to-seconds (match-string 1))
7338 (org-float-time now))))
7339 ((= dcst ?d)
7340 (let ((end (save-excursion (outline-next-heading) (point))))
7341 (if (re-search-forward org-deadline-time-regexp end t)
7342 (org-time-string-to-seconds (match-string 1))
7343 (org-float-time now))))
7344 ((= dcst ?p)
7345 (if (re-search-forward org-priority-regexp (point-at-eol) t)
7346 (string-to-char (match-string 2))
7347 org-default-priority))
7348 ((= dcst ?r)
7349 (or (org-entry-get nil property) ""))
7350 ((= dcst ?o)
7351 (if (looking-at org-complex-heading-regexp)
7352 (- 9999 (length (member (match-string 2)
7353 org-todo-keywords-1)))))
7354 ((= dcst ?f)
7355 (if getkey-func
7356 (progn
7357 (setq tmp (funcall getkey-func))
7358 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
7359 tmp)
7360 (error "Invalid key function `%s'" getkey-func)))
7361 (t (error "Invalid sorting type `%c'" sorting-type)))))
7363 (cond
7364 ((= dcst ?a) 'string<)
7365 ((= dcst ?f) compare-func)
7366 ((member dcst '(?p ?t ?s ?d ?c)) '<)
7367 (t nil)))))
7368 (run-hooks 'org-after-sorting-entries-or-items-hook)
7369 (message "Sorting entries...done")))
7371 (defun org-do-sort (table what &optional with-case sorting-type)
7372 "Sort TABLE of WHAT according to SORTING-TYPE.
7373 The user will be prompted for the SORTING-TYPE if the call to this
7374 function does not specify it. WHAT is only for the prompt, to indicate
7375 what is being sorted. The sorting key will be extracted from
7376 the car of the elements of the table.
7377 If WITH-CASE is non-nil, the sorting will be case-sensitive."
7378 (unless sorting-type
7379 (message
7380 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
7381 what)
7382 (setq sorting-type (read-char-exclusive)))
7383 (let ((dcst (downcase sorting-type))
7384 extractfun comparefun)
7385 ;; Define the appropriate functions
7386 (cond
7387 ((= dcst ?n)
7388 (setq extractfun 'string-to-number
7389 comparefun (if (= dcst sorting-type) '< '>)))
7390 ((= dcst ?a)
7391 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
7392 (lambda(x) (downcase (org-sort-remove-invisible x))))
7393 comparefun (if (= dcst sorting-type)
7394 'string<
7395 (lambda (a b) (and (not (string< a b))
7396 (not (string= a b)))))))
7397 ((= dcst ?t)
7398 (setq extractfun
7399 (lambda (x)
7400 (if (or (string-match org-ts-regexp x)
7401 (string-match org-ts-regexp-both x))
7402 (org-float-time
7403 (org-time-string-to-time (match-string 0 x)))
7405 comparefun (if (= dcst sorting-type) '< '>)))
7406 (t (error "Invalid sorting type `%c'" sorting-type)))
7408 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
7409 table)
7410 (lambda (a b) (funcall comparefun (car a) (car b))))))
7413 ;;; The orgstruct minor mode
7415 ;; Define a minor mode which can be used in other modes in order to
7416 ;; integrate the org-mode structure editing commands.
7418 ;; This is really a hack, because the org-mode structure commands use
7419 ;; keys which normally belong to the major mode. Here is how it
7420 ;; works: The minor mode defines all the keys necessary to operate the
7421 ;; structure commands, but wraps the commands into a function which
7422 ;; tests if the cursor is currently at a headline or a plain list
7423 ;; item. If that is the case, the structure command is used,
7424 ;; temporarily setting many Org-mode variables like regular
7425 ;; expressions for filling etc. However, when any of those keys is
7426 ;; used at a different location, function uses `key-binding' to look
7427 ;; up if the key has an associated command in another currently active
7428 ;; keymap (minor modes, major mode, global), and executes that
7429 ;; command. There might be problems if any of the keys is otherwise
7430 ;; used as a prefix key.
7432 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
7433 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
7434 ;; addresses this by checking explicitly for both bindings.
7436 (defvar orgstruct-mode-map (make-sparse-keymap)
7437 "Keymap for the minor `orgstruct-mode'.")
7439 (defvar org-local-vars nil
7440 "List of local variables, for use by `orgstruct-mode'")
7442 ;;;###autoload
7443 (define-minor-mode orgstruct-mode
7444 "Toggle the minor more `orgstruct-mode'.
7445 This mode is for using Org-mode structure commands in other modes.
7446 The following key behave as if Org-mode was active, if the cursor
7447 is on a headline, or on a plain list item (both in the definition
7448 of Org-mode).
7450 M-up Move entry/item up
7451 M-down Move entry/item down
7452 M-left Promote
7453 M-right Demote
7454 M-S-up Move entry/item up
7455 M-S-down Move entry/item down
7456 M-S-left Promote subtree
7457 M-S-right Demote subtree
7458 M-q Fill paragraph and items like in Org-mode
7459 C-c ^ Sort entries
7460 C-c - Cycle list bullet
7461 TAB Cycle item visibility
7462 M-RET Insert new heading/item
7463 S-M-RET Insert new TODO heading / Checkbox item
7464 C-c C-c Set tags / toggle checkbox"
7465 nil " OrgStruct" nil
7466 (org-load-modules-maybe)
7467 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
7469 ;;;###autoload
7470 (defun turn-on-orgstruct ()
7471 "Unconditionally turn on `orgstruct-mode'."
7472 (orgstruct-mode 1))
7474 (defun orgstruct++-mode (&optional arg)
7475 "Toggle `orgstruct-mode', the enhanced version of it.
7476 In addition to setting orgstruct-mode, this also exports all indentation
7477 and autofilling variables from org-mode into the buffer. It will also
7478 recognize item context in multiline items.
7479 Note that turning off orgstruct-mode will *not* remove the
7480 indentation/paragraph settings. This can only be done by refreshing the
7481 major mode, for example with \\[normal-mode]."
7482 (interactive "P")
7483 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
7484 (if (< arg 1)
7485 (orgstruct-mode -1)
7486 (orgstruct-mode 1)
7487 (let (var val)
7488 (mapc
7489 (lambda (x)
7490 (when (string-match
7491 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7492 (symbol-name (car x)))
7493 (setq var (car x) val (nth 1 x))
7494 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
7495 org-local-vars)
7496 (org-set-local 'orgstruct-is-++ t))))
7498 (defvar orgstruct-is-++ nil
7499 "Is orgstruct-mode in ++ version in the current-buffer?")
7500 (make-variable-buffer-local 'orgstruct-is-++)
7502 ;;;###autoload
7503 (defun turn-on-orgstruct++ ()
7504 "Unconditionally turn on `orgstruct++-mode'."
7505 (orgstruct++-mode 1))
7507 (defun orgstruct-error ()
7508 "Error when there is no default binding for a structure key."
7509 (interactive)
7510 (error "This key has no function outside structure elements"))
7512 (defun orgstruct-setup ()
7513 "Setup orgstruct keymaps."
7514 (let ((nfunc 0)
7515 (bindings
7516 (list
7517 '([(meta up)] org-metaup)
7518 '([(meta down)] org-metadown)
7519 '([(meta left)] org-metaleft)
7520 '([(meta right)] org-metaright)
7521 '([(meta shift up)] org-shiftmetaup)
7522 '([(meta shift down)] org-shiftmetadown)
7523 '([(meta shift left)] org-shiftmetaleft)
7524 '([(meta shift right)] org-shiftmetaright)
7525 '([?\e (up)] org-metaup)
7526 '([?\e (down)] org-metadown)
7527 '([?\e (left)] org-metaleft)
7528 '([?\e (right)] org-metaright)
7529 '([?\e (shift up)] org-shiftmetaup)
7530 '([?\e (shift down)] org-shiftmetadown)
7531 '([?\e (shift left)] org-shiftmetaleft)
7532 '([?\e (shift right)] org-shiftmetaright)
7533 '([(shift up)] org-shiftup)
7534 '([(shift down)] org-shiftdown)
7535 '([(shift left)] org-shiftleft)
7536 '([(shift right)] org-shiftright)
7537 '("\C-c\C-c" org-ctrl-c-ctrl-c)
7538 '("\M-q" fill-paragraph)
7539 '("\C-c^" org-sort)
7540 '("\C-c-" org-cycle-list-bullet)))
7541 elt key fun cmd)
7542 (while (setq elt (pop bindings))
7543 (setq nfunc (1+ nfunc))
7544 (setq key (org-key (car elt))
7545 fun (nth 1 elt)
7546 cmd (orgstruct-make-binding fun nfunc key))
7547 (org-defkey orgstruct-mode-map key cmd))
7549 ;; Special treatment needed for TAB and RET
7550 (org-defkey orgstruct-mode-map [(tab)]
7551 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
7552 (org-defkey orgstruct-mode-map "\C-i"
7553 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
7555 (org-defkey orgstruct-mode-map "\M-\C-m"
7556 (orgstruct-make-binding 'org-insert-heading 105
7557 "\M-\C-m" [(meta return)]))
7558 (org-defkey orgstruct-mode-map [(meta return)]
7559 (orgstruct-make-binding 'org-insert-heading 106
7560 [(meta return)] "\M-\C-m"))
7562 (org-defkey orgstruct-mode-map [(shift meta return)]
7563 (orgstruct-make-binding 'org-insert-todo-heading 107
7564 [(meta return)] "\M-\C-m"))
7566 (org-defkey orgstruct-mode-map "\e\C-m"
7567 (orgstruct-make-binding 'org-insert-heading 108
7568 "\e\C-m" [?\e (return)]))
7569 (org-defkey orgstruct-mode-map [?\e (return)]
7570 (orgstruct-make-binding 'org-insert-heading 109
7571 [?\e (return)] "\e\C-m"))
7572 (org-defkey orgstruct-mode-map [?\e (shift return)]
7573 (orgstruct-make-binding 'org-insert-todo-heading 110
7574 [?\e (return)] "\e\C-m"))
7576 (unless org-local-vars
7577 (setq org-local-vars (org-get-local-variables)))
7581 (defun orgstruct-make-binding (fun n &rest keys)
7582 "Create a function for binding in the structure minor mode.
7583 FUN is the command to call inside a table. N is used to create a unique
7584 command name. KEYS are keys that should be checked in for a command
7585 to execute outside of tables."
7586 (eval
7587 (list 'defun
7588 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
7589 '(arg)
7590 (concat "In Structure, run `" (symbol-name fun) "'.\n"
7591 "Outside of structure, run the binding of `"
7592 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
7593 "'.")
7594 '(interactive "p")
7595 (list 'if
7596 `(org-context-p 'headline 'item
7597 (and orgstruct-is-++
7598 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
7599 'item-body))
7600 (list 'org-run-like-in-org-mode (list 'quote fun))
7601 (list 'let '(orgstruct-mode)
7602 (list 'call-interactively
7603 (append '(or)
7604 (mapcar (lambda (k)
7605 (list 'key-binding k))
7606 keys)
7607 '('orgstruct-error))))))))
7609 (defun org-context-p (&rest contexts)
7610 "Check if local context is any of CONTEXTS.
7611 Possible values in the list of contexts are `table', `headline', and `item'."
7612 (let ((pos (point)))
7613 (goto-char (point-at-bol))
7614 (prog1 (or (and (memq 'table contexts)
7615 (looking-at "[ \t]*|"))
7616 (and (memq 'headline contexts)
7617 ;;????????? (looking-at "\\*+"))
7618 (looking-at outline-regexp))
7619 (and (memq 'item contexts)
7620 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
7621 (and (memq 'item-body contexts)
7622 (org-in-item-p)))
7623 (goto-char pos))))
7625 (defun org-get-local-variables ()
7626 "Return a list of all local variables in an org-mode buffer."
7627 (let (varlist)
7628 (with-current-buffer (get-buffer-create "*Org tmp*")
7629 (erase-buffer)
7630 (org-mode)
7631 (setq varlist (buffer-local-variables)))
7632 (kill-buffer "*Org tmp*")
7633 (delq nil
7634 (mapcar
7635 (lambda (x)
7636 (setq x
7637 (if (symbolp x)
7638 (list x)
7639 (list (car x) (list 'quote (cdr x)))))
7640 (if (string-match
7641 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7642 (symbol-name (car x)))
7643 x nil))
7644 varlist))))
7646 ;;;###autoload
7647 (defun org-run-like-in-org-mode (cmd)
7648 "Run a command, pretending that the current buffer is in Org-mode.
7649 This will temporarily bind local variables that are typically bound in
7650 Org-mode to the values they have in Org-mode, and then interactively
7651 call CMD."
7652 (org-load-modules-maybe)
7653 (unless org-local-vars
7654 (setq org-local-vars (org-get-local-variables)))
7655 (eval (list 'let org-local-vars
7656 (list 'call-interactively (list 'quote cmd)))))
7658 ;;;; Archiving
7660 (defun org-get-category (&optional pos)
7661 "Get the category applying to position POS."
7662 (get-text-property (or pos (point)) 'org-category))
7664 (defun org-refresh-category-properties ()
7665 "Refresh category text properties in the buffer."
7666 (let ((def-cat (cond
7667 ((null org-category)
7668 (if buffer-file-name
7669 (file-name-sans-extension
7670 (file-name-nondirectory buffer-file-name))
7671 "???"))
7672 ((symbolp org-category) (symbol-name org-category))
7673 (t org-category)))
7674 beg end cat pos optionp)
7675 (org-unmodified
7676 (save-excursion
7677 (save-restriction
7678 (widen)
7679 (goto-char (point-min))
7680 (put-text-property (point) (point-max) 'org-category def-cat)
7681 (while (re-search-forward
7682 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
7683 (setq pos (match-end 0)
7684 optionp (equal (char-after (match-beginning 0)) ?#)
7685 cat (org-trim (match-string 2)))
7686 (if optionp
7687 (setq beg (point-at-bol) end (point-max))
7688 (org-back-to-heading t)
7689 (setq beg (point) end (org-end-of-subtree t t)))
7690 (put-text-property beg end 'org-category cat)
7691 (goto-char pos)))))))
7694 ;;;; Link Stuff
7696 ;;; Link abbreviations
7698 (defun org-link-expand-abbrev (link)
7699 "Apply replacements as defined in `org-link-abbrev-alist."
7700 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
7701 (let* ((key (match-string 1 link))
7702 (as (or (assoc key org-link-abbrev-alist-local)
7703 (assoc key org-link-abbrev-alist)))
7704 (tag (and (match-end 2) (match-string 3 link)))
7705 rpl)
7706 (if (not as)
7707 link
7708 (setq rpl (cdr as))
7709 (cond
7710 ((symbolp rpl) (funcall rpl tag))
7711 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
7712 ((string-match "%h" rpl)
7713 (replace-match (url-hexify-string (or tag "")) t t rpl))
7714 (t (concat rpl tag)))))
7715 link))
7717 ;;; Storing and inserting links
7719 (defvar org-insert-link-history nil
7720 "Minibuffer history for links inserted with `org-insert-link'.")
7722 (defvar org-stored-links nil
7723 "Contains the links stored with `org-store-link'.")
7725 (defvar org-store-link-plist nil
7726 "Plist with info about the most recently link created with `org-store-link'.")
7728 (defvar org-link-protocols nil
7729 "Link protocols added to Org-mode using `org-add-link-type'.")
7731 (defvar org-store-link-functions nil
7732 "List of functions that are called to create and store a link.
7733 Each function will be called in turn until one returns a non-nil
7734 value. Each function should check if it is responsible for creating
7735 this link (for example by looking at the major mode).
7736 If not, it must exit and return nil.
7737 If yes, it should return a non-nil value after a calling
7738 `org-store-link-props' with a list of properties and values.
7739 Special properties are:
7741 :type The link prefix. like \"http\". This must be given.
7742 :link The link, like \"http://www.astro.uva.nl/~dominik\".
7743 This is obligatory as well.
7744 :description Optional default description for the second pair
7745 of brackets in an Org-mode link. The user can still change
7746 this when inserting this link into an Org-mode buffer.
7748 In addition to these, any additional properties can be specified
7749 and then used in remember templates.")
7751 (defun org-add-link-type (type &optional follow export)
7752 "Add TYPE to the list of `org-link-types'.
7753 Re-compute all regular expressions depending on `org-link-types'
7755 FOLLOW and EXPORT are two functions.
7757 FOLLOW should take the link path as the single argument and do whatever
7758 is necessary to follow the link, for example find a file or display
7759 a mail message.
7761 EXPORT should format the link path for export to one of the export formats.
7762 It should be a function accepting three arguments:
7764 path the path of the link, the text after the prefix (like \"http:\")
7765 desc the description of the link, if any, nil if there was no description
7766 format the export format, a symbol like `html' or `latex'.
7768 The function may use the FORMAT information to return different values
7769 depending on the format. The return value will be put literally into
7770 the exported file.
7771 Org-mode has a built-in default for exporting links. If you are happy with
7772 this default, there is no need to define an export function for the link
7773 type. For a simple example of an export function, see `org-bbdb.el'."
7774 (add-to-list 'org-link-types type t)
7775 (org-make-link-regexps)
7776 (if (assoc type org-link-protocols)
7777 (setcdr (assoc type org-link-protocols) (list follow export))
7778 (push (list type follow export) org-link-protocols)))
7780 (defvar org-agenda-buffer-name)
7782 ;;;###autoload
7783 (defun org-store-link (arg)
7784 "\\<org-mode-map>Store an org-link to the current location.
7785 This link is added to `org-stored-links' and can later be inserted
7786 into an org-buffer with \\[org-insert-link].
7788 For some link types, a prefix arg is interpreted:
7789 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
7790 For file links, arg negates `org-context-in-file-links'."
7791 (interactive "P")
7792 (org-load-modules-maybe)
7793 (setq org-store-link-plist nil) ; reset
7794 (let ((outline-regexp (org-get-limited-outline-regexp))
7795 link cpltxt desc description search txt custom-id)
7796 (cond
7798 ((run-hook-with-args-until-success 'org-store-link-functions)
7799 (setq link (plist-get org-store-link-plist :link)
7800 desc (or (plist-get org-store-link-plist :description) link)))
7802 ((equal (buffer-name) "*Org Edit Src Example*")
7803 (let (label gc)
7804 (while (or (not label)
7805 (save-excursion
7806 (save-restriction
7807 (widen)
7808 (goto-char (point-min))
7809 (re-search-forward
7810 (regexp-quote (format org-coderef-label-format label))
7811 nil t))))
7812 (when label (message "Label exists already") (sit-for 2))
7813 (setq label (read-string "Code line label: " label)))
7814 (end-of-line 1)
7815 (setq link (format org-coderef-label-format label))
7816 (setq gc (- 79 (length link)))
7817 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
7818 (insert link)
7819 (setq link (concat "(" label ")") desc nil)))
7821 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
7822 ;; We are in the agenda, link to referenced location
7823 (let ((m (or (get-text-property (point) 'org-hd-marker)
7824 (get-text-property (point) 'org-marker))))
7825 (when m
7826 (org-with-point-at m
7827 (call-interactively 'org-store-link)))))
7829 ((eq major-mode 'calendar-mode)
7830 (let ((cd (calendar-cursor-to-date)))
7831 (setq link
7832 (format-time-string
7833 (car org-time-stamp-formats)
7834 (apply 'encode-time
7835 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
7836 nil nil nil))))
7837 (org-store-link-props :type "calendar" :date cd)))
7839 ((eq major-mode 'w3-mode)
7840 (setq cpltxt (if (and (buffer-name)
7841 (not (string-match "Untitled" (buffer-name))))
7842 (buffer-name)
7843 (url-view-url t))
7844 link (org-make-link (url-view-url t)))
7845 (org-store-link-props :type "w3" :url (url-view-url t)))
7847 ((eq major-mode 'w3m-mode)
7848 (setq cpltxt (or w3m-current-title w3m-current-url)
7849 link (org-make-link w3m-current-url))
7850 (org-store-link-props :type "w3m" :url (url-view-url t)))
7852 ((setq search (run-hook-with-args-until-success
7853 'org-create-file-search-functions))
7854 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
7855 "::" search))
7856 (setq cpltxt (or description link)))
7858 ((eq major-mode 'image-mode)
7859 (setq cpltxt (concat "file:"
7860 (abbreviate-file-name buffer-file-name))
7861 link (org-make-link cpltxt))
7862 (org-store-link-props :type "image" :file buffer-file-name))
7864 ((eq major-mode 'dired-mode)
7865 ;; link to the file in the current line
7866 (let ((file (dired-get-filename nil t)))
7867 (setq file (if file
7868 (abbreviate-file-name
7869 (expand-file-name (dired-get-filename nil t)))
7870 ;; otherwise, no file so use current directory.
7871 default-directory))
7872 (setq cpltxt (concat "file:" file)
7873 link (org-make-link cpltxt))))
7875 ((and buffer-file-name (org-mode-p))
7876 (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
7877 (cond
7878 ((org-in-regexp "<<\\(.*?\\)>>")
7879 (setq cpltxt
7880 (concat "file:"
7881 (abbreviate-file-name buffer-file-name)
7882 "::" (match-string 1))
7883 link (org-make-link cpltxt)))
7884 ((and (featurep 'org-id)
7885 (or (eq org-link-to-org-use-id t)
7886 (and (eq org-link-to-org-use-id 'create-if-interactive)
7887 (interactive-p))
7888 (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
7889 (interactive-p)
7890 (not custom-id))
7891 (and org-link-to-org-use-id
7892 (condition-case nil
7893 (org-entry-get nil "ID")
7894 (error nil)))))
7895 ;; We can make a link using the ID.
7896 (setq link (condition-case nil
7897 (prog1 (org-id-store-link)
7898 (setq desc (plist-get org-store-link-plist
7899 :description)))
7900 (error
7901 ;; probably before first headline, link to file only
7902 (concat "file:"
7903 (abbreviate-file-name buffer-file-name))))))
7905 ;; Just link to current headline
7906 (setq cpltxt (concat "file:"
7907 (abbreviate-file-name buffer-file-name)))
7908 ;; Add a context search string
7909 (when (org-xor org-context-in-file-links arg)
7910 (setq txt (cond
7911 ((org-on-heading-p) nil)
7912 ((org-region-active-p)
7913 (buffer-substring (region-beginning) (region-end)))
7914 (t nil)))
7915 (when (or (null txt) (string-match "\\S-" txt))
7916 (setq cpltxt
7917 (concat cpltxt "::"
7918 (condition-case nil
7919 (org-make-org-heading-search-string txt)
7920 (error "")))
7921 desc (or (nth 4 (ignore-errors
7922 (org-heading-components))) "NONE"))))
7923 (if (string-match "::\\'" cpltxt)
7924 (setq cpltxt (substring cpltxt 0 -2)))
7925 (setq link (org-make-link cpltxt)))))
7927 ((buffer-file-name (buffer-base-buffer))
7928 ;; Just link to this file here.
7929 (setq cpltxt (concat "file:"
7930 (abbreviate-file-name
7931 (buffer-file-name (buffer-base-buffer)))))
7932 ;; Add a context string
7933 (when (org-xor org-context-in-file-links arg)
7934 (setq txt (if (org-region-active-p)
7935 (buffer-substring (region-beginning) (region-end))
7936 (buffer-substring (point-at-bol) (point-at-eol))))
7937 ;; Only use search option if there is some text.
7938 (when (string-match "\\S-" txt)
7939 (setq cpltxt
7940 (concat cpltxt "::" (org-make-org-heading-search-string txt))
7941 desc "NONE")))
7942 (setq link (org-make-link cpltxt)))
7944 ((interactive-p)
7945 (error "Cannot link to a buffer which is not visiting a file"))
7947 (t (setq link nil)))
7949 (if (consp link) (setq cpltxt (car link) link (cdr link)))
7950 (setq link (or link cpltxt)
7951 desc (or desc cpltxt))
7952 (if (equal desc "NONE") (setq desc nil))
7954 (if (and (or (interactive-p) executing-kbd-macro) link)
7955 (progn
7956 (setq org-stored-links
7957 (cons (list link desc) org-stored-links))
7958 (message "Stored: %s" (or desc link))
7959 (when custom-id
7960 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
7961 "::#" custom-id))
7962 (setq org-stored-links
7963 (cons (list link desc) org-stored-links))))
7964 (and link (org-make-link-string link desc)))))
7966 (defun org-store-link-props (&rest plist)
7967 "Store link properties, extract names and addresses."
7968 (let (x adr)
7969 (when (setq x (plist-get plist :from))
7970 (setq adr (mail-extract-address-components x))
7971 (setq plist (plist-put plist :fromname (car adr)))
7972 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
7973 (when (setq x (plist-get plist :to))
7974 (setq adr (mail-extract-address-components x))
7975 (setq plist (plist-put plist :toname (car adr)))
7976 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
7977 (let ((from (plist-get plist :from))
7978 (to (plist-get plist :to)))
7979 (when (and from to org-from-is-user-regexp)
7980 (setq plist
7981 (plist-put plist :fromto
7982 (if (string-match org-from-is-user-regexp from)
7983 (concat "to %t")
7984 (concat "from %f"))))))
7985 (setq org-store-link-plist plist))
7987 (defun org-add-link-props (&rest plist)
7988 "Add these properties to the link property list."
7989 (let (key value)
7990 (while plist
7991 (setq key (pop plist) value (pop plist))
7992 (setq org-store-link-plist
7993 (plist-put org-store-link-plist key value)))))
7995 (defun org-email-link-description (&optional fmt)
7996 "Return the description part of an email link.
7997 This takes information from `org-store-link-plist' and formats it
7998 according to FMT (default from `org-email-link-description-format')."
7999 (setq fmt (or fmt org-email-link-description-format))
8000 (let* ((p org-store-link-plist)
8001 (to (plist-get p :toaddress))
8002 (from (plist-get p :fromaddress))
8003 (table
8004 (list
8005 (cons "%c" (plist-get p :fromto))
8006 (cons "%F" (plist-get p :from))
8007 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
8008 (cons "%T" (plist-get p :to))
8009 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
8010 (cons "%s" (plist-get p :subject))
8011 (cons "%m" (plist-get p :message-id)))))
8012 (when (string-match "%c" fmt)
8013 ;; Check if the user wrote this message
8014 (if (and org-from-is-user-regexp from to
8015 (save-match-data (string-match org-from-is-user-regexp from)))
8016 (setq fmt (replace-match "to %t" t t fmt))
8017 (setq fmt (replace-match "from %f" t t fmt))))
8018 (org-replace-escapes fmt table)))
8020 (defun org-make-org-heading-search-string (&optional string heading)
8021 "Make search string for STRING or current headline."
8022 (interactive)
8023 (let ((s (or string (org-get-heading))))
8024 (unless (and string (not heading))
8025 ;; We are using a headline, clean up garbage in there.
8026 (if (string-match org-todo-regexp s)
8027 (setq s (replace-match "" t t s)))
8028 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
8029 (setq s (replace-match "" t t s)))
8030 (setq s (org-trim s))
8031 (if (string-match (concat "^\\(" org-quote-string "\\|"
8032 org-comment-string "\\)") s)
8033 (setq s (replace-match "" t t s)))
8034 (while (string-match org-ts-regexp s)
8035 (setq s (replace-match "" t t s))))
8036 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
8037 (setq s (replace-match " " t t s)))
8038 (or string (setq s (concat "*" s))) ; Add * for headlines
8039 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
8041 (defun org-make-link (&rest strings)
8042 "Concatenate STRINGS."
8043 (apply 'concat strings))
8045 (defun org-make-link-string (link &optional description)
8046 "Make a link with brackets, consisting of LINK and DESCRIPTION."
8047 (unless (string-match "\\S-" link)
8048 (error "Empty link"))
8049 (when (and description
8050 (stringp description)
8051 (not (string-match "\\S-" description)))
8052 (setq description nil))
8053 (when (stringp description)
8054 ;; Remove brackets from the description, they are fatal.
8055 (while (string-match "\\[" description)
8056 (setq description (replace-match "{" t t description)))
8057 (while (string-match "\\]" description)
8058 (setq description (replace-match "}" t t description))))
8059 (when (equal (org-link-escape link) description)
8060 ;; No description needed, it is identical
8061 (setq description nil))
8062 (when (and (not description)
8063 (not (equal link (org-link-escape link))))
8064 (setq description (org-extract-attributes link)))
8065 (concat "[[" (org-link-escape link) "]"
8066 (if description (concat "[" description "]") "")
8067 "]"))
8069 (defconst org-link-escape-chars
8070 '((?\ . "%20")
8071 (?\[ . "%5B")
8072 (?\] . "%5D")
8073 (?\340 . "%E0") ; `a
8074 (?\342 . "%E2") ; ^a
8075 (?\347 . "%E7") ; ,c
8076 (?\350 . "%E8") ; `e
8077 (?\351 . "%E9") ; 'e
8078 (?\352 . "%EA") ; ^e
8079 (?\356 . "%EE") ; ^i
8080 (?\364 . "%F4") ; ^o
8081 (?\371 . "%F9") ; `u
8082 (?\373 . "%FB") ; ^u
8083 (?\; . "%3B")
8084 ;; (?? . "%3F")
8085 (?= . "%3D")
8086 (?+ . "%2B")
8088 "Association list of escapes for some characters problematic in links.
8089 This is the list that is used for internal purposes.")
8091 (defvar org-url-encoding-use-url-hexify nil)
8093 (defconst org-link-escape-chars-browser
8094 '((?\ . "%20")) ; 32 for the SPC char
8095 "Association list of escapes for some characters problematic in links.
8096 This is the list that is used before handing over to the browser.")
8098 (defun org-link-escape (text &optional table)
8099 "Escape characters in TEXT that are problematic for links."
8100 (if (and org-url-encoding-use-url-hexify (not table))
8101 (url-hexify-string text)
8102 (setq table (or table org-link-escape-chars))
8103 (when text
8104 (let ((re (mapconcat (lambda (x) (regexp-quote
8105 (char-to-string (car x))))
8106 table "\\|")))
8107 (while (string-match re text)
8108 (setq text
8109 (replace-match
8110 (cdr (assoc (string-to-char (match-string 0 text))
8111 table))
8112 t t text)))
8113 text))))
8115 (defun org-link-unescape (text &optional table)
8116 "Reverse the action of `org-link-escape'."
8117 (if (and org-url-encoding-use-url-hexify (not table))
8118 (url-unhex-string text)
8119 (setq table (or table org-link-escape-chars))
8120 (when text
8121 (let ((case-fold-search t)
8122 (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
8123 table "\\|")))
8124 (while (string-match re text)
8125 (setq text
8126 (replace-match
8127 (char-to-string (car (rassoc (upcase (match-string 0 text))
8128 table)))
8129 t t text)))
8130 text))))
8132 (defun org-xor (a b)
8133 "Exclusive or."
8134 (if a (not b) b))
8136 (defun org-fixup-message-id-for-http (s)
8137 "Replace special characters in a message id, so it can be used in an http query."
8138 (while (string-match "<" s)
8139 (setq s (replace-match "%3C" t t s)))
8140 (while (string-match ">" s)
8141 (setq s (replace-match "%3E" t t s)))
8142 (while (string-match "@" s)
8143 (setq s (replace-match "%40" t t s)))
8146 ;;;###autoload
8147 (defun org-insert-link-global ()
8148 "Insert a link like Org-mode does.
8149 This command can be called in any mode to insert a link in Org-mode syntax."
8150 (interactive)
8151 (org-load-modules-maybe)
8152 (org-run-like-in-org-mode 'org-insert-link))
8154 (defun org-insert-link (&optional complete-file link-location)
8155 "Insert a link. At the prompt, enter the link.
8157 Completion can be used to insert any of the link protocol prefixes like
8158 http or ftp in use.
8160 The history can be used to select a link previously stored with
8161 `org-store-link'. When the empty string is entered (i.e. if you just
8162 press RET at the prompt), the link defaults to the most recently
8163 stored link. As SPC triggers completion in the minibuffer, you need to
8164 use M-SPC or C-q SPC to force the insertion of a space character.
8166 You will also be prompted for a description, and if one is given, it will
8167 be displayed in the buffer instead of the link.
8169 If there is already a link at point, this command will allow you to edit link
8170 and description parts.
8172 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
8173 be selected using completion. The path to the file will be relative to the
8174 current directory if the file is in the current directory or a subdirectory.
8175 Otherwise, the link will be the absolute path as completed in the minibuffer
8176 \(i.e. normally ~/path/to/file). You can configure this behavior using the
8177 option `org-link-file-path-type'.
8179 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
8180 the current directory or below.
8182 With three \\[universal-argument] prefixes, negate the meaning of
8183 `org-keep-stored-link-after-insertion'.
8185 If `org-make-link-description-function' is non-nil, this function will be
8186 called with the link target, and the result will be the default
8187 link description.
8189 If the LINK-LOCATION parameter is non-nil, this value will be
8190 used as the link location instead of reading one interactively."
8191 (interactive "P")
8192 (let* ((wcf (current-window-configuration))
8193 (region (if (org-region-active-p)
8194 (buffer-substring (region-beginning) (region-end))))
8195 (remove (and region (list (region-beginning) (region-end))))
8196 (desc region)
8197 tmphist ; byte-compile incorrectly complains about this
8198 (link link-location)
8199 entry file all-prefixes)
8200 (cond
8201 (link-location) ; specified by arg, just use it.
8202 ((org-in-regexp org-bracket-link-regexp 1)
8203 ;; We do have a link at point, and we are going to edit it.
8204 (setq remove (list (match-beginning 0) (match-end 0)))
8205 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
8206 (setq link (read-string "Link: "
8207 (org-link-unescape
8208 (org-match-string-no-properties 1)))))
8209 ((or (org-in-regexp org-angle-link-re)
8210 (org-in-regexp org-plain-link-re))
8211 ;; Convert to bracket link
8212 (setq remove (list (match-beginning 0) (match-end 0))
8213 link (read-string "Link: "
8214 (org-remove-angle-brackets (match-string 0)))))
8215 ((member complete-file '((4) (16)))
8216 ;; Completing read for file names.
8217 (setq link (org-file-complete-link complete-file)))
8219 ;; Read link, with completion for stored links.
8220 (with-output-to-temp-buffer "*Org Links*"
8221 (princ "Insert a link.
8222 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
8223 (when org-stored-links
8224 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
8225 (princ (mapconcat
8226 (lambda (x)
8227 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
8228 (reverse org-stored-links) "\n"))))
8229 (let ((cw (selected-window)))
8230 (select-window (get-buffer-window "*Org Links*" 'visible))
8231 (setq truncate-lines t)
8232 (unless (pos-visible-in-window-p (point-max))
8233 (org-fit-window-to-buffer))
8234 (and (window-live-p cw) (select-window cw)))
8235 ;; Fake a link history, containing the stored links.
8236 (setq tmphist (append (mapcar 'car org-stored-links)
8237 org-insert-link-history))
8238 (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
8239 (mapcar 'car org-link-abbrev-alist)
8240 org-link-types))
8241 (unwind-protect
8242 (progn
8243 (setq link
8244 (let ((org-completion-use-ido nil)
8245 (org-completion-use-iswitchb nil))
8246 (org-completing-read
8247 "Link: "
8248 (append
8249 (mapcar (lambda (x) (list (concat x ":")))
8250 all-prefixes)
8251 (mapcar 'car org-stored-links))
8252 nil nil nil
8253 'tmphist
8254 (car (car org-stored-links)))))
8255 (if (not (string-match "\\S-" link))
8256 (error "No link selected"))
8257 (if (or (member link all-prefixes)
8258 (and (equal ":" (substring link -1))
8259 (member (substring link 0 -1) all-prefixes)
8260 (setq link (substring link 0 -1))))
8261 (setq link (org-link-try-special-completion link))))
8262 (set-window-configuration wcf)
8263 (kill-buffer "*Org Links*"))
8264 (setq entry (assoc link org-stored-links))
8265 (or entry (push link org-insert-link-history))
8266 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
8267 (not org-keep-stored-link-after-insertion))
8268 (setq org-stored-links (delq (assoc link org-stored-links)
8269 org-stored-links)))
8270 (setq desc (or desc (nth 1 entry)))))
8272 (if (string-match org-plain-link-re link)
8273 ;; URL-like link, normalize the use of angular brackets.
8274 (setq link (org-make-link (org-remove-angle-brackets link))))
8276 ;; Check if we are linking to the current file with a search option
8277 ;; If yes, simplify the link by using only the search option.
8278 (when (and buffer-file-name
8279 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
8280 (let* ((path (match-string 1 link))
8281 (case-fold-search nil)
8282 (search (match-string 2 link)))
8283 (save-match-data
8284 (if (equal (file-truename buffer-file-name) (file-truename path))
8285 ;; We are linking to this same file, with a search option
8286 (setq link search)))))
8288 ;; Check if we can/should use a relative path. If yes, simplify the link
8289 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
8290 (let* ((type (match-string 1 link))
8291 (path (match-string 2 link))
8292 (origpath path)
8293 (case-fold-search nil))
8294 (cond
8295 ((or (eq org-link-file-path-type 'absolute)
8296 (equal complete-file '(16)))
8297 (setq path (abbreviate-file-name (expand-file-name path))))
8298 ((eq org-link-file-path-type 'noabbrev)
8299 (setq path (expand-file-name path)))
8300 ((eq org-link-file-path-type 'relative)
8301 (setq path (file-relative-name path)))
8303 (save-match-data
8304 (if (string-match (concat "^" (regexp-quote
8305 (file-name-as-directory
8306 (expand-file-name "."))))
8307 (expand-file-name path))
8308 ;; We are linking a file with relative path name.
8309 (setq path (substring (expand-file-name path)
8310 (match-end 0)))
8311 (setq path (abbreviate-file-name (expand-file-name path)))))))
8312 (setq link (concat type path))
8313 (if (equal desc origpath)
8314 (setq desc path))))
8316 (if org-make-link-description-function
8317 (setq desc (funcall org-make-link-description-function link desc)))
8319 (setq desc (read-string "Description: " desc))
8320 (unless (string-match "\\S-" desc) (setq desc nil))
8321 (if remove (apply 'delete-region remove))
8322 (insert (org-make-link-string link desc))))
8324 (defun org-link-try-special-completion (type)
8325 "If there is completion support for link type TYPE, offer it."
8326 (let ((fun (intern (concat "org-" type "-complete-link"))))
8327 (if (functionp fun)
8328 (funcall fun)
8329 (read-string "Link (no completion support): " (concat type ":")))))
8331 (defun org-file-complete-link (&optional arg)
8332 "Create a file link using completion."
8333 (let (file link)
8334 (setq file (read-file-name "File: "))
8335 (let ((pwd (file-name-as-directory (expand-file-name ".")))
8336 (pwd1 (file-name-as-directory (abbreviate-file-name
8337 (expand-file-name ".")))))
8338 (cond
8339 ((equal arg '(16))
8340 (setq link (org-make-link
8341 "file:"
8342 (abbreviate-file-name (expand-file-name file)))))
8343 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
8344 (setq link (org-make-link "file:" (match-string 1 file))))
8345 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
8346 (expand-file-name file))
8347 (setq link (org-make-link
8348 "file:" (match-string 1 (expand-file-name file)))))
8349 (t (setq link (org-make-link "file:" file)))))
8350 link))
8352 (defun org-completing-read (&rest args)
8353 "Completing-read with SPACE being a normal character."
8354 (let ((minibuffer-local-completion-map
8355 (copy-keymap minibuffer-local-completion-map)))
8356 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
8357 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
8358 (apply 'org-icompleting-read args)))
8360 (defun org-completing-read-no-i (&rest args)
8361 (let (org-completion-use-ido org-completion-use-iswitchb)
8362 (apply 'org-completing-read args)))
8364 (defun org-iswitchb-completing-read (prompt choices &rest args)
8365 "Use iswitch as a completing-read replacement to choose from choices.
8366 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
8367 from."
8368 (let* ((iswitchb-use-virtual-buffers nil)
8369 (iswitchb-make-buflist-hook
8370 (lambda ()
8371 (setq iswitchb-temp-buflist choices))))
8372 (iswitchb-read-buffer prompt)))
8374 (defun org-icompleting-read (&rest args)
8375 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
8376 (org-without-partial-completion
8377 (if (and org-completion-use-ido
8378 (fboundp 'ido-completing-read)
8379 (boundp 'ido-mode) ido-mode
8380 (listp (second args)))
8381 (let ((ido-enter-matching-directory nil))
8382 (apply 'ido-completing-read (concat (car args))
8383 (if (consp (car (nth 1 args)))
8384 (mapcar (lambda (x) (car x)) (nth 1 args))
8385 (nth 1 args))
8386 (cddr args)))
8387 (if (and org-completion-use-iswitchb
8388 (boundp 'iswitchb-mode) iswitchb-mode
8389 (listp (second args)))
8390 (apply 'org-iswitchb-completing-read (concat (car args))
8391 (if (consp (car (nth 1 args)))
8392 (mapcar (lambda (x) (car x)) (nth 1 args))
8393 (nth 1 args))
8394 (cddr args))
8395 (apply 'completing-read args)))))
8397 (defun org-extract-attributes (s)
8398 "Extract the attributes cookie from a string and set as text property."
8399 (let (a attr (start 0) key value)
8400 (save-match-data
8401 (when (string-match "{{\\([^}]+\\)}}$" s)
8402 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
8403 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
8404 (setq key (match-string 1 a) value (match-string 2 a)
8405 start (match-end 0)
8406 attr (plist-put attr (intern key) value))))
8407 (org-add-props s nil 'org-attr attr))
8410 (defun org-extract-attributes-from-string (tag)
8411 (let (key value attr)
8412 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
8413 (setq key (match-string 1 tag) value (match-string 2 tag)
8414 tag (replace-match "" t t tag)
8415 attr (plist-put attr (intern key) value)))
8416 (cons tag attr)))
8418 (defun org-attributes-to-string (plist)
8419 "Format a property list into an HTML attribute list."
8420 (let ((s "") key value)
8421 (while plist
8422 (setq key (pop plist) value (pop plist))
8423 (and value
8424 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
8427 ;;; Opening/following a link
8429 (defvar org-link-search-failed nil)
8431 (defvar org-open-link-functions nil
8432 "Hook for functions finding a plain text link.
8433 These functions must take a single argument, the link content.
8434 They will be called for links that look like [[link text][description]]
8435 when LINK TEXT does not have a protocol like \"http:\" and does not look
8436 like a filename (e.g. \"./blue.png\").
8438 These functions will be called *before* Org attempts to resolve the
8439 link by doing text searches in the current buffer - so if you want a
8440 link \"[[target]]\" to still find \"<<target>>\", your function should
8441 handle this as a special case.
8443 When the function does handle the link, it must return a non-nil value.
8444 If it decides that it is not responsible for this link, it must return
8445 nil to indicate that that Org-mode can continue with other options
8446 like exact and fuzzy text search.")
8448 (defun org-next-link ()
8449 "Move forward to the next link.
8450 If the link is in hidden text, expose it."
8451 (interactive)
8452 (when (and org-link-search-failed (eq this-command last-command))
8453 (goto-char (point-min))
8454 (message "Link search wrapped back to beginning of buffer"))
8455 (setq org-link-search-failed nil)
8456 (let* ((pos (point))
8457 (ct (org-context))
8458 (a (assoc :link ct)))
8459 (if a (goto-char (nth 2 a)))
8460 (if (re-search-forward org-any-link-re nil t)
8461 (progn
8462 (goto-char (match-beginning 0))
8463 (if (org-invisible-p) (org-show-context)))
8464 (goto-char pos)
8465 (setq org-link-search-failed t)
8466 (error "No further link found"))))
8468 (defun org-previous-link ()
8469 "Move backward to the previous link.
8470 If the link is in hidden text, expose it."
8471 (interactive)
8472 (when (and org-link-search-failed (eq this-command last-command))
8473 (goto-char (point-max))
8474 (message "Link search wrapped back to end of buffer"))
8475 (setq org-link-search-failed nil)
8476 (let* ((pos (point))
8477 (ct (org-context))
8478 (a (assoc :link ct)))
8479 (if a (goto-char (nth 1 a)))
8480 (if (re-search-backward org-any-link-re nil t)
8481 (progn
8482 (goto-char (match-beginning 0))
8483 (if (org-invisible-p) (org-show-context)))
8484 (goto-char pos)
8485 (setq org-link-search-failed t)
8486 (error "No further link found"))))
8488 (defun org-translate-link (s)
8489 "Translate a link string if a translation function has been defined."
8490 (if (and org-link-translation-function
8491 (fboundp org-link-translation-function)
8492 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
8493 (progn
8494 (setq s (funcall org-link-translation-function
8495 (match-string 1) (match-string 2)))
8496 (concat (car s) ":" (cdr s)))
8499 (defun org-translate-link-from-planner (type path)
8500 "Translate a link from Emacs Planner syntax so that Org can follow it.
8501 This is still an experimental function, your mileage may vary."
8502 (cond
8503 ((member type '("http" "https" "news" "ftp"))
8504 ;; standard Internet links are the same.
8505 nil)
8506 ((and (equal type "irc") (string-match "^//" path))
8507 ;; Planner has two / at the beginning of an irc link, we have 1.
8508 ;; We should have zero, actually....
8509 (setq path (substring path 1)))
8510 ((and (equal type "lisp") (string-match "^/" path))
8511 ;; Planner has a slash, we do not.
8512 (setq type "elisp" path (substring path 1)))
8513 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
8514 ;; A typical message link. Planner has the id after the final slash,
8515 ;; we separate it with a hash mark
8516 (setq path (concat (match-string 1 path) "#"
8517 (org-remove-angle-brackets (match-string 2 path)))))
8519 (cons type path))
8521 (defun org-find-file-at-mouse (ev)
8522 "Open file link or URL at mouse."
8523 (interactive "e")
8524 (mouse-set-point ev)
8525 (org-open-at-point 'in-emacs))
8527 (defun org-open-at-mouse (ev)
8528 "Open file link or URL at mouse."
8529 (interactive "e")
8530 (mouse-set-point ev)
8531 (if (eq major-mode 'org-agenda-mode)
8532 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
8533 (org-open-at-point))
8535 (defvar org-window-config-before-follow-link nil
8536 "The window configuration before following a link.
8537 This is saved in case the need arises to restore it.")
8539 (defvar org-open-link-marker (make-marker)
8540 "Marker pointing to the location where `org-open-at-point; was called.")
8542 ;;;###autoload
8543 (defun org-open-at-point-global ()
8544 "Follow a link like Org-mode does.
8545 This command can be called in any mode to follow a link that has
8546 Org-mode syntax."
8547 (interactive)
8548 (org-run-like-in-org-mode 'org-open-at-point))
8550 ;;;###autoload
8551 (defun org-open-link-from-string (s &optional arg reference-buffer)
8552 "Open a link in the string S, as if it was in Org-mode."
8553 (interactive "sLink: \nP")
8554 (let ((reference-buffer (or reference-buffer (current-buffer))))
8555 (with-temp-buffer
8556 (let ((org-inhibit-startup t))
8557 (org-mode)
8558 (insert s)
8559 (goto-char (point-min))
8560 (when reference-buffer
8561 (setq org-link-abbrev-alist-local
8562 (with-current-buffer reference-buffer
8563 org-link-abbrev-alist-local)))
8564 (org-open-at-point arg reference-buffer)))))
8566 (defun org-open-at-point (&optional in-emacs reference-buffer)
8567 "Open link at or after point.
8568 If there is no link at point, this function will search forward up to
8569 the end of the current line.
8570 Normally, files will be opened by an appropriate application. If the
8571 optional argument IN-EMACS is non-nil, Emacs will visit the file.
8572 With a double prefix argument, try to open outside of Emacs, in the
8573 application the system uses for this file type."
8574 (interactive "P")
8575 (org-load-modules-maybe)
8576 (move-marker org-open-link-marker (point))
8577 (setq org-window-config-before-follow-link (current-window-configuration))
8578 (org-remove-occur-highlights nil nil t)
8579 (cond
8580 ((and (org-on-heading-p)
8581 (not (org-in-regexp
8582 (concat org-plain-link-re "\\|"
8583 org-bracket-link-regexp "\\|"
8584 org-angle-link-re "\\|"
8585 "[ \t]:[^ \t\n]+:[ \t]*$")))
8586 (not (get-text-property (point) 'org-linked-text)))
8587 (or (org-offer-links-in-entry in-emacs)
8588 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
8589 ((org-at-timestamp-p t) (org-follow-timestamp-link))
8590 ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
8591 (org-footnote-action))
8593 (let (type path link line search (pos (point)))
8594 (catch 'match
8595 (save-excursion
8596 (skip-chars-forward "^]\n\r")
8597 (when (org-in-regexp org-bracket-link-regexp 1)
8598 (setq link (org-extract-attributes
8599 (org-link-unescape (org-match-string-no-properties 1))))
8600 (while (string-match " *\n *" link)
8601 (setq link (replace-match " " t t link)))
8602 (setq link (org-link-expand-abbrev link))
8603 (cond
8604 ((or (file-name-absolute-p link)
8605 (string-match "^\\.\\.?/" link))
8606 (setq type "file" path link))
8607 ((string-match org-link-re-with-space3 link)
8608 (setq type (match-string 1 link) path (match-string 2 link)))
8609 (t (setq type "thisfile" path link)))
8610 (throw 'match t)))
8612 (when (get-text-property (point) 'org-linked-text)
8613 (setq type "thisfile"
8614 pos (if (get-text-property (1+ (point)) 'org-linked-text)
8615 (1+ (point)) (point))
8616 path (buffer-substring
8617 (previous-single-property-change pos 'org-linked-text)
8618 (next-single-property-change pos 'org-linked-text)))
8619 (throw 'match t))
8621 (save-excursion
8622 (when (or (org-in-regexp org-angle-link-re)
8623 (org-in-regexp org-plain-link-re))
8624 (setq type (match-string 1) path (match-string 2))
8625 (throw 'match t)))
8626 (save-excursion
8627 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
8628 (setq type "tags"
8629 path (match-string 1))
8630 (while (string-match ":" path)
8631 (setq path (replace-match "+" t t path)))
8632 (throw 'match t)))
8633 (when (org-in-regexp "<\\([^><\n]+\\)>")
8634 (setq type "tree-match"
8635 path (match-string 1))
8636 (throw 'match t)))
8637 (unless path
8638 (error "No link found"))
8640 ;; switch back to reference buffer
8641 ;; needed when if called in a temporary buffer through
8642 ;; org-open-link-from-string
8643 (with-current-buffer (or reference-buffer (current-buffer))
8645 ;; Remove any trailing spaces in path
8646 (if (string-match " +\\'" path)
8647 (setq path (replace-match "" t t path)))
8648 (if (and org-link-translation-function
8649 (fboundp org-link-translation-function))
8650 ;; Check if we need to translate the link
8651 (let ((tmp (funcall org-link-translation-function type path)))
8652 (setq type (car tmp) path (cdr tmp))))
8654 (cond
8656 ((assoc type org-link-protocols)
8657 (funcall (nth 1 (assoc type org-link-protocols)) path))
8659 ((equal type "mailto")
8660 (let ((cmd (car org-link-mailto-program))
8661 (args (cdr org-link-mailto-program)) args1
8662 (address path) (subject "") a)
8663 (if (string-match "\\(.*\\)::\\(.*\\)" path)
8664 (setq address (match-string 1 path)
8665 subject (org-link-escape (match-string 2 path))))
8666 (while args
8667 (cond
8668 ((not (stringp (car args))) (push (pop args) args1))
8669 (t (setq a (pop args))
8670 (if (string-match "%a" a)
8671 (setq a (replace-match address t t a)))
8672 (if (string-match "%s" a)
8673 (setq a (replace-match subject t t a)))
8674 (push a args1))))
8675 (apply cmd (nreverse args1))))
8677 ((member type '("http" "https" "ftp" "news"))
8678 (browse-url (concat type ":" (org-link-escape
8679 path org-link-escape-chars-browser))))
8681 ((member type '("message"))
8682 (browse-url (concat type ":" path)))
8684 ((string= type "tags")
8685 (org-tags-view in-emacs path))
8687 ((string= type "tree-match")
8688 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
8690 ((string= type "file")
8691 (if (string-match "::\\([0-9]+\\)\\'" path)
8692 (setq line (string-to-number (match-string 1 path))
8693 path (substring path 0 (match-beginning 0)))
8694 (if (string-match "::\\(.+\\)\\'" path)
8695 (setq search (match-string 1 path)
8696 path (substring path 0 (match-beginning 0)))))
8697 (if (string-match "[*?{]" (file-name-nondirectory path))
8698 (dired path)
8699 (org-open-file path in-emacs line search)))
8701 ((string= type "news")
8702 (require 'org-gnus)
8703 (org-gnus-follow-link path))
8705 ((string= type "shell")
8706 (let ((cmd path))
8707 (if (or (not org-confirm-shell-link-function)
8708 (funcall org-confirm-shell-link-function
8709 (format "Execute \"%s\" in shell? "
8710 (org-add-props cmd nil
8711 'face 'org-warning))))
8712 (progn
8713 (message "Executing %s" cmd)
8714 (shell-command cmd))
8715 (error "Abort"))))
8717 ((string= type "elisp")
8718 (let ((cmd path))
8719 (if (or (not org-confirm-elisp-link-function)
8720 (funcall org-confirm-elisp-link-function
8721 (format "Execute \"%s\" as elisp? "
8722 (org-add-props cmd nil
8723 'face 'org-warning))))
8724 (message "%s => %s" cmd
8725 (if (equal (string-to-char cmd) ?\()
8726 (eval (read cmd))
8727 (call-interactively (read cmd))))
8728 (error "Abort"))))
8730 ((and (string= type "thisfile")
8731 (run-hook-with-args-until-success
8732 'org-open-link-functions path)))
8734 ((string= type "thisfile")
8735 (if in-emacs
8736 (switch-to-buffer-other-window
8737 (org-get-buffer-for-internal-link (current-buffer)))
8738 (org-mark-ring-push))
8739 (let ((cmd `(org-link-search
8740 ,path
8741 ,(cond ((equal in-emacs '(4)) 'occur)
8742 ((equal in-emacs '(16)) 'org-occur)
8743 (t nil))
8744 ,pos)))
8745 (condition-case nil (eval cmd)
8746 (error (progn (widen) (eval cmd))))))
8749 (browse-url-at-point)))))))
8750 (move-marker org-open-link-marker nil)
8751 (run-hook-with-args 'org-follow-link-hook))
8753 (defun org-offer-links-in-entry (&optional nth zero)
8754 "Offer links in the current entry and follow the selected link.
8755 If there is only one link, follow it immediately as well.
8756 If NTH is an integer, immediately pick the NTH link found.
8757 If ZERO is a string, check also this string for a link, and if
8758 there is one, offer it as link number zero."
8759 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
8760 "\\(" org-angle-link-re "\\)\\|"
8761 "\\(" org-plain-link-re "\\)"))
8762 (cnt ?0)
8763 (in-emacs (if (integerp nth) nil nth))
8764 have-zero end links link c)
8765 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
8766 (push (match-string 0 zero) links)
8767 (setq cnt (1- cnt) have-zero t))
8768 (save-excursion
8769 (org-back-to-heading t)
8770 (setq end (save-excursion (outline-next-heading) (point)))
8771 (while (re-search-forward re end t)
8772 (push (match-string 0) links))
8773 (setq links (org-uniquify (reverse links))))
8775 (cond
8776 ((null links)
8777 (message "No links"))
8778 ((equal (length links) 1)
8779 (setq link (list (car links))))
8780 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
8781 (setq link (nth (if have-zero nth (1- nth)) links)))
8782 (t ; we have to select a link
8783 (save-excursion
8784 (save-window-excursion
8785 (delete-other-windows)
8786 (with-output-to-temp-buffer "*Select Link*"
8787 (mapc (lambda (l)
8788 (if (not (string-match org-bracket-link-regexp l))
8789 (princ (format "[%c] %s\n" (incf cnt)
8790 (org-remove-angle-brackets l)))
8791 (if (match-end 3)
8792 (princ (format "[%c] %s (%s)\n" (incf cnt)
8793 (match-string 3 l) (match-string 1 l)))
8794 (princ (format "[%c] %s\n" (incf cnt)
8795 (match-string 1 l))))))
8796 links))
8797 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
8798 (message "Select link to open, RET to open all:")
8799 (setq c (read-char-exclusive))
8800 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
8801 (when (equal c ?q) (error "Abort"))
8802 (if (equal c ?\C-m)
8803 (setq link links)
8804 (setq nth (- c ?0))
8805 (if have-zero (setq nth (1+ nth)))
8806 (unless (and (integerp nth) (>= (length links) nth))
8807 (error "Invalid link selection"))
8808 (setq link (list (nth (1- nth) links))))))
8809 (if link
8810 (let ((buf (current-buffer)))
8811 (dolist (l link)
8812 (org-open-link-from-string l in-emacs buf))
8814 nil)))
8816 ;; Add special file links that specify the way of opening
8818 (org-add-link-type "file+sys" 'org-open-file-with-system)
8819 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
8820 (defun org-open-file-with-system (path)
8821 "Open file at PATH using the system way of opeing it."
8822 (org-open-file path 'system))
8823 (defun org-open-file-with-emacs (path)
8824 "Open file at PATH in emacs."
8825 (org-open-file path 'emacs))
8826 (defun org-remove-file-link-modifiers ()
8827 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
8828 (goto-char (point-min))
8829 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
8830 (org-if-unprotected
8831 (replace-match "file:" t t))))
8832 (eval-after-load "org-exp"
8833 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
8834 'org-remove-file-link-modifiers))
8836 ;;;; Time estimates
8838 (defun org-get-effort (&optional pom)
8839 "Get the effort estimate for the current entry."
8840 (org-entry-get pom org-effort-property))
8842 ;;; File search
8844 (defvar org-create-file-search-functions nil
8845 "List of functions to construct the right search string for a file link.
8846 These functions are called in turn with point at the location to
8847 which the link should point.
8849 A function in the hook should first test if it would like to
8850 handle this file type, for example by checking the major-mode or
8851 the file extension. If it decides not to handle this file, it
8852 should just return nil to give other functions a chance. If it
8853 does handle the file, it must return the search string to be used
8854 when following the link. The search string will be part of the
8855 file link, given after a double colon, and `org-open-at-point'
8856 will automatically search for it. If special measures must be
8857 taken to make the search successful, another function should be
8858 added to the companion hook `org-execute-file-search-functions',
8859 which see.
8861 A function in this hook may also use `setq' to set the variable
8862 `description' to provide a suggestion for the descriptive text to
8863 be used for this link when it gets inserted into an Org-mode
8864 buffer with \\[org-insert-link].")
8866 (defvar org-execute-file-search-functions nil
8867 "List of functions to execute a file search triggered by a link.
8869 Functions added to this hook must accept a single argument, the
8870 search string that was part of the file link, the part after the
8871 double colon. The function must first check if it would like to
8872 handle this search, for example by checking the major-mode or the
8873 file extension. If it decides not to handle this search, it
8874 should just return nil to give other functions a chance. If it
8875 does handle the search, it must return a non-nil value to keep
8876 other functions from trying.
8878 Each function can access the current prefix argument through the
8879 variable `current-prefix-argument'. Note that a single prefix is
8880 used to force opening a link in Emacs, so it may be good to only
8881 use a numeric or double prefix to guide the search function.
8883 In case this is needed, a function in this hook can also restore
8884 the window configuration before `org-open-at-point' was called using:
8886 (set-window-configuration org-window-config-before-follow-link)")
8888 (defun org-link-search (s &optional type avoid-pos)
8889 "Search for a link search option.
8890 If S is surrounded by forward slashes, it is interpreted as a
8891 regular expression. In org-mode files, this will create an `org-occur'
8892 sparse tree. In ordinary files, `occur' will be used to list matches.
8893 If the current buffer is in `dired-mode', grep will be used to search
8894 in all files. If AVOID-POS is given, ignore matches near that position."
8895 (let ((case-fold-search t)
8896 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
8897 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
8898 (append '(("") (" ") ("\t") ("\n"))
8899 org-emphasis-alist)
8900 "\\|") "\\)"))
8901 (pos (point))
8902 (pre nil) (post nil)
8903 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
8904 (cond
8905 ;; First check if there are any special
8906 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
8907 ;; Now try the builtin stuff
8908 ((and (equal (string-to-char s0) ?#)
8909 (> (length s0) 1)
8910 (save-excursion
8911 (goto-char (point-min))
8912 (and
8913 (re-search-forward
8914 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
8915 (setq type 'dedicated
8916 pos (match-beginning 0))))
8917 ;; There is an exact target for this
8918 (goto-char pos)
8919 (org-back-to-heading t)))
8920 ((save-excursion
8921 (goto-char (point-min))
8922 (and
8923 (re-search-forward
8924 (concat "<<" (regexp-quote s0) ">>") nil t)
8925 (setq type 'dedicated
8926 pos (match-beginning 0))))
8927 ;; There is an exact target for this
8928 (goto-char pos))
8929 ((and (string-match "^(\\(.*\\))$" s0)
8930 (save-excursion
8931 (goto-char (point-min))
8932 (and
8933 (re-search-forward
8934 (concat "[^[]" (regexp-quote
8935 (format org-coderef-label-format
8936 (match-string 1 s0))))
8937 nil t)
8938 (setq type 'dedicated
8939 pos (1+ (match-beginning 0))))))
8940 ;; There is a coderef target for this
8941 (goto-char pos))
8942 ((string-match "^/\\(.*\\)/$" s)
8943 ;; A regular expression
8944 (cond
8945 ((org-mode-p)
8946 (org-occur (match-string 1 s)))
8947 ;;((eq major-mode 'dired-mode)
8948 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
8949 (t (org-do-occur (match-string 1 s)))))
8951 ;; A normal search strings
8952 (when (equal (string-to-char s) ?*)
8953 ;; Anchor on headlines, post may include tags.
8954 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
8955 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
8956 s (substring s 1)))
8957 (remove-text-properties
8958 0 (length s)
8959 '(face nil mouse-face nil keymap nil fontified nil) s)
8960 ;; Make a series of regular expressions to find a match
8961 (setq words (org-split-string s "[ \n\r\t]+")
8963 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
8964 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
8965 "\\)" markers)
8966 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
8967 re2a (concat "[ \t\r\n]" re2a_)
8968 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
8969 re4 (concat "[^a-zA-Z_]" re4_)
8971 re1 (concat pre re2 post)
8972 re3 (concat pre (if pre re4_ re4) post)
8973 re5 (concat pre ".*" re4)
8974 re2 (concat pre re2)
8975 re2a (concat pre (if pre re2a_ re2a))
8976 re4 (concat pre (if pre re4_ re4))
8977 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
8978 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
8979 re5 "\\)"
8981 (cond
8982 ((eq type 'org-occur) (org-occur reall))
8983 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
8984 (t (goto-char (point-min))
8985 (setq type 'fuzzy)
8986 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
8987 (org-search-not-self 1 re1 nil t)
8988 (org-search-not-self 1 re2 nil t)
8989 (org-search-not-self 1 re2a nil t)
8990 (org-search-not-self 1 re3 nil t)
8991 (org-search-not-self 1 re4 nil t)
8992 (org-search-not-self 1 re5 nil t)
8994 (goto-char (match-beginning 1))
8995 (goto-char pos)
8996 (error "No match")))))
8998 ;; Normal string-search
8999 (goto-char (point-min))
9000 (if (search-forward s nil t)
9001 (goto-char (match-beginning 0))
9002 (error "No match"))))
9003 (and (org-mode-p) (org-show-context 'link-search))
9004 type))
9006 (defun org-search-not-self (group &rest args)
9007 "Execute `re-search-forward', but only accept matches that do not
9008 enclose the position of `org-open-link-marker'."
9009 (let ((m org-open-link-marker))
9010 (catch 'exit
9011 (while (apply 're-search-forward args)
9012 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
9013 (goto-char (match-end group))
9014 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
9015 (> (match-beginning 0) (marker-position m))
9016 (< (match-end 0) (marker-position m)))
9017 (save-match-data
9018 (or (not (org-in-regexp
9019 org-bracket-link-analytic-regexp 1))
9020 (not (match-end 4)) ; no description
9021 (and (<= (match-beginning 4) (point))
9022 (>= (match-end 4) (point))))))
9023 (throw 'exit (point))))))))
9025 (defun org-get-buffer-for-internal-link (buffer)
9026 "Return a buffer to be used for displaying the link target of internal links."
9027 (cond
9028 ((not org-display-internal-link-with-indirect-buffer)
9029 buffer)
9030 ((string-match "(Clone)$" (buffer-name buffer))
9031 (message "Buffer is already a clone, not making another one")
9032 ;; we also do not modify visibility in this case
9033 buffer)
9034 (t ; make a new indirect buffer for displaying the link
9035 (let* ((bn (buffer-name buffer))
9036 (ibn (concat bn "(Clone)"))
9037 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
9038 (with-current-buffer ib (org-overview))
9039 ib))))
9041 (defun org-do-occur (regexp &optional cleanup)
9042 "Call the Emacs command `occur'.
9043 If CLEANUP is non-nil, remove the printout of the regular expression
9044 in the *Occur* buffer. This is useful if the regex is long and not useful
9045 to read."
9046 (occur regexp)
9047 (when cleanup
9048 (let ((cwin (selected-window)) win beg end)
9049 (when (setq win (get-buffer-window "*Occur*"))
9050 (select-window win))
9051 (goto-char (point-min))
9052 (when (re-search-forward "match[a-z]+" nil t)
9053 (setq beg (match-end 0))
9054 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
9055 (setq end (1- (match-beginning 0)))))
9056 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
9057 (goto-char (point-min))
9058 (select-window cwin))))
9060 ;;; The mark ring for links jumps
9062 (defvar org-mark-ring nil
9063 "Mark ring for positions before jumps in Org-mode.")
9064 (defvar org-mark-ring-last-goto nil
9065 "Last position in the mark ring used to go back.")
9066 ;; Fill and close the ring
9067 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
9068 (loop for i from 1 to org-mark-ring-length do
9069 (push (make-marker) org-mark-ring))
9070 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
9071 org-mark-ring)
9073 (defun org-mark-ring-push (&optional pos buffer)
9074 "Put the current position or POS into the mark ring and rotate it."
9075 (interactive)
9076 (setq pos (or pos (point)))
9077 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
9078 (move-marker (car org-mark-ring)
9079 (or pos (point))
9080 (or buffer (current-buffer)))
9081 (message "%s"
9082 (substitute-command-keys
9083 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
9085 (defun org-mark-ring-goto (&optional n)
9086 "Jump to the previous position in the mark ring.
9087 With prefix arg N, jump back that many stored positions. When
9088 called several times in succession, walk through the entire ring.
9089 Org-mode commands jumping to a different position in the current file,
9090 or to another Org-mode file, automatically push the old position
9091 onto the ring."
9092 (interactive "p")
9093 (let (p m)
9094 (if (eq last-command this-command)
9095 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
9096 (setq p org-mark-ring))
9097 (setq org-mark-ring-last-goto p)
9098 (setq m (car p))
9099 (switch-to-buffer (marker-buffer m))
9100 (goto-char m)
9101 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
9103 (defun org-remove-angle-brackets (s)
9104 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
9105 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
9107 (defun org-add-angle-brackets (s)
9108 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
9109 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
9111 (defun org-remove-double-quotes (s)
9112 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
9113 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
9116 ;;; Following specific links
9118 (defun org-follow-timestamp-link ()
9119 (cond
9120 ((org-at-date-range-p t)
9121 (let ((org-agenda-start-on-weekday)
9122 (t1 (match-string 1))
9123 (t2 (match-string 2)))
9124 (setq t1 (time-to-days (org-time-string-to-time t1))
9125 t2 (time-to-days (org-time-string-to-time t2)))
9126 (org-agenda-list nil t1 (1+ (- t2 t1)))))
9127 ((org-at-timestamp-p t)
9128 (org-agenda-list nil (time-to-days (org-time-string-to-time
9129 (substring (match-string 1) 0 10)))
9131 (t (error "This should not happen"))))
9134 ;;; Following file links
9135 (defvar org-wait nil)
9136 (defun org-open-file (path &optional in-emacs line search)
9137 "Open the file at PATH.
9138 First, this expands any special file name abbreviations. Then the
9139 configuration variable `org-file-apps' is checked if it contains an
9140 entry for this file type, and if yes, the corresponding command is launched.
9142 If no application is found, Emacs simply visits the file.
9144 With optional prefix argument IN-EMACS, Emacs will visit the file.
9145 With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
9146 and to use an external application to visit the file.
9148 Optional LINE specifies a line to go to, optional SEARCH a string
9149 to search for. If LINE or SEARCH is given, the file will be
9150 opened in Emacs, unless an entry from org-file-apps that makes
9151 use of groups in a regexp matches.
9152 If the file does not exist, an error is thrown."
9153 (let* ((file (if (equal path "")
9154 buffer-file-name
9155 (substitute-in-file-name (expand-file-name path))))
9156 (file-apps (append org-file-apps (org-default-apps)))
9157 (apps (org-remove-if
9158 'org-file-apps-entry-match-against-dlink-p file-apps))
9159 (apps-dlink (org-remove-if-not
9160 'org-file-apps-entry-match-against-dlink-p file-apps))
9161 (remp (and (assq 'remote apps) (org-file-remote-p file)))
9162 (dirp (if remp nil (file-directory-p file)))
9163 (file (if (and dirp org-open-directory-means-index-dot-org)
9164 (concat (file-name-as-directory file) "index.org")
9165 file))
9166 (a-m-a-p (assq 'auto-mode apps))
9167 (dfile (downcase file))
9168 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
9169 (link (cond ((and (eq line nil)
9170 (eq search nil))
9171 file)
9172 (line
9173 (concat file "::" (number-to-string line)))
9174 (search
9175 (concat file "::" search))))
9176 (dlink (downcase link))
9177 (old-buffer (current-buffer))
9178 (old-pos (point))
9179 (old-mode major-mode)
9180 ext cmd link-match-data)
9181 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
9182 (setq ext (match-string 1 dfile))
9183 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
9184 (setq ext (match-string 1 dfile))))
9185 (cond
9186 ((member in-emacs '((16) system))
9187 (setq cmd (cdr (assoc 'system apps))))
9188 (in-emacs (setq cmd 'emacs))
9190 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
9191 (and dirp (cdr (assoc 'directory apps)))
9192 ; first, try matching against apps-dlink
9193 ; if we get a match here, store the match data for later
9194 (let ((match (assoc-default dlink apps-dlink
9195 'string-match)))
9196 (if match
9197 (progn (setq link-match-data (match-data))
9198 match)
9199 (progn (setq in-emacs (or in-emacs line search))
9200 nil))) ; if we have no match in apps-dlink,
9201 ; always open the file in emacs if line or search
9202 ; is given (for backwards compatibility)
9203 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
9204 'string-match)
9205 (cdr (assoc ext apps))
9206 (cdr (assoc t apps))))))
9207 (when (eq cmd 'system)
9208 (setq cmd (cdr (assoc 'system apps))))
9209 (when (eq cmd 'default)
9210 (setq cmd (cdr (assoc t apps))))
9211 (when (eq cmd 'mailcap)
9212 (require 'mailcap)
9213 (mailcap-parse-mailcaps)
9214 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
9215 (command (mailcap-mime-info mime-type)))
9216 (if (stringp command)
9217 (setq cmd command)
9218 (setq cmd 'emacs))))
9219 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
9220 (not (file-exists-p file))
9221 (not org-open-non-existing-files))
9222 (error "No such file: %s" file))
9223 (cond
9224 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
9225 ;; Remove quotes around the file name - we'll use shell-quote-argument.
9226 (while (string-match "['\"]%s['\"]" cmd)
9227 (setq cmd (replace-match "%s" t t cmd)))
9228 (while (string-match "%s" cmd)
9229 (setq cmd (replace-match
9230 (save-match-data
9231 (shell-quote-argument
9232 (convert-standard-filename file)))
9233 t t cmd)))
9235 ;; Replace "%1", "%2" etc. in command with group matches from regex
9236 (save-match-data
9237 (let ((match-index 1)
9238 (number-of-groups (- (/ (length link-match-data) 2) 1)))
9239 (set-match-data link-match-data)
9240 (while (<= match-index number-of-groups)
9241 (let ((regex (concat "%" (number-to-string match-index)))
9242 (replace-with (match-string match-index dlink)))
9243 (while (string-match regex cmd)
9244 (setq cmd (replace-match replace-with t t cmd))))
9245 (setq match-index (+ match-index 1)))))
9247 (save-window-excursion
9248 (start-process-shell-command cmd nil cmd)
9249 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
9251 ((or (stringp cmd)
9252 (eq cmd 'emacs))
9253 (funcall (cdr (assq 'file org-link-frame-setup)) file)
9254 (widen)
9255 (if line (org-goto-line line)
9256 (if search (org-link-search search))))
9257 ((consp cmd)
9258 (let ((file (convert-standard-filename file)))
9259 (save-match-data
9260 (set-match-data link-match-data)
9261 (eval cmd))))
9262 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
9263 (and (org-mode-p) (eq old-mode 'org-mode)
9264 (or (not (equal old-buffer (current-buffer)))
9265 (not (equal old-pos (point))))
9266 (org-mark-ring-push old-pos old-buffer))))
9268 (defun org-file-apps-entry-match-against-dlink-p (entry)
9269 "This function returns non-nil if `entry' uses a regular
9270 expression which should be matched against the whole link by
9271 org-open-file.
9273 It assumes that is the case when the entry uses a regular
9274 expression which has at least one grouping construct and the
9275 action is either a lisp form or a command string containing
9276 '%1', i.e. using at least one subexpression match as a
9277 parameter."
9278 (let ((selector (car entry))
9279 (action (cdr entry)))
9280 (if (stringp selector)
9281 (and (> (regexp-opt-depth selector) 0)
9282 (or (and (stringp action)
9283 (string-match "%[0-9]" action))
9284 (consp action)))
9285 nil)))
9287 (defun org-default-apps ()
9288 "Return the default applications for this operating system."
9289 (cond
9290 ((eq system-type 'darwin)
9291 org-file-apps-defaults-macosx)
9292 ((eq system-type 'windows-nt)
9293 org-file-apps-defaults-windowsnt)
9294 (t org-file-apps-defaults-gnu)))
9296 (defun org-apps-regexp-alist (list &optional add-auto-mode)
9297 "Convert extensions to regular expressions in the cars of LIST.
9298 Also, weed out any non-string entries, because the return value is used
9299 only for regexp matching.
9300 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
9301 point to the symbol `emacs', indicating that the file should
9302 be opened in Emacs."
9303 (append
9304 (delq nil
9305 (mapcar (lambda (x)
9306 (if (not (stringp (car x)))
9308 (if (string-match "\\W" (car x))
9310 (cons (concat "\\." (car x) "\\'") (cdr x)))))
9311 list))
9312 (if add-auto-mode
9313 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
9315 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
9316 (defun org-file-remote-p (file)
9317 "Test whether FILE specifies a location on a remote system.
9318 Return non-nil if the location is indeed remote.
9320 For example, the filename \"/user@host:/foo\" specifies a location
9321 on the system \"/user@host:\"."
9322 (cond ((fboundp 'file-remote-p)
9323 (file-remote-p file))
9324 ((fboundp 'tramp-handle-file-remote-p)
9325 (tramp-handle-file-remote-p file))
9326 ((and (boundp 'ange-ftp-name-format)
9327 (string-match (car ange-ftp-name-format) file))
9329 (t nil)))
9332 ;;;; Refiling
9334 (defun org-get-org-file ()
9335 "Read a filename, with default directory `org-directory'."
9336 (let ((default (or org-default-notes-file remember-data-file)))
9337 (read-file-name (format "File name [%s]: " default)
9338 (file-name-as-directory org-directory)
9339 default)))
9341 (defun org-notes-order-reversed-p ()
9342 "Check if the current file should receive notes in reversed order."
9343 (cond
9344 ((not org-reverse-note-order) nil)
9345 ((eq t org-reverse-note-order) t)
9346 ((not (listp org-reverse-note-order)) nil)
9347 (t (catch 'exit
9348 (let ((all org-reverse-note-order)
9349 entry)
9350 (while (setq entry (pop all))
9351 (if (string-match (car entry) buffer-file-name)
9352 (throw 'exit (cdr entry))))
9353 nil)))))
9355 (defvar org-refile-target-table nil
9356 "The list of refile targets, created by `org-refile'.")
9358 (defvar org-agenda-new-buffers nil
9359 "Buffers created to visit agenda files.")
9361 (defun org-get-refile-targets (&optional default-buffer)
9362 "Produce a table with refile targets."
9363 (let ((case-fold-search nil)
9364 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
9365 (entries (or org-refile-targets '((nil . (:level . 1)))))
9366 targets txt re files f desc descre fast-path-p level pos0)
9367 (message "Getting targets...")
9368 (with-current-buffer (or default-buffer (current-buffer))
9369 (while (setq entry (pop entries))
9370 (setq files (car entry) desc (cdr entry))
9371 (setq fast-path-p nil)
9372 (cond
9373 ((null files) (setq files (list (current-buffer))))
9374 ((eq files 'org-agenda-files)
9375 (setq files (org-agenda-files 'unrestricted)))
9376 ((and (symbolp files) (fboundp files))
9377 (setq files (funcall files)))
9378 ((and (symbolp files) (boundp files))
9379 (setq files (symbol-value files))))
9380 (if (stringp files) (setq files (list files)))
9381 (cond
9382 ((eq (car desc) :tag)
9383 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
9384 ((eq (car desc) :todo)
9385 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
9386 ((eq (car desc) :regexp)
9387 (setq descre (cdr desc)))
9388 ((eq (car desc) :level)
9389 (setq descre (concat "^\\*\\{" (number-to-string
9390 (if org-odd-levels-only
9391 (1- (* 2 (cdr desc)))
9392 (cdr desc)))
9393 "\\}[ \t]")))
9394 ((eq (car desc) :maxlevel)
9395 (setq fast-path-p t)
9396 (setq descre (concat "^\\*\\{1," (number-to-string
9397 (if org-odd-levels-only
9398 (1- (* 2 (cdr desc)))
9399 (cdr desc)))
9400 "\\}[ \t]")))
9401 (t (error "Bad refiling target description %s" desc)))
9402 (while (setq f (pop files))
9403 (with-current-buffer
9404 (if (bufferp f) f (org-get-agenda-file-buffer f))
9405 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
9406 (setq f (and f (expand-file-name f)))
9407 (if (eq org-refile-use-outline-path 'file)
9408 (push (list (file-name-nondirectory f) f nil nil) targets))
9409 (save-excursion
9410 (save-restriction
9411 (widen)
9412 (goto-char (point-min))
9413 (while (re-search-forward descre nil t)
9414 (goto-char (setq pos0 (point-at-bol)))
9415 (catch 'next
9416 (when org-refile-target-verify-function
9417 (save-match-data
9418 (or (funcall org-refile-target-verify-function)
9419 (throw 'next t))))
9420 (when (looking-at org-complex-heading-regexp)
9421 (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
9422 txt (org-link-display-format (match-string 4))
9423 re (concat "^" (regexp-quote
9424 (buffer-substring (match-beginning 1)
9425 (match-end 4)))))
9426 (if (match-end 5) (setq re (concat re "[ \t]+"
9427 (regexp-quote
9428 (match-string 5)))))
9429 (setq re (concat re "[ \t]*$"))
9430 (when org-refile-use-outline-path
9431 (setq txt (mapconcat 'org-protect-slash
9432 (append
9433 (if (eq org-refile-use-outline-path 'file)
9434 (list (file-name-nondirectory
9435 (buffer-file-name (buffer-base-buffer))))
9436 (if (eq org-refile-use-outline-path 'full-file-path)
9437 (list (buffer-file-name (buffer-base-buffer)))))
9438 (org-get-outline-path fast-path-p level txt)
9439 (list txt))
9440 "/")))
9441 (push (list txt f re (point)) targets)))
9442 (when (= (point) pos0)
9443 ;; verification function has not moved point
9444 (goto-char (point-at-eol))))))))))
9445 (message "Getting targets...done")
9446 (nreverse targets)))
9448 (defun org-protect-slash (s)
9449 (while (string-match "/" s)
9450 (setq s (replace-match "\\" t t s)))
9453 (defvar org-olpa (make-vector 20 nil))
9455 (defun org-get-outline-path (&optional fastp level heading)
9456 "Return the outline path to the current entry, as a list.
9457 The parameters FASTP, LEVEL, and HEADING are for use be a scanner
9458 routine which makes outline path derivations for an entire file,
9459 avoiding backtracing."
9460 (if fastp
9461 (progn
9462 (if (> level 19)
9463 (error "Outline path failure, more than 19 levels."))
9464 (loop for i from level upto 19 do
9465 (aset org-olpa i nil))
9466 (prog1
9467 (delq nil (append org-olpa nil))
9468 (aset org-olpa level heading)))
9469 (let (rtn case-fold-search)
9470 (save-excursion
9471 (save-restriction
9472 (widen)
9473 (while (org-up-heading-safe)
9474 (when (looking-at org-complex-heading-regexp)
9475 (push (org-match-string-no-properties 4) rtn)))
9476 rtn)))))
9478 (defun org-format-outline-path (path &optional width prefix)
9479 "Format the outlie path PATH for display.
9480 Width is the maximum number of characters that is available.
9481 Prefix is a prefix to be included in the returned string,
9482 such as the file name."
9483 (setq width (or width 79))
9484 (if prefix (setq width (- width (length prefix))))
9485 (if (not path)
9486 (or prefix "")
9487 (let* ((nsteps (length path))
9488 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
9489 (maxwidth (if (<= total-width width)
9490 10000 ;; everything fits
9491 ;; we need to shorten the level headings
9492 (/ (- width nsteps) nsteps)))
9493 (org-odd-levels-only nil)
9494 (n 0)
9495 (total (1+ (length prefix))))
9496 (setq maxwidth (max maxwidth 10))
9497 (concat prefix
9498 (mapconcat
9499 (lambda (h)
9500 (setq n (1+ n))
9501 (if (and (= n nsteps) (< maxwidth 10000))
9502 (setq maxwidth (- total-width total)))
9503 (if (< (length h) maxwidth)
9504 (progn (setq total (+ total (length h) 1)) h)
9505 (setq h (substring h 0 (- maxwidth 2))
9506 total (+ total maxwidth 1))
9507 (if (string-match "[ \t]+\\'" h)
9508 (setq h (substring h 0 (match-beginning 0))))
9509 (setq h (concat h "..")))
9510 (org-add-props h nil 'face
9511 (nth (% (1- n) org-n-level-faces)
9512 org-level-faces))
9514 path "/")))))
9516 (defun org-display-outline-path (&optional file current)
9517 "Display the current outline path in the echo area."
9518 (interactive "P")
9519 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
9520 (case-fold-search nil)
9521 (path (and (org-mode-p) (org-get-outline-path))))
9522 (if current (setq path (append path
9523 (save-excursion
9524 (org-back-to-heading t)
9525 (if (looking-at org-complex-heading-regexp)
9526 (list (match-string 4)))))))
9527 (message "%s"
9528 (org-format-outline-path
9529 path
9530 (1- (frame-width))
9531 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
9533 (defvar org-refile-history nil
9534 "History for refiling operations.")
9536 (defvar org-after-refile-insert-hook nil
9537 "Hook run after `org-refile' has inserted its stuff at the new location.
9538 Note that this is still *before* the stuff will be removed from
9539 the *old* location.")
9541 (defun org-refile (&optional goto default-buffer rfloc)
9542 "Move the entry at point to another heading.
9543 The list of target headings is compiled using the information in
9544 `org-refile-targets', which see. This list is created before each use
9545 and will therefore always be up-to-date.
9547 At the target location, the entry is filed as a subitem of the target heading.
9548 Depending on `org-reverse-note-order', the new subitem will either be the
9549 first or the last subitem.
9551 If there is an active region, all entries in that region will be moved.
9552 However, the region must fulfil the requirement that the first heading
9553 is the first one sets the top-level of the moved text - at most siblings
9554 below it are allowed.
9556 With prefix arg GOTO, the command will only visit the target location,
9557 not actually move anything.
9558 With a double prefix `C-u C-u', go to the location where the last refiling
9559 operation has put the subtree.
9560 With a prefix argument of `2', refile to the running clock.
9562 RFLOC can be a refile location obtained in a different way.
9564 See also `org-refile-use-outline-path' and `org-completion-use-ido'"
9565 (interactive "P")
9566 (let* ((cbuf (current-buffer))
9567 (regionp (org-region-active-p))
9568 (region-start (and regionp (region-beginning)))
9569 (region-end (and regionp (region-end)))
9570 (region-length (and regionp (- region-end region-start)))
9571 (filename (buffer-file-name (buffer-base-buffer cbuf)))
9572 pos it nbuf file re level reversed)
9573 (setq last-command nil)
9574 (when regionp
9575 (goto-char region-start)
9576 (or (bolp) (goto-char (point-at-bol)))
9577 (setq region-start (point))
9578 (unless (org-kill-is-subtree-p
9579 (buffer-substring region-start region-end))
9580 (error "The region is not a (sequence of) subtree(s)")))
9581 (if (equal goto '(16))
9582 (org-refile-goto-last-stored)
9583 (when (or
9584 (and (equal goto 2)
9585 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
9586 (prog1
9587 (setq it (list (or org-clock-heading "running clock")
9588 (buffer-file-name
9589 (marker-buffer org-clock-hd-marker))
9591 (marker-position org-clock-hd-marker)))
9592 (setq goto nil)))
9593 (setq it (or rfloc
9594 (save-excursion
9595 (org-refile-get-location
9596 (if goto "Goto: " "Refile to: ") default-buffer
9597 org-refile-allow-creating-parent-nodes)))))
9598 (setq file (nth 1 it)
9599 re (nth 2 it)
9600 pos (nth 3 it))
9601 (if (and (not goto)
9603 (equal (buffer-file-name) file)
9604 (if regionp
9605 (and (>= pos region-start)
9606 (<= pos region-end))
9607 (and (>= pos (point))
9608 (< pos (save-excursion
9609 (org-end-of-subtree t t))))))
9610 (error "Cannot refile to position inside the tree or region"))
9612 (setq nbuf (or (find-buffer-visiting file)
9613 (find-file-noselect file)))
9614 (if goto
9615 (progn
9616 (switch-to-buffer nbuf)
9617 (goto-char pos)
9618 (org-show-context 'org-goto))
9619 (if regionp
9620 (progn
9621 (org-kill-new (buffer-substring region-start region-end))
9622 (org-save-markers-in-region region-start region-end))
9623 (org-copy-subtree 1 nil t))
9624 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
9625 (find-file-noselect file)))
9626 (setq reversed (org-notes-order-reversed-p))
9627 (save-excursion
9628 (save-restriction
9629 (widen)
9630 (if pos
9631 (progn
9632 (goto-char pos)
9633 (looking-at outline-regexp)
9634 (setq level (org-get-valid-level (funcall outline-level) 1))
9635 (goto-char
9636 (if reversed
9637 (or (outline-next-heading) (point-max))
9638 (or (save-excursion (org-get-next-sibling))
9639 (org-end-of-subtree t t)
9640 (point-max)))))
9641 (setq level 1)
9642 (if (not reversed)
9643 (goto-char (point-max))
9644 (goto-char (point-min))
9645 (or (outline-next-heading) (goto-char (point-max)))))
9646 (if (not (bolp)) (newline))
9647 (org-paste-subtree level)
9648 (when org-log-refile
9649 (org-add-log-setup 'refile nil nil 'findpos
9650 org-log-refile)
9651 (unless (eq org-log-refile 'note)
9652 (save-excursion (org-add-log-note))))
9653 (and org-auto-align-tags (org-set-tags nil t))
9654 (bookmark-set "org-refile-last-stored")
9655 (if (fboundp 'deactivate-mark) (deactivate-mark))
9656 (run-hooks 'org-after-refile-insert-hook))))
9657 (if regionp
9658 (delete-region (point) (+ (point) region-length))
9659 (org-cut-subtree))
9660 (when (featurep 'org-inlinetask)
9661 (org-inlinetask-remove-END-maybe))
9662 (setq org-markers-to-move nil)
9663 (message "Refiled to \"%s\"" (car it))))))
9664 (org-reveal))
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 (if (featurep 'xemacs) [button1] [mouse-1])
13593 'org-calendar-select-mouse)
13594 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
13595 'org-calendar-select-mouse)
13596 (org-defkey minibuffer-local-map [(meta shift left)]
13597 (lambda () (interactive)
13598 (org-eval-in-calendar '(calendar-backward-month 1))))
13599 (org-defkey minibuffer-local-map [(meta shift right)]
13600 (lambda () (interactive)
13601 (org-eval-in-calendar '(calendar-forward-month 1))))
13602 (org-defkey minibuffer-local-map [(meta shift up)]
13603 (lambda () (interactive)
13604 (org-eval-in-calendar '(calendar-backward-year 1))))
13605 (org-defkey minibuffer-local-map [(meta shift down)]
13606 (lambda () (interactive)
13607 (org-eval-in-calendar '(calendar-forward-year 1))))
13608 (org-defkey minibuffer-local-map [?\e (shift left)]
13609 (lambda () (interactive)
13610 (org-eval-in-calendar '(calendar-backward-month 1))))
13611 (org-defkey minibuffer-local-map [?\e (shift right)]
13612 (lambda () (interactive)
13613 (org-eval-in-calendar '(calendar-forward-month 1))))
13614 (org-defkey minibuffer-local-map [?\e (shift up)]
13615 (lambda () (interactive)
13616 (org-eval-in-calendar '(calendar-backward-year 1))))
13617 (org-defkey minibuffer-local-map [?\e (shift down)]
13618 (lambda () (interactive)
13619 (org-eval-in-calendar '(calendar-forward-year 1))))
13620 (org-defkey minibuffer-local-map [(shift up)]
13621 (lambda () (interactive)
13622 (org-eval-in-calendar '(calendar-backward-week 1))))
13623 (org-defkey minibuffer-local-map [(shift down)]
13624 (lambda () (interactive)
13625 (org-eval-in-calendar '(calendar-forward-week 1))))
13626 (org-defkey minibuffer-local-map [(shift left)]
13627 (lambda () (interactive)
13628 (org-eval-in-calendar '(calendar-backward-day 1))))
13629 (org-defkey minibuffer-local-map [(shift right)]
13630 (lambda () (interactive)
13631 (org-eval-in-calendar '(calendar-forward-day 1))))
13632 (org-defkey minibuffer-local-map ">"
13633 (lambda () (interactive)
13634 (org-eval-in-calendar '(scroll-calendar-left 1))))
13635 (org-defkey minibuffer-local-map "<"
13636 (lambda () (interactive)
13637 (org-eval-in-calendar '(scroll-calendar-right 1))))
13638 (run-hooks 'org-read-date-minibuffer-setup-hook)
13639 (unwind-protect
13640 (progn
13641 (use-local-map map)
13642 (add-hook 'post-command-hook 'org-read-date-display)
13643 (setq org-ans0 (read-string prompt default-input
13644 'org-read-date-history nil))
13645 ;; org-ans0: from prompt
13646 ;; org-ans1: from mouse click
13647 ;; org-ans2: from calendar motion
13648 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
13649 (remove-hook 'post-command-hook 'org-read-date-display)
13650 (use-local-map old-map)
13651 (when org-read-date-overlay
13652 (delete-overlay org-read-date-overlay)
13653 (setq org-read-date-overlay nil)))))))
13655 (t ; Naked prompt only
13656 (unwind-protect
13657 (setq ans (read-string prompt default-input
13658 'org-read-date-history timestr))
13659 (when org-read-date-overlay
13660 (delete-overlay org-read-date-overlay)
13661 (setq org-read-date-overlay nil)))))
13663 (setq final (org-read-date-analyze ans def defdecode))
13664 (setq org-read-date-final-answer ans)
13666 (if to-time
13667 (apply 'encode-time final)
13668 (if (and (boundp 'org-time-was-given) org-time-was-given)
13669 (format "%04d-%02d-%02d %02d:%02d"
13670 (nth 5 final) (nth 4 final) (nth 3 final)
13671 (nth 2 final) (nth 1 final))
13672 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
13674 (defvar def)
13675 (defvar defdecode)
13676 (defvar with-time)
13677 (defvar org-read-date-analyze-futurep nil)
13678 (defun org-read-date-display ()
13679 "Display the current date prompt interpretation in the minibuffer."
13680 (when org-read-date-display-live
13681 (when org-read-date-overlay
13682 (delete-overlay org-read-date-overlay))
13683 (let ((p (point)))
13684 (end-of-line 1)
13685 (while (not (equal (buffer-substring
13686 (max (point-min) (- (point) 4)) (point))
13687 " "))
13688 (insert " "))
13689 (goto-char p))
13690 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
13691 " " (or org-ans1 org-ans2)))
13692 (org-end-time-was-given nil)
13693 (f (org-read-date-analyze ans def defdecode))
13694 (fmts (if org-dcst
13695 org-time-stamp-custom-formats
13696 org-time-stamp-formats))
13697 (fmt (if (or with-time
13698 (and (boundp 'org-time-was-given) org-time-was-given))
13699 (cdr fmts)
13700 (car fmts)))
13701 (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
13702 (when (and org-end-time-was-given
13703 (string-match org-plain-time-of-day-regexp txt))
13704 (setq txt (concat (substring txt 0 (match-end 0)) "-"
13705 org-end-time-was-given
13706 (substring txt (match-end 0)))))
13707 (when org-read-date-analyze-futurep
13708 (setq txt (concat txt " (=>F)")))
13709 (setq org-read-date-overlay
13710 (make-overlay (1- (point-at-eol)) (point-at-eol)))
13711 (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
13713 (defun org-read-date-analyze (ans def defdecode)
13714 "Analyse the combined answer of the date prompt."
13715 ;; FIXME: cleanup and comment
13716 (let ((nowdecode (decode-time (current-time)))
13717 delta deltan deltaw deltadef year month day
13718 hour minute second wday pm h2 m2 tl wday1
13719 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
13720 (setq org-read-date-analyze-futurep nil)
13721 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
13722 (setq ans "+0"))
13724 (when (setq delta (org-read-date-get-relative ans (current-time) def))
13725 (setq ans (replace-match "" t t ans)
13726 deltan (car delta)
13727 deltaw (nth 1 delta)
13728 deltadef (nth 2 delta)))
13730 ;; Check if there is an iso week date in there
13731 ;; If yes, store the info and postpone interpreting it until the rest
13732 ;; of the parsing is done
13733 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
13734 (setq iso-year (if (match-end 1)
13735 (org-small-year-to-year
13736 (string-to-number (match-string 1 ans))))
13737 iso-weekday (if (match-end 3)
13738 (string-to-number (match-string 3 ans)))
13739 iso-week (string-to-number (match-string 2 ans)))
13740 (setq ans (replace-match "" t t ans)))
13742 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
13743 (when (string-match
13744 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
13745 (setq year (if (match-end 2)
13746 (string-to-number (match-string 2 ans))
13747 (progn (setq kill-year t)
13748 (string-to-number (format-time-string "%Y"))))
13749 month (string-to-number (match-string 3 ans))
13750 day (string-to-number (match-string 4 ans)))
13751 (if (< year 100) (setq year (+ 2000 year)))
13752 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
13753 t nil ans)))
13754 ;; Help matching american dates, like 5/30 or 5/30/7
13755 (when (string-match
13756 "^ *\\([0-3]?[0-9]\\)/\\([0-1]?[0-9]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
13757 (setq year (if (match-end 4)
13758 (string-to-number (match-string 4 ans))
13759 (progn (setq kill-year t)
13760 (string-to-number (format-time-string "%Y"))))
13761 month (string-to-number (match-string 1 ans))
13762 day (string-to-number (match-string 2 ans)))
13763 (if (< year 100) (setq year (+ 2000 year)))
13764 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
13765 t nil ans)))
13766 ;; Help matching am/pm times, because `parse-time-string' does not do that.
13767 ;; If there is a time with am/pm, and *no* time without it, we convert
13768 ;; so that matching will be successful.
13769 (loop for i from 1 to 2 do ; twice, for end time as well
13770 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
13771 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
13772 (setq hour (string-to-number (match-string 1 ans))
13773 minute (if (match-end 3)
13774 (string-to-number (match-string 3 ans))
13776 pm (equal ?p
13777 (string-to-char (downcase (match-string 4 ans)))))
13778 (if (and (= hour 12) (not pm))
13779 (setq hour 0)
13780 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
13781 (setq ans (replace-match (format "%02d:%02d" hour minute)
13782 t t ans))))
13784 ;; Check if a time range is given as a duration
13785 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
13786 (setq hour (string-to-number (match-string 1 ans))
13787 h2 (+ hour (string-to-number (match-string 3 ans)))
13788 minute (string-to-number (match-string 2 ans))
13789 m2 (+ minute (if (match-end 5) (string-to-number
13790 (match-string 5 ans))0)))
13791 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
13792 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
13793 t t ans)))
13795 ;; Check if there is a time range
13796 (when (boundp 'org-end-time-was-given)
13797 (setq org-time-was-given nil)
13798 (when (and (string-match org-plain-time-of-day-regexp ans)
13799 (match-end 8))
13800 (setq org-end-time-was-given (match-string 8 ans))
13801 (setq ans (concat (substring ans 0 (match-beginning 7))
13802 (substring ans (match-end 7))))))
13804 (setq tl (parse-time-string ans)
13805 day (or (nth 3 tl) (nth 3 defdecode))
13806 month (or (nth 4 tl)
13807 (if (and org-read-date-prefer-future
13808 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
13809 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
13810 (nth 4 defdecode)))
13811 year (or (and (not kill-year) (nth 5 tl))
13812 (if (and org-read-date-prefer-future
13813 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
13814 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
13815 (nth 5 defdecode)))
13816 hour (or (nth 2 tl) (nth 2 defdecode))
13817 minute (or (nth 1 tl) (nth 1 defdecode))
13818 second (or (nth 0 tl) 0)
13819 wday (nth 6 tl))
13821 (when (and (eq org-read-date-prefer-future 'time)
13822 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
13823 (equal day (nth 3 nowdecode))
13824 (equal month (nth 4 nowdecode))
13825 (equal year (nth 5 nowdecode))
13826 (nth 2 tl)
13827 (or (< (nth 2 tl) (nth 2 nowdecode))
13828 (and (= (nth 2 tl) (nth 2 nowdecode))
13829 (nth 1 tl)
13830 (< (nth 1 tl) (nth 1 nowdecode)))))
13831 (setq day (1+ day)
13832 futurep t))
13834 ;; Special date definitions below
13835 (cond
13836 (iso-week
13837 ;; There was an iso week
13838 (require 'cal-iso)
13839 (setq futurep nil)
13840 (setq year (or iso-year year)
13841 day (or iso-weekday wday 1)
13842 wday nil ; to make sure that the trigger below does not match
13843 iso-date (calendar-gregorian-from-absolute
13844 (calendar-absolute-from-iso
13845 (list iso-week day year))))
13846 ; FIXME: Should we also push ISO weeks into the future?
13847 ; (when (and org-read-date-prefer-future
13848 ; (not iso-year)
13849 ; (< (calendar-absolute-from-gregorian iso-date)
13850 ; (time-to-days (current-time))))
13851 ; (setq year (1+ year)
13852 ; iso-date (calendar-gregorian-from-absolute
13853 ; (calendar-absolute-from-iso
13854 ; (list iso-week day year)))))
13855 (setq month (car iso-date)
13856 year (nth 2 iso-date)
13857 day (nth 1 iso-date)))
13858 (deltan
13859 (setq futurep nil)
13860 (unless deltadef
13861 (let ((now (decode-time (current-time))))
13862 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
13863 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
13864 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
13865 ((equal deltaw "m") (setq month (+ month deltan)))
13866 ((equal deltaw "y") (setq year (+ year deltan)))))
13867 ((and wday (not (nth 3 tl)))
13868 (setq futurep nil)
13869 ;; Weekday was given, but no day, so pick that day in the week
13870 ;; on or after the derived date.
13871 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
13872 (unless (equal wday wday1)
13873 (setq day (+ day (% (- wday wday1 -7) 7))))))
13874 (if (and (boundp 'org-time-was-given)
13875 (nth 2 tl))
13876 (setq org-time-was-given t))
13877 (if (< year 100) (setq year (+ 2000 year)))
13878 (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
13879 (setq org-read-date-analyze-futurep futurep)
13880 (list second minute hour day month year)))
13882 (defvar parse-time-weekdays)
13884 (defun org-read-date-get-relative (s today default)
13885 "Check string S for special relative date string.
13886 TODAY and DEFAULT are internal times, for today and for a default.
13887 Return shift list (N what def-flag)
13888 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
13889 N is the number of WHATs to shift.
13890 DEF-FLAG is t when a double ++ or -- indicates shift relative to
13891 the DEFAULT date rather than TODAY."
13892 (when (and
13893 (string-match
13894 (concat
13895 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
13896 "\\([0-9]+\\)?"
13897 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
13898 "\\([ \t]\\|$\\)") s)
13899 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
13900 (let* ((dir (if (> (match-end 1) (match-beginning 1))
13901 (string-to-char (substring (match-string 1 s) -1))
13902 ?+))
13903 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
13904 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
13905 (what (if (match-end 3) (match-string 3 s) "d"))
13906 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
13907 (date (if rel default today))
13908 (wday (nth 6 (decode-time date)))
13909 delta)
13910 (if wday1
13911 (progn
13912 (setq delta (mod (+ 7 (- wday1 wday)) 7))
13913 (if (= dir ?-) (setq delta (- delta 7)))
13914 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
13915 (list delta "d" rel))
13916 (list (* n (if (= dir ?-) -1 1)) what rel)))))
13918 (defun org-order-calendar-date-args (arg1 arg2 arg3)
13919 "Turn a user-specified date into the internal representation.
13920 The internal representation needed by the calendar is (month day year).
13921 This is a wrapper to handle the brain-dead convention in calendar that
13922 user function argument order change dependent on argument order."
13923 (if (boundp 'calendar-date-style)
13924 (cond
13925 ((eq calendar-date-style 'american)
13926 (list arg1 arg2 arg3))
13927 ((eq calendar-date-style 'european)
13928 (list arg2 arg1 arg3))
13929 ((eq calendar-date-style 'iso)
13930 (list arg2 arg3 arg1)))
13931 (if (org-bound-and-true-p european-calendar-style)
13932 (list arg2 arg1 arg3)
13933 (list arg1 arg2 arg3))))
13935 (defun org-eval-in-calendar (form &optional keepdate)
13936 "Eval FORM in the calendar window and return to current window.
13937 Also, store the cursor date in variable org-ans2."
13938 (let ((sf (selected-frame))
13939 (sw (selected-window)))
13940 (select-window (get-buffer-window "*Calendar*" t))
13941 (eval form)
13942 (when (and (not keepdate) (calendar-cursor-to-date))
13943 (let* ((date (calendar-cursor-to-date))
13944 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13945 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
13946 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
13947 (select-window sw)
13948 (org-select-frame-set-input-focus sf)))
13950 (defun org-calendar-select ()
13951 "Return to `org-read-date' with the date currently selected.
13952 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
13953 (interactive)
13954 (when (calendar-cursor-to-date)
13955 (let* ((date (calendar-cursor-to-date))
13956 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13957 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
13958 (if (active-minibuffer-window) (exit-minibuffer))))
13960 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
13961 "Insert a date stamp for the date given by the internal TIME.
13962 WITH-HM means use the stamp format that includes the time of the day.
13963 INACTIVE means use square brackets instead of angular ones, so that the
13964 stamp will not contribute to the agenda.
13965 PRE and POST are optional strings to be inserted before and after the
13966 stamp.
13967 The command returns the inserted time stamp."
13968 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
13969 stamp)
13970 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
13971 (insert-before-markers (or pre ""))
13972 (insert-before-markers (setq stamp (format-time-string fmt time)))
13973 (when (listp extra)
13974 (setq extra (car extra))
13975 (if (and (stringp extra)
13976 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
13977 (setq extra (format "-%02d:%02d"
13978 (string-to-number (match-string 1 extra))
13979 (string-to-number (match-string 2 extra))))
13980 (setq extra nil)))
13981 (when extra
13982 (backward-char 1)
13983 (insert-before-markers extra)
13984 (forward-char 1))
13985 (insert-before-markers (or post ""))
13986 (setq org-last-inserted-timestamp stamp)))
13988 (defun org-toggle-time-stamp-overlays ()
13989 "Toggle the use of custom time stamp formats."
13990 (interactive)
13991 (setq org-display-custom-times (not org-display-custom-times))
13992 (unless org-display-custom-times
13993 (let ((p (point-min)) (bmp (buffer-modified-p)))
13994 (while (setq p (next-single-property-change p 'display))
13995 (if (and (get-text-property p 'display)
13996 (eq (get-text-property p 'face) 'org-date))
13997 (remove-text-properties
13998 p (setq p (next-single-property-change p 'display))
13999 '(display t))))
14000 (set-buffer-modified-p bmp)))
14001 (if (featurep 'xemacs)
14002 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
14003 (org-restart-font-lock)
14004 (setq org-table-may-need-update t)
14005 (if org-display-custom-times
14006 (message "Time stamps are overlayed with custom format")
14007 (message "Time stamp overlays removed")))
14009 (defun org-display-custom-time (beg end)
14010 "Overlay modified time stamp format over timestamp between BEG and END."
14011 (let* ((ts (buffer-substring beg end))
14012 t1 w1 with-hm tf time str w2 (off 0))
14013 (save-match-data
14014 (setq t1 (org-parse-time-string ts t))
14015 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
14016 (setq off (- (match-end 0) (match-beginning 0)))))
14017 (setq end (- end off))
14018 (setq w1 (- end beg)
14019 with-hm (and (nth 1 t1) (nth 2 t1))
14020 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
14021 time (org-fix-decoded-time t1)
14022 str (org-add-props
14023 (format-time-string
14024 (substring tf 1 -1) (apply 'encode-time time))
14025 nil 'mouse-face 'highlight)
14026 w2 (length str))
14027 (if (not (= w2 w1))
14028 (add-text-properties (1+ beg) (+ 2 beg)
14029 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
14030 (if (featurep 'xemacs)
14031 (progn
14032 (put-text-property beg end 'invisible t)
14033 (put-text-property beg end 'end-glyph (make-glyph str)))
14034 (put-text-property beg end 'display str))))
14036 (defun org-translate-time (string)
14037 "Translate all timestamps in STRING to custom format.
14038 But do this only if the variable `org-display-custom-times' is set."
14039 (when org-display-custom-times
14040 (save-match-data
14041 (let* ((start 0)
14042 (re org-ts-regexp-both)
14043 t1 with-hm inactive tf time str beg end)
14044 (while (setq start (string-match re string start))
14045 (setq beg (match-beginning 0)
14046 end (match-end 0)
14047 t1 (save-match-data
14048 (org-parse-time-string (substring string beg end) t))
14049 with-hm (and (nth 1 t1) (nth 2 t1))
14050 inactive (equal (substring string beg (1+ beg)) "[")
14051 tf (funcall (if with-hm 'cdr 'car)
14052 org-time-stamp-custom-formats)
14053 time (org-fix-decoded-time t1)
14054 str (format-time-string
14055 (concat
14056 (if inactive "[" "<") (substring tf 1 -1)
14057 (if inactive "]" ">"))
14058 (apply 'encode-time time))
14059 string (replace-match str t t string)
14060 start (+ start (length str)))))))
14061 string)
14063 (defun org-fix-decoded-time (time)
14064 "Set 0 instead of nil for the first 6 elements of time.
14065 Don't touch the rest."
14066 (let ((n 0))
14067 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
14069 (defun org-days-to-time (timestamp-string)
14070 "Difference between TIMESTAMP-STRING and now in days."
14071 (- (time-to-days (org-time-string-to-time timestamp-string))
14072 (time-to-days (current-time))))
14074 (defun org-deadline-close (timestamp-string &optional ndays)
14075 "Is the time in TIMESTAMP-STRING close to the current date?"
14076 (setq ndays (or ndays (org-get-wdays timestamp-string)))
14077 (and (< (org-days-to-time timestamp-string) ndays)
14078 (not (org-entry-is-done-p))))
14080 (defun org-get-wdays (ts)
14081 "Get the deadline lead time appropriate for timestring TS."
14082 (cond
14083 ((<= org-deadline-warning-days 0)
14084 ;; 0 or negative, enforce this value no matter what
14085 (- org-deadline-warning-days))
14086 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
14087 ;; lead time is specified.
14088 (floor (* (string-to-number (match-string 1 ts))
14089 (cdr (assoc (match-string 2 ts)
14090 '(("d" . 1) ("w" . 7)
14091 ("m" . 30.4) ("y" . 365.25)))))))
14092 ;; go for the default.
14093 (t org-deadline-warning-days)))
14095 (defun org-calendar-select-mouse (ev)
14096 "Return to `org-read-date' with the date currently selected.
14097 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
14098 (interactive "e")
14099 (mouse-set-point ev)
14100 (when (calendar-cursor-to-date)
14101 (let* ((date (calendar-cursor-to-date))
14102 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
14103 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
14104 (if (active-minibuffer-window) (exit-minibuffer))))
14106 (defun org-check-deadlines (ndays)
14107 "Check if there are any deadlines due or past due.
14108 A deadline is considered due if it happens within `org-deadline-warning-days'
14109 days from today's date. If the deadline appears in an entry marked DONE,
14110 it is not shown. The prefix arg NDAYS can be used to test that many
14111 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
14112 (interactive "P")
14113 (let* ((org-warn-days
14114 (cond
14115 ((equal ndays '(4)) 100000)
14116 (ndays (prefix-numeric-value ndays))
14117 (t (abs org-deadline-warning-days))))
14118 (case-fold-search nil)
14119 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
14120 (callback
14121 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
14123 (message "%d deadlines past-due or due within %d days"
14124 (org-occur regexp nil callback)
14125 org-warn-days)))
14127 (defun org-check-before-date (date)
14128 "Check if there are deadlines or scheduled entries before DATE."
14129 (interactive (list (org-read-date)))
14130 (let ((case-fold-search nil)
14131 (regexp (concat "\\<\\(" org-deadline-string
14132 "\\|" org-scheduled-string
14133 "\\) *<\\([^>]+\\)>"))
14134 (callback
14135 (lambda () (time-less-p
14136 (org-time-string-to-time (match-string 2))
14137 (org-time-string-to-time date)))))
14138 (message "%d entries before %s"
14139 (org-occur regexp nil callback) date)))
14141 (defun org-check-after-date (date)
14142 "Check if there are deadlines or scheduled entries after DATE."
14143 (interactive (list (org-read-date)))
14144 (let ((case-fold-search nil)
14145 (regexp (concat "\\<\\(" org-deadline-string
14146 "\\|" org-scheduled-string
14147 "\\) *<\\([^>]+\\)>"))
14148 (callback
14149 (lambda () (not
14150 (time-less-p
14151 (org-time-string-to-time (match-string 2))
14152 (org-time-string-to-time date))))))
14153 (message "%d entries after %s"
14154 (org-occur regexp nil callback) date)))
14156 (defun org-evaluate-time-range (&optional to-buffer)
14157 "Evaluate a time range by computing the difference between start and end.
14158 Normally the result is just printed in the echo area, but with prefix arg
14159 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
14160 If the time range is actually in a table, the result is inserted into the
14161 next column.
14162 For time difference computation, a year is assumed to be exactly 365
14163 days in order to avoid rounding problems."
14164 (interactive "P")
14166 (org-clock-update-time-maybe)
14167 (save-excursion
14168 (unless (org-at-date-range-p t)
14169 (goto-char (point-at-bol))
14170 (re-search-forward org-tr-regexp-both (point-at-eol) t))
14171 (if (not (org-at-date-range-p t))
14172 (error "Not at a time-stamp range, and none found in current line")))
14173 (let* ((ts1 (match-string 1))
14174 (ts2 (match-string 2))
14175 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
14176 (match-end (match-end 0))
14177 (time1 (org-time-string-to-time ts1))
14178 (time2 (org-time-string-to-time ts2))
14179 (t1 (org-float-time time1))
14180 (t2 (org-float-time time2))
14181 (diff (abs (- t2 t1)))
14182 (negative (< (- t2 t1) 0))
14183 ;; (ys (floor (* 365 24 60 60)))
14184 (ds (* 24 60 60))
14185 (hs (* 60 60))
14186 (fy "%dy %dd %02d:%02d")
14187 (fy1 "%dy %dd")
14188 (fd "%dd %02d:%02d")
14189 (fd1 "%dd")
14190 (fh "%02d:%02d")
14191 y d h m align)
14192 (if havetime
14193 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
14195 d (floor (/ diff ds)) diff (mod diff ds)
14196 h (floor (/ diff hs)) diff (mod diff hs)
14197 m (floor (/ diff 60)))
14198 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
14200 d (floor (+ (/ diff ds) 0.5))
14201 h 0 m 0))
14202 (if (not to-buffer)
14203 (message "%s" (org-make-tdiff-string y d h m))
14204 (if (org-at-table-p)
14205 (progn
14206 (goto-char match-end)
14207 (setq align t)
14208 (and (looking-at " *|") (goto-char (match-end 0))))
14209 (goto-char match-end))
14210 (if (looking-at
14211 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
14212 (replace-match ""))
14213 (if negative (insert " -"))
14214 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
14215 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
14216 (insert " " (format fh h m))))
14217 (if align (org-table-align))
14218 (message "Time difference inserted")))))
14220 (defun org-make-tdiff-string (y d h m)
14221 (let ((fmt "")
14222 (l nil))
14223 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
14224 l (push y l)))
14225 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
14226 l (push d l)))
14227 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
14228 l (push h l)))
14229 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
14230 l (push m l)))
14231 (apply 'format fmt (nreverse l))))
14233 (defun org-time-string-to-time (s)
14234 (apply 'encode-time (org-parse-time-string s)))
14235 (defun org-time-string-to-seconds (s)
14236 (org-float-time (org-time-string-to-time s)))
14238 (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
14239 "Convert a time stamp to an absolute day number.
14240 If there is a specifyer for a cyclic time stamp, get the closest date to
14241 DAYNR.
14242 PREFER and SHOW-ALL are passed through to `org-closest-date'.
14243 the variable date is bound by the calendar when this is called."
14244 (cond
14245 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
14246 (if (org-diary-sexp-entry (match-string 1 s) "" date)
14247 daynr
14248 (+ daynr 1000)))
14249 ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
14250 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
14251 (time-to-days (current-time))) (match-string 0 s)
14252 prefer show-all))
14253 (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
14255 (defun org-days-to-iso-week (days)
14256 "Return the iso week number."
14257 (require 'cal-iso)
14258 (car (calendar-iso-from-absolute days)))
14260 (defun org-small-year-to-year (year)
14261 "Convert 2-digit years into 4-digit years.
14262 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
14263 The year 2000 cannot be abbreviated. Any year larger than 99
14264 is returned unchanged."
14265 (if (< year 38)
14266 (setq year (+ 2000 year))
14267 (if (< year 100)
14268 (setq year (+ 1900 year))))
14269 year)
14271 (defun org-time-from-absolute (d)
14272 "Return the time corresponding to date D.
14273 D may be an absolute day number, or a calendar-type list (month day year)."
14274 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
14275 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
14277 (defun org-calendar-holiday ()
14278 "List of holidays, for Diary display in Org-mode."
14279 (require 'holidays)
14280 (let ((hl (funcall
14281 (if (fboundp 'calendar-check-holidays)
14282 'calendar-check-holidays 'check-calendar-holidays) date)))
14283 (if hl (mapconcat 'identity hl "; "))))
14285 (defun org-diary-sexp-entry (sexp entry date)
14286 "Process a SEXP diary ENTRY for DATE."
14287 (require 'diary-lib)
14288 (let ((result (if calendar-debug-sexp
14289 (let ((stack-trace-on-error t))
14290 (eval (car (read-from-string sexp))))
14291 (condition-case nil
14292 (eval (car (read-from-string sexp)))
14293 (error
14294 (beep)
14295 (message "Bad sexp at line %d in %s: %s"
14296 (org-current-line)
14297 (buffer-file-name) sexp)
14298 (sleep-for 2))))))
14299 (cond ((stringp result) result)
14300 ((and (consp result)
14301 (stringp (cdr result))) (cdr result))
14302 (result entry)
14303 (t nil))))
14305 (defun org-diary-to-ical-string (frombuf)
14306 "Get iCalendar entries from diary entries in buffer FROMBUF.
14307 This uses the icalendar.el library."
14308 (let* ((tmpdir (if (featurep 'xemacs)
14309 (temp-directory)
14310 temporary-file-directory))
14311 (tmpfile (make-temp-name
14312 (expand-file-name "orgics" tmpdir)))
14313 buf rtn b e)
14314 (with-current-buffer frombuf
14315 (icalendar-export-region (point-min) (point-max) tmpfile)
14316 (setq buf (find-buffer-visiting tmpfile))
14317 (set-buffer buf)
14318 (goto-char (point-min))
14319 (if (re-search-forward "^BEGIN:VEVENT" nil t)
14320 (setq b (match-beginning 0)))
14321 (goto-char (point-max))
14322 (if (re-search-backward "^END:VEVENT" nil t)
14323 (setq e (match-end 0)))
14324 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
14325 (kill-buffer buf)
14326 (delete-file tmpfile)
14327 rtn))
14329 (defun org-closest-date (start current change prefer show-all)
14330 "Find the date closest to CURRENT that is consistent with START and CHANGE.
14331 When PREFER is `past' return a date that is either CURRENT or past.
14332 When PREFER is `future', return a date that is either CURRENT or future.
14333 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
14334 ;; Make the proper lists from the dates
14335 (catch 'exit
14336 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
14337 dn dw sday cday n1 n2 n0
14338 d m y y1 y2 date1 date2 nmonths nm ny m2)
14340 (setq start (org-date-to-gregorian start)
14341 current (org-date-to-gregorian
14342 (if show-all
14343 current
14344 (time-to-days (current-time))))
14345 sday (calendar-absolute-from-gregorian start)
14346 cday (calendar-absolute-from-gregorian current))
14348 (if (<= cday sday) (throw 'exit sday))
14350 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
14351 (setq dn (string-to-number (match-string 1 change))
14352 dw (cdr (assoc (match-string 2 change) a1)))
14353 (error "Invalid change specifyer: %s" change))
14354 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
14355 (cond
14356 ((eq dw 'day)
14357 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
14358 n2 (+ n1 dn)))
14359 ((eq dw 'year)
14360 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
14361 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
14362 (setq date1 (list m d y1)
14363 n1 (calendar-absolute-from-gregorian date1)
14364 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
14365 n2 (calendar-absolute-from-gregorian date2)))
14366 ((eq dw 'month)
14367 ;; approx number of month between the two dates
14368 (setq nmonths (floor (/ (- cday sday) 30.436875)))
14369 ;; How often does dn fit in there?
14370 (setq d (nth 1 start) m (car start) y (nth 2 start)
14371 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
14372 m (+ m nm)
14373 ny (floor (/ m 12))
14374 y (+ y ny)
14375 m (- m (* ny 12)))
14376 (while (> m 12) (setq m (- m 12) y (1+ y)))
14377 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
14378 (setq m2 (+ m dn) y2 y)
14379 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
14380 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
14381 (while (<= n2 cday)
14382 (setq n1 n2 m m2 y y2)
14383 (setq m2 (+ m dn) y2 y)
14384 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
14385 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
14386 ;; Make sure n1 is the earlier date
14387 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
14388 (if show-all
14389 (cond
14390 ((eq prefer 'past) (if (= cday n2) n2 n1))
14391 ((eq prefer 'future) (if (= cday n1) n1 n2))
14392 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
14393 (cond
14394 ((eq prefer 'past) (if (= cday n2) n2 n1))
14395 ((eq prefer 'future) (if (= cday n1) n1 n2))
14396 (t (if (= cday n1) n1 n2)))))))
14398 (defun org-date-to-gregorian (date)
14399 "Turn any specification of DATE into a gregorian date for the calendar."
14400 (cond ((integerp date) (calendar-gregorian-from-absolute date))
14401 ((and (listp date) (= (length date) 3)) date)
14402 ((stringp date)
14403 (setq date (org-parse-time-string date))
14404 (list (nth 4 date) (nth 3 date) (nth 5 date)))
14405 ((listp date)
14406 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
14408 (defun org-parse-time-string (s &optional nodefault)
14409 "Parse the standard Org-mode time string.
14410 This should be a lot faster than the normal `parse-time-string'.
14411 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
14412 hour and minute fields will be nil if not given."
14413 (if (string-match org-ts-regexp0 s)
14414 (list 0
14415 (if (or (match-beginning 8) (not nodefault))
14416 (string-to-number (or (match-string 8 s) "0")))
14417 (if (or (match-beginning 7) (not nodefault))
14418 (string-to-number (or (match-string 7 s) "0")))
14419 (string-to-number (match-string 4 s))
14420 (string-to-number (match-string 3 s))
14421 (string-to-number (match-string 2 s))
14422 nil nil nil)
14423 (error "Not a standard Org-mode time string: %s" s)))
14425 (defun org-timestamp-up (&optional arg)
14426 "Increase the date item at the cursor by one.
14427 If the cursor is on the year, change the year. If it is on the month or
14428 the day, change that.
14429 With prefix ARG, change by that many units."
14430 (interactive "p")
14431 (org-timestamp-change (prefix-numeric-value arg)))
14433 (defun org-timestamp-down (&optional arg)
14434 "Decrease the date item at the cursor by one.
14435 If the cursor is on the year, change the year. If it is on the month or
14436 the day, change that.
14437 With prefix ARG, change by that many units."
14438 (interactive "p")
14439 (org-timestamp-change (- (prefix-numeric-value arg))))
14441 (defun org-timestamp-up-day (&optional arg)
14442 "Increase the date in the time stamp by one day.
14443 With prefix ARG, change that many days."
14444 (interactive "p")
14445 (if (and (not (org-at-timestamp-p t))
14446 (org-on-heading-p))
14447 (org-todo 'up)
14448 (org-timestamp-change (prefix-numeric-value arg) 'day)))
14450 (defun org-timestamp-down-day (&optional arg)
14451 "Decrease the date in the time stamp by one day.
14452 With prefix ARG, change that many days."
14453 (interactive "p")
14454 (if (and (not (org-at-timestamp-p t))
14455 (org-on-heading-p))
14456 (org-todo 'down)
14457 (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
14459 (defun org-at-timestamp-p (&optional inactive-ok)
14460 "Determine if the cursor is in or at a timestamp."
14461 (interactive)
14462 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
14463 (pos (point))
14464 (ans (or (looking-at tsr)
14465 (save-excursion
14466 (skip-chars-backward "^[<\n\r\t")
14467 (if (> (point) (point-min)) (backward-char 1))
14468 (and (looking-at tsr)
14469 (> (- (match-end 0) pos) -1))))))
14470 (and ans
14471 (boundp 'org-ts-what)
14472 (setq org-ts-what
14473 (cond
14474 ((= pos (match-beginning 0)) 'bracket)
14475 ((= pos (1- (match-end 0))) 'bracket)
14476 ((org-pos-in-match-range pos 2) 'year)
14477 ((org-pos-in-match-range pos 3) 'month)
14478 ((org-pos-in-match-range pos 7) 'hour)
14479 ((org-pos-in-match-range pos 8) 'minute)
14480 ((or (org-pos-in-match-range pos 4)
14481 (org-pos-in-match-range pos 5)) 'day)
14482 ((and (> pos (or (match-end 8) (match-end 5)))
14483 (< pos (match-end 0)))
14484 (- pos (or (match-end 8) (match-end 5))))
14485 (t 'day))))
14486 ans))
14488 (defun org-toggle-timestamp-type ()
14489 "Toggle the type (<active> or [inactive]) of a time stamp."
14490 (interactive)
14491 (when (org-at-timestamp-p t)
14492 (let ((beg (match-beginning 0)) (end (match-end 0))
14493 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
14494 (save-excursion
14495 (goto-char beg)
14496 (while (re-search-forward "[][<>]" end t)
14497 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
14498 t t)))
14499 (message "Timestamp is now %sactive"
14500 (if (equal (char-after beg) ?<) "" "in")))))
14502 (defun org-timestamp-change (n &optional what)
14503 "Change the date in the time stamp at point.
14504 The date will be changed by N times WHAT. WHAT can be `day', `month',
14505 `year', `minute', `second'. If WHAT is not given, the cursor position
14506 in the timestamp determines what will be changed."
14507 (let ((pos (point))
14508 with-hm inactive
14509 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
14510 org-ts-what
14511 extra rem
14512 ts time time0)
14513 (if (not (org-at-timestamp-p t))
14514 (error "Not at a timestamp"))
14515 (if (and (not what) (eq org-ts-what 'bracket))
14516 (org-toggle-timestamp-type)
14517 (if (and (not what) (not (eq org-ts-what 'day))
14518 org-display-custom-times
14519 (get-text-property (point) 'display)
14520 (not (get-text-property (1- (point)) 'display)))
14521 (setq org-ts-what 'day))
14522 (setq org-ts-what (or what org-ts-what)
14523 inactive (= (char-after (match-beginning 0)) ?\[)
14524 ts (match-string 0))
14525 (replace-match "")
14526 (if (string-match
14527 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
14529 (setq extra (match-string 1 ts)))
14530 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
14531 (setq with-hm t))
14532 (setq time0 (org-parse-time-string ts))
14533 (when (and (eq org-ts-what 'minute)
14534 (eq current-prefix-arg nil))
14535 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
14536 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
14537 (setcar (cdr time0) (+ (nth 1 time0)
14538 (if (> n 0) (- rem) (- dm rem))))))
14539 (setq time
14540 (encode-time (or (car time0) 0)
14541 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
14542 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
14543 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
14544 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
14545 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
14546 (nthcdr 6 time0)))
14547 (when (and (member org-ts-what '(hour minute))
14548 extra
14549 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
14550 (setq extra (org-modify-ts-extra
14551 extra
14552 (if (eq org-ts-what 'hour) 2 5)
14553 n dm)))
14554 (when (integerp org-ts-what)
14555 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
14556 (if (eq what 'calendar)
14557 (let ((cal-date (org-get-date-from-calendar)))
14558 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
14559 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
14560 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
14561 (setcar time0 (or (car time0) 0))
14562 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
14563 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
14564 (setq time (apply 'encode-time time0))))
14565 (setq org-last-changed-timestamp
14566 (org-insert-time-stamp time with-hm inactive nil nil extra))
14567 (org-clock-update-time-maybe)
14568 (goto-char pos)
14569 ;; Try to recenter the calendar window, if any
14570 (if (and org-calendar-follow-timestamp-change
14571 (get-buffer-window "*Calendar*" t)
14572 (memq org-ts-what '(day month year)))
14573 (org-recenter-calendar (time-to-days time))))))
14575 (defun org-modify-ts-extra (s pos n dm)
14576 "Change the different parts of the lead-time and repeat fields in timestamp."
14577 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
14578 ng h m new rem)
14579 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
14580 (cond
14581 ((or (org-pos-in-match-range pos 2)
14582 (org-pos-in-match-range pos 3))
14583 (setq m (string-to-number (match-string 3 s))
14584 h (string-to-number (match-string 2 s)))
14585 (if (org-pos-in-match-range pos 2)
14586 (setq h (+ h n))
14587 (setq n (* dm (org-no-warnings (signum n))))
14588 (when (not (= 0 (setq rem (% m dm))))
14589 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
14590 (setq m (+ m n)))
14591 (if (< m 0) (setq m (+ m 60) h (1- h)))
14592 (if (> m 59) (setq m (- m 60) h (1+ h)))
14593 (setq h (min 24 (max 0 h)))
14594 (setq ng 1 new (format "-%02d:%02d" h m)))
14595 ((org-pos-in-match-range pos 6)
14596 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
14597 ((org-pos-in-match-range pos 5)
14598 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
14600 ((org-pos-in-match-range pos 9)
14601 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
14602 ((org-pos-in-match-range pos 8)
14603 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
14605 (when ng
14606 (setq s (concat
14607 (substring s 0 (match-beginning ng))
14609 (substring s (match-end ng))))))
14612 (defun org-recenter-calendar (date)
14613 "If the calendar is visible, recenter it to DATE."
14614 (let* ((win (selected-window))
14615 (cwin (get-buffer-window "*Calendar*" t))
14616 (calendar-move-hook nil))
14617 (when cwin
14618 (select-window cwin)
14619 (calendar-goto-date (if (listp date) date
14620 (calendar-gregorian-from-absolute date)))
14621 (select-window win))))
14623 (defun org-goto-calendar (&optional arg)
14624 "Go to the Emacs calendar at the current date.
14625 If there is a time stamp in the current line, go to that date.
14626 A prefix ARG can be used to force the current date."
14627 (interactive "P")
14628 (let ((tsr org-ts-regexp) diff
14629 (calendar-move-hook nil)
14630 (calendar-view-holidays-initially-flag nil)
14631 (calendar-view-diary-initially-flag nil))
14632 (if (or (org-at-timestamp-p)
14633 (save-excursion
14634 (beginning-of-line 1)
14635 (looking-at (concat ".*" tsr))))
14636 (let ((d1 (time-to-days (current-time)))
14637 (d2 (time-to-days
14638 (org-time-string-to-time (match-string 1)))))
14639 (setq diff (- d2 d1))))
14640 (calendar)
14641 (calendar-goto-today)
14642 (if (and diff (not arg)) (calendar-forward-day diff))))
14644 (defun org-get-date-from-calendar ()
14645 "Return a list (month day year) of date at point in calendar."
14646 (with-current-buffer "*Calendar*"
14647 (save-match-data
14648 (calendar-cursor-to-date))))
14650 (defun org-date-from-calendar ()
14651 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
14652 If there is already a time stamp at the cursor position, update it."
14653 (interactive)
14654 (if (org-at-timestamp-p t)
14655 (org-timestamp-change 0 'calendar)
14656 (let ((cal-date (org-get-date-from-calendar)))
14657 (org-insert-time-stamp
14658 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
14660 (defun org-minutes-to-hh:mm-string (m)
14661 "Compute H:MM from a number of minutes."
14662 (let ((h (/ m 60)))
14663 (setq m (- m (* 60 h)))
14664 (format org-time-clocksum-format h m)))
14666 (defun org-hh:mm-string-to-minutes (s)
14667 "Convert a string H:MM to a number of minutes.
14668 If the string is just a number, interpret it as minutes.
14669 In fact, the first hh:mm or number in the string will be taken,
14670 there can be extra stuff in the string.
14671 If no number is found, the return value is 0."
14672 (cond
14673 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
14674 (+ (* (string-to-number (match-string 1 s)) 60)
14675 (string-to-number (match-string 2 s))))
14676 ((string-match "\\([0-9]+\\)" s)
14677 (string-to-number (match-string 1 s)))
14678 (t 0)))
14680 ;;;; Files
14682 (defun org-save-all-org-buffers ()
14683 "Save all Org-mode buffers without user confirmation."
14684 (interactive)
14685 (message "Saving all Org-mode buffers...")
14686 (save-some-buffers t 'org-mode-p)
14687 (when (featurep 'org-id) (org-id-locations-save))
14688 (message "Saving all Org-mode buffers... done"))
14690 (defun org-revert-all-org-buffers ()
14691 "Revert all Org-mode buffers.
14692 Prompt for confirmation when there are unsaved changes.
14693 Be sure you know what you are doing before letting this function
14694 overwrite your changes.
14696 This function is useful in a setup where one tracks org files
14697 with a version control system, to revert on one machine after pulling
14698 changes from another. I believe the procedure must be like this:
14700 1. M-x org-save-all-org-buffers
14701 2. Pull changes from the other machine, resolve conflicts
14702 3. M-x org-revert-all-org-buffers"
14703 (interactive)
14704 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
14705 (error "Abort"))
14706 (save-excursion
14707 (save-window-excursion
14708 (mapc
14709 (lambda (b)
14710 (when (and (with-current-buffer b (org-mode-p))
14711 (with-current-buffer b buffer-file-name))
14712 (switch-to-buffer b)
14713 (revert-buffer t 'no-confirm)))
14714 (buffer-list))
14715 (when (and (featurep 'org-id) org-id-track-globally)
14716 (org-id-locations-load)))))
14718 ;;;; Agenda files
14720 ;;;###autoload
14721 (defun org-iswitchb (&optional arg)
14722 "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
14723 With a prefix argument, restrict available to files.
14724 With two prefix arguments, restrict available buffers to agenda files."
14725 (interactive "P")
14726 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
14727 ((equal arg '(16)) (org-buffer-list 'agenda))
14728 (t (org-buffer-list)))))
14729 (switch-to-buffer
14730 (org-icompleting-read "Org buffer: "
14731 (mapcar 'list (mapcar 'buffer-name blist))
14732 nil t))))
14734 ;;;###autoload
14735 (defalias 'org-ido-switchb 'org-iswitchb)
14737 (defun org-buffer-list (&optional predicate exclude-tmp)
14738 "Return a list of Org buffers.
14739 PREDICATE can be `export', `files' or `agenda'.
14741 export restrict the list to Export buffers.
14742 files restrict the list to buffers visiting Org files.
14743 agenda restrict the list to buffers visiting agenda files.
14745 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
14746 (let* ((bfn nil)
14747 (agenda-files (and (eq predicate 'agenda)
14748 (mapcar 'file-truename (org-agenda-files t))))
14749 (filter
14750 (cond
14751 ((eq predicate 'files)
14752 (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
14753 ((eq predicate 'export)
14754 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
14755 ((eq predicate 'agenda)
14756 (lambda (b)
14757 (with-current-buffer b
14758 (and (eq major-mode 'org-mode)
14759 (setq bfn (buffer-file-name b))
14760 (member (file-truename bfn) agenda-files)))))
14761 (t (lambda (b) (with-current-buffer b
14762 (or (eq major-mode 'org-mode)
14763 (string-match "\*Org .*Export"
14764 (buffer-name b)))))))))
14765 (delq nil
14766 (mapcar
14767 (lambda(b)
14768 (if (and (funcall filter b)
14769 (or (not exclude-tmp)
14770 (not (string-match "tmp" (buffer-name b)))))
14772 nil))
14773 (buffer-list)))))
14775 (defun org-agenda-files (&optional unrestricted archives)
14776 "Get the list of agenda files.
14777 Optional UNRESTRICTED means return the full list even if a restriction
14778 is currently in place.
14779 When ARCHIVES is t, include all archive files that are really being
14780 used by the agenda files. If ARCHIVE is `ifmode', do this only if
14781 `org-agenda-archives-mode' is t."
14782 (let ((files
14783 (cond
14784 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
14785 ((stringp org-agenda-files) (org-read-agenda-file-list))
14786 ((listp org-agenda-files) org-agenda-files)
14787 (t (error "Invalid value of `org-agenda-files'")))))
14788 (setq files (apply 'append
14789 (mapcar (lambda (f)
14790 (if (file-directory-p f)
14791 (directory-files
14792 f t org-agenda-file-regexp)
14793 (list f)))
14794 files)))
14795 (when org-agenda-skip-unavailable-files
14796 (setq files (delq nil
14797 (mapcar (function
14798 (lambda (file)
14799 (and (file-readable-p file) file)))
14800 files))))
14801 (when (or (eq archives t)
14802 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
14803 (setq files (org-add-archive-files files)))
14804 files))
14806 (defun org-edit-agenda-file-list ()
14807 "Edit the list of agenda files.
14808 Depending on setup, this either uses customize to edit the variable
14809 `org-agenda-files', or it visits the file that is holding the list. In the
14810 latter case, the buffer is set up in a way that saving it automatically kills
14811 the buffer and restores the previous window configuration."
14812 (interactive)
14813 (if (stringp org-agenda-files)
14814 (let ((cw (current-window-configuration)))
14815 (find-file org-agenda-files)
14816 (org-set-local 'org-window-configuration cw)
14817 (org-add-hook 'after-save-hook
14818 (lambda ()
14819 (set-window-configuration
14820 (prog1 org-window-configuration
14821 (kill-buffer (current-buffer))))
14822 (org-install-agenda-files-menu)
14823 (message "New agenda file list installed"))
14824 nil 'local)
14825 (message "%s" (substitute-command-keys
14826 "Edit list and finish with \\[save-buffer]")))
14827 (customize-variable 'org-agenda-files)))
14829 (defun org-store-new-agenda-file-list (list)
14830 "Set new value for the agenda file list and save it correctly."
14831 (if (stringp org-agenda-files)
14832 (let ((fe (org-read-agenda-file-list t)) b u)
14833 (while (setq b (find-buffer-visiting org-agenda-files))
14834 (kill-buffer b))
14835 (with-temp-file org-agenda-files
14836 (insert
14837 (mapconcat
14838 (lambda (f) ;; Keep un-expanded entries.
14839 (if (setq u (assoc f fe))
14840 (cdr u)
14842 list "\n")
14843 "\n")))
14844 (let ((org-mode-hook nil) (org-inhibit-startup t)
14845 (org-insert-mode-line-in-empty-file nil))
14846 (setq org-agenda-files list)
14847 (customize-save-variable 'org-agenda-files org-agenda-files))))
14849 (defun org-read-agenda-file-list (&optional pair-with-expansion)
14850 "Read the list of agenda files from a file.
14851 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
14852 filenames, used by `org-store-new-agenda-file-list' to write back
14853 un-expanded file names."
14854 (when (file-directory-p org-agenda-files)
14855 (error "`org-agenda-files' cannot be a single directory"))
14856 (when (stringp org-agenda-files)
14857 (with-temp-buffer
14858 (insert-file-contents org-agenda-files)
14859 (mapcar
14860 (lambda (f)
14861 (let ((e (expand-file-name (substitute-in-file-name f)
14862 org-directory)))
14863 (if pair-with-expansion
14864 (cons e f)
14865 e)))
14866 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
14868 ;;;###autoload
14869 (defun org-cycle-agenda-files ()
14870 "Cycle through the files in `org-agenda-files'.
14871 If the current buffer visits an agenda file, find the next one in the list.
14872 If the current buffer does not, find the first agenda file."
14873 (interactive)
14874 (let* ((fs (org-agenda-files t))
14875 (files (append fs (list (car fs))))
14876 (tcf (if buffer-file-name (file-truename buffer-file-name)))
14877 file)
14878 (unless files (error "No agenda files"))
14879 (catch 'exit
14880 (while (setq file (pop files))
14881 (if (equal (file-truename file) tcf)
14882 (when (car files)
14883 (find-file (car files))
14884 (throw 'exit t))))
14885 (find-file (car fs)))
14886 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
14888 (defun org-agenda-file-to-front (&optional to-end)
14889 "Move/add the current file to the top of the agenda file list.
14890 If the file is not present in the list, it is added to the front. If it is
14891 present, it is moved there. With optional argument TO-END, add/move to the
14892 end of the list."
14893 (interactive "P")
14894 (let ((org-agenda-skip-unavailable-files nil)
14895 (file-alist (mapcar (lambda (x)
14896 (cons (file-truename x) x))
14897 (org-agenda-files t)))
14898 (ctf (file-truename buffer-file-name))
14899 x had)
14900 (setq x (assoc ctf file-alist) had x)
14902 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
14903 (if to-end
14904 (setq file-alist (append (delq x file-alist) (list x)))
14905 (setq file-alist (cons x (delq x file-alist))))
14906 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
14907 (org-install-agenda-files-menu)
14908 (message "File %s to %s of agenda file list"
14909 (if had "moved" "added") (if to-end "end" "front"))))
14911 (defun org-remove-file (&optional file)
14912 "Remove current file from the list of files in variable `org-agenda-files'.
14913 These are the files which are being checked for agenda entries.
14914 Optional argument FILE means use this file instead of the current."
14915 (interactive)
14916 (let* ((org-agenda-skip-unavailable-files nil)
14917 (file (or file buffer-file-name))
14918 (true-file (file-truename file))
14919 (afile (abbreviate-file-name file))
14920 (files (delq nil (mapcar
14921 (lambda (x)
14922 (if (equal true-file
14923 (file-truename x))
14924 nil x))
14925 (org-agenda-files t)))))
14926 (if (not (= (length files) (length (org-agenda-files t))))
14927 (progn
14928 (org-store-new-agenda-file-list files)
14929 (org-install-agenda-files-menu)
14930 (message "Removed file: %s" afile))
14931 (message "File was not in list: %s (not removed)" afile))))
14933 (defun org-file-menu-entry (file)
14934 (vector file (list 'find-file file) t))
14936 (defun org-check-agenda-file (file)
14937 "Make sure FILE exists. If not, ask user what to do."
14938 (when (not (file-exists-p file))
14939 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
14940 (abbreviate-file-name file))
14941 (let ((r (downcase (read-char-exclusive))))
14942 (cond
14943 ((equal r ?r)
14944 (org-remove-file file)
14945 (throw 'nextfile t))
14946 (t (error "Abort"))))))
14948 (defun org-get-agenda-file-buffer (file)
14949 "Get a buffer visiting FILE. If the buffer needs to be created, add
14950 it to the list of buffers which might be released later."
14951 (let ((buf (org-find-base-buffer-visiting file)))
14952 (if buf
14953 buf ; just return it
14954 ;; Make a new buffer and remember it
14955 (setq buf (find-file-noselect file))
14956 (if buf (push buf org-agenda-new-buffers))
14957 buf)))
14959 (defun org-release-buffers (blist)
14960 "Release all buffers in list, asking the user for confirmation when needed.
14961 When a buffer is unmodified, it is just killed. When modified, it is saved
14962 \(if the user agrees) and then killed."
14963 (let (buf file)
14964 (while (setq buf (pop blist))
14965 (setq file (buffer-file-name buf))
14966 (when (and (buffer-modified-p buf)
14967 file
14968 (y-or-n-p (format "Save file %s? " file)))
14969 (with-current-buffer buf (save-buffer)))
14970 (kill-buffer buf))))
14972 (defun org-prepare-agenda-buffers (files)
14973 "Create buffers for all agenda files, protect archived trees and comments."
14974 (interactive)
14975 (let ((pa '(:org-archived t))
14976 (pc '(:org-comment t))
14977 (pall '(:org-archived t :org-comment t))
14978 (inhibit-read-only t)
14979 (rea (concat ":" org-archive-tag ":"))
14980 bmp file re)
14981 (save-excursion
14982 (save-restriction
14983 (while (setq file (pop files))
14984 (catch 'nextfile
14985 (if (bufferp file)
14986 (set-buffer file)
14987 (org-check-agenda-file file)
14988 (set-buffer (org-get-agenda-file-buffer file)))
14989 (widen)
14990 (setq bmp (buffer-modified-p))
14991 (org-refresh-category-properties)
14992 (setq org-todo-keywords-for-agenda
14993 (append org-todo-keywords-for-agenda org-todo-keywords-1))
14994 (setq org-done-keywords-for-agenda
14995 (append org-done-keywords-for-agenda org-done-keywords))
14996 (setq org-todo-keyword-alist-for-agenda
14997 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
14998 (setq org-drawers-for-agenda
14999 (append org-drawers-for-agenda org-drawers))
15000 (setq org-tag-alist-for-agenda
15001 (append org-tag-alist-for-agenda org-tag-alist))
15003 (save-excursion
15004 (remove-text-properties (point-min) (point-max) pall)
15005 (when org-agenda-skip-archived-trees
15006 (goto-char (point-min))
15007 (while (re-search-forward rea nil t)
15008 (if (org-on-heading-p t)
15009 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
15010 (goto-char (point-min))
15011 (setq re (concat "^\\*+ +" org-comment-string "\\>"))
15012 (while (re-search-forward re nil t)
15013 (add-text-properties
15014 (match-beginning 0) (org-end-of-subtree t) pc)))
15015 (set-buffer-modified-p bmp)))))
15016 (setq org-todo-keywords-for-agenda
15017 (org-uniquify org-todo-keywords-for-agenda))
15018 (setq org-todo-keyword-alist-for-agenda
15019 (org-uniquify org-todo-keyword-alist-for-agenda)
15020 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
15022 ;;;; Embedded LaTeX
15024 (defvar org-cdlatex-mode-map (make-sparse-keymap)
15025 "Keymap for the minor `org-cdlatex-mode'.")
15027 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
15028 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
15029 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
15030 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
15031 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
15033 (defvar org-cdlatex-texmathp-advice-is-done nil
15034 "Flag remembering if we have applied the advice to texmathp already.")
15036 (define-minor-mode org-cdlatex-mode
15037 "Toggle the minor `org-cdlatex-mode'.
15038 This mode supports entering LaTeX environment and math in LaTeX fragments
15039 in Org-mode.
15040 \\{org-cdlatex-mode-map}"
15041 nil " OCDL" nil
15042 (when org-cdlatex-mode (require 'cdlatex))
15043 (unless org-cdlatex-texmathp-advice-is-done
15044 (setq org-cdlatex-texmathp-advice-is-done t)
15045 (defadvice texmathp (around org-math-always-on activate)
15046 "Always return t in org-mode buffers.
15047 This is because we want to insert math symbols without dollars even outside
15048 the LaTeX math segments. If Orgmode thinks that point is actually inside
15049 an embedded LaTeX fragment, let texmathp do its job.
15050 \\[org-cdlatex-mode-map]"
15051 (interactive)
15052 (let (p)
15053 (cond
15054 ((not (org-mode-p)) ad-do-it)
15055 ((eq this-command 'cdlatex-math-symbol)
15056 (setq ad-return-value t
15057 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
15059 (let ((p (org-inside-LaTeX-fragment-p)))
15060 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
15061 (setq ad-return-value t
15062 texmathp-why '("Org-mode embedded math" . 0))
15063 (if p ad-do-it)))))))))
15065 (defun turn-on-org-cdlatex ()
15066 "Unconditionally turn on `org-cdlatex-mode'."
15067 (org-cdlatex-mode 1))
15069 (defun org-inside-LaTeX-fragment-p ()
15070 "Test if point is inside a LaTeX fragment.
15071 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
15072 sequence appearing also before point.
15073 Even though the matchers for math are configurable, this function assumes
15074 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
15075 delimiters are skipped when they have been removed by customization.
15076 The return value is nil, or a cons cell with the delimiter and
15077 and the position of this delimiter.
15079 This function does a reasonably good job, but can locally be fooled by
15080 for example currency specifications. For example it will assume being in
15081 inline math after \"$22.34\". The LaTeX fragment formatter will only format
15082 fragments that are properly closed, but during editing, we have to live
15083 with the uncertainty caused by missing closing delimiters. This function
15084 looks only before point, not after."
15085 (catch 'exit
15086 (let ((pos (point))
15087 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
15088 (lim (progn
15089 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
15090 (point)))
15091 dd-on str (start 0) m re)
15092 (goto-char pos)
15093 (when dodollar
15094 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
15095 re (nth 1 (assoc "$" org-latex-regexps)))
15096 (while (string-match re str start)
15097 (cond
15098 ((= (match-end 0) (length str))
15099 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
15100 ((= (match-end 0) (- (length str) 5))
15101 (throw 'exit nil))
15102 (t (setq start (match-end 0))))))
15103 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
15104 (goto-char pos)
15105 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
15106 (and (match-beginning 2) (throw 'exit nil))
15107 ;; count $$
15108 (while (re-search-backward "\\$\\$" lim t)
15109 (setq dd-on (not dd-on)))
15110 (goto-char pos)
15111 (if dd-on (cons "$$" m))))))
15113 (defun org-inside-latex-macro-p ()
15114 "Is point inside a LaTeX macro or its arguments?"
15115 (save-match-data
15116 (org-in-regexp
15117 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
15119 (defun test ()
15120 (interactive)
15121 (message "%s" (org-inside-latex-macro-p)))
15123 (defun org-try-cdlatex-tab ()
15124 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
15125 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
15126 - inside a LaTeX fragment, or
15127 - after the first word in a line, where an abbreviation expansion could
15128 insert a LaTeX environment."
15129 (when org-cdlatex-mode
15130 (cond
15131 ((save-excursion
15132 (skip-chars-backward "a-zA-Z0-9*")
15133 (skip-chars-backward " \t")
15134 (bolp))
15135 (cdlatex-tab) t)
15136 ((org-inside-LaTeX-fragment-p)
15137 (cdlatex-tab) t)
15138 (t nil))))
15140 (defun org-cdlatex-underscore-caret (&optional arg)
15141 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
15142 Revert to the normal definition outside of these fragments."
15143 (interactive "P")
15144 (if (org-inside-LaTeX-fragment-p)
15145 (call-interactively 'cdlatex-sub-superscript)
15146 (let (org-cdlatex-mode)
15147 (call-interactively (key-binding (vector last-input-event))))))
15149 (defun org-cdlatex-math-modify (&optional arg)
15150 "Execute `cdlatex-math-modify' in LaTeX fragments.
15151 Revert to the normal definition outside of these fragments."
15152 (interactive "P")
15153 (if (org-inside-LaTeX-fragment-p)
15154 (call-interactively 'cdlatex-math-modify)
15155 (let (org-cdlatex-mode)
15156 (call-interactively (key-binding (vector last-input-event))))))
15158 (defvar org-latex-fragment-image-overlays nil
15159 "List of overlays carrying the images of latex fragments.")
15160 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
15162 (defun org-remove-latex-fragment-image-overlays ()
15163 "Remove all overlays with LaTeX fragment images in current buffer."
15164 (mapc 'delete-overlay org-latex-fragment-image-overlays)
15165 (setq org-latex-fragment-image-overlays nil))
15167 (defun org-preview-latex-fragment (&optional subtree)
15168 "Preview the LaTeX fragment at point, or all locally or globally.
15169 If the cursor is in a LaTeX fragment, create the image and overlay
15170 it over the source code. If there is no fragment at point, display
15171 all fragments in the current text, from one headline to the next. With
15172 prefix SUBTREE, display all fragments in the current subtree. With a
15173 double prefix `C-u C-u', or when the cursor is before the first headline,
15174 display all fragments in the buffer.
15175 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
15176 (interactive "P")
15177 (org-remove-latex-fragment-image-overlays)
15178 (save-excursion
15179 (save-restriction
15180 (let (beg end at msg)
15181 (cond
15182 ((or (equal subtree '(16))
15183 (not (save-excursion
15184 (re-search-backward (concat "^" outline-regexp) nil t))))
15185 (setq beg (point-min) end (point-max)
15186 msg "Creating images for buffer...%s"))
15187 ((equal subtree '(4))
15188 (org-back-to-heading)
15189 (setq beg (point) end (org-end-of-subtree t)
15190 msg "Creating images for subtree...%s"))
15192 (if (setq at (org-inside-LaTeX-fragment-p))
15193 (goto-char (max (point-min) (- (cdr at) 2)))
15194 (org-back-to-heading))
15195 (setq beg (point) end (progn (outline-next-heading) (point))
15196 msg (if at "Creating image...%s"
15197 "Creating images for entry...%s"))))
15198 (message msg "")
15199 (narrow-to-region beg end)
15200 (goto-char beg)
15201 (org-format-latex
15202 (concat "ltxpng/" (file-name-sans-extension
15203 (file-name-nondirectory
15204 buffer-file-name)))
15205 default-directory 'overlays msg at 'forbuffer)
15206 (message msg "done. Use `C-c C-c' to remove images.")))))
15208 (defvar org-latex-regexps
15209 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
15210 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
15211 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
15212 ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
15213 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
15214 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
15215 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
15216 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
15217 "Regular expressions for matching embedded LaTeX.")
15219 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
15220 "Replace LaTeX fragments with links to an image, and produce images.
15221 Some of the options can be changed using the variable
15222 `org-format-latex-options'."
15223 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
15224 (let* ((prefixnodir (file-name-nondirectory prefix))
15225 (absprefix (expand-file-name prefix dir))
15226 (todir (file-name-directory absprefix))
15227 (opt org-format-latex-options)
15228 (matchers (plist-get opt :matchers))
15229 (re-list org-latex-regexps)
15230 (org-format-latex-header-extra
15231 (plist-get (org-infile-export-plist) :latex-header-extra))
15232 (cnt 0) txt hash link beg end re e checkdir
15233 executables-checked
15234 m n block linkfile movefile ov)
15235 ;; Check the different regular expressions
15236 (while (setq e (pop re-list))
15237 (setq m (car e) re (nth 1 e) n (nth 2 e)
15238 block (if (nth 3 e) "\n\n" ""))
15239 (when (member m matchers)
15240 (goto-char (point-min))
15241 (while (re-search-forward re nil t)
15242 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
15243 (not (get-text-property (match-beginning n)
15244 'org-protected))
15245 (or (not overlays)
15246 (not (eq (get-char-property (match-beginning n)
15247 'org-overlay-type)
15248 'org-latex-overlay))))
15249 (setq txt (match-string n)
15250 beg (match-beginning n) end (match-end n)
15251 cnt (1+ cnt))
15252 (let (print-length print-level) ; make sure full list is printed
15253 (setq hash (sha1 (prin1-to-string
15254 (list org-format-latex-header
15255 org-format-latex-header-extra
15256 org-export-latex-default-packages-alist
15257 org-export-latex-packages-alist
15258 org-format-latex-options
15259 forbuffer txt)))
15260 linkfile (format "%s_%s.png" prefix hash)
15261 movefile (format "%s_%s.png" absprefix hash)))
15262 (setq link (concat block "[[file:" linkfile "]]" block))
15263 (if msg (message msg cnt))
15264 (goto-char beg)
15265 (unless checkdir ; make sure the directory exists
15266 (setq checkdir t)
15267 (or (file-directory-p todir) (make-directory todir)))
15269 (unless executables-checked
15270 (org-check-external-command
15271 "latex" "needed to convert LaTeX fragments to images")
15272 (org-check-external-command
15273 "dvipng" "needed to convert LaTeX fragments to images")
15274 (setq executables-checked t))
15276 (unless (file-exists-p movefile)
15277 (org-create-formula-image
15278 txt movefile opt forbuffer))
15279 (if overlays
15280 (progn
15281 (mapc (lambda (o)
15282 (if (eq (overlay-get o 'org-overlay-type)
15283 'org-latex-overlay)
15284 (delete-overlay o)))
15285 (org-overlays-in beg end))
15286 (setq ov (make-overlay beg end))
15287 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
15288 (if (featurep 'xemacs)
15289 (progn
15290 (overlay-put ov 'invisible t)
15291 (overlay-put
15292 ov 'end-glyph
15293 (make-glyph (vector 'png :file movefile))))
15294 (overlay-put
15295 ov 'display
15296 (list 'image :type 'png :file movefile :ascent 'center)))
15297 (push ov org-latex-fragment-image-overlays)
15298 (goto-char end))
15299 (delete-region beg end)
15300 (insert (org-add-props link
15301 (list 'org-latex-src
15302 (replace-regexp-in-string "\"" "" txt)))))))))))
15304 ;; This function borrows from Ganesh Swami's latex2png.el
15305 (defun org-create-formula-image (string tofile options buffer)
15306 "This calls dvipng."
15307 (require 'org-latex)
15308 (let* ((tmpdir (if (featurep 'xemacs)
15309 (temp-directory)
15310 temporary-file-directory))
15311 (texfilebase (make-temp-name
15312 (expand-file-name "orgtex" tmpdir)))
15313 (texfile (concat texfilebase ".tex"))
15314 (dvifile (concat texfilebase ".dvi"))
15315 (pngfile (concat texfilebase ".png"))
15316 (fnh (if (featurep 'xemacs)
15317 (font-height (get-face-font 'default))
15318 (face-attribute 'default :height nil)))
15319 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
15320 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
15321 (fg (or (plist-get options (if buffer :foreground :html-foreground))
15322 "Black"))
15323 (bg (or (plist-get options (if buffer :background :html-background))
15324 "Transparent")))
15325 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
15326 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
15327 (with-temp-file texfile
15328 (insert (org-splice-latex-header
15329 org-format-latex-header
15330 org-export-latex-default-packages-alist
15331 org-export-latex-packages-alist
15332 org-format-latex-header-extra))
15333 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
15334 (require 'org-latex)
15335 (org-export-latex-fix-inputenc))
15336 (let ((dir default-directory))
15337 (condition-case nil
15338 (progn
15339 (cd tmpdir)
15340 (call-process "latex" nil nil nil texfile))
15341 (error nil))
15342 (cd dir))
15343 (if (not (file-exists-p dvifile))
15344 (progn (message "Failed to create dvi file from %s" texfile) nil)
15345 (condition-case nil
15346 (call-process "dvipng" nil nil nil
15347 "-fg" fg "-bg" bg
15348 "-D" dpi
15349 ;;"-x" scale "-y" scale
15350 "-T" "tight"
15351 "-o" pngfile
15352 dvifile)
15353 (error nil))
15354 (if (not (file-exists-p pngfile))
15355 (if org-format-latex-signal-error
15356 (error "Failed to create png file from %s" texfile)
15357 (message "Failed to create png file from %s" texfile)
15358 nil)
15359 ;; Use the requested file name and clean up
15360 (copy-file pngfile tofile 'replace)
15361 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
15362 (delete-file (concat texfilebase e)))
15363 pngfile))))
15365 (defun org-splice-latex-header (tpl def-pkg pkg &optional extra)
15366 "Fill a LaTeX header template TPL.
15367 In the template, the following place holders will be recognized:
15369 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
15370 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
15371 [PACKAGES] \\usepackage statements for PKG
15372 [NO-PACKAGES] do not include PKG
15373 [EXTRA] the string EXTRA
15374 [NO-EXTRA] do not include EXTRA
15376 For backward compatibility, if both the positive and the negative place
15377 holder is missing, the positive one (without the \"NO-\") will be
15378 assumed to be present at the end of the template.
15379 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
15380 EXTRA is a string."
15381 (let (rpl (end ""))
15382 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
15383 (setq rpl (if (or (match-end 1) (not def-pkg))
15384 "" (org-latex-packages-to-string def-pkg t))
15385 tpl (replace-match rpl t t tpl))
15386 (if def-pkg (setq end (org-latex-packages-to-string def-pkg))))
15388 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
15389 (setq rpl (if (or (match-end 1) (not pkg))
15390 "" (org-latex-packages-to-string pkg t))
15391 tpl (replace-match rpl t t tpl))
15392 (if pkg (setq end (concat end "\n" (org-latex-packages-to-string pkg)))))
15394 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
15395 (setq rpl (if (or (match-end 1) (not extra))
15396 "" (concat extra "\n"))
15397 tpl (replace-match rpl t t tpl))
15398 (if (and extra (string-match "\\S-" extra))
15399 (setq end (concat end "\n" extra))))
15401 (if (string-match "\\S-" end)
15402 (concat tpl "\n" end)
15403 tpl)))
15405 (defun org-latex-packages-to-string (pkg &optional newline)
15406 "Turn an alist of packages into a string with the \\usepackage macros."
15407 (setq pkg (mapconcat (lambda(p)
15408 (cond
15409 ((stringp p) p)
15410 ((equal "" (car p))
15411 (format "\\usepackage{%s}" (cadr p)))
15413 (format "\\usepackage[%s]{%s}"
15414 (car p) (cadr p)))))
15416 "\n"))
15417 (if newline (concat pkg "\n") pkg))
15419 (defun org-dvipng-color (attr)
15420 "Return an rgb color specification for dvipng."
15421 (apply 'format "rgb %s %s %s"
15422 (mapcar 'org-normalize-color
15423 (color-values (face-attribute 'default attr nil)))))
15425 (defun org-normalize-color (value)
15426 "Return string to be used as color value for an RGB component."
15427 (format "%g" (/ value 65535.0)))
15429 ;;;; Key bindings
15431 ;; Make `C-c C-x' a prefix key
15432 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
15434 ;; TAB key with modifiers
15435 (org-defkey org-mode-map "\C-i" 'org-cycle)
15436 (org-defkey org-mode-map [(tab)] 'org-cycle)
15437 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
15438 (org-defkey org-mode-map [(meta tab)] 'org-complete)
15439 (org-defkey org-mode-map "\M-\t" 'org-complete)
15440 (org-defkey org-mode-map "\M-\C-i" 'org-complete)
15441 ;; The following line is necessary under Suse GNU/Linux
15442 (unless (featurep 'xemacs)
15443 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
15444 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
15445 (define-key org-mode-map [backtab] 'org-shifttab)
15447 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
15448 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
15449 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
15451 ;; Cursor keys with modifiers
15452 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
15453 (org-defkey org-mode-map [(meta right)] 'org-metaright)
15454 (org-defkey org-mode-map [(meta up)] 'org-metaup)
15455 (org-defkey org-mode-map [(meta down)] 'org-metadown)
15457 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
15458 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
15459 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
15460 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
15462 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
15463 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
15464 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
15465 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
15467 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
15468 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
15470 ;;; Extra keys for tty access.
15471 ;; We only set them when really needed because otherwise the
15472 ;; menus don't show the simple keys
15474 (when (or org-use-extra-keys
15475 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
15476 (not window-system))
15477 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
15478 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
15479 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
15480 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
15481 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
15482 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
15483 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
15484 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
15485 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
15486 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
15487 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
15488 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
15489 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
15490 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
15491 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
15492 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
15493 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
15494 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
15495 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
15496 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
15497 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
15498 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
15499 (org-defkey org-mode-map [?\e (tab)] 'org-complete)
15500 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
15501 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
15502 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
15503 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
15504 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
15506 ;; All the other keys
15508 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
15509 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
15510 (if (boundp 'narrow-map)
15511 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
15512 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
15513 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
15514 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
15515 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
15516 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
15517 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
15518 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
15519 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
15520 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
15521 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
15522 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
15523 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
15524 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
15525 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
15526 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
15527 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
15528 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
15529 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
15530 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
15531 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
15532 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
15533 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
15534 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
15535 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
15536 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
15537 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
15538 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
15539 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
15540 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
15541 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
15542 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
15543 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
15544 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
15545 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
15546 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
15547 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
15548 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
15549 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
15550 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
15551 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
15552 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
15553 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
15554 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
15555 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
15556 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
15557 (org-defkey org-mode-map "\C-c^" 'org-sort)
15558 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
15559 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
15560 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
15561 (org-defkey org-mode-map "\C-m" 'org-return)
15562 (org-defkey org-mode-map "\C-j" 'org-return-indent)
15563 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
15564 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
15565 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
15566 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
15567 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
15568 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
15569 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
15570 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
15571 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
15572 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
15573 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
15574 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
15575 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
15576 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
15577 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
15578 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
15579 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
15580 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
15581 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
15582 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
15584 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
15585 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
15586 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
15587 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
15589 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
15590 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
15591 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
15592 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
15593 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
15594 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
15595 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
15596 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
15597 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
15598 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
15599 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
15600 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
15601 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
15602 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
15603 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
15605 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
15606 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
15607 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
15608 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
15610 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
15612 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
15614 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
15615 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
15617 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
15620 (when (featurep 'xemacs)
15621 (org-defkey org-mode-map 'button3 'popup-mode-menu))
15624 (defconst org-speed-commands-default
15626 ("Outline Navigation")
15627 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
15628 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
15629 ("f" . (org-speed-move-safe 'org-forward-same-level))
15630 ("b" . (org-speed-move-safe 'org-backward-same-level))
15631 ("u" . (org-speed-move-safe 'outline-up-heading))
15632 ("j" . org-goto)
15633 ("g" . (org-refile t))
15634 ("Outline Visibility")
15635 ("c" . org-cycle)
15636 ("C" . org-shifttab)
15637 (" " . org-display-outline-path)
15638 ("Outline Structure Editing")
15639 ("U" . org-shiftmetaup)
15640 ("D" . org-shiftmetadown)
15641 ("r" . org-metaright)
15642 ("l" . org-metaleft)
15643 ("R" . org-shiftmetaright)
15644 ("L" . org-shiftmetaleft)
15645 ("i" . (progn (forward-char 1) (call-interactively
15646 'org-insert-heading-respect-content)))
15647 ("^" . org-sort)
15648 ("w" . org-refile)
15649 ("a" . org-archive-subtree-default-with-confirmation)
15650 ("." . outline-mark-subtree)
15651 ("Clock Commands")
15652 ("I" . org-clock-in)
15653 ("O" . org-clock-out)
15654 ("Meta Data Editing")
15655 ("t" . org-todo)
15656 ("0" . (org-priority ?\ ))
15657 ("1" . (org-priority ?A))
15658 ("2" . (org-priority ?B))
15659 ("3" . (org-priority ?C))
15660 (";" . org-set-tags-command)
15661 ("e" . org-set-effort)
15662 ("Agenda Views etc")
15663 ("v" . org-agenda)
15664 ("/" . org-sparse-tree)
15665 ("Misc")
15666 ("o" . org-open-at-point)
15667 ("?" . org-speed-command-help)
15669 "The default speed commands.")
15671 (defun org-print-speed-command (e)
15672 (if (> (length (car e)) 1)
15673 (progn
15674 (princ "\n")
15675 (princ (car e))
15676 (princ "\n")
15677 (princ (make-string (length (car e)) ?-))
15678 (princ "\n"))
15679 (princ (car e))
15680 (princ " ")
15681 (if (symbolp (cdr e))
15682 (princ (symbol-name (cdr e)))
15683 (prin1 (cdr e)))
15684 (princ "\n")))
15686 (defun org-speed-command-help ()
15687 "Show the available speed commands."
15688 (interactive)
15689 (if (not org-use-speed-commands)
15690 (error "Speed commands are not activated, customize `org-use-speed-commands'.")
15691 (with-output-to-temp-buffer "*Help*"
15692 (princ "User-defined Speed commands\n===========================\n")
15693 (mapc 'org-print-speed-command org-speed-commands-user)
15694 (princ "\n")
15695 (princ "Built-in Speed commands\n=======================\n")
15696 (mapc 'org-print-speed-command org-speed-commands-default))
15697 (with-current-buffer "*Help*"
15698 (setq truncate-lines t))))
15700 (defun org-speed-move-safe (cmd)
15701 "Execute CMD, but make sure that the cursor always ends up in a headline.
15702 If not, return to the original position and throw an error."
15703 (interactive)
15704 (let ((pos (point)))
15705 (call-interactively cmd)
15706 (unless (and (bolp) (org-on-heading-p))
15707 (goto-char pos)
15708 (error "Boundary reached while executing %s" cmd))))
15710 (defvar org-self-insert-command-undo-counter 0)
15712 (defvar org-table-auto-blank-field) ; defined in org-table.el
15713 (defvar org-speed-command nil)
15714 (defun org-self-insert-command (N)
15715 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
15716 If the cursor is in a table looking at whitespace, the whitespace is
15717 overwritten, and the table is not marked as requiring realignment."
15718 (interactive "p")
15719 (cond
15720 ((and org-use-speed-commands
15721 (or (and (bolp) (looking-at outline-regexp))
15722 (and (functionp org-use-speed-commands)
15723 (funcall org-use-speed-commands)))
15724 (setq
15725 org-speed-command
15726 (or (cdr (assoc (this-command-keys) org-speed-commands-user))
15727 (cdr (assoc (this-command-keys) org-speed-commands-default)))))
15728 (cond
15729 ((commandp org-speed-command)
15730 (setq this-command org-speed-command)
15731 (call-interactively org-speed-command))
15732 ((functionp org-speed-command)
15733 (funcall org-speed-command))
15734 ((and org-speed-command (listp org-speed-command))
15735 (eval org-speed-command))
15736 (t (let (org-use-speed-commands)
15737 (call-interactively 'org-self-insert-command)))))
15738 ((and
15739 (org-table-p)
15740 (progn
15741 ;; check if we blank the field, and if that triggers align
15742 (and (featurep 'org-table) org-table-auto-blank-field
15743 (member last-command
15744 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
15745 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
15746 ;; got extra space, this field does not determine column width
15747 (let (org-table-may-need-update) (org-table-blank-field))
15748 ;; no extra space, this field may determine column width
15749 (org-table-blank-field)))
15751 (eq N 1)
15752 (looking-at "[^|\n]* |"))
15753 (let (org-table-may-need-update)
15754 (goto-char (1- (match-end 0)))
15755 (delete-backward-char 1)
15756 (goto-char (match-beginning 0))
15757 (self-insert-command N)))
15759 (setq org-table-may-need-update t)
15760 (self-insert-command N)
15761 (org-fix-tags-on-the-fly)
15762 (if org-self-insert-cluster-for-undo
15763 (if (not (eq last-command 'org-self-insert-command))
15764 (setq org-self-insert-command-undo-counter 1)
15765 (if (>= org-self-insert-command-undo-counter 20)
15766 (setq org-self-insert-command-undo-counter 1)
15767 (and (> org-self-insert-command-undo-counter 0)
15768 buffer-undo-list
15769 (not (cadr buffer-undo-list)) ; remove nil entry
15770 (setcdr buffer-undo-list (cddr buffer-undo-list)))
15771 (setq org-self-insert-command-undo-counter
15772 (1+ org-self-insert-command-undo-counter))))))))
15774 (defun org-fix-tags-on-the-fly ()
15775 (when (and (equal (char-after (point-at-bol)) ?*)
15776 (org-on-heading-p))
15777 (org-align-tags-here org-tags-column)))
15779 (defun org-delete-backward-char (N)
15780 "Like `delete-backward-char', insert whitespace at field end in tables.
15781 When deleting backwards, in tables this function will insert whitespace in
15782 front of the next \"|\" separator, to keep the table aligned. The table will
15783 still be marked for re-alignment if the field did fill the entire column,
15784 because, in this case the deletion might narrow the column."
15785 (interactive "p")
15786 (if (and (org-table-p)
15787 (eq N 1)
15788 (string-match "|" (buffer-substring (point-at-bol) (point)))
15789 (looking-at ".*?|"))
15790 (let ((pos (point))
15791 (noalign (looking-at "[^|\n\r]* |"))
15792 (c org-table-may-need-update))
15793 (backward-delete-char N)
15794 (skip-chars-forward "^|")
15795 (insert " ")
15796 (goto-char (1- pos))
15797 ;; noalign: if there were two spaces at the end, this field
15798 ;; does not determine the width of the column.
15799 (if noalign (setq org-table-may-need-update c)))
15800 (backward-delete-char N)
15801 (org-fix-tags-on-the-fly)))
15803 (defun org-delete-char (N)
15804 "Like `delete-char', but insert whitespace at field end in tables.
15805 When deleting characters, in tables this function will insert whitespace in
15806 front of the next \"|\" separator, to keep the table aligned. The table will
15807 still be marked for re-alignment if the field did fill the entire column,
15808 because, in this case the deletion might narrow the column."
15809 (interactive "p")
15810 (if (and (org-table-p)
15811 (not (bolp))
15812 (not (= (char-after) ?|))
15813 (eq N 1))
15814 (if (looking-at ".*?|")
15815 (let ((pos (point))
15816 (noalign (looking-at "[^|\n\r]* |"))
15817 (c org-table-may-need-update))
15818 (replace-match (concat
15819 (substring (match-string 0) 1 -1)
15820 " |"))
15821 (goto-char pos)
15822 ;; noalign: if there were two spaces at the end, this field
15823 ;; does not determine the width of the column.
15824 (if noalign (setq org-table-may-need-update c)))
15825 (delete-char N))
15826 (delete-char N)
15827 (org-fix-tags-on-the-fly)))
15829 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
15830 (put 'org-self-insert-command 'delete-selection t)
15831 (put 'orgtbl-self-insert-command 'delete-selection t)
15832 (put 'org-delete-char 'delete-selection 'supersede)
15833 (put 'org-delete-backward-char 'delete-selection 'supersede)
15834 (put 'org-yank 'delete-selection 'yank)
15836 ;; Make `flyspell-mode' delay after some commands
15837 (put 'org-self-insert-command 'flyspell-delayed t)
15838 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
15839 (put 'org-delete-char 'flyspell-delayed t)
15840 (put 'org-delete-backward-char 'flyspell-delayed t)
15842 ;; Make pabbrev-mode expand after org-mode commands
15843 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
15844 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
15846 ;; How to do this: Measure non-white length of current string
15847 ;; If equal to column width, we should realign.
15849 (defun org-remap (map &rest commands)
15850 "In MAP, remap the functions given in COMMANDS.
15851 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
15852 (let (new old)
15853 (while commands
15854 (setq old (pop commands) new (pop commands))
15855 (if (fboundp 'command-remapping)
15856 (org-defkey map (vector 'remap old) new)
15857 (substitute-key-definition old new map global-map)))))
15859 (when (eq org-enable-table-editor 'optimized)
15860 ;; If the user wants maximum table support, we need to hijack
15861 ;; some standard editing functions
15862 (org-remap org-mode-map
15863 'self-insert-command 'org-self-insert-command
15864 'delete-char 'org-delete-char
15865 'delete-backward-char 'org-delete-backward-char)
15866 (org-defkey org-mode-map "|" 'org-force-self-insert))
15868 (defvar org-ctrl-c-ctrl-c-hook nil
15869 "Hook for functions attaching themselves to `C-c C-c'.
15870 This can be used to add additional functionality to the C-c C-c key which
15871 executes context-dependent commands.
15872 Each function will be called with no arguments. The function must check
15873 if the context is appropriate for it to act. If yes, it should do its
15874 thing and then return a non-nil value. If the context is wrong,
15875 just do nothing and return nil.")
15877 (defvar org-tab-first-hook nil
15878 "Hook for functions to attach themselves to TAB.
15879 See `org-ctrl-c-ctrl-c-hook' for more information.
15880 This hook runs as the first action when TAB is pressed, even before
15881 `org-cycle' messes around with the `outline-regexp' to cater for
15882 inline tasks and plain list item folding.
15883 If any function in this hook returns t, not other actions like table
15884 field motion visibility cycling will be done.")
15886 (defvar org-tab-after-check-for-table-hook nil
15887 "Hook for functions to attach themselves to TAB.
15888 See `org-ctrl-c-ctrl-c-hook' for more information.
15889 This hook runs after it has been established that the cursor is not in a
15890 table, but before checking if the cursor is in a headline or if global cycling
15891 should be done.
15892 If any function in this hook returns t, not other actions like visibility
15893 cycling will be done.")
15895 (defvar org-tab-after-check-for-cycling-hook nil
15896 "Hook for functions to attach themselves to TAB.
15897 See `org-ctrl-c-ctrl-c-hook' for more information.
15898 This hook runs after it has been established that not table field motion and
15899 not visibility should be done because of current context. This is probably
15900 the place where a package like yasnippets can hook in.")
15902 (defvar org-tab-before-tab-emulation-hook nil
15903 "Hook for functions to attach themselves to TAB.
15904 See `org-ctrl-c-ctrl-c-hook' for more information.
15905 This hook runs after every other options for TAB have been exhausted, but
15906 before indentation and \t insertion takes place.")
15908 (defvar org-metaleft-hook nil
15909 "Hook for functions attaching themselves to `M-left'.
15910 See `org-ctrl-c-ctrl-c-hook' for more information.")
15911 (defvar org-metaright-hook nil
15912 "Hook for functions attaching themselves to `M-right'.
15913 See `org-ctrl-c-ctrl-c-hook' for more information.")
15914 (defvar org-metaup-hook nil
15915 "Hook for functions attaching themselves to `M-up'.
15916 See `org-ctrl-c-ctrl-c-hook' for more information.")
15917 (defvar org-metadown-hook nil
15918 "Hook for functions attaching themselves to `M-down'.
15919 See `org-ctrl-c-ctrl-c-hook' for more information.")
15920 (defvar org-shiftmetaleft-hook nil
15921 "Hook for functions attaching themselves to `M-S-left'.
15922 See `org-ctrl-c-ctrl-c-hook' for more information.")
15923 (defvar org-shiftmetaright-hook nil
15924 "Hook for functions attaching themselves to `M-S-right'.
15925 See `org-ctrl-c-ctrl-c-hook' for more information.")
15926 (defvar org-shiftmetaup-hook nil
15927 "Hook for functions attaching themselves to `M-S-up'.
15928 See `org-ctrl-c-ctrl-c-hook' for more information.")
15929 (defvar org-shiftmetadown-hook nil
15930 "Hook for functions attaching themselves to `M-S-down'.
15931 See `org-ctrl-c-ctrl-c-hook' for more information.")
15932 (defvar org-metareturn-hook nil
15933 "Hook for functions attaching themselves to `M-RET'.
15934 See `org-ctrl-c-ctrl-c-hook' for more information.")
15936 (defun org-modifier-cursor-error ()
15937 "Throw an error, a modified cursor command was applied in wrong context."
15938 (error "This command is active in special context like tables, headlines or items"))
15940 (defun org-shiftselect-error ()
15941 "Throw an error because Shift-Cursor command was applied in wrong context."
15942 (if (and (boundp 'shift-select-mode) shift-select-mode)
15943 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
15944 (error "This command works only in special context like headlines or timestamps")))
15946 (defun org-call-for-shift-select (cmd)
15947 (let ((this-command-keys-shift-translated t))
15948 (call-interactively cmd)))
15950 (defun org-shifttab (&optional arg)
15951 "Global visibility cycling or move to previous table field.
15952 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
15953 on context.
15954 See the individual commands for more information."
15955 (interactive "P")
15956 (cond
15957 ((org-at-table-p) (call-interactively 'org-table-previous-field))
15958 ((integerp arg)
15959 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
15960 (message "Content view to level: %d" arg)
15961 (org-content (prefix-numeric-value arg2))
15962 (setq org-cycle-global-status 'overview)))
15963 (t (call-interactively 'org-global-cycle))))
15965 (defun org-shiftmetaleft ()
15966 "Promote subtree or delete table column.
15967 Calls `org-promote-subtree', `org-outdent-item',
15968 or `org-table-delete-column', depending on context.
15969 See the individual commands for more information."
15970 (interactive)
15971 (cond
15972 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
15973 ((org-at-table-p) (call-interactively 'org-table-delete-column))
15974 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
15975 ((org-at-item-p) (call-interactively 'org-outdent-item))
15976 (t (org-modifier-cursor-error))))
15978 (defun org-shiftmetaright ()
15979 "Demote subtree or insert table column.
15980 Calls `org-demote-subtree', `org-indent-item',
15981 or `org-table-insert-column', depending on context.
15982 See the individual commands for more information."
15983 (interactive)
15984 (cond
15985 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
15986 ((org-at-table-p) (call-interactively 'org-table-insert-column))
15987 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
15988 ((org-at-item-p) (call-interactively 'org-indent-item))
15989 (t (org-modifier-cursor-error))))
15991 (defun org-shiftmetaup (&optional arg)
15992 "Move subtree up or kill table row.
15993 Calls `org-move-subtree-up' or `org-table-kill-row' or
15994 `org-move-item-up' depending on context. See the individual commands
15995 for more information."
15996 (interactive "P")
15997 (cond
15998 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
15999 ((org-at-table-p) (call-interactively 'org-table-kill-row))
16000 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
16001 ((org-at-item-p) (call-interactively 'org-move-item-up))
16002 (t (org-modifier-cursor-error))))
16004 (defun org-shiftmetadown (&optional arg)
16005 "Move subtree down or insert table row.
16006 Calls `org-move-subtree-down' or `org-table-insert-row' or
16007 `org-move-item-down', depending on context. See the individual
16008 commands for more information."
16009 (interactive "P")
16010 (cond
16011 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
16012 ((org-at-table-p) (call-interactively 'org-table-insert-row))
16013 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
16014 ((org-at-item-p) (call-interactively 'org-move-item-down))
16015 (t (org-modifier-cursor-error))))
16017 (defun org-metaleft (&optional arg)
16018 "Promote heading or move table column to left.
16019 Calls `org-do-promote' or `org-table-move-column', depending on context.
16020 With no specific context, calls the Emacs default `backward-word'.
16021 See the individual commands for more information."
16022 (interactive "P")
16023 (cond
16024 ((run-hook-with-args-until-success 'org-metaleft-hook))
16025 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
16026 ((or (org-on-heading-p)
16027 (and (org-region-active-p)
16028 (save-excursion
16029 (goto-char (region-beginning))
16030 (org-on-heading-p))))
16031 (call-interactively 'org-do-promote))
16032 ((or (org-at-item-p)
16033 (and (org-region-active-p)
16034 (save-excursion
16035 (goto-char (region-beginning))
16036 (org-at-item-p))))
16037 (call-interactively 'org-outdent-item))
16038 (t (call-interactively 'backward-word))))
16040 (defun org-metaright (&optional arg)
16041 "Demote subtree or move table column to right.
16042 Calls `org-do-demote' or `org-table-move-column', depending on context.
16043 With no specific context, calls the Emacs default `forward-word'.
16044 See the individual commands for more information."
16045 (interactive "P")
16046 (cond
16047 ((run-hook-with-args-until-success 'org-metaright-hook))
16048 ((org-at-table-p) (call-interactively 'org-table-move-column))
16049 ((or (org-on-heading-p)
16050 (and (org-region-active-p)
16051 (save-excursion
16052 (goto-char (region-beginning))
16053 (org-on-heading-p))))
16054 (call-interactively 'org-do-demote))
16055 ((or (org-at-item-p)
16056 (and (org-region-active-p)
16057 (save-excursion
16058 (goto-char (region-beginning))
16059 (org-at-item-p))))
16060 (call-interactively 'org-indent-item))
16061 (t (call-interactively 'forward-word))))
16063 (defun org-metaup (&optional arg)
16064 "Move subtree up or move table row up.
16065 Calls `org-move-subtree-up' or `org-table-move-row' or
16066 `org-move-item-up', depending on context. See the individual commands
16067 for more information."
16068 (interactive "P")
16069 (cond
16070 ((run-hook-with-args-until-success 'org-metaup-hook))
16071 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
16072 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
16073 ((org-at-item-p) (call-interactively 'org-move-item-up))
16074 (t (transpose-lines 1) (beginning-of-line -1))))
16076 (defun org-metadown (&optional arg)
16077 "Move subtree down or move table row down.
16078 Calls `org-move-subtree-down' or `org-table-move-row' or
16079 `org-move-item-down', depending on context. See the individual
16080 commands for more information."
16081 (interactive "P")
16082 (cond
16083 ((run-hook-with-args-until-success 'org-metadown-hook))
16084 ((org-at-table-p) (call-interactively 'org-table-move-row))
16085 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
16086 ((org-at-item-p) (call-interactively 'org-move-item-down))
16087 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
16089 (defun org-shiftup (&optional arg)
16090 "Increase item in timestamp or increase priority of current headline.
16091 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
16092 depending on context. See the individual commands for more information."
16093 (interactive "P")
16094 (cond
16095 ((and org-support-shift-select (org-region-active-p))
16096 (org-call-for-shift-select 'previous-line))
16097 ((org-at-timestamp-p t)
16098 (call-interactively (if org-edit-timestamp-down-means-later
16099 'org-timestamp-down 'org-timestamp-up)))
16100 ((and (not (eq org-support-shift-select 'always))
16101 org-enable-priority-commands
16102 (org-on-heading-p))
16103 (call-interactively 'org-priority-up))
16104 ((and (not org-support-shift-select) (org-at-item-p))
16105 (call-interactively 'org-previous-item))
16106 ((org-clocktable-try-shift 'up arg))
16107 (org-support-shift-select
16108 (org-call-for-shift-select 'previous-line))
16109 (t (org-shiftselect-error))))
16111 (defun org-shiftdown (&optional arg)
16112 "Decrease item in timestamp or decrease priority of current headline.
16113 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
16114 depending on context. See the individual commands for more information."
16115 (interactive "P")
16116 (cond
16117 ((and org-support-shift-select (org-region-active-p))
16118 (org-call-for-shift-select 'next-line))
16119 ((org-at-timestamp-p t)
16120 (call-interactively (if org-edit-timestamp-down-means-later
16121 'org-timestamp-up 'org-timestamp-down)))
16122 ((and (not (eq org-support-shift-select 'always))
16123 org-enable-priority-commands
16124 (org-on-heading-p))
16125 (call-interactively 'org-priority-down))
16126 ((and (not org-support-shift-select) (org-at-item-p))
16127 (call-interactively 'org-next-item))
16128 ((org-clocktable-try-shift 'down arg))
16129 (org-support-shift-select
16130 (org-call-for-shift-select 'next-line))
16131 (t (org-shiftselect-error))))
16133 (defun org-shiftright (&optional arg)
16134 "Cycle the thing at point or in the current line, depending on context.
16135 Depending on context, this does one of the following:
16137 - switch a timestamp at point one day into the future
16138 - on a headline, switch to the next TODO keyword.
16139 - on an item, switch entire list to the next bullet type
16140 - on a property line, switch to the next allowed value
16141 - on a clocktable definition line, move time block into the future"
16142 (interactive "P")
16143 (cond
16144 ((and org-support-shift-select (org-region-active-p))
16145 (org-call-for-shift-select 'forward-char))
16146 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
16147 ((and (not (eq org-support-shift-select 'always))
16148 (org-on-heading-p))
16149 (let ((org-inhibit-logging
16150 (not org-treat-S-cursor-todo-selection-as-state-change))
16151 (org-inhibit-blocking
16152 (not org-treat-S-cursor-todo-selection-as-state-change)))
16153 (org-call-with-arg 'org-todo 'right)))
16154 ((or (and org-support-shift-select
16155 (not (eq org-support-shift-select 'always))
16156 (org-at-item-bullet-p))
16157 (and (not org-support-shift-select) (org-at-item-p)))
16158 (org-call-with-arg 'org-cycle-list-bullet nil))
16159 ((and (not (eq org-support-shift-select 'always))
16160 (org-at-property-p))
16161 (call-interactively 'org-property-next-allowed-value))
16162 ((org-clocktable-try-shift 'right arg))
16163 (org-support-shift-select
16164 (org-call-for-shift-select 'forward-char))
16165 (t (org-shiftselect-error))))
16167 (defun org-shiftleft (&optional arg)
16168 "Cycle the thing at point or in the current line, depending on context.
16169 Depending on context, this does one of the following:
16171 - switch a timestamp at point one day into the past
16172 - on a headline, switch to the previous TODO keyword.
16173 - on an item, switch entire list to the previous bullet type
16174 - on a property line, switch to the previous allowed value
16175 - on a clocktable definition line, move time block into the past"
16176 (interactive "P")
16177 (cond
16178 ((and org-support-shift-select (org-region-active-p))
16179 (org-call-for-shift-select 'backward-char))
16180 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
16181 ((and (not (eq org-support-shift-select 'always))
16182 (org-on-heading-p))
16183 (let ((org-inhibit-logging
16184 (not org-treat-S-cursor-todo-selection-as-state-change))
16185 (org-inhibit-blocking
16186 (not org-treat-S-cursor-todo-selection-as-state-change)))
16187 (org-call-with-arg 'org-todo 'left)))
16188 ((or (and org-support-shift-select
16189 (not (eq org-support-shift-select 'always))
16190 (org-at-item-bullet-p))
16191 (and (not org-support-shift-select) (org-at-item-p)))
16192 (org-call-with-arg 'org-cycle-list-bullet 'previous))
16193 ((and (not (eq org-support-shift-select 'always))
16194 (org-at-property-p))
16195 (call-interactively 'org-property-previous-allowed-value))
16196 ((org-clocktable-try-shift 'left arg))
16197 (org-support-shift-select
16198 (org-call-for-shift-select 'backward-char))
16199 (t (org-shiftselect-error))))
16201 (defun org-shiftcontrolright ()
16202 "Switch to next TODO set."
16203 (interactive)
16204 (cond
16205 ((and org-support-shift-select (org-region-active-p))
16206 (org-call-for-shift-select 'forward-word))
16207 ((and (not (eq org-support-shift-select 'always))
16208 (org-on-heading-p))
16209 (org-call-with-arg 'org-todo 'nextset))
16210 (org-support-shift-select
16211 (org-call-for-shift-select 'forward-word))
16212 (t (org-shiftselect-error))))
16214 (defun org-shiftcontrolleft ()
16215 "Switch to previous TODO set."
16216 (interactive)
16217 (cond
16218 ((and org-support-shift-select (org-region-active-p))
16219 (org-call-for-shift-select 'backward-word))
16220 ((and (not (eq org-support-shift-select 'always))
16221 (org-on-heading-p))
16222 (org-call-with-arg 'org-todo 'previousset))
16223 (org-support-shift-select
16224 (org-call-for-shift-select 'backward-word))
16225 (t (org-shiftselect-error))))
16227 (defun org-ctrl-c-ret ()
16228 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
16229 (interactive)
16230 (cond
16231 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
16232 (t (call-interactively 'org-insert-heading))))
16234 (defun org-copy-special ()
16235 "Copy region in table or copy current subtree.
16236 Calls `org-table-copy' or `org-copy-subtree', depending on context.
16237 See the individual commands for more information."
16238 (interactive)
16239 (call-interactively
16240 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
16242 (defun org-cut-special ()
16243 "Cut region in table or cut current subtree.
16244 Calls `org-table-copy' or `org-cut-subtree', depending on context.
16245 See the individual commands for more information."
16246 (interactive)
16247 (call-interactively
16248 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
16250 (defun org-paste-special (arg)
16251 "Paste rectangular region into table, or past subtree relative to level.
16252 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
16253 See the individual commands for more information."
16254 (interactive "P")
16255 (if (org-at-table-p)
16256 (org-table-paste-rectangle)
16257 (org-paste-subtree arg)))
16259 (defun org-edit-special ()
16260 "Call a special editor for the stuff at point.
16261 When at a table, call the formula editor with `org-table-edit-formulas'.
16262 When at the first line of an src example, call `org-edit-src-code'.
16263 When in an #+include line, visit the include file. Otherwise call
16264 `ffap' to visit the file at point."
16265 (interactive)
16266 (cond
16267 ((org-at-table.el-p)
16268 (org-edit-src-code))
16269 ((org-at-table-p)
16270 (call-interactively 'org-table-edit-formulas))
16271 ((save-excursion
16272 (beginning-of-line 1)
16273 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
16274 (find-file (org-trim (match-string 1))))
16275 ((org-edit-src-code))
16276 ((org-edit-fixed-width-region))
16277 (t (call-interactively 'ffap))))
16280 (defun org-ctrl-c-ctrl-c (&optional arg)
16281 "Set tags in headline, or update according to changed information at point.
16283 This command does many different things, depending on context:
16285 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
16286 this is what we do.
16288 - If the cursor is on a statistics cookie, update it.
16290 - If the cursor is in a headline, prompt for tags and insert them
16291 into the current line, aligned to `org-tags-column'. When called
16292 with prefix arg, realign all tags in the current buffer.
16294 - If the cursor is in one of the special #+KEYWORD lines, this
16295 triggers scanning the buffer for these lines and updating the
16296 information.
16298 - If the cursor is inside a table, realign the table. This command
16299 works even if the automatic table editor has been turned off.
16301 - If the cursor is on a #+TBLFM line, re-apply the formulas to
16302 the entire table.
16304 - If the cursor is at a footnote reference or definition, jump to
16305 the corresponding definition or references, respectively.
16307 - If the cursor is a the beginning of a dynamic block, update it.
16309 - If the current buffer is a remember buffer, close note and file
16310 it. A prefix argument of 1 files to the default location
16311 without further interaction. A prefix argument of 2 files to
16312 the currently clocking task.
16314 - If the cursor is on a <<<target>>>, update radio targets and corresponding
16315 links in this buffer.
16317 - If the cursor is on a numbered item in a plain list, renumber the
16318 ordered list.
16320 - If the cursor is on a checkbox, toggle it."
16321 (interactive "P")
16322 (let ((org-enable-table-editor t))
16323 (cond
16324 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
16325 org-occur-highlights
16326 org-latex-fragment-image-overlays)
16327 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
16328 (org-remove-occur-highlights)
16329 (org-remove-latex-fragment-image-overlays)
16330 (message "Temporary highlights/overlays removed from current buffer"))
16331 ((and (local-variable-p 'org-finish-function (current-buffer))
16332 (fboundp org-finish-function))
16333 (funcall org-finish-function))
16334 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
16335 ((or (looking-at org-property-start-re)
16336 (org-at-property-p))
16337 (call-interactively 'org-property-action))
16338 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
16339 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
16340 (or (org-on-heading-p) (org-at-item-p)))
16341 (call-interactively 'org-update-statistics-cookies))
16342 ((org-on-heading-p) (call-interactively 'org-set-tags))
16343 ((org-at-table.el-p)
16344 (message "Use C-c ' to edit table.el tables"))
16345 ((org-at-table-p)
16346 (org-table-maybe-eval-formula)
16347 (if arg
16348 (call-interactively 'org-table-recalculate)
16349 (org-table-maybe-recalculate-line))
16350 (call-interactively 'org-table-align))
16351 ((or (org-footnote-at-reference-p)
16352 (org-footnote-at-definition-p))
16353 (call-interactively 'org-footnote-action))
16354 ((org-at-item-checkbox-p)
16355 (call-interactively 'org-toggle-checkbox))
16356 ((org-at-item-p)
16357 (if arg
16358 (call-interactively 'org-toggle-checkbox)
16359 (call-interactively 'org-maybe-renumber-ordered-list)))
16360 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
16361 ;; Dynamic block
16362 (beginning-of-line 1)
16363 (save-excursion (org-update-dblock)))
16364 ((save-excursion
16365 (beginning-of-line 1)
16366 (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
16367 (cond
16368 ((equal (match-string 1) "TBLFM")
16369 ;; Recalculate the table before this line
16370 (save-excursion
16371 (beginning-of-line 1)
16372 (skip-chars-backward " \r\n\t")
16373 (if (org-at-table-p)
16374 (org-call-with-arg 'org-table-recalculate (or arg t)))))
16376 (let ((org-inhibit-startup-visibility-stuff t)
16377 (org-startup-align-all-tables nil))
16378 (org-save-outline-visibility 'use-markers (org-mode-restart)))
16379 (message "Local setup has been refreshed"))))
16380 ((org-clock-update-time-maybe))
16381 (t (error "C-c C-c can do nothing useful at this location")))))
16383 (defun org-mode-restart ()
16384 "Restart Org-mode, to scan again for special lines.
16385 Also updates the keyword regular expressions."
16386 (interactive)
16387 (org-mode)
16388 (message "Org-mode restarted"))
16390 (defun org-kill-note-or-show-branches ()
16391 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
16392 (interactive)
16393 (if (not org-finish-function)
16394 (call-interactively 'show-branches)
16395 (let ((org-note-abort t))
16396 (funcall org-finish-function))))
16398 (defun org-return (&optional indent)
16399 "Goto next table row or insert a newline.
16400 Calls `org-table-next-row' or `newline', depending on context.
16401 See the individual commands for more information."
16402 (interactive)
16403 (cond
16404 ((bobp) (if indent (newline-and-indent) (newline)))
16405 ((org-at-table-p)
16406 (org-table-justify-field-maybe)
16407 (call-interactively 'org-table-next-row))
16408 ((and org-return-follows-link
16409 (eq (get-text-property (point) 'face) 'org-link))
16410 (call-interactively 'org-open-at-point))
16411 ((and (org-at-heading-p)
16412 (looking-at
16413 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
16414 (org-show-entry)
16415 (end-of-line 1)
16416 (newline))
16417 (t (if indent (newline-and-indent) (newline)))))
16419 (defun org-return-indent ()
16420 "Goto next table row or insert a newline and indent.
16421 Calls `org-table-next-row' or `newline-and-indent', depending on
16422 context. See the individual commands for more information."
16423 (interactive)
16424 (org-return t))
16426 (defun org-ctrl-c-star ()
16427 "Compute table, or change heading status of lines.
16428 Calls `org-table-recalculate' or `org-toggle-heading',
16429 depending on context."
16430 (interactive)
16431 (cond
16432 ((org-at-table-p)
16433 (call-interactively 'org-table-recalculate))
16435 ;; Convert all lines in region to list items
16436 (call-interactively 'org-toggle-heading))))
16438 (defun org-ctrl-c-minus ()
16439 "Insert separator line in table or modify bullet status of line.
16440 Also turns a plain line or a region of lines into list items.
16441 Calls `org-table-insert-hline', `org-toggle-item', or
16442 `org-cycle-list-bullet', depending on context."
16443 (interactive)
16444 (cond
16445 ((org-at-table-p)
16446 (call-interactively 'org-table-insert-hline))
16447 ((org-region-active-p)
16448 (call-interactively 'org-toggle-item))
16449 ((org-in-item-p)
16450 (call-interactively 'org-cycle-list-bullet))
16452 (call-interactively 'org-toggle-item))))
16454 (defun org-toggle-item ()
16455 "Convert headings or normal lines to items, items to normal lines.
16456 If there is no active region, only the current line is considered.
16458 If the first line in the region is a headline, convert all headlines to items.
16460 If the first line in the region is an item, convert all items to normal lines.
16462 If the first line is normal text, add an item bullet to each line."
16463 (interactive)
16464 (let (l2 l beg end)
16465 (if (org-region-active-p)
16466 (setq beg (region-beginning) end (region-end))
16467 (setq beg (point-at-bol)
16468 end (min (1+ (point-at-eol)) (point-max))))
16469 (save-excursion
16470 (goto-char end)
16471 (setq l2 (org-current-line))
16472 (goto-char beg)
16473 (beginning-of-line 1)
16474 (setq l (1- (org-current-line)))
16475 (if (org-at-item-p)
16476 ;; We already have items, de-itemize
16477 (while (< (setq l (1+ l)) l2)
16478 (when (org-at-item-p)
16479 (goto-char (match-beginning 2))
16480 (delete-region (match-beginning 2) (match-end 2))
16481 (and (looking-at "[ \t]+") (replace-match "")))
16482 (beginning-of-line 2))
16483 (if (org-on-heading-p)
16484 ;; Headings, convert to items
16485 (while (< (setq l (1+ l)) l2)
16486 (if (looking-at org-outline-regexp)
16487 (replace-match "- " t t))
16488 (beginning-of-line 2))
16489 ;; normal lines, turn them into items
16490 (while (< (setq l (1+ l)) l2)
16491 (unless (org-at-item-p)
16492 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
16493 (replace-match "\\1- \\2")))
16494 (beginning-of-line 2)))))))
16496 (defun org-toggle-heading (&optional nstars)
16497 "Convert headings to normal text, or items or text to headings.
16498 If there is no active region, only the current line is considered.
16500 If the first line is a heading, remove the stars from all headlines
16501 in the region.
16503 If the first line is a plain list item, turn all plain list items
16504 into headings.
16506 If the first line is a normal line, turn each and every line in the
16507 region into a heading.
16509 When converting a line into a heading, the number of stars is chosen
16510 such that the lines become children of the current entry. However,
16511 when a prefix argument is given, its value determines the number of
16512 stars to add."
16513 (interactive "P")
16514 (let (l2 l itemp 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-on-heading-p)
16526 ;; We already have headlines, de-star them
16527 (while (< (setq l (1+ l)) l2)
16528 (when (org-on-heading-p t)
16529 (and (looking-at outline-regexp) (replace-match "")))
16530 (beginning-of-line 2))
16531 (setq itemp (org-at-item-p))
16532 (let* ((stars
16533 (if nstars
16534 (make-string (prefix-numeric-value current-prefix-arg)
16536 (save-excursion
16537 (if (re-search-backward org-complex-heading-regexp nil t)
16538 (match-string 1) ""))))
16539 (add-stars (cond (nstars "")
16540 ((equal stars "") "*")
16541 (org-odd-levels-only "**")
16542 (t "*")))
16543 (rpl (concat stars add-stars " ")))
16544 (while (< (setq l (1+ l)) l2)
16545 (if itemp
16546 (and (org-at-item-p) (replace-match rpl t t))
16547 (unless (org-on-heading-p)
16548 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
16549 (replace-match (concat rpl (match-string 2))))))
16550 (beginning-of-line 2)))))))
16552 (defun org-meta-return (&optional arg)
16553 "Insert a new heading or wrap a region in a table.
16554 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
16555 See the individual commands for more information."
16556 (interactive "P")
16557 (cond
16558 ((run-hook-with-args-until-success 'org-metareturn-hook))
16559 ((org-at-table-p)
16560 (call-interactively 'org-table-wrap-region))
16561 (t (call-interactively 'org-insert-heading))))
16563 ;;; Menu entries
16565 ;; Define the Org-mode menus
16566 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
16567 '("Tbl"
16568 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
16569 ["Next Field" org-cycle (org-at-table-p)]
16570 ["Previous Field" org-shifttab (org-at-table-p)]
16571 ["Next Row" org-return (org-at-table-p)]
16572 "--"
16573 ["Blank Field" org-table-blank-field (org-at-table-p)]
16574 ["Edit Field" org-table-edit-field (org-at-table-p)]
16575 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
16576 "--"
16577 ("Column"
16578 ["Move Column Left" org-metaleft (org-at-table-p)]
16579 ["Move Column Right" org-metaright (org-at-table-p)]
16580 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
16581 ["Insert Column" org-shiftmetaright (org-at-table-p)])
16582 ("Row"
16583 ["Move Row Up" org-metaup (org-at-table-p)]
16584 ["Move Row Down" org-metadown (org-at-table-p)]
16585 ["Delete Row" org-shiftmetaup (org-at-table-p)]
16586 ["Insert Row" org-shiftmetadown (org-at-table-p)]
16587 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
16588 "--"
16589 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
16590 ("Rectangle"
16591 ["Copy Rectangle" org-copy-special (org-at-table-p)]
16592 ["Cut Rectangle" org-cut-special (org-at-table-p)]
16593 ["Paste Rectangle" org-paste-special (org-at-table-p)]
16594 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
16595 "--"
16596 ("Calculate"
16597 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
16598 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
16599 ["Edit Formulas" org-edit-special (org-at-table-p)]
16600 "--"
16601 ["Recalculate line" org-table-recalculate (org-at-table-p)]
16602 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
16603 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
16604 "--"
16605 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
16606 "--"
16607 ["Sum Column/Rectangle" org-table-sum
16608 (or (org-at-table-p) (org-region-active-p))]
16609 ["Which Column?" org-table-current-column (org-at-table-p)])
16610 ["Debug Formulas"
16611 org-table-toggle-formula-debugger
16612 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
16613 ["Show Col/Row Numbers"
16614 org-table-toggle-coordinate-overlays
16615 :style toggle
16616 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
16617 "--"
16618 ["Create" org-table-create (and (not (org-at-table-p))
16619 org-enable-table-editor)]
16620 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
16621 ["Import from File" org-table-import (not (org-at-table-p))]
16622 ["Export to File" org-table-export (org-at-table-p)]
16623 "--"
16624 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
16626 (easy-menu-define org-org-menu org-mode-map "Org menu"
16627 '("Org"
16628 ("Show/Hide"
16629 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
16630 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
16631 ["Sparse Tree..." org-sparse-tree t]
16632 ["Reveal Context" org-reveal t]
16633 ["Show All" show-all t]
16634 "--"
16635 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
16636 "--"
16637 ["New Heading" org-insert-heading t]
16638 ("Navigate Headings"
16639 ["Up" outline-up-heading t]
16640 ["Next" outline-next-visible-heading t]
16641 ["Previous" outline-previous-visible-heading t]
16642 ["Next Same Level" outline-forward-same-level t]
16643 ["Previous Same Level" outline-backward-same-level t]
16644 "--"
16645 ["Jump" org-goto t])
16646 ("Edit Structure"
16647 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
16648 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
16649 "--"
16650 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
16651 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
16652 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
16653 "--"
16654 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
16655 "--"
16656 ["Promote Heading" org-metaleft (not (org-at-table-p))]
16657 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
16658 ["Demote Heading" org-metaright (not (org-at-table-p))]
16659 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
16660 "--"
16661 ["Sort Region/Children" org-sort (not (org-at-table-p))]
16662 "--"
16663 ["Convert to odd levels" org-convert-to-odd-levels t]
16664 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
16665 ("Editing"
16666 ["Emphasis..." org-emphasize t]
16667 ["Edit Source Example" org-edit-special t]
16668 "--"
16669 ["Footnote new/jump" org-footnote-action t]
16670 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
16671 ("Archive"
16672 ["Archive (default method)" org-archive-subtree-default t]
16673 "--"
16674 ["Move Subtree to Archive file" org-advertized-archive-subtree t]
16675 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
16676 ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
16678 "--"
16679 ("Hyperlinks"
16680 ["Store Link (Global)" org-store-link t]
16681 ["Find existing link to here" org-occur-link-in-agenda-files t]
16682 ["Insert Link" org-insert-link t]
16683 ["Follow Link" org-open-at-point t]
16684 "--"
16685 ["Next link" org-next-link t]
16686 ["Previous link" org-previous-link t]
16687 "--"
16688 ["Descriptive Links"
16689 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
16690 :style radio
16691 :selected (member '(org-link) buffer-invisibility-spec)]
16692 ["Literal Links"
16693 (progn
16694 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
16695 :style radio
16696 :selected (not (member '(org-link) buffer-invisibility-spec))])
16697 "--"
16698 ("TODO Lists"
16699 ["TODO/DONE/-" org-todo t]
16700 ("Select keyword"
16701 ["Next keyword" org-shiftright (org-on-heading-p)]
16702 ["Previous keyword" org-shiftleft (org-on-heading-p)]
16703 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
16704 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
16705 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
16706 ["Show TODO Tree" org-show-todo-tree t]
16707 ["Global TODO list" org-todo-list t]
16708 "--"
16709 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
16710 :selected org-enforce-todo-dependencies :style toggle :active t]
16711 "Settings for tree at point"
16712 ["Do Children sequentially" org-toggle-ordered-property :style radio
16713 :selected (ignore-errors (org-entry-get nil "ORDERED"))
16714 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
16715 ["Do Children parallel" org-toggle-ordered-property :style radio
16716 :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
16717 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
16718 "--"
16719 ["Set Priority" org-priority t]
16720 ["Priority Up" org-shiftup t]
16721 ["Priority Down" org-shiftdown t]
16722 "--"
16723 ["Get news from all feeds" org-feed-update-all t]
16724 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
16725 ["Customize feeds" (customize-variable 'org-feed-alist) t])
16726 ("TAGS and Properties"
16727 ["Set Tags" org-set-tags-command t]
16728 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
16729 "--"
16730 ["Set property" org-set-property t]
16731 ["Column view of properties" org-columns t]
16732 ["Insert Column View DBlock" org-insert-columns-dblock t])
16733 ("Dates and Scheduling"
16734 ["Timestamp" org-time-stamp t]
16735 ["Timestamp (inactive)" org-time-stamp-inactive t]
16736 ("Change Date"
16737 ["1 Day Later" org-shiftright t]
16738 ["1 Day Earlier" org-shiftleft t]
16739 ["1 ... Later" org-shiftup t]
16740 ["1 ... Earlier" org-shiftdown t])
16741 ["Compute Time Range" org-evaluate-time-range t]
16742 ["Schedule Item" org-schedule t]
16743 ["Deadline" org-deadline t]
16744 "--"
16745 ["Custom time format" org-toggle-time-stamp-overlays
16746 :style radio :selected org-display-custom-times]
16747 "--"
16748 ["Goto Calendar" org-goto-calendar t]
16749 ["Date from Calendar" org-date-from-calendar t]
16750 "--"
16751 ["Start/Restart Timer" org-timer-start t]
16752 ["Pause/Continue Timer" org-timer-pause-or-continue t]
16753 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
16754 ["Insert Timer String" org-timer t]
16755 ["Insert Timer Item" org-timer-item t])
16756 ("Logging work"
16757 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
16758 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
16759 ["Clock out" org-clock-out t]
16760 ["Clock cancel" org-clock-cancel t]
16761 "--"
16762 ["Mark as default task" org-clock-mark-default-task t]
16763 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
16764 ["Goto running clock" org-clock-goto t]
16765 "--"
16766 ["Display times" org-clock-display t]
16767 ["Create clock table" org-clock-report t]
16768 "--"
16769 ["Record DONE time"
16770 (progn (setq org-log-done (not org-log-done))
16771 (message "Switching to %s will %s record a timestamp"
16772 (car org-done-keywords)
16773 (if org-log-done "automatically" "not")))
16774 :style toggle :selected org-log-done])
16775 "--"
16776 ["Agenda Command..." org-agenda t]
16777 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
16778 ("File List for Agenda")
16779 ("Special views current file"
16780 ["TODO Tree" org-show-todo-tree t]
16781 ["Check Deadlines" org-check-deadlines t]
16782 ["Timeline" org-timeline t]
16783 ["Tags/Property tree" org-match-sparse-tree t])
16784 "--"
16785 ["Export/Publish..." org-export t]
16786 ("LaTeX"
16787 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
16788 :selected org-cdlatex-mode]
16789 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
16790 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
16791 ["Modify math symbol" org-cdlatex-math-modify
16792 (org-inside-LaTeX-fragment-p)]
16793 ["Insert citation" org-reftex-citation t]
16794 "--"
16795 ["Export LaTeX fragments as images"
16796 (if (featurep 'org-exp)
16797 (setq org-export-with-LaTeX-fragments
16798 (not org-export-with-LaTeX-fragments))
16799 (require 'org-exp))
16800 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
16801 org-export-with-LaTeX-fragments)]
16802 "--"
16803 ["Template for BEAMER" org-beamer-settings-template t])
16804 "--"
16805 ("MobileOrg"
16806 ["Push Files and Views" org-mobile-push t]
16807 ["Get Captured and Flagged" org-mobile-pull t]
16808 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
16809 "--"
16810 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
16811 "--"
16812 ("Documentation"
16813 ["Show Version" org-version t]
16814 ["Info Documentation" org-info t])
16815 ("Customize"
16816 ["Browse Org Group" org-customize t]
16817 "--"
16818 ["Expand This Menu" org-create-customize-menu
16819 (fboundp 'customize-menu-create)])
16820 ["Send bug report" org-submit-bug-report t]
16821 "--"
16822 ("Refresh/Reload"
16823 ["Refresh setup current buffer" org-mode-restart t]
16824 ["Reload Org (after update)" org-reload t]
16825 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
16828 (defun org-info (&optional node)
16829 "Read documentation for Org-mode in the info system.
16830 With optional NODE, go directly to that node."
16831 (interactive)
16832 (info (format "(org)%s" (or node ""))))
16834 ;;;###autoload
16835 (defun org-submit-bug-report ()
16836 "Submit a bug report on Org-mode via mail.
16838 Don't hesitate to report any problems or inaccurate documentation.
16840 If you don't have setup sending mail from (X)Emacs, please copy the
16841 output buffer into your mail program, as it gives us important
16842 information about your Org-mode version and configuration."
16843 (interactive)
16844 (require 'reporter)
16845 (org-load-modules-maybe)
16846 (org-require-autoloaded-modules)
16847 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
16848 (reporter-submit-bug-report
16849 "emacs-orgmode@gnu.org"
16850 (org-version)
16851 (let (list)
16852 (save-window-excursion
16853 (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
16854 (delete-other-windows)
16855 (erase-buffer)
16856 (insert "You are about to submit a bug report to the Org-mode mailing list.
16858 We would like to add your full Org-mode and Outline configuration to the
16859 bug report. This greatly simplifies the work of the maintainer and
16860 other experts on the mailing list.
16862 HOWEVER, some variables you have customized may contain private
16863 information. The names of customers, colleagues, or friends, might
16864 appear in the form of file names, tags, todo states, or search strings.
16865 If you answer yes to the prompt, you might want to check and remove
16866 such private information before sending the email.")
16867 (add-text-properties (point-min) (point-max) '(face org-warning))
16868 (when (yes-or-no-p "Include your Org-mode configuration ")
16869 (mapatoms
16870 (lambda (v)
16871 (and (boundp v)
16872 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
16873 (or (and (symbol-value v)
16874 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
16875 (and
16876 (get v 'custom-type) (get v 'standard-value)
16877 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
16878 (push v list)))))
16879 (kill-buffer (get-buffer "*Warn about privacy*"))
16880 list))
16881 nil nil
16882 "Remember to cover the basics, that is, what you expected to happen and
16883 what in fact did happen. You don't know how to make a good report? See
16885 http://orgmode.org/manual/Feedback.html#Feedback
16887 Your bug report will be posted to the Org-mode mailing list.
16888 ------------------------------------------------------------------------")
16889 (save-excursion
16890 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
16891 (replace-match "\\1Bug: \\3 [\\2]")))))
16894 (defun org-install-agenda-files-menu ()
16895 (let ((bl (buffer-list)))
16896 (save-excursion
16897 (while bl
16898 (set-buffer (pop bl))
16899 (if (org-mode-p) (setq bl nil)))
16900 (when (org-mode-p)
16901 (easy-menu-change
16902 '("Org") "File List for Agenda"
16903 (append
16904 (list
16905 ["Edit File List" (org-edit-agenda-file-list) t]
16906 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
16907 ["Remove Current File from List" org-remove-file t]
16908 ["Cycle through agenda files" org-cycle-agenda-files t]
16909 ["Occur in all agenda files" org-occur-in-agenda-files t]
16910 "--")
16911 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
16913 ;;;; Documentation
16915 ;;;###autoload
16916 (defun org-require-autoloaded-modules ()
16917 (interactive)
16918 (mapc 'require
16919 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
16920 org-docbook org-exp org-html org-icalendar
16921 org-id org-latex
16922 org-publish org-remember org-table
16923 org-timer org-xoxo)))
16925 ;;;###autoload
16926 (defun org-reload (&optional uncompiled)
16927 "Reload all org lisp files.
16928 With prefix arg UNCOMPILED, load the uncompiled versions."
16929 (interactive "P")
16930 (require 'find-func)
16931 (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
16932 (dir-org (file-name-directory (org-find-library-name "org")))
16933 (dir-org-contrib (ignore-errors
16934 (file-name-directory
16935 (org-find-library-name "org-contribdir"))))
16936 (files
16937 (append (directory-files dir-org t file-re)
16938 (and dir-org-contrib
16939 (directory-files dir-org-contrib t file-re))))
16940 (remove-re (concat (if (featurep 'xemacs)
16941 "org-colview" "org-colview-xemacs")
16942 "\\'")))
16943 (setq files (mapcar 'file-name-sans-extension files))
16944 (setq files (mapcar
16945 (lambda (x) (if (string-match remove-re x) nil x))
16946 files))
16947 (setq files (delq nil files))
16948 (mapc
16949 (lambda (f)
16950 (when (featurep (intern (file-name-nondirectory f)))
16951 (if (and (not uncompiled)
16952 (file-exists-p (concat f ".elc")))
16953 (load (concat f ".elc") nil nil t)
16954 (load (concat f ".el") nil nil t))))
16955 files))
16956 (org-version))
16958 ;;;###autoload
16959 (defun org-customize ()
16960 "Call the customize function with org as argument."
16961 (interactive)
16962 (org-load-modules-maybe)
16963 (org-require-autoloaded-modules)
16964 (customize-browse 'org))
16966 (defun org-create-customize-menu ()
16967 "Create a full customization menu for Org-mode, insert it into the menu."
16968 (interactive)
16969 (org-load-modules-maybe)
16970 (org-require-autoloaded-modules)
16971 (if (fboundp 'customize-menu-create)
16972 (progn
16973 (easy-menu-change
16974 '("Org") "Customize"
16975 `(["Browse Org group" org-customize t]
16976 "--"
16977 ,(customize-menu-create 'org)
16978 ["Set" Custom-set t]
16979 ["Save" Custom-save t]
16980 ["Reset to Current" Custom-reset-current t]
16981 ["Reset to Saved" Custom-reset-saved t]
16982 ["Reset to Standard Settings" Custom-reset-standard t]))
16983 (message "\"Org\"-menu now contains full customization menu"))
16984 (error "Cannot expand menu (outdated version of cus-edit.el)")))
16986 ;;;; Miscellaneous stuff
16988 ;;; Generally useful functions
16990 (defun org-get-at-bol (property)
16991 "Get text property PROPERTY at beginning of line."
16992 (get-text-property (point-at-bol) property))
16994 (defun org-find-text-property-in-string (prop s)
16995 "Return the first non-nil value of property PROP in string S."
16996 (or (get-text-property 0 prop s)
16997 (get-text-property (or (next-single-property-change 0 prop s) 0)
16998 prop s)))
17000 (defun org-display-warning (message) ;; Copied from Emacs-Muse
17001 "Display the given MESSAGE as a warning."
17002 (if (fboundp 'display-warning)
17003 (display-warning 'org message
17004 (if (featurep 'xemacs)
17005 'warning
17006 :warning))
17007 (let ((buf (get-buffer-create "*Org warnings*")))
17008 (with-current-buffer buf
17009 (goto-char (point-max))
17010 (insert "Warning (Org): " message)
17011 (unless (bolp)
17012 (newline)))
17013 (display-buffer buf)
17014 (sit-for 0))))
17016 (defun org-in-commented-line ()
17017 "Is point in a line starting with `#'?"
17018 (equal (char-after (point-at-bol)) ?#))
17020 (defun org-in-verbatim-emphasis ()
17021 (save-match-data
17022 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
17024 (defun org-goto-marker-or-bmk (marker &optional bookmark)
17025 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
17026 (if (and marker (marker-buffer marker)
17027 (buffer-live-p (marker-buffer marker)))
17028 (progn
17029 (switch-to-buffer (marker-buffer marker))
17030 (if (or (> marker (point-max)) (< marker (point-min)))
17031 (widen))
17032 (goto-char marker)
17033 (org-show-context 'org-goto))
17034 (if bookmark
17035 (bookmark-jump bookmark)
17036 (error "Cannot find location"))))
17038 (defun org-quote-csv-field (s)
17039 "Quote field for inclusion in CSV material."
17040 (if (string-match "[\",]" s)
17041 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
17044 (defun org-plist-delete (plist property)
17045 "Delete PROPERTY from PLIST.
17046 This is in contrast to merely setting it to 0."
17047 (let (p)
17048 (while plist
17049 (if (not (eq property (car plist)))
17050 (setq p (plist-put p (car plist) (nth 1 plist))))
17051 (setq plist (cddr plist)))
17054 (defun org-force-self-insert (N)
17055 "Needed to enforce self-insert under remapping."
17056 (interactive "p")
17057 (self-insert-command N))
17059 (defun org-string-width (s)
17060 "Compute width of string, ignoring invisible characters.
17061 This ignores character with invisibility property `org-link', and also
17062 characters with property `org-cwidth', because these will become invisible
17063 upon the next fontification round."
17064 (let (b l)
17065 (when (or (eq t buffer-invisibility-spec)
17066 (assq 'org-link buffer-invisibility-spec))
17067 (while (setq b (text-property-any 0 (length s)
17068 'invisible 'org-link s))
17069 (setq s (concat (substring s 0 b)
17070 (substring s (or (next-single-property-change
17071 b 'invisible s) (length s)))))))
17072 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
17073 (setq s (concat (substring s 0 b)
17074 (substring s (or (next-single-property-change
17075 b 'org-cwidth s) (length s))))))
17076 (setq l (string-width s) b -1)
17077 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
17078 (setq l (- l (get-text-property b 'org-dwidth-n s))))
17081 (defun org-get-indentation (&optional line)
17082 "Get the indentation of the current line, interpreting tabs.
17083 When LINE is given, assume it represents a line and compute its indentation."
17084 (if line
17085 (if (string-match "^ *" (org-remove-tabs line))
17086 (match-end 0))
17087 (save-excursion
17088 (beginning-of-line 1)
17089 (skip-chars-forward " \t")
17090 (current-column))))
17092 (defun org-remove-tabs (s &optional width)
17093 "Replace tabulators in S with spaces.
17094 Assumes that s is a single line, starting in column 0."
17095 (setq width (or width tab-width))
17096 (while (string-match "\t" s)
17097 (setq s (replace-match
17098 (make-string
17099 (- (* width (/ (+ (match-beginning 0) width) width))
17100 (match-beginning 0)) ?\ )
17101 t t s)))
17104 (defun org-fix-indentation (line ind)
17105 "Fix indentation in LINE.
17106 IND is a cons cell with target and minimum indentation.
17107 If the current indentation in LINE is smaller than the minimum,
17108 leave it alone. If it is larger than ind, set it to the target."
17109 (let* ((l (org-remove-tabs line))
17110 (i (org-get-indentation l))
17111 (i1 (car ind)) (i2 (cdr ind)))
17112 (if (>= i i2) (setq l (substring line i2)))
17113 (if (> i1 0)
17114 (concat (make-string i1 ?\ ) l)
17115 l)))
17117 (defun org-remove-indentation (code &optional n)
17118 "Remove the maximum common indentation from the lines in CODE.
17119 N may optionally be the number of spaces to remove."
17120 (with-temp-buffer
17121 (insert code)
17122 (org-do-remove-indentation n)
17123 (buffer-string)))
17125 (defun org-do-remove-indentation (&optional n)
17126 "Remove the maximum common indentation from the buffer."
17127 (untabify (point-min) (point-max))
17128 (let ((min 10000) re)
17129 (if n
17130 (setq min n)
17131 (goto-char (point-min))
17132 (while (re-search-forward "^ *[^ \n]" nil t)
17133 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
17134 (unless (or (= min 0) (= min 10000))
17135 (setq re (format "^ \\{%d\\}" min))
17136 (goto-char (point-min))
17137 (while (re-search-forward re nil t)
17138 (replace-match "")
17139 (end-of-line 1))
17140 min)))
17142 (defun org-fill-template (template alist)
17143 "Find each %key of ALIST in TEMPLATE and replace it."
17144 (let ((case-fold-search nil)
17145 entry key value)
17146 (setq alist (sort (copy-sequence alist)
17147 (lambda (a b) (< (length (car a)) (length (car b))))))
17148 (while (setq entry (pop alist))
17149 (setq template
17150 (replace-regexp-in-string
17151 (concat "%" (regexp-quote (car entry)))
17152 (cdr entry) template t t)))
17153 template))
17155 (defun org-base-buffer (buffer)
17156 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
17157 (if (not buffer)
17158 buffer
17159 (or (buffer-base-buffer buffer)
17160 buffer)))
17162 (defun org-trim (s)
17163 "Remove whitespace at beginning and end of string."
17164 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
17165 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
17168 (defun org-wrap (string &optional width lines)
17169 "Wrap string to either a number of lines, or a width in characters.
17170 If WIDTH is non-nil, the string is wrapped to that width, however many lines
17171 that costs. If there is a word longer than WIDTH, the text is actually
17172 wrapped to the length of that word.
17173 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
17174 many lines, whatever width that takes.
17175 The return value is a list of lines, without newlines at the end."
17176 (let* ((words (org-split-string string "[ \t\n]+"))
17177 (maxword (apply 'max (mapcar 'org-string-width words)))
17178 w ll)
17179 (cond (width
17180 (org-do-wrap words (max maxword width)))
17181 (lines
17182 (setq w maxword)
17183 (setq ll (org-do-wrap words maxword))
17184 (if (<= (length ll) lines)
17186 (setq ll words)
17187 (while (> (length ll) lines)
17188 (setq w (1+ w))
17189 (setq ll (org-do-wrap words w)))
17190 ll))
17191 (t (error "Cannot wrap this")))))
17193 (defun org-do-wrap (words width)
17194 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
17195 (let (lines line)
17196 (while words
17197 (setq line (pop words))
17198 (while (and words (< (+ (length line) (length (car words))) width))
17199 (setq line (concat line " " (pop words))))
17200 (setq lines (push line lines)))
17201 (nreverse lines)))
17203 (defun org-split-string (string &optional separators)
17204 "Splits STRING into substrings at SEPARATORS.
17205 No empty strings are returned if there are matches at the beginning
17206 and end of string."
17207 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
17208 (start 0)
17209 notfirst
17210 (list nil))
17211 (while (and (string-match rexp string
17212 (if (and notfirst
17213 (= start (match-beginning 0))
17214 (< start (length string)))
17215 (1+ start) start))
17216 (< (match-beginning 0) (length string)))
17217 (setq notfirst t)
17218 (or (eq (match-beginning 0) 0)
17219 (and (eq (match-beginning 0) (match-end 0))
17220 (eq (match-beginning 0) start))
17221 (setq list
17222 (cons (substring string start (match-beginning 0))
17223 list)))
17224 (setq start (match-end 0)))
17225 (or (eq start (length string))
17226 (setq list
17227 (cons (substring string start)
17228 list)))
17229 (nreverse list)))
17231 (defun org-quote-vert (s)
17232 "Replace \"|\" with \"\\vert\"."
17233 (while (string-match "|" s)
17234 (setq s (replace-match "\\vert" t t s)))
17237 (defun org-uuidgen-p (s)
17238 "Is S an ID created by UUIDGEN?"
17239 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
17241 (defun org-context ()
17242 "Return a list of contexts of the current cursor position.
17243 If several contexts apply, all are returned.
17244 Each context entry is a list with a symbol naming the context, and
17245 two positions indicating start and end of the context. Possible
17246 contexts are:
17248 :headline anywhere in a headline
17249 :headline-stars on the leading stars in a headline
17250 :todo-keyword on a TODO keyword (including DONE) in a headline
17251 :tags on the TAGS in a headline
17252 :priority on the priority cookie in a headline
17253 :item on the first line of a plain list item
17254 :item-bullet on the bullet/number of a plain list item
17255 :checkbox on the checkbox in a plain list item
17256 :table in an org-mode table
17257 :table-special on a special filed in a table
17258 :table-table in a table.el table
17259 :link on a hyperlink
17260 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
17261 :target on a <<target>>
17262 :radio-target on a <<<radio-target>>>
17263 :latex-fragment on a LaTeX fragment
17264 :latex-preview on a LaTeX fragment with overlayed preview image
17266 This function expects the position to be visible because it uses font-lock
17267 faces as a help to recognize the following contexts: :table-special, :link,
17268 and :keyword."
17269 (let* ((f (get-text-property (point) 'face))
17270 (faces (if (listp f) f (list f)))
17271 (p (point)) clist o)
17272 ;; First the large context
17273 (cond
17274 ((org-on-heading-p t)
17275 (push (list :headline (point-at-bol) (point-at-eol)) clist)
17276 (when (progn
17277 (beginning-of-line 1)
17278 (looking-at org-todo-line-tags-regexp))
17279 (push (org-point-in-group p 1 :headline-stars) clist)
17280 (push (org-point-in-group p 2 :todo-keyword) clist)
17281 (push (org-point-in-group p 4 :tags) clist))
17282 (goto-char p)
17283 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
17284 (if (looking-at "\\[#[A-Z0-9]\\]")
17285 (push (org-point-in-group p 0 :priority) clist)))
17287 ((org-at-item-p)
17288 (push (org-point-in-group p 2 :item-bullet) clist)
17289 (push (list :item (point-at-bol)
17290 (save-excursion (org-end-of-item) (point)))
17291 clist)
17292 (and (org-at-item-checkbox-p)
17293 (push (org-point-in-group p 0 :checkbox) clist)))
17295 ((org-at-table-p)
17296 (push (list :table (org-table-begin) (org-table-end)) clist)
17297 (if (memq 'org-formula faces)
17298 (push (list :table-special
17299 (previous-single-property-change p 'face)
17300 (next-single-property-change p 'face)) clist)))
17301 ((org-at-table-p 'any)
17302 (push (list :table-table) clist)))
17303 (goto-char p)
17305 ;; Now the small context
17306 (cond
17307 ((org-at-timestamp-p)
17308 (push (org-point-in-group p 0 :timestamp) clist))
17309 ((memq 'org-link faces)
17310 (push (list :link
17311 (previous-single-property-change p 'face)
17312 (next-single-property-change p 'face)) clist))
17313 ((memq 'org-special-keyword faces)
17314 (push (list :keyword
17315 (previous-single-property-change p 'face)
17316 (next-single-property-change p 'face)) clist))
17317 ((org-on-target-p)
17318 (push (org-point-in-group p 0 :target) clist)
17319 (goto-char (1- (match-beginning 0)))
17320 (if (looking-at org-radio-target-regexp)
17321 (push (org-point-in-group p 0 :radio-target) clist))
17322 (goto-char p))
17323 ((setq o (car (delq nil
17324 (mapcar
17325 (lambda (x)
17326 (if (memq x org-latex-fragment-image-overlays) x))
17327 (org-overlays-at (point))))))
17328 (push (list :latex-fragment
17329 (overlay-start o) (overlay-end o)) clist)
17330 (push (list :latex-preview
17331 (overlay-start o) (overlay-end o)) clist))
17332 ((org-inside-LaTeX-fragment-p)
17333 ;; FIXME: positions wrong.
17334 (push (list :latex-fragment (point) (point)) clist)))
17336 (setq clist (nreverse (delq nil clist)))
17337 clist))
17339 ;; FIXME: Compare with at-regexp-p Do we need both?
17340 (defun org-in-regexp (re &optional nlines visually)
17341 "Check if point is inside a match of regexp.
17342 Normally only the current line is checked, but you can include NLINES extra
17343 lines both before and after point into the search.
17344 If VISUALLY is set, require that the cursor is not after the match but
17345 really on, so that the block visually is on the match."
17346 (catch 'exit
17347 (let ((pos (point))
17348 (eol (point-at-eol (+ 1 (or nlines 0))))
17349 (inc (if visually 1 0)))
17350 (save-excursion
17351 (beginning-of-line (- 1 (or nlines 0)))
17352 (while (re-search-forward re eol t)
17353 (if (and (<= (match-beginning 0) pos)
17354 (>= (+ inc (match-end 0)) pos))
17355 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
17357 (defun org-at-regexp-p (regexp)
17358 "Is point inside a match of REGEXP in the current line?"
17359 (catch 'exit
17360 (save-excursion
17361 (let ((pos (point)) (end (point-at-eol)))
17362 (beginning-of-line 1)
17363 (while (re-search-forward regexp end t)
17364 (if (and (<= (match-beginning 0) pos)
17365 (>= (match-end 0) pos))
17366 (throw 'exit t)))
17367 nil))))
17369 (defun org-in-regexps-block-p (start-re end-re)
17370 "Returns t if the current point is between matches of START-RE and END-RE.
17371 This will also return to if point is on one of the two matches."
17372 (interactive)
17373 (let ((p (point)))
17374 (save-excursion
17375 (and (or (org-at-regexp-p start-re)
17376 (re-search-backward start-re nil t))
17377 (re-search-forward end-re nil t)
17378 (>= (point) p)))))
17380 (defun org-occur-in-agenda-files (regexp &optional nlines)
17381 "Call `multi-occur' with buffers for all agenda files."
17382 (interactive "sOrg-files matching: \np")
17383 (let* ((files (org-agenda-files))
17384 (tnames (mapcar 'file-truename files))
17385 (extra org-agenda-text-search-extra-files)
17387 (when (eq (car extra) 'agenda-archives)
17388 (setq extra (cdr extra))
17389 (setq files (org-add-archive-files files)))
17390 (while (setq f (pop extra))
17391 (unless (member (file-truename f) tnames)
17392 (add-to-list 'files f 'append)
17393 (add-to-list 'tnames (file-truename f) 'append)))
17394 (multi-occur
17395 (mapcar (lambda (x)
17396 (with-current-buffer
17397 (or (get-file-buffer x) (find-file-noselect x))
17398 (widen)
17399 (current-buffer)))
17400 files)
17401 regexp)))
17403 (if (boundp 'occur-mode-find-occurrence-hook)
17404 ;; Emacs 23
17405 (add-hook 'occur-mode-find-occurrence-hook
17406 (lambda ()
17407 (when (org-mode-p)
17408 (org-reveal))))
17409 ;; Emacs 22
17410 (defadvice occur-mode-goto-occurrence
17411 (after org-occur-reveal activate)
17412 (and (org-mode-p) (org-reveal)))
17413 (defadvice occur-mode-goto-occurrence-other-window
17414 (after org-occur-reveal activate)
17415 (and (org-mode-p) (org-reveal)))
17416 (defadvice occur-mode-display-occurrence
17417 (after org-occur-reveal activate)
17418 (when (org-mode-p)
17419 (let ((pos (occur-mode-find-occurrence)))
17420 (with-current-buffer (marker-buffer pos)
17421 (save-excursion
17422 (goto-char pos)
17423 (org-reveal)))))))
17425 (defun org-occur-link-in-agenda-files ()
17426 "Create a link and search for it in the agendas.
17427 The link is not stored in `org-stored-links', it is just created
17428 for the search purpose."
17429 (interactive)
17430 (let ((link (condition-case nil
17431 (org-store-link nil)
17432 (error "Unable to create a link to here"))))
17433 (org-occur-in-agenda-files (regexp-quote link))))
17435 (defun org-uniquify (list)
17436 "Remove duplicate elements from LIST."
17437 (let (res)
17438 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
17439 res))
17441 (defun org-delete-all (elts list)
17442 "Remove all elements in ELTS from LIST."
17443 (while elts
17444 (setq list (delete (pop elts) list)))
17445 list)
17447 (defun org-remove-if (predicate seq)
17448 "Remove everything from SEQ that fulfills PREDICATE."
17449 (let (res e)
17450 (while seq
17451 (setq e (pop seq))
17452 (if (not (funcall predicate e)) (push e res)))
17453 (nreverse res)))
17455 (defun org-remove-if-not (predicate seq)
17456 "Remove everything from SEQ that does not fulfill PREDICATE."
17457 (let (res e)
17458 (while seq
17459 (setq e (pop seq))
17460 (if (funcall predicate e) (push e res)))
17461 (nreverse res)))
17463 (defun org-back-over-empty-lines ()
17464 "Move backwards over whitespace, to the beginning of the first empty line.
17465 Returns the number of empty lines passed."
17466 (let ((pos (point)))
17467 (skip-chars-backward " \t\n\r")
17468 (beginning-of-line 2)
17469 (goto-char (min (point) pos))
17470 (count-lines (point) pos)))
17472 (defun org-skip-whitespace ()
17473 (skip-chars-forward " \t\n\r"))
17475 (defun org-point-in-group (point group &optional context)
17476 "Check if POINT is in match-group GROUP.
17477 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
17478 match. If the match group does ot exist or point is not inside it,
17479 return nil."
17480 (and (match-beginning group)
17481 (>= point (match-beginning group))
17482 (<= point (match-end group))
17483 (if context
17484 (list context (match-beginning group) (match-end group))
17485 t)))
17487 (defun org-switch-to-buffer-other-window (&rest args)
17488 "Switch to buffer in a second window on the current frame.
17489 In particular, do not allow pop-up frames."
17490 (let (pop-up-frames special-display-buffer-names special-display-regexps
17491 special-display-function)
17492 (apply 'switch-to-buffer-other-window args)))
17494 (defun org-combine-plists (&rest plists)
17495 "Create a single property list from all plists in PLISTS.
17496 The process starts by copying the first list, and then setting properties
17497 from the other lists. Settings in the last list are the most significant
17498 ones and overrule settings in the other lists."
17499 (let ((rtn (copy-sequence (pop plists)))
17500 p v ls)
17501 (while plists
17502 (setq ls (pop plists))
17503 (while ls
17504 (setq p (pop ls) v (pop ls))
17505 (setq rtn (plist-put rtn p v))))
17506 rtn))
17508 (defun org-move-line-down (arg)
17509 "Move the current line down. With prefix argument, move it past ARG lines."
17510 (interactive "p")
17511 (let ((col (current-column))
17512 beg end pos)
17513 (beginning-of-line 1) (setq beg (point))
17514 (beginning-of-line 2) (setq end (point))
17515 (beginning-of-line (+ 1 arg))
17516 (setq pos (move-marker (make-marker) (point)))
17517 (insert (delete-and-extract-region beg end))
17518 (goto-char pos)
17519 (org-move-to-column col)))
17521 (defun org-move-line-up (arg)
17522 "Move the current line up. With prefix argument, move it past ARG lines."
17523 (interactive "p")
17524 (let ((col (current-column))
17525 beg end pos)
17526 (beginning-of-line 1) (setq beg (point))
17527 (beginning-of-line 2) (setq end (point))
17528 (beginning-of-line (- arg))
17529 (setq pos (move-marker (make-marker) (point)))
17530 (insert (delete-and-extract-region beg end))
17531 (goto-char pos)
17532 (org-move-to-column col)))
17534 (defun org-replace-escapes (string table)
17535 "Replace %-escapes in STRING with values in TABLE.
17536 TABLE is an association list with keys like \"%a\" and string values.
17537 The sequences in STRING may contain normal field width and padding information,
17538 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
17539 so values can contain further %-escapes if they are define later in TABLE."
17540 (let ((case-fold-search nil)
17541 e re rpl)
17542 (while (setq e (pop table))
17543 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
17544 (while (string-match re string)
17545 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
17546 (cdr e)))
17547 (setq string (replace-match rpl t t string))))
17548 string))
17551 (defun org-sublist (list start end)
17552 "Return a section of LIST, from START to END.
17553 Counting starts at 1."
17554 (let (rtn (c start))
17555 (setq list (nthcdr (1- start) list))
17556 (while (and list (<= c end))
17557 (push (pop list) rtn)
17558 (setq c (1+ c)))
17559 (nreverse rtn)))
17561 (defun org-find-base-buffer-visiting (file)
17562 "Like `find-buffer-visiting' but always return the base buffer and
17563 not an indirect buffer."
17564 (let ((buf (or (get-file-buffer file)
17565 (find-buffer-visiting file))))
17566 (if buf
17567 (or (buffer-base-buffer buf) buf)
17568 nil)))
17570 (defun org-image-file-name-regexp (&optional extensions)
17571 "Return regexp matching the file names of images.
17572 If EXTENSIONS is given, only match these."
17573 (if (and (not extensions) (fboundp 'image-file-name-regexp))
17574 (image-file-name-regexp)
17575 (let ((image-file-name-extensions
17576 (or extensions
17577 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
17578 "xbm" "xpm" "pbm" "pgm" "ppm"))))
17579 (concat "\\."
17580 (regexp-opt (nconc (mapcar 'upcase
17581 image-file-name-extensions)
17582 image-file-name-extensions)
17584 "\\'"))))
17586 (defun org-file-image-p (file &optional extensions)
17587 "Return non-nil if FILE is an image."
17588 (save-match-data
17589 (string-match (org-image-file-name-regexp extensions) file)))
17591 (defun org-get-cursor-date ()
17592 "Return the date at cursor in as a time.
17593 This works in the calendar and in the agenda, anywhere else it just
17594 returns the current time."
17595 (let (date day defd)
17596 (cond
17597 ((eq major-mode 'calendar-mode)
17598 (setq date (calendar-cursor-to-date)
17599 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
17600 ((eq major-mode 'org-agenda-mode)
17601 (setq day (get-text-property (point) 'day))
17602 (if day
17603 (setq date (calendar-gregorian-from-absolute day)
17604 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
17605 (nth 2 date))))))
17606 (or defd (current-time))))
17608 (defvar org-agenda-action-marker (make-marker)
17609 "Marker pointing to the entry for the next agenda action.")
17611 (defun org-mark-entry-for-agenda-action ()
17612 "Mark the current entry as target of an agenda action.
17613 Agenda actions are actions executed from the agenda with the key `k',
17614 which make use of the date at the cursor."
17615 (interactive)
17616 (move-marker org-agenda-action-marker
17617 (save-excursion (org-back-to-heading t) (point))
17618 (current-buffer))
17619 (message
17620 "Entry marked for action; press `k' at desired date in agenda or calendar"))
17622 ;;; Paragraph filling stuff.
17623 ;; We want this to be just right, so use the full arsenal.
17625 (defun org-indent-line-function ()
17626 "Indent line like previous, but further if previous was headline or item."
17627 (interactive)
17628 (let* ((pos (point))
17629 (itemp (org-at-item-p))
17630 (case-fold-search t)
17631 (org-drawer-regexp (or org-drawer-regexp "\000"))
17632 column bpos bcol tpos tcol bullet btype bullet-type)
17633 ;; Find the previous relevant line
17634 (beginning-of-line 1)
17635 (cond
17636 ((looking-at "#") (setq column 0))
17637 ((looking-at "\\*+ ") (setq column 0))
17638 ((and (looking-at "[ \t]*:END:")
17639 (save-excursion (re-search-backward org-drawer-regexp nil t)))
17640 (save-excursion
17641 (goto-char (1- (match-beginning 1)))
17642 (setq column (current-column))))
17643 ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
17644 (save-excursion
17645 (re-search-backward
17646 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
17647 (setq column (org-get-indentation (match-string 0))))
17649 (beginning-of-line 0)
17650 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
17651 (not (looking-at "[ \t]*:END:"))
17652 (not (looking-at org-drawer-regexp)))
17653 (beginning-of-line 0))
17654 (cond
17655 ((looking-at "\\*+[ \t]+")
17656 (if (not org-adapt-indentation)
17657 (setq column 0)
17658 (goto-char (match-end 0))
17659 (setq column (current-column))))
17660 ((looking-at org-drawer-regexp)
17661 (goto-char (1- (match-beginning 1)))
17662 (setq column (current-column)))
17663 ((looking-at "\\([ \t]*\\):END:")
17664 (goto-char (match-end 1))
17665 (setq column (current-column)))
17666 ((org-in-item-p)
17667 (org-beginning-of-item)
17668 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
17669 (setq bpos (match-beginning 1) tpos (match-end 0)
17670 bcol (progn (goto-char bpos) (current-column))
17671 tcol (progn (goto-char tpos) (current-column))
17672 bullet (match-string 1)
17673 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
17674 (if (> tcol (+ bcol org-description-max-indent))
17675 (setq tcol (+ bcol 5)))
17676 (if (not itemp)
17677 (setq column tcol)
17678 (goto-char pos)
17679 (beginning-of-line 1)
17680 (if (looking-at "\\S-")
17681 (progn
17682 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
17683 (setq bullet (match-string 1)
17684 btype (if (string-match "[0-9]" bullet) "n" bullet))
17685 (setq column (if (equal btype bullet-type) bcol tcol)))
17686 (setq column (org-get-indentation)))))
17687 (t (setq column (org-get-indentation))))))
17688 (goto-char pos)
17689 (if (<= (current-column) (current-indentation))
17690 (org-indent-line-to column)
17691 (save-excursion (org-indent-line-to column)))
17692 (setq column (current-column))
17693 (beginning-of-line 1)
17694 (if (looking-at
17695 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
17696 (replace-match (concat (match-string 1)
17697 (format org-property-format
17698 (match-string 2) (match-string 3)))
17699 t t))
17700 (org-move-to-column column)))
17702 (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
17703 "Variable to store copy of `adaptive-fill-regexp'.
17704 Since `adaptive-fill-regexp' is set to never match, we need to
17705 store a backup of its value before entering `org-mode' so that
17706 the functionality can be provided as a fall-back.")
17708 (defun org-set-autofill-regexps ()
17709 (interactive)
17710 ;; In the paragraph separator we include headlines, because filling
17711 ;; text in a line directly attached to a headline would otherwise
17712 ;; fill the headline as well.
17713 (org-set-local 'comment-start-skip "^#+[ \t]*")
17714 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
17715 ;; The paragraph starter includes hand-formatted lists.
17716 (org-set-local
17717 'paragraph-start
17718 (concat
17719 "\f" "\\|"
17720 "[ ]*$" "\\|"
17721 "\\*+ " "\\|"
17722 "[ \t]*#" "\\|"
17723 "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
17724 "[ \t]*[:|]" "\\|"
17725 "\\$\\$" "\\|"
17726 "\\\\\\(begin\\|end\\|[][]\\)"))
17727 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
17728 ;; But only if the user has not turned off tables or fixed-width regions
17729 (org-set-local
17730 'auto-fill-inhibit-regexp
17731 (concat "\\*+ \\|#\\+"
17732 "\\|[ \t]*" org-keyword-time-regexp
17733 (if (or org-enable-table-editor org-enable-fixed-width-editor)
17734 (concat
17735 "\\|[ \t]*["
17736 (if org-enable-table-editor "|" "")
17737 (if org-enable-fixed-width-editor ":" "")
17738 "]"))))
17739 ;; We use our own fill-paragraph function, to make sure that tables
17740 ;; and fixed-width regions are not wrapped. That function will pass
17741 ;; through to `fill-paragraph' when appropriate.
17742 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
17743 ;; Adaptive filling: To get full control, first make sure that
17744 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
17745 (unless (local-variable-p 'adaptive-fill-regexp)
17746 (org-set-local 'org-adaptive-fill-regexp-backup
17747 adaptive-fill-regexp))
17748 (org-set-local 'adaptive-fill-regexp "\000")
17749 (org-set-local 'adaptive-fill-function
17750 'org-adaptive-fill-function)
17751 (org-set-local
17752 'align-mode-rules-list
17753 '((org-in-buffer-settings
17754 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
17755 (modes . '(org-mode))))))
17757 (defun org-fill-paragraph (&optional justify)
17758 "Re-align a table, pass through to fill-paragraph if no table."
17759 (let ((table-p (org-at-table-p))
17760 (table.el-p (org-at-table.el-p)))
17761 (cond ((and (equal (char-after (point-at-bol)) ?*)
17762 (save-excursion (goto-char (point-at-bol))
17763 (looking-at outline-regexp)))
17764 t) ; skip headlines
17765 (table.el-p t) ; skip table.el tables
17766 (table-p (org-table-align) t) ; align org-mode tables
17767 (t nil)))) ; call paragraph-fill
17769 ;; For reference, this is the default value of adaptive-fill-regexp
17770 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
17772 (defun org-adaptive-fill-function ()
17773 "Return a fill prefix for org-mode files.
17774 In particular, this makes sure hanging paragraphs for hand-formatted lists
17775 work correctly."
17776 (cond
17777 ;; Comment line
17778 ((looking-at "#[ \t]+")
17779 (match-string-no-properties 0))
17780 ;; Description list
17781 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
17782 (save-excursion
17783 (if (> (match-end 1) (+ (match-beginning 1)
17784 org-description-max-indent))
17785 (goto-char (+ (match-beginning 1) 5))
17786 (goto-char (match-end 0)))
17787 (make-string (current-column) ?\ )))
17788 ;; Ordered or unordered list
17789 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
17790 (save-excursion
17791 (goto-char (match-end 0))
17792 (make-string (current-column) ?\ )))
17793 ;; Other text
17794 ((looking-at org-adaptive-fill-regexp-backup)
17795 (match-string-no-properties 0))))
17797 ;;; Other stuff.
17799 (defun org-toggle-fixed-width-section (arg)
17800 "Toggle the fixed-width export.
17801 If there is no active region, the QUOTE keyword at the current headline is
17802 inserted or removed. When present, it causes the text between this headline
17803 and the next to be exported as fixed-width text, and unmodified.
17804 If there is an active region, this command adds or removes a colon as the
17805 first character of this line. If the first character of a line is a colon,
17806 this line is also exported in fixed-width font."
17807 (interactive "P")
17808 (let* ((cc 0)
17809 (regionp (org-region-active-p))
17810 (beg (if regionp (region-beginning) (point)))
17811 (end (if regionp (region-end)))
17812 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
17813 (case-fold-search nil)
17814 (re "[ \t]*\\(: \\)")
17815 off)
17816 (if regionp
17817 (save-excursion
17818 (goto-char beg)
17819 (setq cc (current-column))
17820 (beginning-of-line 1)
17821 (setq off (looking-at re))
17822 (while (> nlines 0)
17823 (setq nlines (1- nlines))
17824 (beginning-of-line 1)
17825 (cond
17826 (arg
17827 (org-move-to-column cc t)
17828 (insert ": \n")
17829 (forward-line -1))
17830 ((and off (looking-at re))
17831 (replace-match "" t t nil 1))
17832 ((not off) (org-move-to-column cc t) (insert ": ")))
17833 (forward-line 1)))
17834 (save-excursion
17835 (org-back-to-heading)
17836 (if (looking-at (concat outline-regexp
17837 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
17838 (replace-match "" t t nil 1)
17839 (if (looking-at outline-regexp)
17840 (progn
17841 (goto-char (match-end 0))
17842 (insert org-quote-string " "))))))))
17844 (defun org-reftex-citation ()
17845 "Use reftex-citation to insert a citation into the buffer.
17846 This looks for a line like
17848 #+BIBLIOGRAPHY: foo plain option:-d
17850 and derives from it that foo.bib is the bibliography file relevant
17851 for this document. It then installs the necessary environment for RefTeX
17852 to work in this buffer and calls `reftex-citation' to insert a citation
17853 into the buffer.
17855 Export of such citations to both LaTeX and HTML is handled by the contributed
17856 package org-exp-bibtex by Taru Karttunen."
17857 (interactive)
17858 (let ((reftex-docstruct-symbol 'rds)
17859 (reftex-cite-format "\\cite{%l}")
17860 rds bib)
17861 (save-excursion
17862 (save-restriction
17863 (widen)
17864 (let ((case-fold-search t)
17865 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
17866 (if (not (save-excursion
17867 (or (re-search-forward re nil t)
17868 (re-search-backward re nil t))))
17869 (error "No bibliography defined in file")
17870 (setq bib (concat (match-string 1) ".bib")
17871 rds (list (list 'bib bib)))))))
17872 (call-interactively 'reftex-citation)))
17874 ;;;; Functions extending outline functionality
17876 (defun org-beginning-of-line (&optional arg)
17877 "Go to the beginning of the current line. If that is invisible, continue
17878 to a visible line beginning. This makes the function of C-a more intuitive.
17879 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
17880 first attempt, and only move to after the tags when the cursor is already
17881 beyond the end of the headline."
17882 (interactive "P")
17883 (let ((pos (point))
17884 (special (if (consp org-special-ctrl-a/e)
17885 (car org-special-ctrl-a/e)
17886 org-special-ctrl-a/e))
17887 refpos)
17888 (if (org-bound-and-true-p line-move-visual)
17889 (beginning-of-visual-line 1)
17890 (beginning-of-line 1))
17891 (if (and arg (fboundp 'move-beginning-of-line))
17892 (call-interactively 'move-beginning-of-line)
17893 (if (bobp)
17895 (backward-char 1)
17896 (if (org-invisible-p)
17897 (while (and (not (bobp)) (org-invisible-p))
17898 (backward-char 1)
17899 (beginning-of-line 1))
17900 (forward-char 1))))
17901 (when special
17902 (cond
17903 ((and (looking-at org-complex-heading-regexp)
17904 (= (char-after (match-end 1)) ?\ ))
17905 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
17906 (point-at-eol)))
17907 (goto-char
17908 (if (eq special t)
17909 (cond ((> pos refpos) refpos)
17910 ((= pos (point)) refpos)
17911 (t (point)))
17912 (cond ((> pos (point)) (point))
17913 ((not (eq last-command this-command)) (point))
17914 (t refpos)))))
17915 ((org-at-item-p)
17916 (goto-char
17917 (if (eq special t)
17918 (cond ((> pos (match-end 4)) (match-end 4))
17919 ((= pos (point)) (match-end 4))
17920 (t (point)))
17921 (cond ((> pos (point)) (point))
17922 ((not (eq last-command this-command)) (point))
17923 (t (match-end 4))))))))
17924 (org-no-warnings
17925 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
17927 (defun org-end-of-line (&optional arg)
17928 "Go to the end of the line.
17929 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
17930 first attempt, and only move to after the tags when the cursor is already
17931 beyond the end of the headline."
17932 (interactive "P")
17933 (let ((special (if (consp org-special-ctrl-a/e)
17934 (cdr org-special-ctrl-a/e)
17935 org-special-ctrl-a/e)))
17936 (if (or (not special)
17937 (not (org-on-heading-p))
17938 arg)
17939 (call-interactively
17940 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
17941 ((fboundp 'move-end-of-line) 'move-end-of-line)
17942 (t 'end-of-line)))
17943 (let ((pos (point)))
17944 (beginning-of-line 1)
17945 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
17946 (if (eq special t)
17947 (if (or (< pos (match-beginning 1))
17948 (= pos (match-end 0)))
17949 (goto-char (match-beginning 1))
17950 (goto-char (match-end 0)))
17951 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
17952 (goto-char (match-end 0))
17953 (goto-char (match-beginning 1))))
17954 (call-interactively (if (fboundp 'move-end-of-line)
17955 'move-end-of-line
17956 'end-of-line)))))
17957 (org-no-warnings
17958 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
17960 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
17961 (define-key org-mode-map "\C-e" 'org-end-of-line)
17962 (define-key org-mode-map [home] 'org-beginning-of-line)
17963 (define-key org-mode-map [end] 'org-end-of-line)
17965 (defun org-backward-sentence (&optional arg)
17966 "Go to beginning of sentence, or beginning of table field.
17967 This will call `backward-sentence' or `org-table-beginning-of-field',
17968 depending on context."
17969 (interactive "P")
17970 (cond
17971 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
17972 (t (call-interactively 'backward-sentence))))
17974 (defun org-forward-sentence (&optional arg)
17975 "Go to end of sentence, or end of table field.
17976 This will call `forward-sentence' or `org-table-end-of-field',
17977 depending on context."
17978 (interactive "P")
17979 (cond
17980 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
17981 (t (call-interactively 'forward-sentence))))
17983 (define-key org-mode-map "\M-a" 'org-backward-sentence)
17984 (define-key org-mode-map "\M-e" 'org-forward-sentence)
17986 (defun org-kill-line (&optional arg)
17987 "Kill line, to tags or end of line."
17988 (interactive "P")
17989 (cond
17990 ((or (not org-special-ctrl-k)
17991 (bolp)
17992 (not (org-on-heading-p)))
17993 (call-interactively 'kill-line))
17994 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
17995 (kill-region (point) (match-beginning 1))
17996 (org-set-tags nil t))
17997 (t (kill-region (point) (point-at-eol)))))
17999 (define-key org-mode-map "\C-k" 'org-kill-line)
18001 (defun org-yank (&optional arg)
18002 "Yank. If the kill is a subtree, treat it specially.
18003 This command will look at the current kill and check if is a single
18004 subtree, or a series of subtrees[1]. If it passes the test, and if the
18005 cursor is at the beginning of a line or after the stars of a currently
18006 empty headline, then the yank is handled specially. How exactly depends
18007 on the value of the following variables, both set by default.
18009 org-yank-folded-subtrees
18010 When set, the subtree(s) will be folded after insertion, but only
18011 if doing so would now swallow text after the yanked text.
18013 org-yank-adjusted-subtrees
18014 When set, the subtree will be promoted or demoted in order to
18015 fit into the local outline tree structure, which means that the level
18016 will be adjusted so that it becomes the smaller one of the two
18017 *visible* surrounding headings.
18019 Any prefix to this command will cause `yank' to be called directly with
18020 no special treatment. In particular, a simple `C-u' prefix will just
18021 plainly yank the text as it is.
18023 \[1] The test checks if the first non-white line is a heading
18024 and if there are no other headings with fewer stars."
18025 (interactive "P")
18026 (org-yank-generic 'yank arg))
18028 (defun org-yank-generic (command arg)
18029 "Perform some yank-like command.
18031 This function implements the behavior described in the `org-yank'
18032 documentation. However, it has been generalized to work for any
18033 interactive command with similar behavior."
18035 ;; pretend to be command COMMAND
18036 (setq this-command command)
18038 (if arg
18039 (call-interactively command)
18041 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
18042 (and (org-kill-is-subtree-p)
18043 (or (bolp)
18044 (and (looking-at "[ \t]*$")
18045 (string-match
18046 "\\`\\*+\\'"
18047 (buffer-substring (point-at-bol) (point)))))))
18048 swallowp)
18049 (cond
18050 ((and subtreep org-yank-folded-subtrees)
18051 (let ((beg (point))
18052 end)
18053 (if (and subtreep org-yank-adjusted-subtrees)
18054 (org-paste-subtree nil nil 'for-yank)
18055 (call-interactively command))
18057 (setq end (point))
18058 (goto-char beg)
18059 (when (and (bolp) subtreep
18060 (not (setq swallowp
18061 (org-yank-folding-would-swallow-text beg end))))
18062 (or (looking-at outline-regexp)
18063 (re-search-forward (concat "^" outline-regexp) end t))
18064 (while (and (< (point) end) (looking-at outline-regexp))
18065 (hide-subtree)
18066 (org-cycle-show-empty-lines 'folded)
18067 (condition-case nil
18068 (outline-forward-same-level 1)
18069 (error (goto-char end)))))
18070 (when swallowp
18071 (message
18072 "Inserted text not folded because that would swallow text"))
18074 (goto-char end)
18075 (skip-chars-forward " \t\n\r")
18076 (beginning-of-line 1)
18077 (push-mark beg 'nomsg)))
18078 ((and subtreep org-yank-adjusted-subtrees)
18079 (let ((beg (point-at-bol)))
18080 (org-paste-subtree nil nil 'for-yank)
18081 (push-mark beg 'nomsg)))
18083 (call-interactively command))))))
18085 (defun org-yank-folding-would-swallow-text (beg end)
18086 "Would hide-subtree at BEG swallow any text after END?"
18087 (let (level)
18088 (save-excursion
18089 (goto-char beg)
18090 (when (or (looking-at outline-regexp)
18091 (re-search-forward (concat "^" outline-regexp) end t))
18092 (setq level (org-outline-level)))
18093 (goto-char end)
18094 (skip-chars-forward " \t\r\n\v\f")
18095 (if (or (eobp)
18096 (and (bolp) (looking-at org-outline-regexp)
18097 (<= (org-outline-level) level)))
18098 nil ; Nothing would be swallowed
18099 t)))) ; something would swallow
18101 (define-key org-mode-map "\C-y" 'org-yank)
18103 (defun org-invisible-p ()
18104 "Check if point is at a character currently not visible."
18105 ;; Early versions of noutline don't have `outline-invisible-p'.
18106 (if (fboundp 'outline-invisible-p)
18107 (outline-invisible-p)
18108 (get-char-property (point) 'invisible)))
18110 (defun org-invisible-p2 ()
18111 "Check if point is at a character currently not visible."
18112 (save-excursion
18113 (if (and (eolp) (not (bobp))) (backward-char 1))
18114 ;; Early versions of noutline don't have `outline-invisible-p'.
18115 (if (fboundp 'outline-invisible-p)
18116 (outline-invisible-p)
18117 (get-char-property (point) 'invisible))))
18119 (defun org-back-to-heading (&optional invisible-ok)
18120 "Call `outline-back-to-heading', but provide a better error message."
18121 (condition-case nil
18122 (outline-back-to-heading invisible-ok)
18123 (error (error "Before first headline at position %d in buffer %s"
18124 (point) (current-buffer)))))
18126 (defun org-before-first-heading-p ()
18127 "Before first heading?"
18128 (save-excursion
18129 (null (re-search-backward "^\\*+ " nil t))))
18131 (defun org-on-heading-p (&optional ignored)
18132 (outline-on-heading-p t))
18133 (defun org-at-heading-p (&optional ignored)
18134 (outline-on-heading-p t))
18136 (defun org-point-at-end-of-empty-headline ()
18137 "If point is at the end of an empty headline, return t, else nil.
18138 If the heading only contains a TODO keyword, it is still still considered
18139 empty."
18140 (and (looking-at "[ \t]*$")
18141 (save-excursion
18142 (beginning-of-line 1)
18143 (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
18144 "\\)?[ \t]*$")))))
18145 (defun org-at-heading-or-item-p ()
18146 (or (org-on-heading-p) (org-at-item-p)))
18148 (defun org-on-target-p ()
18149 (or (org-in-regexp org-radio-target-regexp)
18150 (org-in-regexp org-target-regexp)))
18152 (defun org-up-heading-all (arg)
18153 "Move to the heading line of which the present line is a subheading.
18154 This function considers both visible and invisible heading lines.
18155 With argument, move up ARG levels."
18156 (if (fboundp 'outline-up-heading-all)
18157 (outline-up-heading-all arg) ; emacs 21 version of outline.el
18158 (outline-up-heading arg t))) ; emacs 22 version of outline.el
18160 (defun org-up-heading-safe ()
18161 "Move to the heading line of which the present line is a subheading.
18162 This version will not throw an error. It will return the level of the
18163 headline found, or nil if no higher level is found.
18165 Also, this function will be a lot faster than `outline-up-heading',
18166 because it relies on stars being the outline starters. This can really
18167 make a significant difference in outlines with very many siblings."
18168 (let (start-level re)
18169 (org-back-to-heading t)
18170 (setq start-level (funcall outline-level))
18171 (if (equal start-level 1)
18173 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
18174 (if (re-search-backward re nil t)
18175 (funcall outline-level)))))
18177 (defun org-first-sibling-p ()
18178 "Is this heading the first child of its parents?"
18179 (interactive)
18180 (let ((re (concat "^" outline-regexp))
18181 level l)
18182 (unless (org-at-heading-p t)
18183 (error "Not at a heading"))
18184 (setq level (funcall outline-level))
18185 (save-excursion
18186 (if (not (re-search-backward re nil t))
18188 (setq l (funcall outline-level))
18189 (< l level)))))
18191 (defun org-goto-sibling (&optional previous)
18192 "Goto the next sibling, even if it is invisible.
18193 When PREVIOUS is set, go to the previous sibling instead. Returns t
18194 when a sibling was found. When none is found, return nil and don't
18195 move point."
18196 (let ((fun (if previous 're-search-backward 're-search-forward))
18197 (pos (point))
18198 (re (concat "^" outline-regexp))
18199 level l)
18200 (when (condition-case nil (org-back-to-heading t) (error nil))
18201 (setq level (funcall outline-level))
18202 (catch 'exit
18203 (or previous (forward-char 1))
18204 (while (funcall fun re nil t)
18205 (setq l (funcall outline-level))
18206 (when (< l level) (goto-char pos) (throw 'exit nil))
18207 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
18208 (goto-char pos)
18209 nil))))
18211 (defun org-show-siblings ()
18212 "Show all siblings of the current headline."
18213 (save-excursion
18214 (while (org-goto-sibling) (org-flag-heading nil)))
18215 (save-excursion
18216 (while (org-goto-sibling 'previous)
18217 (org-flag-heading nil))))
18219 (defun org-show-hidden-entry ()
18220 "Show an entry where even the heading is hidden."
18221 (save-excursion
18222 (org-show-entry)))
18224 (defun org-flag-heading (flag &optional entry)
18225 "Flag the current heading. FLAG non-nil means make invisible.
18226 When ENTRY is non-nil, show the entire entry."
18227 (save-excursion
18228 (org-back-to-heading t)
18229 ;; Check if we should show the entire entry
18230 (if entry
18231 (progn
18232 (org-show-entry)
18233 (save-excursion
18234 (and (outline-next-heading)
18235 (org-flag-heading nil))))
18236 (outline-flag-region (max (point-min) (1- (point)))
18237 (save-excursion (outline-end-of-heading) (point))
18238 flag))))
18240 (defun org-get-next-sibling ()
18241 "Move to next heading of the same level, and return point.
18242 If there is no such heading, return nil.
18243 This is like outline-next-sibling, but invisible headings are ok."
18244 (let ((level (funcall outline-level)))
18245 (outline-next-heading)
18246 (while (and (not (eobp)) (> (funcall outline-level) level))
18247 (outline-next-heading))
18248 (if (or (eobp) (< (funcall outline-level) level))
18250 (point))))
18252 (defun org-get-last-sibling ()
18253 "Move to previous heading of the same level, and return point.
18254 If there is no such heading, return nil."
18255 (let ((opoint (point))
18256 (level (funcall outline-level)))
18257 (outline-previous-heading)
18258 (when (and (/= (point) opoint) (outline-on-heading-p t))
18259 (while (and (> (funcall outline-level) level)
18260 (not (bobp)))
18261 (outline-previous-heading))
18262 (if (< (funcall outline-level) level)
18264 (point)))))
18266 (defun org-end-of-subtree (&optional invisible-OK to-heading)
18267 ;; This contains an exact copy of the original function, but it uses
18268 ;; `org-back-to-heading', to make it work also in invisible
18269 ;; trees. And is uses an invisible-OK argument.
18270 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
18271 ;; Furthermore, when used inside Org, finding the end of a large subtree
18272 ;; with many children and grandchildren etc, this can be much faster
18273 ;; than the outline version.
18274 (org-back-to-heading invisible-OK)
18275 (let ((first t)
18276 (level (funcall outline-level)))
18277 (if (and (org-mode-p) (< level 1000))
18278 ;; A true heading (not a plain list item), in Org-mode
18279 ;; This means we can easily find the end by looking
18280 ;; only for the right number of stars. Using a regexp to do
18281 ;; this is so much faster than using a Lisp loop.
18282 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
18283 (forward-char 1)
18284 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
18285 ;; something else, do it the slow way
18286 (while (and (not (eobp))
18287 (or first (> (funcall outline-level) level)))
18288 (setq first nil)
18289 (outline-next-heading)))
18290 (unless to-heading
18291 (if (memq (preceding-char) '(?\n ?\^M))
18292 (progn
18293 ;; Go to end of line before heading
18294 (forward-char -1)
18295 (if (memq (preceding-char) '(?\n ?\^M))
18296 ;; leave blank line before heading
18297 (forward-char -1))))))
18298 (point))
18300 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
18301 "Use Org version in org-mode, for dramatic speed-up."
18302 (if (eq major-mode 'org-mode)
18303 (progn
18304 (org-end-of-subtree nil t)
18305 (unless (eobp) (backward-char 1)))
18306 ad-do-it))
18308 (defun org-forward-same-level (arg &optional invisible-ok)
18309 "Move forward to the arg'th subheading at same level as this one.
18310 Stop at the first and last subheadings of a superior heading."
18311 (interactive "p")
18312 (org-back-to-heading invisible-ok)
18313 (org-on-heading-p)
18314 (let* ((level (- (match-end 0) (match-beginning 0) 1))
18315 (re (format "^\\*\\{1,%d\\} " level))
18317 (forward-char 1)
18318 (while (> arg 0)
18319 (while (and (re-search-forward re nil 'move)
18320 (setq l (- (match-end 0) (match-beginning 0) 1))
18321 (= l level)
18322 (not invisible-ok)
18323 (progn (backward-char 1) (org-invisible-p)))
18324 (if (< l level) (setq arg 1)))
18325 (setq arg (1- arg)))
18326 (beginning-of-line 1)))
18328 (defun org-backward-same-level (arg &optional invisible-ok)
18329 "Move backward to the arg'th subheading at same level as this one.
18330 Stop at the first and last subheadings of a superior heading."
18331 (interactive "p")
18332 (org-back-to-heading)
18333 (org-on-heading-p)
18334 (let* ((level (- (match-end 0) (match-beginning 0) 1))
18335 (re (format "^\\*\\{1,%d\\} " level))
18337 (while (> arg 0)
18338 (while (and (re-search-backward re nil 'move)
18339 (setq l (- (match-end 0) (match-beginning 0) 1))
18340 (= l level)
18341 (not invisible-ok)
18342 (org-invisible-p))
18343 (if (< l level) (setq arg 1)))
18344 (setq arg (1- arg)))))
18346 (defun org-show-subtree ()
18347 "Show everything after this heading at deeper levels."
18348 (outline-flag-region
18349 (point)
18350 (save-excursion
18351 (org-end-of-subtree t t))
18352 nil))
18354 (defun org-show-entry ()
18355 "Show the body directly following this heading.
18356 Show the heading too, if it is currently invisible."
18357 (interactive)
18358 (save-excursion
18359 (condition-case nil
18360 (progn
18361 (org-back-to-heading t)
18362 (outline-flag-region
18363 (max (point-min) (1- (point)))
18364 (save-excursion
18365 (if (re-search-forward
18366 (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
18367 (match-beginning 1)
18368 (point-max)))
18369 nil)
18370 (org-cycle-hide-drawers 'children))
18371 (error nil))))
18373 (defun org-make-options-regexp (kwds &optional extra)
18374 "Make a regular expression for keyword lines."
18375 (concat
18377 "#?[ \t]*\\+\\("
18378 (mapconcat 'regexp-quote kwds "\\|")
18379 (if extra (concat "\\|" extra))
18380 "\\):[ \t]*"
18381 "\\(.*\\)"))
18383 ;; Make isearch reveal the necessary context
18384 (defun org-isearch-end ()
18385 "Reveal context after isearch exits."
18386 (when isearch-success ; only if search was successful
18387 (if (featurep 'xemacs)
18388 ;; Under XEmacs, the hook is run in the correct place,
18389 ;; we directly show the context.
18390 (org-show-context 'isearch)
18391 ;; In Emacs the hook runs *before* restoring the overlays.
18392 ;; So we have to use a one-time post-command-hook to do this.
18393 ;; (Emacs 22 has a special variable, see function `org-mode')
18394 (unless (and (boundp 'isearch-mode-end-hook-quit)
18395 isearch-mode-end-hook-quit)
18396 ;; Only when the isearch was not quitted.
18397 (org-add-hook 'post-command-hook 'org-isearch-post-command
18398 'append 'local)))))
18400 (defun org-isearch-post-command ()
18401 "Remove self from hook, and show context."
18402 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
18403 (org-show-context 'isearch))
18406 ;;;; Integration with and fixes for other packages
18408 ;;; Imenu support
18410 (defvar org-imenu-markers nil
18411 "All markers currently used by Imenu.")
18412 (make-variable-buffer-local 'org-imenu-markers)
18414 (defun org-imenu-new-marker (&optional pos)
18415 "Return a new marker for use by Imenu, and remember the marker."
18416 (let ((m (make-marker)))
18417 (move-marker m (or pos (point)))
18418 (push m org-imenu-markers)
18421 (defun org-imenu-get-tree ()
18422 "Produce the index for Imenu."
18423 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
18424 (setq org-imenu-markers nil)
18425 (let* ((n org-imenu-depth)
18426 (re (concat "^" outline-regexp))
18427 (subs (make-vector (1+ n) nil))
18428 (last-level 0)
18429 m level head)
18430 (save-excursion
18431 (save-restriction
18432 (widen)
18433 (goto-char (point-max))
18434 (while (re-search-backward re nil t)
18435 (setq level (org-reduced-level (funcall outline-level)))
18436 (when (<= level n)
18437 (looking-at org-complex-heading-regexp)
18438 (setq head (org-link-display-format
18439 (org-match-string-no-properties 4))
18440 m (org-imenu-new-marker))
18441 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
18442 (if (>= level last-level)
18443 (push (cons head m) (aref subs level))
18444 (push (cons head (aref subs (1+ level))) (aref subs level))
18445 (loop for i from (1+ level) to n do (aset subs i nil)))
18446 (setq last-level level)))))
18447 (aref subs 1)))
18449 (eval-after-load "imenu"
18450 '(progn
18451 (add-hook 'imenu-after-jump-hook
18452 (lambda ()
18453 (if (eq major-mode 'org-mode)
18454 (org-show-context 'org-goto))))))
18456 (defun org-link-display-format (link)
18457 "Replace a link with either the description, or the link target
18458 if no description is present"
18459 (save-match-data
18460 (if (string-match org-bracket-link-analytic-regexp link)
18461 (replace-match (if (match-end 5)
18462 (match-string 5 link)
18463 (concat (match-string 1 link)
18464 (match-string 3 link)))
18465 nil t link)
18466 link)))
18468 ;; Speedbar support
18470 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
18471 "Overlay marking the agenda restriction line in speedbar.")
18472 (overlay-put org-speedbar-restriction-lock-overlay
18473 'face 'org-agenda-restriction-lock)
18474 (overlay-put org-speedbar-restriction-lock-overlay
18475 'help-echo "Agendas are currently limited to this item.")
18476 (org-detach-overlay org-speedbar-restriction-lock-overlay)
18478 (defun org-speedbar-set-agenda-restriction ()
18479 "Restrict future agenda commands to the location at point in speedbar.
18480 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
18481 (interactive)
18482 (require 'org-agenda)
18483 (let (p m tp np dir txt)
18484 (cond
18485 ((setq p (text-property-any (point-at-bol) (point-at-eol)
18486 'org-imenu t))
18487 (setq m (get-text-property p 'org-imenu-marker))
18488 (with-current-buffer (marker-buffer m)
18489 (goto-char m)
18490 (org-agenda-set-restriction-lock 'subtree)))
18491 ((setq p (text-property-any (point-at-bol) (point-at-eol)
18492 'speedbar-function 'speedbar-find-file))
18493 (setq tp (previous-single-property-change
18494 (1+ p) 'speedbar-function)
18495 np (next-single-property-change
18496 tp 'speedbar-function)
18497 dir (speedbar-line-directory)
18498 txt (buffer-substring-no-properties (or tp (point-min))
18499 (or np (point-max))))
18500 (with-current-buffer (find-file-noselect
18501 (let ((default-directory dir))
18502 (expand-file-name txt)))
18503 (unless (org-mode-p)
18504 (error "Cannot restrict to non-Org-mode file"))
18505 (org-agenda-set-restriction-lock 'file)))
18506 (t (error "Don't know how to restrict Org-mode's agenda")))
18507 (move-overlay org-speedbar-restriction-lock-overlay
18508 (point-at-bol) (point-at-eol))
18509 (setq current-prefix-arg nil)
18510 (org-agenda-maybe-redo)))
18512 (eval-after-load "speedbar"
18513 '(progn
18514 (speedbar-add-supported-extension ".org")
18515 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
18516 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
18517 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
18518 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
18519 (add-hook 'speedbar-visiting-tag-hook
18520 (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
18523 ;;; Fixes and Hacks for problems with other packages
18525 ;; Make flyspell not check words in links, to not mess up our keymap
18526 (defun org-mode-flyspell-verify ()
18527 "Don't let flyspell put overlays at active buttons."
18528 (and (not (get-text-property (point) 'keymap))
18529 (not (get-text-property (point) 'org-no-flyspell))))
18531 (defun org-remove-flyspell-overlays-in (beg end)
18532 "Remove flyspell overlays in region."
18533 (and (org-bound-and-true-p flyspell-mode)
18534 (fboundp 'flyspell-delete-region-overlays)
18535 (flyspell-delete-region-overlays beg end))
18536 (add-text-properties beg end '(org-no-flyspell t)))
18538 ;; Make `bookmark-jump' shows the jump location if it was hidden.
18539 (eval-after-load "bookmark"
18540 '(if (boundp 'bookmark-after-jump-hook)
18541 ;; We can use the hook
18542 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
18543 ;; Hook not available, use advice
18544 (defadvice bookmark-jump (after org-make-visible activate)
18545 "Make the position visible."
18546 (org-bookmark-jump-unhide))))
18548 ;; Make sure saveplace shows the location if it was hidden
18549 (eval-after-load "saveplace"
18550 '(defadvice save-place-find-file-hook (after org-make-visible activate)
18551 "Make the position visible."
18552 (org-bookmark-jump-unhide)))
18554 ;; Make sure ecb shows the location if it was hidden
18555 (eval-after-load "ecb"
18556 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
18557 "Make hierarchy visible when jumping into location from ECB tree buffer."
18558 (if (eq major-mode 'org-mode)
18559 (org-show-context))))
18561 (defun org-bookmark-jump-unhide ()
18562 "Unhide the current position, to show the bookmark location."
18563 (and (org-mode-p)
18564 (or (org-invisible-p)
18565 (save-excursion (goto-char (max (point-min) (1- (point))))
18566 (org-invisible-p)))
18567 (org-show-context 'bookmark-jump)))
18569 ;; Make session.el ignore our circular variable
18570 (eval-after-load "session"
18571 '(add-to-list 'session-globals-exclude 'org-mark-ring))
18573 ;;;; Experimental code
18575 (defun org-closed-in-range ()
18576 "Sparse tree of items closed in a certain time range.
18577 Still experimental, may disappear in the future."
18578 (interactive)
18579 ;; Get the time interval from the user.
18580 (let* ((time1 (org-float-time
18581 (org-read-date nil 'to-time nil "Starting date: ")))
18582 (time2 (org-float-time
18583 (org-read-date nil 'to-time nil "End date:")))
18584 ;; callback function
18585 (callback (lambda ()
18586 (let ((time
18587 (org-float-time
18588 (apply 'encode-time
18589 (org-parse-time-string
18590 (match-string 1))))))
18591 ;; check if time in interval
18592 (and (>= time time1) (<= time time2))))))
18593 ;; make tree, check each match with the callback
18594 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
18596 ;;;; Finish up
18598 (provide 'org)
18600 (run-hooks 'org-load-hook)
18602 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
18604 ;;; org.el ends here