Always show current value when setting effort property
[org-mode.git] / lisp / org.el
blob28aa9a21c227fc0741bb4b603e2708b925fd2e4d
1 ;;; org.el --- Outline-based notes management and organizer
2 ;; Carstens outline-mode for keeping track of everything.
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
5 ;;
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 6.32trans
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
30 ;; project planning with a fast and effective plain-text system.
32 ;; Org-mode develops organizational tasks around NOTES files that contain
33 ;; information about projects as plain text. Org-mode is implemented on
34 ;; top of outline-mode, which makes it possible to keep the content of
35 ;; large files well structured. Visibility cycling and structure editing
36 ;; help to work with the tree. Tables are easily created with a built-in
37 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
38 ;; and scheduling. It dynamically compiles entries into an agenda that
39 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
40 ;; Plain text URL-like links connect to websites, emails, Usenet
41 ;; messages, BBDB entries, and any files related to the projects. For
42 ;; printing and sharing of notes, an Org-mode file can be exported as a
43 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
44 ;; iCalendar file. It can also serve as a publishing tool for a set of
45 ;; linked webpages.
47 ;; Installation and Activation
48 ;; ---------------------------
49 ;; See the corresponding sections in the manual at
51 ;; http://orgmode.org/org.html#Installation
53 ;; Documentation
54 ;; -------------
55 ;; The documentation of Org-mode can be found in the TeXInfo file. The
56 ;; distribution also contains a PDF version of it. At the homepage of
57 ;; Org-mode, you can read the same text online as HTML. There is also an
58 ;; excellent reference card made by Philip Rooke. This card can be found
59 ;; in the etc/ directory of Emacs 22.
61 ;; A list of recent changes can be found at
62 ;; http://orgmode.org/Changes.html
64 ;;; Code:
66 (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
67 (defvar org-table-formula-constants-local nil
68 "Local version of `org-table-formula-constants'.")
69 (make-variable-buffer-local 'org-table-formula-constants-local)
71 ;;;; Require other packages
73 (eval-when-compile
74 (require 'cl)
75 (require 'gnus-sum)
76 (require 'calendar))
77 ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
78 ;; the file noutline.el being loaded.
79 (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
80 ;; We require noutline, which might be provided in outline.el
81 (require 'outline) (require 'noutline)
82 ;; Other stuff we need.
83 (require 'time-date)
84 (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
85 (require 'easymenu)
87 (require 'org-macs)
88 (require 'org-compat)
89 (require 'org-faces)
90 (require 'org-list)
91 (require 'org-src)
92 (require 'org-footnote)
94 ;;;; Customization variables
96 ;;; Version
98 (defconst org-version "6.32trans"
99 "The version number of the file org.el.")
101 (defun org-version (&optional here)
102 "Show the org-mode version in the echo area.
103 With prefix arg HERE, insert it at point."
104 (interactive "P")
105 (let* ((version org-version)
106 (git-version)
107 (dir (concat (file-name-directory (locate-library "org")) "../" )))
108 (if (and (file-exists-p (expand-file-name ".git" dir))
109 (executable-find "git"))
110 (let ((pwd (substring (pwd) 10)))
111 (cd dir)
112 (if (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
113 (save-excursion
114 (set-buffer "*Shell Command Output*")
115 (goto-char (point-min))
116 (re-search-forward "[^\n]+")
117 (setq git-version (match-string 0))
118 (subst-char-in-string ?- ?. git-version t)
119 (shell-command "git diff-index --name-only HEAD --")
120 (unless (eql 1 (point-max))
121 (setq git-version (concat git-version ".dirty")))
122 (setq version (concat version " (" git-version ")")))
123 (cd pwd))))
124 (setq version (format "Org-mode version %s" version))
125 (if here (insert version))
126 (message version)
127 version))
129 ;;; Compatibility constants
131 ;;; The custom variables
133 (defgroup org nil
134 "Outline-based notes management and organizer."
135 :tag "Org"
136 :group 'outlines
137 :group 'hypermedia
138 :group 'calendar)
140 (defcustom org-mode-hook nil
141 "Mode hook for Org-mode, run after the mode was turned on."
142 :group 'org
143 :type 'hook)
145 (defcustom org-load-hook nil
146 "Hook that is run after org.el has been loaded."
147 :group 'org
148 :type 'hook)
150 (defvar org-modules) ; defined below
151 (defvar org-modules-loaded nil
152 "Have the modules been loaded already?")
154 (defun org-load-modules-maybe (&optional force)
155 "Load all extensions listed in `org-modules'."
156 (when (or force (not org-modules-loaded))
157 (mapc (lambda (ext)
158 (condition-case nil (require ext)
159 (error (message "Problems while trying to load feature `%s'" ext))))
160 org-modules)
161 (setq org-modules-loaded t)))
163 (defun org-set-modules (var value)
164 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
165 (set var value)
166 (when (featurep 'org)
167 (org-load-modules-maybe 'force)))
169 (when (org-bound-and-true-p org-modules)
170 (let ((a (member 'org-infojs org-modules)))
171 (and a (setcar a 'org-jsinfo))))
173 (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
174 "Modules that should always be loaded together with org.el.
175 If a description starts with <C>, the file is not part of Emacs
176 and loading it will require that you have downloaded and properly installed
177 the org-mode distribution.
179 You can also use this system to load external packages (i.e. neither Org
180 core modules, nor modules from the CONTRIB directory). Just add symbols
181 to the end of the list. If the package is called org-xyz.el, then you need
182 to add the symbol `xyz', and the package must have a call to
184 (provide 'org-xyz)"
185 :group 'org
186 :set 'org-set-modules
187 :type
188 '(set :greedy t
189 (const :tag " bbdb: Links to BBDB entries" org-bbdb)
190 (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
191 (const :tag " crypt: Encryption of subtrees" org-crypt)
192 (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
193 (const :tag " id: Global IDs for identifying entries" org-id)
194 (const :tag " info: Links to Info nodes" org-info)
195 (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
196 (const :tag " habit: Track your consistency with habits" org-habit)
197 (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
198 (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
199 (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
200 (const :tag " mew Links to Mew folders/messages" org-mew)
201 (const :tag " mhe: Links to MHE folders/messages" org-mhe)
202 (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
203 (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
204 (const :tag " vm: Links to VM folders/messages" org-vm)
205 (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
206 (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
207 (const :tag " mouse: Additional mouse support" org-mouse)
209 (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
210 (const :tag "C annotation-helper: Call Remember directly from Browser\n\t\t\t(OBSOLETE, use org-protocol)" org-annotation-helper)
211 (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
212 (const :tag "C browser-url: Store link, directly from Browser\n\t\t\t(OBSOLETE, use org-protocol)" org-browser-url)
213 (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
214 (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
215 (const :tag "C collector: Collect properties into tables" org-collector)
216 (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
217 (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
218 (const :tag "C eval: Include command output as text" org-eval)
219 (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
220 (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
221 (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
222 (const :tag "C git-link: Provide org links to specific file version" org-git-link)
223 (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
225 (const :tag "C invoice Help manage client invoices in Org-mode" org-invoice)
227 (const :tag "C jira Add a jira:ticket protocol to Org-mode" org-jira)
228 (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
229 (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
230 (const :tag "C man: Support for links to manpages in Org-mode" org-man)
231 (const :tag "C mtags: Support for muse-like tags" org-mtags)
232 (const :tag "C panel: Simple routines for us with bad memory" org-panel)
233 (const :tag "C R: Computation using the R language" org-R)
234 (const :tag "C registry: A registry for Org-mode links" org-registry)
235 (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
236 (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
237 (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
238 (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
239 (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
240 (const :tag "C track: Keep up with Org-mode development" org-track)
241 (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
243 (defcustom org-support-shift-select nil
244 "Non-nil means, make shift-cursor commands select text when possible.
246 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
247 selecting a region, or enlarge thusly regions started in this way.
248 In Org-mode, in special contexts, these same keys are used for other
249 purposes, important enough to compete with shift selection. Org tries
250 to balance these needs by supporting `shift-select-mode' outside these
251 special contexts, under control of this variable.
253 The default of this variable is nil, to avoid confusing behavior. Shifted
254 cursor keys will then execute Org commands in the following contexts:
255 - on a headline, changing TODO state (left/right) and priority (up/down)
256 - on a time stamp, changing the time
257 - in a plain list item, changing the bullet type
258 - in a property definition line, switching between allowed values
259 - in the BEGIN line of a clock table (changing the time block).
260 Outside these contexts, the commands will throw an error.
262 When this variable is t and the cursor is not in a special context,
263 Org-mode will support shift-selection for making and enlarging regions.
264 To make this more effective, the bullet cycling will no longer happen
265 anywhere in an item line, but only if the cursor is exactly on the bullet.
267 If you set this variable to the symbol `always', then the keys
268 will not be special in headlines, property lines, and item lines, to make
269 shift selection work there as well. If this is what you want, you can
270 use the following alternative commands: `C-c C-t' and `C-c ,' to
271 change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
272 TODO sets, `C-c -' to cycle item bullet types, and properties can be
273 edited by hand or in column view.
275 However, when the cursor is on a timestamp, shift-cursor commands
276 will still edit the time stamp - this is just too good to give up.
278 XEmacs user should have this variable set to nil, because shift-select-mode
279 is Emacs 23 only."
280 :group 'org
281 :type '(choice
282 (const :tag "Never" nil)
283 (const :tag "When outside special context" t)
284 (const :tag "Everywhere except timestamps" always)))
286 (defgroup org-startup nil
287 "Options concerning startup of Org-mode."
288 :tag "Org Startup"
289 :group 'org)
291 (defcustom org-startup-folded t
292 "Non-nil means, entering Org-mode will switch to OVERVIEW.
293 This can also be configured on a per-file basis by adding one of
294 the following lines anywhere in the buffer:
296 #+STARTUP: fold (or `overview', this is equivalent)
297 #+STARTUP: nofold (or `showall', this is equivalent)
298 #+STARTUP: content
299 #+STARTUP: showeverything"
300 :group 'org-startup
301 :type '(choice
302 (const :tag "nofold: show all" nil)
303 (const :tag "fold: overview" t)
304 (const :tag "content: all headlines" content)
305 (const :tag "show everything, even drawers" showeverything)))
307 (defcustom org-startup-truncated t
308 "Non-nil means, entering Org-mode will set `truncate-lines'.
309 This is useful since some lines containing links can be very long and
310 uninteresting. Also tables look terrible when wrapped."
311 :group 'org-startup
312 :type 'boolean)
314 (defcustom org-startup-indented nil
315 "Non-nil means, turn on `org-indent-mode' on startup.
316 This can also be configured on a per-file basis by adding one of
317 the following lines anywhere in the buffer:
319 #+STARTUP: indent
320 #+STARTUP: noindent"
321 :group 'org-structure
322 :type '(choice
323 (const :tag "Not" nil)
324 (const :tag "Globally (slow on startup in large files)" t)))
326 (defcustom org-startup-align-all-tables nil
327 "Non-nil means, align all tables when visiting a file.
328 This is useful when the column width in tables is forced with <N> cookies
329 in table fields. Such tables will look correct only after the first re-align.
330 This can also be configured on a per-file basis by adding one of
331 the following lines anywhere in the buffer:
332 #+STARTUP: align
333 #+STARTUP: noalign"
334 :group 'org-startup
335 :type 'boolean)
337 (defcustom org-insert-mode-line-in-empty-file nil
338 "Non-nil means insert the first line setting Org-mode in empty files.
339 When the function `org-mode' is called interactively in an empty file, this
340 normally means that the file name does not automatically trigger Org-mode.
341 To ensure that the file will always be in Org-mode in the future, a
342 line enforcing Org-mode will be inserted into the buffer, if this option
343 has been set."
344 :group 'org-startup
345 :type 'boolean)
347 (defcustom org-replace-disputed-keys nil
348 "Non-nil means use alternative key bindings for some keys.
349 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
350 These keys are also used by other packages like shift-selection-mode'
351 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
352 If you want to use Org-mode together with one of these other modes,
353 or more generally if you would like to move some Org-mode commands to
354 other keys, set this variable and configure the keys with the variable
355 `org-disputed-keys'.
357 This option is only relevant at load-time of Org-mode, and must be set
358 *before* org.el is loaded. Changing it requires a restart of Emacs to
359 become effective."
360 :group 'org-startup
361 :type 'boolean)
363 (defcustom org-use-extra-keys nil
364 "Non-nil means use extra key sequence definitions for certain
365 commands. This happens automatically if you run XEmacs or if
366 window-system is nil. This variable lets you do the same
367 manually. You must set it before loading org.
369 Example: on Carbon Emacs 22 running graphically, with an external
370 keyboard on a Powerbook, the default way of setting M-left might
371 not work for either Alt or ESC. Setting this variable will make
372 it work for ESC."
373 :group 'org-startup
374 :type 'boolean)
376 (if (fboundp 'defvaralias)
377 (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
379 (defcustom org-disputed-keys
380 '(([(shift up)] . [(meta p)])
381 ([(shift down)] . [(meta n)])
382 ([(shift left)] . [(meta -)])
383 ([(shift right)] . [(meta +)])
384 ([(control shift right)] . [(meta shift +)])
385 ([(control shift left)] . [(meta shift -)]))
386 "Keys for which Org-mode and other modes compete.
387 This is an alist, cars are the default keys, second element specifies
388 the alternative to use when `org-replace-disputed-keys' is t.
390 Keys can be specified in any syntax supported by `define-key'.
391 The value of this option takes effect only at Org-mode's startup,
392 therefore you'll have to restart Emacs to apply it after changing."
393 :group 'org-startup
394 :type 'alist)
396 (defun org-key (key)
397 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
398 Or return the original if not disputed."
399 (if org-replace-disputed-keys
400 (let* ((nkey (key-description key))
401 (x (org-find-if (lambda (x)
402 (equal (key-description (car x)) nkey))
403 org-disputed-keys)))
404 (if x (cdr x) key))
405 key))
407 (defun org-find-if (predicate seq)
408 (catch 'exit
409 (while seq
410 (if (funcall predicate (car seq))
411 (throw 'exit (car seq))
412 (pop seq)))))
414 (defun org-defkey (keymap key def)
415 "Define a key, possibly translated, as returned by `org-key'."
416 (define-key keymap (org-key key) def))
418 (defcustom org-ellipsis nil
419 "The ellipsis to use in the Org-mode outline.
420 When nil, just use the standard three dots. When a string, use that instead,
421 When a face, use the standard 3 dots, but with the specified face.
422 The change affects only Org-mode (which will then use its own display table).
423 Changing this requires executing `M-x org-mode' in a buffer to become
424 effective."
425 :group 'org-startup
426 :type '(choice (const :tag "Default" nil)
427 (face :tag "Face" :value org-warning)
428 (string :tag "String" :value "...#")))
430 (defvar org-display-table nil
431 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
433 (defgroup org-keywords nil
434 "Keywords in Org-mode."
435 :tag "Org Keywords"
436 :group 'org)
438 (defcustom org-deadline-string "DEADLINE:"
439 "String to mark deadline entries.
440 A deadline is this string, followed by a time stamp. Should be a word,
441 terminated by a colon. You can insert a schedule keyword and
442 a timestamp with \\[org-deadline].
443 Changes become only effective after restarting Emacs."
444 :group 'org-keywords
445 :type 'string)
447 (defcustom org-scheduled-string "SCHEDULED:"
448 "String to mark scheduled TODO entries.
449 A schedule is this string, followed by a time stamp. Should be a word,
450 terminated by a colon. You can insert a schedule keyword and
451 a timestamp with \\[org-schedule].
452 Changes become only effective after restarting Emacs."
453 :group 'org-keywords
454 :type 'string)
456 (defcustom org-closed-string "CLOSED:"
457 "String used as the prefix for timestamps logging closing a TODO entry."
458 :group 'org-keywords
459 :type 'string)
461 (defcustom org-clock-string "CLOCK:"
462 "String used as prefix for timestamps clocking work hours on an item."
463 :group 'org-keywords
464 :type 'string)
466 (defcustom org-comment-string "COMMENT"
467 "Entries starting with this keyword will never be exported.
468 An entry can be toggled between COMMENT and normal with
469 \\[org-toggle-comment].
470 Changes become only effective after restarting Emacs."
471 :group 'org-keywords
472 :type 'string)
474 (defcustom org-quote-string "QUOTE"
475 "Entries starting with this keyword will be exported in fixed-width font.
476 Quoting applies only to the text in the entry following the headline, and does
477 not extend beyond the next headline, even if that is lower level.
478 An entry can be toggled between QUOTE and normal with
479 \\[org-toggle-fixed-width-section]."
480 :group 'org-keywords
481 :type 'string)
483 (defconst org-repeat-re
484 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
485 "Regular expression for specifying repeated events.
486 After a match, group 1 contains the repeat expression.")
488 (defgroup org-structure nil
489 "Options concerning the general structure of Org-mode files."
490 :tag "Org Structure"
491 :group 'org)
493 (defgroup org-reveal-location nil
494 "Options about how to make context of a location visible."
495 :tag "Org Reveal Location"
496 :group 'org-structure)
498 (defconst org-context-choice
499 '(choice
500 (const :tag "Always" t)
501 (const :tag "Never" nil)
502 (repeat :greedy t :tag "Individual contexts"
503 (cons
504 (choice :tag "Context"
505 (const agenda)
506 (const org-goto)
507 (const occur-tree)
508 (const tags-tree)
509 (const link-search)
510 (const mark-goto)
511 (const bookmark-jump)
512 (const isearch)
513 (const default))
514 (boolean))))
515 "Contexts for the reveal options.")
517 (defcustom org-show-hierarchy-above '((default . t))
518 "Non-nil means, show full hierarchy when revealing a location.
519 Org-mode often shows locations in an org-mode file which might have
520 been invisible before. When this is set, the hierarchy of headings
521 above the exposed location is shown.
522 Turning this off for example for sparse trees makes them very compact.
523 Instead of t, this can also be an alist specifying this option for different
524 contexts. Valid contexts are
525 agenda when exposing an entry from the agenda
526 org-goto when using the command `org-goto' on key C-c C-j
527 occur-tree when using the command `org-occur' on key C-c /
528 tags-tree when constructing a sparse tree based on tags matches
529 link-search when exposing search matches associated with a link
530 mark-goto when exposing the jump goal of a mark
531 bookmark-jump when exposing a bookmark location
532 isearch when exiting from an incremental search
533 default default for all contexts not set explicitly"
534 :group 'org-reveal-location
535 :type org-context-choice)
537 (defcustom org-show-following-heading '((default . nil))
538 "Non-nil means, show following heading when revealing a location.
539 Org-mode often shows locations in an org-mode file which might have
540 been invisible before. When this is set, the heading following the
541 match is shown.
542 Turning this off for example for sparse trees makes them very compact,
543 but makes it harder to edit the location of the match. In such a case,
544 use the command \\[org-reveal] to show more context.
545 Instead of t, this can also be an alist specifying this option for different
546 contexts. See `org-show-hierarchy-above' for valid contexts."
547 :group 'org-reveal-location
548 :type org-context-choice)
550 (defcustom org-show-siblings '((default . nil) (isearch t))
551 "Non-nil means, show all sibling heading when revealing a location.
552 Org-mode often shows locations in an org-mode file which might have
553 been invisible before. When this is set, the sibling of the current entry
554 heading are all made visible. If `org-show-hierarchy-above' is t,
555 the same happens on each level of the hierarchy above the current entry.
557 By default this is on for the isearch context, off for all other contexts.
558 Turning this off for example for sparse trees makes them very compact,
559 but makes it harder to edit the location of the match. In such a case,
560 use the command \\[org-reveal] to show more context.
561 Instead of t, this can also be an alist specifying this option for different
562 contexts. See `org-show-hierarchy-above' for valid contexts."
563 :group 'org-reveal-location
564 :type org-context-choice)
566 (defcustom org-show-entry-below '((default . nil))
567 "Non-nil means, show the entry below a headline when revealing a location.
568 Org-mode often shows locations in an org-mode file which might have
569 been invisible before. When this is set, the text below the headline that is
570 exposed is also shown.
572 By default this is off for all contexts.
573 Instead of t, this can also be an alist specifying this option for different
574 contexts. See `org-show-hierarchy-above' for valid contexts."
575 :group 'org-reveal-location
576 :type org-context-choice)
578 (defcustom org-indirect-buffer-display 'other-window
579 "How should indirect tree buffers be displayed?
580 This applies to indirect buffers created with the commands
581 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
582 Valid values are:
583 current-window Display in the current window
584 other-window Just display in another window.
585 dedicated-frame Create one new frame, and re-use it each time.
586 new-frame Make a new frame each time. Note that in this case
587 previously-made indirect buffers are kept, and you need to
588 kill these buffers yourself."
589 :group 'org-structure
590 :group 'org-agenda-windows
591 :type '(choice
592 (const :tag "In current window" current-window)
593 (const :tag "In current frame, other window" other-window)
594 (const :tag "Each time a new frame" new-frame)
595 (const :tag "One dedicated frame" dedicated-frame)))
597 (defcustom org-use-speed-commands nil
598 "Non-nil means, activate single letter commands at beginning of a headline."
599 :group 'org-structure
600 :type 'boolean)
602 (defcustom org-speed-commands-user nil
603 "Alist of additional speed commands.
604 This list will be checked before `org-speed-commands-default'
605 when the variable `org-use-speed-commands' is non-nil
606 and when the cursor is at the beginning of a headline.
607 The car if each entry is a string with a single letter, which must
608 be assigned to `self-insert-command' in the global map.
609 The cdr is either a command to be called interactively, a function
610 to be called, or a form to be evaluated."
611 :group 'org-structure
612 :type '(repeat
613 (cons
614 (string "Command letter")
615 (choice
616 (function)
617 (sexp)))))
619 (defgroup org-cycle nil
620 "Options concerning visibility cycling in Org-mode."
621 :tag "Org Cycle"
622 :group 'org-structure)
624 (defcustom org-cycle-skip-children-state-if-no-children t
625 "Non-nil means, skip CHILDREN state in entries that don't have any."
626 :group 'org-cycle
627 :type 'boolean)
629 (defcustom org-cycle-max-level nil
630 "Maximum level which should still be subject to visibility cycling.
631 Levels higher than this will, for cycling, be treated as text, not a headline.
632 When `org-odd-levels-only' is set, a value of N in this variable actually
633 means 2N-1 stars as the limiting headline.
634 When nil, cycle all levels.
635 Note that the limiting level of cycling is also influenced by
636 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
637 `org-inlinetask-min-level' is, cycling will be limited to levels one less
638 than its value."
639 :group 'org-cycle
640 :type '(choice
641 (const :tag "No limit" nil)
642 (integer :tag "Maximum level")))
644 (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
645 "Names of drawers. Drawers are not opened by cycling on the headline above.
646 Drawers only open with a TAB on the drawer line itself. A drawer looks like
647 this:
648 :DRAWERNAME:
649 .....
650 :END:
651 The drawer \"PROPERTIES\" is special for capturing properties through
652 the property API.
654 Drawers can be defined on the per-file basis with a line like:
656 #+DRAWERS: HIDDEN STATE PROPERTIES"
657 :group 'org-structure
658 :group 'org-cycle
659 :type '(repeat (string :tag "Drawer Name")))
661 (defcustom org-hide-block-startup nil
662 "Non-nil means, , entering Org-mode will fold all blocks.
663 This can also be set in on a per-file basis with
665 #+STARTUP: hideblocks
666 #+STARTUP: showblocks"
667 :group 'org-startup
668 :group 'org-cycle
669 :type 'boolean)
671 (defcustom org-cycle-global-at-bob nil
672 "Cycle globally if cursor is at beginning of buffer and not at a headline.
673 This makes it possible to do global cycling without having to use S-TAB or
674 C-u TAB. For this special case to work, the first line of the buffer
675 must not be a headline - it may be empty or some other text. When used in
676 this way, `org-cycle-hook' is disables temporarily, to make sure the
677 cursor stays at the beginning of the buffer.
678 When this option is nil, don't do anything special at the beginning
679 of the buffer."
680 :group 'org-cycle
681 :type 'boolean)
683 (defcustom org-cycle-level-after-item/entry-creation t
684 "Non-nil means, cycle entry level or item indentation in new empty entries.
686 When the cursor is at the end of an empty headline, i.e with only stars
687 and maybe a TODO keyword, TAB will then switch the entry to become a child,
688 and then all possible anchestor states, before returning to the original state.
689 This makes data entry extremely fast: M-RET to create a new hedline,
690 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
692 When the cursor is at the end of an empty plain list item, one TAB will
693 make it a subitem, two or more tabs will back up to make this an item
694 higher up in the item hierarchy."
695 :group 'org-cycle
696 :type 'boolean)
698 (defcustom org-cycle-emulate-tab t
699 "Where should `org-cycle' emulate TAB.
700 nil Never
701 white Only in completely white lines
702 whitestart Only at the beginning of lines, before the first non-white char
703 t Everywhere except in headlines
704 exc-hl-bol Everywhere except at the start of a headline
705 If TAB is used in a place where it does not emulate TAB, the current subtree
706 visibility is cycled."
707 :group 'org-cycle
708 :type '(choice (const :tag "Never" nil)
709 (const :tag "Only in completely white lines" white)
710 (const :tag "Before first char in a line" whitestart)
711 (const :tag "Everywhere except in headlines" t)
712 (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
715 (defcustom org-cycle-separator-lines 2
716 "Number of empty lines needed to keep an empty line between collapsed trees.
717 If you leave an empty line between the end of a subtree and the following
718 headline, this empty line is hidden when the subtree is folded.
719 Org-mode will leave (exactly) one empty line visible if the number of
720 empty lines is equal or larger to the number given in this variable.
721 So the default 2 means, at least 2 empty lines after the end of a subtree
722 are needed to produce free space between a collapsed subtree and the
723 following headline.
725 If the number is negative, and the number of empty lines is at least -N,
726 all empty lines are shown.
728 Special case: when 0, never leave empty lines in collapsed view."
729 :group 'org-cycle
730 :type 'integer)
731 (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
733 (defcustom org-pre-cycle-hook nil
734 "Hook that is run before visibility cycling is happening.
735 The function(s) in this hook must accept a single argument which indicates
736 the new state that will be set right after running this hook. The
737 argument is a symbol. Before a global state change, it can have the values
738 `overview', `content', or `all'. Before a local state change, it can have
739 the values `folded', `children', or `subtree'."
740 :group 'org-cycle
741 :type 'hook)
743 (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
744 org-cycle-hide-drawers
745 org-cycle-show-empty-lines
746 org-optimize-window-after-visibility-change)
747 "Hook that is run after `org-cycle' has changed the buffer visibility.
748 The function(s) in this hook must accept a single argument which indicates
749 the new state that was set by the most recent `org-cycle' command. The
750 argument is a symbol. After a global state change, it can have the values
751 `overview', `content', or `all'. After a local state change, it can have
752 the values `folded', `children', or `subtree'."
753 :group 'org-cycle
754 :type 'hook)
756 (defgroup org-edit-structure nil
757 "Options concerning structure editing in Org-mode."
758 :tag "Org Edit Structure"
759 :group 'org-structure)
761 (defcustom org-odd-levels-only nil
762 "Non-nil means, skip even levels and only use odd levels for the outline.
763 This has the effect that two stars are being added/taken away in
764 promotion/demotion commands. It also influences how levels are
765 handled by the exporters.
766 Changing it requires restart of `font-lock-mode' to become effective
767 for fontification also in regions already fontified.
768 You may also set this on a per-file basis by adding one of the following
769 lines to the buffer:
771 #+STARTUP: odd
772 #+STARTUP: oddeven"
773 :group 'org-edit-structure
774 :group 'org-font-lock
775 :type 'boolean)
777 (defcustom org-adapt-indentation t
778 "Non-nil means, adapt indentation to outline node level.
780 When this variable is set, Org assumes that you write outlines by
781 indenting text in each node to align with the headline (after the stars).
782 The following issues are influenced by this variable:
784 - When this is set and the *entire* text in an entry is indented, the
785 indentation is increased by one space in a demotion command, and
786 decreased by one in a promotion command. If any line in the entry
787 body starts with text at column 0, indentation is not changed at all.
789 - Property drawers and planning information is inserted indented when
790 this variable s set. When nil, they will not be indented.
792 - TAB indents a line relative to context. The lines below a headline
793 will be indented when this variable is set.
795 Note that this is all about true indentation, by adding and removing
796 space characters. See also `org-indent.el' which does level-dependent
797 indentation in a virtual way, i.e. at display time in Emacs."
798 :group 'org-edit-structure
799 :type 'boolean)
801 (defcustom org-special-ctrl-a/e nil
802 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
804 When t, `C-a' will bring back the cursor to the beginning of the
805 headline text, i.e. after the stars and after a possible TODO keyword.
806 In an item, this will be the position after the bullet.
807 When the cursor is already at that position, another `C-a' will bring
808 it to the beginning of the line.
810 `C-e' will jump to the end of the headline, ignoring the presence of tags
811 in the headline. A second `C-e' will then jump to the true end of the
812 line, after any tags. This also means that, when this variable is
813 non-nil, `C-e' also will never jump beyond the end of the heading of a
814 folded section, i.e. not after the ellipses.
816 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
817 going to the true line boundary first. Only a directly following, identical
818 keypress will bring the cursor to the special positions.
820 This may also be a cons cell where the behavior for `C-a' and `C-e' is
821 set separately."
822 :group 'org-edit-structure
823 :type '(choice
824 (const :tag "off" nil)
825 (const :tag "on: after stars/bullet and before tags first" t)
826 (const :tag "reversed: true line boundary first" reversed)
827 (cons :tag "Set C-a and C-e separately"
828 (choice :tag "Special C-a"
829 (const :tag "off" nil)
830 (const :tag "on: after stars/bullet first" t)
831 (const :tag "reversed: before stars/bullet first" reversed))
832 (choice :tag "Special C-e"
833 (const :tag "off" nil)
834 (const :tag "on: before tags first" t)
835 (const :tag "reversed: after tags first" reversed)))))
836 (if (fboundp 'defvaralias)
837 (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
839 (defcustom org-special-ctrl-k nil
840 "Non-nil means `C-k' will behave specially in headlines.
841 When nil, `C-k' will call the default `kill-line' command.
842 When t, the following will happen while the cursor is in the headline:
844 - When the cursor is at the beginning of a headline, kill the entire
845 line and possible the folded subtree below the line.
846 - When in the middle of the headline text, kill the headline up to the tags.
847 - When after the headline text, kill the tags."
848 :group 'org-edit-structure
849 :type 'boolean)
851 (defcustom org-yank-folded-subtrees t
852 "Non-nil means, when yanking subtrees, fold them.
853 If the kill is a single subtree, or a sequence of subtrees, i.e. if
854 it starts with a heading and all other headings in it are either children
855 or siblings, then fold all the subtrees. However, do this only if no
856 text after the yank would be swallowed into a folded tree by this action."
857 :group 'org-edit-structure
858 :type 'boolean)
860 (defcustom org-yank-adjusted-subtrees nil
861 "Non-nil means, when yanking subtrees, adjust the level.
862 With this setting, `org-paste-subtree' is used to insert the subtree, see
863 this function for details."
864 :group 'org-edit-structure
865 :type 'boolean)
867 (defcustom org-M-RET-may-split-line '((default . t))
868 "Non-nil means, M-RET will split the line at the cursor position.
869 When nil, it will go to the end of the line before making a
870 new line.
871 You may also set this option in a different way for different
872 contexts. Valid contexts are:
874 headline when creating a new headline
875 item when creating a new item
876 table in a table field
877 default the value to be used for all contexts not explicitly
878 customized"
879 :group 'org-structure
880 :group 'org-table
881 :type '(choice
882 (const :tag "Always" t)
883 (const :tag "Never" nil)
884 (repeat :greedy t :tag "Individual contexts"
885 (cons
886 (choice :tag "Context"
887 (const headline)
888 (const item)
889 (const table)
890 (const default))
891 (boolean)))))
894 (defcustom org-insert-heading-respect-content nil
895 "Non-nil means, insert new headings after the current subtree.
896 When nil, the new heading is created directly after the current line.
897 The commands \\[org-insert-heading-respect-content] and
898 \\[org-insert-todo-heading-respect-content] turn this variable on
899 for the duration of the command."
900 :group 'org-structure
901 :type 'boolean)
903 (defcustom org-blank-before-new-entry '((heading . auto)
904 (plain-list-item . auto))
905 "Should `org-insert-heading' leave a blank line before new heading/item?
906 The value is an alist, with `heading' and `plain-list-item' as car,
907 and a boolean flag as cdr. For plain lists, if the variable
908 `org-empty-line-terminates-plain-lists' is set, the setting here
909 is ignored and no empty line is inserted, to keep the list in tact."
910 :group 'org-edit-structure
911 :type '(list
912 (cons (const heading)
913 (choice (const :tag "Never" nil)
914 (const :tag "Always" t)
915 (const :tag "Auto" auto)))
916 (cons (const plain-list-item)
917 (choice (const :tag "Never" nil)
918 (const :tag "Always" t)
919 (const :tag "Auto" auto)))))
921 (defcustom org-insert-heading-hook nil
922 "Hook being run after inserting a new heading."
923 :group 'org-edit-structure
924 :type 'hook)
926 (defcustom org-enable-fixed-width-editor t
927 "Non-nil means, lines starting with \":\" are treated as fixed-width.
928 This currently only means, they are never auto-wrapped.
929 When nil, such lines will be treated like ordinary lines.
930 See also the QUOTE keyword."
931 :group 'org-edit-structure
932 :type 'boolean)
935 (defcustom org-goto-auto-isearch t
936 "Non-nil means, typing characters in org-goto starts incremental search."
937 :group 'org-edit-structure
938 :type 'boolean)
940 (defgroup org-sparse-trees nil
941 "Options concerning sparse trees in Org-mode."
942 :tag "Org Sparse Trees"
943 :group 'org-structure)
945 (defcustom org-highlight-sparse-tree-matches t
946 "Non-nil means, highlight all matches that define a sparse tree.
947 The highlights will automatically disappear the next time the buffer is
948 changed by an edit command."
949 :group 'org-sparse-trees
950 :type 'boolean)
952 (defcustom org-remove-highlights-with-change t
953 "Non-nil means, any change to the buffer will remove temporary highlights.
954 Such highlights are created by `org-occur' and `org-clock-display'.
955 When nil, `C-c C-c needs to be used to get rid of the highlights.
956 The highlights created by `org-preview-latex-fragment' always need
957 `C-c C-c' to be removed."
958 :group 'org-sparse-trees
959 :group 'org-time
960 :type 'boolean)
963 (defcustom org-occur-hook '(org-first-headline-recenter)
964 "Hook that is run after `org-occur' has constructed a sparse tree.
965 This can be used to recenter the window to show as much of the structure
966 as possible."
967 :group 'org-sparse-trees
968 :type 'hook)
970 (defgroup org-imenu-and-speedbar nil
971 "Options concerning imenu and speedbar in Org-mode."
972 :tag "Org Imenu and Speedbar"
973 :group 'org-structure)
975 (defcustom org-imenu-depth 2
976 "The maximum level for Imenu access to Org-mode headlines.
977 This also applied for speedbar access."
978 :group 'org-imenu-and-speedbar
979 :type 'integer)
981 (defgroup org-table nil
982 "Options concerning tables in Org-mode."
983 :tag "Org Table"
984 :group 'org)
986 (defcustom org-enable-table-editor 'optimized
987 "Non-nil means, lines starting with \"|\" are handled by the table editor.
988 When nil, such lines will be treated like ordinary lines.
990 When equal to the symbol `optimized', the table editor will be optimized to
991 do the following:
992 - Automatic overwrite mode in front of whitespace in table fields.
993 This makes the structure of the table stay in tact as long as the edited
994 field does not exceed the column width.
995 - Minimize the number of realigns. Normally, the table is aligned each time
996 TAB or RET are pressed to move to another field. With optimization this
997 happens only if changes to a field might have changed the column width.
998 Optimization requires replacing the functions `self-insert-command',
999 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1000 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1001 very good at guessing when a re-align will be necessary, but you can always
1002 force one with \\[org-ctrl-c-ctrl-c].
1004 If you would like to use the optimized version in Org-mode, but the
1005 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1007 This variable can be used to turn on and off the table editor during a session,
1008 but in order to toggle optimization, a restart is required.
1010 See also the variable `org-table-auto-blank-field'."
1011 :group 'org-table
1012 :type '(choice
1013 (const :tag "off" nil)
1014 (const :tag "on" t)
1015 (const :tag "on, optimized" optimized)))
1017 (defcustom org-self-insert-cluster-for-undo t
1018 "Non-nil means cluster self-insert commands for undo when possible.
1019 If this is set, then, like in the Emacs command loop, 20 consequtive
1020 characters will be undone together.
1021 This is configurable, because there is some impact on typing performance."
1022 :group 'org-table
1023 :type 'boolean)
1025 (defcustom org-table-tab-recognizes-table.el t
1026 "Non-nil means, TAB will automatically notice a table.el table.
1027 When it sees such a table, it moves point into it and - if necessary -
1028 calls `table-recognize-table'."
1029 :group 'org-table-editing
1030 :type 'boolean)
1032 (defgroup org-link nil
1033 "Options concerning links in Org-mode."
1034 :tag "Org Link"
1035 :group 'org)
1037 (defvar org-link-abbrev-alist-local nil
1038 "Buffer-local version of `org-link-abbrev-alist', which see.
1039 The value of this is taken from the #+LINK lines.")
1040 (make-variable-buffer-local 'org-link-abbrev-alist-local)
1042 (defcustom org-link-abbrev-alist nil
1043 "Alist of link abbreviations.
1044 The car of each element is a string, to be replaced at the start of a link.
1045 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1046 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1048 [[linkkey:tag][description]]
1050 The 'linkkey' must be a word word, starting with a letter, followed
1051 by letters, numbers, '-' or '_'.
1053 If REPLACE is a string, the tag will simply be appended to create the link.
1054 If the string contains \"%s\", the tag will be inserted there. Alternatively,
1055 the placeholder \"%h\" will cause a url-encoded version of the tag to
1056 be inserted at that point (see the function `url-hexify-string').
1058 REPLACE may also be a function that will be called with the tag as the
1059 only argument to create the link, which should be returned as a string.
1061 See the manual for examples."
1062 :group 'org-link
1063 :type '(repeat
1064 (cons
1065 (string :tag "Protocol")
1066 (choice
1067 (string :tag "Format")
1068 (function)))))
1070 (defcustom org-descriptive-links t
1071 "Non-nil means, hide link part and only show description of bracket links.
1072 Bracket links are like [[link][description]]. This variable sets the initial
1073 state in new org-mode buffers. The setting can then be toggled on a
1074 per-buffer basis from the Org->Hyperlinks menu."
1075 :group 'org-link
1076 :type 'boolean)
1078 (defcustom org-link-file-path-type 'adaptive
1079 "How the path name in file links should be stored.
1080 Valid values are:
1082 relative Relative to the current directory, i.e. the directory of the file
1083 into which the link is being inserted.
1084 absolute Absolute path, if possible with ~ for home directory.
1085 noabbrev Absolute path, no abbreviation of home directory.
1086 adaptive Use relative path for files in the current directory and sub-
1087 directories of it. For other files, use an absolute path."
1088 :group 'org-link
1089 :type '(choice
1090 (const relative)
1091 (const absolute)
1092 (const noabbrev)
1093 (const adaptive)))
1095 (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
1096 "Types of links that should be activated in Org-mode files.
1097 This is a list of symbols, each leading to the activation of a certain link
1098 type. In principle, it does not hurt to turn on most link types - there may
1099 be a small gain when turning off unused link types. The types are:
1101 bracket The recommended [[link][description]] or [[link]] links with hiding.
1102 angular Links in angular brackets that may contain whitespace like
1103 <bbdb:Carsten Dominik>.
1104 plain Plain links in normal text, no whitespace, like http://google.com.
1105 radio Text that is matched by a radio target, see manual for details.
1106 tag Tag settings in a headline (link to tag search).
1107 date Time stamps (link to calendar).
1108 footnote Footnote labels.
1110 Changing this variable requires a restart of Emacs to become effective."
1111 :group 'org-link
1112 :type '(set :greedy t
1113 (const :tag "Double bracket links (new style)" bracket)
1114 (const :tag "Angular bracket links (old style)" angular)
1115 (const :tag "Plain text links" plain)
1116 (const :tag "Radio target matches" radio)
1117 (const :tag "Tags" tag)
1118 (const :tag "Timestamps" date)
1119 (const :tag "Footnotes" footnote)))
1121 (defcustom org-make-link-description-function nil
1122 "Function to use to generate link descriptions from links. If
1123 nil the link location will be used. This function must take two
1124 parameters; the first is the link and the second the description
1125 org-insert-link has generated, and should return the description
1126 to use."
1127 :group 'org-link
1128 :type 'function)
1130 (defgroup org-link-store nil
1131 "Options concerning storing links in Org-mode."
1132 :tag "Org Store Link"
1133 :group 'org-link)
1135 (defcustom org-email-link-description-format "Email %c: %.30s"
1136 "Format of the description part of a link to an email or usenet message.
1137 The following %-escapes will be replaced by corresponding information:
1139 %F full \"From\" field
1140 %f name, taken from \"From\" field, address if no name
1141 %T full \"To\" field
1142 %t first name in \"To\" field, address if no name
1143 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1144 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1145 %s subject
1146 %m message-id.
1148 You may use normal field width specification between the % and the letter.
1149 This is for example useful to limit the length of the subject.
1151 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1152 :group 'org-link-store
1153 :type 'string)
1155 (defcustom org-from-is-user-regexp
1156 (let (r1 r2)
1157 (when (and user-mail-address (not (string= user-mail-address "")))
1158 (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
1159 (when (and user-full-name (not (string= user-full-name "")))
1160 (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
1161 (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
1162 "Regexp matched against the \"From:\" header of an email or usenet message.
1163 It should match if the message is from the user him/herself."
1164 :group 'org-link-store
1165 :type 'regexp)
1167 (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
1168 "Non-nil means, storing a link to an Org file will use entry IDs.
1170 Note that before this variable is even considered, org-id must be loaded,
1171 so please customize `org-modules' and turn it on.
1173 The variable can have the following values:
1175 t Create an ID if needed to make a link to the current entry.
1177 create-if-interactive
1178 If `org-store-link' is called directly (interactively, as a user
1179 command), do create an ID to support the link. But when doing the
1180 job for remember, only use the ID if it already exists. The
1181 purpose of this setting is to avoid proliferation of unwanted
1182 IDs, just because you happen to be in an Org file when you
1183 call `org-remember' that automatically and preemptively
1184 creates a link. If you do want to get an ID link in a remember
1185 template to an entry not having an ID, create it first by
1186 explicitly creating a link to it, using `C-c C-l' first.
1188 create-if-interactive-and-no-custom-id
1189 Like create-if-interactive, but do not create an ID if there is
1190 a CUSTOM_ID property defined in the entry. This is the default.
1192 use-existing
1193 Use existing ID, do not create one.
1195 nil Never use an ID to make a link, instead link using a text search for
1196 the headline text."
1197 :group 'org-link-store
1198 :type '(choice
1199 (const :tag "Create ID to make link" t)
1200 (const :tag "Create if storing link interactively"
1201 create-if-interactive)
1202 (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
1203 create-if-interactive-and-no-custom-id)
1204 (const :tag "Only use existing" use-existing)
1205 (const :tag "Do not use ID to create link" nil)))
1207 (defcustom org-context-in-file-links t
1208 "Non-nil means, file links from `org-store-link' contain context.
1209 A search string will be added to the file name with :: as separator and
1210 used to find the context when the link is activated by the command
1211 `org-open-at-point'.
1212 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1213 negates this setting for the duration of the command."
1214 :group 'org-link-store
1215 :type 'boolean)
1217 (defcustom org-keep-stored-link-after-insertion nil
1218 "Non-nil means, keep link in list for entire session.
1220 The command `org-store-link' adds a link pointing to the current
1221 location to an internal list. These links accumulate during a session.
1222 The command `org-insert-link' can be used to insert links into any
1223 Org-mode file (offering completion for all stored links). When this
1224 option is nil, every link which has been inserted once using \\[org-insert-link]
1225 will be removed from the list, to make completing the unused links
1226 more efficient."
1227 :group 'org-link-store
1228 :type 'boolean)
1230 (defgroup org-link-follow nil
1231 "Options concerning following links in Org-mode."
1232 :tag "Org Follow Link"
1233 :group 'org-link)
1235 (defcustom org-link-translation-function nil
1236 "Function to translate links with different syntax to Org syntax.
1237 This can be used to translate links created for example by the Planner
1238 or emacs-wiki packages to Org syntax.
1239 The function must accept two parameters, a TYPE containing the link
1240 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1241 which is everything after the link protocol. It should return a cons
1242 with possibly modified values of type and path.
1243 Org contains a function for this, so if you set this variable to
1244 `org-translate-link-from-planner', you should be able follow many
1245 links created by planner."
1246 :group 'org-link-follow
1247 :type 'function)
1249 (defcustom org-follow-link-hook nil
1250 "Hook that is run after a link has been followed."
1251 :group 'org-link-follow
1252 :type 'hook)
1254 (defcustom org-tab-follows-link nil
1255 "Non-nil means, on links TAB will follow the link.
1256 Needs to be set before org.el is loaded.
1257 This really should not be used, it does not make sense, and the
1258 implementation is bad."
1259 :group 'org-link-follow
1260 :type 'boolean)
1262 (defcustom org-return-follows-link nil
1263 "Non-nil means, on links RET will follow the link.
1264 Needs to be set before org.el is loaded."
1265 :group 'org-link-follow
1266 :type 'boolean)
1268 (defcustom org-mouse-1-follows-link
1269 (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
1270 "Non-nil means, mouse-1 on a link will follow the link.
1271 A longer mouse click will still set point. Does not work on XEmacs.
1272 Needs to be set before org.el is loaded."
1273 :group 'org-link-follow
1274 :type 'boolean)
1276 (defcustom org-mark-ring-length 4
1277 "Number of different positions to be recorded in the ring
1278 Changing this requires a restart of Emacs to work correctly."
1279 :group 'org-link-follow
1280 :type 'integer)
1282 (defcustom org-link-frame-setup
1283 '((vm . vm-visit-folder-other-frame)
1284 (gnus . gnus-other-frame)
1285 (file . find-file-other-window))
1286 "Setup the frame configuration for following links.
1287 When following a link with Emacs, it may often be useful to display
1288 this link in another window or frame. This variable can be used to
1289 set this up for the different types of links.
1290 For VM, use any of
1291 `vm-visit-folder'
1292 `vm-visit-folder-other-frame'
1293 For Gnus, use any of
1294 `gnus'
1295 `gnus-other-frame'
1296 `org-gnus-no-new-news'
1297 For FILE, use any of
1298 `find-file'
1299 `find-file-other-window'
1300 `find-file-other-frame'
1301 For the calendar, use the variable `calendar-setup'.
1302 For BBDB, it is currently only possible to display the matches in
1303 another window."
1304 :group 'org-link-follow
1305 :type '(list
1306 (cons (const vm)
1307 (choice
1308 (const vm-visit-folder)
1309 (const vm-visit-folder-other-window)
1310 (const vm-visit-folder-other-frame)))
1311 (cons (const gnus)
1312 (choice
1313 (const gnus)
1314 (const gnus-other-frame)
1315 (const org-gnus-no-new-news)))
1316 (cons (const file)
1317 (choice
1318 (const find-file)
1319 (const find-file-other-window)
1320 (const find-file-other-frame)))))
1322 (defcustom org-display-internal-link-with-indirect-buffer nil
1323 "Non-nil means, use indirect buffer to display infile links.
1324 Activating internal links (from one location in a file to another location
1325 in the same file) normally just jumps to the location. When the link is
1326 activated with a C-u prefix (or with mouse-3), the link is displayed in
1327 another window. When this option is set, the other window actually displays
1328 an indirect buffer clone of the current buffer, to avoid any visibility
1329 changes to the current buffer."
1330 :group 'org-link-follow
1331 :type 'boolean)
1333 (defcustom org-open-non-existing-files nil
1334 "Non-nil means, `org-open-file' will open non-existing files.
1335 When nil, an error will be generated.
1336 This variable applies only to external applications because they
1337 might choke on non-existing files. If the link is to a file that
1338 will be openend in Emacs, the variable is ignored."
1339 :group 'org-link-follow
1340 :type 'boolean)
1342 (defcustom org-open-directory-means-index-dot-org nil
1343 "Non-nil means, a link to a directory really means to index.org.
1344 When nil, following a directory link will run dired or open a finder/explorer
1345 window on that directory."
1346 :group 'org-link-follow
1347 :type 'boolean)
1349 (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
1350 "Function and arguments to call for following mailto links.
1351 This is a list with the first element being a lisp function, and the
1352 remaining elements being arguments to the function. In string arguments,
1353 %a will be replaced by the address, and %s will be replaced by the subject
1354 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1355 :group 'org-link-follow
1356 :type '(choice
1357 (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1358 (const :tag "compose-mail" (compose-mail "%a" "%s"))
1359 (const :tag "message-mail" (message-mail "%a" "%s"))
1360 (cons :tag "other" (function) (repeat :tag "argument" sexp))))
1362 (defcustom org-confirm-shell-link-function 'yes-or-no-p
1363 "Non-nil means, ask for confirmation before executing shell links.
1364 Shell links can be dangerous: just think about a link
1366 [[shell:rm -rf ~/*][Google Search]]
1368 This link would show up in your Org-mode document as \"Google Search\",
1369 but really it would remove your entire home directory.
1370 Therefore we advise against setting this variable to nil.
1371 Just change it to `y-or-n-p' if you want to confirm with a
1372 single keystroke rather than having to type \"yes\"."
1373 :group 'org-link-follow
1374 :type '(choice
1375 (const :tag "with yes-or-no (safer)" yes-or-no-p)
1376 (const :tag "with y-or-n (faster)" y-or-n-p)
1377 (const :tag "no confirmation (dangerous)" nil)))
1379 (defcustom org-confirm-elisp-link-function 'yes-or-no-p
1380 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1381 Elisp links can be dangerous: just think about a link
1383 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1385 This link would show up in your Org-mode document as \"Google Search\",
1386 but really it would remove your entire home directory.
1387 Therefore we advise against setting this variable to nil.
1388 Just change it to `y-or-n-p' if you want to confirm with a
1389 single keystroke rather than having to type \"yes\"."
1390 :group 'org-link-follow
1391 :type '(choice
1392 (const :tag "with yes-or-no (safer)" yes-or-no-p)
1393 (const :tag "with y-or-n (faster)" y-or-n-p)
1394 (const :tag "no confirmation (dangerous)" nil)))
1396 (defconst org-file-apps-defaults-gnu
1397 '((remote . emacs)
1398 (system . mailcap)
1399 (t . mailcap))
1400 "Default file applications on a UNIX or GNU/Linux system.
1401 See `org-file-apps'.")
1403 (defconst org-file-apps-defaults-macosx
1404 '((remote . emacs)
1405 (t . "open %s")
1406 (system . "open %s")
1407 ("ps.gz" . "gv %s")
1408 ("eps.gz" . "gv %s")
1409 ("dvi" . "xdvi %s")
1410 ("fig" . "xfig %s"))
1411 "Default file applications on a MacOS X system.
1412 The system \"open\" is known as a default, but we use X11 applications
1413 for some files for which the OS does not have a good default.
1414 See `org-file-apps'.")
1416 (defconst org-file-apps-defaults-windowsnt
1417 (list
1418 '(remote . emacs)
1419 (cons t
1420 (list (if (featurep 'xemacs)
1421 'mswindows-shell-execute
1422 'w32-shell-execute)
1423 "open" 'file))
1424 (cons 'system
1425 (list (if (featurep 'xemacs)
1426 'mswindows-shell-execute
1427 'w32-shell-execute)
1428 "open" 'file)))
1429 "Default file applications on a Windows NT system.
1430 The system \"open\" is used for most files.
1431 See `org-file-apps'.")
1433 (defcustom org-file-apps
1435 (auto-mode . emacs)
1436 ("\\.mm\\'" . default)
1437 ("\\.x?html?\\'" . default)
1438 ("\\.pdf\\'" . default)
1440 "External applications for opening `file:path' items in a document.
1441 Org-mode uses system defaults for different file types, but
1442 you can use this variable to set the application for a given file
1443 extension. The entries in this list are cons cells where the car identifies
1444 files and the cdr the corresponding command. Possible values for the
1445 file identifier are
1446 \"regex\" Regular expression matched against the file name. For backward
1447 compatibility, this can also be a string with only alphanumeric
1448 characters, which is then interpreted as an extension.
1449 `directory' Matches a directory
1450 `remote' Matches a remote file, accessible through tramp or efs.
1451 Remote files most likely should be visited through Emacs
1452 because external applications cannot handle such paths.
1453 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1454 so all files Emacs knows how to handle. Using this with
1455 command `emacs' will open most files in Emacs. Beware that this
1456 will also open html files inside Emacs, unless you add
1457 (\"html\" . default) to the list as well.
1458 t Default for files not matched by any of the other options.
1459 `system' The system command to open files, like `open' on Windows
1460 and Mac OS X, and mailcap under GNU/Linux. This is the command
1461 that will be selected if you call `C-c C-o' with a double
1462 `C-u C-u' prefix.
1464 Possible values for the command are:
1465 `emacs' The file will be visited by the current Emacs process.
1466 `default' Use the default application for this file type, which is the
1467 association for t in the list, most likely in the system-specific
1468 part.
1469 This can be used to overrule an unwanted setting in the
1470 system-specific variable.
1471 `system' Use the system command for opening files, like \"open\".
1472 This command is specified by the entry whose car is `system'.
1473 Most likely, the system-specific version of this variable
1474 does define this command, but you can overrule/replace it
1475 here.
1476 string A command to be executed by a shell; %s will be replaced
1477 by the path to the file.
1478 sexp A Lisp form which will be evaluated. The file path will
1479 be available in the Lisp variable `file'.
1480 For more examples, see the system specific constants
1481 `org-file-apps-defaults-macosx'
1482 `org-file-apps-defaults-windowsnt'
1483 `org-file-apps-defaults-gnu'."
1484 :group 'org-link-follow
1485 :type '(repeat
1486 (cons (choice :value ""
1487 (string :tag "Extension")
1488 (const :tag "System command to open files" system)
1489 (const :tag "Default for unrecognized files" t)
1490 (const :tag "Remote file" remote)
1491 (const :tag "Links to a directory" directory)
1492 (const :tag "Any files that have Emacs modes"
1493 auto-mode))
1494 (choice :value ""
1495 (const :tag "Visit with Emacs" emacs)
1496 (const :tag "Use default" default)
1497 (const :tag "Use the system command" system)
1498 (string :tag "Command")
1499 (sexp :tag "Lisp form")))))
1501 (defgroup org-refile nil
1502 "Options concerning refiling entries in Org-mode."
1503 :tag "Org Refile"
1504 :group 'org)
1506 (defcustom org-directory "~/org"
1507 "Directory with org files.
1508 This is just a default location to look for Org files. There is no need
1509 at all to put your files into this directory. It is only used in the
1510 following situations:
1512 1. When a remember template specifies a target file that is not an
1513 absolute path. The path will then be interpreted relative to
1514 `org-directory'
1515 2. When a remember note is filed away in an interactive way (when exiting the
1516 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1517 with `org-directory' as the default path."
1518 :group 'org-refile
1519 :group 'org-remember
1520 :type 'directory)
1522 (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
1523 "Default target for storing notes.
1524 Used by the hooks for remember.el. This can be a string, or nil to mean
1525 the value of `remember-data-file'.
1526 You can set this on a per-template basis with the variable
1527 `org-remember-templates'."
1528 :group 'org-refile
1529 :group 'org-remember
1530 :type '(choice
1531 (const :tag "Default from remember-data-file" nil)
1532 file))
1534 (defcustom org-goto-interface 'outline
1535 "The default interface to be used for `org-goto'.
1536 Allowed values are:
1537 outline The interface shows an outline of the relevant file
1538 and the correct heading is found by moving through
1539 the outline or by searching with incremental search.
1540 outline-path-completion Headlines in the current buffer are offered via
1541 completion. This is the interface also used by
1542 the refile command."
1543 :group 'org-refile
1544 :type '(choice
1545 (const :tag "Outline" outline)
1546 (const :tag "Outline-path-completion" outline-path-completion)))
1548 (defcustom org-goto-max-level 5
1549 "Maximum level to be considered when running org-goto with refile interface."
1550 :group 'org-refile
1551 :type 'integer)
1553 (defcustom org-reverse-note-order nil
1554 "Non-nil means, store new notes at the beginning of a file or entry.
1555 When nil, new notes will be filed to the end of a file or entry.
1556 This can also be a list with cons cells of regular expressions that
1557 are matched against file names, and values."
1558 :group 'org-remember
1559 :group 'org-refile
1560 :type '(choice
1561 (const :tag "Reverse always" t)
1562 (const :tag "Reverse never" nil)
1563 (repeat :tag "By file name regexp"
1564 (cons regexp boolean))))
1566 (defcustom org-refile-targets nil
1567 "Targets for refiling entries with \\[org-refile].
1568 This is list of cons cells. Each cell contains:
1569 - a specification of the files to be considered, either a list of files,
1570 or a symbol whose function or variable value will be used to retrieve
1571 a file name or a list of file names. If you use `org-agenda-files' for
1572 that, all agenda files will be scanned for targets. Nil means, consider
1573 headings in the current buffer.
1574 - A specification of how to find candidate refile targets. This may be
1575 any of:
1576 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1577 This tag has to be present in all target headlines, inheritance will
1578 not be considered.
1579 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1580 todo keyword.
1581 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1582 headlines that are refiling targets.
1583 - a cons cell (:level . N). Any headline of level N is considered a target.
1584 Note that, when `org-odd-levels-only' is set, level corresponds to
1585 order in hierarchy, not to the number of stars.
1586 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1587 Note that, when `org-odd-levels-only' is set, level corresponds to
1588 order in hierarchy, not to the number of stars.
1590 You can set the variable `org-refile-target-verify-function' to a function
1591 to verify each headline found by the simple critery above.
1593 When this variable is nil, all top-level headlines in the current buffer
1594 are used, equivalent to the value `((nil . (:level . 1))'."
1595 :group 'org-refile
1596 :type '(repeat
1597 (cons
1598 (choice :value org-agenda-files
1599 (const :tag "All agenda files" org-agenda-files)
1600 (const :tag "Current buffer" nil)
1601 (function) (variable) (file))
1602 (choice :tag "Identify target headline by"
1603 (cons :tag "Specific tag" (const :value :tag) (string))
1604 (cons :tag "TODO keyword" (const :value :todo) (string))
1605 (cons :tag "Regular expression" (const :value :regexp) (regexp))
1606 (cons :tag "Level number" (const :value :level) (integer))
1607 (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
1609 (defcustom org-refile-target-verify-function nil
1610 "Function to verify if the headline at point should be a refile target.
1611 The function will be called without arguments, with point at the
1612 beginning of the headline. It should return t and leave point
1613 where it is if the headline is a valid target for refiling.
1615 If the target should not be selected, the function must return nil.
1616 In addition to this, it may move point to a place from where the search
1617 should be continued. For example, the function may decide that the entire
1618 subtree of the current entry should be excluded and move point to the end
1619 of the subtree."
1620 :group 'org-refile
1621 :type 'function)
1623 (defcustom org-refile-use-outline-path nil
1624 "Non-nil means, provide refile targets as paths.
1625 So a level 3 headline will be available as level1/level2/level3.
1627 When the value is `file', also include the file name (without directory)
1628 into the path. In this case, you can also stop the completion after
1629 the file name, to get entries inserted as top level in the file.
1631 When `full-file-path', include the full file path."
1632 :group 'org-refile
1633 :type '(choice
1634 (const :tag "Not" nil)
1635 (const :tag "Yes" t)
1636 (const :tag "Start with file name" file)
1637 (const :tag "Start with full file path" full-file-path)))
1639 (defcustom org-outline-path-complete-in-steps t
1640 "Non-nil means, complete the outline path in hierarchical steps.
1641 When Org-mode uses the refile interface to select an outline path
1642 \(see variable `org-refile-use-outline-path'), the completion of
1643 the path can be done is a single go, or if can be done in steps down
1644 the headline hierarchy. Going in steps is probably the best if you
1645 do not use a special completion package like `ido' or `icicles'.
1646 However, when using these packages, going in one step can be very
1647 fast, while still showing the whole path to the entry."
1648 :group 'org-refile
1649 :type 'boolean)
1651 (defcustom org-refile-allow-creating-parent-nodes nil
1652 "Non-nil means, allow to create new nodes as refile targets.
1653 New nodes are then created by adding \"/new node name\" to the completion
1654 of an existing node. When the value of this variable is `confirm',
1655 new node creation must be confirmed by the user (recommended)
1656 When nil, the completion must match an existing entry.
1658 Note that, if the new heading is not seen by the criteria
1659 listed in `org-refile-targets', multiple instances of the same
1660 heading would be created by trying again to file under the new
1661 heading."
1662 :group 'org-refile
1663 :type '(choice
1664 (const :tag "Never" nil)
1665 (const :tag "Always" t)
1666 (const :tag "Prompt for confirmation" confirm)))
1668 (defgroup org-todo nil
1669 "Options concerning TODO items in Org-mode."
1670 :tag "Org TODO"
1671 :group 'org)
1673 (defgroup org-progress nil
1674 "Options concerning Progress logging in Org-mode."
1675 :tag "Org Progress"
1676 :group 'org-time)
1678 (defvar org-todo-interpretation-widgets
1680 (:tag "Sequence (cycling hits every state)" sequence)
1681 (:tag "Type (cycling directly to DONE)" type))
1682 "The available interpretation symbols for customizing
1683 `org-todo-keywords'.
1684 Interested libraries should add to this list.")
1686 (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
1687 "List of TODO entry keyword sequences and their interpretation.
1688 \\<org-mode-map>This is a list of sequences.
1690 Each sequence starts with a symbol, either `sequence' or `type',
1691 indicating if the keywords should be interpreted as a sequence of
1692 action steps, or as different types of TODO items. The first
1693 keywords are states requiring action - these states will select a headline
1694 for inclusion into the global TODO list Org-mode produces. If one of
1695 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1696 signify that no further action is necessary. If \"|\" is not found,
1697 the last keyword is treated as the only DONE state of the sequence.
1699 The command \\[org-todo] cycles an entry through these states, and one
1700 additional state where no keyword is present. For details about this
1701 cycling, see the manual.
1703 TODO keywords and interpretation can also be set on a per-file basis with
1704 the special #+SEQ_TODO and #+TYP_TODO lines.
1706 Each keyword can optionally specify a character for fast state selection
1707 \(in combination with the variable `org-use-fast-todo-selection')
1708 and specifiers for state change logging, using the same syntax
1709 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1710 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1711 indicates to record a time stamp each time this state is selected.
1713 Each keyword may also specify if a timestamp or a note should be
1714 recorded when entering or leaving the state, by adding additional
1715 characters in the parenthesis after the keyword. This looks like this:
1716 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1717 record only the time of the state change. With X and Y being either
1718 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1719 Y when leaving the state if and only if the *target* state does not
1720 define X. You may omit any of the fast-selection key or X or /Y,
1721 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1723 For backward compatibility, this variable may also be just a list
1724 of keywords - in this case the interpretation (sequence or type) will be
1725 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1726 :group 'org-todo
1727 :group 'org-keywords
1728 :type '(choice
1729 (repeat :tag "Old syntax, just keywords"
1730 (string :tag "Keyword"))
1731 (repeat :tag "New syntax"
1732 (cons
1733 (choice
1734 :tag "Interpretation"
1735 ;;Quick and dirty way to see
1736 ;;`org-todo-interpretations'. This takes the
1737 ;;place of item arguments
1738 :convert-widget
1739 (lambda (widget)
1740 (widget-put widget
1741 :args (mapcar
1742 #'(lambda (x)
1743 (widget-convert
1744 (cons 'const x)))
1745 org-todo-interpretation-widgets))
1746 widget))
1747 (repeat
1748 (string :tag "Keyword"))))))
1750 (defvar org-todo-keywords-1 nil
1751 "All TODO and DONE keywords active in a buffer.")
1752 (make-variable-buffer-local 'org-todo-keywords-1)
1753 (defvar org-todo-keywords-for-agenda nil)
1754 (defvar org-done-keywords-for-agenda nil)
1755 (defvar org-drawers-for-agenda nil)
1756 (defvar org-todo-keyword-alist-for-agenda nil)
1757 (defvar org-tag-alist-for-agenda nil)
1758 (defvar org-agenda-contributing-files nil)
1759 (defvar org-not-done-keywords nil)
1760 (make-variable-buffer-local 'org-not-done-keywords)
1761 (defvar org-done-keywords nil)
1762 (make-variable-buffer-local 'org-done-keywords)
1763 (defvar org-todo-heads nil)
1764 (make-variable-buffer-local 'org-todo-heads)
1765 (defvar org-todo-sets nil)
1766 (make-variable-buffer-local 'org-todo-sets)
1767 (defvar org-todo-log-states nil)
1768 (make-variable-buffer-local 'org-todo-log-states)
1769 (defvar org-todo-kwd-alist nil)
1770 (make-variable-buffer-local 'org-todo-kwd-alist)
1771 (defvar org-todo-key-alist nil)
1772 (make-variable-buffer-local 'org-todo-key-alist)
1773 (defvar org-todo-key-trigger nil)
1774 (make-variable-buffer-local 'org-todo-key-trigger)
1776 (defcustom org-todo-interpretation 'sequence
1777 "Controls how TODO keywords are interpreted.
1778 This variable is in principle obsolete and is only used for
1779 backward compatibility, if the interpretation of todo keywords is
1780 not given already in `org-todo-keywords'. See that variable for
1781 more information."
1782 :group 'org-todo
1783 :group 'org-keywords
1784 :type '(choice (const sequence)
1785 (const type)))
1787 (defcustom org-use-fast-todo-selection t
1788 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1789 This variable describes if and under what circumstances the cycling
1790 mechanism for TODO keywords will be replaced by a single-key, direct
1791 selection scheme.
1793 When nil, fast selection is never used.
1795 When the symbol `prefix', it will be used when `org-todo' is called with
1796 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1797 in an agenda buffer.
1799 When t, fast selection is used by default. In this case, the prefix
1800 argument forces cycling instead.
1802 In all cases, the special interface is only used if access keys have actually
1803 been assigned by the user, i.e. if keywords in the configuration are followed
1804 by a letter in parenthesis, like TODO(t)."
1805 :group 'org-todo
1806 :type '(choice
1807 (const :tag "Never" nil)
1808 (const :tag "By default" t)
1809 (const :tag "Only with C-u C-c C-t" prefix)))
1811 (defcustom org-provide-todo-statistics t
1812 "Non-nil means, update todo statistics after insert and toggle.
1813 ALL-HEADLINES means update todo statistics by including headlines
1814 with no TODO keyword as well, counting them as not done.
1815 A list of TODO keywords means the same, but skip keywords that are
1816 not in this list.
1818 When this is set, todo statistics is updated in the parent of the
1819 current entry each time a todo state is changed."
1820 :group 'org-todo
1821 :type '(choice
1822 (const :tag "Yes, only for TODO entries" t)
1823 (const :tag "Yes, including all entries" 'all-headlines)
1824 (repeat :tag "Yes, for TODOs in this list"
1825 (string :tag "TODO keyword"))
1826 (other :tag "No TODO statistics" nil)))
1828 (defcustom org-hierarchical-todo-statistics t
1829 "Non-nil means, TODO statistics covers just direct children.
1830 When nil, all entries in the subtree are considered.
1831 This has only an effect if `org-provide-todo-statistics' is set.
1832 To set this to nil for only a single subtree, use a COOKIE_DATA
1833 property and include the word \"recursive\" into the value."
1834 :group 'org-todo
1835 :type 'boolean)
1837 (defcustom org-after-todo-state-change-hook nil
1838 "Hook which is run after the state of a TODO item was changed.
1839 The new state (a string with a TODO keyword, or nil) is available in the
1840 Lisp variable `state'."
1841 :group 'org-todo
1842 :type 'hook)
1844 (defvar org-blocker-hook nil
1845 "Hook for functions that are allowed to block a state change.
1847 Each function gets as its single argument a property list, see
1848 `org-trigger-hook' for more information about this list.
1850 If any of the functions in this hook returns nil, the state change
1851 is blocked.")
1853 (defvar org-trigger-hook nil
1854 "Hook for functions that are triggered by a state change.
1856 Each function gets as its single argument a property list with at least
1857 the following elements:
1859 (:type type-of-change :position pos-at-entry-start
1860 :from old-state :to new-state)
1862 Depending on the type, more properties may be present.
1864 This mechanism is currently implemented for:
1866 TODO state changes
1867 ------------------
1868 :type todo-state-change
1869 :from previous state (keyword as a string), or nil, or a symbol
1870 'todo' or 'done', to indicate the general type of state.
1871 :to new state, like in :from")
1873 (defcustom org-enforce-todo-dependencies nil
1874 "Non-nil means, undone TODO entries will block switching the parent to DONE.
1875 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
1876 be blocked if any prior sibling is not yet done.
1877 Finally, if the parent is blocked because of ordered siblings of its own,
1878 the child will also be blocked.
1879 This variable needs to be set before org.el is loaded, and you need to
1880 restart Emacs after a change to make the change effective. The only way
1881 to change is while Emacs is running is through the customize interface."
1882 :set (lambda (var val)
1883 (set var val)
1884 (if val
1885 (add-hook 'org-blocker-hook
1886 'org-block-todo-from-children-or-siblings-or-parent)
1887 (remove-hook 'org-blocker-hook
1888 'org-block-todo-from-children-or-siblings-or-parent)))
1889 :group 'org-todo
1890 :type 'boolean)
1892 (defcustom org-enforce-todo-checkbox-dependencies nil
1893 "Non-nil means, unchecked boxes will block switching the parent to DONE.
1894 When this is nil, checkboxes have no influence on switching TODO states.
1895 When non-nil, you first need to check off all check boxes before the TODO
1896 entry can be switched to DONE.
1897 This variable needs to be set before org.el is loaded, and you need to
1898 restart Emacs after a change to make the change effective. The only way
1899 to change is while Emacs is running is through the customize interface."
1900 :set (lambda (var val)
1901 (set var val)
1902 (if val
1903 (add-hook 'org-blocker-hook
1904 'org-block-todo-from-checkboxes)
1905 (remove-hook 'org-blocker-hook
1906 'org-block-todo-from-checkboxes)))
1907 :group 'org-todo
1908 :type 'boolean)
1910 (defcustom org-treat-insert-todo-heading-as-state-change nil
1911 "Non-nil means, inserting a TODO heading is treated as state change.
1912 So when the command \\[org-insert-todo-heading] is used, state change
1913 logging will apply if appropriate. When nil, the new TODO item will
1914 be inserted directly, and no logging will take place."
1915 :group 'org-todo
1916 :type 'boolean)
1918 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
1919 "Non-nil means, switching TODO states with S-cursor counts as state change.
1920 This is the default behavior. However, setting this to nil allows a
1921 convenient way to select a TODO state and bypass any logging associated
1922 with that."
1923 :group 'org-todo
1924 :type 'boolean)
1926 (defcustom org-todo-state-tags-triggers nil
1927 "Tag changes that should be triggered by TODO state changes.
1928 This is a list. Each entry is
1930 (state-change (tag . flag) .......)
1932 State-change can be a string with a state, and empty string to indicate the
1933 state that has no TODO keyword, or it can be one of the symbols `todo'
1934 or `done', meaning any not-done or done state, respectively."
1935 :group 'org-todo
1936 :group 'org-tags
1937 :type '(repeat
1938 (cons (choice :tag "When changing to"
1939 (const :tag "Not-done state" todo)
1940 (const :tag "Done state" done)
1941 (string :tag "State"))
1942 (repeat
1943 (cons :tag "Tag action"
1944 (string :tag "Tag")
1945 (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
1947 (defcustom org-log-done nil
1948 "Information to record when a task moves to the DONE state.
1950 Possible values are:
1952 nil Don't add anything, just change the keyword
1953 time Add a time stamp to the task
1954 note Prompt a closing note and add it with template `org-log-note-headings'
1956 This option can also be set with on a per-file-basis with
1958 #+STARTUP: nologdone
1959 #+STARTUP: logdone
1960 #+STARTUP: lognotedone
1962 You can have local logging settings for a subtree by setting the LOGGING
1963 property to one or more of these keywords."
1964 :group 'org-todo
1965 :group 'org-progress
1966 :type '(choice
1967 (const :tag "No logging" nil)
1968 (const :tag "Record CLOSED timestamp" time)
1969 (const :tag "Record CLOSED timestamp with closing note." note)))
1971 ;; Normalize old uses of org-log-done.
1972 (cond
1973 ((eq org-log-done t) (setq org-log-done 'time))
1974 ((and (listp org-log-done) (memq 'done org-log-done))
1975 (setq org-log-done 'note)))
1977 (defcustom org-log-note-clock-out nil
1978 "Non-nil means, record a note when clocking out of an item.
1979 This can also be configured on a per-file basis by adding one of
1980 the following lines anywhere in the buffer:
1982 #+STARTUP: lognoteclock-out
1983 #+STARTUP: nolognoteclock-out"
1984 :group 'org-todo
1985 :group 'org-progress
1986 :type 'boolean)
1988 (defcustom org-log-done-with-time t
1989 "Non-nil means, the CLOSED time stamp will contain date and time.
1990 When nil, only the date will be recorded."
1991 :group 'org-progress
1992 :type 'boolean)
1994 (defcustom org-log-note-headings
1995 '((done . "CLOSING NOTE %t")
1996 (state . "State %-12s from %-12S %t")
1997 (note . "Note taken on %t")
1998 (clock-out . ""))
1999 "Headings for notes added to entries.
2000 The value is an alist, with the car being a symbol indicating the note
2001 context, and the cdr is the heading to be used. The heading may also be the
2002 empty string.
2003 %t in the heading will be replaced by a time stamp.
2004 %s will be replaced by the new TODO state, in double quotes.
2005 %S will be replaced by the old TODO state, in double quotes.
2006 %u will be replaced by the user name.
2007 %U will be replaced by the full user name."
2008 :group 'org-todo
2009 :group 'org-progress
2010 :type '(list :greedy t
2011 (cons (const :tag "Heading when closing an item" done) string)
2012 (cons (const :tag
2013 "Heading when changing todo state (todo sequence only)"
2014 state) string)
2015 (cons (const :tag "Heading when just taking a note" note) string)
2016 (cons (const :tag "Heading when clocking out" clock-out) string)))
2018 (unless (assq 'note org-log-note-headings)
2019 (push '(note . "%t") org-log-note-headings))
2021 (defcustom org-log-into-drawer nil
2022 "Non-nil means, insert state change notes and time stamps into a drawer.
2023 When nil, state changes notes will be inserted after the headline and
2024 any scheduling and clock lines, but not inside a drawer.
2026 The value of this variable should be the name of the drawer to use.
2027 LOGBOOK is proposed at the default drawer for this purpose, you can
2028 also set this to a string to define the drawer of your choice.
2030 A value of t is also allowed, representing \"LOGBOOK\".
2032 If this variable is set, `org-log-state-notes-insert-after-drawers'
2033 will be ignored.
2035 You can set the property LOG_INTO_DRAWER to overrule this setting for
2036 a subtree."
2037 :group 'org-todo
2038 :group 'org-progress
2039 :type '(choice
2040 (const :tag "Not into a drawer" nil)
2041 (const :tag "LOGBOOK" t)
2042 (string :tag "Other")))
2044 (if (fboundp 'defvaralias)
2045 (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
2047 (defun org-log-into-drawer ()
2048 "Return the value of `org-log-into-drawer', but let properties overrule.
2049 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2050 used instead of the default value."
2051 (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
2052 (cond
2053 ((or (not p) (equal p "nil")) org-log-into-drawer)
2054 ((equal p "t") "LOGBOOK")
2055 (t p))))
2057 (defcustom org-log-state-notes-insert-after-drawers nil
2058 "Non-nil means, insert state change notes after any drawers in entry.
2059 Only the drawers that *immediately* follow the headline and the
2060 deadline/scheduled line are skipped.
2061 When nil, insert notes right after the heading and perhaps the line
2062 with deadline/scheduling if present.
2064 This variable will have no effect if `org-log-into-drawer' is
2065 set."
2066 :group 'org-todo
2067 :group 'org-progress
2068 :type 'boolean)
2070 (defcustom org-log-states-order-reversed t
2071 "Non-nil means, the latest state change note will be directly after heading.
2072 When nil, the notes will be orderer according to time."
2073 :group 'org-todo
2074 :group 'org-progress
2075 :type 'boolean)
2077 (defcustom org-log-repeat 'time
2078 "Non-nil means, record moving through the DONE state when triggering repeat.
2079 An auto-repeating task is immediately switched back to TODO when
2080 marked DONE. If you are not logging state changes (by adding \"@\"
2081 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2082 record a closing note, there will be no record of the task moving
2083 through DONE. This variable forces taking a note anyway.
2085 nil Don't force a record
2086 time Record a time stamp
2087 note Record a note
2089 This option can also be set with on a per-file-basis with
2091 #+STARTUP: logrepeat
2092 #+STARTUP: lognoterepeat
2093 #+STARTUP: nologrepeat
2095 You can have local logging settings for a subtree by setting the LOGGING
2096 property to one or more of these keywords."
2097 :group 'org-todo
2098 :group 'org-progress
2099 :type '(choice
2100 (const :tag "Don't force a record" nil)
2101 (const :tag "Force recording the DONE state" time)
2102 (const :tag "Force recording a note with the DONE state" note)))
2105 (defgroup org-priorities nil
2106 "Priorities in Org-mode."
2107 :tag "Org Priorities"
2108 :group 'org-todo)
2110 (defcustom org-enable-priority-commands t
2111 "Non-nil means, priority commands are active.
2112 When nil, these commands will be disabled, so that you never accidentally
2113 set a priority."
2114 :group 'org-priorities
2115 :type 'boolean)
2117 (defcustom org-highest-priority ?A
2118 "The highest priority of TODO items. A character like ?A, ?B etc.
2119 Must have a smaller ASCII number than `org-lowest-priority'."
2120 :group 'org-priorities
2121 :type 'character)
2123 (defcustom org-lowest-priority ?C
2124 "The lowest priority of TODO items. A character like ?A, ?B etc.
2125 Must have a larger ASCII number than `org-highest-priority'."
2126 :group 'org-priorities
2127 :type 'character)
2129 (defcustom org-default-priority ?B
2130 "The default priority of TODO items.
2131 This is the priority an item get if no explicit priority is given."
2132 :group 'org-priorities
2133 :type 'character)
2135 (defcustom org-priority-start-cycle-with-default t
2136 "Non-nil means, start with default priority when starting to cycle.
2137 When this is nil, the first step in the cycle will be (depending on the
2138 command used) one higher or lower that the default priority."
2139 :group 'org-priorities
2140 :type 'boolean)
2142 (defgroup org-time nil
2143 "Options concerning time stamps and deadlines in Org-mode."
2144 :tag "Org Time"
2145 :group 'org)
2147 (defcustom org-insert-labeled-timestamps-at-point nil
2148 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
2149 When nil, these labeled time stamps are forces into the second line of an
2150 entry, just after the headline. When scheduling from the global TODO list,
2151 the time stamp will always be forced into the second line."
2152 :group 'org-time
2153 :type 'boolean)
2155 (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
2156 "Formats for `format-time-string' which are used for time stamps.
2157 It is not recommended to change this constant.")
2159 (defcustom org-time-stamp-rounding-minutes '(0 5)
2160 "Number of minutes to round time stamps to.
2161 These are two values, the first applies when first creating a time stamp.
2162 The second applies when changing it with the commands `S-up' and `S-down'.
2163 When changing the time stamp, this means that it will change in steps
2164 of N minutes, as given by the second value.
2166 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2167 numbers should be factors of 60, so for example 5, 10, 15.
2169 When this is larger than 1, you can still force an exact time-stamp by using
2170 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
2171 and by using a prefix arg to `S-up/down' to specify the exact number
2172 of minutes to shift."
2173 :group 'org-time
2174 :get '(lambda (var) ; Make sure all entries have 5 elements
2175 (if (integerp (default-value var))
2176 (list (default-value var) 5)
2177 (default-value var)))
2178 :type '(list
2179 (integer :tag "when inserting times")
2180 (integer :tag "when modifying times")))
2182 ;; Normalize old customizations of this variable.
2183 (when (integerp org-time-stamp-rounding-minutes)
2184 (setq org-time-stamp-rounding-minutes
2185 (list org-time-stamp-rounding-minutes
2186 org-time-stamp-rounding-minutes)))
2188 (defcustom org-display-custom-times nil
2189 "Non-nil means, overlay custom formats over all time stamps.
2190 The formats are defined through the variable `org-time-stamp-custom-formats'.
2191 To turn this on on a per-file basis, insert anywhere in the file:
2192 #+STARTUP: customtime"
2193 :group 'org-time
2194 :set 'set-default
2195 :type 'sexp)
2196 (make-variable-buffer-local 'org-display-custom-times)
2198 (defcustom org-time-stamp-custom-formats
2199 '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
2200 "Custom formats for time stamps. See `format-time-string' for the syntax.
2201 These are overlayed over the default ISO format if the variable
2202 `org-display-custom-times' is set. Time like %H:%M should be at the
2203 end of the second format. The custom formats are also honored by export
2204 commands, if custom time display is turned on at the time of export."
2205 :group 'org-time
2206 :type 'sexp)
2208 (defun org-time-stamp-format (&optional long inactive)
2209 "Get the right format for a time string."
2210 (let ((f (if long (cdr org-time-stamp-formats)
2211 (car org-time-stamp-formats))))
2212 (if inactive
2213 (concat "[" (substring f 1 -1) "]")
2214 f)))
2216 (defcustom org-time-clocksum-format "%d:%02d"
2217 "The format string used when creating CLOCKSUM lines, or when
2218 org-mode generates a time duration."
2219 :group 'org-time
2220 :type 'string)
2222 (defcustom org-time-clocksum-use-fractional nil
2223 "If non-nil, \\[org-clock-display] uses fractional times.
2224 org-mode generates a time duration."
2225 :group 'org-time
2226 :type 'boolean)
2228 (defcustom org-time-clocksum-fractional-format "%.2f"
2229 "The format string used when creating CLOCKSUM lines, or when
2230 org-mode generates a time duration."
2231 :group 'org-time
2232 :type 'string)
2234 (defcustom org-deadline-warning-days 14
2235 "No. of days before expiration during which a deadline becomes active.
2236 This variable governs the display in sparse trees and in the agenda.
2237 When 0 or negative, it means use this number (the absolute value of it)
2238 even if a deadline has a different individual lead time specified.
2240 Custom commands can set this variable in the options section."
2241 :group 'org-time
2242 :group 'org-agenda-daily/weekly
2243 :type 'integer)
2245 (defcustom org-read-date-prefer-future t
2246 "Non-nil means, assume future for incomplete date input from user.
2247 This affects the following situations:
2248 1. The user gives a month but not a year.
2249 For example, if it is april and you enter \"feb 2\", this will be read
2250 as feb 2, *next* year. \"May 5\", however, will be this year.
2251 2. The user gives a day, but no month.
2252 For example, if today is the 15th, and you enter \"3\", Org-mode will
2253 read this as the third of *next* month. However, if you enter \"17\",
2254 it will be considered as *this* month.
2256 If you set this variable to the symbol `time', then also the following
2257 will work:
2259 3. If the user gives a time, but no day. If the time is before now,
2260 to will be interpreted as tomorrow.
2262 Currently none of this works for ISO week specifications.
2264 When this option is nil, the current day, month and year will always be
2265 used as defaults."
2266 :group 'org-time
2267 :type '(choice
2268 (const :tag "Never" nil)
2269 (const :tag "Check month and day" t)
2270 (const :tag "Check month, day, and time" time)))
2272 (defcustom org-read-date-display-live t
2273 "Non-nil means, display current interpretation of date prompt live.
2274 This display will be in an overlay, in the minibuffer."
2275 :group 'org-time
2276 :type 'boolean)
2278 (defcustom org-read-date-popup-calendar t
2279 "Non-nil means, pop up a calendar when prompting for a date.
2280 In the calendar, the date can be selected with mouse-1. However, the
2281 minibuffer will also be active, and you can simply enter the date as well.
2282 When nil, only the minibuffer will be available."
2283 :group 'org-time
2284 :type 'boolean)
2285 (if (fboundp 'defvaralias)
2286 (defvaralias 'org-popup-calendar-for-date-prompt
2287 'org-read-date-popup-calendar))
2289 (defcustom org-read-date-minibuffer-setup-hook nil
2290 "Hook to be used to set up keys for the date/time interface.
2291 Add key definitions to `minibuffer-local-map', which will be a temporary
2292 copy."
2293 :group 'org-time
2294 :type 'hook)
2296 (defcustom org-extend-today-until 0
2297 "The hour when your day really ends. Must be an integer.
2298 This has influence for the following applications:
2299 - When switching the agenda to \"today\". It it is still earlier than
2300 the time given here, the day recognized as TODAY is actually yesterday.
2301 - When a date is read from the user and it is still before the time given
2302 here, the current date and time will be assumed to be yesterday, 23:59.
2303 Also, timestamps inserted in remember templates follow this rule.
2305 IMPORTANT: This is a feature whose implementation is and likely will
2306 remain incomplete. Really, it is only here because past midnight seems to
2307 be the favorite working time of John Wiegley :-)"
2308 :group 'org-time
2309 :type 'integer)
2311 (defcustom org-edit-timestamp-down-means-later nil
2312 "Non-nil means, S-down will increase the time in a time stamp.
2313 When nil, S-up will increase."
2314 :group 'org-time
2315 :type 'boolean)
2317 (defcustom org-calendar-follow-timestamp-change t
2318 "Non-nil means, make the calendar window follow timestamp changes.
2319 When a timestamp is modified and the calendar window is visible, it will be
2320 moved to the new date."
2321 :group 'org-time
2322 :type 'boolean)
2324 (defgroup org-tags nil
2325 "Options concerning tags in Org-mode."
2326 :tag "Org Tags"
2327 :group 'org)
2329 (defcustom org-tag-alist nil
2330 "List of tags allowed in Org-mode files.
2331 When this list is nil, Org-mode will base TAG input on what is already in the
2332 buffer.
2333 The value of this variable is an alist, the car of each entry must be a
2334 keyword as a string, the cdr may be a character that is used to select
2335 that tag through the fast-tag-selection interface.
2336 See the manual for details."
2337 :group 'org-tags
2338 :type '(repeat
2339 (choice
2340 (cons (string :tag "Tag name")
2341 (character :tag "Access char"))
2342 (list :tag "Start radio group"
2343 (const :startgroup)
2344 (option (string :tag "Group description")))
2345 (list :tag "End radio group"
2346 (const :endgroup)
2347 (option (string :tag "Group description")))
2348 (const :tag "New line" (:newline)))))
2350 (defcustom org-tag-persistent-alist nil
2351 "List of tags that will always appear in all Org-mode files.
2352 This is in addition to any in buffer settings or customizations
2353 of `org-tag-alist'.
2354 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2355 The value of this variable is an alist, the car of each entry must be a
2356 keyword as a string, the cdr may be a character that is used to select
2357 that tag through the fast-tag-selection interface.
2358 See the manual for details.
2359 To disable these tags on a per-file basis, insert anywhere in the file:
2360 #+STARTUP: noptag"
2361 :group 'org-tags
2362 :type '(repeat
2363 (choice
2364 (cons (string :tag "Tag name")
2365 (character :tag "Access char"))
2366 (const :tag "Start radio group" (:startgroup))
2367 (const :tag "End radio group" (:endgroup))
2368 (const :tag "New line" (:newline)))))
2370 (defvar org-file-tags nil
2371 "List of tags that can be inherited by all entries in the file.
2372 The tags will be inherited if the variable `org-use-tag-inheritance'
2373 says they should be.
2374 This variable is populated from #+FILETAGS lines.")
2376 (defcustom org-use-fast-tag-selection 'auto
2377 "Non-nil means, use fast tag selection scheme.
2378 This is a special interface to select and deselect tags with single keys.
2379 When nil, fast selection is never used.
2380 When the symbol `auto', fast selection is used if and only if selection
2381 characters for tags have been configured, either through the variable
2382 `org-tag-alist' or through a #+TAGS line in the buffer.
2383 When t, fast selection is always used and selection keys are assigned
2384 automatically if necessary."
2385 :group 'org-tags
2386 :type '(choice
2387 (const :tag "Always" t)
2388 (const :tag "Never" nil)
2389 (const :tag "When selection characters are configured" 'auto)))
2391 (defcustom org-fast-tag-selection-single-key nil
2392 "Non-nil means, fast tag selection exits after first change.
2393 When nil, you have to press RET to exit it.
2394 During fast tag selection, you can toggle this flag with `C-c'.
2395 This variable can also have the value `expert'. In this case, the window
2396 displaying the tags menu is not even shown, until you press C-c again."
2397 :group 'org-tags
2398 :type '(choice
2399 (const :tag "No" nil)
2400 (const :tag "Yes" t)
2401 (const :tag "Expert" expert)))
2403 (defvar org-fast-tag-selection-include-todo nil
2404 "Non-nil means, fast tags selection interface will also offer TODO states.
2405 This is an undocumented feature, you should not rely on it.")
2407 (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
2408 "The column to which tags should be indented in a headline.
2409 If this number is positive, it specifies the column. If it is negative,
2410 it means that the tags should be flushright to that column. For example,
2411 -80 works well for a normal 80 character screen."
2412 :group 'org-tags
2413 :type 'integer)
2415 (defcustom org-auto-align-tags t
2416 "Non-nil means, realign tags after pro/demotion of TODO state change.
2417 These operations change the length of a headline and therefore shift
2418 the tags around. With this options turned on, after each such operation
2419 the tags are again aligned to `org-tags-column'."
2420 :group 'org-tags
2421 :type 'boolean)
2423 (defcustom org-use-tag-inheritance t
2424 "Non-nil means, tags in levels apply also for sublevels.
2425 When nil, only the tags directly given in a specific line apply there.
2426 This may also be a list of tags that should be inherited, or a regexp that
2427 matches tags that should be inherited. Additional control is possible
2428 with the variable `org-tags-exclude-from-inheritance' which gives an
2429 explicit list of tags to be excluded from inheritance., even if the value of
2430 `org-use-tag-inheritance' would select it for inheritance.
2432 If this option is t, a match early-on in a tree can lead to a large
2433 number of matches in the subtree when constructing the agenda or creating
2434 a sparse tree. If you only want to see the first match in a tree during
2435 a search, check out the variable `org-tags-match-list-sublevels'."
2436 :group 'org-tags
2437 :type '(choice
2438 (const :tag "Not" nil)
2439 (const :tag "Always" t)
2440 (repeat :tag "Specific tags" (string :tag "Tag"))
2441 (regexp :tag "Tags matched by regexp")))
2443 (defcustom org-tags-exclude-from-inheritance nil
2444 "List of tags that should never be inherited.
2445 This is a way to exclude a few tags from inheritance. For way to do
2446 the opposite, to actively allow inheritance for selected tags,
2447 see the variable `org-use-tag-inheritance'."
2448 :group 'org-tags
2449 :type '(repeat (string :tag "Tag")))
2451 (defun org-tag-inherit-p (tag)
2452 "Check if TAG is one that should be inherited."
2453 (cond
2454 ((member tag org-tags-exclude-from-inheritance) nil)
2455 ((eq org-use-tag-inheritance t) t)
2456 ((not org-use-tag-inheritance) nil)
2457 ((stringp org-use-tag-inheritance)
2458 (string-match org-use-tag-inheritance tag))
2459 ((listp org-use-tag-inheritance)
2460 (member tag org-use-tag-inheritance))
2461 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
2463 (defcustom org-tags-match-list-sublevels t
2464 "Non-nil means list also sublevels of headlines matching a search.
2465 This variable applies to tags/property searches, and also to stuck
2466 projects because this search is based on a tags match as well.
2468 When set to the symbol `indented', sublevels are indented with
2469 leading dots.
2471 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2472 the sublevels of a headline matching a tag search often also match
2473 the same search. Listing all of them can create very long lists.
2474 Setting this variable to nil causes subtrees of a match to be skipped.
2476 This variable is semi-obsolete and probably should always be true. It
2477 is better to limit inheritance to certain tags using the variables
2478 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
2479 :group 'org-tags
2480 :type '(choice
2481 (const :tag "No, don't list them" nil)
2482 (const :tag "Yes, do list them" t)
2483 (const :tag "List them, indented with leading dots" indented)))
2485 (defcustom org-tags-sort-function nil
2486 "When set, tags are sorted using this function as a comparator"
2487 :group 'org-tags
2488 :type '(choice
2489 (const :tag "No sorting" nil)
2490 (const :tag "Alphabetical" string<)
2491 (const :tag "Reverse alphabetical" string>)
2492 (function :tag "Custom function" nil)))
2494 (defvar org-tags-history nil
2495 "History of minibuffer reads for tags.")
2496 (defvar org-last-tags-completion-table nil
2497 "The last used completion table for tags.")
2498 (defvar org-after-tags-change-hook nil
2499 "Hook that is run after the tags in a line have changed.")
2501 (defgroup org-properties nil
2502 "Options concerning properties in Org-mode."
2503 :tag "Org Properties"
2504 :group 'org)
2506 (defcustom org-property-format "%-10s %s"
2507 "How property key/value pairs should be formatted by `indent-line'.
2508 When `indent-line' hits a property definition, it will format the line
2509 according to this format, mainly to make sure that the values are
2510 lined-up with respect to each other."
2511 :group 'org-properties
2512 :type 'string)
2514 (defcustom org-use-property-inheritance nil
2515 "Non-nil means, properties apply also for sublevels.
2517 This setting is chiefly used during property searches. Turning it on can
2518 cause significant overhead when doing a search, which is why it is not
2519 on by default.
2521 When nil, only the properties directly given in the current entry count.
2522 When t, every property is inherited. The value may also be a list of
2523 properties that should have inheritance, or a regular expression matching
2524 properties that should be inherited.
2526 However, note that some special properties use inheritance under special
2527 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2528 and the properties ending in \"_ALL\" when they are used as descriptor
2529 for valid values of a property.
2531 Note for programmers:
2532 When querying an entry with `org-entry-get', you can control if inheritance
2533 should be used. By default, `org-entry-get' looks only at the local
2534 properties. You can request inheritance by setting the inherit argument
2535 to t (to force inheritance) or to `selective' (to respect the setting
2536 in this variable)."
2537 :group 'org-properties
2538 :type '(choice
2539 (const :tag "Not" nil)
2540 (const :tag "Always" t)
2541 (repeat :tag "Specific properties" (string :tag "Property"))
2542 (regexp :tag "Properties matched by regexp")))
2544 (defun org-property-inherit-p (property)
2545 "Check if PROPERTY is one that should be inherited."
2546 (cond
2547 ((eq org-use-property-inheritance t) t)
2548 ((not org-use-property-inheritance) nil)
2549 ((stringp org-use-property-inheritance)
2550 (string-match org-use-property-inheritance property))
2551 ((listp org-use-property-inheritance)
2552 (member property org-use-property-inheritance))
2553 (t (error "Invalid setting of `org-use-property-inheritance'"))))
2555 (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
2556 "The default column format, if no other format has been defined.
2557 This variable can be set on the per-file basis by inserting a line
2559 #+COLUMNS: %25ITEM ....."
2560 :group 'org-properties
2561 :type 'string)
2563 (defcustom org-columns-ellipses ".."
2564 "The ellipses to be used when a field in column view is truncated.
2565 When this is the empty string, as many characters as possible are shown,
2566 but then there will be no visual indication that the field has been truncated.
2567 When this is a string of length N, the last N characters of a truncated
2568 field are replaced by this string. If the column is narrower than the
2569 ellipses string, only part of the ellipses string will be shown."
2570 :group 'org-properties
2571 :type 'string)
2573 (defcustom org-columns-modify-value-for-display-function nil
2574 "Function that modifies values for display in column view.
2575 For example, it can be used to cut out a certain part from a time stamp.
2576 The function must take 2 arguments:
2578 column-title The title of the column (*not* the property name)
2579 value The value that should be modified.
2581 The function should return the value that should be displayed,
2582 or nil if the normal value should be used."
2583 :group 'org-properties
2584 :type 'function)
2586 (defcustom org-effort-property "Effort"
2587 "The property that is being used to keep track of effort estimates.
2588 Effort estimates given in this property need to have the format H:MM."
2589 :group 'org-properties
2590 :group 'org-progress
2591 :type '(string :tag "Property"))
2593 (defconst org-global-properties-fixed
2594 '(("VISIBILITY_ALL" . "folded children content all")
2595 ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
2596 "List of property/value pairs that can be inherited by any entry.
2598 These are fixed values, for the preset properties. The user variable
2599 that can be used to add to this list is `org-global-properties'.
2601 The entries in this list are cons cells where the car is a property
2602 name and cdr is a string with the value. If the value represents
2603 multiple items like an \"_ALL\" property, separate the items by
2604 spaces.")
2606 (defcustom org-global-properties nil
2607 "List of property/value pairs that can be inherited by any entry.
2609 This list will be combined with the constant `org-global-properties-fixed'.
2611 The entries in this list are cons cells where the car is a property
2612 name and cdr is a string with the value.
2614 You can set buffer-local values for the same purpose in the variable
2615 `org-file-properties' this by adding lines like
2617 #+PROPERTY: NAME VALUE"
2618 :group 'org-properties
2619 :type '(repeat
2620 (cons (string :tag "Property")
2621 (string :tag "Value"))))
2623 (defvar org-file-properties nil
2624 "List of property/value pairs that can be inherited by any entry.
2625 Valid for the current buffer.
2626 This variable is populated from #+PROPERTY lines.")
2627 (make-variable-buffer-local 'org-file-properties)
2629 (defgroup org-agenda nil
2630 "Options concerning agenda views in Org-mode."
2631 :tag "Org Agenda"
2632 :group 'org)
2634 (defvar org-category nil
2635 "Variable used by org files to set a category for agenda display.
2636 Such files should use a file variable to set it, for example
2638 # -*- mode: org; org-category: \"ELisp\"
2640 or contain a special line
2642 #+CATEGORY: ELisp
2644 If the file does not specify a category, then file's base name
2645 is used instead.")
2646 (make-variable-buffer-local 'org-category)
2647 (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
2649 (defcustom org-agenda-files nil
2650 "The files to be used for agenda display.
2651 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2652 \\[org-remove-file]. You can also use customize to edit the list.
2654 If an entry is a directory, all files in that directory that are matched by
2655 `org-agenda-file-regexp' will be part of the file list.
2657 If the value of the variable is not a list but a single file name, then
2658 the list of agenda files is actually stored and maintained in that file, one
2659 agenda file per line."
2660 :group 'org-agenda
2661 :type '(choice
2662 (repeat :tag "List of files and directories" file)
2663 (file :tag "Store list in a file\n" :value "~/.agenda_files")))
2665 (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
2666 "Regular expression to match files for `org-agenda-files'.
2667 If any element in the list in that variable contains a directory instead
2668 of a normal file, all files in that directory that are matched by this
2669 regular expression will be included."
2670 :group 'org-agenda
2671 :type 'regexp)
2673 (defcustom org-agenda-text-search-extra-files nil
2674 "List of extra files to be searched by text search commands.
2675 These files will be search in addition to the agenda files by the
2676 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
2677 Note that these files will only be searched for text search commands,
2678 not for the other agenda views like todo lists, tag searches or the weekly
2679 agenda. This variable is intended to list notes and possibly archive files
2680 that should also be searched by these two commands.
2681 In fact, if the first element in the list is the symbol `agenda-archives',
2682 than all archive files of all agenda files will be added to the search
2683 scope."
2684 :group 'org-agenda
2685 :type '(set :greedy t
2686 (const :tag "Agenda Archives" agenda-archives)
2687 (repeat :inline t (file))))
2689 (if (fboundp 'defvaralias)
2690 (defvaralias 'org-agenda-multi-occur-extra-files
2691 'org-agenda-text-search-extra-files))
2693 (defcustom org-agenda-skip-unavailable-files nil
2694 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
2695 A nil value means to remove them, after a query, from the list."
2696 :group 'org-agenda
2697 :type 'boolean)
2699 (defcustom org-calendar-to-agenda-key [?c]
2700 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2701 The command `org-calendar-goto-agenda' will be bound to this key. The
2702 default is the character `c' because then `c' can be used to switch back and
2703 forth between agenda and calendar."
2704 :group 'org-agenda
2705 :type 'sexp)
2707 (defcustom org-calendar-agenda-action-key [?k]
2708 "The key to be installed in `calendar-mode-map' for agenda-action.
2709 The command `org-agenda-action' will be bound to this key. The
2710 default is the character `k' because we use the same key in the agenda."
2711 :group 'org-agenda
2712 :type 'sexp)
2714 (eval-after-load "calendar"
2715 '(progn
2716 (org-defkey calendar-mode-map org-calendar-to-agenda-key
2717 'org-calendar-goto-agenda)
2718 (org-defkey calendar-mode-map org-calendar-agenda-action-key
2719 'org-agenda-action)))
2721 (defgroup org-latex nil
2722 "Options for embedding LaTeX code into Org-mode."
2723 :tag "Org LaTeX"
2724 :group 'org)
2726 (defcustom org-format-latex-options
2727 '(:foreground default :background default :scale 1.0
2728 :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
2729 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
2730 "Options for creating images from LaTeX fragments.
2731 This is a property list with the following properties:
2732 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
2733 `default' means use the foreground of the default face.
2734 :background the background color, or \"Transparent\".
2735 `default' means use the background of the default face.
2736 :scale a scaling factor for the size of the images.
2737 :html-foreground, :html-background, :html-scale
2738 the same numbers for HTML export.
2739 :matchers a list indicating which matchers should be used to
2740 find LaTeX fragments. Valid members of this list are:
2741 \"begin\" find environments
2742 \"$1\" find single characters surrounded by $.$
2743 \"$\" find math expressions surrounded by $...$
2744 \"$$\" find math expressions surrounded by $$....$$
2745 \"\\(\" find math expressions surrounded by \\(...\\)
2746 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2747 :group 'org-latex
2748 :type 'plist)
2750 (defcustom org-format-latex-header "\\documentclass{article}
2751 \\usepackage{amssymb}
2752 \\usepackage[usenames]{color}
2753 \\usepackage{amsmath}
2754 \\usepackage{latexsym}
2755 \\usepackage[mathscr]{eucal}
2756 \\pagestyle{empty} % do not remove
2757 % The settings below are copied from fullpage.sty
2758 \\setlength{\\textwidth}{\\paperwidth}
2759 \\addtolength{\\textwidth}{-3cm}
2760 \\setlength{\\oddsidemargin}{1.5cm}
2761 \\addtolength{\\oddsidemargin}{-2.54cm}
2762 \\setlength{\\evensidemargin}{\\oddsidemargin}
2763 \\setlength{\\textheight}{\\paperheight}
2764 \\addtolength{\\textheight}{-\\headheight}
2765 \\addtolength{\\textheight}{-\\headsep}
2766 \\addtolength{\\textheight}{-\\footskip}
2767 \\addtolength{\\textheight}{-3cm}
2768 \\setlength{\\topmargin}{1.5cm}
2769 \\addtolength{\\topmargin}{-2.54cm}"
2770 "The document header used for processing LaTeX fragments.
2771 It is imperative that this header make sure that no page number
2772 appears on the page."
2773 :group 'org-latex
2774 :type 'string)
2777 (defgroup org-font-lock nil
2778 "Font-lock settings for highlighting in Org-mode."
2779 :tag "Org Font Lock"
2780 :group 'org)
2782 (defcustom org-level-color-stars-only nil
2783 "Non-nil means fontify only the stars in each headline.
2784 When nil, the entire headline is fontified.
2785 Changing it requires restart of `font-lock-mode' to become effective
2786 also in regions already fontified."
2787 :group 'org-font-lock
2788 :type 'boolean)
2790 (defcustom org-hide-leading-stars nil
2791 "Non-nil means, hide the first N-1 stars in a headline.
2792 This works by using the face `org-hide' for these stars. This
2793 face is white for a light background, and black for a dark
2794 background. You may have to customize the face `org-hide' to
2795 make this work.
2796 Changing it requires restart of `font-lock-mode' to become effective
2797 also in regions already fontified.
2798 You may also set this on a per-file basis by adding one of the following
2799 lines to the buffer:
2801 #+STARTUP: hidestars
2802 #+STARTUP: showstars"
2803 :group 'org-font-lock
2804 :type 'boolean)
2806 (defcustom org-fontify-done-headline nil
2807 "Non-nil means, change the face of a headline if it is marked DONE.
2808 Normally, only the TODO/DONE keyword indicates the state of a headline.
2809 When this is non-nil, the headline after the keyword is set to the
2810 `org-headline-done' as an additional indication."
2811 :group 'org-font-lock
2812 :type 'boolean)
2814 (defcustom org-fontify-emphasized-text t
2815 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
2816 Changing this variable requires a restart of Emacs to take effect."
2817 :group 'org-font-lock
2818 :type 'boolean)
2820 (defcustom org-fontify-whole-heading-line nil
2821 "Non-nil means fontify the whole line for headings.
2822 This is useful when setting a background color for the
2823 org-leve-* faces."
2824 :group 'org-font-lock
2825 :type 'boolean)
2827 (defcustom org-highlight-latex-fragments-and-specials nil
2828 "Non-nil means, fontify what is treated specially by the exporters."
2829 :group 'org-font-lock
2830 :type 'boolean)
2832 (defcustom org-hide-emphasis-markers nil
2833 "Non-nil mean font-lock should hide the emphasis marker characters."
2834 :group 'org-font-lock
2835 :type 'boolean)
2837 (defvar org-emph-re nil
2838 "Regular expression for matching emphasis.")
2839 (defvar org-verbatim-re nil
2840 "Regular expression for matching verbatim text.")
2841 (defvar org-emphasis-regexp-components) ; defined just below
2842 (defvar org-emphasis-alist) ; defined just below
2843 (defun org-set-emph-re (var val)
2844 "Set variable and compute the emphasis regular expression."
2845 (set var val)
2846 (when (and (boundp 'org-emphasis-alist)
2847 (boundp 'org-emphasis-regexp-components)
2848 org-emphasis-alist org-emphasis-regexp-components)
2849 (let* ((e org-emphasis-regexp-components)
2850 (pre (car e))
2851 (post (nth 1 e))
2852 (border (nth 2 e))
2853 (body (nth 3 e))
2854 (nl (nth 4 e))
2855 (body1 (concat body "*?"))
2856 (markers (mapconcat 'car org-emphasis-alist ""))
2857 (vmarkers (mapconcat
2858 (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
2859 org-emphasis-alist "")))
2860 ;; make sure special characters appear at the right position in the class
2861 (if (string-match "\\^" markers)
2862 (setq markers (concat (replace-match "" t t markers) "^")))
2863 (if (string-match "-" markers)
2864 (setq markers (concat (replace-match "" t t markers) "-")))
2865 (if (string-match "\\^" vmarkers)
2866 (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
2867 (if (string-match "-" vmarkers)
2868 (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
2869 (if (> nl 0)
2870 (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
2871 (int-to-string nl) "\\}")))
2872 ;; Make the regexp
2873 (setq org-emph-re
2874 (concat "\\([" pre "]\\|^\\)"
2875 "\\("
2876 "\\([" markers "]\\)"
2877 "\\("
2878 "[^" border "]\\|"
2879 "[^" border "]"
2880 body1
2881 "[^" border "]"
2882 "\\)"
2883 "\\3\\)"
2884 "\\([" post "]\\|$\\)"))
2885 (setq org-verbatim-re
2886 (concat "\\([" pre "]\\|^\\)"
2887 "\\("
2888 "\\([" vmarkers "]\\)"
2889 "\\("
2890 "[^" border "]\\|"
2891 "[^" border "]"
2892 body1
2893 "[^" border "]"
2894 "\\)"
2895 "\\3\\)"
2896 "\\([" post "]\\|$\\)")))))
2898 (defcustom org-emphasis-regexp-components
2899 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
2900 "Components used to build the regular expression for emphasis.
2901 This is a list with 6 entries. Terminology: In an emphasis string
2902 like \" *strong word* \", we call the initial space PREMATCH, the final
2903 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
2904 and \"trong wor\" is the body. The different components in this variable
2905 specify what is allowed/forbidden in each part:
2907 pre Chars allowed as prematch. Beginning of line will be allowed too.
2908 post Chars allowed as postmatch. End of line will be allowed too.
2909 border The chars *forbidden* as border characters.
2910 body-regexp A regexp like \".\" to match a body character. Don't use
2911 non-shy groups here, and don't allow newline here.
2912 newline The maximum number of newlines allowed in an emphasis exp.
2914 Use customize to modify this, or restart Emacs after changing it."
2915 :group 'org-font-lock
2916 :set 'org-set-emph-re
2917 :type '(list
2918 (sexp :tag "Allowed chars in pre ")
2919 (sexp :tag "Allowed chars in post ")
2920 (sexp :tag "Forbidden chars in border ")
2921 (sexp :tag "Regexp for body ")
2922 (integer :tag "number of newlines allowed")
2923 (option (boolean :tag "Please ignore this button"))))
2925 (defcustom org-emphasis-alist
2926 `(("*" bold "<b>" "</b>")
2927 ("/" italic "<i>" "</i>")
2928 ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
2929 ("=" org-code "<code>" "</code>" verbatim)
2930 ("~" org-verbatim "<code>" "</code>" verbatim)
2931 ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
2932 "<del>" "</del>")
2934 "Special syntax for emphasized text.
2935 Text starting and ending with a special character will be emphasized, for
2936 example *bold*, _underlined_ and /italic/. This variable sets the marker
2937 characters, the face to be used by font-lock for highlighting in Org-mode
2938 Emacs buffers, and the HTML tags to be used for this.
2939 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
2940 Use customize to modify this, or restart Emacs after changing it."
2941 :group 'org-font-lock
2942 :set 'org-set-emph-re
2943 :type '(repeat
2944 (list
2945 (string :tag "Marker character")
2946 (choice
2947 (face :tag "Font-lock-face")
2948 (plist :tag "Face property list"))
2949 (string :tag "HTML start tag")
2950 (string :tag "HTML end tag")
2951 (option (const verbatim)))))
2953 (defvar org-protecting-blocks
2954 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
2955 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
2956 This is needed for font-lock setup.")
2958 ;;; Miscellaneous options
2960 (defgroup org-completion nil
2961 "Completion in Org-mode."
2962 :tag "Org Completion"
2963 :group 'org)
2965 (defcustom org-completion-use-ido nil
2966 "Non-nil means, use ido completion wherever possible.
2967 Note that `ido-mode' must be active for this variable to be relevant.
2968 If you decide to turn this variable on, you might well want to turn off
2969 `org-outline-path-complete-in-steps'.
2970 See also `org-completion-use-iswitchb'."
2971 :group 'org-completion
2972 :type 'boolean)
2974 (defcustom org-completion-use-iswitchb nil
2975 "Non-nil means, use iswitchb completion wherever possible.
2976 Note that `iswitchb-mode' must be active for this variable to be relevant.
2977 If you decide to turn this variable on, you might well want to turn off
2978 `org-outline-path-complete-in-steps'.
2979 Note that thi variable has only an effect if `org-completion-use-ido' is nil."
2980 :group 'org-completion
2981 :type 'boolean)
2983 (defcustom org-completion-fallback-command 'hippie-expand
2984 "The expansion command called by \\[org-complete] in normal context.
2985 Normal means, no org-mode-specific context."
2986 :group 'org-completion
2987 :type 'function)
2989 ;;; Functions and variables from ther packages
2990 ;; Declared here to avoid compiler warnings
2992 ;; XEmacs only
2993 (defvar outline-mode-menu-heading)
2994 (defvar outline-mode-menu-show)
2995 (defvar outline-mode-menu-hide)
2996 (defvar zmacs-regions) ; XEmacs regions
2998 ;; Emacs only
2999 (defvar mark-active)
3001 ;; Various packages
3002 (declare-function calendar-absolute-from-iso "cal-iso" (date))
3003 (declare-function calendar-forward-day "cal-move" (arg))
3004 (declare-function calendar-goto-date "cal-move" (date))
3005 (declare-function calendar-goto-today "cal-move" ())
3006 (declare-function calendar-iso-from-absolute "cal-iso" (date))
3007 (defvar calc-embedded-close-formula)
3008 (defvar calc-embedded-open-formula)
3009 (declare-function cdlatex-tab "ext:cdlatex" ())
3010 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
3011 (defvar font-lock-unfontify-region-function)
3012 (declare-function iswitchb-read-buffer "iswitchb"
3013 (prompt &optional default require-match start matches-set))
3014 (defvar iswitchb-temp-buflist)
3015 (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
3016 (defvar org-agenda-tags-todo-honor-ignore-options)
3017 (declare-function org-agenda-skip "org-agenda" ())
3018 (declare-function org-format-agenda-item "org-agenda"
3019 (extra txt &optional category tags dotime noprefix remove-re))
3020 (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
3021 (declare-function org-agenda-change-all-lines "org-agenda"
3022 (newhead hdmarker &optional fixface just-this))
3023 (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
3024 (declare-function org-agenda-maybe-redo "org-agenda" ())
3025 (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
3026 (beg end))
3027 (declare-function org-agenda-copy-local-variable "org-agenda" (var))
3028 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3029 "org-agenda" (&optional end))
3030 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
3031 (declare-function org-indent-mode "org-indent" (&optional arg))
3032 (declare-function parse-time-string "parse-time" (string))
3033 (declare-function org-attach-reveal "org-attach" (&optional if-exists))
3034 (defvar remember-data-file)
3035 (defvar texmathp-why)
3036 (declare-function speedbar-line-directory "speedbar" (&optional depth))
3037 (declare-function table--at-cell-p "table" (position &optional object at-column))
3039 (defvar w3m-current-url)
3040 (defvar w3m-current-title)
3042 (defvar org-latex-regexps)
3044 ;;; Autoload and prepare some org modules
3046 ;; Some table stuff that needs to be defined here, because it is used
3047 ;; by the functions setting up org-mode or checking for table context.
3049 (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
3050 "Detects an org-type or table-type table.")
3051 (defconst org-table-line-regexp "^[ \t]*|"
3052 "Detects an org-type table line.")
3053 (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
3054 "Detects an org-type table line.")
3055 (defconst org-table-hline-regexp "^[ \t]*|-"
3056 "Detects an org-type table hline.")
3057 (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
3058 "Detects a table-type table hline.")
3059 (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
3060 "Searching from within a table (any type) this finds the first line
3061 outside the table.")
3063 ;; Autoload the functions in org-table.el that are needed by functions here.
3065 (eval-and-compile
3066 (org-autoload "org-table"
3067 '(org-table-align org-table-begin org-table-blank-field
3068 org-table-convert org-table-convert-region org-table-copy-down
3069 org-table-copy-region org-table-create
3070 org-table-create-or-convert-from-region
3071 org-table-create-with-table.el org-table-current-dline
3072 org-table-cut-region org-table-delete-column org-table-edit-field
3073 org-table-edit-formulas org-table-end org-table-eval-formula
3074 org-table-export org-table-field-info
3075 org-table-get-stored-formulas org-table-goto-column
3076 org-table-hline-and-move org-table-import org-table-insert-column
3077 org-table-insert-hline org-table-insert-row org-table-iterate
3078 org-table-justify-field-maybe org-table-kill-row
3079 org-table-maybe-eval-formula org-table-maybe-recalculate-line
3080 org-table-move-column org-table-move-column-left
3081 org-table-move-column-right org-table-move-row
3082 org-table-move-row-down org-table-move-row-up
3083 org-table-next-field org-table-next-row org-table-paste-rectangle
3084 org-table-previous-field org-table-recalculate
3085 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
3086 org-table-toggle-coordinate-overlays
3087 org-table-toggle-formula-debugger org-table-wrap-region
3088 orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
3090 (defun org-at-table-p (&optional table-type)
3091 "Return t if the cursor is inside an org-type table.
3092 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3093 (if org-enable-table-editor
3094 (save-excursion
3095 (beginning-of-line 1)
3096 (looking-at (if table-type org-table-any-line-regexp
3097 org-table-line-regexp)))
3098 nil))
3099 (defsubst org-table-p () (org-at-table-p))
3101 (defun org-at-table.el-p ()
3102 "Return t if and only if we are at a table.el table."
3103 (and (org-at-table-p 'any)
3104 (save-excursion
3105 (goto-char (org-table-begin 'any))
3106 (looking-at org-table1-hline-regexp))))
3107 (defun org-table-recognize-table.el ()
3108 "If there is a table.el table nearby, recognize it and move into it."
3109 (if org-table-tab-recognizes-table.el
3110 (if (org-at-table.el-p)
3111 (progn
3112 (beginning-of-line 1)
3113 (if (looking-at org-table-dataline-regexp)
3115 (if (looking-at org-table1-hline-regexp)
3116 (progn
3117 (beginning-of-line 2)
3118 (if (looking-at org-table-any-border-regexp)
3119 (beginning-of-line -1)))))
3120 (if (re-search-forward "|" (org-table-end t) t)
3121 (progn
3122 (require 'table)
3123 (if (table--at-cell-p (point))
3125 (message "recognizing table.el table...")
3126 (table-recognize-table)
3127 (message "recognizing table.el table...done")))
3128 (error "This should not happen..."))
3130 nil)
3131 nil))
3133 (defun org-at-table-hline-p ()
3134 "Return t if the cursor is inside a hline in a table."
3135 (if org-enable-table-editor
3136 (save-excursion
3137 (beginning-of-line 1)
3138 (looking-at org-table-hline-regexp))
3139 nil))
3141 (defvar org-table-clean-did-remove-column nil)
3143 (defun org-table-map-tables (function)
3144 "Apply FUNCTION to the start of all tables in the buffer."
3145 (save-excursion
3146 (save-restriction
3147 (widen)
3148 (goto-char (point-min))
3149 (while (re-search-forward org-table-any-line-regexp nil t)
3150 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
3151 (beginning-of-line 1)
3152 (when (looking-at org-table-line-regexp)
3153 (save-excursion (funcall function))
3154 (or (looking-at org-table-line-regexp)
3155 (forward-char 1)))
3156 (re-search-forward org-table-any-border-regexp nil 1))))
3157 (message "Mapping tables: done"))
3159 ;; Declare and autoload functions from org-exp.el & Co
3161 (declare-function org-default-export-plist "org-exp")
3162 (declare-function org-infile-export-plist "org-exp")
3163 (declare-function org-get-current-options "org-exp")
3164 (eval-and-compile
3165 (org-autoload "org-exp"
3166 '(org-export org-export-visible
3167 org-insert-export-options-template
3168 org-table-clean-before-export))
3169 (org-autoload "org-ascii"
3170 '(org-export-as-ascii org-export-ascii-preprocess
3171 org-export-as-ascii-to-buffer org-replace-region-by-ascii
3172 org-export-region-as-ascii))
3173 (org-autoload "org-html"
3174 '(org-export-as-html-and-open
3175 org-export-as-html-batch org-export-as-html-to-buffer
3176 org-replace-region-by-html org-export-region-as-html
3177 org-export-as-html))
3178 (org-autoload "org-icalendar"
3179 '(org-export-icalendar-this-file
3180 org-export-icalendar-all-agenda-files
3181 org-export-icalendar-combine-agenda-files))
3182 (org-autoload "org-xoxo" '(org-export-as-xoxo)))
3184 ;; Declare and autoload functions from org-agenda.el
3186 (eval-and-compile
3187 (org-autoload "org-agenda"
3188 '(org-agenda org-agenda-list org-search-view
3189 org-todo-list org-tags-view org-agenda-list-stuck-projects
3190 org-diary org-agenda-to-appt
3191 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
3193 ;; Autoload org-remember
3195 (eval-and-compile
3196 (org-autoload "org-remember"
3197 '(org-remember-insinuate org-remember-annotation
3198 org-remember-apply-template org-remember org-remember-handler)))
3200 ;; Autoload org-clock.el
3203 (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
3204 (beg end))
3205 (declare-function org-clock-update-mode-line "org-clock" ())
3206 (declare-function org-resolve-clocks "org-clock"
3207 (&optional also-non-dangling-p prompt last-valid))
3208 (defvar org-clock-start-time)
3209 (defvar org-clock-marker (make-marker)
3210 "Marker recording the last clock-in.")
3211 (defvar org-clock-hd-marker (make-marker)
3212 "Marker recording the last clock-in, but the headline position.")
3213 (defvar org-clock-heading ""
3214 "The heading of the current clock entry.")
3215 (defun org-clock-is-active ()
3216 "Return non-nil if clock is currently running.
3217 The return value is actually the clock marker."
3218 (marker-buffer org-clock-marker))
3220 (eval-and-compile
3221 (org-autoload
3222 "org-clock"
3223 '(org-clock-in org-clock-out org-clock-cancel
3224 org-clock-goto org-clock-sum org-clock-display
3225 org-clock-remove-overlays org-clock-report
3226 org-clocktable-shift org-dblock-write:clocktable
3227 org-get-clocktable org-resolve-clocks)))
3229 (defun org-clock-update-time-maybe ()
3230 "If this is a CLOCK line, update it and return t.
3231 Otherwise, return nil."
3232 (interactive)
3233 (save-excursion
3234 (beginning-of-line 1)
3235 (skip-chars-forward " \t")
3236 (when (looking-at org-clock-string)
3237 (let ((re (concat "[ \t]*" org-clock-string
3238 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
3239 "\\([ \t]*=>.*\\)?\\)?"))
3240 ts te h m s neg)
3241 (cond
3242 ((not (looking-at re))
3243 nil)
3244 ((not (match-end 2))
3245 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
3246 (> org-clock-marker (point))
3247 (<= org-clock-marker (point-at-eol)))
3248 ;; The clock is running here
3249 (setq org-clock-start-time
3250 (apply 'encode-time
3251 (org-parse-time-string (match-string 1))))
3252 (org-clock-update-mode-line)))
3254 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
3255 (end-of-line 1)
3256 (setq ts (match-string 1)
3257 te (match-string 3))
3258 (setq s (- (org-float-time
3259 (apply 'encode-time (org-parse-time-string te)))
3260 (org-float-time
3261 (apply 'encode-time (org-parse-time-string ts))))
3262 neg (< s 0)
3263 s (abs s)
3264 h (floor (/ s 3600))
3265 s (- s (* 3600 h))
3266 m (floor (/ s 60))
3267 s (- s (* 60 s)))
3268 (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
3269 t))))))
3271 (defun org-check-running-clock ()
3272 "Check if the current buffer contains the running clock.
3273 If yes, offer to stop it and to save the buffer with the changes."
3274 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
3275 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
3276 (buffer-name))))
3277 (org-clock-out)
3278 (when (y-or-n-p "Save changed buffer?")
3279 (save-buffer))))
3281 (defun org-clocktable-try-shift (dir n)
3282 "Check if this line starts a clock table, if yes, shift the time block."
3283 (when (org-match-line "#\\+BEGIN: clocktable\\>")
3284 (org-clocktable-shift dir n)))
3286 ;; Autoload org-timer.el
3288 (eval-and-compile
3289 (org-autoload
3290 "org-timer"
3291 '(org-timer-start org-timer org-timer-item
3292 org-timer-change-times-in-region
3293 org-timer-set-timer
3294 org-timer-reset-timers
3295 org-timer-show-remaining-time)))
3297 ;; Autoload org-feed.el
3299 (eval-and-compile
3300 (org-autoload
3301 "org-feed"
3302 '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
3305 ;; Autoload org-indent.el
3307 (eval-and-compile
3308 (org-autoload
3309 "org-indent"
3310 '(org-indent-mode)))
3312 ;; Autoload org-mobile.el
3314 (eval-and-compile
3315 (org-autoload
3316 "org-mobile"
3317 '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
3319 ;; Autoload archiving code
3320 ;; The stuff that is needed for cycling and tags has to be defined here.
3322 (defgroup org-archive nil
3323 "Options concerning archiving in Org-mode."
3324 :tag "Org Archive"
3325 :group 'org-structure)
3327 (defcustom org-archive-location "%s_archive::"
3328 "The location where subtrees should be archived.
3330 The value of this variable is a string, consisting of two parts,
3331 separated by a double-colon. The first part is a filename and
3332 the second part is a headline.
3334 When the filename is omitted, archiving happens in the same file.
3335 %s in the filename will be replaced by the current file
3336 name (without the directory part). Archiving to a different file
3337 is useful to keep archived entries from contributing to the
3338 Org-mode Agenda.
3340 The archived entries will be filed as subtrees of the specified
3341 headline. When the headline is omitted, the subtrees are simply
3342 filed away at the end of the file, as top-level entries. Also in
3343 the heading you can use %s to represent the file name, this can be
3344 useful when using the same archive for a number of different files.
3346 Here are a few examples:
3347 \"%s_archive::\"
3348 If the current file is Projects.org, archive in file
3349 Projects.org_archive, as top-level trees. This is the default.
3351 \"::* Archived Tasks\"
3352 Archive in the current file, under the top-level headline
3353 \"* Archived Tasks\".
3355 \"~/org/archive.org::\"
3356 Archive in file ~/org/archive.org (absolute path), as top-level trees.
3358 \"~/org/archive.org::From %s\"
3359 Archive in file ~/org/archive.org (absolute path), und headlines
3360 \"From FILENAME\" where file name is the current file name.
3362 \"basement::** Finished Tasks\"
3363 Archive in file ./basement (relative path), as level 3 trees
3364 below the level 2 heading \"** Finished Tasks\".
3366 You may set this option on a per-file basis by adding to the buffer a
3367 line like
3369 #+ARCHIVE: basement::** Finished Tasks
3371 You may also define it locally for a subtree by setting an ARCHIVE property
3372 in the entry. If such a property is found in an entry, or anywhere up
3373 the hierarchy, it will be used."
3374 :group 'org-archive
3375 :type 'string)
3377 (defcustom org-archive-tag "ARCHIVE"
3378 "The tag that marks a subtree as archived.
3379 An archived subtree does not open during visibility cycling, and does
3380 not contribute to the agenda listings.
3381 After changing this, font-lock must be restarted in the relevant buffers to
3382 get the proper fontification."
3383 :group 'org-archive
3384 :group 'org-keywords
3385 :type 'string)
3387 (defcustom org-agenda-skip-archived-trees t
3388 "Non-nil means, the agenda will skip any items located in archived trees.
3389 An archived tree is a tree marked with the tag ARCHIVE. The use of this
3390 variable is no longer recommended, you should leave it at the value t.
3391 Instead, use the key `v' to cycle the archives-mode in the agenda."
3392 :group 'org-archive
3393 :group 'org-agenda-skip
3394 :type 'boolean)
3396 (defcustom org-columns-skip-arrchived-trees t
3397 "Non-nil means, irgnore archived trees when creating column view."
3398 :group 'org-archive
3399 :group 'org-properties
3400 :type 'boolean)
3402 (defcustom org-cycle-open-archived-trees nil
3403 "Non-nil means, `org-cycle' will open archived trees.
3404 An archived tree is a tree marked with the tag ARCHIVE.
3405 When nil, archived trees will stay folded. You can still open them with
3406 normal outline commands like `show-all', but not with the cycling commands."
3407 :group 'org-archive
3408 :group 'org-cycle
3409 :type 'boolean)
3411 (defcustom org-sparse-tree-open-archived-trees nil
3412 "Non-nil means sparse tree construction shows matches in archived trees.
3413 When nil, matches in these trees are highlighted, but the trees are kept in
3414 collapsed state."
3415 :group 'org-archive
3416 :group 'org-sparse-trees
3417 :type 'boolean)
3419 (defun org-cycle-hide-archived-subtrees (state)
3420 "Re-hide all archived subtrees after a visibility state change."
3421 (when (and (not org-cycle-open-archived-trees)
3422 (not (memq state '(overview folded))))
3423 (save-excursion
3424 (let* ((globalp (memq state '(contents all)))
3425 (beg (if globalp (point-min) (point)))
3426 (end (if globalp (point-max) (org-end-of-subtree t))))
3427 (org-hide-archived-subtrees beg end)
3428 (goto-char beg)
3429 (if (looking-at (concat ".*:" org-archive-tag ":"))
3430 (message "%s" (substitute-command-keys
3431 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
3433 (defun org-force-cycle-archived ()
3434 "Cycle subtree even if it is archived."
3435 (interactive)
3436 (setq this-command 'org-cycle)
3437 (let ((org-cycle-open-archived-trees t))
3438 (call-interactively 'org-cycle)))
3440 (defun org-hide-archived-subtrees (beg end)
3441 "Re-hide all archived subtrees after a visibility state change."
3442 (save-excursion
3443 (let* ((re (concat ":" org-archive-tag ":")))
3444 (goto-char beg)
3445 (while (re-search-forward re end t)
3446 (and (org-on-heading-p) (org-flag-subtree t))
3447 (org-end-of-subtree t)))))
3449 (defun org-flag-subtree (flag)
3450 (save-excursion
3451 (org-back-to-heading t)
3452 (outline-end-of-heading)
3453 (outline-flag-region (point)
3454 (progn (org-end-of-subtree t) (point))
3455 flag)))
3457 (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
3459 (eval-and-compile
3460 (org-autoload "org-archive"
3461 '(org-add-archive-files org-archive-subtree
3462 org-archive-to-archive-sibling org-toggle-archive-tag)))
3464 ;; Autoload Column View Code
3466 (declare-function org-columns-number-to-string "org-colview")
3467 (declare-function org-columns-get-format-and-top-level "org-colview")
3468 (declare-function org-columns-compute "org-colview")
3470 (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
3471 '(org-columns-number-to-string org-columns-get-format-and-top-level
3472 org-columns-compute org-agenda-columns org-columns-remove-overlays
3473 org-columns org-insert-columns-dblock org-dblock-write:columnview))
3475 ;; Autoload ID code
3477 (declare-function org-id-store-link "org-id")
3478 (declare-function org-id-locations-load "org-id")
3479 (declare-function org-id-locations-save "org-id")
3480 (defvar org-id-track-globally)
3481 (org-autoload "org-id"
3482 '(org-id-get-create org-id-new org-id-copy org-id-get
3483 org-id-get-with-outline-path-completion
3484 org-id-get-with-outline-drilling
3485 org-id-goto org-id-find org-id-store-link))
3487 ;; Autoload Plotting Code
3489 (org-autoload "org-plot"
3490 '(org-plot/gnuplot))
3492 ;;; Variables for pre-computed regular expressions, all buffer local
3494 (defvar org-drawer-regexp nil
3495 "Matches first line of a hidden block.")
3496 (make-variable-buffer-local 'org-drawer-regexp)
3497 (defvar org-todo-regexp nil
3498 "Matches any of the TODO state keywords.")
3499 (make-variable-buffer-local 'org-todo-regexp)
3500 (defvar org-not-done-regexp nil
3501 "Matches any of the TODO state keywords except the last one.")
3502 (make-variable-buffer-local 'org-not-done-regexp)
3503 (defvar org-not-done-heading-regexp nil
3504 "Matches a TODO headline that is not done.")
3505 (make-variable-buffer-local 'org-not-done-regexp)
3506 (defvar org-todo-line-regexp nil
3507 "Matches a headline and puts TODO state into group 2 if present.")
3508 (make-variable-buffer-local 'org-todo-line-regexp)
3509 (defvar org-complex-heading-regexp nil
3510 "Matches a headline and puts everything into groups:
3511 group 1: the stars
3512 group 2: The todo keyword, maybe
3513 group 3: Priority cookie
3514 group 4: True headline
3515 group 5: Tags")
3516 (make-variable-buffer-local 'org-complex-heading-regexp)
3517 (defvar org-complex-heading-regexp-format nil)
3518 (make-variable-buffer-local 'org-complex-heading-regexp-format)
3519 (defvar org-todo-line-tags-regexp nil
3520 "Matches a headline and puts TODO state into group 2 if present.
3521 Also put tags into group 4 if tags are present.")
3522 (make-variable-buffer-local 'org-todo-line-tags-regexp)
3523 (defvar org-nl-done-regexp nil
3524 "Matches newline followed by a headline with the DONE keyword.")
3525 (make-variable-buffer-local 'org-nl-done-regexp)
3526 (defvar org-looking-at-done-regexp nil
3527 "Matches the DONE keyword a point.")
3528 (make-variable-buffer-local 'org-looking-at-done-regexp)
3529 (defvar org-ds-keyword-length 12
3530 "Maximum length of the Deadline and SCHEDULED keywords.")
3531 (make-variable-buffer-local 'org-ds-keyword-length)
3532 (defvar org-deadline-regexp nil
3533 "Matches the DEADLINE keyword.")
3534 (make-variable-buffer-local 'org-deadline-regexp)
3535 (defvar org-deadline-time-regexp nil
3536 "Matches the DEADLINE keyword together with a time stamp.")
3537 (make-variable-buffer-local 'org-deadline-time-regexp)
3538 (defvar org-deadline-line-regexp nil
3539 "Matches the DEADLINE keyword and the rest of the line.")
3540 (make-variable-buffer-local 'org-deadline-line-regexp)
3541 (defvar org-scheduled-regexp nil
3542 "Matches the SCHEDULED keyword.")
3543 (make-variable-buffer-local 'org-scheduled-regexp)
3544 (defvar org-scheduled-time-regexp nil
3545 "Matches the SCHEDULED keyword together with a time stamp.")
3546 (make-variable-buffer-local 'org-scheduled-time-regexp)
3547 (defvar org-closed-time-regexp nil
3548 "Matches the CLOSED keyword together with a time stamp.")
3549 (make-variable-buffer-local 'org-closed-time-regexp)
3551 (defvar org-keyword-time-regexp nil
3552 "Matches any of the 4 keywords, together with the time stamp.")
3553 (make-variable-buffer-local 'org-keyword-time-regexp)
3554 (defvar org-keyword-time-not-clock-regexp nil
3555 "Matches any of the 3 keywords, together with the time stamp.")
3556 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
3557 (defvar org-maybe-keyword-time-regexp nil
3558 "Matches a timestamp, possibly preceeded by a keyword.")
3559 (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
3560 (defvar org-planning-or-clock-line-re nil
3561 "Matches a line with planning or clock info.")
3562 (make-variable-buffer-local 'org-planning-or-clock-line-re)
3564 (defconst org-plain-time-of-day-regexp
3565 (concat
3566 "\\(\\<[012]?[0-9]"
3567 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3568 "\\(--?"
3569 "\\(\\<[012]?[0-9]"
3570 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3571 "\\)?")
3572 "Regular expression to match a plain time or time range.
3573 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3574 groups carry important information:
3575 0 the full match
3576 1 the first time, range or not
3577 8 the second time, if it is a range.")
3579 (defconst org-plain-time-extension-regexp
3580 (concat
3581 "\\(\\<[012]?[0-9]"
3582 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3583 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
3584 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
3585 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3586 groups carry important information:
3587 0 the full match
3588 7 hours of duration
3589 9 minutes of duration")
3591 (defconst org-stamp-time-of-day-regexp
3592 (concat
3593 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
3594 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
3595 "\\(--?"
3596 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
3597 "Regular expression to match a timestamp time or time range.
3598 After a match, the following groups carry important information:
3599 0 the full match
3600 1 date plus weekday, for backreferencing to make sure both times on same day
3601 2 the first time, range or not
3602 4 the second time, if it is a range.")
3604 (defconst org-startup-options
3605 '(("fold" org-startup-folded t)
3606 ("overview" org-startup-folded t)
3607 ("nofold" org-startup-folded nil)
3608 ("showall" org-startup-folded nil)
3609 ("showeverything" org-startup-folded showeverything)
3610 ("content" org-startup-folded content)
3611 ("indent" org-startup-indented t)
3612 ("noindent" org-startup-indented nil)
3613 ("hidestars" org-hide-leading-stars t)
3614 ("showstars" org-hide-leading-stars nil)
3615 ("odd" org-odd-levels-only t)
3616 ("oddeven" org-odd-levels-only nil)
3617 ("align" org-startup-align-all-tables t)
3618 ("noalign" org-startup-align-all-tables nil)
3619 ("customtime" org-display-custom-times t)
3620 ("logdone" org-log-done time)
3621 ("lognotedone" org-log-done note)
3622 ("nologdone" org-log-done nil)
3623 ("lognoteclock-out" org-log-note-clock-out t)
3624 ("nolognoteclock-out" org-log-note-clock-out nil)
3625 ("logrepeat" org-log-repeat state)
3626 ("lognoterepeat" org-log-repeat note)
3627 ("nologrepeat" org-log-repeat nil)
3628 ("fninline" org-footnote-define-inline t)
3629 ("nofninline" org-footnote-define-inline nil)
3630 ("fnlocal" org-footnote-section nil)
3631 ("fnauto" org-footnote-auto-label t)
3632 ("fnprompt" org-footnote-auto-label nil)
3633 ("fnconfirm" org-footnote-auto-label confirm)
3634 ("fnplain" org-footnote-auto-label plain)
3635 ("fnadjust" org-footnote-auto-adjust t)
3636 ("nofnadjust" org-footnote-auto-adjust nil)
3637 ("constcgs" constants-unit-system cgs)
3638 ("constSI" constants-unit-system SI)
3639 ("noptag" org-tag-persistent-alist nil)
3640 ("hideblocks" org-hide-block-startup t)
3641 ("nohideblocks" org-hide-block-startup nil))
3642 "Variable associated with STARTUP options for org-mode.
3643 Each element is a list of three items: The startup options as written
3644 in the #+STARTUP line, the corresponding variable, and the value to
3645 set this variable to if the option is found. An optional forth element PUSH
3646 means to push this value onto the list in the variable.")
3648 (defun org-set-regexps-and-options ()
3649 "Precompute regular expressions for current buffer."
3650 (when (org-mode-p)
3651 (org-set-local 'org-todo-kwd-alist nil)
3652 (org-set-local 'org-todo-key-alist nil)
3653 (org-set-local 'org-todo-key-trigger nil)
3654 (org-set-local 'org-todo-keywords-1 nil)
3655 (org-set-local 'org-done-keywords nil)
3656 (org-set-local 'org-todo-heads nil)
3657 (org-set-local 'org-todo-sets nil)
3658 (org-set-local 'org-todo-log-states nil)
3659 (org-set-local 'org-file-properties nil)
3660 (org-set-local 'org-file-tags nil)
3661 (let ((re (org-make-options-regexp
3662 '("CATEGORY" "TODO" "COLUMNS"
3663 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
3664 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
3665 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
3666 (splitre "[ \t]+")
3667 kwds kws0 kwsa key log value cat arch tags const links hw dws
3668 tail sep kws1 prio props ftags drawers
3669 ext-setup-or-nil setup-contents (start 0))
3670 (save-excursion
3671 (save-restriction
3672 (widen)
3673 (goto-char (point-min))
3674 (while (or (and ext-setup-or-nil
3675 (string-match re ext-setup-or-nil start)
3676 (setq start (match-end 0)))
3677 (and (setq ext-setup-or-nil nil start 0)
3678 (re-search-forward re nil t)))
3679 (setq key (upcase (match-string 1 ext-setup-or-nil))
3680 value (org-match-string-no-properties 2 ext-setup-or-nil))
3681 (cond
3682 ((equal key "CATEGORY")
3683 (if (string-match "[ \t]+$" value)
3684 (setq value (replace-match "" t t value)))
3685 (setq cat value))
3686 ((member key '("SEQ_TODO" "TODO"))
3687 (push (cons 'sequence (org-split-string value splitre)) kwds))
3688 ((equal key "TYP_TODO")
3689 (push (cons 'type (org-split-string value splitre)) kwds))
3690 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
3691 ;; general TODO-like setup
3692 (push (cons (intern (downcase (match-string 1 key)))
3693 (org-split-string value splitre)) kwds))
3694 ((equal key "TAGS")
3695 (setq tags (append tags (if tags '("\\n") nil)
3696 (org-split-string value splitre))))
3697 ((equal key "COLUMNS")
3698 (org-set-local 'org-columns-default-format value))
3699 ((equal key "LINK")
3700 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
3701 (push (cons (match-string 1 value)
3702 (org-trim (match-string 2 value)))
3703 links)))
3704 ((equal key "PRIORITIES")
3705 (setq prio (org-split-string value " +")))
3706 ((equal key "PROPERTY")
3707 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
3708 (push (cons (match-string 1 value) (match-string 2 value))
3709 props)))
3710 ((equal key "FILETAGS")
3711 (when (string-match "\\S-" value)
3712 (setq ftags
3713 (append
3714 ftags
3715 (apply 'append
3716 (mapcar (lambda (x) (org-split-string x ":"))
3717 (org-split-string value)))))))
3718 ((equal key "DRAWERS")
3719 (setq drawers (org-split-string value splitre)))
3720 ((equal key "CONSTANTS")
3721 (setq const (append const (org-split-string value splitre))))
3722 ((equal key "STARTUP")
3723 (let ((opts (org-split-string value splitre))
3724 l var val)
3725 (while (setq l (pop opts))
3726 (when (setq l (assoc l org-startup-options))
3727 (setq var (nth 1 l) val (nth 2 l))
3728 (if (not (nth 3 l))
3729 (set (make-local-variable var) val)
3730 (if (not (listp (symbol-value var)))
3731 (set (make-local-variable var) nil))
3732 (set (make-local-variable var) (symbol-value var))
3733 (add-to-list var val))))))
3734 ((equal key "ARCHIVE")
3735 (string-match " *$" value)
3736 (setq arch (replace-match "" t t value))
3737 (remove-text-properties 0 (length arch)
3738 '(face t fontified t) arch))
3739 ((equal key "SETUPFILE")
3740 (setq setup-contents (org-file-contents
3741 (expand-file-name
3742 (org-remove-double-quotes value))
3743 'noerror))
3744 (if (not ext-setup-or-nil)
3745 (setq ext-setup-or-nil setup-contents start 0)
3746 (setq ext-setup-or-nil
3747 (concat (substring ext-setup-or-nil 0 start)
3748 "\n" setup-contents "\n"
3749 (substring ext-setup-or-nil start)))))
3750 ))))
3751 (when cat
3752 (org-set-local 'org-category (intern cat))
3753 (push (cons "CATEGORY" cat) props))
3754 (when prio
3755 (if (< (length prio) 3) (setq prio '("A" "C" "B")))
3756 (setq prio (mapcar 'string-to-char prio))
3757 (org-set-local 'org-highest-priority (nth 0 prio))
3758 (org-set-local 'org-lowest-priority (nth 1 prio))
3759 (org-set-local 'org-default-priority (nth 2 prio)))
3760 (and props (org-set-local 'org-file-properties (nreverse props)))
3761 (and ftags (org-set-local 'org-file-tags
3762 (mapcar 'org-add-prop-inherited ftags)))
3763 (and drawers (org-set-local 'org-drawers drawers))
3764 (and arch (org-set-local 'org-archive-location arch))
3765 (and links (setq org-link-abbrev-alist-local (nreverse links)))
3766 ;; Process the TODO keywords
3767 (unless kwds
3768 ;; Use the global values as if they had been given locally.
3769 (setq kwds (default-value 'org-todo-keywords))
3770 (if (stringp (car kwds))
3771 (setq kwds (list (cons org-todo-interpretation
3772 (default-value 'org-todo-keywords)))))
3773 (setq kwds (reverse kwds)))
3774 (setq kwds (nreverse kwds))
3775 (let (inter kws kw)
3776 (while (setq kws (pop kwds))
3777 (let ((kws (or
3778 (run-hook-with-args-until-success
3779 'org-todo-setup-filter-hook kws)
3780 kws)))
3781 (setq inter (pop kws) sep (member "|" kws)
3782 kws0 (delete "|" (copy-sequence kws))
3783 kwsa nil
3784 kws1 (mapcar
3785 (lambda (x)
3786 ;; 1 2
3787 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
3788 (progn
3789 (setq kw (match-string 1 x)
3790 key (and (match-end 2) (match-string 2 x))
3791 log (org-extract-log-state-settings x))
3792 (push (cons kw (and key (string-to-char key))) kwsa)
3793 (and log (push log org-todo-log-states))
3795 (error "Invalid TODO keyword %s" x)))
3796 kws0)
3797 kwsa (if kwsa (append '((:startgroup))
3798 (nreverse kwsa)
3799 '((:endgroup))))
3800 hw (car kws1)
3801 dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
3802 tail (list inter hw (car dws) (org-last dws))))
3803 (add-to-list 'org-todo-heads hw 'append)
3804 (push kws1 org-todo-sets)
3805 (setq org-done-keywords (append org-done-keywords dws nil))
3806 (setq org-todo-key-alist (append org-todo-key-alist kwsa))
3807 (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
3808 (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
3809 (setq org-todo-sets (nreverse org-todo-sets)
3810 org-todo-kwd-alist (nreverse org-todo-kwd-alist)
3811 org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
3812 org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
3813 ;; Process the constants
3814 (when const
3815 (let (e cst)
3816 (while (setq e (pop const))
3817 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
3818 (push (cons (match-string 1 e) (match-string 2 e)) cst)))
3819 (setq org-table-formula-constants-local cst)))
3821 ;; Process the tags.
3822 (when tags
3823 (let (e tgs)
3824 (while (setq e (pop tags))
3825 (cond
3826 ((equal e "{") (push '(:startgroup) tgs))
3827 ((equal e "}") (push '(:endgroup) tgs))
3828 ((equal e "\\n") (push '(:newline) tgs))
3829 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
3830 (push (cons (match-string 1 e)
3831 (string-to-char (match-string 2 e)))
3832 tgs))
3833 (t (push (list e) tgs))))
3834 (org-set-local 'org-tag-alist nil)
3835 (while (setq e (pop tgs))
3836 (or (and (stringp (car e))
3837 (assoc (car e) org-tag-alist))
3838 (push e org-tag-alist)))))
3840 ;; Compute the regular expressions and other local variables
3841 (if (not org-done-keywords)
3842 (setq org-done-keywords (and org-todo-keywords-1
3843 (list (org-last org-todo-keywords-1)))))
3844 (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
3845 (length org-scheduled-string)
3846 (length org-clock-string)
3847 (length org-closed-string)))
3848 org-drawer-regexp
3849 (concat "^[ \t]*:\\("
3850 (mapconcat 'regexp-quote org-drawers "\\|")
3851 "\\):[ \t]*$")
3852 org-not-done-keywords
3853 (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
3854 org-todo-regexp
3855 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
3856 "\\|") "\\)\\>")
3857 org-not-done-regexp
3858 (concat "\\<\\("
3859 (mapconcat 'regexp-quote org-not-done-keywords "\\|")
3860 "\\)\\>")
3861 org-not-done-heading-regexp
3862 (concat "^\\(\\*+\\)[ \t]+\\("
3863 (mapconcat 'regexp-quote org-not-done-keywords "\\|")
3864 "\\)\\>")
3865 org-todo-line-regexp
3866 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3867 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
3868 "\\)\\>\\)?[ \t]*\\(.*\\)")
3869 org-complex-heading-regexp
3870 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3871 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
3872 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
3873 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3874 org-complex-heading-regexp-format
3875 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3876 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
3877 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
3878 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3879 org-nl-done-regexp
3880 (concat "\n\\*+[ \t]+"
3881 "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
3882 "\\)" "\\>")
3883 org-todo-line-tags-regexp
3884 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3885 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
3886 (org-re
3887 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
3888 org-looking-at-done-regexp
3889 (concat "^" "\\(?:"
3890 (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
3891 "\\>")
3892 org-deadline-regexp (concat "\\<" org-deadline-string)
3893 org-deadline-time-regexp
3894 (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
3895 org-deadline-line-regexp
3896 (concat "\\<\\(" org-deadline-string "\\).*")
3897 org-scheduled-regexp
3898 (concat "\\<" org-scheduled-string)
3899 org-scheduled-time-regexp
3900 (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
3901 org-closed-time-regexp
3902 (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
3903 org-keyword-time-regexp
3904 (concat "\\<\\(" org-scheduled-string
3905 "\\|" org-deadline-string
3906 "\\|" org-closed-string
3907 "\\|" org-clock-string "\\)"
3908 " *[[<]\\([^]>]+\\)[]>]")
3909 org-keyword-time-not-clock-regexp
3910 (concat "\\<\\(" org-scheduled-string
3911 "\\|" org-deadline-string
3912 "\\|" org-closed-string
3913 "\\)"
3914 " *[[<]\\([^]>]+\\)[]>]")
3915 org-maybe-keyword-time-regexp
3916 (concat "\\(\\<\\(" org-scheduled-string
3917 "\\|" org-deadline-string
3918 "\\|" org-closed-string
3919 "\\|" org-clock-string "\\)\\)?"
3920 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
3921 org-planning-or-clock-line-re
3922 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
3923 "\\|" org-deadline-string
3924 "\\|" org-closed-string "\\|" org-clock-string
3925 "\\)\\>\\)")
3927 (org-compute-latex-and-specials-regexp)
3928 (org-set-font-lock-defaults))))
3930 (defun org-file-contents (file &optional noerror)
3931 "Return the contents of FILE, as a string."
3932 (if (or (not file)
3933 (not (file-readable-p file)))
3934 (if noerror
3935 (progn
3936 (message "Cannot read file %s" file)
3937 (ding) (sit-for 2)
3939 (error "Cannot read file %s" file))
3940 (with-temp-buffer
3941 (insert-file-contents file)
3942 (buffer-string))))
3944 (defun org-extract-log-state-settings (x)
3945 "Extract the log state setting from a TODO keyword string.
3946 This will extract info from a string like \"WAIT(w@/!)\"."
3947 (let (kw key log1 log2)
3948 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
3949 (setq kw (match-string 1 x)
3950 key (and (match-end 2) (match-string 2 x))
3951 log1 (and (match-end 3) (match-string 3 x))
3952 log2 (and (match-end 4) (match-string 4 x)))
3953 (and (or log1 log2)
3954 (list kw
3955 (and log1 (if (equal log1 "!") 'time 'note))
3956 (and log2 (if (equal log2 "!") 'time 'note)))))))
3958 (defun org-remove-keyword-keys (list)
3959 "Remove a pair of parenthesis at the end of each string in LIST."
3960 (mapcar (lambda (x)
3961 (if (string-match "(.*)$" x)
3962 (substring x 0 (match-beginning 0))
3964 list))
3966 ;; FIXME: this could be done much better, using second characters etc.
3967 (defun org-assign-fast-keys (alist)
3968 "Assign fast keys to a keyword-key alist.
3969 Respect keys that are already there."
3970 (let (new e k c c1 c2 (char ?a))
3971 (while (setq e (pop alist))
3972 (cond
3973 ((equal e '(:startgroup)) (push e new))
3974 ((equal e '(:endgroup)) (push e new))
3975 ((equal e '(:newline)) (push e new))
3977 (setq k (car e) c2 nil)
3978 (if (cdr e)
3979 (setq c (cdr e))
3980 ;; automatically assign a character.
3981 (setq c1 (string-to-char
3982 (downcase (substring
3983 k (if (= (string-to-char k) ?@) 1 0)))))
3984 (if (or (rassoc c1 new) (rassoc c1 alist))
3985 (while (or (rassoc char new) (rassoc char alist))
3986 (setq char (1+ char)))
3987 (setq c2 c1))
3988 (setq c (or c2 char)))
3989 (push (cons k c) new))))
3990 (nreverse new)))
3992 ;;; Some variables used in various places
3994 (defvar org-window-configuration nil
3995 "Used in various places to store a window configuration.")
3996 (defvar org-selected-window nil
3997 "Used in various places to store a window configuration.")
3998 (defvar org-finish-function nil
3999 "Function to be called when `C-c C-c' is used.
4000 This is for getting out of special buffers like remember.")
4003 ;; FIXME: Occasionally check by commenting these, to make sure
4004 ;; no other functions uses these, forgetting to let-bind them.
4005 (defvar entry)
4006 (defvar last-state)
4007 (defvar date)
4009 ;; Defined somewhere in this file, but used before definition.
4010 (defvar org-html-entities)
4011 (defvar org-struct-menu)
4012 (defvar org-org-menu)
4013 (defvar org-tbl-menu)
4014 (defvar org-agenda-keymap)
4016 ;;;; Define the Org-mode
4018 (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
4019 (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."))
4022 ;; We use a before-change function to check if a table might need
4023 ;; an update.
4024 (defvar org-table-may-need-update t
4025 "Indicates that a table might need an update.
4026 This variable is set by `org-before-change-function'.
4027 `org-table-align' sets it back to nil.")
4028 (defun org-before-change-function (beg end)
4029 "Every change indicates that a table might need an update."
4030 (setq org-table-may-need-update t))
4031 (defvar org-mode-map)
4032 (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
4033 (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
4034 (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
4035 (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
4036 (defvar org-table-buffer-is-an nil)
4037 (defconst org-outline-regexp "\\*+ ")
4039 ;;;###autoload
4040 (define-derived-mode org-mode outline-mode "Org"
4041 "Outline-based notes management and organizer, alias
4042 \"Carsten's outline-mode for keeping track of everything.\"
4044 Org-mode develops organizational tasks around a NOTES file which
4045 contains information about projects as plain text. Org-mode is
4046 implemented on top of outline-mode, which is ideal to keep the content
4047 of large files well structured. It supports ToDo items, deadlines and
4048 time stamps, which magically appear in the diary listing of the Emacs
4049 calendar. Tables are easily created with a built-in table editor.
4050 Plain text URL-like links connect to websites, emails (VM), Usenet
4051 messages (Gnus), BBDB entries, and any files related to the project.
4052 For printing and sharing of notes, an Org-mode file (or a part of it)
4053 can be exported as a structured ASCII or HTML file.
4055 The following commands are available:
4057 \\{org-mode-map}"
4059 ;; Get rid of Outline menus, they are not needed
4060 ;; Need to do this here because define-derived-mode sets up
4061 ;; the keymap so late. Still, it is a waste to call this each time
4062 ;; we switch another buffer into org-mode.
4063 (if (featurep 'xemacs)
4064 (when (boundp 'outline-mode-menu-heading)
4065 ;; Assume this is Greg's port, it used easymenu
4066 (easy-menu-remove outline-mode-menu-heading)
4067 (easy-menu-remove outline-mode-menu-show)
4068 (easy-menu-remove outline-mode-menu-hide))
4069 (define-key org-mode-map [menu-bar headings] 'undefined)
4070 (define-key org-mode-map [menu-bar hide] 'undefined)
4071 (define-key org-mode-map [menu-bar show] 'undefined))
4073 (org-load-modules-maybe)
4074 (easy-menu-add org-org-menu)
4075 (easy-menu-add org-tbl-menu)
4076 (org-install-agenda-files-menu)
4077 (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
4078 (org-add-to-invisibility-spec '(org-cwidth))
4079 (org-add-to-invisibility-spec '(org-hide-block . t))
4080 (when (featurep 'xemacs)
4081 (org-set-local 'line-move-ignore-invisible t))
4082 (org-set-local 'outline-regexp org-outline-regexp)
4083 (org-set-local 'outline-level 'org-outline-level)
4084 (when (and org-ellipsis
4085 (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
4086 (fboundp 'make-glyph-code))
4087 (unless org-display-table
4088 (setq org-display-table (make-display-table)))
4089 (set-display-table-slot
4090 org-display-table 4
4091 (vconcat (mapcar
4092 (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
4093 org-ellipsis)))
4094 (if (stringp org-ellipsis) org-ellipsis "..."))))
4095 (setq buffer-display-table org-display-table))
4096 (org-set-regexps-and-options)
4097 (when (and org-tag-faces (not org-tags-special-faces-re))
4098 ;; tag faces set outside customize.... force initialization.
4099 (org-set-tag-faces 'org-tag-faces org-tag-faces))
4100 ;; Calc embedded
4101 (org-set-local 'calc-embedded-open-mode "# ")
4102 (modify-syntax-entry ?# "<")
4103 (modify-syntax-entry ?@ "w")
4104 (if org-startup-truncated (setq truncate-lines t))
4105 (org-set-local 'font-lock-unfontify-region-function
4106 'org-unfontify-region)
4107 ;; Activate before-change-function
4108 (org-set-local 'org-table-may-need-update t)
4109 (org-add-hook 'before-change-functions 'org-before-change-function nil
4110 'local)
4111 ;; Check for running clock before killing a buffer
4112 (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
4113 ;; Paragraphs and auto-filling
4114 (org-set-autofill-regexps)
4115 (setq indent-line-function 'org-indent-line-function)
4116 (org-update-radio-target-regexp)
4117 ;; Make sure dependence stuff works reliably, even for users who set it
4118 ;; too late :-(
4119 (if org-enforce-todo-dependencies
4120 (add-hook 'org-blocker-hook
4121 'org-block-todo-from-children-or-siblings-or-parent)
4122 (remove-hook 'org-blocker-hook
4123 'org-block-todo-from-children-or-siblings-or-parent))
4124 (if org-enforce-todo-checkbox-dependencies
4125 (add-hook 'org-blocker-hook
4126 'org-block-todo-from-checkboxes)
4127 (remove-hook 'org-blocker-hook
4128 'org-block-todo-from-checkboxes))
4130 ;; Comment characters
4131 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
4132 (org-set-local 'comment-padding " ")
4134 ;; Align options lines
4135 (org-set-local
4136 'align-mode-rules-list
4137 '((org-in-buffer-settings
4138 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
4139 (modes . '(org-mode)))))
4141 ;; Imenu
4142 (org-set-local 'imenu-create-index-function
4143 'org-imenu-get-tree)
4145 ;; Make isearch reveal context
4146 (if (or (featurep 'xemacs)
4147 (not (boundp 'outline-isearch-open-invisible-function)))
4148 ;; Emacs 21 and XEmacs make use of the hook
4149 (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
4150 ;; Emacs 22 deals with this through a special variable
4151 (org-set-local 'outline-isearch-open-invisible-function
4152 (lambda (&rest ignore) (org-show-context 'isearch))))
4154 ;; If empty file that did not turn on org-mode automatically, make it to.
4155 (if (and org-insert-mode-line-in-empty-file
4156 (interactive-p)
4157 (= (point-min) (point-max)))
4158 (insert "# -*- mode: org -*-\n\n"))
4160 (unless org-inhibit-startup
4161 (when org-startup-align-all-tables
4162 (let ((bmp (buffer-modified-p)))
4163 (org-table-map-tables 'org-table-align)
4164 (set-buffer-modified-p bmp)))
4165 (when org-startup-indented
4166 (require 'org-indent)
4167 (org-indent-mode 1))
4168 (org-set-startup-visibility)))
4170 (when (fboundp 'abbrev-table-put)
4171 (abbrev-table-put org-mode-abbrev-table
4172 :parents (list text-mode-abbrev-table)))
4174 (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
4176 (defun org-current-time ()
4177 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
4178 (if (> (car org-time-stamp-rounding-minutes) 1)
4179 (let ((r (car org-time-stamp-rounding-minutes))
4180 (time (decode-time)))
4181 (apply 'encode-time
4182 (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
4183 (nthcdr 2 time))))
4184 (current-time)))
4186 ;;;; Font-Lock stuff, including the activators
4188 (defvar org-mouse-map (make-sparse-keymap))
4189 (org-defkey org-mouse-map
4190 (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
4191 (org-defkey org-mouse-map
4192 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
4193 (when org-mouse-1-follows-link
4194 (org-defkey org-mouse-map [follow-link] 'mouse-face))
4195 (when org-tab-follows-link
4196 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
4197 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
4199 (require 'font-lock)
4201 (defconst org-non-link-chars "]\t\n\r<>")
4202 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
4203 "shell" "elisp"))
4204 (defvar org-link-types-re nil
4205 "Matches a link that has a url-like prefix like \"http:\"")
4206 (defvar org-link-re-with-space nil
4207 "Matches a link with spaces, optional angular brackets around it.")
4208 (defvar org-link-re-with-space2 nil
4209 "Matches a link with spaces, optional angular brackets around it.")
4210 (defvar org-link-re-with-space3 nil
4211 "Matches a link with spaces, only for internal part in bracket links.")
4212 (defvar org-angle-link-re nil
4213 "Matches link with angular brackets, spaces are allowed.")
4214 (defvar org-plain-link-re nil
4215 "Matches plain link, without spaces.")
4216 (defvar org-bracket-link-regexp nil
4217 "Matches a link in double brackets.")
4218 (defvar org-bracket-link-analytic-regexp nil
4219 "Regular expression used to analyze links.
4220 Here is what the match groups contain after a match:
4221 1: http:
4222 2: http
4223 3: path
4224 4: [desc]
4225 5: desc")
4226 (defvar org-bracket-link-analytic-regexp++ nil
4227 "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
4228 (defvar org-any-link-re nil
4229 "Regular expression matching any link.")
4231 (defun org-make-link-regexps ()
4232 "Update the link regular expressions.
4233 This should be called after the variable `org-link-types' has changed."
4234 (setq org-link-types-re
4235 (concat
4236 "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
4237 org-link-re-with-space
4238 (concat
4239 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4240 "\\([^" org-non-link-chars " ]"
4241 "[^" org-non-link-chars "]*"
4242 "[^" org-non-link-chars " ]\\)>?")
4243 org-link-re-with-space2
4244 (concat
4245 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4246 "\\([^" org-non-link-chars " ]"
4247 "[^\t\n\r]*"
4248 "[^" org-non-link-chars " ]\\)>?")
4249 org-link-re-with-space3
4250 (concat
4251 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4252 "\\([^" org-non-link-chars " ]"
4253 "[^\t\n\r]*\\)")
4254 org-angle-link-re
4255 (concat
4256 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4257 "\\([^" org-non-link-chars " ]"
4258 "[^" org-non-link-chars "]*"
4259 "\\)>")
4260 org-plain-link-re
4261 (concat
4262 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4263 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
4264 org-bracket-link-regexp
4265 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
4266 org-bracket-link-analytic-regexp
4267 (concat
4268 "\\[\\["
4269 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
4270 "\\([^]]+\\)"
4271 "\\]"
4272 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4273 "\\]")
4274 org-bracket-link-analytic-regexp++
4275 (concat
4276 "\\[\\["
4277 "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
4278 "\\([^]]+\\)"
4279 "\\]"
4280 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4281 "\\]")
4282 org-any-link-re
4283 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
4284 org-angle-link-re "\\)\\|\\("
4285 org-plain-link-re "\\)")))
4287 (org-make-link-regexps)
4289 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
4290 "Regular expression for fast time stamp matching.")
4291 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
4292 "Regular expression for fast time stamp matching.")
4293 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4294 "Regular expression matching time strings for analysis.
4295 This one does not require the space after the date, so it can be used
4296 on a string that terminates immediately after the date.")
4297 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4298 "Regular expression matching time strings for analysis.")
4299 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
4300 "Regular expression matching time stamps, with groups.")
4301 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
4302 "Regular expression matching time stamps (also [..]), with groups.")
4303 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
4304 "Regular expression matching a time stamp range.")
4305 (defconst org-tr-regexp-both
4306 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
4307 "Regular expression matching a time stamp range.")
4308 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
4309 org-ts-regexp "\\)?")
4310 "Regular expression matching a time stamp or time stamp range.")
4311 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
4312 org-ts-regexp-both "\\)?")
4313 "Regular expression matching a time stamp or time stamp range.
4314 The time stamps may be either active or inactive.")
4316 (defvar org-emph-face nil)
4318 (defun org-do-emphasis-faces (limit)
4319 "Run through the buffer and add overlays to links."
4320 (let (rtn a)
4321 (while (and (not rtn) (re-search-forward org-emph-re limit t))
4322 (if (not (= (char-after (match-beginning 3))
4323 (char-after (match-beginning 4))))
4324 (progn
4325 (setq rtn t)
4326 (setq a (assoc (match-string 3) org-emphasis-alist))
4327 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
4328 'face
4329 (nth 1 a))
4330 (and (nth 4 a)
4331 (org-remove-flyspell-overlays-in
4332 (match-beginning 0) (match-end 0)))
4333 (add-text-properties (match-beginning 2) (match-end 2)
4334 '(font-lock-multiline t))
4335 (when org-hide-emphasis-markers
4336 (add-text-properties (match-end 4) (match-beginning 5)
4337 '(invisible org-link))
4338 (add-text-properties (match-beginning 3) (match-end 3)
4339 '(invisible org-link)))))
4340 (backward-char 1))
4341 rtn))
4343 (defun org-emphasize (&optional char)
4344 "Insert or change an emphasis, i.e. a font like bold or italic.
4345 If there is an active region, change that region to a new emphasis.
4346 If there is no region, just insert the marker characters and position
4347 the cursor between them.
4348 CHAR should be either the marker character, or the first character of the
4349 HTML tag associated with that emphasis. If CHAR is a space, the means
4350 to remove the emphasis of the selected region.
4351 If char is not given (for example in an interactive call) it
4352 will be prompted for."
4353 (interactive)
4354 (let ((eal org-emphasis-alist) e det
4355 (erc org-emphasis-regexp-components)
4356 (prompt "")
4357 (string "") beg end move tag c s)
4358 (if (org-region-active-p)
4359 (setq beg (region-beginning) end (region-end)
4360 string (buffer-substring beg end))
4361 (setq move t))
4363 (while (setq e (pop eal))
4364 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
4365 c (aref tag 0))
4366 (push (cons c (string-to-char (car e))) det)
4367 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
4368 (substring tag 1)))))
4369 (setq det (nreverse det))
4370 (unless char
4371 (message "%s" (concat "Emphasis marker or tag:" prompt))
4372 (setq char (read-char-exclusive)))
4373 (setq char (or (cdr (assoc char det)) char))
4374 (if (equal char ?\ )
4375 (setq s "" move nil)
4376 (unless (assoc (char-to-string char) org-emphasis-alist)
4377 (error "No such emphasis marker: \"%c\"" char))
4378 (setq s (char-to-string char)))
4379 (while (and (> (length string) 1)
4380 (equal (substring string 0 1) (substring string -1))
4381 (assoc (substring string 0 1) org-emphasis-alist))
4382 (setq string (substring string 1 -1)))
4383 (setq string (concat s string s))
4384 (if beg (delete-region beg end))
4385 (unless (or (bolp)
4386 (string-match (concat "[" (nth 0 erc) "\n]")
4387 (char-to-string (char-before (point)))))
4388 (insert " "))
4389 (unless (string-match (concat "[" (nth 1 erc) "\n]")
4390 (char-to-string (char-after (point))))
4391 (insert " ") (backward-char 1))
4392 (insert string)
4393 (and move (backward-char 1))))
4395 (defconst org-nonsticky-props
4396 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
4398 (defsubst org-rear-nonsticky-at (pos)
4399 (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
4401 (defun org-activate-plain-links (limit)
4402 "Run through the buffer and add overlays to links."
4403 (catch 'exit
4404 (let (f)
4405 (if (re-search-forward org-plain-link-re limit t)
4406 (progn
4407 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4408 (setq f (get-text-property (match-beginning 0) 'face))
4409 (if (or (eq f 'org-tag)
4410 (and (listp f) (memq 'org-tag f)))
4412 (add-text-properties (match-beginning 0) (match-end 0)
4413 (list 'mouse-face 'highlight
4414 'keymap org-mouse-map))
4415 (org-rear-nonsticky-at (match-end 0)))
4416 t)))))
4418 (defun org-activate-code (limit)
4419 (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
4420 (progn
4421 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4422 (remove-text-properties (match-beginning 0) (match-end 0)
4423 '(display t invisible t intangible t))
4424 t)))
4426 (defun org-fontify-meta-lines-and-blocks (limit)
4427 "Fontify #+ lines and blocks, in the correct ways."
4428 (let ((case-fold-search t))
4429 (if (re-search-forward
4430 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
4431 limit t)
4432 (let ((beg (match-beginning 0))
4433 (beg1 (line-beginning-position 2))
4434 (dc1 (downcase (match-string 2)))
4435 (dc3 (downcase (match-string 3)))
4436 end end1 quoting)
4437 (cond
4438 ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
4439 ;; a single line of backend-specific content
4440 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4441 (remove-text-properties (match-beginning 0) (match-end 0)
4442 '(display t invisible t intangible t))
4443 (add-text-properties (match-beginning 1) (match-end 3)
4444 '(font-lock-fontified t face org-meta-line))
4445 (add-text-properties (match-beginning 6) (match-end 6)
4446 '(font-lock-fontified t face org-block))
4448 ((and (match-end 4) (equal dc3 "begin"))
4449 ;; Truely a block
4450 (setq quoting (member (downcase (match-string 5))
4451 org-protecting-blocks))
4452 (when (re-search-forward
4453 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
4454 nil t) ;; on purpose, we look further than LIMIT
4455 (setq end (match-end 0) end1 (1- (match-beginning 0)))
4456 (when quoting
4457 (remove-text-properties beg end
4458 '(display t invisible t intangible t)))
4459 (add-text-properties
4460 beg end
4461 '(font-lock-fontified t font-lock-multiline t))
4462 (add-text-properties beg beg1 '(face org-meta-line))
4463 (add-text-properties end1 end '(face org-meta-line))
4464 (when quoting
4465 (add-text-properties beg1 end1 '(face org-block)))
4467 ((not (member (char-after beg) '(?\ ?\t)))
4468 ;; just any other in-buffer setting, but not indented
4469 (add-text-properties
4470 beg (match-end 0)
4471 '(font-lock-fontified t face org-meta-line))
4473 ((or (member dc1 '("begin:" "end:" "caption:" "label:"
4474 "orgtbl:" "tblfm:" "tblname:"))
4475 (and (match-end 4) (equal dc3 "attr")))
4476 (add-text-properties
4477 beg (match-end 0)
4478 '(font-lock-fontified t face org-meta-line))
4480 ((member dc3 '(" " ""))
4481 (add-text-properties
4482 beg (match-end 0)
4483 '(font-lock-fontified t face font-lock-comment-face)))
4484 (t nil))))))
4486 (defun org-activate-angle-links (limit)
4487 "Run through the buffer and add overlays to links."
4488 (if (re-search-forward org-angle-link-re limit t)
4489 (progn
4490 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4491 (add-text-properties (match-beginning 0) (match-end 0)
4492 (list 'mouse-face 'highlight
4493 'keymap org-mouse-map))
4494 (org-rear-nonsticky-at (match-end 0))
4495 t)))
4497 (defun org-activate-footnote-links (limit)
4498 "Run through the buffer and add overlays to links."
4499 (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
4500 limit t)
4501 (progn
4502 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4503 (add-text-properties (match-beginning 2) (match-end 2)
4504 (list 'mouse-face 'highlight
4505 'keymap org-mouse-map
4506 'help-echo
4507 (if (= (point-at-bol) (match-beginning 2))
4508 "Footnote definition"
4509 "Footnote reference")
4511 (org-rear-nonsticky-at (match-end 2))
4512 t)))
4514 (defun org-activate-bracket-links (limit)
4515 "Run through the buffer and add overlays to bracketed links."
4516 (if (re-search-forward org-bracket-link-regexp limit t)
4517 (let* ((help (concat "LINK: "
4518 (org-match-string-no-properties 1)))
4519 ;; FIXME: above we should remove the escapes.
4520 ;; but that requires another match, protecting match data,
4521 ;; a lot of overhead for font-lock.
4522 (ip (org-maybe-intangible
4523 (list 'invisible 'org-link
4524 'keymap org-mouse-map 'mouse-face 'highlight
4525 'font-lock-multiline t 'help-echo help)))
4526 (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
4527 'font-lock-multiline t 'help-echo help)))
4528 ;; We need to remove the invisible property here. Table narrowing
4529 ;; may have made some of this invisible.
4530 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4531 (remove-text-properties (match-beginning 0) (match-end 0)
4532 '(invisible nil))
4533 (if (match-end 3)
4534 (progn
4535 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
4536 (org-rear-nonsticky-at (match-beginning 3))
4537 (add-text-properties (match-beginning 3) (match-end 3) vp)
4538 (org-rear-nonsticky-at (match-end 3))
4539 (add-text-properties (match-end 3) (match-end 0) ip)
4540 (org-rear-nonsticky-at (match-end 0)))
4541 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
4542 (org-rear-nonsticky-at (match-beginning 1))
4543 (add-text-properties (match-beginning 1) (match-end 1) vp)
4544 (org-rear-nonsticky-at (match-end 1))
4545 (add-text-properties (match-end 1) (match-end 0) ip)
4546 (org-rear-nonsticky-at (match-end 0)))
4547 t)))
4549 (defun org-activate-dates (limit)
4550 "Run through the buffer and add overlays to dates."
4551 (if (re-search-forward org-tsr-regexp-both limit t)
4552 (progn
4553 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4554 (add-text-properties (match-beginning 0) (match-end 0)
4555 (list 'mouse-face 'highlight
4556 'keymap org-mouse-map))
4557 (org-rear-nonsticky-at (match-end 0))
4558 (when org-display-custom-times
4559 (if (match-end 3)
4560 (org-display-custom-time (match-beginning 3) (match-end 3)))
4561 (org-display-custom-time (match-beginning 1) (match-end 1)))
4562 t)))
4564 (defvar org-target-link-regexp nil
4565 "Regular expression matching radio targets in plain text.")
4566 (make-variable-buffer-local 'org-target-link-regexp)
4567 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
4568 "Regular expression matching a link target.")
4569 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
4570 "Regular expression matching a radio target.")
4571 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
4572 "Regular expression matching any target.")
4574 (defun org-activate-target-links (limit)
4575 "Run through the buffer and add overlays to target matches."
4576 (when org-target-link-regexp
4577 (let ((case-fold-search t))
4578 (if (re-search-forward org-target-link-regexp limit t)
4579 (progn
4580 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4581 (add-text-properties (match-beginning 0) (match-end 0)
4582 (list 'mouse-face 'highlight
4583 'keymap org-mouse-map
4584 'help-echo "Radio target link"
4585 'org-linked-text t))
4586 (org-rear-nonsticky-at (match-end 0))
4587 t)))))
4589 (defun org-update-radio-target-regexp ()
4590 "Find all radio targets in this file and update the regular expression."
4591 (interactive)
4592 (when (memq 'radio org-activate-links)
4593 (setq org-target-link-regexp
4594 (org-make-target-link-regexp (org-all-targets 'radio)))
4595 (org-restart-font-lock)))
4597 (defun org-hide-wide-columns (limit)
4598 (let (s e)
4599 (setq s (text-property-any (point) (or limit (point-max))
4600 'org-cwidth t))
4601 (when s
4602 (setq e (next-single-property-change s 'org-cwidth))
4603 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
4604 (goto-char e)
4605 t)))
4607 (defvar org-latex-and-specials-regexp nil
4608 "Regular expression for highlighting export special stuff.")
4609 (defvar org-match-substring-regexp)
4610 (defvar org-match-substring-with-braces-regexp)
4612 ;; This should be with the exporter code, but we also use if for font-locking
4613 (defconst org-export-html-special-string-regexps
4614 '(("\\\\-" . "&shy;")
4615 ("---\\([^-]\\)" . "&mdash;\\1")
4616 ("--\\([^-]\\)" . "&ndash;\\1")
4617 ("\\.\\.\\." . "&hellip;"))
4618 "Regular expressions for special string conversion.")
4621 (defun org-compute-latex-and-specials-regexp ()
4622 "Compute regular expression for stuff treated specially by exporters."
4623 (if (not org-highlight-latex-fragments-and-specials)
4624 (org-set-local 'org-latex-and-specials-regexp nil)
4625 (require 'org-exp)
4626 (let*
4627 ((matchers (plist-get org-format-latex-options :matchers))
4628 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
4629 org-latex-regexps)))
4630 (options (org-combine-plists (org-default-export-plist)
4631 (org-infile-export-plist)))
4632 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
4633 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
4634 (org-export-with-TeX-macros (plist-get options :TeX-macros))
4635 (org-export-html-expand (plist-get options :expand-quoted-html))
4636 (org-export-with-special-strings (plist-get options :special-strings))
4637 (re-sub
4638 (cond
4639 ((equal org-export-with-sub-superscripts '{})
4640 (list org-match-substring-with-braces-regexp))
4641 (org-export-with-sub-superscripts
4642 (list org-match-substring-regexp))
4643 (t nil)))
4644 (re-latex
4645 (if org-export-with-LaTeX-fragments
4646 (mapcar (lambda (x) (nth 1 x)) latexs)))
4647 (re-macros
4648 (if org-export-with-TeX-macros
4649 (list (concat "\\\\"
4650 (regexp-opt
4651 (append (mapcar 'car org-html-entities)
4652 (if (boundp 'org-latex-entities)
4653 (mapcar (lambda (x)
4654 (or (car-safe x) x))
4655 org-latex-entities)
4656 nil))
4657 'words))) ; FIXME
4659 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
4660 (re-special (if org-export-with-special-strings
4661 (mapcar (lambda (x) (car x))
4662 org-export-html-special-string-regexps)))
4663 (re-rest
4664 (delq nil
4665 (list
4666 (if org-export-html-expand "@<[^>\n]+>")
4667 ))))
4668 (org-set-local
4669 'org-latex-and-specials-regexp
4670 (mapconcat 'identity (append re-latex re-sub re-macros re-special
4671 re-rest) "\\|")))))
4673 (defun org-do-latex-and-special-faces (limit)
4674 "Run through the buffer and add overlays to links."
4675 (when org-latex-and-specials-regexp
4676 (let (rtn d)
4677 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
4678 limit t))
4679 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
4680 'face))
4681 '(org-code org-verbatim underline)))
4682 (progn
4683 (setq rtn t
4684 d (cond ((member (char-after (1+ (match-beginning 0)))
4685 '(?_ ?^)) 1)
4686 (t 0)))
4687 (font-lock-prepend-text-property
4688 (+ d (match-beginning 0)) (match-end 0)
4689 'face 'org-latex-and-export-specials)
4690 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
4691 '(font-lock-multiline t)))))
4692 rtn)))
4694 (defun org-restart-font-lock ()
4695 "Restart font-lock-mode, to force refontification."
4696 (when (and (boundp 'font-lock-mode) font-lock-mode)
4697 (font-lock-mode -1)
4698 (font-lock-mode 1)))
4700 (defun org-all-targets (&optional radio)
4701 "Return a list of all targets in this file.
4702 With optional argument RADIO, only find radio targets."
4703 (let ((re (if radio org-radio-target-regexp org-target-regexp))
4704 rtn)
4705 (save-excursion
4706 (goto-char (point-min))
4707 (while (re-search-forward re nil t)
4708 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
4709 rtn)))
4711 (defun org-make-target-link-regexp (targets)
4712 "Make regular expression matching all strings in TARGETS.
4713 The regular expression finds the targets also if there is a line break
4714 between words."
4715 (and targets
4716 (concat
4717 "\\<\\("
4718 (mapconcat
4719 (lambda (x)
4720 (while (string-match " +" x)
4721 (setq x (replace-match "\\s-+" t t x)))
4723 targets
4724 "\\|")
4725 "\\)\\>")))
4727 (defun org-activate-tags (limit)
4728 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
4729 (progn
4730 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4731 (add-text-properties (match-beginning 1) (match-end 1)
4732 (list 'mouse-face 'highlight
4733 'keymap org-mouse-map))
4734 (org-rear-nonsticky-at (match-end 1))
4735 t)))
4737 (defun org-outline-level ()
4738 "Compute the outline level of the heading at point.
4739 This function assumes that the cursor is at the beginning of a line matched
4740 by outline-regexp. Otherwise it returns garbage.
4741 If this is called at a normal headline, the level is the number of stars.
4742 Use `org-reduced-level' to remove the effect of `org-odd-levels'.
4743 For plain list items, if they are matched by `outline-regexp', this returns
4744 1000 plus the line indentation."
4745 (save-excursion
4746 (looking-at outline-regexp)
4747 (if (match-beginning 1)
4748 (+ (org-get-string-indentation (match-string 1)) 1000)
4749 (1- (- (match-end 0) (match-beginning 0))))))
4751 (defvar org-font-lock-keywords nil)
4753 (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
4754 "Regular expression matching a property line.")
4756 (defvar org-font-lock-hook nil
4757 "Functions to be called for special font lock stuff.")
4759 (defun org-font-lock-hook (limit)
4760 (run-hook-with-args 'org-font-lock-hook limit))
4762 (defun org-set-font-lock-defaults ()
4763 (let* ((em org-fontify-emphasized-text)
4764 (lk org-activate-links)
4765 (org-font-lock-extra-keywords
4766 (list
4767 ;; Call the hook
4768 '(org-font-lock-hook)
4769 ;; Headlines
4770 `(,(if org-fontify-whole-heading-line
4771 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
4772 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
4773 (1 (org-get-level-face 1))
4774 (2 (org-get-level-face 2))
4775 (3 (org-get-level-face 3)))
4776 ;; Table lines
4777 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
4778 (1 'org-table t))
4779 ;; Table internals
4780 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
4781 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
4782 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
4783 '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
4784 ;; Drawers
4785 (list org-drawer-regexp '(0 'org-special-keyword t))
4786 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
4787 ;; Properties
4788 (list org-property-re
4789 '(1 'org-special-keyword t)
4790 '(3 'org-property-value t))
4791 ;; Links
4792 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
4793 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
4794 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
4795 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
4796 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
4797 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
4798 (if (memq 'footnote lk) '(org-activate-footnote-links
4799 (2 'org-footnote t)))
4800 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
4801 '(org-hide-wide-columns (0 nil append))
4802 ;; TODO lines
4803 (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
4804 '(1 (org-get-todo-face 1) t))
4805 ;; DONE
4806 (if org-fontify-done-headline
4807 (list (concat "^[*]+ +\\<\\("
4808 (mapconcat 'regexp-quote org-done-keywords "\\|")
4809 "\\)\\(.*\\)")
4810 '(2 'org-headline-done t))
4811 nil)
4812 ;; Priorities
4813 '(org-font-lock-add-priority-faces)
4814 ;; Tags
4815 '(org-font-lock-add-tag-faces)
4816 ;; Special keywords
4817 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
4818 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
4819 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
4820 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
4821 ;; Emphasis
4822 (if em
4823 (if (featurep 'xemacs)
4824 '(org-do-emphasis-faces (0 nil append))
4825 '(org-do-emphasis-faces)))
4826 ;; Checkboxes
4827 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
4828 2 'org-checkbox prepend)
4829 (if org-provide-checkbox-statistics
4830 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
4831 (0 (org-get-checkbox-statistics-face) t)))
4832 ;; Description list items
4833 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
4834 2 'bold prepend)
4835 ;; ARCHIVEd headings
4836 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
4837 '(1 'org-archived prepend))
4838 ;; Specials
4839 '(org-do-latex-and-special-faces)
4840 ;; Code
4841 '(org-activate-code (1 'org-code t))
4842 ;; COMMENT
4843 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
4844 "\\|" org-quote-string "\\)\\>")
4845 '(1 'org-special-keyword t))
4846 '("^#.*" (0 'font-lock-comment-face t))
4847 ;; Blocks and meta lines
4848 '(org-fontify-meta-lines-and-blocks)
4850 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
4851 ;; Now set the full font-lock-keywords
4852 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
4853 (org-set-local 'font-lock-defaults
4854 '(org-font-lock-keywords t nil nil backward-paragraph))
4855 (kill-local-variable 'font-lock-keywords) nil))
4857 (defun org-fontify-like-in-org-mode (s &optional odd-levels)
4858 "Fontify string S like in Org-mode"
4859 (with-temp-buffer
4860 (insert s)
4861 (let ((org-odd-levels-only odd-levels))
4862 (org-mode)
4863 (font-lock-fontify-buffer)
4864 (buffer-string))))
4866 (defvar org-m nil)
4867 (defvar org-l nil)
4868 (defvar org-f nil)
4869 (defun org-get-level-face (n)
4870 "Get the right face for match N in font-lock matching of headlines."
4871 (setq org-l (- (match-end 2) (match-beginning 1) 1))
4872 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
4873 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
4874 (cond
4875 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
4876 ((eq n 2) org-f)
4877 (t (if org-level-color-stars-only nil org-f))))
4879 (defun org-get-todo-face (kwd)
4880 "Get the right face for a TODO keyword KWD.
4881 If KWD is a number, get the corresponding match group."
4882 (if (numberp kwd) (setq kwd (match-string kwd)))
4883 (or (cdr (assoc kwd org-todo-keyword-faces))
4884 (and (member kwd org-done-keywords) 'org-done)
4885 'org-todo))
4887 (defun org-font-lock-add-tag-faces (limit)
4888 "Add the special tag faces."
4889 (when (and org-tag-faces org-tags-special-faces-re)
4890 (while (re-search-forward org-tags-special-faces-re limit t)
4891 (add-text-properties (match-beginning 1) (match-end 1)
4892 (list 'face (org-get-tag-face 1)
4893 'font-lock-fontified t))
4894 (backward-char 1))))
4896 (defun org-font-lock-add-priority-faces (limit)
4897 "Add the special priority faces."
4898 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
4899 (add-text-properties
4900 (match-beginning 0) (match-end 0)
4901 (list 'face (or (cdr (assoc (char-after (match-beginning 1))
4902 org-priority-faces))
4903 'org-special-keyword)
4904 'font-lock-fontified t))))
4906 (defun org-get-tag-face (kwd)
4907 "Get the right face for a TODO keyword KWD.
4908 If KWD is a number, get the corresponding match group."
4909 (if (numberp kwd) (setq kwd (match-string kwd)))
4910 (or (cdr (assoc kwd org-tag-faces))
4911 'org-tag))
4913 (defun org-unfontify-region (beg end &optional maybe_loudly)
4914 "Remove fontification and activation overlays from links."
4915 (font-lock-default-unfontify-region beg end)
4916 (let* ((buffer-undo-list t)
4917 (inhibit-read-only t) (inhibit-point-motion-hooks t)
4918 (inhibit-modification-hooks t)
4919 deactivate-mark buffer-file-name buffer-file-truename)
4920 (remove-text-properties beg end
4921 '(mouse-face t keymap t org-linked-text t
4922 invisible t intangible t
4923 line-prefix t wrap-prefix t
4924 org-no-flyspell t))))
4926 ;;;; Visibility cycling, including org-goto and indirect buffer
4928 ;;; Cycling
4930 (defvar org-cycle-global-status nil)
4931 (make-variable-buffer-local 'org-cycle-global-status)
4932 (defvar org-cycle-subtree-status nil)
4933 (make-variable-buffer-local 'org-cycle-subtree-status)
4935 ;;;###autoload
4937 (defvar org-inlinetask-min-level)
4939 (defun org-cycle (&optional arg)
4940 "TAB-action and visibility cycling for Org-mode.
4942 This is the command invoked in Org-mode by the TAB key. Its main purpose
4943 is outine visibility cycling, but it also invokes other actions
4944 in special contexts.
4946 - When this function is called with a prefix argument, rotate the entire
4947 buffer through 3 states (global cycling)
4948 1. OVERVIEW: Show only top-level headlines.
4949 2. CONTENTS: Show all headlines of all levels, but no body text.
4950 3. SHOW ALL: Show everything.
4951 When called with two `C-u C-u' prefixes, switch to the startup visibility,
4952 determined by the variable `org-startup-folded', and by any VISIBILITY
4953 properties in the buffer.
4954 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
4955 including any drawers.
4957 - When inside a table, re-align the table and move to the next field.
4959 - When point is at the beginning of a headline, rotate the subtree started
4960 by this line through 3 different states (local cycling)
4961 1. FOLDED: Only the main headline is shown.
4962 2. CHILDREN: The main headline and the direct children are shown.
4963 From this state, you can move to one of the children
4964 and zoom in further.
4965 3. SUBTREE: Show the entire subtree, including body text.
4966 If there is no subtree, switch directly from CHILDREN to FOLDED.
4968 - When there is a numeric prefix, go up to a heading with level ARG, do
4969 a `show-subtree' and return to the previous cursor position. If ARG
4970 is negative, go up that many levels.
4972 - When point is not at the beginning of a headline, execute the global
4973 binding for TAB, which is re-indenting the line. See the option
4974 `org-cycle-emulate-tab' for details.
4976 - Special case: if point is at the beginning of the buffer and there is
4977 no headline in line 1, this function will act as if called with prefix arg.
4978 But only if also the variable `org-cycle-global-at-bob' is t."
4979 (interactive "P")
4980 (org-load-modules-maybe)
4981 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
4982 (and org-cycle-level-after-item/entry-creation
4983 (or (org-cycle-level)
4984 (org-cycle-item-indentation))))
4985 (let* ((limit-level
4986 (or org-cycle-max-level
4987 (and (boundp 'org-inlinetask-min-level)
4988 org-inlinetask-min-level
4989 (1- org-inlinetask-min-level))))
4990 (nstars (and limit-level
4991 (if org-odd-levels-only
4992 (and limit-level (1- (* limit-level 2)))
4993 limit-level)))
4994 (outline-regexp
4995 (cond
4996 ((not (org-mode-p)) outline-regexp)
4997 ((or (eq org-cycle-include-plain-lists 'integrate)
4998 (and org-cycle-include-plain-lists (org-at-item-p)))
4999 (concat "\\(?:\\*"
5000 (if nstars (format "\\{1,%d\\}" nstars) "+")
5001 " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
5002 (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
5003 (bob-special (and org-cycle-global-at-bob (bobp)
5004 (not (looking-at outline-regexp))))
5005 (org-cycle-hook
5006 (if bob-special
5007 (delq 'org-optimize-window-after-visibility-change
5008 (copy-sequence org-cycle-hook))
5009 org-cycle-hook))
5010 (pos (point)))
5012 (if (or bob-special (equal arg '(4)))
5013 ;; special case: use global cycling
5014 (setq arg t))
5016 (cond
5018 ((equal arg '(16))
5019 (org-set-startup-visibility)
5020 (message "Startup visibility, plus VISIBILITY properties"))
5022 ((equal arg '(64))
5023 (show-all)
5024 (message "Entire buffer visible, including drawers"))
5026 ((org-at-table-p 'any)
5027 ;; Enter the table or move to the next field in the table
5028 (or (org-table-recognize-table.el)
5029 (progn
5030 (if arg (org-table-edit-field t)
5031 (org-table-justify-field-maybe)
5032 (call-interactively 'org-table-next-field)))))
5034 ((run-hook-with-args-until-success
5035 'org-tab-after-check-for-table-hook))
5037 ((eq arg t) ;; Global cycling
5038 (org-cycle-internal-global))
5040 ((and org-drawers org-drawer-regexp
5041 (save-excursion
5042 (beginning-of-line 1)
5043 (looking-at org-drawer-regexp)))
5044 ;; Toggle block visibility
5045 (org-flag-drawer
5046 (not (get-char-property (match-end 0) 'invisible))))
5048 ((integerp arg)
5049 ;; Show-subtree, ARG levels up from here.
5050 (save-excursion
5051 (org-back-to-heading)
5052 (outline-up-heading (if (< arg 0) (- arg)
5053 (- (funcall outline-level) arg)))
5054 (org-show-subtree)))
5056 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
5057 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
5059 (org-cycle-internal-local))
5061 ;; TAB emulation and template completion
5062 (buffer-read-only (org-back-to-heading))
5064 ((run-hook-with-args-until-success
5065 'org-tab-after-check-for-cycling-hook))
5067 ((org-try-structure-completion))
5069 ((org-try-cdlatex-tab))
5071 ((run-hook-with-args-until-success
5072 'org-tab-before-tab-emulation-hook))
5074 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
5075 (or (not (bolp))
5076 (not (looking-at outline-regexp))))
5077 (call-interactively (global-key-binding "\t")))
5079 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
5080 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
5081 (or (and (eq org-cycle-emulate-tab 'white)
5082 (= (match-end 0) (point-at-eol)))
5083 (and (eq org-cycle-emulate-tab 'whitestart)
5084 (>= (match-end 0) pos))))
5086 (eq org-cycle-emulate-tab t))
5087 (call-interactively (global-key-binding "\t")))
5089 (t (save-excursion
5090 (org-back-to-heading)
5091 (org-cycle)))))))
5093 (defun org-cycle-internal-global ()
5094 "Do the global cycling action."
5095 (cond
5096 ((and (eq last-command this-command)
5097 (eq org-cycle-global-status 'overview))
5098 ;; We just created the overview - now do table of contents
5099 ;; This can be slow in very large buffers, so indicate action
5100 (run-hook-with-args 'org-pre-cycle-hook 'contents)
5101 (message "CONTENTS...")
5102 (org-content)
5103 (message "CONTENTS...done")
5104 (setq org-cycle-global-status 'contents)
5105 (run-hook-with-args 'org-cycle-hook 'contents))
5107 ((and (eq last-command this-command)
5108 (eq org-cycle-global-status 'contents))
5109 ;; We just showed the table of contents - now show everything
5110 (run-hook-with-args 'org-pre-cycle-hook 'all)
5111 (show-all)
5112 (message "SHOW ALL")
5113 (setq org-cycle-global-status 'all)
5114 (run-hook-with-args 'org-cycle-hook 'all))
5117 ;; Default action: go to overview
5118 (run-hook-with-args 'org-pre-cycle-hook 'overview)
5119 (org-overview)
5120 (message "OVERVIEW")
5121 (setq org-cycle-global-status 'overview)
5122 (run-hook-with-args 'org-cycle-hook 'overview))))
5124 (defun org-cycle-internal-local ()
5125 "Do the local cycling action."
5126 (org-back-to-heading)
5127 (let ((goal-column 0) eoh eol eos level has-children children-skipped)
5128 ;; First, some boundaries
5129 (save-excursion
5130 (org-back-to-heading)
5131 (setq level (funcall outline-level))
5132 (save-excursion
5133 (beginning-of-line 2)
5134 (if (or (featurep 'xemacs) (<= emacs-major-version 21))
5135 ; XEmacs does not have `next-single-char-property-change'
5136 ; I'm not sure about Emacs 21.
5137 (while (and (not (eobp)) ;; this is like `next-line'
5138 (get-char-property (1- (point)) 'invisible))
5139 (beginning-of-line 2))
5140 (while (and (not (eobp)) ;; this is like `next-line'
5141 (get-char-property (1- (point)) 'invisible))
5142 (goto-char (next-single-char-property-change (point) 'invisible))
5143 ;;;??? (or (bolp) (beginning-of-line 2))))
5144 (and (eolp) (beginning-of-line 2))))
5145 (setq eol (point)))
5146 (outline-end-of-heading) (setq eoh (point))
5147 (save-excursion
5148 (outline-next-heading)
5149 (setq has-children (and (org-at-heading-p t)
5150 (> (funcall outline-level) level))))
5151 (org-end-of-subtree t)
5152 (unless (eobp)
5153 (skip-chars-forward " \t\n")
5154 (beginning-of-line 1) ; in case this is an item
5156 (setq eos (if (eobp) (point) (1- (point)))))
5157 ;; Find out what to do next and set `this-command'
5158 (cond
5159 ((= eos eoh)
5160 ;; Nothing is hidden behind this heading
5161 (run-hook-with-args 'org-pre-cycle-hook 'empty)
5162 (message "EMPTY ENTRY")
5163 (setq org-cycle-subtree-status nil)
5164 (save-excursion
5165 (goto-char eos)
5166 (outline-next-heading)
5167 (if (org-invisible-p) (org-flag-heading nil))))
5168 ((and (or (>= eol eos)
5169 (not (string-match "\\S-" (buffer-substring eol eos))))
5170 (or has-children
5171 (not (setq children-skipped
5172 org-cycle-skip-children-state-if-no-children))))
5173 ;; Entire subtree is hidden in one line: children view
5174 (run-hook-with-args 'org-pre-cycle-hook 'children)
5175 (org-show-entry)
5176 (show-children)
5177 (message "CHILDREN")
5178 (save-excursion
5179 (goto-char eos)
5180 (outline-next-heading)
5181 (if (org-invisible-p) (org-flag-heading nil)))
5182 (setq org-cycle-subtree-status 'children)
5183 (run-hook-with-args 'org-cycle-hook 'children))
5184 ((or children-skipped
5185 (and (eq last-command this-command)
5186 (eq org-cycle-subtree-status 'children)))
5187 ;; We just showed the children, or no children are there,
5188 ;; now show everything.
5189 (run-hook-with-args 'org-pre-cycle-hook 'subtree)
5190 (org-show-subtree)
5191 (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
5192 (setq org-cycle-subtree-status 'subtree)
5193 (run-hook-with-args 'org-cycle-hook 'subtree))
5195 ;; Default action: hide the subtree.
5196 (run-hook-with-args 'org-pre-cycle-hook 'folded)
5197 (hide-subtree)
5198 (message "FOLDED")
5199 (setq org-cycle-subtree-status 'folded)
5200 (run-hook-with-args 'org-cycle-hook 'folded)))))
5202 ;;;###autoload
5203 (defun org-global-cycle (&optional arg)
5204 "Cycle the global visibility. For details see `org-cycle'.
5205 With C-u prefix arg, switch to startup visibility.
5206 With a numeric prefix, show all headlines up to that level."
5207 (interactive "P")
5208 (let ((org-cycle-include-plain-lists
5209 (if (org-mode-p) org-cycle-include-plain-lists nil)))
5210 (cond
5211 ((integerp arg)
5212 (show-all)
5213 (hide-sublevels arg)
5214 (setq org-cycle-global-status 'contents))
5215 ((equal arg '(4))
5216 (org-set-startup-visibility)
5217 (message "Startup visibility, plus VISIBILITY properties."))
5219 (org-cycle '(4))))))
5221 (defun org-set-startup-visibility ()
5222 "Set the visibility required by startup options and properties."
5223 (cond
5224 ((eq org-startup-folded t)
5225 (org-cycle '(4)))
5226 ((eq org-startup-folded 'content)
5227 (let ((this-command 'org-cycle) (last-command 'org-cycle))
5228 (org-cycle '(4)) (org-cycle '(4)))))
5229 (unless (eq org-startup-folded 'showeverything)
5230 (if org-hide-block-startup (org-hide-block-all))
5231 (org-set-visibility-according-to-property 'no-cleanup)
5232 (org-cycle-hide-archived-subtrees 'all)
5233 (org-cycle-hide-drawers 'all)
5234 (org-cycle-show-empty-lines 'all)))
5236 (defun org-set-visibility-according-to-property (&optional no-cleanup)
5237 "Switch subtree visibilities according to :VISIBILITY: property."
5238 (interactive)
5239 (let (org-show-entry-below state)
5240 (save-excursion
5241 (goto-char (point-min))
5242 (while (re-search-forward
5243 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
5244 nil t)
5245 (setq state (match-string 1))
5246 (save-excursion
5247 (org-back-to-heading t)
5248 (hide-subtree)
5249 (org-reveal)
5250 (cond
5251 ((equal state '("fold" "folded"))
5252 (hide-subtree))
5253 ((equal state "children")
5254 (org-show-hidden-entry)
5255 (show-children))
5256 ((equal state "content")
5257 (save-excursion
5258 (save-restriction
5259 (org-narrow-to-subtree)
5260 (org-content))))
5261 ((member state '("all" "showall"))
5262 (show-subtree)))))
5263 (unless no-cleanup
5264 (org-cycle-hide-archived-subtrees 'all)
5265 (org-cycle-hide-drawers 'all)
5266 (org-cycle-show-empty-lines 'all)))))
5268 (defun org-overview ()
5269 "Switch to overview mode, showing only top-level headlines.
5270 Really, this shows all headlines with level equal or greater than the level
5271 of the first headline in the buffer. This is important, because if the
5272 first headline is not level one, then (hide-sublevels 1) gives confusing
5273 results."
5274 (interactive)
5275 (let ((level (save-excursion
5276 (goto-char (point-min))
5277 (if (re-search-forward (concat "^" outline-regexp) nil t)
5278 (progn
5279 (goto-char (match-beginning 0))
5280 (funcall outline-level))))))
5281 (and level (hide-sublevels level))))
5283 (defun org-content (&optional arg)
5284 "Show all headlines in the buffer, like a table of contents.
5285 With numerical argument N, show content up to level N."
5286 (interactive "P")
5287 (save-excursion
5288 ;; Visit all headings and show their offspring
5289 (and (integerp arg) (org-overview))
5290 (goto-char (point-max))
5291 (catch 'exit
5292 (while (and (progn (condition-case nil
5293 (outline-previous-visible-heading 1)
5294 (error (goto-char (point-min))))
5296 (looking-at outline-regexp))
5297 (if (integerp arg)
5298 (show-children (1- arg))
5299 (show-branches))
5300 (if (bobp) (throw 'exit nil))))))
5303 (defun org-optimize-window-after-visibility-change (state)
5304 "Adjust the window after a change in outline visibility.
5305 This function is the default value of the hook `org-cycle-hook'."
5306 (when (get-buffer-window (current-buffer))
5307 (cond
5308 ((eq state 'content) nil)
5309 ((eq state 'all) nil)
5310 ((eq state 'folded) nil)
5311 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
5312 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
5314 ;; FIXME: no longer in use
5315 (defun org-compact-display-after-subtree-move ()
5316 "Show a compacter version of the tree of the entry's parent."
5317 (save-excursion
5318 (if (org-up-heading-safe)
5319 (progn
5320 (hide-subtree)
5321 (show-entry)
5322 (show-children)
5323 (org-cycle-show-empty-lines 'children)
5324 (org-cycle-hide-drawers 'children))
5325 (org-overview))))
5327 (defun org-remove-empty-overlays-at (pos)
5328 "Remove outline overlays that do not contain non-white stuff."
5329 (mapc
5330 (lambda (o)
5331 (and (eq 'outline (org-overlay-get o 'invisible))
5332 (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
5333 (org-overlay-end o))))
5334 (org-delete-overlay o)))
5335 (org-overlays-at pos)))
5337 (defun org-clean-visibility-after-subtree-move ()
5338 "Fix visibility issues after moving a subtree."
5339 ;; First, find a reasonable region to look at:
5340 ;; Start two siblings above, end three below
5341 (let* ((beg (save-excursion
5342 (and (org-get-last-sibling)
5343 (org-get-last-sibling))
5344 (point)))
5345 (end (save-excursion
5346 (and (org-get-next-sibling)
5347 (org-get-next-sibling)
5348 (org-get-next-sibling))
5349 (if (org-at-heading-p)
5350 (point-at-eol)
5351 (point))))
5352 (level (looking-at "\\*+"))
5353 (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
5354 (save-excursion
5355 (save-restriction
5356 (narrow-to-region beg end)
5357 (when re
5358 ;; Properly fold already folded siblings
5359 (goto-char (point-min))
5360 (while (re-search-forward re nil t)
5361 (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
5362 (hide-entry))))
5363 (org-cycle-show-empty-lines 'overview)
5364 (org-cycle-hide-drawers 'overview)))))
5366 (defun org-cycle-show-empty-lines (state)
5367 "Show empty lines above all visible headlines.
5368 The region to be covered depends on STATE when called through
5369 `org-cycle-hook'. Lisp program can use t for STATE to get the
5370 entire buffer covered. Note that an empty line is only shown if there
5371 are at least `org-cycle-separator-lines' empty lines before the headline."
5372 (when (not (= org-cycle-separator-lines 0))
5373 (save-excursion
5374 (let* ((n (abs org-cycle-separator-lines))
5375 (re (cond
5376 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
5377 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
5378 (t (let ((ns (number-to-string (- n 2))))
5379 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
5380 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
5381 beg end b e)
5382 (cond
5383 ((memq state '(overview contents t))
5384 (setq beg (point-min) end (point-max)))
5385 ((memq state '(children folded))
5386 (setq beg (point) end (progn (org-end-of-subtree t t)
5387 (beginning-of-line 2)
5388 (point)))))
5389 (when beg
5390 (goto-char beg)
5391 (while (re-search-forward re end t)
5392 (unless (get-char-property (match-end 1) 'invisible)
5393 (setq e (match-end 1))
5394 (if (< org-cycle-separator-lines 0)
5395 (setq b (save-excursion
5396 (goto-char (match-beginning 0))
5397 (org-back-over-empty-lines)
5398 (if (save-excursion
5399 (goto-char (max (point-min) (1- (point))))
5400 (org-on-heading-p))
5401 (1- (point))
5402 (point))))
5403 (setq b (match-beginning 1)))
5404 (outline-flag-region b e nil)))))))
5405 ;; Never hide empty lines at the end of the file.
5406 (save-excursion
5407 (goto-char (point-max))
5408 (outline-previous-heading)
5409 (outline-end-of-heading)
5410 (if (and (looking-at "[ \t\n]+")
5411 (= (match-end 0) (point-max)))
5412 (outline-flag-region (point) (match-end 0) nil))))
5414 (defun org-show-empty-lines-in-parent ()
5415 "Move to the parent and re-show empty lines before visible headlines."
5416 (save-excursion
5417 (let ((context (if (org-up-heading-safe) 'children 'overview)))
5418 (org-cycle-show-empty-lines context))))
5420 (defun org-files-list ()
5421 "Return `org-agenda-files' list, plus all open org-mode files.
5422 This is useful for operations that need to scan all of a user's
5423 open and agenda-wise Org files."
5424 (let ((files (mapcar 'expand-file-name (org-agenda-files))))
5425 (dolist (buf (buffer-list))
5426 (with-current-buffer buf
5427 (if (and (eq major-mode 'org-mode) (buffer-file-name))
5428 (let ((file (expand-file-name (buffer-file-name))))
5429 (unless (member file files)
5430 (push file files))))))
5431 files))
5433 (defsubst org-entry-beginning-position ()
5434 "Return the beginning position of the current entry."
5435 (save-excursion (outline-back-to-heading t) (point)))
5437 (defsubst org-entry-end-position ()
5438 "Return the end position of the current entry."
5439 (save-excursion (outline-next-heading) (point)))
5441 (defun org-cycle-hide-drawers (state)
5442 "Re-hide all drawers after a visibility state change."
5443 (when (and (org-mode-p)
5444 (not (memq state '(overview folded contents))))
5445 (save-excursion
5446 (let* ((globalp (memq state '(contents all)))
5447 (beg (if globalp (point-min) (point)))
5448 (end (if globalp (point-max)
5449 (if (eq state 'children)
5450 (save-excursion (outline-next-heading) (point))
5451 (org-end-of-subtree t)))))
5452 (goto-char beg)
5453 (while (re-search-forward org-drawer-regexp end t)
5454 (org-flag-drawer t))))))
5456 (defun org-flag-drawer (flag)
5457 (save-excursion
5458 (beginning-of-line 1)
5459 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
5460 (let ((b (match-end 0))
5461 (outline-regexp org-outline-regexp))
5462 (if (re-search-forward
5463 "^[ \t]*:END:"
5464 (save-excursion (outline-next-heading) (point)) t)
5465 (outline-flag-region b (point-at-eol) flag)
5466 (error ":END: line missing at position %s" b))))))
5468 (defun org-subtree-end-visible-p ()
5469 "Is the end of the current subtree visible?"
5470 (pos-visible-in-window-p
5471 (save-excursion (org-end-of-subtree t) (point))))
5473 (defun org-first-headline-recenter (&optional N)
5474 "Move cursor to the first headline and recenter the headline.
5475 Optional argument N means, put the headline into the Nth line of the window."
5476 (goto-char (point-min))
5477 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
5478 (beginning-of-line)
5479 (recenter (prefix-numeric-value N))))
5481 ;;; Folding of blocks
5483 (defconst org-block-regexp
5485 "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
5486 "Regular expression for hiding blocks.")
5488 (defvar org-hide-block-overlays nil
5489 "Overays hiding blocks.")
5490 (make-variable-buffer-local 'org-hide-block-overlays)
5492 (defun org-block-map (function &optional start end)
5493 "Call func at the head of all source blocks in the current
5494 buffer. Optional arguments START and END can be used to limit
5495 the range."
5496 (let ((start (or start (point-min)))
5497 (end (or end (point-max))))
5498 (save-excursion
5499 (goto-char start)
5500 (while (and (< (point) end) (re-search-forward org-block-regexp end t))
5501 (save-excursion
5502 (save-match-data
5503 (goto-char (match-beginning 0))
5504 (funcall function)))))))
5506 (defun org-hide-block-toggle-all ()
5507 "Toggle the visibility of all blocks in the current buffer."
5508 (org-block-map #'org-hide-block-toggle))
5510 (defun org-hide-block-all ()
5511 "Fold all blocks in the current buffer."
5512 (interactive)
5513 (org-show-block-all)
5514 (org-block-map #'org-hide-block-toggle-maybe))
5516 (defun org-show-block-all ()
5517 "Unfold all blocks in the current buffer."
5518 (mapc 'org-delete-overlay org-hide-block-overlays)
5519 (setq org-hide-block-overlays nil))
5521 (defun org-hide-block-toggle-maybe ()
5522 "Toggle visibility of block at point."
5523 (interactive)
5524 (let ((case-fold-search t))
5525 (if (save-excursion
5526 (beginning-of-line 1)
5527 (looking-at org-block-regexp))
5528 (progn (org-hide-block-toggle)
5529 t) ;; to signal that we took action
5530 nil))) ;; to signal that we did not
5532 (defun org-hide-block-toggle (&optional force)
5533 "Toggle the visibility of the current block."
5534 (interactive)
5535 (save-excursion
5536 (beginning-of-line)
5537 (if (re-search-forward org-block-regexp nil t)
5538 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
5539 (end (match-end 0)) ;; end of entire body
5541 (if (memq t (mapcar (lambda (overlay)
5542 (eq (org-overlay-get overlay 'invisible)
5543 'org-hide-block))
5544 (org-overlays-at start)))
5545 (if (or (not force) (eq force 'off))
5546 (mapc (lambda (ov)
5547 (when (member ov org-hide-block-overlays)
5548 (setq org-hide-block-overlays
5549 (delq ov org-hide-block-overlays)))
5550 (when (eq (org-overlay-get ov 'invisible)
5551 'org-hide-block)
5552 (org-delete-overlay ov)))
5553 (org-overlays-at start)))
5554 (setq ov (org-make-overlay start end))
5555 (org-overlay-put ov 'invisible 'org-hide-block)
5556 ;; make the block accessible to isearch
5557 (org-overlay-put
5558 ov 'isearch-open-invisible
5559 (lambda (ov)
5560 (when (member ov org-hide-block-overlays)
5561 (setq org-hide-block-overlays
5562 (delq ov org-hide-block-overlays)))
5563 (when (eq (org-overlay-get ov 'invisible)
5564 'org-hide-block)
5565 (org-delete-overlay ov))))
5566 (push ov org-hide-block-overlays)))
5567 (error "Not looking at a source block"))))
5569 ;; org-tab-after-check-for-cycling-hook
5570 (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
5571 ;; Remove overlays when changing major mode
5572 (add-hook 'org-mode-hook
5573 (lambda () (org-add-hook 'change-major-mode-hook
5574 'org-show-block-all 'append 'local)))
5576 ;;; Org-goto
5578 (defvar org-goto-window-configuration nil)
5579 (defvar org-goto-marker nil)
5580 (defvar org-goto-map
5581 (let ((map (make-sparse-keymap)))
5582 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
5583 (while (setq cmd (pop cmds))
5584 (substitute-key-definition cmd cmd map global-map)))
5585 (suppress-keymap map)
5586 (org-defkey map "\C-m" 'org-goto-ret)
5587 (org-defkey map [(return)] 'org-goto-ret)
5588 (org-defkey map [(left)] 'org-goto-left)
5589 (org-defkey map [(right)] 'org-goto-right)
5590 (org-defkey map [(control ?g)] 'org-goto-quit)
5591 (org-defkey map "\C-i" 'org-cycle)
5592 (org-defkey map [(tab)] 'org-cycle)
5593 (org-defkey map [(down)] 'outline-next-visible-heading)
5594 (org-defkey map [(up)] 'outline-previous-visible-heading)
5595 (if org-goto-auto-isearch
5596 (if (fboundp 'define-key-after)
5597 (define-key-after map [t] 'org-goto-local-auto-isearch)
5598 nil)
5599 (org-defkey map "q" 'org-goto-quit)
5600 (org-defkey map "n" 'outline-next-visible-heading)
5601 (org-defkey map "p" 'outline-previous-visible-heading)
5602 (org-defkey map "f" 'outline-forward-same-level)
5603 (org-defkey map "b" 'outline-backward-same-level)
5604 (org-defkey map "u" 'outline-up-heading))
5605 (org-defkey map "/" 'org-occur)
5606 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
5607 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
5608 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
5609 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
5610 (org-defkey map "\C-c\C-u" 'outline-up-heading)
5611 map))
5613 (defconst org-goto-help
5614 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
5615 RET=jump to location [Q]uit and return to previous location
5616 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
5618 (defvar org-goto-start-pos) ; dynamically scoped parameter
5620 ;; FIXME: Docstring doe not mention both interfaces
5621 (defun org-goto (&optional alternative-interface)
5622 "Look up a different location in the current file, keeping current visibility.
5624 When you want look-up or go to a different location in a document, the
5625 fastest way is often to fold the entire buffer and then dive into the tree.
5626 This method has the disadvantage, that the previous location will be folded,
5627 which may not be what you want.
5629 This command works around this by showing a copy of the current buffer
5630 in an indirect buffer, in overview mode. You can dive into the tree in
5631 that copy, use org-occur and incremental search to find a location.
5632 When pressing RET or `Q', the command returns to the original buffer in
5633 which the visibility is still unchanged. After RET is will also jump to
5634 the location selected in the indirect buffer and expose the
5635 the headline hierarchy above."
5636 (interactive "P")
5637 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
5638 (org-refile-use-outline-path t)
5639 (org-refile-target-verify-function nil)
5640 (interface
5641 (if (not alternative-interface)
5642 org-goto-interface
5643 (if (eq org-goto-interface 'outline)
5644 'outline-path-completion
5645 'outline)))
5646 (org-goto-start-pos (point))
5647 (selected-point
5648 (if (eq interface 'outline)
5649 (car (org-get-location (current-buffer) org-goto-help))
5650 (nth 3 (org-refile-get-location "Goto: ")))))
5651 (if selected-point
5652 (progn
5653 (org-mark-ring-push org-goto-start-pos)
5654 (goto-char selected-point)
5655 (if (or (org-invisible-p) (org-invisible-p2))
5656 (org-show-context 'org-goto)))
5657 (message "Quit"))))
5659 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
5660 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
5661 (defvar org-goto-local-auto-isearch-map) ; defined below
5663 (defun org-get-location (buf help)
5664 "Let the user select a location in the Org-mode buffer BUF.
5665 This function uses a recursive edit. It returns the selected position
5666 or nil."
5667 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
5668 (isearch-hide-immediately nil)
5669 (isearch-search-fun-function
5670 (lambda () 'org-goto-local-search-headings))
5671 (org-goto-selected-point org-goto-exit-command))
5672 (save-excursion
5673 (save-window-excursion
5674 (delete-other-windows)
5675 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
5676 (switch-to-buffer
5677 (condition-case nil
5678 (make-indirect-buffer (current-buffer) "*org-goto*")
5679 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
5680 (with-output-to-temp-buffer "*Help*"
5681 (princ help))
5682 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
5683 (setq buffer-read-only nil)
5684 (let ((org-startup-truncated t)
5685 (org-startup-folded nil)
5686 (org-startup-align-all-tables nil))
5687 (org-mode)
5688 (org-overview))
5689 (setq buffer-read-only t)
5690 (if (and (boundp 'org-goto-start-pos)
5691 (integer-or-marker-p org-goto-start-pos))
5692 (let ((org-show-hierarchy-above t)
5693 (org-show-siblings t)
5694 (org-show-following-heading t))
5695 (goto-char org-goto-start-pos)
5696 (and (org-invisible-p) (org-show-context)))
5697 (goto-char (point-min)))
5698 (let (org-special-ctrl-a/e) (org-beginning-of-line))
5699 (message "Select location and press RET")
5700 (use-local-map org-goto-map)
5701 (recursive-edit)
5703 (kill-buffer "*org-goto*")
5704 (cons org-goto-selected-point org-goto-exit-command)))
5706 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
5707 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
5708 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
5709 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
5711 (defun org-goto-local-search-headings (string bound noerror)
5712 "Search and make sure that any matches are in headlines."
5713 (catch 'return
5714 (while (if isearch-forward
5715 (search-forward string bound noerror)
5716 (search-backward string bound noerror))
5717 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
5718 (and (member :headline context)
5719 (not (member :tags context))))
5720 (throw 'return (point))))))
5722 (defun org-goto-local-auto-isearch ()
5723 "Start isearch."
5724 (interactive)
5725 (goto-char (point-min))
5726 (let ((keys (this-command-keys)))
5727 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
5728 (isearch-mode t)
5729 (isearch-process-search-char (string-to-char keys)))))
5731 (defun org-goto-ret (&optional arg)
5732 "Finish `org-goto' by going to the new location."
5733 (interactive "P")
5734 (setq org-goto-selected-point (point)
5735 org-goto-exit-command 'return)
5736 (throw 'exit nil))
5738 (defun org-goto-left ()
5739 "Finish `org-goto' by going to the new location."
5740 (interactive)
5741 (if (org-on-heading-p)
5742 (progn
5743 (beginning-of-line 1)
5744 (setq org-goto-selected-point (point)
5745 org-goto-exit-command 'left)
5746 (throw 'exit nil))
5747 (error "Not on a heading")))
5749 (defun org-goto-right ()
5750 "Finish `org-goto' by going to the new location."
5751 (interactive)
5752 (if (org-on-heading-p)
5753 (progn
5754 (setq org-goto-selected-point (point)
5755 org-goto-exit-command 'right)
5756 (throw 'exit nil))
5757 (error "Not on a heading")))
5759 (defun org-goto-quit ()
5760 "Finish `org-goto' without cursor motion."
5761 (interactive)
5762 (setq org-goto-selected-point nil)
5763 (setq org-goto-exit-command 'quit)
5764 (throw 'exit nil))
5766 ;;; Indirect buffer display of subtrees
5768 (defvar org-indirect-dedicated-frame nil
5769 "This is the frame being used for indirect tree display.")
5770 (defvar org-last-indirect-buffer nil)
5772 (defun org-tree-to-indirect-buffer (&optional arg)
5773 "Create indirect buffer and narrow it to current subtree.
5774 With numerical prefix ARG, go up to this level and then take that tree.
5775 If ARG is negative, go up that many levels.
5776 If `org-indirect-buffer-display' is not `new-frame', the command removes the
5777 indirect buffer previously made with this command, to avoid proliferation of
5778 indirect buffers. However, when you call the command with a `C-u' prefix, or
5779 when `org-indirect-buffer-display' is `new-frame', the last buffer
5780 is kept so that you can work with several indirect buffers at the same time.
5781 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
5782 requests that a new frame be made for the new buffer, so that the dedicated
5783 frame is not changed."
5784 (interactive "P")
5785 (let ((cbuf (current-buffer))
5786 (cwin (selected-window))
5787 (pos (point))
5788 beg end level heading ibuf)
5789 (save-excursion
5790 (org-back-to-heading t)
5791 (when (numberp arg)
5792 (setq level (org-outline-level))
5793 (if (< arg 0) (setq arg (+ level arg)))
5794 (while (> (setq level (org-outline-level)) arg)
5795 (outline-up-heading 1 t)))
5796 (setq beg (point)
5797 heading (org-get-heading))
5798 (org-end-of-subtree t t) (setq end (point)))
5799 (if (and (buffer-live-p org-last-indirect-buffer)
5800 (not (eq org-indirect-buffer-display 'new-frame))
5801 (not arg))
5802 (kill-buffer org-last-indirect-buffer))
5803 (setq ibuf (org-get-indirect-buffer cbuf)
5804 org-last-indirect-buffer ibuf)
5805 (cond
5806 ((or (eq org-indirect-buffer-display 'new-frame)
5807 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
5808 (select-frame (make-frame))
5809 (delete-other-windows)
5810 (switch-to-buffer ibuf)
5811 (org-set-frame-title heading))
5812 ((eq org-indirect-buffer-display 'dedicated-frame)
5813 (raise-frame
5814 (select-frame (or (and org-indirect-dedicated-frame
5815 (frame-live-p org-indirect-dedicated-frame)
5816 org-indirect-dedicated-frame)
5817 (setq org-indirect-dedicated-frame (make-frame)))))
5818 (delete-other-windows)
5819 (switch-to-buffer ibuf)
5820 (org-set-frame-title (concat "Indirect: " heading)))
5821 ((eq org-indirect-buffer-display 'current-window)
5822 (switch-to-buffer ibuf))
5823 ((eq org-indirect-buffer-display 'other-window)
5824 (pop-to-buffer ibuf))
5825 (t (error "Invalid value")))
5826 (if (featurep 'xemacs)
5827 (save-excursion (org-mode) (turn-on-font-lock)))
5828 (narrow-to-region beg end)
5829 (show-all)
5830 (goto-char pos)
5831 (and (window-live-p cwin) (select-window cwin))))
5833 (defun org-get-indirect-buffer (&optional buffer)
5834 (setq buffer (or buffer (current-buffer)))
5835 (let ((n 1) (base (buffer-name buffer)) bname)
5836 (while (buffer-live-p
5837 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
5838 (setq n (1+ n)))
5839 (condition-case nil
5840 (make-indirect-buffer buffer bname 'clone)
5841 (error (make-indirect-buffer buffer bname)))))
5843 (defun org-set-frame-title (title)
5844 "Set the title of the current frame to the string TITLE."
5845 ;; FIXME: how to name a single frame in XEmacs???
5846 (unless (featurep 'xemacs)
5847 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
5849 ;;;; Structure editing
5851 ;;; Inserting headlines
5853 (defun org-previous-line-empty-p ()
5854 (save-excursion
5855 (and (not (bobp))
5856 (or (beginning-of-line 0) t)
5857 (save-match-data
5858 (looking-at "[ \t]*$")))))
5860 (defun org-insert-heading (&optional force-heading)
5861 "Insert a new heading or item with same depth at point.
5862 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
5863 If point is at the beginning of a headline, insert a sibling before the
5864 current headline. If point is not at the beginning, do not split the line,
5865 but create the new headline after the current line."
5866 (interactive "P")
5867 (if (= (buffer-size) 0)
5868 (insert "\n* ")
5869 (when (or force-heading (not (org-insert-item)))
5870 (let* ((empty-line-p nil)
5871 (head (save-excursion
5872 (condition-case nil
5873 (progn
5874 (org-back-to-heading)
5875 (setq empty-line-p (org-previous-line-empty-p))
5876 (match-string 0))
5877 (error "*"))))
5878 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
5879 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
5880 pos hide-previous previous-pos)
5881 (cond
5882 ((and (org-on-heading-p) (bolp)
5883 (or (bobp)
5884 (save-excursion (backward-char 1) (not (org-invisible-p)))))
5885 ;; insert before the current line
5886 (open-line (if blank 2 1)))
5887 ((and (bolp)
5888 (not org-insert-heading-respect-content)
5889 (or (bobp)
5890 (save-excursion
5891 (backward-char 1) (not (org-invisible-p)))))
5892 ;; insert right here
5893 nil)
5895 ;; somewhere in the line
5896 (save-excursion
5897 (setq previous-pos (point-at-bol))
5898 (end-of-line)
5899 (setq hide-previous (org-invisible-p)))
5900 (and org-insert-heading-respect-content (org-show-subtree))
5901 (let ((split
5902 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
5903 (save-excursion
5904 (let ((p (point)))
5905 (goto-char (point-at-bol))
5906 (and (looking-at org-complex-heading-regexp)
5907 (> p (match-beginning 4)))))))
5908 tags pos)
5909 (cond
5910 (org-insert-heading-respect-content
5911 (org-end-of-subtree nil t)
5912 (or (bolp) (newline))
5913 (or (org-previous-line-empty-p)
5914 (and blank (newline)))
5915 (open-line 1))
5916 ((org-on-heading-p)
5917 (when hide-previous
5918 (show-children)
5919 (org-show-entry))
5920 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
5921 (setq tags (and (match-end 2) (match-string 2)))
5922 (and (match-end 1)
5923 (delete-region (match-beginning 1) (match-end 1)))
5924 (setq pos (point-at-bol))
5925 (or split (end-of-line 1))
5926 (delete-horizontal-space)
5927 (newline (if blank 2 1))
5928 (when tags
5929 (save-excursion
5930 (goto-char pos)
5931 (end-of-line 1)
5932 (insert " " tags)
5933 (org-set-tags nil 'align))))
5935 (or split (end-of-line 1))
5936 (newline (if blank 2 1)))))))
5937 (insert head) (just-one-space)
5938 (setq pos (point))
5939 (end-of-line 1)
5940 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
5941 (when (and org-insert-heading-respect-content hide-previous)
5942 (save-excursion
5943 (goto-char previous-pos)
5944 (hide-subtree)))
5945 (run-hooks 'org-insert-heading-hook)))))
5947 (defun org-get-heading (&optional no-tags)
5948 "Return the heading of the current entry, without the stars."
5949 (save-excursion
5950 (org-back-to-heading t)
5951 (if (looking-at
5952 (if no-tags
5953 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
5954 "\\*+[ \t]+\\([^\r\n]*\\)"))
5955 (match-string 1) "")))
5957 (defun org-heading-components ()
5958 "Return the components of the current heading.
5959 This is a list with the following elements:
5960 - the level as an integer
5961 - the reduced level, different if `org-odd-levels-only' is set.
5962 - the TODO keyword, or nil
5963 - the priority character, like ?A, or nil if no priority is given
5964 - the headline text itself, or the tags string if no headline text
5965 - the tags string, or nil."
5966 (save-excursion
5967 (org-back-to-heading t)
5968 (if (looking-at org-complex-heading-regexp)
5969 (list (length (match-string 1))
5970 (org-reduced-level (length (match-string 1)))
5971 (org-match-string-no-properties 2)
5972 (and (match-end 3) (aref (match-string 3) 2))
5973 (org-match-string-no-properties 4)
5974 (org-match-string-no-properties 5)))))
5976 (defun org-get-entry ()
5977 "Get the entry text, after heading, entire subtree."
5978 (save-excursion
5979 (org-back-to-heading t)
5980 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
5982 (defun org-insert-heading-after-current ()
5983 "Insert a new heading with same level as current, after current subtree."
5984 (interactive)
5985 (org-back-to-heading)
5986 (org-insert-heading)
5987 (org-move-subtree-down)
5988 (end-of-line 1))
5990 (defun org-insert-heading-respect-content ()
5991 (interactive)
5992 (let ((org-insert-heading-respect-content t))
5993 (org-insert-heading t)))
5995 (defun org-insert-todo-heading-respect-content (&optional force-state)
5996 (interactive "P")
5997 (let ((org-insert-heading-respect-content t))
5998 (org-insert-todo-heading force-state t)))
6000 (defun org-insert-todo-heading (arg &optional force-heading)
6001 "Insert a new heading with the same level and TODO state as current heading.
6002 If the heading has no TODO state, or if the state is DONE, use the first
6003 state (TODO by default). Also with prefix arg, force first state."
6004 (interactive "P")
6005 (when (or force-heading (not (org-insert-item 'checkbox)))
6006 (org-insert-heading force-heading)
6007 (save-excursion
6008 (org-back-to-heading)
6009 (outline-previous-heading)
6010 (looking-at org-todo-line-regexp))
6011 (let*
6012 ((new-mark-x
6013 (if (or arg
6014 (not (match-beginning 2))
6015 (member (match-string 2) org-done-keywords))
6016 (car org-todo-keywords-1)
6017 (match-string 2)))
6018 (new-mark
6020 (run-hook-with-args-until-success
6021 'org-todo-get-default-hook new-mark-x nil)
6022 new-mark-x)))
6023 (beginning-of-line 1)
6024 (and (looking-at "\\*+ ") (goto-char (match-end 0))
6025 (if org-treat-insert-todo-heading-as-state-change
6026 (org-todo new-mark)
6027 (insert new-mark " "))))
6028 (when org-provide-todo-statistics
6029 (org-update-parent-todo-statistics))))
6031 (defun org-insert-subheading (arg)
6032 "Insert a new subheading and demote it.
6033 Works for outline headings and for plain lists alike."
6034 (interactive "P")
6035 (org-insert-heading arg)
6036 (cond
6037 ((org-on-heading-p) (org-do-demote))
6038 ((org-at-item-p) (org-indent-item 1))))
6040 (defun org-insert-todo-subheading (arg)
6041 "Insert a new subheading with TODO keyword or checkbox and demote it.
6042 Works for outline headings and for plain lists alike."
6043 (interactive "P")
6044 (org-insert-todo-heading arg)
6045 (cond
6046 ((org-on-heading-p) (org-do-demote))
6047 ((org-at-item-p) (org-indent-item 1))))
6049 ;;; Promotion and Demotion
6051 (defvar org-after-demote-entry-hook nil
6052 "Hook run after an entry has been demoted.
6053 The cursor will be at the beginning of the entry.
6054 When a subtree is being demoted, the hook will be called for each node.")
6056 (defvar org-after-promote-entry-hook nil
6057 "Hook run after an entry has been promoted.
6058 The cursor will be at the beginning of the entry.
6059 When a subtree is being promoted, the hook will be called for each node.")
6061 (defun org-promote-subtree ()
6062 "Promote the entire subtree.
6063 See also `org-promote'."
6064 (interactive)
6065 (save-excursion
6066 (org-map-tree 'org-promote))
6067 (org-fix-position-after-promote))
6069 (defun org-demote-subtree ()
6070 "Demote the entire subtree. See `org-demote'.
6071 See also `org-promote'."
6072 (interactive)
6073 (save-excursion
6074 (org-map-tree 'org-demote))
6075 (org-fix-position-after-promote))
6078 (defun org-do-promote ()
6079 "Promote the current heading higher up the tree.
6080 If the region is active in `transient-mark-mode', promote all headings
6081 in the region."
6082 (interactive)
6083 (save-excursion
6084 (if (org-region-active-p)
6085 (org-map-region 'org-promote (region-beginning) (region-end))
6086 (org-promote)))
6087 (org-fix-position-after-promote))
6089 (defun org-do-demote ()
6090 "Demote the current heading lower down the tree.
6091 If the region is active in `transient-mark-mode', demote all headings
6092 in the region."
6093 (interactive)
6094 (save-excursion
6095 (if (org-region-active-p)
6096 (org-map-region 'org-demote (region-beginning) (region-end))
6097 (org-demote)))
6098 (org-fix-position-after-promote))
6100 (defun org-fix-position-after-promote ()
6101 "Make sure that after pro/demotion cursor position is right."
6102 (let ((pos (point)))
6103 (when (save-excursion
6104 (beginning-of-line 1)
6105 (looking-at org-todo-line-regexp)
6106 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
6107 (cond ((eobp) (insert " "))
6108 ((eolp) (insert " "))
6109 ((equal (char-after) ?\ ) (forward-char 1))))))
6111 (defun org-current-level ()
6112 "Return the level of the current entry, or nil if before the first headline.
6113 The level is the number of stars at the beginning of the headline."
6114 (save-excursion
6115 (condition-case nil
6116 (progn
6117 (org-back-to-heading t)
6118 (funcall outline-level))
6119 (error nil))))
6121 (defun org-reduced-level (l)
6122 "Compute the effective level of a heading.
6123 This takes into account the setting of `org-odd-levels-only'."
6124 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
6126 (defun org-get-valid-level (level &optional change)
6127 "Rectify a level change under the influence of `org-odd-levels-only'
6128 LEVEL is a current level, CHANGE is by how much the level should be
6129 modified. Even if CHANGE is nil, LEVEL may be returned modified because
6130 even level numbers will become the next higher odd number."
6131 (if org-odd-levels-only
6132 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
6133 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
6134 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
6135 (max 1 (+ level (or change 0)))))
6137 (if (boundp 'define-obsolete-function-alias)
6138 (if (or (featurep 'xemacs) (< emacs-major-version 23))
6139 (define-obsolete-function-alias 'org-get-legal-level
6140 'org-get-valid-level)
6141 (define-obsolete-function-alias 'org-get-legal-level
6142 'org-get-valid-level "23.1")))
6144 (defun org-promote ()
6145 "Promote the current heading higher up the tree.
6146 If the region is active in `transient-mark-mode', promote all headings
6147 in the region."
6148 (org-back-to-heading t)
6149 (let* ((level (save-match-data (funcall outline-level)))
6150 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
6151 (diff (abs (- level (length up-head) -1))))
6152 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
6153 (replace-match up-head nil t)
6154 ;; Fixup tag positioning
6155 (and org-auto-align-tags (org-set-tags nil t))
6156 (if org-adapt-indentation (org-fixup-indentation (- diff)))
6157 (run-hooks 'org-after-promote-entry-hook)))
6159 (defun org-demote ()
6160 "Demote the current heading lower down the tree.
6161 If the region is active in `transient-mark-mode', demote all headings
6162 in the region."
6163 (org-back-to-heading t)
6164 (let* ((level (save-match-data (funcall outline-level)))
6165 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
6166 (diff (abs (- level (length down-head) -1))))
6167 (replace-match down-head nil t)
6168 ;; Fixup tag positioning
6169 (and org-auto-align-tags (org-set-tags nil t))
6170 (if org-adapt-indentation (org-fixup-indentation diff))
6171 (run-hooks 'org-after-demote-entry-hook)))
6173 (defvar org-tab-ind-state nil)
6175 (defun org-cycle-level ()
6176 (let ((org-adapt-indentation nil))
6177 (when (and (looking-at "[ \t]*$")
6178 (looking-back
6179 (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp "\\)?[ \t]*")))
6180 (setq this-command 'org-cycle-level)
6181 (if (eq last-command 'org-cycle-level)
6182 (condition-case nil
6183 (progn (org-do-promote)
6184 (if (equal org-tab-ind-state (org-current-level))
6185 (org-do-promote)))
6186 (error
6187 (progn
6188 (save-excursion
6189 (beginning-of-line 1)
6190 (and (looking-at "\\*+")
6191 (replace-match
6192 (make-string org-tab-ind-state ?*))))
6193 (setq this-command 'org-cycle))))
6194 (setq org-tab-ind-state (- (match-end 1) (match-beginning 1)))
6195 (org-do-demote))
6196 t)))
6198 (defun org-map-tree (fun)
6199 "Call FUN for every heading underneath the current one."
6200 (org-back-to-heading)
6201 (let ((level (funcall outline-level)))
6202 (save-excursion
6203 (funcall fun)
6204 (while (and (progn
6205 (outline-next-heading)
6206 (> (funcall outline-level) level))
6207 (not (eobp)))
6208 (funcall fun)))))
6210 (defun org-map-region (fun beg end)
6211 "Call FUN for every heading between BEG and END."
6212 (let ((org-ignore-region t))
6213 (save-excursion
6214 (setq end (copy-marker end))
6215 (goto-char beg)
6216 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
6217 (< (point) end))
6218 (funcall fun))
6219 (while (and (progn
6220 (outline-next-heading)
6221 (< (point) end))
6222 (not (eobp)))
6223 (funcall fun)))))
6225 (defun org-fixup-indentation (diff)
6226 "Change the indentation in the current entry by DIFF
6227 However, if any line in the current entry has no indentation, or if it
6228 would end up with no indentation after the change, nothing at all is done."
6229 (save-excursion
6230 (let ((end (save-excursion (outline-next-heading)
6231 (point-marker)))
6232 (prohibit (if (> diff 0)
6233 "^\\S-"
6234 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
6235 col)
6236 (unless (save-excursion (end-of-line 1)
6237 (re-search-forward prohibit end t))
6238 (while (and (< (point) end)
6239 (re-search-forward "^[ \t]+" end t))
6240 (goto-char (match-end 0))
6241 (setq col (current-column))
6242 (if (< diff 0) (replace-match ""))
6243 (org-indent-to-column (+ diff col))))
6244 (move-marker end nil))))
6246 (defun org-convert-to-odd-levels ()
6247 "Convert an org-mode file with all levels allowed to one with odd levels.
6248 This will leave level 1 alone, convert level 2 to level 3, level 3 to
6249 level 5 etc."
6250 (interactive)
6251 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
6252 (let ((outline-regexp org-outline-regexp)
6253 (outline-level 'org-outline-level)
6254 (org-odd-levels-only nil) n)
6255 (save-excursion
6256 (goto-char (point-min))
6257 (while (re-search-forward "^\\*\\*+ " nil t)
6258 (setq n (- (length (match-string 0)) 2))
6259 (while (>= (setq n (1- n)) 0)
6260 (org-demote))
6261 (end-of-line 1))))))
6263 (defun org-convert-to-oddeven-levels ()
6264 "Convert an org-mode file with only odd levels to one with odd and even levels.
6265 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
6266 section with an even level, conversion would destroy the structure of the file. An error
6267 is signaled in this case."
6268 (interactive)
6269 (goto-char (point-min))
6270 ;; First check if there are no even levels
6271 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
6272 (org-show-context t)
6273 (error "Not all levels are odd in this file. Conversion not possible"))
6274 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
6275 (let ((outline-regexp org-outline-regexp)
6276 (outline-level 'org-outline-level)
6277 (org-odd-levels-only nil) n)
6278 (save-excursion
6279 (goto-char (point-min))
6280 (while (re-search-forward "^\\*\\*+ " nil t)
6281 (setq n (/ (1- (length (match-string 0))) 2))
6282 (while (>= (setq n (1- n)) 0)
6283 (org-promote))
6284 (end-of-line 1))))))
6286 (defun org-tr-level (n)
6287 "Make N odd if required."
6288 (if org-odd-levels-only (1+ (/ n 2)) n))
6290 ;;; Vertical tree motion, cutting and pasting of subtrees
6292 (defun org-move-subtree-up (&optional arg)
6293 "Move the current subtree up past ARG headlines of the same level."
6294 (interactive "p")
6295 (org-move-subtree-down (- (prefix-numeric-value arg))))
6297 (defun org-move-subtree-down (&optional arg)
6298 "Move the current subtree down past ARG headlines of the same level."
6299 (interactive "p")
6300 (setq arg (prefix-numeric-value arg))
6301 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
6302 'org-get-last-sibling))
6303 (ins-point (make-marker))
6304 (cnt (abs arg))
6305 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
6306 ;; Select the tree
6307 (org-back-to-heading)
6308 (setq beg0 (point))
6309 (save-excursion
6310 (setq ne-beg (org-back-over-empty-lines))
6311 (setq beg (point)))
6312 (save-match-data
6313 (save-excursion (outline-end-of-heading)
6314 (setq folded (org-invisible-p)))
6315 (outline-end-of-subtree))
6316 (outline-next-heading)
6317 (setq ne-end (org-back-over-empty-lines))
6318 (setq end (point))
6319 (goto-char beg0)
6320 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
6321 ;; include less whitespace
6322 (save-excursion
6323 (goto-char beg)
6324 (forward-line (- ne-beg ne-end))
6325 (setq beg (point))))
6326 ;; Find insertion point, with error handling
6327 (while (> cnt 0)
6328 (or (and (funcall movfunc) (looking-at outline-regexp))
6329 (progn (goto-char beg0)
6330 (error "Cannot move past superior level or buffer limit")))
6331 (setq cnt (1- cnt)))
6332 (if (> arg 0)
6333 ;; Moving forward - still need to move over subtree
6334 (progn (org-end-of-subtree t t)
6335 (save-excursion
6336 (org-back-over-empty-lines)
6337 (or (bolp) (newline)))))
6338 (setq ne-ins (org-back-over-empty-lines))
6339 (move-marker ins-point (point))
6340 (setq txt (buffer-substring beg end))
6341 (org-save-markers-in-region beg end)
6342 (delete-region beg end)
6343 (org-remove-empty-overlays-at beg)
6344 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
6345 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
6346 (and (not (bolp)) (looking-at "\n") (forward-char 1))
6347 (let ((bbb (point)))
6348 (insert-before-markers txt)
6349 (org-reinstall-markers-in-region bbb)
6350 (move-marker ins-point bbb))
6351 (or (bolp) (insert "\n"))
6352 (setq ins-end (point))
6353 (goto-char ins-point)
6354 (org-skip-whitespace)
6355 (when (and (< arg 0)
6356 (org-first-sibling-p)
6357 (> ne-ins ne-beg))
6358 ;; Move whitespace back to beginning
6359 (save-excursion
6360 (goto-char ins-end)
6361 (let ((kill-whole-line t))
6362 (kill-line (- ne-ins ne-beg)) (point)))
6363 (insert (make-string (- ne-ins ne-beg) ?\n)))
6364 (move-marker ins-point nil)
6365 (if folded
6366 (hide-subtree)
6367 (org-show-entry)
6368 (show-children)
6369 (org-cycle-hide-drawers 'children))
6370 (org-clean-visibility-after-subtree-move)))
6372 (defvar org-subtree-clip ""
6373 "Clipboard for cut and paste of subtrees.
6374 This is actually only a copy of the kill, because we use the normal kill
6375 ring. We need it to check if the kill was created by `org-copy-subtree'.")
6377 (defvar org-subtree-clip-folded nil
6378 "Was the last copied subtree folded?
6379 This is used to fold the tree back after pasting.")
6381 (defun org-cut-subtree (&optional n)
6382 "Cut the current subtree into the clipboard.
6383 With prefix arg N, cut this many sequential subtrees.
6384 This is a short-hand for marking the subtree and then cutting it."
6385 (interactive "p")
6386 (org-copy-subtree n 'cut))
6388 (defun org-copy-subtree (&optional n cut force-store-markers)
6389 "Cut the current subtree into the clipboard.
6390 With prefix arg N, cut this many sequential subtrees.
6391 This is a short-hand for marking the subtree and then copying it.
6392 If CUT is non-nil, actually cut the subtree.
6393 If FORCE-STORE-MARKERS is non-nil, store the relative locations
6394 of some markers in the region, even if CUT is non-nil. This is
6395 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
6396 (interactive "p")
6397 (let (beg end folded (beg0 (point)))
6398 (if (interactive-p)
6399 (org-back-to-heading nil) ; take what looks like a subtree
6400 (org-back-to-heading t)) ; take what is really there
6401 (org-back-over-empty-lines)
6402 (setq beg (point))
6403 (skip-chars-forward " \t\r\n")
6404 (save-match-data
6405 (save-excursion (outline-end-of-heading)
6406 (setq folded (org-invisible-p)))
6407 (condition-case nil
6408 (org-forward-same-level (1- n) t)
6409 (error nil))
6410 (org-end-of-subtree t t))
6411 (org-back-over-empty-lines)
6412 (setq end (point))
6413 (goto-char beg0)
6414 (when (> end beg)
6415 (setq org-subtree-clip-folded folded)
6416 (when (or cut force-store-markers)
6417 (org-save-markers-in-region beg end))
6418 (if cut (kill-region beg end) (copy-region-as-kill beg end))
6419 (setq org-subtree-clip (current-kill 0))
6420 (message "%s: Subtree(s) with %d characters"
6421 (if cut "Cut" "Copied")
6422 (length org-subtree-clip)))))
6424 (defun org-paste-subtree (&optional level tree for-yank)
6425 "Paste the clipboard as a subtree, with modification of headline level.
6426 The entire subtree is promoted or demoted in order to match a new headline
6427 level.
6429 If the cursor is at the beginning of a headline, the same level as
6430 that headline is used to paste the tree
6432 If not, the new level is derived from the *visible* headings
6433 before and after the insertion point, and taken to be the inferior headline
6434 level of the two. So if the previous visible heading is level 3 and the
6435 next is level 4 (or vice versa), level 4 will be used for insertion.
6436 This makes sure that the subtree remains an independent subtree and does
6437 not swallow low level entries.
6439 You can also force a different level, either by using a numeric prefix
6440 argument, or by inserting the heading marker by hand. For example, if the
6441 cursor is after \"*****\", then the tree will be shifted to level 5.
6443 If optional TREE is given, use this text instead of the kill ring.
6445 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
6446 move back over whitespace before inserting, and move point to the end of
6447 the inserted text when done."
6448 (interactive "P")
6449 (setq tree (or tree (and kill-ring (current-kill 0))))
6450 (unless (org-kill-is-subtree-p tree)
6451 (error "%s"
6452 (substitute-command-keys
6453 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
6454 (let* ((visp (not (org-invisible-p)))
6455 (txt tree)
6456 (^re (concat "^\\(" outline-regexp "\\)"))
6457 (re (concat "\\(" outline-regexp "\\)"))
6458 (^re_ (concat "\\(\\*+\\)[ \t]*"))
6460 (old-level (if (string-match ^re txt)
6461 (- (match-end 0) (match-beginning 0) 1)
6462 -1))
6463 (force-level (cond (level (prefix-numeric-value level))
6464 ((and (looking-at "[ \t]*$")
6465 (string-match
6466 ^re_ (buffer-substring
6467 (point-at-bol) (point))))
6468 (- (match-end 1) (match-beginning 1)))
6469 ((and (bolp)
6470 (looking-at org-outline-regexp))
6471 (- (match-end 0) (point) 1))
6472 (t nil)))
6473 (previous-level (save-excursion
6474 (condition-case nil
6475 (progn
6476 (outline-previous-visible-heading 1)
6477 (if (looking-at re)
6478 (- (match-end 0) (match-beginning 0) 1)
6480 (error 1))))
6481 (next-level (save-excursion
6482 (condition-case nil
6483 (progn
6484 (or (looking-at outline-regexp)
6485 (outline-next-visible-heading 1))
6486 (if (looking-at re)
6487 (- (match-end 0) (match-beginning 0) 1)
6489 (error 1))))
6490 (new-level (or force-level (max previous-level next-level)))
6491 (shift (if (or (= old-level -1)
6492 (= new-level -1)
6493 (= old-level new-level))
6495 (- new-level old-level)))
6496 (delta (if (> shift 0) -1 1))
6497 (func (if (> shift 0) 'org-demote 'org-promote))
6498 (org-odd-levels-only nil)
6499 beg end newend)
6500 ;; Remove the forced level indicator
6501 (if force-level
6502 (delete-region (point-at-bol) (point)))
6503 ;; Paste
6504 (beginning-of-line 1)
6505 (unless for-yank (org-back-over-empty-lines))
6506 (setq beg (point))
6507 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
6508 (insert-before-markers txt)
6509 (unless (string-match "\n\\'" txt) (insert "\n"))
6510 (setq newend (point))
6511 (org-reinstall-markers-in-region beg)
6512 (setq end (point))
6513 (goto-char beg)
6514 (skip-chars-forward " \t\n\r")
6515 (setq beg (point))
6516 (if (and (org-invisible-p) visp)
6517 (save-excursion (outline-show-heading)))
6518 ;; Shift if necessary
6519 (unless (= shift 0)
6520 (save-restriction
6521 (narrow-to-region beg end)
6522 (while (not (= shift 0))
6523 (org-map-region func (point-min) (point-max))
6524 (setq shift (+ delta shift)))
6525 (goto-char (point-min))
6526 (setq newend (point-max))))
6527 (when (or (interactive-p) for-yank)
6528 (message "Clipboard pasted as level %d subtree" new-level))
6529 (if (and (not for-yank) ; in this case, org-yank will decide about folding
6530 kill-ring
6531 (eq org-subtree-clip (current-kill 0))
6532 org-subtree-clip-folded)
6533 ;; The tree was folded before it was killed/copied
6534 (hide-subtree))
6535 (and for-yank (goto-char newend))))
6537 (defun org-kill-is-subtree-p (&optional txt)
6538 "Check if the current kill is an outline subtree, or a set of trees.
6539 Returns nil if kill does not start with a headline, or if the first
6540 headline level is not the largest headline level in the tree.
6541 So this will actually accept several entries of equal levels as well,
6542 which is OK for `org-paste-subtree'.
6543 If optional TXT is given, check this string instead of the current kill."
6544 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
6545 (start-level (and kill
6546 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
6547 org-outline-regexp "\\)")
6548 kill)
6549 (- (match-end 2) (match-beginning 2) 1)))
6550 (re (concat "^" org-outline-regexp))
6551 (start (1+ (or (match-beginning 2) -1))))
6552 (if (not start-level)
6553 (progn
6554 nil) ;; does not even start with a heading
6555 (catch 'exit
6556 (while (setq start (string-match re kill (1+ start)))
6557 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
6558 (throw 'exit nil)))
6559 t))))
6561 (defvar org-markers-to-move nil
6562 "Markers that should be moved with a cut-and-paste operation.
6563 Those markers are stored together with their positions relative to
6564 the start of the region.")
6566 (defun org-save-markers-in-region (beg end)
6567 "Check markers in region.
6568 If these markers are between BEG and END, record their position relative
6569 to BEG, so that after moving the block of text, we can put the markers back
6570 into place.
6571 This function gets called just before an entry or tree gets cut from the
6572 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
6573 called immediately, to move the markers with the entries."
6574 (setq org-markers-to-move nil)
6575 (when (featurep 'org-clock)
6576 (org-clock-save-markers-for-cut-and-paste beg end))
6577 (when (featurep 'org-agenda)
6578 (org-agenda-save-markers-for-cut-and-paste beg end)))
6580 (defun org-check-and-save-marker (marker beg end)
6581 "Check if MARKER is between BEG and END.
6582 If yes, remember the marker and the distance to BEG."
6583 (when (and (marker-buffer marker)
6584 (equal (marker-buffer marker) (current-buffer)))
6585 (if (and (>= marker beg) (< marker end))
6586 (push (cons marker (- marker beg)) org-markers-to-move))))
6588 (defun org-reinstall-markers-in-region (beg)
6589 "Move all remembered markers to their position relative to BEG."
6590 (mapc (lambda (x)
6591 (move-marker (car x) (+ beg (cdr x))))
6592 org-markers-to-move)
6593 (setq org-markers-to-move nil))
6595 (defun org-narrow-to-subtree ()
6596 "Narrow buffer to the current subtree."
6597 (interactive)
6598 (save-excursion
6599 (save-match-data
6600 (narrow-to-region
6601 (progn (org-back-to-heading t) (point))
6602 (progn (org-end-of-subtree t t) (point))))))
6604 (defun org-clone-subtree-with-time-shift (n &optional shift)
6605 "Clone the task (subtree) at point N times.
6606 The clones will be inserted as siblings.
6608 In interactive use, the user will be prompted for the number of clones
6609 to be produced, and for a time SHIFT, which may be a repeater as used
6610 in time stamps, for example `+3d'.
6612 When a valid repeater is given and the entry contains any time stamps,
6613 the clones will become a sequence in time, with time stamps in the
6614 subtree shifted for each clone produced. If SHIFT is nil or the
6615 empty string, time stamps will be left alone.
6617 If the original subtree did contain time stamps with a repeater,
6618 the following will happen:
6619 - the repeater will be removed in each clone
6620 - an additional clone will be produced, with the current, unshifted
6621 date(s) in the entry.
6622 - the original entry will be placed *after* all the clones, with
6623 repeater intact.
6624 - the start days in the repeater in the original entry will be shifted
6625 to past the last clone.
6626 I this way you can spell out a number of instances of a repeating task,
6627 and still retain the repeater to cover future instances of the task."
6628 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
6629 (let (beg end template task
6630 shift-n shift-what doshift nmin nmax (n-no-remove -1))
6631 (if (not (and (integerp n) (> n 0)))
6632 (error "Invalid number of replications %s" n))
6633 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
6634 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
6635 shift)))
6636 (error "Invalid shift specification %s" shift))
6637 (when doshift
6638 (setq shift-n (string-to-number (match-string 1 shift))
6639 shift-what (cdr (assoc (match-string 2 shift)
6640 '(("d" . day) ("w" . week)
6641 ("m" . month) ("y" . year))))))
6642 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
6643 (setq nmin 1 nmax n)
6644 (org-back-to-heading t)
6645 (setq beg (point))
6646 (org-end-of-subtree t t)
6647 (setq end (point))
6648 (setq template (buffer-substring beg end))
6649 (when (and doshift
6650 (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
6651 (delete-region beg end)
6652 (setq end beg)
6653 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
6654 (goto-char end)
6655 (loop for n from nmin to nmax do
6656 (if (not doshift)
6657 (setq task template)
6658 (with-temp-buffer
6659 (insert template)
6660 (org-mode)
6661 (goto-char (point-min))
6662 (while (re-search-forward org-ts-regexp-both nil t)
6663 (org-timestamp-change (* n shift-n) shift-what))
6664 (unless (= n n-no-remove)
6665 (goto-char (point-min))
6666 (while (re-search-forward org-ts-regexp nil t)
6667 (save-excursion
6668 (goto-char (match-beginning 0))
6669 (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
6670 (delete-region (match-beginning 1) (match-end 1))))))
6671 (setq task (buffer-string))))
6672 (insert task))
6673 (goto-char beg)))
6675 ;;; Outline Sorting
6677 (defun org-sort (with-case)
6678 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
6679 Optional argument WITH-CASE means sort case-sensitively.
6680 With a double prefix argument, also remove duplicate entries."
6681 (interactive "P")
6682 (if (org-at-table-p)
6683 (org-call-with-arg 'org-table-sort-lines with-case)
6684 (org-call-with-arg 'org-sort-entries-or-items with-case)))
6686 (defun org-sort-remove-invisible (s)
6687 (remove-text-properties 0 (length s) org-rm-props s)
6688 (while (string-match org-bracket-link-regexp s)
6689 (setq s (replace-match (if (match-end 2)
6690 (match-string 3 s)
6691 (match-string 1 s)) t t s)))
6694 (defvar org-priority-regexp) ; defined later in the file
6696 (defvar org-after-sorting-entries-or-items-hook nil
6697 "Hook that is run after a bunch of entries or items have been sorted.
6698 When children are sorted, the cursor is in the parent line when this
6699 hook gets called. When a region or a plain list is sorted, the cursor
6700 will be in the first entry of the sorted region/list.")
6702 (defun org-sort-entries-or-items
6703 (&optional with-case sorting-type getkey-func compare-func property)
6704 "Sort entries on a certain level of an outline tree, or plain list items.
6705 If there is an active region, the entries in the region are sorted.
6706 Else, if the cursor is before the first entry, sort the top-level items.
6707 Else, the children of the entry at point are sorted.
6708 If the cursor is at the first item in a plain list, the list items will be
6709 sorted.
6711 Sorting can be alphabetically, numerically, by date/time as given by
6712 a time stamp, by a property or by priority.
6714 The command prompts for the sorting type unless it has been given to the
6715 function through the SORTING-TYPE argument, which needs to a character,
6716 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
6717 precise meaning of each character:
6719 n Numerically, by converting the beginning of the entry/item to a number.
6720 a Alphabetically, ignoring the TODO keyword and the priority, if any.
6721 t By date/time, either the first active time stamp in the entry, or, if
6722 none exist, by the first inactive one.
6723 In items, only the first line will be chekced.
6724 s By the scheduled date/time.
6725 d By deadline date/time.
6726 c By creation time, which is assumed to be the first inactive time stamp
6727 at the beginning of a line.
6728 p By priority according to the cookie.
6729 r By the value of a property.
6731 Capital letters will reverse the sort order.
6733 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
6734 called with point at the beginning of the record. It must return either
6735 a string or a number that should serve as the sorting key for that record.
6737 Comparing entries ignores case by default. However, with an optional argument
6738 WITH-CASE, the sorting considers case as well."
6739 (interactive "P")
6740 (let ((case-func (if with-case 'identity 'downcase))
6741 start beg end stars re re2
6742 txt what tmp plain-list-p)
6743 ;; Find beginning and end of region to sort
6744 (cond
6745 ((org-region-active-p)
6746 ;; we will sort the region
6747 (setq end (region-end)
6748 what "region")
6749 (goto-char (region-beginning))
6750 (if (not (org-on-heading-p)) (outline-next-heading))
6751 (setq start (point)))
6752 ((org-at-item-p)
6753 ;; we will sort this plain list
6754 (org-beginning-of-item-list) (setq start (point))
6755 (org-end-of-item-list) (setq end (point))
6756 (goto-char start)
6757 (setq plain-list-p t
6758 what "plain list"))
6759 ((or (org-on-heading-p)
6760 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
6761 ;; we will sort the children of the current headline
6762 (org-back-to-heading)
6763 (setq start (point)
6764 end (progn (org-end-of-subtree t t)
6765 (org-back-over-empty-lines)
6766 (point))
6767 what "children")
6768 (goto-char start)
6769 (show-subtree)
6770 (outline-next-heading))
6772 ;; we will sort the top-level entries in this file
6773 (goto-char (point-min))
6774 (or (org-on-heading-p) (outline-next-heading))
6775 (setq start (point) end (point-max) what "top-level")
6776 (goto-char start)
6777 (show-all)))
6779 (setq beg (point))
6780 (if (>= beg end) (error "Nothing to sort"))
6782 (unless plain-list-p
6783 (looking-at "\\(\\*+\\)")
6784 (setq stars (match-string 1)
6785 re (concat "^" (regexp-quote stars) " +")
6786 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
6787 txt (buffer-substring beg end))
6788 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
6789 (if (and (not (equal stars "*")) (string-match re2 txt))
6790 (error "Region to sort contains a level above the first entry")))
6792 (unless sorting-type
6793 (message
6794 (if plain-list-p
6795 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
6796 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
6797 [t]ime [s]cheduled [d]eadline [c]reated
6798 A/N/T/S/D/C/P/O/F means reversed:")
6799 what)
6800 (setq sorting-type (read-char-exclusive))
6802 (and (= (downcase sorting-type) ?f)
6803 (setq getkey-func
6804 (org-icompleting-read "Sort using function: "
6805 obarray 'fboundp t nil nil))
6806 (setq getkey-func (intern getkey-func)))
6808 (and (= (downcase sorting-type) ?r)
6809 (setq property
6810 (org-icompleting-read "Property: "
6811 (mapcar 'list (org-buffer-property-keys t))
6812 nil t))))
6814 (message "Sorting entries...")
6816 (save-restriction
6817 (narrow-to-region start end)
6819 (let ((dcst (downcase sorting-type))
6820 (case-fold-search nil)
6821 (now (current-time)))
6822 (sort-subr
6823 (/= dcst sorting-type)
6824 ;; This function moves to the beginning character of the "record" to
6825 ;; be sorted.
6826 (if plain-list-p
6827 (lambda nil
6828 (if (org-at-item-p) t (goto-char (point-max))))
6829 (lambda nil
6830 (if (re-search-forward re nil t)
6831 (goto-char (match-beginning 0))
6832 (goto-char (point-max)))))
6833 ;; This function moves to the last character of the "record" being
6834 ;; sorted.
6835 (if plain-list-p
6836 'org-end-of-item
6837 (lambda nil
6838 (save-match-data
6839 (condition-case nil
6840 (outline-forward-same-level 1)
6841 (error
6842 (goto-char (point-max)))))))
6844 ;; This function returns the value that gets sorted against.
6845 (if plain-list-p
6846 (lambda nil
6847 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
6848 (cond
6849 ((= dcst ?n)
6850 (string-to-number (buffer-substring (match-end 0)
6851 (point-at-eol))))
6852 ((= dcst ?a)
6853 (buffer-substring (match-end 0) (point-at-eol)))
6854 ((= dcst ?t)
6855 (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
6856 (re-search-forward org-ts-regexp-both
6857 (point-at-eol) t))
6858 (org-time-string-to-seconds (match-string 0))
6859 (org-float-time now)))
6860 ((= dcst ?f)
6861 (if getkey-func
6862 (progn
6863 (setq tmp (funcall getkey-func))
6864 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
6865 tmp)
6866 (error "Invalid key function `%s'" getkey-func)))
6867 (t (error "Invalid sorting type `%c'" sorting-type)))))
6868 (lambda nil
6869 (cond
6870 ((= dcst ?n)
6871 (if (looking-at org-complex-heading-regexp)
6872 (string-to-number (match-string 4))
6873 nil))
6874 ((= dcst ?a)
6875 (if (looking-at org-complex-heading-regexp)
6876 (funcall case-func (match-string 4))
6877 nil))
6878 ((= dcst ?t)
6879 (let ((end (save-excursion (outline-next-heading) (point))))
6880 (if (or (re-search-forward org-ts-regexp end t)
6881 (re-search-forward org-ts-regexp-both end t))
6882 (org-time-string-to-seconds (match-string 0))
6883 (org-float-time now))))
6884 ((= dcst ?c)
6885 (let ((end (save-excursion (outline-next-heading) (point))))
6886 (if (re-search-forward
6887 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
6888 end t)
6889 (org-time-string-to-seconds (match-string 0))
6890 (org-float-time now))))
6891 ((= dcst ?s)
6892 (let ((end (save-excursion (outline-next-heading) (point))))
6893 (if (re-search-forward org-scheduled-time-regexp end t)
6894 (org-time-string-to-seconds (match-string 1))
6895 (org-float-time now))))
6896 ((= dcst ?d)
6897 (let ((end (save-excursion (outline-next-heading) (point))))
6898 (if (re-search-forward org-deadline-time-regexp end t)
6899 (org-time-string-to-seconds (match-string 1))
6900 (org-float-time now))))
6901 ((= dcst ?p)
6902 (if (re-search-forward org-priority-regexp (point-at-eol) t)
6903 (string-to-char (match-string 2))
6904 org-default-priority))
6905 ((= dcst ?r)
6906 (or (org-entry-get nil property) ""))
6907 ((= dcst ?o)
6908 (if (looking-at org-complex-heading-regexp)
6909 (- 9999 (length (member (match-string 2)
6910 org-todo-keywords-1)))))
6911 ((= dcst ?f)
6912 (if getkey-func
6913 (progn
6914 (setq tmp (funcall getkey-func))
6915 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
6916 tmp)
6917 (error "Invalid key function `%s'" getkey-func)))
6918 (t (error "Invalid sorting type `%c'" sorting-type)))))
6920 (cond
6921 ((= dcst ?a) 'string<)
6922 ((= dcst ?f) compare-func)
6923 ((member dcst '(?p ?t ?s ?d ?c)) '<)
6924 (t nil)))))
6925 (run-hooks 'org-after-sorting-entries-or-items-hook)
6926 (message "Sorting entries...done")))
6928 (defun org-do-sort (table what &optional with-case sorting-type)
6929 "Sort TABLE of WHAT according to SORTING-TYPE.
6930 The user will be prompted for the SORTING-TYPE if the call to this
6931 function does not specify it. WHAT is only for the prompt, to indicate
6932 what is being sorted. The sorting key will be extracted from
6933 the car of the elements of the table.
6934 If WITH-CASE is non-nil, the sorting will be case-sensitive."
6935 (unless sorting-type
6936 (message
6937 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
6938 what)
6939 (setq sorting-type (read-char-exclusive)))
6940 (let ((dcst (downcase sorting-type))
6941 extractfun comparefun)
6942 ;; Define the appropriate functions
6943 (cond
6944 ((= dcst ?n)
6945 (setq extractfun 'string-to-number
6946 comparefun (if (= dcst sorting-type) '< '>)))
6947 ((= dcst ?a)
6948 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
6949 (lambda(x) (downcase (org-sort-remove-invisible x))))
6950 comparefun (if (= dcst sorting-type)
6951 'string<
6952 (lambda (a b) (and (not (string< a b))
6953 (not (string= a b)))))))
6954 ((= dcst ?t)
6955 (setq extractfun
6956 (lambda (x)
6957 (if (or (string-match org-ts-regexp x)
6958 (string-match org-ts-regexp-both x))
6959 (org-float-time
6960 (org-time-string-to-time (match-string 0 x)))
6962 comparefun (if (= dcst sorting-type) '< '>)))
6963 (t (error "Invalid sorting type `%c'" sorting-type)))
6965 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
6966 table)
6967 (lambda (a b) (funcall comparefun (car a) (car b))))))
6970 ;;; The orgstruct minor mode
6972 ;; Define a minor mode which can be used in other modes in order to
6973 ;; integrate the org-mode structure editing commands.
6975 ;; This is really a hack, because the org-mode structure commands use
6976 ;; keys which normally belong to the major mode. Here is how it
6977 ;; works: The minor mode defines all the keys necessary to operate the
6978 ;; structure commands, but wraps the commands into a function which
6979 ;; tests if the cursor is currently at a headline or a plain list
6980 ;; item. If that is the case, the structure command is used,
6981 ;; temporarily setting many Org-mode variables like regular
6982 ;; expressions for filling etc. However, when any of those keys is
6983 ;; used at a different location, function uses `key-binding' to look
6984 ;; up if the key has an associated command in another currently active
6985 ;; keymap (minor modes, major mode, global), and executes that
6986 ;; command. There might be problems if any of the keys is otherwise
6987 ;; used as a prefix key.
6989 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
6990 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
6991 ;; addresses this by checking explicitly for both bindings.
6993 (defvar orgstruct-mode-map (make-sparse-keymap)
6994 "Keymap for the minor `orgstruct-mode'.")
6996 (defvar org-local-vars nil
6997 "List of local variables, for use by `orgstruct-mode'")
6999 ;;;###autoload
7000 (define-minor-mode orgstruct-mode
7001 "Toggle the minor more `orgstruct-mode'.
7002 This mode is for using Org-mode structure commands in other modes.
7003 The following key behave as if Org-mode was active, if the cursor
7004 is on a headline, or on a plain list item (both in the definition
7005 of Org-mode).
7007 M-up Move entry/item up
7008 M-down Move entry/item down
7009 M-left Promote
7010 M-right Demote
7011 M-S-up Move entry/item up
7012 M-S-down Move entry/item down
7013 M-S-left Promote subtree
7014 M-S-right Demote subtree
7015 M-q Fill paragraph and items like in Org-mode
7016 C-c ^ Sort entries
7017 C-c - Cycle list bullet
7018 TAB Cycle item visibility
7019 M-RET Insert new heading/item
7020 S-M-RET Insert new TODO heading / Checkbox item
7021 C-c C-c Set tags / toggle checkbox"
7022 nil " OrgStruct" nil
7023 (org-load-modules-maybe)
7024 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
7026 ;;;###autoload
7027 (defun turn-on-orgstruct ()
7028 "Unconditionally turn on `orgstruct-mode'."
7029 (orgstruct-mode 1))
7031 (defun orgstruct++-mode (&optional arg)
7032 "Toggle `orgstruct-mode', the enhanced version of it.
7033 In addition to setting orgstruct-mode, this also exports all indentation
7034 and autofilling variables from org-mode into the buffer. It will also
7035 recognize item context in multiline items.
7036 Note that turning off orgstruct-mode will *not* remove the
7037 indentation/paragraph settings. This can only be done by refreshing the
7038 major mode, for example with \\[normal-mode]."
7039 (interactive "P")
7040 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
7041 (if (< arg 1)
7042 (orgstruct-mode -1)
7043 (orgstruct-mode 1)
7044 (let (var val)
7045 (mapc
7046 (lambda (x)
7047 (when (string-match
7048 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7049 (symbol-name (car x)))
7050 (setq var (car x) val (nth 1 x))
7051 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
7052 org-local-vars)
7053 (org-set-local 'orgstruct-is-++ t))))
7055 (defvar orgstruct-is-++ nil
7056 "Is orgstruct-mode in ++ version in the current-buffer?")
7057 (make-variable-buffer-local 'orgstruct-is-++)
7059 ;;;###autoload
7060 (defun turn-on-orgstruct++ ()
7061 "Unconditionally turn on `orgstruct++-mode'."
7062 (orgstruct++-mode 1))
7064 (defun orgstruct-error ()
7065 "Error when there is no default binding for a structure key."
7066 (interactive)
7067 (error "This key has no function outside structure elements"))
7069 (defun orgstruct-setup ()
7070 "Setup orgstruct keymaps."
7071 (let ((nfunc 0)
7072 (bindings
7073 (list
7074 '([(meta up)] org-metaup)
7075 '([(meta down)] org-metadown)
7076 '([(meta left)] org-metaleft)
7077 '([(meta right)] org-metaright)
7078 '([(meta shift up)] org-shiftmetaup)
7079 '([(meta shift down)] org-shiftmetadown)
7080 '([(meta shift left)] org-shiftmetaleft)
7081 '([(meta shift right)] org-shiftmetaright)
7082 '([?\e (up)] org-metaup)
7083 '([?\e (down)] org-metadown)
7084 '([?\e (left)] org-metaleft)
7085 '([?\e (right)] org-metaright)
7086 '([?\e (shift up)] org-shiftmetaup)
7087 '([?\e (shift down)] org-shiftmetadown)
7088 '([?\e (shift left)] org-shiftmetaleft)
7089 '([?\e (shift right)] org-shiftmetaright)
7090 '([(shift up)] org-shiftup)
7091 '([(shift down)] org-shiftdown)
7092 '([(shift left)] org-shiftleft)
7093 '([(shift right)] org-shiftright)
7094 '("\C-c\C-c" org-ctrl-c-ctrl-c)
7095 '("\M-q" fill-paragraph)
7096 '("\C-c^" org-sort)
7097 '("\C-c-" org-cycle-list-bullet)))
7098 elt key fun cmd)
7099 (while (setq elt (pop bindings))
7100 (setq nfunc (1+ nfunc))
7101 (setq key (org-key (car elt))
7102 fun (nth 1 elt)
7103 cmd (orgstruct-make-binding fun nfunc key))
7104 (org-defkey orgstruct-mode-map key cmd))
7106 ;; Special treatment needed for TAB and RET
7107 (org-defkey orgstruct-mode-map [(tab)]
7108 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
7109 (org-defkey orgstruct-mode-map "\C-i"
7110 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
7112 (org-defkey orgstruct-mode-map "\M-\C-m"
7113 (orgstruct-make-binding 'org-insert-heading 105
7114 "\M-\C-m" [(meta return)]))
7115 (org-defkey orgstruct-mode-map [(meta return)]
7116 (orgstruct-make-binding 'org-insert-heading 106
7117 [(meta return)] "\M-\C-m"))
7119 (org-defkey orgstruct-mode-map [(shift meta return)]
7120 (orgstruct-make-binding 'org-insert-todo-heading 107
7121 [(meta return)] "\M-\C-m"))
7123 (org-defkey orgstruct-mode-map "\e\C-m"
7124 (orgstruct-make-binding 'org-insert-heading 108
7125 "\e\C-m" [?\e (return)]))
7126 (org-defkey orgstruct-mode-map [?\e (return)]
7127 (orgstruct-make-binding 'org-insert-heading 109
7128 [?\e (return)] "\e\C-m"))
7129 (org-defkey orgstruct-mode-map [?\e (shift return)]
7130 (orgstruct-make-binding 'org-insert-todo-heading 110
7131 [?\e (return)] "\e\C-m"))
7133 (unless org-local-vars
7134 (setq org-local-vars (org-get-local-variables)))
7138 (defun orgstruct-make-binding (fun n &rest keys)
7139 "Create a function for binding in the structure minor mode.
7140 FUN is the command to call inside a table. N is used to create a unique
7141 command name. KEYS are keys that should be checked in for a command
7142 to execute outside of tables."
7143 (eval
7144 (list 'defun
7145 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
7146 '(arg)
7147 (concat "In Structure, run `" (symbol-name fun) "'.\n"
7148 "Outside of structure, run the binding of `"
7149 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
7150 "'.")
7151 '(interactive "p")
7152 (list 'if
7153 `(org-context-p 'headline 'item
7154 (and orgstruct-is-++
7155 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
7156 'item-body))
7157 (list 'org-run-like-in-org-mode (list 'quote fun))
7158 (list 'let '(orgstruct-mode)
7159 (list 'call-interactively
7160 (append '(or)
7161 (mapcar (lambda (k)
7162 (list 'key-binding k))
7163 keys)
7164 '('orgstruct-error))))))))
7166 (defun org-context-p (&rest contexts)
7167 "Check if local context is any of CONTEXTS.
7168 Possible values in the list of contexts are `table', `headline', and `item'."
7169 (let ((pos (point)))
7170 (goto-char (point-at-bol))
7171 (prog1 (or (and (memq 'table contexts)
7172 (looking-at "[ \t]*|"))
7173 (and (memq 'headline contexts)
7174 ;;????????? (looking-at "\\*+"))
7175 (looking-at outline-regexp))
7176 (and (memq 'item contexts)
7177 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
7178 (and (memq 'item-body contexts)
7179 (org-in-item-p)))
7180 (goto-char pos))))
7182 (defun org-get-local-variables ()
7183 "Return a list of all local variables in an org-mode buffer."
7184 (let (varlist)
7185 (with-current-buffer (get-buffer-create "*Org tmp*")
7186 (erase-buffer)
7187 (org-mode)
7188 (setq varlist (buffer-local-variables)))
7189 (kill-buffer "*Org tmp*")
7190 (delq nil
7191 (mapcar
7192 (lambda (x)
7193 (setq x
7194 (if (symbolp x)
7195 (list x)
7196 (list (car x) (list 'quote (cdr x)))))
7197 (if (string-match
7198 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7199 (symbol-name (car x)))
7200 x nil))
7201 varlist))))
7203 ;;;###autoload
7204 (defun org-run-like-in-org-mode (cmd)
7205 "Run a command, pretending that the current buffer is in Org-mode.
7206 This will temporarily bind local variables that are typically bound in
7207 Org-mode to the values they have in Org-mode, and then interactively
7208 call CMD."
7209 (org-load-modules-maybe)
7210 (unless org-local-vars
7211 (setq org-local-vars (org-get-local-variables)))
7212 (eval (list 'let org-local-vars
7213 (list 'call-interactively (list 'quote cmd)))))
7215 ;;;; Archiving
7217 (defun org-get-category (&optional pos)
7218 "Get the category applying to position POS."
7219 (get-text-property (or pos (point)) 'org-category))
7221 (defun org-refresh-category-properties ()
7222 "Refresh category text properties in the buffer."
7223 (let ((def-cat (cond
7224 ((null org-category)
7225 (if buffer-file-name
7226 (file-name-sans-extension
7227 (file-name-nondirectory buffer-file-name))
7228 "???"))
7229 ((symbolp org-category) (symbol-name org-category))
7230 (t org-category)))
7231 beg end cat pos optionp)
7232 (org-unmodified
7233 (save-excursion
7234 (save-restriction
7235 (widen)
7236 (goto-char (point-min))
7237 (put-text-property (point) (point-max) 'org-category def-cat)
7238 (while (re-search-forward
7239 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
7240 (setq pos (match-end 0)
7241 optionp (equal (char-after (match-beginning 0)) ?#)
7242 cat (org-trim (match-string 2)))
7243 (if optionp
7244 (setq beg (point-at-bol) end (point-max))
7245 (org-back-to-heading t)
7246 (setq beg (point) end (org-end-of-subtree t t)))
7247 (put-text-property beg end 'org-category cat)
7248 (goto-char pos)))))))
7251 ;;;; Link Stuff
7253 ;;; Link abbreviations
7255 (defun org-link-expand-abbrev (link)
7256 "Apply replacements as defined in `org-link-abbrev-alist."
7257 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
7258 (let* ((key (match-string 1 link))
7259 (as (or (assoc key org-link-abbrev-alist-local)
7260 (assoc key org-link-abbrev-alist)))
7261 (tag (and (match-end 2) (match-string 3 link)))
7262 rpl)
7263 (if (not as)
7264 link
7265 (setq rpl (cdr as))
7266 (cond
7267 ((symbolp rpl) (funcall rpl tag))
7268 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
7269 ((string-match "%h" rpl)
7270 (replace-match (url-hexify-string (or tag "")) t t rpl))
7271 (t (concat rpl tag)))))
7272 link))
7274 ;;; Storing and inserting links
7276 (defvar org-insert-link-history nil
7277 "Minibuffer history for links inserted with `org-insert-link'.")
7279 (defvar org-stored-links nil
7280 "Contains the links stored with `org-store-link'.")
7282 (defvar org-store-link-plist nil
7283 "Plist with info about the most recently link created with `org-store-link'.")
7285 (defvar org-link-protocols nil
7286 "Link protocols added to Org-mode using `org-add-link-type'.")
7288 (defvar org-store-link-functions nil
7289 "List of functions that are called to create and store a link.
7290 Each function will be called in turn until one returns a non-nil
7291 value. Each function should check if it is responsible for creating
7292 this link (for example by looking at the major mode).
7293 If not, it must exit and return nil.
7294 If yes, it should return a non-nil value after a calling
7295 `org-store-link-props' with a list of properties and values.
7296 Special properties are:
7298 :type The link prefix. like \"http\". This must be given.
7299 :link The link, like \"http://www.astro.uva.nl/~dominik\".
7300 This is obligatory as well.
7301 :description Optional default description for the second pair
7302 of brackets in an Org-mode link. The user can still change
7303 this when inserting this link into an Org-mode buffer.
7305 In addition to these, any additional properties can be specified
7306 and then used in remember templates.")
7308 (defun org-add-link-type (type &optional follow export)
7309 "Add TYPE to the list of `org-link-types'.
7310 Re-compute all regular expressions depending on `org-link-types'
7312 FOLLOW and EXPORT are two functions.
7314 FOLLOW should take the link path as the single argument and do whatever
7315 is necessary to follow the link, for example find a file or display
7316 a mail message.
7318 EXPORT should format the link path for export to one of the export formats.
7319 It should be a function accepting three arguments:
7321 path the path of the link, the text after the prefix (like \"http:\")
7322 desc the description of the link, if any, nil if there was no description
7323 format the export format, a symbol like `html' or `latex'.
7325 The function may use the FORMAT information to return different values
7326 depending on the format. The return value will be put literally into
7327 the exported file.
7328 Org-mode has a built-in default for exporting links. If you are happy with
7329 this default, there is no need to define an export function for the link
7330 type. For a simple example of an export function, see `org-bbdb.el'."
7331 (add-to-list 'org-link-types type t)
7332 (org-make-link-regexps)
7333 (if (assoc type org-link-protocols)
7334 (setcdr (assoc type org-link-protocols) (list follow export))
7335 (push (list type follow export) org-link-protocols)))
7337 (defvar org-agenda-buffer-name)
7339 ;;;###autoload
7340 (defun org-store-link (arg)
7341 "\\<org-mode-map>Store an org-link to the current location.
7342 This link is added to `org-stored-links' and can later be inserted
7343 into an org-buffer with \\[org-insert-link].
7345 For some link types, a prefix arg is interpreted:
7346 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
7347 For file links, arg negates `org-context-in-file-links'."
7348 (interactive "P")
7349 (org-load-modules-maybe)
7350 (setq org-store-link-plist nil) ; reset
7351 (let ((outline-regexp (org-get-limited-outline-regexp))
7352 link cpltxt desc description search txt custom-id)
7353 (cond
7355 ((run-hook-with-args-until-success 'org-store-link-functions)
7356 (setq link (plist-get org-store-link-plist :link)
7357 desc (or (plist-get org-store-link-plist :description) link)))
7359 ((equal (buffer-name) "*Org Edit Src Example*")
7360 (let (label gc)
7361 (while (or (not label)
7362 (save-excursion
7363 (save-restriction
7364 (widen)
7365 (goto-char (point-min))
7366 (re-search-forward
7367 (regexp-quote (format org-coderef-label-format label))
7368 nil t))))
7369 (when label (message "Label exists already") (sit-for 2))
7370 (setq label (read-string "Code line label: " label)))
7371 (end-of-line 1)
7372 (setq link (format org-coderef-label-format label))
7373 (setq gc (- 79 (length link)))
7374 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
7375 (insert link)
7376 (setq link (concat "(" label ")") desc nil)))
7378 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
7379 ;; We are in the agenda, link to referenced location
7380 (let ((m (or (get-text-property (point) 'org-hd-marker)
7381 (get-text-property (point) 'org-marker))))
7382 (when m
7383 (org-with-point-at m
7384 (call-interactively 'org-store-link)))))
7386 ((eq major-mode 'calendar-mode)
7387 (let ((cd (calendar-cursor-to-date)))
7388 (setq link
7389 (format-time-string
7390 (car org-time-stamp-formats)
7391 (apply 'encode-time
7392 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
7393 nil nil nil))))
7394 (org-store-link-props :type "calendar" :date cd)))
7396 ((eq major-mode 'w3-mode)
7397 (setq cpltxt (if (and (buffer-name)
7398 (not (string-match "Untitled" (buffer-name))))
7399 (buffer-name)
7400 (url-view-url t))
7401 link (org-make-link (url-view-url t)))
7402 (org-store-link-props :type "w3" :url (url-view-url t)))
7404 ((eq major-mode 'w3m-mode)
7405 (setq cpltxt (or w3m-current-title w3m-current-url)
7406 link (org-make-link w3m-current-url))
7407 (org-store-link-props :type "w3m" :url (url-view-url t)))
7409 ((setq search (run-hook-with-args-until-success
7410 'org-create-file-search-functions))
7411 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
7412 "::" search))
7413 (setq cpltxt (or description link)))
7415 ((eq major-mode 'image-mode)
7416 (setq cpltxt (concat "file:"
7417 (abbreviate-file-name buffer-file-name))
7418 link (org-make-link cpltxt))
7419 (org-store-link-props :type "image" :file buffer-file-name))
7421 ((eq major-mode 'dired-mode)
7422 ;; link to the file in the current line
7423 (setq cpltxt (concat "file:"
7424 (abbreviate-file-name
7425 (expand-file-name
7426 (dired-get-filename nil t))))
7427 link (org-make-link cpltxt)))
7429 ((and buffer-file-name (org-mode-p))
7430 (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
7431 (cond
7432 ((org-in-regexp "<<\\(.*?\\)>>")
7433 (setq cpltxt
7434 (concat "file:"
7435 (abbreviate-file-name buffer-file-name)
7436 "::" (match-string 1))
7437 link (org-make-link cpltxt)))
7438 ((and (featurep 'org-id)
7439 (or (eq org-link-to-org-use-id t)
7440 (and (eq org-link-to-org-use-id 'create-if-interactive)
7441 (interactive-p))
7442 (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
7443 (interactive-p)
7444 (not custom-id))
7445 (and org-link-to-org-use-id
7446 (condition-case nil
7447 (org-entry-get nil "ID")
7448 (error nil)))))
7449 ;; We can make a link using the ID.
7450 (setq link (condition-case nil
7451 (prog1 (org-id-store-link)
7452 (setq desc (plist-get org-store-link-plist
7453 :description)))
7454 (error
7455 ;; probably before first headline, link to file only
7456 (concat "file:"
7457 (abbreviate-file-name buffer-file-name))))))
7459 ;; Just link to current headline
7460 (setq cpltxt (concat "file:"
7461 (abbreviate-file-name buffer-file-name)))
7462 ;; Add a context search string
7463 (when (org-xor org-context-in-file-links arg)
7464 (setq txt (cond
7465 ((org-on-heading-p) nil)
7466 ((org-region-active-p)
7467 (buffer-substring (region-beginning) (region-end)))
7468 (t nil)))
7469 (when (or (null txt) (string-match "\\S-" txt))
7470 (setq cpltxt
7471 (concat cpltxt "::"
7472 (condition-case nil
7473 (org-make-org-heading-search-string txt)
7474 (error "")))
7475 desc (or (nth 4 (ignore-errors
7476 (org-heading-components))) "NONE"))))
7477 (if (string-match "::\\'" cpltxt)
7478 (setq cpltxt (substring cpltxt 0 -2)))
7479 (setq link (org-make-link cpltxt)))))
7481 ((buffer-file-name (buffer-base-buffer))
7482 ;; Just link to this file here.
7483 (setq cpltxt (concat "file:"
7484 (abbreviate-file-name
7485 (buffer-file-name (buffer-base-buffer)))))
7486 ;; Add a context string
7487 (when (org-xor org-context-in-file-links arg)
7488 (setq txt (if (org-region-active-p)
7489 (buffer-substring (region-beginning) (region-end))
7490 (buffer-substring (point-at-bol) (point-at-eol))))
7491 ;; Only use search option if there is some text.
7492 (when (string-match "\\S-" txt)
7493 (setq cpltxt
7494 (concat cpltxt "::" (org-make-org-heading-search-string txt))
7495 desc "NONE")))
7496 (setq link (org-make-link cpltxt)))
7498 ((interactive-p)
7499 (error "Cannot link to a buffer which is not visiting a file"))
7501 (t (setq link nil)))
7503 (if (consp link) (setq cpltxt (car link) link (cdr link)))
7504 (setq link (or link cpltxt)
7505 desc (or desc cpltxt))
7506 (if (equal desc "NONE") (setq desc nil))
7508 (if (and (or (interactive-p) executing-kbd-macro) link)
7509 (progn
7510 (setq org-stored-links
7511 (cons (list link desc) org-stored-links))
7512 (message "Stored: %s" (or desc link))
7513 (when custom-id
7514 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
7515 "::#" custom-id))
7516 (setq org-stored-links
7517 (cons (list link desc) org-stored-links))))
7518 (and link (org-make-link-string link desc)))))
7520 (defun org-store-link-props (&rest plist)
7521 "Store link properties, extract names and addresses."
7522 (let (x adr)
7523 (when (setq x (plist-get plist :from))
7524 (setq adr (mail-extract-address-components x))
7525 (setq plist (plist-put plist :fromname (car adr)))
7526 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
7527 (when (setq x (plist-get plist :to))
7528 (setq adr (mail-extract-address-components x))
7529 (setq plist (plist-put plist :toname (car adr)))
7530 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
7531 (let ((from (plist-get plist :from))
7532 (to (plist-get plist :to)))
7533 (when (and from to org-from-is-user-regexp)
7534 (setq plist
7535 (plist-put plist :fromto
7536 (if (string-match org-from-is-user-regexp from)
7537 (concat "to %t")
7538 (concat "from %f"))))))
7539 (setq org-store-link-plist plist))
7541 (defun org-add-link-props (&rest plist)
7542 "Add these properties to the link property list."
7543 (let (key value)
7544 (while plist
7545 (setq key (pop plist) value (pop plist))
7546 (setq org-store-link-plist
7547 (plist-put org-store-link-plist key value)))))
7549 (defun org-email-link-description (&optional fmt)
7550 "Return the description part of an email link.
7551 This takes information from `org-store-link-plist' and formats it
7552 according to FMT (default from `org-email-link-description-format')."
7553 (setq fmt (or fmt org-email-link-description-format))
7554 (let* ((p org-store-link-plist)
7555 (to (plist-get p :toaddress))
7556 (from (plist-get p :fromaddress))
7557 (table
7558 (list
7559 (cons "%c" (plist-get p :fromto))
7560 (cons "%F" (plist-get p :from))
7561 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
7562 (cons "%T" (plist-get p :to))
7563 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
7564 (cons "%s" (plist-get p :subject))
7565 (cons "%m" (plist-get p :message-id)))))
7566 (when (string-match "%c" fmt)
7567 ;; Check if the user wrote this message
7568 (if (and org-from-is-user-regexp from to
7569 (save-match-data (string-match org-from-is-user-regexp from)))
7570 (setq fmt (replace-match "to %t" t t fmt))
7571 (setq fmt (replace-match "from %f" t t fmt))))
7572 (org-replace-escapes fmt table)))
7574 (defun org-make-org-heading-search-string (&optional string heading)
7575 "Make search string for STRING or current headline."
7576 (interactive)
7577 (let ((s (or string (org-get-heading))))
7578 (unless (and string (not heading))
7579 ;; We are using a headline, clean up garbage in there.
7580 (if (string-match org-todo-regexp s)
7581 (setq s (replace-match "" t t s)))
7582 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
7583 (setq s (replace-match "" t t s)))
7584 (setq s (org-trim s))
7585 (if (string-match (concat "^\\(" org-quote-string "\\|"
7586 org-comment-string "\\)") s)
7587 (setq s (replace-match "" t t s)))
7588 (while (string-match org-ts-regexp s)
7589 (setq s (replace-match "" t t s))))
7590 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
7591 (setq s (replace-match " " t t s)))
7592 (or string (setq s (concat "*" s))) ; Add * for headlines
7593 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
7595 (defun org-make-link (&rest strings)
7596 "Concatenate STRINGS."
7597 (apply 'concat strings))
7599 (defun org-make-link-string (link &optional description)
7600 "Make a link with brackets, consisting of LINK and DESCRIPTION."
7601 (unless (string-match "\\S-" link)
7602 (error "Empty link"))
7603 (when (stringp description)
7604 ;; Remove brackets from the description, they are fatal.
7605 (while (string-match "\\[" description)
7606 (setq description (replace-match "{" t t description)))
7607 (while (string-match "\\]" description)
7608 (setq description (replace-match "}" t t description))))
7609 (when (equal (org-link-escape link) description)
7610 ;; No description needed, it is identical
7611 (setq description nil))
7612 (when (and (not description)
7613 (not (equal link (org-link-escape link))))
7614 (setq description (org-extract-attributes link)))
7615 (concat "[[" (org-link-escape link) "]"
7616 (if description (concat "[" description "]") "")
7617 "]"))
7619 (defconst org-link-escape-chars
7620 '((?\ . "%20")
7621 (?\[ . "%5B")
7622 (?\] . "%5D")
7623 (?\340 . "%E0") ; `a
7624 (?\342 . "%E2") ; ^a
7625 (?\347 . "%E7") ; ,c
7626 (?\350 . "%E8") ; `e
7627 (?\351 . "%E9") ; 'e
7628 (?\352 . "%EA") ; ^e
7629 (?\356 . "%EE") ; ^i
7630 (?\364 . "%F4") ; ^o
7631 (?\371 . "%F9") ; `u
7632 (?\373 . "%FB") ; ^u
7633 (?\; . "%3B")
7634 (?? . "%3F")
7635 (?= . "%3D")
7636 (?+ . "%2B")
7638 "Association list of escapes for some characters problematic in links.
7639 This is the list that is used for internal purposes.")
7641 (defvar org-url-encoding-use-url-hexify nil)
7643 (defconst org-link-escape-chars-browser
7644 '((?\ . "%20")) ; 32 for the SPC char
7645 "Association list of escapes for some characters problematic in links.
7646 This is the list that is used before handing over to the browser.")
7648 (defun org-link-escape (text &optional table)
7649 "Escape characters in TEXT that are problematic for links."
7650 (if org-url-encoding-use-url-hexify
7651 (url-hexify-string text)
7652 (setq table (or table org-link-escape-chars))
7653 (when text
7654 (let ((re (mapconcat (lambda (x) (regexp-quote
7655 (char-to-string (car x))))
7656 table "\\|")))
7657 (while (string-match re text)
7658 (setq text
7659 (replace-match
7660 (cdr (assoc (string-to-char (match-string 0 text))
7661 table))
7662 t t text)))
7663 text))))
7665 (defun org-link-unescape (text &optional table)
7666 "Reverse the action of `org-link-escape'."
7667 (if org-url-encoding-use-url-hexify
7668 (url-unhex-string text)
7669 (setq table (or table org-link-escape-chars))
7670 (when text
7671 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
7672 table "\\|")))
7673 (while (string-match re text)
7674 (setq text
7675 (replace-match
7676 (char-to-string (car (rassoc (match-string 0 text) table)))
7677 t t text)))
7678 text))))
7680 (defun org-xor (a b)
7681 "Exclusive or."
7682 (if a (not b) b))
7684 (defun org-fixup-message-id-for-http (s)
7685 "Replace special characters in a message id, so it can be used in an http query."
7686 (while (string-match "<" s)
7687 (setq s (replace-match "%3C" t t s)))
7688 (while (string-match ">" s)
7689 (setq s (replace-match "%3E" t t s)))
7690 (while (string-match "@" s)
7691 (setq s (replace-match "%40" t t s)))
7694 ;;;###autoload
7695 (defun org-insert-link-global ()
7696 "Insert a link like Org-mode does.
7697 This command can be called in any mode to insert a link in Org-mode syntax."
7698 (interactive)
7699 (org-load-modules-maybe)
7700 (org-run-like-in-org-mode 'org-insert-link))
7702 (defun org-insert-link (&optional complete-file link-location)
7703 "Insert a link. At the prompt, enter the link.
7705 Completion can be used to insert any of the link protocol prefixes like
7706 http or ftp in use.
7708 The history can be used to select a link previously stored with
7709 `org-store-link'. When the empty string is entered (i.e. if you just
7710 press RET at the prompt), the link defaults to the most recently
7711 stored link. As SPC triggers completion in the minibuffer, you need to
7712 use M-SPC or C-q SPC to force the insertion of a space character.
7714 You will also be prompted for a description, and if one is given, it will
7715 be displayed in the buffer instead of the link.
7717 If there is already a link at point, this command will allow you to edit link
7718 and description parts.
7720 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
7721 be selected using completion. The path to the file will be relative to the
7722 current directory if the file is in the current directory or a subdirectory.
7723 Otherwise, the link will be the absolute path as completed in the minibuffer
7724 \(i.e. normally ~/path/to/file). You can configure this behavior using the
7725 option `org-link-file-path-type'.
7727 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
7728 the current directory or below.
7730 With three \\[universal-argument] prefixes, negate the meaning of
7731 `org-keep-stored-link-after-insertion'.
7733 If `org-make-link-description-function' is non-nil, this function will be
7734 called with the link target, and the result will be the default
7735 link description.
7737 If the LINK-LOCATION parameter is non-nil, this value will be
7738 used as the link location instead of reading one interactively."
7739 (interactive "P")
7740 (let* ((wcf (current-window-configuration))
7741 (region (if (org-region-active-p)
7742 (buffer-substring (region-beginning) (region-end))))
7743 (remove (and region (list (region-beginning) (region-end))))
7744 (desc region)
7745 tmphist ; byte-compile incorrectly complains about this
7746 (link link-location)
7747 entry file all-prefixes)
7748 (cond
7749 (link-location) ; specified by arg, just use it.
7750 ((org-in-regexp org-bracket-link-regexp 1)
7751 ;; We do have a link at point, and we are going to edit it.
7752 (setq remove (list (match-beginning 0) (match-end 0)))
7753 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
7754 (setq link (read-string "Link: "
7755 (org-link-unescape
7756 (org-match-string-no-properties 1)))))
7757 ((or (org-in-regexp org-angle-link-re)
7758 (org-in-regexp org-plain-link-re))
7759 ;; Convert to bracket link
7760 (setq remove (list (match-beginning 0) (match-end 0))
7761 link (read-string "Link: "
7762 (org-remove-angle-brackets (match-string 0)))))
7763 ((member complete-file '((4) (16)))
7764 ;; Completing read for file names.
7765 (setq link (org-file-complete-link complete-file)))
7767 ;; Read link, with completion for stored links.
7768 (with-output-to-temp-buffer "*Org Links*"
7769 (princ "Insert a link.
7770 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
7771 (when org-stored-links
7772 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
7773 (princ (mapconcat
7774 (lambda (x)
7775 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
7776 (reverse org-stored-links) "\n"))))
7777 (let ((cw (selected-window)))
7778 (select-window (get-buffer-window "*Org Links*"))
7779 (setq truncate-lines t)
7780 (unless (pos-visible-in-window-p (point-max))
7781 (org-fit-window-to-buffer))
7782 (and (window-live-p cw) (select-window cw)))
7783 ;; Fake a link history, containing the stored links.
7784 (setq tmphist (append (mapcar 'car org-stored-links)
7785 org-insert-link-history))
7786 (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
7787 (mapcar 'car org-link-abbrev-alist)
7788 org-link-types))
7789 (unwind-protect
7790 (progn
7791 (setq link
7792 (let ((org-completion-use-ido nil)
7793 (org-completion-use-iswitchb nil))
7794 (org-completing-read
7795 "Link: "
7796 (append
7797 (mapcar (lambda (x) (list (concat x ":")))
7798 all-prefixes)
7799 (mapcar 'car org-stored-links))
7800 nil nil nil
7801 'tmphist
7802 (car (car org-stored-links)))))
7803 (if (or (member link all-prefixes)
7804 (and (equal ":" (substring link -1))
7805 (member (substring link 0 -1) all-prefixes)
7806 (setq link (substring link 0 -1))))
7807 (setq link (org-link-try-special-completion link))))
7808 (set-window-configuration wcf)
7809 (kill-buffer "*Org Links*"))
7810 (setq entry (assoc link org-stored-links))
7811 (or entry (push link org-insert-link-history))
7812 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
7813 (not org-keep-stored-link-after-insertion))
7814 (setq org-stored-links (delq (assoc link org-stored-links)
7815 org-stored-links)))
7816 (setq desc (or desc (nth 1 entry)))))
7818 (if (string-match org-plain-link-re link)
7819 ;; URL-like link, normalize the use of angular brackets.
7820 (setq link (org-make-link (org-remove-angle-brackets link))))
7822 ;; Check if we are linking to the current file with a search option
7823 ;; If yes, simplify the link by using only the search option.
7824 (when (and buffer-file-name
7825 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
7826 (let* ((path (match-string 1 link))
7827 (case-fold-search nil)
7828 (search (match-string 2 link)))
7829 (save-match-data
7830 (if (equal (file-truename buffer-file-name) (file-truename path))
7831 ;; We are linking to this same file, with a search option
7832 (setq link search)))))
7834 ;; Check if we can/should use a relative path. If yes, simplify the link
7835 (when (string-match "^file:\\(.*\\)" link)
7836 (let* ((path (match-string 1 link))
7837 (origpath path)
7838 (case-fold-search nil))
7839 (cond
7840 ((or (eq org-link-file-path-type 'absolute)
7841 (equal complete-file '(16)))
7842 (setq path (abbreviate-file-name (expand-file-name path))))
7843 ((eq org-link-file-path-type 'noabbrev)
7844 (setq path (expand-file-name path)))
7845 ((eq org-link-file-path-type 'relative)
7846 (setq path (file-relative-name path)))
7848 (save-match-data
7849 (if (string-match (concat "^" (regexp-quote
7850 (file-name-as-directory
7851 (expand-file-name "."))))
7852 (expand-file-name path))
7853 ;; We are linking a file with relative path name.
7854 (setq path (substring (expand-file-name path)
7855 (match-end 0)))
7856 (setq path (abbreviate-file-name (expand-file-name path)))))))
7857 (setq link (concat "file:" path))
7858 (if (equal desc origpath)
7859 (setq desc path))))
7861 (if org-make-link-description-function
7862 (setq desc (funcall org-make-link-description-function link desc)))
7864 (setq desc (read-string "Description: " desc))
7865 (unless (string-match "\\S-" desc) (setq desc nil))
7866 (if remove (apply 'delete-region remove))
7867 (insert (org-make-link-string link desc))))
7869 (defun org-link-try-special-completion (type)
7870 "If there is completion support for link type TYPE, offer it."
7871 (let ((fun (intern (concat "org-" type "-complete-link"))))
7872 (if (functionp fun)
7873 (funcall fun)
7874 (read-string "Link (no completion support): " (concat type ":")))))
7876 (defun org-file-complete-link (&optional arg)
7877 "Create a file link using completion."
7878 (let (file link)
7879 (setq file (read-file-name "File: "))
7880 (let ((pwd (file-name-as-directory (expand-file-name ".")))
7881 (pwd1 (file-name-as-directory (abbreviate-file-name
7882 (expand-file-name ".")))))
7883 (cond
7884 ((equal arg '(16))
7885 (setq link (org-make-link
7886 "file:"
7887 (abbreviate-file-name (expand-file-name file)))))
7888 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
7889 (setq link (org-make-link "file:" (match-string 1 file))))
7890 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
7891 (expand-file-name file))
7892 (setq link (org-make-link
7893 "file:" (match-string 1 (expand-file-name file)))))
7894 (t (setq link (org-make-link "file:" file)))))
7895 link))
7897 (defun org-completing-read (&rest args)
7898 "Completing-read with SPACE being a normal character."
7899 (let ((minibuffer-local-completion-map
7900 (copy-keymap minibuffer-local-completion-map)))
7901 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
7902 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
7903 (apply 'org-icompleting-read args)))
7905 (defun org-completing-read-no-i (&rest args)
7906 (let (org-completion-use-ido org-completion-use-iswitchb)
7907 (apply 'org-completing-read args)))
7909 (defun org-iswitchb-completing-read (prompt choices &rest args)
7910 "Use iswitch as a completing-read replacement to choose from choices.
7911 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
7912 from."
7913 (let* ((iswitchb-use-virtual-buffers nil)
7914 (iswitchb-make-buflist-hook
7915 (lambda ()
7916 (setq iswitchb-temp-buflist choices))))
7917 (iswitchb-read-buffer prompt)))
7919 (defun org-icompleting-read (&rest args)
7920 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
7921 (org-without-partial-completion
7922 (if (and org-completion-use-ido
7923 (fboundp 'ido-completing-read)
7924 (boundp 'ido-mode) ido-mode
7925 (listp (second args)))
7926 (let ((ido-enter-matching-directory nil))
7927 (apply 'ido-completing-read (concat (car args))
7928 (if (consp (car (nth 1 args)))
7929 (mapcar (lambda (x) (car x)) (nth 1 args))
7930 (nth 1 args))
7931 (cddr args)))
7932 (if (and org-completion-use-iswitchb
7933 (boundp 'iswitchb-mode) iswitchb-mode
7934 (listp (second args)))
7935 (apply 'org-iswitchb-completing-read (concat (car args))
7936 (if (consp (car (nth 1 args)))
7937 (mapcar (lambda (x) (car x)) (nth 1 args))
7938 (nth 1 args))
7939 (cddr args))
7940 (apply 'completing-read args)))))
7942 (defun org-extract-attributes (s)
7943 "Extract the attributes cookie from a string and set as text property."
7944 (let (a attr (start 0) key value)
7945 (save-match-data
7946 (when (string-match "{{\\([^}]+\\)}}$" s)
7947 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
7948 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
7949 (setq key (match-string 1 a) value (match-string 2 a)
7950 start (match-end 0)
7951 attr (plist-put attr (intern key) value))))
7952 (org-add-props s nil 'org-attr attr))
7955 (defun org-extract-attributes-from-string (tag)
7956 (let (key value attr)
7957 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
7958 (setq key (match-string 1 tag) value (match-string 2 tag)
7959 tag (replace-match "" t t tag)
7960 attr (plist-put attr (intern key) value)))
7961 (cons tag attr)))
7963 (defun org-attributes-to-string (plist)
7964 "Format a property list into an HTML attribute list."
7965 (let ((s "") key value)
7966 (while plist
7967 (setq key (pop plist) value (pop plist))
7968 (and value
7969 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
7972 ;;; Opening/following a link
7974 (defvar org-link-search-failed nil)
7976 (defun org-next-link ()
7977 "Move forward to the next link.
7978 If the link is in hidden text, expose it."
7979 (interactive)
7980 (when (and org-link-search-failed (eq this-command last-command))
7981 (goto-char (point-min))
7982 (message "Link search wrapped back to beginning of buffer"))
7983 (setq org-link-search-failed nil)
7984 (let* ((pos (point))
7985 (ct (org-context))
7986 (a (assoc :link ct)))
7987 (if a (goto-char (nth 2 a)))
7988 (if (re-search-forward org-any-link-re nil t)
7989 (progn
7990 (goto-char (match-beginning 0))
7991 (if (org-invisible-p) (org-show-context)))
7992 (goto-char pos)
7993 (setq org-link-search-failed t)
7994 (error "No further link found"))))
7996 (defun org-previous-link ()
7997 "Move backward to the previous link.
7998 If the link is in hidden text, expose it."
7999 (interactive)
8000 (when (and org-link-search-failed (eq this-command last-command))
8001 (goto-char (point-max))
8002 (message "Link search wrapped back to end of buffer"))
8003 (setq org-link-search-failed nil)
8004 (let* ((pos (point))
8005 (ct (org-context))
8006 (a (assoc :link ct)))
8007 (if a (goto-char (nth 1 a)))
8008 (if (re-search-backward org-any-link-re nil t)
8009 (progn
8010 (goto-char (match-beginning 0))
8011 (if (org-invisible-p) (org-show-context)))
8012 (goto-char pos)
8013 (setq org-link-search-failed t)
8014 (error "No further link found"))))
8016 (defun org-translate-link (s)
8017 "Translate a link string if a translation function has been defined."
8018 (if (and org-link-translation-function
8019 (fboundp org-link-translation-function)
8020 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
8021 (progn
8022 (setq s (funcall org-link-translation-function
8023 (match-string 1) (match-string 2)))
8024 (concat (car s) ":" (cdr s)))
8027 (defun org-translate-link-from-planner (type path)
8028 "Translate a link from Emacs Planner syntax so that Org can follow it.
8029 This is still an experimental function, your mileage may vary."
8030 (cond
8031 ((member type '("http" "https" "news" "ftp"))
8032 ;; standard Internet links are the same.
8033 nil)
8034 ((and (equal type "irc") (string-match "^//" path))
8035 ;; Planner has two / at the beginning of an irc link, we have 1.
8036 ;; We should have zero, actually....
8037 (setq path (substring path 1)))
8038 ((and (equal type "lisp") (string-match "^/" path))
8039 ;; Planner has a slash, we do not.
8040 (setq type "elisp" path (substring path 1)))
8041 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
8042 ;; A typical message link. Planner has the id after the fina slash,
8043 ;; we separate it with a hash mark
8044 (setq path (concat (match-string 1 path) "#"
8045 (org-remove-angle-brackets (match-string 2 path)))))
8047 (cons type path))
8049 (defun org-find-file-at-mouse (ev)
8050 "Open file link or URL at mouse."
8051 (interactive "e")
8052 (mouse-set-point ev)
8053 (org-open-at-point 'in-emacs))
8055 (defun org-open-at-mouse (ev)
8056 "Open file link or URL at mouse."
8057 (interactive "e")
8058 (mouse-set-point ev)
8059 (if (eq major-mode 'org-agenda-mode)
8060 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
8061 (org-open-at-point))
8063 (defvar org-window-config-before-follow-link nil
8064 "The window configuration before following a link.
8065 This is saved in case the need arises to restore it.")
8067 (defvar org-open-link-marker (make-marker)
8068 "Marker pointing to the location where `org-open-at-point; was called.")
8070 ;;;###autoload
8071 (defun org-open-at-point-global ()
8072 "Follow a link like Org-mode does.
8073 This command can be called in any mode to follow a link that has
8074 Org-mode syntax."
8075 (interactive)
8076 (org-run-like-in-org-mode 'org-open-at-point))
8078 ;;;###autoload
8079 (defun org-open-link-from-string (s &optional arg reference-buffer)
8080 "Open a link in the string S, as if it was in Org-mode."
8081 (interactive "sLink: \nP")
8082 (let ((reference-buffer (or reference-buffer (current-buffer))))
8083 (with-temp-buffer
8084 (let ((org-inhibit-startup t))
8085 (org-mode)
8086 (insert s)
8087 (goto-char (point-min))
8088 (org-open-at-point arg reference-buffer)))))
8090 (defun org-open-at-point (&optional in-emacs reference-buffer)
8091 "Open link at or after point.
8092 If there is no link at point, this function will search forward up to
8093 the end of the current line.
8094 Normally, files will be opened by an appropriate application. If the
8095 optional argument IN-EMACS is non-nil, Emacs will visit the file.
8096 With a double prefix argument, try to open outside of Emacs, in the
8097 application the system uses for this file type."
8098 (interactive "P")
8099 (org-load-modules-maybe)
8100 (move-marker org-open-link-marker (point))
8101 (setq org-window-config-before-follow-link (current-window-configuration))
8102 (org-remove-occur-highlights nil nil t)
8103 (cond
8104 ((and (org-on-heading-p)
8105 (not (org-in-regexp
8106 (concat org-plain-link-re "\\|"
8107 org-bracket-link-regexp "\\|"
8108 org-angle-link-re "\\|"
8109 "[ \t]:[^ \t\n]+:[ \t]*$"))))
8110 (or (org-offer-links-in-entry in-emacs)
8111 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
8112 ((org-at-timestamp-p t) (org-follow-timestamp-link))
8113 ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
8114 (org-footnote-action))
8116 (let (type path link line search (pos (point)))
8117 (catch 'match
8118 (save-excursion
8119 (skip-chars-forward "^]\n\r")
8120 (when (org-in-regexp org-bracket-link-regexp)
8121 (setq link (org-extract-attributes
8122 (org-link-unescape (org-match-string-no-properties 1))))
8123 (while (string-match " *\n *" link)
8124 (setq link (replace-match " " t t link)))
8125 (setq link (org-link-expand-abbrev link))
8126 (cond
8127 ((or (file-name-absolute-p link)
8128 (string-match "^\\.\\.?/" link))
8129 (setq type "file" path link))
8130 ((string-match org-link-re-with-space3 link)
8131 (setq type (match-string 1 link) path (match-string 2 link)))
8132 (t (setq type "thisfile" path link)))
8133 (throw 'match t)))
8135 (when (get-text-property (point) 'org-linked-text)
8136 (setq type "thisfile"
8137 pos (if (get-text-property (1+ (point)) 'org-linked-text)
8138 (1+ (point)) (point))
8139 path (buffer-substring
8140 (previous-single-property-change pos 'org-linked-text)
8141 (next-single-property-change pos 'org-linked-text)))
8142 (throw 'match t))
8144 (save-excursion
8145 (when (or (org-in-regexp org-angle-link-re)
8146 (org-in-regexp org-plain-link-re))
8147 (setq type (match-string 1) path (match-string 2))
8148 (throw 'match t)))
8149 (save-excursion
8150 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
8151 (setq type "tags"
8152 path (match-string 1))
8153 (while (string-match ":" path)
8154 (setq path (replace-match "+" t t path)))
8155 (throw 'match t)))
8156 (when (org-in-regexp "<\\([^><\n]+\\)>")
8157 (setq type "tree-match"
8158 path (match-string 1))
8159 (throw 'match t)))
8160 (unless path
8161 (error "No link found"))
8163 ;; switch back to reference buffer
8164 ;; needed when if called in a temporary buffer through
8165 ;; org-open-link-from-string
8166 (with-current-buffer (or reference-buffer (current-buffer))
8168 ;; Remove any trailing spaces in path
8169 (if (string-match " +\\'" path)
8170 (setq path (replace-match "" t t path)))
8171 (if (and org-link-translation-function
8172 (fboundp org-link-translation-function))
8173 ;; Check if we need to translate the link
8174 (let ((tmp (funcall org-link-translation-function type path)))
8175 (setq type (car tmp) path (cdr tmp))))
8177 (cond
8179 ((assoc type org-link-protocols)
8180 (funcall (nth 1 (assoc type org-link-protocols)) path))
8182 ((equal type "mailto")
8183 (let ((cmd (car org-link-mailto-program))
8184 (args (cdr org-link-mailto-program)) args1
8185 (address path) (subject "") a)
8186 (if (string-match "\\(.*\\)::\\(.*\\)" path)
8187 (setq address (match-string 1 path)
8188 subject (org-link-escape (match-string 2 path))))
8189 (while args
8190 (cond
8191 ((not (stringp (car args))) (push (pop args) args1))
8192 (t (setq a (pop args))
8193 (if (string-match "%a" a)
8194 (setq a (replace-match address t t a)))
8195 (if (string-match "%s" a)
8196 (setq a (replace-match subject t t a)))
8197 (push a args1))))
8198 (apply cmd (nreverse args1))))
8200 ((member type '("http" "https" "ftp" "news"))
8201 (browse-url (concat type ":" (org-link-escape
8202 path org-link-escape-chars-browser))))
8204 ((member type '("message"))
8205 (browse-url (concat type ":" path)))
8207 ((string= type "tags")
8208 (org-tags-view in-emacs path))
8209 ((string= type "thisfile")
8210 (if in-emacs
8211 (switch-to-buffer-other-window
8212 (org-get-buffer-for-internal-link (current-buffer)))
8213 (org-mark-ring-push))
8214 (let ((cmd `(org-link-search
8215 ,path
8216 ,(cond ((equal in-emacs '(4)) 'occur)
8217 ((equal in-emacs '(16)) 'org-occur)
8218 (t nil))
8219 ,pos)))
8220 (condition-case nil (eval cmd)
8221 (error (progn (widen) (eval cmd))))))
8223 ((string= type "tree-match")
8224 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
8226 ((string= type "file")
8227 (if (string-match "::\\([0-9]+\\)\\'" path)
8228 (setq line (string-to-number (match-string 1 path))
8229 path (substring path 0 (match-beginning 0)))
8230 (if (string-match "::\\(.+\\)\\'" path)
8231 (setq search (match-string 1 path)
8232 path (substring path 0 (match-beginning 0)))))
8233 (if (string-match "[*?{]" (file-name-nondirectory path))
8234 (dired path)
8235 (org-open-file path in-emacs line search)))
8237 ((string= type "news")
8238 (require 'org-gnus)
8239 (org-gnus-follow-link path))
8241 ((string= type "shell")
8242 (let ((cmd path))
8243 (if (or (not org-confirm-shell-link-function)
8244 (funcall org-confirm-shell-link-function
8245 (format "Execute \"%s\" in shell? "
8246 (org-add-props cmd nil
8247 'face 'org-warning))))
8248 (progn
8249 (message "Executing %s" cmd)
8250 (shell-command cmd))
8251 (error "Abort"))))
8253 ((string= type "elisp")
8254 (let ((cmd path))
8255 (if (or (not org-confirm-elisp-link-function)
8256 (funcall org-confirm-elisp-link-function
8257 (format "Execute \"%s\" as elisp? "
8258 (org-add-props cmd nil
8259 'face 'org-warning))))
8260 (message "%s => %s" cmd
8261 (if (equal (string-to-char cmd) ?\()
8262 (eval (read cmd))
8263 (call-interactively (read cmd))))
8264 (error "Abort"))))
8267 (browse-url-at-point)))))))
8268 (move-marker org-open-link-marker nil)
8269 (run-hook-with-args 'org-follow-link-hook))
8271 (defun org-offer-links-in-entry (&optional nth zero)
8272 "Offer links in the curren entry and follow the selected link.
8273 If there is only one link, follow it immediately as well.
8274 If NTH is an integer, immediately pick the NTH link found.
8275 If ZERO is a string, check also this string for a link, and if
8276 there is one, offer it as link number zero."
8277 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
8278 "\\(" org-angle-link-re "\\)\\|"
8279 "\\(" org-plain-link-re "\\)"))
8280 (cnt ?0)
8281 (in-emacs (if (integerp nth) nil nth))
8282 have-zero end links link c)
8283 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
8284 (push (match-string 0 zero) links)
8285 (setq cnt (1- cnt) have-zero t))
8286 (save-excursion
8287 (org-back-to-heading t)
8288 (setq end (save-excursion (outline-next-heading) (point)))
8289 (while (re-search-forward re end t)
8290 (push (match-string 0) links))
8291 (setq links (org-uniquify (reverse links))))
8293 (cond
8294 ((null links)
8295 (message "No links"))
8296 ((equal (length links) 1)
8297 (setq link (car links)))
8298 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
8299 (setq link (nth (if have-zero nth (1- nth)) links)))
8300 (t ; we have to select a link
8301 (save-excursion
8302 (save-window-excursion
8303 (delete-other-windows)
8304 (with-output-to-temp-buffer "*Select Link*"
8305 (mapc (lambda (l)
8306 (if (not (string-match org-bracket-link-regexp l))
8307 (princ (format "[%c] %s\n" (incf cnt)
8308 (org-remove-angle-brackets l)))
8309 (if (match-end 3)
8310 (princ (format "[%c] %s (%s)\n" (incf cnt)
8311 (match-string 3 l) (match-string 1 l)))
8312 (princ (format "[%c] %s\n" (incf cnt)
8313 (match-string 1 l))))))
8314 links))
8315 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
8316 (message "Select link to open:")
8317 (setq c (read-char-exclusive))
8318 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
8319 (when (equal c ?q) (error "Abort"))
8320 (setq nth (- c ?0))
8321 (if have-zero (setq nth (1+ nth)))
8322 (unless (and (integerp nth) (>= (length links) nth))
8323 (error "Invalid link selection"))
8324 (setq link (nth (1- nth) links))))
8325 (if link
8326 (progn (org-open-link-from-string link in-emacs (current-buffer)) t)
8327 nil)))
8329 ;;;; Time estimates
8331 (defun org-get-effort (&optional pom)
8332 "Get the effort estimate for the current entry."
8333 (org-entry-get pom org-effort-property))
8335 ;;; File search
8337 (defvar org-create-file-search-functions nil
8338 "List of functions to construct the right search string for a file link.
8339 These functions are called in turn with point at the location to
8340 which the link should point.
8342 A function in the hook should first test if it would like to
8343 handle this file type, for example by checking the major-mode or
8344 the file extension. If it decides not to handle this file, it
8345 should just return nil to give other functions a chance. If it
8346 does handle the file, it must return the search string to be used
8347 when following the link. The search string will be part of the
8348 file link, given after a double colon, and `org-open-at-point'
8349 will automatically search for it. If special measures must be
8350 taken to make the search successful, another function should be
8351 added to the companion hook `org-execute-file-search-functions',
8352 which see.
8354 A function in this hook may also use `setq' to set the variable
8355 `description' to provide a suggestion for the descriptive text to
8356 be used for this link when it gets inserted into an Org-mode
8357 buffer with \\[org-insert-link].")
8359 (defvar org-execute-file-search-functions nil
8360 "List of functions to execute a file search triggered by a link.
8362 Functions added to this hook must accept a single argument, the
8363 search string that was part of the file link, the part after the
8364 double colon. The function must first check if it would like to
8365 handle this search, for example by checking the major-mode or the
8366 file extension. If it decides not to handle this search, it
8367 should just return nil to give other functions a chance. If it
8368 does handle the search, it must return a non-nil value to keep
8369 other functions from trying.
8371 Each function can access the current prefix argument through the
8372 variable `current-prefix-argument'. Note that a single prefix is
8373 used to force opening a link in Emacs, so it may be good to only
8374 use a numeric or double prefix to guide the search function.
8376 In case this is needed, a function in this hook can also restore
8377 the window configuration before `org-open-at-point' was called using:
8379 (set-window-configuration org-window-config-before-follow-link)")
8381 (defun org-link-search (s &optional type avoid-pos)
8382 "Search for a link search option.
8383 If S is surrounded by forward slashes, it is interpreted as a
8384 regular expression. In org-mode files, this will create an `org-occur'
8385 sparse tree. In ordinary files, `occur' will be used to list matches.
8386 If the current buffer is in `dired-mode', grep will be used to search
8387 in all files. If AVOID-POS is given, ignore matches near that position."
8388 (let ((case-fold-search t)
8389 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
8390 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
8391 (append '(("") (" ") ("\t") ("\n"))
8392 org-emphasis-alist)
8393 "\\|") "\\)"))
8394 (pos (point))
8395 (pre nil) (post nil)
8396 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
8397 (cond
8398 ;; First check if there are any special
8399 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
8400 ;; Now try the builtin stuff
8401 ((and (equal (string-to-char s0) ?#)
8402 (> (length s0) 1)
8403 (save-excursion
8404 (goto-char (point-min))
8405 (and
8406 (re-search-forward
8407 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
8408 (setq type 'dedicated
8409 pos (match-beginning 0))))
8410 ;; There is an exact target for this
8411 (goto-char pos)
8412 (org-back-to-heading t)))
8413 ((save-excursion
8414 (goto-char (point-min))
8415 (and
8416 (re-search-forward
8417 (concat "<<" (regexp-quote s0) ">>") nil t)
8418 (setq type 'dedicated
8419 pos (match-beginning 0))))
8420 ;; There is an exact target for this
8421 (goto-char pos))
8422 ((and (string-match "^(\\(.*\\))$" s0)
8423 (save-excursion
8424 (goto-char (point-min))
8425 (and
8426 (re-search-forward
8427 (concat "[^[]" (regexp-quote
8428 (format org-coderef-label-format
8429 (match-string 1 s0))))
8430 nil t)
8431 (setq type 'dedicated
8432 pos (1+ (match-beginning 0))))))
8433 ;; There is a coderef target for this
8434 (goto-char pos))
8435 ((string-match "^/\\(.*\\)/$" s)
8436 ;; A regular expression
8437 (cond
8438 ((org-mode-p)
8439 (org-occur (match-string 1 s)))
8440 ;;((eq major-mode 'dired-mode)
8441 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
8442 (t (org-do-occur (match-string 1 s)))))
8444 ;; A normal search strings
8445 (when (equal (string-to-char s) ?*)
8446 ;; Anchor on headlines, post may include tags.
8447 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
8448 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
8449 s (substring s 1)))
8450 (remove-text-properties
8451 0 (length s)
8452 '(face nil mouse-face nil keymap nil fontified nil) s)
8453 ;; Make a series of regular expressions to find a match
8454 (setq words (org-split-string s "[ \n\r\t]+")
8456 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
8457 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
8458 "\\)" markers)
8459 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
8460 re2a (concat "[ \t\r\n]" re2a_)
8461 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
8462 re4 (concat "[^a-zA-Z_]" re4_)
8464 re1 (concat pre re2 post)
8465 re3 (concat pre (if pre re4_ re4) post)
8466 re5 (concat pre ".*" re4)
8467 re2 (concat pre re2)
8468 re2a (concat pre (if pre re2a_ re2a))
8469 re4 (concat pre (if pre re4_ re4))
8470 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
8471 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
8472 re5 "\\)"
8474 (cond
8475 ((eq type 'org-occur) (org-occur reall))
8476 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
8477 (t (goto-char (point-min))
8478 (setq type 'fuzzy)
8479 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
8480 (org-search-not-self 1 re1 nil t)
8481 (org-search-not-self 1 re2 nil t)
8482 (org-search-not-self 1 re2a nil t)
8483 (org-search-not-self 1 re3 nil t)
8484 (org-search-not-self 1 re4 nil t)
8485 (org-search-not-self 1 re5 nil t)
8487 (goto-char (match-beginning 1))
8488 (goto-char pos)
8489 (error "No match")))))
8491 ;; Normal string-search
8492 (goto-char (point-min))
8493 (if (search-forward s nil t)
8494 (goto-char (match-beginning 0))
8495 (error "No match"))))
8496 (and (org-mode-p) (org-show-context 'link-search))
8497 type))
8499 (defun org-search-not-self (group &rest args)
8500 "Execute `re-search-forward', but only accept matches that do not
8501 enclose the position of `org-open-link-marker'."
8502 (let ((m org-open-link-marker))
8503 (catch 'exit
8504 (while (apply 're-search-forward args)
8505 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
8506 (goto-char (match-end group))
8507 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
8508 (> (match-beginning 0) (marker-position m))
8509 (< (match-end 0) (marker-position m)))
8510 (save-match-data
8511 (or (not (org-in-regexp
8512 org-bracket-link-analytic-regexp 1))
8513 (not (match-end 4)) ; no description
8514 (and (<= (match-beginning 4) (point))
8515 (>= (match-end 4) (point))))))
8516 (throw 'exit (point))))))))
8518 (defun org-get-buffer-for-internal-link (buffer)
8519 "Return a buffer to be used for displaying the link target of internal links."
8520 (cond
8521 ((not org-display-internal-link-with-indirect-buffer)
8522 buffer)
8523 ((string-match "(Clone)$" (buffer-name buffer))
8524 (message "Buffer is already a clone, not making another one")
8525 ;; we also do not modify visibility in this case
8526 buffer)
8527 (t ; make a new indirect buffer for displaying the link
8528 (let* ((bn (buffer-name buffer))
8529 (ibn (concat bn "(Clone)"))
8530 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
8531 (with-current-buffer ib (org-overview))
8532 ib))))
8534 (defun org-do-occur (regexp &optional cleanup)
8535 "Call the Emacs command `occur'.
8536 If CLEANUP is non-nil, remove the printout of the regular expression
8537 in the *Occur* buffer. This is useful if the regex is long and not useful
8538 to read."
8539 (occur regexp)
8540 (when cleanup
8541 (let ((cwin (selected-window)) win beg end)
8542 (when (setq win (get-buffer-window "*Occur*"))
8543 (select-window win))
8544 (goto-char (point-min))
8545 (when (re-search-forward "match[a-z]+" nil t)
8546 (setq beg (match-end 0))
8547 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
8548 (setq end (1- (match-beginning 0)))))
8549 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
8550 (goto-char (point-min))
8551 (select-window cwin))))
8553 ;;; The mark ring for links jumps
8555 (defvar org-mark-ring nil
8556 "Mark ring for positions before jumps in Org-mode.")
8557 (defvar org-mark-ring-last-goto nil
8558 "Last position in the mark ring used to go back.")
8559 ;; Fill and close the ring
8560 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
8561 (loop for i from 1 to org-mark-ring-length do
8562 (push (make-marker) org-mark-ring))
8563 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
8564 org-mark-ring)
8566 (defun org-mark-ring-push (&optional pos buffer)
8567 "Put the current position or POS into the mark ring and rotate it."
8568 (interactive)
8569 (setq pos (or pos (point)))
8570 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
8571 (move-marker (car org-mark-ring)
8572 (or pos (point))
8573 (or buffer (current-buffer)))
8574 (message "%s"
8575 (substitute-command-keys
8576 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
8578 (defun org-mark-ring-goto (&optional n)
8579 "Jump to the previous position in the mark ring.
8580 With prefix arg N, jump back that many stored positions. When
8581 called several times in succession, walk through the entire ring.
8582 Org-mode commands jumping to a different position in the current file,
8583 or to another Org-mode file, automatically push the old position
8584 onto the ring."
8585 (interactive "p")
8586 (let (p m)
8587 (if (eq last-command this-command)
8588 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
8589 (setq p org-mark-ring))
8590 (setq org-mark-ring-last-goto p)
8591 (setq m (car p))
8592 (switch-to-buffer (marker-buffer m))
8593 (goto-char m)
8594 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
8596 (defun org-remove-angle-brackets (s)
8597 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
8598 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
8600 (defun org-add-angle-brackets (s)
8601 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
8602 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
8604 (defun org-remove-double-quotes (s)
8605 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
8606 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
8609 ;;; Following specific links
8611 (defun org-follow-timestamp-link ()
8612 (cond
8613 ((org-at-date-range-p t)
8614 (let ((org-agenda-start-on-weekday)
8615 (t1 (match-string 1))
8616 (t2 (match-string 2)))
8617 (setq t1 (time-to-days (org-time-string-to-time t1))
8618 t2 (time-to-days (org-time-string-to-time t2)))
8619 (org-agenda-list nil t1 (1+ (- t2 t1)))))
8620 ((org-at-timestamp-p t)
8621 (org-agenda-list nil (time-to-days (org-time-string-to-time
8622 (substring (match-string 1) 0 10)))
8624 (t (error "This should not happen"))))
8627 ;;; Following file links
8628 (defvar org-wait nil)
8629 (defun org-open-file (path &optional in-emacs line search)
8630 "Open the file at PATH.
8631 First, this expands any special file name abbreviations. Then the
8632 configuration variable `org-file-apps' is checked if it contains an
8633 entry for this file type, and if yes, the corresponding command is launched.
8635 If no application is found, Emacs simply visits the file.
8637 With optional prefix argument IN-EMACS, Emacs will visit the file.
8638 With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
8639 and o use an external application to visit the file.
8641 Optional LINE specifies a line to go to, optional SEARCH a string to
8642 search for. If LINE or SEARCH is given, the file will always be
8643 opened in Emacs.
8644 If the file does not exist, an error is thrown."
8645 (setq in-emacs (or in-emacs line search))
8646 (let* ((file (if (equal path "")
8647 buffer-file-name
8648 (substitute-in-file-name (expand-file-name path))))
8649 (apps (append org-file-apps (org-default-apps)))
8650 (remp (and (assq 'remote apps) (org-file-remote-p file)))
8651 (dirp (if remp nil (file-directory-p file)))
8652 (file (if (and dirp org-open-directory-means-index-dot-org)
8653 (concat (file-name-as-directory file) "index.org")
8654 file))
8655 (a-m-a-p (assq 'auto-mode apps))
8656 (dfile (downcase file))
8657 (old-buffer (current-buffer))
8658 (old-pos (point))
8659 (old-mode major-mode)
8660 ext cmd)
8661 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
8662 (setq ext (match-string 1 dfile))
8663 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
8664 (setq ext (match-string 1 dfile))))
8665 (cond
8666 ((equal in-emacs '(16))
8667 (setq cmd (cdr (assoc 'system apps))))
8668 (in-emacs (setq cmd 'emacs))
8670 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
8671 (and dirp (cdr (assoc 'directory apps)))
8672 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
8673 'string-match)
8674 (cdr (assoc ext apps))
8675 (cdr (assoc t apps))))))
8676 (when (eq cmd 'system)
8677 (setq cmd (cdr (assoc 'system apps))))
8678 (when (eq cmd 'default)
8679 (setq cmd (cdr (assoc t apps))))
8680 (when (eq cmd 'mailcap)
8681 (require 'mailcap)
8682 (mailcap-parse-mailcaps)
8683 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
8684 (command (mailcap-mime-info mime-type)))
8685 (if (stringp command)
8686 (setq cmd command)
8687 (setq cmd 'emacs))))
8688 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
8689 (not (file-exists-p file))
8690 (not org-open-non-existing-files))
8691 (error "No such file: %s" file))
8692 (cond
8693 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
8694 ;; Remove quotes around the file name - we'll use shell-quote-argument.
8695 (while (string-match "['\"]%s['\"]" cmd)
8696 (setq cmd (replace-match "%s" t t cmd)))
8697 (while (string-match "%s" cmd)
8698 (setq cmd (replace-match
8699 (save-match-data
8700 (shell-quote-argument
8701 (convert-standard-filename file)))
8702 t t cmd)))
8703 (save-window-excursion
8704 (start-process-shell-command cmd nil cmd)
8705 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
8707 ((or (stringp cmd)
8708 (eq cmd 'emacs))
8709 (funcall (cdr (assq 'file org-link-frame-setup)) file)
8710 (widen)
8711 (if line (org-goto-line line)
8712 (if search (org-link-search search))))
8713 ((consp cmd)
8714 (let ((file (convert-standard-filename file)))
8715 (eval cmd)))
8716 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
8717 (and (org-mode-p) (eq old-mode 'org-mode)
8718 (or (not (equal old-buffer (current-buffer)))
8719 (not (equal old-pos (point))))
8720 (org-mark-ring-push old-pos old-buffer))))
8722 (defun org-default-apps ()
8723 "Return the default applications for this operating system."
8724 (cond
8725 ((eq system-type 'darwin)
8726 org-file-apps-defaults-macosx)
8727 ((eq system-type 'windows-nt)
8728 org-file-apps-defaults-windowsnt)
8729 (t org-file-apps-defaults-gnu)))
8731 (defun org-apps-regexp-alist (list &optional add-auto-mode)
8732 "Convert extensions to regular expressions in the cars of LIST.
8733 Also, weed out any non-string entries, because the return value is used
8734 only for regexp matching.
8735 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
8736 point to the symbol `emacs', indicating that the file should
8737 be opened in Emacs."
8738 (append
8739 (delq nil
8740 (mapcar (lambda (x)
8741 (if (not (stringp (car x)))
8743 (if (string-match "\\W" (car x))
8745 (cons (concat "\\." (car x) "\\'") (cdr x)))))
8746 list))
8747 (if add-auto-mode
8748 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
8750 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
8751 (defun org-file-remote-p (file)
8752 "Test whether FILE specifies a location on a remote system.
8753 Return non-nil if the location is indeed remote.
8755 For example, the filename \"/user@host:/foo\" specifies a location
8756 on the system \"/user@host:\"."
8757 (cond ((fboundp 'file-remote-p)
8758 (file-remote-p file))
8759 ((fboundp 'tramp-handle-file-remote-p)
8760 (tramp-handle-file-remote-p file))
8761 ((and (boundp 'ange-ftp-name-format)
8762 (string-match (car ange-ftp-name-format) file))
8764 (t nil)))
8767 ;;;; Refiling
8769 (defun org-get-org-file ()
8770 "Read a filename, with default directory `org-directory'."
8771 (let ((default (or org-default-notes-file remember-data-file)))
8772 (read-file-name (format "File name [%s]: " default)
8773 (file-name-as-directory org-directory)
8774 default)))
8776 (defun org-notes-order-reversed-p ()
8777 "Check if the current file should receive notes in reversed order."
8778 (cond
8779 ((not org-reverse-note-order) nil)
8780 ((eq t org-reverse-note-order) t)
8781 ((not (listp org-reverse-note-order)) nil)
8782 (t (catch 'exit
8783 (let ((all org-reverse-note-order)
8784 entry)
8785 (while (setq entry (pop all))
8786 (if (string-match (car entry) buffer-file-name)
8787 (throw 'exit (cdr entry))))
8788 nil)))))
8790 (defvar org-refile-target-table nil
8791 "The list of refile targets, created by `org-refile'.")
8793 (defvar org-agenda-new-buffers nil
8794 "Buffers created to visit agenda files.")
8796 (defun org-get-refile-targets (&optional default-buffer)
8797 "Produce a table with refile targets."
8798 (let ((case-fold-search nil)
8799 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
8800 (entries (or org-refile-targets '((nil . (:level . 1)))))
8801 targets txt re files f desc descre fast-path-p level pos0)
8802 (message "Getting targets...")
8803 (with-current-buffer (or default-buffer (current-buffer))
8804 (while (setq entry (pop entries))
8805 (setq files (car entry) desc (cdr entry))
8806 (setq fast-path-p nil)
8807 (cond
8808 ((null files) (setq files (list (current-buffer))))
8809 ((eq files 'org-agenda-files)
8810 (setq files (org-agenda-files 'unrestricted)))
8811 ((and (symbolp files) (fboundp files))
8812 (setq files (funcall files)))
8813 ((and (symbolp files) (boundp files))
8814 (setq files (symbol-value files))))
8815 (if (stringp files) (setq files (list files)))
8816 (cond
8817 ((eq (car desc) :tag)
8818 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
8819 ((eq (car desc) :todo)
8820 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
8821 ((eq (car desc) :regexp)
8822 (setq descre (cdr desc)))
8823 ((eq (car desc) :level)
8824 (setq descre (concat "^\\*\\{" (number-to-string
8825 (if org-odd-levels-only
8826 (1- (* 2 (cdr desc)))
8827 (cdr desc)))
8828 "\\}[ \t]")))
8829 ((eq (car desc) :maxlevel)
8830 (setq fast-path-p t)
8831 (setq descre (concat "^\\*\\{1," (number-to-string
8832 (if org-odd-levels-only
8833 (1- (* 2 (cdr desc)))
8834 (cdr desc)))
8835 "\\}[ \t]")))
8836 (t (error "Bad refiling target description %s" desc)))
8837 (while (setq f (pop files))
8838 (save-excursion
8839 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
8840 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
8841 (setq f (expand-file-name f))
8842 (if (eq org-refile-use-outline-path 'file)
8843 (push (list (file-name-nondirectory f) f nil nil) targets))
8844 (save-excursion
8845 (save-restriction
8846 (widen)
8847 (goto-char (point-min))
8848 (while (re-search-forward descre nil t)
8849 (goto-char (setq pos0 (point-at-bol)))
8850 (catch 'next
8851 (when org-refile-target-verify-function
8852 (save-match-data
8853 (or (funcall org-refile-target-verify-function)
8854 (throw 'next t))))
8855 (when (looking-at org-complex-heading-regexp)
8856 (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
8857 txt (org-link-display-format (match-string 4))
8858 re (concat "^" (regexp-quote
8859 (buffer-substring (match-beginning 1)
8860 (match-end 4)))))
8861 (if (match-end 5) (setq re (concat re "[ \t]+"
8862 (regexp-quote
8863 (match-string 5)))))
8864 (setq re (concat re "[ \t]*$"))
8865 (when org-refile-use-outline-path
8866 (setq txt (mapconcat 'org-protect-slash
8867 (append
8868 (if (eq org-refile-use-outline-path 'file)
8869 (list (file-name-nondirectory
8870 (buffer-file-name (buffer-base-buffer))))
8871 (if (eq org-refile-use-outline-path 'full-file-path)
8872 (list (buffer-file-name (buffer-base-buffer)))))
8873 (org-get-outline-path fast-path-p level txt)
8874 (list txt))
8875 "/")))
8876 (push (list txt f re (point)) targets)))
8877 (when (= (point) pos0)
8878 ;; verification function has not moved point
8879 (goto-char (point-at-eol))))))))))
8880 (message "Getting targets...done")
8881 (nreverse targets)))
8883 (defun org-protect-slash (s)
8884 (while (string-match "/" s)
8885 (setq s (replace-match "\\" t t s)))
8888 (defvar org-olpa (make-vector 20 nil))
8890 (defun org-get-outline-path (&optional fastp level heading)
8891 "Return the outline path to the current entry, as a list."
8892 (if fastp
8893 (progn
8894 (if (> level 19)
8895 (error "Outline path failure, more than 19 levels."))
8896 (loop for i from level upto 19 do
8897 (aset org-olpa i nil))
8898 (prog1
8899 (delq nil (append org-olpa nil))
8900 (aset org-olpa level heading)))
8901 (let (rtn)
8902 (save-excursion
8903 (while (org-up-heading-safe)
8904 (when (looking-at org-complex-heading-regexp)
8905 (push (org-match-string-no-properties 4) rtn)))
8906 rtn))))
8908 (defvar org-refile-history nil
8909 "History for refiling operations.")
8911 (defvar org-after-refile-insert-hook nil
8912 "Hook run after `org-refile' has inserted its stuff at the new location.
8913 Note that this is still *before* the stuff will be removed from
8914 the *old* location.")
8916 (defun org-refile (&optional goto default-buffer rfloc)
8917 "Move the entry at point to another heading.
8918 The list of target headings is compiled using the information in
8919 `org-refile-targets', which see. This list is created before each use
8920 and will therefore always be up-to-date.
8922 At the target location, the entry is filed as a subitem of the target heading.
8923 Depending on `org-reverse-note-order', the new subitem will either be the
8924 first or the last subitem.
8926 If there is an active region, all entries in that region will be moved.
8927 However, the region must fulfil the requirement that the first heading
8928 is the first one sets the top-level of the moved text - at most siblings
8929 below it are allowed.
8931 With prefix arg GOTO, the command will only visit the target location,
8932 not actually move anything.
8933 With a double prefix `C-u C-u', go to the location where the last refiling
8934 operation has put the subtree.
8935 With a prefix argument of `2', refile to the running clock.
8937 RFLOC can be a refile location obtained in a different way.
8939 See also `org-refile-use-outline-path' and `org-completion-use-ido'"
8940 (interactive "P")
8941 (let* ((cbuf (current-buffer))
8942 (regionp (org-region-active-p))
8943 (region-start (and regionp (region-beginning)))
8944 (region-end (and regionp (region-end)))
8945 (region-length (and regionp (- region-end region-start)))
8946 (filename (buffer-file-name (buffer-base-buffer cbuf)))
8947 pos it nbuf file re level reversed)
8948 (when regionp
8949 (goto-char region-start)
8950 (or (bolp) (goto-char (point-at-bol)))
8951 (setq region-start (point))
8952 (unless (org-kill-is-subtree-p
8953 (buffer-substring region-start region-end))
8954 (error "The region is not a (sequence of) subtree(s)")))
8955 (if (equal goto '(16))
8956 (org-refile-goto-last-stored)
8957 (when (or
8958 (and org-clock-hd-marker (marker-buffer org-clock-hd-marker)
8959 (prog1
8960 (setq it (list (or org-clock-heading "running clock")
8961 (buffer-file-name
8962 (marker-buffer org-clock-hd-marker))
8964 (marker-position org-clock-hd-marker)))
8965 (setq goto nil)))
8966 (setq it (or rfloc
8967 (save-excursion
8968 (org-refile-get-location
8969 (if goto "Goto: " "Refile to: ") default-buffer
8970 org-refile-allow-creating-parent-nodes)))))
8971 (setq file (nth 1 it)
8972 re (nth 2 it)
8973 pos (nth 3 it))
8974 (if (and (not goto)
8976 (equal (buffer-file-name) file)
8977 (if regionp
8978 (and (>= pos region-start)
8979 (<= pos region-end))
8980 (and (>= pos (point))
8981 (< pos (save-excursion
8982 (org-end-of-subtree t t))))))
8983 (error "Cannot refile to position inside the tree or region"))
8985 (setq nbuf (or (find-buffer-visiting file)
8986 (find-file-noselect file)))
8987 (if goto
8988 (progn
8989 (switch-to-buffer nbuf)
8990 (goto-char pos)
8991 (org-show-context 'org-goto))
8992 (if regionp
8993 (progn
8994 (org-kill-new (buffer-substring region-start region-end))
8995 (org-save-markers-in-region region-start region-end))
8996 (org-copy-subtree 1 nil t))
8997 (save-excursion
8998 (set-buffer (setq nbuf (or (find-buffer-visiting file)
8999 (find-file-noselect file))))
9000 (setq reversed (org-notes-order-reversed-p))
9001 (save-excursion
9002 (save-restriction
9003 (widen)
9004 (if pos
9005 (progn
9006 (goto-char pos)
9007 (looking-at outline-regexp)
9008 (setq level (org-get-valid-level (funcall outline-level) 1))
9009 (goto-char
9010 (if reversed
9011 (or (outline-next-heading) (point-max))
9012 (or (save-excursion (org-get-next-sibling))
9013 (org-end-of-subtree t t)
9014 (point-max)))))
9015 (setq level 1)
9016 (if (not reversed)
9017 (goto-char (point-max))
9018 (goto-char (point-min))
9019 (or (outline-next-heading) (goto-char (point-max)))))
9020 (if (not (bolp)) (newline))
9021 (bookmark-set "org-refile-last-stored")
9022 (org-paste-subtree level)
9023 (if (fboundp 'deactivate-mark) (deactivate-mark))
9024 (run-hooks 'org-after-refile-insert-hook))))
9025 (if regionp
9026 (delete-region (point) (+ (point) region-length))
9027 (org-cut-subtree))
9028 (when (featurep 'org-inlinetask)
9029 (org-inlinetask-remove-END-maybe))
9030 (setq org-markers-to-move nil)
9031 (message "Refiled to \"%s\"" (car it))))))
9032 (org-reveal))
9034 (defun org-refile-goto-last-stored ()
9035 "Go to the location where the last refile was stored."
9036 (interactive)
9037 (bookmark-jump "org-refile-last-stored")
9038 (message "This is the location of the last refile"))
9040 (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
9041 "Prompt the user for a refile location, using PROMPT."
9042 (let ((org-refile-targets org-refile-targets)
9043 (org-refile-use-outline-path org-refile-use-outline-path))
9044 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
9045 (unless org-refile-target-table
9046 (error "No refile targets"))
9047 (let* ((cbuf (current-buffer))
9048 (partial-completion-mode nil)
9049 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
9050 (cfunc (if (and org-refile-use-outline-path
9051 org-outline-path-complete-in-steps)
9052 'org-olpath-completing-read
9053 'org-icompleting-read))
9054 (extra (if org-refile-use-outline-path "/" ""))
9055 (filename (and cfn (expand-file-name cfn)))
9056 (tbl (mapcar
9057 (lambda (x)
9058 (if (and (not (member org-refile-use-outline-path
9059 '(file full-file-path)))
9060 (not (equal filename (nth 1 x))))
9061 (cons (concat (car x) extra " ("
9062 (file-name-nondirectory (nth 1 x)) ")")
9063 (cdr x))
9064 (cons (concat (car x) extra) (cdr x))))
9065 org-refile-target-table))
9066 (completion-ignore-case t)
9067 pa answ parent-target child parent old-hist)
9068 (setq old-hist org-refile-history)
9069 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
9070 nil 'org-refile-history))
9071 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
9072 (if pa
9073 (progn
9074 (when (or (not org-refile-history)
9075 (not (eq old-hist org-refile-history))
9076 (not (equal (car pa) (car org-refile-history))))
9077 (setq org-refile-history
9078 (cons (car pa) (if (assoc (car org-refile-history) tbl)
9079 org-refile-history
9080 (cdr org-refile-history))))
9081 (if (equal (car org-refile-history) (nth 1 org-refile-history))
9082 (pop org-refile-history)))
9084 (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
9085 (setq parent (match-string 1 answ)
9086 child (match-string 2 answ))
9087 (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
9088 (when (and parent-target
9089 (or (eq new-nodes t)
9090 (and (eq new-nodes 'confirm)
9091 (y-or-n-p (format "Create new node \"%s\"? " child)))))
9092 (org-refile-new-child parent-target child))))))
9094 (defun org-refile-new-child (parent-target child)
9095 "Use refile target PARENT-TARGET to add new CHILD below it."
9096 (unless parent-target
9097 (error "Cannot find parent for new node"))
9098 (let ((file (nth 1 parent-target))
9099 (pos (nth 3 parent-target))
9100 level)
9101 (with-current-buffer (or (find-buffer-visiting file)
9102 (find-file-noselect file))
9103 (save-excursion
9104 (save-restriction
9105 (widen)
9106 (if pos
9107 (goto-char pos)
9108 (goto-char (point-max))
9109 (if (not (bolp)) (newline)))
9110 (when (looking-at outline-regexp)
9111 (setq level (funcall outline-level))
9112 (org-end-of-subtree t t))
9113 (org-back-over-empty-lines)
9114 (insert "\n" (make-string
9115 (if pos (org-get-valid-level level 1) 1) ?*)
9116 " " child "\n")
9117 (beginning-of-line 0)
9118 (list (concat (car parent-target) "/" child) file "" (point)))))))
9120 (defun org-olpath-completing-read (prompt collection &rest args)
9121 "Read an outline path like a file name."
9122 (let ((thetable collection)
9123 (org-completion-use-ido nil) ; does not work with ido.
9124 (org-completion-use-iswitchb nil)) ; or iswitchb
9125 (apply
9126 'org-icompleting-read prompt
9127 (lambda (string predicate &optional flag)
9128 (let (rtn r f (l (length string)))
9129 (cond
9130 ((eq flag nil)
9131 ;; try completion
9132 (try-completion string thetable))
9133 ((eq flag t)
9134 ;; all-completions
9135 (setq rtn (all-completions string thetable predicate))
9136 (mapcar
9137 (lambda (x)
9138 (setq r (substring x l))
9139 (if (string-match " ([^)]*)$" x)
9140 (setq f (match-string 0 x))
9141 (setq f ""))
9142 (if (string-match "/" r)
9143 (concat string (substring r 0 (match-end 0)) f)
9145 rtn))
9146 ((eq flag 'lambda)
9147 ;; exact match?
9148 (assoc string thetable)))
9150 args)))
9152 ;;;; Dynamic blocks
9154 (defun org-find-dblock (name)
9155 "Find the first dynamic block with name NAME in the buffer.
9156 If not found, stay at current position and return nil."
9157 (let (pos)
9158 (save-excursion
9159 (goto-char (point-min))
9160 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
9161 nil t)
9162 (match-beginning 0))))
9163 (if pos (goto-char pos))
9164 pos))
9166 (defconst org-dblock-start-re
9167 "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
9168 "Matches the startline of a dynamic block, with parameters.")
9170 (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
9171 "Matches the end of a dynamic block.")
9173 (defun org-create-dblock (plist)
9174 "Create a dynamic block section, with parameters taken from PLIST.
9175 PLIST must contain a :name entry which is used as name of the block."
9176 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
9177 (end-of-line 1)
9178 (newline))
9179 (let ((col (current-column))
9180 (name (plist-get plist :name)))
9181 (insert "#+BEGIN: " name)
9182 (while plist
9183 (if (eq (car plist) :name)
9184 (setq plist (cddr plist))
9185 (insert " " (prin1-to-string (pop plist)))))
9186 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
9187 (beginning-of-line -2)))
9189 (defun org-prepare-dblock ()
9190 "Prepare dynamic block for refresh.
9191 This empties the block, puts the cursor at the insert position and returns
9192 the property list including an extra property :name with the block name."
9193 (unless (looking-at org-dblock-start-re)
9194 (error "Not at a dynamic block"))
9195 (let* ((begdel (1+ (match-end 0)))
9196 (name (org-no-properties (match-string 1)))
9197 (params (append (list :name name)
9198 (read (concat "(" (match-string 3) ")")))))
9199 (save-excursion
9200 (beginning-of-line 1)
9201 (skip-chars-forward " \t")
9202 (setq params (plist-put params :indentation-column (current-column))))
9203 (unless (re-search-forward org-dblock-end-re nil t)
9204 (error "Dynamic block not terminated"))
9205 (setq params
9206 (append params
9207 (list :content (buffer-substring
9208 begdel (match-beginning 0)))))
9209 (delete-region begdel (match-beginning 0))
9210 (goto-char begdel)
9211 (open-line 1)
9212 params))
9214 (defun org-map-dblocks (&optional command)
9215 "Apply COMMAND to all dynamic blocks in the current buffer.
9216 If COMMAND is not given, use `org-update-dblock'."
9217 (let ((cmd (or command 'org-update-dblock))
9218 pos)
9219 (save-excursion
9220 (goto-char (point-min))
9221 (while (re-search-forward org-dblock-start-re nil t)
9222 (goto-char (setq pos (match-beginning 0)))
9223 (condition-case nil
9224 (funcall cmd)
9225 (error (message "Error during update of dynamic block")))
9226 (goto-char pos)
9227 (unless (re-search-forward org-dblock-end-re nil t)
9228 (error "Dynamic block not terminated"))))))
9230 (defun org-dblock-update (&optional arg)
9231 "User command for updating dynamic blocks.
9232 Update the dynamic block at point. With prefix ARG, update all dynamic
9233 blocks in the buffer."
9234 (interactive "P")
9235 (if arg
9236 (org-update-all-dblocks)
9237 (or (looking-at org-dblock-start-re)
9238 (org-beginning-of-dblock))
9239 (org-update-dblock)))
9241 (defun org-update-dblock ()
9242 "Update the dynamic block at point
9243 This means to empty the block, parse for parameters and then call
9244 the correct writing function."
9245 (save-window-excursion
9246 (let* ((pos (point))
9247 (line (org-current-line))
9248 (params (org-prepare-dblock))
9249 (name (plist-get params :name))
9250 (indent (plist-get params :indentation-column))
9251 (cmd (intern (concat "org-dblock-write:" name))))
9252 (message "Updating dynamic block `%s' at line %d..." name line)
9253 (funcall cmd params)
9254 (message "Updating dynamic block `%s' at line %d...done" name line)
9255 (goto-char pos)
9256 (when (and indent (> indent 0))
9257 (setq indent (make-string indent ?\ ))
9258 (save-excursion
9259 (org-beginning-of-dblock)
9260 (forward-line 1)
9261 (while (not (looking-at org-dblock-end-re))
9262 (insert indent)
9263 (beginning-of-line 2))
9264 (when (looking-at org-dblock-end-re)
9265 (and (looking-at "[ \t]+")
9266 (replace-match ""))
9267 (insert indent)))))))
9269 (defun org-beginning-of-dblock ()
9270 "Find the beginning of the dynamic block at point.
9271 Error if there is no such block at point."
9272 (let ((pos (point))
9273 beg)
9274 (end-of-line 1)
9275 (if (and (re-search-backward org-dblock-start-re nil t)
9276 (setq beg (match-beginning 0))
9277 (re-search-forward org-dblock-end-re nil t)
9278 (> (match-end 0) pos))
9279 (goto-char beg)
9280 (goto-char pos)
9281 (error "Not in a dynamic block"))))
9283 (defun org-update-all-dblocks ()
9284 "Update all dynamic blocks in the buffer.
9285 This function can be used in a hook."
9286 (when (org-mode-p)
9287 (org-map-dblocks 'org-update-dblock)))
9290 ;;;; Completion
9292 (defconst org-additional-option-like-keywords
9293 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
9294 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
9295 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "LATEX_CLASS:" "ATTR_LaTeX"
9296 "BEGIN:" "END:"
9297 "ORGTBL" "TBLFM:" "TBLNAME:"
9298 "BEGIN_EXAMPLE" "END_EXAMPLE"
9299 "BEGIN_QUOTE" "END_QUOTE"
9300 "BEGIN_VERSE" "END_VERSE"
9301 "BEGIN_CENTER" "END_CENTER"
9302 "BEGIN_SRC" "END_SRC"
9303 "CATEGORY" "COLUMNS"
9304 "CAPTION" "LABEL"
9305 "SETUPFILE"
9306 "BIND"
9307 "MACRO"))
9309 (defcustom org-structure-template-alist
9311 ("s" "#+begin_src ?\n\n#+end_src"
9312 "<src lang=\"?\">\n\n</src>")
9313 ("e" "#+begin_example\n?\n#+end_example"
9314 "<example>\n?\n</example>")
9315 ("q" "#+begin_quote\n?\n#+end_quote"
9316 "<quote>\n?\n</quote>")
9317 ("v" "#+begin_verse\n?\n#+end_verse"
9318 "<verse>\n?\n/verse>")
9319 ("c" "#+begin_center\n?\n#+end_center"
9320 "<center>\n?\n/center>")
9321 ("l" "#+begin_latex\n?\n#+end_latex"
9322 "<literal style=\"latex\">\n?\n</literal>")
9323 ("L" "#+latex: "
9324 "<literal style=\"latex\">?</literal>")
9325 ("h" "#+begin_html\n?\n#+end_html"
9326 "<literal style=\"html\">\n?\n</literal>")
9327 ("H" "#+html: "
9328 "<literal style=\"html\">?</literal>")
9329 ("a" "#+begin_ascii\n?\n#+end_ascii")
9330 ("A" "#+ascii: ")
9331 ("i" "#+include %file ?"
9332 "<include file=%file markup=\"?\">")
9334 "Structure completion elements.
9335 This is a list of abbreviation keys and values. The value gets inserted
9336 it you type @samp{.} followed by the key and then the completion key,
9337 usually `M-TAB'. %file will be replaced by a file name after prompting
9338 for the file using completion.
9339 There are two templates for each key, the first uses the original Org syntax,
9340 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
9341 the default when the /org-mtags.el/ module has been loaded. See also the
9342 variable `org-mtags-prefer-muse-templates'.
9343 This is an experimental feature, it is undecided if it is going to stay in."
9344 :group 'org-completion
9345 :type '(repeat
9346 (string :tag "Key")
9347 (string :tag "Template")
9348 (string :tag "Muse Template")))
9350 (defun org-try-structure-completion ()
9351 "Try to complete a structure template before point.
9352 This looks for strings like \"<e\" on an otherwise empty line and
9353 expands them."
9354 (let ((l (buffer-substring (point-at-bol) (point)))
9356 (when (and (looking-at "[ \t]*$")
9357 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
9358 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
9359 (org-complete-expand-structure-template (+ -1 (point-at-bol)
9360 (match-beginning 1)) a)
9361 t)))
9363 (defun org-complete-expand-structure-template (start cell)
9364 "Expand a structure template."
9365 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
9366 (rpl (nth (if musep 2 1) cell))
9367 (ind ""))
9368 (delete-region start (point))
9369 (when (string-match "\\`#\\+" rpl)
9370 (cond
9371 ((bolp))
9372 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
9373 (setq ind (buffer-substring (point-at-bol) (point))))
9374 (t (newline))))
9375 (setq start (point))
9376 (if (string-match "%file" rpl)
9377 (setq rpl (replace-match
9378 (concat
9379 "\""
9380 (save-match-data
9381 (abbreviate-file-name (read-file-name "Include file: ")))
9382 "\"")
9383 t t rpl)))
9384 (setq rpl (mapconcat 'identity (split-string rpl "\n")
9385 (concat "\n" ind)))
9386 (insert rpl)
9387 (if (re-search-backward "\\?" start t) (delete-char 1))))
9390 (defun org-complete (&optional arg)
9391 "Perform completion on word at point.
9392 At the beginning of a headline, this completes TODO keywords as given in
9393 `org-todo-keywords'.
9394 If the current word is preceded by a backslash, completes the TeX symbols
9395 that are supported for HTML support.
9396 If the current word is preceded by \"#+\", completes special words for
9397 setting file options.
9398 In the line after \"#+STARTUP:, complete valid keywords.\"
9399 At all other locations, this simply calls the value of
9400 `org-completion-fallback-command'."
9401 (interactive "P")
9402 (org-without-partial-completion
9403 (catch 'exit
9404 (let* ((a nil)
9405 (end (point))
9406 (beg1 (save-excursion
9407 (skip-chars-backward (org-re "[:alnum:]_@"))
9408 (point)))
9409 (beg (save-excursion
9410 (skip-chars-backward "a-zA-Z0-9_:$")
9411 (point)))
9412 (confirm (lambda (x) (stringp (car x))))
9413 (searchhead (equal (char-before beg) ?*))
9414 (struct
9415 (when (and (member (char-before beg1) '(?. ?<))
9416 (setq a (assoc (buffer-substring beg1 (point))
9417 org-structure-template-alist)))
9418 (org-complete-expand-structure-template (1- beg1) a)
9419 (throw 'exit t)))
9420 (tag (and (equal (char-before beg1) ?:)
9421 (equal (char-after (point-at-bol)) ?*)))
9422 (prop (and (equal (char-before beg1) ?:)
9423 (not (equal (char-after (point-at-bol)) ?*))))
9424 (texp (equal (char-before beg) ?\\))
9425 (link (equal (char-before beg) ?\[))
9426 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
9427 beg)
9428 "#+"))
9429 (startup (string-match "^#\\+STARTUP:.*"
9430 (buffer-substring (point-at-bol) (point))))
9431 (completion-ignore-case opt)
9432 (type nil)
9433 (tbl nil)
9434 (table (cond
9435 (opt
9436 (setq type :opt)
9437 (require 'org-exp)
9438 (append
9439 (delq nil
9440 (mapcar
9441 (lambda (x)
9442 (if (string-match
9443 "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
9444 (cons (match-string 2 x)
9445 (match-string 1 x))))
9446 (org-split-string (org-get-current-options) "\n")))
9447 (mapcar 'list org-additional-option-like-keywords)))
9448 (startup
9449 (setq type :startup)
9450 org-startup-options)
9451 (link (append org-link-abbrev-alist-local
9452 org-link-abbrev-alist))
9453 (texp
9454 (setq type :tex)
9455 org-html-entities)
9456 ((string-match "\\`\\*+[ \t]+\\'"
9457 (buffer-substring (point-at-bol) beg))
9458 (setq type :todo)
9459 (mapcar 'list org-todo-keywords-1))
9460 (searchhead
9461 (setq type :searchhead)
9462 (save-excursion
9463 (goto-char (point-min))
9464 (while (re-search-forward org-todo-line-regexp nil t)
9465 (push (list
9466 (org-make-org-heading-search-string
9467 (match-string 3) t))
9468 tbl)))
9469 tbl)
9470 (tag (setq type :tag beg beg1)
9471 (or org-tag-alist (org-get-buffer-tags)))
9472 (prop (setq type :prop beg beg1)
9473 (mapcar 'list (org-buffer-property-keys nil t t)))
9474 (t (progn
9475 (call-interactively org-completion-fallback-command)
9476 (throw 'exit nil)))))
9477 (pattern (buffer-substring-no-properties beg end))
9478 (completion (try-completion pattern table confirm)))
9479 (cond ((eq completion t)
9480 (if (not (assoc (upcase pattern) table))
9481 (message "Already complete")
9482 (if (and (equal type :opt)
9483 (not (member (car (assoc (upcase pattern) table))
9484 org-additional-option-like-keywords)))
9485 (insert (substring (cdr (assoc (upcase pattern) table))
9486 (length pattern)))
9487 (if (memq type '(:tag :prop)) (insert ":")))))
9488 ((null completion)
9489 (message "Can't find completion for \"%s\"" pattern)
9490 (ding))
9491 ((not (string= pattern completion))
9492 (delete-region beg end)
9493 (if (string-match " +$" completion)
9494 (setq completion (replace-match "" t t completion)))
9495 (insert completion)
9496 (if (get-buffer-window "*Completions*")
9497 (delete-window (get-buffer-window "*Completions*")))
9498 (if (assoc completion table)
9499 (if (eq type :todo) (insert " ")
9500 (if (memq type '(:tag :prop)) (insert ":"))))
9501 (if (and (equal type :opt) (assoc completion table))
9502 (message "%s" (substitute-command-keys
9503 "Press \\[org-complete] again to insert example settings"))))
9505 (message "Making completion list...")
9506 (let ((list (sort (all-completions pattern table confirm)
9507 'string<)))
9508 (with-output-to-temp-buffer "*Completions*"
9509 (condition-case nil
9510 ;; Protection needed for XEmacs and emacs 21
9511 (display-completion-list list pattern)
9512 (error (display-completion-list list)))))
9513 (message "Making completion list...%s" "done")))))))
9515 ;;;; TODO, DEADLINE, Comments
9517 (defun org-toggle-comment ()
9518 "Change the COMMENT state of an entry."
9519 (interactive)
9520 (save-excursion
9521 (org-back-to-heading)
9522 (let (case-fold-search)
9523 (if (looking-at (concat outline-regexp
9524 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
9525 (replace-match "" t t nil 1)
9526 (if (looking-at outline-regexp)
9527 (progn
9528 (goto-char (match-end 0))
9529 (insert org-comment-string " ")))))))
9531 (defvar org-last-todo-state-is-todo nil
9532 "This is non-nil when the last TODO state change led to a TODO state.
9533 If the last change removed the TODO tag or switched to DONE, then
9534 this is nil.")
9536 (defvar org-setting-tags nil) ; dynamically skipped
9538 (defun org-parse-local-options (string var)
9539 "Parse STRING for startup setting relevant for variable VAR."
9540 (let ((rtn (symbol-value var))
9541 e opts)
9542 (save-match-data
9543 (if (or (not string) (not (string-match "\\S-" string)))
9545 (setq opts (delq nil (mapcar (lambda (x)
9546 (setq e (assoc x org-startup-options))
9547 (if (eq (nth 1 e) var) e nil))
9548 (org-split-string string "[ \t]+"))))
9549 (if (not opts)
9551 (setq rtn nil)
9552 (while (setq e (pop opts))
9553 (if (not (nth 3 e))
9554 (setq rtn (nth 2 e))
9555 (if (not (listp rtn)) (setq rtn nil))
9556 (push (nth 2 e) rtn)))
9557 rtn)))))
9559 (defvar org-todo-setup-filter-hook nil
9560 "Hook for functions that pre-filter todo specs.
9562 Each function takes a todo spec and returns either `nil' or the spec
9563 transformed into canonical form." )
9565 (defvar org-todo-get-default-hook nil
9566 "Hook for functions that get a default item for todo.
9568 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
9569 `nil' or a string to be used for the todo mark." )
9571 (defvar org-agenda-headline-snapshot-before-repeat)
9573 (defun org-todo (&optional arg)
9574 "Change the TODO state of an item.
9575 The state of an item is given by a keyword at the start of the heading,
9576 like
9577 *** TODO Write paper
9578 *** DONE Call mom
9580 The different keywords are specified in the variable `org-todo-keywords'.
9581 By default the available states are \"TODO\" and \"DONE\".
9582 So for this example: when the item starts with TODO, it is changed to DONE.
9583 When it starts with DONE, the DONE is removed. And when neither TODO nor
9584 DONE are present, add TODO at the beginning of the heading.
9586 With C-u prefix arg, use completion to determine the new state.
9587 With numeric prefix arg, switch to that state.
9588 With a double C-u prefix, switch to the next set of TODO keywords (nextset).
9589 With a tripple C-u prefix, circumvent any state blocking.
9591 For calling through lisp, arg is also interpreted in the following way:
9592 'none -> empty state
9593 \"\"(empty string) -> switch to empty state
9594 'done -> switch to DONE
9595 'nextset -> switch to the next set of keywords
9596 'previousset -> switch to the previous set of keywords
9597 \"WAITING\" -> switch to the specified keyword, but only if it
9598 really is a member of `org-todo-keywords'."
9599 (interactive "P")
9600 (if (equal arg '(16)) (setq arg 'nextset))
9601 (let ((org-blocker-hook org-blocker-hook)
9602 (case-fold-search nil))
9603 (when (equal arg '(64))
9604 (setq arg nil org-blocker-hook nil))
9605 (when (and org-blocker-hook
9606 (or org-inhibit-blocking
9607 (org-entry-get nil "NOBLOCKING")))
9608 (setq org-blocker-hook nil))
9609 (save-excursion
9610 (catch 'exit
9611 (org-back-to-heading t)
9612 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
9613 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
9614 (looking-at " *"))
9615 (let* ((match-data (match-data))
9616 (startpos (point-at-bol))
9617 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
9618 (org-log-done org-log-done)
9619 (org-log-repeat org-log-repeat)
9620 (org-todo-log-states org-todo-log-states)
9621 (this (match-string 1))
9622 (hl-pos (match-beginning 0))
9623 (head (org-get-todo-sequence-head this))
9624 (ass (assoc head org-todo-kwd-alist))
9625 (interpret (nth 1 ass))
9626 (done-word (nth 3 ass))
9627 (final-done-word (nth 4 ass))
9628 (last-state (or this ""))
9629 (completion-ignore-case t)
9630 (member (member this org-todo-keywords-1))
9631 (tail (cdr member))
9632 (state (cond
9633 ((and org-todo-key-trigger
9634 (or (and (equal arg '(4))
9635 (eq org-use-fast-todo-selection 'prefix))
9636 (and (not arg) org-use-fast-todo-selection
9637 (not (eq org-use-fast-todo-selection
9638 'prefix)))))
9639 ;; Use fast selection
9640 (org-fast-todo-selection))
9641 ((and (equal arg '(4))
9642 (or (not org-use-fast-todo-selection)
9643 (not org-todo-key-trigger)))
9644 ;; Read a state with completion
9645 (org-icompleting-read
9646 "State: " (mapcar (lambda(x) (list x))
9647 org-todo-keywords-1)
9648 nil t))
9649 ((eq arg 'right)
9650 (if this
9651 (if tail (car tail) nil)
9652 (car org-todo-keywords-1)))
9653 ((eq arg 'left)
9654 (if (equal member org-todo-keywords-1)
9656 (if this
9657 (nth (- (length org-todo-keywords-1)
9658 (length tail) 2)
9659 org-todo-keywords-1)
9660 (org-last org-todo-keywords-1))))
9661 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
9662 (setq arg nil))) ; hack to fall back to cycling
9663 (arg
9664 ;; user or caller requests a specific state
9665 (cond
9666 ((equal arg "") nil)
9667 ((eq arg 'none) nil)
9668 ((eq arg 'done) (or done-word (car org-done-keywords)))
9669 ((eq arg 'nextset)
9670 (or (car (cdr (member head org-todo-heads)))
9671 (car org-todo-heads)))
9672 ((eq arg 'previousset)
9673 (let ((org-todo-heads (reverse org-todo-heads)))
9674 (or (car (cdr (member head org-todo-heads)))
9675 (car org-todo-heads))))
9676 ((car (member arg org-todo-keywords-1)))
9677 ((stringp arg)
9678 (error "State `%s' not valid in this file" arg))
9679 ((nth (1- (prefix-numeric-value arg))
9680 org-todo-keywords-1))))
9681 ((null member) (or head (car org-todo-keywords-1)))
9682 ((equal this final-done-word) nil) ;; -> make empty
9683 ((null tail) nil) ;; -> first entry
9684 ((memq interpret '(type priority))
9685 (if (eq this-command last-command)
9686 (car tail)
9687 (if (> (length tail) 0)
9688 (or done-word (car org-done-keywords))
9689 nil)))
9691 (car tail))))
9692 (state (or
9693 (run-hook-with-args-until-success
9694 'org-todo-get-default-hook state last-state)
9695 state))
9696 (next (if state (concat " " state " ") " "))
9697 (change-plist (list :type 'todo-state-change :from this :to state
9698 :position startpos))
9699 dolog now-done-p)
9700 (when org-blocker-hook
9701 (setq org-last-todo-state-is-todo
9702 (not (member this org-done-keywords)))
9703 (unless (save-excursion
9704 (save-match-data
9705 (run-hook-with-args-until-failure
9706 'org-blocker-hook change-plist)))
9707 (if (interactive-p)
9708 (error "TODO state change from %s to %s blocked" this state)
9709 ;; fail silently
9710 (message "TODO state change from %s to %s blocked" this state)
9711 (throw 'exit nil))))
9712 (store-match-data match-data)
9713 (replace-match next t t)
9714 (unless (pos-visible-in-window-p hl-pos)
9715 (message "TODO state changed to %s" (org-trim next)))
9716 (unless head
9717 (setq head (org-get-todo-sequence-head state)
9718 ass (assoc head org-todo-kwd-alist)
9719 interpret (nth 1 ass)
9720 done-word (nth 3 ass)
9721 final-done-word (nth 4 ass)))
9722 (when (memq arg '(nextset previousset))
9723 (message "Keyword-Set %d/%d: %s"
9724 (- (length org-todo-sets) -1
9725 (length (memq (assoc state org-todo-sets) org-todo-sets)))
9726 (length org-todo-sets)
9727 (mapconcat 'identity (assoc state org-todo-sets) " ")))
9728 (setq org-last-todo-state-is-todo
9729 (not (member state org-done-keywords)))
9730 (setq now-done-p (and (member state org-done-keywords)
9731 (not (member this org-done-keywords))))
9732 (and logging (org-local-logging logging))
9733 (when (and (or org-todo-log-states org-log-done)
9734 (not (eq org-inhibit-logging t))
9735 (not (memq arg '(nextset previousset))))
9736 ;; we need to look at recording a time and note
9737 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
9738 (nth 2 (assoc this org-todo-log-states))))
9739 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
9740 (setq dolog 'time))
9741 (when (and state
9742 (member state org-not-done-keywords)
9743 (not (member this org-not-done-keywords)))
9744 ;; This is now a todo state and was not one before
9745 ;; If there was a CLOSED time stamp, get rid of it.
9746 (org-add-planning-info nil nil 'closed))
9747 (when (and now-done-p org-log-done)
9748 ;; It is now done, and it was not done before
9749 (org-add-planning-info 'closed (org-current-time))
9750 (if (and (not dolog) (eq 'note org-log-done))
9751 (org-add-log-setup 'done state this 'findpos 'note)))
9752 (when (and state dolog)
9753 ;; This is a non-nil state, and we need to log it
9754 (org-add-log-setup 'state state this 'findpos dolog)))
9755 ;; Fixup tag positioning
9756 (org-todo-trigger-tag-changes state)
9757 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
9758 (when org-provide-todo-statistics
9759 (org-update-parent-todo-statistics))
9760 (run-hooks 'org-after-todo-state-change-hook)
9761 (if (and arg (not (member state org-done-keywords)))
9762 (setq head (org-get-todo-sequence-head state)))
9763 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
9764 ;; Do we need to trigger a repeat?
9765 (when now-done-p
9766 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
9767 ;; This is for the agenda, take a snapshot of the headline.
9768 (save-match-data
9769 (setq org-agenda-headline-snapshot-before-repeat
9770 (org-get-heading))))
9771 (org-auto-repeat-maybe state))
9772 ;; Fixup cursor location if close to the keyword
9773 (if (and (outline-on-heading-p)
9774 (not (bolp))
9775 (save-excursion (beginning-of-line 1)
9776 (looking-at org-todo-line-regexp))
9777 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
9778 (progn
9779 (goto-char (or (match-end 2) (match-end 1)))
9780 (and (looking-at " ") (just-one-space))))
9781 (when org-trigger-hook
9782 (save-excursion
9783 (run-hook-with-args 'org-trigger-hook change-plist))))))))
9785 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
9786 "Block turning an entry into a TODO, using the hierarchy.
9787 This checks whether the current task should be blocked from state
9788 changes. Such blocking occurs when:
9790 1. The task has children which are not all in a completed state.
9792 2. A task has a parent with the property :ORDERED:, and there
9793 are siblings prior to the current task with incomplete
9794 status.
9796 3. The parent of the task is blocked because it has siblings that should
9797 be done first, or is child of a block grandparent TODO entry."
9799 (catch 'dont-block
9800 ;; If this is not a todo state change, or if this entry is already DONE,
9801 ;; do not block
9802 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
9803 (member (plist-get change-plist :from)
9804 (cons 'done org-done-keywords))
9805 (member (plist-get change-plist :to)
9806 (cons 'todo org-not-done-keywords)))
9807 (throw 'dont-block t))
9808 ;; If this task has children, and any are undone, it's blocked
9809 (save-excursion
9810 (org-back-to-heading t)
9811 (let ((this-level (funcall outline-level)))
9812 (outline-next-heading)
9813 (let ((child-level (funcall outline-level)))
9814 (while (and (not (eobp))
9815 (> child-level this-level))
9816 ;; this todo has children, check whether they are all
9817 ;; completed
9818 (if (and (not (org-entry-is-done-p))
9819 (org-entry-is-todo-p))
9820 (throw 'dont-block nil))
9821 (outline-next-heading)
9822 (setq child-level (funcall outline-level))))))
9823 ;; Otherwise, if the task's parent has the :ORDERED: property, and
9824 ;; any previous siblings are undone, it's blocked
9825 (save-excursion
9826 (org-back-to-heading t)
9827 (let* ((pos (point))
9828 (parent-pos (and (org-up-heading-safe) (point))))
9829 (if (not parent-pos) (throw 'dont-block t)) ; no parent
9830 (when (and (org-entry-get (point) "ORDERED")
9831 (forward-line 1)
9832 (re-search-forward org-not-done-heading-regexp pos t))
9833 (throw 'dont-block nil)) ; block, there is an older sibling not done.
9834 ;; Search further up the hierarchy, to see if an anchestor is blocked
9835 (while t
9836 (goto-char parent-pos)
9837 (if (not (looking-at org-not-done-heading-regexp))
9838 (throw 'dont-block t)) ; do not block, parent is not a TODO
9839 (setq pos (point))
9840 (setq parent-pos (and (org-up-heading-safe) (point)))
9841 (if (not parent-pos) (throw 'dont-block t)) ; no parent
9842 (when (and (org-entry-get (point) "ORDERED")
9843 (forward-line 1)
9844 (re-search-forward org-not-done-heading-regexp pos t))
9845 (throw 'dont-block nil))))))) ; block, older sibling not done.
9847 (defcustom org-track-ordered-property-with-tag nil
9848 "Should the ORDERED property also be shown as a tag?
9849 The ORDERED property decides if an entry should require subtasks to be
9850 completed in sequence. Since a property is not very visible, setting
9851 this option means that toggling the ORDERED property with the command
9852 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
9853 not relevant for the behavior, but it makes things more visible.
9855 Note that toggling the tag with tags commands will not change the property
9856 and therefore not influence behavior!
9858 This can be t, meaning the tag ORDERED should be used, It can also be a
9859 string to select a different tag for this task."
9860 :group 'org-todo
9861 :type '(choice
9862 (const :tag "No tracking" nil)
9863 (const :tag "Track with ORDERED tag" t)
9864 (string :tag "Use other tag")))
9866 (defun org-toggle-ordered-property ()
9867 "Toggle the ORDERED property of the current entry.
9868 For better visibility, you can track the value of this property with a tag.
9869 See variable `org-track-ordered-property-with-tag'."
9870 (interactive)
9871 (let* ((t1 org-track-ordered-property-with-tag)
9872 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
9873 (save-excursion
9874 (org-back-to-heading)
9875 (if (org-entry-get nil "ORDERED")
9876 (progn
9877 (org-delete-property "ORDERED")
9878 (and tag (org-toggle-tag tag 'off))
9879 (message "Subtasks can be completed in arbitrary order"))
9880 (org-entry-put nil "ORDERED" "t")
9881 (and tag (org-toggle-tag tag 'on))
9882 (message "Subtasks must be completed in sequence")))))
9884 (defvar org-blocked-by-checkboxes) ; dynamically scoped
9885 (defun org-block-todo-from-checkboxes (change-plist)
9886 "Block turning an entry into a TODO, using checkboxes.
9887 This checks whether the current task should be blocked from state
9888 changes because there are uncheckd boxes in this entry."
9889 (catch 'dont-block
9890 ;; If this is not a todo state change, or if this entry is already DONE,
9891 ;; do not block
9892 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
9893 (member (plist-get change-plist :from)
9894 (cons 'done org-done-keywords))
9895 (member (plist-get change-plist :to)
9896 (cons 'todo org-not-done-keywords)))
9897 (throw 'dont-block t))
9898 ;; If this task has checkboxes that are not checked, it's blocked
9899 (save-excursion
9900 (org-back-to-heading t)
9901 (let ((beg (point)) end)
9902 (outline-next-heading)
9903 (setq end (point))
9904 (goto-char beg)
9905 (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
9906 end t)
9907 (progn
9908 (if (boundp 'org-blocked-by-checkboxes)
9909 (setq org-blocked-by-checkboxes t))
9910 (throw 'dont-block nil)))))
9911 t)) ; do not block
9913 (defun org-update-statistics-cookies (all)
9914 "Update the statistics cookie, either from TODO or from checkboxes.
9915 This should be called with the cursor in a line with a statistics cookie."
9916 (interactive "P")
9917 (if all
9918 (progn
9919 (org-update-checkbox-count 'all)
9920 (org-map-entries 'org-update-parent-todo-statistics))
9921 (if (not (org-on-heading-p))
9922 (org-update-checkbox-count)
9923 (let ((pos (move-marker (make-marker) (point)))
9924 end l1 l2)
9925 (ignore-errors (org-back-to-heading t))
9926 (if (not (org-on-heading-p))
9927 (org-update-checkbox-count)
9928 (setq l1 (org-outline-level))
9929 (setq end (save-excursion
9930 (outline-next-heading)
9931 (if (org-on-heading-p) (setq l2 (org-outline-level)))
9932 (point)))
9933 (if (and (save-excursion (re-search-forward
9934 "^[ \t]*[-+*] \\[[- X]\\]" end t))
9935 (not (save-excursion (re-search-forward
9936 ":COOKIE_DATA:.*\\<todo\\>" end t))))
9937 (org-update-checkbox-count)
9938 (if (and l2 (> l2 l1))
9939 (progn
9940 (goto-char end)
9941 (org-update-parent-todo-statistics))
9942 (error "No data for statistics cookie"))))
9943 (goto-char pos)
9944 (move-marker pos nil)))))
9946 (defvar org-entry-property-inherited-from) ;; defined below
9947 (defun org-update-parent-todo-statistics ()
9948 "Update any statistics cookie in the parent of the current headline.
9949 When `org-hierarchical-todo-statistics' is nil, statistics will cover
9950 the entire subtree and this will travel up the hierarchy and update
9951 statistics everywhere."
9952 (interactive)
9953 (let* ((lim 0) prop
9954 (recursive (or (not org-hierarchical-todo-statistics)
9955 (string-match
9956 "\\<recursive\\>"
9957 (or (setq prop (org-entry-get
9958 nil "COOKIE_DATA" 'inherit)) ""))))
9959 (lim (or (and prop (marker-position
9960 org-entry-property-inherited-from))
9961 lim))
9962 (first t)
9963 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
9964 level ltoggle l1 new ndel
9965 (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
9966 (catch 'exit
9967 (save-excursion
9968 (beginning-of-line 1)
9969 (if (org-at-heading-p)
9970 (setq ltoggle (funcall outline-level))
9971 (error "This should not happen"))
9972 (while (and (setq level (org-up-heading-safe))
9973 (or recursive first)
9974 (>= (point) lim))
9975 (setq first nil)
9976 (unless (and level
9977 (not (string-match
9978 "\\<checkbox\\>"
9979 (downcase
9980 (or (org-entry-get
9981 nil "COOKIE_DATA")
9982 "")))))
9983 (throw 'exit nil))
9984 (while (re-search-forward box-re (point-at-eol) t)
9985 (setq cnt-all 0 cnt-done 0 cookie-present t)
9986 (setq is-percent (match-end 2))
9987 (save-match-data
9988 (unless (outline-next-heading) (throw 'exit nil))
9989 (while (and (looking-at org-complex-heading-regexp)
9990 (> (setq l1 (length (match-string 1))) level))
9991 (setq kwd (and (or recursive (= l1 ltoggle))
9992 (match-string 2)))
9993 (if (or (eq org-provide-todo-statistics 'all-headlines)
9994 (and (listp org-provide-todo-statistics)
9995 (or (member kwd org-provide-todo-statistics)
9996 (member kwd org-done-keywords))))
9997 (setq cnt-all (1+ cnt-all))
9998 (if (eq org-provide-todo-statistics t)
9999 (and kwd (setq cnt-all (1+ cnt-all)))))
10000 (and (member kwd org-done-keywords)
10001 (setq cnt-done (1+ cnt-done)))
10002 (outline-next-heading)))
10003 (setq new
10004 (if is-percent
10005 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
10006 (format "[%d/%d]" cnt-done cnt-all))
10007 ndel (- (match-end 0) (match-beginning 0)))
10008 (goto-char (match-beginning 0))
10009 (insert new)
10010 (delete-region (point) (+ (point) ndel))))
10011 (when cookie-present
10012 (run-hook-with-args 'org-after-todo-statistics-hook
10013 cnt-done (- cnt-all cnt-done)))))
10014 (run-hooks 'org-todo-statistics-hook)))
10016 (defvar org-after-todo-statistics-hook nil
10017 "Hook that is called after a TODO statistics cookie has been updated.
10018 Each function is called with two arguments: the number of not-done entries
10019 and the number of done entries.
10021 For example, the following function, when added to this hook, will switch
10022 an entry to DONE when all children are done, and back to TODO when new
10023 entries are set to a TODO status. Note that this hook is only called
10024 when there is a statistics cookie in the headline!
10026 (defun org-summary-todo (n-done n-not-done)
10027 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
10028 (let (org-log-done org-log-states) ; turn off logging
10029 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
10032 (defvar org-todo-statistics-hook nil
10033 "Hook that is run whenever Org thinks TODO statistics should be updated.
10034 This hook runs even if there is no statisics cookie present, in which case
10035 `org-after-todo-statistics-hook' would not run.")
10037 (defun org-todo-trigger-tag-changes (state)
10038 "Apply the changes defined in `org-todo-state-tags-triggers'."
10039 (let ((l org-todo-state-tags-triggers)
10040 changes)
10041 (when (or (not state) (equal state ""))
10042 (setq changes (append changes (cdr (assoc "" l)))))
10043 (when (and (stringp state) (> (length state) 0))
10044 (setq changes (append changes (cdr (assoc state l)))))
10045 (when (member state org-not-done-keywords)
10046 (setq changes (append changes (cdr (assoc 'todo l)))))
10047 (when (member state org-done-keywords)
10048 (setq changes (append changes (cdr (assoc 'done l)))))
10049 (dolist (c changes)
10050 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
10052 (defun org-local-logging (value)
10053 "Get logging settings from a property VALUE."
10054 (let* (words w a)
10055 ;; directly set the variables, they are already local.
10056 (setq org-log-done nil
10057 org-log-repeat nil
10058 org-todo-log-states nil)
10059 (setq words (org-split-string value))
10060 (while (setq w (pop words))
10061 (cond
10062 ((setq a (assoc w org-startup-options))
10063 (and (member (nth 1 a) '(org-log-done org-log-repeat))
10064 (set (nth 1 a) (nth 2 a))))
10065 ((setq a (org-extract-log-state-settings w))
10066 (and (member (car a) org-todo-keywords-1)
10067 (push a org-todo-log-states)))))))
10069 (defun org-get-todo-sequence-head (kwd)
10070 "Return the head of the TODO sequence to which KWD belongs.
10071 If KWD is not set, check if there is a text property remembering the
10072 right sequence."
10073 (let (p)
10074 (cond
10075 ((not kwd)
10076 (or (get-text-property (point-at-bol) 'org-todo-head)
10077 (progn
10078 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
10079 nil (point-at-eol)))
10080 (get-text-property p 'org-todo-head))))
10081 ((not (member kwd org-todo-keywords-1))
10082 (car org-todo-keywords-1))
10083 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
10085 (defun org-fast-todo-selection ()
10086 "Fast TODO keyword selection with single keys.
10087 Returns the new TODO keyword, or nil if no state change should occur."
10088 (let* ((fulltable org-todo-key-alist)
10089 (done-keywords org-done-keywords) ;; needed for the faces.
10090 (maxlen (apply 'max (mapcar
10091 (lambda (x)
10092 (if (stringp (car x)) (string-width (car x)) 0))
10093 fulltable)))
10094 (expert nil)
10095 (fwidth (+ maxlen 3 1 3))
10096 (ncol (/ (- (window-width) 4) fwidth))
10097 tg cnt e c tbl
10098 groups ingroup)
10099 (save-excursion
10100 (save-window-excursion
10101 (if expert
10102 (set-buffer (get-buffer-create " *Org todo*"))
10103 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
10104 (erase-buffer)
10105 (org-set-local 'org-done-keywords done-keywords)
10106 (setq tbl fulltable cnt 0)
10107 (while (setq e (pop tbl))
10108 (cond
10109 ((equal e '(:startgroup))
10110 (push '() groups) (setq ingroup t)
10111 (when (not (= cnt 0))
10112 (setq cnt 0)
10113 (insert "\n"))
10114 (insert "{ "))
10115 ((equal e '(:endgroup))
10116 (setq ingroup nil cnt 0)
10117 (insert "}\n"))
10118 ((equal e '(:newline))
10119 (when (not (= cnt 0))
10120 (setq cnt 0)
10121 (insert "\n")
10122 (setq e (car tbl))
10123 (while (equal (car tbl) '(:newline))
10124 (insert "\n")
10125 (setq tbl (cdr tbl)))))
10127 (setq tg (car e) c (cdr e))
10128 (if ingroup (push tg (car groups)))
10129 (setq tg (org-add-props tg nil 'face
10130 (org-get-todo-face tg)))
10131 (if (and (= cnt 0) (not ingroup)) (insert " "))
10132 (insert "[" c "] " tg (make-string
10133 (- fwidth 4 (length tg)) ?\ ))
10134 (when (= (setq cnt (1+ cnt)) ncol)
10135 (insert "\n")
10136 (if ingroup (insert " "))
10137 (setq cnt 0)))))
10138 (insert "\n")
10139 (goto-char (point-min))
10140 (if (not expert) (org-fit-window-to-buffer))
10141 (message "[a-z..]:Set [SPC]:clear")
10142 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
10143 (cond
10144 ((or (= c ?\C-g)
10145 (and (= c ?q) (not (rassoc c fulltable))))
10146 (setq quit-flag t))
10147 ((= c ?\ ) nil)
10148 ((setq e (rassoc c fulltable) tg (car e))
10150 (t (setq quit-flag t)))))))
10152 (defun org-entry-is-todo-p ()
10153 (member (org-get-todo-state) org-not-done-keywords))
10155 (defun org-entry-is-done-p ()
10156 (member (org-get-todo-state) org-done-keywords))
10158 (defun org-get-todo-state ()
10159 (save-excursion
10160 (org-back-to-heading t)
10161 (and (looking-at org-todo-line-regexp)
10162 (match-end 2)
10163 (match-string 2))))
10165 (defun org-at-date-range-p (&optional inactive-ok)
10166 "Is the cursor inside a date range?"
10167 (interactive)
10168 (save-excursion
10169 (catch 'exit
10170 (let ((pos (point)))
10171 (skip-chars-backward "^[<\r\n")
10172 (skip-chars-backward "<[")
10173 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
10174 (>= (match-end 0) pos)
10175 (throw 'exit t))
10176 (skip-chars-backward "^<[\r\n")
10177 (skip-chars-backward "<[")
10178 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
10179 (>= (match-end 0) pos)
10180 (throw 'exit t)))
10181 nil)))
10183 (defun org-get-repeat (&optional tagline)
10184 "Check if there is a deadline/schedule with repeater in this entry."
10185 (save-match-data
10186 (save-excursion
10187 (org-back-to-heading t)
10188 (and (re-search-forward (if tagline
10189 (concat tagline "\\s-*" org-repeat-re)
10190 org-repeat-re)
10191 (org-entry-end-position) t)
10192 (match-string-no-properties 1)))))
10194 (defvar org-last-changed-timestamp)
10195 (defvar org-last-inserted-timestamp)
10196 (defvar org-log-post-message)
10197 (defvar org-log-note-purpose)
10198 (defvar org-log-note-how)
10199 (defvar org-log-note-extra)
10200 (defun org-auto-repeat-maybe (done-word)
10201 "Check if the current headline contains a repeated deadline/schedule.
10202 If yes, set TODO state back to what it was and change the base date
10203 of repeating deadline/scheduled time stamps to new date.
10204 This function is run automatically after each state change to a DONE state."
10205 ;; last-state is dynamically scoped into this function
10206 (let* ((repeat (org-get-repeat))
10207 (aa (assoc last-state org-todo-kwd-alist))
10208 (interpret (nth 1 aa))
10209 (head (nth 2 aa))
10210 (whata '(("d" . day) ("m" . month) ("y" . year)))
10211 (msg "Entry repeats: ")
10212 (org-log-done nil)
10213 (org-todo-log-states nil)
10214 (nshiftmax 10) (nshift 0)
10215 re type n what ts time)
10216 (when repeat
10217 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
10218 (org-todo (if (eq interpret 'type) last-state head))
10219 (org-entry-put nil "LAST_REPEAT" (format-time-string
10220 (org-time-stamp-format t t)))
10221 (when org-log-repeat
10222 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
10223 (memq 'org-add-log-note post-command-hook))
10224 ;; OK, we are already setup for some record
10225 (if (eq org-log-repeat 'note)
10226 ;; make sure we take a note, not only a time stamp
10227 (setq org-log-note-how 'note))
10228 ;; Set up for taking a record
10229 (org-add-log-setup 'state (or done-word (car org-done-keywords))
10230 last-state
10231 'findpos org-log-repeat)))
10232 (org-back-to-heading t)
10233 (org-add-planning-info nil nil 'closed)
10234 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
10235 org-deadline-time-regexp "\\)\\|\\("
10236 org-ts-regexp "\\)"))
10237 (while (re-search-forward
10238 re (save-excursion (outline-next-heading) (point)) t)
10239 (setq type (if (match-end 1) org-scheduled-string
10240 (if (match-end 3) org-deadline-string "Plain:"))
10241 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
10242 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
10243 (setq n (string-to-number (match-string 2 ts))
10244 what (match-string 3 ts))
10245 (if (equal what "w") (setq n (* n 7) what "d"))
10246 ;; Preparation, see if we need to modify the start date for the change
10247 (when (match-end 1)
10248 (setq time (save-match-data (org-time-string-to-time ts)))
10249 (cond
10250 ((equal (match-string 1 ts) ".")
10251 ;; Shift starting date to today
10252 (org-timestamp-change
10253 (- (time-to-days (current-time)) (time-to-days time))
10254 'day))
10255 ((equal (match-string 1 ts) "+")
10256 (while (or (= nshift 0)
10257 (<= (time-to-days time) (time-to-days (current-time))))
10258 (when (= (incf nshift) nshiftmax)
10259 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
10260 (error "Abort")))
10261 (org-timestamp-change n (cdr (assoc what whata)))
10262 (org-at-timestamp-p t)
10263 (setq ts (match-string 1))
10264 (setq time (save-match-data (org-time-string-to-time ts))))
10265 (org-timestamp-change (- n) (cdr (assoc what whata)))
10266 ;; rematch, so that we have everything in place for the real shift
10267 (org-at-timestamp-p t)
10268 (setq ts (match-string 1))
10269 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
10270 (org-timestamp-change n (cdr (assoc what whata)))
10271 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
10272 (setq org-log-post-message msg)
10273 (message "%s" msg))))
10275 (defun org-show-todo-tree (arg)
10276 "Make a compact tree which shows all headlines marked with TODO.
10277 The tree will show the lines where the regexp matches, and all higher
10278 headlines above the match.
10279 With a \\[universal-argument] prefix, prompt for a regexp to match.
10280 With a numeric prefix N, construct a sparse tree for the Nth element
10281 of `org-todo-keywords-1'."
10282 (interactive "P")
10283 (let ((case-fold-search nil)
10284 (kwd-re
10285 (cond ((null arg) org-not-done-regexp)
10286 ((equal arg '(4))
10287 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
10288 (mapcar 'list org-todo-keywords-1))))
10289 (concat "\\("
10290 (mapconcat 'identity (org-split-string kwd "|") "\\|")
10291 "\\)\\>")))
10292 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
10293 (regexp-quote (nth (1- (prefix-numeric-value arg))
10294 org-todo-keywords-1)))
10295 (t (error "Invalid prefix argument: %s" arg)))))
10296 (message "%d TODO entries found"
10297 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
10299 (defun org-deadline (&optional remove time)
10300 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
10301 With argument REMOVE, remove any deadline from the item.
10302 When TIME is set, it should be an internal time specification, and the
10303 scheduling will use the corresponding date."
10304 (interactive "P")
10305 (if remove
10306 (progn
10307 (org-remove-timestamp-with-keyword org-deadline-string)
10308 (message "Item no longer has a deadline."))
10309 (if (org-get-repeat)
10310 (error "Cannot change deadline on task with repeater, please do that by hand")
10311 (org-add-planning-info 'deadline time 'closed)
10312 (message "Deadline on %s" org-last-inserted-timestamp))))
10314 (defun org-schedule (&optional remove time)
10315 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
10316 With argument REMOVE, remove any scheduling date from the item.
10317 When TIME is set, it should be an internal time specification, and the
10318 scheduling will use the corresponding date."
10319 (interactive "P")
10320 (if remove
10321 (progn
10322 (org-remove-timestamp-with-keyword org-scheduled-string)
10323 (message "Item is no longer scheduled."))
10324 (if (org-get-repeat)
10325 (error "Cannot reschedule task with repeater, please do that by hand")
10326 (org-add-planning-info 'scheduled time 'closed)
10327 (message "Scheduled to %s" org-last-inserted-timestamp))))
10329 (defun org-get-scheduled-time (pom &optional inherit)
10330 "Get the scheduled time as a time tuple, of a format suitable
10331 for calling org-schedule with, or if there is no scheduling,
10332 returns nil."
10333 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
10334 (when time
10335 (apply 'encode-time (org-parse-time-string time)))))
10337 (defun org-get-deadline-time (pom &optional inherit)
10338 "Get the deadine as a time tuple, of a format suitable for
10339 calling org-deadlin with, or if there is no scheduling, returns
10340 nil."
10341 (let ((time (org-entry-get pom "DEADLINE" inherit)))
10342 (when time
10343 (apply 'encode-time (org-parse-time-string time)))))
10345 (defun org-remove-timestamp-with-keyword (keyword)
10346 "Remove all time stamps with KEYWORD in the current entry."
10347 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
10348 beg)
10349 (save-excursion
10350 (org-back-to-heading t)
10351 (setq beg (point))
10352 (outline-next-heading)
10353 (while (re-search-backward re beg t)
10354 (replace-match "")
10355 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
10356 (equal (char-before) ?\ ))
10357 (backward-delete-char 1)
10358 (if (string-match "^[ \t]*$" (buffer-substring
10359 (point-at-bol) (point-at-eol)))
10360 (delete-region (point-at-bol)
10361 (min (point-max) (1+ (point-at-eol))))))))))
10363 (defun org-add-planning-info (what &optional time &rest remove)
10364 "Insert new timestamp with keyword in the line directly after the headline.
10365 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
10366 If non is given, the user is prompted for a date.
10367 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
10368 be removed."
10369 (interactive)
10370 (let (org-time-was-given org-end-time-was-given ts
10371 end default-time default-input)
10373 (catch 'exit
10374 (when (and (not time) (memq what '(scheduled deadline)))
10375 ;; Try to get a default date/time from existing timestamp
10376 (save-excursion
10377 (org-back-to-heading t)
10378 (setq end (save-excursion (outline-next-heading) (point)))
10379 (when (re-search-forward (if (eq what 'scheduled)
10380 org-scheduled-time-regexp
10381 org-deadline-time-regexp)
10382 end t)
10383 (setq ts (match-string 1)
10384 default-time
10385 (apply 'encode-time (org-parse-time-string ts))
10386 default-input (and ts (org-get-compact-tod ts))))))
10387 (when what
10388 ;; If necessary, get the time from the user
10389 (setq time (or time (org-read-date nil 'to-time nil nil
10390 default-time default-input))))
10392 (when (and org-insert-labeled-timestamps-at-point
10393 (member what '(scheduled deadline)))
10394 (insert
10395 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
10396 (org-insert-time-stamp time org-time-was-given
10397 nil nil nil (list org-end-time-was-given))
10398 (setq what nil))
10399 (save-excursion
10400 (save-restriction
10401 (let (col list elt ts buffer-invisibility-spec)
10402 (org-back-to-heading t)
10403 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
10404 (goto-char (match-end 1))
10405 (setq col (current-column))
10406 (goto-char (match-end 0))
10407 (if (eobp) (insert "\n") (forward-char 1))
10408 (when (and (not what)
10409 (not (looking-at
10410 (concat "[ \t]*"
10411 org-keyword-time-not-clock-regexp))))
10412 ;; Nothing to add, nothing to remove...... :-)
10413 (throw 'exit nil))
10414 (if (and (not (looking-at outline-regexp))
10415 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
10416 "[^\r\n]*"))
10417 (not (equal (match-string 1) org-clock-string)))
10418 (narrow-to-region (match-beginning 0) (match-end 0))
10419 (insert-before-markers "\n")
10420 (backward-char 1)
10421 (narrow-to-region (point) (point))
10422 (and org-adapt-indentation (org-indent-to-column col)))
10423 ;; Check if we have to remove something.
10424 (setq list (cons what remove))
10425 (while list
10426 (setq elt (pop list))
10427 (goto-char (point-min))
10428 (when (or (and (eq elt 'scheduled)
10429 (re-search-forward org-scheduled-time-regexp nil t))
10430 (and (eq elt 'deadline)
10431 (re-search-forward org-deadline-time-regexp nil t))
10432 (and (eq elt 'closed)
10433 (re-search-forward org-closed-time-regexp nil t)))
10434 (replace-match "")
10435 (if (looking-at "--+<[^>]+>") (replace-match ""))
10436 (skip-chars-backward " ")
10437 (if (looking-at " +") (replace-match ""))))
10438 (goto-char (point-max))
10439 (and org-adapt-indentation (bolp) (org-indent-to-column col))
10440 (when what
10441 (insert
10442 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
10443 (cond ((eq what 'scheduled) org-scheduled-string)
10444 ((eq what 'deadline) org-deadline-string)
10445 ((eq what 'closed) org-closed-string))
10446 " ")
10447 (setq ts (org-insert-time-stamp
10448 time
10449 (or org-time-was-given
10450 (and (eq what 'closed) org-log-done-with-time))
10451 (eq what 'closed)
10452 nil nil (list org-end-time-was-given)))
10453 (end-of-line 1))
10454 (goto-char (point-min))
10455 (widen)
10456 (if (and (looking-at "[ \t]+\n")
10457 (equal (char-before) ?\n))
10458 (delete-region (1- (point)) (point-at-eol)))
10459 ts))))))
10461 (defvar org-log-note-marker (make-marker))
10462 (defvar org-log-note-purpose nil)
10463 (defvar org-log-note-state nil)
10464 (defvar org-log-note-previous-state nil)
10465 (defvar org-log-note-how nil)
10466 (defvar org-log-note-extra nil)
10467 (defvar org-log-note-window-configuration nil)
10468 (defvar org-log-note-return-to (make-marker))
10469 (defvar org-log-post-message nil
10470 "Message to be displayed after a log note has been stored.
10471 The auto-repeater uses this.")
10473 (defun org-add-note ()
10474 "Add a note to the current entry.
10475 This is done in the same way as adding a state change note."
10476 (interactive)
10477 (org-add-log-setup 'note nil nil 'findpos nil))
10479 (defvar org-property-end-re)
10480 (defun org-add-log-setup (&optional purpose state prev-state
10481 findpos how &optional extra)
10482 "Set up the post command hook to take a note.
10483 If this is about to TODO state change, the new state is expected in STATE.
10484 When FINDPOS is non-nil, find the correct position for the note in
10485 the current entry. If not, assume that it can be inserted at point.
10486 HOW is an indicator what kind of note should be created.
10487 EXTRA is additional text that will be inserted into the notes buffer."
10488 (let* ((org-log-into-drawer (org-log-into-drawer))
10489 (drawer (cond ((stringp org-log-into-drawer)
10490 org-log-into-drawer)
10491 (org-log-into-drawer "LOGBOOK")
10492 (t nil))))
10493 (save-restriction
10494 (save-excursion
10495 (when findpos
10496 (org-back-to-heading t)
10497 (narrow-to-region (point) (save-excursion
10498 (outline-next-heading) (point)))
10499 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
10500 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
10501 "[^\r\n]*\\)?"))
10502 (goto-char (match-end 0))
10503 (cond
10504 (drawer
10505 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
10506 nil t)
10507 (progn
10508 (goto-char (match-end 0))
10509 (or org-log-states-order-reversed
10510 (and (re-search-forward org-property-end-re nil t)
10511 (goto-char (1- (match-beginning 0))))))
10512 (insert "\n:" drawer ":\n:END:")
10513 (beginning-of-line 0)
10514 (org-indent-line-function)
10515 (beginning-of-line 2)
10516 (org-indent-line-function)
10517 (end-of-line 0)))
10518 ((and org-log-state-notes-insert-after-drawers
10519 (save-excursion
10520 (forward-line) (looking-at org-drawer-regexp)))
10521 (forward-line)
10522 (while (looking-at org-drawer-regexp)
10523 (goto-char (match-end 0))
10524 (re-search-forward org-property-end-re (point-max) t)
10525 (forward-line))
10526 (forward-line -1)))
10527 (unless org-log-states-order-reversed
10528 (and (= (char-after) ?\n) (forward-char 1))
10529 (org-skip-over-state-notes)
10530 (skip-chars-backward " \t\n\r")))
10531 (move-marker org-log-note-marker (point))
10532 (setq org-log-note-purpose purpose
10533 org-log-note-state state
10534 org-log-note-previous-state prev-state
10535 org-log-note-how how
10536 org-log-note-extra extra)
10537 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
10539 (defun org-skip-over-state-notes ()
10540 "Skip past the list of State notes in an entry."
10541 (if (looking-at "\n[ \t]*- State") (forward-char 1))
10542 (while (looking-at "[ \t]*- State")
10543 (condition-case nil
10544 (org-next-item)
10545 (error (org-end-of-item)))))
10547 (defun org-add-log-note (&optional purpose)
10548 "Pop up a window for taking a note, and add this note later at point."
10549 (remove-hook 'post-command-hook 'org-add-log-note)
10550 (setq org-log-note-window-configuration (current-window-configuration))
10551 (delete-other-windows)
10552 (move-marker org-log-note-return-to (point))
10553 (switch-to-buffer (marker-buffer org-log-note-marker))
10554 (goto-char org-log-note-marker)
10555 (org-switch-to-buffer-other-window "*Org Note*")
10556 (erase-buffer)
10557 (if (memq org-log-note-how '(time state))
10558 (let (current-prefix-arg) (org-store-log-note))
10559 (let ((org-inhibit-startup t)) (org-mode))
10560 (insert (format "# Insert note for %s.
10561 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
10562 (cond
10563 ((eq org-log-note-purpose 'clock-out) "stopped clock")
10564 ((eq org-log-note-purpose 'done) "closed todo item")
10565 ((eq org-log-note-purpose 'state)
10566 (format "state change from \"%s\" to \"%s\""
10567 (or org-log-note-previous-state "")
10568 (or org-log-note-state "")))
10569 ((eq org-log-note-purpose 'note)
10570 "this entry")
10571 (t (error "This should not happen")))))
10572 (if org-log-note-extra (insert org-log-note-extra))
10573 (org-set-local 'org-finish-function 'org-store-log-note)))
10575 (defvar org-note-abort nil) ; dynamically scoped
10576 (defun org-store-log-note ()
10577 "Finish taking a log note, and insert it to where it belongs."
10578 (let ((txt (buffer-string))
10579 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
10580 lines ind)
10581 (kill-buffer (current-buffer))
10582 (while (string-match "\\`#.*\n[ \t\n]*" txt)
10583 (setq txt (replace-match "" t t txt)))
10584 (if (string-match "\\s-+\\'" txt)
10585 (setq txt (replace-match "" t t txt)))
10586 (setq lines (org-split-string txt "\n"))
10587 (when (and note (string-match "\\S-" note))
10588 (setq note
10589 (org-replace-escapes
10590 note
10591 (list (cons "%u" (user-login-name))
10592 (cons "%U" user-full-name)
10593 (cons "%t" (format-time-string
10594 (org-time-stamp-format 'long 'inactive)
10595 (current-time)))
10596 (cons "%s" (if org-log-note-state
10597 (concat "\"" org-log-note-state "\"")
10598 ""))
10599 (cons "%S" (if org-log-note-previous-state
10600 (concat "\"" org-log-note-previous-state "\"")
10601 "\"\"")))))
10602 (if lines (setq note (concat note " \\\\")))
10603 (push note lines))
10604 (when (or current-prefix-arg org-note-abort)
10605 (when org-log-into-drawer
10606 (org-remove-empty-drawer-at
10607 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
10608 org-log-note-marker))
10609 (setq lines nil))
10610 (when lines
10611 (save-excursion
10612 (set-buffer (marker-buffer org-log-note-marker))
10613 (save-excursion
10614 (goto-char org-log-note-marker)
10615 (move-marker org-log-note-marker nil)
10616 (end-of-line 1)
10617 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
10618 (insert "- " (pop lines))
10619 (org-indent-line-function)
10620 (beginning-of-line 1)
10621 (looking-at "[ \t]*")
10622 (setq ind (concat (match-string 0) " "))
10623 (end-of-line 1)
10624 (while lines (insert "\n" ind (pop lines)))
10625 (message "Note stored")
10626 (org-back-to-heading t)
10627 (org-cycle-hide-drawers 'children)))))
10628 (set-window-configuration org-log-note-window-configuration)
10629 (with-current-buffer (marker-buffer org-log-note-return-to)
10630 (goto-char org-log-note-return-to))
10631 (move-marker org-log-note-return-to nil)
10632 (and org-log-post-message (message "%s" org-log-post-message)))
10634 (defun org-remove-empty-drawer-at (drawer pos)
10635 "Remove an emptyr DARWER drawer at position POS.
10636 POS may also be a marker."
10637 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
10638 (save-excursion
10639 (save-restriction
10640 (widen)
10641 (goto-char pos)
10642 (if (org-in-regexp
10643 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
10644 (replace-match ""))))))
10646 (defun org-sparse-tree (&optional arg)
10647 "Create a sparse tree, prompt for the details.
10648 This command can create sparse trees. You first need to select the type
10649 of match used to create the tree:
10651 t Show entries with a specific TODO keyword.
10652 m Show entries selected by a tags/property match.
10653 p Enter a property name and its value (both with completion on existing
10654 names/values) and show entries with that property.
10655 r Show entries matching a regular expression.
10656 d Show deadlines due within `org-deadline-warning-days'.
10657 b Show deadlines and scheduled items before a date.
10658 a Show deadlines and scheduled items after a date."
10659 (interactive "P")
10660 (let (ans kwd value)
10661 (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
10662 (setq ans (read-char-exclusive))
10663 (cond
10664 ((equal ans ?d)
10665 (call-interactively 'org-check-deadlines))
10666 ((equal ans ?b)
10667 (call-interactively 'org-check-before-date))
10668 ((equal ans ?a)
10669 (call-interactively 'org-check-after-date))
10670 ((equal ans ?t)
10671 (org-show-todo-tree '(4)))
10672 ((member ans '(?T ?m))
10673 (call-interactively 'org-match-sparse-tree))
10674 ((member ans '(?p ?P))
10675 (setq kwd (org-icompleting-read "Property: "
10676 (mapcar 'list (org-buffer-property-keys))))
10677 (setq value (org-icompleting-read "Value: "
10678 (mapcar 'list (org-property-values kwd))))
10679 (unless (string-match "\\`{.*}\\'" value)
10680 (setq value (concat "\"" value "\"")))
10681 (org-match-sparse-tree arg (concat kwd "=" value)))
10682 ((member ans '(?r ?R ?/))
10683 (call-interactively 'org-occur))
10684 (t (error "No such sparse tree command \"%c\"" ans)))))
10686 (defvar org-occur-highlights nil
10687 "List of overlays used for occur matches.")
10688 (make-variable-buffer-local 'org-occur-highlights)
10689 (defvar org-occur-parameters nil
10690 "Parameters of the active org-occur calls.
10691 This is a list, each call to org-occur pushes as cons cell,
10692 containing the regular expression and the callback, onto the list.
10693 The list can contain several entries if `org-occur' has been called
10694 several time with the KEEP-PREVIOUS argument. Otherwise, this list
10695 will only contain one set of parameters. When the highlights are
10696 removed (for example with `C-c C-c', or with the next edit (depending
10697 on `org-remove-highlights-with-change'), this variable is emptied
10698 as well.")
10699 (make-variable-buffer-local 'org-occur-parameters)
10701 (defun org-occur (regexp &optional keep-previous callback)
10702 "Make a compact tree which shows all matches of REGEXP.
10703 The tree will show the lines where the regexp matches, and all higher
10704 headlines above the match. It will also show the heading after the match,
10705 to make sure editing the matching entry is easy.
10706 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
10707 call to `org-occur' will be kept, to allow stacking of calls to this
10708 command.
10709 If CALLBACK is non-nil, it is a function which is called to confirm
10710 that the match should indeed be shown."
10711 (interactive "sRegexp: \nP")
10712 (when (equal regexp "")
10713 (error "Regexp cannot be empty"))
10714 (unless keep-previous
10715 (org-remove-occur-highlights nil nil t))
10716 (push (cons regexp callback) org-occur-parameters)
10717 (let ((cnt 0))
10718 (save-excursion
10719 (goto-char (point-min))
10720 (if (or (not keep-previous) ; do not want to keep
10721 (not org-occur-highlights)) ; no previous matches
10722 ;; hide everything
10723 (org-overview))
10724 (while (re-search-forward regexp nil t)
10725 (when (or (not callback)
10726 (save-match-data (funcall callback)))
10727 (setq cnt (1+ cnt))
10728 (when org-highlight-sparse-tree-matches
10729 (org-highlight-new-match (match-beginning 0) (match-end 0)))
10730 (org-show-context 'occur-tree))))
10731 (when org-remove-highlights-with-change
10732 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
10733 nil 'local))
10734 (unless org-sparse-tree-open-archived-trees
10735 (org-hide-archived-subtrees (point-min) (point-max)))
10736 (run-hooks 'org-occur-hook)
10737 (if (interactive-p)
10738 (message "%d match(es) for regexp %s" cnt regexp))
10739 cnt))
10741 (defun org-show-context (&optional key)
10742 "Make sure point and context and visible.
10743 How much context is shown depends upon the variables
10744 `org-show-hierarchy-above', `org-show-following-heading'. and
10745 `org-show-siblings'."
10746 (let ((heading-p (org-on-heading-p t))
10747 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
10748 (following-p (org-get-alist-option org-show-following-heading key))
10749 (entry-p (org-get-alist-option org-show-entry-below key))
10750 (siblings-p (org-get-alist-option org-show-siblings key)))
10751 (catch 'exit
10752 ;; Show heading or entry text
10753 (if (and heading-p (not entry-p))
10754 (org-flag-heading nil) ; only show the heading
10755 (and (or entry-p (org-invisible-p) (org-invisible-p2))
10756 (org-show-hidden-entry))) ; show entire entry
10757 (when following-p
10758 ;; Show next sibling, or heading below text
10759 (save-excursion
10760 (and (if heading-p (org-goto-sibling) (outline-next-heading))
10761 (org-flag-heading nil))))
10762 (when siblings-p (org-show-siblings))
10763 (when hierarchy-p
10764 ;; show all higher headings, possibly with siblings
10765 (save-excursion
10766 (while (and (condition-case nil
10767 (progn (org-up-heading-all 1) t)
10768 (error nil))
10769 (not (bobp)))
10770 (org-flag-heading nil)
10771 (when siblings-p (org-show-siblings))))))))
10773 (defun org-reveal (&optional siblings)
10774 "Show current entry, hierarchy above it, and the following headline.
10775 This can be used to show a consistent set of context around locations
10776 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
10777 not t for the search context.
10779 With optional argument SIBLINGS, on each level of the hierarchy all
10780 siblings are shown. This repairs the tree structure to what it would
10781 look like when opened with hierarchical calls to `org-cycle'."
10782 (interactive "P")
10783 (let ((org-show-hierarchy-above t)
10784 (org-show-following-heading t)
10785 (org-show-siblings (if siblings t org-show-siblings)))
10786 (org-show-context nil)))
10788 (defun org-highlight-new-match (beg end)
10789 "Highlight from BEG to END and mark the highlight is an occur headline."
10790 (let ((ov (org-make-overlay beg end)))
10791 (org-overlay-put ov 'face 'secondary-selection)
10792 (push ov org-occur-highlights)))
10794 (defun org-remove-occur-highlights (&optional beg end noremove)
10795 "Remove the occur highlights from the buffer.
10796 BEG and END are ignored. If NOREMOVE is nil, remove this function
10797 from the `before-change-functions' in the current buffer."
10798 (interactive)
10799 (unless org-inhibit-highlight-removal
10800 (mapc 'org-delete-overlay org-occur-highlights)
10801 (setq org-occur-highlights nil)
10802 (setq org-occur-parameters nil)
10803 (unless noremove
10804 (remove-hook 'before-change-functions
10805 'org-remove-occur-highlights 'local))))
10807 ;;;; Priorities
10809 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
10810 "Regular expression matching the priority indicator.")
10812 (defvar org-remove-priority-next-time nil)
10814 (defun org-priority-up ()
10815 "Increase the priority of the current item."
10816 (interactive)
10817 (org-priority 'up))
10819 (defun org-priority-down ()
10820 "Decrease the priority of the current item."
10821 (interactive)
10822 (org-priority 'down))
10824 (defun org-priority (&optional action)
10825 "Change the priority of an item by ARG.
10826 ACTION can be `set', `up', `down', or a character."
10827 (interactive)
10828 (unless org-enable-priority-commands
10829 (error "Priority commands are disabled"))
10830 (setq action (or action 'set))
10831 (let (current new news have remove)
10832 (save-excursion
10833 (org-back-to-heading t)
10834 (if (looking-at org-priority-regexp)
10835 (setq current (string-to-char (match-string 2))
10836 have t)
10837 (setq current org-default-priority))
10838 (cond
10839 ((eq action 'remove)
10840 (setq remove t new ?\ ))
10841 ((or (eq action 'set)
10842 (if (featurep 'xemacs) (characterp action) (integerp action)))
10843 (if (not (eq action 'set))
10844 (setq new action)
10845 (message "Priority %c-%c, SPC to remove: "
10846 org-highest-priority org-lowest-priority)
10847 (setq new (read-char-exclusive)))
10848 (if (and (= (upcase org-highest-priority) org-highest-priority)
10849 (= (upcase org-lowest-priority) org-lowest-priority))
10850 (setq new (upcase new)))
10851 (cond ((equal new ?\ ) (setq remove t))
10852 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
10853 (error "Priority must be between `%c' and `%c'"
10854 org-highest-priority org-lowest-priority))))
10855 ((eq action 'up)
10856 (if (and (not have) (eq last-command this-command))
10857 (setq new org-lowest-priority)
10858 (setq new (if (and org-priority-start-cycle-with-default (not have))
10859 org-default-priority (1- current)))))
10860 ((eq action 'down)
10861 (if (and (not have) (eq last-command this-command))
10862 (setq new org-highest-priority)
10863 (setq new (if (and org-priority-start-cycle-with-default (not have))
10864 org-default-priority (1+ current)))))
10865 (t (error "Invalid action")))
10866 (if (or (< (upcase new) org-highest-priority)
10867 (> (upcase new) org-lowest-priority))
10868 (setq remove t))
10869 (setq news (format "%c" new))
10870 (if have
10871 (if remove
10872 (replace-match "" t t nil 1)
10873 (replace-match news t t nil 2))
10874 (if remove
10875 (error "No priority cookie found in line")
10876 (let ((case-fold-search nil))
10877 (looking-at org-todo-line-regexp))
10878 (if (match-end 2)
10879 (progn
10880 (goto-char (match-end 2))
10881 (insert " [#" news "]"))
10882 (goto-char (match-beginning 3))
10883 (insert "[#" news "] "))))
10884 (org-preserve-lc (org-set-tags nil 'align)))
10885 (if remove
10886 (message "Priority removed")
10887 (message "Priority of current item set to %s" news))))
10889 (defun org-get-priority (s)
10890 "Find priority cookie and return priority."
10891 (save-match-data
10892 (if (not (string-match org-priority-regexp s))
10893 (* 1000 (- org-lowest-priority org-default-priority))
10894 (* 1000 (- org-lowest-priority
10895 (string-to-char (match-string 2 s)))))))
10897 ;;;; Tags
10899 (defvar org-agenda-archives-mode)
10900 (defvar org-map-continue-from nil
10901 "Position from where mapping should continue.
10902 Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
10904 (defvar org-scanner-tags nil
10905 "The current tag list while the tags scanner is running.")
10906 (defvar org-trust-scanner-tags nil
10907 "Should `org-get-tags-at' use the tags fro the scanner.
10908 This is for internal dynamical scoping only.
10909 When this is non-nil, the function `org-get-tags-at' will return the value
10910 of `org-scanner-tags' instead of building the list by itself. This
10911 can lead to large speed-ups when the tags scanner is used in a file with
10912 many entries, and when the list of tags is retrieved, for example to
10913 obtain a list of properties. Building the tags list for each entry in such
10914 a file becomes an N^2 operation - but with this variable set, it scales
10915 as N.")
10917 (defun org-scan-tags (action matcher &optional todo-only)
10918 "Scan headline tags with inheritance and produce output ACTION.
10920 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
10921 or `agenda' to produce an entry list for an agenda view. It can also be
10922 a Lisp form or a function that should be called at each matched headline, in
10923 this case the return value is a list of all return values from these calls.
10925 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
10926 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
10927 only lines with a TODO keyword are included in the output."
10928 (require 'org-agenda)
10929 (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
10930 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
10931 (org-re
10932 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
10933 (props (list 'face 'default
10934 'done-face 'org-agenda-done
10935 'undone-face 'default
10936 'mouse-face 'highlight
10937 'org-not-done-regexp org-not-done-regexp
10938 'org-todo-regexp org-todo-regexp
10939 'keymap org-agenda-keymap
10940 'help-echo
10941 (format "mouse-2 or RET jump to org file %s"
10942 (abbreviate-file-name
10943 (or (buffer-file-name (buffer-base-buffer))
10944 (buffer-name (buffer-base-buffer)))))))
10945 (case-fold-search nil)
10946 (org-map-continue-from nil)
10947 lspos tags tags-list
10948 (tags-alist (list (cons 0 org-file-tags)))
10949 (llast 0) rtn rtn1 level category i txt
10950 todo marker entry priority)
10951 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
10952 (setq action (list 'lambda nil action)))
10953 (save-excursion
10954 (goto-char (point-min))
10955 (when (eq action 'sparse-tree)
10956 (org-overview)
10957 (org-remove-occur-highlights))
10958 (while (re-search-forward re nil t)
10959 (catch :skip
10960 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
10961 tags (if (match-end 4) (org-match-string-no-properties 4)))
10962 (goto-char (setq lspos (match-beginning 0)))
10963 (setq level (org-reduced-level (funcall outline-level))
10964 category (org-get-category))
10965 (setq i llast llast level)
10966 ;; remove tag lists from same and sublevels
10967 (while (>= i level)
10968 (when (setq entry (assoc i tags-alist))
10969 (setq tags-alist (delete entry tags-alist)))
10970 (setq i (1- i)))
10971 ;; add the next tags
10972 (when tags
10973 (setq tags (org-split-string tags ":")
10974 tags-alist
10975 (cons (cons level tags) tags-alist)))
10976 ;; compile tags for current headline
10977 (setq tags-list
10978 (if org-use-tag-inheritance
10979 (apply 'append (mapcar 'cdr (reverse tags-alist)))
10980 tags)
10981 org-scanner-tags tags-list)
10982 (when org-use-tag-inheritance
10983 (setcdr (car tags-alist)
10984 (mapcar (lambda (x)
10985 (setq x (copy-sequence x))
10986 (org-add-prop-inherited x))
10987 (cdar tags-alist))))
10988 (when (and tags org-use-tag-inheritance
10989 (or (not (eq t org-use-tag-inheritance))
10990 org-tags-exclude-from-inheritance))
10991 ;; selective inheritance, remove uninherited ones
10992 (setcdr (car tags-alist)
10993 (org-remove-uniherited-tags (cdar tags-alist))))
10994 (when (and (or (not todo-only)
10995 (and (member todo org-not-done-keywords)
10996 (or (not org-agenda-tags-todo-honor-ignore-options)
10997 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
10998 (let ((case-fold-search t)) (eval matcher))
11000 (not (member org-archive-tag tags-list))
11001 ;; we have an archive tag, should we use this anyway?
11002 (or (not org-agenda-skip-archived-trees)
11003 (and (eq action 'agenda) org-agenda-archives-mode))))
11004 (unless (eq action 'sparse-tree) (org-agenda-skip))
11006 ;; select this headline
11008 (cond
11009 ((eq action 'sparse-tree)
11010 (and org-highlight-sparse-tree-matches
11011 (org-get-heading) (match-end 0)
11012 (org-highlight-new-match
11013 (match-beginning 0) (match-beginning 1)))
11014 (org-show-context 'tags-tree))
11015 ((eq action 'agenda)
11016 (setq txt (org-format-agenda-item
11018 (concat
11019 (if (eq org-tags-match-list-sublevels 'indented)
11020 (make-string (1- level) ?.) "")
11021 (org-get-heading))
11022 category
11023 tags-list
11025 priority (org-get-priority txt))
11026 (goto-char lspos)
11027 (setq marker (org-agenda-new-marker))
11028 (org-add-props txt props
11029 'org-marker marker 'org-hd-marker marker 'org-category category
11030 'todo-state todo
11031 'priority priority 'type "tagsmatch")
11032 (push txt rtn))
11033 ((functionp action)
11034 (setq org-map-continue-from nil)
11035 (save-excursion
11036 (setq rtn1 (funcall action))
11037 (push rtn1 rtn)))
11038 (t (error "Invalid action")))
11040 ;; if we are to skip sublevels, jump to end of subtree
11041 (unless org-tags-match-list-sublevels
11042 (org-end-of-subtree t)
11043 (backward-char 1))))
11044 ;; Get the correct position from where to continue
11045 (if org-map-continue-from
11046 (goto-char org-map-continue-from)
11047 (and (= (point) lspos) (end-of-line 1)))))
11048 (when (and (eq action 'sparse-tree)
11049 (not org-sparse-tree-open-archived-trees))
11050 (org-hide-archived-subtrees (point-min) (point-max)))
11051 (nreverse rtn)))
11053 (defun org-remove-uniherited-tags (tags)
11054 "Remove all tags that are not inherited from the list TAGS."
11055 (cond
11056 ((eq org-use-tag-inheritance t)
11057 (if org-tags-exclude-from-inheritance
11058 (org-delete-all org-tags-exclude-from-inheritance tags)
11059 tags))
11060 ((not org-use-tag-inheritance) nil)
11061 ((stringp org-use-tag-inheritance)
11062 (delq nil (mapcar
11063 (lambda (x)
11064 (if (and (string-match org-use-tag-inheritance x)
11065 (not (member x org-tags-exclude-from-inheritance)))
11066 x nil))
11067 tags)))
11068 ((listp org-use-tag-inheritance)
11069 (delq nil (mapcar
11070 (lambda (x)
11071 (if (member x org-use-tag-inheritance) x nil))
11072 tags)))))
11074 (defvar todo-only) ;; dynamically scoped
11076 (defun org-match-sparse-tree (&optional todo-only match)
11077 "Create a sparse tree according to tags string MATCH.
11078 MATCH can contain positive and negative selection of tags, like
11079 \"+WORK+URGENT-WITHBOSS\".
11080 If optional argument TODO-ONLY is non-nil, only select lines that are
11081 also TODO lines."
11082 (interactive "P")
11083 (org-prepare-agenda-buffers (list (current-buffer)))
11084 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
11086 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
11088 (defvar org-cached-props nil)
11089 (defun org-cached-entry-get (pom property)
11090 (if (or (eq t org-use-property-inheritance)
11091 (and (stringp org-use-property-inheritance)
11092 (string-match org-use-property-inheritance property))
11093 (and (listp org-use-property-inheritance)
11094 (member property org-use-property-inheritance)))
11095 ;; Caching is not possible, check it directly
11096 (org-entry-get pom property 'inherit)
11097 ;; Get all properties, so that we can do complicated checks easily
11098 (cdr (assoc property (or org-cached-props
11099 (setq org-cached-props
11100 (org-entry-properties pom)))))))
11102 (defun org-global-tags-completion-table (&optional files)
11103 "Return the list of all tags in all agenda buffer/files."
11104 (save-excursion
11105 (org-uniquify
11106 (delq nil
11107 (apply 'append
11108 (mapcar
11109 (lambda (file)
11110 (set-buffer (find-file-noselect file))
11111 (append (org-get-buffer-tags)
11112 (mapcar (lambda (x) (if (stringp (car-safe x))
11113 (list (car-safe x)) nil))
11114 org-tag-alist)))
11115 (if (and files (car files))
11116 files
11117 (org-agenda-files))))))))
11119 (defun org-make-tags-matcher (match)
11120 "Create the TAGS//TODO matcher form for the selection string MATCH."
11121 ;; todo-only is scoped dynamically into this function, and the function
11122 ;; may change it if the matcher asks for it.
11123 (unless match
11124 ;; Get a new match request, with completion
11125 (let ((org-last-tags-completion-table
11126 (org-global-tags-completion-table)))
11127 (setq match (org-completing-read-no-i
11128 "Match: " 'org-tags-completion-function nil nil nil
11129 'org-tags-history))))
11131 ;; Parse the string and create a lisp form
11132 (let ((match0 match)
11133 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
11134 minus tag mm
11135 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
11136 orterms term orlist re-p str-p level-p level-op time-p
11137 prop-p pn pv po cat-p gv rest)
11138 (if (string-match "/+" match)
11139 ;; match contains also a todo-matching request
11140 (progn
11141 (setq tagsmatch (substring match 0 (match-beginning 0))
11142 todomatch (substring match (match-end 0)))
11143 (if (string-match "^!" todomatch)
11144 (setq todo-only t todomatch (substring todomatch 1)))
11145 (if (string-match "^\\s-*$" todomatch)
11146 (setq todomatch nil)))
11147 ;; only matching tags
11148 (setq tagsmatch match todomatch nil))
11150 ;; Make the tags matcher
11151 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
11152 (setq tagsmatcher t)
11153 (setq orterms (org-split-string tagsmatch "|") orlist nil)
11154 (while (setq term (pop orterms))
11155 (while (and (equal (substring term -1) "\\") orterms)
11156 (setq term (concat term "|" (pop orterms)))) ; repair bad split
11157 (while (string-match re term)
11158 (setq rest (substring term (match-end 0))
11159 minus (and (match-end 1)
11160 (equal (match-string 1 term) "-"))
11161 tag (match-string 2 term)
11162 re-p (equal (string-to-char tag) ?{)
11163 level-p (match-end 4)
11164 prop-p (match-end 5)
11165 mm (cond
11166 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
11167 (level-p
11168 (setq level-op (org-op-to-function (match-string 3 term)))
11169 `(,level-op level ,(string-to-number
11170 (match-string 4 term))))
11171 (prop-p
11172 (setq pn (match-string 5 term)
11173 po (match-string 6 term)
11174 pv (match-string 7 term)
11175 cat-p (equal pn "CATEGORY")
11176 re-p (equal (string-to-char pv) ?{)
11177 str-p (equal (string-to-char pv) ?\")
11178 time-p (save-match-data
11179 (string-match "^\"[[<].*[]>]\"$" pv))
11180 pv (if (or re-p str-p) (substring pv 1 -1) pv))
11181 (if time-p (setq pv (org-matcher-time pv)))
11182 (setq po (org-op-to-function po (if time-p 'time str-p)))
11183 (cond
11184 ((equal pn "CATEGORY")
11185 (setq gv '(get-text-property (point) 'org-category)))
11186 ((equal pn "TODO")
11187 (setq gv 'todo))
11189 (setq gv `(org-cached-entry-get nil ,pn))))
11190 (if re-p
11191 (if (eq po 'org<>)
11192 `(not (string-match ,pv (or ,gv "")))
11193 `(string-match ,pv (or ,gv "")))
11194 (if str-p
11195 `(,po (or ,gv "") ,pv)
11196 `(,po (string-to-number (or ,gv ""))
11197 ,(string-to-number pv) ))))
11198 (t `(member ,tag tags-list)))
11199 mm (if minus (list 'not mm) mm)
11200 term rest)
11201 (push mm tagsmatcher))
11202 (push (if (> (length tagsmatcher) 1)
11203 (cons 'and tagsmatcher)
11204 (car tagsmatcher))
11205 orlist)
11206 (setq tagsmatcher nil))
11207 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
11208 (setq tagsmatcher
11209 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
11210 ;; Make the todo matcher
11211 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
11212 (setq todomatcher t)
11213 (setq orterms (org-split-string todomatch "|") orlist nil)
11214 (while (setq term (pop orterms))
11215 (while (string-match re term)
11216 (setq minus (and (match-end 1)
11217 (equal (match-string 1 term) "-"))
11218 kwd (match-string 2 term)
11219 re-p (equal (string-to-char kwd) ?{)
11220 term (substring term (match-end 0))
11221 mm (if re-p
11222 `(string-match ,(substring kwd 1 -1) todo)
11223 (list 'equal 'todo kwd))
11224 mm (if minus (list 'not mm) mm))
11225 (push mm todomatcher))
11226 (push (if (> (length todomatcher) 1)
11227 (cons 'and todomatcher)
11228 (car todomatcher))
11229 orlist)
11230 (setq todomatcher nil))
11231 (setq todomatcher (if (> (length orlist) 1)
11232 (cons 'or orlist) (car orlist))))
11234 ;; Return the string and lisp forms of the matcher
11235 (setq matcher (if todomatcher
11236 (list 'and tagsmatcher todomatcher)
11237 tagsmatcher))
11238 (cons match0 matcher)))
11240 (defun org-op-to-function (op &optional stringp)
11241 "Turn an operator into the appropriate function."
11242 (setq op
11243 (cond
11244 ((equal op "<" ) '(< string< org-time<))
11245 ((equal op ">" ) '(> org-string> org-time>))
11246 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
11247 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
11248 ((member op '("=" "==")) '(= string= org-time=))
11249 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
11250 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
11252 (defun org<> (a b) (not (= a b)))
11253 (defun org-string<= (a b) (or (string= a b) (string< a b)))
11254 (defun org-string>= (a b) (not (string< a b)))
11255 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
11256 (defun org-string<> (a b) (not (string= a b)))
11257 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
11258 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
11259 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
11260 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
11261 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
11262 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
11263 (defun org-2ft (s)
11264 "Convert S to a floating point time.
11265 If S is already a number, just return it. If it is a string, parse
11266 it as a time string and apply `float-time' to it. If S is nil, just return 0."
11267 (cond
11268 ((numberp s) s)
11269 ((stringp s)
11270 (condition-case nil
11271 (float-time (apply 'encode-time (org-parse-time-string s)))
11272 (error 0.)))
11273 (t 0.)))
11275 (defun org-time-today ()
11276 "Time in seconds today at 0:00.
11277 Returns the float number of seconds since the beginning of the
11278 epoch to the beginning of today (00:00)."
11279 (float-time (apply 'encode-time
11280 (append '(0 0 0) (nthcdr 3 (decode-time))))))
11282 (defun org-matcher-time (s)
11283 "Interpret a time comparison value."
11284 (save-match-data
11285 (cond
11286 ((string= s "<now>") (float-time))
11287 ((string= s "<today>") (org-time-today))
11288 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
11289 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
11290 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
11291 (+ (org-time-today)
11292 (* (string-to-number (match-string 1 s))
11293 (cdr (assoc (match-string 2 s)
11294 '(("d" . 86400.0) ("w" . 604800.0)
11295 ("m" . 2678400.0) ("y" . 31557600.0)))))))
11296 (t (org-2ft s)))))
11298 (defun org-match-any-p (re list)
11299 "Does re match any element of list?"
11300 (setq list (mapcar (lambda (x) (string-match re x)) list))
11301 (delq nil list))
11303 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
11304 (defvar org-tags-overlay (org-make-overlay 1 1))
11305 (org-detach-overlay org-tags-overlay)
11307 (defun org-get-local-tags-at (&optional pos)
11308 "Get a list of tags defined in the current headline."
11309 (org-get-tags-at pos 'local))
11311 (defun org-get-local-tags ()
11312 "Get a list of tags defined in the current headline."
11313 (org-get-tags-at nil 'local))
11315 (defun org-get-tags-at (&optional pos local)
11316 "Get a list of all headline tags applicable at POS.
11317 POS defaults to point. If tags are inherited, the list contains
11318 the targets in the same sequence as the headlines appear, i.e.
11319 the tags of the current headline come last.
11320 When LOCAL is non-nil, only return tags from the current headline,
11321 ignore inherited ones."
11322 (interactive)
11323 (if (and org-trust-scanner-tags
11324 (or (not pos) (equal pos (point)))
11325 (not local))
11326 org-scanner-tags
11327 (let (tags ltags lastpos parent)
11328 (save-excursion
11329 (save-restriction
11330 (widen)
11331 (goto-char (or pos (point)))
11332 (save-match-data
11333 (catch 'done
11334 (condition-case nil
11335 (progn
11336 (org-back-to-heading t)
11337 (while (not (equal lastpos (point)))
11338 (setq lastpos (point))
11339 (when (looking-at
11340 (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
11341 (setq ltags (org-split-string
11342 (org-match-string-no-properties 1) ":"))
11343 (when parent
11344 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
11345 (setq tags (append
11346 (if parent
11347 (org-remove-uniherited-tags ltags)
11348 ltags)
11349 tags)))
11350 (or org-use-tag-inheritance (throw 'done t))
11351 (if local (throw 'done t))
11352 (or (org-up-heading-safe) (error nil))
11353 (setq parent t)))
11354 (error nil)))))
11355 (append (org-remove-uniherited-tags org-file-tags) tags)))))
11357 (defun org-add-prop-inherited (s)
11358 (add-text-properties 0 (length s) '(inherited t) s)
11361 (defun org-toggle-tag (tag &optional onoff)
11362 "Toggle the tag TAG for the current line.
11363 If ONOFF is `on' or `off', don't toggle but set to this state."
11364 (let (res current)
11365 (save-excursion
11366 (org-back-to-heading t)
11367 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
11368 (point-at-eol) t)
11369 (progn
11370 (setq current (match-string 1))
11371 (replace-match ""))
11372 (setq current ""))
11373 (setq current (nreverse (org-split-string current ":")))
11374 (cond
11375 ((eq onoff 'on)
11376 (setq res t)
11377 (or (member tag current) (push tag current)))
11378 ((eq onoff 'off)
11379 (or (not (member tag current)) (setq current (delete tag current))))
11380 (t (if (member tag current)
11381 (setq current (delete tag current))
11382 (setq res t)
11383 (push tag current))))
11384 (end-of-line 1)
11385 (if current
11386 (progn
11387 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
11388 (org-set-tags nil t))
11389 (delete-horizontal-space))
11390 (run-hooks 'org-after-tags-change-hook))
11391 res))
11393 (defun org-align-tags-here (to-col)
11394 ;; Assumes that this is a headline
11395 (let ((pos (point)) (col (current-column)) ncol tags-l p)
11396 (beginning-of-line 1)
11397 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
11398 (< pos (match-beginning 2)))
11399 (progn
11400 (setq tags-l (- (match-end 2) (match-beginning 2)))
11401 (goto-char (match-beginning 1))
11402 (insert " ")
11403 (delete-region (point) (1+ (match-beginning 2)))
11404 (setq ncol (max (1+ (current-column))
11405 (1+ col)
11406 (if (> to-col 0)
11407 to-col
11408 (- (abs to-col) tags-l))))
11409 (setq p (point))
11410 (insert (make-string (- ncol (current-column)) ?\ ))
11411 (setq ncol (current-column))
11412 (when indent-tabs-mode (tabify p (point-at-eol)))
11413 (org-move-to-column (min ncol col) t))
11414 (goto-char pos))))
11416 (defun org-set-tags-command (&optional arg just-align)
11417 "Call the set-tags command for the current entry."
11418 (interactive "P")
11419 (if (org-on-heading-p)
11420 (org-set-tags arg just-align)
11421 (save-excursion
11422 (org-back-to-heading t)
11423 (org-set-tags arg just-align))))
11425 (defun org-set-tags-to (data)
11426 "Set the tags of the current entry to DATA, replacing the current tags.
11427 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
11428 If DATA is nil or the empty string, any tags will be removed."
11429 (interactive "sTags: ")
11430 (setq data
11431 (cond
11432 ((eq data nil) "")
11433 ((equal data "") "")
11434 ((stringp data)
11435 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
11436 ":"))
11437 ((listp data)
11438 (concat ":" (mapconcat 'identity data ":") ":"))
11439 (t nil)))
11440 (when data
11441 (save-excursion
11442 (org-back-to-heading t)
11443 (when (looking-at org-complex-heading-regexp)
11444 (if (match-end 5)
11445 (progn
11446 (goto-char (match-beginning 5))
11447 (insert data)
11448 (delete-region (point) (point-at-eol))
11449 (org-set-tags nil 'align))
11450 (goto-char (point-at-eol))
11451 (insert " " data)
11452 (org-set-tags nil 'align)))
11453 (beginning-of-line 1)
11454 (if (looking-at ".*?\\([ \t]+\\)$")
11455 (delete-region (match-beginning 1) (match-end 1))))))
11457 (defun org-set-tags (&optional arg just-align)
11458 "Set the tags for the current headline.
11459 With prefix ARG, realign all tags in headings in the current buffer."
11460 (interactive "P")
11461 (let* ((re (concat "^" outline-regexp))
11462 (current (org-get-tags-string))
11463 (col (current-column))
11464 (org-setting-tags t)
11465 table current-tags inherited-tags ; computed below when needed
11466 tags p0 c0 c1 rpl)
11467 (if arg
11468 (save-excursion
11469 (goto-char (point-min))
11470 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
11471 (while (re-search-forward re nil t)
11472 (org-set-tags nil t)
11473 (end-of-line 1)))
11474 (message "All tags realigned to column %d" org-tags-column))
11475 (if just-align
11476 (setq tags current)
11477 ;; Get a new set of tags from the user
11478 (save-excursion
11479 (setq table (append org-tag-persistent-alist
11480 (or org-tag-alist (org-get-buffer-tags)))
11481 org-last-tags-completion-table table
11482 current-tags (org-split-string current ":")
11483 inherited-tags (nreverse
11484 (nthcdr (length current-tags)
11485 (nreverse (org-get-tags-at))))
11486 tags
11487 (if (or (eq t org-use-fast-tag-selection)
11488 (and org-use-fast-tag-selection
11489 (delq nil (mapcar 'cdr table))))
11490 (org-fast-tag-selection
11491 current-tags inherited-tags table
11492 (if org-fast-tag-selection-include-todo org-todo-key-alist))
11493 (let ((org-add-colon-after-tag-completion t))
11494 (org-trim
11495 (org-without-partial-completion
11496 (org-icompleting-read "Tags: " 'org-tags-completion-function
11497 nil nil current 'org-tags-history)))))))
11498 (while (string-match "[-+&]+" tags)
11499 ;; No boolean logic, just a list
11500 (setq tags (replace-match ":" t t tags))))
11502 (if org-tags-sort-function
11503 (setq tags (mapconcat 'identity
11504 (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
11505 org-tags-sort-function) ":")))
11507 (if (string-match "\\`[\t ]*\\'" tags)
11508 (setq tags "")
11509 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
11510 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
11512 ;; Insert new tags at the correct column
11513 (beginning-of-line 1)
11514 (cond
11515 ((and (equal current "") (equal tags "")))
11516 ((re-search-forward
11517 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
11518 (point-at-eol) t)
11519 (if (equal tags "")
11520 (setq rpl "")
11521 (goto-char (match-beginning 0))
11522 (setq c0 (current-column) p0 (point)
11523 c1 (max (1+ c0) (if (> org-tags-column 0)
11524 org-tags-column
11525 (- (- org-tags-column) (length tags))))
11526 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
11527 (replace-match rpl t t)
11528 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
11529 tags)
11530 (t (error "Tags alignment failed")))
11531 (org-move-to-column col)
11532 (unless just-align
11533 (run-hooks 'org-after-tags-change-hook)))))
11535 (defun org-change-tag-in-region (beg end tag off)
11536 "Add or remove TAG for each entry in the region.
11537 This works in the agenda, and also in an org-mode buffer."
11538 (interactive
11539 (list (region-beginning) (region-end)
11540 (let ((org-last-tags-completion-table
11541 (if (org-mode-p)
11542 (org-get-buffer-tags)
11543 (org-global-tags-completion-table))))
11544 (org-icompleting-read
11545 "Tag: " 'org-tags-completion-function nil nil nil
11546 'org-tags-history))
11547 (progn
11548 (message "[s]et or [r]emove? ")
11549 (equal (read-char-exclusive) ?r))))
11550 (if (fboundp 'deactivate-mark) (deactivate-mark))
11551 (let ((agendap (equal major-mode 'org-agenda-mode))
11552 l1 l2 m buf pos newhead (cnt 0))
11553 (goto-char end)
11554 (setq l2 (1- (org-current-line)))
11555 (goto-char beg)
11556 (setq l1 (org-current-line))
11557 (loop for l from l1 to l2 do
11558 (org-goto-line l)
11559 (setq m (get-text-property (point) 'org-hd-marker))
11560 (when (or (and (org-mode-p) (org-on-heading-p))
11561 (and agendap m))
11562 (setq buf (if agendap (marker-buffer m) (current-buffer))
11563 pos (if agendap m (point)))
11564 (with-current-buffer buf
11565 (save-excursion
11566 (save-restriction
11567 (goto-char pos)
11568 (setq cnt (1+ cnt))
11569 (org-toggle-tag tag (if off 'off 'on))
11570 (setq newhead (org-get-heading)))))
11571 (and agendap (org-agenda-change-all-lines newhead m))))
11572 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
11574 (defun org-tags-completion-function (string predicate &optional flag)
11575 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
11576 (confirm (lambda (x) (stringp (car x)))))
11577 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
11578 (setq s1 (match-string 1 string)
11579 s2 (match-string 2 string))
11580 (setq s1 "" s2 string))
11581 (cond
11582 ((eq flag nil)
11583 ;; try completion
11584 (setq rtn (try-completion s2 ctable confirm))
11585 (if (stringp rtn)
11586 (setq rtn
11587 (concat s1 s2 (substring rtn (length s2))
11588 (if (and org-add-colon-after-tag-completion
11589 (assoc rtn ctable))
11590 ":" ""))))
11591 rtn)
11592 ((eq flag t)
11593 ;; all-completions
11594 (all-completions s2 ctable confirm)
11596 ((eq flag 'lambda)
11597 ;; exact match?
11598 (assoc s2 ctable)))
11601 (defun org-fast-tag-insert (kwd tags face &optional end)
11602 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
11603 (insert (format "%-12s" (concat kwd ":"))
11604 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
11605 (or end "")))
11607 (defun org-fast-tag-show-exit (flag)
11608 (save-excursion
11609 (org-goto-line 3)
11610 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
11611 (replace-match ""))
11612 (when flag
11613 (end-of-line 1)
11614 (org-move-to-column (- (window-width) 19) t)
11615 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
11617 (defun org-set-current-tags-overlay (current prefix)
11618 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
11619 (if (featurep 'xemacs)
11620 (org-overlay-display org-tags-overlay (concat prefix s)
11621 'secondary-selection)
11622 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
11623 (org-overlay-display org-tags-overlay (concat prefix s)))))
11625 (defun org-fast-tag-selection (current inherited table &optional todo-table)
11626 "Fast tag selection with single keys.
11627 CURRENT is the current list of tags in the headline, INHERITED is the
11628 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
11629 possibly with grouping information. TODO-TABLE is a similar table with
11630 TODO keywords, should these have keys assigned to them.
11631 If the keys are nil, a-z are automatically assigned.
11632 Returns the new tags string, or nil to not change the current settings."
11633 (let* ((fulltable (append table todo-table))
11634 (maxlen (apply 'max (mapcar
11635 (lambda (x)
11636 (if (stringp (car x)) (string-width (car x)) 0))
11637 fulltable)))
11638 (buf (current-buffer))
11639 (expert (eq org-fast-tag-selection-single-key 'expert))
11640 (buffer-tags nil)
11641 (fwidth (+ maxlen 3 1 3))
11642 (ncol (/ (- (window-width) 4) fwidth))
11643 (i-face 'org-done)
11644 (c-face 'org-todo)
11645 tg cnt e c char c1 c2 ntable tbl rtn
11646 ov-start ov-end ov-prefix
11647 (exit-after-next org-fast-tag-selection-single-key)
11648 (done-keywords org-done-keywords)
11649 groups ingroup)
11650 (save-excursion
11651 (beginning-of-line 1)
11652 (if (looking-at
11653 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
11654 (setq ov-start (match-beginning 1)
11655 ov-end (match-end 1)
11656 ov-prefix "")
11657 (setq ov-start (1- (point-at-eol))
11658 ov-end (1+ ov-start))
11659 (skip-chars-forward "^\n\r")
11660 (setq ov-prefix
11661 (concat
11662 (buffer-substring (1- (point)) (point))
11663 (if (> (current-column) org-tags-column)
11665 (make-string (- org-tags-column (current-column)) ?\ ))))))
11666 (org-move-overlay org-tags-overlay ov-start ov-end)
11667 (save-window-excursion
11668 (if expert
11669 (set-buffer (get-buffer-create " *Org tags*"))
11670 (delete-other-windows)
11671 (split-window-vertically)
11672 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
11673 (erase-buffer)
11674 (org-set-local 'org-done-keywords done-keywords)
11675 (org-fast-tag-insert "Inherited" inherited i-face "\n")
11676 (org-fast-tag-insert "Current" current c-face "\n\n")
11677 (org-fast-tag-show-exit exit-after-next)
11678 (org-set-current-tags-overlay current ov-prefix)
11679 (setq tbl fulltable char ?a cnt 0)
11680 (while (setq e (pop tbl))
11681 (cond
11682 ((equal (car e) :startgroup)
11683 (push '() groups) (setq ingroup t)
11684 (when (not (= cnt 0))
11685 (setq cnt 0)
11686 (insert "\n"))
11687 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
11688 ((equal (car e) :endgroup)
11689 (setq ingroup nil cnt 0)
11690 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
11691 ((equal e '(:newline))
11692 (when (not (= cnt 0))
11693 (setq cnt 0)
11694 (insert "\n")
11695 (setq e (car tbl))
11696 (while (equal (car tbl) '(:newline))
11697 (insert "\n")
11698 (setq tbl (cdr tbl)))))
11700 (setq tg (copy-sequence (car e)) c2 nil)
11701 (if (cdr e)
11702 (setq c (cdr e))
11703 ;; automatically assign a character.
11704 (setq c1 (string-to-char
11705 (downcase (substring
11706 tg (if (= (string-to-char tg) ?@) 1 0)))))
11707 (if (or (rassoc c1 ntable) (rassoc c1 table))
11708 (while (or (rassoc char ntable) (rassoc char table))
11709 (setq char (1+ char)))
11710 (setq c2 c1))
11711 (setq c (or c2 char)))
11712 (if ingroup (push tg (car groups)))
11713 (setq tg (org-add-props tg nil 'face
11714 (cond
11715 ((not (assoc tg table))
11716 (org-get-todo-face tg))
11717 ((member tg current) c-face)
11718 ((member tg inherited) i-face)
11719 (t nil))))
11720 (if (and (= cnt 0) (not ingroup)) (insert " "))
11721 (insert "[" c "] " tg (make-string
11722 (- fwidth 4 (length tg)) ?\ ))
11723 (push (cons tg c) ntable)
11724 (when (= (setq cnt (1+ cnt)) ncol)
11725 (insert "\n")
11726 (if ingroup (insert " "))
11727 (setq cnt 0)))))
11728 (setq ntable (nreverse ntable))
11729 (insert "\n")
11730 (goto-char (point-min))
11731 (if (not expert) (org-fit-window-to-buffer))
11732 (setq rtn
11733 (catch 'exit
11734 (while t
11735 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
11736 (if (not groups) "no " "")
11737 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
11738 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
11739 (cond
11740 ((= c ?\r) (throw 'exit t))
11741 ((= c ?!)
11742 (setq groups (not groups))
11743 (goto-char (point-min))
11744 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
11745 ((= c ?\C-c)
11746 (if (not expert)
11747 (org-fast-tag-show-exit
11748 (setq exit-after-next (not exit-after-next)))
11749 (setq expert nil)
11750 (delete-other-windows)
11751 (split-window-vertically)
11752 (org-switch-to-buffer-other-window " *Org tags*")
11753 (org-fit-window-to-buffer)))
11754 ((or (= c ?\C-g)
11755 (and (= c ?q) (not (rassoc c ntable))))
11756 (org-detach-overlay org-tags-overlay)
11757 (setq quit-flag t))
11758 ((= c ?\ )
11759 (setq current nil)
11760 (if exit-after-next (setq exit-after-next 'now)))
11761 ((= c ?\t)
11762 (condition-case nil
11763 (setq tg (org-icompleting-read
11764 "Tag: "
11765 (or buffer-tags
11766 (with-current-buffer buf
11767 (org-get-buffer-tags)))))
11768 (quit (setq tg "")))
11769 (when (string-match "\\S-" tg)
11770 (add-to-list 'buffer-tags (list tg))
11771 (if (member tg current)
11772 (setq current (delete tg current))
11773 (push tg current)))
11774 (if exit-after-next (setq exit-after-next 'now)))
11775 ((setq e (rassoc c todo-table) tg (car e))
11776 (with-current-buffer buf
11777 (save-excursion (org-todo tg)))
11778 (if exit-after-next (setq exit-after-next 'now)))
11779 ((setq e (rassoc c ntable) tg (car e))
11780 (if (member tg current)
11781 (setq current (delete tg current))
11782 (loop for g in groups do
11783 (if (member tg g)
11784 (mapc (lambda (x)
11785 (setq current (delete x current)))
11786 g)))
11787 (push tg current))
11788 (if exit-after-next (setq exit-after-next 'now))))
11790 ;; Create a sorted list
11791 (setq current
11792 (sort current
11793 (lambda (a b)
11794 (assoc b (cdr (memq (assoc a ntable) ntable))))))
11795 (if (eq exit-after-next 'now) (throw 'exit t))
11796 (goto-char (point-min))
11797 (beginning-of-line 2)
11798 (delete-region (point) (point-at-eol))
11799 (org-fast-tag-insert "Current" current c-face)
11800 (org-set-current-tags-overlay current ov-prefix)
11801 (while (re-search-forward
11802 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
11803 (setq tg (match-string 1))
11804 (add-text-properties
11805 (match-beginning 1) (match-end 1)
11806 (list 'face
11807 (cond
11808 ((member tg current) c-face)
11809 ((member tg inherited) i-face)
11810 (t (get-text-property (match-beginning 1) 'face))))))
11811 (goto-char (point-min)))))
11812 (org-detach-overlay org-tags-overlay)
11813 (if rtn
11814 (mapconcat 'identity current ":")
11815 nil))))
11817 (defun org-get-tags-string ()
11818 "Get the TAGS string in the current headline."
11819 (unless (org-on-heading-p t)
11820 (error "Not on a heading"))
11821 (save-excursion
11822 (beginning-of-line 1)
11823 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
11824 (org-match-string-no-properties 1)
11825 "")))
11827 (defun org-get-tags ()
11828 "Get the list of tags specified in the current headline."
11829 (org-split-string (org-get-tags-string) ":"))
11831 (defun org-get-buffer-tags ()
11832 "Get a table of all tags used in the buffer, for completion."
11833 (let (tags)
11834 (save-excursion
11835 (goto-char (point-min))
11836 (while (re-search-forward
11837 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
11838 (when (equal (char-after (point-at-bol 0)) ?*)
11839 (mapc (lambda (x) (add-to-list 'tags x))
11840 (org-split-string (org-match-string-no-properties 1) ":")))))
11841 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
11842 (mapcar 'list tags)))
11844 ;;;; The mapping API
11846 ;;;###autoload
11847 (defun org-map-entries (func &optional match scope &rest skip)
11848 "Call FUNC at each headline selected by MATCH in SCOPE.
11850 FUNC is a function or a lisp form. The function will be called without
11851 arguments, with the cursor positioned at the beginning of the headline.
11852 The return values of all calls to the function will be collected and
11853 returned as a list.
11855 The call to FUNC will be wrapped into a save-excursion form, so FUNC
11856 does not need to preserve point. After evaluation, the cursor will be
11857 moved to the end of the line (presumably of the headline of the
11858 processed entry) and search continues from there. Under some
11859 circumstances, this may not produce the wanted results. For example,
11860 if you have removed (e.g. archived) the current (sub)tree it could
11861 mean that the next entry will be skipped entirely. In such cases, you
11862 can specify the position from where search should continue by making
11863 FUNC set the variable `org-map-continue-from' to the desired buffer
11864 position.
11866 MATCH is a tags/property/todo match as it is used in the agenda tags view.
11867 Only headlines that are matched by this query will be considered during
11868 the iteration. When MATCH is nil or t, all headlines will be
11869 visited by the iteration.
11871 SCOPE determines the scope of this command. It can be any of:
11873 nil The current buffer, respecting the restriction if any
11874 tree The subtree started with the entry at point
11875 file The current buffer, without restriction
11876 file-with-archives
11877 The current buffer, and any archives associated with it
11878 agenda All agenda files
11879 agenda-with-archives
11880 All agenda files with any archive files associated with them
11881 \(file1 file2 ...)
11882 If this is a list, all files in the list will be scanned
11884 The remaining args are treated as settings for the skipping facilities of
11885 the scanner. The following items can be given here:
11887 archive skip trees with the archive tag.
11888 comment skip trees with the COMMENT keyword
11889 function or Emacs Lisp form:
11890 will be used as value for `org-agenda-skip-function', so whenever
11891 the function returns t, FUNC will not be called for that
11892 entry and search will continue from the point where the
11893 function leaves it.
11895 If your function needs to retrieve the tags including inherited tags
11896 at the *current* entry, you can use the value of the variable
11897 `org-scanner-tags' which will be much faster than getting the value
11898 with `org-get-tags-at'. If your function gets properties with
11899 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
11900 to t around the call to `org-entry-properties' to get the same speedup.
11901 Note that if your function moves around to retrieve tags and properties at
11902 a *different* entry, you cannot use these techniques."
11903 (let* ((org-agenda-archives-mode nil) ; just to make sure
11904 (org-agenda-skip-archived-trees (memq 'archive skip))
11905 (org-agenda-skip-comment-trees (memq 'comment skip))
11906 (org-agenda-skip-function
11907 (car (org-delete-all '(comment archive) skip)))
11908 (org-tags-match-list-sublevels t)
11909 matcher file res
11910 org-todo-keywords-for-agenda
11911 org-done-keywords-for-agenda
11912 org-todo-keyword-alist-for-agenda
11913 org-drawers-for-agenda
11914 org-tag-alist-for-agenda)
11916 (cond
11917 ((eq match t) (setq matcher t))
11918 ((eq match nil) (setq matcher t))
11919 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
11921 (save-excursion
11922 (save-restriction
11923 (when (eq scope 'tree)
11924 (org-back-to-heading t)
11925 (org-narrow-to-subtree)
11926 (setq scope nil))
11928 (if (not scope)
11929 (progn
11930 (org-prepare-agenda-buffers
11931 (list (buffer-file-name (current-buffer))))
11932 (setq res (org-scan-tags func matcher)))
11933 ;; Get the right scope
11934 (cond
11935 ((and scope (listp scope) (symbolp (car scope)))
11936 (setq scope (eval scope)))
11937 ((eq scope 'agenda)
11938 (setq scope (org-agenda-files t)))
11939 ((eq scope 'agenda-with-archives)
11940 (setq scope (org-agenda-files t))
11941 (setq scope (org-add-archive-files scope)))
11942 ((eq scope 'file)
11943 (setq scope (list (buffer-file-name))))
11944 ((eq scope 'file-with-archives)
11945 (setq scope (org-add-archive-files (list (buffer-file-name))))))
11946 (org-prepare-agenda-buffers scope)
11947 (while (setq file (pop scope))
11948 (with-current-buffer (org-find-base-buffer-visiting file)
11949 (save-excursion
11950 (save-restriction
11951 (widen)
11952 (goto-char (point-min))
11953 (setq res (append res (org-scan-tags func matcher))))))))))
11954 res))
11956 ;;;; Properties
11958 ;;; Setting and retrieving properties
11960 (defconst org-special-properties
11961 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
11962 "TIMESTAMP" "TIMESTAMP_IA")
11963 "The special properties valid in Org-mode.
11965 These are properties that are not defined in the property drawer,
11966 but in some other way.")
11968 (defconst org-default-properties
11969 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
11970 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
11971 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
11972 "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
11973 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
11974 "CLOCK_MODELINE_TOTAL" "STYLE")
11975 "Some properties that are used by Org-mode for various purposes.
11976 Being in this list makes sure that they are offered for completion.")
11978 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
11979 "Regular expression matching the first line of a property drawer.")
11981 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
11982 "Regular expression matching the first line of a property drawer.")
11984 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
11985 "Regular expression matching the first line of a property drawer.")
11987 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
11988 "Regular expression matching the first line of a property drawer.")
11990 (defconst org-property-drawer-re
11991 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
11992 org-property-end-re "\\)\n?")
11993 "Matches an entire property drawer.")
11995 (defconst org-clock-drawer-re
11996 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
11997 org-property-end-re "\\)\n?")
11998 "Matches an entire clock drawer.")
12000 (defun org-property-action ()
12001 "Do an action on properties."
12002 (interactive)
12003 (let (c)
12004 (org-at-property-p)
12005 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
12006 (setq c (read-char-exclusive))
12007 (cond
12008 ((equal c ?s)
12009 (call-interactively 'org-set-property))
12010 ((equal c ?d)
12011 (call-interactively 'org-delete-property))
12012 ((equal c ?D)
12013 (call-interactively 'org-delete-property-globally))
12014 ((equal c ?c)
12015 (call-interactively 'org-compute-property-at-point))
12016 (t (error "No such property action %c" c)))))
12018 (defun org-set-effort (&optional value)
12019 "Set the effort property of the current entry.
12020 With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
12021 allowed value."
12022 (interactive "P")
12023 (if (equal value 0) (setq value 10))
12024 (let* ((completion-ignore-case t)
12025 (prop org-effort-property)
12026 (cur (org-entry-get nil prop))
12027 (allowed (org-property-get-allowed-values nil prop 'table))
12028 (existing (mapcar 'list (org-property-values prop)))
12029 (val (cond
12030 ((stringp value) value)
12031 ((and allowed (integerp value))
12032 (or (car (nth (1- value) allowed))
12033 (car (org-last allowed))))
12034 (allowed
12035 (message "Select 1-9,0, [RET%s]: %s"
12036 (if cur (concat "=" cur) "")
12037 (mapconcat 'car allowed " "))
12038 (setq rpl (read-char-exclusive))
12039 (if (equal rpl ?\r)
12040 (setq val cur)
12041 (setq rpl (- rpl ?0))
12042 (if (equal rpl 0) (setq rpl 10))
12043 (if (and (> rpl 0) (<= rpl (length allowed)))
12044 (car (nth (1- rpl) allowed))
12045 (org-completing-read "Value: " allowed nil))))
12047 (let (org-completion-use-ido org-completion-use-iswitchb)
12048 (org-completing-read
12049 (concat "Value " (if (and cur (string-match "\\S-" cur))
12050 (concat "[" cur "]") "")
12051 ": ")
12052 existing nil nil "" nil cur))))))
12053 (unless (equal (org-entry-get nil prop) val)
12054 (org-entry-put nil prop val))
12055 (message "%s is now %s" prop val)))
12057 (defun org-at-property-p ()
12058 "Is the cursor in a property line?"
12059 ;; FIXME: Does not check if we are actually in the drawer.
12060 ;; FIXME: also returns true on any drawers.....
12061 ;; This is used by C-c C-c for property action.
12062 (save-excursion
12063 (beginning-of-line 1)
12064 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
12066 (defun org-get-property-block (&optional beg end force)
12067 "Return the (beg . end) range of the body of the property drawer.
12068 BEG and END can be beginning and end of subtree, if not given
12069 they will be found.
12070 If the drawer does not exist and FORCE is non-nil, create the drawer."
12071 (catch 'exit
12072 (save-excursion
12073 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
12074 (end (or end (progn (outline-next-heading) (point)))))
12075 (goto-char beg)
12076 (if (re-search-forward org-property-start-re end t)
12077 (setq beg (1+ (match-end 0)))
12078 (if force
12079 (save-excursion
12080 (org-insert-property-drawer)
12081 (setq end (progn (outline-next-heading) (point))))
12082 (throw 'exit nil))
12083 (goto-char beg)
12084 (if (re-search-forward org-property-start-re end t)
12085 (setq beg (1+ (match-end 0)))))
12086 (if (re-search-forward org-property-end-re end t)
12087 (setq end (match-beginning 0))
12088 (or force (throw 'exit nil))
12089 (goto-char beg)
12090 (setq end beg)
12091 (org-indent-line-function)
12092 (insert ":END:\n"))
12093 (cons beg end)))))
12095 (defun org-entry-properties (&optional pom which)
12096 "Get all properties of the entry at point-or-marker POM.
12097 This includes the TODO keyword, the tags, time strings for deadline,
12098 scheduled, and clocking, and any additional properties defined in the
12099 entry. The return value is an alist, keys may occur multiple times
12100 if the property key was used several times.
12101 POM may also be nil, in which case the current entry is used.
12102 If WHICH is nil or `all', get all properties. If WHICH is
12103 `special' or `standard', only get that subclass."
12104 (setq which (or which 'all))
12105 (org-with-point-at pom
12106 (let ((clockstr (substring org-clock-string 0 -1))
12107 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
12108 beg end range props sum-props key value string clocksum)
12109 (save-excursion
12110 (when (condition-case nil
12111 (and (org-mode-p) (org-back-to-heading t))
12112 (error nil))
12113 (setq beg (point))
12114 (setq sum-props (get-text-property (point) 'org-summaries))
12115 (setq clocksum (get-text-property (point) :org-clock-minutes))
12116 (outline-next-heading)
12117 (setq end (point))
12118 (when (memq which '(all special))
12119 ;; Get the special properties, like TODO and tags
12120 (goto-char beg)
12121 (when (and (looking-at org-todo-line-regexp) (match-end 2))
12122 (push (cons "TODO" (org-match-string-no-properties 2)) props))
12123 (when (looking-at org-priority-regexp)
12124 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
12125 (when (and (setq value (org-get-tags-string))
12126 (string-match "\\S-" value))
12127 (push (cons "TAGS" value) props))
12128 (when (setq value (org-get-tags-at))
12129 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
12130 props))
12131 (while (re-search-forward org-maybe-keyword-time-regexp end t)
12132 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
12133 string (if (equal key clockstr)
12134 (org-no-properties
12135 (org-trim
12136 (buffer-substring
12137 (match-beginning 3) (goto-char (point-at-eol)))))
12138 (substring (org-match-string-no-properties 3) 1 -1)))
12139 (unless key
12140 (if (= (char-after (match-beginning 3)) ?\[)
12141 (setq key "TIMESTAMP_IA")
12142 (setq key "TIMESTAMP")))
12143 (when (or (equal key clockstr) (not (assoc key props)))
12144 (push (cons key string) props)))
12148 (when (memq which '(all standard))
12149 ;; Get the standard properties, like :PROP: ...
12150 (setq range (org-get-property-block beg end))
12151 (when range
12152 (goto-char (car range))
12153 (while (re-search-forward
12154 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
12155 (cdr range) t)
12156 (setq key (org-match-string-no-properties 1)
12157 value (org-trim (or (org-match-string-no-properties 2) "")))
12158 (unless (member key excluded)
12159 (push (cons key (or value "")) props)))))
12160 (if clocksum
12161 (push (cons "CLOCKSUM"
12162 (org-columns-number-to-string (/ (float clocksum) 60.)
12163 'add_times))
12164 props))
12165 (unless (assoc "CATEGORY" props)
12166 (setq value (or (org-get-category)
12167 (progn (org-refresh-category-properties)
12168 (org-get-category))))
12169 (push (cons "CATEGORY" value) props))
12170 (append sum-props (nreverse props)))))))
12172 (defun org-entry-get (pom property &optional inherit)
12173 "Get value of PROPERTY for entry at point-or-marker POM.
12174 If INHERIT is non-nil and the entry does not have the property,
12175 then also check higher levels of the hierarchy.
12176 If INHERIT is the symbol `selective', use inheritance only if the setting
12177 in `org-use-property-inheritance' selects PROPERTY for inheritance.
12178 If the property is present but empty, the return value is the empty string.
12179 If the property is not present at all, nil is returned."
12180 (org-with-point-at pom
12181 (if (and inherit (if (eq inherit 'selective)
12182 (org-property-inherit-p property)
12184 (org-entry-get-with-inheritance property)
12185 (if (member property org-special-properties)
12186 ;; We need a special property. Use brute force, get all properties.
12187 (cdr (assoc property (org-entry-properties nil 'special)))
12188 (let ((range (org-get-property-block)))
12189 (if (and range
12190 (goto-char (car range))
12191 (re-search-forward
12192 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
12193 (cdr range) t))
12194 ;; Found the property, return it.
12195 (if (match-end 1)
12196 (org-match-string-no-properties 1)
12197 "")))))))
12199 (defun org-property-or-variable-value (var &optional inherit)
12200 "Check if there is a property fixing the value of VAR.
12201 If yes, return this value. If not, return the current value of the variable."
12202 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
12203 (if (and prop (stringp prop) (string-match "\\S-" prop))
12204 (read prop)
12205 (symbol-value var))))
12207 (defun org-entry-delete (pom property)
12208 "Delete the property PROPERTY from entry at point-or-marker POM."
12209 (org-with-point-at pom
12210 (if (member property org-special-properties)
12211 nil ; cannot delete these properties.
12212 (let ((range (org-get-property-block)))
12213 (if (and range
12214 (goto-char (car range))
12215 (re-search-forward
12216 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
12217 (cdr range) t))
12218 (progn
12219 (delete-region (match-beginning 0) (1+ (point-at-eol)))
12221 nil)))))
12223 ;; Multi-values properties are properties that contain multiple values
12224 ;; These values are assumed to be single words, separated by whitespace.
12225 (defun org-entry-add-to-multivalued-property (pom property value)
12226 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
12227 (let* ((old (org-entry-get pom property))
12228 (values (and old (org-split-string old "[ \t]"))))
12229 (setq value (org-entry-protect-space value))
12230 (unless (member value values)
12231 (setq values (cons value values))
12232 (org-entry-put pom property
12233 (mapconcat 'identity values " ")))))
12235 (defun org-entry-remove-from-multivalued-property (pom property value)
12236 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
12237 (let* ((old (org-entry-get pom property))
12238 (values (and old (org-split-string old "[ \t]"))))
12239 (setq value (org-entry-protect-space value))
12240 (when (member value values)
12241 (setq values (delete value values))
12242 (org-entry-put pom property
12243 (mapconcat 'identity values " ")))))
12245 (defun org-entry-member-in-multivalued-property (pom property value)
12246 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
12247 (let* ((old (org-entry-get pom property))
12248 (values (and old (org-split-string old "[ \t]"))))
12249 (setq value (org-entry-protect-space value))
12250 (member value values)))
12252 (defun org-entry-get-multivalued-property (pom property)
12253 "Return a list of values in a multivalued property."
12254 (let* ((value (org-entry-get pom property))
12255 (values (and value (org-split-string value "[ \t]"))))
12256 (mapcar 'org-entry-restore-space values)))
12258 (defun org-entry-put-multivalued-property (pom property &rest values)
12259 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
12260 VALUES should be a list of strings. Spaces will be protected."
12261 (org-entry-put pom property
12262 (mapconcat 'org-entry-protect-space values " "))
12263 (let* ((value (org-entry-get pom property))
12264 (values (and value (org-split-string value "[ \t]"))))
12265 (mapcar 'org-entry-restore-space values)))
12267 (defun org-entry-protect-space (s)
12268 "Protect spaces and newline in string S."
12269 (while (string-match " " s)
12270 (setq s (replace-match "%20" t t s)))
12271 (while (string-match "\n" s)
12272 (setq s (replace-match "%0A" t t s)))
12275 (defun org-entry-restore-space (s)
12276 "Restore spaces and newline in string S."
12277 (while (string-match "%20" s)
12278 (setq s (replace-match " " t t s)))
12279 (while (string-match "%0A" s)
12280 (setq s (replace-match "\n" t t s)))
12283 (defvar org-entry-property-inherited-from (make-marker)
12284 "Marker pointing to the entry from where a property was inherited.
12285 Each call to `org-entry-get-with-inheritance' will set this marker to the
12286 location of the entry where the inheritance search matched. If there was
12287 no match, the marker will point nowhere.
12288 Note that also `org-entry-get' calls this function, if the INHERIT flag
12289 is set.")
12291 (defun org-entry-get-with-inheritance (property)
12292 "Get entry property, and search higher levels if not present."
12293 (move-marker org-entry-property-inherited-from nil)
12294 (let (tmp)
12295 (save-excursion
12296 (save-restriction
12297 (widen)
12298 (catch 'ex
12299 (while t
12300 (when (setq tmp (org-entry-get nil property))
12301 (org-back-to-heading t)
12302 (move-marker org-entry-property-inherited-from (point))
12303 (throw 'ex tmp))
12304 (or (org-up-heading-safe) (throw 'ex nil)))))
12305 (or tmp
12306 (cdr (assoc property org-file-properties))
12307 (cdr (assoc property org-global-properties))
12308 (cdr (assoc property org-global-properties-fixed))))))
12310 (defun org-entry-put (pom property value)
12311 "Set PROPERTY to VALUE for entry at point-or-marker POM."
12312 (org-with-point-at pom
12313 (org-back-to-heading t)
12314 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
12315 range)
12316 (cond
12317 ((equal property "TODO")
12318 (when (and (stringp value) (string-match "\\S-" value)
12319 (not (member value org-todo-keywords-1)))
12320 (error "\"%s\" is not a valid TODO state" value))
12321 (if (or (not value)
12322 (not (string-match "\\S-" value)))
12323 (setq value 'none))
12324 (org-todo value)
12325 (org-set-tags nil 'align))
12326 ((equal property "PRIORITY")
12327 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
12328 (string-to-char value) ?\ ))
12329 (org-set-tags nil 'align))
12330 ((equal property "SCHEDULED")
12331 (if (re-search-forward org-scheduled-time-regexp end t)
12332 (cond
12333 ((eq value 'earlier) (org-timestamp-change -1 'day))
12334 ((eq value 'later) (org-timestamp-change 1 'day))
12335 (t (call-interactively 'org-schedule)))
12336 (call-interactively 'org-schedule)))
12337 ((equal property "DEADLINE")
12338 (if (re-search-forward org-deadline-time-regexp end t)
12339 (cond
12340 ((eq value 'earlier) (org-timestamp-change -1 'day))
12341 ((eq value 'later) (org-timestamp-change 1 'day))
12342 (t (call-interactively 'org-deadline)))
12343 (call-interactively 'org-deadline)))
12344 ((member property org-special-properties)
12345 (error "The %s property can not yet be set with `org-entry-put'"
12346 property))
12347 (t ; a non-special property
12348 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
12349 (setq range (org-get-property-block beg end 'force))
12350 (goto-char (car range))
12351 (if (re-search-forward
12352 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
12353 (progn
12354 (delete-region (match-beginning 1) (match-end 1))
12355 (goto-char (match-beginning 1)))
12356 (goto-char (cdr range))
12357 (insert "\n")
12358 (backward-char 1)
12359 (org-indent-line-function)
12360 (insert ":" property ":"))
12361 (and value (insert " " value))
12362 (org-indent-line-function)))))))
12364 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
12365 "Get all property keys in the current buffer.
12366 With INCLUDE-SPECIALS, also list the special properties that reflect things
12367 like tags and TODO state.
12368 With INCLUDE-DEFAULTS, also include properties that has special meaning
12369 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
12370 With INCLUDE-COLUMNS, also include property names given in COLUMN
12371 formats in the current buffer."
12372 (let (rtn range cfmt s p)
12373 (save-excursion
12374 (save-restriction
12375 (widen)
12376 (goto-char (point-min))
12377 (while (re-search-forward org-property-start-re nil t)
12378 (setq range (org-get-property-block))
12379 (goto-char (car range))
12380 (while (re-search-forward
12381 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
12382 (cdr range) t)
12383 (add-to-list 'rtn (org-match-string-no-properties 1)))
12384 (outline-next-heading))))
12386 (when include-specials
12387 (setq rtn (append org-special-properties rtn)))
12389 (when include-defaults
12390 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
12391 (add-to-list 'rtn org-effort-property))
12393 (when include-columns
12394 (save-excursion
12395 (save-restriction
12396 (widen)
12397 (goto-char (point-min))
12398 (while (re-search-forward
12399 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
12400 nil t)
12401 (setq cfmt (match-string 2) s 0)
12402 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
12403 cfmt s)
12404 (setq s (match-end 0)
12405 p (match-string 1 cfmt))
12406 (unless (or (equal p "ITEM")
12407 (member p org-special-properties))
12408 (add-to-list 'rtn (match-string 1 cfmt))))))))
12410 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
12412 (defun org-property-values (key)
12413 "Return a list of all values of property KEY."
12414 (save-excursion
12415 (save-restriction
12416 (widen)
12417 (goto-char (point-min))
12418 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
12419 values)
12420 (while (re-search-forward re nil t)
12421 (add-to-list 'values (org-trim (match-string 1))))
12422 (delete "" values)))))
12424 (defun org-insert-property-drawer ()
12425 "Insert a property drawer into the current entry."
12426 (interactive)
12427 (org-back-to-heading t)
12428 (looking-at outline-regexp)
12429 (let ((indent (if org-adapt-indentation
12430 (- (match-end 0)(match-beginning 0))
12432 (beg (point))
12433 (re (concat "^[ \t]*" org-keyword-time-regexp))
12434 end hiddenp)
12435 (outline-next-heading)
12436 (setq end (point))
12437 (goto-char beg)
12438 (while (re-search-forward re end t))
12439 (setq hiddenp (org-invisible-p))
12440 (end-of-line 1)
12441 (and (equal (char-after) ?\n) (forward-char 1))
12442 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
12443 (if (member (match-string 1) '("CLOCK:" ":END:"))
12444 ;; just skip this line
12445 (beginning-of-line 2)
12446 ;; Drawer start, find the end
12447 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
12448 (beginning-of-line 1)))
12449 (org-skip-over-state-notes)
12450 (skip-chars-backward " \t\n\r")
12451 (if (eq (char-before) ?*) (forward-char 1))
12452 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
12453 (beginning-of-line 0)
12454 (org-indent-to-column indent)
12455 (beginning-of-line 2)
12456 (org-indent-to-column indent)
12457 (beginning-of-line 0)
12458 (if hiddenp
12459 (save-excursion
12460 (org-back-to-heading t)
12461 (hide-entry))
12462 (org-flag-drawer t))))
12464 (defun org-set-property (property value)
12465 "In the current entry, set PROPERTY to VALUE.
12466 When called interactively, this will prompt for a property name, offering
12467 completion on existing and default properties. And then it will prompt
12468 for a value, offering completion either on allowed values (via an inherited
12469 xxx_ALL property) or on existing values in other instances of this property
12470 in the current file."
12471 (interactive
12472 (let* ((completion-ignore-case t)
12473 (keys (org-buffer-property-keys nil t t))
12474 (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
12475 (prop (if (member prop0 keys)
12476 prop0
12477 (or (cdr (assoc (downcase prop0)
12478 (mapcar (lambda (x) (cons (downcase x) x))
12479 keys)))
12480 prop0)))
12481 (cur (org-entry-get nil prop))
12482 (allowed (org-property-get-allowed-values nil prop 'table))
12483 (existing (mapcar 'list (org-property-values prop)))
12484 (val (if allowed
12485 (org-completing-read "Value: " allowed nil 'req-match)
12486 (let (org-completion-use-ido org-completion-use-iswitchb)
12487 (org-completing-read
12488 (concat "Value " (if (and cur (string-match "\\S-" cur))
12489 (concat "[" cur "]") "")
12490 ": ")
12491 existing nil nil "" nil cur)))))
12492 (list prop (if (equal val "") cur val))))
12493 (unless (equal (org-entry-get nil property) value)
12494 (org-entry-put nil property value)))
12496 (defun org-delete-property (property)
12497 "In the current entry, delete PROPERTY."
12498 (interactive
12499 (let* ((completion-ignore-case t)
12500 (prop (org-icompleting-read
12501 "Property: " (org-entry-properties nil 'standard))))
12502 (list prop)))
12503 (message "Property %s %s" property
12504 (if (org-entry-delete nil property)
12505 "deleted"
12506 "was not present in the entry")))
12508 (defun org-delete-property-globally (property)
12509 "Remove PROPERTY globally, from all entries."
12510 (interactive
12511 (let* ((completion-ignore-case t)
12512 (prop (org-icompleting-read
12513 "Globally remove property: "
12514 (mapcar 'list (org-buffer-property-keys)))))
12515 (list prop)))
12516 (save-excursion
12517 (save-restriction
12518 (widen)
12519 (goto-char (point-min))
12520 (let ((cnt 0))
12521 (while (re-search-forward
12522 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
12523 nil t)
12524 (setq cnt (1+ cnt))
12525 (replace-match ""))
12526 (message "Property \"%s\" removed from %d entries" property cnt)))))
12528 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
12530 (defun org-compute-property-at-point ()
12531 "Compute the property at point.
12532 This looks for an enclosing column format, extracts the operator and
12533 then applies it to the property in the column format's scope."
12534 (interactive)
12535 (unless (org-at-property-p)
12536 (error "Not at a property"))
12537 (let ((prop (org-match-string-no-properties 2)))
12538 (org-columns-get-format-and-top-level)
12539 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
12540 (error "No operator defined for property %s" prop))
12541 (org-columns-compute prop)))
12543 (defun org-property-get-allowed-values (pom property &optional table)
12544 "Get allowed values for the property PROPERTY.
12545 When TABLE is non-nil, return an alist that can directly be used for
12546 completion."
12547 (let (vals)
12548 (cond
12549 ((equal property "TODO")
12550 (setq vals (org-with-point-at pom
12551 (append org-todo-keywords-1 '("")))))
12552 ((equal property "PRIORITY")
12553 (let ((n org-lowest-priority))
12554 (while (>= n org-highest-priority)
12555 (push (char-to-string n) vals)
12556 (setq n (1- n)))))
12557 ((member property org-special-properties))
12559 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
12561 (when (and vals (string-match "\\S-" vals))
12562 (setq vals (car (read-from-string (concat "(" vals ")"))))
12563 (setq vals (mapcar (lambda (x)
12564 (cond ((stringp x) x)
12565 ((numberp x) (number-to-string x))
12566 ((symbolp x) (symbol-name x))
12567 (t "???")))
12568 vals)))))
12569 (if table (mapcar 'list vals) vals)))
12571 (defun org-property-previous-allowed-value (&optional previous)
12572 "Switch to the next allowed value for this property."
12573 (interactive)
12574 (org-property-next-allowed-value t))
12576 (defun org-property-next-allowed-value (&optional previous)
12577 "Switch to the next allowed value for this property."
12578 (interactive)
12579 (unless (org-at-property-p)
12580 (error "Not at a property"))
12581 (let* ((key (match-string 2))
12582 (value (match-string 3))
12583 (allowed (or (org-property-get-allowed-values (point) key)
12584 (and (member value '("[ ]" "[-]" "[X]"))
12585 '("[ ]" "[X]"))))
12586 nval)
12587 (unless allowed
12588 (error "Allowed values for this property have not been defined"))
12589 (if previous (setq allowed (reverse allowed)))
12590 (if (member value allowed)
12591 (setq nval (car (cdr (member value allowed)))))
12592 (setq nval (or nval (car allowed)))
12593 (if (equal nval value)
12594 (error "Only one allowed value for this property"))
12595 (org-at-property-p)
12596 (replace-match (concat " :" key ": " nval) t t)
12597 (org-indent-line-function)
12598 (beginning-of-line 1)
12599 (skip-chars-forward " \t")))
12601 (defun org-find-entry-with-id (ident)
12602 "Locate the entry that contains the ID property with exact value IDENT.
12603 IDENT can be a string, a symbol or a number, this function will search for
12604 the string representation of it.
12605 Return the position where this entry starts, or nil if there is no such entry."
12606 (interactive "sID: ")
12607 (let ((id (cond
12608 ((stringp ident) ident)
12609 ((symbol-name ident) (symbol-name ident))
12610 ((numberp ident) (number-to-string ident))
12611 (t (error "IDENT %s must be a string, symbol or number" ident))))
12612 (case-fold-search nil))
12613 (save-excursion
12614 (save-restriction
12615 (widen)
12616 (goto-char (point-min))
12617 (when (re-search-forward
12618 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
12619 nil t)
12620 (org-back-to-heading t)
12621 (point))))))
12623 ;;;; Timestamps
12625 (defvar org-last-changed-timestamp nil)
12626 (defvar org-last-inserted-timestamp nil
12627 "The last time stamp inserted with `org-insert-time-stamp'.")
12628 (defvar org-time-was-given) ; dynamically scoped parameter
12629 (defvar org-end-time-was-given) ; dynamically scoped parameter
12630 (defvar org-ts-what) ; dynamically scoped parameter
12632 (defun org-time-stamp (arg &optional inactive)
12633 "Prompt for a date/time and insert a time stamp.
12634 If the user specifies a time like HH:MM, or if this command is called
12635 with a prefix argument, the time stamp will contain date and time.
12636 Otherwise, only the date will be included. All parts of a date not
12637 specified by the user will be filled in from the current date/time.
12638 So if you press just return without typing anything, the time stamp
12639 will represent the current date/time. If there is already a timestamp
12640 at the cursor, it will be modified."
12641 (interactive "P")
12642 (let* ((ts nil)
12643 (default-time
12644 ;; Default time is either today, or, when entering a range,
12645 ;; the range start.
12646 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
12647 (save-excursion
12648 (re-search-backward
12649 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
12650 (- (point) 20) t)))
12651 (apply 'encode-time (org-parse-time-string (match-string 1)))
12652 (current-time)))
12653 (default-input (and ts (org-get-compact-tod ts)))
12654 org-time-was-given org-end-time-was-given time)
12655 (cond
12656 ((and (org-at-timestamp-p t)
12657 (memq last-command '(org-time-stamp org-time-stamp-inactive))
12658 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
12659 (insert "--")
12660 (setq time (let ((this-command this-command))
12661 (org-read-date arg 'totime nil nil
12662 default-time default-input)))
12663 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
12664 ((org-at-timestamp-p t)
12665 (setq time (let ((this-command this-command))
12666 (org-read-date arg 'totime nil nil default-time default-input)))
12667 (when (org-at-timestamp-p t) ; just to get the match data
12668 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
12669 (replace-match "")
12670 (setq org-last-changed-timestamp
12671 (org-insert-time-stamp
12672 time (or org-time-was-given arg)
12673 inactive nil nil (list org-end-time-was-given))))
12674 (message "Timestamp updated"))
12676 (setq time (let ((this-command this-command))
12677 (org-read-date arg 'totime nil nil default-time default-input)))
12678 (org-insert-time-stamp time (or org-time-was-given arg) inactive
12679 nil nil (list org-end-time-was-given))))))
12681 ;; FIXME: can we use this for something else, like computing time differences?
12682 (defun org-get-compact-tod (s)
12683 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
12684 (let* ((t1 (match-string 1 s))
12685 (h1 (string-to-number (match-string 2 s)))
12686 (m1 (string-to-number (match-string 3 s)))
12687 (t2 (and (match-end 4) (match-string 5 s)))
12688 (h2 (and t2 (string-to-number (match-string 6 s))))
12689 (m2 (and t2 (string-to-number (match-string 7 s))))
12690 dh dm)
12691 (if (not t2)
12693 (setq dh (- h2 h1) dm (- m2 m1))
12694 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
12695 (concat t1 "+" (number-to-string dh)
12696 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
12698 (defun org-time-stamp-inactive (&optional arg)
12699 "Insert an inactive time stamp.
12700 An inactive time stamp is enclosed in square brackets instead of angle
12701 brackets. It is inactive in the sense that it does not trigger agenda entries,
12702 does not link to the calendar and cannot be changed with the S-cursor keys.
12703 So these are more for recording a certain time/date."
12704 (interactive "P")
12705 (org-time-stamp arg 'inactive))
12707 (defvar org-date-ovl (org-make-overlay 1 1))
12708 (org-overlay-put org-date-ovl 'face 'org-warning)
12709 (org-detach-overlay org-date-ovl)
12711 (defvar org-ans1) ; dynamically scoped parameter
12712 (defvar org-ans2) ; dynamically scoped parameter
12714 (defvar org-plain-time-of-day-regexp) ; defined below
12716 (defvar org-overriding-default-time nil) ; dynamically scoped
12717 (defvar org-read-date-overlay nil)
12718 (defvar org-dcst nil) ; dynamically scoped
12719 (defvar org-read-date-history nil)
12720 (defvar org-read-date-final-answer nil)
12722 (defun org-read-date (&optional with-time to-time from-string prompt
12723 default-time default-input)
12724 "Read a date, possibly a time, and make things smooth for the user.
12725 The prompt will suggest to enter an ISO date, but you can also enter anything
12726 which will at least partially be understood by `parse-time-string'.
12727 Unrecognized parts of the date will default to the current day, month, year,
12728 hour and minute. If this command is called to replace a timestamp at point,
12729 of to enter the second timestamp of a range, the default time is taken from the
12730 existing stamp. For example,
12731 3-2-5 --> 2003-02-05
12732 feb 15 --> currentyear-02-15
12733 sep 12 9 --> 2009-09-12
12734 12:45 --> today 12:45
12735 22 sept 0:34 --> currentyear-09-22 0:34
12736 12 --> currentyear-currentmonth-12
12737 Fri --> nearest Friday (today or later)
12738 etc.
12740 Furthermore you can specify a relative date by giving, as the *first* thing
12741 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
12742 change in days weeks, months, years.
12743 With a single plus or minus, the date is relative to today. With a double
12744 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
12745 +4d --> four days from today
12746 +4 --> same as above
12747 +2w --> two weeks from today
12748 ++5 --> five days from default date
12750 The function understands only English month and weekday abbreviations,
12751 but this can be configured with the variables `parse-time-months' and
12752 `parse-time-weekdays'.
12754 While prompting, a calendar is popped up - you can also select the
12755 date with the mouse (button 1). The calendar shows a period of three
12756 months. To scroll it to other months, use the keys `>' and `<'.
12757 If you don't like the calendar, turn it off with
12758 \(setq org-read-date-popup-calendar nil)
12760 With optional argument TO-TIME, the date will immediately be converted
12761 to an internal time.
12762 With an optional argument WITH-TIME, the prompt will suggest to also
12763 insert a time. Note that when WITH-TIME is not set, you can still
12764 enter a time, and this function will inform the calling routine about
12765 this change. The calling routine may then choose to change the format
12766 used to insert the time stamp into the buffer to include the time.
12767 With optional argument FROM-STRING, read from this string instead from
12768 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
12769 the time/date that is used for everything that is not specified by the
12770 user."
12771 (require 'parse-time)
12772 (let* ((org-time-stamp-rounding-minutes
12773 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
12774 (org-dcst org-display-custom-times)
12775 (ct (org-current-time))
12776 (def (or org-overriding-default-time default-time ct))
12777 (defdecode (decode-time def))
12778 (dummy (progn
12779 (when (< (nth 2 defdecode) org-extend-today-until)
12780 (setcar (nthcdr 2 defdecode) -1)
12781 (setcar (nthcdr 1 defdecode) 59)
12782 (setq def (apply 'encode-time defdecode)
12783 defdecode (decode-time def)))))
12784 (calendar-frame-setup nil)
12785 (calendar-move-hook nil)
12786 (calendar-view-diary-initially-flag nil)
12787 (view-diary-entries-initially nil)
12788 (calendar-view-holidays-initially-flag nil)
12789 (view-calendar-holidays-initially nil)
12790 (timestr (format-time-string
12791 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
12792 (prompt (concat (if prompt (concat prompt " ") "")
12793 (format "Date+time [%s]: " timestr)))
12794 ans (org-ans0 "") org-ans1 org-ans2 final)
12796 (cond
12797 (from-string (setq ans from-string))
12798 (org-read-date-popup-calendar
12799 (save-excursion
12800 (save-window-excursion
12801 (calendar)
12802 (calendar-forward-day (- (time-to-days def)
12803 (calendar-absolute-from-gregorian
12804 (calendar-current-date))))
12805 (org-eval-in-calendar nil t)
12806 (let* ((old-map (current-local-map))
12807 (map (copy-keymap calendar-mode-map))
12808 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
12809 (org-defkey map (kbd "RET") 'org-calendar-select)
12810 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
12811 'org-calendar-select-mouse)
12812 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
12813 'org-calendar-select-mouse)
12814 (org-defkey minibuffer-local-map [(meta shift left)]
12815 (lambda () (interactive)
12816 (org-eval-in-calendar '(calendar-backward-month 1))))
12817 (org-defkey minibuffer-local-map [(meta shift right)]
12818 (lambda () (interactive)
12819 (org-eval-in-calendar '(calendar-forward-month 1))))
12820 (org-defkey minibuffer-local-map [(meta shift up)]
12821 (lambda () (interactive)
12822 (org-eval-in-calendar '(calendar-backward-year 1))))
12823 (org-defkey minibuffer-local-map [(meta shift down)]
12824 (lambda () (interactive)
12825 (org-eval-in-calendar '(calendar-forward-year 1))))
12826 (org-defkey minibuffer-local-map [?\e (shift left)]
12827 (lambda () (interactive)
12828 (org-eval-in-calendar '(calendar-backward-month 1))))
12829 (org-defkey minibuffer-local-map [?\e (shift right)]
12830 (lambda () (interactive)
12831 (org-eval-in-calendar '(calendar-forward-month 1))))
12832 (org-defkey minibuffer-local-map [?\e (shift up)]
12833 (lambda () (interactive)
12834 (org-eval-in-calendar '(calendar-backward-year 1))))
12835 (org-defkey minibuffer-local-map [?\e (shift down)]
12836 (lambda () (interactive)
12837 (org-eval-in-calendar '(calendar-forward-year 1))))
12838 (org-defkey minibuffer-local-map [(shift up)]
12839 (lambda () (interactive)
12840 (org-eval-in-calendar '(calendar-backward-week 1))))
12841 (org-defkey minibuffer-local-map [(shift down)]
12842 (lambda () (interactive)
12843 (org-eval-in-calendar '(calendar-forward-week 1))))
12844 (org-defkey minibuffer-local-map [(shift left)]
12845 (lambda () (interactive)
12846 (org-eval-in-calendar '(calendar-backward-day 1))))
12847 (org-defkey minibuffer-local-map [(shift right)]
12848 (lambda () (interactive)
12849 (org-eval-in-calendar '(calendar-forward-day 1))))
12850 (org-defkey minibuffer-local-map ">"
12851 (lambda () (interactive)
12852 (org-eval-in-calendar '(scroll-calendar-left 1))))
12853 (org-defkey minibuffer-local-map "<"
12854 (lambda () (interactive)
12855 (org-eval-in-calendar '(scroll-calendar-right 1))))
12856 (run-hooks 'org-read-date-minibuffer-setup-hook)
12857 (unwind-protect
12858 (progn
12859 (use-local-map map)
12860 (add-hook 'post-command-hook 'org-read-date-display)
12861 (setq org-ans0 (read-string prompt default-input
12862 'org-read-date-history nil))
12863 ;; org-ans0: from prompt
12864 ;; org-ans1: from mouse click
12865 ;; org-ans2: from calendar motion
12866 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
12867 (remove-hook 'post-command-hook 'org-read-date-display)
12868 (use-local-map old-map)
12869 (when org-read-date-overlay
12870 (org-delete-overlay org-read-date-overlay)
12871 (setq org-read-date-overlay nil)))))))
12873 (t ; Naked prompt only
12874 (unwind-protect
12875 (setq ans (read-string prompt default-input
12876 'org-read-date-history timestr))
12877 (when org-read-date-overlay
12878 (org-delete-overlay org-read-date-overlay)
12879 (setq org-read-date-overlay nil)))))
12881 (setq final (org-read-date-analyze ans def defdecode))
12882 (setq org-read-date-final-answer ans)
12884 (if to-time
12885 (apply 'encode-time final)
12886 (if (and (boundp 'org-time-was-given) org-time-was-given)
12887 (format "%04d-%02d-%02d %02d:%02d"
12888 (nth 5 final) (nth 4 final) (nth 3 final)
12889 (nth 2 final) (nth 1 final))
12890 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
12892 (defvar def)
12893 (defvar defdecode)
12894 (defvar with-time)
12895 (defvar org-read-date-analyze-futurep nil)
12896 (defun org-read-date-display ()
12897 "Display the current date prompt interpretation in the minibuffer."
12898 (when org-read-date-display-live
12899 (when org-read-date-overlay
12900 (org-delete-overlay org-read-date-overlay))
12901 (let ((p (point)))
12902 (end-of-line 1)
12903 (while (not (equal (buffer-substring
12904 (max (point-min) (- (point) 4)) (point))
12905 " "))
12906 (insert " "))
12907 (goto-char p))
12908 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
12909 " " (or org-ans1 org-ans2)))
12910 (org-end-time-was-given nil)
12911 (f (org-read-date-analyze ans def defdecode))
12912 (fmts (if org-dcst
12913 org-time-stamp-custom-formats
12914 org-time-stamp-formats))
12915 (fmt (if (or with-time
12916 (and (boundp 'org-time-was-given) org-time-was-given))
12917 (cdr fmts)
12918 (car fmts)))
12919 (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
12920 (when (and org-end-time-was-given
12921 (string-match org-plain-time-of-day-regexp txt))
12922 (setq txt (concat (substring txt 0 (match-end 0)) "-"
12923 org-end-time-was-given
12924 (substring txt (match-end 0)))))
12925 (when org-read-date-analyze-futurep
12926 (setq txt (concat txt " (=>F)")))
12927 (setq org-read-date-overlay
12928 (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
12929 (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
12931 (defun org-read-date-analyze (ans def defdecode)
12932 "Analyse the combined answer of the date prompt."
12933 ;; FIXME: cleanup and comment
12934 (let (delta deltan deltaw deltadef year month day
12935 hour minute second wday pm h2 m2 tl wday1
12936 iso-year iso-weekday iso-week iso-year iso-date futurep)
12937 (setq org-read-date-analyze-futurep nil)
12938 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
12939 (setq ans "+0"))
12941 (when (setq delta (org-read-date-get-relative ans (current-time) def))
12942 (setq ans (replace-match "" t t ans)
12943 deltan (car delta)
12944 deltaw (nth 1 delta)
12945 deltadef (nth 2 delta)))
12947 ;; Check if there is an iso week date in there
12948 ;; If yes, sore the info and postpone interpreting it until the rest
12949 ;; of the parsing is done
12950 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
12951 (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
12952 iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
12953 iso-week (string-to-number (match-string 2 ans)))
12954 (setq ans (replace-match "" t t ans)))
12956 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
12957 (when (string-match
12958 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
12959 (setq year (if (match-end 2)
12960 (string-to-number (match-string 2 ans))
12961 (string-to-number (format-time-string "%Y")))
12962 month (string-to-number (match-string 3 ans))
12963 day (string-to-number (match-string 4 ans)))
12964 (if (< year 100) (setq year (+ 2000 year)))
12965 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
12966 t nil ans)))
12967 ;; Help matching am/pm times, because `parse-time-string' does not do that.
12968 ;; If there is a time with am/pm, and *no* time without it, we convert
12969 ;; so that matching will be successful.
12970 (loop for i from 1 to 2 do ; twice, for end time as well
12971 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
12972 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
12973 (setq hour (string-to-number (match-string 1 ans))
12974 minute (if (match-end 3)
12975 (string-to-number (match-string 3 ans))
12977 pm (equal ?p
12978 (string-to-char (downcase (match-string 4 ans)))))
12979 (if (and (= hour 12) (not pm))
12980 (setq hour 0)
12981 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
12982 (setq ans (replace-match (format "%02d:%02d" hour minute)
12983 t t ans))))
12985 ;; Check if a time range is given as a duration
12986 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
12987 (setq hour (string-to-number (match-string 1 ans))
12988 h2 (+ hour (string-to-number (match-string 3 ans)))
12989 minute (string-to-number (match-string 2 ans))
12990 m2 (+ minute (if (match-end 5) (string-to-number
12991 (match-string 5 ans))0)))
12992 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
12993 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
12994 t t ans)))
12996 ;; Check if there is a time range
12997 (when (boundp 'org-end-time-was-given)
12998 (setq org-time-was-given nil)
12999 (when (and (string-match org-plain-time-of-day-regexp ans)
13000 (match-end 8))
13001 (setq org-end-time-was-given (match-string 8 ans))
13002 (setq ans (concat (substring ans 0 (match-beginning 7))
13003 (substring ans (match-end 7))))))
13005 (setq tl (parse-time-string ans)
13006 day (or (nth 3 tl) (nth 3 defdecode))
13007 month (or (nth 4 tl)
13008 (if (and org-read-date-prefer-future
13009 (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
13010 (prog1 (1+ (nth 4 defdecode)) (setq futurep t))
13011 (nth 4 defdecode)))
13012 year (or (nth 5 tl)
13013 (if (and org-read-date-prefer-future
13014 (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
13015 (prog1 (1+ (nth 5 defdecode)) (setq futurep t))
13016 (nth 5 defdecode)))
13017 hour (or (nth 2 tl) (nth 2 defdecode))
13018 minute (or (nth 1 tl) (nth 1 defdecode))
13019 second (or (nth 0 tl) 0)
13020 wday (nth 6 tl))
13022 (when (and (eq org-read-date-prefer-future 'time)
13023 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
13024 (equal day (nth 3 defdecode))
13025 (equal month (nth 4 defdecode))
13026 (equal year (nth 5 defdecode))
13027 (nth 2 tl)
13028 (or (< (nth 2 tl) (nth 2 defdecode))
13029 (and (= (nth 2 tl) (nth 2 defdecode))
13030 (nth 1 tl)
13031 (< (nth 1 tl) (nth 1 defdecode)))))
13032 (setq day (1+ day)
13033 futurep t))
13035 ;; Special date definitions below
13036 (cond
13037 (iso-week
13038 ;; There was an iso week
13039 (setq futurep nil)
13040 (setq year (or iso-year year)
13041 day (or iso-weekday wday 1)
13042 wday nil ; to make sure that the trigger below does not match
13043 iso-date (calendar-gregorian-from-absolute
13044 (calendar-absolute-from-iso
13045 (list iso-week day year))))
13046 ; FIXME: Should we also push ISO weeks into the future?
13047 ; (when (and org-read-date-prefer-future
13048 ; (not iso-year)
13049 ; (< (calendar-absolute-from-gregorian iso-date)
13050 ; (time-to-days (current-time))))
13051 ; (setq year (1+ year)
13052 ; iso-date (calendar-gregorian-from-absolute
13053 ; (calendar-absolute-from-iso
13054 ; (list iso-week day year)))))
13055 (setq month (car iso-date)
13056 year (nth 2 iso-date)
13057 day (nth 1 iso-date)))
13058 (deltan
13059 (setq futurep nil)
13060 (unless deltadef
13061 (let ((now (decode-time (current-time))))
13062 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
13063 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
13064 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
13065 ((equal deltaw "m") (setq month (+ month deltan)))
13066 ((equal deltaw "y") (setq year (+ year deltan)))))
13067 ((and wday (not (nth 3 tl)))
13068 (setq futurep nil)
13069 ;; Weekday was given, but no day, so pick that day in the week
13070 ;; on or after the derived date.
13071 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
13072 (unless (equal wday wday1)
13073 (setq day (+ day (% (- wday wday1 -7) 7))))))
13074 (if (and (boundp 'org-time-was-given)
13075 (nth 2 tl))
13076 (setq org-time-was-given t))
13077 (if (< year 100) (setq year (+ 2000 year)))
13078 (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
13079 (setq org-read-date-analyze-futurep futurep)
13080 (list second minute hour day month year)))
13082 (defvar parse-time-weekdays)
13084 (defun org-read-date-get-relative (s today default)
13085 "Check string S for special relative date string.
13086 TODAY and DEFAULT are internal times, for today and for a default.
13087 Return shift list (N what def-flag)
13088 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
13089 N is the number of WHATs to shift.
13090 DEF-FLAG is t when a double ++ or -- indicates shift relative to
13091 the DEFAULT date rather than TODAY."
13092 (when (and
13093 (string-match
13094 (concat
13095 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
13096 "\\([0-9]+\\)?"
13097 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
13098 "\\([ \t]\\|$\\)") s)
13099 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
13100 (let* ((dir (if (> (match-end 1) (match-beginning 1))
13101 (string-to-char (substring (match-string 1 s) -1))
13102 ?+))
13103 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
13104 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
13105 (what (if (match-end 3) (match-string 3 s) "d"))
13106 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
13107 (date (if rel default today))
13108 (wday (nth 6 (decode-time date)))
13109 delta)
13110 (if wday1
13111 (progn
13112 (setq delta (mod (+ 7 (- wday1 wday)) 7))
13113 (if (= dir ?-) (setq delta (- delta 7)))
13114 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
13115 (list delta "d" rel))
13116 (list (* n (if (= dir ?-) -1 1)) what rel)))))
13118 (defun org-eval-in-calendar (form &optional keepdate)
13119 "Eval FORM in the calendar window and return to current window.
13120 Also, store the cursor date in variable org-ans2."
13121 (let ((sf (selected-frame))
13122 (sw (selected-window)))
13123 (select-window (get-buffer-window "*Calendar*" t))
13124 (eval form)
13125 (when (and (not keepdate) (calendar-cursor-to-date))
13126 (let* ((date (calendar-cursor-to-date))
13127 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13128 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
13129 (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
13130 (select-window sw)
13131 (org-select-frame-set-input-focus sf)))
13133 (defun org-calendar-select ()
13134 "Return to `org-read-date' with the date currently selected.
13135 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
13136 (interactive)
13137 (when (calendar-cursor-to-date)
13138 (let* ((date (calendar-cursor-to-date))
13139 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13140 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
13141 (if (active-minibuffer-window) (exit-minibuffer))))
13143 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
13144 "Insert a date stamp for the date given by the internal TIME.
13145 WITH-HM means, use the stamp format that includes the time of the day.
13146 INACTIVE means use square brackets instead of angular ones, so that the
13147 stamp will not contribute to the agenda.
13148 PRE and POST are optional strings to be inserted before and after the
13149 stamp.
13150 The command returns the inserted time stamp."
13151 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
13152 stamp)
13153 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
13154 (insert-before-markers (or pre ""))
13155 (insert-before-markers (setq stamp (format-time-string fmt time)))
13156 (when (listp extra)
13157 (setq extra (car extra))
13158 (if (and (stringp extra)
13159 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
13160 (setq extra (format "-%02d:%02d"
13161 (string-to-number (match-string 1 extra))
13162 (string-to-number (match-string 2 extra))))
13163 (setq extra nil)))
13164 (when extra
13165 (backward-char 1)
13166 (insert-before-markers extra)
13167 (forward-char 1))
13168 (insert-before-markers (or post ""))
13169 (setq org-last-inserted-timestamp stamp)))
13171 (defun org-toggle-time-stamp-overlays ()
13172 "Toggle the use of custom time stamp formats."
13173 (interactive)
13174 (setq org-display-custom-times (not org-display-custom-times))
13175 (unless org-display-custom-times
13176 (let ((p (point-min)) (bmp (buffer-modified-p)))
13177 (while (setq p (next-single-property-change p 'display))
13178 (if (and (get-text-property p 'display)
13179 (eq (get-text-property p 'face) 'org-date))
13180 (remove-text-properties
13181 p (setq p (next-single-property-change p 'display))
13182 '(display t))))
13183 (set-buffer-modified-p bmp)))
13184 (if (featurep 'xemacs)
13185 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
13186 (org-restart-font-lock)
13187 (setq org-table-may-need-update t)
13188 (if org-display-custom-times
13189 (message "Time stamps are overlayed with custom format")
13190 (message "Time stamp overlays removed")))
13192 (defun org-display-custom-time (beg end)
13193 "Overlay modified time stamp format over timestamp between BEG and END."
13194 (let* ((ts (buffer-substring beg end))
13195 t1 w1 with-hm tf time str w2 (off 0))
13196 (save-match-data
13197 (setq t1 (org-parse-time-string ts t))
13198 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
13199 (setq off (- (match-end 0) (match-beginning 0)))))
13200 (setq end (- end off))
13201 (setq w1 (- end beg)
13202 with-hm (and (nth 1 t1) (nth 2 t1))
13203 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
13204 time (org-fix-decoded-time t1)
13205 str (org-add-props
13206 (format-time-string
13207 (substring tf 1 -1) (apply 'encode-time time))
13208 nil 'mouse-face 'highlight)
13209 w2 (length str))
13210 (if (not (= w2 w1))
13211 (add-text-properties (1+ beg) (+ 2 beg)
13212 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
13213 (if (featurep 'xemacs)
13214 (progn
13215 (put-text-property beg end 'invisible t)
13216 (put-text-property beg end 'end-glyph (make-glyph str)))
13217 (put-text-property beg end 'display str))))
13219 (defun org-translate-time (string)
13220 "Translate all timestamps in STRING to custom format.
13221 But do this only if the variable `org-display-custom-times' is set."
13222 (when org-display-custom-times
13223 (save-match-data
13224 (let* ((start 0)
13225 (re org-ts-regexp-both)
13226 t1 with-hm inactive tf time str beg end)
13227 (while (setq start (string-match re string start))
13228 (setq beg (match-beginning 0)
13229 end (match-end 0)
13230 t1 (save-match-data
13231 (org-parse-time-string (substring string beg end) t))
13232 with-hm (and (nth 1 t1) (nth 2 t1))
13233 inactive (equal (substring string beg (1+ beg)) "[")
13234 tf (funcall (if with-hm 'cdr 'car)
13235 org-time-stamp-custom-formats)
13236 time (org-fix-decoded-time t1)
13237 str (format-time-string
13238 (concat
13239 (if inactive "[" "<") (substring tf 1 -1)
13240 (if inactive "]" ">"))
13241 (apply 'encode-time time))
13242 string (replace-match str t t string)
13243 start (+ start (length str)))))))
13244 string)
13246 (defun org-fix-decoded-time (time)
13247 "Set 0 instead of nil for the first 6 elements of time.
13248 Don't touch the rest."
13249 (let ((n 0))
13250 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
13252 (defun org-days-to-time (timestamp-string)
13253 "Difference between TIMESTAMP-STRING and now in days."
13254 (- (time-to-days (org-time-string-to-time timestamp-string))
13255 (time-to-days (current-time))))
13257 (defun org-deadline-close (timestamp-string &optional ndays)
13258 "Is the time in TIMESTAMP-STRING close to the current date?"
13259 (setq ndays (or ndays (org-get-wdays timestamp-string)))
13260 (and (< (org-days-to-time timestamp-string) ndays)
13261 (not (org-entry-is-done-p))))
13263 (defun org-get-wdays (ts)
13264 "Get the deadline lead time appropriate for timestring TS."
13265 (cond
13266 ((<= org-deadline-warning-days 0)
13267 ;; 0 or negative, enforce this value no matter what
13268 (- org-deadline-warning-days))
13269 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
13270 ;; lead time is specified.
13271 (floor (* (string-to-number (match-string 1 ts))
13272 (cdr (assoc (match-string 2 ts)
13273 '(("d" . 1) ("w" . 7)
13274 ("m" . 30.4) ("y" . 365.25)))))))
13275 ;; go for the default.
13276 (t org-deadline-warning-days)))
13278 (defun org-calendar-select-mouse (ev)
13279 "Return to `org-read-date' with the date currently selected.
13280 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
13281 (interactive "e")
13282 (mouse-set-point ev)
13283 (when (calendar-cursor-to-date)
13284 (let* ((date (calendar-cursor-to-date))
13285 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13286 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
13287 (if (active-minibuffer-window) (exit-minibuffer))))
13289 (defun org-check-deadlines (ndays)
13290 "Check if there are any deadlines due or past due.
13291 A deadline is considered due if it happens within `org-deadline-warning-days'
13292 days from today's date. If the deadline appears in an entry marked DONE,
13293 it is not shown. The prefix arg NDAYS can be used to test that many
13294 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
13295 (interactive "P")
13296 (let* ((org-warn-days
13297 (cond
13298 ((equal ndays '(4)) 100000)
13299 (ndays (prefix-numeric-value ndays))
13300 (t (abs org-deadline-warning-days))))
13301 (case-fold-search nil)
13302 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
13303 (callback
13304 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
13306 (message "%d deadlines past-due or due within %d days"
13307 (org-occur regexp nil callback)
13308 org-warn-days)))
13310 (defun org-check-before-date (date)
13311 "Check if there are deadlines or scheduled entries before DATE."
13312 (interactive (list (org-read-date)))
13313 (let ((case-fold-search nil)
13314 (regexp (concat "\\<\\(" org-deadline-string
13315 "\\|" org-scheduled-string
13316 "\\) *<\\([^>]+\\)>"))
13317 (callback
13318 (lambda () (time-less-p
13319 (org-time-string-to-time (match-string 2))
13320 (org-time-string-to-time date)))))
13321 (message "%d entries before %s"
13322 (org-occur regexp nil callback) date)))
13324 (defun org-check-after-date (date)
13325 "Check if there are deadlines or scheduled entries after DATE."
13326 (interactive (list (org-read-date)))
13327 (let ((case-fold-search nil)
13328 (regexp (concat "\\<\\(" org-deadline-string
13329 "\\|" org-scheduled-string
13330 "\\) *<\\([^>]+\\)>"))
13331 (callback
13332 (lambda () (not
13333 (time-less-p
13334 (org-time-string-to-time (match-string 2))
13335 (org-time-string-to-time date))))))
13336 (message "%d entries after %s"
13337 (org-occur regexp nil callback) date)))
13339 (defun org-evaluate-time-range (&optional to-buffer)
13340 "Evaluate a time range by computing the difference between start and end.
13341 Normally the result is just printed in the echo area, but with prefix arg
13342 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
13343 If the time range is actually in a table, the result is inserted into the
13344 next column.
13345 For time difference computation, a year is assumed to be exactly 365
13346 days in order to avoid rounding problems."
13347 (interactive "P")
13349 (org-clock-update-time-maybe)
13350 (save-excursion
13351 (unless (org-at-date-range-p t)
13352 (goto-char (point-at-bol))
13353 (re-search-forward org-tr-regexp-both (point-at-eol) t))
13354 (if (not (org-at-date-range-p t))
13355 (error "Not at a time-stamp range, and none found in current line")))
13356 (let* ((ts1 (match-string 1))
13357 (ts2 (match-string 2))
13358 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
13359 (match-end (match-end 0))
13360 (time1 (org-time-string-to-time ts1))
13361 (time2 (org-time-string-to-time ts2))
13362 (t1 (org-float-time time1))
13363 (t2 (org-float-time time2))
13364 (diff (abs (- t2 t1)))
13365 (negative (< (- t2 t1) 0))
13366 ;; (ys (floor (* 365 24 60 60)))
13367 (ds (* 24 60 60))
13368 (hs (* 60 60))
13369 (fy "%dy %dd %02d:%02d")
13370 (fy1 "%dy %dd")
13371 (fd "%dd %02d:%02d")
13372 (fd1 "%dd")
13373 (fh "%02d:%02d")
13374 y d h m align)
13375 (if havetime
13376 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
13378 d (floor (/ diff ds)) diff (mod diff ds)
13379 h (floor (/ diff hs)) diff (mod diff hs)
13380 m (floor (/ diff 60)))
13381 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
13383 d (floor (+ (/ diff ds) 0.5))
13384 h 0 m 0))
13385 (if (not to-buffer)
13386 (message "%s" (org-make-tdiff-string y d h m))
13387 (if (org-at-table-p)
13388 (progn
13389 (goto-char match-end)
13390 (setq align t)
13391 (and (looking-at " *|") (goto-char (match-end 0))))
13392 (goto-char match-end))
13393 (if (looking-at
13394 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
13395 (replace-match ""))
13396 (if negative (insert " -"))
13397 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
13398 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
13399 (insert " " (format fh h m))))
13400 (if align (org-table-align))
13401 (message "Time difference inserted")))))
13403 (defun org-make-tdiff-string (y d h m)
13404 (let ((fmt "")
13405 (l nil))
13406 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
13407 l (push y l)))
13408 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
13409 l (push d l)))
13410 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
13411 l (push h l)))
13412 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
13413 l (push m l)))
13414 (apply 'format fmt (nreverse l))))
13416 (defun org-time-string-to-time (s)
13417 (apply 'encode-time (org-parse-time-string s)))
13418 (defun org-time-string-to-seconds (s)
13419 (org-float-time (org-time-string-to-time s)))
13421 (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
13422 "Convert a time stamp to an absolute day number.
13423 If there is a specifyer for a cyclic time stamp, get the closest date to
13424 DAYNR.
13425 PREFER and SHOW-ALL are passed through to `org-closest-date'.
13426 the variable date is bound by the calendar when this is called."
13427 (cond
13428 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
13429 (if (org-diary-sexp-entry (match-string 1 s) "" date)
13430 daynr
13431 (+ daynr 1000)))
13432 ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
13433 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
13434 (time-to-days (current-time))) (match-string 0 s)
13435 prefer show-all))
13436 (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
13438 (defun org-days-to-iso-week (days)
13439 "Return the iso week number."
13440 (require 'cal-iso)
13441 (car (calendar-iso-from-absolute days)))
13443 (defun org-small-year-to-year (year)
13444 "Convert 2-digit years into 4-digit years.
13445 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
13446 The year 2000 cannot be abbreviated. Any year larger than 99
13447 is returned unchanged."
13448 (if (< year 38)
13449 (setq year (+ 2000 year))
13450 (if (< year 100)
13451 (setq year (+ 1900 year))))
13452 year)
13454 (defun org-time-from-absolute (d)
13455 "Return the time corresponding to date D.
13456 D may be an absolute day number, or a calendar-type list (month day year)."
13457 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
13458 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
13460 (defun org-calendar-holiday ()
13461 "List of holidays, for Diary display in Org-mode."
13462 (require 'holidays)
13463 (let ((hl (funcall
13464 (if (fboundp 'calendar-check-holidays)
13465 'calendar-check-holidays 'check-calendar-holidays) date)))
13466 (if hl (mapconcat 'identity hl "; "))))
13468 (defun org-diary-sexp-entry (sexp entry date)
13469 "Process a SEXP diary ENTRY for DATE."
13470 (require 'diary-lib)
13471 (let ((result (if calendar-debug-sexp
13472 (let ((stack-trace-on-error t))
13473 (eval (car (read-from-string sexp))))
13474 (condition-case nil
13475 (eval (car (read-from-string sexp)))
13476 (error
13477 (beep)
13478 (message "Bad sexp at line %d in %s: %s"
13479 (org-current-line)
13480 (buffer-file-name) sexp)
13481 (sleep-for 2))))))
13482 (cond ((stringp result) result)
13483 ((and (consp result)
13484 (stringp (cdr result))) (cdr result))
13485 (result entry)
13486 (t nil))))
13488 (defun org-diary-to-ical-string (frombuf)
13489 "Get iCalendar entries from diary entries in buffer FROMBUF.
13490 This uses the icalendar.el library."
13491 (let* ((tmpdir (if (featurep 'xemacs)
13492 (temp-directory)
13493 temporary-file-directory))
13494 (tmpfile (make-temp-name
13495 (expand-file-name "orgics" tmpdir)))
13496 buf rtn b e)
13497 (save-excursion
13498 (set-buffer frombuf)
13499 (icalendar-export-region (point-min) (point-max) tmpfile)
13500 (setq buf (find-buffer-visiting tmpfile))
13501 (set-buffer buf)
13502 (goto-char (point-min))
13503 (if (re-search-forward "^BEGIN:VEVENT" nil t)
13504 (setq b (match-beginning 0)))
13505 (goto-char (point-max))
13506 (if (re-search-backward "^END:VEVENT" nil t)
13507 (setq e (match-end 0)))
13508 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
13509 (kill-buffer buf)
13510 (delete-file tmpfile)
13511 rtn))
13513 (defun org-closest-date (start current change prefer show-all)
13514 "Find the date closest to CURRENT that is consistent with START and CHANGE.
13515 When PREFER is `past' return a date that is either CURRENT or past.
13516 When PREFER is `future', return a date that is either CURRENT or future.
13517 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
13518 ;; Make the proper lists from the dates
13519 (catch 'exit
13520 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
13521 dn dw sday cday n1 n2 n0
13522 d m y y1 y2 date1 date2 nmonths nm ny m2)
13524 (setq start (org-date-to-gregorian start)
13525 current (org-date-to-gregorian
13526 (if show-all
13527 current
13528 (time-to-days (current-time))))
13529 sday (calendar-absolute-from-gregorian start)
13530 cday (calendar-absolute-from-gregorian current))
13532 (if (<= cday sday) (throw 'exit sday))
13534 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
13535 (setq dn (string-to-number (match-string 1 change))
13536 dw (cdr (assoc (match-string 2 change) a1)))
13537 (error "Invalid change specifyer: %s" change))
13538 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
13539 (cond
13540 ((eq dw 'day)
13541 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
13542 n2 (+ n1 dn)))
13543 ((eq dw 'year)
13544 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
13545 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
13546 (setq date1 (list m d y1)
13547 n1 (calendar-absolute-from-gregorian date1)
13548 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
13549 n2 (calendar-absolute-from-gregorian date2)))
13550 ((eq dw 'month)
13551 ;; approx number of month between the two dates
13552 (setq nmonths (floor (/ (- cday sday) 30.436875)))
13553 ;; How often does dn fit in there?
13554 (setq d (nth 1 start) m (car start) y (nth 2 start)
13555 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
13556 m (+ m nm)
13557 ny (floor (/ m 12))
13558 y (+ y ny)
13559 m (- m (* ny 12)))
13560 (while (> m 12) (setq m (- m 12) y (1+ y)))
13561 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
13562 (setq m2 (+ m dn) y2 y)
13563 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
13564 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
13565 (while (<= n2 cday)
13566 (setq n1 n2 m m2 y y2)
13567 (setq m2 (+ m dn) y2 y)
13568 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
13569 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
13570 ;; Make sure n1 is the earlier date
13571 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
13572 (if show-all
13573 (cond
13574 ((eq prefer 'past) (if (= cday n2) n2 n1))
13575 ((eq prefer 'future) (if (= cday n1) n1 n2))
13576 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
13577 (cond
13578 ((eq prefer 'past) (if (= cday n2) n2 n1))
13579 ((eq prefer 'future) (if (= cday n1) n1 n2))
13580 (t (if (= cday n1) n1 n2)))))))
13582 (defun org-date-to-gregorian (date)
13583 "Turn any specification of DATE into a gregorian date for the calendar."
13584 (cond ((integerp date) (calendar-gregorian-from-absolute date))
13585 ((and (listp date) (= (length date) 3)) date)
13586 ((stringp date)
13587 (setq date (org-parse-time-string date))
13588 (list (nth 4 date) (nth 3 date) (nth 5 date)))
13589 ((listp date)
13590 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
13592 (defun org-parse-time-string (s &optional nodefault)
13593 "Parse the standard Org-mode time string.
13594 This should be a lot faster than the normal `parse-time-string'.
13595 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
13596 hour and minute fields will be nil if not given."
13597 (if (string-match org-ts-regexp0 s)
13598 (list 0
13599 (if (or (match-beginning 8) (not nodefault))
13600 (string-to-number (or (match-string 8 s) "0")))
13601 (if (or (match-beginning 7) (not nodefault))
13602 (string-to-number (or (match-string 7 s) "0")))
13603 (string-to-number (match-string 4 s))
13604 (string-to-number (match-string 3 s))
13605 (string-to-number (match-string 2 s))
13606 nil nil nil)
13607 (error "Not a standard Org-mode time string: %s" s)))
13609 (defun org-timestamp-up (&optional arg)
13610 "Increase the date item at the cursor by one.
13611 If the cursor is on the year, change the year. If it is on the month or
13612 the day, change that.
13613 With prefix ARG, change by that many units."
13614 (interactive "p")
13615 (org-timestamp-change (prefix-numeric-value arg)))
13617 (defun org-timestamp-down (&optional arg)
13618 "Decrease the date item at the cursor by one.
13619 If the cursor is on the year, change the year. If it is on the month or
13620 the day, change that.
13621 With prefix ARG, change by that many units."
13622 (interactive "p")
13623 (org-timestamp-change (- (prefix-numeric-value arg))))
13625 (defun org-timestamp-up-day (&optional arg)
13626 "Increase the date in the time stamp by one day.
13627 With prefix ARG, change that many days."
13628 (interactive "p")
13629 (if (and (not (org-at-timestamp-p t))
13630 (org-on-heading-p))
13631 (org-todo 'up)
13632 (org-timestamp-change (prefix-numeric-value arg) 'day)))
13634 (defun org-timestamp-down-day (&optional arg)
13635 "Decrease the date in the time stamp by one day.
13636 With prefix ARG, change that many days."
13637 (interactive "p")
13638 (if (and (not (org-at-timestamp-p t))
13639 (org-on-heading-p))
13640 (org-todo 'down)
13641 (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
13643 (defun org-at-timestamp-p (&optional inactive-ok)
13644 "Determine if the cursor is in or at a timestamp."
13645 (interactive)
13646 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
13647 (pos (point))
13648 (ans (or (looking-at tsr)
13649 (save-excursion
13650 (skip-chars-backward "^[<\n\r\t")
13651 (if (> (point) (point-min)) (backward-char 1))
13652 (and (looking-at tsr)
13653 (> (- (match-end 0) pos) -1))))))
13654 (and ans
13655 (boundp 'org-ts-what)
13656 (setq org-ts-what
13657 (cond
13658 ((= pos (match-beginning 0)) 'bracket)
13659 ((= pos (1- (match-end 0))) 'bracket)
13660 ((org-pos-in-match-range pos 2) 'year)
13661 ((org-pos-in-match-range pos 3) 'month)
13662 ((org-pos-in-match-range pos 7) 'hour)
13663 ((org-pos-in-match-range pos 8) 'minute)
13664 ((or (org-pos-in-match-range pos 4)
13665 (org-pos-in-match-range pos 5)) 'day)
13666 ((and (> pos (or (match-end 8) (match-end 5)))
13667 (< pos (match-end 0)))
13668 (- pos (or (match-end 8) (match-end 5))))
13669 (t 'day))))
13670 ans))
13672 (defun org-toggle-timestamp-type ()
13673 "Toggle the type (<active> or [inactive]) of a time stamp."
13674 (interactive)
13675 (when (org-at-timestamp-p t)
13676 (let ((beg (match-beginning 0)) (end (match-end 0))
13677 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
13678 (save-excursion
13679 (goto-char beg)
13680 (while (re-search-forward "[][<>]" end t)
13681 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
13682 t t)))
13683 (message "Timestamp is now %sactive"
13684 (if (equal (char-after beg) ?<) "" "in")))))
13686 (defun org-timestamp-change (n &optional what)
13687 "Change the date in the time stamp at point.
13688 The date will be changed by N times WHAT. WHAT can be `day', `month',
13689 `year', `minute', `second'. If WHAT is not given, the cursor position
13690 in the timestamp determines what will be changed."
13691 (let ((pos (point))
13692 with-hm inactive
13693 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
13694 org-ts-what
13695 extra rem
13696 ts time time0)
13697 (if (not (org-at-timestamp-p t))
13698 (error "Not at a timestamp"))
13699 (if (and (not what) (eq org-ts-what 'bracket))
13700 (org-toggle-timestamp-type)
13701 (if (and (not what) (not (eq org-ts-what 'day))
13702 org-display-custom-times
13703 (get-text-property (point) 'display)
13704 (not (get-text-property (1- (point)) 'display)))
13705 (setq org-ts-what 'day))
13706 (setq org-ts-what (or what org-ts-what)
13707 inactive (= (char-after (match-beginning 0)) ?\[)
13708 ts (match-string 0))
13709 (replace-match "")
13710 (if (string-match
13711 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
13713 (setq extra (match-string 1 ts)))
13714 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
13715 (setq with-hm t))
13716 (setq time0 (org-parse-time-string ts))
13717 (when (and (eq org-ts-what 'minute)
13718 (eq current-prefix-arg nil))
13719 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
13720 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
13721 (setcar (cdr time0) (+ (nth 1 time0)
13722 (if (> n 0) (- rem) (- dm rem))))))
13723 (setq time
13724 (encode-time (or (car time0) 0)
13725 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
13726 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
13727 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
13728 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
13729 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
13730 (nthcdr 6 time0)))
13731 (when (and (member org-ts-what '(hour minute))
13732 extra
13733 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
13734 (setq extra (org-modify-ts-extra
13735 extra
13736 (if (eq org-ts-what 'hour) 2 5)
13737 n dm)))
13738 (when (integerp org-ts-what)
13739 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
13740 (if (eq what 'calendar)
13741 (let ((cal-date (org-get-date-from-calendar)))
13742 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
13743 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
13744 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
13745 (setcar time0 (or (car time0) 0))
13746 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
13747 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
13748 (setq time (apply 'encode-time time0))))
13749 (setq org-last-changed-timestamp
13750 (org-insert-time-stamp time with-hm inactive nil nil extra))
13751 (org-clock-update-time-maybe)
13752 (goto-char pos)
13753 ;; Try to recenter the calendar window, if any
13754 (if (and org-calendar-follow-timestamp-change
13755 (get-buffer-window "*Calendar*" t)
13756 (memq org-ts-what '(day month year)))
13757 (org-recenter-calendar (time-to-days time))))))
13759 (defun org-modify-ts-extra (s pos n dm)
13760 "Change the different parts of the lead-time and repeat fields in timestamp."
13761 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
13762 ng h m new rem)
13763 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
13764 (cond
13765 ((or (org-pos-in-match-range pos 2)
13766 (org-pos-in-match-range pos 3))
13767 (setq m (string-to-number (match-string 3 s))
13768 h (string-to-number (match-string 2 s)))
13769 (if (org-pos-in-match-range pos 2)
13770 (setq h (+ h n))
13771 (setq n (* dm (org-no-warnings (signum n))))
13772 (when (not (= 0 (setq rem (% m dm))))
13773 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
13774 (setq m (+ m n)))
13775 (if (< m 0) (setq m (+ m 60) h (1- h)))
13776 (if (> m 59) (setq m (- m 60) h (1+ h)))
13777 (setq h (min 24 (max 0 h)))
13778 (setq ng 1 new (format "-%02d:%02d" h m)))
13779 ((org-pos-in-match-range pos 6)
13780 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
13781 ((org-pos-in-match-range pos 5)
13782 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
13784 ((org-pos-in-match-range pos 9)
13785 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
13786 ((org-pos-in-match-range pos 8)
13787 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
13789 (when ng
13790 (setq s (concat
13791 (substring s 0 (match-beginning ng))
13793 (substring s (match-end ng))))))
13796 (defun org-recenter-calendar (date)
13797 "If the calendar is visible, recenter it to DATE."
13798 (let* ((win (selected-window))
13799 (cwin (get-buffer-window "*Calendar*" t))
13800 (calendar-move-hook nil))
13801 (when cwin
13802 (select-window cwin)
13803 (calendar-goto-date (if (listp date) date
13804 (calendar-gregorian-from-absolute date)))
13805 (select-window win))))
13807 (defun org-goto-calendar (&optional arg)
13808 "Go to the Emacs calendar at the current date.
13809 If there is a time stamp in the current line, go to that date.
13810 A prefix ARG can be used to force the current date."
13811 (interactive "P")
13812 (let ((tsr org-ts-regexp) diff
13813 (calendar-move-hook nil)
13814 (calendar-view-holidays-initially-flag nil)
13815 (view-calendar-holidays-initially nil)
13816 (calendar-view-diary-initially-flag nil)
13817 (view-diary-entries-initially nil))
13818 (if (or (org-at-timestamp-p)
13819 (save-excursion
13820 (beginning-of-line 1)
13821 (looking-at (concat ".*" tsr))))
13822 (let ((d1 (time-to-days (current-time)))
13823 (d2 (time-to-days
13824 (org-time-string-to-time (match-string 1)))))
13825 (setq diff (- d2 d1))))
13826 (calendar)
13827 (calendar-goto-today)
13828 (if (and diff (not arg)) (calendar-forward-day diff))))
13830 (defun org-get-date-from-calendar ()
13831 "Return a list (month day year) of date at point in calendar."
13832 (with-current-buffer "*Calendar*"
13833 (save-match-data
13834 (calendar-cursor-to-date))))
13836 (defun org-date-from-calendar ()
13837 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
13838 If there is already a time stamp at the cursor position, update it."
13839 (interactive)
13840 (if (org-at-timestamp-p t)
13841 (org-timestamp-change 0 'calendar)
13842 (let ((cal-date (org-get-date-from-calendar)))
13843 (org-insert-time-stamp
13844 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
13846 (defun org-minutes-to-hh:mm-string (m)
13847 "Compute H:MM from a number of minutes."
13848 (let ((h (/ m 60)))
13849 (setq m (- m (* 60 h)))
13850 (format org-time-clocksum-format h m)))
13852 (defun org-hh:mm-string-to-minutes (s)
13853 "Convert a string H:MM to a number of minutes.
13854 If the string is just a number, interprete it as minutes.
13855 In fact, the first hh:mm or number in the string will be taken,
13856 there can be extra stuff in the string.
13857 If no number is found, the return value is 0."
13858 (cond
13859 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
13860 (+ (* (string-to-number (match-string 1 s)) 60)
13861 (string-to-number (match-string 2 s))))
13862 ((string-match "\\([0-9]+\\)" s)
13863 (string-to-number (match-string 1 s)))
13864 (t 0)))
13866 ;;;; Files
13868 (defun org-save-all-org-buffers ()
13869 "Save all Org-mode buffers without user confirmation."
13870 (interactive)
13871 (message "Saving all Org-mode buffers...")
13872 (save-some-buffers t 'org-mode-p)
13873 (when (featurep 'org-id) (org-id-locations-save))
13874 (message "Saving all Org-mode buffers... done"))
13876 (defun org-revert-all-org-buffers ()
13877 "Revert all Org-mode buffers.
13878 Prompt for confirmation when there are unsaved changes.
13879 Be sure you know what you are doing before letting this function
13880 overwrite your changes.
13882 This function is useful in a setup where one tracks org files
13883 with a version control system, to revert on one machine after pulling
13884 changes from another. I believe the procedure must be like this:
13886 1. M-x org-save-all-org-buffers
13887 2. Pull changes from the other machine, resolve conflicts
13888 3. M-x org-revert-all-org-buffers"
13889 (interactive)
13890 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
13891 (error "Abort"))
13892 (save-excursion
13893 (save-window-excursion
13894 (mapc
13895 (lambda (b)
13896 (when (and (with-current-buffer b (org-mode-p))
13897 (with-current-buffer b buffer-file-name))
13898 (switch-to-buffer b)
13899 (revert-buffer t 'no-confirm)))
13900 (buffer-list))
13901 (when (and (featurep 'org-id) org-id-track-globally)
13902 (org-id-locations-load)))))
13904 ;;;; Agenda files
13906 ;;;###autoload
13907 (defun org-iswitchb (&optional arg)
13908 "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
13909 With a prefix argument, restrict available to files.
13910 With two prefix arguments, restrict available buffers to agenda files."
13911 (interactive "P")
13912 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
13913 ((equal arg '(16)) (org-buffer-list 'agenda))
13914 (t (org-buffer-list)))))
13915 (switch-to-buffer
13916 (org-icompleting-read "Org buffer: "
13917 (mapcar 'list (mapcar 'buffer-name blist))
13918 nil t))))
13920 ;;;###autoload
13921 (defalias 'org-ido-switchb 'org-iswitchb)
13923 (defun org-buffer-list (&optional predicate exclude-tmp)
13924 "Return a list of Org buffers.
13925 PREDICATE can be `export', `files' or `agenda'.
13927 export restrict the list to Export buffers.
13928 files restrict the list to buffers visiting Org files.
13929 agenda restrict the list to buffers visiting agenda files.
13931 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
13932 (let* ((bfn nil)
13933 (agenda-files (and (eq predicate 'agenda)
13934 (mapcar 'file-truename (org-agenda-files t))))
13935 (filter
13936 (cond
13937 ((eq predicate 'files)
13938 (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
13939 ((eq predicate 'export)
13940 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
13941 ((eq predicate 'agenda)
13942 (lambda (b)
13943 (with-current-buffer b
13944 (and (eq major-mode 'org-mode)
13945 (setq bfn (buffer-file-name b))
13946 (member (file-truename bfn) agenda-files)))))
13947 (t (lambda (b) (with-current-buffer b
13948 (or (eq major-mode 'org-mode)
13949 (string-match "\*Org .*Export"
13950 (buffer-name b)))))))))
13951 (delq nil
13952 (mapcar
13953 (lambda(b)
13954 (if (and (funcall filter b)
13955 (or (not exclude-tmp)
13956 (not (string-match "tmp" (buffer-name b)))))
13958 nil))
13959 (buffer-list)))))
13961 (defun org-agenda-files (&optional unrestricted archives)
13962 "Get the list of agenda files.
13963 Optional UNRESTRICTED means return the full list even if a restriction
13964 is currently in place.
13965 When ARCHIVES is t, include all archive files hat are really being
13966 used by the agenda files. If ARCHIVE is `ifmode', do this only if
13967 `org-agenda-archives-mode' is t."
13968 (let ((files
13969 (cond
13970 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
13971 ((stringp org-agenda-files) (org-read-agenda-file-list))
13972 ((listp org-agenda-files) org-agenda-files)
13973 (t (error "Invalid value of `org-agenda-files'")))))
13974 (setq files (apply 'append
13975 (mapcar (lambda (f)
13976 (if (file-directory-p f)
13977 (directory-files
13978 f t org-agenda-file-regexp)
13979 (list f)))
13980 files)))
13981 (when org-agenda-skip-unavailable-files
13982 (setq files (delq nil
13983 (mapcar (function
13984 (lambda (file)
13985 (and (file-readable-p file) file)))
13986 files))))
13987 (when (or (eq archives t)
13988 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
13989 (setq files (org-add-archive-files files)))
13990 files))
13992 (defun org-edit-agenda-file-list ()
13993 "Edit the list of agenda files.
13994 Depending on setup, this either uses customize to edit the variable
13995 `org-agenda-files', or it visits the file that is holding the list. In the
13996 latter case, the buffer is set up in a way that saving it automatically kills
13997 the buffer and restores the previous window configuration."
13998 (interactive)
13999 (if (stringp org-agenda-files)
14000 (let ((cw (current-window-configuration)))
14001 (find-file org-agenda-files)
14002 (org-set-local 'org-window-configuration cw)
14003 (org-add-hook 'after-save-hook
14004 (lambda ()
14005 (set-window-configuration
14006 (prog1 org-window-configuration
14007 (kill-buffer (current-buffer))))
14008 (org-install-agenda-files-menu)
14009 (message "New agenda file list installed"))
14010 nil 'local)
14011 (message "%s" (substitute-command-keys
14012 "Edit list and finish with \\[save-buffer]")))
14013 (customize-variable 'org-agenda-files)))
14015 (defun org-store-new-agenda-file-list (list)
14016 "Set new value for the agenda file list and save it correctly."
14017 (if (stringp org-agenda-files)
14018 (let ((f org-agenda-files) b)
14019 (while (setq b (find-buffer-visiting f)) (kill-buffer b))
14020 (with-temp-file f
14021 (insert (mapconcat 'identity list "\n") "\n")))
14022 (let ((org-mode-hook nil) (org-inhibit-startup t)
14023 (org-insert-mode-line-in-empty-file nil))
14024 (setq org-agenda-files list)
14025 (customize-save-variable 'org-agenda-files org-agenda-files))))
14027 (defun org-read-agenda-file-list ()
14028 "Read the list of agenda files from a file."
14029 (when (file-directory-p org-agenda-files)
14030 (error "`org-agenda-files' cannot be a single directory"))
14031 (when (stringp org-agenda-files)
14032 (with-temp-buffer
14033 (insert-file-contents org-agenda-files)
14034 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
14037 ;;;###autoload
14038 (defun org-cycle-agenda-files ()
14039 "Cycle through the files in `org-agenda-files'.
14040 If the current buffer visits an agenda file, find the next one in the list.
14041 If the current buffer does not, find the first agenda file."
14042 (interactive)
14043 (let* ((fs (org-agenda-files t))
14044 (files (append fs (list (car fs))))
14045 (tcf (if buffer-file-name (file-truename buffer-file-name)))
14046 file)
14047 (unless files (error "No agenda files"))
14048 (catch 'exit
14049 (while (setq file (pop files))
14050 (if (equal (file-truename file) tcf)
14051 (when (car files)
14052 (find-file (car files))
14053 (throw 'exit t))))
14054 (find-file (car fs)))
14055 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
14057 (defun org-agenda-file-to-front (&optional to-end)
14058 "Move/add the current file to the top of the agenda file list.
14059 If the file is not present in the list, it is added to the front. If it is
14060 present, it is moved there. With optional argument TO-END, add/move to the
14061 end of the list."
14062 (interactive "P")
14063 (let ((org-agenda-skip-unavailable-files nil)
14064 (file-alist (mapcar (lambda (x)
14065 (cons (file-truename x) x))
14066 (org-agenda-files t)))
14067 (ctf (file-truename buffer-file-name))
14068 x had)
14069 (setq x (assoc ctf file-alist) had x)
14071 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
14072 (if to-end
14073 (setq file-alist (append (delq x file-alist) (list x)))
14074 (setq file-alist (cons x (delq x file-alist))))
14075 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
14076 (org-install-agenda-files-menu)
14077 (message "File %s to %s of agenda file list"
14078 (if had "moved" "added") (if to-end "end" "front"))))
14080 (defun org-remove-file (&optional file)
14081 "Remove current file from the list of files in variable `org-agenda-files'.
14082 These are the files which are being checked for agenda entries.
14083 Optional argument FILE means, use this file instead of the current."
14084 (interactive)
14085 (let* ((org-agenda-skip-unavailable-files nil)
14086 (file (or file buffer-file-name))
14087 (true-file (file-truename file))
14088 (afile (abbreviate-file-name file))
14089 (files (delq nil (mapcar
14090 (lambda (x)
14091 (if (equal true-file
14092 (file-truename x))
14093 nil x))
14094 (org-agenda-files t)))))
14095 (if (not (= (length files) (length (org-agenda-files t))))
14096 (progn
14097 (org-store-new-agenda-file-list files)
14098 (org-install-agenda-files-menu)
14099 (message "Removed file: %s" afile))
14100 (message "File was not in list: %s (not removed)" afile))))
14102 (defun org-file-menu-entry (file)
14103 (vector file (list 'find-file file) t))
14105 (defun org-check-agenda-file (file)
14106 "Make sure FILE exists. If not, ask user what to do."
14107 (when (not (file-exists-p file))
14108 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
14109 (abbreviate-file-name file))
14110 (let ((r (downcase (read-char-exclusive))))
14111 (cond
14112 ((equal r ?r)
14113 (org-remove-file file)
14114 (throw 'nextfile t))
14115 (t (error "Abort"))))))
14117 (defun org-get-agenda-file-buffer (file)
14118 "Get a buffer visiting FILE. If the buffer needs to be created, add
14119 it to the list of buffers which might be released later."
14120 (let ((buf (org-find-base-buffer-visiting file)))
14121 (if buf
14122 buf ; just return it
14123 ;; Make a new buffer and remember it
14124 (setq buf (find-file-noselect file))
14125 (if buf (push buf org-agenda-new-buffers))
14126 buf)))
14128 (defun org-release-buffers (blist)
14129 "Release all buffers in list, asking the user for confirmation when needed.
14130 When a buffer is unmodified, it is just killed. When modified, it is saved
14131 \(if the user agrees) and then killed."
14132 (let (buf file)
14133 (while (setq buf (pop blist))
14134 (setq file (buffer-file-name buf))
14135 (when (and (buffer-modified-p buf)
14136 file
14137 (y-or-n-p (format "Save file %s? " file)))
14138 (with-current-buffer buf (save-buffer)))
14139 (kill-buffer buf))))
14141 (defun org-prepare-agenda-buffers (files)
14142 "Create buffers for all agenda files, protect archived trees and comments."
14143 (interactive)
14144 (let ((pa '(:org-archived t))
14145 (pc '(:org-comment t))
14146 (pall '(:org-archived t :org-comment t))
14147 (inhibit-read-only t)
14148 (rea (concat ":" org-archive-tag ":"))
14149 bmp file re)
14150 (save-excursion
14151 (save-restriction
14152 (while (setq file (pop files))
14153 (catch 'nextfile
14154 (if (bufferp file)
14155 (set-buffer file)
14156 (org-check-agenda-file file)
14157 (set-buffer (org-get-agenda-file-buffer file)))
14158 (widen)
14159 (setq bmp (buffer-modified-p))
14160 (org-refresh-category-properties)
14161 (setq org-todo-keywords-for-agenda
14162 (append org-todo-keywords-for-agenda org-todo-keywords-1))
14163 (setq org-done-keywords-for-agenda
14164 (append org-done-keywords-for-agenda org-done-keywords))
14165 (setq org-todo-keyword-alist-for-agenda
14166 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
14167 (setq org-drawers-for-agenda
14168 (append org-drawers-for-agenda org-drawers))
14169 (setq org-tag-alist-for-agenda
14170 (append org-tag-alist-for-agenda org-tag-alist))
14172 (save-excursion
14173 (remove-text-properties (point-min) (point-max) pall)
14174 (when org-agenda-skip-archived-trees
14175 (goto-char (point-min))
14176 (while (re-search-forward rea nil t)
14177 (if (org-on-heading-p t)
14178 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
14179 (goto-char (point-min))
14180 (setq re (concat "^\\*+ +" org-comment-string "\\>"))
14181 (while (re-search-forward re nil t)
14182 (add-text-properties
14183 (match-beginning 0) (org-end-of-subtree t) pc)))
14184 (set-buffer-modified-p bmp)))))
14185 (setq org-todo-keyword-alist-for-agenda
14186 (org-uniquify org-todo-keyword-alist-for-agenda)
14187 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
14189 ;;;; Embedded LaTeX
14191 (defvar org-cdlatex-mode-map (make-sparse-keymap)
14192 "Keymap for the minor `org-cdlatex-mode'.")
14194 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
14195 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
14196 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
14197 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
14198 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
14200 (defvar org-cdlatex-texmathp-advice-is-done nil
14201 "Flag remembering if we have applied the advice to texmathp already.")
14203 (define-minor-mode org-cdlatex-mode
14204 "Toggle the minor `org-cdlatex-mode'.
14205 This mode supports entering LaTeX environment and math in LaTeX fragments
14206 in Org-mode.
14207 \\{org-cdlatex-mode-map}"
14208 nil " OCDL" nil
14209 (when org-cdlatex-mode (require 'cdlatex))
14210 (unless org-cdlatex-texmathp-advice-is-done
14211 (setq org-cdlatex-texmathp-advice-is-done t)
14212 (defadvice texmathp (around org-math-always-on activate)
14213 "Always return t in org-mode buffers.
14214 This is because we want to insert math symbols without dollars even outside
14215 the LaTeX math segments. If Orgmode thinks that point is actually inside
14216 an embedded LaTeX fragment, let texmathp do its job.
14217 \\[org-cdlatex-mode-map]"
14218 (interactive)
14219 (let (p)
14220 (cond
14221 ((not (org-mode-p)) ad-do-it)
14222 ((eq this-command 'cdlatex-math-symbol)
14223 (setq ad-return-value t
14224 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
14226 (let ((p (org-inside-LaTeX-fragment-p)))
14227 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
14228 (setq ad-return-value t
14229 texmathp-why '("Org-mode embedded math" . 0))
14230 (if p ad-do-it)))))))))
14232 (defun turn-on-org-cdlatex ()
14233 "Unconditionally turn on `org-cdlatex-mode'."
14234 (org-cdlatex-mode 1))
14236 (defun org-inside-LaTeX-fragment-p ()
14237 "Test if point is inside a LaTeX fragment.
14238 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
14239 sequence appearing also before point.
14240 Even though the matchers for math are configurable, this function assumes
14241 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
14242 delimiters are skipped when they have been removed by customization.
14243 The return value is nil, or a cons cell with the delimiter and
14244 and the position of this delimiter.
14246 This function does a reasonably good job, but can locally be fooled by
14247 for example currency specifications. For example it will assume being in
14248 inline math after \"$22.34\". The LaTeX fragment formatter will only format
14249 fragments that are properly closed, but during editing, we have to live
14250 with the uncertainty caused by missing closing delimiters. This function
14251 looks only before point, not after."
14252 (catch 'exit
14253 (let ((pos (point))
14254 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
14255 (lim (progn
14256 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
14257 (point)))
14258 dd-on str (start 0) m re)
14259 (goto-char pos)
14260 (when dodollar
14261 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
14262 re (nth 1 (assoc "$" org-latex-regexps)))
14263 (while (string-match re str start)
14264 (cond
14265 ((= (match-end 0) (length str))
14266 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
14267 ((= (match-end 0) (- (length str) 5))
14268 (throw 'exit nil))
14269 (t (setq start (match-end 0))))))
14270 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
14271 (goto-char pos)
14272 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
14273 (and (match-beginning 2) (throw 'exit nil))
14274 ;; count $$
14275 (while (re-search-backward "\\$\\$" lim t)
14276 (setq dd-on (not dd-on)))
14277 (goto-char pos)
14278 (if dd-on (cons "$$" m))))))
14281 (defun org-try-cdlatex-tab ()
14282 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
14283 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
14284 - inside a LaTeX fragment, or
14285 - after the first word in a line, where an abbreviation expansion could
14286 insert a LaTeX environment."
14287 (when org-cdlatex-mode
14288 (cond
14289 ((save-excursion
14290 (skip-chars-backward "a-zA-Z0-9*")
14291 (skip-chars-backward " \t")
14292 (bolp))
14293 (cdlatex-tab) t)
14294 ((org-inside-LaTeX-fragment-p)
14295 (cdlatex-tab) t)
14296 (t nil))))
14298 (defun org-cdlatex-underscore-caret (&optional arg)
14299 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
14300 Revert to the normal definition outside of these fragments."
14301 (interactive "P")
14302 (if (org-inside-LaTeX-fragment-p)
14303 (call-interactively 'cdlatex-sub-superscript)
14304 (let (org-cdlatex-mode)
14305 (call-interactively (key-binding (vector last-input-event))))))
14307 (defun org-cdlatex-math-modify (&optional arg)
14308 "Execute `cdlatex-math-modify' in LaTeX fragments.
14309 Revert to the normal definition outside of these fragments."
14310 (interactive "P")
14311 (if (org-inside-LaTeX-fragment-p)
14312 (call-interactively 'cdlatex-math-modify)
14313 (let (org-cdlatex-mode)
14314 (call-interactively (key-binding (vector last-input-event))))))
14316 (defvar org-latex-fragment-image-overlays nil
14317 "List of overlays carrying the images of latex fragments.")
14318 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
14320 (defun org-remove-latex-fragment-image-overlays ()
14321 "Remove all overlays with LaTeX fragment images in current buffer."
14322 (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
14323 (setq org-latex-fragment-image-overlays nil))
14325 (defun org-preview-latex-fragment (&optional subtree)
14326 "Preview the LaTeX fragment at point, or all locally or globally.
14327 If the cursor is in a LaTeX fragment, create the image and overlay
14328 it over the source code. If there is no fragment at point, display
14329 all fragments in the current text, from one headline to the next. With
14330 prefix SUBTREE, display all fragments in the current subtree. With a
14331 double prefix `C-u C-u', or when the cursor is before the first headline,
14332 display all fragments in the buffer.
14333 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
14334 (interactive "P")
14335 (org-remove-latex-fragment-image-overlays)
14336 (save-excursion
14337 (save-restriction
14338 (let (beg end at msg)
14339 (cond
14340 ((or (equal subtree '(16))
14341 (not (save-excursion
14342 (re-search-backward (concat "^" outline-regexp) nil t))))
14343 (setq beg (point-min) end (point-max)
14344 msg "Creating images for buffer...%s"))
14345 ((equal subtree '(4))
14346 (org-back-to-heading)
14347 (setq beg (point) end (org-end-of-subtree t)
14348 msg "Creating images for subtree...%s"))
14350 (if (setq at (org-inside-LaTeX-fragment-p))
14351 (goto-char (max (point-min) (- (cdr at) 2)))
14352 (org-back-to-heading))
14353 (setq beg (point) end (progn (outline-next-heading) (point))
14354 msg (if at "Creating image...%s"
14355 "Creating images for entry...%s"))))
14356 (message msg "")
14357 (narrow-to-region beg end)
14358 (goto-char beg)
14359 (org-format-latex
14360 (concat "ltxpng/" (file-name-sans-extension
14361 (file-name-nondirectory
14362 buffer-file-name)))
14363 default-directory 'overlays msg at 'forbuffer)
14364 (message msg "done. Use `C-c C-c' to remove images.")))))
14366 (defvar org-latex-regexps
14367 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
14368 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
14369 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
14370 ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
14371 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
14372 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
14373 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
14374 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
14375 "Regular expressions for matching embedded LaTeX.")
14377 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
14378 "Replace LaTeX fragments with links to an image, and produce images.
14379 Some of the options can be changed using the variable
14380 `org-format-latex-options'."
14381 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
14382 (let* ((prefixnodir (file-name-nondirectory prefix))
14383 (absprefix (expand-file-name prefix dir))
14384 (todir (file-name-directory absprefix))
14385 (opt org-format-latex-options)
14386 (matchers (plist-get opt :matchers))
14387 (re-list org-latex-regexps)
14388 (cnt 0) txt link beg end re e checkdir
14389 executables-checked
14390 m n block linkfile movefile ov)
14391 ;; Check if there are old images files with this prefix, and remove them
14392 (when (file-directory-p todir)
14393 (mapc 'delete-file
14394 (directory-files
14395 todir 'full
14396 (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
14397 ;; Check the different regular expressions
14398 (while (setq e (pop re-list))
14399 (setq m (car e) re (nth 1 e) n (nth 2 e)
14400 block (if (nth 3 e) "\n\n" ""))
14401 (when (member m matchers)
14402 (goto-char (point-min))
14403 (while (re-search-forward re nil t)
14404 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
14405 (not (get-text-property (match-beginning n)
14406 'org-protected))
14407 (or (not overlays)
14408 (not (eq (get-char-property (match-beginning n)
14409 'org-overlay-type)
14410 'org-latex-overlay))))
14411 (setq txt (match-string n)
14412 beg (match-beginning n) end (match-end n)
14413 cnt (1+ cnt)
14414 linkfile (format "%s_%04d.png" prefix cnt)
14415 movefile (format "%s_%04d.png" absprefix cnt)
14416 link (concat block "[[file:" linkfile "]]" block))
14417 (if msg (message msg cnt))
14418 (goto-char beg)
14419 (unless checkdir ; make sure the directory exists
14420 (setq checkdir t)
14421 (or (file-directory-p todir) (make-directory todir)))
14423 (unless executables-checked
14424 (org-check-external-command
14425 "latex" "needed to convert LaTeX fragments to images")
14426 (org-check-external-command
14427 "dvipng" "needed to convert LaTeX fragments to images")
14428 (setq executables-checked t))
14430 (org-create-formula-image
14431 txt movefile opt forbuffer)
14432 (if overlays
14433 (progn
14434 (mapc (lambda (o)
14435 (if (eq (org-overlay-get o 'org-overlay-type)
14436 'org-latex-overlay)
14437 (org-delete-overlay o)))
14438 (org-overlays-in beg end))
14439 (setq ov (org-make-overlay beg end))
14440 (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
14441 (if (featurep 'xemacs)
14442 (progn
14443 (org-overlay-put ov 'invisible t)
14444 (org-overlay-put
14445 ov 'end-glyph
14446 (make-glyph (vector 'png :file movefile))))
14447 (org-overlay-put
14448 ov 'display
14449 (list 'image :type 'png :file movefile :ascent 'center)))
14450 (push ov org-latex-fragment-image-overlays)
14451 (goto-char end))
14452 (delete-region beg end)
14453 (insert link))))))))
14455 (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
14456 ;; This function borrows from Ganesh Swami's latex2png.el
14457 (defun org-create-formula-image (string tofile options buffer)
14458 "This calls dvipng."
14459 (require 'org-latex)
14460 (let* ((tmpdir (if (featurep 'xemacs)
14461 (temp-directory)
14462 temporary-file-directory))
14463 (texfilebase (make-temp-name
14464 (expand-file-name "orgtex" tmpdir)))
14465 (texfile (concat texfilebase ".tex"))
14466 (dvifile (concat texfilebase ".dvi"))
14467 (pngfile (concat texfilebase ".png"))
14468 (fnh (if (featurep 'xemacs)
14469 (font-height (get-face-font 'default))
14470 (face-attribute 'default :height nil)))
14471 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
14472 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
14473 (fg (or (plist-get options (if buffer :foreground :html-foreground))
14474 "Black"))
14475 (bg (or (plist-get options (if buffer :background :html-background))
14476 "Transparent")))
14477 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
14478 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
14479 (with-temp-file texfile
14480 (insert org-format-latex-header
14481 (if org-export-latex-packages-alist
14482 (concat "\n"
14483 (mapconcat (lambda(p)
14484 (if (equal "" (car p))
14485 (format "\\usepackage{%s}" (cadr p))
14486 (format "\\usepackage[%s]{%s}"
14487 (car p) (cadr p))))
14488 org-export-latex-packages-alist "\n"))
14490 "\n\\begin{document}\n" string "\n\\end{document}\n"))
14491 (let ((dir default-directory))
14492 (condition-case nil
14493 (progn
14494 (cd tmpdir)
14495 (call-process "latex" nil nil nil texfile))
14496 (error nil))
14497 (cd dir))
14498 (if (not (file-exists-p dvifile))
14499 (progn (message "Failed to create dvi file from %s" texfile) nil)
14500 (condition-case nil
14501 (call-process "dvipng" nil nil nil
14502 "-fg" fg "-bg" bg
14503 "-D" dpi
14504 ;;"-x" scale "-y" scale
14505 "-T" "tight"
14506 "-o" pngfile
14507 dvifile)
14508 (error nil))
14509 (if (not (file-exists-p pngfile))
14510 (progn (message "Failed to create png file from %s" texfile) nil)
14511 ;; Use the requested file name and clean up
14512 (copy-file pngfile tofile 'replace)
14513 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
14514 (delete-file (concat texfilebase e)))
14515 pngfile))))
14517 (defun org-dvipng-color (attr)
14518 "Return an rgb color specification for dvipng."
14519 (apply 'format "rgb %s %s %s"
14520 (mapcar 'org-normalize-color
14521 (color-values (face-attribute 'default attr nil)))))
14523 (defun org-normalize-color (value)
14524 "Return string to be used as color value for an RGB component."
14525 (format "%g" (/ value 65535.0)))
14527 ;;;; Key bindings
14529 ;; Make `C-c C-x' a prefix key
14530 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
14532 ;; TAB key with modifiers
14533 (org-defkey org-mode-map "\C-i" 'org-cycle)
14534 (org-defkey org-mode-map [(tab)] 'org-cycle)
14535 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
14536 (org-defkey org-mode-map [(meta tab)] 'org-complete)
14537 (org-defkey org-mode-map "\M-\t" 'org-complete)
14538 (org-defkey org-mode-map "\M-\C-i" 'org-complete)
14539 ;; The following line is necessary under Suse GNU/Linux
14540 (unless (featurep 'xemacs)
14541 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
14542 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
14543 (define-key org-mode-map [backtab] 'org-shifttab)
14545 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
14546 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
14547 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
14549 ;; Cursor keys with modifiers
14550 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
14551 (org-defkey org-mode-map [(meta right)] 'org-metaright)
14552 (org-defkey org-mode-map [(meta up)] 'org-metaup)
14553 (org-defkey org-mode-map [(meta down)] 'org-metadown)
14555 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
14556 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
14557 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
14558 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
14560 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
14561 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
14562 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
14563 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
14565 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
14566 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
14568 ;;; Extra keys for tty access.
14569 ;; We only set them when really needed because otherwise the
14570 ;; menus don't show the simple keys
14572 (when (or org-use-extra-keys
14573 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
14574 (not window-system))
14575 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
14576 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
14577 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
14578 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
14579 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
14580 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
14581 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
14582 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
14583 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
14584 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
14585 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
14586 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
14587 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
14588 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
14589 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
14590 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
14591 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
14592 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
14593 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
14594 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
14595 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
14596 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
14597 (org-defkey org-mode-map [?\e (tab)] 'org-complete)
14598 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
14599 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
14600 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
14601 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
14602 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
14604 ;; All the other keys
14606 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
14607 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
14608 (if (boundp 'narrow-map)
14609 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
14610 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
14611 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
14612 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
14613 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
14614 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
14615 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
14616 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
14617 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
14618 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
14619 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
14620 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
14621 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
14622 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
14623 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
14624 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
14625 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
14626 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
14627 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
14628 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
14629 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
14630 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
14631 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
14632 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
14633 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
14634 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
14635 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
14636 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
14637 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
14638 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
14639 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
14640 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
14641 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
14642 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
14643 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
14644 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
14645 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
14646 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
14647 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
14648 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
14649 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
14650 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
14651 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
14652 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
14653 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
14654 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
14655 (org-defkey org-mode-map "\C-c^" 'org-sort)
14656 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
14657 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
14658 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
14659 (org-defkey org-mode-map "\C-m" 'org-return)
14660 (org-defkey org-mode-map "\C-j" 'org-return-indent)
14661 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
14662 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
14663 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
14664 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
14665 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
14666 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
14667 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
14668 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
14669 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
14670 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
14671 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
14672 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
14673 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
14674 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
14675 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
14676 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
14677 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
14678 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
14679 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
14680 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
14682 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
14683 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
14684 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
14685 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
14687 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
14688 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
14689 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
14690 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
14691 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
14692 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
14693 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
14694 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
14695 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
14696 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
14697 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
14698 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
14699 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
14700 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
14701 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
14703 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
14704 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
14705 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
14706 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
14708 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
14710 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
14712 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
14713 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
14715 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
14718 (when (featurep 'xemacs)
14719 (org-defkey org-mode-map 'button3 'popup-mode-menu))
14722 (defconst org-speed-commands-default
14724 ("a" . org-agenda)
14725 ("/" . org-sparse-tree)
14726 (";" . org-set-tags-command)
14727 ("I" . org-clock-in)
14728 ("O" . org-clock-out)
14729 ("c" . org-cycle)
14730 ("C" . org-shifttab)
14731 ("n" . outline-next-visible-heading)
14732 ("p" . outline-previous-visible-heading)
14733 ("f" . org-forward-same-level)
14734 ("b" . org-backward-same-level)
14735 ("u" . outline-up-heading)
14736 ("U" . org-shiftmetaup)
14737 ("D" . org-shiftmetadown)
14738 ("r" . org-metaright)
14739 ("l" . org-metaleft)
14740 ("R" . org-shiftmetaright)
14741 ("L" . org-shiftmetaleft)
14742 ("i" . (progn (forward-char 1) (call-interactively
14743 'org-insert-heading-respect-content)))
14744 ("o" . org-open-at-point)
14745 ("t" . org-todo)
14746 ("j" . org-goto)
14747 ("g" . (org-refile t))
14748 ("e" . org-set-effort)
14749 ("0" . (org-priority ?\ ))
14750 ("1" . (org-priority ?A))
14751 ("2" . (org-priority ?B))
14752 ("3" . (org-priority ?C))
14753 ("." . outline-mark-subtree)
14754 ("^" . org-sort)
14755 ("w" . org-refile)
14756 ("a" . org-archive-subtree-default-with-confirmation)
14757 ("/" . org-sparse-tree)
14758 ("?" . org-speed-command-help)
14760 "The default speed commands.")
14762 (defun org-print-speed-command (e)
14763 (princ (car e))
14764 (princ " ")
14765 (if (symbolp (cdr e))
14766 (princ (symbol-name (cdr e)))
14767 (prin1 (cdr e)))
14768 (princ "\n"))
14770 (defun org-speed-command-help ()
14771 "Show the available speed commands."
14772 (interactive)
14773 (if (not org-use-speed-commands)
14774 (error "Speed commands are not activated, customize `org-use-speed-commands'.")
14775 (with-output-to-temp-buffer "*Help*"
14776 (princ "Speed commands\n==============\n")
14777 (mapc 'org-print-speed-command org-speed-commands-user)
14778 (princ "\n")
14779 (mapc 'org-print-speed-command org-speed-commands-default))))
14781 (defvar org-self-insert-command-undo-counter 0)
14783 (defvar org-table-auto-blank-field) ; defined in org-table.el
14784 (defvar org-speed-command nil)
14785 (defun org-self-insert-command (N)
14786 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
14787 If the cursor is in a table looking at whitespace, the whitespace is
14788 overwritten, and the table is not marked as requiring realignment."
14789 (interactive "p")
14790 (cond
14791 ((and org-use-speed-commands
14792 (bolp)
14793 (looking-at outline-regexp)
14794 (setq
14795 org-speed-command
14796 (or (cdr (assoc (this-command-keys) org-speed-commands-user))
14797 (cdr (assoc (this-command-keys) org-speed-commands-default)))))
14798 (cond
14799 ((commandp org-speed-command)
14800 (setq this-command org-speed-command)
14801 (call-interactively org-speed-command))
14802 ((functionp org-speed-command)
14803 (funcall org-speed-command))
14804 ((and org-speed-command (listp org-speed-command))
14805 (eval org-speed-command))
14806 (t (let (org-use-speed-commands)
14807 (call-interactively 'org-self-insert-command)))))
14808 ((and
14809 (org-table-p)
14810 (progn
14811 ;; check if we blank the field, and if that triggers align
14812 (and (featurep 'org-table) org-table-auto-blank-field
14813 (member last-command
14814 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
14815 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
14816 ;; got extra space, this field does not determine column width
14817 (let (org-table-may-need-update) (org-table-blank-field))
14818 ;; no extra space, this field may determine column width
14819 (org-table-blank-field)))
14821 (eq N 1)
14822 (looking-at "[^|\n]* |"))
14823 (let (org-table-may-need-update)
14824 (goto-char (1- (match-end 0)))
14825 (delete-backward-char 1)
14826 (goto-char (match-beginning 0))
14827 (self-insert-command N)))
14829 (setq org-table-may-need-update t)
14830 (self-insert-command N)
14831 (org-fix-tags-on-the-fly)
14832 (if org-self-insert-cluster-for-undo
14833 (if (not (eq last-command 'org-self-insert-command))
14834 (setq org-self-insert-command-undo-counter 1)
14835 (if (>= org-self-insert-command-undo-counter 20)
14836 (setq org-self-insert-command-undo-counter 1)
14837 (and (> org-self-insert-command-undo-counter 0)
14838 buffer-undo-list
14839 (not (cadr buffer-undo-list)) ; remove nil entry
14840 (setcdr buffer-undo-list (cddr buffer-undo-list)))
14841 (setq org-self-insert-command-undo-counter
14842 (1+ org-self-insert-command-undo-counter))))))))
14844 (defun org-fix-tags-on-the-fly ()
14845 (when (and (equal (char-after (point-at-bol)) ?*)
14846 (org-on-heading-p))
14847 (org-align-tags-here org-tags-column)))
14849 (defun org-delete-backward-char (N)
14850 "Like `delete-backward-char', insert whitespace at field end in tables.
14851 When deleting backwards, in tables this function will insert whitespace in
14852 front of the next \"|\" separator, to keep the table aligned. The table will
14853 still be marked for re-alignment if the field did fill the entire column,
14854 because, in this case the deletion might narrow the column."
14855 (interactive "p")
14856 (if (and (org-table-p)
14857 (eq N 1)
14858 (string-match "|" (buffer-substring (point-at-bol) (point)))
14859 (looking-at ".*?|"))
14860 (let ((pos (point))
14861 (noalign (looking-at "[^|\n\r]* |"))
14862 (c org-table-may-need-update))
14863 (backward-delete-char N)
14864 (skip-chars-forward "^|")
14865 (insert " ")
14866 (goto-char (1- pos))
14867 ;; noalign: if there were two spaces at the end, this field
14868 ;; does not determine the width of the column.
14869 (if noalign (setq org-table-may-need-update c)))
14870 (backward-delete-char N)
14871 (org-fix-tags-on-the-fly)))
14873 (defun org-delete-char (N)
14874 "Like `delete-char', but insert whitespace at field end in tables.
14875 When deleting characters, in tables this function will insert whitespace in
14876 front of the next \"|\" separator, to keep the table aligned. The table will
14877 still be marked for re-alignment if the field did fill the entire column,
14878 because, in this case the deletion might narrow the column."
14879 (interactive "p")
14880 (if (and (org-table-p)
14881 (not (bolp))
14882 (not (= (char-after) ?|))
14883 (eq N 1))
14884 (if (looking-at ".*?|")
14885 (let ((pos (point))
14886 (noalign (looking-at "[^|\n\r]* |"))
14887 (c org-table-may-need-update))
14888 (replace-match (concat
14889 (substring (match-string 0) 1 -1)
14890 " |"))
14891 (goto-char pos)
14892 ;; noalign: if there were two spaces at the end, this field
14893 ;; does not determine the width of the column.
14894 (if noalign (setq org-table-may-need-update c)))
14895 (delete-char N))
14896 (delete-char N)
14897 (org-fix-tags-on-the-fly)))
14899 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
14900 (put 'org-self-insert-command 'delete-selection t)
14901 (put 'orgtbl-self-insert-command 'delete-selection t)
14902 (put 'org-delete-char 'delete-selection 'supersede)
14903 (put 'org-delete-backward-char 'delete-selection 'supersede)
14904 (put 'org-yank 'delete-selection 'yank)
14906 ;; Make `flyspell-mode' delay after some commands
14907 (put 'org-self-insert-command 'flyspell-delayed t)
14908 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
14909 (put 'org-delete-char 'flyspell-delayed t)
14910 (put 'org-delete-backward-char 'flyspell-delayed t)
14912 ;; Make pabbrev-mode expand after org-mode commands
14913 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
14914 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
14916 ;; How to do this: Measure non-white length of current string
14917 ;; If equal to column width, we should realign.
14919 (defun org-remap (map &rest commands)
14920 "In MAP, remap the functions given in COMMANDS.
14921 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
14922 (let (new old)
14923 (while commands
14924 (setq old (pop commands) new (pop commands))
14925 (if (fboundp 'command-remapping)
14926 (org-defkey map (vector 'remap old) new)
14927 (substitute-key-definition old new map global-map)))))
14929 (when (eq org-enable-table-editor 'optimized)
14930 ;; If the user wants maximum table support, we need to hijack
14931 ;; some standard editing functions
14932 (org-remap org-mode-map
14933 'self-insert-command 'org-self-insert-command
14934 'delete-char 'org-delete-char
14935 'delete-backward-char 'org-delete-backward-char)
14936 (org-defkey org-mode-map "|" 'org-force-self-insert))
14938 (defvar org-ctrl-c-ctrl-c-hook nil
14939 "Hook for functions attaching themselves to `C-c C-c'.
14940 This can be used to add additional functionality to the C-c C-c key which
14941 executes context-dependent commands.
14942 Each function will be called with no arguments. The function must check
14943 if the context is appropriate for it to act. If yes, it should do its
14944 thing and then return a non-nil value. If the context is wrong,
14945 just do nothing and return nil.")
14947 (defvar org-tab-first-hook nil
14948 "Hook for functions to attach themselves to TAB.
14949 See `org-ctrl-c-ctrl-c-hook' for more information.
14950 This hook runs as the first action when TAB is pressed, even before
14951 `org-cycle' messes around with the `outline-regexp' to cater for
14952 inline tasks and plain list item folding.
14953 If any function in this hook returns t, not other actions like table
14954 field motion visibility cycling will be done.")
14956 (defvar org-tab-after-check-for-table-hook nil
14957 "Hook for functions to attach themselves to TAB.
14958 See `org-ctrl-c-ctrl-c-hook' for more information.
14959 This hook runs after it has been established that the cursor is not in a
14960 table, but before checking if the cursor is in a headline or if global cycling
14961 should be done.
14962 If any function in this hook returns t, not other actions like visibility
14963 cycling will be done.")
14965 (defvar org-tab-after-check-for-cycling-hook nil
14966 "Hook for functions to attach themselves to TAB.
14967 See `org-ctrl-c-ctrl-c-hook' for more information.
14968 This hook runs after it has been established that not table field motion and
14969 not visibility should be done because of current context. This is probably
14970 the place where a package like yasnippets can hook in.")
14972 (defvar org-tab-before-tab-emulation-hook nil
14973 "Hook for functions to attach themselves to TAB.
14974 See `org-ctrl-c-ctrl-c-hook' for more information.
14975 This hook runs after every other options for TAB have been exhausted, but
14976 before indentation and \t insertion takes place.")
14978 (defvar org-metaleft-hook nil
14979 "Hook for functions attaching themselves to `M-left'.
14980 See `org-ctrl-c-ctrl-c-hook' for more information.")
14981 (defvar org-metaright-hook nil
14982 "Hook for functions attaching themselves to `M-right'.
14983 See `org-ctrl-c-ctrl-c-hook' for more information.")
14984 (defvar org-metaup-hook nil
14985 "Hook for functions attaching themselves to `M-up'.
14986 See `org-ctrl-c-ctrl-c-hook' for more information.")
14987 (defvar org-metadown-hook nil
14988 "Hook for functions attaching themselves to `M-down'.
14989 See `org-ctrl-c-ctrl-c-hook' for more information.")
14990 (defvar org-shiftmetaleft-hook nil
14991 "Hook for functions attaching themselves to `M-S-left'.
14992 See `org-ctrl-c-ctrl-c-hook' for more information.")
14993 (defvar org-shiftmetaright-hook nil
14994 "Hook for functions attaching themselves to `M-S-right'.
14995 See `org-ctrl-c-ctrl-c-hook' for more information.")
14996 (defvar org-shiftmetaup-hook nil
14997 "Hook for functions attaching themselves to `M-S-up'.
14998 See `org-ctrl-c-ctrl-c-hook' for more information.")
14999 (defvar org-shiftmetadown-hook nil
15000 "Hook for functions attaching themselves to `M-S-down'.
15001 See `org-ctrl-c-ctrl-c-hook' for more information.")
15002 (defvar org-metareturn-hook nil
15003 "Hook for functions attaching themselves to `M-RET'.
15004 See `org-ctrl-c-ctrl-c-hook' for more information.")
15006 (defun org-modifier-cursor-error ()
15007 "Throw an error, a modified cursor command was applied in wrong context."
15008 (error "This command is active in special context like tables, headlines or items"))
15010 (defun org-shiftselect-error ()
15011 "Throw an error because Shift-Cursor command was applied in wrong context."
15012 (if (and (boundp 'shift-select-mode) shift-select-mode)
15013 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
15014 (error "This command works only in special context like headlines or timestamps")))
15016 (defun org-call-for-shift-select (cmd)
15017 (let ((this-command-keys-shift-translated t))
15018 (call-interactively cmd)))
15020 (defun org-shifttab (&optional arg)
15021 "Global visibility cycling or move to previous table field.
15022 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
15023 on context.
15024 See the individual commands for more information."
15025 (interactive "P")
15026 (cond
15027 ((org-at-table-p) (call-interactively 'org-table-previous-field))
15028 ((integerp arg)
15029 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
15030 (message "Content view to level: %d" arg)
15031 (org-content (prefix-numeric-value arg2))
15032 (setq org-cycle-global-status 'overview)))
15033 (t (call-interactively 'org-global-cycle))))
15035 (defun org-shiftmetaleft ()
15036 "Promote subtree or delete table column.
15037 Calls `org-promote-subtree', `org-outdent-item',
15038 or `org-table-delete-column', depending on context.
15039 See the individual commands for more information."
15040 (interactive)
15041 (cond
15042 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
15043 ((org-at-table-p) (call-interactively 'org-table-delete-column))
15044 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
15045 ((org-at-item-p) (call-interactively 'org-outdent-item))
15046 (t (org-modifier-cursor-error))))
15048 (defun org-shiftmetaright ()
15049 "Demote subtree or insert table column.
15050 Calls `org-demote-subtree', `org-indent-item',
15051 or `org-table-insert-column', depending on context.
15052 See the individual commands for more information."
15053 (interactive)
15054 (cond
15055 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
15056 ((org-at-table-p) (call-interactively 'org-table-insert-column))
15057 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
15058 ((org-at-item-p) (call-interactively 'org-indent-item))
15059 (t (org-modifier-cursor-error))))
15061 (defun org-shiftmetaup (&optional arg)
15062 "Move subtree up or kill table row.
15063 Calls `org-move-subtree-up' or `org-table-kill-row' or
15064 `org-move-item-up' depending on context. See the individual commands
15065 for more information."
15066 (interactive "P")
15067 (cond
15068 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
15069 ((org-at-table-p) (call-interactively 'org-table-kill-row))
15070 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
15071 ((org-at-item-p) (call-interactively 'org-move-item-up))
15072 (t (org-modifier-cursor-error))))
15074 (defun org-shiftmetadown (&optional arg)
15075 "Move subtree down or insert table row.
15076 Calls `org-move-subtree-down' or `org-table-insert-row' or
15077 `org-move-item-down', depending on context. See the individual
15078 commands for more information."
15079 (interactive "P")
15080 (cond
15081 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
15082 ((org-at-table-p) (call-interactively 'org-table-insert-row))
15083 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
15084 ((org-at-item-p) (call-interactively 'org-move-item-down))
15085 (t (org-modifier-cursor-error))))
15087 (defun org-metaleft (&optional arg)
15088 "Promote heading or move table column to left.
15089 Calls `org-do-promote' or `org-table-move-column', depending on context.
15090 With no specific context, calls the Emacs default `backward-word'.
15091 See the individual commands for more information."
15092 (interactive "P")
15093 (cond
15094 ((run-hook-with-args-until-success 'org-metaleft-hook))
15095 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
15096 ((or (org-on-heading-p)
15097 (and (org-region-active-p)
15098 (save-excursion
15099 (goto-char (region-beginning))
15100 (org-on-heading-p))))
15101 (call-interactively 'org-do-promote))
15102 ((or (org-at-item-p)
15103 (and (org-region-active-p)
15104 (save-excursion
15105 (goto-char (region-beginning))
15106 (org-at-item-p))))
15107 (call-interactively 'org-outdent-item))
15108 (t (call-interactively 'backward-word))))
15110 (defun org-metaright (&optional arg)
15111 "Demote subtree or move table column to right.
15112 Calls `org-do-demote' or `org-table-move-column', depending on context.
15113 With no specific context, calls the Emacs default `forward-word'.
15114 See the individual commands for more information."
15115 (interactive "P")
15116 (cond
15117 ((run-hook-with-args-until-success 'org-metaright-hook))
15118 ((org-at-table-p) (call-interactively 'org-table-move-column))
15119 ((or (org-on-heading-p)
15120 (and (org-region-active-p)
15121 (save-excursion
15122 (goto-char (region-beginning))
15123 (org-on-heading-p))))
15124 (call-interactively 'org-do-demote))
15125 ((or (org-at-item-p)
15126 (and (org-region-active-p)
15127 (save-excursion
15128 (goto-char (region-beginning))
15129 (org-at-item-p))))
15130 (call-interactively 'org-indent-item))
15131 (t (call-interactively 'forward-word))))
15133 (defun org-metaup (&optional arg)
15134 "Move subtree up or move table row up.
15135 Calls `org-move-subtree-up' or `org-table-move-row' or
15136 `org-move-item-up', depending on context. See the individual commands
15137 for more information."
15138 (interactive "P")
15139 (cond
15140 ((run-hook-with-args-until-success 'org-metaup-hook))
15141 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
15142 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
15143 ((org-at-item-p) (call-interactively 'org-move-item-up))
15144 (t (transpose-lines 1) (beginning-of-line -1))))
15146 (defun org-metadown (&optional arg)
15147 "Move subtree down or move table row down.
15148 Calls `org-move-subtree-down' or `org-table-move-row' or
15149 `org-move-item-down', depending on context. See the individual
15150 commands for more information."
15151 (interactive "P")
15152 (cond
15153 ((run-hook-with-args-until-success 'org-metadown-hook))
15154 ((org-at-table-p) (call-interactively 'org-table-move-row))
15155 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
15156 ((org-at-item-p) (call-interactively 'org-move-item-down))
15157 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
15159 (defun org-shiftup (&optional arg)
15160 "Increase item in timestamp or increase priority of current headline.
15161 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
15162 depending on context. See the individual commands for more information."
15163 (interactive "P")
15164 (cond
15165 ((and org-support-shift-select (org-region-active-p))
15166 (org-call-for-shift-select 'previous-line))
15167 ((org-at-timestamp-p t)
15168 (call-interactively (if org-edit-timestamp-down-means-later
15169 'org-timestamp-down 'org-timestamp-up)))
15170 ((and (not (eq org-support-shift-select 'always))
15171 org-enable-priority-commands
15172 (org-on-heading-p))
15173 (call-interactively 'org-priority-up))
15174 ((and (not org-support-shift-select) (org-at-item-p))
15175 (call-interactively 'org-previous-item))
15176 ((org-clocktable-try-shift 'up arg))
15177 (org-support-shift-select
15178 (org-call-for-shift-select 'previous-line))
15179 (t (org-shiftselect-error))))
15181 (defun org-shiftdown (&optional arg)
15182 "Decrease item in timestamp or decrease priority of current headline.
15183 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
15184 depending on context. See the individual commands for more information."
15185 (interactive "P")
15186 (cond
15187 ((and org-support-shift-select (org-region-active-p))
15188 (org-call-for-shift-select 'next-line))
15189 ((org-at-timestamp-p t)
15190 (call-interactively (if org-edit-timestamp-down-means-later
15191 'org-timestamp-up 'org-timestamp-down)))
15192 ((and (not (eq org-support-shift-select 'always))
15193 org-enable-priority-commands
15194 (org-on-heading-p))
15195 (call-interactively 'org-priority-down))
15196 ((and (not org-support-shift-select) (org-at-item-p))
15197 (call-interactively 'org-next-item))
15198 ((org-clocktable-try-shift 'down arg))
15199 (org-support-shift-select
15200 (org-call-for-shift-select 'next-line))
15201 (t (org-shiftselect-error))))
15203 (defun org-shiftright (&optional arg)
15204 "Cycle the thing at point or in the current line, depending on context.
15205 Depending on context, this does one of the following:
15207 - switch a timestamp at point one day into the future
15208 - on a headline, switch to the next TODO keyword.
15209 - on an item, switch entire list to the next bullet type
15210 - on a property line, switch to the next allowed value
15211 - on a clocktable definition line, move time block into the future"
15212 (interactive "P")
15213 (cond
15214 ((and org-support-shift-select (org-region-active-p))
15215 (org-call-for-shift-select 'forward-char))
15216 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
15217 ((and (not (eq org-support-shift-select 'always))
15218 (org-on-heading-p))
15219 (let ((org-inhibit-logging
15220 (not org-treat-S-cursor-todo-selection-as-state-change))
15221 (org-inhibit-blocking
15222 (not org-treat-S-cursor-todo-selection-as-state-change)))
15223 (org-call-with-arg 'org-todo 'right)))
15224 ((or (and org-support-shift-select
15225 (not (eq org-support-shift-select 'always))
15226 (org-at-item-bullet-p))
15227 (and (not org-support-shift-select) (org-at-item-p)))
15228 (org-call-with-arg 'org-cycle-list-bullet nil))
15229 ((and (not (eq org-support-shift-select 'always))
15230 (org-at-property-p))
15231 (call-interactively 'org-property-next-allowed-value))
15232 ((org-clocktable-try-shift 'right arg))
15233 (org-support-shift-select
15234 (org-call-for-shift-select 'forward-char))
15235 (t (org-shiftselect-error))))
15237 (defun org-shiftleft (&optional arg)
15238 "Cycle the thing at point or in the current line, depending on context.
15239 Depending on context, this does one of the following:
15241 - switch a timestamp at point one day into the past
15242 - on a headline, switch to the previous TODO keyword.
15243 - on an item, switch entire list to the previous bullet type
15244 - on a property line, switch to the previous allowed value
15245 - on a clocktable definition line, move time block into the past"
15246 (interactive "P")
15247 (cond
15248 ((and org-support-shift-select (org-region-active-p))
15249 (org-call-for-shift-select 'backward-char))
15250 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
15251 ((and (not (eq org-support-shift-select 'always))
15252 (org-on-heading-p))
15253 (let ((org-inhibit-logging
15254 (not org-treat-S-cursor-todo-selection-as-state-change))
15255 (org-inhibit-blocking
15256 (not org-treat-S-cursor-todo-selection-as-state-change)))
15257 (org-call-with-arg 'org-todo 'left)))
15258 ((or (and org-support-shift-select
15259 (not (eq org-support-shift-select 'always))
15260 (org-at-item-bullet-p))
15261 (and (not org-support-shift-select) (org-at-item-p)))
15262 (org-call-with-arg 'org-cycle-list-bullet 'previous))
15263 ((and (not (eq org-support-shift-select 'always))
15264 (org-at-property-p))
15265 (call-interactively 'org-property-previous-allowed-value))
15266 ((org-clocktable-try-shift 'left arg))
15267 (org-support-shift-select
15268 (org-call-for-shift-select 'backward-char))
15269 (t (org-shiftselect-error))))
15271 (defun org-shiftcontrolright ()
15272 "Switch to next TODO set."
15273 (interactive)
15274 (cond
15275 ((and org-support-shift-select (org-region-active-p))
15276 (org-call-for-shift-select 'forward-word))
15277 ((and (not (eq org-support-shift-select 'always))
15278 (org-on-heading-p))
15279 (org-call-with-arg 'org-todo 'nextset))
15280 (org-support-shift-select
15281 (org-call-for-shift-select 'forward-word))
15282 (t (org-shiftselect-error))))
15284 (defun org-shiftcontrolleft ()
15285 "Switch to previous TODO set."
15286 (interactive)
15287 (cond
15288 ((and org-support-shift-select (org-region-active-p))
15289 (org-call-for-shift-select 'backward-word))
15290 ((and (not (eq org-support-shift-select 'always))
15291 (org-on-heading-p))
15292 (org-call-with-arg 'org-todo 'previousset))
15293 (org-support-shift-select
15294 (org-call-for-shift-select 'backward-word))
15295 (t (org-shiftselect-error))))
15297 (defun org-ctrl-c-ret ()
15298 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
15299 (interactive)
15300 (cond
15301 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
15302 (t (call-interactively 'org-insert-heading))))
15304 (defun org-copy-special ()
15305 "Copy region in table or copy current subtree.
15306 Calls `org-table-copy' or `org-copy-subtree', depending on context.
15307 See the individual commands for more information."
15308 (interactive)
15309 (call-interactively
15310 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
15312 (defun org-cut-special ()
15313 "Cut region in table or cut current subtree.
15314 Calls `org-table-copy' or `org-cut-subtree', depending on context.
15315 See the individual commands for more information."
15316 (interactive)
15317 (call-interactively
15318 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
15320 (defun org-paste-special (arg)
15321 "Paste rectangular region into table, or past subtree relative to level.
15322 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
15323 See the individual commands for more information."
15324 (interactive "P")
15325 (if (org-at-table-p)
15326 (org-table-paste-rectangle)
15327 (org-paste-subtree arg)))
15329 (defun org-edit-special ()
15330 "Call a special editor for the stuff at point.
15331 When at a table, call the formula editor with `org-table-edit-formulas'.
15332 When at the first line of an src example, call `org-edit-src-code'.
15333 When in an #+include line, visit the include file. Otherwise call
15334 `ffap' to visit the file at point."
15335 (interactive)
15336 (cond
15337 ((org-at-table-p)
15338 (call-interactively 'org-table-edit-formulas))
15339 ((save-excursion
15340 (beginning-of-line 1)
15341 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
15342 (find-file (org-trim (match-string 1))))
15343 ((org-edit-src-code))
15344 ((org-edit-fixed-width-region))
15345 (t (call-interactively 'ffap))))
15348 (defun org-ctrl-c-ctrl-c (&optional arg)
15349 "Set tags in headline, or update according to changed information at point.
15351 This command does many different things, depending on context:
15353 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
15354 this is what we do.
15356 - If the cursor is on a statistics cookie, update it.
15358 - If the cursor is in a headline, prompt for tags and insert them
15359 into the current line, aligned to `org-tags-column'. When called
15360 with prefix arg, realign all tags in the current buffer.
15362 - If the cursor is in one of the special #+KEYWORD lines, this
15363 triggers scanning the buffer for these lines and updating the
15364 information.
15366 - If the cursor is inside a table, realign the table. This command
15367 works even if the automatic table editor has been turned off.
15369 - If the cursor is on a #+TBLFM line, re-apply the formulas to
15370 the entire table.
15372 - If the cursor is at a footnote reference or definition, jump to
15373 the corresponding definition or references, respectively.
15375 - If the cursor is a the beginning of a dynamic block, update it.
15377 - If the cursor is inside a table created by the table.el package,
15378 activate that table.
15380 - If the current buffer is a remember buffer, close note and file
15381 it. A prefix argument of 1 files to the default location
15382 without further interaction. A prefix argument of 2 files to
15383 the currently clocking task.
15385 - If the cursor is on a <<<target>>>, update radio targets and corresponding
15386 links in this buffer.
15388 - If the cursor is on a numbered item in a plain list, renumber the
15389 ordered list.
15391 - If the cursor is on a checkbox, toggle it."
15392 (interactive "P")
15393 (let ((org-enable-table-editor t))
15394 (cond
15395 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
15396 org-occur-highlights
15397 org-latex-fragment-image-overlays)
15398 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
15399 (org-remove-occur-highlights)
15400 (org-remove-latex-fragment-image-overlays)
15401 (message "Temporary highlights/overlays removed from current buffer"))
15402 ((and (local-variable-p 'org-finish-function (current-buffer))
15403 (fboundp org-finish-function))
15404 (funcall org-finish-function))
15405 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
15406 ((org-at-property-p)
15407 (call-interactively 'org-property-action))
15408 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
15409 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
15410 (or (org-on-heading-p) (org-at-item-p)))
15411 (call-interactively 'org-update-statistics-cookies))
15412 ((org-on-heading-p) (call-interactively 'org-set-tags))
15413 ((org-at-table.el-p)
15414 (require 'table)
15415 (beginning-of-line 1)
15416 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
15417 (call-interactively 'table-recognize-table))
15418 ((org-at-table-p)
15419 (org-table-maybe-eval-formula)
15420 (if arg
15421 (call-interactively 'org-table-recalculate)
15422 (org-table-maybe-recalculate-line))
15423 (call-interactively 'org-table-align))
15424 ((or (org-footnote-at-reference-p)
15425 (org-footnote-at-definition-p))
15426 (call-interactively 'org-footnote-action))
15427 ((org-at-item-checkbox-p)
15428 (call-interactively 'org-toggle-checkbox))
15429 ((org-at-item-p)
15430 (if arg
15431 (call-interactively 'org-toggle-checkbox)
15432 (call-interactively 'org-maybe-renumber-ordered-list)))
15433 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
15434 ;; Dynamic block
15435 (beginning-of-line 1)
15436 (save-excursion (org-update-dblock)))
15437 ((save-excursion
15438 (beginning-of-line 1)
15439 (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
15440 (cond
15441 ((equal (match-string 1) "TBLFM")
15442 ;; Recalculate the table before this line
15443 (save-excursion
15444 (beginning-of-line 1)
15445 (skip-chars-backward " \r\n\t")
15446 (if (org-at-table-p)
15447 (org-call-with-arg 'org-table-recalculate (or arg t)))))
15449 ; (org-set-regexps-and-options)
15450 ; (org-restart-font-lock)
15451 (let ((org-inhibit-startup t)) (org-mode-restart))
15452 (message "Local setup has been refreshed"))))
15453 ((org-clock-update-time-maybe))
15454 (t (error "C-c C-c can do nothing useful at this location")))))
15456 (defun org-mode-restart ()
15457 "Restart Org-mode, to scan again for special lines.
15458 Also updates the keyword regular expressions."
15459 (interactive)
15460 (org-mode)
15461 (message "Org-mode restarted"))
15463 (defun org-kill-note-or-show-branches ()
15464 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
15465 (interactive)
15466 (if (not org-finish-function)
15467 (call-interactively 'show-branches)
15468 (let ((org-note-abort t))
15469 (funcall org-finish-function))))
15471 (defun org-return (&optional indent)
15472 "Goto next table row or insert a newline.
15473 Calls `org-table-next-row' or `newline', depending on context.
15474 See the individual commands for more information."
15475 (interactive)
15476 (cond
15477 ((bobp) (if indent (newline-and-indent) (newline)))
15478 ((org-at-table-p)
15479 (org-table-justify-field-maybe)
15480 (call-interactively 'org-table-next-row))
15481 ((and org-return-follows-link
15482 (eq (get-text-property (point) 'face) 'org-link))
15483 (call-interactively 'org-open-at-point))
15484 ((and (org-at-heading-p)
15485 (looking-at
15486 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
15487 (org-show-entry)
15488 (end-of-line 1)
15489 (newline))
15490 (t (if indent (newline-and-indent) (newline)))))
15492 (defun org-return-indent ()
15493 "Goto next table row or insert a newline and indent.
15494 Calls `org-table-next-row' or `newline-and-indent', depending on
15495 context. See the individual commands for more information."
15496 (interactive)
15497 (org-return t))
15499 (defun org-ctrl-c-star ()
15500 "Compute table, or change heading status of lines.
15501 Calls `org-table-recalculate' or `org-toggle-heading',
15502 depending on context."
15503 (interactive)
15504 (cond
15505 ((org-at-table-p)
15506 (call-interactively 'org-table-recalculate))
15508 ;; Convert all lines in region to list items
15509 (call-interactively 'org-toggle-heading))))
15511 (defun org-ctrl-c-minus ()
15512 "Insert separator line in table or modify bullet status of line.
15513 Also turns a plain line or a region of lines into list items.
15514 Calls `org-table-insert-hline', `org-toggle-item', or
15515 `org-cycle-list-bullet', depending on context."
15516 (interactive)
15517 (cond
15518 ((org-at-table-p)
15519 (call-interactively 'org-table-insert-hline))
15520 ((org-region-active-p)
15521 (call-interactively 'org-toggle-item))
15522 ((org-in-item-p)
15523 (call-interactively 'org-cycle-list-bullet))
15525 (call-interactively 'org-toggle-item))))
15527 (defun org-toggle-item ()
15528 "Convert headings or normal lines to items, items to normal lines.
15529 If there is no active region, only the current line is considered.
15531 If the first line in the region is a headline, convert all headlines to items.
15533 If the first line in the region is an item, convert all items to normal lines.
15535 If the first line is normal text, add an item bullet to each line."
15536 (interactive)
15537 (let (l2 l beg end)
15538 (if (org-region-active-p)
15539 (setq beg (region-beginning) end (region-end))
15540 (setq beg (point-at-bol)
15541 end (min (1+ (point-at-eol)) (point-max))))
15542 (save-excursion
15543 (goto-char end)
15544 (setq l2 (org-current-line))
15545 (goto-char beg)
15546 (beginning-of-line 1)
15547 (setq l (1- (org-current-line)))
15548 (if (org-at-item-p)
15549 ;; We already have items, de-itemize
15550 (while (< (setq l (1+ l)) l2)
15551 (when (org-at-item-p)
15552 (goto-char (match-beginning 2))
15553 (delete-region (match-beginning 2) (match-end 2))
15554 (and (looking-at "[ \t]+") (replace-match "")))
15555 (beginning-of-line 2))
15556 (if (org-on-heading-p)
15557 ;; Headings, convert to items
15558 (while (< (setq l (1+ l)) l2)
15559 (if (looking-at org-outline-regexp)
15560 (replace-match "- " t t))
15561 (beginning-of-line 2))
15562 ;; normal lines, turn them into items
15563 (while (< (setq l (1+ l)) l2)
15564 (unless (org-at-item-p)
15565 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
15566 (replace-match "\\1- \\2")))
15567 (beginning-of-line 2)))))))
15569 (defun org-toggle-heading (&optional nstars)
15570 "Convert headings to normal text, or items or text to headings.
15571 If there is no active region, only the current line is considered.
15573 If the first line is a heading, remove the stars from all headlines
15574 in the region.
15576 If the first line is a plain list item, turn all plain list items
15577 into headings.
15579 If the first line is a normal line, turn each and every line in the
15580 region into a heading.
15582 When converting a line into a heading, the number of stars is chosen
15583 such that the lines become children of the current entry. However,
15584 when a prefix argument is given, its value determines the number of
15585 stars to add."
15586 (interactive "P")
15587 (let (l2 l itemp beg end)
15588 (if (org-region-active-p)
15589 (setq beg (region-beginning) end (region-end))
15590 (setq beg (point-at-bol)
15591 end (min (1+ (point-at-eol)) (point-max))))
15592 (save-excursion
15593 (goto-char end)
15594 (setq l2 (org-current-line))
15595 (goto-char beg)
15596 (beginning-of-line 1)
15597 (setq l (1- (org-current-line)))
15598 (if (org-on-heading-p)
15599 ;; We already have headlines, de-star them
15600 (while (< (setq l (1+ l)) l2)
15601 (when (org-on-heading-p t)
15602 (and (looking-at outline-regexp) (replace-match "")))
15603 (beginning-of-line 2))
15604 (setq itemp (org-at-item-p))
15605 (let* ((stars
15606 (if nstars
15607 (make-string (prefix-numeric-value current-prefix-arg)
15609 (save-excursion
15610 (if (re-search-backward org-complex-heading-regexp nil t)
15611 (match-string 1) ""))))
15612 (add-stars (cond (nstars "")
15613 ((equal stars "") "*")
15614 (org-odd-levels-only "**")
15615 (t "*")))
15616 (rpl (concat stars add-stars " ")))
15617 (while (< (setq l (1+ l)) l2)
15618 (if itemp
15619 (and (org-at-item-p) (replace-match rpl t t))
15620 (unless (org-on-heading-p)
15621 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
15622 (replace-match (concat rpl (match-string 2))))))
15623 (beginning-of-line 2)))))))
15625 (defun org-meta-return (&optional arg)
15626 "Insert a new heading or wrap a region in a table.
15627 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
15628 See the individual commands for more information."
15629 (interactive "P")
15630 (cond
15631 ((run-hook-with-args-until-success 'org-metareturn-hook))
15632 ((org-at-table-p)
15633 (call-interactively 'org-table-wrap-region))
15634 (t (call-interactively 'org-insert-heading))))
15636 ;;; Menu entries
15638 ;; Define the Org-mode menus
15639 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
15640 '("Tbl"
15641 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
15642 ["Next Field" org-cycle (org-at-table-p)]
15643 ["Previous Field" org-shifttab (org-at-table-p)]
15644 ["Next Row" org-return (org-at-table-p)]
15645 "--"
15646 ["Blank Field" org-table-blank-field (org-at-table-p)]
15647 ["Edit Field" org-table-edit-field (org-at-table-p)]
15648 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
15649 "--"
15650 ("Column"
15651 ["Move Column Left" org-metaleft (org-at-table-p)]
15652 ["Move Column Right" org-metaright (org-at-table-p)]
15653 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
15654 ["Insert Column" org-shiftmetaright (org-at-table-p)])
15655 ("Row"
15656 ["Move Row Up" org-metaup (org-at-table-p)]
15657 ["Move Row Down" org-metadown (org-at-table-p)]
15658 ["Delete Row" org-shiftmetaup (org-at-table-p)]
15659 ["Insert Row" org-shiftmetadown (org-at-table-p)]
15660 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
15661 "--"
15662 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
15663 ("Rectangle"
15664 ["Copy Rectangle" org-copy-special (org-at-table-p)]
15665 ["Cut Rectangle" org-cut-special (org-at-table-p)]
15666 ["Paste Rectangle" org-paste-special (org-at-table-p)]
15667 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
15668 "--"
15669 ("Calculate"
15670 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
15671 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
15672 ["Edit Formulas" org-edit-special (org-at-table-p)]
15673 "--"
15674 ["Recalculate line" org-table-recalculate (org-at-table-p)]
15675 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
15676 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
15677 "--"
15678 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
15679 "--"
15680 ["Sum Column/Rectangle" org-table-sum
15681 (or (org-at-table-p) (org-region-active-p))]
15682 ["Which Column?" org-table-current-column (org-at-table-p)])
15683 ["Debug Formulas"
15684 org-table-toggle-formula-debugger
15685 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
15686 ["Show Col/Row Numbers"
15687 org-table-toggle-coordinate-overlays
15688 :style toggle
15689 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
15690 "--"
15691 ["Create" org-table-create (and (not (org-at-table-p))
15692 org-enable-table-editor)]
15693 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
15694 ["Import from File" org-table-import (not (org-at-table-p))]
15695 ["Export to File" org-table-export (org-at-table-p)]
15696 "--"
15697 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
15699 (easy-menu-define org-org-menu org-mode-map "Org menu"
15700 '("Org"
15701 ("Show/Hide"
15702 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
15703 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
15704 ["Sparse Tree..." org-sparse-tree t]
15705 ["Reveal Context" org-reveal t]
15706 ["Show All" show-all t]
15707 "--"
15708 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
15709 "--"
15710 ["New Heading" org-insert-heading t]
15711 ("Navigate Headings"
15712 ["Up" outline-up-heading t]
15713 ["Next" outline-next-visible-heading t]
15714 ["Previous" outline-previous-visible-heading t]
15715 ["Next Same Level" outline-forward-same-level t]
15716 ["Previous Same Level" outline-backward-same-level t]
15717 "--"
15718 ["Jump" org-goto t])
15719 ("Edit Structure"
15720 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
15721 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
15722 "--"
15723 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
15724 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
15725 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
15726 "--"
15727 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
15728 "--"
15729 ["Promote Heading" org-metaleft (not (org-at-table-p))]
15730 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
15731 ["Demote Heading" org-metaright (not (org-at-table-p))]
15732 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
15733 "--"
15734 ["Sort Region/Children" org-sort (not (org-at-table-p))]
15735 "--"
15736 ["Convert to odd levels" org-convert-to-odd-levels t]
15737 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
15738 ("Editing"
15739 ["Emphasis..." org-emphasize t]
15740 ["Edit Source Example" org-edit-special t]
15741 "--"
15742 ["Footnote new/jump" org-footnote-action t]
15743 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
15744 ("Archive"
15745 ["Archive (default method)" org-archive-subtree-default t]
15746 "--"
15747 ["Move Subtree to Archive file" org-advertized-archive-subtree t]
15748 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
15749 ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
15751 "--"
15752 ("Hyperlinks"
15753 ["Store Link (Global)" org-store-link t]
15754 ["Find existing link to here" org-occur-link-in-agenda-files t]
15755 ["Insert Link" org-insert-link t]
15756 ["Follow Link" org-open-at-point t]
15757 "--"
15758 ["Next link" org-next-link t]
15759 ["Previous link" org-previous-link t]
15760 "--"
15761 ["Descriptive Links"
15762 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
15763 :style radio
15764 :selected (member '(org-link) buffer-invisibility-spec)]
15765 ["Literal Links"
15766 (progn
15767 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
15768 :style radio
15769 :selected (not (member '(org-link) buffer-invisibility-spec))])
15770 "--"
15771 ("TODO Lists"
15772 ["TODO/DONE/-" org-todo t]
15773 ("Select keyword"
15774 ["Next keyword" org-shiftright (org-on-heading-p)]
15775 ["Previous keyword" org-shiftleft (org-on-heading-p)]
15776 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
15777 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
15778 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
15779 ["Show TODO Tree" org-show-todo-tree t]
15780 ["Global TODO list" org-todo-list t]
15781 "--"
15782 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
15783 :selected org-enforce-todo-dependencies :style toggle :active t]
15784 "Settings for tree at point"
15785 ["Do Children sequentially" org-toggle-ordered-property :style radio
15786 :selected (ignore-errors (org-entry-get nil "ORDERED"))
15787 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
15788 ["Do Children parallel" org-toggle-ordered-property :style radio
15789 :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
15790 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
15791 "--"
15792 ["Set Priority" org-priority t]
15793 ["Priority Up" org-shiftup t]
15794 ["Priority Down" org-shiftdown t]
15795 "--"
15796 ["Get news from all feeds" org-feed-update-all t]
15797 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
15798 ["Customize feeds" (customize-variable 'org-feed-alist) t])
15799 ("TAGS and Properties"
15800 ["Set Tags" org-set-tags-command t]
15801 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
15802 "--"
15803 ["Set property" org-set-property t]
15804 ["Column view of properties" org-columns t]
15805 ["Insert Column View DBlock" org-insert-columns-dblock t])
15806 ("Dates and Scheduling"
15807 ["Timestamp" org-time-stamp t]
15808 ["Timestamp (inactive)" org-time-stamp-inactive t]
15809 ("Change Date"
15810 ["1 Day Later" org-shiftright t]
15811 ["1 Day Earlier" org-shiftleft t]
15812 ["1 ... Later" org-shiftup t]
15813 ["1 ... Earlier" org-shiftdown t])
15814 ["Compute Time Range" org-evaluate-time-range t]
15815 ["Schedule Item" org-schedule t]
15816 ["Deadline" org-deadline t]
15817 "--"
15818 ["Custom time format" org-toggle-time-stamp-overlays
15819 :style radio :selected org-display-custom-times]
15820 "--"
15821 ["Goto Calendar" org-goto-calendar t]
15822 ["Date from Calendar" org-date-from-calendar t]
15823 "--"
15824 ["Start/Restart Timer" org-timer-start t]
15825 ["Pause/Continue Timer" org-timer-pause-or-continue t]
15826 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
15827 ["Insert Timer String" org-timer t]
15828 ["Insert Timer Item" org-timer-item t])
15829 ("Logging work"
15830 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
15831 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
15832 ["Clock out" org-clock-out t]
15833 ["Clock cancel" org-clock-cancel t]
15834 "--"
15835 ["Mark as default task" org-clock-mark-default-task t]
15836 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
15837 ["Goto running clock" org-clock-goto t]
15838 "--"
15839 ["Display times" org-clock-display t]
15840 ["Create clock table" org-clock-report t]
15841 "--"
15842 ["Record DONE time"
15843 (progn (setq org-log-done (not org-log-done))
15844 (message "Switching to %s will %s record a timestamp"
15845 (car org-done-keywords)
15846 (if org-log-done "automatically" "not")))
15847 :style toggle :selected org-log-done])
15848 "--"
15849 ["Agenda Command..." org-agenda t]
15850 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
15851 ("File List for Agenda")
15852 ("Special views current file"
15853 ["TODO Tree" org-show-todo-tree t]
15854 ["Check Deadlines" org-check-deadlines t]
15855 ["Timeline" org-timeline t]
15856 ["Tags/Property tree" org-match-sparse-tree t])
15857 "--"
15858 ["Export/Publish..." org-export t]
15859 ("LaTeX"
15860 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
15861 :selected org-cdlatex-mode]
15862 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
15863 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
15864 ["Modify math symbol" org-cdlatex-math-modify
15865 (org-inside-LaTeX-fragment-p)]
15866 ["Insert citation" org-reftex-citation t]
15867 "--"
15868 ["Export LaTeX fragments as images"
15869 (if (featurep 'org-exp)
15870 (setq org-export-with-LaTeX-fragments
15871 (not org-export-with-LaTeX-fragments))
15872 (require 'org-exp))
15873 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
15874 org-export-with-LaTeX-fragments)])
15875 "--"
15876 ("MobileOrg"
15877 ["Push Files and Views" org-mobile-push t]
15878 ["Get Captured and Flagged" org-mobile-pull t]
15879 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
15880 "--"
15881 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
15882 "--"
15883 ("Documentation"
15884 ["Show Version" org-version t]
15885 ["Info Documentation" org-info t])
15886 ("Customize"
15887 ["Browse Org Group" org-customize t]
15888 "--"
15889 ["Expand This Menu" org-create-customize-menu
15890 (fboundp 'customize-menu-create)])
15891 ["Send bug report" org-submit-bug-report t]
15892 "--"
15893 ("Refresh/Reload"
15894 ["Refresh setup current buffer" org-mode-restart t]
15895 ["Reload Org (after update)" org-reload t]
15896 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
15899 (defun org-info (&optional node)
15900 "Read documentation for Org-mode in the info system.
15901 With optional NODE, go directly to that node."
15902 (interactive)
15903 (info (format "(org)%s" (or node ""))))
15905 ;;;###autoload
15906 (defun org-submit-bug-report ()
15907 "Submit a bug report on Org-mode via mail.
15909 Don't hesitate to report any problems or inaccurate documentation.
15911 If you don't have setup sending mail from (X)Emacs, please copy the
15912 output buffer into your mail program, as it gives us important
15913 information about your Org-mode version and configuration."
15914 (interactive)
15915 (require 'reporter)
15916 (org-load-modules-maybe)
15917 (org-require-autoloaded-modules)
15918 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
15919 (reporter-submit-bug-report
15920 "emacs-orgmode@gnu.org"
15921 (org-version)
15922 (let (list)
15923 (save-window-excursion
15924 (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
15925 (delete-other-windows)
15926 (erase-buffer)
15927 (insert "You are about to submit a bug report to the Org-mode mailing list.
15929 We would like to add your full Org-mode and Outline configuration to the
15930 bug report. This greatly simplifies the work of the maintainer and
15931 other experts on the mailing list.
15933 HOWEVER, some variables you have customized may contain private
15934 information. The names of customers, colleagues, or friends, might
15935 appear in the form of file names, tags, todo states, or search strings.
15936 If you answer yes to the prompt, you might want to check and remove
15937 such private information before sending the email.")
15938 (add-text-properties (point-min) (point-max) '(face org-warning))
15939 (when (yes-or-no-p "Include your Org-mode configuration ")
15940 (mapatoms
15941 (lambda (v)
15942 (and (boundp v)
15943 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
15944 (or (and (symbol-value v)
15945 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
15946 (and
15947 (get v 'custom-type) (get v 'standard-value)
15948 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
15949 (push v list)))))
15950 (kill-buffer (get-buffer "*Warn about privacy*"))
15951 list))
15952 nil nil
15953 "Remember to cover the basics, that is, what you expected to happen and
15954 what in fact did happen. You don't know how to make a good report? See
15956 http://orgmode.org/manual/Feedback.html#Feedback
15958 Your bug report will be posted to the Org-mode mailing list.
15959 ------------------------------------------------------------------------")))
15961 (defun org-install-agenda-files-menu ()
15962 (let ((bl (buffer-list)))
15963 (save-excursion
15964 (while bl
15965 (set-buffer (pop bl))
15966 (if (org-mode-p) (setq bl nil)))
15967 (when (org-mode-p)
15968 (easy-menu-change
15969 '("Org") "File List for Agenda"
15970 (append
15971 (list
15972 ["Edit File List" (org-edit-agenda-file-list) t]
15973 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
15974 ["Remove Current File from List" org-remove-file t]
15975 ["Cycle through agenda files" org-cycle-agenda-files t]
15976 ["Occur in all agenda files" org-occur-in-agenda-files t]
15977 "--")
15978 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
15980 ;;;; Documentation
15982 ;;;###autoload
15983 (defun org-require-autoloaded-modules ()
15984 (interactive)
15985 (mapc 'require
15986 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
15987 org-docbook org-exp org-html org-icalendar
15988 org-id org-latex
15989 org-publish org-remember org-table
15990 org-timer org-xoxo)))
15992 ;;;###autoload
15993 (defun org-reload (&optional uncompiled)
15994 "Reload all org lisp files.
15995 With prefix arg UNCOMPILED, load the uncompiled versions."
15996 (interactive "P")
15997 (require 'find-func)
15998 (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
15999 (dir-org (file-name-directory (org-find-library-name "org")))
16000 (dir-org-contrib (ignore-errors
16001 (file-name-directory
16002 (org-find-library-name "org-contribdir"))))
16003 (files
16004 (append (directory-files dir-org t file-re)
16005 (and dir-org-contrib
16006 (directory-files dir-org-contrib t file-re))))
16007 (remove-re (concat (if (featurep 'xemacs)
16008 "org-colview" "org-colview-xemacs")
16009 "\\'")))
16010 (setq files (mapcar 'file-name-sans-extension files))
16011 (setq files (mapcar
16012 (lambda (x) (if (string-match remove-re x) nil x))
16013 files))
16014 (setq files (delq nil files))
16015 (mapc
16016 (lambda (f)
16017 (when (featurep (intern (file-name-nondirectory f)))
16018 (if (and (not uncompiled)
16019 (file-exists-p (concat f ".elc")))
16020 (load (concat f ".elc") nil nil t)
16021 (load (concat f ".el") nil nil t))))
16022 files))
16023 (org-version))
16025 ;;;###autoload
16026 (defun org-customize ()
16027 "Call the customize function with org as argument."
16028 (interactive)
16029 (org-load-modules-maybe)
16030 (org-require-autoloaded-modules)
16031 (customize-browse 'org))
16033 (defun org-create-customize-menu ()
16034 "Create a full customization menu for Org-mode, insert it into the menu."
16035 (interactive)
16036 (org-load-modules-maybe)
16037 (org-require-autoloaded-modules)
16038 (if (fboundp 'customize-menu-create)
16039 (progn
16040 (easy-menu-change
16041 '("Org") "Customize"
16042 `(["Browse Org group" org-customize t]
16043 "--"
16044 ,(customize-menu-create 'org)
16045 ["Set" Custom-set t]
16046 ["Save" Custom-save t]
16047 ["Reset to Current" Custom-reset-current t]
16048 ["Reset to Saved" Custom-reset-saved t]
16049 ["Reset to Standard Settings" Custom-reset-standard t]))
16050 (message "\"Org\"-menu now contains full customization menu"))
16051 (error "Cannot expand menu (outdated version of cus-edit.el)")))
16053 ;;;; Miscellaneous stuff
16055 ;;; Generally useful functions
16057 (defun org-get-at-bol (property)
16058 "Get text property PROPERTY at beginning of line."
16059 (get-text-property (point-at-bol) property))
16061 (defun org-find-text-property-in-string (prop s)
16062 "Return the first non-nil value of property PROP in string S."
16063 (or (get-text-property 0 prop s)
16064 (get-text-property (or (next-single-property-change 0 prop s) 0)
16065 prop s)))
16067 (defun org-display-warning (message) ;; Copied from Emacs-Muse
16068 "Display the given MESSAGE as a warning."
16069 (if (fboundp 'display-warning)
16070 (display-warning 'org message
16071 (if (featurep 'xemacs)
16072 'warning
16073 :warning))
16074 (let ((buf (get-buffer-create "*Org warnings*")))
16075 (with-current-buffer buf
16076 (goto-char (point-max))
16077 (insert "Warning (Org): " message)
16078 (unless (bolp)
16079 (newline)))
16080 (display-buffer buf)
16081 (sit-for 0))))
16083 (defun org-in-commented-line ()
16084 "Is point in a line starting with `#'?"
16085 (equal (char-after (point-at-bol)) ?#))
16087 (defun org-in-verbatim-emphasis ()
16088 (save-match-data
16089 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
16091 (defun org-goto-marker-or-bmk (marker &optional bookmark)
16092 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
16093 (if (and marker (marker-buffer marker)
16094 (buffer-live-p (marker-buffer marker)))
16095 (progn
16096 (switch-to-buffer (marker-buffer marker))
16097 (if (or (> marker (point-max)) (< marker (point-min)))
16098 (widen))
16099 (goto-char marker)
16100 (org-show-context 'org-goto))
16101 (if bookmark
16102 (bookmark-jump bookmark)
16103 (error "Cannot find location"))))
16105 (defun org-quote-csv-field (s)
16106 "Quote field for inclusion in CSV material."
16107 (if (string-match "[\",]" s)
16108 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
16111 (defun org-plist-delete (plist property)
16112 "Delete PROPERTY from PLIST.
16113 This is in contrast to merely setting it to 0."
16114 (let (p)
16115 (while plist
16116 (if (not (eq property (car plist)))
16117 (setq p (plist-put p (car plist) (nth 1 plist))))
16118 (setq plist (cddr plist)))
16121 (defun org-force-self-insert (N)
16122 "Needed to enforce self-insert under remapping."
16123 (interactive "p")
16124 (self-insert-command N))
16126 (defun org-string-width (s)
16127 "Compute width of string, ignoring invisible characters.
16128 This ignores character with invisibility property `org-link', and also
16129 characters with property `org-cwidth', because these will become invisible
16130 upon the next fontification round."
16131 (let (b l)
16132 (when (or (eq t buffer-invisibility-spec)
16133 (assq 'org-link buffer-invisibility-spec))
16134 (while (setq b (text-property-any 0 (length s)
16135 'invisible 'org-link s))
16136 (setq s (concat (substring s 0 b)
16137 (substring s (or (next-single-property-change
16138 b 'invisible s) (length s)))))))
16139 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
16140 (setq s (concat (substring s 0 b)
16141 (substring s (or (next-single-property-change
16142 b 'org-cwidth s) (length s))))))
16143 (setq l (string-width s) b -1)
16144 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
16145 (setq l (- l (get-text-property b 'org-dwidth-n s))))
16148 (defun org-get-indentation (&optional line)
16149 "Get the indentation of the current line, interpreting tabs.
16150 When LINE is given, assume it represents a line and compute its indentation."
16151 (if line
16152 (if (string-match "^ *" (org-remove-tabs line))
16153 (match-end 0))
16154 (save-excursion
16155 (beginning-of-line 1)
16156 (skip-chars-forward " \t")
16157 (current-column))))
16159 (defun org-remove-tabs (s &optional width)
16160 "Replace tabulators in S with spaces.
16161 Assumes that s is a single line, starting in column 0."
16162 (setq width (or width tab-width))
16163 (while (string-match "\t" s)
16164 (setq s (replace-match
16165 (make-string
16166 (- (* width (/ (+ (match-beginning 0) width) width))
16167 (match-beginning 0)) ?\ )
16168 t t s)))
16171 (defun org-fix-indentation (line ind)
16172 "Fix indentation in LINE.
16173 IND is a cons cell with target and minimum indentation.
16174 If the current indentation in LINE is smaller than the minimum,
16175 leave it alone. If it is larger than ind, set it to the target."
16176 (let* ((l (org-remove-tabs line))
16177 (i (org-get-indentation l))
16178 (i1 (car ind)) (i2 (cdr ind)))
16179 (if (>= i i2) (setq l (substring line i2)))
16180 (if (> i1 0)
16181 (concat (make-string i1 ?\ ) l)
16182 l)))
16184 (defun org-remove-indentation (code &optional n)
16185 "Remove the maximum common indentation from the lines in CODE.
16186 N may optionally be the number of spaces to remove."
16187 (with-temp-buffer
16188 (insert code)
16189 (org-do-remove-indentation n)
16190 (buffer-string)))
16192 (defun org-do-remove-indentation (&optional n)
16193 "Remove the maximum common indentation from the buffer."
16194 (untabify (point-min) (point-max))
16195 (let ((min 10000) re)
16196 (if n
16197 (setq min n)
16198 (goto-char (point-min))
16199 (while (re-search-forward "^ *[^ \n]" nil t)
16200 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
16201 (unless (or (= min 0) (= min 10000))
16202 (setq re (format "^ \\{%d\\}" min))
16203 (goto-char (point-min))
16204 (while (re-search-forward re nil t)
16205 (replace-match "")
16206 (end-of-line 1))
16207 min)))
16209 (defun org-fill-template (template alist)
16210 "Find each %key of ALIST in TEMPLATE and replace it."
16211 (let (entry key value)
16212 (setq alist (sort (copy-sequence alist)
16213 (lambda (a b) (< (length (car a)) (length (car b))))))
16214 (while (setq entry (pop alist))
16215 (setq template
16216 (replace-regexp-in-string
16217 (concat "%" (regexp-quote (car entry)))
16218 (cdr entry) template t t)))
16219 template))
16221 (defun org-base-buffer (buffer)
16222 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
16223 (if (not buffer)
16224 buffer
16225 (or (buffer-base-buffer buffer)
16226 buffer)))
16228 (defun org-trim (s)
16229 "Remove whitespace at beginning and end of string."
16230 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
16231 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
16234 (defun org-wrap (string &optional width lines)
16235 "Wrap string to either a number of lines, or a width in characters.
16236 If WIDTH is non-nil, the string is wrapped to that width, however many lines
16237 that costs. If there is a word longer than WIDTH, the text is actually
16238 wrapped to the length of that word.
16239 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
16240 many lines, whatever width that takes.
16241 The return value is a list of lines, without newlines at the end."
16242 (let* ((words (org-split-string string "[ \t\n]+"))
16243 (maxword (apply 'max (mapcar 'org-string-width words)))
16244 w ll)
16245 (cond (width
16246 (org-do-wrap words (max maxword width)))
16247 (lines
16248 (setq w maxword)
16249 (setq ll (org-do-wrap words maxword))
16250 (if (<= (length ll) lines)
16252 (setq ll words)
16253 (while (> (length ll) lines)
16254 (setq w (1+ w))
16255 (setq ll (org-do-wrap words w)))
16256 ll))
16257 (t (error "Cannot wrap this")))))
16259 (defun org-do-wrap (words width)
16260 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
16261 (let (lines line)
16262 (while words
16263 (setq line (pop words))
16264 (while (and words (< (+ (length line) (length (car words))) width))
16265 (setq line (concat line " " (pop words))))
16266 (setq lines (push line lines)))
16267 (nreverse lines)))
16269 (defun org-split-string (string &optional separators)
16270 "Splits STRING into substrings at SEPARATORS.
16271 No empty strings are returned if there are matches at the beginning
16272 and end of string."
16273 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
16274 (start 0)
16275 notfirst
16276 (list nil))
16277 (while (and (string-match rexp string
16278 (if (and notfirst
16279 (= start (match-beginning 0))
16280 (< start (length string)))
16281 (1+ start) start))
16282 (< (match-beginning 0) (length string)))
16283 (setq notfirst t)
16284 (or (eq (match-beginning 0) 0)
16285 (and (eq (match-beginning 0) (match-end 0))
16286 (eq (match-beginning 0) start))
16287 (setq list
16288 (cons (substring string start (match-beginning 0))
16289 list)))
16290 (setq start (match-end 0)))
16291 (or (eq start (length string))
16292 (setq list
16293 (cons (substring string start)
16294 list)))
16295 (nreverse list)))
16297 (defun org-quote-vert (s)
16298 "Replace \"|\" with \"\\vert\"."
16299 (while (string-match "|" s)
16300 (setq s (replace-match "\\vert" t t s)))
16303 (defun org-uuidgen-p (s)
16304 "Is S an ID created by UUIDGEN?"
16305 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
16307 (defun org-context ()
16308 "Return a list of contexts of the current cursor position.
16309 If several contexts apply, all are returned.
16310 Each context entry is a list with a symbol naming the context, and
16311 two positions indicating start and end of the context. Possible
16312 contexts are:
16314 :headline anywhere in a headline
16315 :headline-stars on the leading stars in a headline
16316 :todo-keyword on a TODO keyword (including DONE) in a headline
16317 :tags on the TAGS in a headline
16318 :priority on the priority cookie in a headline
16319 :item on the first line of a plain list item
16320 :item-bullet on the bullet/number of a plain list item
16321 :checkbox on the checkbox in a plain list item
16322 :table in an org-mode table
16323 :table-special on a special filed in a table
16324 :table-table in a table.el table
16325 :link on a hyperlink
16326 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
16327 :target on a <<target>>
16328 :radio-target on a <<<radio-target>>>
16329 :latex-fragment on a LaTeX fragment
16330 :latex-preview on a LaTeX fragment with overlayed preview image
16332 This function expects the position to be visible because it uses font-lock
16333 faces as a help to recognize the following contexts: :table-special, :link,
16334 and :keyword."
16335 (let* ((f (get-text-property (point) 'face))
16336 (faces (if (listp f) f (list f)))
16337 (p (point)) clist o)
16338 ;; First the large context
16339 (cond
16340 ((org-on-heading-p t)
16341 (push (list :headline (point-at-bol) (point-at-eol)) clist)
16342 (when (progn
16343 (beginning-of-line 1)
16344 (looking-at org-todo-line-tags-regexp))
16345 (push (org-point-in-group p 1 :headline-stars) clist)
16346 (push (org-point-in-group p 2 :todo-keyword) clist)
16347 (push (org-point-in-group p 4 :tags) clist))
16348 (goto-char p)
16349 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
16350 (if (looking-at "\\[#[A-Z0-9]\\]")
16351 (push (org-point-in-group p 0 :priority) clist)))
16353 ((org-at-item-p)
16354 (push (org-point-in-group p 2 :item-bullet) clist)
16355 (push (list :item (point-at-bol)
16356 (save-excursion (org-end-of-item) (point)))
16357 clist)
16358 (and (org-at-item-checkbox-p)
16359 (push (org-point-in-group p 0 :checkbox) clist)))
16361 ((org-at-table-p)
16362 (push (list :table (org-table-begin) (org-table-end)) clist)
16363 (if (memq 'org-formula faces)
16364 (push (list :table-special
16365 (previous-single-property-change p 'face)
16366 (next-single-property-change p 'face)) clist)))
16367 ((org-at-table-p 'any)
16368 (push (list :table-table) clist)))
16369 (goto-char p)
16371 ;; Now the small context
16372 (cond
16373 ((org-at-timestamp-p)
16374 (push (org-point-in-group p 0 :timestamp) clist))
16375 ((memq 'org-link faces)
16376 (push (list :link
16377 (previous-single-property-change p 'face)
16378 (next-single-property-change p 'face)) clist))
16379 ((memq 'org-special-keyword faces)
16380 (push (list :keyword
16381 (previous-single-property-change p 'face)
16382 (next-single-property-change p 'face)) clist))
16383 ((org-on-target-p)
16384 (push (org-point-in-group p 0 :target) clist)
16385 (goto-char (1- (match-beginning 0)))
16386 (if (looking-at org-radio-target-regexp)
16387 (push (org-point-in-group p 0 :radio-target) clist))
16388 (goto-char p))
16389 ((setq o (car (delq nil
16390 (mapcar
16391 (lambda (x)
16392 (if (memq x org-latex-fragment-image-overlays) x))
16393 (org-overlays-at (point))))))
16394 (push (list :latex-fragment
16395 (org-overlay-start o) (org-overlay-end o)) clist)
16396 (push (list :latex-preview
16397 (org-overlay-start o) (org-overlay-end o)) clist))
16398 ((org-inside-LaTeX-fragment-p)
16399 ;; FIXME: positions wrong.
16400 (push (list :latex-fragment (point) (point)) clist)))
16402 (setq clist (nreverse (delq nil clist)))
16403 clist))
16405 ;; FIXME: Compare with at-regexp-p Do we need both?
16406 (defun org-in-regexp (re &optional nlines visually)
16407 "Check if point is inside a match of regexp.
16408 Normally only the current line is checked, but you can include NLINES extra
16409 lines both before and after point into the search.
16410 If VISUALLY is set, require that the cursor is not after the match but
16411 really on, so that the block visually is on the match."
16412 (catch 'exit
16413 (let ((pos (point))
16414 (eol (point-at-eol (+ 1 (or nlines 0))))
16415 (inc (if visually 1 0)))
16416 (save-excursion
16417 (beginning-of-line (- 1 (or nlines 0)))
16418 (while (re-search-forward re eol t)
16419 (if (and (<= (match-beginning 0) pos)
16420 (>= (+ inc (match-end 0)) pos))
16421 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
16423 (defun org-at-regexp-p (regexp)
16424 "Is point inside a match of REGEXP in the current line?"
16425 (catch 'exit
16426 (save-excursion
16427 (let ((pos (point)) (end (point-at-eol)))
16428 (beginning-of-line 1)
16429 (while (re-search-forward regexp end t)
16430 (if (and (<= (match-beginning 0) pos)
16431 (>= (match-end 0) pos))
16432 (throw 'exit t)))
16433 nil))))
16435 (defun org-occur-in-agenda-files (regexp &optional nlines)
16436 "Call `multi-occur' with buffers for all agenda files."
16437 (interactive "sOrg-files matching: \np")
16438 (let* ((files (org-agenda-files))
16439 (tnames (mapcar 'file-truename files))
16440 (extra org-agenda-text-search-extra-files)
16442 (when (eq (car extra) 'agenda-archives)
16443 (setq extra (cdr extra))
16444 (setq files (org-add-archive-files files)))
16445 (while (setq f (pop extra))
16446 (unless (member (file-truename f) tnames)
16447 (add-to-list 'files f 'append)
16448 (add-to-list 'tnames (file-truename f) 'append)))
16449 (multi-occur
16450 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
16451 regexp)))
16453 (if (boundp 'occur-mode-find-occurrence-hook)
16454 ;; Emacs 23
16455 (add-hook 'occur-mode-find-occurrence-hook
16456 (lambda ()
16457 (when (org-mode-p)
16458 (org-reveal))))
16459 ;; Emacs 22
16460 (defadvice occur-mode-goto-occurrence
16461 (after org-occur-reveal activate)
16462 (and (org-mode-p) (org-reveal)))
16463 (defadvice occur-mode-goto-occurrence-other-window
16464 (after org-occur-reveal activate)
16465 (and (org-mode-p) (org-reveal)))
16466 (defadvice occur-mode-display-occurrence
16467 (after org-occur-reveal activate)
16468 (when (org-mode-p)
16469 (let ((pos (occur-mode-find-occurrence)))
16470 (with-current-buffer (marker-buffer pos)
16471 (save-excursion
16472 (goto-char pos)
16473 (org-reveal)))))))
16475 (defun org-occur-link-in-agenda-files ()
16476 "Create a link and search for it in the agendas.
16477 The link is not stored in `org-stored-links', it is just created
16478 for the search purpose."
16479 (interactive)
16480 (let ((link (condition-case nil
16481 (org-store-link nil)
16482 (error "Unable to create a link to here"))))
16483 (org-occur-in-agenda-files (regexp-quote link))))
16485 (defun org-uniquify (list)
16486 "Remove duplicate elements from LIST."
16487 (let (res)
16488 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
16489 res))
16491 (defun org-delete-all (elts list)
16492 "Remove all elements in ELTS from LIST."
16493 (while elts
16494 (setq list (delete (pop elts) list)))
16495 list)
16497 (defun org-back-over-empty-lines ()
16498 "Move backwards over whitespace, to the beginning of the first empty line.
16499 Returns the number of empty lines passed."
16500 (let ((pos (point)))
16501 (skip-chars-backward " \t\n\r")
16502 (beginning-of-line 2)
16503 (goto-char (min (point) pos))
16504 (count-lines (point) pos)))
16506 (defun org-skip-whitespace ()
16507 (skip-chars-forward " \t\n\r"))
16509 (defun org-point-in-group (point group &optional context)
16510 "Check if POINT is in match-group GROUP.
16511 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
16512 match. If the match group does ot exist or point is not inside it,
16513 return nil."
16514 (and (match-beginning group)
16515 (>= point (match-beginning group))
16516 (<= point (match-end group))
16517 (if context
16518 (list context (match-beginning group) (match-end group))
16519 t)))
16521 (defun org-switch-to-buffer-other-window (&rest args)
16522 "Switch to buffer in a second window on the current frame.
16523 In particular, do not allow pop-up frames."
16524 (let (pop-up-frames special-display-buffer-names special-display-regexps
16525 special-display-function)
16526 (apply 'switch-to-buffer-other-window args)))
16528 (defun org-combine-plists (&rest plists)
16529 "Create a single property list from all plists in PLISTS.
16530 The process starts by copying the first list, and then setting properties
16531 from the other lists. Settings in the last list are the most significant
16532 ones and overrule settings in the other lists."
16533 (let ((rtn (copy-sequence (pop plists)))
16534 p v ls)
16535 (while plists
16536 (setq ls (pop plists))
16537 (while ls
16538 (setq p (pop ls) v (pop ls))
16539 (setq rtn (plist-put rtn p v))))
16540 rtn))
16542 (defun org-move-line-down (arg)
16543 "Move the current line down. With prefix argument, move it past ARG lines."
16544 (interactive "p")
16545 (let ((col (current-column))
16546 beg end pos)
16547 (beginning-of-line 1) (setq beg (point))
16548 (beginning-of-line 2) (setq end (point))
16549 (beginning-of-line (+ 1 arg))
16550 (setq pos (move-marker (make-marker) (point)))
16551 (insert (delete-and-extract-region beg end))
16552 (goto-char pos)
16553 (org-move-to-column col)))
16555 (defun org-move-line-up (arg)
16556 "Move the current line up. With prefix argument, move it past ARG lines."
16557 (interactive "p")
16558 (let ((col (current-column))
16559 beg end pos)
16560 (beginning-of-line 1) (setq beg (point))
16561 (beginning-of-line 2) (setq end (point))
16562 (beginning-of-line (- arg))
16563 (setq pos (move-marker (make-marker) (point)))
16564 (insert (delete-and-extract-region beg end))
16565 (goto-char pos)
16566 (org-move-to-column col)))
16568 (defun org-replace-escapes (string table)
16569 "Replace %-escapes in STRING with values in TABLE.
16570 TABLE is an association list with keys like \"%a\" and string values.
16571 The sequences in STRING may contain normal field width and padding information,
16572 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
16573 so values can contain further %-escapes if they are define later in TABLE."
16574 (let ((case-fold-search nil)
16575 e re rpl)
16576 (while (setq e (pop table))
16577 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
16578 (while (string-match re string)
16579 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
16580 (cdr e)))
16581 (setq string (replace-match rpl t t string))))
16582 string))
16585 (defun org-sublist (list start end)
16586 "Return a section of LIST, from START to END.
16587 Counting starts at 1."
16588 (let (rtn (c start))
16589 (setq list (nthcdr (1- start) list))
16590 (while (and list (<= c end))
16591 (push (pop list) rtn)
16592 (setq c (1+ c)))
16593 (nreverse rtn)))
16595 (defun org-find-base-buffer-visiting (file)
16596 "Like `find-buffer-visiting' but always return the base buffer and
16597 not an indirect buffer."
16598 (let ((buf (or (get-file-buffer file)
16599 (find-buffer-visiting file))))
16600 (if buf
16601 (or (buffer-base-buffer buf) buf)
16602 nil)))
16604 (defun org-image-file-name-regexp (&optional extensions)
16605 "Return regexp matching the file names of images.
16606 If EXTENSIONS is given, only match these."
16607 (if (and (not extensions) (fboundp 'image-file-name-regexp))
16608 (image-file-name-regexp)
16609 (let ((image-file-name-extensions
16610 (or extensions
16611 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
16612 "xbm" "xpm" "pbm" "pgm" "ppm"))))
16613 (concat "\\."
16614 (regexp-opt (nconc (mapcar 'upcase
16615 image-file-name-extensions)
16616 image-file-name-extensions)
16618 "\\'"))))
16620 (defun org-file-image-p (file &optional extensions)
16621 "Return non-nil if FILE is an image."
16622 (save-match-data
16623 (string-match (org-image-file-name-regexp extensions) file)))
16625 (defun org-get-cursor-date ()
16626 "Return the date at cursor in as a time.
16627 This works in the calendar and in the agenda, anywhere else it just
16628 returns the current time."
16629 (let (date day defd)
16630 (cond
16631 ((eq major-mode 'calendar-mode)
16632 (setq date (calendar-cursor-to-date)
16633 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16634 ((eq major-mode 'org-agenda-mode)
16635 (setq day (get-text-property (point) 'day))
16636 (if day
16637 (setq date (calendar-gregorian-from-absolute day)
16638 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
16639 (nth 2 date))))))
16640 (or defd (current-time))))
16642 (defvar org-agenda-action-marker (make-marker)
16643 "Marker pointing to the entry for the next agenda action.")
16645 (defun org-mark-entry-for-agenda-action ()
16646 "Mark the current entry as target of an agenda action.
16647 Agenda actions are actions executed from the agenda with the key `k',
16648 which make use of the date at the cursor."
16649 (interactive)
16650 (move-marker org-agenda-action-marker
16651 (save-excursion (org-back-to-heading t) (point))
16652 (current-buffer))
16653 (message
16654 "Entry marked for action; press `k' at desired date in agenda or calendar"))
16656 ;;; Paragraph filling stuff.
16657 ;; We want this to be just right, so use the full arsenal.
16659 (defun org-indent-line-function ()
16660 "Indent line like previous, but further if previous was headline or item."
16661 (interactive)
16662 (let* ((pos (point))
16663 (itemp (org-at-item-p))
16664 (case-fold-search t)
16665 (org-drawer-regexp (or org-drawer-regexp "\000"))
16666 column bpos bcol tpos tcol bullet btype bullet-type)
16667 ;; Find the previous relevant line
16668 (beginning-of-line 1)
16669 (cond
16670 ((looking-at "#") (setq column 0))
16671 ((looking-at "\\*+ ") (setq column 0))
16672 ((and (looking-at "[ \t]*:END:")
16673 (save-excursion (re-search-backward org-drawer-regexp nil t)))
16674 (save-excursion
16675 (goto-char (1- (match-beginning 1)))
16676 (setq column (current-column))))
16677 ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
16678 (save-excursion
16679 (re-search-backward
16680 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
16681 (setq column (org-get-indentation (match-string 0))))
16683 (beginning-of-line 0)
16684 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
16685 (not (looking-at "[ \t]*:END:"))
16686 (not (looking-at org-drawer-regexp)))
16687 (beginning-of-line 0))
16688 (cond
16689 ((looking-at "\\*+[ \t]+")
16690 (if (not org-adapt-indentation)
16691 (setq column 0)
16692 (goto-char (match-end 0))
16693 (setq column (current-column))))
16694 ((looking-at org-drawer-regexp)
16695 (goto-char (1- (match-beginning 1)))
16696 (setq column (current-column)))
16697 ((looking-at "\\([ \t]*\\):END:")
16698 (goto-char (match-end 1))
16699 (setq column (current-column)))
16700 ((org-in-item-p)
16701 (org-beginning-of-item)
16702 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
16703 (setq bpos (match-beginning 1) tpos (match-end 0)
16704 bcol (progn (goto-char bpos) (current-column))
16705 tcol (progn (goto-char tpos) (current-column))
16706 bullet (match-string 1)
16707 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
16708 (if (> tcol (+ bcol org-description-max-indent))
16709 (setq tcol (+ bcol 5)))
16710 (if (not itemp)
16711 (setq column tcol)
16712 (goto-char pos)
16713 (beginning-of-line 1)
16714 (if (looking-at "\\S-")
16715 (progn
16716 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
16717 (setq bullet (match-string 1)
16718 btype (if (string-match "[0-9]" bullet) "n" bullet))
16719 (setq column (if (equal btype bullet-type) bcol tcol)))
16720 (setq column (org-get-indentation)))))
16721 (t (setq column (org-get-indentation))))))
16722 (goto-char pos)
16723 (if (<= (current-column) (current-indentation))
16724 (org-indent-line-to column)
16725 (save-excursion (org-indent-line-to column)))
16726 (setq column (current-column))
16727 (beginning-of-line 1)
16728 (if (looking-at
16729 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
16730 (replace-match (concat (match-string 1)
16731 (format org-property-format
16732 (match-string 2) (match-string 3)))
16733 t t))
16734 (org-move-to-column column)))
16736 (defun org-set-autofill-regexps ()
16737 (interactive)
16738 ;; In the paragraph separator we include headlines, because filling
16739 ;; text in a line directly attached to a headline would otherwise
16740 ;; fill the headline as well.
16741 (org-set-local 'comment-start-skip "^#+[ \t]*")
16742 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
16743 ;; The paragraph starter includes hand-formatted lists.
16744 (org-set-local
16745 'paragraph-start
16746 (concat
16747 "\f" "\\|"
16748 "[ ]*$" "\\|"
16749 "\\*+ " "\\|"
16750 "[ \t]*#" "\\|"
16751 "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
16752 "[ \t]*[:|]" "\\|"
16753 "\\$\\$" "\\|"
16754 "\\\\\\(begin\\|end\\|[][]\\)"))
16755 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
16756 ;; But only if the user has not turned off tables or fixed-width regions
16757 (org-set-local
16758 'auto-fill-inhibit-regexp
16759 (concat "\\*+ \\|#\\+"
16760 "\\|[ \t]*" org-keyword-time-regexp
16761 (if (or org-enable-table-editor org-enable-fixed-width-editor)
16762 (concat
16763 "\\|[ \t]*["
16764 (if org-enable-table-editor "|" "")
16765 (if org-enable-fixed-width-editor ":" "")
16766 "]"))))
16767 ;; We use our own fill-paragraph function, to make sure that tables
16768 ;; and fixed-width regions are not wrapped. That function will pass
16769 ;; through to `fill-paragraph' when appropriate.
16770 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
16771 ; Adaptive filling: To get full control, first make sure that
16772 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
16773 (org-set-local 'adaptive-fill-regexp "\000")
16774 (org-set-local 'adaptive-fill-function
16775 'org-adaptive-fill-function)
16776 (org-set-local
16777 'align-mode-rules-list
16778 '((org-in-buffer-settings
16779 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
16780 (modes . '(org-mode))))))
16782 (defun org-fill-paragraph (&optional justify)
16783 "Re-align a table, pass through to fill-paragraph if no table."
16784 (let ((table-p (org-at-table-p))
16785 (table.el-p (org-at-table.el-p)))
16786 (cond ((and (equal (char-after (point-at-bol)) ?*)
16787 (save-excursion (goto-char (point-at-bol))
16788 (looking-at outline-regexp)))
16789 t) ; skip headlines
16790 (table.el-p t) ; skip table.el tables
16791 (table-p (org-table-align) t) ; align org-mode tables
16792 (t nil)))) ; call paragraph-fill
16794 ;; For reference, this is the default value of adaptive-fill-regexp
16795 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
16797 (defun org-adaptive-fill-function ()
16798 "Return a fill prefix for org-mode files.
16799 In particular, this makes sure hanging paragraphs for hand-formatted lists
16800 work correctly."
16801 (cond ((looking-at "#[ \t]+")
16802 (match-string 0))
16803 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
16804 (save-excursion
16805 (if (> (match-end 1) (+ (match-beginning 1)
16806 org-description-max-indent))
16807 (goto-char (+ (match-beginning 1) 5))
16808 (goto-char (match-end 0)))
16809 (make-string (current-column) ?\ )))
16810 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
16811 (save-excursion
16812 (goto-char (match-end 0))
16813 (make-string (current-column) ?\ )))
16814 (t nil)))
16816 ;;; Other stuff.
16818 (defun org-toggle-fixed-width-section (arg)
16819 "Toggle the fixed-width export.
16820 If there is no active region, the QUOTE keyword at the current headline is
16821 inserted or removed. When present, it causes the text between this headline
16822 and the next to be exported as fixed-width text, and unmodified.
16823 If there is an active region, this command adds or removes a colon as the
16824 first character of this line. If the first character of a line is a colon,
16825 this line is also exported in fixed-width font."
16826 (interactive "P")
16827 (let* ((cc 0)
16828 (regionp (org-region-active-p))
16829 (beg (if regionp (region-beginning) (point)))
16830 (end (if regionp (region-end)))
16831 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
16832 (case-fold-search nil)
16833 (re "[ \t]*\\(: \\)")
16834 off)
16835 (if regionp
16836 (save-excursion
16837 (goto-char beg)
16838 (setq cc (current-column))
16839 (beginning-of-line 1)
16840 (setq off (looking-at re))
16841 (while (> nlines 0)
16842 (setq nlines (1- nlines))
16843 (beginning-of-line 1)
16844 (cond
16845 (arg
16846 (org-move-to-column cc t)
16847 (insert ": \n")
16848 (forward-line -1))
16849 ((and off (looking-at re))
16850 (replace-match "" t t nil 1))
16851 ((not off) (org-move-to-column cc t) (insert ": ")))
16852 (forward-line 1)))
16853 (save-excursion
16854 (org-back-to-heading)
16855 (if (looking-at (concat outline-regexp
16856 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
16857 (replace-match "" t t nil 1)
16858 (if (looking-at outline-regexp)
16859 (progn
16860 (goto-char (match-end 0))
16861 (insert org-quote-string " "))))))))
16863 (defun org-reftex-citation ()
16864 "Use reftex-citation to insert a citation into the buffer.
16865 This looks for a line like
16867 #+BIBLIOGRAPHY: foo plain option:-d
16869 and derives from it that foo.bib is the bbliography file relevant
16870 for this document. It then installs the necessary environment for RefTeX
16871 to work in this buffer and calls `reftex-citation' to insert a citation
16872 into the buffer.
16874 Export of such citations to both LaTeX and HTML is handled by the contributed
16875 package org-exp-bibtex by Taru Karttunen."
16876 (interactive)
16877 (let ((reftex-docstruct-symbol 'rds)
16878 (reftex-cite-format "\\cite{%l}")
16879 rds bib)
16880 (save-excursion
16881 (save-restriction
16882 (widen)
16883 (let ((case-fold-search t)
16884 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
16885 (if (not (save-excursion
16886 (or (re-search-forward re nil t)
16887 (re-search-backward re nil t))))
16888 (error "No bibliography defined in file")
16889 (setq bib (concat (match-string 1) ".bib")
16890 rds (list (list 'bib bib)))))))
16891 (call-interactively 'reftex-citation)))
16893 ;;;; Functions extending outline functionality
16895 (defun org-beginning-of-line (&optional arg)
16896 "Go to the beginning of the current line. If that is invisible, continue
16897 to a visible line beginning. This makes the function of C-a more intuitive.
16898 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
16899 first attempt, and only move to after the tags when the cursor is already
16900 beyond the end of the headline."
16901 (interactive "P")
16902 (let ((pos (point))
16903 (special (if (consp org-special-ctrl-a/e)
16904 (car org-special-ctrl-a/e)
16905 org-special-ctrl-a/e))
16906 refpos)
16907 (if (org-bound-and-true-p line-move-visual)
16908 (beginning-of-visual-line 1)
16909 (beginning-of-line 1))
16910 (if (and arg (fboundp 'move-beginning-of-line))
16911 (call-interactively 'move-beginning-of-line)
16912 (if (bobp)
16914 (backward-char 1)
16915 (if (org-invisible-p)
16916 (while (and (not (bobp)) (org-invisible-p))
16917 (backward-char 1)
16918 (beginning-of-line 1))
16919 (forward-char 1))))
16920 (when special
16921 (cond
16922 ((and (looking-at org-complex-heading-regexp)
16923 (= (char-after (match-end 1)) ?\ ))
16924 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
16925 (point-at-eol)))
16926 (goto-char
16927 (if (eq special t)
16928 (cond ((> pos refpos) refpos)
16929 ((= pos (point)) refpos)
16930 (t (point)))
16931 (cond ((> pos (point)) (point))
16932 ((not (eq last-command this-command)) (point))
16933 (t refpos)))))
16934 ((org-at-item-p)
16935 (goto-char
16936 (if (eq special t)
16937 (cond ((> pos (match-end 4)) (match-end 4))
16938 ((= pos (point)) (match-end 4))
16939 (t (point)))
16940 (cond ((> pos (point)) (point))
16941 ((not (eq last-command this-command)) (point))
16942 (t (match-end 4))))))))
16943 (org-no-warnings
16944 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
16946 (defun org-end-of-line (&optional arg)
16947 "Go to the end of the line.
16948 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
16949 first attempt, and only move to after the tags when the cursor is already
16950 beyond the end of the headline."
16951 (interactive "P")
16952 (let ((special (if (consp org-special-ctrl-a/e)
16953 (cdr org-special-ctrl-a/e)
16954 org-special-ctrl-a/e)))
16955 (if (or (not special)
16956 (not (org-on-heading-p))
16957 arg)
16958 (call-interactively
16959 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
16960 ((fboundp 'move-end-of-line) 'move-end-of-line)
16961 (t 'end-of-line)))
16962 (let ((pos (point)))
16963 (beginning-of-line 1)
16964 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
16965 (if (eq special t)
16966 (if (or (< pos (match-beginning 1))
16967 (= pos (match-end 0)))
16968 (goto-char (match-beginning 1))
16969 (goto-char (match-end 0)))
16970 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
16971 (goto-char (match-end 0))
16972 (goto-char (match-beginning 1))))
16973 (call-interactively (if (fboundp 'move-end-of-line)
16974 'move-end-of-line
16975 'end-of-line)))))
16976 (org-no-warnings
16977 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
16979 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
16980 (define-key org-mode-map "\C-e" 'org-end-of-line)
16981 (define-key org-mode-map [home] 'org-beginning-of-line)
16982 (define-key org-mode-map [end] 'org-end-of-line)
16984 (defun org-backward-sentence (&optional arg)
16985 "Go to beginning of sentence, or beginning of table field.
16986 This will call `backward-sentence' or `org-table-beginning-of-field',
16987 depending on context."
16988 (interactive "P")
16989 (cond
16990 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
16991 (t (call-interactively 'backward-sentence))))
16993 (defun org-forward-sentence (&optional arg)
16994 "Go to end of sentence, or end of table field.
16995 This will call `forward-sentence' or `org-table-end-of-field',
16996 depending on context."
16997 (interactive "P")
16998 (cond
16999 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
17000 (t (call-interactively 'forward-sentence))))
17002 (define-key org-mode-map "\M-a" 'org-backward-sentence)
17003 (define-key org-mode-map "\M-e" 'org-forward-sentence)
17005 (defun org-kill-line (&optional arg)
17006 "Kill line, to tags or end of line."
17007 (interactive "P")
17008 (cond
17009 ((or (not org-special-ctrl-k)
17010 (bolp)
17011 (not (org-on-heading-p)))
17012 (call-interactively 'kill-line))
17013 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
17014 (kill-region (point) (match-beginning 1))
17015 (org-set-tags nil t))
17016 (t (kill-region (point) (point-at-eol)))))
17018 (define-key org-mode-map "\C-k" 'org-kill-line)
17020 (defun org-yank (&optional arg)
17021 "Yank. If the kill is a subtree, treat it specially.
17022 This command will look at the current kill and check if is a single
17023 subtree, or a series of subtrees[1]. If it passes the test, and if the
17024 cursor is at the beginning of a line or after the stars of a currently
17025 empty headline, then the yank is handled specially. How exactly depends
17026 on the value of the following variables, both set by default.
17028 org-yank-folded-subtrees
17029 When set, the subtree(s) will be folded after insertion, but only
17030 if doing so would now swallow text after the yanked text.
17032 org-yank-adjusted-subtrees
17033 When set, the subtree will be promoted or demoted in order to
17034 fit into the local outline tree structure, which means that the level
17035 will be adjusted so that it becomes the smaller one of the two
17036 *visible* surrounding headings.
17038 Any prefix to this command will cause `yank' to be called directly with
17039 no special treatment. In particular, a simple `C-u' prefix will just
17040 plainly yank the text as it is.
17042 \[1] The test checks if the first non-white line is a heading
17043 and if there are no other headings with fewer stars."
17044 (interactive "P")
17045 (org-yank-generic 'yank arg))
17047 (defun org-yank-generic (command arg)
17048 "Perform some yank-like command.
17050 This function implements the behavior described in the `org-yank'
17051 documentation. However, it has been generalized to work for any
17052 interactive command with similar behavior."
17054 ;; pretend to be command COMMAND
17055 (setq this-command command)
17057 (if arg
17058 (call-interactively command)
17060 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
17061 (and (org-kill-is-subtree-p)
17062 (or (bolp)
17063 (and (looking-at "[ \t]*$")
17064 (string-match
17065 "\\`\\*+\\'"
17066 (buffer-substring (point-at-bol) (point)))))))
17067 swallowp)
17068 (cond
17069 ((and subtreep org-yank-folded-subtrees)
17070 (let ((beg (point))
17071 end)
17072 (if (and subtreep org-yank-adjusted-subtrees)
17073 (org-paste-subtree nil nil 'for-yank)
17074 (call-interactively command))
17076 (setq end (point))
17077 (goto-char beg)
17078 (when (and (bolp) subtreep
17079 (not (setq swallowp
17080 (org-yank-folding-would-swallow-text beg end))))
17081 (or (looking-at outline-regexp)
17082 (re-search-forward (concat "^" outline-regexp) end t))
17083 (while (and (< (point) end) (looking-at outline-regexp))
17084 (hide-subtree)
17085 (org-cycle-show-empty-lines 'folded)
17086 (condition-case nil
17087 (outline-forward-same-level 1)
17088 (error (goto-char end)))))
17089 (when swallowp
17090 (message
17091 "Inserted text not folded because that would swallow text"))
17093 (goto-char end)
17094 (skip-chars-forward " \t\n\r")
17095 (beginning-of-line 1)
17096 (push-mark beg 'nomsg)))
17097 ((and subtreep org-yank-adjusted-subtrees)
17098 (let ((beg (point-at-bol)))
17099 (org-paste-subtree nil nil 'for-yank)
17100 (push-mark beg 'nomsg)))
17102 (call-interactively command))))))
17104 (defun org-yank-folding-would-swallow-text (beg end)
17105 "Would hide-subtree at BEG swallow any text after END?"
17106 (let (level)
17107 (save-excursion
17108 (goto-char beg)
17109 (when (or (looking-at outline-regexp)
17110 (re-search-forward (concat "^" outline-regexp) end t))
17111 (setq level (org-outline-level)))
17112 (goto-char end)
17113 (skip-chars-forward " \t\r\n\v\f")
17114 (if (or (eobp)
17115 (and (bolp) (looking-at org-outline-regexp)
17116 (<= (org-outline-level) level)))
17117 nil ; Nothing would be swallowed
17118 t)))) ; something would swallow
17120 (define-key org-mode-map "\C-y" 'org-yank)
17122 (defun org-invisible-p ()
17123 "Check if point is at a character currently not visible."
17124 ;; Early versions of noutline don't have `outline-invisible-p'.
17125 (if (fboundp 'outline-invisible-p)
17126 (outline-invisible-p)
17127 (get-char-property (point) 'invisible)))
17129 (defun org-invisible-p2 ()
17130 "Check if point is at a character currently not visible."
17131 (save-excursion
17132 (if (and (eolp) (not (bobp))) (backward-char 1))
17133 ;; Early versions of noutline don't have `outline-invisible-p'.
17134 (if (fboundp 'outline-invisible-p)
17135 (outline-invisible-p)
17136 (get-char-property (point) 'invisible))))
17138 (defun org-back-to-heading (&optional invisible-ok)
17139 "Call `outline-back-to-heading', but provide a better error message."
17140 (condition-case nil
17141 (outline-back-to-heading invisible-ok)
17142 (error (error "Before first headline at position %d in buffer %s"
17143 (point) (current-buffer)))))
17145 (defun org-before-first-heading-p ()
17146 "Before first heading?"
17147 (save-excursion
17148 (null (re-search-backward "^\\*+ " nil t))))
17150 (defun org-on-heading-p (&optional ignored)
17151 (outline-on-heading-p t))
17152 (defun org-at-heading-p (&optional ignored)
17153 (outline-on-heading-p t))
17155 (defun org-at-heading-or-item-p ()
17156 (or (org-on-heading-p) (org-at-item-p)))
17158 (defun org-on-target-p ()
17159 (or (org-in-regexp org-radio-target-regexp)
17160 (org-in-regexp org-target-regexp)))
17162 (defun org-up-heading-all (arg)
17163 "Move to the heading line of which the present line is a subheading.
17164 This function considers both visible and invisible heading lines.
17165 With argument, move up ARG levels."
17166 (if (fboundp 'outline-up-heading-all)
17167 (outline-up-heading-all arg) ; emacs 21 version of outline.el
17168 (outline-up-heading arg t))) ; emacs 22 version of outline.el
17170 (defun org-up-heading-safe ()
17171 "Move to the heading line of which the present line is a subheading.
17172 This version will not throw an error. It will return the level of the
17173 headline found, or nil if no higher level is found.
17175 Also, this function will be a lot faster than `outline-up-heading',
17176 because it relies on stars being the outline starters. This can really
17177 make a significant difference in outlines with very many siblings."
17178 (let (start-level re)
17179 (org-back-to-heading t)
17180 (setq start-level (funcall outline-level))
17181 (if (equal start-level 1)
17183 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
17184 (if (re-search-backward re nil t)
17185 (funcall outline-level)))))
17187 (defun org-first-sibling-p ()
17188 "Is this heading the first child of its parents?"
17189 (interactive)
17190 (let ((re (concat "^" outline-regexp))
17191 level l)
17192 (unless (org-at-heading-p t)
17193 (error "Not at a heading"))
17194 (setq level (funcall outline-level))
17195 (save-excursion
17196 (if (not (re-search-backward re nil t))
17198 (setq l (funcall outline-level))
17199 (< l level)))))
17201 (defun org-goto-sibling (&optional previous)
17202 "Goto the next sibling, even if it is invisible.
17203 When PREVIOUS is set, go to the previous sibling instead. Returns t
17204 when a sibling was found. When none is found, return nil and don't
17205 move point."
17206 (let ((fun (if previous 're-search-backward 're-search-forward))
17207 (pos (point))
17208 (re (concat "^" outline-regexp))
17209 level l)
17210 (when (condition-case nil (org-back-to-heading t) (error nil))
17211 (setq level (funcall outline-level))
17212 (catch 'exit
17213 (or previous (forward-char 1))
17214 (while (funcall fun re nil t)
17215 (setq l (funcall outline-level))
17216 (when (< l level) (goto-char pos) (throw 'exit nil))
17217 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
17218 (goto-char pos)
17219 nil))))
17221 (defun org-show-siblings ()
17222 "Show all siblings of the current headline."
17223 (save-excursion
17224 (while (org-goto-sibling) (org-flag-heading nil)))
17225 (save-excursion
17226 (while (org-goto-sibling 'previous)
17227 (org-flag-heading nil))))
17229 (defun org-show-hidden-entry ()
17230 "Show an entry where even the heading is hidden."
17231 (save-excursion
17232 (org-show-entry)))
17234 (defun org-flag-heading (flag &optional entry)
17235 "Flag the current heading. FLAG non-nil means make invisible.
17236 When ENTRY is non-nil, show the entire entry."
17237 (save-excursion
17238 (org-back-to-heading t)
17239 ;; Check if we should show the entire entry
17240 (if entry
17241 (progn
17242 (org-show-entry)
17243 (save-excursion
17244 (and (outline-next-heading)
17245 (org-flag-heading nil))))
17246 (outline-flag-region (max (point-min) (1- (point)))
17247 (save-excursion (outline-end-of-heading) (point))
17248 flag))))
17250 (defun org-get-next-sibling ()
17251 "Move to next heading of the same level, and return point.
17252 If there is no such heading, return nil.
17253 This is like outline-next-sibling, but invisible headings are ok."
17254 (let ((level (funcall outline-level)))
17255 (outline-next-heading)
17256 (while (and (not (eobp)) (> (funcall outline-level) level))
17257 (outline-next-heading))
17258 (if (or (eobp) (< (funcall outline-level) level))
17260 (point))))
17262 (defun org-get-last-sibling ()
17263 "Move to previous heading of the same level, and return point.
17264 If there is no such heading, return nil."
17265 (let ((opoint (point))
17266 (level (funcall outline-level)))
17267 (outline-previous-heading)
17268 (when (and (/= (point) opoint) (outline-on-heading-p t))
17269 (while (and (> (funcall outline-level) level)
17270 (not (bobp)))
17271 (outline-previous-heading))
17272 (if (< (funcall outline-level) level)
17274 (point)))))
17276 (defun org-end-of-subtree (&optional invisible-OK to-heading)
17277 ;; This contains an exact copy of the original function, but it uses
17278 ;; `org-back-to-heading', to make it work also in invisible
17279 ;; trees. And is uses an invisible-OK argument.
17280 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
17281 ;; Furthermore, when used inside Org, finding the end of a large subtree
17282 ;; with many children and grandchildren etc, this can be much faster
17283 ;; than the outline version.
17284 (org-back-to-heading invisible-OK)
17285 (let ((first t)
17286 (level (funcall outline-level)))
17287 (if (and (org-mode-p) (< level 1000))
17288 ;; A true heading (not a plain list item), in Org-mode
17289 ;; This means we can easily find the end by looking
17290 ;; only for the right number of stars. Using a regexp to do
17291 ;; this is so much faster than using a Lisp loop.
17292 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
17293 (forward-char 1)
17294 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
17295 ;; something else, do it the slow way
17296 (while (and (not (eobp))
17297 (or first (> (funcall outline-level) level)))
17298 (setq first nil)
17299 (outline-next-heading)))
17300 (unless to-heading
17301 (if (memq (preceding-char) '(?\n ?\^M))
17302 (progn
17303 ;; Go to end of line before heading
17304 (forward-char -1)
17305 (if (memq (preceding-char) '(?\n ?\^M))
17306 ;; leave blank line before heading
17307 (forward-char -1))))))
17308 (point))
17310 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
17311 "Use Org version in org-mode, for dramatic speed-up."
17312 (if (eq major-mode 'org-mode)
17313 (progn
17314 (org-end-of-subtree nil t)
17315 (unless (eobp) (backward-char 1)))
17316 ad-do-it))
17318 (defun org-forward-same-level (arg &optional invisible-ok)
17319 "Move forward to the arg'th subheading at same level as this one.
17320 Stop at the first and last subheadings of a superior heading."
17321 (interactive "p")
17322 (org-back-to-heading invisible-ok)
17323 (org-on-heading-p)
17324 (let* ((level (- (match-end 0) (match-beginning 0) 1))
17325 (re (format "^\\*\\{1,%d\\} " level))
17327 (forward-char 1)
17328 (while (> arg 0)
17329 (while (and (re-search-forward re nil 'move)
17330 (setq l (- (match-end 0) (match-beginning 0) 1))
17331 (= l level)
17332 (not invisible-ok)
17333 (org-invisible-p))
17334 (if (< l level) (setq arg 1)))
17335 (setq arg (1- arg)))
17336 (beginning-of-line 1)))
17338 (defun org-backward-same-level (arg &optional invisible-ok)
17339 "Move backward to the arg'th subheading at same level as this one.
17340 Stop at the first and last subheadings of a superior heading."
17341 (interactive "p")
17342 (org-back-to-heading)
17343 (org-on-heading-p)
17344 (let* ((level (- (match-end 0) (match-beginning 0) 1))
17345 (re (format "^\\*\\{1,%d\\} " level))
17347 (while (> arg 0)
17348 (while (and (re-search-backward re nil 'move)
17349 (setq l (- (match-end 0) (match-beginning 0) 1))
17350 (= l level)
17351 (not invisible-ok)
17352 (org-invisible-p))
17353 (if (< l level) (setq arg 1)))
17354 (setq arg (1- arg)))))
17356 (defun org-show-subtree ()
17357 "Show everything after this heading at deeper levels."
17358 (outline-flag-region
17359 (point)
17360 (save-excursion
17361 (org-end-of-subtree t t))
17362 nil))
17364 (defun org-show-entry ()
17365 "Show the body directly following this heading.
17366 Show the heading too, if it is currently invisible."
17367 (interactive)
17368 (save-excursion
17369 (condition-case nil
17370 (progn
17371 (org-back-to-heading t)
17372 (outline-flag-region
17373 (max (point-min) (1- (point)))
17374 (save-excursion
17375 (if (re-search-forward
17376 (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
17377 (match-beginning 1)
17378 (point-max)))
17379 nil)
17380 (org-cycle-hide-drawers 'children))
17381 (error nil))))
17383 (defun org-make-options-regexp (kwds &optional extra)
17384 "Make a regular expression for keyword lines."
17385 (concat
17387 "#?[ \t]*\\+\\("
17388 (mapconcat 'regexp-quote kwds "\\|")
17389 (if extra (concat "\\|" extra))
17390 "\\):[ \t]*"
17391 "\\(.*\\)"))
17393 ;; Make isearch reveal the necessary context
17394 (defun org-isearch-end ()
17395 "Reveal context after isearch exits."
17396 (when isearch-success ; only if search was successful
17397 (if (featurep 'xemacs)
17398 ;; Under XEmacs, the hook is run in the correct place,
17399 ;; we directly show the context.
17400 (org-show-context 'isearch)
17401 ;; In Emacs the hook runs *before* restoring the overlays.
17402 ;; So we have to use a one-time post-command-hook to do this.
17403 ;; (Emacs 22 has a special variable, see function `org-mode')
17404 (unless (and (boundp 'isearch-mode-end-hook-quit)
17405 isearch-mode-end-hook-quit)
17406 ;; Only when the isearch was not quitted.
17407 (org-add-hook 'post-command-hook 'org-isearch-post-command
17408 'append 'local)))))
17410 (defun org-isearch-post-command ()
17411 "Remove self from hook, and show context."
17412 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
17413 (org-show-context 'isearch))
17416 ;;;; Integration with and fixes for other packages
17418 ;;; Imenu support
17420 (defvar org-imenu-markers nil
17421 "All markers currently used by Imenu.")
17422 (make-variable-buffer-local 'org-imenu-markers)
17424 (defun org-imenu-new-marker (&optional pos)
17425 "Return a new marker for use by Imenu, and remember the marker."
17426 (let ((m (make-marker)))
17427 (move-marker m (or pos (point)))
17428 (push m org-imenu-markers)
17431 (defun org-imenu-get-tree ()
17432 "Produce the index for Imenu."
17433 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
17434 (setq org-imenu-markers nil)
17435 (let* ((n org-imenu-depth)
17436 (re (concat "^" outline-regexp))
17437 (subs (make-vector (1+ n) nil))
17438 (last-level 0)
17439 m level head)
17440 (save-excursion
17441 (save-restriction
17442 (widen)
17443 (goto-char (point-max))
17444 (while (re-search-backward re nil t)
17445 (setq level (org-reduced-level (funcall outline-level)))
17446 (when (<= level n)
17447 (looking-at org-complex-heading-regexp)
17448 (setq head (org-link-display-format
17449 (org-match-string-no-properties 4))
17450 m (org-imenu-new-marker))
17451 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
17452 (if (>= level last-level)
17453 (push (cons head m) (aref subs level))
17454 (push (cons head (aref subs (1+ level))) (aref subs level))
17455 (loop for i from (1+ level) to n do (aset subs i nil)))
17456 (setq last-level level)))))
17457 (aref subs 1)))
17459 (eval-after-load "imenu"
17460 '(progn
17461 (add-hook 'imenu-after-jump-hook
17462 (lambda ()
17463 (if (eq major-mode 'org-mode)
17464 (org-show-context 'org-goto))))))
17466 (defun org-link-display-format (link)
17467 "Replace a link with either the description, or the link target
17468 if no description is present"
17469 (save-match-data
17470 (if (string-match org-bracket-link-analytic-regexp link)
17471 (replace-match (or (match-string 5 link)
17472 (concat (match-string 1 link)
17473 (match-string 3 link)))
17474 nil nil link)
17475 link)))
17477 ;; Speedbar support
17479 (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
17480 "Overlay marking the agenda restriction line in speedbar.")
17481 (org-overlay-put org-speedbar-restriction-lock-overlay
17482 'face 'org-agenda-restriction-lock)
17483 (org-overlay-put org-speedbar-restriction-lock-overlay
17484 'help-echo "Agendas are currently limited to this item.")
17485 (org-detach-overlay org-speedbar-restriction-lock-overlay)
17487 (defun org-speedbar-set-agenda-restriction ()
17488 "Restrict future agenda commands to the location at point in speedbar.
17489 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
17490 (interactive)
17491 (require 'org-agenda)
17492 (let (p m tp np dir txt)
17493 (cond
17494 ((setq p (text-property-any (point-at-bol) (point-at-eol)
17495 'org-imenu t))
17496 (setq m (get-text-property p 'org-imenu-marker))
17497 (save-excursion
17498 (save-restriction
17499 (set-buffer (marker-buffer m))
17500 (goto-char m)
17501 (org-agenda-set-restriction-lock 'subtree))))
17502 ((setq p (text-property-any (point-at-bol) (point-at-eol)
17503 'speedbar-function 'speedbar-find-file))
17504 (setq tp (previous-single-property-change
17505 (1+ p) 'speedbar-function)
17506 np (next-single-property-change
17507 tp 'speedbar-function)
17508 dir (speedbar-line-directory)
17509 txt (buffer-substring-no-properties (or tp (point-min))
17510 (or np (point-max))))
17511 (save-excursion
17512 (save-restriction
17513 (set-buffer (find-file-noselect
17514 (let ((default-directory dir))
17515 (expand-file-name txt))))
17516 (unless (org-mode-p)
17517 (error "Cannot restrict to non-Org-mode file"))
17518 (org-agenda-set-restriction-lock 'file))))
17519 (t (error "Don't know how to restrict Org-mode's agenda")))
17520 (org-move-overlay org-speedbar-restriction-lock-overlay
17521 (point-at-bol) (point-at-eol))
17522 (setq current-prefix-arg nil)
17523 (org-agenda-maybe-redo)))
17525 (eval-after-load "speedbar"
17526 '(progn
17527 (speedbar-add-supported-extension ".org")
17528 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
17529 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
17530 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
17531 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
17532 (add-hook 'speedbar-visiting-tag-hook
17533 (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
17536 ;;; Fixes and Hacks for problems with other packages
17538 ;; Make flyspell not check words in links, to not mess up our keymap
17539 (defun org-mode-flyspell-verify ()
17540 "Don't let flyspell put overlays at active buttons."
17541 (and (not (get-text-property (point) 'keymap))
17542 (not (get-text-property (point) 'org-no-flyspell))))
17544 (defun org-remove-flyspell-overlays-in (beg end)
17545 "Remove flyspell overlays in region."
17546 (and (org-bound-and-true-p flyspell-mode)
17547 (fboundp 'flyspell-delete-region-overlays)
17548 (flyspell-delete-region-overlays beg end))
17549 (add-text-properties beg end '(org-no-flyspell t)))
17551 ;; Make `bookmark-jump' shows the jump location if it was hidden.
17552 (eval-after-load "bookmark"
17553 '(if (boundp 'bookmark-after-jump-hook)
17554 ;; We can use the hook
17555 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
17556 ;; Hook not available, use advice
17557 (defadvice bookmark-jump (after org-make-visible activate)
17558 "Make the position visible."
17559 (org-bookmark-jump-unhide))))
17561 ;; Make sure saveplace shows the location if it was hidden
17562 (eval-after-load "saveplace"
17563 '(defadvice save-place-find-file-hook (after org-make-visible activate)
17564 "Make the position visible."
17565 (org-bookmark-jump-unhide)))
17567 ;; Make sure ecb shows the location if it was hidden
17568 (eval-after-load "ecb"
17569 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
17570 "Make hierarchy visible when jumping into location from ECB tree buffer."
17571 (if (eq major-mode 'org-mode)
17572 (org-show-context))))
17574 (defun org-bookmark-jump-unhide ()
17575 "Unhide the current position, to show the bookmark location."
17576 (and (org-mode-p)
17577 (or (org-invisible-p)
17578 (save-excursion (goto-char (max (point-min) (1- (point))))
17579 (org-invisible-p)))
17580 (org-show-context 'bookmark-jump)))
17582 ;; Make session.el ignore our circular variable
17583 (eval-after-load "session"
17584 '(add-to-list 'session-globals-exclude 'org-mark-ring))
17586 ;;;; Experimental code
17588 (defun org-closed-in-range ()
17589 "Sparse tree of items closed in a certain time range.
17590 Still experimental, may disappear in the future."
17591 (interactive)
17592 ;; Get the time interval from the user.
17593 (let* ((time1 (org-float-time
17594 (org-read-date nil 'to-time nil "Starting date: ")))
17595 (time2 (org-float-time
17596 (org-read-date nil 'to-time nil "End date:")))
17597 ;; callback function
17598 (callback (lambda ()
17599 (let ((time
17600 (org-float-time
17601 (apply 'encode-time
17602 (org-parse-time-string
17603 (match-string 1))))))
17604 ;; check if time in interval
17605 (and (>= time time1) (<= time time2))))))
17606 ;; make tree, check each match with the callback
17607 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
17609 ;;;; Finish up
17611 (provide 'org)
17613 (run-hooks 'org-load-hook)
17615 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
17617 ;;; org.el ends here