Merge commit 'johnw/master'
[org-mode.git] / lisp / org.el
blob265acd135a623f965f2a91cbcc61bdf41fbf12e4
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 (equal goto 2)
8959 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
8960 (prog1
8961 (setq it (list (or org-clock-heading "running clock")
8962 (buffer-file-name
8963 (marker-buffer org-clock-hd-marker))
8965 (marker-position org-clock-hd-marker)))
8966 (setq goto nil)))
8967 (setq it (or rfloc
8968 (save-excursion
8969 (org-refile-get-location
8970 (if goto "Goto: " "Refile to: ") default-buffer
8971 org-refile-allow-creating-parent-nodes)))))
8972 (setq file (nth 1 it)
8973 re (nth 2 it)
8974 pos (nth 3 it))
8975 (if (and (not goto)
8977 (equal (buffer-file-name) file)
8978 (if regionp
8979 (and (>= pos region-start)
8980 (<= pos region-end))
8981 (and (>= pos (point))
8982 (< pos (save-excursion
8983 (org-end-of-subtree t t))))))
8984 (error "Cannot refile to position inside the tree or region"))
8986 (setq nbuf (or (find-buffer-visiting file)
8987 (find-file-noselect file)))
8988 (if goto
8989 (progn
8990 (switch-to-buffer nbuf)
8991 (goto-char pos)
8992 (org-show-context 'org-goto))
8993 (if regionp
8994 (progn
8995 (org-kill-new (buffer-substring region-start region-end))
8996 (org-save-markers-in-region region-start region-end))
8997 (org-copy-subtree 1 nil t))
8998 (save-excursion
8999 (set-buffer (setq nbuf (or (find-buffer-visiting file)
9000 (find-file-noselect file))))
9001 (setq reversed (org-notes-order-reversed-p))
9002 (save-excursion
9003 (save-restriction
9004 (widen)
9005 (if pos
9006 (progn
9007 (goto-char pos)
9008 (looking-at outline-regexp)
9009 (setq level (org-get-valid-level (funcall outline-level) 1))
9010 (goto-char
9011 (if reversed
9012 (or (outline-next-heading) (point-max))
9013 (or (save-excursion (org-get-next-sibling))
9014 (org-end-of-subtree t t)
9015 (point-max)))))
9016 (setq level 1)
9017 (if (not reversed)
9018 (goto-char (point-max))
9019 (goto-char (point-min))
9020 (or (outline-next-heading) (goto-char (point-max)))))
9021 (if (not (bolp)) (newline))
9022 (bookmark-set "org-refile-last-stored")
9023 (org-paste-subtree level)
9024 (if (fboundp 'deactivate-mark) (deactivate-mark))
9025 (run-hooks 'org-after-refile-insert-hook))))
9026 (if regionp
9027 (delete-region (point) (+ (point) region-length))
9028 (org-cut-subtree))
9029 (when (featurep 'org-inlinetask)
9030 (org-inlinetask-remove-END-maybe))
9031 (setq org-markers-to-move nil)
9032 (message "Refiled to \"%s\"" (car it))))))
9033 (org-reveal))
9035 (defun org-refile-goto-last-stored ()
9036 "Go to the location where the last refile was stored."
9037 (interactive)
9038 (bookmark-jump "org-refile-last-stored")
9039 (message "This is the location of the last refile"))
9041 (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
9042 "Prompt the user for a refile location, using PROMPT."
9043 (let ((org-refile-targets org-refile-targets)
9044 (org-refile-use-outline-path org-refile-use-outline-path))
9045 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
9046 (unless org-refile-target-table
9047 (error "No refile targets"))
9048 (let* ((cbuf (current-buffer))
9049 (partial-completion-mode nil)
9050 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
9051 (cfunc (if (and org-refile-use-outline-path
9052 org-outline-path-complete-in-steps)
9053 'org-olpath-completing-read
9054 'org-icompleting-read))
9055 (extra (if org-refile-use-outline-path "/" ""))
9056 (filename (and cfn (expand-file-name cfn)))
9057 (tbl (mapcar
9058 (lambda (x)
9059 (if (and (not (member org-refile-use-outline-path
9060 '(file full-file-path)))
9061 (not (equal filename (nth 1 x))))
9062 (cons (concat (car x) extra " ("
9063 (file-name-nondirectory (nth 1 x)) ")")
9064 (cdr x))
9065 (cons (concat (car x) extra) (cdr x))))
9066 org-refile-target-table))
9067 (completion-ignore-case t)
9068 pa answ parent-target child parent old-hist)
9069 (setq old-hist org-refile-history)
9070 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
9071 nil 'org-refile-history))
9072 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
9073 (if pa
9074 (progn
9075 (when (or (not org-refile-history)
9076 (not (eq old-hist org-refile-history))
9077 (not (equal (car pa) (car org-refile-history))))
9078 (setq org-refile-history
9079 (cons (car pa) (if (assoc (car org-refile-history) tbl)
9080 org-refile-history
9081 (cdr org-refile-history))))
9082 (if (equal (car org-refile-history) (nth 1 org-refile-history))
9083 (pop org-refile-history)))
9085 (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
9086 (setq parent (match-string 1 answ)
9087 child (match-string 2 answ))
9088 (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
9089 (when (and parent-target
9090 (or (eq new-nodes t)
9091 (and (eq new-nodes 'confirm)
9092 (y-or-n-p (format "Create new node \"%s\"? " child)))))
9093 (org-refile-new-child parent-target child))))))
9095 (defun org-refile-new-child (parent-target child)
9096 "Use refile target PARENT-TARGET to add new CHILD below it."
9097 (unless parent-target
9098 (error "Cannot find parent for new node"))
9099 (let ((file (nth 1 parent-target))
9100 (pos (nth 3 parent-target))
9101 level)
9102 (with-current-buffer (or (find-buffer-visiting file)
9103 (find-file-noselect file))
9104 (save-excursion
9105 (save-restriction
9106 (widen)
9107 (if pos
9108 (goto-char pos)
9109 (goto-char (point-max))
9110 (if (not (bolp)) (newline)))
9111 (when (looking-at outline-regexp)
9112 (setq level (funcall outline-level))
9113 (org-end-of-subtree t t))
9114 (org-back-over-empty-lines)
9115 (insert "\n" (make-string
9116 (if pos (org-get-valid-level level 1) 1) ?*)
9117 " " child "\n")
9118 (beginning-of-line 0)
9119 (list (concat (car parent-target) "/" child) file "" (point)))))))
9121 (defun org-olpath-completing-read (prompt collection &rest args)
9122 "Read an outline path like a file name."
9123 (let ((thetable collection)
9124 (org-completion-use-ido nil) ; does not work with ido.
9125 (org-completion-use-iswitchb nil)) ; or iswitchb
9126 (apply
9127 'org-icompleting-read prompt
9128 (lambda (string predicate &optional flag)
9129 (let (rtn r f (l (length string)))
9130 (cond
9131 ((eq flag nil)
9132 ;; try completion
9133 (try-completion string thetable))
9134 ((eq flag t)
9135 ;; all-completions
9136 (setq rtn (all-completions string thetable predicate))
9137 (mapcar
9138 (lambda (x)
9139 (setq r (substring x l))
9140 (if (string-match " ([^)]*)$" x)
9141 (setq f (match-string 0 x))
9142 (setq f ""))
9143 (if (string-match "/" r)
9144 (concat string (substring r 0 (match-end 0)) f)
9146 rtn))
9147 ((eq flag 'lambda)
9148 ;; exact match?
9149 (assoc string thetable)))
9151 args)))
9153 ;;;; Dynamic blocks
9155 (defun org-find-dblock (name)
9156 "Find the first dynamic block with name NAME in the buffer.
9157 If not found, stay at current position and return nil."
9158 (let (pos)
9159 (save-excursion
9160 (goto-char (point-min))
9161 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
9162 nil t)
9163 (match-beginning 0))))
9164 (if pos (goto-char pos))
9165 pos))
9167 (defconst org-dblock-start-re
9168 "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
9169 "Matches the startline of a dynamic block, with parameters.")
9171 (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
9172 "Matches the end of a dynamic block.")
9174 (defun org-create-dblock (plist)
9175 "Create a dynamic block section, with parameters taken from PLIST.
9176 PLIST must contain a :name entry which is used as name of the block."
9177 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
9178 (end-of-line 1)
9179 (newline))
9180 (let ((col (current-column))
9181 (name (plist-get plist :name)))
9182 (insert "#+BEGIN: " name)
9183 (while plist
9184 (if (eq (car plist) :name)
9185 (setq plist (cddr plist))
9186 (insert " " (prin1-to-string (pop plist)))))
9187 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
9188 (beginning-of-line -2)))
9190 (defun org-prepare-dblock ()
9191 "Prepare dynamic block for refresh.
9192 This empties the block, puts the cursor at the insert position and returns
9193 the property list including an extra property :name with the block name."
9194 (unless (looking-at org-dblock-start-re)
9195 (error "Not at a dynamic block"))
9196 (let* ((begdel (1+ (match-end 0)))
9197 (name (org-no-properties (match-string 1)))
9198 (params (append (list :name name)
9199 (read (concat "(" (match-string 3) ")")))))
9200 (save-excursion
9201 (beginning-of-line 1)
9202 (skip-chars-forward " \t")
9203 (setq params (plist-put params :indentation-column (current-column))))
9204 (unless (re-search-forward org-dblock-end-re nil t)
9205 (error "Dynamic block not terminated"))
9206 (setq params
9207 (append params
9208 (list :content (buffer-substring
9209 begdel (match-beginning 0)))))
9210 (delete-region begdel (match-beginning 0))
9211 (goto-char begdel)
9212 (open-line 1)
9213 params))
9215 (defun org-map-dblocks (&optional command)
9216 "Apply COMMAND to all dynamic blocks in the current buffer.
9217 If COMMAND is not given, use `org-update-dblock'."
9218 (let ((cmd (or command 'org-update-dblock))
9219 pos)
9220 (save-excursion
9221 (goto-char (point-min))
9222 (while (re-search-forward org-dblock-start-re nil t)
9223 (goto-char (setq pos (match-beginning 0)))
9224 (condition-case nil
9225 (funcall cmd)
9226 (error (message "Error during update of dynamic block")))
9227 (goto-char pos)
9228 (unless (re-search-forward org-dblock-end-re nil t)
9229 (error "Dynamic block not terminated"))))))
9231 (defun org-dblock-update (&optional arg)
9232 "User command for updating dynamic blocks.
9233 Update the dynamic block at point. With prefix ARG, update all dynamic
9234 blocks in the buffer."
9235 (interactive "P")
9236 (if arg
9237 (org-update-all-dblocks)
9238 (or (looking-at org-dblock-start-re)
9239 (org-beginning-of-dblock))
9240 (org-update-dblock)))
9242 (defun org-update-dblock ()
9243 "Update the dynamic block at point
9244 This means to empty the block, parse for parameters and then call
9245 the correct writing function."
9246 (save-window-excursion
9247 (let* ((pos (point))
9248 (line (org-current-line))
9249 (params (org-prepare-dblock))
9250 (name (plist-get params :name))
9251 (indent (plist-get params :indentation-column))
9252 (cmd (intern (concat "org-dblock-write:" name))))
9253 (message "Updating dynamic block `%s' at line %d..." name line)
9254 (funcall cmd params)
9255 (message "Updating dynamic block `%s' at line %d...done" name line)
9256 (goto-char pos)
9257 (when (and indent (> indent 0))
9258 (setq indent (make-string indent ?\ ))
9259 (save-excursion
9260 (org-beginning-of-dblock)
9261 (forward-line 1)
9262 (while (not (looking-at org-dblock-end-re))
9263 (insert indent)
9264 (beginning-of-line 2))
9265 (when (looking-at org-dblock-end-re)
9266 (and (looking-at "[ \t]+")
9267 (replace-match ""))
9268 (insert indent)))))))
9270 (defun org-beginning-of-dblock ()
9271 "Find the beginning of the dynamic block at point.
9272 Error if there is no such block at point."
9273 (let ((pos (point))
9274 beg)
9275 (end-of-line 1)
9276 (if (and (re-search-backward org-dblock-start-re nil t)
9277 (setq beg (match-beginning 0))
9278 (re-search-forward org-dblock-end-re nil t)
9279 (> (match-end 0) pos))
9280 (goto-char beg)
9281 (goto-char pos)
9282 (error "Not in a dynamic block"))))
9284 (defun org-update-all-dblocks ()
9285 "Update all dynamic blocks in the buffer.
9286 This function can be used in a hook."
9287 (when (org-mode-p)
9288 (org-map-dblocks 'org-update-dblock)))
9291 ;;;; Completion
9293 (defconst org-additional-option-like-keywords
9294 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
9295 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
9296 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "LATEX_CLASS:" "ATTR_LaTeX"
9297 "BEGIN:" "END:"
9298 "ORGTBL" "TBLFM:" "TBLNAME:"
9299 "BEGIN_EXAMPLE" "END_EXAMPLE"
9300 "BEGIN_QUOTE" "END_QUOTE"
9301 "BEGIN_VERSE" "END_VERSE"
9302 "BEGIN_CENTER" "END_CENTER"
9303 "BEGIN_SRC" "END_SRC"
9304 "CATEGORY" "COLUMNS"
9305 "CAPTION" "LABEL"
9306 "SETUPFILE"
9307 "BIND"
9308 "MACRO"))
9310 (defcustom org-structure-template-alist
9312 ("s" "#+begin_src ?\n\n#+end_src"
9313 "<src lang=\"?\">\n\n</src>")
9314 ("e" "#+begin_example\n?\n#+end_example"
9315 "<example>\n?\n</example>")
9316 ("q" "#+begin_quote\n?\n#+end_quote"
9317 "<quote>\n?\n</quote>")
9318 ("v" "#+begin_verse\n?\n#+end_verse"
9319 "<verse>\n?\n/verse>")
9320 ("c" "#+begin_center\n?\n#+end_center"
9321 "<center>\n?\n/center>")
9322 ("l" "#+begin_latex\n?\n#+end_latex"
9323 "<literal style=\"latex\">\n?\n</literal>")
9324 ("L" "#+latex: "
9325 "<literal style=\"latex\">?</literal>")
9326 ("h" "#+begin_html\n?\n#+end_html"
9327 "<literal style=\"html\">\n?\n</literal>")
9328 ("H" "#+html: "
9329 "<literal style=\"html\">?</literal>")
9330 ("a" "#+begin_ascii\n?\n#+end_ascii")
9331 ("A" "#+ascii: ")
9332 ("i" "#+include %file ?"
9333 "<include file=%file markup=\"?\">")
9335 "Structure completion elements.
9336 This is a list of abbreviation keys and values. The value gets inserted
9337 it you type @samp{.} followed by the key and then the completion key,
9338 usually `M-TAB'. %file will be replaced by a file name after prompting
9339 for the file using completion.
9340 There are two templates for each key, the first uses the original Org syntax,
9341 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
9342 the default when the /org-mtags.el/ module has been loaded. See also the
9343 variable `org-mtags-prefer-muse-templates'.
9344 This is an experimental feature, it is undecided if it is going to stay in."
9345 :group 'org-completion
9346 :type '(repeat
9347 (string :tag "Key")
9348 (string :tag "Template")
9349 (string :tag "Muse Template")))
9351 (defun org-try-structure-completion ()
9352 "Try to complete a structure template before point.
9353 This looks for strings like \"<e\" on an otherwise empty line and
9354 expands them."
9355 (let ((l (buffer-substring (point-at-bol) (point)))
9357 (when (and (looking-at "[ \t]*$")
9358 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
9359 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
9360 (org-complete-expand-structure-template (+ -1 (point-at-bol)
9361 (match-beginning 1)) a)
9362 t)))
9364 (defun org-complete-expand-structure-template (start cell)
9365 "Expand a structure template."
9366 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
9367 (rpl (nth (if musep 2 1) cell))
9368 (ind ""))
9369 (delete-region start (point))
9370 (when (string-match "\\`#\\+" rpl)
9371 (cond
9372 ((bolp))
9373 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
9374 (setq ind (buffer-substring (point-at-bol) (point))))
9375 (t (newline))))
9376 (setq start (point))
9377 (if (string-match "%file" rpl)
9378 (setq rpl (replace-match
9379 (concat
9380 "\""
9381 (save-match-data
9382 (abbreviate-file-name (read-file-name "Include file: ")))
9383 "\"")
9384 t t rpl)))
9385 (setq rpl (mapconcat 'identity (split-string rpl "\n")
9386 (concat "\n" ind)))
9387 (insert rpl)
9388 (if (re-search-backward "\\?" start t) (delete-char 1))))
9391 (defun org-complete (&optional arg)
9392 "Perform completion on word at point.
9393 At the beginning of a headline, this completes TODO keywords as given in
9394 `org-todo-keywords'.
9395 If the current word is preceded by a backslash, completes the TeX symbols
9396 that are supported for HTML support.
9397 If the current word is preceded by \"#+\", completes special words for
9398 setting file options.
9399 In the line after \"#+STARTUP:, complete valid keywords.\"
9400 At all other locations, this simply calls the value of
9401 `org-completion-fallback-command'."
9402 (interactive "P")
9403 (org-without-partial-completion
9404 (catch 'exit
9405 (let* ((a nil)
9406 (end (point))
9407 (beg1 (save-excursion
9408 (skip-chars-backward (org-re "[:alnum:]_@"))
9409 (point)))
9410 (beg (save-excursion
9411 (skip-chars-backward "a-zA-Z0-9_:$")
9412 (point)))
9413 (confirm (lambda (x) (stringp (car x))))
9414 (searchhead (equal (char-before beg) ?*))
9415 (struct
9416 (when (and (member (char-before beg1) '(?. ?<))
9417 (setq a (assoc (buffer-substring beg1 (point))
9418 org-structure-template-alist)))
9419 (org-complete-expand-structure-template (1- beg1) a)
9420 (throw 'exit t)))
9421 (tag (and (equal (char-before beg1) ?:)
9422 (equal (char-after (point-at-bol)) ?*)))
9423 (prop (and (equal (char-before beg1) ?:)
9424 (not (equal (char-after (point-at-bol)) ?*))))
9425 (texp (equal (char-before beg) ?\\))
9426 (link (equal (char-before beg) ?\[))
9427 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
9428 beg)
9429 "#+"))
9430 (startup (string-match "^#\\+STARTUP:.*"
9431 (buffer-substring (point-at-bol) (point))))
9432 (completion-ignore-case opt)
9433 (type nil)
9434 (tbl nil)
9435 (table (cond
9436 (opt
9437 (setq type :opt)
9438 (require 'org-exp)
9439 (append
9440 (delq nil
9441 (mapcar
9442 (lambda (x)
9443 (if (string-match
9444 "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
9445 (cons (match-string 2 x)
9446 (match-string 1 x))))
9447 (org-split-string (org-get-current-options) "\n")))
9448 (mapcar 'list org-additional-option-like-keywords)))
9449 (startup
9450 (setq type :startup)
9451 org-startup-options)
9452 (link (append org-link-abbrev-alist-local
9453 org-link-abbrev-alist))
9454 (texp
9455 (setq type :tex)
9456 org-html-entities)
9457 ((string-match "\\`\\*+[ \t]+\\'"
9458 (buffer-substring (point-at-bol) beg))
9459 (setq type :todo)
9460 (mapcar 'list org-todo-keywords-1))
9461 (searchhead
9462 (setq type :searchhead)
9463 (save-excursion
9464 (goto-char (point-min))
9465 (while (re-search-forward org-todo-line-regexp nil t)
9466 (push (list
9467 (org-make-org-heading-search-string
9468 (match-string 3) t))
9469 tbl)))
9470 tbl)
9471 (tag (setq type :tag beg beg1)
9472 (or org-tag-alist (org-get-buffer-tags)))
9473 (prop (setq type :prop beg beg1)
9474 (mapcar 'list (org-buffer-property-keys nil t t)))
9475 (t (progn
9476 (call-interactively org-completion-fallback-command)
9477 (throw 'exit nil)))))
9478 (pattern (buffer-substring-no-properties beg end))
9479 (completion (try-completion pattern table confirm)))
9480 (cond ((eq completion t)
9481 (if (not (assoc (upcase pattern) table))
9482 (message "Already complete")
9483 (if (and (equal type :opt)
9484 (not (member (car (assoc (upcase pattern) table))
9485 org-additional-option-like-keywords)))
9486 (insert (substring (cdr (assoc (upcase pattern) table))
9487 (length pattern)))
9488 (if (memq type '(:tag :prop)) (insert ":")))))
9489 ((null completion)
9490 (message "Can't find completion for \"%s\"" pattern)
9491 (ding))
9492 ((not (string= pattern completion))
9493 (delete-region beg end)
9494 (if (string-match " +$" completion)
9495 (setq completion (replace-match "" t t completion)))
9496 (insert completion)
9497 (if (get-buffer-window "*Completions*")
9498 (delete-window (get-buffer-window "*Completions*")))
9499 (if (assoc completion table)
9500 (if (eq type :todo) (insert " ")
9501 (if (memq type '(:tag :prop)) (insert ":"))))
9502 (if (and (equal type :opt) (assoc completion table))
9503 (message "%s" (substitute-command-keys
9504 "Press \\[org-complete] again to insert example settings"))))
9506 (message "Making completion list...")
9507 (let ((list (sort (all-completions pattern table confirm)
9508 'string<)))
9509 (with-output-to-temp-buffer "*Completions*"
9510 (condition-case nil
9511 ;; Protection needed for XEmacs and emacs 21
9512 (display-completion-list list pattern)
9513 (error (display-completion-list list)))))
9514 (message "Making completion list...%s" "done")))))))
9516 ;;;; TODO, DEADLINE, Comments
9518 (defun org-toggle-comment ()
9519 "Change the COMMENT state of an entry."
9520 (interactive)
9521 (save-excursion
9522 (org-back-to-heading)
9523 (let (case-fold-search)
9524 (if (looking-at (concat outline-regexp
9525 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
9526 (replace-match "" t t nil 1)
9527 (if (looking-at outline-regexp)
9528 (progn
9529 (goto-char (match-end 0))
9530 (insert org-comment-string " ")))))))
9532 (defvar org-last-todo-state-is-todo nil
9533 "This is non-nil when the last TODO state change led to a TODO state.
9534 If the last change removed the TODO tag or switched to DONE, then
9535 this is nil.")
9537 (defvar org-setting-tags nil) ; dynamically skipped
9539 (defun org-parse-local-options (string var)
9540 "Parse STRING for startup setting relevant for variable VAR."
9541 (let ((rtn (symbol-value var))
9542 e opts)
9543 (save-match-data
9544 (if (or (not string) (not (string-match "\\S-" string)))
9546 (setq opts (delq nil (mapcar (lambda (x)
9547 (setq e (assoc x org-startup-options))
9548 (if (eq (nth 1 e) var) e nil))
9549 (org-split-string string "[ \t]+"))))
9550 (if (not opts)
9552 (setq rtn nil)
9553 (while (setq e (pop opts))
9554 (if (not (nth 3 e))
9555 (setq rtn (nth 2 e))
9556 (if (not (listp rtn)) (setq rtn nil))
9557 (push (nth 2 e) rtn)))
9558 rtn)))))
9560 (defvar org-todo-setup-filter-hook nil
9561 "Hook for functions that pre-filter todo specs.
9563 Each function takes a todo spec and returns either `nil' or the spec
9564 transformed into canonical form." )
9566 (defvar org-todo-get-default-hook nil
9567 "Hook for functions that get a default item for todo.
9569 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
9570 `nil' or a string to be used for the todo mark." )
9572 (defvar org-agenda-headline-snapshot-before-repeat)
9574 (defun org-todo (&optional arg)
9575 "Change the TODO state of an item.
9576 The state of an item is given by a keyword at the start of the heading,
9577 like
9578 *** TODO Write paper
9579 *** DONE Call mom
9581 The different keywords are specified in the variable `org-todo-keywords'.
9582 By default the available states are \"TODO\" and \"DONE\".
9583 So for this example: when the item starts with TODO, it is changed to DONE.
9584 When it starts with DONE, the DONE is removed. And when neither TODO nor
9585 DONE are present, add TODO at the beginning of the heading.
9587 With C-u prefix arg, use completion to determine the new state.
9588 With numeric prefix arg, switch to that state.
9589 With a double C-u prefix, switch to the next set of TODO keywords (nextset).
9590 With a tripple C-u prefix, circumvent any state blocking.
9592 For calling through lisp, arg is also interpreted in the following way:
9593 'none -> empty state
9594 \"\"(empty string) -> switch to empty state
9595 'done -> switch to DONE
9596 'nextset -> switch to the next set of keywords
9597 'previousset -> switch to the previous set of keywords
9598 \"WAITING\" -> switch to the specified keyword, but only if it
9599 really is a member of `org-todo-keywords'."
9600 (interactive "P")
9601 (if (equal arg '(16)) (setq arg 'nextset))
9602 (let ((org-blocker-hook org-blocker-hook)
9603 (case-fold-search nil))
9604 (when (equal arg '(64))
9605 (setq arg nil org-blocker-hook nil))
9606 (when (and org-blocker-hook
9607 (or org-inhibit-blocking
9608 (org-entry-get nil "NOBLOCKING")))
9609 (setq org-blocker-hook nil))
9610 (save-excursion
9611 (catch 'exit
9612 (org-back-to-heading t)
9613 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
9614 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
9615 (looking-at " *"))
9616 (let* ((match-data (match-data))
9617 (startpos (point-at-bol))
9618 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
9619 (org-log-done org-log-done)
9620 (org-log-repeat org-log-repeat)
9621 (org-todo-log-states org-todo-log-states)
9622 (this (match-string 1))
9623 (hl-pos (match-beginning 0))
9624 (head (org-get-todo-sequence-head this))
9625 (ass (assoc head org-todo-kwd-alist))
9626 (interpret (nth 1 ass))
9627 (done-word (nth 3 ass))
9628 (final-done-word (nth 4 ass))
9629 (last-state (or this ""))
9630 (completion-ignore-case t)
9631 (member (member this org-todo-keywords-1))
9632 (tail (cdr member))
9633 (state (cond
9634 ((and org-todo-key-trigger
9635 (or (and (equal arg '(4))
9636 (eq org-use-fast-todo-selection 'prefix))
9637 (and (not arg) org-use-fast-todo-selection
9638 (not (eq org-use-fast-todo-selection
9639 'prefix)))))
9640 ;; Use fast selection
9641 (org-fast-todo-selection))
9642 ((and (equal arg '(4))
9643 (or (not org-use-fast-todo-selection)
9644 (not org-todo-key-trigger)))
9645 ;; Read a state with completion
9646 (org-icompleting-read
9647 "State: " (mapcar (lambda(x) (list x))
9648 org-todo-keywords-1)
9649 nil t))
9650 ((eq arg 'right)
9651 (if this
9652 (if tail (car tail) nil)
9653 (car org-todo-keywords-1)))
9654 ((eq arg 'left)
9655 (if (equal member org-todo-keywords-1)
9657 (if this
9658 (nth (- (length org-todo-keywords-1)
9659 (length tail) 2)
9660 org-todo-keywords-1)
9661 (org-last org-todo-keywords-1))))
9662 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
9663 (setq arg nil))) ; hack to fall back to cycling
9664 (arg
9665 ;; user or caller requests a specific state
9666 (cond
9667 ((equal arg "") nil)
9668 ((eq arg 'none) nil)
9669 ((eq arg 'done) (or done-word (car org-done-keywords)))
9670 ((eq arg 'nextset)
9671 (or (car (cdr (member head org-todo-heads)))
9672 (car org-todo-heads)))
9673 ((eq arg 'previousset)
9674 (let ((org-todo-heads (reverse org-todo-heads)))
9675 (or (car (cdr (member head org-todo-heads)))
9676 (car org-todo-heads))))
9677 ((car (member arg org-todo-keywords-1)))
9678 ((stringp arg)
9679 (error "State `%s' not valid in this file" arg))
9680 ((nth (1- (prefix-numeric-value arg))
9681 org-todo-keywords-1))))
9682 ((null member) (or head (car org-todo-keywords-1)))
9683 ((equal this final-done-word) nil) ;; -> make empty
9684 ((null tail) nil) ;; -> first entry
9685 ((memq interpret '(type priority))
9686 (if (eq this-command last-command)
9687 (car tail)
9688 (if (> (length tail) 0)
9689 (or done-word (car org-done-keywords))
9690 nil)))
9692 (car tail))))
9693 (state (or
9694 (run-hook-with-args-until-success
9695 'org-todo-get-default-hook state last-state)
9696 state))
9697 (next (if state (concat " " state " ") " "))
9698 (change-plist (list :type 'todo-state-change :from this :to state
9699 :position startpos))
9700 dolog now-done-p)
9701 (when org-blocker-hook
9702 (setq org-last-todo-state-is-todo
9703 (not (member this org-done-keywords)))
9704 (unless (save-excursion
9705 (save-match-data
9706 (run-hook-with-args-until-failure
9707 'org-blocker-hook change-plist)))
9708 (if (interactive-p)
9709 (error "TODO state change from %s to %s blocked" this state)
9710 ;; fail silently
9711 (message "TODO state change from %s to %s blocked" this state)
9712 (throw 'exit nil))))
9713 (store-match-data match-data)
9714 (replace-match next t t)
9715 (unless (pos-visible-in-window-p hl-pos)
9716 (message "TODO state changed to %s" (org-trim next)))
9717 (unless head
9718 (setq head (org-get-todo-sequence-head state)
9719 ass (assoc head org-todo-kwd-alist)
9720 interpret (nth 1 ass)
9721 done-word (nth 3 ass)
9722 final-done-word (nth 4 ass)))
9723 (when (memq arg '(nextset previousset))
9724 (message "Keyword-Set %d/%d: %s"
9725 (- (length org-todo-sets) -1
9726 (length (memq (assoc state org-todo-sets) org-todo-sets)))
9727 (length org-todo-sets)
9728 (mapconcat 'identity (assoc state org-todo-sets) " ")))
9729 (setq org-last-todo-state-is-todo
9730 (not (member state org-done-keywords)))
9731 (setq now-done-p (and (member state org-done-keywords)
9732 (not (member this org-done-keywords))))
9733 (and logging (org-local-logging logging))
9734 (when (and (or org-todo-log-states org-log-done)
9735 (not (eq org-inhibit-logging t))
9736 (not (memq arg '(nextset previousset))))
9737 ;; we need to look at recording a time and note
9738 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
9739 (nth 2 (assoc this org-todo-log-states))))
9740 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
9741 (setq dolog 'time))
9742 (when (and state
9743 (member state org-not-done-keywords)
9744 (not (member this org-not-done-keywords)))
9745 ;; This is now a todo state and was not one before
9746 ;; If there was a CLOSED time stamp, get rid of it.
9747 (org-add-planning-info nil nil 'closed))
9748 (when (and now-done-p org-log-done)
9749 ;; It is now done, and it was not done before
9750 (org-add-planning-info 'closed (org-current-time))
9751 (if (and (not dolog) (eq 'note org-log-done))
9752 (org-add-log-setup 'done state this 'findpos 'note)))
9753 (when (and state dolog)
9754 ;; This is a non-nil state, and we need to log it
9755 (org-add-log-setup 'state state this 'findpos dolog)))
9756 ;; Fixup tag positioning
9757 (org-todo-trigger-tag-changes state)
9758 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
9759 (when org-provide-todo-statistics
9760 (org-update-parent-todo-statistics))
9761 (run-hooks 'org-after-todo-state-change-hook)
9762 (if (and arg (not (member state org-done-keywords)))
9763 (setq head (org-get-todo-sequence-head state)))
9764 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
9765 ;; Do we need to trigger a repeat?
9766 (when now-done-p
9767 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
9768 ;; This is for the agenda, take a snapshot of the headline.
9769 (save-match-data
9770 (setq org-agenda-headline-snapshot-before-repeat
9771 (org-get-heading))))
9772 (org-auto-repeat-maybe state))
9773 ;; Fixup cursor location if close to the keyword
9774 (if (and (outline-on-heading-p)
9775 (not (bolp))
9776 (save-excursion (beginning-of-line 1)
9777 (looking-at org-todo-line-regexp))
9778 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
9779 (progn
9780 (goto-char (or (match-end 2) (match-end 1)))
9781 (and (looking-at " ") (just-one-space))))
9782 (when org-trigger-hook
9783 (save-excursion
9784 (run-hook-with-args 'org-trigger-hook change-plist))))))))
9786 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
9787 "Block turning an entry into a TODO, using the hierarchy.
9788 This checks whether the current task should be blocked from state
9789 changes. Such blocking occurs when:
9791 1. The task has children which are not all in a completed state.
9793 2. A task has a parent with the property :ORDERED:, and there
9794 are siblings prior to the current task with incomplete
9795 status.
9797 3. The parent of the task is blocked because it has siblings that should
9798 be done first, or is child of a block grandparent TODO entry."
9800 (catch 'dont-block
9801 ;; If this is not a todo state change, or if this entry is already DONE,
9802 ;; do not block
9803 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
9804 (member (plist-get change-plist :from)
9805 (cons 'done org-done-keywords))
9806 (member (plist-get change-plist :to)
9807 (cons 'todo org-not-done-keywords)))
9808 (throw 'dont-block t))
9809 ;; If this task has children, and any are undone, it's blocked
9810 (save-excursion
9811 (org-back-to-heading t)
9812 (let ((this-level (funcall outline-level)))
9813 (outline-next-heading)
9814 (let ((child-level (funcall outline-level)))
9815 (while (and (not (eobp))
9816 (> child-level this-level))
9817 ;; this todo has children, check whether they are all
9818 ;; completed
9819 (if (and (not (org-entry-is-done-p))
9820 (org-entry-is-todo-p))
9821 (throw 'dont-block nil))
9822 (outline-next-heading)
9823 (setq child-level (funcall outline-level))))))
9824 ;; Otherwise, if the task's parent has the :ORDERED: property, and
9825 ;; any previous siblings are undone, it's blocked
9826 (save-excursion
9827 (org-back-to-heading t)
9828 (let* ((pos (point))
9829 (parent-pos (and (org-up-heading-safe) (point))))
9830 (if (not parent-pos) (throw 'dont-block t)) ; no parent
9831 (when (and (org-entry-get (point) "ORDERED")
9832 (forward-line 1)
9833 (re-search-forward org-not-done-heading-regexp pos t))
9834 (throw 'dont-block nil)) ; block, there is an older sibling not done.
9835 ;; Search further up the hierarchy, to see if an anchestor is blocked
9836 (while t
9837 (goto-char parent-pos)
9838 (if (not (looking-at org-not-done-heading-regexp))
9839 (throw 'dont-block t)) ; do not block, parent is not a TODO
9840 (setq pos (point))
9841 (setq parent-pos (and (org-up-heading-safe) (point)))
9842 (if (not parent-pos) (throw 'dont-block t)) ; no parent
9843 (when (and (org-entry-get (point) "ORDERED")
9844 (forward-line 1)
9845 (re-search-forward org-not-done-heading-regexp pos t))
9846 (throw 'dont-block nil))))))) ; block, older sibling not done.
9848 (defcustom org-track-ordered-property-with-tag nil
9849 "Should the ORDERED property also be shown as a tag?
9850 The ORDERED property decides if an entry should require subtasks to be
9851 completed in sequence. Since a property is not very visible, setting
9852 this option means that toggling the ORDERED property with the command
9853 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
9854 not relevant for the behavior, but it makes things more visible.
9856 Note that toggling the tag with tags commands will not change the property
9857 and therefore not influence behavior!
9859 This can be t, meaning the tag ORDERED should be used, It can also be a
9860 string to select a different tag for this task."
9861 :group 'org-todo
9862 :type '(choice
9863 (const :tag "No tracking" nil)
9864 (const :tag "Track with ORDERED tag" t)
9865 (string :tag "Use other tag")))
9867 (defun org-toggle-ordered-property ()
9868 "Toggle the ORDERED property of the current entry.
9869 For better visibility, you can track the value of this property with a tag.
9870 See variable `org-track-ordered-property-with-tag'."
9871 (interactive)
9872 (let* ((t1 org-track-ordered-property-with-tag)
9873 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
9874 (save-excursion
9875 (org-back-to-heading)
9876 (if (org-entry-get nil "ORDERED")
9877 (progn
9878 (org-delete-property "ORDERED")
9879 (and tag (org-toggle-tag tag 'off))
9880 (message "Subtasks can be completed in arbitrary order"))
9881 (org-entry-put nil "ORDERED" "t")
9882 (and tag (org-toggle-tag tag 'on))
9883 (message "Subtasks must be completed in sequence")))))
9885 (defvar org-blocked-by-checkboxes) ; dynamically scoped
9886 (defun org-block-todo-from-checkboxes (change-plist)
9887 "Block turning an entry into a TODO, using checkboxes.
9888 This checks whether the current task should be blocked from state
9889 changes because there are uncheckd boxes in this entry."
9890 (catch 'dont-block
9891 ;; If this is not a todo state change, or if this entry is already DONE,
9892 ;; do not block
9893 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
9894 (member (plist-get change-plist :from)
9895 (cons 'done org-done-keywords))
9896 (member (plist-get change-plist :to)
9897 (cons 'todo org-not-done-keywords)))
9898 (throw 'dont-block t))
9899 ;; If this task has checkboxes that are not checked, it's blocked
9900 (save-excursion
9901 (org-back-to-heading t)
9902 (let ((beg (point)) end)
9903 (outline-next-heading)
9904 (setq end (point))
9905 (goto-char beg)
9906 (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
9907 end t)
9908 (progn
9909 (if (boundp 'org-blocked-by-checkboxes)
9910 (setq org-blocked-by-checkboxes t))
9911 (throw 'dont-block nil)))))
9912 t)) ; do not block
9914 (defun org-update-statistics-cookies (all)
9915 "Update the statistics cookie, either from TODO or from checkboxes.
9916 This should be called with the cursor in a line with a statistics cookie."
9917 (interactive "P")
9918 (if all
9919 (progn
9920 (org-update-checkbox-count 'all)
9921 (org-map-entries 'org-update-parent-todo-statistics))
9922 (if (not (org-on-heading-p))
9923 (org-update-checkbox-count)
9924 (let ((pos (move-marker (make-marker) (point)))
9925 end l1 l2)
9926 (ignore-errors (org-back-to-heading t))
9927 (if (not (org-on-heading-p))
9928 (org-update-checkbox-count)
9929 (setq l1 (org-outline-level))
9930 (setq end (save-excursion
9931 (outline-next-heading)
9932 (if (org-on-heading-p) (setq l2 (org-outline-level)))
9933 (point)))
9934 (if (and (save-excursion (re-search-forward
9935 "^[ \t]*[-+*] \\[[- X]\\]" end t))
9936 (not (save-excursion (re-search-forward
9937 ":COOKIE_DATA:.*\\<todo\\>" end t))))
9938 (org-update-checkbox-count)
9939 (if (and l2 (> l2 l1))
9940 (progn
9941 (goto-char end)
9942 (org-update-parent-todo-statistics))
9943 (error "No data for statistics cookie"))))
9944 (goto-char pos)
9945 (move-marker pos nil)))))
9947 (defvar org-entry-property-inherited-from) ;; defined below
9948 (defun org-update-parent-todo-statistics ()
9949 "Update any statistics cookie in the parent of the current headline.
9950 When `org-hierarchical-todo-statistics' is nil, statistics will cover
9951 the entire subtree and this will travel up the hierarchy and update
9952 statistics everywhere."
9953 (interactive)
9954 (let* ((lim 0) prop
9955 (recursive (or (not org-hierarchical-todo-statistics)
9956 (string-match
9957 "\\<recursive\\>"
9958 (or (setq prop (org-entry-get
9959 nil "COOKIE_DATA" 'inherit)) ""))))
9960 (lim (or (and prop (marker-position
9961 org-entry-property-inherited-from))
9962 lim))
9963 (first t)
9964 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
9965 level ltoggle l1 new ndel
9966 (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
9967 (catch 'exit
9968 (save-excursion
9969 (beginning-of-line 1)
9970 (if (org-at-heading-p)
9971 (setq ltoggle (funcall outline-level))
9972 (error "This should not happen"))
9973 (while (and (setq level (org-up-heading-safe))
9974 (or recursive first)
9975 (>= (point) lim))
9976 (setq first nil cookie-present nil)
9977 (unless (and level
9978 (not (string-match
9979 "\\<checkbox\\>"
9980 (downcase
9981 (or (org-entry-get
9982 nil "COOKIE_DATA")
9983 "")))))
9984 (throw 'exit nil))
9985 (while (re-search-forward box-re (point-at-eol) t)
9986 (setq cnt-all 0 cnt-done 0 cookie-present t)
9987 (setq is-percent (match-end 2))
9988 (save-match-data
9989 (unless (outline-next-heading) (throw 'exit nil))
9990 (while (and (looking-at org-complex-heading-regexp)
9991 (> (setq l1 (length (match-string 1))) level))
9992 (setq kwd (and (or recursive (= l1 ltoggle))
9993 (match-string 2)))
9994 (if (or (eq org-provide-todo-statistics 'all-headlines)
9995 (and (listp org-provide-todo-statistics)
9996 (or (member kwd org-provide-todo-statistics)
9997 (member kwd org-done-keywords))))
9998 (setq cnt-all (1+ cnt-all))
9999 (if (eq org-provide-todo-statistics t)
10000 (and kwd (setq cnt-all (1+ cnt-all)))))
10001 (and (member kwd org-done-keywords)
10002 (setq cnt-done (1+ cnt-done)))
10003 (outline-next-heading)))
10004 (setq new
10005 (if is-percent
10006 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
10007 (format "[%d/%d]" cnt-done cnt-all))
10008 ndel (- (match-end 0) (match-beginning 0)))
10009 (goto-char (match-beginning 0))
10010 (insert new)
10011 (delete-region (point) (+ (point) ndel)))
10012 (when cookie-present
10013 (run-hook-with-args 'org-after-todo-statistics-hook
10014 cnt-done (- cnt-all cnt-done))))))
10015 (run-hooks 'org-todo-statistics-hook)))
10017 (defvar org-after-todo-statistics-hook nil
10018 "Hook that is called after a TODO statistics cookie has been updated.
10019 Each function is called with two arguments: the number of not-done entries
10020 and the number of done entries.
10022 For example, the following function, when added to this hook, will switch
10023 an entry to DONE when all children are done, and back to TODO when new
10024 entries are set to a TODO status. Note that this hook is only called
10025 when there is a statistics cookie in the headline!
10027 (defun org-summary-todo (n-done n-not-done)
10028 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
10029 (let (org-log-done org-log-states) ; turn off logging
10030 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
10033 (defvar org-todo-statistics-hook nil
10034 "Hook that is run whenever Org thinks TODO statistics should be updated.
10035 This hook runs even if there is no statisics cookie present, in which case
10036 `org-after-todo-statistics-hook' would not run.")
10038 (defun org-todo-trigger-tag-changes (state)
10039 "Apply the changes defined in `org-todo-state-tags-triggers'."
10040 (let ((l org-todo-state-tags-triggers)
10041 changes)
10042 (when (or (not state) (equal state ""))
10043 (setq changes (append changes (cdr (assoc "" l)))))
10044 (when (and (stringp state) (> (length state) 0))
10045 (setq changes (append changes (cdr (assoc state l)))))
10046 (when (member state org-not-done-keywords)
10047 (setq changes (append changes (cdr (assoc 'todo l)))))
10048 (when (member state org-done-keywords)
10049 (setq changes (append changes (cdr (assoc 'done l)))))
10050 (dolist (c changes)
10051 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
10053 (defun org-local-logging (value)
10054 "Get logging settings from a property VALUE."
10055 (let* (words w a)
10056 ;; directly set the variables, they are already local.
10057 (setq org-log-done nil
10058 org-log-repeat nil
10059 org-todo-log-states nil)
10060 (setq words (org-split-string value))
10061 (while (setq w (pop words))
10062 (cond
10063 ((setq a (assoc w org-startup-options))
10064 (and (member (nth 1 a) '(org-log-done org-log-repeat))
10065 (set (nth 1 a) (nth 2 a))))
10066 ((setq a (org-extract-log-state-settings w))
10067 (and (member (car a) org-todo-keywords-1)
10068 (push a org-todo-log-states)))))))
10070 (defun org-get-todo-sequence-head (kwd)
10071 "Return the head of the TODO sequence to which KWD belongs.
10072 If KWD is not set, check if there is a text property remembering the
10073 right sequence."
10074 (let (p)
10075 (cond
10076 ((not kwd)
10077 (or (get-text-property (point-at-bol) 'org-todo-head)
10078 (progn
10079 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
10080 nil (point-at-eol)))
10081 (get-text-property p 'org-todo-head))))
10082 ((not (member kwd org-todo-keywords-1))
10083 (car org-todo-keywords-1))
10084 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
10086 (defun org-fast-todo-selection ()
10087 "Fast TODO keyword selection with single keys.
10088 Returns the new TODO keyword, or nil if no state change should occur."
10089 (let* ((fulltable org-todo-key-alist)
10090 (done-keywords org-done-keywords) ;; needed for the faces.
10091 (maxlen (apply 'max (mapcar
10092 (lambda (x)
10093 (if (stringp (car x)) (string-width (car x)) 0))
10094 fulltable)))
10095 (expert nil)
10096 (fwidth (+ maxlen 3 1 3))
10097 (ncol (/ (- (window-width) 4) fwidth))
10098 tg cnt e c tbl
10099 groups ingroup)
10100 (save-excursion
10101 (save-window-excursion
10102 (if expert
10103 (set-buffer (get-buffer-create " *Org todo*"))
10104 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
10105 (erase-buffer)
10106 (org-set-local 'org-done-keywords done-keywords)
10107 (setq tbl fulltable cnt 0)
10108 (while (setq e (pop tbl))
10109 (cond
10110 ((equal e '(:startgroup))
10111 (push '() groups) (setq ingroup t)
10112 (when (not (= cnt 0))
10113 (setq cnt 0)
10114 (insert "\n"))
10115 (insert "{ "))
10116 ((equal e '(:endgroup))
10117 (setq ingroup nil cnt 0)
10118 (insert "}\n"))
10119 ((equal e '(:newline))
10120 (when (not (= cnt 0))
10121 (setq cnt 0)
10122 (insert "\n")
10123 (setq e (car tbl))
10124 (while (equal (car tbl) '(:newline))
10125 (insert "\n")
10126 (setq tbl (cdr tbl)))))
10128 (setq tg (car e) c (cdr e))
10129 (if ingroup (push tg (car groups)))
10130 (setq tg (org-add-props tg nil 'face
10131 (org-get-todo-face tg)))
10132 (if (and (= cnt 0) (not ingroup)) (insert " "))
10133 (insert "[" c "] " tg (make-string
10134 (- fwidth 4 (length tg)) ?\ ))
10135 (when (= (setq cnt (1+ cnt)) ncol)
10136 (insert "\n")
10137 (if ingroup (insert " "))
10138 (setq cnt 0)))))
10139 (insert "\n")
10140 (goto-char (point-min))
10141 (if (not expert) (org-fit-window-to-buffer))
10142 (message "[a-z..]:Set [SPC]:clear")
10143 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
10144 (cond
10145 ((or (= c ?\C-g)
10146 (and (= c ?q) (not (rassoc c fulltable))))
10147 (setq quit-flag t))
10148 ((= c ?\ ) nil)
10149 ((setq e (rassoc c fulltable) tg (car e))
10151 (t (setq quit-flag t)))))))
10153 (defun org-entry-is-todo-p ()
10154 (member (org-get-todo-state) org-not-done-keywords))
10156 (defun org-entry-is-done-p ()
10157 (member (org-get-todo-state) org-done-keywords))
10159 (defun org-get-todo-state ()
10160 (save-excursion
10161 (org-back-to-heading t)
10162 (and (looking-at org-todo-line-regexp)
10163 (match-end 2)
10164 (match-string 2))))
10166 (defun org-at-date-range-p (&optional inactive-ok)
10167 "Is the cursor inside a date range?"
10168 (interactive)
10169 (save-excursion
10170 (catch 'exit
10171 (let ((pos (point)))
10172 (skip-chars-backward "^[<\r\n")
10173 (skip-chars-backward "<[")
10174 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
10175 (>= (match-end 0) pos)
10176 (throw 'exit t))
10177 (skip-chars-backward "^<[\r\n")
10178 (skip-chars-backward "<[")
10179 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
10180 (>= (match-end 0) pos)
10181 (throw 'exit t)))
10182 nil)))
10184 (defun org-get-repeat (&optional tagline)
10185 "Check if there is a deadline/schedule with repeater in this entry."
10186 (save-match-data
10187 (save-excursion
10188 (org-back-to-heading t)
10189 (and (re-search-forward (if tagline
10190 (concat tagline "\\s-*" org-repeat-re)
10191 org-repeat-re)
10192 (org-entry-end-position) t)
10193 (match-string-no-properties 1)))))
10195 (defvar org-last-changed-timestamp)
10196 (defvar org-last-inserted-timestamp)
10197 (defvar org-log-post-message)
10198 (defvar org-log-note-purpose)
10199 (defvar org-log-note-how)
10200 (defvar org-log-note-extra)
10201 (defun org-auto-repeat-maybe (done-word)
10202 "Check if the current headline contains a repeated deadline/schedule.
10203 If yes, set TODO state back to what it was and change the base date
10204 of repeating deadline/scheduled time stamps to new date.
10205 This function is run automatically after each state change to a DONE state."
10206 ;; last-state is dynamically scoped into this function
10207 (let* ((repeat (org-get-repeat))
10208 (aa (assoc last-state org-todo-kwd-alist))
10209 (interpret (nth 1 aa))
10210 (head (nth 2 aa))
10211 (whata '(("d" . day) ("m" . month) ("y" . year)))
10212 (msg "Entry repeats: ")
10213 (org-log-done nil)
10214 (org-todo-log-states nil)
10215 (nshiftmax 10) (nshift 0)
10216 re type n what ts time)
10217 (when repeat
10218 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
10219 (org-todo (if (eq interpret 'type) last-state head))
10220 (org-entry-put nil "LAST_REPEAT" (format-time-string
10221 (org-time-stamp-format t t)))
10222 (when org-log-repeat
10223 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
10224 (memq 'org-add-log-note post-command-hook))
10225 ;; OK, we are already setup for some record
10226 (if (eq org-log-repeat 'note)
10227 ;; make sure we take a note, not only a time stamp
10228 (setq org-log-note-how 'note))
10229 ;; Set up for taking a record
10230 (org-add-log-setup 'state (or done-word (car org-done-keywords))
10231 last-state
10232 'findpos org-log-repeat)))
10233 (org-back-to-heading t)
10234 (org-add-planning-info nil nil 'closed)
10235 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
10236 org-deadline-time-regexp "\\)\\|\\("
10237 org-ts-regexp "\\)"))
10238 (while (re-search-forward
10239 re (save-excursion (outline-next-heading) (point)) t)
10240 (setq type (if (match-end 1) org-scheduled-string
10241 (if (match-end 3) org-deadline-string "Plain:"))
10242 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
10243 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
10244 (setq n (string-to-number (match-string 2 ts))
10245 what (match-string 3 ts))
10246 (if (equal what "w") (setq n (* n 7) what "d"))
10247 ;; Preparation, see if we need to modify the start date for the change
10248 (when (match-end 1)
10249 (setq time (save-match-data (org-time-string-to-time ts)))
10250 (cond
10251 ((equal (match-string 1 ts) ".")
10252 ;; Shift starting date to today
10253 (org-timestamp-change
10254 (- (time-to-days (current-time)) (time-to-days time))
10255 'day))
10256 ((equal (match-string 1 ts) "+")
10257 (while (or (= nshift 0)
10258 (<= (time-to-days time) (time-to-days (current-time))))
10259 (when (= (incf nshift) nshiftmax)
10260 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
10261 (error "Abort")))
10262 (org-timestamp-change n (cdr (assoc what whata)))
10263 (org-at-timestamp-p t)
10264 (setq ts (match-string 1))
10265 (setq time (save-match-data (org-time-string-to-time ts))))
10266 (org-timestamp-change (- n) (cdr (assoc what whata)))
10267 ;; rematch, so that we have everything in place for the real shift
10268 (org-at-timestamp-p t)
10269 (setq ts (match-string 1))
10270 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
10271 (org-timestamp-change n (cdr (assoc what whata)))
10272 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
10273 (setq org-log-post-message msg)
10274 (message "%s" msg))))
10276 (defun org-show-todo-tree (arg)
10277 "Make a compact tree which shows all headlines marked with TODO.
10278 The tree will show the lines where the regexp matches, and all higher
10279 headlines above the match.
10280 With a \\[universal-argument] prefix, prompt for a regexp to match.
10281 With a numeric prefix N, construct a sparse tree for the Nth element
10282 of `org-todo-keywords-1'."
10283 (interactive "P")
10284 (let ((case-fold-search nil)
10285 (kwd-re
10286 (cond ((null arg) org-not-done-regexp)
10287 ((equal arg '(4))
10288 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
10289 (mapcar 'list org-todo-keywords-1))))
10290 (concat "\\("
10291 (mapconcat 'identity (org-split-string kwd "|") "\\|")
10292 "\\)\\>")))
10293 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
10294 (regexp-quote (nth (1- (prefix-numeric-value arg))
10295 org-todo-keywords-1)))
10296 (t (error "Invalid prefix argument: %s" arg)))))
10297 (message "%d TODO entries found"
10298 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
10300 (defun org-deadline (&optional remove time)
10301 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
10302 With argument REMOVE, remove any deadline from the item.
10303 When TIME is set, it should be an internal time specification, and the
10304 scheduling will use the corresponding date."
10305 (interactive "P")
10306 (if remove
10307 (progn
10308 (org-remove-timestamp-with-keyword org-deadline-string)
10309 (message "Item no longer has a deadline."))
10310 (if (org-get-repeat)
10311 (error "Cannot change deadline on task with repeater, please do that by hand")
10312 (org-add-planning-info 'deadline time 'closed)
10313 (message "Deadline on %s" org-last-inserted-timestamp))))
10315 (defun org-schedule (&optional remove time)
10316 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
10317 With argument REMOVE, remove any scheduling date from the item.
10318 When TIME is set, it should be an internal time specification, and the
10319 scheduling will use the corresponding date."
10320 (interactive "P")
10321 (if remove
10322 (progn
10323 (org-remove-timestamp-with-keyword org-scheduled-string)
10324 (message "Item is no longer scheduled."))
10325 (if (org-get-repeat)
10326 (error "Cannot reschedule task with repeater, please do that by hand")
10327 (org-add-planning-info 'scheduled time 'closed)
10328 (message "Scheduled to %s" org-last-inserted-timestamp))))
10330 (defun org-get-scheduled-time (pom &optional inherit)
10331 "Get the scheduled time as a time tuple, of a format suitable
10332 for calling org-schedule with, or if there is no scheduling,
10333 returns nil."
10334 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
10335 (when time
10336 (apply 'encode-time (org-parse-time-string time)))))
10338 (defun org-get-deadline-time (pom &optional inherit)
10339 "Get the deadine as a time tuple, of a format suitable for
10340 calling org-deadlin with, or if there is no scheduling, returns
10341 nil."
10342 (let ((time (org-entry-get pom "DEADLINE" inherit)))
10343 (when time
10344 (apply 'encode-time (org-parse-time-string time)))))
10346 (defun org-remove-timestamp-with-keyword (keyword)
10347 "Remove all time stamps with KEYWORD in the current entry."
10348 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
10349 beg)
10350 (save-excursion
10351 (org-back-to-heading t)
10352 (setq beg (point))
10353 (outline-next-heading)
10354 (while (re-search-backward re beg t)
10355 (replace-match "")
10356 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
10357 (equal (char-before) ?\ ))
10358 (backward-delete-char 1)
10359 (if (string-match "^[ \t]*$" (buffer-substring
10360 (point-at-bol) (point-at-eol)))
10361 (delete-region (point-at-bol)
10362 (min (point-max) (1+ (point-at-eol))))))))))
10364 (defun org-add-planning-info (what &optional time &rest remove)
10365 "Insert new timestamp with keyword in the line directly after the headline.
10366 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
10367 If non is given, the user is prompted for a date.
10368 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
10369 be removed."
10370 (interactive)
10371 (let (org-time-was-given org-end-time-was-given ts
10372 end default-time default-input)
10374 (catch 'exit
10375 (when (and (not time) (memq what '(scheduled deadline)))
10376 ;; Try to get a default date/time from existing timestamp
10377 (save-excursion
10378 (org-back-to-heading t)
10379 (setq end (save-excursion (outline-next-heading) (point)))
10380 (when (re-search-forward (if (eq what 'scheduled)
10381 org-scheduled-time-regexp
10382 org-deadline-time-regexp)
10383 end t)
10384 (setq ts (match-string 1)
10385 default-time
10386 (apply 'encode-time (org-parse-time-string ts))
10387 default-input (and ts (org-get-compact-tod ts))))))
10388 (when what
10389 ;; If necessary, get the time from the user
10390 (setq time (or time (org-read-date nil 'to-time nil nil
10391 default-time default-input))))
10393 (when (and org-insert-labeled-timestamps-at-point
10394 (member what '(scheduled deadline)))
10395 (insert
10396 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
10397 (org-insert-time-stamp time org-time-was-given
10398 nil nil nil (list org-end-time-was-given))
10399 (setq what nil))
10400 (save-excursion
10401 (save-restriction
10402 (let (col list elt ts buffer-invisibility-spec)
10403 (org-back-to-heading t)
10404 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
10405 (goto-char (match-end 1))
10406 (setq col (current-column))
10407 (goto-char (match-end 0))
10408 (if (eobp) (insert "\n") (forward-char 1))
10409 (when (and (not what)
10410 (not (looking-at
10411 (concat "[ \t]*"
10412 org-keyword-time-not-clock-regexp))))
10413 ;; Nothing to add, nothing to remove...... :-)
10414 (throw 'exit nil))
10415 (if (and (not (looking-at outline-regexp))
10416 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
10417 "[^\r\n]*"))
10418 (not (equal (match-string 1) org-clock-string)))
10419 (narrow-to-region (match-beginning 0) (match-end 0))
10420 (insert-before-markers "\n")
10421 (backward-char 1)
10422 (narrow-to-region (point) (point))
10423 (and org-adapt-indentation (org-indent-to-column col)))
10424 ;; Check if we have to remove something.
10425 (setq list (cons what remove))
10426 (while list
10427 (setq elt (pop list))
10428 (goto-char (point-min))
10429 (when (or (and (eq elt 'scheduled)
10430 (re-search-forward org-scheduled-time-regexp nil t))
10431 (and (eq elt 'deadline)
10432 (re-search-forward org-deadline-time-regexp nil t))
10433 (and (eq elt 'closed)
10434 (re-search-forward org-closed-time-regexp nil t)))
10435 (replace-match "")
10436 (if (looking-at "--+<[^>]+>") (replace-match ""))
10437 (skip-chars-backward " ")
10438 (if (looking-at " +") (replace-match ""))))
10439 (goto-char (point-max))
10440 (and org-adapt-indentation (bolp) (org-indent-to-column col))
10441 (when what
10442 (insert
10443 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
10444 (cond ((eq what 'scheduled) org-scheduled-string)
10445 ((eq what 'deadline) org-deadline-string)
10446 ((eq what 'closed) org-closed-string))
10447 " ")
10448 (setq ts (org-insert-time-stamp
10449 time
10450 (or org-time-was-given
10451 (and (eq what 'closed) org-log-done-with-time))
10452 (eq what 'closed)
10453 nil nil (list org-end-time-was-given)))
10454 (end-of-line 1))
10455 (goto-char (point-min))
10456 (widen)
10457 (if (and (looking-at "[ \t]+\n")
10458 (equal (char-before) ?\n))
10459 (delete-region (1- (point)) (point-at-eol)))
10460 ts))))))
10462 (defvar org-log-note-marker (make-marker))
10463 (defvar org-log-note-purpose nil)
10464 (defvar org-log-note-state nil)
10465 (defvar org-log-note-previous-state nil)
10466 (defvar org-log-note-how nil)
10467 (defvar org-log-note-extra nil)
10468 (defvar org-log-note-window-configuration nil)
10469 (defvar org-log-note-return-to (make-marker))
10470 (defvar org-log-post-message nil
10471 "Message to be displayed after a log note has been stored.
10472 The auto-repeater uses this.")
10474 (defun org-add-note ()
10475 "Add a note to the current entry.
10476 This is done in the same way as adding a state change note."
10477 (interactive)
10478 (org-add-log-setup 'note nil nil 'findpos nil))
10480 (defvar org-property-end-re)
10481 (defun org-add-log-setup (&optional purpose state prev-state
10482 findpos how &optional extra)
10483 "Set up the post command hook to take a note.
10484 If this is about to TODO state change, the new state is expected in STATE.
10485 When FINDPOS is non-nil, find the correct position for the note in
10486 the current entry. If not, assume that it can be inserted at point.
10487 HOW is an indicator what kind of note should be created.
10488 EXTRA is additional text that will be inserted into the notes buffer."
10489 (let* ((org-log-into-drawer (org-log-into-drawer))
10490 (drawer (cond ((stringp org-log-into-drawer)
10491 org-log-into-drawer)
10492 (org-log-into-drawer "LOGBOOK")
10493 (t nil))))
10494 (save-restriction
10495 (save-excursion
10496 (when findpos
10497 (org-back-to-heading t)
10498 (narrow-to-region (point) (save-excursion
10499 (outline-next-heading) (point)))
10500 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
10501 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
10502 "[^\r\n]*\\)?"))
10503 (goto-char (match-end 0))
10504 (cond
10505 (drawer
10506 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
10507 nil t)
10508 (progn
10509 (goto-char (match-end 0))
10510 (or org-log-states-order-reversed
10511 (and (re-search-forward org-property-end-re nil t)
10512 (goto-char (1- (match-beginning 0))))))
10513 (insert "\n:" drawer ":\n:END:")
10514 (beginning-of-line 0)
10515 (org-indent-line-function)
10516 (beginning-of-line 2)
10517 (org-indent-line-function)
10518 (end-of-line 0)))
10519 ((and org-log-state-notes-insert-after-drawers
10520 (save-excursion
10521 (forward-line) (looking-at org-drawer-regexp)))
10522 (forward-line)
10523 (while (looking-at org-drawer-regexp)
10524 (goto-char (match-end 0))
10525 (re-search-forward org-property-end-re (point-max) t)
10526 (forward-line))
10527 (forward-line -1)))
10528 (unless org-log-states-order-reversed
10529 (and (= (char-after) ?\n) (forward-char 1))
10530 (org-skip-over-state-notes)
10531 (skip-chars-backward " \t\n\r")))
10532 (move-marker org-log-note-marker (point))
10533 (setq org-log-note-purpose purpose
10534 org-log-note-state state
10535 org-log-note-previous-state prev-state
10536 org-log-note-how how
10537 org-log-note-extra extra)
10538 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
10540 (defun org-skip-over-state-notes ()
10541 "Skip past the list of State notes in an entry."
10542 (if (looking-at "\n[ \t]*- State") (forward-char 1))
10543 (while (looking-at "[ \t]*- State")
10544 (condition-case nil
10545 (org-next-item)
10546 (error (org-end-of-item)))))
10548 (defun org-add-log-note (&optional purpose)
10549 "Pop up a window for taking a note, and add this note later at point."
10550 (remove-hook 'post-command-hook 'org-add-log-note)
10551 (setq org-log-note-window-configuration (current-window-configuration))
10552 (delete-other-windows)
10553 (move-marker org-log-note-return-to (point))
10554 (switch-to-buffer (marker-buffer org-log-note-marker))
10555 (goto-char org-log-note-marker)
10556 (org-switch-to-buffer-other-window "*Org Note*")
10557 (erase-buffer)
10558 (if (memq org-log-note-how '(time state))
10559 (let (current-prefix-arg) (org-store-log-note))
10560 (let ((org-inhibit-startup t)) (org-mode))
10561 (insert (format "# Insert note for %s.
10562 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
10563 (cond
10564 ((eq org-log-note-purpose 'clock-out) "stopped clock")
10565 ((eq org-log-note-purpose 'done) "closed todo item")
10566 ((eq org-log-note-purpose 'state)
10567 (format "state change from \"%s\" to \"%s\""
10568 (or org-log-note-previous-state "")
10569 (or org-log-note-state "")))
10570 ((eq org-log-note-purpose 'note)
10571 "this entry")
10572 (t (error "This should not happen")))))
10573 (if org-log-note-extra (insert org-log-note-extra))
10574 (org-set-local 'org-finish-function 'org-store-log-note)))
10576 (defvar org-note-abort nil) ; dynamically scoped
10577 (defun org-store-log-note ()
10578 "Finish taking a log note, and insert it to where it belongs."
10579 (let ((txt (buffer-string))
10580 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
10581 lines ind)
10582 (kill-buffer (current-buffer))
10583 (while (string-match "\\`#.*\n[ \t\n]*" txt)
10584 (setq txt (replace-match "" t t txt)))
10585 (if (string-match "\\s-+\\'" txt)
10586 (setq txt (replace-match "" t t txt)))
10587 (setq lines (org-split-string txt "\n"))
10588 (when (and note (string-match "\\S-" note))
10589 (setq note
10590 (org-replace-escapes
10591 note
10592 (list (cons "%u" (user-login-name))
10593 (cons "%U" user-full-name)
10594 (cons "%t" (format-time-string
10595 (org-time-stamp-format 'long 'inactive)
10596 (current-time)))
10597 (cons "%s" (if org-log-note-state
10598 (concat "\"" org-log-note-state "\"")
10599 ""))
10600 (cons "%S" (if org-log-note-previous-state
10601 (concat "\"" org-log-note-previous-state "\"")
10602 "\"\"")))))
10603 (if lines (setq note (concat note " \\\\")))
10604 (push note lines))
10605 (when (or current-prefix-arg org-note-abort)
10606 (when org-log-into-drawer
10607 (org-remove-empty-drawer-at
10608 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
10609 org-log-note-marker))
10610 (setq lines nil))
10611 (when lines
10612 (save-excursion
10613 (set-buffer (marker-buffer org-log-note-marker))
10614 (save-excursion
10615 (goto-char org-log-note-marker)
10616 (move-marker org-log-note-marker nil)
10617 (end-of-line 1)
10618 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
10619 (insert "- " (pop lines))
10620 (org-indent-line-function)
10621 (beginning-of-line 1)
10622 (looking-at "[ \t]*")
10623 (setq ind (concat (match-string 0) " "))
10624 (end-of-line 1)
10625 (while lines (insert "\n" ind (pop lines)))
10626 (message "Note stored")
10627 (org-back-to-heading t)
10628 (org-cycle-hide-drawers 'children)))))
10629 (set-window-configuration org-log-note-window-configuration)
10630 (with-current-buffer (marker-buffer org-log-note-return-to)
10631 (goto-char org-log-note-return-to))
10632 (move-marker org-log-note-return-to nil)
10633 (and org-log-post-message (message "%s" org-log-post-message)))
10635 (defun org-remove-empty-drawer-at (drawer pos)
10636 "Remove an emptyr DARWER drawer at position POS.
10637 POS may also be a marker."
10638 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
10639 (save-excursion
10640 (save-restriction
10641 (widen)
10642 (goto-char pos)
10643 (if (org-in-regexp
10644 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
10645 (replace-match ""))))))
10647 (defun org-sparse-tree (&optional arg)
10648 "Create a sparse tree, prompt for the details.
10649 This command can create sparse trees. You first need to select the type
10650 of match used to create the tree:
10652 t Show entries with a specific TODO keyword.
10653 m Show entries selected by a tags/property match.
10654 p Enter a property name and its value (both with completion on existing
10655 names/values) and show entries with that property.
10656 / Show entries matching a regular expression (`r' can be used as well)
10657 d Show deadlines due within `org-deadline-warning-days'.
10658 b Show deadlines and scheduled items before a date.
10659 a Show deadlines and scheduled items after a date."
10660 (interactive "P")
10661 (let (ans kwd value)
10662 (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
10663 (setq ans (read-char-exclusive))
10664 (cond
10665 ((equal ans ?d)
10666 (call-interactively 'org-check-deadlines))
10667 ((equal ans ?b)
10668 (call-interactively 'org-check-before-date))
10669 ((equal ans ?a)
10670 (call-interactively 'org-check-after-date))
10671 ((equal ans ?t)
10672 (org-show-todo-tree '(4)))
10673 ((member ans '(?T ?m))
10674 (call-interactively 'org-match-sparse-tree))
10675 ((member ans '(?p ?P))
10676 (setq kwd (org-icompleting-read "Property: "
10677 (mapcar 'list (org-buffer-property-keys))))
10678 (setq value (org-icompleting-read "Value: "
10679 (mapcar 'list (org-property-values kwd))))
10680 (unless (string-match "\\`{.*}\\'" value)
10681 (setq value (concat "\"" value "\"")))
10682 (org-match-sparse-tree arg (concat kwd "=" value)))
10683 ((member ans '(?r ?R ?/))
10684 (call-interactively 'org-occur))
10685 (t (error "No such sparse tree command \"%c\"" ans)))))
10687 (defvar org-occur-highlights nil
10688 "List of overlays used for occur matches.")
10689 (make-variable-buffer-local 'org-occur-highlights)
10690 (defvar org-occur-parameters nil
10691 "Parameters of the active org-occur calls.
10692 This is a list, each call to org-occur pushes as cons cell,
10693 containing the regular expression and the callback, onto the list.
10694 The list can contain several entries if `org-occur' has been called
10695 several time with the KEEP-PREVIOUS argument. Otherwise, this list
10696 will only contain one set of parameters. When the highlights are
10697 removed (for example with `C-c C-c', or with the next edit (depending
10698 on `org-remove-highlights-with-change'), this variable is emptied
10699 as well.")
10700 (make-variable-buffer-local 'org-occur-parameters)
10702 (defun org-occur (regexp &optional keep-previous callback)
10703 "Make a compact tree which shows all matches of REGEXP.
10704 The tree will show the lines where the regexp matches, and all higher
10705 headlines above the match. It will also show the heading after the match,
10706 to make sure editing the matching entry is easy.
10707 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
10708 call to `org-occur' will be kept, to allow stacking of calls to this
10709 command.
10710 If CALLBACK is non-nil, it is a function which is called to confirm
10711 that the match should indeed be shown."
10712 (interactive "sRegexp: \nP")
10713 (when (equal regexp "")
10714 (error "Regexp cannot be empty"))
10715 (unless keep-previous
10716 (org-remove-occur-highlights nil nil t))
10717 (push (cons regexp callback) org-occur-parameters)
10718 (let ((cnt 0))
10719 (save-excursion
10720 (goto-char (point-min))
10721 (if (or (not keep-previous) ; do not want to keep
10722 (not org-occur-highlights)) ; no previous matches
10723 ;; hide everything
10724 (org-overview))
10725 (while (re-search-forward regexp nil t)
10726 (when (or (not callback)
10727 (save-match-data (funcall callback)))
10728 (setq cnt (1+ cnt))
10729 (when org-highlight-sparse-tree-matches
10730 (org-highlight-new-match (match-beginning 0) (match-end 0)))
10731 (org-show-context 'occur-tree))))
10732 (when org-remove-highlights-with-change
10733 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
10734 nil 'local))
10735 (unless org-sparse-tree-open-archived-trees
10736 (org-hide-archived-subtrees (point-min) (point-max)))
10737 (run-hooks 'org-occur-hook)
10738 (if (interactive-p)
10739 (message "%d match(es) for regexp %s" cnt regexp))
10740 cnt))
10742 (defun org-show-context (&optional key)
10743 "Make sure point and context and visible.
10744 How much context is shown depends upon the variables
10745 `org-show-hierarchy-above', `org-show-following-heading'. and
10746 `org-show-siblings'."
10747 (let ((heading-p (org-on-heading-p t))
10748 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
10749 (following-p (org-get-alist-option org-show-following-heading key))
10750 (entry-p (org-get-alist-option org-show-entry-below key))
10751 (siblings-p (org-get-alist-option org-show-siblings key)))
10752 (catch 'exit
10753 ;; Show heading or entry text
10754 (if (and heading-p (not entry-p))
10755 (org-flag-heading nil) ; only show the heading
10756 (and (or entry-p (org-invisible-p) (org-invisible-p2))
10757 (org-show-hidden-entry))) ; show entire entry
10758 (when following-p
10759 ;; Show next sibling, or heading below text
10760 (save-excursion
10761 (and (if heading-p (org-goto-sibling) (outline-next-heading))
10762 (org-flag-heading nil))))
10763 (when siblings-p (org-show-siblings))
10764 (when hierarchy-p
10765 ;; show all higher headings, possibly with siblings
10766 (save-excursion
10767 (while (and (condition-case nil
10768 (progn (org-up-heading-all 1) t)
10769 (error nil))
10770 (not (bobp)))
10771 (org-flag-heading nil)
10772 (when siblings-p (org-show-siblings))))))))
10774 (defun org-reveal (&optional siblings)
10775 "Show current entry, hierarchy above it, and the following headline.
10776 This can be used to show a consistent set of context around locations
10777 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
10778 not t for the search context.
10780 With optional argument SIBLINGS, on each level of the hierarchy all
10781 siblings are shown. This repairs the tree structure to what it would
10782 look like when opened with hierarchical calls to `org-cycle'."
10783 (interactive "P")
10784 (let ((org-show-hierarchy-above t)
10785 (org-show-following-heading t)
10786 (org-show-siblings (if siblings t org-show-siblings)))
10787 (org-show-context nil)))
10789 (defun org-highlight-new-match (beg end)
10790 "Highlight from BEG to END and mark the highlight is an occur headline."
10791 (let ((ov (org-make-overlay beg end)))
10792 (org-overlay-put ov 'face 'secondary-selection)
10793 (push ov org-occur-highlights)))
10795 (defun org-remove-occur-highlights (&optional beg end noremove)
10796 "Remove the occur highlights from the buffer.
10797 BEG and END are ignored. If NOREMOVE is nil, remove this function
10798 from the `before-change-functions' in the current buffer."
10799 (interactive)
10800 (unless org-inhibit-highlight-removal
10801 (mapc 'org-delete-overlay org-occur-highlights)
10802 (setq org-occur-highlights nil)
10803 (setq org-occur-parameters nil)
10804 (unless noremove
10805 (remove-hook 'before-change-functions
10806 'org-remove-occur-highlights 'local))))
10808 ;;;; Priorities
10810 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
10811 "Regular expression matching the priority indicator.")
10813 (defvar org-remove-priority-next-time nil)
10815 (defun org-priority-up ()
10816 "Increase the priority of the current item."
10817 (interactive)
10818 (org-priority 'up))
10820 (defun org-priority-down ()
10821 "Decrease the priority of the current item."
10822 (interactive)
10823 (org-priority 'down))
10825 (defun org-priority (&optional action)
10826 "Change the priority of an item by ARG.
10827 ACTION can be `set', `up', `down', or a character."
10828 (interactive)
10829 (unless org-enable-priority-commands
10830 (error "Priority commands are disabled"))
10831 (setq action (or action 'set))
10832 (let (current new news have remove)
10833 (save-excursion
10834 (org-back-to-heading t)
10835 (if (looking-at org-priority-regexp)
10836 (setq current (string-to-char (match-string 2))
10837 have t)
10838 (setq current org-default-priority))
10839 (cond
10840 ((eq action 'remove)
10841 (setq remove t new ?\ ))
10842 ((or (eq action 'set)
10843 (if (featurep 'xemacs) (characterp action) (integerp action)))
10844 (if (not (eq action 'set))
10845 (setq new action)
10846 (message "Priority %c-%c, SPC to remove: "
10847 org-highest-priority org-lowest-priority)
10848 (setq new (read-char-exclusive)))
10849 (if (and (= (upcase org-highest-priority) org-highest-priority)
10850 (= (upcase org-lowest-priority) org-lowest-priority))
10851 (setq new (upcase new)))
10852 (cond ((equal new ?\ ) (setq remove t))
10853 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
10854 (error "Priority must be between `%c' and `%c'"
10855 org-highest-priority org-lowest-priority))))
10856 ((eq action 'up)
10857 (if (and (not have) (eq last-command this-command))
10858 (setq new org-lowest-priority)
10859 (setq new (if (and org-priority-start-cycle-with-default (not have))
10860 org-default-priority (1- current)))))
10861 ((eq action 'down)
10862 (if (and (not have) (eq last-command this-command))
10863 (setq new org-highest-priority)
10864 (setq new (if (and org-priority-start-cycle-with-default (not have))
10865 org-default-priority (1+ current)))))
10866 (t (error "Invalid action")))
10867 (if (or (< (upcase new) org-highest-priority)
10868 (> (upcase new) org-lowest-priority))
10869 (setq remove t))
10870 (setq news (format "%c" new))
10871 (if have
10872 (if remove
10873 (replace-match "" t t nil 1)
10874 (replace-match news t t nil 2))
10875 (if remove
10876 (error "No priority cookie found in line")
10877 (let ((case-fold-search nil))
10878 (looking-at org-todo-line-regexp))
10879 (if (match-end 2)
10880 (progn
10881 (goto-char (match-end 2))
10882 (insert " [#" news "]"))
10883 (goto-char (match-beginning 3))
10884 (insert "[#" news "] "))))
10885 (org-preserve-lc (org-set-tags nil 'align)))
10886 (if remove
10887 (message "Priority removed")
10888 (message "Priority of current item set to %s" news))))
10890 (defun org-get-priority (s)
10891 "Find priority cookie and return priority."
10892 (save-match-data
10893 (if (not (string-match org-priority-regexp s))
10894 (* 1000 (- org-lowest-priority org-default-priority))
10895 (* 1000 (- org-lowest-priority
10896 (string-to-char (match-string 2 s)))))))
10898 ;;;; Tags
10900 (defvar org-agenda-archives-mode)
10901 (defvar org-map-continue-from nil
10902 "Position from where mapping should continue.
10903 Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
10905 (defvar org-scanner-tags nil
10906 "The current tag list while the tags scanner is running.")
10907 (defvar org-trust-scanner-tags nil
10908 "Should `org-get-tags-at' use the tags fro the scanner.
10909 This is for internal dynamical scoping only.
10910 When this is non-nil, the function `org-get-tags-at' will return the value
10911 of `org-scanner-tags' instead of building the list by itself. This
10912 can lead to large speed-ups when the tags scanner is used in a file with
10913 many entries, and when the list of tags is retrieved, for example to
10914 obtain a list of properties. Building the tags list for each entry in such
10915 a file becomes an N^2 operation - but with this variable set, it scales
10916 as N.")
10918 (defun org-scan-tags (action matcher &optional todo-only)
10919 "Scan headline tags with inheritance and produce output ACTION.
10921 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
10922 or `agenda' to produce an entry list for an agenda view. It can also be
10923 a Lisp form or a function that should be called at each matched headline, in
10924 this case the return value is a list of all return values from these calls.
10926 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
10927 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
10928 only lines with a TODO keyword are included in the output."
10929 (require 'org-agenda)
10930 (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
10931 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
10932 (org-re
10933 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
10934 (props (list 'face 'default
10935 'done-face 'org-agenda-done
10936 'undone-face 'default
10937 'mouse-face 'highlight
10938 'org-not-done-regexp org-not-done-regexp
10939 'org-todo-regexp org-todo-regexp
10940 'keymap org-agenda-keymap
10941 'help-echo
10942 (format "mouse-2 or RET jump to org file %s"
10943 (abbreviate-file-name
10944 (or (buffer-file-name (buffer-base-buffer))
10945 (buffer-name (buffer-base-buffer)))))))
10946 (case-fold-search nil)
10947 (org-map-continue-from nil)
10948 lspos tags tags-list
10949 (tags-alist (list (cons 0 org-file-tags)))
10950 (llast 0) rtn rtn1 level category i txt
10951 todo marker entry priority)
10952 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
10953 (setq action (list 'lambda nil action)))
10954 (save-excursion
10955 (goto-char (point-min))
10956 (when (eq action 'sparse-tree)
10957 (org-overview)
10958 (org-remove-occur-highlights))
10959 (while (re-search-forward re nil t)
10960 (catch :skip
10961 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
10962 tags (if (match-end 4) (org-match-string-no-properties 4)))
10963 (goto-char (setq lspos (match-beginning 0)))
10964 (setq level (org-reduced-level (funcall outline-level))
10965 category (org-get-category))
10966 (setq i llast llast level)
10967 ;; remove tag lists from same and sublevels
10968 (while (>= i level)
10969 (when (setq entry (assoc i tags-alist))
10970 (setq tags-alist (delete entry tags-alist)))
10971 (setq i (1- i)))
10972 ;; add the next tags
10973 (when tags
10974 (setq tags (org-split-string tags ":")
10975 tags-alist
10976 (cons (cons level tags) tags-alist)))
10977 ;; compile tags for current headline
10978 (setq tags-list
10979 (if org-use-tag-inheritance
10980 (apply 'append (mapcar 'cdr (reverse tags-alist)))
10981 tags)
10982 org-scanner-tags tags-list)
10983 (when org-use-tag-inheritance
10984 (setcdr (car tags-alist)
10985 (mapcar (lambda (x)
10986 (setq x (copy-sequence x))
10987 (org-add-prop-inherited x))
10988 (cdar tags-alist))))
10989 (when (and tags org-use-tag-inheritance
10990 (or (not (eq t org-use-tag-inheritance))
10991 org-tags-exclude-from-inheritance))
10992 ;; selective inheritance, remove uninherited ones
10993 (setcdr (car tags-alist)
10994 (org-remove-uniherited-tags (cdar tags-alist))))
10995 (when (and (or (not todo-only)
10996 (and (member todo org-not-done-keywords)
10997 (or (not org-agenda-tags-todo-honor-ignore-options)
10998 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
10999 (let ((case-fold-search t)) (eval matcher))
11001 (not (member org-archive-tag tags-list))
11002 ;; we have an archive tag, should we use this anyway?
11003 (or (not org-agenda-skip-archived-trees)
11004 (and (eq action 'agenda) org-agenda-archives-mode))))
11005 (unless (eq action 'sparse-tree) (org-agenda-skip))
11007 ;; select this headline
11009 (cond
11010 ((eq action 'sparse-tree)
11011 (and org-highlight-sparse-tree-matches
11012 (org-get-heading) (match-end 0)
11013 (org-highlight-new-match
11014 (match-beginning 0) (match-beginning 1)))
11015 (org-show-context 'tags-tree))
11016 ((eq action 'agenda)
11017 (setq txt (org-format-agenda-item
11019 (concat
11020 (if (eq org-tags-match-list-sublevels 'indented)
11021 (make-string (1- level) ?.) "")
11022 (org-get-heading))
11023 category
11024 tags-list
11026 priority (org-get-priority txt))
11027 (goto-char lspos)
11028 (setq marker (org-agenda-new-marker))
11029 (org-add-props txt props
11030 'org-marker marker 'org-hd-marker marker 'org-category category
11031 'todo-state todo
11032 'priority priority 'type "tagsmatch")
11033 (push txt rtn))
11034 ((functionp action)
11035 (setq org-map-continue-from nil)
11036 (save-excursion
11037 (setq rtn1 (funcall action))
11038 (push rtn1 rtn)))
11039 (t (error "Invalid action")))
11041 ;; if we are to skip sublevels, jump to end of subtree
11042 (unless org-tags-match-list-sublevels
11043 (org-end-of-subtree t)
11044 (backward-char 1))))
11045 ;; Get the correct position from where to continue
11046 (if org-map-continue-from
11047 (goto-char org-map-continue-from)
11048 (and (= (point) lspos) (end-of-line 1)))))
11049 (when (and (eq action 'sparse-tree)
11050 (not org-sparse-tree-open-archived-trees))
11051 (org-hide-archived-subtrees (point-min) (point-max)))
11052 (nreverse rtn)))
11054 (defun org-remove-uniherited-tags (tags)
11055 "Remove all tags that are not inherited from the list TAGS."
11056 (cond
11057 ((eq org-use-tag-inheritance t)
11058 (if org-tags-exclude-from-inheritance
11059 (org-delete-all org-tags-exclude-from-inheritance tags)
11060 tags))
11061 ((not org-use-tag-inheritance) nil)
11062 ((stringp org-use-tag-inheritance)
11063 (delq nil (mapcar
11064 (lambda (x)
11065 (if (and (string-match org-use-tag-inheritance x)
11066 (not (member x org-tags-exclude-from-inheritance)))
11067 x nil))
11068 tags)))
11069 ((listp org-use-tag-inheritance)
11070 (delq nil (mapcar
11071 (lambda (x)
11072 (if (member x org-use-tag-inheritance) x nil))
11073 tags)))))
11075 (defvar todo-only) ;; dynamically scoped
11077 (defun org-match-sparse-tree (&optional todo-only match)
11078 "Create a sparse tree according to tags string MATCH.
11079 MATCH can contain positive and negative selection of tags, like
11080 \"+WORK+URGENT-WITHBOSS\".
11081 If optional argument TODO-ONLY is non-nil, only select lines that are
11082 also TODO lines."
11083 (interactive "P")
11084 (org-prepare-agenda-buffers (list (current-buffer)))
11085 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
11087 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
11089 (defvar org-cached-props nil)
11090 (defun org-cached-entry-get (pom property)
11091 (if (or (eq t org-use-property-inheritance)
11092 (and (stringp org-use-property-inheritance)
11093 (string-match org-use-property-inheritance property))
11094 (and (listp org-use-property-inheritance)
11095 (member property org-use-property-inheritance)))
11096 ;; Caching is not possible, check it directly
11097 (org-entry-get pom property 'inherit)
11098 ;; Get all properties, so that we can do complicated checks easily
11099 (cdr (assoc property (or org-cached-props
11100 (setq org-cached-props
11101 (org-entry-properties pom)))))))
11103 (defun org-global-tags-completion-table (&optional files)
11104 "Return the list of all tags in all agenda buffer/files."
11105 (save-excursion
11106 (org-uniquify
11107 (delq nil
11108 (apply 'append
11109 (mapcar
11110 (lambda (file)
11111 (set-buffer (find-file-noselect file))
11112 (append (org-get-buffer-tags)
11113 (mapcar (lambda (x) (if (stringp (car-safe x))
11114 (list (car-safe x)) nil))
11115 org-tag-alist)))
11116 (if (and files (car files))
11117 files
11118 (org-agenda-files))))))))
11120 (defun org-make-tags-matcher (match)
11121 "Create the TAGS//TODO matcher form for the selection string MATCH."
11122 ;; todo-only is scoped dynamically into this function, and the function
11123 ;; may change it if the matcher asks for it.
11124 (unless match
11125 ;; Get a new match request, with completion
11126 (let ((org-last-tags-completion-table
11127 (org-global-tags-completion-table)))
11128 (setq match (org-completing-read-no-i
11129 "Match: " 'org-tags-completion-function nil nil nil
11130 'org-tags-history))))
11132 ;; Parse the string and create a lisp form
11133 (let ((match0 match)
11134 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
11135 minus tag mm
11136 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
11137 orterms term orlist re-p str-p level-p level-op time-p
11138 prop-p pn pv po cat-p gv rest)
11139 (if (string-match "/+" match)
11140 ;; match contains also a todo-matching request
11141 (progn
11142 (setq tagsmatch (substring match 0 (match-beginning 0))
11143 todomatch (substring match (match-end 0)))
11144 (if (string-match "^!" todomatch)
11145 (setq todo-only t todomatch (substring todomatch 1)))
11146 (if (string-match "^\\s-*$" todomatch)
11147 (setq todomatch nil)))
11148 ;; only matching tags
11149 (setq tagsmatch match todomatch nil))
11151 ;; Make the tags matcher
11152 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
11153 (setq tagsmatcher t)
11154 (setq orterms (org-split-string tagsmatch "|") orlist nil)
11155 (while (setq term (pop orterms))
11156 (while (and (equal (substring term -1) "\\") orterms)
11157 (setq term (concat term "|" (pop orterms)))) ; repair bad split
11158 (while (string-match re term)
11159 (setq rest (substring term (match-end 0))
11160 minus (and (match-end 1)
11161 (equal (match-string 1 term) "-"))
11162 tag (match-string 2 term)
11163 re-p (equal (string-to-char tag) ?{)
11164 level-p (match-end 4)
11165 prop-p (match-end 5)
11166 mm (cond
11167 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
11168 (level-p
11169 (setq level-op (org-op-to-function (match-string 3 term)))
11170 `(,level-op level ,(string-to-number
11171 (match-string 4 term))))
11172 (prop-p
11173 (setq pn (match-string 5 term)
11174 po (match-string 6 term)
11175 pv (match-string 7 term)
11176 cat-p (equal pn "CATEGORY")
11177 re-p (equal (string-to-char pv) ?{)
11178 str-p (equal (string-to-char pv) ?\")
11179 time-p (save-match-data
11180 (string-match "^\"[[<].*[]>]\"$" pv))
11181 pv (if (or re-p str-p) (substring pv 1 -1) pv))
11182 (if time-p (setq pv (org-matcher-time pv)))
11183 (setq po (org-op-to-function po (if time-p 'time str-p)))
11184 (cond
11185 ((equal pn "CATEGORY")
11186 (setq gv '(get-text-property (point) 'org-category)))
11187 ((equal pn "TODO")
11188 (setq gv 'todo))
11190 (setq gv `(org-cached-entry-get nil ,pn))))
11191 (if re-p
11192 (if (eq po 'org<>)
11193 `(not (string-match ,pv (or ,gv "")))
11194 `(string-match ,pv (or ,gv "")))
11195 (if str-p
11196 `(,po (or ,gv "") ,pv)
11197 `(,po (string-to-number (or ,gv ""))
11198 ,(string-to-number pv) ))))
11199 (t `(member ,tag tags-list)))
11200 mm (if minus (list 'not mm) mm)
11201 term rest)
11202 (push mm tagsmatcher))
11203 (push (if (> (length tagsmatcher) 1)
11204 (cons 'and tagsmatcher)
11205 (car tagsmatcher))
11206 orlist)
11207 (setq tagsmatcher nil))
11208 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
11209 (setq tagsmatcher
11210 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
11211 ;; Make the todo matcher
11212 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
11213 (setq todomatcher t)
11214 (setq orterms (org-split-string todomatch "|") orlist nil)
11215 (while (setq term (pop orterms))
11216 (while (string-match re term)
11217 (setq minus (and (match-end 1)
11218 (equal (match-string 1 term) "-"))
11219 kwd (match-string 2 term)
11220 re-p (equal (string-to-char kwd) ?{)
11221 term (substring term (match-end 0))
11222 mm (if re-p
11223 `(string-match ,(substring kwd 1 -1) todo)
11224 (list 'equal 'todo kwd))
11225 mm (if minus (list 'not mm) mm))
11226 (push mm todomatcher))
11227 (push (if (> (length todomatcher) 1)
11228 (cons 'and todomatcher)
11229 (car todomatcher))
11230 orlist)
11231 (setq todomatcher nil))
11232 (setq todomatcher (if (> (length orlist) 1)
11233 (cons 'or orlist) (car orlist))))
11235 ;; Return the string and lisp forms of the matcher
11236 (setq matcher (if todomatcher
11237 (list 'and tagsmatcher todomatcher)
11238 tagsmatcher))
11239 (cons match0 matcher)))
11241 (defun org-op-to-function (op &optional stringp)
11242 "Turn an operator into the appropriate function."
11243 (setq op
11244 (cond
11245 ((equal op "<" ) '(< string< org-time<))
11246 ((equal op ">" ) '(> org-string> org-time>))
11247 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
11248 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
11249 ((member op '("=" "==")) '(= string= org-time=))
11250 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
11251 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
11253 (defun org<> (a b) (not (= a b)))
11254 (defun org-string<= (a b) (or (string= a b) (string< a b)))
11255 (defun org-string>= (a b) (not (string< a b)))
11256 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
11257 (defun org-string<> (a b) (not (string= 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) (>= a b)))
11263 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
11264 (defun org-2ft (s)
11265 "Convert S to a floating point time.
11266 If S is already a number, just return it. If it is a string, parse
11267 it as a time string and apply `float-time' to it. If S is nil, just return 0."
11268 (cond
11269 ((numberp s) s)
11270 ((stringp s)
11271 (condition-case nil
11272 (float-time (apply 'encode-time (org-parse-time-string s)))
11273 (error 0.)))
11274 (t 0.)))
11276 (defun org-time-today ()
11277 "Time in seconds today at 0:00.
11278 Returns the float number of seconds since the beginning of the
11279 epoch to the beginning of today (00:00)."
11280 (float-time (apply 'encode-time
11281 (append '(0 0 0) (nthcdr 3 (decode-time))))))
11283 (defun org-matcher-time (s)
11284 "Interpret a time comparison value."
11285 (save-match-data
11286 (cond
11287 ((string= s "<now>") (float-time))
11288 ((string= s "<today>") (org-time-today))
11289 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
11290 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
11291 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
11292 (+ (org-time-today)
11293 (* (string-to-number (match-string 1 s))
11294 (cdr (assoc (match-string 2 s)
11295 '(("d" . 86400.0) ("w" . 604800.0)
11296 ("m" . 2678400.0) ("y" . 31557600.0)))))))
11297 (t (org-2ft s)))))
11299 (defun org-match-any-p (re list)
11300 "Does re match any element of list?"
11301 (setq list (mapcar (lambda (x) (string-match re x)) list))
11302 (delq nil list))
11304 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
11305 (defvar org-tags-overlay (org-make-overlay 1 1))
11306 (org-detach-overlay org-tags-overlay)
11308 (defun org-get-local-tags-at (&optional pos)
11309 "Get a list of tags defined in the current headline."
11310 (org-get-tags-at pos 'local))
11312 (defun org-get-local-tags ()
11313 "Get a list of tags defined in the current headline."
11314 (org-get-tags-at nil 'local))
11316 (defun org-get-tags-at (&optional pos local)
11317 "Get a list of all headline tags applicable at POS.
11318 POS defaults to point. If tags are inherited, the list contains
11319 the targets in the same sequence as the headlines appear, i.e.
11320 the tags of the current headline come last.
11321 When LOCAL is non-nil, only return tags from the current headline,
11322 ignore inherited ones."
11323 (interactive)
11324 (if (and org-trust-scanner-tags
11325 (or (not pos) (equal pos (point)))
11326 (not local))
11327 org-scanner-tags
11328 (let (tags ltags lastpos parent)
11329 (save-excursion
11330 (save-restriction
11331 (widen)
11332 (goto-char (or pos (point)))
11333 (save-match-data
11334 (catch 'done
11335 (condition-case nil
11336 (progn
11337 (org-back-to-heading t)
11338 (while (not (equal lastpos (point)))
11339 (setq lastpos (point))
11340 (when (looking-at
11341 (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
11342 (setq ltags (org-split-string
11343 (org-match-string-no-properties 1) ":"))
11344 (when parent
11345 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
11346 (setq tags (append
11347 (if parent
11348 (org-remove-uniherited-tags ltags)
11349 ltags)
11350 tags)))
11351 (or org-use-tag-inheritance (throw 'done t))
11352 (if local (throw 'done t))
11353 (or (org-up-heading-safe) (error nil))
11354 (setq parent t)))
11355 (error nil)))))
11356 (append (org-remove-uniherited-tags org-file-tags) tags)))))
11358 (defun org-add-prop-inherited (s)
11359 (add-text-properties 0 (length s) '(inherited t) s)
11362 (defun org-toggle-tag (tag &optional onoff)
11363 "Toggle the tag TAG for the current line.
11364 If ONOFF is `on' or `off', don't toggle but set to this state."
11365 (let (res current)
11366 (save-excursion
11367 (org-back-to-heading t)
11368 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
11369 (point-at-eol) t)
11370 (progn
11371 (setq current (match-string 1))
11372 (replace-match ""))
11373 (setq current ""))
11374 (setq current (nreverse (org-split-string current ":")))
11375 (cond
11376 ((eq onoff 'on)
11377 (setq res t)
11378 (or (member tag current) (push tag current)))
11379 ((eq onoff 'off)
11380 (or (not (member tag current)) (setq current (delete tag current))))
11381 (t (if (member tag current)
11382 (setq current (delete tag current))
11383 (setq res t)
11384 (push tag current))))
11385 (end-of-line 1)
11386 (if current
11387 (progn
11388 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
11389 (org-set-tags nil t))
11390 (delete-horizontal-space))
11391 (run-hooks 'org-after-tags-change-hook))
11392 res))
11394 (defun org-align-tags-here (to-col)
11395 ;; Assumes that this is a headline
11396 (let ((pos (point)) (col (current-column)) ncol tags-l p)
11397 (beginning-of-line 1)
11398 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
11399 (< pos (match-beginning 2)))
11400 (progn
11401 (setq tags-l (- (match-end 2) (match-beginning 2)))
11402 (goto-char (match-beginning 1))
11403 (insert " ")
11404 (delete-region (point) (1+ (match-beginning 2)))
11405 (setq ncol (max (1+ (current-column))
11406 (1+ col)
11407 (if (> to-col 0)
11408 to-col
11409 (- (abs to-col) tags-l))))
11410 (setq p (point))
11411 (insert (make-string (- ncol (current-column)) ?\ ))
11412 (setq ncol (current-column))
11413 (when indent-tabs-mode (tabify p (point-at-eol)))
11414 (org-move-to-column (min ncol col) t))
11415 (goto-char pos))))
11417 (defun org-set-tags-command (&optional arg just-align)
11418 "Call the set-tags command for the current entry."
11419 (interactive "P")
11420 (if (org-on-heading-p)
11421 (org-set-tags arg just-align)
11422 (save-excursion
11423 (org-back-to-heading t)
11424 (org-set-tags arg just-align))))
11426 (defun org-set-tags-to (data)
11427 "Set the tags of the current entry to DATA, replacing the current tags.
11428 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
11429 If DATA is nil or the empty string, any tags will be removed."
11430 (interactive "sTags: ")
11431 (setq data
11432 (cond
11433 ((eq data nil) "")
11434 ((equal data "") "")
11435 ((stringp data)
11436 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
11437 ":"))
11438 ((listp data)
11439 (concat ":" (mapconcat 'identity data ":") ":"))
11440 (t nil)))
11441 (when data
11442 (save-excursion
11443 (org-back-to-heading t)
11444 (when (looking-at org-complex-heading-regexp)
11445 (if (match-end 5)
11446 (progn
11447 (goto-char (match-beginning 5))
11448 (insert data)
11449 (delete-region (point) (point-at-eol))
11450 (org-set-tags nil 'align))
11451 (goto-char (point-at-eol))
11452 (insert " " data)
11453 (org-set-tags nil 'align)))
11454 (beginning-of-line 1)
11455 (if (looking-at ".*?\\([ \t]+\\)$")
11456 (delete-region (match-beginning 1) (match-end 1))))))
11458 (defun org-set-tags (&optional arg just-align)
11459 "Set the tags for the current headline.
11460 With prefix ARG, realign all tags in headings in the current buffer."
11461 (interactive "P")
11462 (let* ((re (concat "^" outline-regexp))
11463 (current (org-get-tags-string))
11464 (col (current-column))
11465 (org-setting-tags t)
11466 table current-tags inherited-tags ; computed below when needed
11467 tags p0 c0 c1 rpl)
11468 (if arg
11469 (save-excursion
11470 (goto-char (point-min))
11471 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
11472 (while (re-search-forward re nil t)
11473 (org-set-tags nil t)
11474 (end-of-line 1)))
11475 (message "All tags realigned to column %d" org-tags-column))
11476 (if just-align
11477 (setq tags current)
11478 ;; Get a new set of tags from the user
11479 (save-excursion
11480 (setq table (append org-tag-persistent-alist
11481 (or org-tag-alist (org-get-buffer-tags)))
11482 org-last-tags-completion-table table
11483 current-tags (org-split-string current ":")
11484 inherited-tags (nreverse
11485 (nthcdr (length current-tags)
11486 (nreverse (org-get-tags-at))))
11487 tags
11488 (if (or (eq t org-use-fast-tag-selection)
11489 (and org-use-fast-tag-selection
11490 (delq nil (mapcar 'cdr table))))
11491 (org-fast-tag-selection
11492 current-tags inherited-tags table
11493 (if org-fast-tag-selection-include-todo org-todo-key-alist))
11494 (let ((org-add-colon-after-tag-completion t))
11495 (org-trim
11496 (org-without-partial-completion
11497 (org-icompleting-read "Tags: " 'org-tags-completion-function
11498 nil nil current 'org-tags-history)))))))
11499 (while (string-match "[-+&]+" tags)
11500 ;; No boolean logic, just a list
11501 (setq tags (replace-match ":" t t tags))))
11503 (if org-tags-sort-function
11504 (setq tags (mapconcat 'identity
11505 (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
11506 org-tags-sort-function) ":")))
11508 (if (string-match "\\`[\t ]*\\'" tags)
11509 (setq tags "")
11510 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
11511 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
11513 ;; Insert new tags at the correct column
11514 (beginning-of-line 1)
11515 (cond
11516 ((and (equal current "") (equal tags "")))
11517 ((re-search-forward
11518 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
11519 (point-at-eol) t)
11520 (if (equal tags "")
11521 (setq rpl "")
11522 (goto-char (match-beginning 0))
11523 (setq c0 (current-column) p0 (point)
11524 c1 (max (1+ c0) (if (> org-tags-column 0)
11525 org-tags-column
11526 (- (- org-tags-column) (length tags))))
11527 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
11528 (replace-match rpl t t)
11529 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
11530 tags)
11531 (t (error "Tags alignment failed")))
11532 (org-move-to-column col)
11533 (unless just-align
11534 (run-hooks 'org-after-tags-change-hook)))))
11536 (defun org-change-tag-in-region (beg end tag off)
11537 "Add or remove TAG for each entry in the region.
11538 This works in the agenda, and also in an org-mode buffer."
11539 (interactive
11540 (list (region-beginning) (region-end)
11541 (let ((org-last-tags-completion-table
11542 (if (org-mode-p)
11543 (org-get-buffer-tags)
11544 (org-global-tags-completion-table))))
11545 (org-icompleting-read
11546 "Tag: " 'org-tags-completion-function nil nil nil
11547 'org-tags-history))
11548 (progn
11549 (message "[s]et or [r]emove? ")
11550 (equal (read-char-exclusive) ?r))))
11551 (if (fboundp 'deactivate-mark) (deactivate-mark))
11552 (let ((agendap (equal major-mode 'org-agenda-mode))
11553 l1 l2 m buf pos newhead (cnt 0))
11554 (goto-char end)
11555 (setq l2 (1- (org-current-line)))
11556 (goto-char beg)
11557 (setq l1 (org-current-line))
11558 (loop for l from l1 to l2 do
11559 (org-goto-line l)
11560 (setq m (get-text-property (point) 'org-hd-marker))
11561 (when (or (and (org-mode-p) (org-on-heading-p))
11562 (and agendap m))
11563 (setq buf (if agendap (marker-buffer m) (current-buffer))
11564 pos (if agendap m (point)))
11565 (with-current-buffer buf
11566 (save-excursion
11567 (save-restriction
11568 (goto-char pos)
11569 (setq cnt (1+ cnt))
11570 (org-toggle-tag tag (if off 'off 'on))
11571 (setq newhead (org-get-heading)))))
11572 (and agendap (org-agenda-change-all-lines newhead m))))
11573 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
11575 (defun org-tags-completion-function (string predicate &optional flag)
11576 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
11577 (confirm (lambda (x) (stringp (car x)))))
11578 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
11579 (setq s1 (match-string 1 string)
11580 s2 (match-string 2 string))
11581 (setq s1 "" s2 string))
11582 (cond
11583 ((eq flag nil)
11584 ;; try completion
11585 (setq rtn (try-completion s2 ctable confirm))
11586 (if (stringp rtn)
11587 (setq rtn
11588 (concat s1 s2 (substring rtn (length s2))
11589 (if (and org-add-colon-after-tag-completion
11590 (assoc rtn ctable))
11591 ":" ""))))
11592 rtn)
11593 ((eq flag t)
11594 ;; all-completions
11595 (all-completions s2 ctable confirm)
11597 ((eq flag 'lambda)
11598 ;; exact match?
11599 (assoc s2 ctable)))
11602 (defun org-fast-tag-insert (kwd tags face &optional end)
11603 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
11604 (insert (format "%-12s" (concat kwd ":"))
11605 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
11606 (or end "")))
11608 (defun org-fast-tag-show-exit (flag)
11609 (save-excursion
11610 (org-goto-line 3)
11611 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
11612 (replace-match ""))
11613 (when flag
11614 (end-of-line 1)
11615 (org-move-to-column (- (window-width) 19) t)
11616 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
11618 (defun org-set-current-tags-overlay (current prefix)
11619 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
11620 (if (featurep 'xemacs)
11621 (org-overlay-display org-tags-overlay (concat prefix s)
11622 'secondary-selection)
11623 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
11624 (org-overlay-display org-tags-overlay (concat prefix s)))))
11626 (defun org-fast-tag-selection (current inherited table &optional todo-table)
11627 "Fast tag selection with single keys.
11628 CURRENT is the current list of tags in the headline, INHERITED is the
11629 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
11630 possibly with grouping information. TODO-TABLE is a similar table with
11631 TODO keywords, should these have keys assigned to them.
11632 If the keys are nil, a-z are automatically assigned.
11633 Returns the new tags string, or nil to not change the current settings."
11634 (let* ((fulltable (append table todo-table))
11635 (maxlen (apply 'max (mapcar
11636 (lambda (x)
11637 (if (stringp (car x)) (string-width (car x)) 0))
11638 fulltable)))
11639 (buf (current-buffer))
11640 (expert (eq org-fast-tag-selection-single-key 'expert))
11641 (buffer-tags nil)
11642 (fwidth (+ maxlen 3 1 3))
11643 (ncol (/ (- (window-width) 4) fwidth))
11644 (i-face 'org-done)
11645 (c-face 'org-todo)
11646 tg cnt e c char c1 c2 ntable tbl rtn
11647 ov-start ov-end ov-prefix
11648 (exit-after-next org-fast-tag-selection-single-key)
11649 (done-keywords org-done-keywords)
11650 groups ingroup)
11651 (save-excursion
11652 (beginning-of-line 1)
11653 (if (looking-at
11654 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
11655 (setq ov-start (match-beginning 1)
11656 ov-end (match-end 1)
11657 ov-prefix "")
11658 (setq ov-start (1- (point-at-eol))
11659 ov-end (1+ ov-start))
11660 (skip-chars-forward "^\n\r")
11661 (setq ov-prefix
11662 (concat
11663 (buffer-substring (1- (point)) (point))
11664 (if (> (current-column) org-tags-column)
11666 (make-string (- org-tags-column (current-column)) ?\ ))))))
11667 (org-move-overlay org-tags-overlay ov-start ov-end)
11668 (save-window-excursion
11669 (if expert
11670 (set-buffer (get-buffer-create " *Org tags*"))
11671 (delete-other-windows)
11672 (split-window-vertically)
11673 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
11674 (erase-buffer)
11675 (org-set-local 'org-done-keywords done-keywords)
11676 (org-fast-tag-insert "Inherited" inherited i-face "\n")
11677 (org-fast-tag-insert "Current" current c-face "\n\n")
11678 (org-fast-tag-show-exit exit-after-next)
11679 (org-set-current-tags-overlay current ov-prefix)
11680 (setq tbl fulltable char ?a cnt 0)
11681 (while (setq e (pop tbl))
11682 (cond
11683 ((equal (car e) :startgroup)
11684 (push '() groups) (setq ingroup t)
11685 (when (not (= cnt 0))
11686 (setq cnt 0)
11687 (insert "\n"))
11688 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
11689 ((equal (car e) :endgroup)
11690 (setq ingroup nil cnt 0)
11691 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
11692 ((equal e '(:newline))
11693 (when (not (= cnt 0))
11694 (setq cnt 0)
11695 (insert "\n")
11696 (setq e (car tbl))
11697 (while (equal (car tbl) '(:newline))
11698 (insert "\n")
11699 (setq tbl (cdr tbl)))))
11701 (setq tg (copy-sequence (car e)) c2 nil)
11702 (if (cdr e)
11703 (setq c (cdr e))
11704 ;; automatically assign a character.
11705 (setq c1 (string-to-char
11706 (downcase (substring
11707 tg (if (= (string-to-char tg) ?@) 1 0)))))
11708 (if (or (rassoc c1 ntable) (rassoc c1 table))
11709 (while (or (rassoc char ntable) (rassoc char table))
11710 (setq char (1+ char)))
11711 (setq c2 c1))
11712 (setq c (or c2 char)))
11713 (if ingroup (push tg (car groups)))
11714 (setq tg (org-add-props tg nil 'face
11715 (cond
11716 ((not (assoc tg table))
11717 (org-get-todo-face tg))
11718 ((member tg current) c-face)
11719 ((member tg inherited) i-face)
11720 (t nil))))
11721 (if (and (= cnt 0) (not ingroup)) (insert " "))
11722 (insert "[" c "] " tg (make-string
11723 (- fwidth 4 (length tg)) ?\ ))
11724 (push (cons tg c) ntable)
11725 (when (= (setq cnt (1+ cnt)) ncol)
11726 (insert "\n")
11727 (if ingroup (insert " "))
11728 (setq cnt 0)))))
11729 (setq ntable (nreverse ntable))
11730 (insert "\n")
11731 (goto-char (point-min))
11732 (if (not expert) (org-fit-window-to-buffer))
11733 (setq rtn
11734 (catch 'exit
11735 (while t
11736 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
11737 (if (not groups) "no " "")
11738 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
11739 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
11740 (cond
11741 ((= c ?\r) (throw 'exit t))
11742 ((= c ?!)
11743 (setq groups (not groups))
11744 (goto-char (point-min))
11745 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
11746 ((= c ?\C-c)
11747 (if (not expert)
11748 (org-fast-tag-show-exit
11749 (setq exit-after-next (not exit-after-next)))
11750 (setq expert nil)
11751 (delete-other-windows)
11752 (split-window-vertically)
11753 (org-switch-to-buffer-other-window " *Org tags*")
11754 (org-fit-window-to-buffer)))
11755 ((or (= c ?\C-g)
11756 (and (= c ?q) (not (rassoc c ntable))))
11757 (org-detach-overlay org-tags-overlay)
11758 (setq quit-flag t))
11759 ((= c ?\ )
11760 (setq current nil)
11761 (if exit-after-next (setq exit-after-next 'now)))
11762 ((= c ?\t)
11763 (condition-case nil
11764 (setq tg (org-icompleting-read
11765 "Tag: "
11766 (or buffer-tags
11767 (with-current-buffer buf
11768 (org-get-buffer-tags)))))
11769 (quit (setq tg "")))
11770 (when (string-match "\\S-" tg)
11771 (add-to-list 'buffer-tags (list tg))
11772 (if (member tg current)
11773 (setq current (delete tg current))
11774 (push tg current)))
11775 (if exit-after-next (setq exit-after-next 'now)))
11776 ((setq e (rassoc c todo-table) tg (car e))
11777 (with-current-buffer buf
11778 (save-excursion (org-todo tg)))
11779 (if exit-after-next (setq exit-after-next 'now)))
11780 ((setq e (rassoc c ntable) tg (car e))
11781 (if (member tg current)
11782 (setq current (delete tg current))
11783 (loop for g in groups do
11784 (if (member tg g)
11785 (mapc (lambda (x)
11786 (setq current (delete x current)))
11787 g)))
11788 (push tg current))
11789 (if exit-after-next (setq exit-after-next 'now))))
11791 ;; Create a sorted list
11792 (setq current
11793 (sort current
11794 (lambda (a b)
11795 (assoc b (cdr (memq (assoc a ntable) ntable))))))
11796 (if (eq exit-after-next 'now) (throw 'exit t))
11797 (goto-char (point-min))
11798 (beginning-of-line 2)
11799 (delete-region (point) (point-at-eol))
11800 (org-fast-tag-insert "Current" current c-face)
11801 (org-set-current-tags-overlay current ov-prefix)
11802 (while (re-search-forward
11803 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
11804 (setq tg (match-string 1))
11805 (add-text-properties
11806 (match-beginning 1) (match-end 1)
11807 (list 'face
11808 (cond
11809 ((member tg current) c-face)
11810 ((member tg inherited) i-face)
11811 (t (get-text-property (match-beginning 1) 'face))))))
11812 (goto-char (point-min)))))
11813 (org-detach-overlay org-tags-overlay)
11814 (if rtn
11815 (mapconcat 'identity current ":")
11816 nil))))
11818 (defun org-get-tags-string ()
11819 "Get the TAGS string in the current headline."
11820 (unless (org-on-heading-p t)
11821 (error "Not on a heading"))
11822 (save-excursion
11823 (beginning-of-line 1)
11824 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
11825 (org-match-string-no-properties 1)
11826 "")))
11828 (defun org-get-tags ()
11829 "Get the list of tags specified in the current headline."
11830 (org-split-string (org-get-tags-string) ":"))
11832 (defun org-get-buffer-tags ()
11833 "Get a table of all tags used in the buffer, for completion."
11834 (let (tags)
11835 (save-excursion
11836 (goto-char (point-min))
11837 (while (re-search-forward
11838 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
11839 (when (equal (char-after (point-at-bol 0)) ?*)
11840 (mapc (lambda (x) (add-to-list 'tags x))
11841 (org-split-string (org-match-string-no-properties 1) ":")))))
11842 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
11843 (mapcar 'list tags)))
11845 ;;;; The mapping API
11847 ;;;###autoload
11848 (defun org-map-entries (func &optional match scope &rest skip)
11849 "Call FUNC at each headline selected by MATCH in SCOPE.
11851 FUNC is a function or a lisp form. The function will be called without
11852 arguments, with the cursor positioned at the beginning of the headline.
11853 The return values of all calls to the function will be collected and
11854 returned as a list.
11856 The call to FUNC will be wrapped into a save-excursion form, so FUNC
11857 does not need to preserve point. After evaluation, the cursor will be
11858 moved to the end of the line (presumably of the headline of the
11859 processed entry) and search continues from there. Under some
11860 circumstances, this may not produce the wanted results. For example,
11861 if you have removed (e.g. archived) the current (sub)tree it could
11862 mean that the next entry will be skipped entirely. In such cases, you
11863 can specify the position from where search should continue by making
11864 FUNC set the variable `org-map-continue-from' to the desired buffer
11865 position.
11867 MATCH is a tags/property/todo match as it is used in the agenda tags view.
11868 Only headlines that are matched by this query will be considered during
11869 the iteration. When MATCH is nil or t, all headlines will be
11870 visited by the iteration.
11872 SCOPE determines the scope of this command. It can be any of:
11874 nil The current buffer, respecting the restriction if any
11875 tree The subtree started with the entry at point
11876 file The current buffer, without restriction
11877 file-with-archives
11878 The current buffer, and any archives associated with it
11879 agenda All agenda files
11880 agenda-with-archives
11881 All agenda files with any archive files associated with them
11882 \(file1 file2 ...)
11883 If this is a list, all files in the list will be scanned
11885 The remaining args are treated as settings for the skipping facilities of
11886 the scanner. The following items can be given here:
11888 archive skip trees with the archive tag.
11889 comment skip trees with the COMMENT keyword
11890 function or Emacs Lisp form:
11891 will be used as value for `org-agenda-skip-function', so whenever
11892 the function returns t, FUNC will not be called for that
11893 entry and search will continue from the point where the
11894 function leaves it.
11896 If your function needs to retrieve the tags including inherited tags
11897 at the *current* entry, you can use the value of the variable
11898 `org-scanner-tags' which will be much faster than getting the value
11899 with `org-get-tags-at'. If your function gets properties with
11900 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
11901 to t around the call to `org-entry-properties' to get the same speedup.
11902 Note that if your function moves around to retrieve tags and properties at
11903 a *different* entry, you cannot use these techniques."
11904 (let* ((org-agenda-archives-mode nil) ; just to make sure
11905 (org-agenda-skip-archived-trees (memq 'archive skip))
11906 (org-agenda-skip-comment-trees (memq 'comment skip))
11907 (org-agenda-skip-function
11908 (car (org-delete-all '(comment archive) skip)))
11909 (org-tags-match-list-sublevels t)
11910 matcher file res
11911 org-todo-keywords-for-agenda
11912 org-done-keywords-for-agenda
11913 org-todo-keyword-alist-for-agenda
11914 org-drawers-for-agenda
11915 org-tag-alist-for-agenda)
11917 (cond
11918 ((eq match t) (setq matcher t))
11919 ((eq match nil) (setq matcher t))
11920 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
11922 (save-excursion
11923 (save-restriction
11924 (when (eq scope 'tree)
11925 (org-back-to-heading t)
11926 (org-narrow-to-subtree)
11927 (setq scope nil))
11929 (if (not scope)
11930 (progn
11931 (org-prepare-agenda-buffers
11932 (list (buffer-file-name (current-buffer))))
11933 (setq res (org-scan-tags func matcher)))
11934 ;; Get the right scope
11935 (cond
11936 ((and scope (listp scope) (symbolp (car scope)))
11937 (setq scope (eval scope)))
11938 ((eq scope 'agenda)
11939 (setq scope (org-agenda-files t)))
11940 ((eq scope 'agenda-with-archives)
11941 (setq scope (org-agenda-files t))
11942 (setq scope (org-add-archive-files scope)))
11943 ((eq scope 'file)
11944 (setq scope (list (buffer-file-name))))
11945 ((eq scope 'file-with-archives)
11946 (setq scope (org-add-archive-files (list (buffer-file-name))))))
11947 (org-prepare-agenda-buffers scope)
11948 (while (setq file (pop scope))
11949 (with-current-buffer (org-find-base-buffer-visiting file)
11950 (save-excursion
11951 (save-restriction
11952 (widen)
11953 (goto-char (point-min))
11954 (setq res (append res (org-scan-tags func matcher))))))))))
11955 res))
11957 ;;;; Properties
11959 ;;; Setting and retrieving properties
11961 (defconst org-special-properties
11962 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
11963 "TIMESTAMP" "TIMESTAMP_IA")
11964 "The special properties valid in Org-mode.
11966 These are properties that are not defined in the property drawer,
11967 but in some other way.")
11969 (defconst org-default-properties
11970 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
11971 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
11972 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
11973 "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
11974 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
11975 "CLOCK_MODELINE_TOTAL" "STYLE")
11976 "Some properties that are used by Org-mode for various purposes.
11977 Being in this list makes sure that they are offered for completion.")
11979 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
11980 "Regular expression matching the first line of a property drawer.")
11982 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
11983 "Regular expression matching the first line of a property drawer.")
11985 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
11986 "Regular expression matching the first line of a property drawer.")
11988 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
11989 "Regular expression matching the first line of a property drawer.")
11991 (defconst org-property-drawer-re
11992 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
11993 org-property-end-re "\\)\n?")
11994 "Matches an entire property drawer.")
11996 (defconst org-clock-drawer-re
11997 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
11998 org-property-end-re "\\)\n?")
11999 "Matches an entire clock drawer.")
12001 (defun org-property-action ()
12002 "Do an action on properties."
12003 (interactive)
12004 (let (c)
12005 (org-at-property-p)
12006 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
12007 (setq c (read-char-exclusive))
12008 (cond
12009 ((equal c ?s)
12010 (call-interactively 'org-set-property))
12011 ((equal c ?d)
12012 (call-interactively 'org-delete-property))
12013 ((equal c ?D)
12014 (call-interactively 'org-delete-property-globally))
12015 ((equal c ?c)
12016 (call-interactively 'org-compute-property-at-point))
12017 (t (error "No such property action %c" c)))))
12019 (defun org-set-effort (&optional value)
12020 "Set the effort property of the current entry.
12021 With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
12022 allowed value."
12023 (interactive "P")
12024 (if (equal value 0) (setq value 10))
12025 (let* ((completion-ignore-case t)
12026 (prop org-effort-property)
12027 (cur (org-entry-get nil prop))
12028 (allowed (org-property-get-allowed-values nil prop 'table))
12029 (existing (mapcar 'list (org-property-values prop)))
12030 (val (cond
12031 ((stringp value) value)
12032 ((and allowed (integerp value))
12033 (or (car (nth (1- value) allowed))
12034 (car (org-last allowed))))
12035 (allowed
12036 (message "Select 1-9,0, [RET%s]: %s"
12037 (if cur (concat "=" cur) "")
12038 (mapconcat 'car allowed " "))
12039 (setq rpl (read-char-exclusive))
12040 (if (equal rpl ?\r)
12041 (setq val cur)
12042 (setq rpl (- rpl ?0))
12043 (if (equal rpl 0) (setq rpl 10))
12044 (if (and (> rpl 0) (<= rpl (length allowed)))
12045 (car (nth (1- rpl) allowed))
12046 (org-completing-read "Value: " allowed nil))))
12048 (let (org-completion-use-ido org-completion-use-iswitchb)
12049 (org-completing-read
12050 (concat "Value " (if (and cur (string-match "\\S-" cur))
12051 (concat "[" cur "]") "")
12052 ": ")
12053 existing nil nil "" nil cur))))))
12054 (unless (equal (org-entry-get nil prop) val)
12055 (org-entry-put nil prop val))
12056 (message "%s is now %s" prop val)))
12058 (defun org-at-property-p ()
12059 "Is the cursor in a property line?"
12060 ;; FIXME: Does not check if we are actually in the drawer.
12061 ;; FIXME: also returns true on any drawers.....
12062 ;; This is used by C-c C-c for property action.
12063 (save-excursion
12064 (beginning-of-line 1)
12065 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
12067 (defun org-get-property-block (&optional beg end force)
12068 "Return the (beg . end) range of the body of the property drawer.
12069 BEG and END can be beginning and end of subtree, if not given
12070 they will be found.
12071 If the drawer does not exist and FORCE is non-nil, create the drawer."
12072 (catch 'exit
12073 (save-excursion
12074 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
12075 (end (or end (progn (outline-next-heading) (point)))))
12076 (goto-char beg)
12077 (if (re-search-forward org-property-start-re end t)
12078 (setq beg (1+ (match-end 0)))
12079 (if force
12080 (save-excursion
12081 (org-insert-property-drawer)
12082 (setq end (progn (outline-next-heading) (point))))
12083 (throw 'exit nil))
12084 (goto-char beg)
12085 (if (re-search-forward org-property-start-re end t)
12086 (setq beg (1+ (match-end 0)))))
12087 (if (re-search-forward org-property-end-re end t)
12088 (setq end (match-beginning 0))
12089 (or force (throw 'exit nil))
12090 (goto-char beg)
12091 (setq end beg)
12092 (org-indent-line-function)
12093 (insert ":END:\n"))
12094 (cons beg end)))))
12096 (defun org-entry-properties (&optional pom which)
12097 "Get all properties of the entry at point-or-marker POM.
12098 This includes the TODO keyword, the tags, time strings for deadline,
12099 scheduled, and clocking, and any additional properties defined in the
12100 entry. The return value is an alist, keys may occur multiple times
12101 if the property key was used several times.
12102 POM may also be nil, in which case the current entry is used.
12103 If WHICH is nil or `all', get all properties. If WHICH is
12104 `special' or `standard', only get that subclass."
12105 (setq which (or which 'all))
12106 (org-with-point-at pom
12107 (let ((clockstr (substring org-clock-string 0 -1))
12108 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
12109 beg end range props sum-props key value string clocksum)
12110 (save-excursion
12111 (when (condition-case nil
12112 (and (org-mode-p) (org-back-to-heading t))
12113 (error nil))
12114 (setq beg (point))
12115 (setq sum-props (get-text-property (point) 'org-summaries))
12116 (setq clocksum (get-text-property (point) :org-clock-minutes))
12117 (outline-next-heading)
12118 (setq end (point))
12119 (when (memq which '(all special))
12120 ;; Get the special properties, like TODO and tags
12121 (goto-char beg)
12122 (when (and (looking-at org-todo-line-regexp) (match-end 2))
12123 (push (cons "TODO" (org-match-string-no-properties 2)) props))
12124 (when (looking-at org-priority-regexp)
12125 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
12126 (when (and (setq value (org-get-tags-string))
12127 (string-match "\\S-" value))
12128 (push (cons "TAGS" value) props))
12129 (when (setq value (org-get-tags-at))
12130 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
12131 props))
12132 (while (re-search-forward org-maybe-keyword-time-regexp end t)
12133 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
12134 string (if (equal key clockstr)
12135 (org-no-properties
12136 (org-trim
12137 (buffer-substring
12138 (match-beginning 3) (goto-char (point-at-eol)))))
12139 (substring (org-match-string-no-properties 3) 1 -1)))
12140 (unless key
12141 (if (= (char-after (match-beginning 3)) ?\[)
12142 (setq key "TIMESTAMP_IA")
12143 (setq key "TIMESTAMP")))
12144 (when (or (equal key clockstr) (not (assoc key props)))
12145 (push (cons key string) props)))
12149 (when (memq which '(all standard))
12150 ;; Get the standard properties, like :PROP: ...
12151 (setq range (org-get-property-block beg end))
12152 (when range
12153 (goto-char (car range))
12154 (while (re-search-forward
12155 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
12156 (cdr range) t)
12157 (setq key (org-match-string-no-properties 1)
12158 value (org-trim (or (org-match-string-no-properties 2) "")))
12159 (unless (member key excluded)
12160 (push (cons key (or value "")) props)))))
12161 (if clocksum
12162 (push (cons "CLOCKSUM"
12163 (org-columns-number-to-string (/ (float clocksum) 60.)
12164 'add_times))
12165 props))
12166 (unless (assoc "CATEGORY" props)
12167 (setq value (or (org-get-category)
12168 (progn (org-refresh-category-properties)
12169 (org-get-category))))
12170 (push (cons "CATEGORY" value) props))
12171 (append sum-props (nreverse props)))))))
12173 (defun org-entry-get (pom property &optional inherit)
12174 "Get value of PROPERTY for entry at point-or-marker POM.
12175 If INHERIT is non-nil and the entry does not have the property,
12176 then also check higher levels of the hierarchy.
12177 If INHERIT is the symbol `selective', use inheritance only if the setting
12178 in `org-use-property-inheritance' selects PROPERTY for inheritance.
12179 If the property is present but empty, the return value is the empty string.
12180 If the property is not present at all, nil is returned."
12181 (org-with-point-at pom
12182 (if (and inherit (if (eq inherit 'selective)
12183 (org-property-inherit-p property)
12185 (org-entry-get-with-inheritance property)
12186 (if (member property org-special-properties)
12187 ;; We need a special property. Use brute force, get all properties.
12188 (cdr (assoc property (org-entry-properties nil 'special)))
12189 (let ((range (org-get-property-block)))
12190 (if (and range
12191 (goto-char (car range))
12192 (re-search-forward
12193 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
12194 (cdr range) t))
12195 ;; Found the property, return it.
12196 (if (match-end 1)
12197 (org-match-string-no-properties 1)
12198 "")))))))
12200 (defun org-property-or-variable-value (var &optional inherit)
12201 "Check if there is a property fixing the value of VAR.
12202 If yes, return this value. If not, return the current value of the variable."
12203 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
12204 (if (and prop (stringp prop) (string-match "\\S-" prop))
12205 (read prop)
12206 (symbol-value var))))
12208 (defun org-entry-delete (pom property)
12209 "Delete the property PROPERTY from entry at point-or-marker POM."
12210 (org-with-point-at pom
12211 (if (member property org-special-properties)
12212 nil ; cannot delete these properties.
12213 (let ((range (org-get-property-block)))
12214 (if (and range
12215 (goto-char (car range))
12216 (re-search-forward
12217 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
12218 (cdr range) t))
12219 (progn
12220 (delete-region (match-beginning 0) (1+ (point-at-eol)))
12222 nil)))))
12224 ;; Multi-values properties are properties that contain multiple values
12225 ;; These values are assumed to be single words, separated by whitespace.
12226 (defun org-entry-add-to-multivalued-property (pom property value)
12227 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
12228 (let* ((old (org-entry-get pom property))
12229 (values (and old (org-split-string old "[ \t]"))))
12230 (setq value (org-entry-protect-space value))
12231 (unless (member value values)
12232 (setq values (cons value values))
12233 (org-entry-put pom property
12234 (mapconcat 'identity values " ")))))
12236 (defun org-entry-remove-from-multivalued-property (pom property value)
12237 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
12238 (let* ((old (org-entry-get pom property))
12239 (values (and old (org-split-string old "[ \t]"))))
12240 (setq value (org-entry-protect-space value))
12241 (when (member value values)
12242 (setq values (delete value values))
12243 (org-entry-put pom property
12244 (mapconcat 'identity values " ")))))
12246 (defun org-entry-member-in-multivalued-property (pom property value)
12247 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
12248 (let* ((old (org-entry-get pom property))
12249 (values (and old (org-split-string old "[ \t]"))))
12250 (setq value (org-entry-protect-space value))
12251 (member value values)))
12253 (defun org-entry-get-multivalued-property (pom property)
12254 "Return a list of values in a multivalued property."
12255 (let* ((value (org-entry-get pom property))
12256 (values (and value (org-split-string value "[ \t]"))))
12257 (mapcar 'org-entry-restore-space values)))
12259 (defun org-entry-put-multivalued-property (pom property &rest values)
12260 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
12261 VALUES should be a list of strings. Spaces will be protected."
12262 (org-entry-put pom property
12263 (mapconcat 'org-entry-protect-space values " "))
12264 (let* ((value (org-entry-get pom property))
12265 (values (and value (org-split-string value "[ \t]"))))
12266 (mapcar 'org-entry-restore-space values)))
12268 (defun org-entry-protect-space (s)
12269 "Protect spaces and newline in string S."
12270 (while (string-match " " s)
12271 (setq s (replace-match "%20" t t s)))
12272 (while (string-match "\n" s)
12273 (setq s (replace-match "%0A" t t s)))
12276 (defun org-entry-restore-space (s)
12277 "Restore spaces and newline in string S."
12278 (while (string-match "%20" s)
12279 (setq s (replace-match " " t t s)))
12280 (while (string-match "%0A" s)
12281 (setq s (replace-match "\n" t t s)))
12284 (defvar org-entry-property-inherited-from (make-marker)
12285 "Marker pointing to the entry from where a property was inherited.
12286 Each call to `org-entry-get-with-inheritance' will set this marker to the
12287 location of the entry where the inheritance search matched. If there was
12288 no match, the marker will point nowhere.
12289 Note that also `org-entry-get' calls this function, if the INHERIT flag
12290 is set.")
12292 (defun org-entry-get-with-inheritance (property)
12293 "Get entry property, and search higher levels if not present."
12294 (move-marker org-entry-property-inherited-from nil)
12295 (let (tmp)
12296 (save-excursion
12297 (save-restriction
12298 (widen)
12299 (catch 'ex
12300 (while t
12301 (when (setq tmp (org-entry-get nil property))
12302 (org-back-to-heading t)
12303 (move-marker org-entry-property-inherited-from (point))
12304 (throw 'ex tmp))
12305 (or (org-up-heading-safe) (throw 'ex nil)))))
12306 (or tmp
12307 (cdr (assoc property org-file-properties))
12308 (cdr (assoc property org-global-properties))
12309 (cdr (assoc property org-global-properties-fixed))))))
12311 (defun org-entry-put (pom property value)
12312 "Set PROPERTY to VALUE for entry at point-or-marker POM."
12313 (org-with-point-at pom
12314 (org-back-to-heading t)
12315 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
12316 range)
12317 (cond
12318 ((equal property "TODO")
12319 (when (and (stringp value) (string-match "\\S-" value)
12320 (not (member value org-todo-keywords-1)))
12321 (error "\"%s\" is not a valid TODO state" value))
12322 (if (or (not value)
12323 (not (string-match "\\S-" value)))
12324 (setq value 'none))
12325 (org-todo value)
12326 (org-set-tags nil 'align))
12327 ((equal property "PRIORITY")
12328 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
12329 (string-to-char value) ?\ ))
12330 (org-set-tags nil 'align))
12331 ((equal property "SCHEDULED")
12332 (if (re-search-forward org-scheduled-time-regexp end t)
12333 (cond
12334 ((eq value 'earlier) (org-timestamp-change -1 'day))
12335 ((eq value 'later) (org-timestamp-change 1 'day))
12336 (t (call-interactively 'org-schedule)))
12337 (call-interactively 'org-schedule)))
12338 ((equal property "DEADLINE")
12339 (if (re-search-forward org-deadline-time-regexp end t)
12340 (cond
12341 ((eq value 'earlier) (org-timestamp-change -1 'day))
12342 ((eq value 'later) (org-timestamp-change 1 'day))
12343 (t (call-interactively 'org-deadline)))
12344 (call-interactively 'org-deadline)))
12345 ((member property org-special-properties)
12346 (error "The %s property can not yet be set with `org-entry-put'"
12347 property))
12348 (t ; a non-special property
12349 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
12350 (setq range (org-get-property-block beg end 'force))
12351 (goto-char (car range))
12352 (if (re-search-forward
12353 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
12354 (progn
12355 (delete-region (match-beginning 1) (match-end 1))
12356 (goto-char (match-beginning 1)))
12357 (goto-char (cdr range))
12358 (insert "\n")
12359 (backward-char 1)
12360 (org-indent-line-function)
12361 (insert ":" property ":"))
12362 (and value (insert " " value))
12363 (org-indent-line-function)))))))
12365 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
12366 "Get all property keys in the current buffer.
12367 With INCLUDE-SPECIALS, also list the special properties that reflect things
12368 like tags and TODO state.
12369 With INCLUDE-DEFAULTS, also include properties that has special meaning
12370 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
12371 With INCLUDE-COLUMNS, also include property names given in COLUMN
12372 formats in the current buffer."
12373 (let (rtn range cfmt s p)
12374 (save-excursion
12375 (save-restriction
12376 (widen)
12377 (goto-char (point-min))
12378 (while (re-search-forward org-property-start-re nil t)
12379 (setq range (org-get-property-block))
12380 (goto-char (car range))
12381 (while (re-search-forward
12382 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
12383 (cdr range) t)
12384 (add-to-list 'rtn (org-match-string-no-properties 1)))
12385 (outline-next-heading))))
12387 (when include-specials
12388 (setq rtn (append org-special-properties rtn)))
12390 (when include-defaults
12391 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
12392 (add-to-list 'rtn org-effort-property))
12394 (when include-columns
12395 (save-excursion
12396 (save-restriction
12397 (widen)
12398 (goto-char (point-min))
12399 (while (re-search-forward
12400 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
12401 nil t)
12402 (setq cfmt (match-string 2) s 0)
12403 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
12404 cfmt s)
12405 (setq s (match-end 0)
12406 p (match-string 1 cfmt))
12407 (unless (or (equal p "ITEM")
12408 (member p org-special-properties))
12409 (add-to-list 'rtn (match-string 1 cfmt))))))))
12411 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
12413 (defun org-property-values (key)
12414 "Return a list of all values of property KEY."
12415 (save-excursion
12416 (save-restriction
12417 (widen)
12418 (goto-char (point-min))
12419 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
12420 values)
12421 (while (re-search-forward re nil t)
12422 (add-to-list 'values (org-trim (match-string 1))))
12423 (delete "" values)))))
12425 (defun org-insert-property-drawer ()
12426 "Insert a property drawer into the current entry."
12427 (interactive)
12428 (org-back-to-heading t)
12429 (looking-at outline-regexp)
12430 (let ((indent (if org-adapt-indentation
12431 (- (match-end 0)(match-beginning 0))
12433 (beg (point))
12434 (re (concat "^[ \t]*" org-keyword-time-regexp))
12435 end hiddenp)
12436 (outline-next-heading)
12437 (setq end (point))
12438 (goto-char beg)
12439 (while (re-search-forward re end t))
12440 (setq hiddenp (org-invisible-p))
12441 (end-of-line 1)
12442 (and (equal (char-after) ?\n) (forward-char 1))
12443 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
12444 (if (member (match-string 1) '("CLOCK:" ":END:"))
12445 ;; just skip this line
12446 (beginning-of-line 2)
12447 ;; Drawer start, find the end
12448 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
12449 (beginning-of-line 1)))
12450 (org-skip-over-state-notes)
12451 (skip-chars-backward " \t\n\r")
12452 (if (eq (char-before) ?*) (forward-char 1))
12453 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
12454 (beginning-of-line 0)
12455 (org-indent-to-column indent)
12456 (beginning-of-line 2)
12457 (org-indent-to-column indent)
12458 (beginning-of-line 0)
12459 (if hiddenp
12460 (save-excursion
12461 (org-back-to-heading t)
12462 (hide-entry))
12463 (org-flag-drawer t))))
12465 (defun org-set-property (property value)
12466 "In the current entry, set PROPERTY to VALUE.
12467 When called interactively, this will prompt for a property name, offering
12468 completion on existing and default properties. And then it will prompt
12469 for a value, offering completion either on allowed values (via an inherited
12470 xxx_ALL property) or on existing values in other instances of this property
12471 in the current file."
12472 (interactive
12473 (let* ((completion-ignore-case t)
12474 (keys (org-buffer-property-keys nil t t))
12475 (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
12476 (prop (if (member prop0 keys)
12477 prop0
12478 (or (cdr (assoc (downcase prop0)
12479 (mapcar (lambda (x) (cons (downcase x) x))
12480 keys)))
12481 prop0)))
12482 (cur (org-entry-get nil prop))
12483 (allowed (org-property-get-allowed-values nil prop 'table))
12484 (existing (mapcar 'list (org-property-values prop)))
12485 (val (if allowed
12486 (org-completing-read "Value: " allowed nil 'req-match)
12487 (let (org-completion-use-ido org-completion-use-iswitchb)
12488 (org-completing-read
12489 (concat "Value " (if (and cur (string-match "\\S-" cur))
12490 (concat "[" cur "]") "")
12491 ": ")
12492 existing nil nil "" nil cur)))))
12493 (list prop (if (equal val "") cur val))))
12494 (unless (equal (org-entry-get nil property) value)
12495 (org-entry-put nil property value)))
12497 (defun org-delete-property (property)
12498 "In the current entry, delete PROPERTY."
12499 (interactive
12500 (let* ((completion-ignore-case t)
12501 (prop (org-icompleting-read
12502 "Property: " (org-entry-properties nil 'standard))))
12503 (list prop)))
12504 (message "Property %s %s" property
12505 (if (org-entry-delete nil property)
12506 "deleted"
12507 "was not present in the entry")))
12509 (defun org-delete-property-globally (property)
12510 "Remove PROPERTY globally, from all entries."
12511 (interactive
12512 (let* ((completion-ignore-case t)
12513 (prop (org-icompleting-read
12514 "Globally remove property: "
12515 (mapcar 'list (org-buffer-property-keys)))))
12516 (list prop)))
12517 (save-excursion
12518 (save-restriction
12519 (widen)
12520 (goto-char (point-min))
12521 (let ((cnt 0))
12522 (while (re-search-forward
12523 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
12524 nil t)
12525 (setq cnt (1+ cnt))
12526 (replace-match ""))
12527 (message "Property \"%s\" removed from %d entries" property cnt)))))
12529 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
12531 (defun org-compute-property-at-point ()
12532 "Compute the property at point.
12533 This looks for an enclosing column format, extracts the operator and
12534 then applies it to the property in the column format's scope."
12535 (interactive)
12536 (unless (org-at-property-p)
12537 (error "Not at a property"))
12538 (let ((prop (org-match-string-no-properties 2)))
12539 (org-columns-get-format-and-top-level)
12540 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
12541 (error "No operator defined for property %s" prop))
12542 (org-columns-compute prop)))
12544 (defun org-property-get-allowed-values (pom property &optional table)
12545 "Get allowed values for the property PROPERTY.
12546 When TABLE is non-nil, return an alist that can directly be used for
12547 completion."
12548 (let (vals)
12549 (cond
12550 ((equal property "TODO")
12551 (setq vals (org-with-point-at pom
12552 (append org-todo-keywords-1 '("")))))
12553 ((equal property "PRIORITY")
12554 (let ((n org-lowest-priority))
12555 (while (>= n org-highest-priority)
12556 (push (char-to-string n) vals)
12557 (setq n (1- n)))))
12558 ((member property org-special-properties))
12560 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
12562 (when (and vals (string-match "\\S-" vals))
12563 (setq vals (car (read-from-string (concat "(" vals ")"))))
12564 (setq vals (mapcar (lambda (x)
12565 (cond ((stringp x) x)
12566 ((numberp x) (number-to-string x))
12567 ((symbolp x) (symbol-name x))
12568 (t "???")))
12569 vals)))))
12570 (if table (mapcar 'list vals) vals)))
12572 (defun org-property-previous-allowed-value (&optional previous)
12573 "Switch to the next allowed value for this property."
12574 (interactive)
12575 (org-property-next-allowed-value t))
12577 (defun org-property-next-allowed-value (&optional previous)
12578 "Switch to the next allowed value for this property."
12579 (interactive)
12580 (unless (org-at-property-p)
12581 (error "Not at a property"))
12582 (let* ((key (match-string 2))
12583 (value (match-string 3))
12584 (allowed (or (org-property-get-allowed-values (point) key)
12585 (and (member value '("[ ]" "[-]" "[X]"))
12586 '("[ ]" "[X]"))))
12587 nval)
12588 (unless allowed
12589 (error "Allowed values for this property have not been defined"))
12590 (if previous (setq allowed (reverse allowed)))
12591 (if (member value allowed)
12592 (setq nval (car (cdr (member value allowed)))))
12593 (setq nval (or nval (car allowed)))
12594 (if (equal nval value)
12595 (error "Only one allowed value for this property"))
12596 (org-at-property-p)
12597 (replace-match (concat " :" key ": " nval) t t)
12598 (org-indent-line-function)
12599 (beginning-of-line 1)
12600 (skip-chars-forward " \t")))
12602 (defun org-find-entry-with-id (ident)
12603 "Locate the entry that contains the ID property with exact value IDENT.
12604 IDENT can be a string, a symbol or a number, this function will search for
12605 the string representation of it.
12606 Return the position where this entry starts, or nil if there is no such entry."
12607 (interactive "sID: ")
12608 (let ((id (cond
12609 ((stringp ident) ident)
12610 ((symbol-name ident) (symbol-name ident))
12611 ((numberp ident) (number-to-string ident))
12612 (t (error "IDENT %s must be a string, symbol or number" ident))))
12613 (case-fold-search nil))
12614 (save-excursion
12615 (save-restriction
12616 (widen)
12617 (goto-char (point-min))
12618 (when (re-search-forward
12619 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
12620 nil t)
12621 (org-back-to-heading t)
12622 (point))))))
12624 ;;;; Timestamps
12626 (defvar org-last-changed-timestamp nil)
12627 (defvar org-last-inserted-timestamp nil
12628 "The last time stamp inserted with `org-insert-time-stamp'.")
12629 (defvar org-time-was-given) ; dynamically scoped parameter
12630 (defvar org-end-time-was-given) ; dynamically scoped parameter
12631 (defvar org-ts-what) ; dynamically scoped parameter
12633 (defun org-time-stamp (arg &optional inactive)
12634 "Prompt for a date/time and insert a time stamp.
12635 If the user specifies a time like HH:MM, or if this command is called
12636 with a prefix argument, the time stamp will contain date and time.
12637 Otherwise, only the date will be included. All parts of a date not
12638 specified by the user will be filled in from the current date/time.
12639 So if you press just return without typing anything, the time stamp
12640 will represent the current date/time. If there is already a timestamp
12641 at the cursor, it will be modified."
12642 (interactive "P")
12643 (let* ((ts nil)
12644 (default-time
12645 ;; Default time is either today, or, when entering a range,
12646 ;; the range start.
12647 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
12648 (save-excursion
12649 (re-search-backward
12650 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
12651 (- (point) 20) t)))
12652 (apply 'encode-time (org-parse-time-string (match-string 1)))
12653 (current-time)))
12654 (default-input (and ts (org-get-compact-tod ts)))
12655 org-time-was-given org-end-time-was-given time)
12656 (cond
12657 ((and (org-at-timestamp-p t)
12658 (memq last-command '(org-time-stamp org-time-stamp-inactive))
12659 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
12660 (insert "--")
12661 (setq time (let ((this-command this-command))
12662 (org-read-date arg 'totime nil nil
12663 default-time default-input)))
12664 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
12665 ((org-at-timestamp-p t)
12666 (setq time (let ((this-command this-command))
12667 (org-read-date arg 'totime nil nil default-time default-input)))
12668 (when (org-at-timestamp-p t) ; just to get the match data
12669 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
12670 (replace-match "")
12671 (setq org-last-changed-timestamp
12672 (org-insert-time-stamp
12673 time (or org-time-was-given arg)
12674 inactive nil nil (list org-end-time-was-given))))
12675 (message "Timestamp updated"))
12677 (setq time (let ((this-command this-command))
12678 (org-read-date arg 'totime nil nil default-time default-input)))
12679 (org-insert-time-stamp time (or org-time-was-given arg) inactive
12680 nil nil (list org-end-time-was-given))))))
12682 ;; FIXME: can we use this for something else, like computing time differences?
12683 (defun org-get-compact-tod (s)
12684 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
12685 (let* ((t1 (match-string 1 s))
12686 (h1 (string-to-number (match-string 2 s)))
12687 (m1 (string-to-number (match-string 3 s)))
12688 (t2 (and (match-end 4) (match-string 5 s)))
12689 (h2 (and t2 (string-to-number (match-string 6 s))))
12690 (m2 (and t2 (string-to-number (match-string 7 s))))
12691 dh dm)
12692 (if (not t2)
12694 (setq dh (- h2 h1) dm (- m2 m1))
12695 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
12696 (concat t1 "+" (number-to-string dh)
12697 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
12699 (defun org-time-stamp-inactive (&optional arg)
12700 "Insert an inactive time stamp.
12701 An inactive time stamp is enclosed in square brackets instead of angle
12702 brackets. It is inactive in the sense that it does not trigger agenda entries,
12703 does not link to the calendar and cannot be changed with the S-cursor keys.
12704 So these are more for recording a certain time/date."
12705 (interactive "P")
12706 (org-time-stamp arg 'inactive))
12708 (defvar org-date-ovl (org-make-overlay 1 1))
12709 (org-overlay-put org-date-ovl 'face 'org-warning)
12710 (org-detach-overlay org-date-ovl)
12712 (defvar org-ans1) ; dynamically scoped parameter
12713 (defvar org-ans2) ; dynamically scoped parameter
12715 (defvar org-plain-time-of-day-regexp) ; defined below
12717 (defvar org-overriding-default-time nil) ; dynamically scoped
12718 (defvar org-read-date-overlay nil)
12719 (defvar org-dcst nil) ; dynamically scoped
12720 (defvar org-read-date-history nil)
12721 (defvar org-read-date-final-answer nil)
12723 (defun org-read-date (&optional with-time to-time from-string prompt
12724 default-time default-input)
12725 "Read a date, possibly a time, and make things smooth for the user.
12726 The prompt will suggest to enter an ISO date, but you can also enter anything
12727 which will at least partially be understood by `parse-time-string'.
12728 Unrecognized parts of the date will default to the current day, month, year,
12729 hour and minute. If this command is called to replace a timestamp at point,
12730 of to enter the second timestamp of a range, the default time is taken from the
12731 existing stamp. For example,
12732 3-2-5 --> 2003-02-05
12733 feb 15 --> currentyear-02-15
12734 sep 12 9 --> 2009-09-12
12735 12:45 --> today 12:45
12736 22 sept 0:34 --> currentyear-09-22 0:34
12737 12 --> currentyear-currentmonth-12
12738 Fri --> nearest Friday (today or later)
12739 etc.
12741 Furthermore you can specify a relative date by giving, as the *first* thing
12742 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
12743 change in days weeks, months, years.
12744 With a single plus or minus, the date is relative to today. With a double
12745 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
12746 +4d --> four days from today
12747 +4 --> same as above
12748 +2w --> two weeks from today
12749 ++5 --> five days from default date
12751 The function understands only English month and weekday abbreviations,
12752 but this can be configured with the variables `parse-time-months' and
12753 `parse-time-weekdays'.
12755 While prompting, a calendar is popped up - you can also select the
12756 date with the mouse (button 1). The calendar shows a period of three
12757 months. To scroll it to other months, use the keys `>' and `<'.
12758 If you don't like the calendar, turn it off with
12759 \(setq org-read-date-popup-calendar nil)
12761 With optional argument TO-TIME, the date will immediately be converted
12762 to an internal time.
12763 With an optional argument WITH-TIME, the prompt will suggest to also
12764 insert a time. Note that when WITH-TIME is not set, you can still
12765 enter a time, and this function will inform the calling routine about
12766 this change. The calling routine may then choose to change the format
12767 used to insert the time stamp into the buffer to include the time.
12768 With optional argument FROM-STRING, read from this string instead from
12769 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
12770 the time/date that is used for everything that is not specified by the
12771 user."
12772 (require 'parse-time)
12773 (let* ((org-time-stamp-rounding-minutes
12774 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
12775 (org-dcst org-display-custom-times)
12776 (ct (org-current-time))
12777 (def (or org-overriding-default-time default-time ct))
12778 (defdecode (decode-time def))
12779 (dummy (progn
12780 (when (< (nth 2 defdecode) org-extend-today-until)
12781 (setcar (nthcdr 2 defdecode) -1)
12782 (setcar (nthcdr 1 defdecode) 59)
12783 (setq def (apply 'encode-time defdecode)
12784 defdecode (decode-time def)))))
12785 (calendar-frame-setup nil)
12786 (calendar-move-hook nil)
12787 (calendar-view-diary-initially-flag nil)
12788 (view-diary-entries-initially nil)
12789 (calendar-view-holidays-initially-flag nil)
12790 (view-calendar-holidays-initially nil)
12791 (timestr (format-time-string
12792 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
12793 (prompt (concat (if prompt (concat prompt " ") "")
12794 (format "Date+time [%s]: " timestr)))
12795 ans (org-ans0 "") org-ans1 org-ans2 final)
12797 (cond
12798 (from-string (setq ans from-string))
12799 (org-read-date-popup-calendar
12800 (save-excursion
12801 (save-window-excursion
12802 (calendar)
12803 (calendar-forward-day (- (time-to-days def)
12804 (calendar-absolute-from-gregorian
12805 (calendar-current-date))))
12806 (org-eval-in-calendar nil t)
12807 (let* ((old-map (current-local-map))
12808 (map (copy-keymap calendar-mode-map))
12809 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
12810 (org-defkey map (kbd "RET") 'org-calendar-select)
12811 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
12812 'org-calendar-select-mouse)
12813 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
12814 'org-calendar-select-mouse)
12815 (org-defkey minibuffer-local-map [(meta shift left)]
12816 (lambda () (interactive)
12817 (org-eval-in-calendar '(calendar-backward-month 1))))
12818 (org-defkey minibuffer-local-map [(meta shift right)]
12819 (lambda () (interactive)
12820 (org-eval-in-calendar '(calendar-forward-month 1))))
12821 (org-defkey minibuffer-local-map [(meta shift up)]
12822 (lambda () (interactive)
12823 (org-eval-in-calendar '(calendar-backward-year 1))))
12824 (org-defkey minibuffer-local-map [(meta shift down)]
12825 (lambda () (interactive)
12826 (org-eval-in-calendar '(calendar-forward-year 1))))
12827 (org-defkey minibuffer-local-map [?\e (shift left)]
12828 (lambda () (interactive)
12829 (org-eval-in-calendar '(calendar-backward-month 1))))
12830 (org-defkey minibuffer-local-map [?\e (shift right)]
12831 (lambda () (interactive)
12832 (org-eval-in-calendar '(calendar-forward-month 1))))
12833 (org-defkey minibuffer-local-map [?\e (shift up)]
12834 (lambda () (interactive)
12835 (org-eval-in-calendar '(calendar-backward-year 1))))
12836 (org-defkey minibuffer-local-map [?\e (shift down)]
12837 (lambda () (interactive)
12838 (org-eval-in-calendar '(calendar-forward-year 1))))
12839 (org-defkey minibuffer-local-map [(shift up)]
12840 (lambda () (interactive)
12841 (org-eval-in-calendar '(calendar-backward-week 1))))
12842 (org-defkey minibuffer-local-map [(shift down)]
12843 (lambda () (interactive)
12844 (org-eval-in-calendar '(calendar-forward-week 1))))
12845 (org-defkey minibuffer-local-map [(shift left)]
12846 (lambda () (interactive)
12847 (org-eval-in-calendar '(calendar-backward-day 1))))
12848 (org-defkey minibuffer-local-map [(shift right)]
12849 (lambda () (interactive)
12850 (org-eval-in-calendar '(calendar-forward-day 1))))
12851 (org-defkey minibuffer-local-map ">"
12852 (lambda () (interactive)
12853 (org-eval-in-calendar '(scroll-calendar-left 1))))
12854 (org-defkey minibuffer-local-map "<"
12855 (lambda () (interactive)
12856 (org-eval-in-calendar '(scroll-calendar-right 1))))
12857 (run-hooks 'org-read-date-minibuffer-setup-hook)
12858 (unwind-protect
12859 (progn
12860 (use-local-map map)
12861 (add-hook 'post-command-hook 'org-read-date-display)
12862 (setq org-ans0 (read-string prompt default-input
12863 'org-read-date-history nil))
12864 ;; org-ans0: from prompt
12865 ;; org-ans1: from mouse click
12866 ;; org-ans2: from calendar motion
12867 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
12868 (remove-hook 'post-command-hook 'org-read-date-display)
12869 (use-local-map old-map)
12870 (when org-read-date-overlay
12871 (org-delete-overlay org-read-date-overlay)
12872 (setq org-read-date-overlay nil)))))))
12874 (t ; Naked prompt only
12875 (unwind-protect
12876 (setq ans (read-string prompt default-input
12877 'org-read-date-history timestr))
12878 (when org-read-date-overlay
12879 (org-delete-overlay org-read-date-overlay)
12880 (setq org-read-date-overlay nil)))))
12882 (setq final (org-read-date-analyze ans def defdecode))
12883 (setq org-read-date-final-answer ans)
12885 (if to-time
12886 (apply 'encode-time final)
12887 (if (and (boundp 'org-time-was-given) org-time-was-given)
12888 (format "%04d-%02d-%02d %02d:%02d"
12889 (nth 5 final) (nth 4 final) (nth 3 final)
12890 (nth 2 final) (nth 1 final))
12891 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
12893 (defvar def)
12894 (defvar defdecode)
12895 (defvar with-time)
12896 (defvar org-read-date-analyze-futurep nil)
12897 (defun org-read-date-display ()
12898 "Display the current date prompt interpretation in the minibuffer."
12899 (when org-read-date-display-live
12900 (when org-read-date-overlay
12901 (org-delete-overlay org-read-date-overlay))
12902 (let ((p (point)))
12903 (end-of-line 1)
12904 (while (not (equal (buffer-substring
12905 (max (point-min) (- (point) 4)) (point))
12906 " "))
12907 (insert " "))
12908 (goto-char p))
12909 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
12910 " " (or org-ans1 org-ans2)))
12911 (org-end-time-was-given nil)
12912 (f (org-read-date-analyze ans def defdecode))
12913 (fmts (if org-dcst
12914 org-time-stamp-custom-formats
12915 org-time-stamp-formats))
12916 (fmt (if (or with-time
12917 (and (boundp 'org-time-was-given) org-time-was-given))
12918 (cdr fmts)
12919 (car fmts)))
12920 (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
12921 (when (and org-end-time-was-given
12922 (string-match org-plain-time-of-day-regexp txt))
12923 (setq txt (concat (substring txt 0 (match-end 0)) "-"
12924 org-end-time-was-given
12925 (substring txt (match-end 0)))))
12926 (when org-read-date-analyze-futurep
12927 (setq txt (concat txt " (=>F)")))
12928 (setq org-read-date-overlay
12929 (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
12930 (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
12932 (defun org-read-date-analyze (ans def defdecode)
12933 "Analyse the combined answer of the date prompt."
12934 ;; FIXME: cleanup and comment
12935 (let (delta deltan deltaw deltadef year month day
12936 hour minute second wday pm h2 m2 tl wday1
12937 iso-year iso-weekday iso-week iso-year iso-date futurep)
12938 (setq org-read-date-analyze-futurep nil)
12939 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
12940 (setq ans "+0"))
12942 (when (setq delta (org-read-date-get-relative ans (current-time) def))
12943 (setq ans (replace-match "" t t ans)
12944 deltan (car delta)
12945 deltaw (nth 1 delta)
12946 deltadef (nth 2 delta)))
12948 ;; Check if there is an iso week date in there
12949 ;; If yes, sore the info and postpone interpreting it until the rest
12950 ;; of the parsing is done
12951 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
12952 (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
12953 iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
12954 iso-week (string-to-number (match-string 2 ans)))
12955 (setq ans (replace-match "" t t ans)))
12957 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
12958 (when (string-match
12959 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
12960 (setq year (if (match-end 2)
12961 (string-to-number (match-string 2 ans))
12962 (string-to-number (format-time-string "%Y")))
12963 month (string-to-number (match-string 3 ans))
12964 day (string-to-number (match-string 4 ans)))
12965 (if (< year 100) (setq year (+ 2000 year)))
12966 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
12967 t nil ans)))
12968 ;; Help matching am/pm times, because `parse-time-string' does not do that.
12969 ;; If there is a time with am/pm, and *no* time without it, we convert
12970 ;; so that matching will be successful.
12971 (loop for i from 1 to 2 do ; twice, for end time as well
12972 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
12973 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
12974 (setq hour (string-to-number (match-string 1 ans))
12975 minute (if (match-end 3)
12976 (string-to-number (match-string 3 ans))
12978 pm (equal ?p
12979 (string-to-char (downcase (match-string 4 ans)))))
12980 (if (and (= hour 12) (not pm))
12981 (setq hour 0)
12982 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
12983 (setq ans (replace-match (format "%02d:%02d" hour minute)
12984 t t ans))))
12986 ;; Check if a time range is given as a duration
12987 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
12988 (setq hour (string-to-number (match-string 1 ans))
12989 h2 (+ hour (string-to-number (match-string 3 ans)))
12990 minute (string-to-number (match-string 2 ans))
12991 m2 (+ minute (if (match-end 5) (string-to-number
12992 (match-string 5 ans))0)))
12993 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
12994 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
12995 t t ans)))
12997 ;; Check if there is a time range
12998 (when (boundp 'org-end-time-was-given)
12999 (setq org-time-was-given nil)
13000 (when (and (string-match org-plain-time-of-day-regexp ans)
13001 (match-end 8))
13002 (setq org-end-time-was-given (match-string 8 ans))
13003 (setq ans (concat (substring ans 0 (match-beginning 7))
13004 (substring ans (match-end 7))))))
13006 (setq tl (parse-time-string ans)
13007 day (or (nth 3 tl) (nth 3 defdecode))
13008 month (or (nth 4 tl)
13009 (if (and org-read-date-prefer-future
13010 (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
13011 (prog1 (1+ (nth 4 defdecode)) (setq futurep t))
13012 (nth 4 defdecode)))
13013 year (or (nth 5 tl)
13014 (if (and org-read-date-prefer-future
13015 (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
13016 (prog1 (1+ (nth 5 defdecode)) (setq futurep t))
13017 (nth 5 defdecode)))
13018 hour (or (nth 2 tl) (nth 2 defdecode))
13019 minute (or (nth 1 tl) (nth 1 defdecode))
13020 second (or (nth 0 tl) 0)
13021 wday (nth 6 tl))
13023 (when (and (eq org-read-date-prefer-future 'time)
13024 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
13025 (equal day (nth 3 defdecode))
13026 (equal month (nth 4 defdecode))
13027 (equal year (nth 5 defdecode))
13028 (nth 2 tl)
13029 (or (< (nth 2 tl) (nth 2 defdecode))
13030 (and (= (nth 2 tl) (nth 2 defdecode))
13031 (nth 1 tl)
13032 (< (nth 1 tl) (nth 1 defdecode)))))
13033 (setq day (1+ day)
13034 futurep t))
13036 ;; Special date definitions below
13037 (cond
13038 (iso-week
13039 ;; There was an iso week
13040 (setq futurep nil)
13041 (setq year (or iso-year year)
13042 day (or iso-weekday wday 1)
13043 wday nil ; to make sure that the trigger below does not match
13044 iso-date (calendar-gregorian-from-absolute
13045 (calendar-absolute-from-iso
13046 (list iso-week day year))))
13047 ; FIXME: Should we also push ISO weeks into the future?
13048 ; (when (and org-read-date-prefer-future
13049 ; (not iso-year)
13050 ; (< (calendar-absolute-from-gregorian iso-date)
13051 ; (time-to-days (current-time))))
13052 ; (setq year (1+ year)
13053 ; iso-date (calendar-gregorian-from-absolute
13054 ; (calendar-absolute-from-iso
13055 ; (list iso-week day year)))))
13056 (setq month (car iso-date)
13057 year (nth 2 iso-date)
13058 day (nth 1 iso-date)))
13059 (deltan
13060 (setq futurep nil)
13061 (unless deltadef
13062 (let ((now (decode-time (current-time))))
13063 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
13064 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
13065 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
13066 ((equal deltaw "m") (setq month (+ month deltan)))
13067 ((equal deltaw "y") (setq year (+ year deltan)))))
13068 ((and wday (not (nth 3 tl)))
13069 (setq futurep nil)
13070 ;; Weekday was given, but no day, so pick that day in the week
13071 ;; on or after the derived date.
13072 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
13073 (unless (equal wday wday1)
13074 (setq day (+ day (% (- wday wday1 -7) 7))))))
13075 (if (and (boundp 'org-time-was-given)
13076 (nth 2 tl))
13077 (setq org-time-was-given t))
13078 (if (< year 100) (setq year (+ 2000 year)))
13079 (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
13080 (setq org-read-date-analyze-futurep futurep)
13081 (list second minute hour day month year)))
13083 (defvar parse-time-weekdays)
13085 (defun org-read-date-get-relative (s today default)
13086 "Check string S for special relative date string.
13087 TODAY and DEFAULT are internal times, for today and for a default.
13088 Return shift list (N what def-flag)
13089 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
13090 N is the number of WHATs to shift.
13091 DEF-FLAG is t when a double ++ or -- indicates shift relative to
13092 the DEFAULT date rather than TODAY."
13093 (when (and
13094 (string-match
13095 (concat
13096 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
13097 "\\([0-9]+\\)?"
13098 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
13099 "\\([ \t]\\|$\\)") s)
13100 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
13101 (let* ((dir (if (> (match-end 1) (match-beginning 1))
13102 (string-to-char (substring (match-string 1 s) -1))
13103 ?+))
13104 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
13105 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
13106 (what (if (match-end 3) (match-string 3 s) "d"))
13107 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
13108 (date (if rel default today))
13109 (wday (nth 6 (decode-time date)))
13110 delta)
13111 (if wday1
13112 (progn
13113 (setq delta (mod (+ 7 (- wday1 wday)) 7))
13114 (if (= dir ?-) (setq delta (- delta 7)))
13115 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
13116 (list delta "d" rel))
13117 (list (* n (if (= dir ?-) -1 1)) what rel)))))
13119 (defun org-eval-in-calendar (form &optional keepdate)
13120 "Eval FORM in the calendar window and return to current window.
13121 Also, store the cursor date in variable org-ans2."
13122 (let ((sf (selected-frame))
13123 (sw (selected-window)))
13124 (select-window (get-buffer-window "*Calendar*" t))
13125 (eval form)
13126 (when (and (not keepdate) (calendar-cursor-to-date))
13127 (let* ((date (calendar-cursor-to-date))
13128 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13129 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
13130 (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
13131 (select-window sw)
13132 (org-select-frame-set-input-focus sf)))
13134 (defun org-calendar-select ()
13135 "Return to `org-read-date' with the date currently selected.
13136 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
13137 (interactive)
13138 (when (calendar-cursor-to-date)
13139 (let* ((date (calendar-cursor-to-date))
13140 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13141 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
13142 (if (active-minibuffer-window) (exit-minibuffer))))
13144 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
13145 "Insert a date stamp for the date given by the internal TIME.
13146 WITH-HM means, use the stamp format that includes the time of the day.
13147 INACTIVE means use square brackets instead of angular ones, so that the
13148 stamp will not contribute to the agenda.
13149 PRE and POST are optional strings to be inserted before and after the
13150 stamp.
13151 The command returns the inserted time stamp."
13152 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
13153 stamp)
13154 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
13155 (insert-before-markers (or pre ""))
13156 (insert-before-markers (setq stamp (format-time-string fmt time)))
13157 (when (listp extra)
13158 (setq extra (car extra))
13159 (if (and (stringp extra)
13160 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
13161 (setq extra (format "-%02d:%02d"
13162 (string-to-number (match-string 1 extra))
13163 (string-to-number (match-string 2 extra))))
13164 (setq extra nil)))
13165 (when extra
13166 (backward-char 1)
13167 (insert-before-markers extra)
13168 (forward-char 1))
13169 (insert-before-markers (or post ""))
13170 (setq org-last-inserted-timestamp stamp)))
13172 (defun org-toggle-time-stamp-overlays ()
13173 "Toggle the use of custom time stamp formats."
13174 (interactive)
13175 (setq org-display-custom-times (not org-display-custom-times))
13176 (unless org-display-custom-times
13177 (let ((p (point-min)) (bmp (buffer-modified-p)))
13178 (while (setq p (next-single-property-change p 'display))
13179 (if (and (get-text-property p 'display)
13180 (eq (get-text-property p 'face) 'org-date))
13181 (remove-text-properties
13182 p (setq p (next-single-property-change p 'display))
13183 '(display t))))
13184 (set-buffer-modified-p bmp)))
13185 (if (featurep 'xemacs)
13186 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
13187 (org-restart-font-lock)
13188 (setq org-table-may-need-update t)
13189 (if org-display-custom-times
13190 (message "Time stamps are overlayed with custom format")
13191 (message "Time stamp overlays removed")))
13193 (defun org-display-custom-time (beg end)
13194 "Overlay modified time stamp format over timestamp between BEG and END."
13195 (let* ((ts (buffer-substring beg end))
13196 t1 w1 with-hm tf time str w2 (off 0))
13197 (save-match-data
13198 (setq t1 (org-parse-time-string ts t))
13199 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
13200 (setq off (- (match-end 0) (match-beginning 0)))))
13201 (setq end (- end off))
13202 (setq w1 (- end beg)
13203 with-hm (and (nth 1 t1) (nth 2 t1))
13204 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
13205 time (org-fix-decoded-time t1)
13206 str (org-add-props
13207 (format-time-string
13208 (substring tf 1 -1) (apply 'encode-time time))
13209 nil 'mouse-face 'highlight)
13210 w2 (length str))
13211 (if (not (= w2 w1))
13212 (add-text-properties (1+ beg) (+ 2 beg)
13213 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
13214 (if (featurep 'xemacs)
13215 (progn
13216 (put-text-property beg end 'invisible t)
13217 (put-text-property beg end 'end-glyph (make-glyph str)))
13218 (put-text-property beg end 'display str))))
13220 (defun org-translate-time (string)
13221 "Translate all timestamps in STRING to custom format.
13222 But do this only if the variable `org-display-custom-times' is set."
13223 (when org-display-custom-times
13224 (save-match-data
13225 (let* ((start 0)
13226 (re org-ts-regexp-both)
13227 t1 with-hm inactive tf time str beg end)
13228 (while (setq start (string-match re string start))
13229 (setq beg (match-beginning 0)
13230 end (match-end 0)
13231 t1 (save-match-data
13232 (org-parse-time-string (substring string beg end) t))
13233 with-hm (and (nth 1 t1) (nth 2 t1))
13234 inactive (equal (substring string beg (1+ beg)) "[")
13235 tf (funcall (if with-hm 'cdr 'car)
13236 org-time-stamp-custom-formats)
13237 time (org-fix-decoded-time t1)
13238 str (format-time-string
13239 (concat
13240 (if inactive "[" "<") (substring tf 1 -1)
13241 (if inactive "]" ">"))
13242 (apply 'encode-time time))
13243 string (replace-match str t t string)
13244 start (+ start (length str)))))))
13245 string)
13247 (defun org-fix-decoded-time (time)
13248 "Set 0 instead of nil for the first 6 elements of time.
13249 Don't touch the rest."
13250 (let ((n 0))
13251 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
13253 (defun org-days-to-time (timestamp-string)
13254 "Difference between TIMESTAMP-STRING and now in days."
13255 (- (time-to-days (org-time-string-to-time timestamp-string))
13256 (time-to-days (current-time))))
13258 (defun org-deadline-close (timestamp-string &optional ndays)
13259 "Is the time in TIMESTAMP-STRING close to the current date?"
13260 (setq ndays (or ndays (org-get-wdays timestamp-string)))
13261 (and (< (org-days-to-time timestamp-string) ndays)
13262 (not (org-entry-is-done-p))))
13264 (defun org-get-wdays (ts)
13265 "Get the deadline lead time appropriate for timestring TS."
13266 (cond
13267 ((<= org-deadline-warning-days 0)
13268 ;; 0 or negative, enforce this value no matter what
13269 (- org-deadline-warning-days))
13270 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
13271 ;; lead time is specified.
13272 (floor (* (string-to-number (match-string 1 ts))
13273 (cdr (assoc (match-string 2 ts)
13274 '(("d" . 1) ("w" . 7)
13275 ("m" . 30.4) ("y" . 365.25)))))))
13276 ;; go for the default.
13277 (t org-deadline-warning-days)))
13279 (defun org-calendar-select-mouse (ev)
13280 "Return to `org-read-date' with the date currently selected.
13281 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
13282 (interactive "e")
13283 (mouse-set-point ev)
13284 (when (calendar-cursor-to-date)
13285 (let* ((date (calendar-cursor-to-date))
13286 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13287 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
13288 (if (active-minibuffer-window) (exit-minibuffer))))
13290 (defun org-check-deadlines (ndays)
13291 "Check if there are any deadlines due or past due.
13292 A deadline is considered due if it happens within `org-deadline-warning-days'
13293 days from today's date. If the deadline appears in an entry marked DONE,
13294 it is not shown. The prefix arg NDAYS can be used to test that many
13295 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
13296 (interactive "P")
13297 (let* ((org-warn-days
13298 (cond
13299 ((equal ndays '(4)) 100000)
13300 (ndays (prefix-numeric-value ndays))
13301 (t (abs org-deadline-warning-days))))
13302 (case-fold-search nil)
13303 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
13304 (callback
13305 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
13307 (message "%d deadlines past-due or due within %d days"
13308 (org-occur regexp nil callback)
13309 org-warn-days)))
13311 (defun org-check-before-date (date)
13312 "Check if there are deadlines or scheduled entries before DATE."
13313 (interactive (list (org-read-date)))
13314 (let ((case-fold-search nil)
13315 (regexp (concat "\\<\\(" org-deadline-string
13316 "\\|" org-scheduled-string
13317 "\\) *<\\([^>]+\\)>"))
13318 (callback
13319 (lambda () (time-less-p
13320 (org-time-string-to-time (match-string 2))
13321 (org-time-string-to-time date)))))
13322 (message "%d entries before %s"
13323 (org-occur regexp nil callback) date)))
13325 (defun org-check-after-date (date)
13326 "Check if there are deadlines or scheduled entries after DATE."
13327 (interactive (list (org-read-date)))
13328 (let ((case-fold-search nil)
13329 (regexp (concat "\\<\\(" org-deadline-string
13330 "\\|" org-scheduled-string
13331 "\\) *<\\([^>]+\\)>"))
13332 (callback
13333 (lambda () (not
13334 (time-less-p
13335 (org-time-string-to-time (match-string 2))
13336 (org-time-string-to-time date))))))
13337 (message "%d entries after %s"
13338 (org-occur regexp nil callback) date)))
13340 (defun org-evaluate-time-range (&optional to-buffer)
13341 "Evaluate a time range by computing the difference between start and end.
13342 Normally the result is just printed in the echo area, but with prefix arg
13343 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
13344 If the time range is actually in a table, the result is inserted into the
13345 next column.
13346 For time difference computation, a year is assumed to be exactly 365
13347 days in order to avoid rounding problems."
13348 (interactive "P")
13350 (org-clock-update-time-maybe)
13351 (save-excursion
13352 (unless (org-at-date-range-p t)
13353 (goto-char (point-at-bol))
13354 (re-search-forward org-tr-regexp-both (point-at-eol) t))
13355 (if (not (org-at-date-range-p t))
13356 (error "Not at a time-stamp range, and none found in current line")))
13357 (let* ((ts1 (match-string 1))
13358 (ts2 (match-string 2))
13359 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
13360 (match-end (match-end 0))
13361 (time1 (org-time-string-to-time ts1))
13362 (time2 (org-time-string-to-time ts2))
13363 (t1 (org-float-time time1))
13364 (t2 (org-float-time time2))
13365 (diff (abs (- t2 t1)))
13366 (negative (< (- t2 t1) 0))
13367 ;; (ys (floor (* 365 24 60 60)))
13368 (ds (* 24 60 60))
13369 (hs (* 60 60))
13370 (fy "%dy %dd %02d:%02d")
13371 (fy1 "%dy %dd")
13372 (fd "%dd %02d:%02d")
13373 (fd1 "%dd")
13374 (fh "%02d:%02d")
13375 y d h m align)
13376 (if havetime
13377 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
13379 d (floor (/ diff ds)) diff (mod diff ds)
13380 h (floor (/ diff hs)) diff (mod diff hs)
13381 m (floor (/ diff 60)))
13382 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
13384 d (floor (+ (/ diff ds) 0.5))
13385 h 0 m 0))
13386 (if (not to-buffer)
13387 (message "%s" (org-make-tdiff-string y d h m))
13388 (if (org-at-table-p)
13389 (progn
13390 (goto-char match-end)
13391 (setq align t)
13392 (and (looking-at " *|") (goto-char (match-end 0))))
13393 (goto-char match-end))
13394 (if (looking-at
13395 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
13396 (replace-match ""))
13397 (if negative (insert " -"))
13398 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
13399 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
13400 (insert " " (format fh h m))))
13401 (if align (org-table-align))
13402 (message "Time difference inserted")))))
13404 (defun org-make-tdiff-string (y d h m)
13405 (let ((fmt "")
13406 (l nil))
13407 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
13408 l (push y l)))
13409 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
13410 l (push d l)))
13411 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
13412 l (push h l)))
13413 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
13414 l (push m l)))
13415 (apply 'format fmt (nreverse l))))
13417 (defun org-time-string-to-time (s)
13418 (apply 'encode-time (org-parse-time-string s)))
13419 (defun org-time-string-to-seconds (s)
13420 (org-float-time (org-time-string-to-time s)))
13422 (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
13423 "Convert a time stamp to an absolute day number.
13424 If there is a specifyer for a cyclic time stamp, get the closest date to
13425 DAYNR.
13426 PREFER and SHOW-ALL are passed through to `org-closest-date'.
13427 the variable date is bound by the calendar when this is called."
13428 (cond
13429 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
13430 (if (org-diary-sexp-entry (match-string 1 s) "" date)
13431 daynr
13432 (+ daynr 1000)))
13433 ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
13434 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
13435 (time-to-days (current-time))) (match-string 0 s)
13436 prefer show-all))
13437 (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
13439 (defun org-days-to-iso-week (days)
13440 "Return the iso week number."
13441 (require 'cal-iso)
13442 (car (calendar-iso-from-absolute days)))
13444 (defun org-small-year-to-year (year)
13445 "Convert 2-digit years into 4-digit years.
13446 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
13447 The year 2000 cannot be abbreviated. Any year larger than 99
13448 is returned unchanged."
13449 (if (< year 38)
13450 (setq year (+ 2000 year))
13451 (if (< year 100)
13452 (setq year (+ 1900 year))))
13453 year)
13455 (defun org-time-from-absolute (d)
13456 "Return the time corresponding to date D.
13457 D may be an absolute day number, or a calendar-type list (month day year)."
13458 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
13459 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
13461 (defun org-calendar-holiday ()
13462 "List of holidays, for Diary display in Org-mode."
13463 (require 'holidays)
13464 (let ((hl (funcall
13465 (if (fboundp 'calendar-check-holidays)
13466 'calendar-check-holidays 'check-calendar-holidays) date)))
13467 (if hl (mapconcat 'identity hl "; "))))
13469 (defun org-diary-sexp-entry (sexp entry date)
13470 "Process a SEXP diary ENTRY for DATE."
13471 (require 'diary-lib)
13472 (let ((result (if calendar-debug-sexp
13473 (let ((stack-trace-on-error t))
13474 (eval (car (read-from-string sexp))))
13475 (condition-case nil
13476 (eval (car (read-from-string sexp)))
13477 (error
13478 (beep)
13479 (message "Bad sexp at line %d in %s: %s"
13480 (org-current-line)
13481 (buffer-file-name) sexp)
13482 (sleep-for 2))))))
13483 (cond ((stringp result) result)
13484 ((and (consp result)
13485 (stringp (cdr result))) (cdr result))
13486 (result entry)
13487 (t nil))))
13489 (defun org-diary-to-ical-string (frombuf)
13490 "Get iCalendar entries from diary entries in buffer FROMBUF.
13491 This uses the icalendar.el library."
13492 (let* ((tmpdir (if (featurep 'xemacs)
13493 (temp-directory)
13494 temporary-file-directory))
13495 (tmpfile (make-temp-name
13496 (expand-file-name "orgics" tmpdir)))
13497 buf rtn b e)
13498 (save-excursion
13499 (set-buffer frombuf)
13500 (icalendar-export-region (point-min) (point-max) tmpfile)
13501 (setq buf (find-buffer-visiting tmpfile))
13502 (set-buffer buf)
13503 (goto-char (point-min))
13504 (if (re-search-forward "^BEGIN:VEVENT" nil t)
13505 (setq b (match-beginning 0)))
13506 (goto-char (point-max))
13507 (if (re-search-backward "^END:VEVENT" nil t)
13508 (setq e (match-end 0)))
13509 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
13510 (kill-buffer buf)
13511 (delete-file tmpfile)
13512 rtn))
13514 (defun org-closest-date (start current change prefer show-all)
13515 "Find the date closest to CURRENT that is consistent with START and CHANGE.
13516 When PREFER is `past' return a date that is either CURRENT or past.
13517 When PREFER is `future', return a date that is either CURRENT or future.
13518 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
13519 ;; Make the proper lists from the dates
13520 (catch 'exit
13521 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
13522 dn dw sday cday n1 n2 n0
13523 d m y y1 y2 date1 date2 nmonths nm ny m2)
13525 (setq start (org-date-to-gregorian start)
13526 current (org-date-to-gregorian
13527 (if show-all
13528 current
13529 (time-to-days (current-time))))
13530 sday (calendar-absolute-from-gregorian start)
13531 cday (calendar-absolute-from-gregorian current))
13533 (if (<= cday sday) (throw 'exit sday))
13535 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
13536 (setq dn (string-to-number (match-string 1 change))
13537 dw (cdr (assoc (match-string 2 change) a1)))
13538 (error "Invalid change specifyer: %s" change))
13539 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
13540 (cond
13541 ((eq dw 'day)
13542 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
13543 n2 (+ n1 dn)))
13544 ((eq dw 'year)
13545 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
13546 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
13547 (setq date1 (list m d y1)
13548 n1 (calendar-absolute-from-gregorian date1)
13549 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
13550 n2 (calendar-absolute-from-gregorian date2)))
13551 ((eq dw 'month)
13552 ;; approx number of month between the two dates
13553 (setq nmonths (floor (/ (- cday sday) 30.436875)))
13554 ;; How often does dn fit in there?
13555 (setq d (nth 1 start) m (car start) y (nth 2 start)
13556 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
13557 m (+ m nm)
13558 ny (floor (/ m 12))
13559 y (+ y ny)
13560 m (- m (* ny 12)))
13561 (while (> m 12) (setq m (- m 12) y (1+ y)))
13562 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
13563 (setq m2 (+ m dn) y2 y)
13564 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
13565 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
13566 (while (<= n2 cday)
13567 (setq n1 n2 m m2 y y2)
13568 (setq m2 (+ m dn) y2 y)
13569 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
13570 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
13571 ;; Make sure n1 is the earlier date
13572 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
13573 (if show-all
13574 (cond
13575 ((eq prefer 'past) (if (= cday n2) n2 n1))
13576 ((eq prefer 'future) (if (= cday n1) n1 n2))
13577 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
13578 (cond
13579 ((eq prefer 'past) (if (= cday n2) n2 n1))
13580 ((eq prefer 'future) (if (= cday n1) n1 n2))
13581 (t (if (= cday n1) n1 n2)))))))
13583 (defun org-date-to-gregorian (date)
13584 "Turn any specification of DATE into a gregorian date for the calendar."
13585 (cond ((integerp date) (calendar-gregorian-from-absolute date))
13586 ((and (listp date) (= (length date) 3)) date)
13587 ((stringp date)
13588 (setq date (org-parse-time-string date))
13589 (list (nth 4 date) (nth 3 date) (nth 5 date)))
13590 ((listp date)
13591 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
13593 (defun org-parse-time-string (s &optional nodefault)
13594 "Parse the standard Org-mode time string.
13595 This should be a lot faster than the normal `parse-time-string'.
13596 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
13597 hour and minute fields will be nil if not given."
13598 (if (string-match org-ts-regexp0 s)
13599 (list 0
13600 (if (or (match-beginning 8) (not nodefault))
13601 (string-to-number (or (match-string 8 s) "0")))
13602 (if (or (match-beginning 7) (not nodefault))
13603 (string-to-number (or (match-string 7 s) "0")))
13604 (string-to-number (match-string 4 s))
13605 (string-to-number (match-string 3 s))
13606 (string-to-number (match-string 2 s))
13607 nil nil nil)
13608 (error "Not a standard Org-mode time string: %s" s)))
13610 (defun org-timestamp-up (&optional arg)
13611 "Increase the date item at the cursor by one.
13612 If the cursor is on the year, change the year. If it is on the month or
13613 the day, change that.
13614 With prefix ARG, change by that many units."
13615 (interactive "p")
13616 (org-timestamp-change (prefix-numeric-value arg)))
13618 (defun org-timestamp-down (&optional arg)
13619 "Decrease the date item at the cursor by one.
13620 If the cursor is on the year, change the year. If it is on the month or
13621 the day, change that.
13622 With prefix ARG, change by that many units."
13623 (interactive "p")
13624 (org-timestamp-change (- (prefix-numeric-value arg))))
13626 (defun org-timestamp-up-day (&optional arg)
13627 "Increase the date in the time stamp by one day.
13628 With prefix ARG, change that many days."
13629 (interactive "p")
13630 (if (and (not (org-at-timestamp-p t))
13631 (org-on-heading-p))
13632 (org-todo 'up)
13633 (org-timestamp-change (prefix-numeric-value arg) 'day)))
13635 (defun org-timestamp-down-day (&optional arg)
13636 "Decrease the date in the time stamp by one day.
13637 With prefix ARG, change that many days."
13638 (interactive "p")
13639 (if (and (not (org-at-timestamp-p t))
13640 (org-on-heading-p))
13641 (org-todo 'down)
13642 (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
13644 (defun org-at-timestamp-p (&optional inactive-ok)
13645 "Determine if the cursor is in or at a timestamp."
13646 (interactive)
13647 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
13648 (pos (point))
13649 (ans (or (looking-at tsr)
13650 (save-excursion
13651 (skip-chars-backward "^[<\n\r\t")
13652 (if (> (point) (point-min)) (backward-char 1))
13653 (and (looking-at tsr)
13654 (> (- (match-end 0) pos) -1))))))
13655 (and ans
13656 (boundp 'org-ts-what)
13657 (setq org-ts-what
13658 (cond
13659 ((= pos (match-beginning 0)) 'bracket)
13660 ((= pos (1- (match-end 0))) 'bracket)
13661 ((org-pos-in-match-range pos 2) 'year)
13662 ((org-pos-in-match-range pos 3) 'month)
13663 ((org-pos-in-match-range pos 7) 'hour)
13664 ((org-pos-in-match-range pos 8) 'minute)
13665 ((or (org-pos-in-match-range pos 4)
13666 (org-pos-in-match-range pos 5)) 'day)
13667 ((and (> pos (or (match-end 8) (match-end 5)))
13668 (< pos (match-end 0)))
13669 (- pos (or (match-end 8) (match-end 5))))
13670 (t 'day))))
13671 ans))
13673 (defun org-toggle-timestamp-type ()
13674 "Toggle the type (<active> or [inactive]) of a time stamp."
13675 (interactive)
13676 (when (org-at-timestamp-p t)
13677 (let ((beg (match-beginning 0)) (end (match-end 0))
13678 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
13679 (save-excursion
13680 (goto-char beg)
13681 (while (re-search-forward "[][<>]" end t)
13682 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
13683 t t)))
13684 (message "Timestamp is now %sactive"
13685 (if (equal (char-after beg) ?<) "" "in")))))
13687 (defun org-timestamp-change (n &optional what)
13688 "Change the date in the time stamp at point.
13689 The date will be changed by N times WHAT. WHAT can be `day', `month',
13690 `year', `minute', `second'. If WHAT is not given, the cursor position
13691 in the timestamp determines what will be changed."
13692 (let ((pos (point))
13693 with-hm inactive
13694 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
13695 org-ts-what
13696 extra rem
13697 ts time time0)
13698 (if (not (org-at-timestamp-p t))
13699 (error "Not at a timestamp"))
13700 (if (and (not what) (eq org-ts-what 'bracket))
13701 (org-toggle-timestamp-type)
13702 (if (and (not what) (not (eq org-ts-what 'day))
13703 org-display-custom-times
13704 (get-text-property (point) 'display)
13705 (not (get-text-property (1- (point)) 'display)))
13706 (setq org-ts-what 'day))
13707 (setq org-ts-what (or what org-ts-what)
13708 inactive (= (char-after (match-beginning 0)) ?\[)
13709 ts (match-string 0))
13710 (replace-match "")
13711 (if (string-match
13712 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
13714 (setq extra (match-string 1 ts)))
13715 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
13716 (setq with-hm t))
13717 (setq time0 (org-parse-time-string ts))
13718 (when (and (eq org-ts-what 'minute)
13719 (eq current-prefix-arg nil))
13720 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
13721 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
13722 (setcar (cdr time0) (+ (nth 1 time0)
13723 (if (> n 0) (- rem) (- dm rem))))))
13724 (setq time
13725 (encode-time (or (car time0) 0)
13726 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
13727 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
13728 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
13729 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
13730 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
13731 (nthcdr 6 time0)))
13732 (when (and (member org-ts-what '(hour minute))
13733 extra
13734 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
13735 (setq extra (org-modify-ts-extra
13736 extra
13737 (if (eq org-ts-what 'hour) 2 5)
13738 n dm)))
13739 (when (integerp org-ts-what)
13740 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
13741 (if (eq what 'calendar)
13742 (let ((cal-date (org-get-date-from-calendar)))
13743 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
13744 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
13745 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
13746 (setcar time0 (or (car time0) 0))
13747 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
13748 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
13749 (setq time (apply 'encode-time time0))))
13750 (setq org-last-changed-timestamp
13751 (org-insert-time-stamp time with-hm inactive nil nil extra))
13752 (org-clock-update-time-maybe)
13753 (goto-char pos)
13754 ;; Try to recenter the calendar window, if any
13755 (if (and org-calendar-follow-timestamp-change
13756 (get-buffer-window "*Calendar*" t)
13757 (memq org-ts-what '(day month year)))
13758 (org-recenter-calendar (time-to-days time))))))
13760 (defun org-modify-ts-extra (s pos n dm)
13761 "Change the different parts of the lead-time and repeat fields in timestamp."
13762 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
13763 ng h m new rem)
13764 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
13765 (cond
13766 ((or (org-pos-in-match-range pos 2)
13767 (org-pos-in-match-range pos 3))
13768 (setq m (string-to-number (match-string 3 s))
13769 h (string-to-number (match-string 2 s)))
13770 (if (org-pos-in-match-range pos 2)
13771 (setq h (+ h n))
13772 (setq n (* dm (org-no-warnings (signum n))))
13773 (when (not (= 0 (setq rem (% m dm))))
13774 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
13775 (setq m (+ m n)))
13776 (if (< m 0) (setq m (+ m 60) h (1- h)))
13777 (if (> m 59) (setq m (- m 60) h (1+ h)))
13778 (setq h (min 24 (max 0 h)))
13779 (setq ng 1 new (format "-%02d:%02d" h m)))
13780 ((org-pos-in-match-range pos 6)
13781 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
13782 ((org-pos-in-match-range pos 5)
13783 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
13785 ((org-pos-in-match-range pos 9)
13786 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
13787 ((org-pos-in-match-range pos 8)
13788 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
13790 (when ng
13791 (setq s (concat
13792 (substring s 0 (match-beginning ng))
13794 (substring s (match-end ng))))))
13797 (defun org-recenter-calendar (date)
13798 "If the calendar is visible, recenter it to DATE."
13799 (let* ((win (selected-window))
13800 (cwin (get-buffer-window "*Calendar*" t))
13801 (calendar-move-hook nil))
13802 (when cwin
13803 (select-window cwin)
13804 (calendar-goto-date (if (listp date) date
13805 (calendar-gregorian-from-absolute date)))
13806 (select-window win))))
13808 (defun org-goto-calendar (&optional arg)
13809 "Go to the Emacs calendar at the current date.
13810 If there is a time stamp in the current line, go to that date.
13811 A prefix ARG can be used to force the current date."
13812 (interactive "P")
13813 (let ((tsr org-ts-regexp) diff
13814 (calendar-move-hook nil)
13815 (calendar-view-holidays-initially-flag nil)
13816 (view-calendar-holidays-initially nil)
13817 (calendar-view-diary-initially-flag nil)
13818 (view-diary-entries-initially nil))
13819 (if (or (org-at-timestamp-p)
13820 (save-excursion
13821 (beginning-of-line 1)
13822 (looking-at (concat ".*" tsr))))
13823 (let ((d1 (time-to-days (current-time)))
13824 (d2 (time-to-days
13825 (org-time-string-to-time (match-string 1)))))
13826 (setq diff (- d2 d1))))
13827 (calendar)
13828 (calendar-goto-today)
13829 (if (and diff (not arg)) (calendar-forward-day diff))))
13831 (defun org-get-date-from-calendar ()
13832 "Return a list (month day year) of date at point in calendar."
13833 (with-current-buffer "*Calendar*"
13834 (save-match-data
13835 (calendar-cursor-to-date))))
13837 (defun org-date-from-calendar ()
13838 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
13839 If there is already a time stamp at the cursor position, update it."
13840 (interactive)
13841 (if (org-at-timestamp-p t)
13842 (org-timestamp-change 0 'calendar)
13843 (let ((cal-date (org-get-date-from-calendar)))
13844 (org-insert-time-stamp
13845 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
13847 (defun org-minutes-to-hh:mm-string (m)
13848 "Compute H:MM from a number of minutes."
13849 (let ((h (/ m 60)))
13850 (setq m (- m (* 60 h)))
13851 (format org-time-clocksum-format h m)))
13853 (defun org-hh:mm-string-to-minutes (s)
13854 "Convert a string H:MM to a number of minutes.
13855 If the string is just a number, interprete it as minutes.
13856 In fact, the first hh:mm or number in the string will be taken,
13857 there can be extra stuff in the string.
13858 If no number is found, the return value is 0."
13859 (cond
13860 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
13861 (+ (* (string-to-number (match-string 1 s)) 60)
13862 (string-to-number (match-string 2 s))))
13863 ((string-match "\\([0-9]+\\)" s)
13864 (string-to-number (match-string 1 s)))
13865 (t 0)))
13867 ;;;; Files
13869 (defun org-save-all-org-buffers ()
13870 "Save all Org-mode buffers without user confirmation."
13871 (interactive)
13872 (message "Saving all Org-mode buffers...")
13873 (save-some-buffers t 'org-mode-p)
13874 (when (featurep 'org-id) (org-id-locations-save))
13875 (message "Saving all Org-mode buffers... done"))
13877 (defun org-revert-all-org-buffers ()
13878 "Revert all Org-mode buffers.
13879 Prompt for confirmation when there are unsaved changes.
13880 Be sure you know what you are doing before letting this function
13881 overwrite your changes.
13883 This function is useful in a setup where one tracks org files
13884 with a version control system, to revert on one machine after pulling
13885 changes from another. I believe the procedure must be like this:
13887 1. M-x org-save-all-org-buffers
13888 2. Pull changes from the other machine, resolve conflicts
13889 3. M-x org-revert-all-org-buffers"
13890 (interactive)
13891 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
13892 (error "Abort"))
13893 (save-excursion
13894 (save-window-excursion
13895 (mapc
13896 (lambda (b)
13897 (when (and (with-current-buffer b (org-mode-p))
13898 (with-current-buffer b buffer-file-name))
13899 (switch-to-buffer b)
13900 (revert-buffer t 'no-confirm)))
13901 (buffer-list))
13902 (when (and (featurep 'org-id) org-id-track-globally)
13903 (org-id-locations-load)))))
13905 ;;;; Agenda files
13907 ;;;###autoload
13908 (defun org-iswitchb (&optional arg)
13909 "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
13910 With a prefix argument, restrict available to files.
13911 With two prefix arguments, restrict available buffers to agenda files."
13912 (interactive "P")
13913 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
13914 ((equal arg '(16)) (org-buffer-list 'agenda))
13915 (t (org-buffer-list)))))
13916 (switch-to-buffer
13917 (org-icompleting-read "Org buffer: "
13918 (mapcar 'list (mapcar 'buffer-name blist))
13919 nil t))))
13921 ;;;###autoload
13922 (defalias 'org-ido-switchb 'org-iswitchb)
13924 (defun org-buffer-list (&optional predicate exclude-tmp)
13925 "Return a list of Org buffers.
13926 PREDICATE can be `export', `files' or `agenda'.
13928 export restrict the list to Export buffers.
13929 files restrict the list to buffers visiting Org files.
13930 agenda restrict the list to buffers visiting agenda files.
13932 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
13933 (let* ((bfn nil)
13934 (agenda-files (and (eq predicate 'agenda)
13935 (mapcar 'file-truename (org-agenda-files t))))
13936 (filter
13937 (cond
13938 ((eq predicate 'files)
13939 (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
13940 ((eq predicate 'export)
13941 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
13942 ((eq predicate 'agenda)
13943 (lambda (b)
13944 (with-current-buffer b
13945 (and (eq major-mode 'org-mode)
13946 (setq bfn (buffer-file-name b))
13947 (member (file-truename bfn) agenda-files)))))
13948 (t (lambda (b) (with-current-buffer b
13949 (or (eq major-mode 'org-mode)
13950 (string-match "\*Org .*Export"
13951 (buffer-name b)))))))))
13952 (delq nil
13953 (mapcar
13954 (lambda(b)
13955 (if (and (funcall filter b)
13956 (or (not exclude-tmp)
13957 (not (string-match "tmp" (buffer-name b)))))
13959 nil))
13960 (buffer-list)))))
13962 (defun org-agenda-files (&optional unrestricted archives)
13963 "Get the list of agenda files.
13964 Optional UNRESTRICTED means return the full list even if a restriction
13965 is currently in place.
13966 When ARCHIVES is t, include all archive files hat are really being
13967 used by the agenda files. If ARCHIVE is `ifmode', do this only if
13968 `org-agenda-archives-mode' is t."
13969 (let ((files
13970 (cond
13971 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
13972 ((stringp org-agenda-files) (org-read-agenda-file-list))
13973 ((listp org-agenda-files) org-agenda-files)
13974 (t (error "Invalid value of `org-agenda-files'")))))
13975 (setq files (apply 'append
13976 (mapcar (lambda (f)
13977 (if (file-directory-p f)
13978 (directory-files
13979 f t org-agenda-file-regexp)
13980 (list f)))
13981 files)))
13982 (when org-agenda-skip-unavailable-files
13983 (setq files (delq nil
13984 (mapcar (function
13985 (lambda (file)
13986 (and (file-readable-p file) file)))
13987 files))))
13988 (when (or (eq archives t)
13989 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
13990 (setq files (org-add-archive-files files)))
13991 files))
13993 (defun org-edit-agenda-file-list ()
13994 "Edit the list of agenda files.
13995 Depending on setup, this either uses customize to edit the variable
13996 `org-agenda-files', or it visits the file that is holding the list. In the
13997 latter case, the buffer is set up in a way that saving it automatically kills
13998 the buffer and restores the previous window configuration."
13999 (interactive)
14000 (if (stringp org-agenda-files)
14001 (let ((cw (current-window-configuration)))
14002 (find-file org-agenda-files)
14003 (org-set-local 'org-window-configuration cw)
14004 (org-add-hook 'after-save-hook
14005 (lambda ()
14006 (set-window-configuration
14007 (prog1 org-window-configuration
14008 (kill-buffer (current-buffer))))
14009 (org-install-agenda-files-menu)
14010 (message "New agenda file list installed"))
14011 nil 'local)
14012 (message "%s" (substitute-command-keys
14013 "Edit list and finish with \\[save-buffer]")))
14014 (customize-variable 'org-agenda-files)))
14016 (defun org-store-new-agenda-file-list (list)
14017 "Set new value for the agenda file list and save it correctly."
14018 (if (stringp org-agenda-files)
14019 (let ((f org-agenda-files) b)
14020 (while (setq b (find-buffer-visiting f)) (kill-buffer b))
14021 (with-temp-file f
14022 (insert (mapconcat 'identity list "\n") "\n")))
14023 (let ((org-mode-hook nil) (org-inhibit-startup t)
14024 (org-insert-mode-line-in-empty-file nil))
14025 (setq org-agenda-files list)
14026 (customize-save-variable 'org-agenda-files org-agenda-files))))
14028 (defun org-read-agenda-file-list ()
14029 "Read the list of agenda files from a file."
14030 (when (file-directory-p org-agenda-files)
14031 (error "`org-agenda-files' cannot be a single directory"))
14032 (when (stringp org-agenda-files)
14033 (with-temp-buffer
14034 (insert-file-contents org-agenda-files)
14035 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
14038 ;;;###autoload
14039 (defun org-cycle-agenda-files ()
14040 "Cycle through the files in `org-agenda-files'.
14041 If the current buffer visits an agenda file, find the next one in the list.
14042 If the current buffer does not, find the first agenda file."
14043 (interactive)
14044 (let* ((fs (org-agenda-files t))
14045 (files (append fs (list (car fs))))
14046 (tcf (if buffer-file-name (file-truename buffer-file-name)))
14047 file)
14048 (unless files (error "No agenda files"))
14049 (catch 'exit
14050 (while (setq file (pop files))
14051 (if (equal (file-truename file) tcf)
14052 (when (car files)
14053 (find-file (car files))
14054 (throw 'exit t))))
14055 (find-file (car fs)))
14056 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
14058 (defun org-agenda-file-to-front (&optional to-end)
14059 "Move/add the current file to the top of the agenda file list.
14060 If the file is not present in the list, it is added to the front. If it is
14061 present, it is moved there. With optional argument TO-END, add/move to the
14062 end of the list."
14063 (interactive "P")
14064 (let ((org-agenda-skip-unavailable-files nil)
14065 (file-alist (mapcar (lambda (x)
14066 (cons (file-truename x) x))
14067 (org-agenda-files t)))
14068 (ctf (file-truename buffer-file-name))
14069 x had)
14070 (setq x (assoc ctf file-alist) had x)
14072 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
14073 (if to-end
14074 (setq file-alist (append (delq x file-alist) (list x)))
14075 (setq file-alist (cons x (delq x file-alist))))
14076 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
14077 (org-install-agenda-files-menu)
14078 (message "File %s to %s of agenda file list"
14079 (if had "moved" "added") (if to-end "end" "front"))))
14081 (defun org-remove-file (&optional file)
14082 "Remove current file from the list of files in variable `org-agenda-files'.
14083 These are the files which are being checked for agenda entries.
14084 Optional argument FILE means, use this file instead of the current."
14085 (interactive)
14086 (let* ((org-agenda-skip-unavailable-files nil)
14087 (file (or file buffer-file-name))
14088 (true-file (file-truename file))
14089 (afile (abbreviate-file-name file))
14090 (files (delq nil (mapcar
14091 (lambda (x)
14092 (if (equal true-file
14093 (file-truename x))
14094 nil x))
14095 (org-agenda-files t)))))
14096 (if (not (= (length files) (length (org-agenda-files t))))
14097 (progn
14098 (org-store-new-agenda-file-list files)
14099 (org-install-agenda-files-menu)
14100 (message "Removed file: %s" afile))
14101 (message "File was not in list: %s (not removed)" afile))))
14103 (defun org-file-menu-entry (file)
14104 (vector file (list 'find-file file) t))
14106 (defun org-check-agenda-file (file)
14107 "Make sure FILE exists. If not, ask user what to do."
14108 (when (not (file-exists-p file))
14109 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
14110 (abbreviate-file-name file))
14111 (let ((r (downcase (read-char-exclusive))))
14112 (cond
14113 ((equal r ?r)
14114 (org-remove-file file)
14115 (throw 'nextfile t))
14116 (t (error "Abort"))))))
14118 (defun org-get-agenda-file-buffer (file)
14119 "Get a buffer visiting FILE. If the buffer needs to be created, add
14120 it to the list of buffers which might be released later."
14121 (let ((buf (org-find-base-buffer-visiting file)))
14122 (if buf
14123 buf ; just return it
14124 ;; Make a new buffer and remember it
14125 (setq buf (find-file-noselect file))
14126 (if buf (push buf org-agenda-new-buffers))
14127 buf)))
14129 (defun org-release-buffers (blist)
14130 "Release all buffers in list, asking the user for confirmation when needed.
14131 When a buffer is unmodified, it is just killed. When modified, it is saved
14132 \(if the user agrees) and then killed."
14133 (let (buf file)
14134 (while (setq buf (pop blist))
14135 (setq file (buffer-file-name buf))
14136 (when (and (buffer-modified-p buf)
14137 file
14138 (y-or-n-p (format "Save file %s? " file)))
14139 (with-current-buffer buf (save-buffer)))
14140 (kill-buffer buf))))
14142 (defun org-prepare-agenda-buffers (files)
14143 "Create buffers for all agenda files, protect archived trees and comments."
14144 (interactive)
14145 (let ((pa '(:org-archived t))
14146 (pc '(:org-comment t))
14147 (pall '(:org-archived t :org-comment t))
14148 (inhibit-read-only t)
14149 (rea (concat ":" org-archive-tag ":"))
14150 bmp file re)
14151 (save-excursion
14152 (save-restriction
14153 (while (setq file (pop files))
14154 (catch 'nextfile
14155 (if (bufferp file)
14156 (set-buffer file)
14157 (org-check-agenda-file file)
14158 (set-buffer (org-get-agenda-file-buffer file)))
14159 (widen)
14160 (setq bmp (buffer-modified-p))
14161 (org-refresh-category-properties)
14162 (setq org-todo-keywords-for-agenda
14163 (append org-todo-keywords-for-agenda org-todo-keywords-1))
14164 (setq org-done-keywords-for-agenda
14165 (append org-done-keywords-for-agenda org-done-keywords))
14166 (setq org-todo-keyword-alist-for-agenda
14167 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
14168 (setq org-drawers-for-agenda
14169 (append org-drawers-for-agenda org-drawers))
14170 (setq org-tag-alist-for-agenda
14171 (append org-tag-alist-for-agenda org-tag-alist))
14173 (save-excursion
14174 (remove-text-properties (point-min) (point-max) pall)
14175 (when org-agenda-skip-archived-trees
14176 (goto-char (point-min))
14177 (while (re-search-forward rea nil t)
14178 (if (org-on-heading-p t)
14179 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
14180 (goto-char (point-min))
14181 (setq re (concat "^\\*+ +" org-comment-string "\\>"))
14182 (while (re-search-forward re nil t)
14183 (add-text-properties
14184 (match-beginning 0) (org-end-of-subtree t) pc)))
14185 (set-buffer-modified-p bmp)))))
14186 (setq org-todo-keyword-alist-for-agenda
14187 (org-uniquify org-todo-keyword-alist-for-agenda)
14188 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
14190 ;;;; Embedded LaTeX
14192 (defvar org-cdlatex-mode-map (make-sparse-keymap)
14193 "Keymap for the minor `org-cdlatex-mode'.")
14195 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
14196 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
14197 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
14198 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
14199 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
14201 (defvar org-cdlatex-texmathp-advice-is-done nil
14202 "Flag remembering if we have applied the advice to texmathp already.")
14204 (define-minor-mode org-cdlatex-mode
14205 "Toggle the minor `org-cdlatex-mode'.
14206 This mode supports entering LaTeX environment and math in LaTeX fragments
14207 in Org-mode.
14208 \\{org-cdlatex-mode-map}"
14209 nil " OCDL" nil
14210 (when org-cdlatex-mode (require 'cdlatex))
14211 (unless org-cdlatex-texmathp-advice-is-done
14212 (setq org-cdlatex-texmathp-advice-is-done t)
14213 (defadvice texmathp (around org-math-always-on activate)
14214 "Always return t in org-mode buffers.
14215 This is because we want to insert math symbols without dollars even outside
14216 the LaTeX math segments. If Orgmode thinks that point is actually inside
14217 an embedded LaTeX fragment, let texmathp do its job.
14218 \\[org-cdlatex-mode-map]"
14219 (interactive)
14220 (let (p)
14221 (cond
14222 ((not (org-mode-p)) ad-do-it)
14223 ((eq this-command 'cdlatex-math-symbol)
14224 (setq ad-return-value t
14225 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
14227 (let ((p (org-inside-LaTeX-fragment-p)))
14228 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
14229 (setq ad-return-value t
14230 texmathp-why '("Org-mode embedded math" . 0))
14231 (if p ad-do-it)))))))))
14233 (defun turn-on-org-cdlatex ()
14234 "Unconditionally turn on `org-cdlatex-mode'."
14235 (org-cdlatex-mode 1))
14237 (defun org-inside-LaTeX-fragment-p ()
14238 "Test if point is inside a LaTeX fragment.
14239 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
14240 sequence appearing also before point.
14241 Even though the matchers for math are configurable, this function assumes
14242 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
14243 delimiters are skipped when they have been removed by customization.
14244 The return value is nil, or a cons cell with the delimiter and
14245 and the position of this delimiter.
14247 This function does a reasonably good job, but can locally be fooled by
14248 for example currency specifications. For example it will assume being in
14249 inline math after \"$22.34\". The LaTeX fragment formatter will only format
14250 fragments that are properly closed, but during editing, we have to live
14251 with the uncertainty caused by missing closing delimiters. This function
14252 looks only before point, not after."
14253 (catch 'exit
14254 (let ((pos (point))
14255 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
14256 (lim (progn
14257 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
14258 (point)))
14259 dd-on str (start 0) m re)
14260 (goto-char pos)
14261 (when dodollar
14262 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
14263 re (nth 1 (assoc "$" org-latex-regexps)))
14264 (while (string-match re str start)
14265 (cond
14266 ((= (match-end 0) (length str))
14267 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
14268 ((= (match-end 0) (- (length str) 5))
14269 (throw 'exit nil))
14270 (t (setq start (match-end 0))))))
14271 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
14272 (goto-char pos)
14273 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
14274 (and (match-beginning 2) (throw 'exit nil))
14275 ;; count $$
14276 (while (re-search-backward "\\$\\$" lim t)
14277 (setq dd-on (not dd-on)))
14278 (goto-char pos)
14279 (if dd-on (cons "$$" m))))))
14282 (defun org-try-cdlatex-tab ()
14283 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
14284 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
14285 - inside a LaTeX fragment, or
14286 - after the first word in a line, where an abbreviation expansion could
14287 insert a LaTeX environment."
14288 (when org-cdlatex-mode
14289 (cond
14290 ((save-excursion
14291 (skip-chars-backward "a-zA-Z0-9*")
14292 (skip-chars-backward " \t")
14293 (bolp))
14294 (cdlatex-tab) t)
14295 ((org-inside-LaTeX-fragment-p)
14296 (cdlatex-tab) t)
14297 (t nil))))
14299 (defun org-cdlatex-underscore-caret (&optional arg)
14300 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
14301 Revert to the normal definition outside of these fragments."
14302 (interactive "P")
14303 (if (org-inside-LaTeX-fragment-p)
14304 (call-interactively 'cdlatex-sub-superscript)
14305 (let (org-cdlatex-mode)
14306 (call-interactively (key-binding (vector last-input-event))))))
14308 (defun org-cdlatex-math-modify (&optional arg)
14309 "Execute `cdlatex-math-modify' in LaTeX fragments.
14310 Revert to the normal definition outside of these fragments."
14311 (interactive "P")
14312 (if (org-inside-LaTeX-fragment-p)
14313 (call-interactively 'cdlatex-math-modify)
14314 (let (org-cdlatex-mode)
14315 (call-interactively (key-binding (vector last-input-event))))))
14317 (defvar org-latex-fragment-image-overlays nil
14318 "List of overlays carrying the images of latex fragments.")
14319 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
14321 (defun org-remove-latex-fragment-image-overlays ()
14322 "Remove all overlays with LaTeX fragment images in current buffer."
14323 (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
14324 (setq org-latex-fragment-image-overlays nil))
14326 (defun org-preview-latex-fragment (&optional subtree)
14327 "Preview the LaTeX fragment at point, or all locally or globally.
14328 If the cursor is in a LaTeX fragment, create the image and overlay
14329 it over the source code. If there is no fragment at point, display
14330 all fragments in the current text, from one headline to the next. With
14331 prefix SUBTREE, display all fragments in the current subtree. With a
14332 double prefix `C-u C-u', or when the cursor is before the first headline,
14333 display all fragments in the buffer.
14334 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
14335 (interactive "P")
14336 (org-remove-latex-fragment-image-overlays)
14337 (save-excursion
14338 (save-restriction
14339 (let (beg end at msg)
14340 (cond
14341 ((or (equal subtree '(16))
14342 (not (save-excursion
14343 (re-search-backward (concat "^" outline-regexp) nil t))))
14344 (setq beg (point-min) end (point-max)
14345 msg "Creating images for buffer...%s"))
14346 ((equal subtree '(4))
14347 (org-back-to-heading)
14348 (setq beg (point) end (org-end-of-subtree t)
14349 msg "Creating images for subtree...%s"))
14351 (if (setq at (org-inside-LaTeX-fragment-p))
14352 (goto-char (max (point-min) (- (cdr at) 2)))
14353 (org-back-to-heading))
14354 (setq beg (point) end (progn (outline-next-heading) (point))
14355 msg (if at "Creating image...%s"
14356 "Creating images for entry...%s"))))
14357 (message msg "")
14358 (narrow-to-region beg end)
14359 (goto-char beg)
14360 (org-format-latex
14361 (concat "ltxpng/" (file-name-sans-extension
14362 (file-name-nondirectory
14363 buffer-file-name)))
14364 default-directory 'overlays msg at 'forbuffer)
14365 (message msg "done. Use `C-c C-c' to remove images.")))))
14367 (defvar org-latex-regexps
14368 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
14369 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
14370 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
14371 ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
14372 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
14373 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
14374 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
14375 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
14376 "Regular expressions for matching embedded LaTeX.")
14378 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
14379 "Replace LaTeX fragments with links to an image, and produce images.
14380 Some of the options can be changed using the variable
14381 `org-format-latex-options'."
14382 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
14383 (let* ((prefixnodir (file-name-nondirectory prefix))
14384 (absprefix (expand-file-name prefix dir))
14385 (todir (file-name-directory absprefix))
14386 (opt org-format-latex-options)
14387 (matchers (plist-get opt :matchers))
14388 (re-list org-latex-regexps)
14389 (cnt 0) txt link beg end re e checkdir
14390 executables-checked
14391 m n block linkfile movefile ov)
14392 ;; Check if there are old images files with this prefix, and remove them
14393 (when (file-directory-p todir)
14394 (mapc 'delete-file
14395 (directory-files
14396 todir 'full
14397 (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
14398 ;; Check the different regular expressions
14399 (while (setq e (pop re-list))
14400 (setq m (car e) re (nth 1 e) n (nth 2 e)
14401 block (if (nth 3 e) "\n\n" ""))
14402 (when (member m matchers)
14403 (goto-char (point-min))
14404 (while (re-search-forward re nil t)
14405 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
14406 (not (get-text-property (match-beginning n)
14407 'org-protected))
14408 (or (not overlays)
14409 (not (eq (get-char-property (match-beginning n)
14410 'org-overlay-type)
14411 'org-latex-overlay))))
14412 (setq txt (match-string n)
14413 beg (match-beginning n) end (match-end n)
14414 cnt (1+ cnt)
14415 linkfile (format "%s_%04d.png" prefix cnt)
14416 movefile (format "%s_%04d.png" absprefix cnt)
14417 link (concat block "[[file:" linkfile "]]" block))
14418 (if msg (message msg cnt))
14419 (goto-char beg)
14420 (unless checkdir ; make sure the directory exists
14421 (setq checkdir t)
14422 (or (file-directory-p todir) (make-directory todir)))
14424 (unless executables-checked
14425 (org-check-external-command
14426 "latex" "needed to convert LaTeX fragments to images")
14427 (org-check-external-command
14428 "dvipng" "needed to convert LaTeX fragments to images")
14429 (setq executables-checked t))
14431 (org-create-formula-image
14432 txt movefile opt forbuffer)
14433 (if overlays
14434 (progn
14435 (mapc (lambda (o)
14436 (if (eq (org-overlay-get o 'org-overlay-type)
14437 'org-latex-overlay)
14438 (org-delete-overlay o)))
14439 (org-overlays-in beg end))
14440 (setq ov (org-make-overlay beg end))
14441 (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
14442 (if (featurep 'xemacs)
14443 (progn
14444 (org-overlay-put ov 'invisible t)
14445 (org-overlay-put
14446 ov 'end-glyph
14447 (make-glyph (vector 'png :file movefile))))
14448 (org-overlay-put
14449 ov 'display
14450 (list 'image :type 'png :file movefile :ascent 'center)))
14451 (push ov org-latex-fragment-image-overlays)
14452 (goto-char end))
14453 (delete-region beg end)
14454 (insert link))))))))
14456 (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
14457 ;; This function borrows from Ganesh Swami's latex2png.el
14458 (defun org-create-formula-image (string tofile options buffer)
14459 "This calls dvipng."
14460 (require 'org-latex)
14461 (let* ((tmpdir (if (featurep 'xemacs)
14462 (temp-directory)
14463 temporary-file-directory))
14464 (texfilebase (make-temp-name
14465 (expand-file-name "orgtex" tmpdir)))
14466 (texfile (concat texfilebase ".tex"))
14467 (dvifile (concat texfilebase ".dvi"))
14468 (pngfile (concat texfilebase ".png"))
14469 (fnh (if (featurep 'xemacs)
14470 (font-height (get-face-font 'default))
14471 (face-attribute 'default :height nil)))
14472 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
14473 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
14474 (fg (or (plist-get options (if buffer :foreground :html-foreground))
14475 "Black"))
14476 (bg (or (plist-get options (if buffer :background :html-background))
14477 "Transparent")))
14478 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
14479 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
14480 (with-temp-file texfile
14481 (insert org-format-latex-header
14482 (if org-export-latex-packages-alist
14483 (concat "\n"
14484 (mapconcat (lambda(p)
14485 (if (equal "" (car p))
14486 (format "\\usepackage{%s}" (cadr p))
14487 (format "\\usepackage[%s]{%s}"
14488 (car p) (cadr p))))
14489 org-export-latex-packages-alist "\n"))
14491 "\n\\begin{document}\n" string "\n\\end{document}\n"))
14492 (let ((dir default-directory))
14493 (condition-case nil
14494 (progn
14495 (cd tmpdir)
14496 (call-process "latex" nil nil nil texfile))
14497 (error nil))
14498 (cd dir))
14499 (if (not (file-exists-p dvifile))
14500 (progn (message "Failed to create dvi file from %s" texfile) nil)
14501 (condition-case nil
14502 (call-process "dvipng" nil nil nil
14503 "-fg" fg "-bg" bg
14504 "-D" dpi
14505 ;;"-x" scale "-y" scale
14506 "-T" "tight"
14507 "-o" pngfile
14508 dvifile)
14509 (error nil))
14510 (if (not (file-exists-p pngfile))
14511 (progn (message "Failed to create png file from %s" texfile) nil)
14512 ;; Use the requested file name and clean up
14513 (copy-file pngfile tofile 'replace)
14514 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
14515 (delete-file (concat texfilebase e)))
14516 pngfile))))
14518 (defun org-dvipng-color (attr)
14519 "Return an rgb color specification for dvipng."
14520 (apply 'format "rgb %s %s %s"
14521 (mapcar 'org-normalize-color
14522 (color-values (face-attribute 'default attr nil)))))
14524 (defun org-normalize-color (value)
14525 "Return string to be used as color value for an RGB component."
14526 (format "%g" (/ value 65535.0)))
14528 ;;;; Key bindings
14530 ;; Make `C-c C-x' a prefix key
14531 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
14533 ;; TAB key with modifiers
14534 (org-defkey org-mode-map "\C-i" 'org-cycle)
14535 (org-defkey org-mode-map [(tab)] 'org-cycle)
14536 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
14537 (org-defkey org-mode-map [(meta tab)] 'org-complete)
14538 (org-defkey org-mode-map "\M-\t" 'org-complete)
14539 (org-defkey org-mode-map "\M-\C-i" 'org-complete)
14540 ;; The following line is necessary under Suse GNU/Linux
14541 (unless (featurep 'xemacs)
14542 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
14543 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
14544 (define-key org-mode-map [backtab] 'org-shifttab)
14546 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
14547 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
14548 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
14550 ;; Cursor keys with modifiers
14551 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
14552 (org-defkey org-mode-map [(meta right)] 'org-metaright)
14553 (org-defkey org-mode-map [(meta up)] 'org-metaup)
14554 (org-defkey org-mode-map [(meta down)] 'org-metadown)
14556 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
14557 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
14558 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
14559 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
14561 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
14562 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
14563 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
14564 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
14566 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
14567 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
14569 ;;; Extra keys for tty access.
14570 ;; We only set them when really needed because otherwise the
14571 ;; menus don't show the simple keys
14573 (when (or org-use-extra-keys
14574 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
14575 (not window-system))
14576 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
14577 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
14578 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
14579 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
14580 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
14581 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
14582 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
14583 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
14584 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
14585 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
14586 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
14587 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
14588 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
14589 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
14590 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
14591 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
14592 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
14593 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
14594 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
14595 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
14596 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
14597 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
14598 (org-defkey org-mode-map [?\e (tab)] 'org-complete)
14599 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
14600 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
14601 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
14602 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
14603 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
14605 ;; All the other keys
14607 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
14608 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
14609 (if (boundp 'narrow-map)
14610 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
14611 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
14612 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
14613 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
14614 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
14615 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
14616 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
14617 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
14618 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
14619 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
14620 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
14621 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
14622 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
14623 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
14624 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
14625 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
14626 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
14627 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
14628 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
14629 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
14630 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
14631 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
14632 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
14633 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
14634 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
14635 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
14636 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
14637 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
14638 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
14639 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
14640 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
14641 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
14642 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
14643 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
14644 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
14645 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
14646 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
14647 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
14648 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
14649 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
14650 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
14651 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
14652 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
14653 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
14654 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
14655 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
14656 (org-defkey org-mode-map "\C-c^" 'org-sort)
14657 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
14658 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
14659 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
14660 (org-defkey org-mode-map "\C-m" 'org-return)
14661 (org-defkey org-mode-map "\C-j" 'org-return-indent)
14662 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
14663 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
14664 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
14665 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
14666 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
14667 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
14668 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
14669 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
14670 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
14671 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
14672 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
14673 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
14674 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
14675 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
14676 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
14677 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
14678 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
14679 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
14680 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
14681 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
14683 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
14684 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
14685 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
14686 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
14688 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
14689 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
14690 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
14691 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
14692 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
14693 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
14694 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
14695 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
14696 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
14697 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
14698 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
14699 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
14700 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
14701 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
14702 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
14704 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
14705 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
14706 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
14707 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
14709 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
14711 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
14713 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
14714 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
14716 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
14719 (when (featurep 'xemacs)
14720 (org-defkey org-mode-map 'button3 'popup-mode-menu))
14723 (defconst org-speed-commands-default
14725 ("a" . org-agenda)
14726 ("/" . org-sparse-tree)
14727 (";" . org-set-tags-command)
14728 ("I" . org-clock-in)
14729 ("O" . org-clock-out)
14730 ("c" . org-cycle)
14731 ("C" . org-shifttab)
14732 ("n" . outline-next-visible-heading)
14733 ("p" . outline-previous-visible-heading)
14734 ("f" . org-forward-same-level)
14735 ("b" . org-backward-same-level)
14736 ("u" . outline-up-heading)
14737 ("U" . org-shiftmetaup)
14738 ("D" . org-shiftmetadown)
14739 ("r" . org-metaright)
14740 ("l" . org-metaleft)
14741 ("R" . org-shiftmetaright)
14742 ("L" . org-shiftmetaleft)
14743 ("i" . (progn (forward-char 1) (call-interactively
14744 'org-insert-heading-respect-content)))
14745 ("o" . org-open-at-point)
14746 ("t" . org-todo)
14747 ("j" . org-goto)
14748 ("g" . (org-refile t))
14749 ("e" . org-set-effort)
14750 ("0" . (org-priority ?\ ))
14751 ("1" . (org-priority ?A))
14752 ("2" . (org-priority ?B))
14753 ("3" . (org-priority ?C))
14754 ("." . outline-mark-subtree)
14755 ("^" . org-sort)
14756 ("w" . org-refile)
14757 ("a" . org-archive-subtree-default-with-confirmation)
14758 ("/" . org-sparse-tree)
14759 ("?" . org-speed-command-help)
14761 "The default speed commands.")
14763 (defun org-print-speed-command (e)
14764 (princ (car e))
14765 (princ " ")
14766 (if (symbolp (cdr e))
14767 (princ (symbol-name (cdr e)))
14768 (prin1 (cdr e)))
14769 (princ "\n"))
14771 (defun org-speed-command-help ()
14772 "Show the available speed commands."
14773 (interactive)
14774 (if (not org-use-speed-commands)
14775 (error "Speed commands are not activated, customize `org-use-speed-commands'.")
14776 (with-output-to-temp-buffer "*Help*"
14777 (princ "Speed commands\n==============\n")
14778 (mapc 'org-print-speed-command org-speed-commands-user)
14779 (princ "\n")
14780 (mapc 'org-print-speed-command org-speed-commands-default))))
14782 (defvar org-self-insert-command-undo-counter 0)
14784 (defvar org-table-auto-blank-field) ; defined in org-table.el
14785 (defvar org-speed-command nil)
14786 (defun org-self-insert-command (N)
14787 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
14788 If the cursor is in a table looking at whitespace, the whitespace is
14789 overwritten, and the table is not marked as requiring realignment."
14790 (interactive "p")
14791 (cond
14792 ((and org-use-speed-commands
14793 (bolp)
14794 (looking-at outline-regexp)
14795 (setq
14796 org-speed-command
14797 (or (cdr (assoc (this-command-keys) org-speed-commands-user))
14798 (cdr (assoc (this-command-keys) org-speed-commands-default)))))
14799 (cond
14800 ((commandp org-speed-command)
14801 (setq this-command org-speed-command)
14802 (call-interactively org-speed-command))
14803 ((functionp org-speed-command)
14804 (funcall org-speed-command))
14805 ((and org-speed-command (listp org-speed-command))
14806 (eval org-speed-command))
14807 (t (let (org-use-speed-commands)
14808 (call-interactively 'org-self-insert-command)))))
14809 ((and
14810 (org-table-p)
14811 (progn
14812 ;; check if we blank the field, and if that triggers align
14813 (and (featurep 'org-table) org-table-auto-blank-field
14814 (member last-command
14815 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
14816 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
14817 ;; got extra space, this field does not determine column width
14818 (let (org-table-may-need-update) (org-table-blank-field))
14819 ;; no extra space, this field may determine column width
14820 (org-table-blank-field)))
14822 (eq N 1)
14823 (looking-at "[^|\n]* |"))
14824 (let (org-table-may-need-update)
14825 (goto-char (1- (match-end 0)))
14826 (delete-backward-char 1)
14827 (goto-char (match-beginning 0))
14828 (self-insert-command N)))
14830 (setq org-table-may-need-update t)
14831 (self-insert-command N)
14832 (org-fix-tags-on-the-fly)
14833 (if org-self-insert-cluster-for-undo
14834 (if (not (eq last-command 'org-self-insert-command))
14835 (setq org-self-insert-command-undo-counter 1)
14836 (if (>= org-self-insert-command-undo-counter 20)
14837 (setq org-self-insert-command-undo-counter 1)
14838 (and (> org-self-insert-command-undo-counter 0)
14839 buffer-undo-list
14840 (not (cadr buffer-undo-list)) ; remove nil entry
14841 (setcdr buffer-undo-list (cddr buffer-undo-list)))
14842 (setq org-self-insert-command-undo-counter
14843 (1+ org-self-insert-command-undo-counter))))))))
14845 (defun org-fix-tags-on-the-fly ()
14846 (when (and (equal (char-after (point-at-bol)) ?*)
14847 (org-on-heading-p))
14848 (org-align-tags-here org-tags-column)))
14850 (defun org-delete-backward-char (N)
14851 "Like `delete-backward-char', insert whitespace at field end in tables.
14852 When deleting backwards, in tables this function will insert whitespace in
14853 front of the next \"|\" separator, to keep the table aligned. The table will
14854 still be marked for re-alignment if the field did fill the entire column,
14855 because, in this case the deletion might narrow the column."
14856 (interactive "p")
14857 (if (and (org-table-p)
14858 (eq N 1)
14859 (string-match "|" (buffer-substring (point-at-bol) (point)))
14860 (looking-at ".*?|"))
14861 (let ((pos (point))
14862 (noalign (looking-at "[^|\n\r]* |"))
14863 (c org-table-may-need-update))
14864 (backward-delete-char N)
14865 (skip-chars-forward "^|")
14866 (insert " ")
14867 (goto-char (1- pos))
14868 ;; noalign: if there were two spaces at the end, this field
14869 ;; does not determine the width of the column.
14870 (if noalign (setq org-table-may-need-update c)))
14871 (backward-delete-char N)
14872 (org-fix-tags-on-the-fly)))
14874 (defun org-delete-char (N)
14875 "Like `delete-char', but insert whitespace at field end in tables.
14876 When deleting characters, in tables this function will insert whitespace in
14877 front of the next \"|\" separator, to keep the table aligned. The table will
14878 still be marked for re-alignment if the field did fill the entire column,
14879 because, in this case the deletion might narrow the column."
14880 (interactive "p")
14881 (if (and (org-table-p)
14882 (not (bolp))
14883 (not (= (char-after) ?|))
14884 (eq N 1))
14885 (if (looking-at ".*?|")
14886 (let ((pos (point))
14887 (noalign (looking-at "[^|\n\r]* |"))
14888 (c org-table-may-need-update))
14889 (replace-match (concat
14890 (substring (match-string 0) 1 -1)
14891 " |"))
14892 (goto-char pos)
14893 ;; noalign: if there were two spaces at the end, this field
14894 ;; does not determine the width of the column.
14895 (if noalign (setq org-table-may-need-update c)))
14896 (delete-char N))
14897 (delete-char N)
14898 (org-fix-tags-on-the-fly)))
14900 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
14901 (put 'org-self-insert-command 'delete-selection t)
14902 (put 'orgtbl-self-insert-command 'delete-selection t)
14903 (put 'org-delete-char 'delete-selection 'supersede)
14904 (put 'org-delete-backward-char 'delete-selection 'supersede)
14905 (put 'org-yank 'delete-selection 'yank)
14907 ;; Make `flyspell-mode' delay after some commands
14908 (put 'org-self-insert-command 'flyspell-delayed t)
14909 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
14910 (put 'org-delete-char 'flyspell-delayed t)
14911 (put 'org-delete-backward-char 'flyspell-delayed t)
14913 ;; Make pabbrev-mode expand after org-mode commands
14914 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
14915 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
14917 ;; How to do this: Measure non-white length of current string
14918 ;; If equal to column width, we should realign.
14920 (defun org-remap (map &rest commands)
14921 "In MAP, remap the functions given in COMMANDS.
14922 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
14923 (let (new old)
14924 (while commands
14925 (setq old (pop commands) new (pop commands))
14926 (if (fboundp 'command-remapping)
14927 (org-defkey map (vector 'remap old) new)
14928 (substitute-key-definition old new map global-map)))))
14930 (when (eq org-enable-table-editor 'optimized)
14931 ;; If the user wants maximum table support, we need to hijack
14932 ;; some standard editing functions
14933 (org-remap org-mode-map
14934 'self-insert-command 'org-self-insert-command
14935 'delete-char 'org-delete-char
14936 'delete-backward-char 'org-delete-backward-char)
14937 (org-defkey org-mode-map "|" 'org-force-self-insert))
14939 (defvar org-ctrl-c-ctrl-c-hook nil
14940 "Hook for functions attaching themselves to `C-c C-c'.
14941 This can be used to add additional functionality to the C-c C-c key which
14942 executes context-dependent commands.
14943 Each function will be called with no arguments. The function must check
14944 if the context is appropriate for it to act. If yes, it should do its
14945 thing and then return a non-nil value. If the context is wrong,
14946 just do nothing and return nil.")
14948 (defvar org-tab-first-hook nil
14949 "Hook for functions to attach themselves to TAB.
14950 See `org-ctrl-c-ctrl-c-hook' for more information.
14951 This hook runs as the first action when TAB is pressed, even before
14952 `org-cycle' messes around with the `outline-regexp' to cater for
14953 inline tasks and plain list item folding.
14954 If any function in this hook returns t, not other actions like table
14955 field motion visibility cycling will be done.")
14957 (defvar org-tab-after-check-for-table-hook nil
14958 "Hook for functions to attach themselves to TAB.
14959 See `org-ctrl-c-ctrl-c-hook' for more information.
14960 This hook runs after it has been established that the cursor is not in a
14961 table, but before checking if the cursor is in a headline or if global cycling
14962 should be done.
14963 If any function in this hook returns t, not other actions like visibility
14964 cycling will be done.")
14966 (defvar org-tab-after-check-for-cycling-hook nil
14967 "Hook for functions to attach themselves to TAB.
14968 See `org-ctrl-c-ctrl-c-hook' for more information.
14969 This hook runs after it has been established that not table field motion and
14970 not visibility should be done because of current context. This is probably
14971 the place where a package like yasnippets can hook in.")
14973 (defvar org-tab-before-tab-emulation-hook nil
14974 "Hook for functions to attach themselves to TAB.
14975 See `org-ctrl-c-ctrl-c-hook' for more information.
14976 This hook runs after every other options for TAB have been exhausted, but
14977 before indentation and \t insertion takes place.")
14979 (defvar org-metaleft-hook nil
14980 "Hook for functions attaching themselves to `M-left'.
14981 See `org-ctrl-c-ctrl-c-hook' for more information.")
14982 (defvar org-metaright-hook nil
14983 "Hook for functions attaching themselves to `M-right'.
14984 See `org-ctrl-c-ctrl-c-hook' for more information.")
14985 (defvar org-metaup-hook nil
14986 "Hook for functions attaching themselves to `M-up'.
14987 See `org-ctrl-c-ctrl-c-hook' for more information.")
14988 (defvar org-metadown-hook nil
14989 "Hook for functions attaching themselves to `M-down'.
14990 See `org-ctrl-c-ctrl-c-hook' for more information.")
14991 (defvar org-shiftmetaleft-hook nil
14992 "Hook for functions attaching themselves to `M-S-left'.
14993 See `org-ctrl-c-ctrl-c-hook' for more information.")
14994 (defvar org-shiftmetaright-hook nil
14995 "Hook for functions attaching themselves to `M-S-right'.
14996 See `org-ctrl-c-ctrl-c-hook' for more information.")
14997 (defvar org-shiftmetaup-hook nil
14998 "Hook for functions attaching themselves to `M-S-up'.
14999 See `org-ctrl-c-ctrl-c-hook' for more information.")
15000 (defvar org-shiftmetadown-hook nil
15001 "Hook for functions attaching themselves to `M-S-down'.
15002 See `org-ctrl-c-ctrl-c-hook' for more information.")
15003 (defvar org-metareturn-hook nil
15004 "Hook for functions attaching themselves to `M-RET'.
15005 See `org-ctrl-c-ctrl-c-hook' for more information.")
15007 (defun org-modifier-cursor-error ()
15008 "Throw an error, a modified cursor command was applied in wrong context."
15009 (error "This command is active in special context like tables, headlines or items"))
15011 (defun org-shiftselect-error ()
15012 "Throw an error because Shift-Cursor command was applied in wrong context."
15013 (if (and (boundp 'shift-select-mode) shift-select-mode)
15014 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
15015 (error "This command works only in special context like headlines or timestamps")))
15017 (defun org-call-for-shift-select (cmd)
15018 (let ((this-command-keys-shift-translated t))
15019 (call-interactively cmd)))
15021 (defun org-shifttab (&optional arg)
15022 "Global visibility cycling or move to previous table field.
15023 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
15024 on context.
15025 See the individual commands for more information."
15026 (interactive "P")
15027 (cond
15028 ((org-at-table-p) (call-interactively 'org-table-previous-field))
15029 ((integerp arg)
15030 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
15031 (message "Content view to level: %d" arg)
15032 (org-content (prefix-numeric-value arg2))
15033 (setq org-cycle-global-status 'overview)))
15034 (t (call-interactively 'org-global-cycle))))
15036 (defun org-shiftmetaleft ()
15037 "Promote subtree or delete table column.
15038 Calls `org-promote-subtree', `org-outdent-item',
15039 or `org-table-delete-column', depending on context.
15040 See the individual commands for more information."
15041 (interactive)
15042 (cond
15043 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
15044 ((org-at-table-p) (call-interactively 'org-table-delete-column))
15045 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
15046 ((org-at-item-p) (call-interactively 'org-outdent-item))
15047 (t (org-modifier-cursor-error))))
15049 (defun org-shiftmetaright ()
15050 "Demote subtree or insert table column.
15051 Calls `org-demote-subtree', `org-indent-item',
15052 or `org-table-insert-column', depending on context.
15053 See the individual commands for more information."
15054 (interactive)
15055 (cond
15056 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
15057 ((org-at-table-p) (call-interactively 'org-table-insert-column))
15058 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
15059 ((org-at-item-p) (call-interactively 'org-indent-item))
15060 (t (org-modifier-cursor-error))))
15062 (defun org-shiftmetaup (&optional arg)
15063 "Move subtree up or kill table row.
15064 Calls `org-move-subtree-up' or `org-table-kill-row' or
15065 `org-move-item-up' depending on context. See the individual commands
15066 for more information."
15067 (interactive "P")
15068 (cond
15069 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
15070 ((org-at-table-p) (call-interactively 'org-table-kill-row))
15071 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
15072 ((org-at-item-p) (call-interactively 'org-move-item-up))
15073 (t (org-modifier-cursor-error))))
15075 (defun org-shiftmetadown (&optional arg)
15076 "Move subtree down or insert table row.
15077 Calls `org-move-subtree-down' or `org-table-insert-row' or
15078 `org-move-item-down', depending on context. See the individual
15079 commands for more information."
15080 (interactive "P")
15081 (cond
15082 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
15083 ((org-at-table-p) (call-interactively 'org-table-insert-row))
15084 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
15085 ((org-at-item-p) (call-interactively 'org-move-item-down))
15086 (t (org-modifier-cursor-error))))
15088 (defun org-metaleft (&optional arg)
15089 "Promote heading or move table column to left.
15090 Calls `org-do-promote' or `org-table-move-column', depending on context.
15091 With no specific context, calls the Emacs default `backward-word'.
15092 See the individual commands for more information."
15093 (interactive "P")
15094 (cond
15095 ((run-hook-with-args-until-success 'org-metaleft-hook))
15096 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
15097 ((or (org-on-heading-p)
15098 (and (org-region-active-p)
15099 (save-excursion
15100 (goto-char (region-beginning))
15101 (org-on-heading-p))))
15102 (call-interactively 'org-do-promote))
15103 ((or (org-at-item-p)
15104 (and (org-region-active-p)
15105 (save-excursion
15106 (goto-char (region-beginning))
15107 (org-at-item-p))))
15108 (call-interactively 'org-outdent-item))
15109 (t (call-interactively 'backward-word))))
15111 (defun org-metaright (&optional arg)
15112 "Demote subtree or move table column to right.
15113 Calls `org-do-demote' or `org-table-move-column', depending on context.
15114 With no specific context, calls the Emacs default `forward-word'.
15115 See the individual commands for more information."
15116 (interactive "P")
15117 (cond
15118 ((run-hook-with-args-until-success 'org-metaright-hook))
15119 ((org-at-table-p) (call-interactively 'org-table-move-column))
15120 ((or (org-on-heading-p)
15121 (and (org-region-active-p)
15122 (save-excursion
15123 (goto-char (region-beginning))
15124 (org-on-heading-p))))
15125 (call-interactively 'org-do-demote))
15126 ((or (org-at-item-p)
15127 (and (org-region-active-p)
15128 (save-excursion
15129 (goto-char (region-beginning))
15130 (org-at-item-p))))
15131 (call-interactively 'org-indent-item))
15132 (t (call-interactively 'forward-word))))
15134 (defun org-metaup (&optional arg)
15135 "Move subtree up or move table row up.
15136 Calls `org-move-subtree-up' or `org-table-move-row' or
15137 `org-move-item-up', depending on context. See the individual commands
15138 for more information."
15139 (interactive "P")
15140 (cond
15141 ((run-hook-with-args-until-success 'org-metaup-hook))
15142 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
15143 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
15144 ((org-at-item-p) (call-interactively 'org-move-item-up))
15145 (t (transpose-lines 1) (beginning-of-line -1))))
15147 (defun org-metadown (&optional arg)
15148 "Move subtree down or move table row down.
15149 Calls `org-move-subtree-down' or `org-table-move-row' or
15150 `org-move-item-down', depending on context. See the individual
15151 commands for more information."
15152 (interactive "P")
15153 (cond
15154 ((run-hook-with-args-until-success 'org-metadown-hook))
15155 ((org-at-table-p) (call-interactively 'org-table-move-row))
15156 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
15157 ((org-at-item-p) (call-interactively 'org-move-item-down))
15158 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
15160 (defun org-shiftup (&optional arg)
15161 "Increase item in timestamp or increase priority of current headline.
15162 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
15163 depending on context. See the individual commands for more information."
15164 (interactive "P")
15165 (cond
15166 ((and org-support-shift-select (org-region-active-p))
15167 (org-call-for-shift-select 'previous-line))
15168 ((org-at-timestamp-p t)
15169 (call-interactively (if org-edit-timestamp-down-means-later
15170 'org-timestamp-down 'org-timestamp-up)))
15171 ((and (not (eq org-support-shift-select 'always))
15172 org-enable-priority-commands
15173 (org-on-heading-p))
15174 (call-interactively 'org-priority-up))
15175 ((and (not org-support-shift-select) (org-at-item-p))
15176 (call-interactively 'org-previous-item))
15177 ((org-clocktable-try-shift 'up arg))
15178 (org-support-shift-select
15179 (org-call-for-shift-select 'previous-line))
15180 (t (org-shiftselect-error))))
15182 (defun org-shiftdown (&optional arg)
15183 "Decrease item in timestamp or decrease priority of current headline.
15184 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
15185 depending on context. See the individual commands for more information."
15186 (interactive "P")
15187 (cond
15188 ((and org-support-shift-select (org-region-active-p))
15189 (org-call-for-shift-select 'next-line))
15190 ((org-at-timestamp-p t)
15191 (call-interactively (if org-edit-timestamp-down-means-later
15192 'org-timestamp-up 'org-timestamp-down)))
15193 ((and (not (eq org-support-shift-select 'always))
15194 org-enable-priority-commands
15195 (org-on-heading-p))
15196 (call-interactively 'org-priority-down))
15197 ((and (not org-support-shift-select) (org-at-item-p))
15198 (call-interactively 'org-next-item))
15199 ((org-clocktable-try-shift 'down arg))
15200 (org-support-shift-select
15201 (org-call-for-shift-select 'next-line))
15202 (t (org-shiftselect-error))))
15204 (defun org-shiftright (&optional arg)
15205 "Cycle the thing at point or in the current line, depending on context.
15206 Depending on context, this does one of the following:
15208 - switch a timestamp at point one day into the future
15209 - on a headline, switch to the next TODO keyword.
15210 - on an item, switch entire list to the next bullet type
15211 - on a property line, switch to the next allowed value
15212 - on a clocktable definition line, move time block into the future"
15213 (interactive "P")
15214 (cond
15215 ((and org-support-shift-select (org-region-active-p))
15216 (org-call-for-shift-select 'forward-char))
15217 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
15218 ((and (not (eq org-support-shift-select 'always))
15219 (org-on-heading-p))
15220 (let ((org-inhibit-logging
15221 (not org-treat-S-cursor-todo-selection-as-state-change))
15222 (org-inhibit-blocking
15223 (not org-treat-S-cursor-todo-selection-as-state-change)))
15224 (org-call-with-arg 'org-todo 'right)))
15225 ((or (and org-support-shift-select
15226 (not (eq org-support-shift-select 'always))
15227 (org-at-item-bullet-p))
15228 (and (not org-support-shift-select) (org-at-item-p)))
15229 (org-call-with-arg 'org-cycle-list-bullet nil))
15230 ((and (not (eq org-support-shift-select 'always))
15231 (org-at-property-p))
15232 (call-interactively 'org-property-next-allowed-value))
15233 ((org-clocktable-try-shift 'right arg))
15234 (org-support-shift-select
15235 (org-call-for-shift-select 'forward-char))
15236 (t (org-shiftselect-error))))
15238 (defun org-shiftleft (&optional arg)
15239 "Cycle the thing at point or in the current line, depending on context.
15240 Depending on context, this does one of the following:
15242 - switch a timestamp at point one day into the past
15243 - on a headline, switch to the previous TODO keyword.
15244 - on an item, switch entire list to the previous bullet type
15245 - on a property line, switch to the previous allowed value
15246 - on a clocktable definition line, move time block into the past"
15247 (interactive "P")
15248 (cond
15249 ((and org-support-shift-select (org-region-active-p))
15250 (org-call-for-shift-select 'backward-char))
15251 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
15252 ((and (not (eq org-support-shift-select 'always))
15253 (org-on-heading-p))
15254 (let ((org-inhibit-logging
15255 (not org-treat-S-cursor-todo-selection-as-state-change))
15256 (org-inhibit-blocking
15257 (not org-treat-S-cursor-todo-selection-as-state-change)))
15258 (org-call-with-arg 'org-todo 'left)))
15259 ((or (and org-support-shift-select
15260 (not (eq org-support-shift-select 'always))
15261 (org-at-item-bullet-p))
15262 (and (not org-support-shift-select) (org-at-item-p)))
15263 (org-call-with-arg 'org-cycle-list-bullet 'previous))
15264 ((and (not (eq org-support-shift-select 'always))
15265 (org-at-property-p))
15266 (call-interactively 'org-property-previous-allowed-value))
15267 ((org-clocktable-try-shift 'left arg))
15268 (org-support-shift-select
15269 (org-call-for-shift-select 'backward-char))
15270 (t (org-shiftselect-error))))
15272 (defun org-shiftcontrolright ()
15273 "Switch to next TODO set."
15274 (interactive)
15275 (cond
15276 ((and org-support-shift-select (org-region-active-p))
15277 (org-call-for-shift-select 'forward-word))
15278 ((and (not (eq org-support-shift-select 'always))
15279 (org-on-heading-p))
15280 (org-call-with-arg 'org-todo 'nextset))
15281 (org-support-shift-select
15282 (org-call-for-shift-select 'forward-word))
15283 (t (org-shiftselect-error))))
15285 (defun org-shiftcontrolleft ()
15286 "Switch to previous TODO set."
15287 (interactive)
15288 (cond
15289 ((and org-support-shift-select (org-region-active-p))
15290 (org-call-for-shift-select 'backward-word))
15291 ((and (not (eq org-support-shift-select 'always))
15292 (org-on-heading-p))
15293 (org-call-with-arg 'org-todo 'previousset))
15294 (org-support-shift-select
15295 (org-call-for-shift-select 'backward-word))
15296 (t (org-shiftselect-error))))
15298 (defun org-ctrl-c-ret ()
15299 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
15300 (interactive)
15301 (cond
15302 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
15303 (t (call-interactively 'org-insert-heading))))
15305 (defun org-copy-special ()
15306 "Copy region in table or copy current subtree.
15307 Calls `org-table-copy' or `org-copy-subtree', depending on context.
15308 See the individual commands for more information."
15309 (interactive)
15310 (call-interactively
15311 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
15313 (defun org-cut-special ()
15314 "Cut region in table or cut current subtree.
15315 Calls `org-table-copy' or `org-cut-subtree', depending on context.
15316 See the individual commands for more information."
15317 (interactive)
15318 (call-interactively
15319 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
15321 (defun org-paste-special (arg)
15322 "Paste rectangular region into table, or past subtree relative to level.
15323 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
15324 See the individual commands for more information."
15325 (interactive "P")
15326 (if (org-at-table-p)
15327 (org-table-paste-rectangle)
15328 (org-paste-subtree arg)))
15330 (defun org-edit-special ()
15331 "Call a special editor for the stuff at point.
15332 When at a table, call the formula editor with `org-table-edit-formulas'.
15333 When at the first line of an src example, call `org-edit-src-code'.
15334 When in an #+include line, visit the include file. Otherwise call
15335 `ffap' to visit the file at point."
15336 (interactive)
15337 (cond
15338 ((org-at-table-p)
15339 (call-interactively 'org-table-edit-formulas))
15340 ((save-excursion
15341 (beginning-of-line 1)
15342 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
15343 (find-file (org-trim (match-string 1))))
15344 ((org-edit-src-code))
15345 ((org-edit-fixed-width-region))
15346 (t (call-interactively 'ffap))))
15349 (defun org-ctrl-c-ctrl-c (&optional arg)
15350 "Set tags in headline, or update according to changed information at point.
15352 This command does many different things, depending on context:
15354 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
15355 this is what we do.
15357 - If the cursor is on a statistics cookie, update it.
15359 - If the cursor is in a headline, prompt for tags and insert them
15360 into the current line, aligned to `org-tags-column'. When called
15361 with prefix arg, realign all tags in the current buffer.
15363 - If the cursor is in one of the special #+KEYWORD lines, this
15364 triggers scanning the buffer for these lines and updating the
15365 information.
15367 - If the cursor is inside a table, realign the table. This command
15368 works even if the automatic table editor has been turned off.
15370 - If the cursor is on a #+TBLFM line, re-apply the formulas to
15371 the entire table.
15373 - If the cursor is at a footnote reference or definition, jump to
15374 the corresponding definition or references, respectively.
15376 - If the cursor is a the beginning of a dynamic block, update it.
15378 - If the cursor is inside a table created by the table.el package,
15379 activate that table.
15381 - If the current buffer is a remember buffer, close note and file
15382 it. A prefix argument of 1 files to the default location
15383 without further interaction. A prefix argument of 2 files to
15384 the currently clocking task.
15386 - If the cursor is on a <<<target>>>, update radio targets and corresponding
15387 links in this buffer.
15389 - If the cursor is on a numbered item in a plain list, renumber the
15390 ordered list.
15392 - If the cursor is on a checkbox, toggle it."
15393 (interactive "P")
15394 (let ((org-enable-table-editor t))
15395 (cond
15396 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
15397 org-occur-highlights
15398 org-latex-fragment-image-overlays)
15399 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
15400 (org-remove-occur-highlights)
15401 (org-remove-latex-fragment-image-overlays)
15402 (message "Temporary highlights/overlays removed from current buffer"))
15403 ((and (local-variable-p 'org-finish-function (current-buffer))
15404 (fboundp org-finish-function))
15405 (funcall org-finish-function))
15406 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
15407 ((org-at-property-p)
15408 (call-interactively 'org-property-action))
15409 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
15410 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
15411 (or (org-on-heading-p) (org-at-item-p)))
15412 (call-interactively 'org-update-statistics-cookies))
15413 ((org-on-heading-p) (call-interactively 'org-set-tags))
15414 ((org-at-table.el-p)
15415 (require 'table)
15416 (beginning-of-line 1)
15417 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
15418 (call-interactively 'table-recognize-table))
15419 ((org-at-table-p)
15420 (org-table-maybe-eval-formula)
15421 (if arg
15422 (call-interactively 'org-table-recalculate)
15423 (org-table-maybe-recalculate-line))
15424 (call-interactively 'org-table-align))
15425 ((or (org-footnote-at-reference-p)
15426 (org-footnote-at-definition-p))
15427 (call-interactively 'org-footnote-action))
15428 ((org-at-item-checkbox-p)
15429 (call-interactively 'org-toggle-checkbox))
15430 ((org-at-item-p)
15431 (if arg
15432 (call-interactively 'org-toggle-checkbox)
15433 (call-interactively 'org-maybe-renumber-ordered-list)))
15434 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
15435 ;; Dynamic block
15436 (beginning-of-line 1)
15437 (save-excursion (org-update-dblock)))
15438 ((save-excursion
15439 (beginning-of-line 1)
15440 (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
15441 (cond
15442 ((equal (match-string 1) "TBLFM")
15443 ;; Recalculate the table before this line
15444 (save-excursion
15445 (beginning-of-line 1)
15446 (skip-chars-backward " \r\n\t")
15447 (if (org-at-table-p)
15448 (org-call-with-arg 'org-table-recalculate (or arg t)))))
15450 ; (org-set-regexps-and-options)
15451 ; (org-restart-font-lock)
15452 (let ((org-inhibit-startup t)) (org-mode-restart))
15453 (message "Local setup has been refreshed"))))
15454 ((org-clock-update-time-maybe))
15455 (t (error "C-c C-c can do nothing useful at this location")))))
15457 (defun org-mode-restart ()
15458 "Restart Org-mode, to scan again for special lines.
15459 Also updates the keyword regular expressions."
15460 (interactive)
15461 (org-mode)
15462 (message "Org-mode restarted"))
15464 (defun org-kill-note-or-show-branches ()
15465 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
15466 (interactive)
15467 (if (not org-finish-function)
15468 (call-interactively 'show-branches)
15469 (let ((org-note-abort t))
15470 (funcall org-finish-function))))
15472 (defun org-return (&optional indent)
15473 "Goto next table row or insert a newline.
15474 Calls `org-table-next-row' or `newline', depending on context.
15475 See the individual commands for more information."
15476 (interactive)
15477 (cond
15478 ((bobp) (if indent (newline-and-indent) (newline)))
15479 ((org-at-table-p)
15480 (org-table-justify-field-maybe)
15481 (call-interactively 'org-table-next-row))
15482 ((and org-return-follows-link
15483 (eq (get-text-property (point) 'face) 'org-link))
15484 (call-interactively 'org-open-at-point))
15485 ((and (org-at-heading-p)
15486 (looking-at
15487 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
15488 (org-show-entry)
15489 (end-of-line 1)
15490 (newline))
15491 (t (if indent (newline-and-indent) (newline)))))
15493 (defun org-return-indent ()
15494 "Goto next table row or insert a newline and indent.
15495 Calls `org-table-next-row' or `newline-and-indent', depending on
15496 context. See the individual commands for more information."
15497 (interactive)
15498 (org-return t))
15500 (defun org-ctrl-c-star ()
15501 "Compute table, or change heading status of lines.
15502 Calls `org-table-recalculate' or `org-toggle-heading',
15503 depending on context."
15504 (interactive)
15505 (cond
15506 ((org-at-table-p)
15507 (call-interactively 'org-table-recalculate))
15509 ;; Convert all lines in region to list items
15510 (call-interactively 'org-toggle-heading))))
15512 (defun org-ctrl-c-minus ()
15513 "Insert separator line in table or modify bullet status of line.
15514 Also turns a plain line or a region of lines into list items.
15515 Calls `org-table-insert-hline', `org-toggle-item', or
15516 `org-cycle-list-bullet', depending on context."
15517 (interactive)
15518 (cond
15519 ((org-at-table-p)
15520 (call-interactively 'org-table-insert-hline))
15521 ((org-region-active-p)
15522 (call-interactively 'org-toggle-item))
15523 ((org-in-item-p)
15524 (call-interactively 'org-cycle-list-bullet))
15526 (call-interactively 'org-toggle-item))))
15528 (defun org-toggle-item ()
15529 "Convert headings or normal lines to items, items to normal lines.
15530 If there is no active region, only the current line is considered.
15532 If the first line in the region is a headline, convert all headlines to items.
15534 If the first line in the region is an item, convert all items to normal lines.
15536 If the first line is normal text, add an item bullet to each line."
15537 (interactive)
15538 (let (l2 l beg end)
15539 (if (org-region-active-p)
15540 (setq beg (region-beginning) end (region-end))
15541 (setq beg (point-at-bol)
15542 end (min (1+ (point-at-eol)) (point-max))))
15543 (save-excursion
15544 (goto-char end)
15545 (setq l2 (org-current-line))
15546 (goto-char beg)
15547 (beginning-of-line 1)
15548 (setq l (1- (org-current-line)))
15549 (if (org-at-item-p)
15550 ;; We already have items, de-itemize
15551 (while (< (setq l (1+ l)) l2)
15552 (when (org-at-item-p)
15553 (goto-char (match-beginning 2))
15554 (delete-region (match-beginning 2) (match-end 2))
15555 (and (looking-at "[ \t]+") (replace-match "")))
15556 (beginning-of-line 2))
15557 (if (org-on-heading-p)
15558 ;; Headings, convert to items
15559 (while (< (setq l (1+ l)) l2)
15560 (if (looking-at org-outline-regexp)
15561 (replace-match "- " t t))
15562 (beginning-of-line 2))
15563 ;; normal lines, turn them into items
15564 (while (< (setq l (1+ l)) l2)
15565 (unless (org-at-item-p)
15566 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
15567 (replace-match "\\1- \\2")))
15568 (beginning-of-line 2)))))))
15570 (defun org-toggle-heading (&optional nstars)
15571 "Convert headings to normal text, or items or text to headings.
15572 If there is no active region, only the current line is considered.
15574 If the first line is a heading, remove the stars from all headlines
15575 in the region.
15577 If the first line is a plain list item, turn all plain list items
15578 into headings.
15580 If the first line is a normal line, turn each and every line in the
15581 region into a heading.
15583 When converting a line into a heading, the number of stars is chosen
15584 such that the lines become children of the current entry. However,
15585 when a prefix argument is given, its value determines the number of
15586 stars to add."
15587 (interactive "P")
15588 (let (l2 l itemp beg end)
15589 (if (org-region-active-p)
15590 (setq beg (region-beginning) end (region-end))
15591 (setq beg (point-at-bol)
15592 end (min (1+ (point-at-eol)) (point-max))))
15593 (save-excursion
15594 (goto-char end)
15595 (setq l2 (org-current-line))
15596 (goto-char beg)
15597 (beginning-of-line 1)
15598 (setq l (1- (org-current-line)))
15599 (if (org-on-heading-p)
15600 ;; We already have headlines, de-star them
15601 (while (< (setq l (1+ l)) l2)
15602 (when (org-on-heading-p t)
15603 (and (looking-at outline-regexp) (replace-match "")))
15604 (beginning-of-line 2))
15605 (setq itemp (org-at-item-p))
15606 (let* ((stars
15607 (if nstars
15608 (make-string (prefix-numeric-value current-prefix-arg)
15610 (save-excursion
15611 (if (re-search-backward org-complex-heading-regexp nil t)
15612 (match-string 1) ""))))
15613 (add-stars (cond (nstars "")
15614 ((equal stars "") "*")
15615 (org-odd-levels-only "**")
15616 (t "*")))
15617 (rpl (concat stars add-stars " ")))
15618 (while (< (setq l (1+ l)) l2)
15619 (if itemp
15620 (and (org-at-item-p) (replace-match rpl t t))
15621 (unless (org-on-heading-p)
15622 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
15623 (replace-match (concat rpl (match-string 2))))))
15624 (beginning-of-line 2)))))))
15626 (defun org-meta-return (&optional arg)
15627 "Insert a new heading or wrap a region in a table.
15628 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
15629 See the individual commands for more information."
15630 (interactive "P")
15631 (cond
15632 ((run-hook-with-args-until-success 'org-metareturn-hook))
15633 ((org-at-table-p)
15634 (call-interactively 'org-table-wrap-region))
15635 (t (call-interactively 'org-insert-heading))))
15637 ;;; Menu entries
15639 ;; Define the Org-mode menus
15640 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
15641 '("Tbl"
15642 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
15643 ["Next Field" org-cycle (org-at-table-p)]
15644 ["Previous Field" org-shifttab (org-at-table-p)]
15645 ["Next Row" org-return (org-at-table-p)]
15646 "--"
15647 ["Blank Field" org-table-blank-field (org-at-table-p)]
15648 ["Edit Field" org-table-edit-field (org-at-table-p)]
15649 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
15650 "--"
15651 ("Column"
15652 ["Move Column Left" org-metaleft (org-at-table-p)]
15653 ["Move Column Right" org-metaright (org-at-table-p)]
15654 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
15655 ["Insert Column" org-shiftmetaright (org-at-table-p)])
15656 ("Row"
15657 ["Move Row Up" org-metaup (org-at-table-p)]
15658 ["Move Row Down" org-metadown (org-at-table-p)]
15659 ["Delete Row" org-shiftmetaup (org-at-table-p)]
15660 ["Insert Row" org-shiftmetadown (org-at-table-p)]
15661 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
15662 "--"
15663 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
15664 ("Rectangle"
15665 ["Copy Rectangle" org-copy-special (org-at-table-p)]
15666 ["Cut Rectangle" org-cut-special (org-at-table-p)]
15667 ["Paste Rectangle" org-paste-special (org-at-table-p)]
15668 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
15669 "--"
15670 ("Calculate"
15671 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
15672 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
15673 ["Edit Formulas" org-edit-special (org-at-table-p)]
15674 "--"
15675 ["Recalculate line" org-table-recalculate (org-at-table-p)]
15676 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
15677 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
15678 "--"
15679 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
15680 "--"
15681 ["Sum Column/Rectangle" org-table-sum
15682 (or (org-at-table-p) (org-region-active-p))]
15683 ["Which Column?" org-table-current-column (org-at-table-p)])
15684 ["Debug Formulas"
15685 org-table-toggle-formula-debugger
15686 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
15687 ["Show Col/Row Numbers"
15688 org-table-toggle-coordinate-overlays
15689 :style toggle
15690 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
15691 "--"
15692 ["Create" org-table-create (and (not (org-at-table-p))
15693 org-enable-table-editor)]
15694 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
15695 ["Import from File" org-table-import (not (org-at-table-p))]
15696 ["Export to File" org-table-export (org-at-table-p)]
15697 "--"
15698 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
15700 (easy-menu-define org-org-menu org-mode-map "Org menu"
15701 '("Org"
15702 ("Show/Hide"
15703 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
15704 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
15705 ["Sparse Tree..." org-sparse-tree t]
15706 ["Reveal Context" org-reveal t]
15707 ["Show All" show-all t]
15708 "--"
15709 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
15710 "--"
15711 ["New Heading" org-insert-heading t]
15712 ("Navigate Headings"
15713 ["Up" outline-up-heading t]
15714 ["Next" outline-next-visible-heading t]
15715 ["Previous" outline-previous-visible-heading t]
15716 ["Next Same Level" outline-forward-same-level t]
15717 ["Previous Same Level" outline-backward-same-level t]
15718 "--"
15719 ["Jump" org-goto t])
15720 ("Edit Structure"
15721 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
15722 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
15723 "--"
15724 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
15725 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
15726 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
15727 "--"
15728 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
15729 "--"
15730 ["Promote Heading" org-metaleft (not (org-at-table-p))]
15731 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
15732 ["Demote Heading" org-metaright (not (org-at-table-p))]
15733 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
15734 "--"
15735 ["Sort Region/Children" org-sort (not (org-at-table-p))]
15736 "--"
15737 ["Convert to odd levels" org-convert-to-odd-levels t]
15738 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
15739 ("Editing"
15740 ["Emphasis..." org-emphasize t]
15741 ["Edit Source Example" org-edit-special t]
15742 "--"
15743 ["Footnote new/jump" org-footnote-action t]
15744 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
15745 ("Archive"
15746 ["Archive (default method)" org-archive-subtree-default t]
15747 "--"
15748 ["Move Subtree to Archive file" org-advertized-archive-subtree t]
15749 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
15750 ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
15752 "--"
15753 ("Hyperlinks"
15754 ["Store Link (Global)" org-store-link t]
15755 ["Find existing link to here" org-occur-link-in-agenda-files t]
15756 ["Insert Link" org-insert-link t]
15757 ["Follow Link" org-open-at-point t]
15758 "--"
15759 ["Next link" org-next-link t]
15760 ["Previous link" org-previous-link t]
15761 "--"
15762 ["Descriptive Links"
15763 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
15764 :style radio
15765 :selected (member '(org-link) buffer-invisibility-spec)]
15766 ["Literal Links"
15767 (progn
15768 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
15769 :style radio
15770 :selected (not (member '(org-link) buffer-invisibility-spec))])
15771 "--"
15772 ("TODO Lists"
15773 ["TODO/DONE/-" org-todo t]
15774 ("Select keyword"
15775 ["Next keyword" org-shiftright (org-on-heading-p)]
15776 ["Previous keyword" org-shiftleft (org-on-heading-p)]
15777 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
15778 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
15779 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
15780 ["Show TODO Tree" org-show-todo-tree t]
15781 ["Global TODO list" org-todo-list t]
15782 "--"
15783 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
15784 :selected org-enforce-todo-dependencies :style toggle :active t]
15785 "Settings for tree at point"
15786 ["Do Children sequentially" org-toggle-ordered-property :style radio
15787 :selected (ignore-errors (org-entry-get nil "ORDERED"))
15788 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
15789 ["Do Children parallel" org-toggle-ordered-property :style radio
15790 :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
15791 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
15792 "--"
15793 ["Set Priority" org-priority t]
15794 ["Priority Up" org-shiftup t]
15795 ["Priority Down" org-shiftdown t]
15796 "--"
15797 ["Get news from all feeds" org-feed-update-all t]
15798 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
15799 ["Customize feeds" (customize-variable 'org-feed-alist) t])
15800 ("TAGS and Properties"
15801 ["Set Tags" org-set-tags-command t]
15802 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
15803 "--"
15804 ["Set property" org-set-property t]
15805 ["Column view of properties" org-columns t]
15806 ["Insert Column View DBlock" org-insert-columns-dblock t])
15807 ("Dates and Scheduling"
15808 ["Timestamp" org-time-stamp t]
15809 ["Timestamp (inactive)" org-time-stamp-inactive t]
15810 ("Change Date"
15811 ["1 Day Later" org-shiftright t]
15812 ["1 Day Earlier" org-shiftleft t]
15813 ["1 ... Later" org-shiftup t]
15814 ["1 ... Earlier" org-shiftdown t])
15815 ["Compute Time Range" org-evaluate-time-range t]
15816 ["Schedule Item" org-schedule t]
15817 ["Deadline" org-deadline t]
15818 "--"
15819 ["Custom time format" org-toggle-time-stamp-overlays
15820 :style radio :selected org-display-custom-times]
15821 "--"
15822 ["Goto Calendar" org-goto-calendar t]
15823 ["Date from Calendar" org-date-from-calendar t]
15824 "--"
15825 ["Start/Restart Timer" org-timer-start t]
15826 ["Pause/Continue Timer" org-timer-pause-or-continue t]
15827 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
15828 ["Insert Timer String" org-timer t]
15829 ["Insert Timer Item" org-timer-item t])
15830 ("Logging work"
15831 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
15832 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
15833 ["Clock out" org-clock-out t]
15834 ["Clock cancel" org-clock-cancel t]
15835 "--"
15836 ["Mark as default task" org-clock-mark-default-task t]
15837 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
15838 ["Goto running clock" org-clock-goto t]
15839 "--"
15840 ["Display times" org-clock-display t]
15841 ["Create clock table" org-clock-report t]
15842 "--"
15843 ["Record DONE time"
15844 (progn (setq org-log-done (not org-log-done))
15845 (message "Switching to %s will %s record a timestamp"
15846 (car org-done-keywords)
15847 (if org-log-done "automatically" "not")))
15848 :style toggle :selected org-log-done])
15849 "--"
15850 ["Agenda Command..." org-agenda t]
15851 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
15852 ("File List for Agenda")
15853 ("Special views current file"
15854 ["TODO Tree" org-show-todo-tree t]
15855 ["Check Deadlines" org-check-deadlines t]
15856 ["Timeline" org-timeline t]
15857 ["Tags/Property tree" org-match-sparse-tree t])
15858 "--"
15859 ["Export/Publish..." org-export t]
15860 ("LaTeX"
15861 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
15862 :selected org-cdlatex-mode]
15863 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
15864 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
15865 ["Modify math symbol" org-cdlatex-math-modify
15866 (org-inside-LaTeX-fragment-p)]
15867 ["Insert citation" org-reftex-citation t]
15868 "--"
15869 ["Export LaTeX fragments as images"
15870 (if (featurep 'org-exp)
15871 (setq org-export-with-LaTeX-fragments
15872 (not org-export-with-LaTeX-fragments))
15873 (require 'org-exp))
15874 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
15875 org-export-with-LaTeX-fragments)])
15876 "--"
15877 ("MobileOrg"
15878 ["Push Files and Views" org-mobile-push t]
15879 ["Get Captured and Flagged" org-mobile-pull t]
15880 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
15881 "--"
15882 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
15883 "--"
15884 ("Documentation"
15885 ["Show Version" org-version t]
15886 ["Info Documentation" org-info t])
15887 ("Customize"
15888 ["Browse Org Group" org-customize t]
15889 "--"
15890 ["Expand This Menu" org-create-customize-menu
15891 (fboundp 'customize-menu-create)])
15892 ["Send bug report" org-submit-bug-report t]
15893 "--"
15894 ("Refresh/Reload"
15895 ["Refresh setup current buffer" org-mode-restart t]
15896 ["Reload Org (after update)" org-reload t]
15897 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
15900 (defun org-info (&optional node)
15901 "Read documentation for Org-mode in the info system.
15902 With optional NODE, go directly to that node."
15903 (interactive)
15904 (info (format "(org)%s" (or node ""))))
15906 ;;;###autoload
15907 (defun org-submit-bug-report ()
15908 "Submit a bug report on Org-mode via mail.
15910 Don't hesitate to report any problems or inaccurate documentation.
15912 If you don't have setup sending mail from (X)Emacs, please copy the
15913 output buffer into your mail program, as it gives us important
15914 information about your Org-mode version and configuration."
15915 (interactive)
15916 (require 'reporter)
15917 (org-load-modules-maybe)
15918 (org-require-autoloaded-modules)
15919 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
15920 (reporter-submit-bug-report
15921 "emacs-orgmode@gnu.org"
15922 (org-version)
15923 (let (list)
15924 (save-window-excursion
15925 (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
15926 (delete-other-windows)
15927 (erase-buffer)
15928 (insert "You are about to submit a bug report to the Org-mode mailing list.
15930 We would like to add your full Org-mode and Outline configuration to the
15931 bug report. This greatly simplifies the work of the maintainer and
15932 other experts on the mailing list.
15934 HOWEVER, some variables you have customized may contain private
15935 information. The names of customers, colleagues, or friends, might
15936 appear in the form of file names, tags, todo states, or search strings.
15937 If you answer yes to the prompt, you might want to check and remove
15938 such private information before sending the email.")
15939 (add-text-properties (point-min) (point-max) '(face org-warning))
15940 (when (yes-or-no-p "Include your Org-mode configuration ")
15941 (mapatoms
15942 (lambda (v)
15943 (and (boundp v)
15944 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
15945 (or (and (symbol-value v)
15946 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
15947 (and
15948 (get v 'custom-type) (get v 'standard-value)
15949 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
15950 (push v list)))))
15951 (kill-buffer (get-buffer "*Warn about privacy*"))
15952 list))
15953 nil nil
15954 "Remember to cover the basics, that is, what you expected to happen and
15955 what in fact did happen. You don't know how to make a good report? See
15957 http://orgmode.org/manual/Feedback.html#Feedback
15959 Your bug report will be posted to the Org-mode mailing list.
15960 ------------------------------------------------------------------------")))
15962 (defun org-install-agenda-files-menu ()
15963 (let ((bl (buffer-list)))
15964 (save-excursion
15965 (while bl
15966 (set-buffer (pop bl))
15967 (if (org-mode-p) (setq bl nil)))
15968 (when (org-mode-p)
15969 (easy-menu-change
15970 '("Org") "File List for Agenda"
15971 (append
15972 (list
15973 ["Edit File List" (org-edit-agenda-file-list) t]
15974 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
15975 ["Remove Current File from List" org-remove-file t]
15976 ["Cycle through agenda files" org-cycle-agenda-files t]
15977 ["Occur in all agenda files" org-occur-in-agenda-files t]
15978 "--")
15979 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
15981 ;;;; Documentation
15983 ;;;###autoload
15984 (defun org-require-autoloaded-modules ()
15985 (interactive)
15986 (mapc 'require
15987 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
15988 org-docbook org-exp org-html org-icalendar
15989 org-id org-latex
15990 org-publish org-remember org-table
15991 org-timer org-xoxo)))
15993 ;;;###autoload
15994 (defun org-reload (&optional uncompiled)
15995 "Reload all org lisp files.
15996 With prefix arg UNCOMPILED, load the uncompiled versions."
15997 (interactive "P")
15998 (require 'find-func)
15999 (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
16000 (dir-org (file-name-directory (org-find-library-name "org")))
16001 (dir-org-contrib (ignore-errors
16002 (file-name-directory
16003 (org-find-library-name "org-contribdir"))))
16004 (files
16005 (append (directory-files dir-org t file-re)
16006 (and dir-org-contrib
16007 (directory-files dir-org-contrib t file-re))))
16008 (remove-re (concat (if (featurep 'xemacs)
16009 "org-colview" "org-colview-xemacs")
16010 "\\'")))
16011 (setq files (mapcar 'file-name-sans-extension files))
16012 (setq files (mapcar
16013 (lambda (x) (if (string-match remove-re x) nil x))
16014 files))
16015 (setq files (delq nil files))
16016 (mapc
16017 (lambda (f)
16018 (when (featurep (intern (file-name-nondirectory f)))
16019 (if (and (not uncompiled)
16020 (file-exists-p (concat f ".elc")))
16021 (load (concat f ".elc") nil nil t)
16022 (load (concat f ".el") nil nil t))))
16023 files))
16024 (org-version))
16026 ;;;###autoload
16027 (defun org-customize ()
16028 "Call the customize function with org as argument."
16029 (interactive)
16030 (org-load-modules-maybe)
16031 (org-require-autoloaded-modules)
16032 (customize-browse 'org))
16034 (defun org-create-customize-menu ()
16035 "Create a full customization menu for Org-mode, insert it into the menu."
16036 (interactive)
16037 (org-load-modules-maybe)
16038 (org-require-autoloaded-modules)
16039 (if (fboundp 'customize-menu-create)
16040 (progn
16041 (easy-menu-change
16042 '("Org") "Customize"
16043 `(["Browse Org group" org-customize t]
16044 "--"
16045 ,(customize-menu-create 'org)
16046 ["Set" Custom-set t]
16047 ["Save" Custom-save t]
16048 ["Reset to Current" Custom-reset-current t]
16049 ["Reset to Saved" Custom-reset-saved t]
16050 ["Reset to Standard Settings" Custom-reset-standard t]))
16051 (message "\"Org\"-menu now contains full customization menu"))
16052 (error "Cannot expand menu (outdated version of cus-edit.el)")))
16054 ;;;; Miscellaneous stuff
16056 ;;; Generally useful functions
16058 (defun org-get-at-bol (property)
16059 "Get text property PROPERTY at beginning of line."
16060 (get-text-property (point-at-bol) property))
16062 (defun org-find-text-property-in-string (prop s)
16063 "Return the first non-nil value of property PROP in string S."
16064 (or (get-text-property 0 prop s)
16065 (get-text-property (or (next-single-property-change 0 prop s) 0)
16066 prop s)))
16068 (defun org-display-warning (message) ;; Copied from Emacs-Muse
16069 "Display the given MESSAGE as a warning."
16070 (if (fboundp 'display-warning)
16071 (display-warning 'org message
16072 (if (featurep 'xemacs)
16073 'warning
16074 :warning))
16075 (let ((buf (get-buffer-create "*Org warnings*")))
16076 (with-current-buffer buf
16077 (goto-char (point-max))
16078 (insert "Warning (Org): " message)
16079 (unless (bolp)
16080 (newline)))
16081 (display-buffer buf)
16082 (sit-for 0))))
16084 (defun org-in-commented-line ()
16085 "Is point in a line starting with `#'?"
16086 (equal (char-after (point-at-bol)) ?#))
16088 (defun org-in-verbatim-emphasis ()
16089 (save-match-data
16090 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
16092 (defun org-goto-marker-or-bmk (marker &optional bookmark)
16093 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
16094 (if (and marker (marker-buffer marker)
16095 (buffer-live-p (marker-buffer marker)))
16096 (progn
16097 (switch-to-buffer (marker-buffer marker))
16098 (if (or (> marker (point-max)) (< marker (point-min)))
16099 (widen))
16100 (goto-char marker)
16101 (org-show-context 'org-goto))
16102 (if bookmark
16103 (bookmark-jump bookmark)
16104 (error "Cannot find location"))))
16106 (defun org-quote-csv-field (s)
16107 "Quote field for inclusion in CSV material."
16108 (if (string-match "[\",]" s)
16109 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
16112 (defun org-plist-delete (plist property)
16113 "Delete PROPERTY from PLIST.
16114 This is in contrast to merely setting it to 0."
16115 (let (p)
16116 (while plist
16117 (if (not (eq property (car plist)))
16118 (setq p (plist-put p (car plist) (nth 1 plist))))
16119 (setq plist (cddr plist)))
16122 (defun org-force-self-insert (N)
16123 "Needed to enforce self-insert under remapping."
16124 (interactive "p")
16125 (self-insert-command N))
16127 (defun org-string-width (s)
16128 "Compute width of string, ignoring invisible characters.
16129 This ignores character with invisibility property `org-link', and also
16130 characters with property `org-cwidth', because these will become invisible
16131 upon the next fontification round."
16132 (let (b l)
16133 (when (or (eq t buffer-invisibility-spec)
16134 (assq 'org-link buffer-invisibility-spec))
16135 (while (setq b (text-property-any 0 (length s)
16136 'invisible 'org-link s))
16137 (setq s (concat (substring s 0 b)
16138 (substring s (or (next-single-property-change
16139 b 'invisible s) (length s)))))))
16140 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
16141 (setq s (concat (substring s 0 b)
16142 (substring s (or (next-single-property-change
16143 b 'org-cwidth s) (length s))))))
16144 (setq l (string-width s) b -1)
16145 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
16146 (setq l (- l (get-text-property b 'org-dwidth-n s))))
16149 (defun org-get-indentation (&optional line)
16150 "Get the indentation of the current line, interpreting tabs.
16151 When LINE is given, assume it represents a line and compute its indentation."
16152 (if line
16153 (if (string-match "^ *" (org-remove-tabs line))
16154 (match-end 0))
16155 (save-excursion
16156 (beginning-of-line 1)
16157 (skip-chars-forward " \t")
16158 (current-column))))
16160 (defun org-remove-tabs (s &optional width)
16161 "Replace tabulators in S with spaces.
16162 Assumes that s is a single line, starting in column 0."
16163 (setq width (or width tab-width))
16164 (while (string-match "\t" s)
16165 (setq s (replace-match
16166 (make-string
16167 (- (* width (/ (+ (match-beginning 0) width) width))
16168 (match-beginning 0)) ?\ )
16169 t t s)))
16172 (defun org-fix-indentation (line ind)
16173 "Fix indentation in LINE.
16174 IND is a cons cell with target and minimum indentation.
16175 If the current indentation in LINE is smaller than the minimum,
16176 leave it alone. If it is larger than ind, set it to the target."
16177 (let* ((l (org-remove-tabs line))
16178 (i (org-get-indentation l))
16179 (i1 (car ind)) (i2 (cdr ind)))
16180 (if (>= i i2) (setq l (substring line i2)))
16181 (if (> i1 0)
16182 (concat (make-string i1 ?\ ) l)
16183 l)))
16185 (defun org-remove-indentation (code &optional n)
16186 "Remove the maximum common indentation from the lines in CODE.
16187 N may optionally be the number of spaces to remove."
16188 (with-temp-buffer
16189 (insert code)
16190 (org-do-remove-indentation n)
16191 (buffer-string)))
16193 (defun org-do-remove-indentation (&optional n)
16194 "Remove the maximum common indentation from the buffer."
16195 (untabify (point-min) (point-max))
16196 (let ((min 10000) re)
16197 (if n
16198 (setq min n)
16199 (goto-char (point-min))
16200 (while (re-search-forward "^ *[^ \n]" nil t)
16201 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
16202 (unless (or (= min 0) (= min 10000))
16203 (setq re (format "^ \\{%d\\}" min))
16204 (goto-char (point-min))
16205 (while (re-search-forward re nil t)
16206 (replace-match "")
16207 (end-of-line 1))
16208 min)))
16210 (defun org-fill-template (template alist)
16211 "Find each %key of ALIST in TEMPLATE and replace it."
16212 (let (entry key value)
16213 (setq alist (sort (copy-sequence alist)
16214 (lambda (a b) (< (length (car a)) (length (car b))))))
16215 (while (setq entry (pop alist))
16216 (setq template
16217 (replace-regexp-in-string
16218 (concat "%" (regexp-quote (car entry)))
16219 (cdr entry) template t t)))
16220 template))
16222 (defun org-base-buffer (buffer)
16223 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
16224 (if (not buffer)
16225 buffer
16226 (or (buffer-base-buffer buffer)
16227 buffer)))
16229 (defun org-trim (s)
16230 "Remove whitespace at beginning and end of string."
16231 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
16232 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
16235 (defun org-wrap (string &optional width lines)
16236 "Wrap string to either a number of lines, or a width in characters.
16237 If WIDTH is non-nil, the string is wrapped to that width, however many lines
16238 that costs. If there is a word longer than WIDTH, the text is actually
16239 wrapped to the length of that word.
16240 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
16241 many lines, whatever width that takes.
16242 The return value is a list of lines, without newlines at the end."
16243 (let* ((words (org-split-string string "[ \t\n]+"))
16244 (maxword (apply 'max (mapcar 'org-string-width words)))
16245 w ll)
16246 (cond (width
16247 (org-do-wrap words (max maxword width)))
16248 (lines
16249 (setq w maxword)
16250 (setq ll (org-do-wrap words maxword))
16251 (if (<= (length ll) lines)
16253 (setq ll words)
16254 (while (> (length ll) lines)
16255 (setq w (1+ w))
16256 (setq ll (org-do-wrap words w)))
16257 ll))
16258 (t (error "Cannot wrap this")))))
16260 (defun org-do-wrap (words width)
16261 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
16262 (let (lines line)
16263 (while words
16264 (setq line (pop words))
16265 (while (and words (< (+ (length line) (length (car words))) width))
16266 (setq line (concat line " " (pop words))))
16267 (setq lines (push line lines)))
16268 (nreverse lines)))
16270 (defun org-split-string (string &optional separators)
16271 "Splits STRING into substrings at SEPARATORS.
16272 No empty strings are returned if there are matches at the beginning
16273 and end of string."
16274 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
16275 (start 0)
16276 notfirst
16277 (list nil))
16278 (while (and (string-match rexp string
16279 (if (and notfirst
16280 (= start (match-beginning 0))
16281 (< start (length string)))
16282 (1+ start) start))
16283 (< (match-beginning 0) (length string)))
16284 (setq notfirst t)
16285 (or (eq (match-beginning 0) 0)
16286 (and (eq (match-beginning 0) (match-end 0))
16287 (eq (match-beginning 0) start))
16288 (setq list
16289 (cons (substring string start (match-beginning 0))
16290 list)))
16291 (setq start (match-end 0)))
16292 (or (eq start (length string))
16293 (setq list
16294 (cons (substring string start)
16295 list)))
16296 (nreverse list)))
16298 (defun org-quote-vert (s)
16299 "Replace \"|\" with \"\\vert\"."
16300 (while (string-match "|" s)
16301 (setq s (replace-match "\\vert" t t s)))
16304 (defun org-uuidgen-p (s)
16305 "Is S an ID created by UUIDGEN?"
16306 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
16308 (defun org-context ()
16309 "Return a list of contexts of the current cursor position.
16310 If several contexts apply, all are returned.
16311 Each context entry is a list with a symbol naming the context, and
16312 two positions indicating start and end of the context. Possible
16313 contexts are:
16315 :headline anywhere in a headline
16316 :headline-stars on the leading stars in a headline
16317 :todo-keyword on a TODO keyword (including DONE) in a headline
16318 :tags on the TAGS in a headline
16319 :priority on the priority cookie in a headline
16320 :item on the first line of a plain list item
16321 :item-bullet on the bullet/number of a plain list item
16322 :checkbox on the checkbox in a plain list item
16323 :table in an org-mode table
16324 :table-special on a special filed in a table
16325 :table-table in a table.el table
16326 :link on a hyperlink
16327 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
16328 :target on a <<target>>
16329 :radio-target on a <<<radio-target>>>
16330 :latex-fragment on a LaTeX fragment
16331 :latex-preview on a LaTeX fragment with overlayed preview image
16333 This function expects the position to be visible because it uses font-lock
16334 faces as a help to recognize the following contexts: :table-special, :link,
16335 and :keyword."
16336 (let* ((f (get-text-property (point) 'face))
16337 (faces (if (listp f) f (list f)))
16338 (p (point)) clist o)
16339 ;; First the large context
16340 (cond
16341 ((org-on-heading-p t)
16342 (push (list :headline (point-at-bol) (point-at-eol)) clist)
16343 (when (progn
16344 (beginning-of-line 1)
16345 (looking-at org-todo-line-tags-regexp))
16346 (push (org-point-in-group p 1 :headline-stars) clist)
16347 (push (org-point-in-group p 2 :todo-keyword) clist)
16348 (push (org-point-in-group p 4 :tags) clist))
16349 (goto-char p)
16350 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
16351 (if (looking-at "\\[#[A-Z0-9]\\]")
16352 (push (org-point-in-group p 0 :priority) clist)))
16354 ((org-at-item-p)
16355 (push (org-point-in-group p 2 :item-bullet) clist)
16356 (push (list :item (point-at-bol)
16357 (save-excursion (org-end-of-item) (point)))
16358 clist)
16359 (and (org-at-item-checkbox-p)
16360 (push (org-point-in-group p 0 :checkbox) clist)))
16362 ((org-at-table-p)
16363 (push (list :table (org-table-begin) (org-table-end)) clist)
16364 (if (memq 'org-formula faces)
16365 (push (list :table-special
16366 (previous-single-property-change p 'face)
16367 (next-single-property-change p 'face)) clist)))
16368 ((org-at-table-p 'any)
16369 (push (list :table-table) clist)))
16370 (goto-char p)
16372 ;; Now the small context
16373 (cond
16374 ((org-at-timestamp-p)
16375 (push (org-point-in-group p 0 :timestamp) clist))
16376 ((memq 'org-link faces)
16377 (push (list :link
16378 (previous-single-property-change p 'face)
16379 (next-single-property-change p 'face)) clist))
16380 ((memq 'org-special-keyword faces)
16381 (push (list :keyword
16382 (previous-single-property-change p 'face)
16383 (next-single-property-change p 'face)) clist))
16384 ((org-on-target-p)
16385 (push (org-point-in-group p 0 :target) clist)
16386 (goto-char (1- (match-beginning 0)))
16387 (if (looking-at org-radio-target-regexp)
16388 (push (org-point-in-group p 0 :radio-target) clist))
16389 (goto-char p))
16390 ((setq o (car (delq nil
16391 (mapcar
16392 (lambda (x)
16393 (if (memq x org-latex-fragment-image-overlays) x))
16394 (org-overlays-at (point))))))
16395 (push (list :latex-fragment
16396 (org-overlay-start o) (org-overlay-end o)) clist)
16397 (push (list :latex-preview
16398 (org-overlay-start o) (org-overlay-end o)) clist))
16399 ((org-inside-LaTeX-fragment-p)
16400 ;; FIXME: positions wrong.
16401 (push (list :latex-fragment (point) (point)) clist)))
16403 (setq clist (nreverse (delq nil clist)))
16404 clist))
16406 ;; FIXME: Compare with at-regexp-p Do we need both?
16407 (defun org-in-regexp (re &optional nlines visually)
16408 "Check if point is inside a match of regexp.
16409 Normally only the current line is checked, but you can include NLINES extra
16410 lines both before and after point into the search.
16411 If VISUALLY is set, require that the cursor is not after the match but
16412 really on, so that the block visually is on the match."
16413 (catch 'exit
16414 (let ((pos (point))
16415 (eol (point-at-eol (+ 1 (or nlines 0))))
16416 (inc (if visually 1 0)))
16417 (save-excursion
16418 (beginning-of-line (- 1 (or nlines 0)))
16419 (while (re-search-forward re eol t)
16420 (if (and (<= (match-beginning 0) pos)
16421 (>= (+ inc (match-end 0)) pos))
16422 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
16424 (defun org-at-regexp-p (regexp)
16425 "Is point inside a match of REGEXP in the current line?"
16426 (catch 'exit
16427 (save-excursion
16428 (let ((pos (point)) (end (point-at-eol)))
16429 (beginning-of-line 1)
16430 (while (re-search-forward regexp end t)
16431 (if (and (<= (match-beginning 0) pos)
16432 (>= (match-end 0) pos))
16433 (throw 'exit t)))
16434 nil))))
16436 (defun org-occur-in-agenda-files (regexp &optional nlines)
16437 "Call `multi-occur' with buffers for all agenda files."
16438 (interactive "sOrg-files matching: \np")
16439 (let* ((files (org-agenda-files))
16440 (tnames (mapcar 'file-truename files))
16441 (extra org-agenda-text-search-extra-files)
16443 (when (eq (car extra) 'agenda-archives)
16444 (setq extra (cdr extra))
16445 (setq files (org-add-archive-files files)))
16446 (while (setq f (pop extra))
16447 (unless (member (file-truename f) tnames)
16448 (add-to-list 'files f 'append)
16449 (add-to-list 'tnames (file-truename f) 'append)))
16450 (multi-occur
16451 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
16452 regexp)))
16454 (if (boundp 'occur-mode-find-occurrence-hook)
16455 ;; Emacs 23
16456 (add-hook 'occur-mode-find-occurrence-hook
16457 (lambda ()
16458 (when (org-mode-p)
16459 (org-reveal))))
16460 ;; Emacs 22
16461 (defadvice occur-mode-goto-occurrence
16462 (after org-occur-reveal activate)
16463 (and (org-mode-p) (org-reveal)))
16464 (defadvice occur-mode-goto-occurrence-other-window
16465 (after org-occur-reveal activate)
16466 (and (org-mode-p) (org-reveal)))
16467 (defadvice occur-mode-display-occurrence
16468 (after org-occur-reveal activate)
16469 (when (org-mode-p)
16470 (let ((pos (occur-mode-find-occurrence)))
16471 (with-current-buffer (marker-buffer pos)
16472 (save-excursion
16473 (goto-char pos)
16474 (org-reveal)))))))
16476 (defun org-occur-link-in-agenda-files ()
16477 "Create a link and search for it in the agendas.
16478 The link is not stored in `org-stored-links', it is just created
16479 for the search purpose."
16480 (interactive)
16481 (let ((link (condition-case nil
16482 (org-store-link nil)
16483 (error "Unable to create a link to here"))))
16484 (org-occur-in-agenda-files (regexp-quote link))))
16486 (defun org-uniquify (list)
16487 "Remove duplicate elements from LIST."
16488 (let (res)
16489 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
16490 res))
16492 (defun org-delete-all (elts list)
16493 "Remove all elements in ELTS from LIST."
16494 (while elts
16495 (setq list (delete (pop elts) list)))
16496 list)
16498 (defun org-back-over-empty-lines ()
16499 "Move backwards over whitespace, to the beginning of the first empty line.
16500 Returns the number of empty lines passed."
16501 (let ((pos (point)))
16502 (skip-chars-backward " \t\n\r")
16503 (beginning-of-line 2)
16504 (goto-char (min (point) pos))
16505 (count-lines (point) pos)))
16507 (defun org-skip-whitespace ()
16508 (skip-chars-forward " \t\n\r"))
16510 (defun org-point-in-group (point group &optional context)
16511 "Check if POINT is in match-group GROUP.
16512 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
16513 match. If the match group does ot exist or point is not inside it,
16514 return nil."
16515 (and (match-beginning group)
16516 (>= point (match-beginning group))
16517 (<= point (match-end group))
16518 (if context
16519 (list context (match-beginning group) (match-end group))
16520 t)))
16522 (defun org-switch-to-buffer-other-window (&rest args)
16523 "Switch to buffer in a second window on the current frame.
16524 In particular, do not allow pop-up frames."
16525 (let (pop-up-frames special-display-buffer-names special-display-regexps
16526 special-display-function)
16527 (apply 'switch-to-buffer-other-window args)))
16529 (defun org-combine-plists (&rest plists)
16530 "Create a single property list from all plists in PLISTS.
16531 The process starts by copying the first list, and then setting properties
16532 from the other lists. Settings in the last list are the most significant
16533 ones and overrule settings in the other lists."
16534 (let ((rtn (copy-sequence (pop plists)))
16535 p v ls)
16536 (while plists
16537 (setq ls (pop plists))
16538 (while ls
16539 (setq p (pop ls) v (pop ls))
16540 (setq rtn (plist-put rtn p v))))
16541 rtn))
16543 (defun org-move-line-down (arg)
16544 "Move the current line down. With prefix argument, move it past ARG lines."
16545 (interactive "p")
16546 (let ((col (current-column))
16547 beg end pos)
16548 (beginning-of-line 1) (setq beg (point))
16549 (beginning-of-line 2) (setq end (point))
16550 (beginning-of-line (+ 1 arg))
16551 (setq pos (move-marker (make-marker) (point)))
16552 (insert (delete-and-extract-region beg end))
16553 (goto-char pos)
16554 (org-move-to-column col)))
16556 (defun org-move-line-up (arg)
16557 "Move the current line up. With prefix argument, move it past ARG lines."
16558 (interactive "p")
16559 (let ((col (current-column))
16560 beg end pos)
16561 (beginning-of-line 1) (setq beg (point))
16562 (beginning-of-line 2) (setq end (point))
16563 (beginning-of-line (- arg))
16564 (setq pos (move-marker (make-marker) (point)))
16565 (insert (delete-and-extract-region beg end))
16566 (goto-char pos)
16567 (org-move-to-column col)))
16569 (defun org-replace-escapes (string table)
16570 "Replace %-escapes in STRING with values in TABLE.
16571 TABLE is an association list with keys like \"%a\" and string values.
16572 The sequences in STRING may contain normal field width and padding information,
16573 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
16574 so values can contain further %-escapes if they are define later in TABLE."
16575 (let ((case-fold-search nil)
16576 e re rpl)
16577 (while (setq e (pop table))
16578 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
16579 (while (string-match re string)
16580 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
16581 (cdr e)))
16582 (setq string (replace-match rpl t t string))))
16583 string))
16586 (defun org-sublist (list start end)
16587 "Return a section of LIST, from START to END.
16588 Counting starts at 1."
16589 (let (rtn (c start))
16590 (setq list (nthcdr (1- start) list))
16591 (while (and list (<= c end))
16592 (push (pop list) rtn)
16593 (setq c (1+ c)))
16594 (nreverse rtn)))
16596 (defun org-find-base-buffer-visiting (file)
16597 "Like `find-buffer-visiting' but always return the base buffer and
16598 not an indirect buffer."
16599 (let ((buf (or (get-file-buffer file)
16600 (find-buffer-visiting file))))
16601 (if buf
16602 (or (buffer-base-buffer buf) buf)
16603 nil)))
16605 (defun org-image-file-name-regexp (&optional extensions)
16606 "Return regexp matching the file names of images.
16607 If EXTENSIONS is given, only match these."
16608 (if (and (not extensions) (fboundp 'image-file-name-regexp))
16609 (image-file-name-regexp)
16610 (let ((image-file-name-extensions
16611 (or extensions
16612 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
16613 "xbm" "xpm" "pbm" "pgm" "ppm"))))
16614 (concat "\\."
16615 (regexp-opt (nconc (mapcar 'upcase
16616 image-file-name-extensions)
16617 image-file-name-extensions)
16619 "\\'"))))
16621 (defun org-file-image-p (file &optional extensions)
16622 "Return non-nil if FILE is an image."
16623 (save-match-data
16624 (string-match (org-image-file-name-regexp extensions) file)))
16626 (defun org-get-cursor-date ()
16627 "Return the date at cursor in as a time.
16628 This works in the calendar and in the agenda, anywhere else it just
16629 returns the current time."
16630 (let (date day defd)
16631 (cond
16632 ((eq major-mode 'calendar-mode)
16633 (setq date (calendar-cursor-to-date)
16634 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16635 ((eq major-mode 'org-agenda-mode)
16636 (setq day (get-text-property (point) 'day))
16637 (if day
16638 (setq date (calendar-gregorian-from-absolute day)
16639 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
16640 (nth 2 date))))))
16641 (or defd (current-time))))
16643 (defvar org-agenda-action-marker (make-marker)
16644 "Marker pointing to the entry for the next agenda action.")
16646 (defun org-mark-entry-for-agenda-action ()
16647 "Mark the current entry as target of an agenda action.
16648 Agenda actions are actions executed from the agenda with the key `k',
16649 which make use of the date at the cursor."
16650 (interactive)
16651 (move-marker org-agenda-action-marker
16652 (save-excursion (org-back-to-heading t) (point))
16653 (current-buffer))
16654 (message
16655 "Entry marked for action; press `k' at desired date in agenda or calendar"))
16657 ;;; Paragraph filling stuff.
16658 ;; We want this to be just right, so use the full arsenal.
16660 (defun org-indent-line-function ()
16661 "Indent line like previous, but further if previous was headline or item."
16662 (interactive)
16663 (let* ((pos (point))
16664 (itemp (org-at-item-p))
16665 (case-fold-search t)
16666 (org-drawer-regexp (or org-drawer-regexp "\000"))
16667 column bpos bcol tpos tcol bullet btype bullet-type)
16668 ;; Find the previous relevant line
16669 (beginning-of-line 1)
16670 (cond
16671 ((looking-at "#") (setq column 0))
16672 ((looking-at "\\*+ ") (setq column 0))
16673 ((and (looking-at "[ \t]*:END:")
16674 (save-excursion (re-search-backward org-drawer-regexp nil t)))
16675 (save-excursion
16676 (goto-char (1- (match-beginning 1)))
16677 (setq column (current-column))))
16678 ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
16679 (save-excursion
16680 (re-search-backward
16681 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
16682 (setq column (org-get-indentation (match-string 0))))
16684 (beginning-of-line 0)
16685 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
16686 (not (looking-at "[ \t]*:END:"))
16687 (not (looking-at org-drawer-regexp)))
16688 (beginning-of-line 0))
16689 (cond
16690 ((looking-at "\\*+[ \t]+")
16691 (if (not org-adapt-indentation)
16692 (setq column 0)
16693 (goto-char (match-end 0))
16694 (setq column (current-column))))
16695 ((looking-at org-drawer-regexp)
16696 (goto-char (1- (match-beginning 1)))
16697 (setq column (current-column)))
16698 ((looking-at "\\([ \t]*\\):END:")
16699 (goto-char (match-end 1))
16700 (setq column (current-column)))
16701 ((org-in-item-p)
16702 (org-beginning-of-item)
16703 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
16704 (setq bpos (match-beginning 1) tpos (match-end 0)
16705 bcol (progn (goto-char bpos) (current-column))
16706 tcol (progn (goto-char tpos) (current-column))
16707 bullet (match-string 1)
16708 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
16709 (if (> tcol (+ bcol org-description-max-indent))
16710 (setq tcol (+ bcol 5)))
16711 (if (not itemp)
16712 (setq column tcol)
16713 (goto-char pos)
16714 (beginning-of-line 1)
16715 (if (looking-at "\\S-")
16716 (progn
16717 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
16718 (setq bullet (match-string 1)
16719 btype (if (string-match "[0-9]" bullet) "n" bullet))
16720 (setq column (if (equal btype bullet-type) bcol tcol)))
16721 (setq column (org-get-indentation)))))
16722 (t (setq column (org-get-indentation))))))
16723 (goto-char pos)
16724 (if (<= (current-column) (current-indentation))
16725 (org-indent-line-to column)
16726 (save-excursion (org-indent-line-to column)))
16727 (setq column (current-column))
16728 (beginning-of-line 1)
16729 (if (looking-at
16730 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
16731 (replace-match (concat (match-string 1)
16732 (format org-property-format
16733 (match-string 2) (match-string 3)))
16734 t t))
16735 (org-move-to-column column)))
16737 (defun org-set-autofill-regexps ()
16738 (interactive)
16739 ;; In the paragraph separator we include headlines, because filling
16740 ;; text in a line directly attached to a headline would otherwise
16741 ;; fill the headline as well.
16742 (org-set-local 'comment-start-skip "^#+[ \t]*")
16743 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
16744 ;; The paragraph starter includes hand-formatted lists.
16745 (org-set-local
16746 'paragraph-start
16747 (concat
16748 "\f" "\\|"
16749 "[ ]*$" "\\|"
16750 "\\*+ " "\\|"
16751 "[ \t]*#" "\\|"
16752 "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
16753 "[ \t]*[:|]" "\\|"
16754 "\\$\\$" "\\|"
16755 "\\\\\\(begin\\|end\\|[][]\\)"))
16756 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
16757 ;; But only if the user has not turned off tables or fixed-width regions
16758 (org-set-local
16759 'auto-fill-inhibit-regexp
16760 (concat "\\*+ \\|#\\+"
16761 "\\|[ \t]*" org-keyword-time-regexp
16762 (if (or org-enable-table-editor org-enable-fixed-width-editor)
16763 (concat
16764 "\\|[ \t]*["
16765 (if org-enable-table-editor "|" "")
16766 (if org-enable-fixed-width-editor ":" "")
16767 "]"))))
16768 ;; We use our own fill-paragraph function, to make sure that tables
16769 ;; and fixed-width regions are not wrapped. That function will pass
16770 ;; through to `fill-paragraph' when appropriate.
16771 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
16772 ; Adaptive filling: To get full control, first make sure that
16773 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
16774 (org-set-local 'adaptive-fill-regexp "\000")
16775 (org-set-local 'adaptive-fill-function
16776 'org-adaptive-fill-function)
16777 (org-set-local
16778 'align-mode-rules-list
16779 '((org-in-buffer-settings
16780 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
16781 (modes . '(org-mode))))))
16783 (defun org-fill-paragraph (&optional justify)
16784 "Re-align a table, pass through to fill-paragraph if no table."
16785 (let ((table-p (org-at-table-p))
16786 (table.el-p (org-at-table.el-p)))
16787 (cond ((and (equal (char-after (point-at-bol)) ?*)
16788 (save-excursion (goto-char (point-at-bol))
16789 (looking-at outline-regexp)))
16790 t) ; skip headlines
16791 (table.el-p t) ; skip table.el tables
16792 (table-p (org-table-align) t) ; align org-mode tables
16793 (t nil)))) ; call paragraph-fill
16795 ;; For reference, this is the default value of adaptive-fill-regexp
16796 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
16798 (defun org-adaptive-fill-function ()
16799 "Return a fill prefix for org-mode files.
16800 In particular, this makes sure hanging paragraphs for hand-formatted lists
16801 work correctly."
16802 (cond ((looking-at "#[ \t]+")
16803 (match-string 0))
16804 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
16805 (save-excursion
16806 (if (> (match-end 1) (+ (match-beginning 1)
16807 org-description-max-indent))
16808 (goto-char (+ (match-beginning 1) 5))
16809 (goto-char (match-end 0)))
16810 (make-string (current-column) ?\ )))
16811 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
16812 (save-excursion
16813 (goto-char (match-end 0))
16814 (make-string (current-column) ?\ )))
16815 (t nil)))
16817 ;;; Other stuff.
16819 (defun org-toggle-fixed-width-section (arg)
16820 "Toggle the fixed-width export.
16821 If there is no active region, the QUOTE keyword at the current headline is
16822 inserted or removed. When present, it causes the text between this headline
16823 and the next to be exported as fixed-width text, and unmodified.
16824 If there is an active region, this command adds or removes a colon as the
16825 first character of this line. If the first character of a line is a colon,
16826 this line is also exported in fixed-width font."
16827 (interactive "P")
16828 (let* ((cc 0)
16829 (regionp (org-region-active-p))
16830 (beg (if regionp (region-beginning) (point)))
16831 (end (if regionp (region-end)))
16832 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
16833 (case-fold-search nil)
16834 (re "[ \t]*\\(: \\)")
16835 off)
16836 (if regionp
16837 (save-excursion
16838 (goto-char beg)
16839 (setq cc (current-column))
16840 (beginning-of-line 1)
16841 (setq off (looking-at re))
16842 (while (> nlines 0)
16843 (setq nlines (1- nlines))
16844 (beginning-of-line 1)
16845 (cond
16846 (arg
16847 (org-move-to-column cc t)
16848 (insert ": \n")
16849 (forward-line -1))
16850 ((and off (looking-at re))
16851 (replace-match "" t t nil 1))
16852 ((not off) (org-move-to-column cc t) (insert ": ")))
16853 (forward-line 1)))
16854 (save-excursion
16855 (org-back-to-heading)
16856 (if (looking-at (concat outline-regexp
16857 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
16858 (replace-match "" t t nil 1)
16859 (if (looking-at outline-regexp)
16860 (progn
16861 (goto-char (match-end 0))
16862 (insert org-quote-string " "))))))))
16864 (defun org-reftex-citation ()
16865 "Use reftex-citation to insert a citation into the buffer.
16866 This looks for a line like
16868 #+BIBLIOGRAPHY: foo plain option:-d
16870 and derives from it that foo.bib is the bbliography file relevant
16871 for this document. It then installs the necessary environment for RefTeX
16872 to work in this buffer and calls `reftex-citation' to insert a citation
16873 into the buffer.
16875 Export of such citations to both LaTeX and HTML is handled by the contributed
16876 package org-exp-bibtex by Taru Karttunen."
16877 (interactive)
16878 (let ((reftex-docstruct-symbol 'rds)
16879 (reftex-cite-format "\\cite{%l}")
16880 rds bib)
16881 (save-excursion
16882 (save-restriction
16883 (widen)
16884 (let ((case-fold-search t)
16885 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
16886 (if (not (save-excursion
16887 (or (re-search-forward re nil t)
16888 (re-search-backward re nil t))))
16889 (error "No bibliography defined in file")
16890 (setq bib (concat (match-string 1) ".bib")
16891 rds (list (list 'bib bib)))))))
16892 (call-interactively 'reftex-citation)))
16894 ;;;; Functions extending outline functionality
16896 (defun org-beginning-of-line (&optional arg)
16897 "Go to the beginning of the current line. If that is invisible, continue
16898 to a visible line beginning. This makes the function of C-a more intuitive.
16899 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
16900 first attempt, and only move to after the tags when the cursor is already
16901 beyond the end of the headline."
16902 (interactive "P")
16903 (let ((pos (point))
16904 (special (if (consp org-special-ctrl-a/e)
16905 (car org-special-ctrl-a/e)
16906 org-special-ctrl-a/e))
16907 refpos)
16908 (if (org-bound-and-true-p line-move-visual)
16909 (beginning-of-visual-line 1)
16910 (beginning-of-line 1))
16911 (if (and arg (fboundp 'move-beginning-of-line))
16912 (call-interactively 'move-beginning-of-line)
16913 (if (bobp)
16915 (backward-char 1)
16916 (if (org-invisible-p)
16917 (while (and (not (bobp)) (org-invisible-p))
16918 (backward-char 1)
16919 (beginning-of-line 1))
16920 (forward-char 1))))
16921 (when special
16922 (cond
16923 ((and (looking-at org-complex-heading-regexp)
16924 (= (char-after (match-end 1)) ?\ ))
16925 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
16926 (point-at-eol)))
16927 (goto-char
16928 (if (eq special t)
16929 (cond ((> pos refpos) refpos)
16930 ((= pos (point)) refpos)
16931 (t (point)))
16932 (cond ((> pos (point)) (point))
16933 ((not (eq last-command this-command)) (point))
16934 (t refpos)))))
16935 ((org-at-item-p)
16936 (goto-char
16937 (if (eq special t)
16938 (cond ((> pos (match-end 4)) (match-end 4))
16939 ((= pos (point)) (match-end 4))
16940 (t (point)))
16941 (cond ((> pos (point)) (point))
16942 ((not (eq last-command this-command)) (point))
16943 (t (match-end 4))))))))
16944 (org-no-warnings
16945 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
16947 (defun org-end-of-line (&optional arg)
16948 "Go to the end of the line.
16949 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
16950 first attempt, and only move to after the tags when the cursor is already
16951 beyond the end of the headline."
16952 (interactive "P")
16953 (let ((special (if (consp org-special-ctrl-a/e)
16954 (cdr org-special-ctrl-a/e)
16955 org-special-ctrl-a/e)))
16956 (if (or (not special)
16957 (not (org-on-heading-p))
16958 arg)
16959 (call-interactively
16960 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
16961 ((fboundp 'move-end-of-line) 'move-end-of-line)
16962 (t 'end-of-line)))
16963 (let ((pos (point)))
16964 (beginning-of-line 1)
16965 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
16966 (if (eq special t)
16967 (if (or (< pos (match-beginning 1))
16968 (= pos (match-end 0)))
16969 (goto-char (match-beginning 1))
16970 (goto-char (match-end 0)))
16971 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
16972 (goto-char (match-end 0))
16973 (goto-char (match-beginning 1))))
16974 (call-interactively (if (fboundp 'move-end-of-line)
16975 'move-end-of-line
16976 'end-of-line)))))
16977 (org-no-warnings
16978 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
16980 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
16981 (define-key org-mode-map "\C-e" 'org-end-of-line)
16982 (define-key org-mode-map [home] 'org-beginning-of-line)
16983 (define-key org-mode-map [end] 'org-end-of-line)
16985 (defun org-backward-sentence (&optional arg)
16986 "Go to beginning of sentence, or beginning of table field.
16987 This will call `backward-sentence' or `org-table-beginning-of-field',
16988 depending on context."
16989 (interactive "P")
16990 (cond
16991 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
16992 (t (call-interactively 'backward-sentence))))
16994 (defun org-forward-sentence (&optional arg)
16995 "Go to end of sentence, or end of table field.
16996 This will call `forward-sentence' or `org-table-end-of-field',
16997 depending on context."
16998 (interactive "P")
16999 (cond
17000 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
17001 (t (call-interactively 'forward-sentence))))
17003 (define-key org-mode-map "\M-a" 'org-backward-sentence)
17004 (define-key org-mode-map "\M-e" 'org-forward-sentence)
17006 (defun org-kill-line (&optional arg)
17007 "Kill line, to tags or end of line."
17008 (interactive "P")
17009 (cond
17010 ((or (not org-special-ctrl-k)
17011 (bolp)
17012 (not (org-on-heading-p)))
17013 (call-interactively 'kill-line))
17014 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
17015 (kill-region (point) (match-beginning 1))
17016 (org-set-tags nil t))
17017 (t (kill-region (point) (point-at-eol)))))
17019 (define-key org-mode-map "\C-k" 'org-kill-line)
17021 (defun org-yank (&optional arg)
17022 "Yank. If the kill is a subtree, treat it specially.
17023 This command will look at the current kill and check if is a single
17024 subtree, or a series of subtrees[1]. If it passes the test, and if the
17025 cursor is at the beginning of a line or after the stars of a currently
17026 empty headline, then the yank is handled specially. How exactly depends
17027 on the value of the following variables, both set by default.
17029 org-yank-folded-subtrees
17030 When set, the subtree(s) will be folded after insertion, but only
17031 if doing so would now swallow text after the yanked text.
17033 org-yank-adjusted-subtrees
17034 When set, the subtree will be promoted or demoted in order to
17035 fit into the local outline tree structure, which means that the level
17036 will be adjusted so that it becomes the smaller one of the two
17037 *visible* surrounding headings.
17039 Any prefix to this command will cause `yank' to be called directly with
17040 no special treatment. In particular, a simple `C-u' prefix will just
17041 plainly yank the text as it is.
17043 \[1] The test checks if the first non-white line is a heading
17044 and if there are no other headings with fewer stars."
17045 (interactive "P")
17046 (org-yank-generic 'yank arg))
17048 (defun org-yank-generic (command arg)
17049 "Perform some yank-like command.
17051 This function implements the behavior described in the `org-yank'
17052 documentation. However, it has been generalized to work for any
17053 interactive command with similar behavior."
17055 ;; pretend to be command COMMAND
17056 (setq this-command command)
17058 (if arg
17059 (call-interactively command)
17061 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
17062 (and (org-kill-is-subtree-p)
17063 (or (bolp)
17064 (and (looking-at "[ \t]*$")
17065 (string-match
17066 "\\`\\*+\\'"
17067 (buffer-substring (point-at-bol) (point)))))))
17068 swallowp)
17069 (cond
17070 ((and subtreep org-yank-folded-subtrees)
17071 (let ((beg (point))
17072 end)
17073 (if (and subtreep org-yank-adjusted-subtrees)
17074 (org-paste-subtree nil nil 'for-yank)
17075 (call-interactively command))
17077 (setq end (point))
17078 (goto-char beg)
17079 (when (and (bolp) subtreep
17080 (not (setq swallowp
17081 (org-yank-folding-would-swallow-text beg end))))
17082 (or (looking-at outline-regexp)
17083 (re-search-forward (concat "^" outline-regexp) end t))
17084 (while (and (< (point) end) (looking-at outline-regexp))
17085 (hide-subtree)
17086 (org-cycle-show-empty-lines 'folded)
17087 (condition-case nil
17088 (outline-forward-same-level 1)
17089 (error (goto-char end)))))
17090 (when swallowp
17091 (message
17092 "Inserted text not folded because that would swallow text"))
17094 (goto-char end)
17095 (skip-chars-forward " \t\n\r")
17096 (beginning-of-line 1)
17097 (push-mark beg 'nomsg)))
17098 ((and subtreep org-yank-adjusted-subtrees)
17099 (let ((beg (point-at-bol)))
17100 (org-paste-subtree nil nil 'for-yank)
17101 (push-mark beg 'nomsg)))
17103 (call-interactively command))))))
17105 (defun org-yank-folding-would-swallow-text (beg end)
17106 "Would hide-subtree at BEG swallow any text after END?"
17107 (let (level)
17108 (save-excursion
17109 (goto-char beg)
17110 (when (or (looking-at outline-regexp)
17111 (re-search-forward (concat "^" outline-regexp) end t))
17112 (setq level (org-outline-level)))
17113 (goto-char end)
17114 (skip-chars-forward " \t\r\n\v\f")
17115 (if (or (eobp)
17116 (and (bolp) (looking-at org-outline-regexp)
17117 (<= (org-outline-level) level)))
17118 nil ; Nothing would be swallowed
17119 t)))) ; something would swallow
17121 (define-key org-mode-map "\C-y" 'org-yank)
17123 (defun org-invisible-p ()
17124 "Check if point is at a character currently not visible."
17125 ;; Early versions of noutline don't have `outline-invisible-p'.
17126 (if (fboundp 'outline-invisible-p)
17127 (outline-invisible-p)
17128 (get-char-property (point) 'invisible)))
17130 (defun org-invisible-p2 ()
17131 "Check if point is at a character currently not visible."
17132 (save-excursion
17133 (if (and (eolp) (not (bobp))) (backward-char 1))
17134 ;; Early versions of noutline don't have `outline-invisible-p'.
17135 (if (fboundp 'outline-invisible-p)
17136 (outline-invisible-p)
17137 (get-char-property (point) 'invisible))))
17139 (defun org-back-to-heading (&optional invisible-ok)
17140 "Call `outline-back-to-heading', but provide a better error message."
17141 (condition-case nil
17142 (outline-back-to-heading invisible-ok)
17143 (error (error "Before first headline at position %d in buffer %s"
17144 (point) (current-buffer)))))
17146 (defun org-before-first-heading-p ()
17147 "Before first heading?"
17148 (save-excursion
17149 (null (re-search-backward "^\\*+ " nil t))))
17151 (defun org-on-heading-p (&optional ignored)
17152 (outline-on-heading-p t))
17153 (defun org-at-heading-p (&optional ignored)
17154 (outline-on-heading-p t))
17156 (defun org-at-heading-or-item-p ()
17157 (or (org-on-heading-p) (org-at-item-p)))
17159 (defun org-on-target-p ()
17160 (or (org-in-regexp org-radio-target-regexp)
17161 (org-in-regexp org-target-regexp)))
17163 (defun org-up-heading-all (arg)
17164 "Move to the heading line of which the present line is a subheading.
17165 This function considers both visible and invisible heading lines.
17166 With argument, move up ARG levels."
17167 (if (fboundp 'outline-up-heading-all)
17168 (outline-up-heading-all arg) ; emacs 21 version of outline.el
17169 (outline-up-heading arg t))) ; emacs 22 version of outline.el
17171 (defun org-up-heading-safe ()
17172 "Move to the heading line of which the present line is a subheading.
17173 This version will not throw an error. It will return the level of the
17174 headline found, or nil if no higher level is found.
17176 Also, this function will be a lot faster than `outline-up-heading',
17177 because it relies on stars being the outline starters. This can really
17178 make a significant difference in outlines with very many siblings."
17179 (let (start-level re)
17180 (org-back-to-heading t)
17181 (setq start-level (funcall outline-level))
17182 (if (equal start-level 1)
17184 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
17185 (if (re-search-backward re nil t)
17186 (funcall outline-level)))))
17188 (defun org-first-sibling-p ()
17189 "Is this heading the first child of its parents?"
17190 (interactive)
17191 (let ((re (concat "^" outline-regexp))
17192 level l)
17193 (unless (org-at-heading-p t)
17194 (error "Not at a heading"))
17195 (setq level (funcall outline-level))
17196 (save-excursion
17197 (if (not (re-search-backward re nil t))
17199 (setq l (funcall outline-level))
17200 (< l level)))))
17202 (defun org-goto-sibling (&optional previous)
17203 "Goto the next sibling, even if it is invisible.
17204 When PREVIOUS is set, go to the previous sibling instead. Returns t
17205 when a sibling was found. When none is found, return nil and don't
17206 move point."
17207 (let ((fun (if previous 're-search-backward 're-search-forward))
17208 (pos (point))
17209 (re (concat "^" outline-regexp))
17210 level l)
17211 (when (condition-case nil (org-back-to-heading t) (error nil))
17212 (setq level (funcall outline-level))
17213 (catch 'exit
17214 (or previous (forward-char 1))
17215 (while (funcall fun re nil t)
17216 (setq l (funcall outline-level))
17217 (when (< l level) (goto-char pos) (throw 'exit nil))
17218 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
17219 (goto-char pos)
17220 nil))))
17222 (defun org-show-siblings ()
17223 "Show all siblings of the current headline."
17224 (save-excursion
17225 (while (org-goto-sibling) (org-flag-heading nil)))
17226 (save-excursion
17227 (while (org-goto-sibling 'previous)
17228 (org-flag-heading nil))))
17230 (defun org-show-hidden-entry ()
17231 "Show an entry where even the heading is hidden."
17232 (save-excursion
17233 (org-show-entry)))
17235 (defun org-flag-heading (flag &optional entry)
17236 "Flag the current heading. FLAG non-nil means make invisible.
17237 When ENTRY is non-nil, show the entire entry."
17238 (save-excursion
17239 (org-back-to-heading t)
17240 ;; Check if we should show the entire entry
17241 (if entry
17242 (progn
17243 (org-show-entry)
17244 (save-excursion
17245 (and (outline-next-heading)
17246 (org-flag-heading nil))))
17247 (outline-flag-region (max (point-min) (1- (point)))
17248 (save-excursion (outline-end-of-heading) (point))
17249 flag))))
17251 (defun org-get-next-sibling ()
17252 "Move to next heading of the same level, and return point.
17253 If there is no such heading, return nil.
17254 This is like outline-next-sibling, but invisible headings are ok."
17255 (let ((level (funcall outline-level)))
17256 (outline-next-heading)
17257 (while (and (not (eobp)) (> (funcall outline-level) level))
17258 (outline-next-heading))
17259 (if (or (eobp) (< (funcall outline-level) level))
17261 (point))))
17263 (defun org-get-last-sibling ()
17264 "Move to previous heading of the same level, and return point.
17265 If there is no such heading, return nil."
17266 (let ((opoint (point))
17267 (level (funcall outline-level)))
17268 (outline-previous-heading)
17269 (when (and (/= (point) opoint) (outline-on-heading-p t))
17270 (while (and (> (funcall outline-level) level)
17271 (not (bobp)))
17272 (outline-previous-heading))
17273 (if (< (funcall outline-level) level)
17275 (point)))))
17277 (defun org-end-of-subtree (&optional invisible-OK to-heading)
17278 ;; This contains an exact copy of the original function, but it uses
17279 ;; `org-back-to-heading', to make it work also in invisible
17280 ;; trees. And is uses an invisible-OK argument.
17281 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
17282 ;; Furthermore, when used inside Org, finding the end of a large subtree
17283 ;; with many children and grandchildren etc, this can be much faster
17284 ;; than the outline version.
17285 (org-back-to-heading invisible-OK)
17286 (let ((first t)
17287 (level (funcall outline-level)))
17288 (if (and (org-mode-p) (< level 1000))
17289 ;; A true heading (not a plain list item), in Org-mode
17290 ;; This means we can easily find the end by looking
17291 ;; only for the right number of stars. Using a regexp to do
17292 ;; this is so much faster than using a Lisp loop.
17293 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
17294 (forward-char 1)
17295 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
17296 ;; something else, do it the slow way
17297 (while (and (not (eobp))
17298 (or first (> (funcall outline-level) level)))
17299 (setq first nil)
17300 (outline-next-heading)))
17301 (unless to-heading
17302 (if (memq (preceding-char) '(?\n ?\^M))
17303 (progn
17304 ;; Go to end of line before heading
17305 (forward-char -1)
17306 (if (memq (preceding-char) '(?\n ?\^M))
17307 ;; leave blank line before heading
17308 (forward-char -1))))))
17309 (point))
17311 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
17312 "Use Org version in org-mode, for dramatic speed-up."
17313 (if (eq major-mode 'org-mode)
17314 (progn
17315 (org-end-of-subtree nil t)
17316 (unless (eobp) (backward-char 1)))
17317 ad-do-it))
17319 (defun org-forward-same-level (arg &optional invisible-ok)
17320 "Move forward to the arg'th subheading at same level as this one.
17321 Stop at the first and last subheadings of a superior heading."
17322 (interactive "p")
17323 (org-back-to-heading invisible-ok)
17324 (org-on-heading-p)
17325 (let* ((level (- (match-end 0) (match-beginning 0) 1))
17326 (re (format "^\\*\\{1,%d\\} " level))
17328 (forward-char 1)
17329 (while (> arg 0)
17330 (while (and (re-search-forward re nil 'move)
17331 (setq l (- (match-end 0) (match-beginning 0) 1))
17332 (= l level)
17333 (not invisible-ok)
17334 (org-invisible-p))
17335 (if (< l level) (setq arg 1)))
17336 (setq arg (1- arg)))
17337 (beginning-of-line 1)))
17339 (defun org-backward-same-level (arg &optional invisible-ok)
17340 "Move backward to the arg'th subheading at same level as this one.
17341 Stop at the first and last subheadings of a superior heading."
17342 (interactive "p")
17343 (org-back-to-heading)
17344 (org-on-heading-p)
17345 (let* ((level (- (match-end 0) (match-beginning 0) 1))
17346 (re (format "^\\*\\{1,%d\\} " level))
17348 (while (> arg 0)
17349 (while (and (re-search-backward re nil 'move)
17350 (setq l (- (match-end 0) (match-beginning 0) 1))
17351 (= l level)
17352 (not invisible-ok)
17353 (org-invisible-p))
17354 (if (< l level) (setq arg 1)))
17355 (setq arg (1- arg)))))
17357 (defun org-show-subtree ()
17358 "Show everything after this heading at deeper levels."
17359 (outline-flag-region
17360 (point)
17361 (save-excursion
17362 (org-end-of-subtree t t))
17363 nil))
17365 (defun org-show-entry ()
17366 "Show the body directly following this heading.
17367 Show the heading too, if it is currently invisible."
17368 (interactive)
17369 (save-excursion
17370 (condition-case nil
17371 (progn
17372 (org-back-to-heading t)
17373 (outline-flag-region
17374 (max (point-min) (1- (point)))
17375 (save-excursion
17376 (if (re-search-forward
17377 (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
17378 (match-beginning 1)
17379 (point-max)))
17380 nil)
17381 (org-cycle-hide-drawers 'children))
17382 (error nil))))
17384 (defun org-make-options-regexp (kwds &optional extra)
17385 "Make a regular expression for keyword lines."
17386 (concat
17388 "#?[ \t]*\\+\\("
17389 (mapconcat 'regexp-quote kwds "\\|")
17390 (if extra (concat "\\|" extra))
17391 "\\):[ \t]*"
17392 "\\(.*\\)"))
17394 ;; Make isearch reveal the necessary context
17395 (defun org-isearch-end ()
17396 "Reveal context after isearch exits."
17397 (when isearch-success ; only if search was successful
17398 (if (featurep 'xemacs)
17399 ;; Under XEmacs, the hook is run in the correct place,
17400 ;; we directly show the context.
17401 (org-show-context 'isearch)
17402 ;; In Emacs the hook runs *before* restoring the overlays.
17403 ;; So we have to use a one-time post-command-hook to do this.
17404 ;; (Emacs 22 has a special variable, see function `org-mode')
17405 (unless (and (boundp 'isearch-mode-end-hook-quit)
17406 isearch-mode-end-hook-quit)
17407 ;; Only when the isearch was not quitted.
17408 (org-add-hook 'post-command-hook 'org-isearch-post-command
17409 'append 'local)))))
17411 (defun org-isearch-post-command ()
17412 "Remove self from hook, and show context."
17413 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
17414 (org-show-context 'isearch))
17417 ;;;; Integration with and fixes for other packages
17419 ;;; Imenu support
17421 (defvar org-imenu-markers nil
17422 "All markers currently used by Imenu.")
17423 (make-variable-buffer-local 'org-imenu-markers)
17425 (defun org-imenu-new-marker (&optional pos)
17426 "Return a new marker for use by Imenu, and remember the marker."
17427 (let ((m (make-marker)))
17428 (move-marker m (or pos (point)))
17429 (push m org-imenu-markers)
17432 (defun org-imenu-get-tree ()
17433 "Produce the index for Imenu."
17434 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
17435 (setq org-imenu-markers nil)
17436 (let* ((n org-imenu-depth)
17437 (re (concat "^" outline-regexp))
17438 (subs (make-vector (1+ n) nil))
17439 (last-level 0)
17440 m level head)
17441 (save-excursion
17442 (save-restriction
17443 (widen)
17444 (goto-char (point-max))
17445 (while (re-search-backward re nil t)
17446 (setq level (org-reduced-level (funcall outline-level)))
17447 (when (<= level n)
17448 (looking-at org-complex-heading-regexp)
17449 (setq head (org-link-display-format
17450 (org-match-string-no-properties 4))
17451 m (org-imenu-new-marker))
17452 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
17453 (if (>= level last-level)
17454 (push (cons head m) (aref subs level))
17455 (push (cons head (aref subs (1+ level))) (aref subs level))
17456 (loop for i from (1+ level) to n do (aset subs i nil)))
17457 (setq last-level level)))))
17458 (aref subs 1)))
17460 (eval-after-load "imenu"
17461 '(progn
17462 (add-hook 'imenu-after-jump-hook
17463 (lambda ()
17464 (if (eq major-mode 'org-mode)
17465 (org-show-context 'org-goto))))))
17467 (defun org-link-display-format (link)
17468 "Replace a link with either the description, or the link target
17469 if no description is present"
17470 (save-match-data
17471 (if (string-match org-bracket-link-analytic-regexp link)
17472 (replace-match (or (match-string 5 link)
17473 (concat (match-string 1 link)
17474 (match-string 3 link)))
17475 nil nil link)
17476 link)))
17478 ;; Speedbar support
17480 (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
17481 "Overlay marking the agenda restriction line in speedbar.")
17482 (org-overlay-put org-speedbar-restriction-lock-overlay
17483 'face 'org-agenda-restriction-lock)
17484 (org-overlay-put org-speedbar-restriction-lock-overlay
17485 'help-echo "Agendas are currently limited to this item.")
17486 (org-detach-overlay org-speedbar-restriction-lock-overlay)
17488 (defun org-speedbar-set-agenda-restriction ()
17489 "Restrict future agenda commands to the location at point in speedbar.
17490 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
17491 (interactive)
17492 (require 'org-agenda)
17493 (let (p m tp np dir txt)
17494 (cond
17495 ((setq p (text-property-any (point-at-bol) (point-at-eol)
17496 'org-imenu t))
17497 (setq m (get-text-property p 'org-imenu-marker))
17498 (save-excursion
17499 (save-restriction
17500 (set-buffer (marker-buffer m))
17501 (goto-char m)
17502 (org-agenda-set-restriction-lock 'subtree))))
17503 ((setq p (text-property-any (point-at-bol) (point-at-eol)
17504 'speedbar-function 'speedbar-find-file))
17505 (setq tp (previous-single-property-change
17506 (1+ p) 'speedbar-function)
17507 np (next-single-property-change
17508 tp 'speedbar-function)
17509 dir (speedbar-line-directory)
17510 txt (buffer-substring-no-properties (or tp (point-min))
17511 (or np (point-max))))
17512 (save-excursion
17513 (save-restriction
17514 (set-buffer (find-file-noselect
17515 (let ((default-directory dir))
17516 (expand-file-name txt))))
17517 (unless (org-mode-p)
17518 (error "Cannot restrict to non-Org-mode file"))
17519 (org-agenda-set-restriction-lock 'file))))
17520 (t (error "Don't know how to restrict Org-mode's agenda")))
17521 (org-move-overlay org-speedbar-restriction-lock-overlay
17522 (point-at-bol) (point-at-eol))
17523 (setq current-prefix-arg nil)
17524 (org-agenda-maybe-redo)))
17526 (eval-after-load "speedbar"
17527 '(progn
17528 (speedbar-add-supported-extension ".org")
17529 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
17530 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
17531 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
17532 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
17533 (add-hook 'speedbar-visiting-tag-hook
17534 (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
17537 ;;; Fixes and Hacks for problems with other packages
17539 ;; Make flyspell not check words in links, to not mess up our keymap
17540 (defun org-mode-flyspell-verify ()
17541 "Don't let flyspell put overlays at active buttons."
17542 (and (not (get-text-property (point) 'keymap))
17543 (not (get-text-property (point) 'org-no-flyspell))))
17545 (defun org-remove-flyspell-overlays-in (beg end)
17546 "Remove flyspell overlays in region."
17547 (and (org-bound-and-true-p flyspell-mode)
17548 (fboundp 'flyspell-delete-region-overlays)
17549 (flyspell-delete-region-overlays beg end))
17550 (add-text-properties beg end '(org-no-flyspell t)))
17552 ;; Make `bookmark-jump' shows the jump location if it was hidden.
17553 (eval-after-load "bookmark"
17554 '(if (boundp 'bookmark-after-jump-hook)
17555 ;; We can use the hook
17556 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
17557 ;; Hook not available, use advice
17558 (defadvice bookmark-jump (after org-make-visible activate)
17559 "Make the position visible."
17560 (org-bookmark-jump-unhide))))
17562 ;; Make sure saveplace shows the location if it was hidden
17563 (eval-after-load "saveplace"
17564 '(defadvice save-place-find-file-hook (after org-make-visible activate)
17565 "Make the position visible."
17566 (org-bookmark-jump-unhide)))
17568 ;; Make sure ecb shows the location if it was hidden
17569 (eval-after-load "ecb"
17570 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
17571 "Make hierarchy visible when jumping into location from ECB tree buffer."
17572 (if (eq major-mode 'org-mode)
17573 (org-show-context))))
17575 (defun org-bookmark-jump-unhide ()
17576 "Unhide the current position, to show the bookmark location."
17577 (and (org-mode-p)
17578 (or (org-invisible-p)
17579 (save-excursion (goto-char (max (point-min) (1- (point))))
17580 (org-invisible-p)))
17581 (org-show-context 'bookmark-jump)))
17583 ;; Make session.el ignore our circular variable
17584 (eval-after-load "session"
17585 '(add-to-list 'session-globals-exclude 'org-mark-ring))
17587 ;;;; Experimental code
17589 (defun org-closed-in-range ()
17590 "Sparse tree of items closed in a certain time range.
17591 Still experimental, may disappear in the future."
17592 (interactive)
17593 ;; Get the time interval from the user.
17594 (let* ((time1 (org-float-time
17595 (org-read-date nil 'to-time nil "Starting date: ")))
17596 (time2 (org-float-time
17597 (org-read-date nil 'to-time nil "End date:")))
17598 ;; callback function
17599 (callback (lambda ()
17600 (let ((time
17601 (org-float-time
17602 (apply 'encode-time
17603 (org-parse-time-string
17604 (match-string 1))))))
17605 ;; check if time in interval
17606 (and (>= time time1) (<= time time2))))))
17607 ;; make tree, check each match with the callback
17608 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
17610 ;;;; Finish up
17612 (provide 'org)
17614 (run-hooks 'org-load-hook)
17616 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
17618 ;;; org.el ends here