Remove redundant line from org-src.el
[rgr-org-mode.git] / lisp / org.el
blob2a227b98e013730523785c1d67abd3fc35db358e
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 interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
224 (const :tag "C invoice Help manage client invoices in Org-mode" org-invoice)
226 (const :tag "C jira Add a jira:ticket protocol to Org-mode" org-jira)
227 (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
228 (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
229 (const :tag "C man: Support for links to manpages in Org-mode" org-man)
230 (const :tag "C mtags: Support for muse-like tags" org-mtags)
231 (const :tag "C panel: Simple routines for us with bad memory" org-panel)
232 (const :tag "C R: Computation using the R language" org-R)
233 (const :tag "C registry: A registry for Org-mode links" org-registry)
234 (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
235 (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
236 (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
237 (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
238 (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
239 (const :tag "C track: Keep up with Org-mode development" org-track)
240 (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
242 (defcustom org-support-shift-select nil
243 "Non-nil means, make shift-cursor commands select text when possible.
245 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
246 selecting a region, or enlarge thusly regions started in this way.
247 In Org-mode, in special contexts, these same keys are used for other
248 purposes, important enough to compete with shift selection. Org tries
249 to balance these needs by supporting `shift-select-mode' outside these
250 special contexts, under control of this variable.
252 The default of this variable is nil, to avoid confusing behavior. Shifted
253 cursor keys will then execute Org commands in the following contexts:
254 - on a headline, changing TODO state (left/right) and priority (up/down)
255 - on a time stamp, changing the time
256 - in a plain list item, changing the bullet type
257 - in a property definition line, switching between allowed values
258 - in the BEGIN line of a clock table (changing the time block).
259 Outside these contexts, the commands will throw an error.
261 When this variable is t and the cursor is not in a special context,
262 Org-mode will support shift-selection for making and enlarging regions.
263 To make this more effective, the bullet cycling will no longer happen
264 anywhere in an item line, but only if the cursor is exactly on the bullet.
266 If you set this variable to the symbol `always', then the keys
267 will not be special in headlines, property lines, and item lines, to make
268 shift selection work there as well. If this is what you want, you can
269 use the following alternative commands: `C-c C-t' and `C-c ,' to
270 change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
271 TODO sets, `C-c -' to cycle item bullet types, and properties can be
272 edited by hand or in column view.
274 However, when the cursor is on a timestamp, shift-cursor commands
275 will still edit the time stamp - this is just too good to give up.
277 XEmacs user should have this variable set to nil, because shift-select-mode
278 is Emacs 23 only."
279 :group 'org
280 :type '(choice
281 (const :tag "Never" nil)
282 (const :tag "When outside special context" t)
283 (const :tag "Everywhere except timestamps" always)))
285 (defgroup org-startup nil
286 "Options concerning startup of Org-mode."
287 :tag "Org Startup"
288 :group 'org)
290 (defcustom org-startup-folded t
291 "Non-nil means, entering Org-mode will switch to OVERVIEW.
292 This can also be configured on a per-file basis by adding one of
293 the following lines anywhere in the buffer:
295 #+STARTUP: fold (or `overview', this is equivalent)
296 #+STARTUP: nofold (or `showall', this is equivalent)
297 #+STARTUP: content
298 #+STARTUP: showeverything"
299 :group 'org-startup
300 :type '(choice
301 (const :tag "nofold: show all" nil)
302 (const :tag "fold: overview" t)
303 (const :tag "content: all headlines" content)
304 (const :tag "show everything, even drawers" showeverything)))
306 (defcustom org-startup-truncated t
307 "Non-nil means, entering Org-mode will set `truncate-lines'.
308 This is useful since some lines containing links can be very long and
309 uninteresting. Also tables look terrible when wrapped."
310 :group 'org-startup
311 :type 'boolean)
313 (defcustom org-startup-indented nil
314 "Non-nil means, turn on `org-indent-mode' on startup.
315 This can also be configured on a per-file basis by adding one of
316 the following lines anywhere in the buffer:
318 #+STARTUP: indent
319 #+STARTUP: noindent"
320 :group 'org-structure
321 :type '(choice
322 (const :tag "Not" nil)
323 (const :tag "Globally (slow on startup in large files)" t)))
325 (defcustom org-startup-align-all-tables nil
326 "Non-nil means, align all tables when visiting a file.
327 This is useful when the column width in tables is forced with <N> cookies
328 in table fields. Such tables will look correct only after the first re-align.
329 This can also be configured on a per-file basis by adding one of
330 the following lines anywhere in the buffer:
331 #+STARTUP: align
332 #+STARTUP: noalign"
333 :group 'org-startup
334 :type 'boolean)
336 (defcustom org-insert-mode-line-in-empty-file nil
337 "Non-nil means insert the first line setting Org-mode in empty files.
338 When the function `org-mode' is called interactively in an empty file, this
339 normally means that the file name does not automatically trigger Org-mode.
340 To ensure that the file will always be in Org-mode in the future, a
341 line enforcing Org-mode will be inserted into the buffer, if this option
342 has been set."
343 :group 'org-startup
344 :type 'boolean)
346 (defcustom org-replace-disputed-keys nil
347 "Non-nil means use alternative key bindings for some keys.
348 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
349 These keys are also used by other packages like shift-selection-mode'
350 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
351 If you want to use Org-mode together with one of these other modes,
352 or more generally if you would like to move some Org-mode commands to
353 other keys, set this variable and configure the keys with the variable
354 `org-disputed-keys'.
356 This option is only relevant at load-time of Org-mode, and must be set
357 *before* org.el is loaded. Changing it requires a restart of Emacs to
358 become effective."
359 :group 'org-startup
360 :type 'boolean)
362 (defcustom org-use-extra-keys nil
363 "Non-nil means use extra key sequence definitions for certain
364 commands. This happens automatically if you run XEmacs or if
365 window-system is nil. This variable lets you do the same
366 manually. You must set it before loading org.
368 Example: on Carbon Emacs 22 running graphically, with an external
369 keyboard on a Powerbook, the default way of setting M-left might
370 not work for either Alt or ESC. Setting this variable will make
371 it work for ESC."
372 :group 'org-startup
373 :type 'boolean)
375 (if (fboundp 'defvaralias)
376 (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
378 (defcustom org-disputed-keys
379 '(([(shift up)] . [(meta p)])
380 ([(shift down)] . [(meta n)])
381 ([(shift left)] . [(meta -)])
382 ([(shift right)] . [(meta +)])
383 ([(control shift right)] . [(meta shift +)])
384 ([(control shift left)] . [(meta shift -)]))
385 "Keys for which Org-mode and other modes compete.
386 This is an alist, cars are the default keys, second element specifies
387 the alternative to use when `org-replace-disputed-keys' is t.
389 Keys can be specified in any syntax supported by `define-key'.
390 The value of this option takes effect only at Org-mode's startup,
391 therefore you'll have to restart Emacs to apply it after changing."
392 :group 'org-startup
393 :type 'alist)
395 (defun org-key (key)
396 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
397 Or return the original if not disputed."
398 (if org-replace-disputed-keys
399 (let* ((nkey (key-description key))
400 (x (org-find-if (lambda (x)
401 (equal (key-description (car x)) nkey))
402 org-disputed-keys)))
403 (if x (cdr x) key))
404 key))
406 (defun org-find-if (predicate seq)
407 (catch 'exit
408 (while seq
409 (if (funcall predicate (car seq))
410 (throw 'exit (car seq))
411 (pop seq)))))
413 (defun org-defkey (keymap key def)
414 "Define a key, possibly translated, as returned by `org-key'."
415 (define-key keymap (org-key key) def))
417 (defcustom org-ellipsis nil
418 "The ellipsis to use in the Org-mode outline.
419 When nil, just use the standard three dots. When a string, use that instead,
420 When a face, use the standard 3 dots, but with the specified face.
421 The change affects only Org-mode (which will then use its own display table).
422 Changing this requires executing `M-x org-mode' in a buffer to become
423 effective."
424 :group 'org-startup
425 :type '(choice (const :tag "Default" nil)
426 (face :tag "Face" :value org-warning)
427 (string :tag "String" :value "...#")))
429 (defvar org-display-table nil
430 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
432 (defgroup org-keywords nil
433 "Keywords in Org-mode."
434 :tag "Org Keywords"
435 :group 'org)
437 (defcustom org-deadline-string "DEADLINE:"
438 "String to mark deadline entries.
439 A deadline is this string, followed by a time stamp. Should be a word,
440 terminated by a colon. You can insert a schedule keyword and
441 a timestamp with \\[org-deadline].
442 Changes become only effective after restarting Emacs."
443 :group 'org-keywords
444 :type 'string)
446 (defcustom org-scheduled-string "SCHEDULED:"
447 "String to mark scheduled TODO entries.
448 A schedule is this string, followed by a time stamp. Should be a word,
449 terminated by a colon. You can insert a schedule keyword and
450 a timestamp with \\[org-schedule].
451 Changes become only effective after restarting Emacs."
452 :group 'org-keywords
453 :type 'string)
455 (defcustom org-closed-string "CLOSED:"
456 "String used as the prefix for timestamps logging closing a TODO entry."
457 :group 'org-keywords
458 :type 'string)
460 (defcustom org-clock-string "CLOCK:"
461 "String used as prefix for timestamps clocking work hours on an item."
462 :group 'org-keywords
463 :type 'string)
465 (defcustom org-comment-string "COMMENT"
466 "Entries starting with this keyword will never be exported.
467 An entry can be toggled between COMMENT and normal with
468 \\[org-toggle-comment].
469 Changes become only effective after restarting Emacs."
470 :group 'org-keywords
471 :type 'string)
473 (defcustom org-quote-string "QUOTE"
474 "Entries starting with this keyword will be exported in fixed-width font.
475 Quoting applies only to the text in the entry following the headline, and does
476 not extend beyond the next headline, even if that is lower level.
477 An entry can be toggled between QUOTE and normal with
478 \\[org-toggle-fixed-width-section]."
479 :group 'org-keywords
480 :type 'string)
482 (defconst org-repeat-re
483 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
484 "Regular expression for specifying repeated events.
485 After a match, group 1 contains the repeat expression.")
487 (defgroup org-structure nil
488 "Options concerning the general structure of Org-mode files."
489 :tag "Org Structure"
490 :group 'org)
492 (defgroup org-reveal-location nil
493 "Options about how to make context of a location visible."
494 :tag "Org Reveal Location"
495 :group 'org-structure)
497 (defconst org-context-choice
498 '(choice
499 (const :tag "Always" t)
500 (const :tag "Never" nil)
501 (repeat :greedy t :tag "Individual contexts"
502 (cons
503 (choice :tag "Context"
504 (const agenda)
505 (const org-goto)
506 (const occur-tree)
507 (const tags-tree)
508 (const link-search)
509 (const mark-goto)
510 (const bookmark-jump)
511 (const isearch)
512 (const default))
513 (boolean))))
514 "Contexts for the reveal options.")
516 (defcustom org-show-hierarchy-above '((default . t))
517 "Non-nil means, show full hierarchy when revealing a location.
518 Org-mode often shows locations in an org-mode file which might have
519 been invisible before. When this is set, the hierarchy of headings
520 above the exposed location is shown.
521 Turning this off for example for sparse trees makes them very compact.
522 Instead of t, this can also be an alist specifying this option for different
523 contexts. Valid contexts are
524 agenda when exposing an entry from the agenda
525 org-goto when using the command `org-goto' on key C-c C-j
526 occur-tree when using the command `org-occur' on key C-c /
527 tags-tree when constructing a sparse tree based on tags matches
528 link-search when exposing search matches associated with a link
529 mark-goto when exposing the jump goal of a mark
530 bookmark-jump when exposing a bookmark location
531 isearch when exiting from an incremental search
532 default default for all contexts not set explicitly"
533 :group 'org-reveal-location
534 :type org-context-choice)
536 (defcustom org-show-following-heading '((default . nil))
537 "Non-nil means, show following heading when revealing a location.
538 Org-mode often shows locations in an org-mode file which might have
539 been invisible before. When this is set, the heading following the
540 match is shown.
541 Turning this off for example for sparse trees makes them very compact,
542 but makes it harder to edit the location of the match. In such a case,
543 use the command \\[org-reveal] to show more context.
544 Instead of t, this can also be an alist specifying this option for different
545 contexts. See `org-show-hierarchy-above' for valid contexts."
546 :group 'org-reveal-location
547 :type org-context-choice)
549 (defcustom org-show-siblings '((default . nil) (isearch t))
550 "Non-nil means, show all sibling heading when revealing a location.
551 Org-mode often shows locations in an org-mode file which might have
552 been invisible before. When this is set, the sibling of the current entry
553 heading are all made visible. If `org-show-hierarchy-above' is t,
554 the same happens on each level of the hierarchy above the current entry.
556 By default this is on for the isearch context, off for all other contexts.
557 Turning this off for example for sparse trees makes them very compact,
558 but makes it harder to edit the location of the match. In such a case,
559 use the command \\[org-reveal] to show more context.
560 Instead of t, this can also be an alist specifying this option for different
561 contexts. See `org-show-hierarchy-above' for valid contexts."
562 :group 'org-reveal-location
563 :type org-context-choice)
565 (defcustom org-show-entry-below '((default . nil))
566 "Non-nil means, show the entry below a headline when revealing a location.
567 Org-mode often shows locations in an org-mode file which might have
568 been invisible before. When this is set, the text below the headline that is
569 exposed is also shown.
571 By default this is off for all contexts.
572 Instead of t, this can also be an alist specifying this option for different
573 contexts. See `org-show-hierarchy-above' for valid contexts."
574 :group 'org-reveal-location
575 :type org-context-choice)
577 (defcustom org-indirect-buffer-display 'other-window
578 "How should indirect tree buffers be displayed?
579 This applies to indirect buffers created with the commands
580 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
581 Valid values are:
582 current-window Display in the current window
583 other-window Just display in another window.
584 dedicated-frame Create one new frame, and re-use it each time.
585 new-frame Make a new frame each time. Note that in this case
586 previously-made indirect buffers are kept, and you need to
587 kill these buffers yourself."
588 :group 'org-structure
589 :group 'org-agenda-windows
590 :type '(choice
591 (const :tag "In current window" current-window)
592 (const :tag "In current frame, other window" other-window)
593 (const :tag "Each time a new frame" new-frame)
594 (const :tag "One dedicated frame" dedicated-frame)))
596 (defgroup org-cycle nil
597 "Options concerning visibility cycling in Org-mode."
598 :tag "Org Cycle"
599 :group 'org-structure)
601 (defcustom org-cycle-skip-children-state-if-no-children t
602 "Non-nil means, skip CHILDREN state in entries that don't have any."
603 :group 'org-cycle
604 :type 'boolean)
606 (defcustom org-cycle-max-level nil
607 "Maximum level which should still be subject to visibility cycling.
608 Levels higher than this will, for cycling, be treated as text, not a headline.
609 When `org-odd-levels-only' is set, a value of N in this variable actually
610 means 2N-1 stars as the limiting headline.
611 When nil, cycle all levels.
612 Note that the limiting level of cycling is also influenced by
613 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
614 `org-inlinetask-min-level' is, cycling will be limited to levels one less
615 than its value."
616 :group 'org-cycle
617 :type '(choice
618 (const :tag "No limit" nil)
619 (integer :tag "Maximum level")))
621 (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
622 "Names of drawers. Drawers are not opened by cycling on the headline above.
623 Drawers only open with a TAB on the drawer line itself. A drawer looks like
624 this:
625 :DRAWERNAME:
626 .....
627 :END:
628 The drawer \"PROPERTIES\" is special for capturing properties through
629 the property API.
631 Drawers can be defined on the per-file basis with a line like:
633 #+DRAWERS: HIDDEN STATE PROPERTIES"
634 :group 'org-structure
635 :group 'org-cycle
636 :type '(repeat (string :tag "Drawer Name")))
638 (defcustom org-hide-block-startup nil
639 "Non-nil means, , entering Org-mode will fold all blocks.
640 This can also be set in on a per-file basis with
642 #+STARTUP: hideblocks
643 #+STARTUP: showblocks"
644 :group 'org-startup
645 :group 'org-cycle
646 :type 'boolean)
648 (defcustom org-cycle-global-at-bob nil
649 "Cycle globally if cursor is at beginning of buffer and not at a headline.
650 This makes it possible to do global cycling without having to use S-TAB or
651 C-u TAB. For this special case to work, the first line of the buffer
652 must not be a headline - it may be empty or some other text. When used in
653 this way, `org-cycle-hook' is disables temporarily, to make sure the
654 cursor stays at the beginning of the buffer.
655 When this option is nil, don't do anything special at the beginning
656 of the buffer."
657 :group 'org-cycle
658 :type 'boolean)
660 (defcustom org-cycle-emulate-tab t
661 "Where should `org-cycle' emulate TAB.
662 nil Never
663 white Only in completely white lines
664 whitestart Only at the beginning of lines, before the first non-white char
665 t Everywhere except in headlines
666 exc-hl-bol Everywhere except at the start of a headline
667 If TAB is used in a place where it does not emulate TAB, the current subtree
668 visibility is cycled."
669 :group 'org-cycle
670 :type '(choice (const :tag "Never" nil)
671 (const :tag "Only in completely white lines" white)
672 (const :tag "Before first char in a line" whitestart)
673 (const :tag "Everywhere except in headlines" t)
674 (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
677 (defcustom org-cycle-separator-lines 2
678 "Number of empty lines needed to keep an empty line between collapsed trees.
679 If you leave an empty line between the end of a subtree and the following
680 headline, this empty line is hidden when the subtree is folded.
681 Org-mode will leave (exactly) one empty line visible if the number of
682 empty lines is equal or larger to the number given in this variable.
683 So the default 2 means, at least 2 empty lines after the end of a subtree
684 are needed to produce free space between a collapsed subtree and the
685 following headline.
687 If the number is negative, and the number of empty lines is at least -N,
688 all empty lines are shown.
690 Special case: when 0, never leave empty lines in collapsed view."
691 :group 'org-cycle
692 :type 'integer)
693 (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
695 (defcustom org-pre-cycle-hook nil
696 "Hook that is run before visibility cycling is happening.
697 The function(s) in this hook must accept a single argument which indicates
698 the new state that will be set right after running this hook. The
699 argument is a symbol. Before a global state change, it can have the values
700 `overview', `content', or `all'. Before a local state change, it can have
701 the values `folded', `children', or `subtree'."
702 :group 'org-cycle
703 :type 'hook)
705 (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
706 org-cycle-hide-drawers
707 org-cycle-show-empty-lines
708 org-optimize-window-after-visibility-change)
709 "Hook that is run after `org-cycle' has changed the buffer visibility.
710 The function(s) in this hook must accept a single argument which indicates
711 the new state that was set by the most recent `org-cycle' command. The
712 argument is a symbol. After a global state change, it can have the values
713 `overview', `content', or `all'. After a local state change, it can have
714 the values `folded', `children', or `subtree'."
715 :group 'org-cycle
716 :type 'hook)
718 (defgroup org-edit-structure nil
719 "Options concerning structure editing in Org-mode."
720 :tag "Org Edit Structure"
721 :group 'org-structure)
723 (defcustom org-odd-levels-only nil
724 "Non-nil means, skip even levels and only use odd levels for the outline.
725 This has the effect that two stars are being added/taken away in
726 promotion/demotion commands. It also influences how levels are
727 handled by the exporters.
728 Changing it requires restart of `font-lock-mode' to become effective
729 for fontification also in regions already fontified.
730 You may also set this on a per-file basis by adding one of the following
731 lines to the buffer:
733 #+STARTUP: odd
734 #+STARTUP: oddeven"
735 :group 'org-edit-structure
736 :group 'org-font-lock
737 :type 'boolean)
739 (defcustom org-adapt-indentation t
740 "Non-nil means, adapt indentation to outline node level.
742 When this variable is set, Org assumes that you write outlines by
743 indenting text in each node to align with the headline (after the stars).
744 The following issues are influenced by this variable:
746 - When this is set and the *entire* text in an entry is indented, the
747 indentation is increased by one space in a demotion command, and
748 decreased by one in a promotion command. If any line in the entry
749 body starts with text at column 0, indentation is not changed at all.
751 - Property drawers and planning information is inserted indented when
752 this variable s set. When nil, they will not be indented.
754 - TAB indents a line relative to context. The lines below a headline
755 will be indented when this variable is set.
757 Note that this is all about true indentation, by adding and removing
758 space characters. See also `org-indent.el' which does level-dependent
759 indentation in a virtual way, i.e. at display time in Emacs."
760 :group 'org-edit-structure
761 :type 'boolean)
763 (defcustom org-special-ctrl-a/e nil
764 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
766 When t, `C-a' will bring back the cursor to the beginning of the
767 headline text, i.e. after the stars and after a possible TODO keyword.
768 In an item, this will be the position after the bullet.
769 When the cursor is already at that position, another `C-a' will bring
770 it to the beginning of the line.
772 `C-e' will jump to the end of the headline, ignoring the presence of tags
773 in the headline. A second `C-e' will then jump to the true end of the
774 line, after any tags. This also means that, when this variable is
775 non-nil, `C-e' also will never jump beyond the end of the heading of a
776 folded section, i.e. not after the ellipses.
778 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
779 going to the true line boundary first. Only a directly following, identical
780 keypress will bring the cursor to the special positions.
782 This may also be a cons cell where the behavior for `C-a' and `C-e' is
783 set separately."
784 :group 'org-edit-structure
785 :type '(choice
786 (const :tag "off" nil)
787 (const :tag "on: after stars/bullet and before tags first" t)
788 (const :tag "reversed: true line boundary first" reversed)
789 (cons :tag "Set C-a and C-e separately"
790 (choice :tag "Special C-a"
791 (const :tag "off" nil)
792 (const :tag "on: after stars/bullet first" t)
793 (const :tag "reversed: before stars/bullet first" reversed))
794 (choice :tag "Special C-e"
795 (const :tag "off" nil)
796 (const :tag "on: before tags first" t)
797 (const :tag "reversed: after tags first" reversed)))))
798 (if (fboundp 'defvaralias)
799 (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
801 (defcustom org-special-ctrl-k nil
802 "Non-nil means `C-k' will behave specially in headlines.
803 When nil, `C-k' will call the default `kill-line' command.
804 When t, the following will happen while the cursor is in the headline:
806 - When the cursor is at the beginning of a headline, kill the entire
807 line and possible the folded subtree below the line.
808 - When in the middle of the headline text, kill the headline up to the tags.
809 - When after the headline text, kill the tags."
810 :group 'org-edit-structure
811 :type 'boolean)
813 (defcustom org-yank-folded-subtrees t
814 "Non-nil means, when yanking subtrees, fold them.
815 If the kill is a single subtree, or a sequence of subtrees, i.e. if
816 it starts with a heading and all other headings in it are either children
817 or siblings, then fold all the subtrees. However, do this only if no
818 text after the yank would be swallowed into a folded tree by this action."
819 :group 'org-edit-structure
820 :type 'boolean)
822 (defcustom org-yank-adjusted-subtrees nil
823 "Non-nil means, when yanking subtrees, adjust the level.
824 With this setting, `org-paste-subtree' is used to insert the subtree, see
825 this function for details."
826 :group 'org-edit-structure
827 :type 'boolean)
829 (defcustom org-M-RET-may-split-line '((default . t))
830 "Non-nil means, M-RET will split the line at the cursor position.
831 When nil, it will go to the end of the line before making a
832 new line.
833 You may also set this option in a different way for different
834 contexts. Valid contexts are:
836 headline when creating a new headline
837 item when creating a new item
838 table in a table field
839 default the value to be used for all contexts not explicitly
840 customized"
841 :group 'org-structure
842 :group 'org-table
843 :type '(choice
844 (const :tag "Always" t)
845 (const :tag "Never" nil)
846 (repeat :greedy t :tag "Individual contexts"
847 (cons
848 (choice :tag "Context"
849 (const headline)
850 (const item)
851 (const table)
852 (const default))
853 (boolean)))))
856 (defcustom org-insert-heading-respect-content nil
857 "Non-nil means, insert new headings after the current subtree.
858 When nil, the new heading is created directly after the current line.
859 The commands \\[org-insert-heading-respect-content] and
860 \\[org-insert-todo-heading-respect-content] turn this variable on
861 for the duration of the command."
862 :group 'org-structure
863 :type 'boolean)
865 (defcustom org-blank-before-new-entry '((heading . auto)
866 (plain-list-item . auto))
867 "Should `org-insert-heading' leave a blank line before new heading/item?
868 The value is an alist, with `heading' and `plain-list-item' as car,
869 and a boolean flag as cdr. For plain lists, if the variable
870 `org-empty-line-terminates-plain-lists' is set, the setting here
871 is ignored and no empty line is inserted, to keep the list in tact."
872 :group 'org-edit-structure
873 :type '(list
874 (cons (const heading)
875 (choice (const :tag "Never" nil)
876 (const :tag "Always" t)
877 (const :tag "Auto" auto)))
878 (cons (const plain-list-item)
879 (choice (const :tag "Never" nil)
880 (const :tag "Always" t)
881 (const :tag "Auto" auto)))))
883 (defcustom org-insert-heading-hook nil
884 "Hook being run after inserting a new heading."
885 :group 'org-edit-structure
886 :type 'hook)
888 (defcustom org-enable-fixed-width-editor t
889 "Non-nil means, lines starting with \":\" are treated as fixed-width.
890 This currently only means, they are never auto-wrapped.
891 When nil, such lines will be treated like ordinary lines.
892 See also the QUOTE keyword."
893 :group 'org-edit-structure
894 :type 'boolean)
897 (defcustom org-goto-auto-isearch t
898 "Non-nil means, typing characters in org-goto starts incremental search."
899 :group 'org-edit-structure
900 :type 'boolean)
902 (defgroup org-sparse-trees nil
903 "Options concerning sparse trees in Org-mode."
904 :tag "Org Sparse Trees"
905 :group 'org-structure)
907 (defcustom org-highlight-sparse-tree-matches t
908 "Non-nil means, highlight all matches that define a sparse tree.
909 The highlights will automatically disappear the next time the buffer is
910 changed by an edit command."
911 :group 'org-sparse-trees
912 :type 'boolean)
914 (defcustom org-remove-highlights-with-change t
915 "Non-nil means, any change to the buffer will remove temporary highlights.
916 Such highlights are created by `org-occur' and `org-clock-display'.
917 When nil, `C-c C-c needs to be used to get rid of the highlights.
918 The highlights created by `org-preview-latex-fragment' always need
919 `C-c C-c' to be removed."
920 :group 'org-sparse-trees
921 :group 'org-time
922 :type 'boolean)
925 (defcustom org-occur-hook '(org-first-headline-recenter)
926 "Hook that is run after `org-occur' has constructed a sparse tree.
927 This can be used to recenter the window to show as much of the structure
928 as possible."
929 :group 'org-sparse-trees
930 :type 'hook)
932 (defgroup org-imenu-and-speedbar nil
933 "Options concerning imenu and speedbar in Org-mode."
934 :tag "Org Imenu and Speedbar"
935 :group 'org-structure)
937 (defcustom org-imenu-depth 2
938 "The maximum level for Imenu access to Org-mode headlines.
939 This also applied for speedbar access."
940 :group 'org-imenu-and-speedbar
941 :type 'integer)
943 (defgroup org-table nil
944 "Options concerning tables in Org-mode."
945 :tag "Org Table"
946 :group 'org)
948 (defcustom org-enable-table-editor 'optimized
949 "Non-nil means, lines starting with \"|\" are handled by the table editor.
950 When nil, such lines will be treated like ordinary lines.
952 When equal to the symbol `optimized', the table editor will be optimized to
953 do the following:
954 - Automatic overwrite mode in front of whitespace in table fields.
955 This makes the structure of the table stay in tact as long as the edited
956 field does not exceed the column width.
957 - Minimize the number of realigns. Normally, the table is aligned each time
958 TAB or RET are pressed to move to another field. With optimization this
959 happens only if changes to a field might have changed the column width.
960 Optimization requires replacing the functions `self-insert-command',
961 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
962 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
963 very good at guessing when a re-align will be necessary, but you can always
964 force one with \\[org-ctrl-c-ctrl-c].
966 If you would like to use the optimized version in Org-mode, but the
967 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
969 This variable can be used to turn on and off the table editor during a session,
970 but in order to toggle optimization, a restart is required.
972 See also the variable `org-table-auto-blank-field'."
973 :group 'org-table
974 :type '(choice
975 (const :tag "off" nil)
976 (const :tag "on" t)
977 (const :tag "on, optimized" optimized)))
979 (defcustom org-self-insert-cluster-for-undo t
980 "Non-nil means cluster self-insert commands for undo when possible.
981 If this is set, then, like in the Emacs command loop, 20 consequtive
982 characters will be undone together.
983 This is configurable, because there is some impact on typing performance."
984 :group 'org-table
985 :type 'boolean)
987 (defcustom org-table-tab-recognizes-table.el t
988 "Non-nil means, TAB will automatically notice a table.el table.
989 When it sees such a table, it moves point into it and - if necessary -
990 calls `table-recognize-table'."
991 :group 'org-table-editing
992 :type 'boolean)
994 (defgroup org-link nil
995 "Options concerning links in Org-mode."
996 :tag "Org Link"
997 :group 'org)
999 (defvar org-link-abbrev-alist-local nil
1000 "Buffer-local version of `org-link-abbrev-alist', which see.
1001 The value of this is taken from the #+LINK lines.")
1002 (make-variable-buffer-local 'org-link-abbrev-alist-local)
1004 (defcustom org-link-abbrev-alist nil
1005 "Alist of link abbreviations.
1006 The car of each element is a string, to be replaced at the start of a link.
1007 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1008 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1010 [[linkkey:tag][description]]
1012 The 'linkkey' must be a word word, starting with a letter, followed
1013 by letters, numbers, '-' or '_'.
1015 If REPLACE is a string, the tag will simply be appended to create the link.
1016 If the string contains \"%s\", the tag will be inserted there. Alternatively,
1017 the placeholder \"%h\" will cause a url-encoded version of the tag to
1018 be inserted at that point (see the function `url-hexify-string').
1020 REPLACE may also be a function that will be called with the tag as the
1021 only argument to create the link, which should be returned as a string.
1023 See the manual for examples."
1024 :group 'org-link
1025 :type '(repeat
1026 (cons
1027 (string :tag "Protocol")
1028 (choice
1029 (string :tag "Format")
1030 (function)))))
1032 (defcustom org-descriptive-links t
1033 "Non-nil means, hide link part and only show description of bracket links.
1034 Bracket links are like [[link][description]]. This variable sets the initial
1035 state in new org-mode buffers. The setting can then be toggled on a
1036 per-buffer basis from the Org->Hyperlinks menu."
1037 :group 'org-link
1038 :type 'boolean)
1040 (defcustom org-link-file-path-type 'adaptive
1041 "How the path name in file links should be stored.
1042 Valid values are:
1044 relative Relative to the current directory, i.e. the directory of the file
1045 into which the link is being inserted.
1046 absolute Absolute path, if possible with ~ for home directory.
1047 noabbrev Absolute path, no abbreviation of home directory.
1048 adaptive Use relative path for files in the current directory and sub-
1049 directories of it. For other files, use an absolute path."
1050 :group 'org-link
1051 :type '(choice
1052 (const relative)
1053 (const absolute)
1054 (const noabbrev)
1055 (const adaptive)))
1057 (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
1058 "Types of links that should be activated in Org-mode files.
1059 This is a list of symbols, each leading to the activation of a certain link
1060 type. In principle, it does not hurt to turn on most link types - there may
1061 be a small gain when turning off unused link types. The types are:
1063 bracket The recommended [[link][description]] or [[link]] links with hiding.
1064 angular Links in angular brackets that may contain whitespace like
1065 <bbdb:Carsten Dominik>.
1066 plain Plain links in normal text, no whitespace, like http://google.com.
1067 radio Text that is matched by a radio target, see manual for details.
1068 tag Tag settings in a headline (link to tag search).
1069 date Time stamps (link to calendar).
1070 footnote Footnote labels.
1072 Changing this variable requires a restart of Emacs to become effective."
1073 :group 'org-link
1074 :type '(set :greedy t
1075 (const :tag "Double bracket links (new style)" bracket)
1076 (const :tag "Angular bracket links (old style)" angular)
1077 (const :tag "Plain text links" plain)
1078 (const :tag "Radio target matches" radio)
1079 (const :tag "Tags" tag)
1080 (const :tag "Timestamps" date)
1081 (const :tag "Footnotes" footnote)))
1083 (defcustom org-make-link-description-function nil
1084 "Function to use to generate link descriptions from links. If
1085 nil the link location will be used. This function must take two
1086 parameters; the first is the link and the second the description
1087 org-insert-link has generated, and should return the description
1088 to use."
1089 :group 'org-link
1090 :type 'function)
1092 (defgroup org-link-store nil
1093 "Options concerning storing links in Org-mode."
1094 :tag "Org Store Link"
1095 :group 'org-link)
1097 (defcustom org-email-link-description-format "Email %c: %.30s"
1098 "Format of the description part of a link to an email or usenet message.
1099 The following %-escapes will be replaced by corresponding information:
1101 %F full \"From\" field
1102 %f name, taken from \"From\" field, address if no name
1103 %T full \"To\" field
1104 %t first name in \"To\" field, address if no name
1105 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1106 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1107 %s subject
1108 %m message-id.
1110 You may use normal field width specification between the % and the letter.
1111 This is for example useful to limit the length of the subject.
1113 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1114 :group 'org-link-store
1115 :type 'string)
1117 (defcustom org-from-is-user-regexp
1118 (let (r1 r2)
1119 (when (and user-mail-address (not (string= user-mail-address "")))
1120 (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
1121 (when (and user-full-name (not (string= user-full-name "")))
1122 (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
1123 (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
1124 "Regexp matched against the \"From:\" header of an email or usenet message.
1125 It should match if the message is from the user him/herself."
1126 :group 'org-link-store
1127 :type 'regexp)
1129 (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
1130 "Non-nil means, storing a link to an Org file will use entry IDs.
1132 Note that before this variable is even considered, org-id must be loaded,
1133 so please customize `org-modules' and turn it on.
1135 The variable can have the following values:
1137 t Create an ID if needed to make a link to the current entry.
1139 create-if-interactive
1140 If `org-store-link' is called directly (interactively, as a user
1141 command), do create an ID to support the link. But when doing the
1142 job for remember, only use the ID if it already exists. The
1143 purpose of this setting is to avoid proliferation of unwanted
1144 IDs, just because you happen to be in an Org file when you
1145 call `org-remember' that automatically and preemptively
1146 creates a link. If you do want to get an ID link in a remember
1147 template to an entry not having an ID, create it first by
1148 explicitly creating a link to it, using `C-c C-l' first.
1150 create-if-interactive-and-no-custom-id
1151 Like create-if-interactive, but do not create an ID if there is
1152 a CUSTOM_ID property defined in the entry. This is the default.
1154 use-existing
1155 Use existing ID, do not create one.
1157 nil Never use an ID to make a link, instead link using a text search for
1158 the headline text."
1159 :group 'org-link-store
1160 :type '(choice
1161 (const :tag "Create ID to make link" t)
1162 (const :tag "Create if storing link interactively"
1163 create-if-interactive)
1164 (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
1165 create-if-interactive-and-no-custom-id)
1166 (const :tag "Only use existing" use-existing)
1167 (const :tag "Do not use ID to create link" nil)))
1169 (defcustom org-context-in-file-links t
1170 "Non-nil means, file links from `org-store-link' contain context.
1171 A search string will be added to the file name with :: as separator and
1172 used to find the context when the link is activated by the command
1173 `org-open-at-point'.
1174 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1175 negates this setting for the duration of the command."
1176 :group 'org-link-store
1177 :type 'boolean)
1179 (defcustom org-keep-stored-link-after-insertion nil
1180 "Non-nil means, keep link in list for entire session.
1182 The command `org-store-link' adds a link pointing to the current
1183 location to an internal list. These links accumulate during a session.
1184 The command `org-insert-link' can be used to insert links into any
1185 Org-mode file (offering completion for all stored links). When this
1186 option is nil, every link which has been inserted once using \\[org-insert-link]
1187 will be removed from the list, to make completing the unused links
1188 more efficient."
1189 :group 'org-link-store
1190 :type 'boolean)
1192 (defgroup org-link-follow nil
1193 "Options concerning following links in Org-mode."
1194 :tag "Org Follow Link"
1195 :group 'org-link)
1197 (defcustom org-link-translation-function nil
1198 "Function to translate links with different syntax to Org syntax.
1199 This can be used to translate links created for example by the Planner
1200 or emacs-wiki packages to Org syntax.
1201 The function must accept two parameters, a TYPE containing the link
1202 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1203 which is everything after the link protocol. It should return a cons
1204 with possibly modified values of type and path.
1205 Org contains a function for this, so if you set this variable to
1206 `org-translate-link-from-planner', you should be able follow many
1207 links created by planner."
1208 :group 'org-link-follow
1209 :type 'function)
1211 (defcustom org-follow-link-hook nil
1212 "Hook that is run after a link has been followed."
1213 :group 'org-link-follow
1214 :type 'hook)
1216 (defcustom org-tab-follows-link nil
1217 "Non-nil means, on links TAB will follow the link.
1218 Needs to be set before org.el is loaded.
1219 This really should not be used, it does not make sense, and the
1220 implementation is bad."
1221 :group 'org-link-follow
1222 :type 'boolean)
1224 (defcustom org-return-follows-link nil
1225 "Non-nil means, on links RET will follow the link.
1226 Needs to be set before org.el is loaded."
1227 :group 'org-link-follow
1228 :type 'boolean)
1230 (defcustom org-mouse-1-follows-link
1231 (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
1232 "Non-nil means, mouse-1 on a link will follow the link.
1233 A longer mouse click will still set point. Does not work on XEmacs.
1234 Needs to be set before org.el is loaded."
1235 :group 'org-link-follow
1236 :type 'boolean)
1238 (defcustom org-mark-ring-length 4
1239 "Number of different positions to be recorded in the ring
1240 Changing this requires a restart of Emacs to work correctly."
1241 :group 'org-link-follow
1242 :type 'integer)
1244 (defcustom org-link-frame-setup
1245 '((vm . vm-visit-folder-other-frame)
1246 (gnus . gnus-other-frame)
1247 (file . find-file-other-window))
1248 "Setup the frame configuration for following links.
1249 When following a link with Emacs, it may often be useful to display
1250 this link in another window or frame. This variable can be used to
1251 set this up for the different types of links.
1252 For VM, use any of
1253 `vm-visit-folder'
1254 `vm-visit-folder-other-frame'
1255 For Gnus, use any of
1256 `gnus'
1257 `gnus-other-frame'
1258 `org-gnus-no-new-news'
1259 For FILE, use any of
1260 `find-file'
1261 `find-file-other-window'
1262 `find-file-other-frame'
1263 For the calendar, use the variable `calendar-setup'.
1264 For BBDB, it is currently only possible to display the matches in
1265 another window."
1266 :group 'org-link-follow
1267 :type '(list
1268 (cons (const vm)
1269 (choice
1270 (const vm-visit-folder)
1271 (const vm-visit-folder-other-window)
1272 (const vm-visit-folder-other-frame)))
1273 (cons (const gnus)
1274 (choice
1275 (const gnus)
1276 (const gnus-other-frame)
1277 (const org-gnus-no-new-news)))
1278 (cons (const file)
1279 (choice
1280 (const find-file)
1281 (const find-file-other-window)
1282 (const find-file-other-frame)))))
1284 (defcustom org-display-internal-link-with-indirect-buffer nil
1285 "Non-nil means, use indirect buffer to display infile links.
1286 Activating internal links (from one location in a file to another location
1287 in the same file) normally just jumps to the location. When the link is
1288 activated with a C-u prefix (or with mouse-3), the link is displayed in
1289 another window. When this option is set, the other window actually displays
1290 an indirect buffer clone of the current buffer, to avoid any visibility
1291 changes to the current buffer."
1292 :group 'org-link-follow
1293 :type 'boolean)
1295 (defcustom org-open-non-existing-files nil
1296 "Non-nil means, `org-open-file' will open non-existing files.
1297 When nil, an error will be generated.
1298 This variable applies only to external applications because they
1299 might choke on non-existing files. If the link is to a file that
1300 will be openend in Emacs, the variable is ignored."
1301 :group 'org-link-follow
1302 :type 'boolean)
1304 (defcustom org-open-directory-means-index-dot-org nil
1305 "Non-nil means, a link to a directory really means to index.org.
1306 When nil, following a directory link will run dired or open a finder/explorer
1307 window on that directory."
1308 :group 'org-link-follow
1309 :type 'boolean)
1311 (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
1312 "Function and arguments to call for following mailto links.
1313 This is a list with the first element being a lisp function, and the
1314 remaining elements being arguments to the function. In string arguments,
1315 %a will be replaced by the address, and %s will be replaced by the subject
1316 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1317 :group 'org-link-follow
1318 :type '(choice
1319 (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1320 (const :tag "compose-mail" (compose-mail "%a" "%s"))
1321 (const :tag "message-mail" (message-mail "%a" "%s"))
1322 (cons :tag "other" (function) (repeat :tag "argument" sexp))))
1324 (defcustom org-confirm-shell-link-function 'yes-or-no-p
1325 "Non-nil means, ask for confirmation before executing shell links.
1326 Shell links can be dangerous: just think about a link
1328 [[shell:rm -rf ~/*][Google Search]]
1330 This link would show up in your Org-mode document as \"Google Search\",
1331 but really it would remove your entire home directory.
1332 Therefore we advise against setting this variable to nil.
1333 Just change it to `y-or-n-p' if you want to confirm with a
1334 single keystroke rather than having to type \"yes\"."
1335 :group 'org-link-follow
1336 :type '(choice
1337 (const :tag "with yes-or-no (safer)" yes-or-no-p)
1338 (const :tag "with y-or-n (faster)" y-or-n-p)
1339 (const :tag "no confirmation (dangerous)" nil)))
1341 (defcustom org-confirm-elisp-link-function 'yes-or-no-p
1342 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1343 Elisp links can be dangerous: just think about a link
1345 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1347 This link would show up in your Org-mode document as \"Google Search\",
1348 but really it would remove your entire home directory.
1349 Therefore we advise against setting this variable to nil.
1350 Just change it to `y-or-n-p' if you want to confirm with a
1351 single keystroke rather than having to type \"yes\"."
1352 :group 'org-link-follow
1353 :type '(choice
1354 (const :tag "with yes-or-no (safer)" yes-or-no-p)
1355 (const :tag "with y-or-n (faster)" y-or-n-p)
1356 (const :tag "no confirmation (dangerous)" nil)))
1358 (defconst org-file-apps-defaults-gnu
1359 '((remote . emacs)
1360 (system . mailcap)
1361 (t . mailcap))
1362 "Default file applications on a UNIX or GNU/Linux system.
1363 See `org-file-apps'.")
1365 (defconst org-file-apps-defaults-macosx
1366 '((remote . emacs)
1367 (t . "open %s")
1368 (system . "open %s")
1369 ("ps.gz" . "gv %s")
1370 ("eps.gz" . "gv %s")
1371 ("dvi" . "xdvi %s")
1372 ("fig" . "xfig %s"))
1373 "Default file applications on a MacOS X system.
1374 The system \"open\" is known as a default, but we use X11 applications
1375 for some files for which the OS does not have a good default.
1376 See `org-file-apps'.")
1378 (defconst org-file-apps-defaults-windowsnt
1379 (list
1380 '(remote . emacs)
1381 (cons t
1382 (list (if (featurep 'xemacs)
1383 'mswindows-shell-execute
1384 'w32-shell-execute)
1385 "open" 'file))
1386 (cons 'system
1387 (list (if (featurep 'xemacs)
1388 'mswindows-shell-execute
1389 'w32-shell-execute)
1390 "open" 'file)))
1391 "Default file applications on a Windows NT system.
1392 The system \"open\" is used for most files.
1393 See `org-file-apps'.")
1395 (defcustom org-file-apps
1397 (auto-mode . emacs)
1398 ("\\.mm\\'" . default)
1399 ("\\.x?html?\\'" . default)
1400 ("\\.pdf\\'" . default)
1402 "External applications for opening `file:path' items in a document.
1403 Org-mode uses system defaults for different file types, but
1404 you can use this variable to set the application for a given file
1405 extension. The entries in this list are cons cells where the car identifies
1406 files and the cdr the corresponding command. Possible values for the
1407 file identifier are
1408 \"regex\" Regular expression matched against the file name. For backward
1409 compatibility, this can also be a string with only alphanumeric
1410 characters, which is then interpreted as an extension.
1411 `directory' Matches a directory
1412 `remote' Matches a remote file, accessible through tramp or efs.
1413 Remote files most likely should be visited through Emacs
1414 because external applications cannot handle such paths.
1415 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1416 so all files Emacs knows how to handle. Using this with
1417 command `emacs' will open most files in Emacs. Beware that this
1418 will also open html files inside Emacs, unless you add
1419 (\"html\" . default) to the list as well.
1420 t Default for files not matched by any of the other options.
1421 `system' The system command to open files, like `open' on Windows
1422 and Mac OS X, and mailcap under GNU/Linux. This is the command
1423 that will be selected if you call `C-c C-o' with a double
1424 `C-u C-u' prefix.
1426 Possible values for the command are:
1427 `emacs' The file will be visited by the current Emacs process.
1428 `default' Use the default application for this file type, which is the
1429 association for t in the list, most likely in the system-specific
1430 part.
1431 This can be used to overrule an unwanted setting in the
1432 system-specific variable.
1433 `system' Use the system command for opening files, like \"open\".
1434 This command is specified by the entry whose car is `system'.
1435 Most likely, the system-specific version of this variable
1436 does define this command, but you can overrule/replace it
1437 here.
1438 string A command to be executed by a shell; %s will be replaced
1439 by the path to the file.
1440 sexp A Lisp form which will be evaluated. The file path will
1441 be available in the Lisp variable `file'.
1442 For more examples, see the system specific constants
1443 `org-file-apps-defaults-macosx'
1444 `org-file-apps-defaults-windowsnt'
1445 `org-file-apps-defaults-gnu'."
1446 :group 'org-link-follow
1447 :type '(repeat
1448 (cons (choice :value ""
1449 (string :tag "Extension")
1450 (const :tag "System command to open files" system)
1451 (const :tag "Default for unrecognized files" t)
1452 (const :tag "Remote file" remote)
1453 (const :tag "Links to a directory" directory)
1454 (const :tag "Any files that have Emacs modes"
1455 auto-mode))
1456 (choice :value ""
1457 (const :tag "Visit with Emacs" emacs)
1458 (const :tag "Use default" default)
1459 (const :tag "Use the system command" system)
1460 (string :tag "Command")
1461 (sexp :tag "Lisp form")))))
1463 (defgroup org-refile nil
1464 "Options concerning refiling entries in Org-mode."
1465 :tag "Org Refile"
1466 :group 'org)
1468 (defcustom org-directory "~/org"
1469 "Directory with org files.
1470 This is just a default location to look for Org files. There is no need
1471 at all to put your files into this directory. It is only used in the
1472 following situations:
1474 1. When a remember template specifies a target file that is not an
1475 absolute path. The path will then be interpreted relative to
1476 `org-directory'
1477 2. When a remember note is filed away in an interactive way (when exiting the
1478 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1479 with `org-directory' as the default path."
1480 :group 'org-refile
1481 :group 'org-remember
1482 :type 'directory)
1484 (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
1485 "Default target for storing notes.
1486 Used by the hooks for remember.el. This can be a string, or nil to mean
1487 the value of `remember-data-file'.
1488 You can set this on a per-template basis with the variable
1489 `org-remember-templates'."
1490 :group 'org-refile
1491 :group 'org-remember
1492 :type '(choice
1493 (const :tag "Default from remember-data-file" nil)
1494 file))
1496 (defcustom org-goto-interface 'outline
1497 "The default interface to be used for `org-goto'.
1498 Allowed values are:
1499 outline The interface shows an outline of the relevant file
1500 and the correct heading is found by moving through
1501 the outline or by searching with incremental search.
1502 outline-path-completion Headlines in the current buffer are offered via
1503 completion. This is the interface also used by
1504 the refile command."
1505 :group 'org-refile
1506 :type '(choice
1507 (const :tag "Outline" outline)
1508 (const :tag "Outline-path-completion" outline-path-completion)))
1510 (defcustom org-goto-max-level 5
1511 "Maximum level to be considered when running org-goto with refile interface."
1512 :group 'org-refile
1513 :type 'integer)
1515 (defcustom org-reverse-note-order nil
1516 "Non-nil means, store new notes at the beginning of a file or entry.
1517 When nil, new notes will be filed to the end of a file or entry.
1518 This can also be a list with cons cells of regular expressions that
1519 are matched against file names, and values."
1520 :group 'org-remember
1521 :group 'org-refile
1522 :type '(choice
1523 (const :tag "Reverse always" t)
1524 (const :tag "Reverse never" nil)
1525 (repeat :tag "By file name regexp"
1526 (cons regexp boolean))))
1528 (defcustom org-refile-targets nil
1529 "Targets for refiling entries with \\[org-refile].
1530 This is list of cons cells. Each cell contains:
1531 - a specification of the files to be considered, either a list of files,
1532 or a symbol whose function or variable value will be used to retrieve
1533 a file name or a list of file names. If you use `org-agenda-files' for
1534 that, all agenda files will be scanned for targets. Nil means, consider
1535 headings in the current buffer.
1536 - A specification of how to find candidate refile targets. This may be
1537 any of:
1538 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1539 This tag has to be present in all target headlines, inheritance will
1540 not be considered.
1541 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1542 todo keyword.
1543 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1544 headlines that are refiling targets.
1545 - a cons cell (:level . N). Any headline of level N is considered a target.
1546 Note that, when `org-odd-levels-only' is set, level corresponds to
1547 order in hierarchy, not to the number of stars.
1548 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1549 Note that, when `org-odd-levels-only' is set, level corresponds to
1550 order in hierarchy, not to the number of stars.
1552 You can set the variable `org-refile-target-verify-function' to a function
1553 to verify each headline found by the simple critery above.
1555 When this variable is nil, all top-level headlines in the current buffer
1556 are used, equivalent to the value `((nil . (:level . 1))'."
1557 :group 'org-refile
1558 :type '(repeat
1559 (cons
1560 (choice :value org-agenda-files
1561 (const :tag "All agenda files" org-agenda-files)
1562 (const :tag "Current buffer" nil)
1563 (function) (variable) (file))
1564 (choice :tag "Identify target headline by"
1565 (cons :tag "Specific tag" (const :value :tag) (string))
1566 (cons :tag "TODO keyword" (const :value :todo) (string))
1567 (cons :tag "Regular expression" (const :value :regexp) (regexp))
1568 (cons :tag "Level number" (const :value :level) (integer))
1569 (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
1571 (defcustom org-refile-target-verify-function nil
1572 "Function to verify if the headline at point should be a refile target.
1573 The function will be called without arguments, with point at the
1574 beginning of the headline. It should return t and leave point
1575 where it is if the headline is a valid target for refiling.
1577 If the target should not be selected, the function must return nil.
1578 In addition to this, it may move point to a place from where the search
1579 should be continued. For example, the function may decide that the entire
1580 subtree of the current entry should be excluded and move point to the end
1581 of the subtree."
1582 :group 'org-refile
1583 :type 'function)
1585 (defcustom org-refile-use-outline-path nil
1586 "Non-nil means, provide refile targets as paths.
1587 So a level 3 headline will be available as level1/level2/level3.
1589 When the value is `file', also include the file name (without directory)
1590 into the path. In this case, you can also stop the completion after
1591 the file name, to get entries inserted as top level in the file.
1593 When `full-file-path', include the full file path."
1594 :group 'org-refile
1595 :type '(choice
1596 (const :tag "Not" nil)
1597 (const :tag "Yes" t)
1598 (const :tag "Start with file name" file)
1599 (const :tag "Start with full file path" full-file-path)))
1601 (defcustom org-outline-path-complete-in-steps t
1602 "Non-nil means, complete the outline path in hierarchical steps.
1603 When Org-mode uses the refile interface to select an outline path
1604 \(see variable `org-refile-use-outline-path'), the completion of
1605 the path can be done is a single go, or if can be done in steps down
1606 the headline hierarchy. Going in steps is probably the best if you
1607 do not use a special completion package like `ido' or `icicles'.
1608 However, when using these packages, going in one step can be very
1609 fast, while still showing the whole path to the entry."
1610 :group 'org-refile
1611 :type 'boolean)
1613 (defcustom org-refile-allow-creating-parent-nodes nil
1614 "Non-nil means, allow to create new nodes as refile targets.
1615 New nodes are then created by adding \"/new node name\" to the completion
1616 of an existing node. When the value of this variable is `confirm',
1617 new node creation must be confirmed by the user (recommended)
1618 When nil, the completion must match an existing entry.
1620 Note that, if the new heading is not seen by the criteria
1621 listed in `org-refile-targets', multiple instances of the same
1622 heading would be created by trying again to file under the new
1623 heading."
1624 :group 'org-refile
1625 :type '(choice
1626 (const :tag "Never" nil)
1627 (const :tag "Always" t)
1628 (const :tag "Prompt for confirmation" confirm)))
1630 (defgroup org-todo nil
1631 "Options concerning TODO items in Org-mode."
1632 :tag "Org TODO"
1633 :group 'org)
1635 (defgroup org-progress nil
1636 "Options concerning Progress logging in Org-mode."
1637 :tag "Org Progress"
1638 :group 'org-time)
1640 (defvar org-todo-interpretation-widgets
1642 (:tag "Sequence (cycling hits every state)" sequence)
1643 (:tag "Type (cycling directly to DONE)" type))
1644 "The available interpretation symbols for customizing
1645 `org-todo-keywords'.
1646 Interested libraries should add to this list.")
1648 (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
1649 "List of TODO entry keyword sequences and their interpretation.
1650 \\<org-mode-map>This is a list of sequences.
1652 Each sequence starts with a symbol, either `sequence' or `type',
1653 indicating if the keywords should be interpreted as a sequence of
1654 action steps, or as different types of TODO items. The first
1655 keywords are states requiring action - these states will select a headline
1656 for inclusion into the global TODO list Org-mode produces. If one of
1657 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1658 signify that no further action is necessary. If \"|\" is not found,
1659 the last keyword is treated as the only DONE state of the sequence.
1661 The command \\[org-todo] cycles an entry through these states, and one
1662 additional state where no keyword is present. For details about this
1663 cycling, see the manual.
1665 TODO keywords and interpretation can also be set on a per-file basis with
1666 the special #+SEQ_TODO and #+TYP_TODO lines.
1668 Each keyword can optionally specify a character for fast state selection
1669 \(in combination with the variable `org-use-fast-todo-selection')
1670 and specifiers for state change logging, using the same syntax
1671 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1672 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1673 indicates to record a time stamp each time this state is selected.
1675 Each keyword may also specify if a timestamp or a note should be
1676 recorded when entering or leaving the state, by adding additional
1677 characters in the parenthesis after the keyword. This looks like this:
1678 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1679 record only the time of the state change. With X and Y being either
1680 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1681 Y when leaving the state if and only if the *target* state does not
1682 define X. You may omit any of the fast-selection key or X or /Y,
1683 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1685 For backward compatibility, this variable may also be just a list
1686 of keywords - in this case the interpretation (sequence or type) will be
1687 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1688 :group 'org-todo
1689 :group 'org-keywords
1690 :type '(choice
1691 (repeat :tag "Old syntax, just keywords"
1692 (string :tag "Keyword"))
1693 (repeat :tag "New syntax"
1694 (cons
1695 (choice
1696 :tag "Interpretation"
1697 ;;Quick and dirty way to see
1698 ;;`org-todo-interpretations'. This takes the
1699 ;;place of item arguments
1700 :convert-widget
1701 (lambda (widget)
1702 (widget-put widget
1703 :args (mapcar
1704 #'(lambda (x)
1705 (widget-convert
1706 (cons 'const x)))
1707 org-todo-interpretation-widgets))
1708 widget))
1709 (repeat
1710 (string :tag "Keyword"))))))
1712 (defvar org-todo-keywords-1 nil
1713 "All TODO and DONE keywords active in a buffer.")
1714 (make-variable-buffer-local 'org-todo-keywords-1)
1715 (defvar org-todo-keywords-for-agenda nil)
1716 (defvar org-done-keywords-for-agenda nil)
1717 (defvar org-drawers-for-agenda nil)
1718 (defvar org-todo-keyword-alist-for-agenda nil)
1719 (defvar org-tag-alist-for-agenda nil)
1720 (defvar org-agenda-contributing-files nil)
1721 (defvar org-not-done-keywords nil)
1722 (make-variable-buffer-local 'org-not-done-keywords)
1723 (defvar org-done-keywords nil)
1724 (make-variable-buffer-local 'org-done-keywords)
1725 (defvar org-todo-heads nil)
1726 (make-variable-buffer-local 'org-todo-heads)
1727 (defvar org-todo-sets nil)
1728 (make-variable-buffer-local 'org-todo-sets)
1729 (defvar org-todo-log-states nil)
1730 (make-variable-buffer-local 'org-todo-log-states)
1731 (defvar org-todo-kwd-alist nil)
1732 (make-variable-buffer-local 'org-todo-kwd-alist)
1733 (defvar org-todo-key-alist nil)
1734 (make-variable-buffer-local 'org-todo-key-alist)
1735 (defvar org-todo-key-trigger nil)
1736 (make-variable-buffer-local 'org-todo-key-trigger)
1738 (defcustom org-todo-interpretation 'sequence
1739 "Controls how TODO keywords are interpreted.
1740 This variable is in principle obsolete and is only used for
1741 backward compatibility, if the interpretation of todo keywords is
1742 not given already in `org-todo-keywords'. See that variable for
1743 more information."
1744 :group 'org-todo
1745 :group 'org-keywords
1746 :type '(choice (const sequence)
1747 (const type)))
1749 (defcustom org-use-fast-todo-selection t
1750 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1751 This variable describes if and under what circumstances the cycling
1752 mechanism for TODO keywords will be replaced by a single-key, direct
1753 selection scheme.
1755 When nil, fast selection is never used.
1757 When the symbol `prefix', it will be used when `org-todo' is called with
1758 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1759 in an agenda buffer.
1761 When t, fast selection is used by default. In this case, the prefix
1762 argument forces cycling instead.
1764 In all cases, the special interface is only used if access keys have actually
1765 been assigned by the user, i.e. if keywords in the configuration are followed
1766 by a letter in parenthesis, like TODO(t)."
1767 :group 'org-todo
1768 :type '(choice
1769 (const :tag "Never" nil)
1770 (const :tag "By default" t)
1771 (const :tag "Only with C-u C-c C-t" prefix)))
1773 (defcustom org-provide-todo-statistics t
1774 "Non-nil means, update todo statistics after insert and toggle.
1775 ALL-HEADLINES means update todo statistics by including headlines
1776 with no TODO keyword as well, counting them as not done.
1777 A list of TODO keywords means the same, but skip keywords that are
1778 not in this list.
1780 When this is set, todo statistics is updated in the parent of the
1781 current entry each time a todo state is changed."
1782 :group 'org-todo
1783 :type '(choice
1784 (const :tag "Yes, only for TODO entries" t)
1785 (const :tag "Yes, including all entries" 'all-headlines)
1786 (repeat :tag "Yes, for TODOs in this list"
1787 (string :tag "TODO keyword"))
1788 (other :tag "No TODO statistics" nil)))
1790 (defcustom org-hierarchical-todo-statistics t
1791 "Non-nil means, TODO statistics covers just direct children.
1792 When nil, all entries in the subtree are considered.
1793 This has only an effect if `org-provide-todo-statistics' is set.
1794 To set this to nil for only a single subtree, use a COOKIE_DATA
1795 property and include the word \"recursive\" into the value."
1796 :group 'org-todo
1797 :type 'boolean)
1799 (defcustom org-after-todo-state-change-hook nil
1800 "Hook which is run after the state of a TODO item was changed.
1801 The new state (a string with a TODO keyword, or nil) is available in the
1802 Lisp variable `state'."
1803 :group 'org-todo
1804 :type 'hook)
1806 (defvar org-blocker-hook nil
1807 "Hook for functions that are allowed to block a state change.
1809 Each function gets as its single argument a property list, see
1810 `org-trigger-hook' for more information about this list.
1812 If any of the functions in this hook returns nil, the state change
1813 is blocked.")
1815 (defvar org-trigger-hook nil
1816 "Hook for functions that are triggered by a state change.
1818 Each function gets as its single argument a property list with at least
1819 the following elements:
1821 (:type type-of-change :position pos-at-entry-start
1822 :from old-state :to new-state)
1824 Depending on the type, more properties may be present.
1826 This mechanism is currently implemented for:
1828 TODO state changes
1829 ------------------
1830 :type todo-state-change
1831 :from previous state (keyword as a string), or nil, or a symbol
1832 'todo' or 'done', to indicate the general type of state.
1833 :to new state, like in :from")
1835 (defcustom org-enforce-todo-dependencies nil
1836 "Non-nil means, undone TODO entries will block switching the parent to DONE.
1837 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
1838 be blocked if any prior sibling is not yet done.
1839 Finally, if the parent is blocked because of ordered siblings of its own,
1840 the child will also be blocked.
1841 This variable needs to be set before org.el is loaded, and you need to
1842 restart Emacs after a change to make the change effective. The only way
1843 to change is while Emacs is running is through the customize interface."
1844 :set (lambda (var val)
1845 (set var val)
1846 (if val
1847 (add-hook 'org-blocker-hook
1848 'org-block-todo-from-children-or-siblings-or-parent)
1849 (remove-hook 'org-blocker-hook
1850 'org-block-todo-from-children-or-siblings-or-parent)))
1851 :group 'org-todo
1852 :type 'boolean)
1854 (defcustom org-enforce-todo-checkbox-dependencies nil
1855 "Non-nil means, unchecked boxes will block switching the parent to DONE.
1856 When this is nil, checkboxes have no influence on switching TODO states.
1857 When non-nil, you first need to check off all check boxes before the TODO
1858 entry can be switched to DONE.
1859 This variable needs to be set before org.el is loaded, and you need to
1860 restart Emacs after a change to make the change effective. The only way
1861 to change is while Emacs is running is through the customize interface."
1862 :set (lambda (var val)
1863 (set var val)
1864 (if val
1865 (add-hook 'org-blocker-hook
1866 'org-block-todo-from-checkboxes)
1867 (remove-hook 'org-blocker-hook
1868 'org-block-todo-from-checkboxes)))
1869 :group 'org-todo
1870 :type 'boolean)
1872 (defcustom org-treat-insert-todo-heading-as-state-change nil
1873 "Non-nil means, inserting a TODO heading is treated as state change.
1874 So when the command \\[org-insert-todo-heading] is used, state change
1875 logging will apply if appropriate. When nil, the new TODO item will
1876 be inserted directly, and no logging will take place."
1877 :group 'org-todo
1878 :type 'boolean)
1880 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
1881 "Non-nil means, switching TODO states with S-cursor counts as state change.
1882 This is the default behavior. However, setting this to nil allows a
1883 convenient way to select a TODO state and bypass any logging associated
1884 with that."
1885 :group 'org-todo
1886 :type 'boolean)
1888 (defcustom org-todo-state-tags-triggers nil
1889 "Tag changes that should be triggered by TODO state changes.
1890 This is a list. Each entry is
1892 (state-change (tag . flag) .......)
1894 State-change can be a string with a state, and empty string to indicate the
1895 state that has no TODO keyword, or it can be one of the symbols `todo'
1896 or `done', meaning any not-done or done state, respectively."
1897 :group 'org-todo
1898 :group 'org-tags
1899 :type '(repeat
1900 (cons (choice :tag "When changing to"
1901 (const :tag "Not-done state" todo)
1902 (const :tag "Done state" done)
1903 (string :tag "State"))
1904 (repeat
1905 (cons :tag "Tag action"
1906 (string :tag "Tag")
1907 (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
1909 (defcustom org-log-done nil
1910 "Information to record when a task moves to the DONE state.
1912 Possible values are:
1914 nil Don't add anything, just change the keyword
1915 time Add a time stamp to the task
1916 note Prompt a closing note and add it with template `org-log-note-headings'
1918 This option can also be set with on a per-file-basis with
1920 #+STARTUP: nologdone
1921 #+STARTUP: logdone
1922 #+STARTUP: lognotedone
1924 You can have local logging settings for a subtree by setting the LOGGING
1925 property to one or more of these keywords."
1926 :group 'org-todo
1927 :group 'org-progress
1928 :type '(choice
1929 (const :tag "No logging" nil)
1930 (const :tag "Record CLOSED timestamp" time)
1931 (const :tag "Record CLOSED timestamp with closing note." note)))
1933 ;; Normalize old uses of org-log-done.
1934 (cond
1935 ((eq org-log-done t) (setq org-log-done 'time))
1936 ((and (listp org-log-done) (memq 'done org-log-done))
1937 (setq org-log-done 'note)))
1939 (defcustom org-log-note-clock-out nil
1940 "Non-nil means, record a note when clocking out of an item.
1941 This can also be configured on a per-file basis by adding one of
1942 the following lines anywhere in the buffer:
1944 #+STARTUP: lognoteclock-out
1945 #+STARTUP: nolognoteclock-out"
1946 :group 'org-todo
1947 :group 'org-progress
1948 :type 'boolean)
1950 (defcustom org-log-done-with-time t
1951 "Non-nil means, the CLOSED time stamp will contain date and time.
1952 When nil, only the date will be recorded."
1953 :group 'org-progress
1954 :type 'boolean)
1956 (defcustom org-log-note-headings
1957 '((done . "CLOSING NOTE %t")
1958 (state . "State %-12s from %-12S %t")
1959 (note . "Note taken on %t")
1960 (clock-out . ""))
1961 "Headings for notes added to entries.
1962 The value is an alist, with the car being a symbol indicating the note
1963 context, and the cdr is the heading to be used. The heading may also be the
1964 empty string.
1965 %t in the heading will be replaced by a time stamp.
1966 %s will be replaced by the new TODO state, in double quotes.
1967 %S will be replaced by the old TODO state, in double quotes.
1968 %u will be replaced by the user name.
1969 %U will be replaced by the full user name."
1970 :group 'org-todo
1971 :group 'org-progress
1972 :type '(list :greedy t
1973 (cons (const :tag "Heading when closing an item" done) string)
1974 (cons (const :tag
1975 "Heading when changing todo state (todo sequence only)"
1976 state) string)
1977 (cons (const :tag "Heading when just taking a note" note) string)
1978 (cons (const :tag "Heading when clocking out" clock-out) string)))
1980 (unless (assq 'note org-log-note-headings)
1981 (push '(note . "%t") org-log-note-headings))
1983 (defcustom org-log-into-drawer nil
1984 "Non-nil means, insert state change notes and time stamps into a drawer.
1985 When nil, state changes notes will be inserted after the headline and
1986 any scheduling and clock lines, but not inside a drawer.
1988 The value of this variable should be the name of the drawer to use.
1989 LOGBOOK is proposed at the default drawer for this purpose, you can
1990 also set this to a string to define the drawer of your choice.
1992 A value of t is also allowed, representing \"LOGBOOK\".
1994 If this variable is set, `org-log-state-notes-insert-after-drawers'
1995 will be ignored.
1997 You can set the property LOG_INTO_DRAWER to overrule this setting for
1998 a subtree."
1999 :group 'org-todo
2000 :group 'org-progress
2001 :type '(choice
2002 (const :tag "Not into a drawer" nil)
2003 (const :tag "LOGBOOK" t)
2004 (string :tag "Other")))
2006 (if (fboundp 'defvaralias)
2007 (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
2009 (defun org-log-into-drawer ()
2010 "Return the value of `org-log-into-drawer', but let properties overrule.
2011 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2012 used instead of the default value."
2013 (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
2014 (cond
2015 ((or (not p) (equal p "nil")) org-log-into-drawer)
2016 ((equal p "t") "LOGBOOK")
2017 (t p))))
2019 (defcustom org-log-state-notes-insert-after-drawers nil
2020 "Non-nil means, insert state change notes after any drawers in entry.
2021 Only the drawers that *immediately* follow the headline and the
2022 deadline/scheduled line are skipped.
2023 When nil, insert notes right after the heading and perhaps the line
2024 with deadline/scheduling if present.
2026 This variable will have no effect if `org-log-into-drawer' is
2027 set."
2028 :group 'org-todo
2029 :group 'org-progress
2030 :type 'boolean)
2032 (defcustom org-log-states-order-reversed t
2033 "Non-nil means, the latest state change note will be directly after heading.
2034 When nil, the notes will be orderer according to time."
2035 :group 'org-todo
2036 :group 'org-progress
2037 :type 'boolean)
2039 (defcustom org-log-repeat 'time
2040 "Non-nil means, record moving through the DONE state when triggering repeat.
2041 An auto-repeating task is immediately switched back to TODO when
2042 marked DONE. If you are not logging state changes (by adding \"@\"
2043 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2044 record a closing note, there will be no record of the task moving
2045 through DONE. This variable forces taking a note anyway.
2047 nil Don't force a record
2048 time Record a time stamp
2049 note Record a note
2051 This option can also be set with on a per-file-basis with
2053 #+STARTUP: logrepeat
2054 #+STARTUP: lognoterepeat
2055 #+STARTUP: nologrepeat
2057 You can have local logging settings for a subtree by setting the LOGGING
2058 property to one or more of these keywords."
2059 :group 'org-todo
2060 :group 'org-progress
2061 :type '(choice
2062 (const :tag "Don't force a record" nil)
2063 (const :tag "Force recording the DONE state" time)
2064 (const :tag "Force recording a note with the DONE state" note)))
2067 (defgroup org-priorities nil
2068 "Priorities in Org-mode."
2069 :tag "Org Priorities"
2070 :group 'org-todo)
2072 (defcustom org-enable-priority-commands t
2073 "Non-nil means, priority commands are active.
2074 When nil, these commands will be disabled, so that you never accidentally
2075 set a priority."
2076 :group 'org-priorities
2077 :type 'boolean)
2079 (defcustom org-highest-priority ?A
2080 "The highest priority of TODO items. A character like ?A, ?B etc.
2081 Must have a smaller ASCII number than `org-lowest-priority'."
2082 :group 'org-priorities
2083 :type 'character)
2085 (defcustom org-lowest-priority ?C
2086 "The lowest priority of TODO items. A character like ?A, ?B etc.
2087 Must have a larger ASCII number than `org-highest-priority'."
2088 :group 'org-priorities
2089 :type 'character)
2091 (defcustom org-default-priority ?B
2092 "The default priority of TODO items.
2093 This is the priority an item get if no explicit priority is given."
2094 :group 'org-priorities
2095 :type 'character)
2097 (defcustom org-priority-start-cycle-with-default t
2098 "Non-nil means, start with default priority when starting to cycle.
2099 When this is nil, the first step in the cycle will be (depending on the
2100 command used) one higher or lower that the default priority."
2101 :group 'org-priorities
2102 :type 'boolean)
2104 (defgroup org-time nil
2105 "Options concerning time stamps and deadlines in Org-mode."
2106 :tag "Org Time"
2107 :group 'org)
2109 (defcustom org-insert-labeled-timestamps-at-point nil
2110 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
2111 When nil, these labeled time stamps are forces into the second line of an
2112 entry, just after the headline. When scheduling from the global TODO list,
2113 the time stamp will always be forced into the second line."
2114 :group 'org-time
2115 :type 'boolean)
2117 (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
2118 "Formats for `format-time-string' which are used for time stamps.
2119 It is not recommended to change this constant.")
2121 (defcustom org-time-stamp-rounding-minutes '(0 5)
2122 "Number of minutes to round time stamps to.
2123 These are two values, the first applies when first creating a time stamp.
2124 The second applies when changing it with the commands `S-up' and `S-down'.
2125 When changing the time stamp, this means that it will change in steps
2126 of N minutes, as given by the second value.
2128 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2129 numbers should be factors of 60, so for example 5, 10, 15.
2131 When this is larger than 1, you can still force an exact time-stamp by using
2132 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
2133 and by using a prefix arg to `S-up/down' to specify the exact number
2134 of minutes to shift."
2135 :group 'org-time
2136 :get '(lambda (var) ; Make sure all entries have 5 elements
2137 (if (integerp (default-value var))
2138 (list (default-value var) 5)
2139 (default-value var)))
2140 :type '(list
2141 (integer :tag "when inserting times")
2142 (integer :tag "when modifying times")))
2144 ;; Normalize old customizations of this variable.
2145 (when (integerp org-time-stamp-rounding-minutes)
2146 (setq org-time-stamp-rounding-minutes
2147 (list org-time-stamp-rounding-minutes
2148 org-time-stamp-rounding-minutes)))
2150 (defcustom org-display-custom-times nil
2151 "Non-nil means, overlay custom formats over all time stamps.
2152 The formats are defined through the variable `org-time-stamp-custom-formats'.
2153 To turn this on on a per-file basis, insert anywhere in the file:
2154 #+STARTUP: customtime"
2155 :group 'org-time
2156 :set 'set-default
2157 :type 'sexp)
2158 (make-variable-buffer-local 'org-display-custom-times)
2160 (defcustom org-time-stamp-custom-formats
2161 '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
2162 "Custom formats for time stamps. See `format-time-string' for the syntax.
2163 These are overlayed over the default ISO format if the variable
2164 `org-display-custom-times' is set. Time like %H:%M should be at the
2165 end of the second format. The custom formats are also honored by export
2166 commands, if custom time display is turned on at the time of export."
2167 :group 'org-time
2168 :type 'sexp)
2170 (defun org-time-stamp-format (&optional long inactive)
2171 "Get the right format for a time string."
2172 (let ((f (if long (cdr org-time-stamp-formats)
2173 (car org-time-stamp-formats))))
2174 (if inactive
2175 (concat "[" (substring f 1 -1) "]")
2176 f)))
2178 (defcustom org-time-clocksum-format "%d:%02d"
2179 "The format string used when creating CLOCKSUM lines, or when
2180 org-mode generates a time duration."
2181 :group 'org-time
2182 :type 'string)
2184 (defcustom org-time-clocksum-use-fractional nil
2185 "If non-nil, \\[org-clock-display] uses fractional times.
2186 org-mode generates a time duration."
2187 :group 'org-time
2188 :type 'boolean)
2190 (defcustom org-time-clocksum-fractional-format "%.2f"
2191 "The format string used when creating CLOCKSUM lines, or when
2192 org-mode generates a time duration."
2193 :group 'org-time
2194 :type 'string)
2196 (defcustom org-deadline-warning-days 14
2197 "No. of days before expiration during which a deadline becomes active.
2198 This variable governs the display in sparse trees and in the agenda.
2199 When 0 or negative, it means use this number (the absolute value of it)
2200 even if a deadline has a different individual lead time specified.
2202 Custom commands can set this variable in the options section."
2203 :group 'org-time
2204 :group 'org-agenda-daily/weekly
2205 :type 'integer)
2207 (defcustom org-read-date-prefer-future t
2208 "Non-nil means, assume future for incomplete date input from user.
2209 This affects the following situations:
2210 1. The user gives a month but not a year.
2211 For example, if it is april and you enter \"feb 2\", this will be read
2212 as feb 2, *next* year. \"May 5\", however, will be this year.
2213 2. The user gives a day, but no month.
2214 For example, if today is the 15th, and you enter \"3\", Org-mode will
2215 read this as the third of *next* month. However, if you enter \"17\",
2216 it will be considered as *this* month.
2218 If you set this variable to the symbol `time', then also the following
2219 will work:
2221 3. If the user gives a time, but no day. If the time is before now,
2222 to will be interpreted as tomorrow.
2224 Currently none of this works for ISO week specifications.
2226 When this option is nil, the current day, month and year will always be
2227 used as defaults."
2228 :group 'org-time
2229 :type '(choice
2230 (const :tag "Never" nil)
2231 (const :tag "Check month and day" t)
2232 (const :tag "Check month, day, and time" time)))
2234 (defcustom org-read-date-display-live t
2235 "Non-nil means, display current interpretation of date prompt live.
2236 This display will be in an overlay, in the minibuffer."
2237 :group 'org-time
2238 :type 'boolean)
2240 (defcustom org-read-date-popup-calendar t
2241 "Non-nil means, pop up a calendar when prompting for a date.
2242 In the calendar, the date can be selected with mouse-1. However, the
2243 minibuffer will also be active, and you can simply enter the date as well.
2244 When nil, only the minibuffer will be available."
2245 :group 'org-time
2246 :type 'boolean)
2247 (if (fboundp 'defvaralias)
2248 (defvaralias 'org-popup-calendar-for-date-prompt
2249 'org-read-date-popup-calendar))
2251 (defcustom org-read-date-minibuffer-setup-hook nil
2252 "Hook to be used to set up keys for the date/time interface.
2253 Add key definitions to `minibuffer-local-map', which will be a temporary
2254 copy."
2255 :group 'org-time
2256 :type 'hook)
2258 (defcustom org-extend-today-until 0
2259 "The hour when your day really ends. Must be an integer.
2260 This has influence for the following applications:
2261 - When switching the agenda to \"today\". It it is still earlier than
2262 the time given here, the day recognized as TODAY is actually yesterday.
2263 - When a date is read from the user and it is still before the time given
2264 here, the current date and time will be assumed to be yesterday, 23:59.
2265 Also, timestamps inserted in remember templates follow this rule.
2267 IMPORTANT: This is a feature whose implementation is and likely will
2268 remain incomplete. Really, it is only here because past midnight seems to
2269 be the favorite working time of John Wiegley :-)"
2270 :group 'org-time
2271 :type 'integer)
2273 (defcustom org-edit-timestamp-down-means-later nil
2274 "Non-nil means, S-down will increase the time in a time stamp.
2275 When nil, S-up will increase."
2276 :group 'org-time
2277 :type 'boolean)
2279 (defcustom org-calendar-follow-timestamp-change t
2280 "Non-nil means, make the calendar window follow timestamp changes.
2281 When a timestamp is modified and the calendar window is visible, it will be
2282 moved to the new date."
2283 :group 'org-time
2284 :type 'boolean)
2286 (defgroup org-tags nil
2287 "Options concerning tags in Org-mode."
2288 :tag "Org Tags"
2289 :group 'org)
2291 (defcustom org-tag-alist nil
2292 "List of tags allowed in Org-mode files.
2293 When this list is nil, Org-mode will base TAG input on what is already in the
2294 buffer.
2295 The value of this variable is an alist, the car of each entry must be a
2296 keyword as a string, the cdr may be a character that is used to select
2297 that tag through the fast-tag-selection interface.
2298 See the manual for details."
2299 :group 'org-tags
2300 :type '(repeat
2301 (choice
2302 (cons (string :tag "Tag name")
2303 (character :tag "Access char"))
2304 (list :tag "Start radio group"
2305 (const :startgroup)
2306 (option (string :tag "Group description")))
2307 (list :tag "End radio group"
2308 (const :endgroup)
2309 (option (string :tag "Group description")))
2310 (const :tag "New line" (:newline)))))
2312 (defcustom org-tag-persistent-alist nil
2313 "List of tags that will always appear in all Org-mode files.
2314 This is in addition to any in buffer settings or customizations
2315 of `org-tag-alist'.
2316 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2317 The value of this variable is an alist, the car of each entry must be a
2318 keyword as a string, the cdr may be a character that is used to select
2319 that tag through the fast-tag-selection interface.
2320 See the manual for details.
2321 To disable these tags on a per-file basis, insert anywhere in the file:
2322 #+STARTUP: noptag"
2323 :group 'org-tags
2324 :type '(repeat
2325 (choice
2326 (cons (string :tag "Tag name")
2327 (character :tag "Access char"))
2328 (const :tag "Start radio group" (:startgroup))
2329 (const :tag "End radio group" (:endgroup))
2330 (const :tag "New line" (:newline)))))
2332 (defvar org-file-tags nil
2333 "List of tags that can be inherited by all entries in the file.
2334 The tags will be inherited if the variable `org-use-tag-inheritance'
2335 says they should be.
2336 This variable is populated from #+FILETAGS lines.")
2338 (defcustom org-use-fast-tag-selection 'auto
2339 "Non-nil means, use fast tag selection scheme.
2340 This is a special interface to select and deselect tags with single keys.
2341 When nil, fast selection is never used.
2342 When the symbol `auto', fast selection is used if and only if selection
2343 characters for tags have been configured, either through the variable
2344 `org-tag-alist' or through a #+TAGS line in the buffer.
2345 When t, fast selection is always used and selection keys are assigned
2346 automatically if necessary."
2347 :group 'org-tags
2348 :type '(choice
2349 (const :tag "Always" t)
2350 (const :tag "Never" nil)
2351 (const :tag "When selection characters are configured" 'auto)))
2353 (defcustom org-fast-tag-selection-single-key nil
2354 "Non-nil means, fast tag selection exits after first change.
2355 When nil, you have to press RET to exit it.
2356 During fast tag selection, you can toggle this flag with `C-c'.
2357 This variable can also have the value `expert'. In this case, the window
2358 displaying the tags menu is not even shown, until you press C-c again."
2359 :group 'org-tags
2360 :type '(choice
2361 (const :tag "No" nil)
2362 (const :tag "Yes" t)
2363 (const :tag "Expert" expert)))
2365 (defvar org-fast-tag-selection-include-todo nil
2366 "Non-nil means, fast tags selection interface will also offer TODO states.
2367 This is an undocumented feature, you should not rely on it.")
2369 (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
2370 "The column to which tags should be indented in a headline.
2371 If this number is positive, it specifies the column. If it is negative,
2372 it means that the tags should be flushright to that column. For example,
2373 -80 works well for a normal 80 character screen."
2374 :group 'org-tags
2375 :type 'integer)
2377 (defcustom org-auto-align-tags t
2378 "Non-nil means, realign tags after pro/demotion of TODO state change.
2379 These operations change the length of a headline and therefore shift
2380 the tags around. With this options turned on, after each such operation
2381 the tags are again aligned to `org-tags-column'."
2382 :group 'org-tags
2383 :type 'boolean)
2385 (defcustom org-use-tag-inheritance t
2386 "Non-nil means, tags in levels apply also for sublevels.
2387 When nil, only the tags directly given in a specific line apply there.
2388 This may also be a list of tags that should be inherited, or a regexp that
2389 matches tags that should be inherited. Additional control is possible
2390 with the variable `org-tags-exclude-from-inheritance' which gives an
2391 explicit list of tags to be excluded from inheritance., even if the value of
2392 `org-use-tag-inheritance' would select it for inheritance.
2394 If this option is t, a match early-on in a tree can lead to a large
2395 number of matches in the subtree when constructing the agenda or creating
2396 a sparse tree. If you only want to see the first match in a tree during
2397 a search, check out the variable `org-tags-match-list-sublevels'."
2398 :group 'org-tags
2399 :type '(choice
2400 (const :tag "Not" nil)
2401 (const :tag "Always" t)
2402 (repeat :tag "Specific tags" (string :tag "Tag"))
2403 (regexp :tag "Tags matched by regexp")))
2405 (defcustom org-tags-exclude-from-inheritance nil
2406 "List of tags that should never be inherited.
2407 This is a way to exclude a few tags from inheritance. For way to do
2408 the opposite, to actively allow inheritance for selected tags,
2409 see the variable `org-use-tag-inheritance'."
2410 :group 'org-tags
2411 :type '(repeat (string :tag "Tag")))
2413 (defun org-tag-inherit-p (tag)
2414 "Check if TAG is one that should be inherited."
2415 (cond
2416 ((member tag org-tags-exclude-from-inheritance) nil)
2417 ((eq org-use-tag-inheritance t) t)
2418 ((not org-use-tag-inheritance) nil)
2419 ((stringp org-use-tag-inheritance)
2420 (string-match org-use-tag-inheritance tag))
2421 ((listp org-use-tag-inheritance)
2422 (member tag org-use-tag-inheritance))
2423 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
2425 (defcustom org-tags-match-list-sublevels t
2426 "Non-nil means list also sublevels of headlines matching a search.
2427 This variable applies to tags/property searches, and also to stuck
2428 projects because this search is based on a tags match as well.
2430 When set to the symbol `indented', sublevels are indented with
2431 leading dots.
2433 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2434 the sublevels of a headline matching a tag search often also match
2435 the same search. Listing all of them can create very long lists.
2436 Setting this variable to nil causes subtrees of a match to be skipped.
2438 This variable is semi-obsolete and probably should always be true. It
2439 is better to limit inheritance to certain tags using the variables
2440 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
2441 :group 'org-tags
2442 :type '(choice
2443 (const :tag "No, don't list them" nil)
2444 (const :tag "Yes, do list them" t)
2445 (const :tag "List them, indented with leading dots" indented)))
2447 (defcustom org-tags-sort-function nil
2448 "When set, tags are sorted using this function as a comparator"
2449 :group 'org-tags
2450 :type '(choice
2451 (const :tag "No sorting" nil)
2452 (const :tag "Alphabetical" string<)
2453 (const :tag "Reverse alphabetical" string>)
2454 (function :tag "Custom function" nil)))
2456 (defvar org-tags-history nil
2457 "History of minibuffer reads for tags.")
2458 (defvar org-last-tags-completion-table nil
2459 "The last used completion table for tags.")
2460 (defvar org-after-tags-change-hook nil
2461 "Hook that is run after the tags in a line have changed.")
2463 (defgroup org-properties nil
2464 "Options concerning properties in Org-mode."
2465 :tag "Org Properties"
2466 :group 'org)
2468 (defcustom org-property-format "%-10s %s"
2469 "How property key/value pairs should be formatted by `indent-line'.
2470 When `indent-line' hits a property definition, it will format the line
2471 according to this format, mainly to make sure that the values are
2472 lined-up with respect to each other."
2473 :group 'org-properties
2474 :type 'string)
2476 (defcustom org-use-property-inheritance nil
2477 "Non-nil means, properties apply also for sublevels.
2479 This setting is chiefly used during property searches. Turning it on can
2480 cause significant overhead when doing a search, which is why it is not
2481 on by default.
2483 When nil, only the properties directly given in the current entry count.
2484 When t, every property is inherited. The value may also be a list of
2485 properties that should have inheritance, or a regular expression matching
2486 properties that should be inherited.
2488 However, note that some special properties use inheritance under special
2489 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2490 and the properties ending in \"_ALL\" when they are used as descriptor
2491 for valid values of a property.
2493 Note for programmers:
2494 When querying an entry with `org-entry-get', you can control if inheritance
2495 should be used. By default, `org-entry-get' looks only at the local
2496 properties. You can request inheritance by setting the inherit argument
2497 to t (to force inheritance) or to `selective' (to respect the setting
2498 in this variable)."
2499 :group 'org-properties
2500 :type '(choice
2501 (const :tag "Not" nil)
2502 (const :tag "Always" t)
2503 (repeat :tag "Specific properties" (string :tag "Property"))
2504 (regexp :tag "Properties matched by regexp")))
2506 (defun org-property-inherit-p (property)
2507 "Check if PROPERTY is one that should be inherited."
2508 (cond
2509 ((eq org-use-property-inheritance t) t)
2510 ((not org-use-property-inheritance) nil)
2511 ((stringp org-use-property-inheritance)
2512 (string-match org-use-property-inheritance property))
2513 ((listp org-use-property-inheritance)
2514 (member property org-use-property-inheritance))
2515 (t (error "Invalid setting of `org-use-property-inheritance'"))))
2517 (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
2518 "The default column format, if no other format has been defined.
2519 This variable can be set on the per-file basis by inserting a line
2521 #+COLUMNS: %25ITEM ....."
2522 :group 'org-properties
2523 :type 'string)
2525 (defcustom org-columns-ellipses ".."
2526 "The ellipses to be used when a field in column view is truncated.
2527 When this is the empty string, as many characters as possible are shown,
2528 but then there will be no visual indication that the field has been truncated.
2529 When this is a string of length N, the last N characters of a truncated
2530 field are replaced by this string. If the column is narrower than the
2531 ellipses string, only part of the ellipses string will be shown."
2532 :group 'org-properties
2533 :type 'string)
2535 (defcustom org-columns-modify-value-for-display-function nil
2536 "Function that modifies values for display in column view.
2537 For example, it can be used to cut out a certain part from a time stamp.
2538 The function must take 2 arguments:
2540 column-title The title of the column (*not* the property name)
2541 value The value that should be modified.
2543 The function should return the value that should be displayed,
2544 or nil if the normal value should be used."
2545 :group 'org-properties
2546 :type 'function)
2548 (defcustom org-effort-property "Effort"
2549 "The property that is being used to keep track of effort estimates.
2550 Effort estimates given in this property need to have the format H:MM."
2551 :group 'org-properties
2552 :group 'org-progress
2553 :type '(string :tag "Property"))
2555 (defconst org-global-properties-fixed
2556 '(("VISIBILITY_ALL" . "folded children content all")
2557 ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
2558 "List of property/value pairs that can be inherited by any entry.
2560 These are fixed values, for the preset properties. The user variable
2561 that can be used to add to this list is `org-global-properties'.
2563 The entries in this list are cons cells where the car is a property
2564 name and cdr is a string with the value. If the value represents
2565 multiple items like an \"_ALL\" property, separate the items by
2566 spaces.")
2568 (defcustom org-global-properties nil
2569 "List of property/value pairs that can be inherited by any entry.
2571 This list will be combined with the constant `org-global-properties-fixed'.
2573 The entries in this list are cons cells where the car is a property
2574 name and cdr is a string with the value.
2576 You can set buffer-local values for the same purpose in the variable
2577 `org-file-properties' this by adding lines like
2579 #+PROPERTY: NAME VALUE"
2580 :group 'org-properties
2581 :type '(repeat
2582 (cons (string :tag "Property")
2583 (string :tag "Value"))))
2585 (defvar org-file-properties nil
2586 "List of property/value pairs that can be inherited by any entry.
2587 Valid for the current buffer.
2588 This variable is populated from #+PROPERTY lines.")
2589 (make-variable-buffer-local 'org-file-properties)
2591 (defgroup org-agenda nil
2592 "Options concerning agenda views in Org-mode."
2593 :tag "Org Agenda"
2594 :group 'org)
2596 (defvar org-category nil
2597 "Variable used by org files to set a category for agenda display.
2598 Such files should use a file variable to set it, for example
2600 # -*- mode: org; org-category: \"ELisp\"
2602 or contain a special line
2604 #+CATEGORY: ELisp
2606 If the file does not specify a category, then file's base name
2607 is used instead.")
2608 (make-variable-buffer-local 'org-category)
2609 (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
2611 (defcustom org-agenda-files nil
2612 "The files to be used for agenda display.
2613 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2614 \\[org-remove-file]. You can also use customize to edit the list.
2616 If an entry is a directory, all files in that directory that are matched by
2617 `org-agenda-file-regexp' will be part of the file list.
2619 If the value of the variable is not a list but a single file name, then
2620 the list of agenda files is actually stored and maintained in that file, one
2621 agenda file per line."
2622 :group 'org-agenda
2623 :type '(choice
2624 (repeat :tag "List of files and directories" file)
2625 (file :tag "Store list in a file\n" :value "~/.agenda_files")))
2627 (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
2628 "Regular expression to match files for `org-agenda-files'.
2629 If any element in the list in that variable contains a directory instead
2630 of a normal file, all files in that directory that are matched by this
2631 regular expression will be included."
2632 :group 'org-agenda
2633 :type 'regexp)
2635 (defcustom org-agenda-text-search-extra-files nil
2636 "List of extra files to be searched by text search commands.
2637 These files will be search in addition to the agenda files by the
2638 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
2639 Note that these files will only be searched for text search commands,
2640 not for the other agenda views like todo lists, tag searches or the weekly
2641 agenda. This variable is intended to list notes and possibly archive files
2642 that should also be searched by these two commands.
2643 In fact, if the first element in the list is the symbol `agenda-archives',
2644 than all archive files of all agenda files will be added to the search
2645 scope."
2646 :group 'org-agenda
2647 :type '(set :greedy t
2648 (const :tag "Agenda Archives" agenda-archives)
2649 (repeat :inline t (file))))
2651 (if (fboundp 'defvaralias)
2652 (defvaralias 'org-agenda-multi-occur-extra-files
2653 'org-agenda-text-search-extra-files))
2655 (defcustom org-agenda-skip-unavailable-files nil
2656 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
2657 A nil value means to remove them, after a query, from the list."
2658 :group 'org-agenda
2659 :type 'boolean)
2661 (defcustom org-calendar-to-agenda-key [?c]
2662 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2663 The command `org-calendar-goto-agenda' will be bound to this key. The
2664 default is the character `c' because then `c' can be used to switch back and
2665 forth between agenda and calendar."
2666 :group 'org-agenda
2667 :type 'sexp)
2669 (defcustom org-calendar-agenda-action-key [?k]
2670 "The key to be installed in `calendar-mode-map' for agenda-action.
2671 The command `org-agenda-action' will be bound to this key. The
2672 default is the character `k' because we use the same key in the agenda."
2673 :group 'org-agenda
2674 :type 'sexp)
2676 (eval-after-load "calendar"
2677 '(progn
2678 (org-defkey calendar-mode-map org-calendar-to-agenda-key
2679 'org-calendar-goto-agenda)
2680 (org-defkey calendar-mode-map org-calendar-agenda-action-key
2681 'org-agenda-action)))
2683 (defgroup org-latex nil
2684 "Options for embedding LaTeX code into Org-mode."
2685 :tag "Org LaTeX"
2686 :group 'org)
2688 (defcustom org-format-latex-options
2689 '(:foreground default :background default :scale 1.0
2690 :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
2691 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
2692 "Options for creating images from LaTeX fragments.
2693 This is a property list with the following properties:
2694 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
2695 `default' means use the foreground of the default face.
2696 :background the background color, or \"Transparent\".
2697 `default' means use the background of the default face.
2698 :scale a scaling factor for the size of the images.
2699 :html-foreground, :html-background, :html-scale
2700 the same numbers for HTML export.
2701 :matchers a list indicating which matchers should be used to
2702 find LaTeX fragments. Valid members of this list are:
2703 \"begin\" find environments
2704 \"$1\" find single characters surrounded by $.$
2705 \"$\" find math expressions surrounded by $...$
2706 \"$$\" find math expressions surrounded by $$....$$
2707 \"\\(\" find math expressions surrounded by \\(...\\)
2708 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2709 :group 'org-latex
2710 :type 'plist)
2712 (defcustom org-format-latex-header "\\documentclass{article}
2713 \\usepackage{amssymb}
2714 \\usepackage[usenames]{color}
2715 \\usepackage{amsmath}
2716 \\usepackage{latexsym}
2717 \\usepackage[mathscr]{eucal}
2718 \\pagestyle{empty} % do not remove
2719 % The settings below are copied from fullpage.sty
2720 \\setlength{\\textwidth}{\\paperwidth}
2721 \\addtolength{\\textwidth}{-3cm}
2722 \\setlength{\\oddsidemargin}{1.5cm}
2723 \\addtolength{\\oddsidemargin}{-2.54cm}
2724 \\setlength{\\evensidemargin}{\\oddsidemargin}
2725 \\setlength{\\textheight}{\\paperheight}
2726 \\addtolength{\\textheight}{-\\headheight}
2727 \\addtolength{\\textheight}{-\\headsep}
2728 \\addtolength{\\textheight}{-\\footskip}
2729 \\addtolength{\\textheight}{-3cm}
2730 \\setlength{\\topmargin}{1.5cm}
2731 \\addtolength{\\topmargin}{-2.54cm}"
2732 "The document header used for processing LaTeX fragments.
2733 It is imperative that this header make sure that no page number
2734 appears on the page."
2735 :group 'org-latex
2736 :type 'string)
2739 (defgroup org-font-lock nil
2740 "Font-lock settings for highlighting in Org-mode."
2741 :tag "Org Font Lock"
2742 :group 'org)
2744 (defcustom org-level-color-stars-only nil
2745 "Non-nil means fontify only the stars in each headline.
2746 When nil, the entire headline is fontified.
2747 Changing it requires restart of `font-lock-mode' to become effective
2748 also in regions already fontified."
2749 :group 'org-font-lock
2750 :type 'boolean)
2752 (defcustom org-hide-leading-stars nil
2753 "Non-nil means, hide the first N-1 stars in a headline.
2754 This works by using the face `org-hide' for these stars. This
2755 face is white for a light background, and black for a dark
2756 background. You may have to customize the face `org-hide' to
2757 make this work.
2758 Changing it requires restart of `font-lock-mode' to become effective
2759 also in regions already fontified.
2760 You may also set this on a per-file basis by adding one of the following
2761 lines to the buffer:
2763 #+STARTUP: hidestars
2764 #+STARTUP: showstars"
2765 :group 'org-font-lock
2766 :type 'boolean)
2768 (defcustom org-fontify-done-headline nil
2769 "Non-nil means, change the face of a headline if it is marked DONE.
2770 Normally, only the TODO/DONE keyword indicates the state of a headline.
2771 When this is non-nil, the headline after the keyword is set to the
2772 `org-headline-done' as an additional indication."
2773 :group 'org-font-lock
2774 :type 'boolean)
2776 (defcustom org-fontify-emphasized-text t
2777 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
2778 Changing this variable requires a restart of Emacs to take effect."
2779 :group 'org-font-lock
2780 :type 'boolean)
2782 (defcustom org-fontify-whole-heading-line nil
2783 "Non-nil means fontify the whole line for headings.
2784 This is useful when setting a background color for the
2785 org-leve-* faces."
2786 :group 'org-font-lock
2787 :type 'boolean)
2789 (defcustom org-highlight-latex-fragments-and-specials nil
2790 "Non-nil means, fontify what is treated specially by the exporters."
2791 :group 'org-font-lock
2792 :type 'boolean)
2794 (defcustom org-hide-emphasis-markers nil
2795 "Non-nil mean font-lock should hide the emphasis marker characters."
2796 :group 'org-font-lock
2797 :type 'boolean)
2799 (defvar org-emph-re nil
2800 "Regular expression for matching emphasis.")
2801 (defvar org-verbatim-re nil
2802 "Regular expression for matching verbatim text.")
2803 (defvar org-emphasis-regexp-components) ; defined just below
2804 (defvar org-emphasis-alist) ; defined just below
2805 (defun org-set-emph-re (var val)
2806 "Set variable and compute the emphasis regular expression."
2807 (set var val)
2808 (when (and (boundp 'org-emphasis-alist)
2809 (boundp 'org-emphasis-regexp-components)
2810 org-emphasis-alist org-emphasis-regexp-components)
2811 (let* ((e org-emphasis-regexp-components)
2812 (pre (car e))
2813 (post (nth 1 e))
2814 (border (nth 2 e))
2815 (body (nth 3 e))
2816 (nl (nth 4 e))
2817 (body1 (concat body "*?"))
2818 (markers (mapconcat 'car org-emphasis-alist ""))
2819 (vmarkers (mapconcat
2820 (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
2821 org-emphasis-alist "")))
2822 ;; make sure special characters appear at the right position in the class
2823 (if (string-match "\\^" markers)
2824 (setq markers (concat (replace-match "" t t markers) "^")))
2825 (if (string-match "-" markers)
2826 (setq markers (concat (replace-match "" t t markers) "-")))
2827 (if (string-match "\\^" vmarkers)
2828 (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
2829 (if (string-match "-" vmarkers)
2830 (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
2831 (if (> nl 0)
2832 (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
2833 (int-to-string nl) "\\}")))
2834 ;; Make the regexp
2835 (setq org-emph-re
2836 (concat "\\([" pre "]\\|^\\)"
2837 "\\("
2838 "\\([" markers "]\\)"
2839 "\\("
2840 "[^" border "]\\|"
2841 "[^" border "]"
2842 body1
2843 "[^" border "]"
2844 "\\)"
2845 "\\3\\)"
2846 "\\([" post "]\\|$\\)"))
2847 (setq org-verbatim-re
2848 (concat "\\([" pre "]\\|^\\)"
2849 "\\("
2850 "\\([" vmarkers "]\\)"
2851 "\\("
2852 "[^" border "]\\|"
2853 "[^" border "]"
2854 body1
2855 "[^" border "]"
2856 "\\)"
2857 "\\3\\)"
2858 "\\([" post "]\\|$\\)")))))
2860 (defcustom org-emphasis-regexp-components
2861 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
2862 "Components used to build the regular expression for emphasis.
2863 This is a list with 6 entries. Terminology: In an emphasis string
2864 like \" *strong word* \", we call the initial space PREMATCH, the final
2865 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
2866 and \"trong wor\" is the body. The different components in this variable
2867 specify what is allowed/forbidden in each part:
2869 pre Chars allowed as prematch. Beginning of line will be allowed too.
2870 post Chars allowed as postmatch. End of line will be allowed too.
2871 border The chars *forbidden* as border characters.
2872 body-regexp A regexp like \".\" to match a body character. Don't use
2873 non-shy groups here, and don't allow newline here.
2874 newline The maximum number of newlines allowed in an emphasis exp.
2876 Use customize to modify this, or restart Emacs after changing it."
2877 :group 'org-font-lock
2878 :set 'org-set-emph-re
2879 :type '(list
2880 (sexp :tag "Allowed chars in pre ")
2881 (sexp :tag "Allowed chars in post ")
2882 (sexp :tag "Forbidden chars in border ")
2883 (sexp :tag "Regexp for body ")
2884 (integer :tag "number of newlines allowed")
2885 (option (boolean :tag "Please ignore this button"))))
2887 (defcustom org-emphasis-alist
2888 `(("*" bold "<b>" "</b>")
2889 ("/" italic "<i>" "</i>")
2890 ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
2891 ("=" org-code "<code>" "</code>" verbatim)
2892 ("~" org-verbatim "<code>" "</code>" verbatim)
2893 ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
2894 "<del>" "</del>")
2896 "Special syntax for emphasized text.
2897 Text starting and ending with a special character will be emphasized, for
2898 example *bold*, _underlined_ and /italic/. This variable sets the marker
2899 characters, the face to be used by font-lock for highlighting in Org-mode
2900 Emacs buffers, and the HTML tags to be used for this.
2901 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
2902 Use customize to modify this, or restart Emacs after changing it."
2903 :group 'org-font-lock
2904 :set 'org-set-emph-re
2905 :type '(repeat
2906 (list
2907 (string :tag "Marker character")
2908 (choice
2909 (face :tag "Font-lock-face")
2910 (plist :tag "Face property list"))
2911 (string :tag "HTML start tag")
2912 (string :tag "HTML end tag")
2913 (option (const verbatim)))))
2915 (defvar org-protecting-blocks
2916 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
2917 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
2918 This is needed for font-lock setup.")
2920 ;;; Miscellaneous options
2922 (defgroup org-completion nil
2923 "Completion in Org-mode."
2924 :tag "Org Completion"
2925 :group 'org)
2927 (defcustom org-completion-use-ido nil
2928 "Non-nil means, use ido completion wherever possible.
2929 Note that `ido-mode' must be active for this variable to be relevant.
2930 If you decide to turn this variable on, you might well want to turn off
2931 `org-outline-path-complete-in-steps'.
2932 See also `org-completion-use-iswitchb'."
2933 :group 'org-completion
2934 :type 'boolean)
2936 (defcustom org-completion-use-iswitchb nil
2937 "Non-nil means, use iswitchb completion wherever possible.
2938 Note that `iswitchb-mode' must be active for this variable to be relevant.
2939 If you decide to turn this variable on, you might well want to turn off
2940 `org-outline-path-complete-in-steps'.
2941 Note that thi variable has only an effect if `org-completion-use-ido' is nil."
2942 :group 'org-completion
2943 :type 'boolean)
2945 (defcustom org-completion-fallback-command 'hippie-expand
2946 "The expansion command called by \\[org-complete] in normal context.
2947 Normal means, no org-mode-specific context."
2948 :group 'org-completion
2949 :type 'function)
2951 ;;; Functions and variables from ther packages
2952 ;; Declared here to avoid compiler warnings
2954 ;; XEmacs only
2955 (defvar outline-mode-menu-heading)
2956 (defvar outline-mode-menu-show)
2957 (defvar outline-mode-menu-hide)
2958 (defvar zmacs-regions) ; XEmacs regions
2960 ;; Emacs only
2961 (defvar mark-active)
2963 ;; Various packages
2964 (declare-function calendar-absolute-from-iso "cal-iso" (date))
2965 (declare-function calendar-forward-day "cal-move" (arg))
2966 (declare-function calendar-goto-date "cal-move" (date))
2967 (declare-function calendar-goto-today "cal-move" ())
2968 (declare-function calendar-iso-from-absolute "cal-iso" (date))
2969 (defvar calc-embedded-close-formula)
2970 (defvar calc-embedded-open-formula)
2971 (declare-function cdlatex-tab "ext:cdlatex" ())
2972 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
2973 (defvar font-lock-unfontify-region-function)
2974 (declare-function iswitchb-read-buffer "iswitchb"
2975 (prompt &optional default require-match start matches-set))
2976 (defvar iswitchb-temp-buflist)
2977 (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
2978 (defvar org-agenda-tags-todo-honor-ignore-options)
2979 (declare-function org-agenda-skip "org-agenda" ())
2980 (declare-function org-format-agenda-item "org-agenda"
2981 (extra txt &optional category tags dotime noprefix remove-re))
2982 (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
2983 (declare-function org-agenda-change-all-lines "org-agenda"
2984 (newhead hdmarker &optional fixface just-this))
2985 (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
2986 (declare-function org-agenda-maybe-redo "org-agenda" ())
2987 (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
2988 (beg end))
2989 (declare-function org-agenda-copy-local-variable "org-agenda" (var))
2990 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
2991 "org-agenda" (&optional end))
2992 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
2993 (declare-function org-indent-mode "org-indent" (&optional arg))
2994 (declare-function parse-time-string "parse-time" (string))
2995 (defvar remember-data-file)
2996 (defvar texmathp-why)
2997 (declare-function speedbar-line-directory "speedbar" (&optional depth))
2998 (declare-function table--at-cell-p "table" (position &optional object at-column))
3000 (defvar w3m-current-url)
3001 (defvar w3m-current-title)
3003 (defvar org-latex-regexps)
3005 ;;; Autoload and prepare some org modules
3007 ;; Some table stuff that needs to be defined here, because it is used
3008 ;; by the functions setting up org-mode or checking for table context.
3010 (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
3011 "Detects an org-type or table-type table.")
3012 (defconst org-table-line-regexp "^[ \t]*|"
3013 "Detects an org-type table line.")
3014 (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
3015 "Detects an org-type table line.")
3016 (defconst org-table-hline-regexp "^[ \t]*|-"
3017 "Detects an org-type table hline.")
3018 (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
3019 "Detects a table-type table hline.")
3020 (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
3021 "Searching from within a table (any type) this finds the first line
3022 outside the table.")
3024 ;; Autoload the functions in org-table.el that are needed by functions here.
3026 (eval-and-compile
3027 (org-autoload "org-table"
3028 '(org-table-align org-table-begin org-table-blank-field
3029 org-table-convert org-table-convert-region org-table-copy-down
3030 org-table-copy-region org-table-create
3031 org-table-create-or-convert-from-region
3032 org-table-create-with-table.el org-table-current-dline
3033 org-table-cut-region org-table-delete-column org-table-edit-field
3034 org-table-edit-formulas org-table-end org-table-eval-formula
3035 org-table-export org-table-field-info
3036 org-table-get-stored-formulas org-table-goto-column
3037 org-table-hline-and-move org-table-import org-table-insert-column
3038 org-table-insert-hline org-table-insert-row org-table-iterate
3039 org-table-justify-field-maybe org-table-kill-row
3040 org-table-maybe-eval-formula org-table-maybe-recalculate-line
3041 org-table-move-column org-table-move-column-left
3042 org-table-move-column-right org-table-move-row
3043 org-table-move-row-down org-table-move-row-up
3044 org-table-next-field org-table-next-row org-table-paste-rectangle
3045 org-table-previous-field org-table-recalculate
3046 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
3047 org-table-toggle-coordinate-overlays
3048 org-table-toggle-formula-debugger org-table-wrap-region
3049 orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
3051 (defun org-at-table-p (&optional table-type)
3052 "Return t if the cursor is inside an org-type table.
3053 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3054 (if org-enable-table-editor
3055 (save-excursion
3056 (beginning-of-line 1)
3057 (looking-at (if table-type org-table-any-line-regexp
3058 org-table-line-regexp)))
3059 nil))
3060 (defsubst org-table-p () (org-at-table-p))
3062 (defun org-at-table.el-p ()
3063 "Return t if and only if we are at a table.el table."
3064 (and (org-at-table-p 'any)
3065 (save-excursion
3066 (goto-char (org-table-begin 'any))
3067 (looking-at org-table1-hline-regexp))))
3068 (defun org-table-recognize-table.el ()
3069 "If there is a table.el table nearby, recognize it and move into it."
3070 (if org-table-tab-recognizes-table.el
3071 (if (org-at-table.el-p)
3072 (progn
3073 (beginning-of-line 1)
3074 (if (looking-at org-table-dataline-regexp)
3076 (if (looking-at org-table1-hline-regexp)
3077 (progn
3078 (beginning-of-line 2)
3079 (if (looking-at org-table-any-border-regexp)
3080 (beginning-of-line -1)))))
3081 (if (re-search-forward "|" (org-table-end t) t)
3082 (progn
3083 (require 'table)
3084 (if (table--at-cell-p (point))
3086 (message "recognizing table.el table...")
3087 (table-recognize-table)
3088 (message "recognizing table.el table...done")))
3089 (error "This should not happen..."))
3091 nil)
3092 nil))
3094 (defun org-at-table-hline-p ()
3095 "Return t if the cursor is inside a hline in a table."
3096 (if org-enable-table-editor
3097 (save-excursion
3098 (beginning-of-line 1)
3099 (looking-at org-table-hline-regexp))
3100 nil))
3102 (defvar org-table-clean-did-remove-column nil)
3104 (defun org-table-map-tables (function)
3105 "Apply FUNCTION to the start of all tables in the buffer."
3106 (save-excursion
3107 (save-restriction
3108 (widen)
3109 (goto-char (point-min))
3110 (while (re-search-forward org-table-any-line-regexp nil t)
3111 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
3112 (beginning-of-line 1)
3113 (when (looking-at org-table-line-regexp)
3114 (save-excursion (funcall function))
3115 (or (looking-at org-table-line-regexp)
3116 (forward-char 1)))
3117 (re-search-forward org-table-any-border-regexp nil 1))))
3118 (message "Mapping tables: done"))
3120 ;; Declare and autoload functions from org-exp.el & Co
3122 (declare-function org-default-export-plist "org-exp")
3123 (declare-function org-infile-export-plist "org-exp")
3124 (declare-function org-get-current-options "org-exp")
3125 (eval-and-compile
3126 (org-autoload "org-exp"
3127 '(org-export org-export-visible
3128 org-insert-export-options-template
3129 org-table-clean-before-export))
3130 (org-autoload "org-ascii"
3131 '(org-export-as-ascii org-export-ascii-preprocess
3132 org-export-as-ascii-to-buffer org-replace-region-by-ascii
3133 org-export-region-as-ascii))
3134 (org-autoload "org-html"
3135 '(org-export-as-html-and-open
3136 org-export-as-html-batch org-export-as-html-to-buffer
3137 org-replace-region-by-html org-export-region-as-html
3138 org-export-as-html))
3139 (org-autoload "org-icalendar"
3140 '(org-export-icalendar-this-file
3141 org-export-icalendar-all-agenda-files
3142 org-export-icalendar-combine-agenda-files))
3143 (org-autoload "org-xoxo" '(org-export-as-xoxo)))
3145 ;; Declare and autoload functions from org-agenda.el
3147 (eval-and-compile
3148 (org-autoload "org-agenda"
3149 '(org-agenda org-agenda-list org-search-view
3150 org-todo-list org-tags-view org-agenda-list-stuck-projects
3151 org-diary org-agenda-to-appt
3152 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
3154 ;; Autoload org-remember
3156 (eval-and-compile
3157 (org-autoload "org-remember"
3158 '(org-remember-insinuate org-remember-annotation
3159 org-remember-apply-template org-remember org-remember-handler)))
3161 ;; Autoload org-clock.el
3164 (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
3165 (beg end))
3166 (declare-function org-clock-update-mode-line "org-clock" ())
3167 (declare-function org-resolve-clocks "org-clock"
3168 (&optional also-non-dangling-p prompt last-valid))
3169 (defvar org-clock-start-time)
3170 (defvar org-clock-marker (make-marker)
3171 "Marker recording the last clock-in.")
3172 (defvar org-clock-hd-marker (make-marker)
3173 "Marker recording the last clock-in, but the headline position.")
3174 (defun org-clock-is-active ()
3175 "Return non-nil if clock is currently running.
3176 The return value is actually the clock marker."
3177 (marker-buffer org-clock-marker))
3179 (eval-and-compile
3180 (org-autoload
3181 "org-clock"
3182 '(org-clock-in org-clock-out org-clock-cancel
3183 org-clock-goto org-clock-sum org-clock-display
3184 org-clock-remove-overlays org-clock-report
3185 org-clocktable-shift org-dblock-write:clocktable
3186 org-get-clocktable org-resolve-clocks)))
3188 (defun org-clock-update-time-maybe ()
3189 "If this is a CLOCK line, update it and return t.
3190 Otherwise, return nil."
3191 (interactive)
3192 (save-excursion
3193 (beginning-of-line 1)
3194 (skip-chars-forward " \t")
3195 (when (looking-at org-clock-string)
3196 (let ((re (concat "[ \t]*" org-clock-string
3197 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
3198 "\\([ \t]*=>.*\\)?\\)?"))
3199 ts te h m s neg)
3200 (cond
3201 ((not (looking-at re))
3202 nil)
3203 ((not (match-end 2))
3204 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
3205 (> org-clock-marker (point))
3206 (<= org-clock-marker (point-at-eol)))
3207 ;; The clock is running here
3208 (setq org-clock-start-time
3209 (apply 'encode-time
3210 (org-parse-time-string (match-string 1))))
3211 (org-clock-update-mode-line)))
3213 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
3214 (end-of-line 1)
3215 (setq ts (match-string 1)
3216 te (match-string 3))
3217 (setq s (- (org-float-time
3218 (apply 'encode-time (org-parse-time-string te)))
3219 (org-float-time
3220 (apply 'encode-time (org-parse-time-string ts))))
3221 neg (< s 0)
3222 s (abs s)
3223 h (floor (/ s 3600))
3224 s (- s (* 3600 h))
3225 m (floor (/ s 60))
3226 s (- s (* 60 s)))
3227 (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
3228 t))))))
3230 (defun org-check-running-clock ()
3231 "Check if the current buffer contains the running clock.
3232 If yes, offer to stop it and to save the buffer with the changes."
3233 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
3234 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
3235 (buffer-name))))
3236 (org-clock-out)
3237 (when (y-or-n-p "Save changed buffer?")
3238 (save-buffer))))
3240 (defun org-clocktable-try-shift (dir n)
3241 "Check if this line starts a clock table, if yes, shift the time block."
3242 (when (org-match-line "#\\+BEGIN: clocktable\\>")
3243 (org-clocktable-shift dir n)))
3245 ;; Autoload org-timer.el
3247 (eval-and-compile
3248 (org-autoload
3249 "org-timer"
3250 '(org-timer-start org-timer org-timer-item
3251 org-timer-change-times-in-region
3252 org-timer-set-timer
3253 org-timer-reset-timers
3254 org-timer-show-remaining-time)))
3256 ;; Autoload org-feed.el
3258 (eval-and-compile
3259 (org-autoload
3260 "org-feed"
3261 '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
3264 ;; Autoload org-indent.el
3266 (eval-and-compile
3267 (org-autoload
3268 "org-indent"
3269 '(org-indent-mode)))
3271 ;; Autoload org-mobile.el
3273 (eval-and-compile
3274 (org-autoload
3275 "org-mobile"
3276 '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
3278 ;; Autoload archiving code
3279 ;; The stuff that is needed for cycling and tags has to be defined here.
3281 (defgroup org-archive nil
3282 "Options concerning archiving in Org-mode."
3283 :tag "Org Archive"
3284 :group 'org-structure)
3286 (defcustom org-archive-location "%s_archive::"
3287 "The location where subtrees should be archived.
3289 The value of this variable is a string, consisting of two parts,
3290 separated by a double-colon. The first part is a filename and
3291 the second part is a headline.
3293 When the filename is omitted, archiving happens in the same file.
3294 %s in the filename will be replaced by the current file
3295 name (without the directory part). Archiving to a different file
3296 is useful to keep archived entries from contributing to the
3297 Org-mode Agenda.
3299 The archived entries will be filed as subtrees of the specified
3300 headline. When the headline is omitted, the subtrees are simply
3301 filed away at the end of the file, as top-level entries. Also in
3302 the heading you can use %s to represent the file name, this can be
3303 useful when using the same archive for a number of different files.
3305 Here are a few examples:
3306 \"%s_archive::\"
3307 If the current file is Projects.org, archive in file
3308 Projects.org_archive, as top-level trees. This is the default.
3310 \"::* Archived Tasks\"
3311 Archive in the current file, under the top-level headline
3312 \"* Archived Tasks\".
3314 \"~/org/archive.org::\"
3315 Archive in file ~/org/archive.org (absolute path), as top-level trees.
3317 \"~/org/archive.org::From %s\"
3318 Archive in file ~/org/archive.org (absolute path), und headlines
3319 \"From FILENAME\" where file name is the current file name.
3321 \"basement::** Finished Tasks\"
3322 Archive in file ./basement (relative path), as level 3 trees
3323 below the level 2 heading \"** Finished Tasks\".
3325 You may set this option on a per-file basis by adding to the buffer a
3326 line like
3328 #+ARCHIVE: basement::** Finished Tasks
3330 You may also define it locally for a subtree by setting an ARCHIVE property
3331 in the entry. If such a property is found in an entry, or anywhere up
3332 the hierarchy, it will be used."
3333 :group 'org-archive
3334 :type 'string)
3336 (defcustom org-archive-tag "ARCHIVE"
3337 "The tag that marks a subtree as archived.
3338 An archived subtree does not open during visibility cycling, and does
3339 not contribute to the agenda listings.
3340 After changing this, font-lock must be restarted in the relevant buffers to
3341 get the proper fontification."
3342 :group 'org-archive
3343 :group 'org-keywords
3344 :type 'string)
3346 (defcustom org-agenda-skip-archived-trees t
3347 "Non-nil means, the agenda will skip any items located in archived trees.
3348 An archived tree is a tree marked with the tag ARCHIVE. The use of this
3349 variable is no longer recommended, you should leave it at the value t.
3350 Instead, use the key `v' to cycle the archives-mode in the agenda."
3351 :group 'org-archive
3352 :group 'org-agenda-skip
3353 :type 'boolean)
3355 (defcustom org-columns-skip-arrchived-trees t
3356 "Non-nil means, irgnore archived trees when creating column view."
3357 :group 'org-archive
3358 :group 'org-properties
3359 :type 'boolean)
3361 (defcustom org-cycle-open-archived-trees nil
3362 "Non-nil means, `org-cycle' will open archived trees.
3363 An archived tree is a tree marked with the tag ARCHIVE.
3364 When nil, archived trees will stay folded. You can still open them with
3365 normal outline commands like `show-all', but not with the cycling commands."
3366 :group 'org-archive
3367 :group 'org-cycle
3368 :type 'boolean)
3370 (defcustom org-sparse-tree-open-archived-trees nil
3371 "Non-nil means sparse tree construction shows matches in archived trees.
3372 When nil, matches in these trees are highlighted, but the trees are kept in
3373 collapsed state."
3374 :group 'org-archive
3375 :group 'org-sparse-trees
3376 :type 'boolean)
3378 (defun org-cycle-hide-archived-subtrees (state)
3379 "Re-hide all archived subtrees after a visibility state change."
3380 (when (and (not org-cycle-open-archived-trees)
3381 (not (memq state '(overview folded))))
3382 (save-excursion
3383 (let* ((globalp (memq state '(contents all)))
3384 (beg (if globalp (point-min) (point)))
3385 (end (if globalp (point-max) (org-end-of-subtree t))))
3386 (org-hide-archived-subtrees beg end)
3387 (goto-char beg)
3388 (if (looking-at (concat ".*:" org-archive-tag ":"))
3389 (message "%s" (substitute-command-keys
3390 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
3392 (defun org-force-cycle-archived ()
3393 "Cycle subtree even if it is archived."
3394 (interactive)
3395 (setq this-command 'org-cycle)
3396 (let ((org-cycle-open-archived-trees t))
3397 (call-interactively 'org-cycle)))
3399 (defun org-hide-archived-subtrees (beg end)
3400 "Re-hide all archived subtrees after a visibility state change."
3401 (save-excursion
3402 (let* ((re (concat ":" org-archive-tag ":")))
3403 (goto-char beg)
3404 (while (re-search-forward re end t)
3405 (and (org-on-heading-p) (hide-subtree))
3406 (org-end-of-subtree t)))))
3408 (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
3410 (eval-and-compile
3411 (org-autoload "org-archive"
3412 '(org-add-archive-files org-archive-subtree
3413 org-archive-to-archive-sibling org-toggle-archive-tag)))
3415 ;; Autoload Column View Code
3417 (declare-function org-columns-number-to-string "org-colview")
3418 (declare-function org-columns-get-format-and-top-level "org-colview")
3419 (declare-function org-columns-compute "org-colview")
3421 (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
3422 '(org-columns-number-to-string org-columns-get-format-and-top-level
3423 org-columns-compute org-agenda-columns org-columns-remove-overlays
3424 org-columns org-insert-columns-dblock org-dblock-write:columnview))
3426 ;; Autoload ID code
3428 (declare-function org-id-store-link "org-id")
3429 (declare-function org-id-locations-load "org-id")
3430 (declare-function org-id-locations-save "org-id")
3431 (defvar org-id-track-globally)
3432 (org-autoload "org-id"
3433 '(org-id-get-create org-id-new org-id-copy org-id-get
3434 org-id-get-with-outline-path-completion
3435 org-id-get-with-outline-drilling
3436 org-id-goto org-id-find org-id-store-link))
3438 ;; Autoload Plotting Code
3440 (org-autoload "org-plot"
3441 '(org-plot/gnuplot))
3443 ;;; Variables for pre-computed regular expressions, all buffer local
3445 (defvar org-drawer-regexp nil
3446 "Matches first line of a hidden block.")
3447 (make-variable-buffer-local 'org-drawer-regexp)
3448 (defvar org-todo-regexp nil
3449 "Matches any of the TODO state keywords.")
3450 (make-variable-buffer-local 'org-todo-regexp)
3451 (defvar org-not-done-regexp nil
3452 "Matches any of the TODO state keywords except the last one.")
3453 (make-variable-buffer-local 'org-not-done-regexp)
3454 (defvar org-not-done-heading-regexp nil
3455 "Matches a TODO headline that is not done.")
3456 (make-variable-buffer-local 'org-not-done-regexp)
3457 (defvar org-todo-line-regexp nil
3458 "Matches a headline and puts TODO state into group 2 if present.")
3459 (make-variable-buffer-local 'org-todo-line-regexp)
3460 (defvar org-complex-heading-regexp nil
3461 "Matches a headline and puts everything into groups:
3462 group 1: the stars
3463 group 2: The todo keyword, maybe
3464 group 3: Priority cookie
3465 group 4: True headline
3466 group 5: Tags")
3467 (make-variable-buffer-local 'org-complex-heading-regexp)
3468 (defvar org-complex-heading-regexp-format nil)
3469 (make-variable-buffer-local 'org-complex-heading-regexp-format)
3470 (defvar org-todo-line-tags-regexp nil
3471 "Matches a headline and puts TODO state into group 2 if present.
3472 Also put tags into group 4 if tags are present.")
3473 (make-variable-buffer-local 'org-todo-line-tags-regexp)
3474 (defvar org-nl-done-regexp nil
3475 "Matches newline followed by a headline with the DONE keyword.")
3476 (make-variable-buffer-local 'org-nl-done-regexp)
3477 (defvar org-looking-at-done-regexp nil
3478 "Matches the DONE keyword a point.")
3479 (make-variable-buffer-local 'org-looking-at-done-regexp)
3480 (defvar org-ds-keyword-length 12
3481 "Maximum length of the Deadline and SCHEDULED keywords.")
3482 (make-variable-buffer-local 'org-ds-keyword-length)
3483 (defvar org-deadline-regexp nil
3484 "Matches the DEADLINE keyword.")
3485 (make-variable-buffer-local 'org-deadline-regexp)
3486 (defvar org-deadline-time-regexp nil
3487 "Matches the DEADLINE keyword together with a time stamp.")
3488 (make-variable-buffer-local 'org-deadline-time-regexp)
3489 (defvar org-deadline-line-regexp nil
3490 "Matches the DEADLINE keyword and the rest of the line.")
3491 (make-variable-buffer-local 'org-deadline-line-regexp)
3492 (defvar org-scheduled-regexp nil
3493 "Matches the SCHEDULED keyword.")
3494 (make-variable-buffer-local 'org-scheduled-regexp)
3495 (defvar org-scheduled-time-regexp nil
3496 "Matches the SCHEDULED keyword together with a time stamp.")
3497 (make-variable-buffer-local 'org-scheduled-time-regexp)
3498 (defvar org-closed-time-regexp nil
3499 "Matches the CLOSED keyword together with a time stamp.")
3500 (make-variable-buffer-local 'org-closed-time-regexp)
3502 (defvar org-keyword-time-regexp nil
3503 "Matches any of the 4 keywords, together with the time stamp.")
3504 (make-variable-buffer-local 'org-keyword-time-regexp)
3505 (defvar org-keyword-time-not-clock-regexp nil
3506 "Matches any of the 3 keywords, together with the time stamp.")
3507 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
3508 (defvar org-maybe-keyword-time-regexp nil
3509 "Matches a timestamp, possibly preceeded by a keyword.")
3510 (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
3511 (defvar org-planning-or-clock-line-re nil
3512 "Matches a line with planning or clock info.")
3513 (make-variable-buffer-local 'org-planning-or-clock-line-re)
3515 (defconst org-plain-time-of-day-regexp
3516 (concat
3517 "\\(\\<[012]?[0-9]"
3518 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3519 "\\(--?"
3520 "\\(\\<[012]?[0-9]"
3521 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3522 "\\)?")
3523 "Regular expression to match a plain time or time range.
3524 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3525 groups carry important information:
3526 0 the full match
3527 1 the first time, range or not
3528 8 the second time, if it is a range.")
3530 (defconst org-plain-time-extension-regexp
3531 (concat
3532 "\\(\\<[012]?[0-9]"
3533 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3534 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
3535 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
3536 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3537 groups carry important information:
3538 0 the full match
3539 7 hours of duration
3540 9 minutes of duration")
3542 (defconst org-stamp-time-of-day-regexp
3543 (concat
3544 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
3545 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
3546 "\\(--?"
3547 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
3548 "Regular expression to match a timestamp time or time range.
3549 After a match, the following groups carry important information:
3550 0 the full match
3551 1 date plus weekday, for backreferencing to make sure both times on same day
3552 2 the first time, range or not
3553 4 the second time, if it is a range.")
3555 (defconst org-startup-options
3556 '(("fold" org-startup-folded t)
3557 ("overview" org-startup-folded t)
3558 ("nofold" org-startup-folded nil)
3559 ("showall" org-startup-folded nil)
3560 ("showeverything" org-startup-folded showeverything)
3561 ("content" org-startup-folded content)
3562 ("indent" org-startup-indented t)
3563 ("noindent" org-startup-indented nil)
3564 ("hidestars" org-hide-leading-stars t)
3565 ("showstars" org-hide-leading-stars nil)
3566 ("odd" org-odd-levels-only t)
3567 ("oddeven" org-odd-levels-only nil)
3568 ("align" org-startup-align-all-tables t)
3569 ("noalign" org-startup-align-all-tables nil)
3570 ("customtime" org-display-custom-times t)
3571 ("logdone" org-log-done time)
3572 ("lognotedone" org-log-done note)
3573 ("nologdone" org-log-done nil)
3574 ("lognoteclock-out" org-log-note-clock-out t)
3575 ("nolognoteclock-out" org-log-note-clock-out nil)
3576 ("logrepeat" org-log-repeat state)
3577 ("lognoterepeat" org-log-repeat note)
3578 ("nologrepeat" org-log-repeat nil)
3579 ("fninline" org-footnote-define-inline t)
3580 ("nofninline" org-footnote-define-inline nil)
3581 ("fnlocal" org-footnote-section nil)
3582 ("fnauto" org-footnote-auto-label t)
3583 ("fnprompt" org-footnote-auto-label nil)
3584 ("fnconfirm" org-footnote-auto-label confirm)
3585 ("fnplain" org-footnote-auto-label plain)
3586 ("fnadjust" org-footnote-auto-adjust t)
3587 ("nofnadjust" org-footnote-auto-adjust nil)
3588 ("constcgs" constants-unit-system cgs)
3589 ("constSI" constants-unit-system SI)
3590 ("noptag" org-tag-persistent-alist nil)
3591 ("hideblocks" org-hide-block-startup t)
3592 ("nohideblocks" org-hide-block-startup nil))
3593 "Variable associated with STARTUP options for org-mode.
3594 Each element is a list of three items: The startup options as written
3595 in the #+STARTUP line, the corresponding variable, and the value to
3596 set this variable to if the option is found. An optional forth element PUSH
3597 means to push this value onto the list in the variable.")
3599 (defun org-set-regexps-and-options ()
3600 "Precompute regular expressions for current buffer."
3601 (when (org-mode-p)
3602 (org-set-local 'org-todo-kwd-alist nil)
3603 (org-set-local 'org-todo-key-alist nil)
3604 (org-set-local 'org-todo-key-trigger nil)
3605 (org-set-local 'org-todo-keywords-1 nil)
3606 (org-set-local 'org-done-keywords nil)
3607 (org-set-local 'org-todo-heads nil)
3608 (org-set-local 'org-todo-sets nil)
3609 (org-set-local 'org-todo-log-states nil)
3610 (org-set-local 'org-file-properties nil)
3611 (org-set-local 'org-file-tags nil)
3612 (let ((re (org-make-options-regexp
3613 '("CATEGORY" "TODO" "COLUMNS"
3614 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
3615 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
3616 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
3617 (splitre "[ \t]+")
3618 kwds kws0 kwsa key log value cat arch tags const links hw dws
3619 tail sep kws1 prio props ftags drawers
3620 ext-setup-or-nil setup-contents (start 0))
3621 (save-excursion
3622 (save-restriction
3623 (widen)
3624 (goto-char (point-min))
3625 (while (or (and ext-setup-or-nil
3626 (string-match re ext-setup-or-nil start)
3627 (setq start (match-end 0)))
3628 (and (setq ext-setup-or-nil nil start 0)
3629 (re-search-forward re nil t)))
3630 (setq key (upcase (match-string 1 ext-setup-or-nil))
3631 value (org-match-string-no-properties 2 ext-setup-or-nil))
3632 (cond
3633 ((equal key "CATEGORY")
3634 (if (string-match "[ \t]+$" value)
3635 (setq value (replace-match "" t t value)))
3636 (setq cat value))
3637 ((member key '("SEQ_TODO" "TODO"))
3638 (push (cons 'sequence (org-split-string value splitre)) kwds))
3639 ((equal key "TYP_TODO")
3640 (push (cons 'type (org-split-string value splitre)) kwds))
3641 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
3642 ;; general TODO-like setup
3643 (push (cons (intern (downcase (match-string 1 key)))
3644 (org-split-string value splitre)) kwds))
3645 ((equal key "TAGS")
3646 (setq tags (append tags (if tags '("\\n") nil)
3647 (org-split-string value splitre))))
3648 ((equal key "COLUMNS")
3649 (org-set-local 'org-columns-default-format value))
3650 ((equal key "LINK")
3651 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
3652 (push (cons (match-string 1 value)
3653 (org-trim (match-string 2 value)))
3654 links)))
3655 ((equal key "PRIORITIES")
3656 (setq prio (org-split-string value " +")))
3657 ((equal key "PROPERTY")
3658 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
3659 (push (cons (match-string 1 value) (match-string 2 value))
3660 props)))
3661 ((equal key "FILETAGS")
3662 (when (string-match "\\S-" value)
3663 (setq ftags
3664 (append
3665 ftags
3666 (apply 'append
3667 (mapcar (lambda (x) (org-split-string x ":"))
3668 (org-split-string value)))))))
3669 ((equal key "DRAWERS")
3670 (setq drawers (org-split-string value splitre)))
3671 ((equal key "CONSTANTS")
3672 (setq const (append const (org-split-string value splitre))))
3673 ((equal key "STARTUP")
3674 (let ((opts (org-split-string value splitre))
3675 l var val)
3676 (while (setq l (pop opts))
3677 (when (setq l (assoc l org-startup-options))
3678 (setq var (nth 1 l) val (nth 2 l))
3679 (if (not (nth 3 l))
3680 (set (make-local-variable var) val)
3681 (if (not (listp (symbol-value var)))
3682 (set (make-local-variable var) nil))
3683 (set (make-local-variable var) (symbol-value var))
3684 (add-to-list var val))))))
3685 ((equal key "ARCHIVE")
3686 (string-match " *$" value)
3687 (setq arch (replace-match "" t t value))
3688 (remove-text-properties 0 (length arch)
3689 '(face t fontified t) arch))
3690 ((equal key "SETUPFILE")
3691 (setq setup-contents (org-file-contents
3692 (expand-file-name
3693 (org-remove-double-quotes value))
3694 'noerror))
3695 (if (not ext-setup-or-nil)
3696 (setq ext-setup-or-nil setup-contents start 0)
3697 (setq ext-setup-or-nil
3698 (concat (substring ext-setup-or-nil 0 start)
3699 "\n" setup-contents "\n"
3700 (substring ext-setup-or-nil start)))))
3701 ))))
3702 (when cat
3703 (org-set-local 'org-category (intern cat))
3704 (push (cons "CATEGORY" cat) props))
3705 (when prio
3706 (if (< (length prio) 3) (setq prio '("A" "C" "B")))
3707 (setq prio (mapcar 'string-to-char prio))
3708 (org-set-local 'org-highest-priority (nth 0 prio))
3709 (org-set-local 'org-lowest-priority (nth 1 prio))
3710 (org-set-local 'org-default-priority (nth 2 prio)))
3711 (and props (org-set-local 'org-file-properties (nreverse props)))
3712 (and ftags (org-set-local 'org-file-tags
3713 (mapcar 'org-add-prop-inherited ftags)))
3714 (and drawers (org-set-local 'org-drawers drawers))
3715 (and arch (org-set-local 'org-archive-location arch))
3716 (and links (setq org-link-abbrev-alist-local (nreverse links)))
3717 ;; Process the TODO keywords
3718 (unless kwds
3719 ;; Use the global values as if they had been given locally.
3720 (setq kwds (default-value 'org-todo-keywords))
3721 (if (stringp (car kwds))
3722 (setq kwds (list (cons org-todo-interpretation
3723 (default-value 'org-todo-keywords)))))
3724 (setq kwds (reverse kwds)))
3725 (setq kwds (nreverse kwds))
3726 (let (inter kws kw)
3727 (while (setq kws (pop kwds))
3728 (let ((kws (or
3729 (run-hook-with-args-until-success
3730 'org-todo-setup-filter-hook kws)
3731 kws)))
3732 (setq inter (pop kws) sep (member "|" kws)
3733 kws0 (delete "|" (copy-sequence kws))
3734 kwsa nil
3735 kws1 (mapcar
3736 (lambda (x)
3737 ;; 1 2
3738 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
3739 (progn
3740 (setq kw (match-string 1 x)
3741 key (and (match-end 2) (match-string 2 x))
3742 log (org-extract-log-state-settings x))
3743 (push (cons kw (and key (string-to-char key))) kwsa)
3744 (and log (push log org-todo-log-states))
3746 (error "Invalid TODO keyword %s" x)))
3747 kws0)
3748 kwsa (if kwsa (append '((:startgroup))
3749 (nreverse kwsa)
3750 '((:endgroup))))
3751 hw (car kws1)
3752 dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
3753 tail (list inter hw (car dws) (org-last dws))))
3754 (add-to-list 'org-todo-heads hw 'append)
3755 (push kws1 org-todo-sets)
3756 (setq org-done-keywords (append org-done-keywords dws nil))
3757 (setq org-todo-key-alist (append org-todo-key-alist kwsa))
3758 (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
3759 (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
3760 (setq org-todo-sets (nreverse org-todo-sets)
3761 org-todo-kwd-alist (nreverse org-todo-kwd-alist)
3762 org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
3763 org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
3764 ;; Process the constants
3765 (when const
3766 (let (e cst)
3767 (while (setq e (pop const))
3768 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
3769 (push (cons (match-string 1 e) (match-string 2 e)) cst)))
3770 (setq org-table-formula-constants-local cst)))
3772 ;; Process the tags.
3773 (when tags
3774 (let (e tgs)
3775 (while (setq e (pop tags))
3776 (cond
3777 ((equal e "{") (push '(:startgroup) tgs))
3778 ((equal e "}") (push '(:endgroup) tgs))
3779 ((equal e "\\n") (push '(:newline) tgs))
3780 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
3781 (push (cons (match-string 1 e)
3782 (string-to-char (match-string 2 e)))
3783 tgs))
3784 (t (push (list e) tgs))))
3785 (org-set-local 'org-tag-alist nil)
3786 (while (setq e (pop tgs))
3787 (or (and (stringp (car e))
3788 (assoc (car e) org-tag-alist))
3789 (push e org-tag-alist)))))
3791 ;; Compute the regular expressions and other local variables
3792 (if (not org-done-keywords)
3793 (setq org-done-keywords (and org-todo-keywords-1
3794 (list (org-last org-todo-keywords-1)))))
3795 (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
3796 (length org-scheduled-string)
3797 (length org-clock-string)
3798 (length org-closed-string)))
3799 org-drawer-regexp
3800 (concat "^[ \t]*:\\("
3801 (mapconcat 'regexp-quote org-drawers "\\|")
3802 "\\):[ \t]*$")
3803 org-not-done-keywords
3804 (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
3805 org-todo-regexp
3806 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
3807 "\\|") "\\)\\>")
3808 org-not-done-regexp
3809 (concat "\\<\\("
3810 (mapconcat 'regexp-quote org-not-done-keywords "\\|")
3811 "\\)\\>")
3812 org-not-done-heading-regexp
3813 (concat "^\\(\\*+\\)[ \t]+\\("
3814 (mapconcat 'regexp-quote org-not-done-keywords "\\|")
3815 "\\)\\>")
3816 org-todo-line-regexp
3817 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3818 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
3819 "\\)\\>\\)?[ \t]*\\(.*\\)")
3820 org-complex-heading-regexp
3821 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3822 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
3823 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
3824 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3825 org-complex-heading-regexp-format
3826 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3827 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
3828 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
3829 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3830 org-nl-done-regexp
3831 (concat "\n\\*+[ \t]+"
3832 "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
3833 "\\)" "\\>")
3834 org-todo-line-tags-regexp
3835 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3836 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
3837 (org-re
3838 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
3839 org-looking-at-done-regexp
3840 (concat "^" "\\(?:"
3841 (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
3842 "\\>")
3843 org-deadline-regexp (concat "\\<" org-deadline-string)
3844 org-deadline-time-regexp
3845 (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
3846 org-deadline-line-regexp
3847 (concat "\\<\\(" org-deadline-string "\\).*")
3848 org-scheduled-regexp
3849 (concat "\\<" org-scheduled-string)
3850 org-scheduled-time-regexp
3851 (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
3852 org-closed-time-regexp
3853 (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
3854 org-keyword-time-regexp
3855 (concat "\\<\\(" org-scheduled-string
3856 "\\|" org-deadline-string
3857 "\\|" org-closed-string
3858 "\\|" org-clock-string "\\)"
3859 " *[[<]\\([^]>]+\\)[]>]")
3860 org-keyword-time-not-clock-regexp
3861 (concat "\\<\\(" org-scheduled-string
3862 "\\|" org-deadline-string
3863 "\\|" org-closed-string
3864 "\\)"
3865 " *[[<]\\([^]>]+\\)[]>]")
3866 org-maybe-keyword-time-regexp
3867 (concat "\\(\\<\\(" org-scheduled-string
3868 "\\|" org-deadline-string
3869 "\\|" org-closed-string
3870 "\\|" org-clock-string "\\)\\)?"
3871 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
3872 org-planning-or-clock-line-re
3873 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
3874 "\\|" org-deadline-string
3875 "\\|" org-closed-string "\\|" org-clock-string
3876 "\\)\\>\\)")
3878 (org-compute-latex-and-specials-regexp)
3879 (org-set-font-lock-defaults))))
3881 (defun org-file-contents (file &optional noerror)
3882 "Return the contents of FILE, as a string."
3883 (if (or (not file)
3884 (not (file-readable-p file)))
3885 (if noerror
3886 (progn
3887 (message "Cannot read file %s" file)
3888 (ding) (sit-for 2)
3890 (error "Cannot read file %s" file))
3891 (with-temp-buffer
3892 (insert-file-contents file)
3893 (buffer-string))))
3895 (defun org-extract-log-state-settings (x)
3896 "Extract the log state setting from a TODO keyword string.
3897 This will extract info from a string like \"WAIT(w@/!)\"."
3898 (let (kw key log1 log2)
3899 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
3900 (setq kw (match-string 1 x)
3901 key (and (match-end 2) (match-string 2 x))
3902 log1 (and (match-end 3) (match-string 3 x))
3903 log2 (and (match-end 4) (match-string 4 x)))
3904 (and (or log1 log2)
3905 (list kw
3906 (and log1 (if (equal log1 "!") 'time 'note))
3907 (and log2 (if (equal log2 "!") 'time 'note)))))))
3909 (defun org-remove-keyword-keys (list)
3910 "Remove a pair of parenthesis at the end of each string in LIST."
3911 (mapcar (lambda (x)
3912 (if (string-match "(.*)$" x)
3913 (substring x 0 (match-beginning 0))
3915 list))
3917 ;; FIXME: this could be done much better, using second characters etc.
3918 (defun org-assign-fast-keys (alist)
3919 "Assign fast keys to a keyword-key alist.
3920 Respect keys that are already there."
3921 (let (new e k c c1 c2 (char ?a))
3922 (while (setq e (pop alist))
3923 (cond
3924 ((equal e '(:startgroup)) (push e new))
3925 ((equal e '(:endgroup)) (push e new))
3926 ((equal e '(:newline)) (push e new))
3928 (setq k (car e) c2 nil)
3929 (if (cdr e)
3930 (setq c (cdr e))
3931 ;; automatically assign a character.
3932 (setq c1 (string-to-char
3933 (downcase (substring
3934 k (if (= (string-to-char k) ?@) 1 0)))))
3935 (if (or (rassoc c1 new) (rassoc c1 alist))
3936 (while (or (rassoc char new) (rassoc char alist))
3937 (setq char (1+ char)))
3938 (setq c2 c1))
3939 (setq c (or c2 char)))
3940 (push (cons k c) new))))
3941 (nreverse new)))
3943 ;;; Some variables used in various places
3945 (defvar org-window-configuration nil
3946 "Used in various places to store a window configuration.")
3947 (defvar org-selected-window nil
3948 "Used in various places to store a window configuration.")
3949 (defvar org-finish-function nil
3950 "Function to be called when `C-c C-c' is used.
3951 This is for getting out of special buffers like remember.")
3954 ;; FIXME: Occasionally check by commenting these, to make sure
3955 ;; no other functions uses these, forgetting to let-bind them.
3956 (defvar entry)
3957 (defvar last-state)
3958 (defvar date)
3960 ;; Defined somewhere in this file, but used before definition.
3961 (defvar org-html-entities)
3962 (defvar org-struct-menu)
3963 (defvar org-org-menu)
3964 (defvar org-tbl-menu)
3965 (defvar org-agenda-keymap)
3967 ;;;; Define the Org-mode
3969 (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
3970 (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."))
3973 ;; We use a before-change function to check if a table might need
3974 ;; an update.
3975 (defvar org-table-may-need-update t
3976 "Indicates that a table might need an update.
3977 This variable is set by `org-before-change-function'.
3978 `org-table-align' sets it back to nil.")
3979 (defun org-before-change-function (beg end)
3980 "Every change indicates that a table might need an update."
3981 (setq org-table-may-need-update t))
3982 (defvar org-mode-map)
3983 (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
3984 (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
3985 (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
3986 (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
3987 (defvar org-table-buffer-is-an nil)
3988 (defconst org-outline-regexp "\\*+ ")
3990 ;;;###autoload
3991 (define-derived-mode org-mode outline-mode "Org"
3992 "Outline-based notes management and organizer, alias
3993 \"Carsten's outline-mode for keeping track of everything.\"
3995 Org-mode develops organizational tasks around a NOTES file which
3996 contains information about projects as plain text. Org-mode is
3997 implemented on top of outline-mode, which is ideal to keep the content
3998 of large files well structured. It supports ToDo items, deadlines and
3999 time stamps, which magically appear in the diary listing of the Emacs
4000 calendar. Tables are easily created with a built-in table editor.
4001 Plain text URL-like links connect to websites, emails (VM), Usenet
4002 messages (Gnus), BBDB entries, and any files related to the project.
4003 For printing and sharing of notes, an Org-mode file (or a part of it)
4004 can be exported as a structured ASCII or HTML file.
4006 The following commands are available:
4008 \\{org-mode-map}"
4010 ;; Get rid of Outline menus, they are not needed
4011 ;; Need to do this here because define-derived-mode sets up
4012 ;; the keymap so late. Still, it is a waste to call this each time
4013 ;; we switch another buffer into org-mode.
4014 (if (featurep 'xemacs)
4015 (when (boundp 'outline-mode-menu-heading)
4016 ;; Assume this is Greg's port, it used easymenu
4017 (easy-menu-remove outline-mode-menu-heading)
4018 (easy-menu-remove outline-mode-menu-show)
4019 (easy-menu-remove outline-mode-menu-hide))
4020 (define-key org-mode-map [menu-bar headings] 'undefined)
4021 (define-key org-mode-map [menu-bar hide] 'undefined)
4022 (define-key org-mode-map [menu-bar show] 'undefined))
4024 (org-load-modules-maybe)
4025 (easy-menu-add org-org-menu)
4026 (easy-menu-add org-tbl-menu)
4027 (org-install-agenda-files-menu)
4028 (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
4029 (org-add-to-invisibility-spec '(org-cwidth))
4030 (org-add-to-invisibility-spec '(org-hide-block . t))
4031 (when (featurep 'xemacs)
4032 (org-set-local 'line-move-ignore-invisible t))
4033 (org-set-local 'outline-regexp org-outline-regexp)
4034 (org-set-local 'outline-level 'org-outline-level)
4035 (when (and org-ellipsis
4036 (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
4037 (fboundp 'make-glyph-code))
4038 (unless org-display-table
4039 (setq org-display-table (make-display-table)))
4040 (set-display-table-slot
4041 org-display-table 4
4042 (vconcat (mapcar
4043 (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
4044 org-ellipsis)))
4045 (if (stringp org-ellipsis) org-ellipsis "..."))))
4046 (setq buffer-display-table org-display-table))
4047 (org-set-regexps-and-options)
4048 (when (and org-tag-faces (not org-tags-special-faces-re))
4049 ;; tag faces set outside customize.... force initialization.
4050 (org-set-tag-faces 'org-tag-faces org-tag-faces))
4051 ;; Calc embedded
4052 (org-set-local 'calc-embedded-open-mode "# ")
4053 (modify-syntax-entry ?# "<")
4054 (modify-syntax-entry ?@ "w")
4055 (if org-startup-truncated (setq truncate-lines t))
4056 (org-set-local 'font-lock-unfontify-region-function
4057 'org-unfontify-region)
4058 ;; Activate before-change-function
4059 (org-set-local 'org-table-may-need-update t)
4060 (org-add-hook 'before-change-functions 'org-before-change-function nil
4061 'local)
4062 ;; Check for running clock before killing a buffer
4063 (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
4064 ;; Paragraphs and auto-filling
4065 (org-set-autofill-regexps)
4066 (setq indent-line-function 'org-indent-line-function)
4067 (org-update-radio-target-regexp)
4068 ;; Make sure dependence stuff works reliably, even for users who set it
4069 ;; too late :-(
4070 (if org-enforce-todo-dependencies
4071 (add-hook 'org-blocker-hook
4072 'org-block-todo-from-children-or-siblings-or-parent)
4073 (remove-hook 'org-blocker-hook
4074 'org-block-todo-from-children-or-siblings-or-parent))
4075 (if org-enforce-todo-checkbox-dependencies
4076 (add-hook 'org-blocker-hook
4077 'org-block-todo-from-checkboxes)
4078 (remove-hook 'org-blocker-hook
4079 'org-block-todo-from-checkboxes))
4081 ;; Comment characters
4082 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
4083 (org-set-local 'comment-padding " ")
4085 ;; Align options lines
4086 (org-set-local
4087 'align-mode-rules-list
4088 '((org-in-buffer-settings
4089 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
4090 (modes . '(org-mode)))))
4092 ;; Imenu
4093 (org-set-local 'imenu-create-index-function
4094 'org-imenu-get-tree)
4096 ;; Make isearch reveal context
4097 (if (or (featurep 'xemacs)
4098 (not (boundp 'outline-isearch-open-invisible-function)))
4099 ;; Emacs 21 and XEmacs make use of the hook
4100 (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
4101 ;; Emacs 22 deals with this through a special variable
4102 (org-set-local 'outline-isearch-open-invisible-function
4103 (lambda (&rest ignore) (org-show-context 'isearch))))
4105 ;; If empty file that did not turn on org-mode automatically, make it to.
4106 (if (and org-insert-mode-line-in-empty-file
4107 (interactive-p)
4108 (= (point-min) (point-max)))
4109 (insert "# -*- mode: org -*-\n\n"))
4111 (unless org-inhibit-startup
4112 (when org-startup-align-all-tables
4113 (let ((bmp (buffer-modified-p)))
4114 (org-table-map-tables 'org-table-align)
4115 (set-buffer-modified-p bmp)))
4116 (when org-startup-indented
4117 (require 'org-indent)
4118 (org-indent-mode 1))
4119 (org-set-startup-visibility)))
4121 (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
4123 (defun org-current-time ()
4124 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
4125 (if (> (car org-time-stamp-rounding-minutes) 1)
4126 (let ((r (car org-time-stamp-rounding-minutes))
4127 (time (decode-time)))
4128 (apply 'encode-time
4129 (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
4130 (nthcdr 2 time))))
4131 (current-time)))
4133 ;;;; Font-Lock stuff, including the activators
4135 (defvar org-mouse-map (make-sparse-keymap))
4136 (org-defkey org-mouse-map
4137 (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
4138 (org-defkey org-mouse-map
4139 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
4140 (when org-mouse-1-follows-link
4141 (org-defkey org-mouse-map [follow-link] 'mouse-face))
4142 (when org-tab-follows-link
4143 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
4144 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
4146 (require 'font-lock)
4148 (defconst org-non-link-chars "]\t\n\r<>")
4149 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
4150 "shell" "elisp"))
4151 (defvar org-link-types-re nil
4152 "Matches a link that has a url-like prefix like \"http:\"")
4153 (defvar org-link-re-with-space nil
4154 "Matches a link with spaces, optional angular brackets around it.")
4155 (defvar org-link-re-with-space2 nil
4156 "Matches a link with spaces, optional angular brackets around it.")
4157 (defvar org-link-re-with-space3 nil
4158 "Matches a link with spaces, only for internal part in bracket links.")
4159 (defvar org-angle-link-re nil
4160 "Matches link with angular brackets, spaces are allowed.")
4161 (defvar org-plain-link-re nil
4162 "Matches plain link, without spaces.")
4163 (defvar org-bracket-link-regexp nil
4164 "Matches a link in double brackets.")
4165 (defvar org-bracket-link-analytic-regexp nil
4166 "Regular expression used to analyze links.
4167 Here is what the match groups contain after a match:
4168 1: http:
4169 2: http
4170 3: path
4171 4: [desc]
4172 5: desc")
4173 (defvar org-bracket-link-analytic-regexp++ nil
4174 "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
4175 (defvar org-any-link-re nil
4176 "Regular expression matching any link.")
4178 (defun org-make-link-regexps ()
4179 "Update the link regular expressions.
4180 This should be called after the variable `org-link-types' has changed."
4181 (setq org-link-types-re
4182 (concat
4183 "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
4184 org-link-re-with-space
4185 (concat
4186 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4187 "\\([^" org-non-link-chars " ]"
4188 "[^" org-non-link-chars "]*"
4189 "[^" org-non-link-chars " ]\\)>?")
4190 org-link-re-with-space2
4191 (concat
4192 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4193 "\\([^" org-non-link-chars " ]"
4194 "[^\t\n\r]*"
4195 "[^" org-non-link-chars " ]\\)>?")
4196 org-link-re-with-space3
4197 (concat
4198 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4199 "\\([^" org-non-link-chars " ]"
4200 "[^\t\n\r]*\\)")
4201 org-angle-link-re
4202 (concat
4203 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4204 "\\([^" org-non-link-chars " ]"
4205 "[^" org-non-link-chars "]*"
4206 "\\)>")
4207 org-plain-link-re
4208 (concat
4209 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4210 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
4211 org-bracket-link-regexp
4212 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
4213 org-bracket-link-analytic-regexp
4214 (concat
4215 "\\[\\["
4216 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
4217 "\\([^]]+\\)"
4218 "\\]"
4219 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4220 "\\]")
4221 org-bracket-link-analytic-regexp++
4222 (concat
4223 "\\[\\["
4224 "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
4225 "\\([^]]+\\)"
4226 "\\]"
4227 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4228 "\\]")
4229 org-any-link-re
4230 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
4231 org-angle-link-re "\\)\\|\\("
4232 org-plain-link-re "\\)")))
4234 (org-make-link-regexps)
4236 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
4237 "Regular expression for fast time stamp matching.")
4238 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
4239 "Regular expression for fast time stamp matching.")
4240 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4241 "Regular expression matching time strings for analysis.
4242 This one does not require the space after the date, so it can be used
4243 on a string that terminates immediately after the date.")
4244 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4245 "Regular expression matching time strings for analysis.")
4246 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
4247 "Regular expression matching time stamps, with groups.")
4248 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
4249 "Regular expression matching time stamps (also [..]), with groups.")
4250 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
4251 "Regular expression matching a time stamp range.")
4252 (defconst org-tr-regexp-both
4253 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
4254 "Regular expression matching a time stamp range.")
4255 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
4256 org-ts-regexp "\\)?")
4257 "Regular expression matching a time stamp or time stamp range.")
4258 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
4259 org-ts-regexp-both "\\)?")
4260 "Regular expression matching a time stamp or time stamp range.
4261 The time stamps may be either active or inactive.")
4263 (defvar org-emph-face nil)
4265 (defun org-do-emphasis-faces (limit)
4266 "Run through the buffer and add overlays to links."
4267 (let (rtn a)
4268 (while (and (not rtn) (re-search-forward org-emph-re limit t))
4269 (if (not (= (char-after (match-beginning 3))
4270 (char-after (match-beginning 4))))
4271 (progn
4272 (setq rtn t)
4273 (setq a (assoc (match-string 3) org-emphasis-alist))
4274 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
4275 'face
4276 (nth 1 a))
4277 (and (nth 4 a)
4278 (org-remove-flyspell-overlays-in
4279 (match-beginning 0) (match-end 0)))
4280 (add-text-properties (match-beginning 2) (match-end 2)
4281 '(font-lock-multiline t))
4282 (when org-hide-emphasis-markers
4283 (add-text-properties (match-end 4) (match-beginning 5)
4284 '(invisible org-link))
4285 (add-text-properties (match-beginning 3) (match-end 3)
4286 '(invisible org-link)))))
4287 (backward-char 1))
4288 rtn))
4290 (defun org-emphasize (&optional char)
4291 "Insert or change an emphasis, i.e. a font like bold or italic.
4292 If there is an active region, change that region to a new emphasis.
4293 If there is no region, just insert the marker characters and position
4294 the cursor between them.
4295 CHAR should be either the marker character, or the first character of the
4296 HTML tag associated with that emphasis. If CHAR is a space, the means
4297 to remove the emphasis of the selected region.
4298 If char is not given (for example in an interactive call) it
4299 will be prompted for."
4300 (interactive)
4301 (let ((eal org-emphasis-alist) e det
4302 (erc org-emphasis-regexp-components)
4303 (prompt "")
4304 (string "") beg end move tag c s)
4305 (if (org-region-active-p)
4306 (setq beg (region-beginning) end (region-end)
4307 string (buffer-substring beg end))
4308 (setq move t))
4310 (while (setq e (pop eal))
4311 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
4312 c (aref tag 0))
4313 (push (cons c (string-to-char (car e))) det)
4314 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
4315 (substring tag 1)))))
4316 (setq det (nreverse det))
4317 (unless char
4318 (message "%s" (concat "Emphasis marker or tag:" prompt))
4319 (setq char (read-char-exclusive)))
4320 (setq char (or (cdr (assoc char det)) char))
4321 (if (equal char ?\ )
4322 (setq s "" move nil)
4323 (unless (assoc (char-to-string char) org-emphasis-alist)
4324 (error "No such emphasis marker: \"%c\"" char))
4325 (setq s (char-to-string char)))
4326 (while (and (> (length string) 1)
4327 (equal (substring string 0 1) (substring string -1))
4328 (assoc (substring string 0 1) org-emphasis-alist))
4329 (setq string (substring string 1 -1)))
4330 (setq string (concat s string s))
4331 (if beg (delete-region beg end))
4332 (unless (or (bolp)
4333 (string-match (concat "[" (nth 0 erc) "\n]")
4334 (char-to-string (char-before (point)))))
4335 (insert " "))
4336 (unless (string-match (concat "[" (nth 1 erc) "\n]")
4337 (char-to-string (char-after (point))))
4338 (insert " ") (backward-char 1))
4339 (insert string)
4340 (and move (backward-char 1))))
4342 (defconst org-nonsticky-props
4343 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
4345 (defsubst org-rear-nonsticky-at (pos)
4346 (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
4348 (defun org-activate-plain-links (limit)
4349 "Run through the buffer and add overlays to links."
4350 (catch 'exit
4351 (let (f)
4352 (if (re-search-forward org-plain-link-re limit t)
4353 (progn
4354 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4355 (setq f (get-text-property (match-beginning 0) 'face))
4356 (if (or (eq f 'org-tag)
4357 (and (listp f) (memq 'org-tag f)))
4359 (add-text-properties (match-beginning 0) (match-end 0)
4360 (list 'mouse-face 'highlight
4361 'keymap org-mouse-map))
4362 (org-rear-nonsticky-at (match-end 0)))
4363 t)))))
4365 (defun org-activate-code (limit)
4366 (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
4367 (progn
4368 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4369 (remove-text-properties (match-beginning 0) (match-end 0)
4370 '(display t invisible t intangible t))
4371 t)))
4373 (defun org-fontify-meta-lines-and-blocks (limit)
4374 "Fontify #+ lines and blocks, in the correct ways."
4375 (let ((case-fold-search t))
4376 (if (re-search-forward
4377 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
4378 limit t)
4379 (let ((beg (match-beginning 0))
4380 (beg1 (line-beginning-position 2))
4381 (dc1 (downcase (match-string 2)))
4382 (dc3 (downcase (match-string 3)))
4383 end end1 quoting)
4384 (cond
4385 ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
4386 ;; a single line of backend-specific content
4387 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4388 (remove-text-properties (match-beginning 0) (match-end 0)
4389 '(display t invisible t intangible t))
4390 (add-text-properties (match-beginning 1) (match-end 3)
4391 '(font-lock-fontified t face org-meta-line))
4392 (add-text-properties (match-beginning 6) (match-end 6)
4393 '(font-lock-fontified t face org-block))
4395 ((and (match-end 4) (equal dc3 "begin"))
4396 ;; Truely a block
4397 (setq quoting (member (downcase (match-string 5))
4398 org-protecting-blocks))
4399 (when (re-search-forward
4400 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
4401 nil t) ;; on purpose, we look further than LIMIT
4402 (setq end (match-end 0) end1 (1- (match-beginning 0)))
4403 (when quoting
4404 (remove-text-properties beg end
4405 '(display t invisible t intangible t)))
4406 (add-text-properties
4407 beg end
4408 '(font-lock-fontified t font-lock-multiline t))
4409 (add-text-properties beg beg1 '(face org-meta-line))
4410 (add-text-properties end1 end '(face org-meta-line))
4411 (when quoting
4412 (add-text-properties beg1 end1 '(face org-block)))
4414 ((not (member (char-after beg) '(?\ ?\t)))
4415 ;; just any other in-buffer setting, but not indented
4416 (add-text-properties
4417 beg (match-end 0)
4418 '(font-lock-fontified t face org-meta-line))
4420 ((or (member dc1 '("begin:" "end:" "caption:" "label:"
4421 "orgtbl:" "tblfm:" "tblname:"))
4422 (and (match-end 4) (equal dc3 "attr")))
4423 (add-text-properties
4424 beg (match-end 0)
4425 '(font-lock-fontified t face org-meta-line))
4427 ((member dc3 '(" " ""))
4428 (add-text-properties
4429 beg (match-end 0)
4430 '(font-lock-fontified t face font-lock-comment-face)))
4431 (t nil))))))
4433 (defun org-activate-angle-links (limit)
4434 "Run through the buffer and add overlays to links."
4435 (if (re-search-forward org-angle-link-re limit t)
4436 (progn
4437 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4438 (add-text-properties (match-beginning 0) (match-end 0)
4439 (list 'mouse-face 'highlight
4440 'keymap org-mouse-map))
4441 (org-rear-nonsticky-at (match-end 0))
4442 t)))
4444 (defun org-activate-footnote-links (limit)
4445 "Run through the buffer and add overlays to links."
4446 (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
4447 limit t)
4448 (progn
4449 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4450 (add-text-properties (match-beginning 2) (match-end 2)
4451 (list 'mouse-face 'highlight
4452 'keymap org-mouse-map
4453 'help-echo
4454 (if (= (point-at-bol) (match-beginning 2))
4455 "Footnote definition"
4456 "Footnote reference")
4458 (org-rear-nonsticky-at (match-end 2))
4459 t)))
4461 (defun org-activate-bracket-links (limit)
4462 "Run through the buffer and add overlays to bracketed links."
4463 (if (re-search-forward org-bracket-link-regexp limit t)
4464 (let* ((help (concat "LINK: "
4465 (org-match-string-no-properties 1)))
4466 ;; FIXME: above we should remove the escapes.
4467 ;; but that requires another match, protecting match data,
4468 ;; a lot of overhead for font-lock.
4469 (ip (org-maybe-intangible
4470 (list 'invisible 'org-link
4471 'keymap org-mouse-map 'mouse-face 'highlight
4472 'font-lock-multiline t 'help-echo help)))
4473 (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
4474 'font-lock-multiline t 'help-echo help)))
4475 ;; We need to remove the invisible property here. Table narrowing
4476 ;; may have made some of this invisible.
4477 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4478 (remove-text-properties (match-beginning 0) (match-end 0)
4479 '(invisible nil))
4480 (if (match-end 3)
4481 (progn
4482 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
4483 (org-rear-nonsticky-at (match-beginning 3))
4484 (add-text-properties (match-beginning 3) (match-end 3) vp)
4485 (org-rear-nonsticky-at (match-end 3))
4486 (add-text-properties (match-end 3) (match-end 0) ip)
4487 (org-rear-nonsticky-at (match-end 0)))
4488 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
4489 (org-rear-nonsticky-at (match-beginning 1))
4490 (add-text-properties (match-beginning 1) (match-end 1) vp)
4491 (org-rear-nonsticky-at (match-end 1))
4492 (add-text-properties (match-end 1) (match-end 0) ip)
4493 (org-rear-nonsticky-at (match-end 0)))
4494 t)))
4496 (defun org-activate-dates (limit)
4497 "Run through the buffer and add overlays to dates."
4498 (if (re-search-forward org-tsr-regexp-both limit t)
4499 (progn
4500 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4501 (add-text-properties (match-beginning 0) (match-end 0)
4502 (list 'mouse-face 'highlight
4503 'keymap org-mouse-map))
4504 (org-rear-nonsticky-at (match-end 0))
4505 (when org-display-custom-times
4506 (if (match-end 3)
4507 (org-display-custom-time (match-beginning 3) (match-end 3)))
4508 (org-display-custom-time (match-beginning 1) (match-end 1)))
4509 t)))
4511 (defvar org-target-link-regexp nil
4512 "Regular expression matching radio targets in plain text.")
4513 (make-variable-buffer-local 'org-target-link-regexp)
4514 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
4515 "Regular expression matching a link target.")
4516 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
4517 "Regular expression matching a radio target.")
4518 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
4519 "Regular expression matching any target.")
4521 (defun org-activate-target-links (limit)
4522 "Run through the buffer and add overlays to target matches."
4523 (when org-target-link-regexp
4524 (let ((case-fold-search t))
4525 (if (re-search-forward org-target-link-regexp limit t)
4526 (progn
4527 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4528 (add-text-properties (match-beginning 0) (match-end 0)
4529 (list 'mouse-face 'highlight
4530 'keymap org-mouse-map
4531 'help-echo "Radio target link"
4532 'org-linked-text t))
4533 (org-rear-nonsticky-at (match-end 0))
4534 t)))))
4536 (defun org-update-radio-target-regexp ()
4537 "Find all radio targets in this file and update the regular expression."
4538 (interactive)
4539 (when (memq 'radio org-activate-links)
4540 (setq org-target-link-regexp
4541 (org-make-target-link-regexp (org-all-targets 'radio)))
4542 (org-restart-font-lock)))
4544 (defun org-hide-wide-columns (limit)
4545 (let (s e)
4546 (setq s (text-property-any (point) (or limit (point-max))
4547 'org-cwidth t))
4548 (when s
4549 (setq e (next-single-property-change s 'org-cwidth))
4550 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
4551 (goto-char e)
4552 t)))
4554 (defvar org-latex-and-specials-regexp nil
4555 "Regular expression for highlighting export special stuff.")
4556 (defvar org-match-substring-regexp)
4557 (defvar org-match-substring-with-braces-regexp)
4559 ;; This should be with the exporter code, but we also use if for font-locking
4560 (defconst org-export-html-special-string-regexps
4561 '(("\\\\-" . "&shy;")
4562 ("---\\([^-]\\)" . "&mdash;\\1")
4563 ("--\\([^-]\\)" . "&ndash;\\1")
4564 ("\\.\\.\\." . "&hellip;"))
4565 "Regular expressions for special string conversion.")
4568 (defun org-compute-latex-and-specials-regexp ()
4569 "Compute regular expression for stuff treated specially by exporters."
4570 (if (not org-highlight-latex-fragments-and-specials)
4571 (org-set-local 'org-latex-and-specials-regexp nil)
4572 (require 'org-exp)
4573 (let*
4574 ((matchers (plist-get org-format-latex-options :matchers))
4575 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
4576 org-latex-regexps)))
4577 (options (org-combine-plists (org-default-export-plist)
4578 (org-infile-export-plist)))
4579 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
4580 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
4581 (org-export-with-TeX-macros (plist-get options :TeX-macros))
4582 (org-export-html-expand (plist-get options :expand-quoted-html))
4583 (org-export-with-special-strings (plist-get options :special-strings))
4584 (re-sub
4585 (cond
4586 ((equal org-export-with-sub-superscripts '{})
4587 (list org-match-substring-with-braces-regexp))
4588 (org-export-with-sub-superscripts
4589 (list org-match-substring-regexp))
4590 (t nil)))
4591 (re-latex
4592 (if org-export-with-LaTeX-fragments
4593 (mapcar (lambda (x) (nth 1 x)) latexs)))
4594 (re-macros
4595 (if org-export-with-TeX-macros
4596 (list (concat "\\\\"
4597 (regexp-opt
4598 (append (mapcar 'car org-html-entities)
4599 (if (boundp 'org-latex-entities)
4600 (mapcar (lambda (x)
4601 (or (car-safe x) x))
4602 org-latex-entities)
4603 nil))
4604 'words))) ; FIXME
4606 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
4607 (re-special (if org-export-with-special-strings
4608 (mapcar (lambda (x) (car x))
4609 org-export-html-special-string-regexps)))
4610 (re-rest
4611 (delq nil
4612 (list
4613 (if org-export-html-expand "@<[^>\n]+>")
4614 ))))
4615 (org-set-local
4616 'org-latex-and-specials-regexp
4617 (mapconcat 'identity (append re-latex re-sub re-macros re-special
4618 re-rest) "\\|")))))
4620 (defun org-do-latex-and-special-faces (limit)
4621 "Run through the buffer and add overlays to links."
4622 (when org-latex-and-specials-regexp
4623 (let (rtn d)
4624 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
4625 limit t))
4626 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
4627 'face))
4628 '(org-code org-verbatim underline)))
4629 (progn
4630 (setq rtn t
4631 d (cond ((member (char-after (1+ (match-beginning 0)))
4632 '(?_ ?^)) 1)
4633 (t 0)))
4634 (font-lock-prepend-text-property
4635 (+ d (match-beginning 0)) (match-end 0)
4636 'face 'org-latex-and-export-specials)
4637 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
4638 '(font-lock-multiline t)))))
4639 rtn)))
4641 (defun org-restart-font-lock ()
4642 "Restart font-lock-mode, to force refontification."
4643 (when (and (boundp 'font-lock-mode) font-lock-mode)
4644 (font-lock-mode -1)
4645 (font-lock-mode 1)))
4647 (defun org-all-targets (&optional radio)
4648 "Return a list of all targets in this file.
4649 With optional argument RADIO, only find radio targets."
4650 (let ((re (if radio org-radio-target-regexp org-target-regexp))
4651 rtn)
4652 (save-excursion
4653 (goto-char (point-min))
4654 (while (re-search-forward re nil t)
4655 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
4656 rtn)))
4658 (defun org-make-target-link-regexp (targets)
4659 "Make regular expression matching all strings in TARGETS.
4660 The regular expression finds the targets also if there is a line break
4661 between words."
4662 (and targets
4663 (concat
4664 "\\<\\("
4665 (mapconcat
4666 (lambda (x)
4667 (while (string-match " +" x)
4668 (setq x (replace-match "\\s-+" t t x)))
4670 targets
4671 "\\|")
4672 "\\)\\>")))
4674 (defun org-activate-tags (limit)
4675 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
4676 (progn
4677 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4678 (add-text-properties (match-beginning 1) (match-end 1)
4679 (list 'mouse-face 'highlight
4680 'keymap org-mouse-map))
4681 (org-rear-nonsticky-at (match-end 1))
4682 t)))
4684 (defun org-outline-level ()
4685 "Compute the outline level of the heading at point.
4686 This function assumes that the cursor is at the beginning of a line matched
4687 by outline-regexp. Otherwise it returns garbage.
4688 If this is called at a normal headline, the level is the number of stars.
4689 Use `org-reduced-level' to remove the effect of `org-odd-levels'.
4690 For plain list items, if they are matched by `outline-regexp', this returns
4691 1000 plus the line indentation."
4692 (save-excursion
4693 (looking-at outline-regexp)
4694 (if (match-beginning 1)
4695 (+ (org-get-string-indentation (match-string 1)) 1000)
4696 (1- (- (match-end 0) (match-beginning 0))))))
4698 (defvar org-font-lock-keywords nil)
4700 (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
4701 "Regular expression matching a property line.")
4703 (defvar org-font-lock-hook nil
4704 "Functions to be called for special font lock stuff.")
4706 (defun org-font-lock-hook (limit)
4707 (run-hook-with-args 'org-font-lock-hook limit))
4709 (defun org-set-font-lock-defaults ()
4710 (let* ((em org-fontify-emphasized-text)
4711 (lk org-activate-links)
4712 (org-font-lock-extra-keywords
4713 (list
4714 ;; Call the hook
4715 '(org-font-lock-hook)
4716 ;; Headlines
4717 `(,(if org-fontify-whole-heading-line
4718 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
4719 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
4720 (1 (org-get-level-face 1))
4721 (2 (org-get-level-face 2))
4722 (3 (org-get-level-face 3)))
4723 ;; Table lines
4724 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
4725 (1 'org-table t))
4726 ;; Table internals
4727 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
4728 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
4729 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
4730 '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
4731 ;; Drawers
4732 (list org-drawer-regexp '(0 'org-special-keyword t))
4733 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
4734 ;; Properties
4735 (list org-property-re
4736 '(1 'org-special-keyword t)
4737 '(3 'org-property-value t))
4738 ;; Links
4739 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
4740 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
4741 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
4742 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
4743 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
4744 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
4745 (if (memq 'footnote lk) '(org-activate-footnote-links
4746 (2 'org-footnote t)))
4747 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
4748 '(org-hide-wide-columns (0 nil append))
4749 ;; TODO lines
4750 (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
4751 '(1 (org-get-todo-face 1) t))
4752 ;; DONE
4753 (if org-fontify-done-headline
4754 (list (concat "^[*]+ +\\<\\("
4755 (mapconcat 'regexp-quote org-done-keywords "\\|")
4756 "\\)\\(.*\\)")
4757 '(2 'org-headline-done t))
4758 nil)
4759 ;; Priorities
4760 '(org-font-lock-add-priority-faces)
4761 ;; Tags
4762 '(org-font-lock-add-tag-faces)
4763 ;; Special keywords
4764 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
4765 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
4766 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
4767 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
4768 ;; Emphasis
4769 (if em
4770 (if (featurep 'xemacs)
4771 '(org-do-emphasis-faces (0 nil append))
4772 '(org-do-emphasis-faces)))
4773 ;; Checkboxes
4774 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
4775 2 'org-checkbox prepend)
4776 (if org-provide-checkbox-statistics
4777 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
4778 (0 (org-get-checkbox-statistics-face) t)))
4779 ;; Description list items
4780 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
4781 2 'bold prepend)
4782 ;; ARCHIVEd headings
4783 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
4784 '(1 'org-archived prepend))
4785 ;; Specials
4786 '(org-do-latex-and-special-faces)
4787 ;; Code
4788 '(org-activate-code (1 'org-code t))
4789 ;; COMMENT
4790 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
4791 "\\|" org-quote-string "\\)\\>")
4792 '(1 'org-special-keyword t))
4793 '("^#.*" (0 'font-lock-comment-face t))
4794 ;; Blocks and meta lines
4795 '(org-fontify-meta-lines-and-blocks)
4797 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
4798 ;; Now set the full font-lock-keywords
4799 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
4800 (org-set-local 'font-lock-defaults
4801 '(org-font-lock-keywords t nil nil backward-paragraph))
4802 (kill-local-variable 'font-lock-keywords) nil))
4804 (defun org-fontify-like-in-org-mode (s &optional odd-levels)
4805 "Fontify string S like in Org-mode"
4806 (with-temp-buffer
4807 (insert s)
4808 (let ((org-odd-levels-only odd-levels))
4809 (org-mode)
4810 (font-lock-fontify-buffer)
4811 (buffer-string))))
4813 (defvar org-m nil)
4814 (defvar org-l nil)
4815 (defvar org-f nil)
4816 (defun org-get-level-face (n)
4817 "Get the right face for match N in font-lock matching of headlines."
4818 (setq org-l (- (match-end 2) (match-beginning 1) 1))
4819 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
4820 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
4821 (cond
4822 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
4823 ((eq n 2) org-f)
4824 (t (if org-level-color-stars-only nil org-f))))
4826 (defun org-get-todo-face (kwd)
4827 "Get the right face for a TODO keyword KWD.
4828 If KWD is a number, get the corresponding match group."
4829 (if (numberp kwd) (setq kwd (match-string kwd)))
4830 (or (cdr (assoc kwd org-todo-keyword-faces))
4831 (and (member kwd org-done-keywords) 'org-done)
4832 'org-todo))
4834 (defun org-font-lock-add-tag-faces (limit)
4835 "Add the special tag faces."
4836 (when (and org-tag-faces org-tags-special-faces-re)
4837 (while (re-search-forward org-tags-special-faces-re limit t)
4838 (add-text-properties (match-beginning 1) (match-end 1)
4839 (list 'face (org-get-tag-face 1)
4840 'font-lock-fontified t))
4841 (backward-char 1))))
4843 (defun org-font-lock-add-priority-faces (limit)
4844 "Add the special priority faces."
4845 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
4846 (add-text-properties
4847 (match-beginning 0) (match-end 0)
4848 (list 'face (or (cdr (assoc (char-after (match-beginning 1))
4849 org-priority-faces))
4850 'org-special-keyword)
4851 'font-lock-fontified t))))
4853 (defun org-get-tag-face (kwd)
4854 "Get the right face for a TODO keyword KWD.
4855 If KWD is a number, get the corresponding match group."
4856 (if (numberp kwd) (setq kwd (match-string kwd)))
4857 (or (cdr (assoc kwd org-tag-faces))
4858 'org-tag))
4860 (defun org-unfontify-region (beg end &optional maybe_loudly)
4861 "Remove fontification and activation overlays from links."
4862 (font-lock-default-unfontify-region beg end)
4863 (let* ((buffer-undo-list t)
4864 (inhibit-read-only t) (inhibit-point-motion-hooks t)
4865 (inhibit-modification-hooks t)
4866 deactivate-mark buffer-file-name buffer-file-truename)
4867 (remove-text-properties beg end
4868 '(mouse-face t keymap t org-linked-text t
4869 invisible t intangible t
4870 line-prefix t wrap-prefix t
4871 org-no-flyspell t))))
4873 ;;;; Visibility cycling, including org-goto and indirect buffer
4875 ;;; Cycling
4877 (defvar org-cycle-global-status nil)
4878 (make-variable-buffer-local 'org-cycle-global-status)
4879 (defvar org-cycle-subtree-status nil)
4880 (make-variable-buffer-local 'org-cycle-subtree-status)
4882 ;;;###autoload
4884 (defvar org-inlinetask-min-level)
4886 (defun org-cycle (&optional arg)
4887 "TAB-action and visibility cycling for Org-mode.
4889 This is the command invoked in Org-mode by the TAB key. Its main purpose
4890 is outine visibility cycling, but it also invokes other actions
4891 in special contexts.
4893 - When this function is called with a prefix argument, rotate the entire
4894 buffer through 3 states (global cycling)
4895 1. OVERVIEW: Show only top-level headlines.
4896 2. CONTENTS: Show all headlines of all levels, but no body text.
4897 3. SHOW ALL: Show everything.
4898 When called with two `C-u C-u' prefixes, switch to the startup visibility,
4899 determined by the variable `org-startup-folded', and by any VISIBILITY
4900 properties in the buffer.
4901 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
4902 including any drawers.
4904 - When inside a table, re-align the table and move to the next field.
4906 - When point is at the beginning of a headline, rotate the subtree started
4907 by this line through 3 different states (local cycling)
4908 1. FOLDED: Only the main headline is shown.
4909 2. CHILDREN: The main headline and the direct children are shown.
4910 From this state, you can move to one of the children
4911 and zoom in further.
4912 3. SUBTREE: Show the entire subtree, including body text.
4913 If there is no subtree, switch directly from CHILDREN to FOLDED.
4915 - When there is a numeric prefix, go up to a heading with level ARG, do
4916 a `show-subtree' and return to the previous cursor position. If ARG
4917 is negative, go up that many levels.
4919 - When point is not at the beginning of a headline, execute the global
4920 binding for TAB, which is re-indenting the line. See the option
4921 `org-cycle-emulate-tab' for details.
4923 - Special case: if point is at the beginning of the buffer and there is
4924 no headline in line 1, this function will act as if called with prefix arg.
4925 But only if also the variable `org-cycle-global-at-bob' is t."
4926 (interactive "P")
4927 (org-load-modules-maybe)
4928 (unless (run-hook-with-args-until-success 'org-tab-first-hook)
4929 (let* ((limit-level
4930 (or org-cycle-max-level
4931 (and (boundp 'org-inlinetask-min-level)
4932 org-inlinetask-min-level
4933 (1- org-inlinetask-min-level))))
4934 (nstars (and limit-level
4935 (if org-odd-levels-only
4936 (and limit-level (1- (* limit-level 2)))
4937 limit-level)))
4938 (outline-regexp
4939 (cond
4940 ((not (org-mode-p)) outline-regexp)
4941 ((or (eq org-cycle-include-plain-lists 'integrate)
4942 (and org-cycle-include-plain-lists (org-at-item-p)))
4943 (concat "\\(?:\\*"
4944 (if nstars (format "\\{1,%d\\}" nstars) "+")
4945 " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
4946 (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
4947 (bob-special (and org-cycle-global-at-bob (bobp)
4948 (not (looking-at outline-regexp))))
4949 (org-cycle-hook
4950 (if bob-special
4951 (delq 'org-optimize-window-after-visibility-change
4952 (copy-sequence org-cycle-hook))
4953 org-cycle-hook))
4954 (pos (point)))
4956 (if (or bob-special (equal arg '(4)))
4957 ;; special case: use global cycling
4958 (setq arg t))
4960 (cond
4962 ((equal arg '(16))
4963 (org-set-startup-visibility)
4964 (message "Startup visibility, plus VISIBILITY properties"))
4966 ((equal arg '(64))
4967 (show-all)
4968 (message "Entire buffer visible, including drawers"))
4970 ((org-at-table-p 'any)
4971 ;; Enter the table or move to the next field in the table
4972 (or (org-table-recognize-table.el)
4973 (progn
4974 (if arg (org-table-edit-field t)
4975 (org-table-justify-field-maybe)
4976 (call-interactively 'org-table-next-field)))))
4978 ((run-hook-with-args-until-success
4979 'org-tab-after-check-for-table-hook))
4981 ((eq arg t) ;; Global cycling
4982 (org-cycle-internal-global))
4984 ((and org-drawers org-drawer-regexp
4985 (save-excursion
4986 (beginning-of-line 1)
4987 (looking-at org-drawer-regexp)))
4988 ;; Toggle block visibility
4989 (org-flag-drawer
4990 (not (get-char-property (match-end 0) 'invisible))))
4992 ((integerp arg)
4993 ;; Show-subtree, ARG levels up from here.
4994 (save-excursion
4995 (org-back-to-heading)
4996 (outline-up-heading (if (< arg 0) (- arg)
4997 (- (funcall outline-level) arg)))
4998 (org-show-subtree)))
5000 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
5001 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
5003 (org-cycle-internal-local))
5005 ;; TAB emulation and template completion
5006 (buffer-read-only (org-back-to-heading))
5008 ((run-hook-with-args-until-success
5009 'org-tab-after-check-for-cycling-hook))
5011 ((org-try-structure-completion))
5013 ((org-try-cdlatex-tab))
5015 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
5016 (or (not (bolp))
5017 (not (looking-at outline-regexp))))
5018 (call-interactively (global-key-binding "\t")))
5020 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
5021 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
5022 (or (and (eq org-cycle-emulate-tab 'white)
5023 (= (match-end 0) (point-at-eol)))
5024 (and (eq org-cycle-emulate-tab 'whitestart)
5025 (>= (match-end 0) pos))))
5027 (eq org-cycle-emulate-tab t))
5028 (call-interactively (global-key-binding "\t")))
5030 (t (save-excursion
5031 (org-back-to-heading)
5032 (org-cycle)))))))
5034 (defun org-cycle-internal-global ()
5035 "Do the global cycling action."
5036 (cond
5037 ((and (eq last-command this-command)
5038 (eq org-cycle-global-status 'overview))
5039 ;; We just created the overview - now do table of contents
5040 ;; This can be slow in very large buffers, so indicate action
5041 (run-hook-with-args 'org-pre-cycle-hook 'contents)
5042 (message "CONTENTS...")
5043 (org-content)
5044 (message "CONTENTS...done")
5045 (setq org-cycle-global-status 'contents)
5046 (run-hook-with-args 'org-cycle-hook 'contents))
5048 ((and (eq last-command this-command)
5049 (eq org-cycle-global-status 'contents))
5050 ;; We just showed the table of contents - now show everything
5051 (run-hook-with-args 'org-pre-cycle-hook 'all)
5052 (show-all)
5053 (message "SHOW ALL")
5054 (setq org-cycle-global-status 'all)
5055 (run-hook-with-args 'org-cycle-hook 'all))
5058 ;; Default action: go to overview
5059 (run-hook-with-args 'org-pre-cycle-hook 'overview)
5060 (org-overview)
5061 (message "OVERVIEW")
5062 (setq org-cycle-global-status 'overview)
5063 (run-hook-with-args 'org-cycle-hook 'overview))))
5065 (defun org-cycle-internal-local ()
5066 "Do the local cycling action."
5067 (org-back-to-heading)
5068 (let ((goal-column 0) eoh eol eos level has-children children-skipped)
5069 ;; First, some boundaries
5070 (save-excursion
5071 (org-back-to-heading)
5072 (setq level (funcall outline-level))
5073 (save-excursion
5074 (beginning-of-line 2)
5075 (if (or (featurep 'xemacs) (<= emacs-major-version 21))
5076 ; XEmacs does not have `next-single-char-property-change'
5077 ; I'm not sure about Emacs 21.
5078 (while (and (not (eobp)) ;; this is like `next-line'
5079 (get-char-property (1- (point)) 'invisible))
5080 (beginning-of-line 2))
5081 (while (and (not (eobp)) ;; this is like `next-line'
5082 (get-char-property (1- (point)) 'invisible))
5083 (goto-char (next-single-char-property-change (point) 'invisible))
5084 ;;;??? (or (bolp) (beginning-of-line 2))))
5085 (and (eolp) (beginning-of-line 2))))
5086 (setq eol (point)))
5087 (outline-end-of-heading) (setq eoh (point))
5088 (save-excursion
5089 (outline-next-heading)
5090 (setq has-children (and (org-at-heading-p t)
5091 (> (funcall outline-level) level))))
5092 (org-end-of-subtree t)
5093 (unless (eobp)
5094 (skip-chars-forward " \t\n")
5095 (beginning-of-line 1) ; in case this is an item
5097 (setq eos (if (eobp) (point) (1- (point)))))
5098 ;; Find out what to do next and set `this-command'
5099 (cond
5100 ((= eos eoh)
5101 ;; Nothing is hidden behind this heading
5102 (run-hook-with-args 'org-pre-cycle-hook 'empty)
5103 (message "EMPTY ENTRY")
5104 (setq org-cycle-subtree-status nil)
5105 (save-excursion
5106 (goto-char eos)
5107 (outline-next-heading)
5108 (if (org-invisible-p) (org-flag-heading nil))))
5109 ((and (or (>= eol eos)
5110 (not (string-match "\\S-" (buffer-substring eol eos))))
5111 (or has-children
5112 (not (setq children-skipped
5113 org-cycle-skip-children-state-if-no-children))))
5114 ;; Entire subtree is hidden in one line: children view
5115 (run-hook-with-args 'org-pre-cycle-hook 'children)
5116 (org-show-entry)
5117 (show-children)
5118 (message "CHILDREN")
5119 (save-excursion
5120 (goto-char eos)
5121 (outline-next-heading)
5122 (if (org-invisible-p) (org-flag-heading nil)))
5123 (setq org-cycle-subtree-status 'children)
5124 (run-hook-with-args 'org-cycle-hook 'children))
5125 ((or children-skipped
5126 (and (eq last-command this-command)
5127 (eq org-cycle-subtree-status 'children)))
5128 ;; We just showed the children, or no children are there,
5129 ;; now show everything.
5130 (run-hook-with-args 'org-pre-cycle-hook 'subtree)
5131 (org-show-subtree)
5132 (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
5133 (setq org-cycle-subtree-status 'subtree)
5134 (run-hook-with-args 'org-cycle-hook 'subtree))
5136 ;; Default action: hide the subtree.
5137 (run-hook-with-args 'org-pre-cycle-hook 'folded)
5138 (hide-subtree)
5139 (message "FOLDED")
5140 (setq org-cycle-subtree-status 'folded)
5141 (run-hook-with-args 'org-cycle-hook 'folded)))))
5143 ;;;###autoload
5144 (defun org-global-cycle (&optional arg)
5145 "Cycle the global visibility. For details see `org-cycle'.
5146 With C-u prefix arg, switch to startup visibility.
5147 With a numeric prefix, show all headlines up to that level."
5148 (interactive "P")
5149 (let ((org-cycle-include-plain-lists
5150 (if (org-mode-p) org-cycle-include-plain-lists nil)))
5151 (cond
5152 ((integerp arg)
5153 (show-all)
5154 (hide-sublevels arg)
5155 (setq org-cycle-global-status 'contents))
5156 ((equal arg '(4))
5157 (org-set-startup-visibility)
5158 (message "Startup visibility, plus VISIBILITY properties."))
5160 (org-cycle '(4))))))
5162 (defun org-set-startup-visibility ()
5163 "Set the visibility required by startup options and properties."
5164 (cond
5165 ((eq org-startup-folded t)
5166 (org-cycle '(4)))
5167 ((eq org-startup-folded 'content)
5168 (let ((this-command 'org-cycle) (last-command 'org-cycle))
5169 (org-cycle '(4)) (org-cycle '(4)))))
5170 (unless (eq org-startup-folded 'showeverything)
5171 (if org-hide-block-startup (org-hide-block-all))
5172 (org-set-visibility-according-to-property 'no-cleanup)
5173 (org-cycle-hide-archived-subtrees 'all)
5174 (org-cycle-hide-drawers 'all)
5175 (org-cycle-show-empty-lines 'all)))
5177 (defun org-set-visibility-according-to-property (&optional no-cleanup)
5178 "Switch subtree visibilities according to :VISIBILITY: property."
5179 (interactive)
5180 (let (org-show-entry-below state)
5181 (save-excursion
5182 (goto-char (point-min))
5183 (while (re-search-forward
5184 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
5185 nil t)
5186 (setq state (match-string 1))
5187 (save-excursion
5188 (org-back-to-heading t)
5189 (hide-subtree)
5190 (org-reveal)
5191 (cond
5192 ((equal state '("fold" "folded"))
5193 (hide-subtree))
5194 ((equal state "children")
5195 (org-show-hidden-entry)
5196 (show-children))
5197 ((equal state "content")
5198 (save-excursion
5199 (save-restriction
5200 (org-narrow-to-subtree)
5201 (org-content))))
5202 ((member state '("all" "showall"))
5203 (show-subtree)))))
5204 (unless no-cleanup
5205 (org-cycle-hide-archived-subtrees 'all)
5206 (org-cycle-hide-drawers 'all)
5207 (org-cycle-show-empty-lines 'all)))))
5209 (defun org-overview ()
5210 "Switch to overview mode, showing only top-level headlines.
5211 Really, this shows all headlines with level equal or greater than the level
5212 of the first headline in the buffer. This is important, because if the
5213 first headline is not level one, then (hide-sublevels 1) gives confusing
5214 results."
5215 (interactive)
5216 (let ((level (save-excursion
5217 (goto-char (point-min))
5218 (if (re-search-forward (concat "^" outline-regexp) nil t)
5219 (progn
5220 (goto-char (match-beginning 0))
5221 (funcall outline-level))))))
5222 (and level (hide-sublevels level))))
5224 (defun org-content (&optional arg)
5225 "Show all headlines in the buffer, like a table of contents.
5226 With numerical argument N, show content up to level N."
5227 (interactive "P")
5228 (save-excursion
5229 ;; Visit all headings and show their offspring
5230 (and (integerp arg) (org-overview))
5231 (goto-char (point-max))
5232 (catch 'exit
5233 (while (and (progn (condition-case nil
5234 (outline-previous-visible-heading 1)
5235 (error (goto-char (point-min))))
5237 (looking-at outline-regexp))
5238 (if (integerp arg)
5239 (show-children (1- arg))
5240 (show-branches))
5241 (if (bobp) (throw 'exit nil))))))
5244 (defun org-optimize-window-after-visibility-change (state)
5245 "Adjust the window after a change in outline visibility.
5246 This function is the default value of the hook `org-cycle-hook'."
5247 (when (get-buffer-window (current-buffer))
5248 (cond
5249 ((eq state 'content) nil)
5250 ((eq state 'all) nil)
5251 ((eq state 'folded) nil)
5252 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
5253 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
5255 ;; FIXME: no longer in use
5256 (defun org-compact-display-after-subtree-move ()
5257 "Show a compacter version of the tree of the entry's parent."
5258 (save-excursion
5259 (if (org-up-heading-safe)
5260 (progn
5261 (hide-subtree)
5262 (show-entry)
5263 (show-children)
5264 (org-cycle-show-empty-lines 'children)
5265 (org-cycle-hide-drawers 'children))
5266 (org-overview))))
5268 (defun org-remove-empty-overlays-at (pos)
5269 "Remove outline overlays that do not contain non-white stuff."
5270 (mapc
5271 (lambda (o)
5272 (and (eq 'outline (org-overlay-get o 'invisible))
5273 (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
5274 (org-overlay-end o))))
5275 (org-delete-overlay o)))
5276 (org-overlays-at pos)))
5278 (defun org-clean-visibility-after-subtree-move ()
5279 "Fix visibility issues after moving a subtree."
5280 ;; First, find a reasonable region to look at:
5281 ;; Start two siblings above, end three below
5282 (let* ((beg (save-excursion
5283 (and (org-get-last-sibling)
5284 (org-get-last-sibling))
5285 (point)))
5286 (end (save-excursion
5287 (and (org-get-next-sibling)
5288 (org-get-next-sibling)
5289 (org-get-next-sibling))
5290 (if (org-at-heading-p)
5291 (point-at-eol)
5292 (point))))
5293 (level (looking-at "\\*+"))
5294 (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
5295 (save-excursion
5296 (save-restriction
5297 (narrow-to-region beg end)
5298 (when re
5299 ;; Properly fold already folded siblings
5300 (goto-char (point-min))
5301 (while (re-search-forward re nil t)
5302 (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
5303 (hide-entry))))
5304 (org-cycle-show-empty-lines 'overview)
5305 (org-cycle-hide-drawers 'overview)))))
5307 (defun org-cycle-show-empty-lines (state)
5308 "Show empty lines above all visible headlines.
5309 The region to be covered depends on STATE when called through
5310 `org-cycle-hook'. Lisp program can use t for STATE to get the
5311 entire buffer covered. Note that an empty line is only shown if there
5312 are at least `org-cycle-separator-lines' empty lines before the headline."
5313 (when (not (= org-cycle-separator-lines 0))
5314 (save-excursion
5315 (let* ((n (abs org-cycle-separator-lines))
5316 (re (cond
5317 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
5318 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
5319 (t (let ((ns (number-to-string (- n 2))))
5320 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
5321 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
5322 beg end b e)
5323 (cond
5324 ((memq state '(overview contents t))
5325 (setq beg (point-min) end (point-max)))
5326 ((memq state '(children folded))
5327 (setq beg (point) end (progn (org-end-of-subtree t t)
5328 (beginning-of-line 2)
5329 (point)))))
5330 (when beg
5331 (goto-char beg)
5332 (while (re-search-forward re end t)
5333 (unless (get-char-property (match-end 1) 'invisible)
5334 (setq e (match-end 1))
5335 (if (< org-cycle-separator-lines 0)
5336 (setq b (save-excursion
5337 (goto-char (match-beginning 0))
5338 (org-back-over-empty-lines)
5339 (if (save-excursion
5340 (goto-char (max (point-min) (1- (point))))
5341 (org-on-heading-p))
5342 (1- (point))
5343 (point))))
5344 (setq b (match-beginning 1)))
5345 (outline-flag-region b e nil)))))))
5346 ;; Never hide empty lines at the end of the file.
5347 (save-excursion
5348 (goto-char (point-max))
5349 (outline-previous-heading)
5350 (outline-end-of-heading)
5351 (if (and (looking-at "[ \t\n]+")
5352 (= (match-end 0) (point-max)))
5353 (outline-flag-region (point) (match-end 0) nil))))
5355 (defun org-show-empty-lines-in-parent ()
5356 "Move to the parent and re-show empty lines before visible headlines."
5357 (save-excursion
5358 (let ((context (if (org-up-heading-safe) 'children 'overview)))
5359 (org-cycle-show-empty-lines context))))
5361 (defun org-files-list ()
5362 "Return `org-agenda-files' list, plus all open org-mode files.
5363 This is useful for operations that need to scan all of a user's
5364 open and agenda-wise Org files."
5365 (let ((files (mapcar 'expand-file-name (org-agenda-files))))
5366 (dolist (buf (buffer-list))
5367 (with-current-buffer buf
5368 (if (and (eq major-mode 'org-mode) (buffer-file-name))
5369 (let ((file (expand-file-name (buffer-file-name))))
5370 (unless (member file files)
5371 (push file files))))))
5372 files))
5374 (defsubst org-entry-beginning-position ()
5375 "Return the beginning position of the current entry."
5376 (save-excursion (outline-back-to-heading t) (point)))
5378 (defsubst org-entry-end-position ()
5379 "Return the end position of the current entry."
5380 (save-excursion (outline-next-heading) (point)))
5382 (defun org-cycle-hide-drawers (state)
5383 "Re-hide all drawers after a visibility state change."
5384 (when (and (org-mode-p)
5385 (not (memq state '(overview folded contents))))
5386 (save-excursion
5387 (let* ((globalp (memq state '(contents all)))
5388 (beg (if globalp (point-min) (point)))
5389 (end (if globalp (point-max)
5390 (if (eq state 'children)
5391 (save-excursion (outline-next-heading) (point))
5392 (org-end-of-subtree t)))))
5393 (goto-char beg)
5394 (while (re-search-forward org-drawer-regexp end t)
5395 (org-flag-drawer t))))))
5397 (defun org-flag-drawer (flag)
5398 (save-excursion
5399 (beginning-of-line 1)
5400 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
5401 (let ((b (match-end 0))
5402 (outline-regexp org-outline-regexp))
5403 (if (re-search-forward
5404 "^[ \t]*:END:"
5405 (save-excursion (outline-next-heading) (point)) t)
5406 (outline-flag-region b (point-at-eol) flag)
5407 (error ":END: line missing at position %s" b))))))
5409 (defun org-subtree-end-visible-p ()
5410 "Is the end of the current subtree visible?"
5411 (pos-visible-in-window-p
5412 (save-excursion (org-end-of-subtree t) (point))))
5414 (defun org-first-headline-recenter (&optional N)
5415 "Move cursor to the first headline and recenter the headline.
5416 Optional argument N means, put the headline into the Nth line of the window."
5417 (goto-char (point-min))
5418 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
5419 (beginning-of-line)
5420 (recenter (prefix-numeric-value N))))
5422 ;;; Folding of blocks
5424 (defconst org-block-regexp
5426 "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
5427 "Regular expression for hiding blocks.")
5429 (defvar org-hide-block-overlays nil
5430 "Overays hiding blocks.")
5431 (make-variable-buffer-local 'org-hide-block-overlays)
5433 (defun org-block-map (function &optional start end)
5434 "Call func at the head of all source blocks in the current
5435 buffer. Optional arguments START and END can be used to limit
5436 the range."
5437 (let ((start (or start (point-min)))
5438 (end (or end (point-max))))
5439 (save-excursion
5440 (goto-char start)
5441 (while (and (< (point) end) (re-search-forward org-block-regexp end t))
5442 (save-excursion
5443 (save-match-data
5444 (goto-char (match-beginning 0))
5445 (funcall function)))))))
5447 (defun org-hide-block-toggle-all ()
5448 "Toggle the visibility of all blocks in the current buffer."
5449 (org-block-map #'org-hide-block-toggle))
5451 (defun org-hide-block-all ()
5452 "Fold all blocks in the current buffer."
5453 (interactive)
5454 (org-show-block-all)
5455 (org-block-map #'org-hide-block-toggle-maybe))
5457 (defun org-show-block-all ()
5458 "Unfold all blocks in the current buffer."
5459 (mapc 'org-delete-overlay org-hide-block-overlays)
5460 (setq org-hide-block-overlays nil))
5462 (defun org-hide-block-toggle-maybe ()
5463 "Toggle visibility of block at point."
5464 (interactive)
5465 (let ((case-fold-search t))
5466 (if (save-excursion
5467 (beginning-of-line 1)
5468 (looking-at org-block-regexp))
5469 (progn (org-hide-block-toggle)
5470 t) ;; to signal that we took action
5471 nil))) ;; to signal that we did not
5473 (defun org-hide-block-toggle (&optional force)
5474 "Toggle the visibility of the current block."
5475 (interactive)
5476 (save-excursion
5477 (beginning-of-line)
5478 (if (re-search-forward org-block-regexp nil t)
5479 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
5480 (end (match-end 0)) ;; end of entire body
5482 (if (memq t (mapcar (lambda (overlay)
5483 (eq (org-overlay-get overlay 'invisible)
5484 'org-hide-block))
5485 (org-overlays-at start)))
5486 (if (or (not force) (eq force 'off))
5487 (mapc (lambda (ov)
5488 (when (member ov org-hide-block-overlays)
5489 (setq org-hide-block-overlays
5490 (delq ov org-hide-block-overlays)))
5491 (when (eq (org-overlay-get ov 'invisible)
5492 'org-hide-block)
5493 (org-delete-overlay ov)))
5494 (org-overlays-at start)))
5495 (setq ov (org-make-overlay start end))
5496 (org-overlay-put ov 'invisible 'org-hide-block)
5497 ;; make the block accessible to isearch
5498 (org-overlay-put
5499 ov 'isearch-open-invisible
5500 (lambda (ov)
5501 (when (member ov org-hide-block-overlays)
5502 (setq org-hide-block-overlays
5503 (delq ov org-hide-block-overlays)))
5504 (when (eq (org-overlay-get ov 'invisible)
5505 'org-hide-block)
5506 (org-delete-overlay ov))))
5507 (push ov org-hide-block-overlays)))
5508 (error "Not looking at a source block"))))
5510 ;; org-tab-after-check-for-cycling-hook
5511 (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
5512 ;; Remove overlays when changing major mode
5513 (add-hook 'org-mode-hook
5514 (lambda () (org-add-hook 'change-major-mode-hook
5515 'org-show-block-all 'append 'local)))
5517 ;;; Org-goto
5519 (defvar org-goto-window-configuration nil)
5520 (defvar org-goto-marker nil)
5521 (defvar org-goto-map
5522 (let ((map (make-sparse-keymap)))
5523 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
5524 (while (setq cmd (pop cmds))
5525 (substitute-key-definition cmd cmd map global-map)))
5526 (suppress-keymap map)
5527 (org-defkey map "\C-m" 'org-goto-ret)
5528 (org-defkey map [(return)] 'org-goto-ret)
5529 (org-defkey map [(left)] 'org-goto-left)
5530 (org-defkey map [(right)] 'org-goto-right)
5531 (org-defkey map [(control ?g)] 'org-goto-quit)
5532 (org-defkey map "\C-i" 'org-cycle)
5533 (org-defkey map [(tab)] 'org-cycle)
5534 (org-defkey map [(down)] 'outline-next-visible-heading)
5535 (org-defkey map [(up)] 'outline-previous-visible-heading)
5536 (if org-goto-auto-isearch
5537 (if (fboundp 'define-key-after)
5538 (define-key-after map [t] 'org-goto-local-auto-isearch)
5539 nil)
5540 (org-defkey map "q" 'org-goto-quit)
5541 (org-defkey map "n" 'outline-next-visible-heading)
5542 (org-defkey map "p" 'outline-previous-visible-heading)
5543 (org-defkey map "f" 'outline-forward-same-level)
5544 (org-defkey map "b" 'outline-backward-same-level)
5545 (org-defkey map "u" 'outline-up-heading))
5546 (org-defkey map "/" 'org-occur)
5547 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
5548 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
5549 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
5550 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
5551 (org-defkey map "\C-c\C-u" 'outline-up-heading)
5552 map))
5554 (defconst org-goto-help
5555 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
5556 RET=jump to location [Q]uit and return to previous location
5557 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
5559 (defvar org-goto-start-pos) ; dynamically scoped parameter
5561 ;; FIXME: Docstring doe not mention both interfaces
5562 (defun org-goto (&optional alternative-interface)
5563 "Look up a different location in the current file, keeping current visibility.
5565 When you want look-up or go to a different location in a document, the
5566 fastest way is often to fold the entire buffer and then dive into the tree.
5567 This method has the disadvantage, that the previous location will be folded,
5568 which may not be what you want.
5570 This command works around this by showing a copy of the current buffer
5571 in an indirect buffer, in overview mode. You can dive into the tree in
5572 that copy, use org-occur and incremental search to find a location.
5573 When pressing RET or `Q', the command returns to the original buffer in
5574 which the visibility is still unchanged. After RET is will also jump to
5575 the location selected in the indirect buffer and expose the
5576 the headline hierarchy above."
5577 (interactive "P")
5578 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
5579 (org-refile-use-outline-path t)
5580 (org-refile-target-verify-function nil)
5581 (interface
5582 (if (not alternative-interface)
5583 org-goto-interface
5584 (if (eq org-goto-interface 'outline)
5585 'outline-path-completion
5586 'outline)))
5587 (org-goto-start-pos (point))
5588 (selected-point
5589 (if (eq interface 'outline)
5590 (car (org-get-location (current-buffer) org-goto-help))
5591 (nth 3 (org-refile-get-location "Goto: ")))))
5592 (if selected-point
5593 (progn
5594 (org-mark-ring-push org-goto-start-pos)
5595 (goto-char selected-point)
5596 (if (or (org-invisible-p) (org-invisible-p2))
5597 (org-show-context 'org-goto)))
5598 (message "Quit"))))
5600 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
5601 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
5602 (defvar org-goto-local-auto-isearch-map) ; defined below
5604 (defun org-get-location (buf help)
5605 "Let the user select a location in the Org-mode buffer BUF.
5606 This function uses a recursive edit. It returns the selected position
5607 or nil."
5608 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
5609 (isearch-hide-immediately nil)
5610 (isearch-search-fun-function
5611 (lambda () 'org-goto-local-search-headings))
5612 (org-goto-selected-point org-goto-exit-command))
5613 (save-excursion
5614 (save-window-excursion
5615 (delete-other-windows)
5616 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
5617 (switch-to-buffer
5618 (condition-case nil
5619 (make-indirect-buffer (current-buffer) "*org-goto*")
5620 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
5621 (with-output-to-temp-buffer "*Help*"
5622 (princ help))
5623 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
5624 (setq buffer-read-only nil)
5625 (let ((org-startup-truncated t)
5626 (org-startup-folded nil)
5627 (org-startup-align-all-tables nil))
5628 (org-mode)
5629 (org-overview))
5630 (setq buffer-read-only t)
5631 (if (and (boundp 'org-goto-start-pos)
5632 (integer-or-marker-p org-goto-start-pos))
5633 (let ((org-show-hierarchy-above t)
5634 (org-show-siblings t)
5635 (org-show-following-heading t))
5636 (goto-char org-goto-start-pos)
5637 (and (org-invisible-p) (org-show-context)))
5638 (goto-char (point-min)))
5639 (let (org-special-ctrl-a/e) (org-beginning-of-line))
5640 (message "Select location and press RET")
5641 (use-local-map org-goto-map)
5642 (recursive-edit)
5644 (kill-buffer "*org-goto*")
5645 (cons org-goto-selected-point org-goto-exit-command)))
5647 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
5648 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
5649 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
5650 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
5652 (defun org-goto-local-search-headings (string bound noerror)
5653 "Search and make sure that any matches are in headlines."
5654 (catch 'return
5655 (while (if isearch-forward
5656 (search-forward string bound noerror)
5657 (search-backward string bound noerror))
5658 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
5659 (and (member :headline context)
5660 (not (member :tags context))))
5661 (throw 'return (point))))))
5663 (defun org-goto-local-auto-isearch ()
5664 "Start isearch."
5665 (interactive)
5666 (goto-char (point-min))
5667 (let ((keys (this-command-keys)))
5668 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
5669 (isearch-mode t)
5670 (isearch-process-search-char (string-to-char keys)))))
5672 (defun org-goto-ret (&optional arg)
5673 "Finish `org-goto' by going to the new location."
5674 (interactive "P")
5675 (setq org-goto-selected-point (point)
5676 org-goto-exit-command 'return)
5677 (throw 'exit nil))
5679 (defun org-goto-left ()
5680 "Finish `org-goto' by going to the new location."
5681 (interactive)
5682 (if (org-on-heading-p)
5683 (progn
5684 (beginning-of-line 1)
5685 (setq org-goto-selected-point (point)
5686 org-goto-exit-command 'left)
5687 (throw 'exit nil))
5688 (error "Not on a heading")))
5690 (defun org-goto-right ()
5691 "Finish `org-goto' by going to the new location."
5692 (interactive)
5693 (if (org-on-heading-p)
5694 (progn
5695 (setq org-goto-selected-point (point)
5696 org-goto-exit-command 'right)
5697 (throw 'exit nil))
5698 (error "Not on a heading")))
5700 (defun org-goto-quit ()
5701 "Finish `org-goto' without cursor motion."
5702 (interactive)
5703 (setq org-goto-selected-point nil)
5704 (setq org-goto-exit-command 'quit)
5705 (throw 'exit nil))
5707 ;;; Indirect buffer display of subtrees
5709 (defvar org-indirect-dedicated-frame nil
5710 "This is the frame being used for indirect tree display.")
5711 (defvar org-last-indirect-buffer nil)
5713 (defun org-tree-to-indirect-buffer (&optional arg)
5714 "Create indirect buffer and narrow it to current subtree.
5715 With numerical prefix ARG, go up to this level and then take that tree.
5716 If ARG is negative, go up that many levels.
5717 If `org-indirect-buffer-display' is not `new-frame', the command removes the
5718 indirect buffer previously made with this command, to avoid proliferation of
5719 indirect buffers. However, when you call the command with a `C-u' prefix, or
5720 when `org-indirect-buffer-display' is `new-frame', the last buffer
5721 is kept so that you can work with several indirect buffers at the same time.
5722 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
5723 requests that a new frame be made for the new buffer, so that the dedicated
5724 frame is not changed."
5725 (interactive "P")
5726 (let ((cbuf (current-buffer))
5727 (cwin (selected-window))
5728 (pos (point))
5729 beg end level heading ibuf)
5730 (save-excursion
5731 (org-back-to-heading t)
5732 (when (numberp arg)
5733 (setq level (org-outline-level))
5734 (if (< arg 0) (setq arg (+ level arg)))
5735 (while (> (setq level (org-outline-level)) arg)
5736 (outline-up-heading 1 t)))
5737 (setq beg (point)
5738 heading (org-get-heading))
5739 (org-end-of-subtree t t) (setq end (point)))
5740 (if (and (buffer-live-p org-last-indirect-buffer)
5741 (not (eq org-indirect-buffer-display 'new-frame))
5742 (not arg))
5743 (kill-buffer org-last-indirect-buffer))
5744 (setq ibuf (org-get-indirect-buffer cbuf)
5745 org-last-indirect-buffer ibuf)
5746 (cond
5747 ((or (eq org-indirect-buffer-display 'new-frame)
5748 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
5749 (select-frame (make-frame))
5750 (delete-other-windows)
5751 (switch-to-buffer ibuf)
5752 (org-set-frame-title heading))
5753 ((eq org-indirect-buffer-display 'dedicated-frame)
5754 (raise-frame
5755 (select-frame (or (and org-indirect-dedicated-frame
5756 (frame-live-p org-indirect-dedicated-frame)
5757 org-indirect-dedicated-frame)
5758 (setq org-indirect-dedicated-frame (make-frame)))))
5759 (delete-other-windows)
5760 (switch-to-buffer ibuf)
5761 (org-set-frame-title (concat "Indirect: " heading)))
5762 ((eq org-indirect-buffer-display 'current-window)
5763 (switch-to-buffer ibuf))
5764 ((eq org-indirect-buffer-display 'other-window)
5765 (pop-to-buffer ibuf))
5766 (t (error "Invalid value")))
5767 (if (featurep 'xemacs)
5768 (save-excursion (org-mode) (turn-on-font-lock)))
5769 (narrow-to-region beg end)
5770 (show-all)
5771 (goto-char pos)
5772 (and (window-live-p cwin) (select-window cwin))))
5774 (defun org-get-indirect-buffer (&optional buffer)
5775 (setq buffer (or buffer (current-buffer)))
5776 (let ((n 1) (base (buffer-name buffer)) bname)
5777 (while (buffer-live-p
5778 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
5779 (setq n (1+ n)))
5780 (condition-case nil
5781 (make-indirect-buffer buffer bname 'clone)
5782 (error (make-indirect-buffer buffer bname)))))
5784 (defun org-set-frame-title (title)
5785 "Set the title of the current frame to the string TITLE."
5786 ;; FIXME: how to name a single frame in XEmacs???
5787 (unless (featurep 'xemacs)
5788 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
5790 ;;;; Structure editing
5792 ;;; Inserting headlines
5794 (defun org-previous-line-empty-p ()
5795 (save-excursion
5796 (and (not (bobp))
5797 (or (beginning-of-line 0) t)
5798 (save-match-data
5799 (looking-at "[ \t]*$")))))
5801 (defun org-insert-heading (&optional force-heading)
5802 "Insert a new heading or item with same depth at point.
5803 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
5804 If point is at the beginning of a headline, insert a sibling before the
5805 current headline. If point is not at the beginning, do not split the line,
5806 but create the new headline after the current line."
5807 (interactive "P")
5808 (if (= (buffer-size) 0)
5809 (insert "\n* ")
5810 (when (or force-heading (not (org-insert-item)))
5811 (let* ((empty-line-p nil)
5812 (head (save-excursion
5813 (condition-case nil
5814 (progn
5815 (org-back-to-heading)
5816 (setq empty-line-p (org-previous-line-empty-p))
5817 (match-string 0))
5818 (error "*"))))
5819 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
5820 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
5821 pos hide-previous previous-pos)
5822 (cond
5823 ((and (org-on-heading-p) (bolp)
5824 (or (bobp)
5825 (save-excursion (backward-char 1) (not (org-invisible-p)))))
5826 ;; insert before the current line
5827 (open-line (if blank 2 1)))
5828 ((and (bolp)
5829 (not org-insert-heading-respect-content)
5830 (or (bobp)
5831 (save-excursion
5832 (backward-char 1) (not (org-invisible-p)))))
5833 ;; insert right here
5834 nil)
5836 ;; somewhere in the line
5837 (save-excursion
5838 (setq previous-pos (point-at-bol))
5839 (end-of-line)
5840 (setq hide-previous (org-invisible-p)))
5841 (and org-insert-heading-respect-content (org-show-subtree))
5842 (let ((split
5843 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
5844 (save-excursion
5845 (let ((p (point)))
5846 (goto-char (point-at-bol))
5847 (and (looking-at org-complex-heading-regexp)
5848 (> p (match-beginning 4)))))))
5849 tags pos)
5850 (cond
5851 (org-insert-heading-respect-content
5852 (org-end-of-subtree nil t)
5853 (or (bolp) (newline))
5854 (or (org-previous-line-empty-p)
5855 (and blank (newline)))
5856 (open-line 1))
5857 ((org-on-heading-p)
5858 (when hide-previous
5859 (show-children)
5860 (org-show-entry))
5861 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
5862 (setq tags (and (match-end 2) (match-string 2)))
5863 (and (match-end 1)
5864 (delete-region (match-beginning 1) (match-end 1)))
5865 (setq pos (point-at-bol))
5866 (or split (end-of-line 1))
5867 (delete-horizontal-space)
5868 (newline (if blank 2 1))
5869 (when tags
5870 (save-excursion
5871 (goto-char pos)
5872 (end-of-line 1)
5873 (insert " " tags)
5874 (org-set-tags nil 'align))))
5876 (or split (end-of-line 1))
5877 (newline (if blank 2 1)))))))
5878 (insert head) (just-one-space)
5879 (setq pos (point))
5880 (end-of-line 1)
5881 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
5882 (when (and org-insert-heading-respect-content hide-previous)
5883 (save-excursion
5884 (goto-char previous-pos)
5885 (hide-subtree)))
5886 (run-hooks 'org-insert-heading-hook)))))
5888 (defun org-get-heading (&optional no-tags)
5889 "Return the heading of the current entry, without the stars."
5890 (save-excursion
5891 (org-back-to-heading t)
5892 (if (looking-at
5893 (if no-tags
5894 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
5895 "\\*+[ \t]+\\([^\r\n]*\\)"))
5896 (match-string 1) "")))
5898 (defun org-heading-components ()
5899 "Return the components of the current heading.
5900 This is a list with the following elements:
5901 - the level as an integer
5902 - the reduced level, different if `org-odd-levels-only' is set.
5903 - the TODO keyword, or nil
5904 - the priority character, like ?A, or nil if no priority is given
5905 - the headline text itself, or the tags string if no headline text
5906 - the tags string, or nil."
5907 (save-excursion
5908 (org-back-to-heading t)
5909 (if (looking-at org-complex-heading-regexp)
5910 (list (length (match-string 1))
5911 (org-reduced-level (length (match-string 1)))
5912 (org-match-string-no-properties 2)
5913 (and (match-end 3) (aref (match-string 3) 2))
5914 (org-match-string-no-properties 4)
5915 (org-match-string-no-properties 5)))))
5917 (defun org-get-entry ()
5918 "Get the entry text, after heading, entire subtree."
5919 (save-excursion
5920 (org-back-to-heading t)
5921 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
5923 (defun org-insert-heading-after-current ()
5924 "Insert a new heading with same level as current, after current subtree."
5925 (interactive)
5926 (org-back-to-heading)
5927 (org-insert-heading)
5928 (org-move-subtree-down)
5929 (end-of-line 1))
5931 (defun org-insert-heading-respect-content ()
5932 (interactive)
5933 (let ((org-insert-heading-respect-content t))
5934 (org-insert-heading t)))
5936 (defun org-insert-todo-heading-respect-content (&optional force-state)
5937 (interactive "P")
5938 (let ((org-insert-heading-respect-content t))
5939 (org-insert-todo-heading force-state t)))
5941 (defun org-insert-todo-heading (arg &optional force-heading)
5942 "Insert a new heading with the same level and TODO state as current heading.
5943 If the heading has no TODO state, or if the state is DONE, use the first
5944 state (TODO by default). Also with prefix arg, force first state."
5945 (interactive "P")
5946 (when (or force-heading (not (org-insert-item 'checkbox)))
5947 (org-insert-heading force-heading)
5948 (save-excursion
5949 (org-back-to-heading)
5950 (outline-previous-heading)
5951 (looking-at org-todo-line-regexp))
5952 (let*
5953 ((new-mark-x
5954 (if (or arg
5955 (not (match-beginning 2))
5956 (member (match-string 2) org-done-keywords))
5957 (car org-todo-keywords-1)
5958 (match-string 2)))
5959 (new-mark
5961 (run-hook-with-args-until-success
5962 'org-todo-get-default-hook new-mark-x nil)
5963 new-mark-x)))
5964 (beginning-of-line 1)
5965 (and (looking-at "\\*+ ") (goto-char (match-end 0))
5966 (if org-treat-insert-todo-heading-as-state-change
5967 (org-todo new-mark)
5968 (insert new-mark " "))))
5969 (when org-provide-todo-statistics
5970 (org-update-parent-todo-statistics))))
5972 (defun org-insert-subheading (arg)
5973 "Insert a new subheading and demote it.
5974 Works for outline headings and for plain lists alike."
5975 (interactive "P")
5976 (org-insert-heading arg)
5977 (cond
5978 ((org-on-heading-p) (org-do-demote))
5979 ((org-at-item-p) (org-indent-item 1))))
5981 (defun org-insert-todo-subheading (arg)
5982 "Insert a new subheading with TODO keyword or checkbox and demote it.
5983 Works for outline headings and for plain lists alike."
5984 (interactive "P")
5985 (org-insert-todo-heading arg)
5986 (cond
5987 ((org-on-heading-p) (org-do-demote))
5988 ((org-at-item-p) (org-indent-item 1))))
5990 ;;; Promotion and Demotion
5992 (defvar org-after-demote-entry-hook nil
5993 "Hook run after an entry has been demoted.
5994 The cursor will be at the beginning of the entry.
5995 When a subtree is being demoted, the hook will be called for each node.")
5997 (defvar org-after-promote-entry-hook nil
5998 "Hook run after an entry has been promoted.
5999 The cursor will be at the beginning of the entry.
6000 When a subtree is being promoted, the hook will be called for each node.")
6002 (defun org-promote-subtree ()
6003 "Promote the entire subtree.
6004 See also `org-promote'."
6005 (interactive)
6006 (save-excursion
6007 (org-map-tree 'org-promote))
6008 (org-fix-position-after-promote))
6010 (defun org-demote-subtree ()
6011 "Demote the entire subtree. See `org-demote'.
6012 See also `org-promote'."
6013 (interactive)
6014 (save-excursion
6015 (org-map-tree 'org-demote))
6016 (org-fix-position-after-promote))
6019 (defun org-do-promote ()
6020 "Promote the current heading higher up the tree.
6021 If the region is active in `transient-mark-mode', promote all headings
6022 in the region."
6023 (interactive)
6024 (save-excursion
6025 (if (org-region-active-p)
6026 (org-map-region 'org-promote (region-beginning) (region-end))
6027 (org-promote)))
6028 (org-fix-position-after-promote))
6030 (defun org-do-demote ()
6031 "Demote the current heading lower down the tree.
6032 If the region is active in `transient-mark-mode', demote all headings
6033 in the region."
6034 (interactive)
6035 (save-excursion
6036 (if (org-region-active-p)
6037 (org-map-region 'org-demote (region-beginning) (region-end))
6038 (org-demote)))
6039 (org-fix-position-after-promote))
6041 (defun org-fix-position-after-promote ()
6042 "Make sure that after pro/demotion cursor position is right."
6043 (let ((pos (point)))
6044 (when (save-excursion
6045 (beginning-of-line 1)
6046 (looking-at org-todo-line-regexp)
6047 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
6048 (cond ((eobp) (insert " "))
6049 ((eolp) (insert " "))
6050 ((equal (char-after) ?\ ) (forward-char 1))))))
6052 (defun org-reduced-level (l)
6053 "Compute the effective level of a heading.
6054 This takes into account the setting of `org-odd-levels-only'."
6055 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
6057 (defun org-get-valid-level (level &optional change)
6058 "Rectify a level change under the influence of `org-odd-levels-only'
6059 LEVEL is a current level, CHANGE is by how much the level should be
6060 modified. Even if CHANGE is nil, LEVEL may be returned modified because
6061 even level numbers will become the next higher odd number."
6062 (if org-odd-levels-only
6063 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
6064 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
6065 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
6066 (max 1 (+ level (or change 0)))))
6068 (if (boundp 'define-obsolete-function-alias)
6069 (if (or (featurep 'xemacs) (< emacs-major-version 23))
6070 (define-obsolete-function-alias 'org-get-legal-level
6071 'org-get-valid-level)
6072 (define-obsolete-function-alias 'org-get-legal-level
6073 'org-get-valid-level "23.1")))
6075 (defun org-promote ()
6076 "Promote the current heading higher up the tree.
6077 If the region is active in `transient-mark-mode', promote all headings
6078 in the region."
6079 (org-back-to-heading t)
6080 (let* ((level (save-match-data (funcall outline-level)))
6081 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
6082 (diff (abs (- level (length up-head) -1))))
6083 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
6084 (replace-match up-head nil t)
6085 ;; Fixup tag positioning
6086 (and org-auto-align-tags (org-set-tags nil t))
6087 (if org-adapt-indentation (org-fixup-indentation (- diff)))
6088 (run-hooks 'org-after-promote-entry-hook)))
6090 (defun org-demote ()
6091 "Demote the current heading lower down the tree.
6092 If the region is active in `transient-mark-mode', demote all headings
6093 in the region."
6094 (org-back-to-heading t)
6095 (let* ((level (save-match-data (funcall outline-level)))
6096 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
6097 (diff (abs (- level (length down-head) -1))))
6098 (replace-match down-head nil t)
6099 ;; Fixup tag positioning
6100 (and org-auto-align-tags (org-set-tags nil t))
6101 (if org-adapt-indentation (org-fixup-indentation diff))
6102 (run-hooks 'org-after-demote-entry-hook)))
6104 (defun org-map-tree (fun)
6105 "Call FUN for every heading underneath the current one."
6106 (org-back-to-heading)
6107 (let ((level (funcall outline-level)))
6108 (save-excursion
6109 (funcall fun)
6110 (while (and (progn
6111 (outline-next-heading)
6112 (> (funcall outline-level) level))
6113 (not (eobp)))
6114 (funcall fun)))))
6116 (defun org-map-region (fun beg end)
6117 "Call FUN for every heading between BEG and END."
6118 (let ((org-ignore-region t))
6119 (save-excursion
6120 (setq end (copy-marker end))
6121 (goto-char beg)
6122 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
6123 (< (point) end))
6124 (funcall fun))
6125 (while (and (progn
6126 (outline-next-heading)
6127 (< (point) end))
6128 (not (eobp)))
6129 (funcall fun)))))
6131 (defun org-fixup-indentation (diff)
6132 "Change the indentation in the current entry by DIFF
6133 However, if any line in the current entry has no indentation, or if it
6134 would end up with no indentation after the change, nothing at all is done."
6135 (save-excursion
6136 (let ((end (save-excursion (outline-next-heading)
6137 (point-marker)))
6138 (prohibit (if (> diff 0)
6139 "^\\S-"
6140 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
6141 col)
6142 (unless (save-excursion (end-of-line 1)
6143 (re-search-forward prohibit end t))
6144 (while (and (< (point) end)
6145 (re-search-forward "^[ \t]+" end t))
6146 (goto-char (match-end 0))
6147 (setq col (current-column))
6148 (if (< diff 0) (replace-match ""))
6149 (org-indent-to-column (+ diff col))))
6150 (move-marker end nil))))
6152 (defun org-convert-to-odd-levels ()
6153 "Convert an org-mode file with all levels allowed to one with odd levels.
6154 This will leave level 1 alone, convert level 2 to level 3, level 3 to
6155 level 5 etc."
6156 (interactive)
6157 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
6158 (let ((outline-regexp org-outline-regexp)
6159 (outline-level 'org-outline-level)
6160 (org-odd-levels-only nil) n)
6161 (save-excursion
6162 (goto-char (point-min))
6163 (while (re-search-forward "^\\*\\*+ " nil t)
6164 (setq n (- (length (match-string 0)) 2))
6165 (while (>= (setq n (1- n)) 0)
6166 (org-demote))
6167 (end-of-line 1))))))
6169 (defun org-convert-to-oddeven-levels ()
6170 "Convert an org-mode file with only odd levels to one with odd and even levels.
6171 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
6172 section with an even level, conversion would destroy the structure of the file. An error
6173 is signaled in this case."
6174 (interactive)
6175 (goto-char (point-min))
6176 ;; First check if there are no even levels
6177 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
6178 (org-show-context t)
6179 (error "Not all levels are odd in this file. Conversion not possible"))
6180 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
6181 (let ((outline-regexp org-outline-regexp)
6182 (outline-level 'org-outline-level)
6183 (org-odd-levels-only nil) n)
6184 (save-excursion
6185 (goto-char (point-min))
6186 (while (re-search-forward "^\\*\\*+ " nil t)
6187 (setq n (/ (1- (length (match-string 0))) 2))
6188 (while (>= (setq n (1- n)) 0)
6189 (org-promote))
6190 (end-of-line 1))))))
6192 (defun org-tr-level (n)
6193 "Make N odd if required."
6194 (if org-odd-levels-only (1+ (/ n 2)) n))
6196 ;;; Vertical tree motion, cutting and pasting of subtrees
6198 (defun org-move-subtree-up (&optional arg)
6199 "Move the current subtree up past ARG headlines of the same level."
6200 (interactive "p")
6201 (org-move-subtree-down (- (prefix-numeric-value arg))))
6203 (defun org-move-subtree-down (&optional arg)
6204 "Move the current subtree down past ARG headlines of the same level."
6205 (interactive "p")
6206 (setq arg (prefix-numeric-value arg))
6207 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
6208 'org-get-last-sibling))
6209 (ins-point (make-marker))
6210 (cnt (abs arg))
6211 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
6212 ;; Select the tree
6213 (org-back-to-heading)
6214 (setq beg0 (point))
6215 (save-excursion
6216 (setq ne-beg (org-back-over-empty-lines))
6217 (setq beg (point)))
6218 (save-match-data
6219 (save-excursion (outline-end-of-heading)
6220 (setq folded (org-invisible-p)))
6221 (outline-end-of-subtree))
6222 (outline-next-heading)
6223 (setq ne-end (org-back-over-empty-lines))
6224 (setq end (point))
6225 (goto-char beg0)
6226 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
6227 ;; include less whitespace
6228 (save-excursion
6229 (goto-char beg)
6230 (forward-line (- ne-beg ne-end))
6231 (setq beg (point))))
6232 ;; Find insertion point, with error handling
6233 (while (> cnt 0)
6234 (or (and (funcall movfunc) (looking-at outline-regexp))
6235 (progn (goto-char beg0)
6236 (error "Cannot move past superior level or buffer limit")))
6237 (setq cnt (1- cnt)))
6238 (if (> arg 0)
6239 ;; Moving forward - still need to move over subtree
6240 (progn (org-end-of-subtree t t)
6241 (save-excursion
6242 (org-back-over-empty-lines)
6243 (or (bolp) (newline)))))
6244 (setq ne-ins (org-back-over-empty-lines))
6245 (move-marker ins-point (point))
6246 (setq txt (buffer-substring beg end))
6247 (org-save-markers-in-region beg end)
6248 (delete-region beg end)
6249 (org-remove-empty-overlays-at beg)
6250 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
6251 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
6252 (and (not (bolp)) (looking-at "\n") (forward-char 1))
6253 (let ((bbb (point)))
6254 (insert-before-markers txt)
6255 (org-reinstall-markers-in-region bbb)
6256 (move-marker ins-point bbb))
6257 (or (bolp) (insert "\n"))
6258 (setq ins-end (point))
6259 (goto-char ins-point)
6260 (org-skip-whitespace)
6261 (when (and (< arg 0)
6262 (org-first-sibling-p)
6263 (> ne-ins ne-beg))
6264 ;; Move whitespace back to beginning
6265 (save-excursion
6266 (goto-char ins-end)
6267 (let ((kill-whole-line t))
6268 (kill-line (- ne-ins ne-beg)) (point)))
6269 (insert (make-string (- ne-ins ne-beg) ?\n)))
6270 (move-marker ins-point nil)
6271 (if folded
6272 (hide-subtree)
6273 (org-show-entry)
6274 (show-children)
6275 (org-cycle-hide-drawers 'children))
6276 (org-clean-visibility-after-subtree-move)))
6278 (defvar org-subtree-clip ""
6279 "Clipboard for cut and paste of subtrees.
6280 This is actually only a copy of the kill, because we use the normal kill
6281 ring. We need it to check if the kill was created by `org-copy-subtree'.")
6283 (defvar org-subtree-clip-folded nil
6284 "Was the last copied subtree folded?
6285 This is used to fold the tree back after pasting.")
6287 (defun org-cut-subtree (&optional n)
6288 "Cut the current subtree into the clipboard.
6289 With prefix arg N, cut this many sequential subtrees.
6290 This is a short-hand for marking the subtree and then cutting it."
6291 (interactive "p")
6292 (org-copy-subtree n 'cut))
6294 (defun org-copy-subtree (&optional n cut force-store-markers)
6295 "Cut the current subtree into the clipboard.
6296 With prefix arg N, cut this many sequential subtrees.
6297 This is a short-hand for marking the subtree and then copying it.
6298 If CUT is non-nil, actually cut the subtree.
6299 If FORCE-STORE-MARKERS is non-nil, store the relative locations
6300 of some markers in the region, even if CUT is non-nil. This is
6301 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
6302 (interactive "p")
6303 (let (beg end folded (beg0 (point)))
6304 (if (interactive-p)
6305 (org-back-to-heading nil) ; take what looks like a subtree
6306 (org-back-to-heading t)) ; take what is really there
6307 (org-back-over-empty-lines)
6308 (setq beg (point))
6309 (skip-chars-forward " \t\r\n")
6310 (save-match-data
6311 (save-excursion (outline-end-of-heading)
6312 (setq folded (org-invisible-p)))
6313 (condition-case nil
6314 (org-forward-same-level (1- n) t)
6315 (error nil))
6316 (org-end-of-subtree t t))
6317 (org-back-over-empty-lines)
6318 (setq end (point))
6319 (goto-char beg0)
6320 (when (> end beg)
6321 (setq org-subtree-clip-folded folded)
6322 (when (or cut force-store-markers)
6323 (org-save-markers-in-region beg end))
6324 (if cut (kill-region beg end) (copy-region-as-kill beg end))
6325 (setq org-subtree-clip (current-kill 0))
6326 (message "%s: Subtree(s) with %d characters"
6327 (if cut "Cut" "Copied")
6328 (length org-subtree-clip)))))
6330 (defun org-paste-subtree (&optional level tree for-yank)
6331 "Paste the clipboard as a subtree, with modification of headline level.
6332 The entire subtree is promoted or demoted in order to match a new headline
6333 level.
6335 If the cursor is at the beginning of a headline, the same level as
6336 that headline is used to paste the tree
6338 If not, the new level is derived from the *visible* headings
6339 before and after the insertion point, and taken to be the inferior headline
6340 level of the two. So if the previous visible heading is level 3 and the
6341 next is level 4 (or vice versa), level 4 will be used for insertion.
6342 This makes sure that the subtree remains an independent subtree and does
6343 not swallow low level entries.
6345 You can also force a different level, either by using a numeric prefix
6346 argument, or by inserting the heading marker by hand. For example, if the
6347 cursor is after \"*****\", then the tree will be shifted to level 5.
6349 If optional TREE is given, use this text instead of the kill ring.
6351 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
6352 move back over whitespace before inserting, and move point to the end of
6353 the inserted text when done."
6354 (interactive "P")
6355 (setq tree (or tree (and kill-ring (current-kill 0))))
6356 (unless (org-kill-is-subtree-p tree)
6357 (error "%s"
6358 (substitute-command-keys
6359 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
6360 (let* ((visp (not (org-invisible-p)))
6361 (txt tree)
6362 (^re (concat "^\\(" outline-regexp "\\)"))
6363 (re (concat "\\(" outline-regexp "\\)"))
6364 (^re_ (concat "\\(\\*+\\)[ \t]*"))
6366 (old-level (if (string-match ^re txt)
6367 (- (match-end 0) (match-beginning 0) 1)
6368 -1))
6369 (force-level (cond (level (prefix-numeric-value level))
6370 ((and (looking-at "[ \t]*$")
6371 (string-match
6372 ^re_ (buffer-substring
6373 (point-at-bol) (point))))
6374 (- (match-end 1) (match-beginning 1)))
6375 ((and (bolp)
6376 (looking-at org-outline-regexp))
6377 (- (match-end 0) (point) 1))
6378 (t nil)))
6379 (previous-level (save-excursion
6380 (condition-case nil
6381 (progn
6382 (outline-previous-visible-heading 1)
6383 (if (looking-at re)
6384 (- (match-end 0) (match-beginning 0) 1)
6386 (error 1))))
6387 (next-level (save-excursion
6388 (condition-case nil
6389 (progn
6390 (or (looking-at outline-regexp)
6391 (outline-next-visible-heading 1))
6392 (if (looking-at re)
6393 (- (match-end 0) (match-beginning 0) 1)
6395 (error 1))))
6396 (new-level (or force-level (max previous-level next-level)))
6397 (shift (if (or (= old-level -1)
6398 (= new-level -1)
6399 (= old-level new-level))
6401 (- new-level old-level)))
6402 (delta (if (> shift 0) -1 1))
6403 (func (if (> shift 0) 'org-demote 'org-promote))
6404 (org-odd-levels-only nil)
6405 beg end newend)
6406 ;; Remove the forced level indicator
6407 (if force-level
6408 (delete-region (point-at-bol) (point)))
6409 ;; Paste
6410 (beginning-of-line 1)
6411 (unless for-yank (org-back-over-empty-lines))
6412 (setq beg (point))
6413 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
6414 (insert-before-markers txt)
6415 (unless (string-match "\n\\'" txt) (insert "\n"))
6416 (setq newend (point))
6417 (org-reinstall-markers-in-region beg)
6418 (setq end (point))
6419 (goto-char beg)
6420 (skip-chars-forward " \t\n\r")
6421 (setq beg (point))
6422 (if (and (org-invisible-p) visp)
6423 (save-excursion (outline-show-heading)))
6424 ;; Shift if necessary
6425 (unless (= shift 0)
6426 (save-restriction
6427 (narrow-to-region beg end)
6428 (while (not (= shift 0))
6429 (org-map-region func (point-min) (point-max))
6430 (setq shift (+ delta shift)))
6431 (goto-char (point-min))
6432 (setq newend (point-max))))
6433 (when (or (interactive-p) for-yank)
6434 (message "Clipboard pasted as level %d subtree" new-level))
6435 (if (and (not for-yank) ; in this case, org-yank will decide about folding
6436 kill-ring
6437 (eq org-subtree-clip (current-kill 0))
6438 org-subtree-clip-folded)
6439 ;; The tree was folded before it was killed/copied
6440 (hide-subtree))
6441 (and for-yank (goto-char newend))))
6443 (defun org-kill-is-subtree-p (&optional txt)
6444 "Check if the current kill is an outline subtree, or a set of trees.
6445 Returns nil if kill does not start with a headline, or if the first
6446 headline level is not the largest headline level in the tree.
6447 So this will actually accept several entries of equal levels as well,
6448 which is OK for `org-paste-subtree'.
6449 If optional TXT is given, check this string instead of the current kill."
6450 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
6451 (start-level (and kill
6452 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
6453 org-outline-regexp "\\)")
6454 kill)
6455 (- (match-end 2) (match-beginning 2) 1)))
6456 (re (concat "^" org-outline-regexp))
6457 (start (1+ (or (match-beginning 2) -1))))
6458 (if (not start-level)
6459 (progn
6460 nil) ;; does not even start with a heading
6461 (catch 'exit
6462 (while (setq start (string-match re kill (1+ start)))
6463 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
6464 (throw 'exit nil)))
6465 t))))
6467 (defvar org-markers-to-move nil
6468 "Markers that should be moved with a cut-and-paste operation.
6469 Those markers are stored together with their positions relative to
6470 the start of the region.")
6472 (defun org-save-markers-in-region (beg end)
6473 "Check markers in region.
6474 If these markers are between BEG and END, record their position relative
6475 to BEG, so that after moving the block of text, we can put the markers back
6476 into place.
6477 This function gets called just before an entry or tree gets cut from the
6478 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
6479 called immediately, to move the markers with the entries."
6480 (setq org-markers-to-move nil)
6481 (when (featurep 'org-clock)
6482 (org-clock-save-markers-for-cut-and-paste beg end))
6483 (when (featurep 'org-agenda)
6484 (org-agenda-save-markers-for-cut-and-paste beg end)))
6486 (defun org-check-and-save-marker (marker beg end)
6487 "Check if MARKER is between BEG and END.
6488 If yes, remember the marker and the distance to BEG."
6489 (when (and (marker-buffer marker)
6490 (equal (marker-buffer marker) (current-buffer)))
6491 (if (and (>= marker beg) (< marker end))
6492 (push (cons marker (- marker beg)) org-markers-to-move))))
6494 (defun org-reinstall-markers-in-region (beg)
6495 "Move all remembered markers to their position relative to BEG."
6496 (mapc (lambda (x)
6497 (move-marker (car x) (+ beg (cdr x))))
6498 org-markers-to-move)
6499 (setq org-markers-to-move nil))
6501 (defun org-narrow-to-subtree ()
6502 "Narrow buffer to the current subtree."
6503 (interactive)
6504 (save-excursion
6505 (save-match-data
6506 (narrow-to-region
6507 (progn (org-back-to-heading t) (point))
6508 (progn (org-end-of-subtree t t) (point))))))
6510 (defun org-clone-subtree-with-time-shift (n &optional shift)
6511 "Clone the task (subtree) at point N times.
6512 The clones will be inserted as siblings.
6514 In interactive use, the user will be prompted for the number of clones
6515 to be produced, and for a time SHIFT, which may be a repeater as used
6516 in time stamps, for example `+3d'.
6518 When a valid repeater is given and the entry contains any time stamps,
6519 the clones will become a sequence in time, with time stamps in the
6520 subtree shifted for each clone produced. If SHIFT is nil or the
6521 empty string, time stamps will be left alone.
6523 If the original subtree did contain time stamps with a repeater,
6524 the following will happen:
6525 - the repeater will be removed in each clone
6526 - an additional clone will be produced, with the current, unshifted
6527 date(s) in the entry.
6528 - the original entry will be placed *after* all the clones, with
6529 repeater intact.
6530 - the start days in the repeater in the original entry will be shifted
6531 to past the last clone.
6532 I this way you can spell out a number of instances of a repeating task,
6533 and still retain the repeater to cover future instances of the task."
6534 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
6535 (let (beg end template task
6536 shift-n shift-what doshift nmin nmax (n-no-remove -1))
6537 (if (not (and (integerp n) (> n 0)))
6538 (error "Invalid number of replications %s" n))
6539 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
6540 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
6541 shift)))
6542 (error "Invalid shift specification %s" shift))
6543 (when doshift
6544 (setq shift-n (string-to-number (match-string 1 shift))
6545 shift-what (cdr (assoc (match-string 2 shift)
6546 '(("d" . day) ("w" . week)
6547 ("m" . month) ("y" . year))))))
6548 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
6549 (setq nmin 1 nmax n)
6550 (org-back-to-heading t)
6551 (setq beg (point))
6552 (org-end-of-subtree t t)
6553 (setq end (point))
6554 (setq template (buffer-substring beg end))
6555 (when (and doshift
6556 (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
6557 (delete-region beg end)
6558 (setq end beg)
6559 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
6560 (goto-char end)
6561 (loop for n from nmin to nmax do
6562 (if (not doshift)
6563 (setq task template)
6564 (with-temp-buffer
6565 (insert template)
6566 (org-mode)
6567 (goto-char (point-min))
6568 (while (re-search-forward org-ts-regexp-both nil t)
6569 (org-timestamp-change (* n shift-n) shift-what))
6570 (unless (= n n-no-remove)
6571 (goto-char (point-min))
6572 (while (re-search-forward org-ts-regexp nil t)
6573 (save-excursion
6574 (goto-char (match-beginning 0))
6575 (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
6576 (delete-region (match-beginning 1) (match-end 1))))))
6577 (setq task (buffer-string))))
6578 (insert task))
6579 (goto-char beg)))
6581 ;;; Outline Sorting
6583 (defun org-sort (with-case)
6584 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
6585 Optional argument WITH-CASE means sort case-sensitively.
6586 With a double prefix argument, also remove duplicate entries."
6587 (interactive "P")
6588 (if (org-at-table-p)
6589 (org-call-with-arg 'org-table-sort-lines with-case)
6590 (org-call-with-arg 'org-sort-entries-or-items with-case)))
6592 (defun org-sort-remove-invisible (s)
6593 (remove-text-properties 0 (length s) org-rm-props s)
6594 (while (string-match org-bracket-link-regexp s)
6595 (setq s (replace-match (if (match-end 2)
6596 (match-string 3 s)
6597 (match-string 1 s)) t t s)))
6600 (defvar org-priority-regexp) ; defined later in the file
6602 (defvar org-after-sorting-entries-or-items-hook nil
6603 "Hook that is run after a bunch of entries or items have been sorted.
6604 When children are sorted, the cursor is in the parent line when this
6605 hook gets called. When a region or a plain list is sorted, the cursor
6606 will be in the first entry of the sorted region/list.")
6608 (defun org-sort-entries-or-items
6609 (&optional with-case sorting-type getkey-func compare-func property)
6610 "Sort entries on a certain level of an outline tree, or plain list items.
6611 If there is an active region, the entries in the region are sorted.
6612 Else, if the cursor is before the first entry, sort the top-level items.
6613 Else, the children of the entry at point are sorted.
6614 If the cursor is at the first item in a plain list, the list items will be
6615 sorted.
6617 Sorting can be alphabetically, numerically, by date/time as given by
6618 a time stamp, by a property or by priority.
6620 The command prompts for the sorting type unless it has been given to the
6621 function through the SORTING-TYPE argument, which needs to a character,
6622 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
6623 precise meaning of each character:
6625 n Numerically, by converting the beginning of the entry/item to a number.
6626 a Alphabetically, ignoring the TODO keyword and the priority, if any.
6627 t By date/time, either the first active time stamp in the entry, or, if
6628 none exist, by the first inactive one.
6629 In items, only the first line will be chekced.
6630 s By the scheduled date/time.
6631 d By deadline date/time.
6632 c By creation time, which is assumed to be the first inactive time stamp
6633 at the beginning of a line.
6634 p By priority according to the cookie.
6635 r By the value of a property.
6637 Capital letters will reverse the sort order.
6639 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
6640 called with point at the beginning of the record. It must return either
6641 a string or a number that should serve as the sorting key for that record.
6643 Comparing entries ignores case by default. However, with an optional argument
6644 WITH-CASE, the sorting considers case as well."
6645 (interactive "P")
6646 (let ((case-func (if with-case 'identity 'downcase))
6647 start beg end stars re re2
6648 txt what tmp plain-list-p)
6649 ;; Find beginning and end of region to sort
6650 (cond
6651 ((org-region-active-p)
6652 ;; we will sort the region
6653 (setq end (region-end)
6654 what "region")
6655 (goto-char (region-beginning))
6656 (if (not (org-on-heading-p)) (outline-next-heading))
6657 (setq start (point)))
6658 ((org-at-item-p)
6659 ;; we will sort this plain list
6660 (org-beginning-of-item-list) (setq start (point))
6661 (org-end-of-item-list) (setq end (point))
6662 (goto-char start)
6663 (setq plain-list-p t
6664 what "plain list"))
6665 ((or (org-on-heading-p)
6666 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
6667 ;; we will sort the children of the current headline
6668 (org-back-to-heading)
6669 (setq start (point)
6670 end (progn (org-end-of-subtree t t)
6671 (org-back-over-empty-lines)
6672 (point))
6673 what "children")
6674 (goto-char start)
6675 (show-subtree)
6676 (outline-next-heading))
6678 ;; we will sort the top-level entries in this file
6679 (goto-char (point-min))
6680 (or (org-on-heading-p) (outline-next-heading))
6681 (setq start (point) end (point-max) what "top-level")
6682 (goto-char start)
6683 (show-all)))
6685 (setq beg (point))
6686 (if (>= beg end) (error "Nothing to sort"))
6688 (unless plain-list-p
6689 (looking-at "\\(\\*+\\)")
6690 (setq stars (match-string 1)
6691 re (concat "^" (regexp-quote stars) " +")
6692 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
6693 txt (buffer-substring beg end))
6694 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
6695 (if (and (not (equal stars "*")) (string-match re2 txt))
6696 (error "Region to sort contains a level above the first entry")))
6698 (unless sorting-type
6699 (message
6700 (if plain-list-p
6701 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
6702 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
6703 [t]ime [s]cheduled [d]eadline [c]reated
6704 A/N/T/S/D/C/P/O/F means reversed:")
6705 what)
6706 (setq sorting-type (read-char-exclusive))
6708 (and (= (downcase sorting-type) ?f)
6709 (setq getkey-func
6710 (org-icompleting-read "Sort using function: "
6711 obarray 'fboundp t nil nil))
6712 (setq getkey-func (intern getkey-func)))
6714 (and (= (downcase sorting-type) ?r)
6715 (setq property
6716 (org-icompleting-read "Property: "
6717 (mapcar 'list (org-buffer-property-keys t))
6718 nil t))))
6720 (message "Sorting entries...")
6722 (save-restriction
6723 (narrow-to-region start end)
6725 (let ((dcst (downcase sorting-type))
6726 (case-fold-search nil)
6727 (now (current-time)))
6728 (sort-subr
6729 (/= dcst sorting-type)
6730 ;; This function moves to the beginning character of the "record" to
6731 ;; be sorted.
6732 (if plain-list-p
6733 (lambda nil
6734 (if (org-at-item-p) t (goto-char (point-max))))
6735 (lambda nil
6736 (if (re-search-forward re nil t)
6737 (goto-char (match-beginning 0))
6738 (goto-char (point-max)))))
6739 ;; This function moves to the last character of the "record" being
6740 ;; sorted.
6741 (if plain-list-p
6742 'org-end-of-item
6743 (lambda nil
6744 (save-match-data
6745 (condition-case nil
6746 (outline-forward-same-level 1)
6747 (error
6748 (goto-char (point-max)))))))
6750 ;; This function returns the value that gets sorted against.
6751 (if plain-list-p
6752 (lambda nil
6753 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
6754 (cond
6755 ((= dcst ?n)
6756 (string-to-number (buffer-substring (match-end 0)
6757 (point-at-eol))))
6758 ((= dcst ?a)
6759 (buffer-substring (match-end 0) (point-at-eol)))
6760 ((= dcst ?t)
6761 (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
6762 (re-search-forward org-ts-regexp-both
6763 (point-at-eol) t))
6764 (org-time-string-to-seconds (match-string 0))
6765 (org-float-time now)))
6766 ((= dcst ?f)
6767 (if getkey-func
6768 (progn
6769 (setq tmp (funcall getkey-func))
6770 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
6771 tmp)
6772 (error "Invalid key function `%s'" getkey-func)))
6773 (t (error "Invalid sorting type `%c'" sorting-type)))))
6774 (lambda nil
6775 (cond
6776 ((= dcst ?n)
6777 (if (looking-at org-complex-heading-regexp)
6778 (string-to-number (match-string 4))
6779 nil))
6780 ((= dcst ?a)
6781 (if (looking-at org-complex-heading-regexp)
6782 (funcall case-func (match-string 4))
6783 nil))
6784 ((= dcst ?t)
6785 (let ((end (save-excursion (outline-next-heading) (point))))
6786 (if (or (re-search-forward org-ts-regexp end t)
6787 (re-search-forward org-ts-regexp-both end t))
6788 (org-time-string-to-seconds (match-string 0))
6789 (org-float-time now))))
6790 ((= dcst ?c)
6791 (let ((end (save-excursion (outline-next-heading) (point))))
6792 (if (re-search-forward
6793 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
6794 end t)
6795 (org-time-string-to-seconds (match-string 0))
6796 (org-float-time now))))
6797 ((= dcst ?s)
6798 (let ((end (save-excursion (outline-next-heading) (point))))
6799 (if (re-search-forward org-scheduled-time-regexp end t)
6800 (org-time-string-to-seconds (match-string 1))
6801 (org-float-time now))))
6802 ((= dcst ?d)
6803 (let ((end (save-excursion (outline-next-heading) (point))))
6804 (if (re-search-forward org-deadline-time-regexp end t)
6805 (org-time-string-to-seconds (match-string 1))
6806 (org-float-time now))))
6807 ((= dcst ?p)
6808 (if (re-search-forward org-priority-regexp (point-at-eol) t)
6809 (string-to-char (match-string 2))
6810 org-default-priority))
6811 ((= dcst ?r)
6812 (or (org-entry-get nil property) ""))
6813 ((= dcst ?o)
6814 (if (looking-at org-complex-heading-regexp)
6815 (- 9999 (length (member (match-string 2)
6816 org-todo-keywords-1)))))
6817 ((= dcst ?f)
6818 (if getkey-func
6819 (progn
6820 (setq tmp (funcall getkey-func))
6821 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
6822 tmp)
6823 (error "Invalid key function `%s'" getkey-func)))
6824 (t (error "Invalid sorting type `%c'" sorting-type)))))
6826 (cond
6827 ((= dcst ?a) 'string<)
6828 ((= dcst ?f) compare-func)
6829 ((member dcst '(?p ?t ?s ?d ?c)) '<)
6830 (t nil)))))
6831 (run-hooks 'org-after-sorting-entries-or-items-hook)
6832 (message "Sorting entries...done")))
6834 (defun org-do-sort (table what &optional with-case sorting-type)
6835 "Sort TABLE of WHAT according to SORTING-TYPE.
6836 The user will be prompted for the SORTING-TYPE if the call to this
6837 function does not specify it. WHAT is only for the prompt, to indicate
6838 what is being sorted. The sorting key will be extracted from
6839 the car of the elements of the table.
6840 If WITH-CASE is non-nil, the sorting will be case-sensitive."
6841 (unless sorting-type
6842 (message
6843 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
6844 what)
6845 (setq sorting-type (read-char-exclusive)))
6846 (let ((dcst (downcase sorting-type))
6847 extractfun comparefun)
6848 ;; Define the appropriate functions
6849 (cond
6850 ((= dcst ?n)
6851 (setq extractfun 'string-to-number
6852 comparefun (if (= dcst sorting-type) '< '>)))
6853 ((= dcst ?a)
6854 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
6855 (lambda(x) (downcase (org-sort-remove-invisible x))))
6856 comparefun (if (= dcst sorting-type)
6857 'string<
6858 (lambda (a b) (and (not (string< a b))
6859 (not (string= a b)))))))
6860 ((= dcst ?t)
6861 (setq extractfun
6862 (lambda (x)
6863 (if (or (string-match org-ts-regexp x)
6864 (string-match org-ts-regexp-both x))
6865 (org-float-time
6866 (org-time-string-to-time (match-string 0 x)))
6868 comparefun (if (= dcst sorting-type) '< '>)))
6869 (t (error "Invalid sorting type `%c'" sorting-type)))
6871 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
6872 table)
6873 (lambda (a b) (funcall comparefun (car a) (car b))))))
6876 ;;; The orgstruct minor mode
6878 ;; Define a minor mode which can be used in other modes in order to
6879 ;; integrate the org-mode structure editing commands.
6881 ;; This is really a hack, because the org-mode structure commands use
6882 ;; keys which normally belong to the major mode. Here is how it
6883 ;; works: The minor mode defines all the keys necessary to operate the
6884 ;; structure commands, but wraps the commands into a function which
6885 ;; tests if the cursor is currently at a headline or a plain list
6886 ;; item. If that is the case, the structure command is used,
6887 ;; temporarily setting many Org-mode variables like regular
6888 ;; expressions for filling etc. However, when any of those keys is
6889 ;; used at a different location, function uses `key-binding' to look
6890 ;; up if the key has an associated command in another currently active
6891 ;; keymap (minor modes, major mode, global), and executes that
6892 ;; command. There might be problems if any of the keys is otherwise
6893 ;; used as a prefix key.
6895 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
6896 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
6897 ;; addresses this by checking explicitly for both bindings.
6899 (defvar orgstruct-mode-map (make-sparse-keymap)
6900 "Keymap for the minor `orgstruct-mode'.")
6902 (defvar org-local-vars nil
6903 "List of local variables, for use by `orgstruct-mode'")
6905 ;;;###autoload
6906 (define-minor-mode orgstruct-mode
6907 "Toggle the minor more `orgstruct-mode'.
6908 This mode is for using Org-mode structure commands in other modes.
6909 The following key behave as if Org-mode was active, if the cursor
6910 is on a headline, or on a plain list item (both in the definition
6911 of Org-mode).
6913 M-up Move entry/item up
6914 M-down Move entry/item down
6915 M-left Promote
6916 M-right Demote
6917 M-S-up Move entry/item up
6918 M-S-down Move entry/item down
6919 M-S-left Promote subtree
6920 M-S-right Demote subtree
6921 M-q Fill paragraph and items like in Org-mode
6922 C-c ^ Sort entries
6923 C-c - Cycle list bullet
6924 TAB Cycle item visibility
6925 M-RET Insert new heading/item
6926 S-M-RET Insert new TODO heading / Checkbox item
6927 C-c C-c Set tags / toggle checkbox"
6928 nil " OrgStruct" nil
6929 (org-load-modules-maybe)
6930 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
6932 ;;;###autoload
6933 (defun turn-on-orgstruct ()
6934 "Unconditionally turn on `orgstruct-mode'."
6935 (orgstruct-mode 1))
6937 (defun orgstruct++-mode (&optional arg)
6938 "Toggle `orgstruct-mode', the enhanced version of it.
6939 In addition to setting orgstruct-mode, this also exports all indentation
6940 and autofilling variables from org-mode into the buffer. It will also
6941 recognize item context in multiline items.
6942 Note that turning off orgstruct-mode will *not* remove the
6943 indentation/paragraph settings. This can only be done by refreshing the
6944 major mode, for example with \\[normal-mode]."
6945 (interactive "P")
6946 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
6947 (if (< arg 1)
6948 (orgstruct-mode -1)
6949 (orgstruct-mode 1)
6950 (let (var val)
6951 (mapc
6952 (lambda (x)
6953 (when (string-match
6954 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6955 (symbol-name (car x)))
6956 (setq var (car x) val (nth 1 x))
6957 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
6958 org-local-vars)
6959 (org-set-local 'orgstruct-is-++ t))))
6961 (defvar orgstruct-is-++ nil
6962 "Is orgstruct-mode in ++ version in the current-buffer?")
6963 (make-variable-buffer-local 'orgstruct-is-++)
6965 ;;;###autoload
6966 (defun turn-on-orgstruct++ ()
6967 "Unconditionally turn on `orgstruct++-mode'."
6968 (orgstruct++-mode 1))
6970 (defun orgstruct-error ()
6971 "Error when there is no default binding for a structure key."
6972 (interactive)
6973 (error "This key has no function outside structure elements"))
6975 (defun orgstruct-setup ()
6976 "Setup orgstruct keymaps."
6977 (let ((nfunc 0)
6978 (bindings
6979 (list
6980 '([(meta up)] org-metaup)
6981 '([(meta down)] org-metadown)
6982 '([(meta left)] org-metaleft)
6983 '([(meta right)] org-metaright)
6984 '([(meta shift up)] org-shiftmetaup)
6985 '([(meta shift down)] org-shiftmetadown)
6986 '([(meta shift left)] org-shiftmetaleft)
6987 '([(meta shift right)] org-shiftmetaright)
6988 '([?\e (up)] org-metaup)
6989 '([?\e (down)] org-metadown)
6990 '([?\e (left)] org-metaleft)
6991 '([?\e (right)] org-metaright)
6992 '([?\e (shift up)] org-shiftmetaup)
6993 '([?\e (shift down)] org-shiftmetadown)
6994 '([?\e (shift left)] org-shiftmetaleft)
6995 '([?\e (shift right)] org-shiftmetaright)
6996 '([(shift up)] org-shiftup)
6997 '([(shift down)] org-shiftdown)
6998 '([(shift left)] org-shiftleft)
6999 '([(shift right)] org-shiftright)
7000 '("\C-c\C-c" org-ctrl-c-ctrl-c)
7001 '("\M-q" fill-paragraph)
7002 '("\C-c^" org-sort)
7003 '("\C-c-" org-cycle-list-bullet)))
7004 elt key fun cmd)
7005 (while (setq elt (pop bindings))
7006 (setq nfunc (1+ nfunc))
7007 (setq key (org-key (car elt))
7008 fun (nth 1 elt)
7009 cmd (orgstruct-make-binding fun nfunc key))
7010 (org-defkey orgstruct-mode-map key cmd))
7012 ;; Special treatment needed for TAB and RET
7013 (org-defkey orgstruct-mode-map [(tab)]
7014 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
7015 (org-defkey orgstruct-mode-map "\C-i"
7016 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
7018 (org-defkey orgstruct-mode-map "\M-\C-m"
7019 (orgstruct-make-binding 'org-insert-heading 105
7020 "\M-\C-m" [(meta return)]))
7021 (org-defkey orgstruct-mode-map [(meta return)]
7022 (orgstruct-make-binding 'org-insert-heading 106
7023 [(meta return)] "\M-\C-m"))
7025 (org-defkey orgstruct-mode-map [(shift meta return)]
7026 (orgstruct-make-binding 'org-insert-todo-heading 107
7027 [(meta return)] "\M-\C-m"))
7029 (org-defkey orgstruct-mode-map "\e\C-m"
7030 (orgstruct-make-binding 'org-insert-heading 108
7031 "\e\C-m" [?\e (return)]))
7032 (org-defkey orgstruct-mode-map [?\e (return)]
7033 (orgstruct-make-binding 'org-insert-heading 109
7034 [?\e (return)] "\e\C-m"))
7035 (org-defkey orgstruct-mode-map [?\e (shift return)]
7036 (orgstruct-make-binding 'org-insert-todo-heading 110
7037 [?\e (return)] "\e\C-m"))
7039 (unless org-local-vars
7040 (setq org-local-vars (org-get-local-variables)))
7044 (defun orgstruct-make-binding (fun n &rest keys)
7045 "Create a function for binding in the structure minor mode.
7046 FUN is the command to call inside a table. N is used to create a unique
7047 command name. KEYS are keys that should be checked in for a command
7048 to execute outside of tables."
7049 (eval
7050 (list 'defun
7051 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
7052 '(arg)
7053 (concat "In Structure, run `" (symbol-name fun) "'.\n"
7054 "Outside of structure, run the binding of `"
7055 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
7056 "'.")
7057 '(interactive "p")
7058 (list 'if
7059 `(org-context-p 'headline 'item
7060 (and orgstruct-is-++
7061 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
7062 'item-body))
7063 (list 'org-run-like-in-org-mode (list 'quote fun))
7064 (list 'let '(orgstruct-mode)
7065 (list 'call-interactively
7066 (append '(or)
7067 (mapcar (lambda (k)
7068 (list 'key-binding k))
7069 keys)
7070 '('orgstruct-error))))))))
7072 (defun org-context-p (&rest contexts)
7073 "Check if local context is any of CONTEXTS.
7074 Possible values in the list of contexts are `table', `headline', and `item'."
7075 (let ((pos (point)))
7076 (goto-char (point-at-bol))
7077 (prog1 (or (and (memq 'table contexts)
7078 (looking-at "[ \t]*|"))
7079 (and (memq 'headline contexts)
7080 ;;????????? (looking-at "\\*+"))
7081 (looking-at outline-regexp))
7082 (and (memq 'item contexts)
7083 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
7084 (and (memq 'item-body contexts)
7085 (org-in-item-p)))
7086 (goto-char pos))))
7088 (defun org-get-local-variables ()
7089 "Return a list of all local variables in an org-mode buffer."
7090 (let (varlist)
7091 (with-current-buffer (get-buffer-create "*Org tmp*")
7092 (erase-buffer)
7093 (org-mode)
7094 (setq varlist (buffer-local-variables)))
7095 (kill-buffer "*Org tmp*")
7096 (delq nil
7097 (mapcar
7098 (lambda (x)
7099 (setq x
7100 (if (symbolp x)
7101 (list x)
7102 (list (car x) (list 'quote (cdr x)))))
7103 (if (string-match
7104 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7105 (symbol-name (car x)))
7106 x nil))
7107 varlist))))
7109 ;;;###autoload
7110 (defun org-run-like-in-org-mode (cmd)
7111 "Run a command, pretending that the current buffer is in Org-mode.
7112 This will temporarily bind local variables that are typically bound in
7113 Org-mode to the values they have in Org-mode, and then interactively
7114 call CMD."
7115 (org-load-modules-maybe)
7116 (unless org-local-vars
7117 (setq org-local-vars (org-get-local-variables)))
7118 (eval (list 'let org-local-vars
7119 (list 'call-interactively (list 'quote cmd)))))
7121 ;;;; Archiving
7123 (defun org-get-category (&optional pos)
7124 "Get the category applying to position POS."
7125 (get-text-property (or pos (point)) 'org-category))
7127 (defun org-refresh-category-properties ()
7128 "Refresh category text properties in the buffer."
7129 (let ((def-cat (cond
7130 ((null org-category)
7131 (if buffer-file-name
7132 (file-name-sans-extension
7133 (file-name-nondirectory buffer-file-name))
7134 "???"))
7135 ((symbolp org-category) (symbol-name org-category))
7136 (t org-category)))
7137 beg end cat pos optionp)
7138 (org-unmodified
7139 (save-excursion
7140 (save-restriction
7141 (widen)
7142 (goto-char (point-min))
7143 (put-text-property (point) (point-max) 'org-category def-cat)
7144 (while (re-search-forward
7145 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
7146 (setq pos (match-end 0)
7147 optionp (equal (char-after (match-beginning 0)) ?#)
7148 cat (org-trim (match-string 2)))
7149 (if optionp
7150 (setq beg (point-at-bol) end (point-max))
7151 (org-back-to-heading t)
7152 (setq beg (point) end (org-end-of-subtree t t)))
7153 (put-text-property beg end 'org-category cat)
7154 (goto-char pos)))))))
7157 ;;;; Link Stuff
7159 ;;; Link abbreviations
7161 (defun org-link-expand-abbrev (link)
7162 "Apply replacements as defined in `org-link-abbrev-alist."
7163 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
7164 (let* ((key (match-string 1 link))
7165 (as (or (assoc key org-link-abbrev-alist-local)
7166 (assoc key org-link-abbrev-alist)))
7167 (tag (and (match-end 2) (match-string 3 link)))
7168 rpl)
7169 (if (not as)
7170 link
7171 (setq rpl (cdr as))
7172 (cond
7173 ((symbolp rpl) (funcall rpl tag))
7174 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
7175 ((string-match "%h" rpl)
7176 (replace-match (url-hexify-string (or tag "")) t t rpl))
7177 (t (concat rpl tag)))))
7178 link))
7180 ;;; Storing and inserting links
7182 (defvar org-insert-link-history nil
7183 "Minibuffer history for links inserted with `org-insert-link'.")
7185 (defvar org-stored-links nil
7186 "Contains the links stored with `org-store-link'.")
7188 (defvar org-store-link-plist nil
7189 "Plist with info about the most recently link created with `org-store-link'.")
7191 (defvar org-link-protocols nil
7192 "Link protocols added to Org-mode using `org-add-link-type'.")
7194 (defvar org-store-link-functions nil
7195 "List of functions that are called to create and store a link.
7196 Each function will be called in turn until one returns a non-nil
7197 value. Each function should check if it is responsible for creating
7198 this link (for example by looking at the major mode).
7199 If not, it must exit and return nil.
7200 If yes, it should return a non-nil value after a calling
7201 `org-store-link-props' with a list of properties and values.
7202 Special properties are:
7204 :type The link prefix. like \"http\". This must be given.
7205 :link The link, like \"http://www.astro.uva.nl/~dominik\".
7206 This is obligatory as well.
7207 :description Optional default description for the second pair
7208 of brackets in an Org-mode link. The user can still change
7209 this when inserting this link into an Org-mode buffer.
7211 In addition to these, any additional properties can be specified
7212 and then used in remember templates.")
7214 (defun org-add-link-type (type &optional follow export)
7215 "Add TYPE to the list of `org-link-types'.
7216 Re-compute all regular expressions depending on `org-link-types'
7218 FOLLOW and EXPORT are two functions.
7220 FOLLOW should take the link path as the single argument and do whatever
7221 is necessary to follow the link, for example find a file or display
7222 a mail message.
7224 EXPORT should format the link path for export to one of the export formats.
7225 It should be a function accepting three arguments:
7227 path the path of the link, the text after the prefix (like \"http:\")
7228 desc the description of the link, if any, nil if there was no description
7229 format the export format, a symbol like `html' or `latex'.
7231 The function may use the FORMAT information to return different values
7232 depending on the format. The return value will be put literally into
7233 the exported file.
7234 Org-mode has a built-in default for exporting links. If you are happy with
7235 this default, there is no need to define an export function for the link
7236 type. For a simple example of an export function, see `org-bbdb.el'."
7237 (add-to-list 'org-link-types type t)
7238 (org-make-link-regexps)
7239 (if (assoc type org-link-protocols)
7240 (setcdr (assoc type org-link-protocols) (list follow export))
7241 (push (list type follow export) org-link-protocols)))
7243 (defvar org-agenda-buffer-name)
7245 ;;;###autoload
7246 (defun org-store-link (arg)
7247 "\\<org-mode-map>Store an org-link to the current location.
7248 This link is added to `org-stored-links' and can later be inserted
7249 into an org-buffer with \\[org-insert-link].
7251 For some link types, a prefix arg is interpreted:
7252 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
7253 For file links, arg negates `org-context-in-file-links'."
7254 (interactive "P")
7255 (org-load-modules-maybe)
7256 (setq org-store-link-plist nil) ; reset
7257 (let ((outline-regexp (org-get-limited-outline-regexp))
7258 link cpltxt desc description search txt custom-id)
7259 (cond
7261 ((run-hook-with-args-until-success 'org-store-link-functions)
7262 (setq link (plist-get org-store-link-plist :link)
7263 desc (or (plist-get org-store-link-plist :description) link)))
7265 ((equal (buffer-name) "*Org Edit Src Example*")
7266 (let (label gc)
7267 (while (or (not label)
7268 (save-excursion
7269 (save-restriction
7270 (widen)
7271 (goto-char (point-min))
7272 (re-search-forward
7273 (regexp-quote (format org-coderef-label-format label))
7274 nil t))))
7275 (when label (message "Label exists already") (sit-for 2))
7276 (setq label (read-string "Code line label: " label)))
7277 (end-of-line 1)
7278 (setq link (format org-coderef-label-format label))
7279 (setq gc (- 79 (length link)))
7280 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
7281 (insert link)
7282 (setq link (concat "(" label ")") desc nil)))
7284 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
7285 ;; We are in the agenda, link to referenced location
7286 (let ((m (or (get-text-property (point) 'org-hd-marker)
7287 (get-text-property (point) 'org-marker))))
7288 (when m
7289 (org-with-point-at m
7290 (call-interactively 'org-store-link)))))
7292 ((eq major-mode 'calendar-mode)
7293 (let ((cd (calendar-cursor-to-date)))
7294 (setq link
7295 (format-time-string
7296 (car org-time-stamp-formats)
7297 (apply 'encode-time
7298 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
7299 nil nil nil))))
7300 (org-store-link-props :type "calendar" :date cd)))
7302 ((eq major-mode 'w3-mode)
7303 (setq cpltxt (if (and (buffer-name)
7304 (not (string-match "Untitled" (buffer-name))))
7305 (buffer-name)
7306 (url-view-url t))
7307 link (org-make-link (url-view-url t)))
7308 (org-store-link-props :type "w3" :url (url-view-url t)))
7310 ((eq major-mode 'w3m-mode)
7311 (setq cpltxt (or w3m-current-title w3m-current-url)
7312 link (org-make-link w3m-current-url))
7313 (org-store-link-props :type "w3m" :url (url-view-url t)))
7315 ((setq search (run-hook-with-args-until-success
7316 'org-create-file-search-functions))
7317 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
7318 "::" search))
7319 (setq cpltxt (or description link)))
7321 ((eq major-mode 'image-mode)
7322 (setq cpltxt (concat "file:"
7323 (abbreviate-file-name buffer-file-name))
7324 link (org-make-link cpltxt))
7325 (org-store-link-props :type "image" :file buffer-file-name))
7327 ((eq major-mode 'dired-mode)
7328 ;; link to the file in the current line
7329 (setq cpltxt (concat "file:"
7330 (abbreviate-file-name
7331 (expand-file-name
7332 (dired-get-filename nil t))))
7333 link (org-make-link cpltxt)))
7335 ((and buffer-file-name (org-mode-p))
7336 (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
7337 (cond
7338 ((org-in-regexp "<<\\(.*?\\)>>")
7339 (setq cpltxt
7340 (concat "file:"
7341 (abbreviate-file-name buffer-file-name)
7342 "::" (match-string 1))
7343 link (org-make-link cpltxt)))
7344 ((and (featurep 'org-id)
7345 (or (eq org-link-to-org-use-id t)
7346 (and (eq org-link-to-org-use-id 'create-if-interactive)
7347 (interactive-p))
7348 (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
7349 (interactive-p)
7350 (not custom-id))
7351 (and org-link-to-org-use-id
7352 (condition-case nil
7353 (org-entry-get nil "ID")
7354 (error nil)))))
7355 ;; We can make a link using the ID.
7356 (setq link (condition-case nil
7357 (prog1 (org-id-store-link)
7358 (setq desc (plist-get org-store-link-plist
7359 :description)))
7360 (error
7361 ;; probably before first headline, link to file only
7362 (concat "file:"
7363 (abbreviate-file-name buffer-file-name))))))
7365 ;; Just link to current headline
7366 (setq cpltxt (concat "file:"
7367 (abbreviate-file-name buffer-file-name)))
7368 ;; Add a context search string
7369 (when (org-xor org-context-in-file-links arg)
7370 (setq txt (cond
7371 ((org-on-heading-p) nil)
7372 ((org-region-active-p)
7373 (buffer-substring (region-beginning) (region-end)))
7374 (t nil)))
7375 (when (or (null txt) (string-match "\\S-" txt))
7376 (setq cpltxt
7377 (concat cpltxt "::"
7378 (condition-case nil
7379 (org-make-org-heading-search-string txt)
7380 (error "")))
7381 desc (or (nth 4 (ignore-errors
7382 (org-heading-components))) "NONE"))))
7383 (if (string-match "::\\'" cpltxt)
7384 (setq cpltxt (substring cpltxt 0 -2)))
7385 (setq link (org-make-link cpltxt)))))
7387 ((buffer-file-name (buffer-base-buffer))
7388 ;; Just link to this file here.
7389 (setq cpltxt (concat "file:"
7390 (abbreviate-file-name
7391 (buffer-file-name (buffer-base-buffer)))))
7392 ;; Add a context string
7393 (when (org-xor org-context-in-file-links arg)
7394 (setq txt (if (org-region-active-p)
7395 (buffer-substring (region-beginning) (region-end))
7396 (buffer-substring (point-at-bol) (point-at-eol))))
7397 ;; Only use search option if there is some text.
7398 (when (string-match "\\S-" txt)
7399 (setq cpltxt
7400 (concat cpltxt "::" (org-make-org-heading-search-string txt))
7401 desc "NONE")))
7402 (setq link (org-make-link cpltxt)))
7404 ((interactive-p)
7405 (error "Cannot link to a buffer which is not visiting a file"))
7407 (t (setq link nil)))
7409 (if (consp link) (setq cpltxt (car link) link (cdr link)))
7410 (setq link (or link cpltxt)
7411 desc (or desc cpltxt))
7412 (if (equal desc "NONE") (setq desc nil))
7414 (if (and (or (interactive-p) executing-kbd-macro) link)
7415 (progn
7416 (setq org-stored-links
7417 (cons (list link desc) org-stored-links))
7418 (message "Stored: %s" (or desc link))
7419 (when custom-id
7420 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
7421 "::#" custom-id))
7422 (setq org-stored-links
7423 (cons (list link desc) org-stored-links))))
7424 (and link (org-make-link-string link desc)))))
7426 (defun org-store-link-props (&rest plist)
7427 "Store link properties, extract names and addresses."
7428 (let (x adr)
7429 (when (setq x (plist-get plist :from))
7430 (setq adr (mail-extract-address-components x))
7431 (setq plist (plist-put plist :fromname (car adr)))
7432 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
7433 (when (setq x (plist-get plist :to))
7434 (setq adr (mail-extract-address-components x))
7435 (setq plist (plist-put plist :toname (car adr)))
7436 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
7437 (let ((from (plist-get plist :from))
7438 (to (plist-get plist :to)))
7439 (when (and from to org-from-is-user-regexp)
7440 (setq plist
7441 (plist-put plist :fromto
7442 (if (string-match org-from-is-user-regexp from)
7443 (concat "to %t")
7444 (concat "from %f"))))))
7445 (setq org-store-link-plist plist))
7447 (defun org-add-link-props (&rest plist)
7448 "Add these properties to the link property list."
7449 (let (key value)
7450 (while plist
7451 (setq key (pop plist) value (pop plist))
7452 (setq org-store-link-plist
7453 (plist-put org-store-link-plist key value)))))
7455 (defun org-email-link-description (&optional fmt)
7456 "Return the description part of an email link.
7457 This takes information from `org-store-link-plist' and formats it
7458 according to FMT (default from `org-email-link-description-format')."
7459 (setq fmt (or fmt org-email-link-description-format))
7460 (let* ((p org-store-link-plist)
7461 (to (plist-get p :toaddress))
7462 (from (plist-get p :fromaddress))
7463 (table
7464 (list
7465 (cons "%c" (plist-get p :fromto))
7466 (cons "%F" (plist-get p :from))
7467 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
7468 (cons "%T" (plist-get p :to))
7469 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
7470 (cons "%s" (plist-get p :subject))
7471 (cons "%m" (plist-get p :message-id)))))
7472 (when (string-match "%c" fmt)
7473 ;; Check if the user wrote this message
7474 (if (and org-from-is-user-regexp from to
7475 (save-match-data (string-match org-from-is-user-regexp from)))
7476 (setq fmt (replace-match "to %t" t t fmt))
7477 (setq fmt (replace-match "from %f" t t fmt))))
7478 (org-replace-escapes fmt table)))
7480 (defun org-make-org-heading-search-string (&optional string heading)
7481 "Make search string for STRING or current headline."
7482 (interactive)
7483 (let ((s (or string (org-get-heading))))
7484 (unless (and string (not heading))
7485 ;; We are using a headline, clean up garbage in there.
7486 (if (string-match org-todo-regexp s)
7487 (setq s (replace-match "" t t s)))
7488 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
7489 (setq s (replace-match "" t t s)))
7490 (setq s (org-trim s))
7491 (if (string-match (concat "^\\(" org-quote-string "\\|"
7492 org-comment-string "\\)") s)
7493 (setq s (replace-match "" t t s)))
7494 (while (string-match org-ts-regexp s)
7495 (setq s (replace-match "" t t s))))
7496 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
7497 (setq s (replace-match " " t t s)))
7498 (or string (setq s (concat "*" s))) ; Add * for headlines
7499 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
7501 (defun org-make-link (&rest strings)
7502 "Concatenate STRINGS."
7503 (apply 'concat strings))
7505 (defun org-make-link-string (link &optional description)
7506 "Make a link with brackets, consisting of LINK and DESCRIPTION."
7507 (unless (string-match "\\S-" link)
7508 (error "Empty link"))
7509 (when (stringp description)
7510 ;; Remove brackets from the description, they are fatal.
7511 (while (string-match "\\[" description)
7512 (setq description (replace-match "{" t t description)))
7513 (while (string-match "\\]" description)
7514 (setq description (replace-match "}" t t description))))
7515 (when (equal (org-link-escape link) description)
7516 ;; No description needed, it is identical
7517 (setq description nil))
7518 (when (and (not description)
7519 (not (equal link (org-link-escape link))))
7520 (setq description (org-extract-attributes link)))
7521 (concat "[[" (org-link-escape link) "]"
7522 (if description (concat "[" description "]") "")
7523 "]"))
7525 (defconst org-link-escape-chars
7526 '((?\ . "%20")
7527 (?\[ . "%5B")
7528 (?\] . "%5D")
7529 (?\340 . "%E0") ; `a
7530 (?\342 . "%E2") ; ^a
7531 (?\347 . "%E7") ; ,c
7532 (?\350 . "%E8") ; `e
7533 (?\351 . "%E9") ; 'e
7534 (?\352 . "%EA") ; ^e
7535 (?\356 . "%EE") ; ^i
7536 (?\364 . "%F4") ; ^o
7537 (?\371 . "%F9") ; `u
7538 (?\373 . "%FB") ; ^u
7539 (?\; . "%3B")
7540 (?? . "%3F")
7541 (?= . "%3D")
7542 (?+ . "%2B")
7544 "Association list of escapes for some characters problematic in links.
7545 This is the list that is used for internal purposes.")
7547 (defvar org-url-encoding-use-url-hexify nil)
7549 (defconst org-link-escape-chars-browser
7550 '((?\ . "%20")) ; 32 for the SPC char
7551 "Association list of escapes for some characters problematic in links.
7552 This is the list that is used before handing over to the browser.")
7554 (defun org-link-escape (text &optional table)
7555 "Escape characters in TEXT that are problematic for links."
7556 (if org-url-encoding-use-url-hexify
7557 (url-hexify-string text)
7558 (setq table (or table org-link-escape-chars))
7559 (when text
7560 (let ((re (mapconcat (lambda (x) (regexp-quote
7561 (char-to-string (car x))))
7562 table "\\|")))
7563 (while (string-match re text)
7564 (setq text
7565 (replace-match
7566 (cdr (assoc (string-to-char (match-string 0 text))
7567 table))
7568 t t text)))
7569 text))))
7571 (defun org-link-unescape (text &optional table)
7572 "Reverse the action of `org-link-escape'."
7573 (if org-url-encoding-use-url-hexify
7574 (url-unhex-string text)
7575 (setq table (or table org-link-escape-chars))
7576 (when text
7577 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
7578 table "\\|")))
7579 (while (string-match re text)
7580 (setq text
7581 (replace-match
7582 (char-to-string (car (rassoc (match-string 0 text) table)))
7583 t t text)))
7584 text))))
7586 (defun org-xor (a b)
7587 "Exclusive or."
7588 (if a (not b) b))
7590 (defun org-fixup-message-id-for-http (s)
7591 "Replace special characters in a message id, so it can be used in an http query."
7592 (while (string-match "<" s)
7593 (setq s (replace-match "%3C" t t s)))
7594 (while (string-match ">" s)
7595 (setq s (replace-match "%3E" t t s)))
7596 (while (string-match "@" s)
7597 (setq s (replace-match "%40" t t s)))
7600 ;;;###autoload
7601 (defun org-insert-link-global ()
7602 "Insert a link like Org-mode does.
7603 This command can be called in any mode to insert a link in Org-mode syntax."
7604 (interactive)
7605 (org-load-modules-maybe)
7606 (org-run-like-in-org-mode 'org-insert-link))
7608 (defun org-insert-link (&optional complete-file link-location)
7609 "Insert a link. At the prompt, enter the link.
7611 Completion can be used to insert any of the link protocol prefixes like
7612 http or ftp in use.
7614 The history can be used to select a link previously stored with
7615 `org-store-link'. When the empty string is entered (i.e. if you just
7616 press RET at the prompt), the link defaults to the most recently
7617 stored link. As SPC triggers completion in the minibuffer, you need to
7618 use M-SPC or C-q SPC to force the insertion of a space character.
7620 You will also be prompted for a description, and if one is given, it will
7621 be displayed in the buffer instead of the link.
7623 If there is already a link at point, this command will allow you to edit link
7624 and description parts.
7626 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
7627 be selected using completion. The path to the file will be relative to the
7628 current directory if the file is in the current directory or a subdirectory.
7629 Otherwise, the link will be the absolute path as completed in the minibuffer
7630 \(i.e. normally ~/path/to/file). You can configure this behavior using the
7631 option `org-link-file-path-type'.
7633 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
7634 the current directory or below.
7636 With three \\[universal-argument] prefixes, negate the meaning of
7637 `org-keep-stored-link-after-insertion'.
7639 If `org-make-link-description-function' is non-nil, this function will be
7640 called with the link target, and the result will be the default
7641 link description.
7643 If the LINK-LOCATION parameter is non-nil, this value will be
7644 used as the link location instead of reading one interactively."
7645 (interactive "P")
7646 (let* ((wcf (current-window-configuration))
7647 (region (if (org-region-active-p)
7648 (buffer-substring (region-beginning) (region-end))))
7649 (remove (and region (list (region-beginning) (region-end))))
7650 (desc region)
7651 tmphist ; byte-compile incorrectly complains about this
7652 (link link-location)
7653 entry file all-prefixes)
7654 (cond
7655 (link-location) ; specified by arg, just use it.
7656 ((org-in-regexp org-bracket-link-regexp 1)
7657 ;; We do have a link at point, and we are going to edit it.
7658 (setq remove (list (match-beginning 0) (match-end 0)))
7659 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
7660 (setq link (read-string "Link: "
7661 (org-link-unescape
7662 (org-match-string-no-properties 1)))))
7663 ((or (org-in-regexp org-angle-link-re)
7664 (org-in-regexp org-plain-link-re))
7665 ;; Convert to bracket link
7666 (setq remove (list (match-beginning 0) (match-end 0))
7667 link (read-string "Link: "
7668 (org-remove-angle-brackets (match-string 0)))))
7669 ((member complete-file '((4) (16)))
7670 ;; Completing read for file names.
7671 (setq link (org-file-complete-link complete-file)))
7673 ;; Read link, with completion for stored links.
7674 (with-output-to-temp-buffer "*Org Links*"
7675 (princ "Insert a link.
7676 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
7677 (when org-stored-links
7678 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
7679 (princ (mapconcat
7680 (lambda (x)
7681 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
7682 (reverse org-stored-links) "\n"))))
7683 (let ((cw (selected-window)))
7684 (select-window (get-buffer-window "*Org Links*"))
7685 (setq truncate-lines t)
7686 (unless (pos-visible-in-window-p (point-max))
7687 (org-fit-window-to-buffer))
7688 (and (window-live-p cw) (select-window cw)))
7689 ;; Fake a link history, containing the stored links.
7690 (setq tmphist (append (mapcar 'car org-stored-links)
7691 org-insert-link-history))
7692 (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
7693 (mapcar 'car org-link-abbrev-alist)
7694 org-link-types))
7695 (unwind-protect
7696 (progn
7697 (setq link
7698 (let ((org-completion-use-ido nil)
7699 (org-completion-use-iswitchb nil))
7700 (org-completing-read
7701 "Link: "
7702 (append
7703 (mapcar (lambda (x) (list (concat x ":")))
7704 all-prefixes)
7705 (mapcar 'car org-stored-links))
7706 nil nil nil
7707 'tmphist
7708 (car (car org-stored-links)))))
7709 (if (or (member link all-prefixes)
7710 (and (equal ":" (substring link -1))
7711 (member (substring link 0 -1) all-prefixes)
7712 (setq link (substring link 0 -1))))
7713 (setq link (org-link-try-special-completion link))))
7714 (set-window-configuration wcf)
7715 (kill-buffer "*Org Links*"))
7716 (setq entry (assoc link org-stored-links))
7717 (or entry (push link org-insert-link-history))
7718 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
7719 (not org-keep-stored-link-after-insertion))
7720 (setq org-stored-links (delq (assoc link org-stored-links)
7721 org-stored-links)))
7722 (setq desc (or desc (nth 1 entry)))))
7724 (if (string-match org-plain-link-re link)
7725 ;; URL-like link, normalize the use of angular brackets.
7726 (setq link (org-make-link (org-remove-angle-brackets link))))
7728 ;; Check if we are linking to the current file with a search option
7729 ;; If yes, simplify the link by using only the search option.
7730 (when (and buffer-file-name
7731 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
7732 (let* ((path (match-string 1 link))
7733 (case-fold-search nil)
7734 (search (match-string 2 link)))
7735 (save-match-data
7736 (if (equal (file-truename buffer-file-name) (file-truename path))
7737 ;; We are linking to this same file, with a search option
7738 (setq link search)))))
7740 ;; Check if we can/should use a relative path. If yes, simplify the link
7741 (when (string-match "^file:\\(.*\\)" link)
7742 (let* ((path (match-string 1 link))
7743 (origpath path)
7744 (case-fold-search nil))
7745 (cond
7746 ((or (eq org-link-file-path-type 'absolute)
7747 (equal complete-file '(16)))
7748 (setq path (abbreviate-file-name (expand-file-name path))))
7749 ((eq org-link-file-path-type 'noabbrev)
7750 (setq path (expand-file-name path)))
7751 ((eq org-link-file-path-type 'relative)
7752 (setq path (file-relative-name path)))
7754 (save-match-data
7755 (if (string-match (concat "^" (regexp-quote
7756 (file-name-as-directory
7757 (expand-file-name "."))))
7758 (expand-file-name path))
7759 ;; We are linking a file with relative path name.
7760 (setq path (substring (expand-file-name path)
7761 (match-end 0)))
7762 (setq path (abbreviate-file-name (expand-file-name path)))))))
7763 (setq link (concat "file:" path))
7764 (if (equal desc origpath)
7765 (setq desc path))))
7767 (if org-make-link-description-function
7768 (setq desc (funcall org-make-link-description-function link desc)))
7770 (setq desc (read-string "Description: " desc))
7771 (unless (string-match "\\S-" desc) (setq desc nil))
7772 (if remove (apply 'delete-region remove))
7773 (insert (org-make-link-string link desc))))
7775 (defun org-link-try-special-completion (type)
7776 "If there is completion support for link type TYPE, offer it."
7777 (let ((fun (intern (concat "org-" type "-complete-link"))))
7778 (if (functionp fun)
7779 (funcall fun)
7780 (read-string "Link (no completion support): " (concat type ":")))))
7782 (defun org-file-complete-link (&optional arg)
7783 "Create a file link using completion."
7784 (let (file link)
7785 (setq file (read-file-name "File: "))
7786 (let ((pwd (file-name-as-directory (expand-file-name ".")))
7787 (pwd1 (file-name-as-directory (abbreviate-file-name
7788 (expand-file-name ".")))))
7789 (cond
7790 ((equal arg '(16))
7791 (setq link (org-make-link
7792 "file:"
7793 (abbreviate-file-name (expand-file-name file)))))
7794 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
7795 (setq link (org-make-link "file:" (match-string 1 file))))
7796 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
7797 (expand-file-name file))
7798 (setq link (org-make-link
7799 "file:" (match-string 1 (expand-file-name file)))))
7800 (t (setq link (org-make-link "file:" file)))))
7801 link))
7803 (defun org-completing-read (&rest args)
7804 "Completing-read with SPACE being a normal character."
7805 (let ((minibuffer-local-completion-map
7806 (copy-keymap minibuffer-local-completion-map)))
7807 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
7808 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
7809 (apply 'org-icompleting-read args)))
7811 (defun org-completing-read-no-i (&rest args)
7812 (let (org-completion-use-ido org-completion-use-iswitchb)
7813 (apply 'org-completing-read args)))
7815 (defun org-iswitchb-completing-read (prompt choices &rest args)
7816 "Use iswitch as a completing-read replacement to choose from choices.
7817 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
7818 from."
7819 (let* ((iswitchb-use-virtual-buffers nil)
7820 (iswitchb-make-buflist-hook
7821 (lambda ()
7822 (setq iswitchb-temp-buflist choices))))
7823 (iswitchb-read-buffer prompt)))
7825 (defun org-icompleting-read (&rest args)
7826 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
7827 (org-without-partial-completion
7828 (if (and org-completion-use-ido
7829 (fboundp 'ido-completing-read)
7830 (boundp 'ido-mode) ido-mode
7831 (listp (second args)))
7832 (let ((ido-enter-matching-directory nil))
7833 (apply 'ido-completing-read (concat (car args))
7834 (if (consp (car (nth 1 args)))
7835 (mapcar (lambda (x) (car x)) (nth 1 args))
7836 (nth 1 args))
7837 (cddr args)))
7838 (if (and org-completion-use-iswitchb
7839 (boundp 'iswitchb-mode) iswitchb-mode
7840 (listp (second args)))
7841 (apply 'org-iswitchb-completing-read (concat (car args))
7842 (if (consp (car (nth 1 args)))
7843 (mapcar (lambda (x) (car x)) (nth 1 args))
7844 (nth 1 args))
7845 (cddr args))
7846 (apply 'completing-read args)))))
7848 (defun org-extract-attributes (s)
7849 "Extract the attributes cookie from a string and set as text property."
7850 (let (a attr (start 0) key value)
7851 (save-match-data
7852 (when (string-match "{{\\([^}]+\\)}}$" s)
7853 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
7854 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
7855 (setq key (match-string 1 a) value (match-string 2 a)
7856 start (match-end 0)
7857 attr (plist-put attr (intern key) value))))
7858 (org-add-props s nil 'org-attr attr))
7861 (defun org-extract-attributes-from-string (tag)
7862 (let (key value attr)
7863 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
7864 (setq key (match-string 1 tag) value (match-string 2 tag)
7865 tag (replace-match "" t t tag)
7866 attr (plist-put attr (intern key) value)))
7867 (cons tag attr)))
7869 (defun org-attributes-to-string (plist)
7870 "Format a property list into an HTML attribute list."
7871 (let ((s "") key value)
7872 (while plist
7873 (setq key (pop plist) value (pop plist))
7874 (and value
7875 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
7878 ;;; Opening/following a link
7880 (defvar org-link-search-failed nil)
7882 (defun org-next-link ()
7883 "Move forward to the next link.
7884 If the link is in hidden text, expose it."
7885 (interactive)
7886 (when (and org-link-search-failed (eq this-command last-command))
7887 (goto-char (point-min))
7888 (message "Link search wrapped back to beginning of buffer"))
7889 (setq org-link-search-failed nil)
7890 (let* ((pos (point))
7891 (ct (org-context))
7892 (a (assoc :link ct)))
7893 (if a (goto-char (nth 2 a)))
7894 (if (re-search-forward org-any-link-re nil t)
7895 (progn
7896 (goto-char (match-beginning 0))
7897 (if (org-invisible-p) (org-show-context)))
7898 (goto-char pos)
7899 (setq org-link-search-failed t)
7900 (error "No further link found"))))
7902 (defun org-previous-link ()
7903 "Move backward to the previous link.
7904 If the link is in hidden text, expose it."
7905 (interactive)
7906 (when (and org-link-search-failed (eq this-command last-command))
7907 (goto-char (point-max))
7908 (message "Link search wrapped back to end of buffer"))
7909 (setq org-link-search-failed nil)
7910 (let* ((pos (point))
7911 (ct (org-context))
7912 (a (assoc :link ct)))
7913 (if a (goto-char (nth 1 a)))
7914 (if (re-search-backward org-any-link-re nil t)
7915 (progn
7916 (goto-char (match-beginning 0))
7917 (if (org-invisible-p) (org-show-context)))
7918 (goto-char pos)
7919 (setq org-link-search-failed t)
7920 (error "No further link found"))))
7922 (defun org-translate-link (s)
7923 "Translate a link string if a translation function has been defined."
7924 (if (and org-link-translation-function
7925 (fboundp org-link-translation-function)
7926 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
7927 (progn
7928 (setq s (funcall org-link-translation-function
7929 (match-string 1) (match-string 2)))
7930 (concat (car s) ":" (cdr s)))
7933 (defun org-translate-link-from-planner (type path)
7934 "Translate a link from Emacs Planner syntax so that Org can follow it.
7935 This is still an experimental function, your mileage may vary."
7936 (cond
7937 ((member type '("http" "https" "news" "ftp"))
7938 ;; standard Internet links are the same.
7939 nil)
7940 ((and (equal type "irc") (string-match "^//" path))
7941 ;; Planner has two / at the beginning of an irc link, we have 1.
7942 ;; We should have zero, actually....
7943 (setq path (substring path 1)))
7944 ((and (equal type "lisp") (string-match "^/" path))
7945 ;; Planner has a slash, we do not.
7946 (setq type "elisp" path (substring path 1)))
7947 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
7948 ;; A typical message link. Planner has the id after the fina slash,
7949 ;; we separate it with a hash mark
7950 (setq path (concat (match-string 1 path) "#"
7951 (org-remove-angle-brackets (match-string 2 path)))))
7953 (cons type path))
7955 (defun org-find-file-at-mouse (ev)
7956 "Open file link or URL at mouse."
7957 (interactive "e")
7958 (mouse-set-point ev)
7959 (org-open-at-point 'in-emacs))
7961 (defun org-open-at-mouse (ev)
7962 "Open file link or URL at mouse."
7963 (interactive "e")
7964 (mouse-set-point ev)
7965 (if (eq major-mode 'org-agenda-mode)
7966 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
7967 (org-open-at-point))
7969 (defvar org-window-config-before-follow-link nil
7970 "The window configuration before following a link.
7971 This is saved in case the need arises to restore it.")
7973 (defvar org-open-link-marker (make-marker)
7974 "Marker pointing to the location where `org-open-at-point; was called.")
7976 ;;;###autoload
7977 (defun org-open-at-point-global ()
7978 "Follow a link like Org-mode does.
7979 This command can be called in any mode to follow a link that has
7980 Org-mode syntax."
7981 (interactive)
7982 (org-run-like-in-org-mode 'org-open-at-point))
7984 ;;;###autoload
7985 (defun org-open-link-from-string (s &optional arg reference-buffer)
7986 "Open a link in the string S, as if it was in Org-mode."
7987 (interactive "sLink: \nP")
7988 (let ((reference-buffer (or reference-buffer (current-buffer))))
7989 (with-temp-buffer
7990 (let ((org-inhibit-startup t))
7991 (org-mode)
7992 (insert s)
7993 (goto-char (point-min))
7994 (org-open-at-point arg reference-buffer)))))
7996 (defun org-open-at-point (&optional in-emacs reference-buffer)
7997 "Open link at or after point.
7998 If there is no link at point, this function will search forward up to
7999 the end of the current line.
8000 Normally, files will be opened by an appropriate application. If the
8001 optional argument IN-EMACS is non-nil, Emacs will visit the file.
8002 With a double prefix argument, try to open outside of Emacs, in the
8003 application the system uses for this file type."
8004 (interactive "P")
8005 (org-load-modules-maybe)
8006 (move-marker org-open-link-marker (point))
8007 (setq org-window-config-before-follow-link (current-window-configuration))
8008 (org-remove-occur-highlights nil nil t)
8009 (cond
8010 ((and (org-on-heading-p)
8011 (not (org-in-regexp
8012 (concat org-plain-link-re "\\|"
8013 org-bracket-link-regexp "\\|"
8014 org-angle-link-re "\\|"
8015 "[ \t]:[^ \t\n]+:[ \t]*$"))))
8016 (org-offer-links-in-entry in-emacs))
8017 ((org-at-timestamp-p t) (org-follow-timestamp-link))
8018 ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
8019 (org-footnote-action))
8021 (let (type path link line search (pos (point)))
8022 (catch 'match
8023 (save-excursion
8024 (skip-chars-forward "^]\n\r")
8025 (when (org-in-regexp org-bracket-link-regexp)
8026 (setq link (org-extract-attributes
8027 (org-link-unescape (org-match-string-no-properties 1))))
8028 (while (string-match " *\n *" link)
8029 (setq link (replace-match " " t t link)))
8030 (setq link (org-link-expand-abbrev link))
8031 (cond
8032 ((or (file-name-absolute-p link)
8033 (string-match "^\\.\\.?/" link))
8034 (setq type "file" path link))
8035 ((string-match org-link-re-with-space3 link)
8036 (setq type (match-string 1 link) path (match-string 2 link)))
8037 (t (setq type "thisfile" path link)))
8038 (throw 'match t)))
8040 (when (get-text-property (point) 'org-linked-text)
8041 (setq type "thisfile"
8042 pos (if (get-text-property (1+ (point)) 'org-linked-text)
8043 (1+ (point)) (point))
8044 path (buffer-substring
8045 (previous-single-property-change pos 'org-linked-text)
8046 (next-single-property-change pos 'org-linked-text)))
8047 (throw 'match t))
8049 (save-excursion
8050 (when (or (org-in-regexp org-angle-link-re)
8051 (org-in-regexp org-plain-link-re))
8052 (setq type (match-string 1) path (match-string 2))
8053 (throw 'match t)))
8054 (save-excursion
8055 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
8056 (setq type "tags"
8057 path (match-string 1))
8058 (while (string-match ":" path)
8059 (setq path (replace-match "+" t t path)))
8060 (throw 'match t)))
8061 (when (org-in-regexp "<\\([^><\n]+\\)>")
8062 (setq type "tree-match"
8063 path (match-string 1))
8064 (throw 'match t)))
8065 (unless path
8066 (error "No link found"))
8068 ;; switch back to reference buffer
8069 ;; needed when if called in a temporary buffer through
8070 ;; org-open-link-from-string
8071 (with-current-buffer (or reference-buffer (current-buffer))
8073 ;; Remove any trailing spaces in path
8074 (if (string-match " +\\'" path)
8075 (setq path (replace-match "" t t path)))
8076 (if (and org-link-translation-function
8077 (fboundp org-link-translation-function))
8078 ;; Check if we need to translate the link
8079 (let ((tmp (funcall org-link-translation-function type path)))
8080 (setq type (car tmp) path (cdr tmp))))
8082 (cond
8084 ((assoc type org-link-protocols)
8085 (funcall (nth 1 (assoc type org-link-protocols)) path))
8087 ((equal type "mailto")
8088 (let ((cmd (car org-link-mailto-program))
8089 (args (cdr org-link-mailto-program)) args1
8090 (address path) (subject "") a)
8091 (if (string-match "\\(.*\\)::\\(.*\\)" path)
8092 (setq address (match-string 1 path)
8093 subject (org-link-escape (match-string 2 path))))
8094 (while args
8095 (cond
8096 ((not (stringp (car args))) (push (pop args) args1))
8097 (t (setq a (pop args))
8098 (if (string-match "%a" a)
8099 (setq a (replace-match address t t a)))
8100 (if (string-match "%s" a)
8101 (setq a (replace-match subject t t a)))
8102 (push a args1))))
8103 (apply cmd (nreverse args1))))
8105 ((member type '("http" "https" "ftp" "news"))
8106 (browse-url (concat type ":" (org-link-escape
8107 path org-link-escape-chars-browser))))
8109 ((member type '("message"))
8110 (browse-url (concat type ":" path)))
8112 ((string= type "tags")
8113 (org-tags-view in-emacs path))
8114 ((string= type "thisfile")
8115 (if in-emacs
8116 (switch-to-buffer-other-window
8117 (org-get-buffer-for-internal-link (current-buffer)))
8118 (org-mark-ring-push))
8119 (let ((cmd `(org-link-search
8120 ,path
8121 ,(cond ((equal in-emacs '(4)) 'occur)
8122 ((equal in-emacs '(16)) 'org-occur)
8123 (t nil))
8124 ,pos)))
8125 (condition-case nil (eval cmd)
8126 (error (progn (widen) (eval cmd))))))
8128 ((string= type "tree-match")
8129 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
8131 ((string= type "file")
8132 (if (string-match "::\\([0-9]+\\)\\'" path)
8133 (setq line (string-to-number (match-string 1 path))
8134 path (substring path 0 (match-beginning 0)))
8135 (if (string-match "::\\(.+\\)\\'" path)
8136 (setq search (match-string 1 path)
8137 path (substring path 0 (match-beginning 0)))))
8138 (if (string-match "[*?{]" (file-name-nondirectory path))
8139 (dired path)
8140 (org-open-file path in-emacs line search)))
8142 ((string= type "news")
8143 (require 'org-gnus)
8144 (org-gnus-follow-link path))
8146 ((string= type "shell")
8147 (let ((cmd path))
8148 (if (or (not org-confirm-shell-link-function)
8149 (funcall org-confirm-shell-link-function
8150 (format "Execute \"%s\" in shell? "
8151 (org-add-props cmd nil
8152 'face 'org-warning))))
8153 (progn
8154 (message "Executing %s" cmd)
8155 (shell-command cmd))
8156 (error "Abort"))))
8158 ((string= type "elisp")
8159 (let ((cmd path))
8160 (if (or (not org-confirm-elisp-link-function)
8161 (funcall org-confirm-elisp-link-function
8162 (format "Execute \"%s\" as elisp? "
8163 (org-add-props cmd nil
8164 'face 'org-warning))))
8165 (message "%s => %s" cmd
8166 (if (equal (string-to-char cmd) ?\()
8167 (eval (read cmd))
8168 (call-interactively (read cmd))))
8169 (error "Abort"))))
8172 (browse-url-at-point)))))))
8173 (move-marker org-open-link-marker nil)
8174 (run-hook-with-args 'org-follow-link-hook))
8176 (defun org-offer-links-in-entry (&optional nth zero)
8177 "Offer links in the curren entry and follow the selected link.
8178 If there is only one link, follow it immediately as well.
8179 If NTH is an integer, immediately pick the NTH link found.
8180 If ZERO is a string, check also this string for a link, and if
8181 there is one, offer it as link number zero."
8182 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
8183 "\\(" org-angle-link-re "\\)\\|"
8184 "\\(" org-plain-link-re "\\)"))
8185 (cnt ?0)
8186 (in-emacs (if (integerp nth) nil nth))
8187 have-zero end links link c)
8188 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
8189 (push (match-string 0 zero) links)
8190 (setq cnt (1- cnt) have-zero t))
8191 (save-excursion
8192 (org-back-to-heading t)
8193 (setq end (save-excursion (outline-next-heading) (point)))
8194 (while (re-search-forward re end t)
8195 (push (match-string 0) links))
8196 (setq links (org-uniquify (reverse links))))
8198 (cond
8199 ((null links) (error "No links"))
8200 ((equal (length links) 1)
8201 (setq link (car links)))
8202 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
8203 (setq link (nth (if have-zero nth (1- nth)) links)))
8204 (t ; we have to select a link
8205 (save-excursion
8206 (save-window-excursion
8207 (delete-other-windows)
8208 (with-output-to-temp-buffer "*Select Link*"
8209 (mapc (lambda (l)
8210 (if (not (string-match org-bracket-link-regexp l))
8211 (princ (format "[%c] %s\n" (incf cnt)
8212 (org-remove-angle-brackets l)))
8213 (if (match-end 3)
8214 (princ (format "[%c] %s (%s)\n" (incf cnt)
8215 (match-string 3 l) (match-string 1 l)))
8216 (princ (format "[%c] %s\n" (incf cnt)
8217 (match-string 1 l))))))
8218 links))
8219 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
8220 (message "Select link to open:")
8221 (setq c (read-char-exclusive))
8222 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
8223 (when (equal c ?q) (error "Abort"))
8224 (setq nth (- c ?0))
8225 (if have-zero (setq nth (1+ nth)))
8226 (unless (and (integerp nth) (>= (length links) nth))
8227 (error "Invalid link selection"))
8228 (setq link (nth (1- nth) links))))
8229 (org-open-link-from-string link in-emacs (current-buffer))))
8231 ;;;; Time estimates
8233 (defun org-get-effort (&optional pom)
8234 "Get the effort estimate for the current entry."
8235 (org-entry-get pom org-effort-property))
8237 ;;; File search
8239 (defvar org-create-file-search-functions nil
8240 "List of functions to construct the right search string for a file link.
8241 These functions are called in turn with point at the location to
8242 which the link should point.
8244 A function in the hook should first test if it would like to
8245 handle this file type, for example by checking the major-mode or
8246 the file extension. If it decides not to handle this file, it
8247 should just return nil to give other functions a chance. If it
8248 does handle the file, it must return the search string to be used
8249 when following the link. The search string will be part of the
8250 file link, given after a double colon, and `org-open-at-point'
8251 will automatically search for it. If special measures must be
8252 taken to make the search successful, another function should be
8253 added to the companion hook `org-execute-file-search-functions',
8254 which see.
8256 A function in this hook may also use `setq' to set the variable
8257 `description' to provide a suggestion for the descriptive text to
8258 be used for this link when it gets inserted into an Org-mode
8259 buffer with \\[org-insert-link].")
8261 (defvar org-execute-file-search-functions nil
8262 "List of functions to execute a file search triggered by a link.
8264 Functions added to this hook must accept a single argument, the
8265 search string that was part of the file link, the part after the
8266 double colon. The function must first check if it would like to
8267 handle this search, for example by checking the major-mode or the
8268 file extension. If it decides not to handle this search, it
8269 should just return nil to give other functions a chance. If it
8270 does handle the search, it must return a non-nil value to keep
8271 other functions from trying.
8273 Each function can access the current prefix argument through the
8274 variable `current-prefix-argument'. Note that a single prefix is
8275 used to force opening a link in Emacs, so it may be good to only
8276 use a numeric or double prefix to guide the search function.
8278 In case this is needed, a function in this hook can also restore
8279 the window configuration before `org-open-at-point' was called using:
8281 (set-window-configuration org-window-config-before-follow-link)")
8283 (defun org-link-search (s &optional type avoid-pos)
8284 "Search for a link search option.
8285 If S is surrounded by forward slashes, it is interpreted as a
8286 regular expression. In org-mode files, this will create an `org-occur'
8287 sparse tree. In ordinary files, `occur' will be used to list matches.
8288 If the current buffer is in `dired-mode', grep will be used to search
8289 in all files. If AVOID-POS is given, ignore matches near that position."
8290 (let ((case-fold-search t)
8291 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
8292 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
8293 (append '(("") (" ") ("\t") ("\n"))
8294 org-emphasis-alist)
8295 "\\|") "\\)"))
8296 (pos (point))
8297 (pre nil) (post nil)
8298 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
8299 (cond
8300 ;; First check if there are any special
8301 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
8302 ;; Now try the builtin stuff
8303 ((and (equal (string-to-char s0) ?#)
8304 (> (length s0) 1)
8305 (save-excursion
8306 (goto-char (point-min))
8307 (and
8308 (re-search-forward
8309 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
8310 (setq type 'dedicated
8311 pos (match-beginning 0))))
8312 ;; There is an exact target for this
8313 (goto-char pos)
8314 (org-back-to-heading t)))
8315 ((save-excursion
8316 (goto-char (point-min))
8317 (and
8318 (re-search-forward
8319 (concat "<<" (regexp-quote s0) ">>") nil t)
8320 (setq type 'dedicated
8321 pos (match-beginning 0))))
8322 ;; There is an exact target for this
8323 (goto-char pos))
8324 ((and (string-match "^(\\(.*\\))$" s0)
8325 (save-excursion
8326 (goto-char (point-min))
8327 (and
8328 (re-search-forward
8329 (concat "[^[]" (regexp-quote
8330 (format org-coderef-label-format
8331 (match-string 1 s0))))
8332 nil t)
8333 (setq type 'dedicated
8334 pos (1+ (match-beginning 0))))))
8335 ;; There is a coderef target for this
8336 (goto-char pos))
8337 ((string-match "^/\\(.*\\)/$" s)
8338 ;; A regular expression
8339 (cond
8340 ((org-mode-p)
8341 (org-occur (match-string 1 s)))
8342 ;;((eq major-mode 'dired-mode)
8343 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
8344 (t (org-do-occur (match-string 1 s)))))
8346 ;; A normal search strings
8347 (when (equal (string-to-char s) ?*)
8348 ;; Anchor on headlines, post may include tags.
8349 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
8350 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
8351 s (substring s 1)))
8352 (remove-text-properties
8353 0 (length s)
8354 '(face nil mouse-face nil keymap nil fontified nil) s)
8355 ;; Make a series of regular expressions to find a match
8356 (setq words (org-split-string s "[ \n\r\t]+")
8358 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
8359 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
8360 "\\)" markers)
8361 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
8362 re2a (concat "[ \t\r\n]" re2a_)
8363 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
8364 re4 (concat "[^a-zA-Z_]" re4_)
8366 re1 (concat pre re2 post)
8367 re3 (concat pre (if pre re4_ re4) post)
8368 re5 (concat pre ".*" re4)
8369 re2 (concat pre re2)
8370 re2a (concat pre (if pre re2a_ re2a))
8371 re4 (concat pre (if pre re4_ re4))
8372 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
8373 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
8374 re5 "\\)"
8376 (cond
8377 ((eq type 'org-occur) (org-occur reall))
8378 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
8379 (t (goto-char (point-min))
8380 (setq type 'fuzzy)
8381 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
8382 (org-search-not-self 1 re1 nil t)
8383 (org-search-not-self 1 re2 nil t)
8384 (org-search-not-self 1 re2a nil t)
8385 (org-search-not-self 1 re3 nil t)
8386 (org-search-not-self 1 re4 nil t)
8387 (org-search-not-self 1 re5 nil t)
8389 (goto-char (match-beginning 1))
8390 (goto-char pos)
8391 (error "No match")))))
8393 ;; Normal string-search
8394 (goto-char (point-min))
8395 (if (search-forward s nil t)
8396 (goto-char (match-beginning 0))
8397 (error "No match"))))
8398 (and (org-mode-p) (org-show-context 'link-search))
8399 type))
8401 (defun org-search-not-self (group &rest args)
8402 "Execute `re-search-forward', but only accept matches that do not
8403 enclose the position of `org-open-link-marker'."
8404 (let ((m org-open-link-marker))
8405 (catch 'exit
8406 (while (apply 're-search-forward args)
8407 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
8408 (goto-char (match-end group))
8409 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
8410 (> (match-beginning 0) (marker-position m))
8411 (< (match-end 0) (marker-position m)))
8412 (save-match-data
8413 (or (not (org-in-regexp
8414 org-bracket-link-analytic-regexp 1))
8415 (not (match-end 4)) ; no description
8416 (and (<= (match-beginning 4) (point))
8417 (>= (match-end 4) (point))))))
8418 (throw 'exit (point))))))))
8420 (defun org-get-buffer-for-internal-link (buffer)
8421 "Return a buffer to be used for displaying the link target of internal links."
8422 (cond
8423 ((not org-display-internal-link-with-indirect-buffer)
8424 buffer)
8425 ((string-match "(Clone)$" (buffer-name buffer))
8426 (message "Buffer is already a clone, not making another one")
8427 ;; we also do not modify visibility in this case
8428 buffer)
8429 (t ; make a new indirect buffer for displaying the link
8430 (let* ((bn (buffer-name buffer))
8431 (ibn (concat bn "(Clone)"))
8432 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
8433 (with-current-buffer ib (org-overview))
8434 ib))))
8436 (defun org-do-occur (regexp &optional cleanup)
8437 "Call the Emacs command `occur'.
8438 If CLEANUP is non-nil, remove the printout of the regular expression
8439 in the *Occur* buffer. This is useful if the regex is long and not useful
8440 to read."
8441 (occur regexp)
8442 (when cleanup
8443 (let ((cwin (selected-window)) win beg end)
8444 (when (setq win (get-buffer-window "*Occur*"))
8445 (select-window win))
8446 (goto-char (point-min))
8447 (when (re-search-forward "match[a-z]+" nil t)
8448 (setq beg (match-end 0))
8449 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
8450 (setq end (1- (match-beginning 0)))))
8451 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
8452 (goto-char (point-min))
8453 (select-window cwin))))
8455 ;;; The mark ring for links jumps
8457 (defvar org-mark-ring nil
8458 "Mark ring for positions before jumps in Org-mode.")
8459 (defvar org-mark-ring-last-goto nil
8460 "Last position in the mark ring used to go back.")
8461 ;; Fill and close the ring
8462 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
8463 (loop for i from 1 to org-mark-ring-length do
8464 (push (make-marker) org-mark-ring))
8465 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
8466 org-mark-ring)
8468 (defun org-mark-ring-push (&optional pos buffer)
8469 "Put the current position or POS into the mark ring and rotate it."
8470 (interactive)
8471 (setq pos (or pos (point)))
8472 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
8473 (move-marker (car org-mark-ring)
8474 (or pos (point))
8475 (or buffer (current-buffer)))
8476 (message "%s"
8477 (substitute-command-keys
8478 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
8480 (defun org-mark-ring-goto (&optional n)
8481 "Jump to the previous position in the mark ring.
8482 With prefix arg N, jump back that many stored positions. When
8483 called several times in succession, walk through the entire ring.
8484 Org-mode commands jumping to a different position in the current file,
8485 or to another Org-mode file, automatically push the old position
8486 onto the ring."
8487 (interactive "p")
8488 (let (p m)
8489 (if (eq last-command this-command)
8490 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
8491 (setq p org-mark-ring))
8492 (setq org-mark-ring-last-goto p)
8493 (setq m (car p))
8494 (switch-to-buffer (marker-buffer m))
8495 (goto-char m)
8496 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
8498 (defun org-remove-angle-brackets (s)
8499 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
8500 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
8502 (defun org-add-angle-brackets (s)
8503 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
8504 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
8506 (defun org-remove-double-quotes (s)
8507 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
8508 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
8511 ;;; Following specific links
8513 (defun org-follow-timestamp-link ()
8514 (cond
8515 ((org-at-date-range-p t)
8516 (let ((org-agenda-start-on-weekday)
8517 (t1 (match-string 1))
8518 (t2 (match-string 2)))
8519 (setq t1 (time-to-days (org-time-string-to-time t1))
8520 t2 (time-to-days (org-time-string-to-time t2)))
8521 (org-agenda-list nil t1 (1+ (- t2 t1)))))
8522 ((org-at-timestamp-p t)
8523 (org-agenda-list nil (time-to-days (org-time-string-to-time
8524 (substring (match-string 1) 0 10)))
8526 (t (error "This should not happen"))))
8529 ;;; Following file links
8530 (defvar org-wait nil)
8531 (defun org-open-file (path &optional in-emacs line search)
8532 "Open the file at PATH.
8533 First, this expands any special file name abbreviations. Then the
8534 configuration variable `org-file-apps' is checked if it contains an
8535 entry for this file type, and if yes, the corresponding command is launched.
8537 If no application is found, Emacs simply visits the file.
8539 With optional prefix argument IN-EMACS, Emacs will visit the file.
8540 With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
8541 and o use an external application to visit the file.
8543 Optional LINE specifies a line to go to, optional SEARCH a string to
8544 search for. If LINE or SEARCH is given, the file will always be
8545 opened in Emacs.
8546 If the file does not exist, an error is thrown."
8547 (setq in-emacs (or in-emacs line search))
8548 (let* ((file (if (equal path "")
8549 buffer-file-name
8550 (substitute-in-file-name (expand-file-name path))))
8551 (apps (append org-file-apps (org-default-apps)))
8552 (remp (and (assq 'remote apps) (org-file-remote-p file)))
8553 (dirp (if remp nil (file-directory-p file)))
8554 (file (if (and dirp org-open-directory-means-index-dot-org)
8555 (concat (file-name-as-directory file) "index.org")
8556 file))
8557 (a-m-a-p (assq 'auto-mode apps))
8558 (dfile (downcase file))
8559 (old-buffer (current-buffer))
8560 (old-pos (point))
8561 (old-mode major-mode)
8562 ext cmd)
8563 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
8564 (setq ext (match-string 1 dfile))
8565 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
8566 (setq ext (match-string 1 dfile))))
8567 (cond
8568 ((equal in-emacs '(16))
8569 (setq cmd (cdr (assoc 'system apps))))
8570 (in-emacs (setq cmd 'emacs))
8572 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
8573 (and dirp (cdr (assoc 'directory apps)))
8574 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
8575 'string-match)
8576 (cdr (assoc ext apps))
8577 (cdr (assoc t apps))))))
8578 (when (eq cmd 'system)
8579 (setq cmd (cdr (assoc 'system apps))))
8580 (when (eq cmd 'default)
8581 (setq cmd (cdr (assoc t apps))))
8582 (when (eq cmd 'mailcap)
8583 (require 'mailcap)
8584 (mailcap-parse-mailcaps)
8585 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
8586 (command (mailcap-mime-info mime-type)))
8587 (if (stringp command)
8588 (setq cmd command)
8589 (setq cmd 'emacs))))
8590 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
8591 (not (file-exists-p file))
8592 (not org-open-non-existing-files))
8593 (error "No such file: %s" file))
8594 (cond
8595 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
8596 ;; Remove quotes around the file name - we'll use shell-quote-argument.
8597 (while (string-match "['\"]%s['\"]" cmd)
8598 (setq cmd (replace-match "%s" t t cmd)))
8599 (while (string-match "%s" cmd)
8600 (setq cmd (replace-match
8601 (save-match-data
8602 (shell-quote-argument
8603 (convert-standard-filename file)))
8604 t t cmd)))
8605 (save-window-excursion
8606 (start-process-shell-command cmd nil cmd)
8607 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
8609 ((or (stringp cmd)
8610 (eq cmd 'emacs))
8611 (funcall (cdr (assq 'file org-link-frame-setup)) file)
8612 (widen)
8613 (if line (org-goto-line line)
8614 (if search (org-link-search search))))
8615 ((consp cmd)
8616 (let ((file (convert-standard-filename file)))
8617 (eval cmd)))
8618 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
8619 (and (org-mode-p) (eq old-mode 'org-mode)
8620 (or (not (equal old-buffer (current-buffer)))
8621 (not (equal old-pos (point))))
8622 (org-mark-ring-push old-pos old-buffer))))
8624 (defun org-default-apps ()
8625 "Return the default applications for this operating system."
8626 (cond
8627 ((eq system-type 'darwin)
8628 org-file-apps-defaults-macosx)
8629 ((eq system-type 'windows-nt)
8630 org-file-apps-defaults-windowsnt)
8631 (t org-file-apps-defaults-gnu)))
8633 (defun org-apps-regexp-alist (list &optional add-auto-mode)
8634 "Convert extensions to regular expressions in the cars of LIST.
8635 Also, weed out any non-string entries, because the return value is used
8636 only for regexp matching.
8637 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
8638 point to the symbol `emacs', indicating that the file should
8639 be opened in Emacs."
8640 (append
8641 (delq nil
8642 (mapcar (lambda (x)
8643 (if (not (stringp (car x)))
8645 (if (string-match "\\W" (car x))
8647 (cons (concat "\\." (car x) "\\'") (cdr x)))))
8648 list))
8649 (if add-auto-mode
8650 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
8652 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
8653 (defun org-file-remote-p (file)
8654 "Test whether FILE specifies a location on a remote system.
8655 Return non-nil if the location is indeed remote.
8657 For example, the filename \"/user@host:/foo\" specifies a location
8658 on the system \"/user@host:\"."
8659 (cond ((fboundp 'file-remote-p)
8660 (file-remote-p file))
8661 ((fboundp 'tramp-handle-file-remote-p)
8662 (tramp-handle-file-remote-p file))
8663 ((and (boundp 'ange-ftp-name-format)
8664 (string-match (car ange-ftp-name-format) file))
8666 (t nil)))
8669 ;;;; Refiling
8671 (defun org-get-org-file ()
8672 "Read a filename, with default directory `org-directory'."
8673 (let ((default (or org-default-notes-file remember-data-file)))
8674 (read-file-name (format "File name [%s]: " default)
8675 (file-name-as-directory org-directory)
8676 default)))
8678 (defun org-notes-order-reversed-p ()
8679 "Check if the current file should receive notes in reversed order."
8680 (cond
8681 ((not org-reverse-note-order) nil)
8682 ((eq t org-reverse-note-order) t)
8683 ((not (listp org-reverse-note-order)) nil)
8684 (t (catch 'exit
8685 (let ((all org-reverse-note-order)
8686 entry)
8687 (while (setq entry (pop all))
8688 (if (string-match (car entry) buffer-file-name)
8689 (throw 'exit (cdr entry))))
8690 nil)))))
8692 (defvar org-refile-target-table nil
8693 "The list of refile targets, created by `org-refile'.")
8695 (defvar org-agenda-new-buffers nil
8696 "Buffers created to visit agenda files.")
8698 (defun org-get-refile-targets (&optional default-buffer)
8699 "Produce a table with refile targets."
8700 (let ((case-fold-search nil)
8701 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
8702 (entries (or org-refile-targets '((nil . (:level . 1)))))
8703 targets txt re files f desc descre fast-path-p level pos0)
8704 (message "Getting targets...")
8705 (with-current-buffer (or default-buffer (current-buffer))
8706 (while (setq entry (pop entries))
8707 (setq files (car entry) desc (cdr entry))
8708 (setq fast-path-p nil)
8709 (cond
8710 ((null files) (setq files (list (current-buffer))))
8711 ((eq files 'org-agenda-files)
8712 (setq files (org-agenda-files 'unrestricted)))
8713 ((and (symbolp files) (fboundp files))
8714 (setq files (funcall files)))
8715 ((and (symbolp files) (boundp files))
8716 (setq files (symbol-value files))))
8717 (if (stringp files) (setq files (list files)))
8718 (cond
8719 ((eq (car desc) :tag)
8720 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
8721 ((eq (car desc) :todo)
8722 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
8723 ((eq (car desc) :regexp)
8724 (setq descre (cdr desc)))
8725 ((eq (car desc) :level)
8726 (setq descre (concat "^\\*\\{" (number-to-string
8727 (if org-odd-levels-only
8728 (1- (* 2 (cdr desc)))
8729 (cdr desc)))
8730 "\\}[ \t]")))
8731 ((eq (car desc) :maxlevel)
8732 (setq fast-path-p t)
8733 (setq descre (concat "^\\*\\{1," (number-to-string
8734 (if org-odd-levels-only
8735 (1- (* 2 (cdr desc)))
8736 (cdr desc)))
8737 "\\}[ \t]")))
8738 (t (error "Bad refiling target description %s" desc)))
8739 (while (setq f (pop files))
8740 (save-excursion
8741 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
8742 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
8743 (setq f (expand-file-name f))
8744 (if (eq org-refile-use-outline-path 'file)
8745 (push (list (file-name-nondirectory f) f nil nil) targets))
8746 (save-excursion
8747 (save-restriction
8748 (widen)
8749 (goto-char (point-min))
8750 (while (re-search-forward descre nil t)
8751 (goto-char (setq pos0 (point-at-bol)))
8752 (catch 'next
8753 (when org-refile-target-verify-function
8754 (save-match-data
8755 (or (funcall org-refile-target-verify-function)
8756 (throw 'next t))))
8757 (when (looking-at org-complex-heading-regexp)
8758 (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
8759 txt (org-link-display-format (match-string 4))
8760 re (concat "^" (regexp-quote
8761 (buffer-substring (match-beginning 1)
8762 (match-end 4)))))
8763 (if (match-end 5) (setq re (concat re "[ \t]+"
8764 (regexp-quote
8765 (match-string 5)))))
8766 (setq re (concat re "[ \t]*$"))
8767 (when org-refile-use-outline-path
8768 (setq txt (mapconcat 'org-protect-slash
8769 (append
8770 (if (eq org-refile-use-outline-path 'file)
8771 (list (file-name-nondirectory
8772 (buffer-file-name (buffer-base-buffer))))
8773 (if (eq org-refile-use-outline-path 'full-file-path)
8774 (list (buffer-file-name (buffer-base-buffer)))))
8775 (org-get-outline-path fast-path-p level txt)
8776 (list txt))
8777 "/")))
8778 (push (list txt f re (point)) targets)))
8779 (when (= (point) pos0)
8780 ;; verification function has not moved point
8781 (goto-char (point-at-eol))))))))))
8782 (message "Getting targets...done")
8783 (nreverse targets)))
8785 (defun org-protect-slash (s)
8786 (while (string-match "/" s)
8787 (setq s (replace-match "\\" t t s)))
8790 (defvar org-olpa (make-vector 20 nil))
8792 (defun org-get-outline-path (&optional fastp level heading)
8793 "Return the outline path to the current entry, as a list."
8794 (if fastp
8795 (progn
8796 (if (> level 19)
8797 (error "Outline path failure, more than 19 levels."))
8798 (loop for i from level upto 19 do
8799 (aset org-olpa i nil))
8800 (prog1
8801 (delq nil (append org-olpa nil))
8802 (aset org-olpa level heading)))
8803 (let (rtn)
8804 (save-excursion
8805 (while (org-up-heading-safe)
8806 (when (looking-at org-complex-heading-regexp)
8807 (push (org-match-string-no-properties 4) rtn)))
8808 rtn))))
8810 (defvar org-refile-history nil
8811 "History for refiling operations.")
8813 (defvar org-after-refile-insert-hook nil
8814 "Hook run after `org-refile' has inserted its stuff at the new location.
8815 Note that this is still *before* the stuff will be removed from
8816 the *old* location.")
8818 (defun org-refile (&optional goto default-buffer rfloc)
8819 "Move the entry at point to another heading.
8820 The list of target headings is compiled using the information in
8821 `org-refile-targets', which see. This list is created before each use
8822 and will therefore always be up-to-date.
8824 At the target location, the entry is filed as a subitem of the target heading.
8825 Depending on `org-reverse-note-order', the new subitem will either be the
8826 first or the last subitem.
8828 If there is an active region, all entries in that region will be moved.
8829 However, the region must fulfil the requirement that the first heading
8830 is the first one sets the top-level of the moved text - at most siblings
8831 below it are allowed.
8833 With prefix arg GOTO, the command will only visit the target location,
8834 not actually move anything.
8835 With a double prefix `C-u C-u', go to the location where the last refiling
8836 operation has put the subtree.
8838 RFLOC can be a refile location obtained in a different way.
8840 See also `org-refile-use-outline-path' and `org-completion-use-ido'"
8841 (interactive "P")
8842 (let* ((cbuf (current-buffer))
8843 (regionp (org-region-active-p))
8844 (region-start (and regionp (region-beginning)))
8845 (region-end (and regionp (region-end)))
8846 (region-length (and regionp (- region-end region-start)))
8847 (filename (buffer-file-name (buffer-base-buffer cbuf)))
8848 pos it nbuf file re level reversed)
8849 (when regionp
8850 (goto-char region-start)
8851 (or (bolp) (goto-char (point-at-bol)))
8852 (setq region-start (point))
8853 (unless (org-kill-is-subtree-p
8854 (buffer-substring region-start region-end))
8855 (error "The region is not a (sequence of) subtree(s)")))
8856 (if (equal goto '(16))
8857 (org-refile-goto-last-stored)
8858 (when (setq it (or rfloc
8859 (save-excursion
8860 (org-refile-get-location
8861 (if goto "Goto: " "Refile to: ") default-buffer
8862 org-refile-allow-creating-parent-nodes))))
8863 (setq file (nth 1 it)
8864 re (nth 2 it)
8865 pos (nth 3 it))
8866 (if (and (not goto)
8868 (equal (buffer-file-name) file)
8869 (if regionp
8870 (and (>= pos region-start)
8871 (<= pos region-end))
8872 (and (>= pos (point))
8873 (< pos (save-excursion
8874 (org-end-of-subtree t t))))))
8875 (error "Cannot refile to position inside the tree or region"))
8877 (setq nbuf (or (find-buffer-visiting file)
8878 (find-file-noselect file)))
8879 (if goto
8880 (progn
8881 (switch-to-buffer nbuf)
8882 (goto-char pos)
8883 (org-show-context 'org-goto))
8884 (if regionp
8885 (progn
8886 (org-kill-new (buffer-substring region-start region-end))
8887 (org-save-markers-in-region region-start region-end))
8888 (org-copy-subtree 1 nil t))
8889 (save-excursion
8890 (set-buffer (setq nbuf (or (find-buffer-visiting file)
8891 (find-file-noselect file))))
8892 (setq reversed (org-notes-order-reversed-p))
8893 (save-excursion
8894 (save-restriction
8895 (widen)
8896 (if pos
8897 (progn
8898 (goto-char pos)
8899 (looking-at outline-regexp)
8900 (setq level (org-get-valid-level (funcall outline-level) 1))
8901 (goto-char
8902 (if reversed
8903 (or (outline-next-heading) (point-max))
8904 (or (save-excursion (org-get-next-sibling))
8905 (org-end-of-subtree t t)
8906 (point-max)))))
8907 (setq level 1)
8908 (if (not reversed)
8909 (goto-char (point-max))
8910 (goto-char (point-min))
8911 (or (outline-next-heading) (goto-char (point-max)))))
8912 (if (not (bolp)) (newline))
8913 (bookmark-set "org-refile-last-stored")
8914 (org-paste-subtree level)
8915 (if (fboundp 'deactivate-mark) (deactivate-mark))
8916 (run-hooks 'org-after-refile-insert-hook))))
8917 (if regionp
8918 (delete-region (point) (+ (point) region-length))
8919 (org-cut-subtree))
8920 (when (featurep 'org-inlinetask)
8921 (org-inlinetask-remove-END-maybe))
8922 (setq org-markers-to-move nil)
8923 (message "Refiled to \"%s\"" (car it))))))
8924 (org-reveal))
8926 (defun org-refile-goto-last-stored ()
8927 "Go to the location where the last refile was stored."
8928 (interactive)
8929 (bookmark-jump "org-refile-last-stored")
8930 (message "This is the location of the last refile"))
8932 (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
8933 "Prompt the user for a refile location, using PROMPT."
8934 (let ((org-refile-targets org-refile-targets)
8935 (org-refile-use-outline-path org-refile-use-outline-path))
8936 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
8937 (unless org-refile-target-table
8938 (error "No refile targets"))
8939 (let* ((cbuf (current-buffer))
8940 (partial-completion-mode nil)
8941 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
8942 (cfunc (if (and org-refile-use-outline-path
8943 org-outline-path-complete-in-steps)
8944 'org-olpath-completing-read
8945 'org-icompleting-read))
8946 (extra (if org-refile-use-outline-path "/" ""))
8947 (filename (and cfn (expand-file-name cfn)))
8948 (tbl (mapcar
8949 (lambda (x)
8950 (if (and (not (member org-refile-use-outline-path
8951 '(file full-file-path)))
8952 (not (equal filename (nth 1 x))))
8953 (cons (concat (car x) extra " ("
8954 (file-name-nondirectory (nth 1 x)) ")")
8955 (cdr x))
8956 (cons (concat (car x) extra) (cdr x))))
8957 org-refile-target-table))
8958 (completion-ignore-case t)
8959 pa answ parent-target child parent old-hist)
8960 (setq old-hist org-refile-history)
8961 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
8962 nil 'org-refile-history))
8963 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
8964 (if pa
8965 (progn
8966 (when (or (not org-refile-history)
8967 (not (eq old-hist org-refile-history))
8968 (not (equal (car pa) (car org-refile-history))))
8969 (setq org-refile-history
8970 (cons (car pa) (if (assoc (car org-refile-history) tbl)
8971 org-refile-history
8972 (cdr org-refile-history))))
8973 (if (equal (car org-refile-history) (nth 1 org-refile-history))
8974 (pop org-refile-history)))
8976 (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
8977 (setq parent (match-string 1 answ)
8978 child (match-string 2 answ))
8979 (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
8980 (when (and parent-target
8981 (or (eq new-nodes t)
8982 (and (eq new-nodes 'confirm)
8983 (y-or-n-p (format "Create new node \"%s\"? " child)))))
8984 (org-refile-new-child parent-target child))))))
8986 (defun org-refile-new-child (parent-target child)
8987 "Use refile target PARENT-TARGET to add new CHILD below it."
8988 (unless parent-target
8989 (error "Cannot find parent for new node"))
8990 (let ((file (nth 1 parent-target))
8991 (pos (nth 3 parent-target))
8992 level)
8993 (with-current-buffer (or (find-buffer-visiting file)
8994 (find-file-noselect file))
8995 (save-excursion
8996 (save-restriction
8997 (widen)
8998 (if pos
8999 (goto-char pos)
9000 (goto-char (point-max))
9001 (if (not (bolp)) (newline)))
9002 (when (looking-at outline-regexp)
9003 (setq level (funcall outline-level))
9004 (org-end-of-subtree t t))
9005 (org-back-over-empty-lines)
9006 (insert "\n" (make-string
9007 (if pos (org-get-valid-level level 1) 1) ?*)
9008 " " child "\n")
9009 (beginning-of-line 0)
9010 (list (concat (car parent-target) "/" child) file "" (point)))))))
9012 (defun org-olpath-completing-read (prompt collection &rest args)
9013 "Read an outline path like a file name."
9014 (let ((thetable collection)
9015 (org-completion-use-ido nil) ; does not work with ido.
9016 (org-completion-use-iswitchb nil)) ; or iswitchb
9017 (apply
9018 'org-icompleting-read prompt
9019 (lambda (string predicate &optional flag)
9020 (let (rtn r f (l (length string)))
9021 (cond
9022 ((eq flag nil)
9023 ;; try completion
9024 (try-completion string thetable))
9025 ((eq flag t)
9026 ;; all-completions
9027 (setq rtn (all-completions string thetable predicate))
9028 (mapcar
9029 (lambda (x)
9030 (setq r (substring x l))
9031 (if (string-match " ([^)]*)$" x)
9032 (setq f (match-string 0 x))
9033 (setq f ""))
9034 (if (string-match "/" r)
9035 (concat string (substring r 0 (match-end 0)) f)
9037 rtn))
9038 ((eq flag 'lambda)
9039 ;; exact match?
9040 (assoc string thetable)))
9042 args)))
9044 ;;;; Dynamic blocks
9046 (defun org-find-dblock (name)
9047 "Find the first dynamic block with name NAME in the buffer.
9048 If not found, stay at current position and return nil."
9049 (let (pos)
9050 (save-excursion
9051 (goto-char (point-min))
9052 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
9053 nil t)
9054 (match-beginning 0))))
9055 (if pos (goto-char pos))
9056 pos))
9058 (defconst org-dblock-start-re
9059 "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
9060 "Matches the startline of a dynamic block, with parameters.")
9062 (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
9063 "Matches the end of a dynamic block.")
9065 (defun org-create-dblock (plist)
9066 "Create a dynamic block section, with parameters taken from PLIST.
9067 PLIST must contain a :name entry which is used as name of the block."
9068 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
9069 (end-of-line 1)
9070 (newline))
9071 (let ((col (current-column))
9072 (name (plist-get plist :name)))
9073 (insert "#+BEGIN: " name)
9074 (while plist
9075 (if (eq (car plist) :name)
9076 (setq plist (cddr plist))
9077 (insert " " (prin1-to-string (pop plist)))))
9078 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
9079 (beginning-of-line -2)))
9081 (defun org-prepare-dblock ()
9082 "Prepare dynamic block for refresh.
9083 This empties the block, puts the cursor at the insert position and returns
9084 the property list including an extra property :name with the block name."
9085 (unless (looking-at org-dblock-start-re)
9086 (error "Not at a dynamic block"))
9087 (let* ((begdel (1+ (match-end 0)))
9088 (name (org-no-properties (match-string 1)))
9089 (params (append (list :name name)
9090 (read (concat "(" (match-string 3) ")")))))
9091 (save-excursion
9092 (beginning-of-line 1)
9093 (skip-chars-forward " \t")
9094 (setq params (plist-put params :indentation-column (current-column))))
9095 (unless (re-search-forward org-dblock-end-re nil t)
9096 (error "Dynamic block not terminated"))
9097 (setq params
9098 (append params
9099 (list :content (buffer-substring
9100 begdel (match-beginning 0)))))
9101 (delete-region begdel (match-beginning 0))
9102 (goto-char begdel)
9103 (open-line 1)
9104 params))
9106 (defun org-map-dblocks (&optional command)
9107 "Apply COMMAND to all dynamic blocks in the current buffer.
9108 If COMMAND is not given, use `org-update-dblock'."
9109 (let ((cmd (or command 'org-update-dblock))
9110 pos)
9111 (save-excursion
9112 (goto-char (point-min))
9113 (while (re-search-forward org-dblock-start-re nil t)
9114 (goto-char (setq pos (match-beginning 0)))
9115 (condition-case nil
9116 (funcall cmd)
9117 (error (message "Error during update of dynamic block")))
9118 (goto-char pos)
9119 (unless (re-search-forward org-dblock-end-re nil t)
9120 (error "Dynamic block not terminated"))))))
9122 (defun org-dblock-update (&optional arg)
9123 "User command for updating dynamic blocks.
9124 Update the dynamic block at point. With prefix ARG, update all dynamic
9125 blocks in the buffer."
9126 (interactive "P")
9127 (if arg
9128 (org-update-all-dblocks)
9129 (or (looking-at org-dblock-start-re)
9130 (org-beginning-of-dblock))
9131 (org-update-dblock)))
9133 (defun org-update-dblock ()
9134 "Update the dynamic block at point
9135 This means to empty the block, parse for parameters and then call
9136 the correct writing function."
9137 (save-window-excursion
9138 (let* ((pos (point))
9139 (line (org-current-line))
9140 (params (org-prepare-dblock))
9141 (name (plist-get params :name))
9142 (indent (plist-get params :indentation-column))
9143 (cmd (intern (concat "org-dblock-write:" name))))
9144 (message "Updating dynamic block `%s' at line %d..." name line)
9145 (funcall cmd params)
9146 (message "Updating dynamic block `%s' at line %d...done" name line)
9147 (goto-char pos)
9148 (when (and indent (> indent 0))
9149 (setq indent (make-string indent ?\ ))
9150 (save-excursion
9151 (org-beginning-of-dblock)
9152 (forward-line 1)
9153 (while (not (looking-at org-dblock-end-re))
9154 (insert indent)
9155 (beginning-of-line 2))
9156 (when (looking-at org-dblock-end-re)
9157 (and (looking-at "[ \t]+")
9158 (replace-match ""))
9159 (insert indent)))))))
9161 (defun org-beginning-of-dblock ()
9162 "Find the beginning of the dynamic block at point.
9163 Error if there is no such block at point."
9164 (let ((pos (point))
9165 beg)
9166 (end-of-line 1)
9167 (if (and (re-search-backward org-dblock-start-re nil t)
9168 (setq beg (match-beginning 0))
9169 (re-search-forward org-dblock-end-re nil t)
9170 (> (match-end 0) pos))
9171 (goto-char beg)
9172 (goto-char pos)
9173 (error "Not in a dynamic block"))))
9175 (defun org-update-all-dblocks ()
9176 "Update all dynamic blocks in the buffer.
9177 This function can be used in a hook."
9178 (when (org-mode-p)
9179 (org-map-dblocks 'org-update-dblock)))
9182 ;;;; Completion
9184 (defconst org-additional-option-like-keywords
9185 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
9186 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
9187 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "LATEX_CLASS:" "ATTR_LaTeX"
9188 "BEGIN:" "END:"
9189 "ORGTBL" "TBLFM:" "TBLNAME:"
9190 "BEGIN_EXAMPLE" "END_EXAMPLE"
9191 "BEGIN_QUOTE" "END_QUOTE"
9192 "BEGIN_VERSE" "END_VERSE"
9193 "BEGIN_CENTER" "END_CENTER"
9194 "BEGIN_SRC" "END_SRC"
9195 "CATEGORY" "COLUMNS"
9196 "CAPTION" "LABEL"
9197 "SETUPFILE"
9198 "BIND"
9199 "MACRO"))
9201 (defcustom org-structure-template-alist
9203 ("s" "#+begin_src ?\n\n#+end_src"
9204 "<src lang=\"?\">\n\n</src>")
9205 ("e" "#+begin_example\n?\n#+end_example"
9206 "<example>\n?\n</example>")
9207 ("q" "#+begin_quote\n?\n#+end_quote"
9208 "<quote>\n?\n</quote>")
9209 ("v" "#+begin_verse\n?\n#+end_verse"
9210 "<verse>\n?\n/verse>")
9211 ("c" "#+begin_center\n?\n#+end_center"
9212 "<center>\n?\n/center>")
9213 ("l" "#+begin_latex\n?\n#+end_latex"
9214 "<literal style=\"latex\">\n?\n</literal>")
9215 ("L" "#+latex: "
9216 "<literal style=\"latex\">?</literal>")
9217 ("h" "#+begin_html\n?\n#+end_html"
9218 "<literal style=\"html\">\n?\n</literal>")
9219 ("H" "#+html: "
9220 "<literal style=\"html\">?</literal>")
9221 ("a" "#+begin_ascii\n?\n#+end_ascii")
9222 ("A" "#+ascii: ")
9223 ("i" "#+include %file ?"
9224 "<include file=%file markup=\"?\">")
9226 "Structure completion elements.
9227 This is a list of abbreviation keys and values. The value gets inserted
9228 it you type @samp{.} followed by the key and then the completion key,
9229 usually `M-TAB'. %file will be replaced by a file name after prompting
9230 for the file using completion.
9231 There are two templates for each key, the first uses the original Org syntax,
9232 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
9233 the default when the /org-mtags.el/ module has been loaded. See also the
9234 variable `org-mtags-prefer-muse-templates'.
9235 This is an experimental feature, it is undecided if it is going to stay in."
9236 :group 'org-completion
9237 :type '(repeat
9238 (string :tag "Key")
9239 (string :tag "Template")
9240 (string :tag "Muse Template")))
9242 (defun org-try-structure-completion ()
9243 "Try to complete a structure template before point.
9244 This looks for strings like \"<e\" on an otherwise empty line and
9245 expands them."
9246 (let ((l (buffer-substring (point-at-bol) (point)))
9248 (when (and (looking-at "[ \t]*$")
9249 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
9250 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
9251 (org-complete-expand-structure-template (+ -1 (point-at-bol)
9252 (match-beginning 1)) a)
9253 t)))
9255 (defun org-complete-expand-structure-template (start cell)
9256 "Expand a structure template."
9257 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
9258 (rpl (nth (if musep 2 1) cell))
9259 (ind ""))
9260 (delete-region start (point))
9261 (when (string-match "\\`#\\+" rpl)
9262 (cond
9263 ((bolp))
9264 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
9265 (setq ind (buffer-substring (point-at-bol) (point))))
9266 (t (newline))))
9267 (setq start (point))
9268 (if (string-match "%file" rpl)
9269 (setq rpl (replace-match
9270 (concat
9271 "\""
9272 (save-match-data
9273 (abbreviate-file-name (read-file-name "Include file: ")))
9274 "\"")
9275 t t rpl)))
9276 (setq rpl (mapconcat 'identity (split-string rpl "\n")
9277 (concat "\n" ind)))
9278 (insert rpl)
9279 (if (re-search-backward "\\?" start t) (delete-char 1))))
9282 (defun org-complete (&optional arg)
9283 "Perform completion on word at point.
9284 At the beginning of a headline, this completes TODO keywords as given in
9285 `org-todo-keywords'.
9286 If the current word is preceded by a backslash, completes the TeX symbols
9287 that are supported for HTML support.
9288 If the current word is preceded by \"#+\", completes special words for
9289 setting file options.
9290 In the line after \"#+STARTUP:, complete valid keywords.\"
9291 At all other locations, this simply calls the value of
9292 `org-completion-fallback-command'."
9293 (interactive "P")
9294 (org-without-partial-completion
9295 (catch 'exit
9296 (let* ((a nil)
9297 (end (point))
9298 (beg1 (save-excursion
9299 (skip-chars-backward (org-re "[:alnum:]_@"))
9300 (point)))
9301 (beg (save-excursion
9302 (skip-chars-backward "a-zA-Z0-9_:$")
9303 (point)))
9304 (confirm (lambda (x) (stringp (car x))))
9305 (searchhead (equal (char-before beg) ?*))
9306 (struct
9307 (when (and (member (char-before beg1) '(?. ?<))
9308 (setq a (assoc (buffer-substring beg1 (point))
9309 org-structure-template-alist)))
9310 (org-complete-expand-structure-template (1- beg1) a)
9311 (throw 'exit t)))
9312 (tag (and (equal (char-before beg1) ?:)
9313 (equal (char-after (point-at-bol)) ?*)))
9314 (prop (and (equal (char-before beg1) ?:)
9315 (not (equal (char-after (point-at-bol)) ?*))))
9316 (texp (equal (char-before beg) ?\\))
9317 (link (equal (char-before beg) ?\[))
9318 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
9319 beg)
9320 "#+"))
9321 (startup (string-match "^#\\+STARTUP:.*"
9322 (buffer-substring (point-at-bol) (point))))
9323 (completion-ignore-case opt)
9324 (type nil)
9325 (tbl nil)
9326 (table (cond
9327 (opt
9328 (setq type :opt)
9329 (require 'org-exp)
9330 (append
9331 (delq nil
9332 (mapcar
9333 (lambda (x)
9334 (if (string-match
9335 "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
9336 (cons (match-string 2 x)
9337 (match-string 1 x))))
9338 (org-split-string (org-get-current-options) "\n")))
9339 (mapcar 'list org-additional-option-like-keywords)))
9340 (startup
9341 (setq type :startup)
9342 org-startup-options)
9343 (link (append org-link-abbrev-alist-local
9344 org-link-abbrev-alist))
9345 (texp
9346 (setq type :tex)
9347 org-html-entities)
9348 ((string-match "\\`\\*+[ \t]+\\'"
9349 (buffer-substring (point-at-bol) beg))
9350 (setq type :todo)
9351 (mapcar 'list org-todo-keywords-1))
9352 (searchhead
9353 (setq type :searchhead)
9354 (save-excursion
9355 (goto-char (point-min))
9356 (while (re-search-forward org-todo-line-regexp nil t)
9357 (push (list
9358 (org-make-org-heading-search-string
9359 (match-string 3) t))
9360 tbl)))
9361 tbl)
9362 (tag (setq type :tag beg beg1)
9363 (or org-tag-alist (org-get-buffer-tags)))
9364 (prop (setq type :prop beg beg1)
9365 (mapcar 'list (org-buffer-property-keys nil t t)))
9366 (t (progn
9367 (call-interactively org-completion-fallback-command)
9368 (throw 'exit nil)))))
9369 (pattern (buffer-substring-no-properties beg end))
9370 (completion (try-completion pattern table confirm)))
9371 (cond ((eq completion t)
9372 (if (not (assoc (upcase pattern) table))
9373 (message "Already complete")
9374 (if (and (equal type :opt)
9375 (not (member (car (assoc (upcase pattern) table))
9376 org-additional-option-like-keywords)))
9377 (insert (substring (cdr (assoc (upcase pattern) table))
9378 (length pattern)))
9379 (if (memq type '(:tag :prop)) (insert ":")))))
9380 ((null completion)
9381 (message "Can't find completion for \"%s\"" pattern)
9382 (ding))
9383 ((not (string= pattern completion))
9384 (delete-region beg end)
9385 (if (string-match " +$" completion)
9386 (setq completion (replace-match "" t t completion)))
9387 (insert completion)
9388 (if (get-buffer-window "*Completions*")
9389 (delete-window (get-buffer-window "*Completions*")))
9390 (if (assoc completion table)
9391 (if (eq type :todo) (insert " ")
9392 (if (memq type '(:tag :prop)) (insert ":"))))
9393 (if (and (equal type :opt) (assoc completion table))
9394 (message "%s" (substitute-command-keys
9395 "Press \\[org-complete] again to insert example settings"))))
9397 (message "Making completion list...")
9398 (let ((list (sort (all-completions pattern table confirm)
9399 'string<)))
9400 (with-output-to-temp-buffer "*Completions*"
9401 (condition-case nil
9402 ;; Protection needed for XEmacs and emacs 21
9403 (display-completion-list list pattern)
9404 (error (display-completion-list list)))))
9405 (message "Making completion list...%s" "done")))))))
9407 ;;;; TODO, DEADLINE, Comments
9409 (defun org-toggle-comment ()
9410 "Change the COMMENT state of an entry."
9411 (interactive)
9412 (save-excursion
9413 (org-back-to-heading)
9414 (let (case-fold-search)
9415 (if (looking-at (concat outline-regexp
9416 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
9417 (replace-match "" t t nil 1)
9418 (if (looking-at outline-regexp)
9419 (progn
9420 (goto-char (match-end 0))
9421 (insert org-comment-string " ")))))))
9423 (defvar org-last-todo-state-is-todo nil
9424 "This is non-nil when the last TODO state change led to a TODO state.
9425 If the last change removed the TODO tag or switched to DONE, then
9426 this is nil.")
9428 (defvar org-setting-tags nil) ; dynamically skipped
9430 (defun org-parse-local-options (string var)
9431 "Parse STRING for startup setting relevant for variable VAR."
9432 (let ((rtn (symbol-value var))
9433 e opts)
9434 (save-match-data
9435 (if (or (not string) (not (string-match "\\S-" string)))
9437 (setq opts (delq nil (mapcar (lambda (x)
9438 (setq e (assoc x org-startup-options))
9439 (if (eq (nth 1 e) var) e nil))
9440 (org-split-string string "[ \t]+"))))
9441 (if (not opts)
9443 (setq rtn nil)
9444 (while (setq e (pop opts))
9445 (if (not (nth 3 e))
9446 (setq rtn (nth 2 e))
9447 (if (not (listp rtn)) (setq rtn nil))
9448 (push (nth 2 e) rtn)))
9449 rtn)))))
9451 (defvar org-todo-setup-filter-hook nil
9452 "Hook for functions that pre-filter todo specs.
9454 Each function takes a todo spec and returns either `nil' or the spec
9455 transformed into canonical form." )
9457 (defvar org-todo-get-default-hook nil
9458 "Hook for functions that get a default item for todo.
9460 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
9461 `nil' or a string to be used for the todo mark." )
9463 (defvar org-agenda-headline-snapshot-before-repeat)
9465 (defun org-todo (&optional arg)
9466 "Change the TODO state of an item.
9467 The state of an item is given by a keyword at the start of the heading,
9468 like
9469 *** TODO Write paper
9470 *** DONE Call mom
9472 The different keywords are specified in the variable `org-todo-keywords'.
9473 By default the available states are \"TODO\" and \"DONE\".
9474 So for this example: when the item starts with TODO, it is changed to DONE.
9475 When it starts with DONE, the DONE is removed. And when neither TODO nor
9476 DONE are present, add TODO at the beginning of the heading.
9478 With C-u prefix arg, use completion to determine the new state.
9479 With numeric prefix arg, switch to that state.
9480 With a double C-u prefix, switch to the next set of TODO keywords (nextset).
9481 With a tripple C-u prefix, circumvent any state blocking.
9483 For calling through lisp, arg is also interpreted in the following way:
9484 'none -> empty state
9485 \"\"(empty string) -> switch to empty state
9486 'done -> switch to DONE
9487 'nextset -> switch to the next set of keywords
9488 'previousset -> switch to the previous set of keywords
9489 \"WAITING\" -> switch to the specified keyword, but only if it
9490 really is a member of `org-todo-keywords'."
9491 (interactive "P")
9492 (if (equal arg '(16)) (setq arg 'nextset))
9493 (let ((org-blocker-hook org-blocker-hook)
9494 (case-fold-search nil))
9495 (when (equal arg '(64))
9496 (setq arg nil org-blocker-hook nil))
9497 (when (and org-blocker-hook
9498 (or org-inhibit-blocking
9499 (org-entry-get nil "NOBLOCKING")))
9500 (setq org-blocker-hook nil))
9501 (save-excursion
9502 (catch 'exit
9503 (org-back-to-heading t)
9504 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
9505 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
9506 (looking-at " *"))
9507 (let* ((match-data (match-data))
9508 (startpos (point-at-bol))
9509 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
9510 (org-log-done org-log-done)
9511 (org-log-repeat org-log-repeat)
9512 (org-todo-log-states org-todo-log-states)
9513 (this (match-string 1))
9514 (hl-pos (match-beginning 0))
9515 (head (org-get-todo-sequence-head this))
9516 (ass (assoc head org-todo-kwd-alist))
9517 (interpret (nth 1 ass))
9518 (done-word (nth 3 ass))
9519 (final-done-word (nth 4 ass))
9520 (last-state (or this ""))
9521 (completion-ignore-case t)
9522 (member (member this org-todo-keywords-1))
9523 (tail (cdr member))
9524 (state (cond
9525 ((and org-todo-key-trigger
9526 (or (and (equal arg '(4))
9527 (eq org-use-fast-todo-selection 'prefix))
9528 (and (not arg) org-use-fast-todo-selection
9529 (not (eq org-use-fast-todo-selection
9530 'prefix)))))
9531 ;; Use fast selection
9532 (org-fast-todo-selection))
9533 ((and (equal arg '(4))
9534 (or (not org-use-fast-todo-selection)
9535 (not org-todo-key-trigger)))
9536 ;; Read a state with completion
9537 (org-icompleting-read
9538 "State: " (mapcar (lambda(x) (list x))
9539 org-todo-keywords-1)
9540 nil t))
9541 ((eq arg 'right)
9542 (if this
9543 (if tail (car tail) nil)
9544 (car org-todo-keywords-1)))
9545 ((eq arg 'left)
9546 (if (equal member org-todo-keywords-1)
9548 (if this
9549 (nth (- (length org-todo-keywords-1)
9550 (length tail) 2)
9551 org-todo-keywords-1)
9552 (org-last org-todo-keywords-1))))
9553 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
9554 (setq arg nil))) ; hack to fall back to cycling
9555 (arg
9556 ;; user or caller requests a specific state
9557 (cond
9558 ((equal arg "") nil)
9559 ((eq arg 'none) nil)
9560 ((eq arg 'done) (or done-word (car org-done-keywords)))
9561 ((eq arg 'nextset)
9562 (or (car (cdr (member head org-todo-heads)))
9563 (car org-todo-heads)))
9564 ((eq arg 'previousset)
9565 (let ((org-todo-heads (reverse org-todo-heads)))
9566 (or (car (cdr (member head org-todo-heads)))
9567 (car org-todo-heads))))
9568 ((car (member arg org-todo-keywords-1)))
9569 ((stringp arg)
9570 (error "State `%s' not valid in this file" arg))
9571 ((nth (1- (prefix-numeric-value arg))
9572 org-todo-keywords-1))))
9573 ((null member) (or head (car org-todo-keywords-1)))
9574 ((equal this final-done-word) nil) ;; -> make empty
9575 ((null tail) nil) ;; -> first entry
9576 ((memq interpret '(type priority))
9577 (if (eq this-command last-command)
9578 (car tail)
9579 (if (> (length tail) 0)
9580 (or done-word (car org-done-keywords))
9581 nil)))
9583 (car tail))))
9584 (state (or
9585 (run-hook-with-args-until-success
9586 'org-todo-get-default-hook state last-state)
9587 state))
9588 (next (if state (concat " " state " ") " "))
9589 (change-plist (list :type 'todo-state-change :from this :to state
9590 :position startpos))
9591 dolog now-done-p)
9592 (when org-blocker-hook
9593 (setq org-last-todo-state-is-todo
9594 (not (member this org-done-keywords)))
9595 (unless (save-excursion
9596 (save-match-data
9597 (run-hook-with-args-until-failure
9598 'org-blocker-hook change-plist)))
9599 (if (interactive-p)
9600 (error "TODO state change from %s to %s blocked" this state)
9601 ;; fail silently
9602 (message "TODO state change from %s to %s blocked" this state)
9603 (throw 'exit nil))))
9604 (store-match-data match-data)
9605 (replace-match next t t)
9606 (unless (pos-visible-in-window-p hl-pos)
9607 (message "TODO state changed to %s" (org-trim next)))
9608 (unless head
9609 (setq head (org-get-todo-sequence-head state)
9610 ass (assoc head org-todo-kwd-alist)
9611 interpret (nth 1 ass)
9612 done-word (nth 3 ass)
9613 final-done-word (nth 4 ass)))
9614 (when (memq arg '(nextset previousset))
9615 (message "Keyword-Set %d/%d: %s"
9616 (- (length org-todo-sets) -1
9617 (length (memq (assoc state org-todo-sets) org-todo-sets)))
9618 (length org-todo-sets)
9619 (mapconcat 'identity (assoc state org-todo-sets) " ")))
9620 (setq org-last-todo-state-is-todo
9621 (not (member state org-done-keywords)))
9622 (setq now-done-p (and (member state org-done-keywords)
9623 (not (member this org-done-keywords))))
9624 (and logging (org-local-logging logging))
9625 (when (and (or org-todo-log-states org-log-done)
9626 (not (eq org-inhibit-logging t))
9627 (not (memq arg '(nextset previousset))))
9628 ;; we need to look at recording a time and note
9629 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
9630 (nth 2 (assoc this org-todo-log-states))))
9631 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
9632 (setq dolog 'time))
9633 (when (and state
9634 (member state org-not-done-keywords)
9635 (not (member this org-not-done-keywords)))
9636 ;; This is now a todo state and was not one before
9637 ;; If there was a CLOSED time stamp, get rid of it.
9638 (org-add-planning-info nil nil 'closed))
9639 (when (and now-done-p org-log-done)
9640 ;; It is now done, and it was not done before
9641 (org-add-planning-info 'closed (org-current-time))
9642 (if (and (not dolog) (eq 'note org-log-done))
9643 (org-add-log-setup 'done state this 'findpos 'note)))
9644 (when (and state dolog)
9645 ;; This is a non-nil state, and we need to log it
9646 (org-add-log-setup 'state state this 'findpos dolog)))
9647 ;; Fixup tag positioning
9648 (org-todo-trigger-tag-changes state)
9649 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
9650 (when org-provide-todo-statistics
9651 (org-update-parent-todo-statistics))
9652 (run-hooks 'org-after-todo-state-change-hook)
9653 (if (and arg (not (member state org-done-keywords)))
9654 (setq head (org-get-todo-sequence-head state)))
9655 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
9656 ;; Do we need to trigger a repeat?
9657 (when now-done-p
9658 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
9659 ;; This is for the agenda, take a snapshot of the headline.
9660 (save-match-data
9661 (setq org-agenda-headline-snapshot-before-repeat
9662 (org-get-heading))))
9663 (org-auto-repeat-maybe state))
9664 ;; Fixup cursor location if close to the keyword
9665 (if (and (outline-on-heading-p)
9666 (not (bolp))
9667 (save-excursion (beginning-of-line 1)
9668 (looking-at org-todo-line-regexp))
9669 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
9670 (progn
9671 (goto-char (or (match-end 2) (match-end 1)))
9672 (and (looking-at " ") (just-one-space))))
9673 (when org-trigger-hook
9674 (save-excursion
9675 (run-hook-with-args 'org-trigger-hook change-plist))))))))
9677 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
9678 "Block turning an entry into a TODO, using the hierarchy.
9679 This checks whether the current task should be blocked from state
9680 changes. Such blocking occurs when:
9682 1. The task has children which are not all in a completed state.
9684 2. A task has a parent with the property :ORDERED:, and there
9685 are siblings prior to the current task with incomplete
9686 status.
9688 3. The parent of the task is blocked because it has siblings that should
9689 be done first, or is child of a block grandparent TODO entry."
9691 (catch 'dont-block
9692 ;; If this is not a todo state change, or if this entry is already DONE,
9693 ;; do not block
9694 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
9695 (member (plist-get change-plist :from)
9696 (cons 'done org-done-keywords))
9697 (member (plist-get change-plist :to)
9698 (cons 'todo org-not-done-keywords)))
9699 (throw 'dont-block t))
9700 ;; If this task has children, and any are undone, it's blocked
9701 (save-excursion
9702 (org-back-to-heading t)
9703 (let ((this-level (funcall outline-level)))
9704 (outline-next-heading)
9705 (let ((child-level (funcall outline-level)))
9706 (while (and (not (eobp))
9707 (> child-level this-level))
9708 ;; this todo has children, check whether they are all
9709 ;; completed
9710 (if (and (not (org-entry-is-done-p))
9711 (org-entry-is-todo-p))
9712 (throw 'dont-block nil))
9713 (outline-next-heading)
9714 (setq child-level (funcall outline-level))))))
9715 ;; Otherwise, if the task's parent has the :ORDERED: property, and
9716 ;; any previous siblings are undone, it's blocked
9717 (save-excursion
9718 (org-back-to-heading t)
9719 (let* ((pos (point))
9720 (parent-pos (and (org-up-heading-safe) (point))))
9721 (if (not parent-pos) (throw 'dont-block t)) ; no parent
9722 (when (and (org-entry-get (point) "ORDERED")
9723 (forward-line 1)
9724 (re-search-forward org-not-done-heading-regexp pos t))
9725 (throw 'dont-block nil)) ; block, there is an older sibling not done.
9726 ;; Search further up the hierarchy, to see if an anchestor is blocked
9727 (while t
9728 (goto-char parent-pos)
9729 (if (not (looking-at org-not-done-heading-regexp))
9730 (throw 'dont-block t)) ; do not block, parent is not a TODO
9731 (setq pos (point))
9732 (setq parent-pos (and (org-up-heading-safe) (point)))
9733 (if (not parent-pos) (throw 'dont-block t)) ; no parent
9734 (when (and (org-entry-get (point) "ORDERED")
9735 (forward-line 1)
9736 (re-search-forward org-not-done-heading-regexp pos t))
9737 (throw 'dont-block nil))))))) ; block, older sibling not done.
9739 (defcustom org-track-ordered-property-with-tag nil
9740 "Should the ORDERED property also be shown as a tag?
9741 The ORDERED property decides if an entry should require subtasks to be
9742 completed in sequence. Since a property is not very visible, setting
9743 this option means that toggling the ORDERED property with the command
9744 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
9745 not relevant for the behavior, but it makes things more visible.
9747 Note that toggling the tag with tags commands will not change the property
9748 and therefore not influence behavior!
9750 This can be t, meaning the tag ORDERED should be used, It can also be a
9751 string to select a different tag for this task."
9752 :group 'org-todo
9753 :type '(choice
9754 (const :tag "No tracking" nil)
9755 (const :tag "Track with ORDERED tag" t)
9756 (string :tag "Use other tag")))
9758 (defun org-toggle-ordered-property ()
9759 "Toggle the ORDERED property of the current entry.
9760 For better visibility, you can track the value of this property with a tag.
9761 See variable `org-track-ordered-property-with-tag'."
9762 (interactive)
9763 (let* ((t1 org-track-ordered-property-with-tag)
9764 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
9765 (save-excursion
9766 (org-back-to-heading)
9767 (if (org-entry-get nil "ORDERED")
9768 (progn
9769 (org-delete-property "ORDERED")
9770 (and tag (org-toggle-tag tag 'off))
9771 (message "Subtasks can be completed in arbitrary order"))
9772 (org-entry-put nil "ORDERED" "t")
9773 (and tag (org-toggle-tag tag 'on))
9774 (message "Subtasks must be completed in sequence")))))
9776 (defvar org-blocked-by-checkboxes) ; dynamically scoped
9777 (defun org-block-todo-from-checkboxes (change-plist)
9778 "Block turning an entry into a TODO, using checkboxes.
9779 This checks whether the current task should be blocked from state
9780 changes because there are uncheckd boxes in this entry."
9781 (catch 'dont-block
9782 ;; If this is not a todo state change, or if this entry is already DONE,
9783 ;; do not block
9784 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
9785 (member (plist-get change-plist :from)
9786 (cons 'done org-done-keywords))
9787 (member (plist-get change-plist :to)
9788 (cons 'todo org-not-done-keywords)))
9789 (throw 'dont-block t))
9790 ;; If this task has checkboxes that are not checked, it's blocked
9791 (save-excursion
9792 (org-back-to-heading t)
9793 (let ((beg (point)) end)
9794 (outline-next-heading)
9795 (setq end (point))
9796 (goto-char beg)
9797 (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
9798 end t)
9799 (progn
9800 (if (boundp 'org-blocked-by-checkboxes)
9801 (setq org-blocked-by-checkboxes t))
9802 (throw 'dont-block nil)))))
9803 t)) ; do not block
9805 (defun org-update-statistics-cookies (all)
9806 "Update the statistics cookie, either from TODO or from checkboxes.
9807 This should be called with the cursor in a line with a statistics cookie."
9808 (interactive "P")
9809 (if all
9810 (progn
9811 (org-update-checkbox-count 'all)
9812 (org-map-entries 'org-update-parent-todo-statistics))
9813 (if (not (org-on-heading-p))
9814 (org-update-checkbox-count)
9815 (let ((pos (move-marker (make-marker) (point)))
9816 end l1 l2)
9817 (ignore-errors (org-back-to-heading t))
9818 (if (not (org-on-heading-p))
9819 (org-update-checkbox-count)
9820 (setq l1 (org-outline-level))
9821 (setq end (save-excursion
9822 (outline-next-heading)
9823 (if (org-on-heading-p) (setq l2 (org-outline-level)))
9824 (point)))
9825 (if (and (save-excursion (re-search-forward
9826 "^[ \t]*[-+*] \\[[- X]\\]" end t))
9827 (not (save-excursion (re-search-forward
9828 ":COOKIE_DATA:.*\\<todo\\>" end t))))
9829 (org-update-checkbox-count)
9830 (if (and l2 (> l2 l1))
9831 (progn
9832 (goto-char end)
9833 (org-update-parent-todo-statistics))
9834 (error "No data for statistics cookie"))))
9835 (goto-char pos)
9836 (move-marker pos nil)))))
9838 (defvar org-entry-property-inherited-from) ;; defined below
9839 (defun org-update-parent-todo-statistics ()
9840 "Update any statistics cookie in the parent of the current headline.
9841 When `org-hierarchical-todo-statistics' is nil, statistics will cover
9842 the entire subtree and this will travel up the hierarchy and update
9843 statistics everywhere."
9844 (interactive)
9845 (let* ((lim 0) prop
9846 (recursive (or (not org-hierarchical-todo-statistics)
9847 (string-match
9848 "\\<recursive\\>"
9849 (or (setq prop (org-entry-get
9850 nil "COOKIE_DATA" 'inherit)) ""))))
9851 (lim (or (and prop (marker-position
9852 org-entry-property-inherited-from))
9853 lim))
9854 (first t)
9855 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
9856 level ltoggle l1 new ndel
9857 (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
9858 (catch 'exit
9859 (save-excursion
9860 (beginning-of-line 1)
9861 (if (org-at-heading-p)
9862 (setq ltoggle (funcall outline-level))
9863 (error "This should not happen"))
9864 (while (and (setq level (org-up-heading-safe))
9865 (or recursive first)
9866 (>= (point) lim))
9867 (setq first nil)
9868 (unless (and level
9869 (not (string-match
9870 "\\<checkbox\\>"
9871 (downcase
9872 (or (org-entry-get
9873 nil "COOKIE_DATA")
9874 "")))))
9875 (throw 'exit nil))
9876 (while (re-search-forward box-re (point-at-eol) t)
9877 (setq cnt-all 0 cnt-done 0 cookie-present t)
9878 (setq is-percent (match-end 2))
9879 (save-match-data
9880 (unless (outline-next-heading) (throw 'exit nil))
9881 (while (and (looking-at org-complex-heading-regexp)
9882 (> (setq l1 (length (match-string 1))) level))
9883 (setq kwd (and (or recursive (= l1 ltoggle))
9884 (match-string 2)))
9885 (if (or (eq org-provide-todo-statistics 'all-headlines)
9886 (and (listp org-provide-todo-statistics)
9887 (or (member kwd org-provide-todo-statistics)
9888 (member kwd org-done-keywords))))
9889 (setq cnt-all (1+ cnt-all))
9890 (if (eq org-provide-todo-statistics t)
9891 (and kwd (setq cnt-all (1+ cnt-all)))))
9892 (and (member kwd org-done-keywords)
9893 (setq cnt-done (1+ cnt-done)))
9894 (outline-next-heading)))
9895 (setq new
9896 (if is-percent
9897 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
9898 (format "[%d/%d]" cnt-done cnt-all))
9899 ndel (- (match-end 0) (match-beginning 0)))
9900 (goto-char (match-beginning 0))
9901 (insert new)
9902 (delete-region (point) (+ (point) ndel))))
9903 (when cookie-present
9904 (run-hook-with-args 'org-after-todo-statistics-hook
9905 cnt-done (- cnt-all cnt-done)))))
9906 (run-hooks 'org-todo-statistics-hook)))
9908 (defvar org-after-todo-statistics-hook nil
9909 "Hook that is called after a TODO statistics cookie has been updated.
9910 Each function is called with two arguments: the number of not-done entries
9911 and the number of done entries.
9913 For example, the following function, when added to this hook, will switch
9914 an entry to DONE when all children are done, and back to TODO when new
9915 entries are set to a TODO status. Note that this hook is only called
9916 when there is a statistics cookie in the headline!
9918 (defun org-summary-todo (n-done n-not-done)
9919 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
9920 (let (org-log-done org-log-states) ; turn off logging
9921 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
9924 (defvar org-todo-statistics-hook nil
9925 "Hook that is run whenever Org thinks TODO statistics should be updated.
9926 This hook runs even if there is no statisics cookie present, in which case
9927 `org-after-todo-statistics-hook' would not run.")
9929 (defun org-todo-trigger-tag-changes (state)
9930 "Apply the changes defined in `org-todo-state-tags-triggers'."
9931 (let ((l org-todo-state-tags-triggers)
9932 changes)
9933 (when (or (not state) (equal state ""))
9934 (setq changes (append changes (cdr (assoc "" l)))))
9935 (when (and (stringp state) (> (length state) 0))
9936 (setq changes (append changes (cdr (assoc state l)))))
9937 (when (member state org-not-done-keywords)
9938 (setq changes (append changes (cdr (assoc 'todo l)))))
9939 (when (member state org-done-keywords)
9940 (setq changes (append changes (cdr (assoc 'done l)))))
9941 (dolist (c changes)
9942 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
9944 (defun org-local-logging (value)
9945 "Get logging settings from a property VALUE."
9946 (let* (words w a)
9947 ;; directly set the variables, they are already local.
9948 (setq org-log-done nil
9949 org-log-repeat nil
9950 org-todo-log-states nil)
9951 (setq words (org-split-string value))
9952 (while (setq w (pop words))
9953 (cond
9954 ((setq a (assoc w org-startup-options))
9955 (and (member (nth 1 a) '(org-log-done org-log-repeat))
9956 (set (nth 1 a) (nth 2 a))))
9957 ((setq a (org-extract-log-state-settings w))
9958 (and (member (car a) org-todo-keywords-1)
9959 (push a org-todo-log-states)))))))
9961 (defun org-get-todo-sequence-head (kwd)
9962 "Return the head of the TODO sequence to which KWD belongs.
9963 If KWD is not set, check if there is a text property remembering the
9964 right sequence."
9965 (let (p)
9966 (cond
9967 ((not kwd)
9968 (or (get-text-property (point-at-bol) 'org-todo-head)
9969 (progn
9970 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
9971 nil (point-at-eol)))
9972 (get-text-property p 'org-todo-head))))
9973 ((not (member kwd org-todo-keywords-1))
9974 (car org-todo-keywords-1))
9975 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
9977 (defun org-fast-todo-selection ()
9978 "Fast TODO keyword selection with single keys.
9979 Returns the new TODO keyword, or nil if no state change should occur."
9980 (let* ((fulltable org-todo-key-alist)
9981 (done-keywords org-done-keywords) ;; needed for the faces.
9982 (maxlen (apply 'max (mapcar
9983 (lambda (x)
9984 (if (stringp (car x)) (string-width (car x)) 0))
9985 fulltable)))
9986 (expert nil)
9987 (fwidth (+ maxlen 3 1 3))
9988 (ncol (/ (- (window-width) 4) fwidth))
9989 tg cnt e c tbl
9990 groups ingroup)
9991 (save-excursion
9992 (save-window-excursion
9993 (if expert
9994 (set-buffer (get-buffer-create " *Org todo*"))
9995 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
9996 (erase-buffer)
9997 (org-set-local 'org-done-keywords done-keywords)
9998 (setq tbl fulltable cnt 0)
9999 (while (setq e (pop tbl))
10000 (cond
10001 ((equal e '(:startgroup))
10002 (push '() groups) (setq ingroup t)
10003 (when (not (= cnt 0))
10004 (setq cnt 0)
10005 (insert "\n"))
10006 (insert "{ "))
10007 ((equal e '(:endgroup))
10008 (setq ingroup nil cnt 0)
10009 (insert "}\n"))
10010 ((equal e '(:newline))
10011 (when (not (= cnt 0))
10012 (setq cnt 0)
10013 (insert "\n")
10014 (setq e (car tbl))
10015 (while (equal (car tbl) '(:newline))
10016 (insert "\n")
10017 (setq tbl (cdr tbl)))))
10019 (setq tg (car e) c (cdr e))
10020 (if ingroup (push tg (car groups)))
10021 (setq tg (org-add-props tg nil 'face
10022 (org-get-todo-face tg)))
10023 (if (and (= cnt 0) (not ingroup)) (insert " "))
10024 (insert "[" c "] " tg (make-string
10025 (- fwidth 4 (length tg)) ?\ ))
10026 (when (= (setq cnt (1+ cnt)) ncol)
10027 (insert "\n")
10028 (if ingroup (insert " "))
10029 (setq cnt 0)))))
10030 (insert "\n")
10031 (goto-char (point-min))
10032 (if (not expert) (org-fit-window-to-buffer))
10033 (message "[a-z..]:Set [SPC]:clear")
10034 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
10035 (cond
10036 ((or (= c ?\C-g)
10037 (and (= c ?q) (not (rassoc c fulltable))))
10038 (setq quit-flag t))
10039 ((= c ?\ ) nil)
10040 ((setq e (rassoc c fulltable) tg (car e))
10042 (t (setq quit-flag t)))))))
10044 (defun org-entry-is-todo-p ()
10045 (member (org-get-todo-state) org-not-done-keywords))
10047 (defun org-entry-is-done-p ()
10048 (member (org-get-todo-state) org-done-keywords))
10050 (defun org-get-todo-state ()
10051 (save-excursion
10052 (org-back-to-heading t)
10053 (and (looking-at org-todo-line-regexp)
10054 (match-end 2)
10055 (match-string 2))))
10057 (defun org-at-date-range-p (&optional inactive-ok)
10058 "Is the cursor inside a date range?"
10059 (interactive)
10060 (save-excursion
10061 (catch 'exit
10062 (let ((pos (point)))
10063 (skip-chars-backward "^[<\r\n")
10064 (skip-chars-backward "<[")
10065 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
10066 (>= (match-end 0) pos)
10067 (throw 'exit t))
10068 (skip-chars-backward "^<[\r\n")
10069 (skip-chars-backward "<[")
10070 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
10071 (>= (match-end 0) pos)
10072 (throw 'exit t)))
10073 nil)))
10075 (defun org-get-repeat (&optional tagline)
10076 "Check if there is a deadline/schedule with repeater in this entry."
10077 (save-match-data
10078 (save-excursion
10079 (org-back-to-heading t)
10080 (and (re-search-forward (if tagline
10081 (concat tagline "\\s-*" org-repeat-re)
10082 org-repeat-re)
10083 (org-entry-end-position) t)
10084 (match-string-no-properties 1)))))
10086 (defvar org-last-changed-timestamp)
10087 (defvar org-last-inserted-timestamp)
10088 (defvar org-log-post-message)
10089 (defvar org-log-note-purpose)
10090 (defvar org-log-note-how)
10091 (defvar org-log-note-extra)
10092 (defun org-auto-repeat-maybe (done-word)
10093 "Check if the current headline contains a repeated deadline/schedule.
10094 If yes, set TODO state back to what it was and change the base date
10095 of repeating deadline/scheduled time stamps to new date.
10096 This function is run automatically after each state change to a DONE state."
10097 ;; last-state is dynamically scoped into this function
10098 (let* ((repeat (org-get-repeat))
10099 (aa (assoc last-state org-todo-kwd-alist))
10100 (interpret (nth 1 aa))
10101 (head (nth 2 aa))
10102 (whata '(("d" . day) ("m" . month) ("y" . year)))
10103 (msg "Entry repeats: ")
10104 (org-log-done nil)
10105 (org-todo-log-states nil)
10106 (nshiftmax 10) (nshift 0)
10107 re type n what ts time)
10108 (when repeat
10109 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
10110 (org-todo (if (eq interpret 'type) last-state head))
10111 (org-entry-put nil "LAST_REPEAT" (format-time-string
10112 (org-time-stamp-format t t)))
10113 (when org-log-repeat
10114 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
10115 (memq 'org-add-log-note post-command-hook))
10116 ;; OK, we are already setup for some record
10117 (if (eq org-log-repeat 'note)
10118 ;; make sure we take a note, not only a time stamp
10119 (setq org-log-note-how 'note))
10120 ;; Set up for taking a record
10121 (org-add-log-setup 'state (or done-word (car org-done-keywords))
10122 last-state
10123 'findpos org-log-repeat)))
10124 (org-back-to-heading t)
10125 (org-add-planning-info nil nil 'closed)
10126 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
10127 org-deadline-time-regexp "\\)\\|\\("
10128 org-ts-regexp "\\)"))
10129 (while (re-search-forward
10130 re (save-excursion (outline-next-heading) (point)) t)
10131 (setq type (if (match-end 1) org-scheduled-string
10132 (if (match-end 3) org-deadline-string "Plain:"))
10133 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
10134 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
10135 (setq n (string-to-number (match-string 2 ts))
10136 what (match-string 3 ts))
10137 (if (equal what "w") (setq n (* n 7) what "d"))
10138 ;; Preparation, see if we need to modify the start date for the change
10139 (when (match-end 1)
10140 (setq time (save-match-data (org-time-string-to-time ts)))
10141 (cond
10142 ((equal (match-string 1 ts) ".")
10143 ;; Shift starting date to today
10144 (org-timestamp-change
10145 (- (time-to-days (current-time)) (time-to-days time))
10146 'day))
10147 ((equal (match-string 1 ts) "+")
10148 (while (or (= nshift 0)
10149 (<= (time-to-days time) (time-to-days (current-time))))
10150 (when (= (incf nshift) nshiftmax)
10151 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
10152 (error "Abort")))
10153 (org-timestamp-change n (cdr (assoc what whata)))
10154 (org-at-timestamp-p t)
10155 (setq ts (match-string 1))
10156 (setq time (save-match-data (org-time-string-to-time ts))))
10157 (org-timestamp-change (- n) (cdr (assoc what whata)))
10158 ;; rematch, so that we have everything in place for the real shift
10159 (org-at-timestamp-p t)
10160 (setq ts (match-string 1))
10161 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
10162 (org-timestamp-change n (cdr (assoc what whata)))
10163 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
10164 (setq org-log-post-message msg)
10165 (message "%s" msg))))
10167 (defun org-show-todo-tree (arg)
10168 "Make a compact tree which shows all headlines marked with TODO.
10169 The tree will show the lines where the regexp matches, and all higher
10170 headlines above the match.
10171 With a \\[universal-argument] prefix, prompt for a regexp to match.
10172 With a numeric prefix N, construct a sparse tree for the Nth element
10173 of `org-todo-keywords-1'."
10174 (interactive "P")
10175 (let ((case-fold-search nil)
10176 (kwd-re
10177 (cond ((null arg) org-not-done-regexp)
10178 ((equal arg '(4))
10179 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
10180 (mapcar 'list org-todo-keywords-1))))
10181 (concat "\\("
10182 (mapconcat 'identity (org-split-string kwd "|") "\\|")
10183 "\\)\\>")))
10184 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
10185 (regexp-quote (nth (1- (prefix-numeric-value arg))
10186 org-todo-keywords-1)))
10187 (t (error "Invalid prefix argument: %s" arg)))))
10188 (message "%d TODO entries found"
10189 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
10191 (defun org-deadline (&optional remove time)
10192 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
10193 With argument REMOVE, remove any deadline from the item.
10194 When TIME is set, it should be an internal time specification, and the
10195 scheduling will use the corresponding date."
10196 (interactive "P")
10197 (if remove
10198 (progn
10199 (org-remove-timestamp-with-keyword org-deadline-string)
10200 (message "Item no longer has a deadline."))
10201 (if (org-get-repeat)
10202 (error "Cannot change deadline on task with repeater, please do that by hand")
10203 (org-add-planning-info 'deadline time 'closed)
10204 (message "Deadline on %s" org-last-inserted-timestamp))))
10206 (defun org-schedule (&optional remove time)
10207 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
10208 With argument REMOVE, remove any scheduling date from the item.
10209 When TIME is set, it should be an internal time specification, and the
10210 scheduling will use the corresponding date."
10211 (interactive "P")
10212 (if remove
10213 (progn
10214 (org-remove-timestamp-with-keyword org-scheduled-string)
10215 (message "Item is no longer scheduled."))
10216 (if (org-get-repeat)
10217 (error "Cannot reschedule task with repeater, please do that by hand")
10218 (org-add-planning-info 'scheduled time 'closed)
10219 (message "Scheduled to %s" org-last-inserted-timestamp))))
10221 (defun org-get-scheduled-time (pom &optional inherit)
10222 "Get the scheduled time as a time tuple, of a format suitable
10223 for calling org-schedule with, or if there is no scheduling,
10224 returns nil."
10225 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
10226 (when time
10227 (apply 'encode-time (org-parse-time-string time)))))
10229 (defun org-get-deadline-time (pom &optional inherit)
10230 "Get the deadine as a time tuple, of a format suitable for
10231 calling org-deadlin with, or if there is no scheduling, returns
10232 nil."
10233 (let ((time (org-entry-get pom "DEADLINE" inherit)))
10234 (when time
10235 (apply 'encode-time (org-parse-time-string time)))))
10237 (defun org-remove-timestamp-with-keyword (keyword)
10238 "Remove all time stamps with KEYWORD in the current entry."
10239 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
10240 beg)
10241 (save-excursion
10242 (org-back-to-heading t)
10243 (setq beg (point))
10244 (outline-next-heading)
10245 (while (re-search-backward re beg t)
10246 (replace-match "")
10247 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
10248 (equal (char-before) ?\ ))
10249 (backward-delete-char 1)
10250 (if (string-match "^[ \t]*$" (buffer-substring
10251 (point-at-bol) (point-at-eol)))
10252 (delete-region (point-at-bol)
10253 (min (point-max) (1+ (point-at-eol))))))))))
10255 (defun org-add-planning-info (what &optional time &rest remove)
10256 "Insert new timestamp with keyword in the line directly after the headline.
10257 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
10258 If non is given, the user is prompted for a date.
10259 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
10260 be removed."
10261 (interactive)
10262 (let (org-time-was-given org-end-time-was-given ts
10263 end default-time default-input)
10265 (catch 'exit
10266 (when (and (not time) (memq what '(scheduled deadline)))
10267 ;; Try to get a default date/time from existing timestamp
10268 (save-excursion
10269 (org-back-to-heading t)
10270 (setq end (save-excursion (outline-next-heading) (point)))
10271 (when (re-search-forward (if (eq what 'scheduled)
10272 org-scheduled-time-regexp
10273 org-deadline-time-regexp)
10274 end t)
10275 (setq ts (match-string 1)
10276 default-time
10277 (apply 'encode-time (org-parse-time-string ts))
10278 default-input (and ts (org-get-compact-tod ts))))))
10279 (when what
10280 ;; If necessary, get the time from the user
10281 (setq time (or time (org-read-date nil 'to-time nil nil
10282 default-time default-input))))
10284 (when (and org-insert-labeled-timestamps-at-point
10285 (member what '(scheduled deadline)))
10286 (insert
10287 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
10288 (org-insert-time-stamp time org-time-was-given
10289 nil nil nil (list org-end-time-was-given))
10290 (setq what nil))
10291 (save-excursion
10292 (save-restriction
10293 (let (col list elt ts buffer-invisibility-spec)
10294 (org-back-to-heading t)
10295 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
10296 (goto-char (match-end 1))
10297 (setq col (current-column))
10298 (goto-char (match-end 0))
10299 (if (eobp) (insert "\n") (forward-char 1))
10300 (when (and (not what)
10301 (not (looking-at
10302 (concat "[ \t]*"
10303 org-keyword-time-not-clock-regexp))))
10304 ;; Nothing to add, nothing to remove...... :-)
10305 (throw 'exit nil))
10306 (if (and (not (looking-at outline-regexp))
10307 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
10308 "[^\r\n]*"))
10309 (not (equal (match-string 1) org-clock-string)))
10310 (narrow-to-region (match-beginning 0) (match-end 0))
10311 (insert-before-markers "\n")
10312 (backward-char 1)
10313 (narrow-to-region (point) (point))
10314 (and org-adapt-indentation (org-indent-to-column col)))
10315 ;; Check if we have to remove something.
10316 (setq list (cons what remove))
10317 (while list
10318 (setq elt (pop list))
10319 (goto-char (point-min))
10320 (when (or (and (eq elt 'scheduled)
10321 (re-search-forward org-scheduled-time-regexp nil t))
10322 (and (eq elt 'deadline)
10323 (re-search-forward org-deadline-time-regexp nil t))
10324 (and (eq elt 'closed)
10325 (re-search-forward org-closed-time-regexp nil t)))
10326 (replace-match "")
10327 (if (looking-at "--+<[^>]+>") (replace-match ""))
10328 (skip-chars-backward " ")
10329 (if (looking-at " +") (replace-match ""))))
10330 (goto-char (point-max))
10331 (and org-adapt-indentation (bolp) (org-indent-to-column col))
10332 (when what
10333 (insert
10334 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
10335 (cond ((eq what 'scheduled) org-scheduled-string)
10336 ((eq what 'deadline) org-deadline-string)
10337 ((eq what 'closed) org-closed-string))
10338 " ")
10339 (setq ts (org-insert-time-stamp
10340 time
10341 (or org-time-was-given
10342 (and (eq what 'closed) org-log-done-with-time))
10343 (eq what 'closed)
10344 nil nil (list org-end-time-was-given)))
10345 (end-of-line 1))
10346 (goto-char (point-min))
10347 (widen)
10348 (if (and (looking-at "[ \t]+\n")
10349 (equal (char-before) ?\n))
10350 (delete-region (1- (point)) (point-at-eol)))
10351 ts))))))
10353 (defvar org-log-note-marker (make-marker))
10354 (defvar org-log-note-purpose nil)
10355 (defvar org-log-note-state nil)
10356 (defvar org-log-note-previous-state nil)
10357 (defvar org-log-note-how nil)
10358 (defvar org-log-note-extra nil)
10359 (defvar org-log-note-window-configuration nil)
10360 (defvar org-log-note-return-to (make-marker))
10361 (defvar org-log-post-message nil
10362 "Message to be displayed after a log note has been stored.
10363 The auto-repeater uses this.")
10365 (defun org-add-note ()
10366 "Add a note to the current entry.
10367 This is done in the same way as adding a state change note."
10368 (interactive)
10369 (org-add-log-setup 'note nil nil 'findpos nil))
10371 (defvar org-property-end-re)
10372 (defun org-add-log-setup (&optional purpose state prev-state
10373 findpos how &optional extra)
10374 "Set up the post command hook to take a note.
10375 If this is about to TODO state change, the new state is expected in STATE.
10376 When FINDPOS is non-nil, find the correct position for the note in
10377 the current entry. If not, assume that it can be inserted at point.
10378 HOW is an indicator what kind of note should be created.
10379 EXTRA is additional text that will be inserted into the notes buffer."
10380 (let* ((org-log-into-drawer (org-log-into-drawer))
10381 (drawer (cond ((stringp org-log-into-drawer)
10382 org-log-into-drawer)
10383 (org-log-into-drawer "LOGBOOK")
10384 (t nil))))
10385 (save-restriction
10386 (save-excursion
10387 (when findpos
10388 (org-back-to-heading t)
10389 (narrow-to-region (point) (save-excursion
10390 (outline-next-heading) (point)))
10391 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
10392 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
10393 "[^\r\n]*\\)?"))
10394 (goto-char (match-end 0))
10395 (cond
10396 (drawer
10397 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
10398 nil t)
10399 (progn
10400 (goto-char (match-end 0))
10401 (or org-log-states-order-reversed
10402 (and (re-search-forward org-property-end-re nil t)
10403 (goto-char (1- (match-beginning 0))))))
10404 (insert "\n:" drawer ":\n:END:")
10405 (beginning-of-line 0)
10406 (org-indent-line-function)
10407 (beginning-of-line 2)
10408 (org-indent-line-function)
10409 (end-of-line 0)))
10410 ((and org-log-state-notes-insert-after-drawers
10411 (save-excursion
10412 (forward-line) (looking-at org-drawer-regexp)))
10413 (forward-line)
10414 (while (looking-at org-drawer-regexp)
10415 (goto-char (match-end 0))
10416 (re-search-forward org-property-end-re (point-max) t)
10417 (forward-line))
10418 (forward-line -1)))
10419 (unless org-log-states-order-reversed
10420 (and (= (char-after) ?\n) (forward-char 1))
10421 (org-skip-over-state-notes)
10422 (skip-chars-backward " \t\n\r")))
10423 (move-marker org-log-note-marker (point))
10424 (setq org-log-note-purpose purpose
10425 org-log-note-state state
10426 org-log-note-previous-state prev-state
10427 org-log-note-how how
10428 org-log-note-extra extra)
10429 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
10431 (defun org-skip-over-state-notes ()
10432 "Skip past the list of State notes in an entry."
10433 (if (looking-at "\n[ \t]*- State") (forward-char 1))
10434 (while (looking-at "[ \t]*- State")
10435 (condition-case nil
10436 (org-next-item)
10437 (error (org-end-of-item)))))
10439 (defun org-add-log-note (&optional purpose)
10440 "Pop up a window for taking a note, and add this note later at point."
10441 (remove-hook 'post-command-hook 'org-add-log-note)
10442 (setq org-log-note-window-configuration (current-window-configuration))
10443 (delete-other-windows)
10444 (move-marker org-log-note-return-to (point))
10445 (switch-to-buffer (marker-buffer org-log-note-marker))
10446 (goto-char org-log-note-marker)
10447 (org-switch-to-buffer-other-window "*Org Note*")
10448 (erase-buffer)
10449 (if (memq org-log-note-how '(time state))
10450 (let (current-prefix-arg) (org-store-log-note))
10451 (let ((org-inhibit-startup t)) (org-mode))
10452 (insert (format "# Insert note for %s.
10453 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
10454 (cond
10455 ((eq org-log-note-purpose 'clock-out) "stopped clock")
10456 ((eq org-log-note-purpose 'done) "closed todo item")
10457 ((eq org-log-note-purpose 'state)
10458 (format "state change from \"%s\" to \"%s\""
10459 (or org-log-note-previous-state "")
10460 (or org-log-note-state "")))
10461 ((eq org-log-note-purpose 'note)
10462 "this entry")
10463 (t (error "This should not happen")))))
10464 (if org-log-note-extra (insert org-log-note-extra))
10465 (org-set-local 'org-finish-function 'org-store-log-note)))
10467 (defvar org-note-abort nil) ; dynamically scoped
10468 (defun org-store-log-note ()
10469 "Finish taking a log note, and insert it to where it belongs."
10470 (let ((txt (buffer-string))
10471 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
10472 lines ind)
10473 (kill-buffer (current-buffer))
10474 (while (string-match "\\`#.*\n[ \t\n]*" txt)
10475 (setq txt (replace-match "" t t txt)))
10476 (if (string-match "\\s-+\\'" txt)
10477 (setq txt (replace-match "" t t txt)))
10478 (setq lines (org-split-string txt "\n"))
10479 (when (and note (string-match "\\S-" note))
10480 (setq note
10481 (org-replace-escapes
10482 note
10483 (list (cons "%u" (user-login-name))
10484 (cons "%U" user-full-name)
10485 (cons "%t" (format-time-string
10486 (org-time-stamp-format 'long 'inactive)
10487 (current-time)))
10488 (cons "%s" (if org-log-note-state
10489 (concat "\"" org-log-note-state "\"")
10490 ""))
10491 (cons "%S" (if org-log-note-previous-state
10492 (concat "\"" org-log-note-previous-state "\"")
10493 "\"\"")))))
10494 (if lines (setq note (concat note " \\\\")))
10495 (push note lines))
10496 (when (or current-prefix-arg org-note-abort)
10497 (when org-log-into-drawer
10498 (org-remove-empty-drawer-at
10499 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
10500 org-log-note-marker))
10501 (setq lines nil))
10502 (when lines
10503 (save-excursion
10504 (set-buffer (marker-buffer org-log-note-marker))
10505 (save-excursion
10506 (goto-char org-log-note-marker)
10507 (move-marker org-log-note-marker nil)
10508 (end-of-line 1)
10509 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
10510 (insert "- " (pop lines))
10511 (org-indent-line-function)
10512 (beginning-of-line 1)
10513 (looking-at "[ \t]*")
10514 (setq ind (concat (match-string 0) " "))
10515 (end-of-line 1)
10516 (while lines (insert "\n" ind (pop lines)))
10517 (message "Note stored")
10518 (org-back-to-heading t)
10519 (org-cycle-hide-drawers 'children)))))
10520 (set-window-configuration org-log-note-window-configuration)
10521 (with-current-buffer (marker-buffer org-log-note-return-to)
10522 (goto-char org-log-note-return-to))
10523 (move-marker org-log-note-return-to nil)
10524 (and org-log-post-message (message "%s" org-log-post-message)))
10526 (defun org-remove-empty-drawer-at (drawer pos)
10527 "Remove an emptyr DARWER drawer at position POS.
10528 POS may also be a marker."
10529 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
10530 (save-excursion
10531 (save-restriction
10532 (widen)
10533 (goto-char pos)
10534 (if (org-in-regexp
10535 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
10536 (replace-match ""))))))
10538 (defun org-sparse-tree (&optional arg)
10539 "Create a sparse tree, prompt for the details.
10540 This command can create sparse trees. You first need to select the type
10541 of match used to create the tree:
10543 t Show entries with a specific TODO keyword.
10544 m Show entries selected by a tags/property match.
10545 p Enter a property name and its value (both with completion on existing
10546 names/values) and show entries with that property.
10547 r Show entries matching a regular expression.
10548 d Show deadlines due within `org-deadline-warning-days'.
10549 b Show deadlines and scheduled items before a date.
10550 a Show deadlines and scheduled items after a date."
10551 (interactive "P")
10552 (let (ans kwd value)
10553 (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
10554 (setq ans (read-char-exclusive))
10555 (cond
10556 ((equal ans ?d)
10557 (call-interactively 'org-check-deadlines))
10558 ((equal ans ?b)
10559 (call-interactively 'org-check-before-date))
10560 ((equal ans ?a)
10561 (call-interactively 'org-check-after-date))
10562 ((equal ans ?t)
10563 (org-show-todo-tree '(4)))
10564 ((member ans '(?T ?m))
10565 (call-interactively 'org-match-sparse-tree))
10566 ((member ans '(?p ?P))
10567 (setq kwd (org-icompleting-read "Property: "
10568 (mapcar 'list (org-buffer-property-keys))))
10569 (setq value (org-icompleting-read "Value: "
10570 (mapcar 'list (org-property-values kwd))))
10571 (unless (string-match "\\`{.*}\\'" value)
10572 (setq value (concat "\"" value "\"")))
10573 (org-match-sparse-tree arg (concat kwd "=" value)))
10574 ((member ans '(?r ?R ?/))
10575 (call-interactively 'org-occur))
10576 (t (error "No such sparse tree command \"%c\"" ans)))))
10578 (defvar org-occur-highlights nil
10579 "List of overlays used for occur matches.")
10580 (make-variable-buffer-local 'org-occur-highlights)
10581 (defvar org-occur-parameters nil
10582 "Parameters of the active org-occur calls.
10583 This is a list, each call to org-occur pushes as cons cell,
10584 containing the regular expression and the callback, onto the list.
10585 The list can contain several entries if `org-occur' has been called
10586 several time with the KEEP-PREVIOUS argument. Otherwise, this list
10587 will only contain one set of parameters. When the highlights are
10588 removed (for example with `C-c C-c', or with the next edit (depending
10589 on `org-remove-highlights-with-change'), this variable is emptied
10590 as well.")
10591 (make-variable-buffer-local 'org-occur-parameters)
10593 (defun org-occur (regexp &optional keep-previous callback)
10594 "Make a compact tree which shows all matches of REGEXP.
10595 The tree will show the lines where the regexp matches, and all higher
10596 headlines above the match. It will also show the heading after the match,
10597 to make sure editing the matching entry is easy.
10598 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
10599 call to `org-occur' will be kept, to allow stacking of calls to this
10600 command.
10601 If CALLBACK is non-nil, it is a function which is called to confirm
10602 that the match should indeed be shown."
10603 (interactive "sRegexp: \nP")
10604 (when (equal regexp "")
10605 (error "Regexp cannot be empty"))
10606 (unless keep-previous
10607 (org-remove-occur-highlights nil nil t))
10608 (push (cons regexp callback) org-occur-parameters)
10609 (let ((cnt 0))
10610 (save-excursion
10611 (goto-char (point-min))
10612 (if (or (not keep-previous) ; do not want to keep
10613 (not org-occur-highlights)) ; no previous matches
10614 ;; hide everything
10615 (org-overview))
10616 (while (re-search-forward regexp nil t)
10617 (when (or (not callback)
10618 (save-match-data (funcall callback)))
10619 (setq cnt (1+ cnt))
10620 (when org-highlight-sparse-tree-matches
10621 (org-highlight-new-match (match-beginning 0) (match-end 0)))
10622 (org-show-context 'occur-tree))))
10623 (when org-remove-highlights-with-change
10624 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
10625 nil 'local))
10626 (unless org-sparse-tree-open-archived-trees
10627 (org-hide-archived-subtrees (point-min) (point-max)))
10628 (run-hooks 'org-occur-hook)
10629 (if (interactive-p)
10630 (message "%d match(es) for regexp %s" cnt regexp))
10631 cnt))
10633 (defun org-show-context (&optional key)
10634 "Make sure point and context and visible.
10635 How much context is shown depends upon the variables
10636 `org-show-hierarchy-above', `org-show-following-heading'. and
10637 `org-show-siblings'."
10638 (let ((heading-p (org-on-heading-p t))
10639 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
10640 (following-p (org-get-alist-option org-show-following-heading key))
10641 (entry-p (org-get-alist-option org-show-entry-below key))
10642 (siblings-p (org-get-alist-option org-show-siblings key)))
10643 (catch 'exit
10644 ;; Show heading or entry text
10645 (if (and heading-p (not entry-p))
10646 (org-flag-heading nil) ; only show the heading
10647 (and (or entry-p (org-invisible-p) (org-invisible-p2))
10648 (org-show-hidden-entry))) ; show entire entry
10649 (when following-p
10650 ;; Show next sibling, or heading below text
10651 (save-excursion
10652 (and (if heading-p (org-goto-sibling) (outline-next-heading))
10653 (org-flag-heading nil))))
10654 (when siblings-p (org-show-siblings))
10655 (when hierarchy-p
10656 ;; show all higher headings, possibly with siblings
10657 (save-excursion
10658 (while (and (condition-case nil
10659 (progn (org-up-heading-all 1) t)
10660 (error nil))
10661 (not (bobp)))
10662 (org-flag-heading nil)
10663 (when siblings-p (org-show-siblings))))))))
10665 (defun org-reveal (&optional siblings)
10666 "Show current entry, hierarchy above it, and the following headline.
10667 This can be used to show a consistent set of context around locations
10668 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
10669 not t for the search context.
10671 With optional argument SIBLINGS, on each level of the hierarchy all
10672 siblings are shown. This repairs the tree structure to what it would
10673 look like when opened with hierarchical calls to `org-cycle'."
10674 (interactive "P")
10675 (let ((org-show-hierarchy-above t)
10676 (org-show-following-heading t)
10677 (org-show-siblings (if siblings t org-show-siblings)))
10678 (org-show-context nil)))
10680 (defun org-highlight-new-match (beg end)
10681 "Highlight from BEG to END and mark the highlight is an occur headline."
10682 (let ((ov (org-make-overlay beg end)))
10683 (org-overlay-put ov 'face 'secondary-selection)
10684 (push ov org-occur-highlights)))
10686 (defun org-remove-occur-highlights (&optional beg end noremove)
10687 "Remove the occur highlights from the buffer.
10688 BEG and END are ignored. If NOREMOVE is nil, remove this function
10689 from the `before-change-functions' in the current buffer."
10690 (interactive)
10691 (unless org-inhibit-highlight-removal
10692 (mapc 'org-delete-overlay org-occur-highlights)
10693 (setq org-occur-highlights nil)
10694 (setq org-occur-parameters nil)
10695 (unless noremove
10696 (remove-hook 'before-change-functions
10697 'org-remove-occur-highlights 'local))))
10699 ;;;; Priorities
10701 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
10702 "Regular expression matching the priority indicator.")
10704 (defvar org-remove-priority-next-time nil)
10706 (defun org-priority-up ()
10707 "Increase the priority of the current item."
10708 (interactive)
10709 (org-priority 'up))
10711 (defun org-priority-down ()
10712 "Decrease the priority of the current item."
10713 (interactive)
10714 (org-priority 'down))
10716 (defun org-priority (&optional action)
10717 "Change the priority of an item by ARG.
10718 ACTION can be `set', `up', `down', or a character."
10719 (interactive)
10720 (unless org-enable-priority-commands
10721 (error "Priority commands are disabled"))
10722 (setq action (or action 'set))
10723 (let (current new news have remove)
10724 (save-excursion
10725 (org-back-to-heading t)
10726 (if (looking-at org-priority-regexp)
10727 (setq current (string-to-char (match-string 2))
10728 have t)
10729 (setq current org-default-priority))
10730 (cond
10731 ((or (eq action 'set)
10732 (if (featurep 'xemacs) (characterp action) (integerp action)))
10733 (if (not (eq action 'set))
10734 (setq new action)
10735 (message "Priority %c-%c, SPC to remove: "
10736 org-highest-priority org-lowest-priority)
10737 (setq new (read-char-exclusive)))
10738 (if (and (= (upcase org-highest-priority) org-highest-priority)
10739 (= (upcase org-lowest-priority) org-lowest-priority))
10740 (setq new (upcase new)))
10741 (cond ((equal new ?\ ) (setq remove t))
10742 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
10743 (error "Priority must be between `%c' and `%c'"
10744 org-highest-priority org-lowest-priority))))
10745 ((eq action 'up)
10746 (if (and (not have) (eq last-command this-command))
10747 (setq new org-lowest-priority)
10748 (setq new (if (and org-priority-start-cycle-with-default (not have))
10749 org-default-priority (1- current)))))
10750 ((eq action 'down)
10751 (if (and (not have) (eq last-command this-command))
10752 (setq new org-highest-priority)
10753 (setq new (if (and org-priority-start-cycle-with-default (not have))
10754 org-default-priority (1+ current)))))
10755 (t (error "Invalid action")))
10756 (if (or (< (upcase new) org-highest-priority)
10757 (> (upcase new) org-lowest-priority))
10758 (setq remove t))
10759 (setq news (format "%c" new))
10760 (if have
10761 (if remove
10762 (replace-match "" t t nil 1)
10763 (replace-match news t t nil 2))
10764 (if remove
10765 (error "No priority cookie found in line")
10766 (let ((case-fold-search nil))
10767 (looking-at org-todo-line-regexp))
10768 (if (match-end 2)
10769 (progn
10770 (goto-char (match-end 2))
10771 (insert " [#" news "]"))
10772 (goto-char (match-beginning 3))
10773 (insert "[#" news "] "))))
10774 (org-preserve-lc (org-set-tags nil 'align)))
10775 (if remove
10776 (message "Priority removed")
10777 (message "Priority of current item set to %s" news))))
10779 (defun org-get-priority (s)
10780 "Find priority cookie and return priority."
10781 (save-match-data
10782 (if (not (string-match org-priority-regexp s))
10783 (* 1000 (- org-lowest-priority org-default-priority))
10784 (* 1000 (- org-lowest-priority
10785 (string-to-char (match-string 2 s)))))))
10787 ;;;; Tags
10789 (defvar org-agenda-archives-mode)
10790 (defvar org-map-continue-from nil
10791 "Position from where mapping should continue.
10792 Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
10794 (defvar org-scanner-tags nil
10795 "The current tag list while the tags scanner is running.")
10796 (defvar org-trust-scanner-tags nil
10797 "Should `org-get-tags-at' use the tags fro the scanner.
10798 This is for internal dynamical scoping only.
10799 When this is non-nil, the function `org-get-tags-at' will return the value
10800 of `org-scanner-tags' instead of building the list by itself. This
10801 can lead to large speed-ups when the tags scanner is used in a file with
10802 many entries, and when the list of tags is retrieved, for example to
10803 obtain a list of properties. Building the tags list for each entry in such
10804 a file becomes an N^2 operation - but with this variable set, it scales
10805 as N.")
10807 (defun org-scan-tags (action matcher &optional todo-only)
10808 "Scan headline tags with inheritance and produce output ACTION.
10810 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
10811 or `agenda' to produce an entry list for an agenda view. It can also be
10812 a Lisp form or a function that should be called at each matched headline, in
10813 this case the return value is a list of all return values from these calls.
10815 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
10816 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
10817 only lines with a TODO keyword are included in the output."
10818 (require 'org-agenda)
10819 (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
10820 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
10821 (org-re
10822 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
10823 (props (list 'face 'default
10824 'done-face 'org-agenda-done
10825 'undone-face 'default
10826 'mouse-face 'highlight
10827 'org-not-done-regexp org-not-done-regexp
10828 'org-todo-regexp org-todo-regexp
10829 'keymap org-agenda-keymap
10830 'help-echo
10831 (format "mouse-2 or RET jump to org file %s"
10832 (abbreviate-file-name
10833 (or (buffer-file-name (buffer-base-buffer))
10834 (buffer-name (buffer-base-buffer)))))))
10835 (case-fold-search nil)
10836 (org-map-continue-from nil)
10837 lspos tags tags-list
10838 (tags-alist (list (cons 0 org-file-tags)))
10839 (llast 0) rtn rtn1 level category i txt
10840 todo marker entry priority)
10841 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
10842 (setq action (list 'lambda nil action)))
10843 (save-excursion
10844 (goto-char (point-min))
10845 (when (eq action 'sparse-tree)
10846 (org-overview)
10847 (org-remove-occur-highlights))
10848 (while (re-search-forward re nil t)
10849 (catch :skip
10850 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
10851 tags (if (match-end 4) (org-match-string-no-properties 4)))
10852 (goto-char (setq lspos (match-beginning 0)))
10853 (setq level (org-reduced-level (funcall outline-level))
10854 category (org-get-category))
10855 (setq i llast llast level)
10856 ;; remove tag lists from same and sublevels
10857 (while (>= i level)
10858 (when (setq entry (assoc i tags-alist))
10859 (setq tags-alist (delete entry tags-alist)))
10860 (setq i (1- i)))
10861 ;; add the next tags
10862 (when tags
10863 (setq tags (org-split-string tags ":")
10864 tags-alist
10865 (cons (cons level tags) tags-alist)))
10866 ;; compile tags for current headline
10867 (setq tags-list
10868 (if org-use-tag-inheritance
10869 (apply 'append (mapcar 'cdr (reverse tags-alist)))
10870 tags)
10871 org-scanner-tags tags-list)
10872 (when org-use-tag-inheritance
10873 (setcdr (car tags-alist)
10874 (mapcar (lambda (x)
10875 (setq x (copy-sequence x))
10876 (org-add-prop-inherited x))
10877 (cdar tags-alist))))
10878 (when (and tags org-use-tag-inheritance
10879 (or (not (eq t org-use-tag-inheritance))
10880 org-tags-exclude-from-inheritance))
10881 ;; selective inheritance, remove uninherited ones
10882 (setcdr (car tags-alist)
10883 (org-remove-uniherited-tags (cdar tags-alist))))
10884 (when (and (or (not todo-only)
10885 (and (member todo org-not-done-keywords)
10886 (or (not org-agenda-tags-todo-honor-ignore-options)
10887 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
10888 (let ((case-fold-search t)) (eval matcher))
10890 (not (member org-archive-tag tags-list))
10891 ;; we have an archive tag, should we use this anyway?
10892 (or (not org-agenda-skip-archived-trees)
10893 (and (eq action 'agenda) org-agenda-archives-mode))))
10894 (unless (eq action 'sparse-tree) (org-agenda-skip))
10896 ;; select this headline
10898 (cond
10899 ((eq action 'sparse-tree)
10900 (and org-highlight-sparse-tree-matches
10901 (org-get-heading) (match-end 0)
10902 (org-highlight-new-match
10903 (match-beginning 0) (match-beginning 1)))
10904 (org-show-context 'tags-tree))
10905 ((eq action 'agenda)
10906 (setq txt (org-format-agenda-item
10908 (concat
10909 (if (eq org-tags-match-list-sublevels 'indented)
10910 (make-string (1- level) ?.) "")
10911 (org-get-heading))
10912 category
10913 tags-list
10915 priority (org-get-priority txt))
10916 (goto-char lspos)
10917 (setq marker (org-agenda-new-marker))
10918 (org-add-props txt props
10919 'org-marker marker 'org-hd-marker marker 'org-category category
10920 'todo-state todo
10921 'priority priority 'type "tagsmatch")
10922 (push txt rtn))
10923 ((functionp action)
10924 (setq org-map-continue-from nil)
10925 (save-excursion
10926 (setq rtn1 (funcall action))
10927 (push rtn1 rtn)))
10928 (t (error "Invalid action")))
10930 ;; if we are to skip sublevels, jump to end of subtree
10931 (unless org-tags-match-list-sublevels
10932 (org-end-of-subtree t)
10933 (backward-char 1))))
10934 ;; Get the correct position from where to continue
10935 (if org-map-continue-from
10936 (goto-char org-map-continue-from)
10937 (and (= (point) lspos) (end-of-line 1)))))
10938 (when (and (eq action 'sparse-tree)
10939 (not org-sparse-tree-open-archived-trees))
10940 (org-hide-archived-subtrees (point-min) (point-max)))
10941 (nreverse rtn)))
10943 (defun org-remove-uniherited-tags (tags)
10944 "Remove all tags that are not inherited from the list TAGS."
10945 (cond
10946 ((eq org-use-tag-inheritance t)
10947 (if org-tags-exclude-from-inheritance
10948 (org-delete-all org-tags-exclude-from-inheritance tags)
10949 tags))
10950 ((not org-use-tag-inheritance) nil)
10951 ((stringp org-use-tag-inheritance)
10952 (delq nil (mapcar
10953 (lambda (x)
10954 (if (and (string-match org-use-tag-inheritance x)
10955 (not (member x org-tags-exclude-from-inheritance)))
10956 x nil))
10957 tags)))
10958 ((listp org-use-tag-inheritance)
10959 (delq nil (mapcar
10960 (lambda (x)
10961 (if (member x org-use-tag-inheritance) x nil))
10962 tags)))))
10964 (defvar todo-only) ;; dynamically scoped
10966 (defun org-match-sparse-tree (&optional todo-only match)
10967 "Create a sparse tree according to tags string MATCH.
10968 MATCH can contain positive and negative selection of tags, like
10969 \"+WORK+URGENT-WITHBOSS\".
10970 If optional argument TODO-ONLY is non-nil, only select lines that are
10971 also TODO lines."
10972 (interactive "P")
10973 (org-prepare-agenda-buffers (list (current-buffer)))
10974 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
10976 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
10978 (defvar org-cached-props nil)
10979 (defun org-cached-entry-get (pom property)
10980 (if (or (eq t org-use-property-inheritance)
10981 (and (stringp org-use-property-inheritance)
10982 (string-match org-use-property-inheritance property))
10983 (and (listp org-use-property-inheritance)
10984 (member property org-use-property-inheritance)))
10985 ;; Caching is not possible, check it directly
10986 (org-entry-get pom property 'inherit)
10987 ;; Get all properties, so that we can do complicated checks easily
10988 (cdr (assoc property (or org-cached-props
10989 (setq org-cached-props
10990 (org-entry-properties pom)))))))
10992 (defun org-global-tags-completion-table (&optional files)
10993 "Return the list of all tags in all agenda buffer/files."
10994 (save-excursion
10995 (org-uniquify
10996 (delq nil
10997 (apply 'append
10998 (mapcar
10999 (lambda (file)
11000 (set-buffer (find-file-noselect file))
11001 (append (org-get-buffer-tags)
11002 (mapcar (lambda (x) (if (stringp (car-safe x))
11003 (list (car-safe x)) nil))
11004 org-tag-alist)))
11005 (if (and files (car files))
11006 files
11007 (org-agenda-files))))))))
11009 (defun org-make-tags-matcher (match)
11010 "Create the TAGS//TODO matcher form for the selection string MATCH."
11011 ;; todo-only is scoped dynamically into this function, and the function
11012 ;; may change it if the matcher asks for it.
11013 (unless match
11014 ;; Get a new match request, with completion
11015 (let ((org-last-tags-completion-table
11016 (org-global-tags-completion-table)))
11017 (setq match (org-completing-read-no-i
11018 "Match: " 'org-tags-completion-function nil nil nil
11019 'org-tags-history))))
11021 ;; Parse the string and create a lisp form
11022 (let ((match0 match)
11023 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
11024 minus tag mm
11025 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
11026 orterms term orlist re-p str-p level-p level-op time-p
11027 prop-p pn pv po cat-p gv rest)
11028 (if (string-match "/+" match)
11029 ;; match contains also a todo-matching request
11030 (progn
11031 (setq tagsmatch (substring match 0 (match-beginning 0))
11032 todomatch (substring match (match-end 0)))
11033 (if (string-match "^!" todomatch)
11034 (setq todo-only t todomatch (substring todomatch 1)))
11035 (if (string-match "^\\s-*$" todomatch)
11036 (setq todomatch nil)))
11037 ;; only matching tags
11038 (setq tagsmatch match todomatch nil))
11040 ;; Make the tags matcher
11041 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
11042 (setq tagsmatcher t)
11043 (setq orterms (org-split-string tagsmatch "|") orlist nil)
11044 (while (setq term (pop orterms))
11045 (while (and (equal (substring term -1) "\\") orterms)
11046 (setq term (concat term "|" (pop orterms)))) ; repair bad split
11047 (while (string-match re term)
11048 (setq rest (substring term (match-end 0))
11049 minus (and (match-end 1)
11050 (equal (match-string 1 term) "-"))
11051 tag (match-string 2 term)
11052 re-p (equal (string-to-char tag) ?{)
11053 level-p (match-end 4)
11054 prop-p (match-end 5)
11055 mm (cond
11056 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
11057 (level-p
11058 (setq level-op (org-op-to-function (match-string 3 term)))
11059 `(,level-op level ,(string-to-number
11060 (match-string 4 term))))
11061 (prop-p
11062 (setq pn (match-string 5 term)
11063 po (match-string 6 term)
11064 pv (match-string 7 term)
11065 cat-p (equal pn "CATEGORY")
11066 re-p (equal (string-to-char pv) ?{)
11067 str-p (equal (string-to-char pv) ?\")
11068 time-p (save-match-data
11069 (string-match "^\"[[<].*[]>]\"$" pv))
11070 pv (if (or re-p str-p) (substring pv 1 -1) pv))
11071 (if time-p (setq pv (org-matcher-time pv)))
11072 (setq po (org-op-to-function po (if time-p 'time str-p)))
11073 (cond
11074 ((equal pn "CATEGORY")
11075 (setq gv '(get-text-property (point) 'org-category)))
11076 ((equal pn "TODO")
11077 (setq gv 'todo))
11079 (setq gv `(org-cached-entry-get nil ,pn))))
11080 (if re-p
11081 (if (eq po 'org<>)
11082 `(not (string-match ,pv (or ,gv "")))
11083 `(string-match ,pv (or ,gv "")))
11084 (if str-p
11085 `(,po (or ,gv "") ,pv)
11086 `(,po (string-to-number (or ,gv ""))
11087 ,(string-to-number pv) ))))
11088 (t `(member ,tag tags-list)))
11089 mm (if minus (list 'not mm) mm)
11090 term rest)
11091 (push mm tagsmatcher))
11092 (push (if (> (length tagsmatcher) 1)
11093 (cons 'and tagsmatcher)
11094 (car tagsmatcher))
11095 orlist)
11096 (setq tagsmatcher nil))
11097 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
11098 (setq tagsmatcher
11099 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
11100 ;; Make the todo matcher
11101 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
11102 (setq todomatcher t)
11103 (setq orterms (org-split-string todomatch "|") orlist nil)
11104 (while (setq term (pop orterms))
11105 (while (string-match re term)
11106 (setq minus (and (match-end 1)
11107 (equal (match-string 1 term) "-"))
11108 kwd (match-string 2 term)
11109 re-p (equal (string-to-char kwd) ?{)
11110 term (substring term (match-end 0))
11111 mm (if re-p
11112 `(string-match ,(substring kwd 1 -1) todo)
11113 (list 'equal 'todo kwd))
11114 mm (if minus (list 'not mm) mm))
11115 (push mm todomatcher))
11116 (push (if (> (length todomatcher) 1)
11117 (cons 'and todomatcher)
11118 (car todomatcher))
11119 orlist)
11120 (setq todomatcher nil))
11121 (setq todomatcher (if (> (length orlist) 1)
11122 (cons 'or orlist) (car orlist))))
11124 ;; Return the string and lisp forms of the matcher
11125 (setq matcher (if todomatcher
11126 (list 'and tagsmatcher todomatcher)
11127 tagsmatcher))
11128 (cons match0 matcher)))
11130 (defun org-op-to-function (op &optional stringp)
11131 "Turn an operator into the appropriate function."
11132 (setq op
11133 (cond
11134 ((equal op "<" ) '(< string< org-time<))
11135 ((equal op ">" ) '(> org-string> org-time>))
11136 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
11137 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
11138 ((member op '("=" "==")) '(= string= org-time=))
11139 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
11140 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
11142 (defun org<> (a b) (not (= a b)))
11143 (defun org-string<= (a b) (or (string= a b) (string< a b)))
11144 (defun org-string>= (a b) (not (string< a b)))
11145 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
11146 (defun org-string<> (a b) (not (string= a b)))
11147 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
11148 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
11149 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
11150 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
11151 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
11152 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
11153 (defun org-2ft (s)
11154 "Convert S to a floating point time.
11155 If S is already a number, just return it. If it is a string, parse
11156 it as a time string and apply `float-time' to it. If S is nil, just return 0."
11157 (cond
11158 ((numberp s) s)
11159 ((stringp s)
11160 (condition-case nil
11161 (float-time (apply 'encode-time (org-parse-time-string s)))
11162 (error 0.)))
11163 (t 0.)))
11165 (defun org-time-today ()
11166 "Time in seconds today at 0:00.
11167 Returns the float number of seconds since the beginning of the
11168 epoch to the beginning of today (00:00)."
11169 (float-time (apply 'encode-time
11170 (append '(0 0 0) (nthcdr 3 (decode-time))))))
11172 (defun org-matcher-time (s)
11173 "Interpret a time comparison value."
11174 (save-match-data
11175 (cond
11176 ((string= s "<now>") (float-time))
11177 ((string= s "<today>") (org-time-today))
11178 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
11179 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
11180 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
11181 (+ (org-time-today)
11182 (* (string-to-number (match-string 1 s))
11183 (cdr (assoc (match-string 2 s)
11184 '(("d" . 86400.0) ("w" . 604800.0)
11185 ("m" . 2678400.0) ("y" . 31557600.0)))))))
11186 (t (org-2ft s)))))
11188 (defun org-match-any-p (re list)
11189 "Does re match any element of list?"
11190 (setq list (mapcar (lambda (x) (string-match re x)) list))
11191 (delq nil list))
11193 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
11194 (defvar org-tags-overlay (org-make-overlay 1 1))
11195 (org-detach-overlay org-tags-overlay)
11197 (defun org-get-local-tags-at (&optional pos)
11198 "Get a list of tags defined in the current headline."
11199 (org-get-tags-at pos 'local))
11201 (defun org-get-local-tags ()
11202 "Get a list of tags defined in the current headline."
11203 (org-get-tags-at nil 'local))
11205 (defun org-get-tags-at (&optional pos local)
11206 "Get a list of all headline tags applicable at POS.
11207 POS defaults to point. If tags are inherited, the list contains
11208 the targets in the same sequence as the headlines appear, i.e.
11209 the tags of the current headline come last.
11210 When LOCAL is non-nil, only return tags from the current headline,
11211 ignore inherited ones."
11212 (interactive)
11213 (if (and org-trust-scanner-tags
11214 (or (not pos) (equal pos (point)))
11215 (not local))
11216 org-scanner-tags
11217 (let (tags ltags lastpos parent)
11218 (save-excursion
11219 (save-restriction
11220 (widen)
11221 (goto-char (or pos (point)))
11222 (save-match-data
11223 (catch 'done
11224 (condition-case nil
11225 (progn
11226 (org-back-to-heading t)
11227 (while (not (equal lastpos (point)))
11228 (setq lastpos (point))
11229 (when (looking-at
11230 (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
11231 (setq ltags (org-split-string
11232 (org-match-string-no-properties 1) ":"))
11233 (when parent
11234 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
11235 (setq tags (append
11236 (if parent
11237 (org-remove-uniherited-tags ltags)
11238 ltags)
11239 tags)))
11240 (or org-use-tag-inheritance (throw 'done t))
11241 (if local (throw 'done t))
11242 (or (org-up-heading-safe) (error nil))
11243 (setq parent t)))
11244 (error nil)))))
11245 (append (org-remove-uniherited-tags org-file-tags) tags)))))
11247 (defun org-add-prop-inherited (s)
11248 (add-text-properties 0 (length s) '(inherited t) s)
11251 (defun org-toggle-tag (tag &optional onoff)
11252 "Toggle the tag TAG for the current line.
11253 If ONOFF is `on' or `off', don't toggle but set to this state."
11254 (let (res current)
11255 (save-excursion
11256 (org-back-to-heading t)
11257 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
11258 (point-at-eol) t)
11259 (progn
11260 (setq current (match-string 1))
11261 (replace-match ""))
11262 (setq current ""))
11263 (setq current (nreverse (org-split-string current ":")))
11264 (cond
11265 ((eq onoff 'on)
11266 (setq res t)
11267 (or (member tag current) (push tag current)))
11268 ((eq onoff 'off)
11269 (or (not (member tag current)) (setq current (delete tag current))))
11270 (t (if (member tag current)
11271 (setq current (delete tag current))
11272 (setq res t)
11273 (push tag current))))
11274 (end-of-line 1)
11275 (if current
11276 (progn
11277 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
11278 (org-set-tags nil t))
11279 (delete-horizontal-space))
11280 (run-hooks 'org-after-tags-change-hook))
11281 res))
11283 (defun org-align-tags-here (to-col)
11284 ;; Assumes that this is a headline
11285 (let ((pos (point)) (col (current-column)) ncol tags-l p)
11286 (beginning-of-line 1)
11287 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
11288 (< pos (match-beginning 2)))
11289 (progn
11290 (setq tags-l (- (match-end 2) (match-beginning 2)))
11291 (goto-char (match-beginning 1))
11292 (insert " ")
11293 (delete-region (point) (1+ (match-beginning 2)))
11294 (setq ncol (max (1+ (current-column))
11295 (1+ col)
11296 (if (> to-col 0)
11297 to-col
11298 (- (abs to-col) tags-l))))
11299 (setq p (point))
11300 (insert (make-string (- ncol (current-column)) ?\ ))
11301 (setq ncol (current-column))
11302 (when indent-tabs-mode (tabify p (point-at-eol)))
11303 (org-move-to-column (min ncol col) t))
11304 (goto-char pos))))
11306 (defun org-set-tags-command (&optional arg just-align)
11307 "Call the set-tags command for the current entry."
11308 (interactive "P")
11309 (if (org-on-heading-p)
11310 (org-set-tags arg just-align)
11311 (save-excursion
11312 (org-back-to-heading t)
11313 (org-set-tags arg just-align))))
11315 (defun org-set-tags-to (data)
11316 "Set the tags of the current entry to DATA, replacing the current tags.
11317 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
11318 If DATA is nil or the empty string, any tags will be removed."
11319 (interactive "sTags: ")
11320 (setq data
11321 (cond
11322 ((eq data nil) "")
11323 ((equal data "") "")
11324 ((stringp data)
11325 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
11326 ":"))
11327 ((listp data)
11328 (concat ":" (mapconcat 'identity data ":") ":"))
11329 (t nil)))
11330 (when data
11331 (save-excursion
11332 (org-back-to-heading t)
11333 (when (looking-at org-complex-heading-regexp)
11334 (if (match-end 5)
11335 (progn
11336 (goto-char (match-beginning 5))
11337 (insert data)
11338 (delete-region (point) (point-at-eol))
11339 (org-set-tags nil 'align))
11340 (goto-char (point-at-eol))
11341 (insert " " data)
11342 (org-set-tags nil 'align)))
11343 (beginning-of-line 1)
11344 (if (looking-at ".*?\\([ \t]+\\)$")
11345 (delete-region (match-beginning 1) (match-end 1))))))
11347 (defun org-set-tags (&optional arg just-align)
11348 "Set the tags for the current headline.
11349 With prefix ARG, realign all tags in headings in the current buffer."
11350 (interactive "P")
11351 (let* ((re (concat "^" outline-regexp))
11352 (current (org-get-tags-string))
11353 (col (current-column))
11354 (org-setting-tags t)
11355 table current-tags inherited-tags ; computed below when needed
11356 tags p0 c0 c1 rpl)
11357 (if arg
11358 (save-excursion
11359 (goto-char (point-min))
11360 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
11361 (while (re-search-forward re nil t)
11362 (org-set-tags nil t)
11363 (end-of-line 1)))
11364 (message "All tags realigned to column %d" org-tags-column))
11365 (if just-align
11366 (setq tags current)
11367 ;; Get a new set of tags from the user
11368 (save-excursion
11369 (setq table (append org-tag-persistent-alist
11370 (or org-tag-alist (org-get-buffer-tags)))
11371 org-last-tags-completion-table table
11372 current-tags (org-split-string current ":")
11373 inherited-tags (nreverse
11374 (nthcdr (length current-tags)
11375 (nreverse (org-get-tags-at))))
11376 tags
11377 (if (or (eq t org-use-fast-tag-selection)
11378 (and org-use-fast-tag-selection
11379 (delq nil (mapcar 'cdr table))))
11380 (org-fast-tag-selection
11381 current-tags inherited-tags table
11382 (if org-fast-tag-selection-include-todo org-todo-key-alist))
11383 (let ((org-add-colon-after-tag-completion t))
11384 (org-trim
11385 (org-without-partial-completion
11386 (org-icompleting-read "Tags: " 'org-tags-completion-function
11387 nil nil current 'org-tags-history)))))))
11388 (while (string-match "[-+&]+" tags)
11389 ;; No boolean logic, just a list
11390 (setq tags (replace-match ":" t t tags))))
11392 (if org-tags-sort-function
11393 (setq tags (mapconcat 'identity
11394 (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
11395 org-tags-sort-function) ":")))
11397 (if (string-match "\\`[\t ]*\\'" tags)
11398 (setq tags "")
11399 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
11400 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
11402 ;; Insert new tags at the correct column
11403 (beginning-of-line 1)
11404 (cond
11405 ((and (equal current "") (equal tags "")))
11406 ((re-search-forward
11407 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
11408 (point-at-eol) t)
11409 (if (equal tags "")
11410 (setq rpl "")
11411 (goto-char (match-beginning 0))
11412 (setq c0 (current-column) p0 (point)
11413 c1 (max (1+ c0) (if (> org-tags-column 0)
11414 org-tags-column
11415 (- (- org-tags-column) (length tags))))
11416 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
11417 (replace-match rpl t t)
11418 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
11419 tags)
11420 (t (error "Tags alignment failed")))
11421 (org-move-to-column col)
11422 (unless just-align
11423 (run-hooks 'org-after-tags-change-hook)))))
11425 (defun org-change-tag-in-region (beg end tag off)
11426 "Add or remove TAG for each entry in the region.
11427 This works in the agenda, and also in an org-mode buffer."
11428 (interactive
11429 (list (region-beginning) (region-end)
11430 (let ((org-last-tags-completion-table
11431 (if (org-mode-p)
11432 (org-get-buffer-tags)
11433 (org-global-tags-completion-table))))
11434 (org-icompleting-read
11435 "Tag: " 'org-tags-completion-function nil nil nil
11436 'org-tags-history))
11437 (progn
11438 (message "[s]et or [r]emove? ")
11439 (equal (read-char-exclusive) ?r))))
11440 (if (fboundp 'deactivate-mark) (deactivate-mark))
11441 (let ((agendap (equal major-mode 'org-agenda-mode))
11442 l1 l2 m buf pos newhead (cnt 0))
11443 (goto-char end)
11444 (setq l2 (1- (org-current-line)))
11445 (goto-char beg)
11446 (setq l1 (org-current-line))
11447 (loop for l from l1 to l2 do
11448 (org-goto-line l)
11449 (setq m (get-text-property (point) 'org-hd-marker))
11450 (when (or (and (org-mode-p) (org-on-heading-p))
11451 (and agendap m))
11452 (setq buf (if agendap (marker-buffer m) (current-buffer))
11453 pos (if agendap m (point)))
11454 (with-current-buffer buf
11455 (save-excursion
11456 (save-restriction
11457 (goto-char pos)
11458 (setq cnt (1+ cnt))
11459 (org-toggle-tag tag (if off 'off 'on))
11460 (setq newhead (org-get-heading)))))
11461 (and agendap (org-agenda-change-all-lines newhead m))))
11462 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
11464 (defun org-tags-completion-function (string predicate &optional flag)
11465 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
11466 (confirm (lambda (x) (stringp (car x)))))
11467 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
11468 (setq s1 (match-string 1 string)
11469 s2 (match-string 2 string))
11470 (setq s1 "" s2 string))
11471 (cond
11472 ((eq flag nil)
11473 ;; try completion
11474 (setq rtn (try-completion s2 ctable confirm))
11475 (if (stringp rtn)
11476 (setq rtn
11477 (concat s1 s2 (substring rtn (length s2))
11478 (if (and org-add-colon-after-tag-completion
11479 (assoc rtn ctable))
11480 ":" ""))))
11481 rtn)
11482 ((eq flag t)
11483 ;; all-completions
11484 (all-completions s2 ctable confirm)
11486 ((eq flag 'lambda)
11487 ;; exact match?
11488 (assoc s2 ctable)))
11491 (defun org-fast-tag-insert (kwd tags face &optional end)
11492 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
11493 (insert (format "%-12s" (concat kwd ":"))
11494 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
11495 (or end "")))
11497 (defun org-fast-tag-show-exit (flag)
11498 (save-excursion
11499 (org-goto-line 3)
11500 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
11501 (replace-match ""))
11502 (when flag
11503 (end-of-line 1)
11504 (org-move-to-column (- (window-width) 19) t)
11505 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
11507 (defun org-set-current-tags-overlay (current prefix)
11508 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
11509 (if (featurep 'xemacs)
11510 (org-overlay-display org-tags-overlay (concat prefix s)
11511 'secondary-selection)
11512 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
11513 (org-overlay-display org-tags-overlay (concat prefix s)))))
11515 (defun org-fast-tag-selection (current inherited table &optional todo-table)
11516 "Fast tag selection with single keys.
11517 CURRENT is the current list of tags in the headline, INHERITED is the
11518 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
11519 possibly with grouping information. TODO-TABLE is a similar table with
11520 TODO keywords, should these have keys assigned to them.
11521 If the keys are nil, a-z are automatically assigned.
11522 Returns the new tags string, or nil to not change the current settings."
11523 (let* ((fulltable (append table todo-table))
11524 (maxlen (apply 'max (mapcar
11525 (lambda (x)
11526 (if (stringp (car x)) (string-width (car x)) 0))
11527 fulltable)))
11528 (buf (current-buffer))
11529 (expert (eq org-fast-tag-selection-single-key 'expert))
11530 (buffer-tags nil)
11531 (fwidth (+ maxlen 3 1 3))
11532 (ncol (/ (- (window-width) 4) fwidth))
11533 (i-face 'org-done)
11534 (c-face 'org-todo)
11535 tg cnt e c char c1 c2 ntable tbl rtn
11536 ov-start ov-end ov-prefix
11537 (exit-after-next org-fast-tag-selection-single-key)
11538 (done-keywords org-done-keywords)
11539 groups ingroup)
11540 (save-excursion
11541 (beginning-of-line 1)
11542 (if (looking-at
11543 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
11544 (setq ov-start (match-beginning 1)
11545 ov-end (match-end 1)
11546 ov-prefix "")
11547 (setq ov-start (1- (point-at-eol))
11548 ov-end (1+ ov-start))
11549 (skip-chars-forward "^\n\r")
11550 (setq ov-prefix
11551 (concat
11552 (buffer-substring (1- (point)) (point))
11553 (if (> (current-column) org-tags-column)
11555 (make-string (- org-tags-column (current-column)) ?\ ))))))
11556 (org-move-overlay org-tags-overlay ov-start ov-end)
11557 (save-window-excursion
11558 (if expert
11559 (set-buffer (get-buffer-create " *Org tags*"))
11560 (delete-other-windows)
11561 (split-window-vertically)
11562 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
11563 (erase-buffer)
11564 (org-set-local 'org-done-keywords done-keywords)
11565 (org-fast-tag-insert "Inherited" inherited i-face "\n")
11566 (org-fast-tag-insert "Current" current c-face "\n\n")
11567 (org-fast-tag-show-exit exit-after-next)
11568 (org-set-current-tags-overlay current ov-prefix)
11569 (setq tbl fulltable char ?a cnt 0)
11570 (while (setq e (pop tbl))
11571 (cond
11572 ((equal (car e) :startgroup)
11573 (push '() groups) (setq ingroup t)
11574 (when (not (= cnt 0))
11575 (setq cnt 0)
11576 (insert "\n"))
11577 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
11578 ((equal (car e) :endgroup)
11579 (setq ingroup nil cnt 0)
11580 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
11581 ((equal e '(:newline))
11582 (when (not (= cnt 0))
11583 (setq cnt 0)
11584 (insert "\n")
11585 (setq e (car tbl))
11586 (while (equal (car tbl) '(:newline))
11587 (insert "\n")
11588 (setq tbl (cdr tbl)))))
11590 (setq tg (copy-sequence (car e)) c2 nil)
11591 (if (cdr e)
11592 (setq c (cdr e))
11593 ;; automatically assign a character.
11594 (setq c1 (string-to-char
11595 (downcase (substring
11596 tg (if (= (string-to-char tg) ?@) 1 0)))))
11597 (if (or (rassoc c1 ntable) (rassoc c1 table))
11598 (while (or (rassoc char ntable) (rassoc char table))
11599 (setq char (1+ char)))
11600 (setq c2 c1))
11601 (setq c (or c2 char)))
11602 (if ingroup (push tg (car groups)))
11603 (setq tg (org-add-props tg nil 'face
11604 (cond
11605 ((not (assoc tg table))
11606 (org-get-todo-face tg))
11607 ((member tg current) c-face)
11608 ((member tg inherited) i-face)
11609 (t nil))))
11610 (if (and (= cnt 0) (not ingroup)) (insert " "))
11611 (insert "[" c "] " tg (make-string
11612 (- fwidth 4 (length tg)) ?\ ))
11613 (push (cons tg c) ntable)
11614 (when (= (setq cnt (1+ cnt)) ncol)
11615 (insert "\n")
11616 (if ingroup (insert " "))
11617 (setq cnt 0)))))
11618 (setq ntable (nreverse ntable))
11619 (insert "\n")
11620 (goto-char (point-min))
11621 (if (not expert) (org-fit-window-to-buffer))
11622 (setq rtn
11623 (catch 'exit
11624 (while t
11625 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
11626 (if (not groups) "no " "")
11627 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
11628 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
11629 (cond
11630 ((= c ?\r) (throw 'exit t))
11631 ((= c ?!)
11632 (setq groups (not groups))
11633 (goto-char (point-min))
11634 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
11635 ((= c ?\C-c)
11636 (if (not expert)
11637 (org-fast-tag-show-exit
11638 (setq exit-after-next (not exit-after-next)))
11639 (setq expert nil)
11640 (delete-other-windows)
11641 (split-window-vertically)
11642 (org-switch-to-buffer-other-window " *Org tags*")
11643 (org-fit-window-to-buffer)))
11644 ((or (= c ?\C-g)
11645 (and (= c ?q) (not (rassoc c ntable))))
11646 (org-detach-overlay org-tags-overlay)
11647 (setq quit-flag t))
11648 ((= c ?\ )
11649 (setq current nil)
11650 (if exit-after-next (setq exit-after-next 'now)))
11651 ((= c ?\t)
11652 (condition-case nil
11653 (setq tg (org-icompleting-read
11654 "Tag: "
11655 (or buffer-tags
11656 (with-current-buffer buf
11657 (org-get-buffer-tags)))))
11658 (quit (setq tg "")))
11659 (when (string-match "\\S-" tg)
11660 (add-to-list 'buffer-tags (list tg))
11661 (if (member tg current)
11662 (setq current (delete tg current))
11663 (push tg current)))
11664 (if exit-after-next (setq exit-after-next 'now)))
11665 ((setq e (rassoc c todo-table) tg (car e))
11666 (with-current-buffer buf
11667 (save-excursion (org-todo tg)))
11668 (if exit-after-next (setq exit-after-next 'now)))
11669 ((setq e (rassoc c ntable) tg (car e))
11670 (if (member tg current)
11671 (setq current (delete tg current))
11672 (loop for g in groups do
11673 (if (member tg g)
11674 (mapc (lambda (x)
11675 (setq current (delete x current)))
11676 g)))
11677 (push tg current))
11678 (if exit-after-next (setq exit-after-next 'now))))
11680 ;; Create a sorted list
11681 (setq current
11682 (sort current
11683 (lambda (a b)
11684 (assoc b (cdr (memq (assoc a ntable) ntable))))))
11685 (if (eq exit-after-next 'now) (throw 'exit t))
11686 (goto-char (point-min))
11687 (beginning-of-line 2)
11688 (delete-region (point) (point-at-eol))
11689 (org-fast-tag-insert "Current" current c-face)
11690 (org-set-current-tags-overlay current ov-prefix)
11691 (while (re-search-forward
11692 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
11693 (setq tg (match-string 1))
11694 (add-text-properties
11695 (match-beginning 1) (match-end 1)
11696 (list 'face
11697 (cond
11698 ((member tg current) c-face)
11699 ((member tg inherited) i-face)
11700 (t (get-text-property (match-beginning 1) 'face))))))
11701 (goto-char (point-min)))))
11702 (org-detach-overlay org-tags-overlay)
11703 (if rtn
11704 (mapconcat 'identity current ":")
11705 nil))))
11707 (defun org-get-tags-string ()
11708 "Get the TAGS string in the current headline."
11709 (unless (org-on-heading-p t)
11710 (error "Not on a heading"))
11711 (save-excursion
11712 (beginning-of-line 1)
11713 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
11714 (org-match-string-no-properties 1)
11715 "")))
11717 (defun org-get-tags ()
11718 "Get the list of tags specified in the current headline."
11719 (org-split-string (org-get-tags-string) ":"))
11721 (defun org-get-buffer-tags ()
11722 "Get a table of all tags used in the buffer, for completion."
11723 (let (tags)
11724 (save-excursion
11725 (goto-char (point-min))
11726 (while (re-search-forward
11727 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
11728 (when (equal (char-after (point-at-bol 0)) ?*)
11729 (mapc (lambda (x) (add-to-list 'tags x))
11730 (org-split-string (org-match-string-no-properties 1) ":")))))
11731 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
11732 (mapcar 'list tags)))
11734 ;;;; The mapping API
11736 ;;;###autoload
11737 (defun org-map-entries (func &optional match scope &rest skip)
11738 "Call FUNC at each headline selected by MATCH in SCOPE.
11740 FUNC is a function or a lisp form. The function will be called without
11741 arguments, with the cursor positioned at the beginning of the headline.
11742 The return values of all calls to the function will be collected and
11743 returned as a list.
11745 The call to FUNC will be wrapped into a save-excursion form, so FUNC
11746 does not need to preserve point. After evaluation, the cursor will be
11747 moved to the end of the line (presumably of the headline of the
11748 processed entry) and search continues from there. Under some
11749 circumstances, this may not produce the wanted results. For example,
11750 if you have removed (e.g. archived) the current (sub)tree it could
11751 mean that the next entry will be skipped entirely. In such cases, you
11752 can specify the position from where search should continue by making
11753 FUNC set the variable `org-map-continue-from' to the desired buffer
11754 position.
11756 MATCH is a tags/property/todo match as it is used in the agenda tags view.
11757 Only headlines that are matched by this query will be considered during
11758 the iteration. When MATCH is nil or t, all headlines will be
11759 visited by the iteration.
11761 SCOPE determines the scope of this command. It can be any of:
11763 nil The current buffer, respecting the restriction if any
11764 tree The subtree started with the entry at point
11765 file The current buffer, without restriction
11766 file-with-archives
11767 The current buffer, and any archives associated with it
11768 agenda All agenda files
11769 agenda-with-archives
11770 All agenda files with any archive files associated with them
11771 \(file1 file2 ...)
11772 If this is a list, all files in the list will be scanned
11774 The remaining args are treated as settings for the skipping facilities of
11775 the scanner. The following items can be given here:
11777 archive skip trees with the archive tag.
11778 comment skip trees with the COMMENT keyword
11779 function or Emacs Lisp form:
11780 will be used as value for `org-agenda-skip-function', so whenever
11781 the function returns t, FUNC will not be called for that
11782 entry and search will continue from the point where the
11783 function leaves it.
11785 If your function needs to retrieve the tags including inherited tags
11786 at the *current* entry, you can use the value of the variable
11787 `org-scanner-tags' which will be much faster than getting the value
11788 with `org-get-tags-at'. If your function gets properties with
11789 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
11790 to t around the call to `org-entry-properties' to get the same speedup.
11791 Note that if your function moves around to retrieve tags and properties at
11792 a *different* entry, you cannot use these techniques."
11793 (let* ((org-agenda-archives-mode nil) ; just to make sure
11794 (org-agenda-skip-archived-trees (memq 'archive skip))
11795 (org-agenda-skip-comment-trees (memq 'comment skip))
11796 (org-agenda-skip-function
11797 (car (org-delete-all '(comment archive) skip)))
11798 (org-tags-match-list-sublevels t)
11799 matcher file res
11800 org-todo-keywords-for-agenda
11801 org-done-keywords-for-agenda
11802 org-todo-keyword-alist-for-agenda
11803 org-drawers-for-agenda
11804 org-tag-alist-for-agenda)
11806 (cond
11807 ((eq match t) (setq matcher t))
11808 ((eq match nil) (setq matcher t))
11809 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
11811 (save-excursion
11812 (save-restriction
11813 (when (eq scope 'tree)
11814 (org-back-to-heading t)
11815 (org-narrow-to-subtree)
11816 (setq scope nil))
11818 (if (not scope)
11819 (progn
11820 (org-prepare-agenda-buffers
11821 (list (buffer-file-name (current-buffer))))
11822 (setq res (org-scan-tags func matcher)))
11823 ;; Get the right scope
11824 (cond
11825 ((and scope (listp scope) (symbolp (car scope)))
11826 (setq scope (eval scope)))
11827 ((eq scope 'agenda)
11828 (setq scope (org-agenda-files t)))
11829 ((eq scope 'agenda-with-archives)
11830 (setq scope (org-agenda-files t))
11831 (setq scope (org-add-archive-files scope)))
11832 ((eq scope 'file)
11833 (setq scope (list (buffer-file-name))))
11834 ((eq scope 'file-with-archives)
11835 (setq scope (org-add-archive-files (list (buffer-file-name))))))
11836 (org-prepare-agenda-buffers scope)
11837 (while (setq file (pop scope))
11838 (with-current-buffer (org-find-base-buffer-visiting file)
11839 (save-excursion
11840 (save-restriction
11841 (widen)
11842 (goto-char (point-min))
11843 (setq res (append res (org-scan-tags func matcher))))))))))
11844 res))
11846 ;;;; Properties
11848 ;;; Setting and retrieving properties
11850 (defconst org-special-properties
11851 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
11852 "TIMESTAMP" "TIMESTAMP_IA")
11853 "The special properties valid in Org-mode.
11855 These are properties that are not defined in the property drawer,
11856 but in some other way.")
11858 (defconst org-default-properties
11859 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
11860 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
11861 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
11862 "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
11863 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
11864 "CLOCK_MODELINE_TOTAL" "STYLE")
11865 "Some properties that are used by Org-mode for various purposes.
11866 Being in this list makes sure that they are offered for completion.")
11868 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
11869 "Regular expression matching the first line of a property drawer.")
11871 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
11872 "Regular expression matching the first line of a property drawer.")
11874 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
11875 "Regular expression matching the first line of a property drawer.")
11877 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
11878 "Regular expression matching the first line of a property drawer.")
11880 (defconst org-property-drawer-re
11881 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
11882 org-property-end-re "\\)\n?")
11883 "Matches an entire property drawer.")
11885 (defconst org-clock-drawer-re
11886 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
11887 org-property-end-re "\\)\n?")
11888 "Matches an entire clock drawer.")
11890 (defun org-property-action ()
11891 "Do an action on properties."
11892 (interactive)
11893 (let (c)
11894 (org-at-property-p)
11895 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
11896 (setq c (read-char-exclusive))
11897 (cond
11898 ((equal c ?s)
11899 (call-interactively 'org-set-property))
11900 ((equal c ?d)
11901 (call-interactively 'org-delete-property))
11902 ((equal c ?D)
11903 (call-interactively 'org-delete-property-globally))
11904 ((equal c ?c)
11905 (call-interactively 'org-compute-property-at-point))
11906 (t (error "No such property action %c" c)))))
11908 (defun org-set-effort (&optional value)
11909 "Set the effort property of the current entry.
11910 With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
11911 allowed value."
11912 (interactive "P")
11913 (if (equal value 0) (setq value 10))
11914 (let* ((completion-ignore-case t)
11915 (prop org-effort-property)
11916 (cur (org-entry-get nil prop))
11917 (allowed (org-property-get-allowed-values nil prop 'table))
11918 (existing (mapcar 'list (org-property-values prop)))
11919 (val (cond
11920 ((stringp value) value)
11921 ((and allowed (integerp value))
11922 (or (car (nth (1- value) allowed))
11923 (car (org-last allowed))))
11924 (allowed
11925 (org-completing-read "Value: " allowed nil 'req-match))
11927 (let (org-completion-use-ido org-completion-use-iswitchb)
11928 (org-completing-read
11929 (concat "Value " (if (and cur (string-match "\\S-" cur))
11930 (concat "[" cur "]") "")
11931 ": ")
11932 existing nil nil "" nil cur))))))
11933 (unless (equal (org-entry-get nil prop) val)
11934 (org-entry-put nil prop val))
11935 (message "%s is now %s" prop val)))
11937 (defun org-at-property-p ()
11938 "Is the cursor in a property line?"
11939 ;; FIXME: Does not check if we are actually in the drawer.
11940 ;; FIXME: also returns true on any drawers.....
11941 ;; This is used by C-c C-c for property action.
11942 (save-excursion
11943 (beginning-of-line 1)
11944 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
11946 (defun org-get-property-block (&optional beg end force)
11947 "Return the (beg . end) range of the body of the property drawer.
11948 BEG and END can be beginning and end of subtree, if not given
11949 they will be found.
11950 If the drawer does not exist and FORCE is non-nil, create the drawer."
11951 (catch 'exit
11952 (save-excursion
11953 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
11954 (end (or end (progn (outline-next-heading) (point)))))
11955 (goto-char beg)
11956 (if (re-search-forward org-property-start-re end t)
11957 (setq beg (1+ (match-end 0)))
11958 (if force
11959 (save-excursion
11960 (org-insert-property-drawer)
11961 (setq end (progn (outline-next-heading) (point))))
11962 (throw 'exit nil))
11963 (goto-char beg)
11964 (if (re-search-forward org-property-start-re end t)
11965 (setq beg (1+ (match-end 0)))))
11966 (if (re-search-forward org-property-end-re end t)
11967 (setq end (match-beginning 0))
11968 (or force (throw 'exit nil))
11969 (goto-char beg)
11970 (setq end beg)
11971 (org-indent-line-function)
11972 (insert ":END:\n"))
11973 (cons beg end)))))
11975 (defun org-entry-properties (&optional pom which)
11976 "Get all properties of the entry at point-or-marker POM.
11977 This includes the TODO keyword, the tags, time strings for deadline,
11978 scheduled, and clocking, and any additional properties defined in the
11979 entry. The return value is an alist, keys may occur multiple times
11980 if the property key was used several times.
11981 POM may also be nil, in which case the current entry is used.
11982 If WHICH is nil or `all', get all properties. If WHICH is
11983 `special' or `standard', only get that subclass."
11984 (setq which (or which 'all))
11985 (org-with-point-at pom
11986 (let ((clockstr (substring org-clock-string 0 -1))
11987 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
11988 beg end range props sum-props key value string clocksum)
11989 (save-excursion
11990 (when (condition-case nil
11991 (and (org-mode-p) (org-back-to-heading t))
11992 (error nil))
11993 (setq beg (point))
11994 (setq sum-props (get-text-property (point) 'org-summaries))
11995 (setq clocksum (get-text-property (point) :org-clock-minutes))
11996 (outline-next-heading)
11997 (setq end (point))
11998 (when (memq which '(all special))
11999 ;; Get the special properties, like TODO and tags
12000 (goto-char beg)
12001 (when (and (looking-at org-todo-line-regexp) (match-end 2))
12002 (push (cons "TODO" (org-match-string-no-properties 2)) props))
12003 (when (looking-at org-priority-regexp)
12004 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
12005 (when (and (setq value (org-get-tags-string))
12006 (string-match "\\S-" value))
12007 (push (cons "TAGS" value) props))
12008 (when (setq value (org-get-tags-at))
12009 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
12010 props))
12011 (while (re-search-forward org-maybe-keyword-time-regexp end t)
12012 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
12013 string (if (equal key clockstr)
12014 (org-no-properties
12015 (org-trim
12016 (buffer-substring
12017 (match-beginning 3) (goto-char (point-at-eol)))))
12018 (substring (org-match-string-no-properties 3) 1 -1)))
12019 (unless key
12020 (if (= (char-after (match-beginning 3)) ?\[)
12021 (setq key "TIMESTAMP_IA")
12022 (setq key "TIMESTAMP")))
12023 (when (or (equal key clockstr) (not (assoc key props)))
12024 (push (cons key string) props)))
12028 (when (memq which '(all standard))
12029 ;; Get the standard properties, like :PROP: ...
12030 (setq range (org-get-property-block beg end))
12031 (when range
12032 (goto-char (car range))
12033 (while (re-search-forward
12034 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
12035 (cdr range) t)
12036 (setq key (org-match-string-no-properties 1)
12037 value (org-trim (or (org-match-string-no-properties 2) "")))
12038 (unless (member key excluded)
12039 (push (cons key (or value "")) props)))))
12040 (if clocksum
12041 (push (cons "CLOCKSUM"
12042 (org-columns-number-to-string (/ (float clocksum) 60.)
12043 'add_times))
12044 props))
12045 (unless (assoc "CATEGORY" props)
12046 (setq value (or (org-get-category)
12047 (progn (org-refresh-category-properties)
12048 (org-get-category))))
12049 (push (cons "CATEGORY" value) props))
12050 (append sum-props (nreverse props)))))))
12052 (defun org-entry-get (pom property &optional inherit)
12053 "Get value of PROPERTY for entry at point-or-marker POM.
12054 If INHERIT is non-nil and the entry does not have the property,
12055 then also check higher levels of the hierarchy.
12056 If INHERIT is the symbol `selective', use inheritance only if the setting
12057 in `org-use-property-inheritance' selects PROPERTY for inheritance.
12058 If the property is present but empty, the return value is the empty string.
12059 If the property is not present at all, nil is returned."
12060 (org-with-point-at pom
12061 (if (and inherit (if (eq inherit 'selective)
12062 (org-property-inherit-p property)
12064 (org-entry-get-with-inheritance property)
12065 (if (member property org-special-properties)
12066 ;; We need a special property. Use brute force, get all properties.
12067 (cdr (assoc property (org-entry-properties nil 'special)))
12068 (let ((range (org-get-property-block)))
12069 (if (and range
12070 (goto-char (car range))
12071 (re-search-forward
12072 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
12073 (cdr range) t))
12074 ;; Found the property, return it.
12075 (if (match-end 1)
12076 (org-match-string-no-properties 1)
12077 "")))))))
12079 (defun org-property-or-variable-value (var &optional inherit)
12080 "Check if there is a property fixing the value of VAR.
12081 If yes, return this value. If not, return the current value of the variable."
12082 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
12083 (if (and prop (stringp prop) (string-match "\\S-" prop))
12084 (read prop)
12085 (symbol-value var))))
12087 (defun org-entry-delete (pom property)
12088 "Delete the property PROPERTY from entry at point-or-marker POM."
12089 (org-with-point-at pom
12090 (if (member property org-special-properties)
12091 nil ; cannot delete these properties.
12092 (let ((range (org-get-property-block)))
12093 (if (and range
12094 (goto-char (car range))
12095 (re-search-forward
12096 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
12097 (cdr range) t))
12098 (progn
12099 (delete-region (match-beginning 0) (1+ (point-at-eol)))
12101 nil)))))
12103 ;; Multi-values properties are properties that contain multiple values
12104 ;; These values are assumed to be single words, separated by whitespace.
12105 (defun org-entry-add-to-multivalued-property (pom property value)
12106 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
12107 (let* ((old (org-entry-get pom property))
12108 (values (and old (org-split-string old "[ \t]"))))
12109 (setq value (org-entry-protect-space value))
12110 (unless (member value values)
12111 (setq values (cons value values))
12112 (org-entry-put pom property
12113 (mapconcat 'identity values " ")))))
12115 (defun org-entry-remove-from-multivalued-property (pom property value)
12116 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
12117 (let* ((old (org-entry-get pom property))
12118 (values (and old (org-split-string old "[ \t]"))))
12119 (setq value (org-entry-protect-space value))
12120 (when (member value values)
12121 (setq values (delete value values))
12122 (org-entry-put pom property
12123 (mapconcat 'identity values " ")))))
12125 (defun org-entry-member-in-multivalued-property (pom property value)
12126 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
12127 (let* ((old (org-entry-get pom property))
12128 (values (and old (org-split-string old "[ \t]"))))
12129 (setq value (org-entry-protect-space value))
12130 (member value values)))
12132 (defun org-entry-get-multivalued-property (pom property)
12133 "Return a list of values in a multivalued property."
12134 (let* ((value (org-entry-get pom property))
12135 (values (and value (org-split-string value "[ \t]"))))
12136 (mapcar 'org-entry-restore-space values)))
12138 (defun org-entry-put-multivalued-property (pom property &rest values)
12139 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
12140 VALUES should be a list of strings. Spaces will be protected."
12141 (org-entry-put pom property
12142 (mapconcat 'org-entry-protect-space values " "))
12143 (let* ((value (org-entry-get pom property))
12144 (values (and value (org-split-string value "[ \t]"))))
12145 (mapcar 'org-entry-restore-space values)))
12147 (defun org-entry-protect-space (s)
12148 "Protect spaces and newline in string S."
12149 (while (string-match " " s)
12150 (setq s (replace-match "%20" t t s)))
12151 (while (string-match "\n" s)
12152 (setq s (replace-match "%0A" t t s)))
12155 (defun org-entry-restore-space (s)
12156 "Restore spaces and newline in string S."
12157 (while (string-match "%20" s)
12158 (setq s (replace-match " " t t s)))
12159 (while (string-match "%0A" s)
12160 (setq s (replace-match "\n" t t s)))
12163 (defvar org-entry-property-inherited-from (make-marker)
12164 "Marker pointing to the entry from where a property was inherited.
12165 Each call to `org-entry-get-with-inheritance' will set this marker to the
12166 location of the entry where the inheritance search matched. If there was
12167 no match, the marker will point nowhere.
12168 Note that also `org-entry-get' calls this function, if the INHERIT flag
12169 is set.")
12171 (defun org-entry-get-with-inheritance (property)
12172 "Get entry property, and search higher levels if not present."
12173 (move-marker org-entry-property-inherited-from nil)
12174 (let (tmp)
12175 (save-excursion
12176 (save-restriction
12177 (widen)
12178 (catch 'ex
12179 (while t
12180 (when (setq tmp (org-entry-get nil property))
12181 (org-back-to-heading t)
12182 (move-marker org-entry-property-inherited-from (point))
12183 (throw 'ex tmp))
12184 (or (org-up-heading-safe) (throw 'ex nil)))))
12185 (or tmp
12186 (cdr (assoc property org-file-properties))
12187 (cdr (assoc property org-global-properties))
12188 (cdr (assoc property org-global-properties-fixed))))))
12190 (defun org-entry-put (pom property value)
12191 "Set PROPERTY to VALUE for entry at point-or-marker POM."
12192 (org-with-point-at pom
12193 (org-back-to-heading t)
12194 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
12195 range)
12196 (cond
12197 ((equal property "TODO")
12198 (when (and (stringp value) (string-match "\\S-" value)
12199 (not (member value org-todo-keywords-1)))
12200 (error "\"%s\" is not a valid TODO state" value))
12201 (if (or (not value)
12202 (not (string-match "\\S-" value)))
12203 (setq value 'none))
12204 (org-todo value)
12205 (org-set-tags nil 'align))
12206 ((equal property "PRIORITY")
12207 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
12208 (string-to-char value) ?\ ))
12209 (org-set-tags nil 'align))
12210 ((equal property "SCHEDULED")
12211 (if (re-search-forward org-scheduled-time-regexp end t)
12212 (cond
12213 ((eq value 'earlier) (org-timestamp-change -1 'day))
12214 ((eq value 'later) (org-timestamp-change 1 'day))
12215 (t (call-interactively 'org-schedule)))
12216 (call-interactively 'org-schedule)))
12217 ((equal property "DEADLINE")
12218 (if (re-search-forward org-deadline-time-regexp end t)
12219 (cond
12220 ((eq value 'earlier) (org-timestamp-change -1 'day))
12221 ((eq value 'later) (org-timestamp-change 1 'day))
12222 (t (call-interactively 'org-deadline)))
12223 (call-interactively 'org-deadline)))
12224 ((member property org-special-properties)
12225 (error "The %s property can not yet be set with `org-entry-put'"
12226 property))
12227 (t ; a non-special property
12228 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
12229 (setq range (org-get-property-block beg end 'force))
12230 (goto-char (car range))
12231 (if (re-search-forward
12232 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
12233 (progn
12234 (delete-region (match-beginning 1) (match-end 1))
12235 (goto-char (match-beginning 1)))
12236 (goto-char (cdr range))
12237 (insert "\n")
12238 (backward-char 1)
12239 (org-indent-line-function)
12240 (insert ":" property ":"))
12241 (and value (insert " " value))
12242 (org-indent-line-function)))))))
12244 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
12245 "Get all property keys in the current buffer.
12246 With INCLUDE-SPECIALS, also list the special properties that reflect things
12247 like tags and TODO state.
12248 With INCLUDE-DEFAULTS, also include properties that has special meaning
12249 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
12250 With INCLUDE-COLUMNS, also include property names given in COLUMN
12251 formats in the current buffer."
12252 (let (rtn range cfmt s p)
12253 (save-excursion
12254 (save-restriction
12255 (widen)
12256 (goto-char (point-min))
12257 (while (re-search-forward org-property-start-re nil t)
12258 (setq range (org-get-property-block))
12259 (goto-char (car range))
12260 (while (re-search-forward
12261 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
12262 (cdr range) t)
12263 (add-to-list 'rtn (org-match-string-no-properties 1)))
12264 (outline-next-heading))))
12266 (when include-specials
12267 (setq rtn (append org-special-properties rtn)))
12269 (when include-defaults
12270 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
12271 (add-to-list 'rtn org-effort-property))
12273 (when include-columns
12274 (save-excursion
12275 (save-restriction
12276 (widen)
12277 (goto-char (point-min))
12278 (while (re-search-forward
12279 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
12280 nil t)
12281 (setq cfmt (match-string 2) s 0)
12282 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
12283 cfmt s)
12284 (setq s (match-end 0)
12285 p (match-string 1 cfmt))
12286 (unless (or (equal p "ITEM")
12287 (member p org-special-properties))
12288 (add-to-list 'rtn (match-string 1 cfmt))))))))
12290 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
12292 (defun org-property-values (key)
12293 "Return a list of all values of property KEY."
12294 (save-excursion
12295 (save-restriction
12296 (widen)
12297 (goto-char (point-min))
12298 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
12299 values)
12300 (while (re-search-forward re nil t)
12301 (add-to-list 'values (org-trim (match-string 1))))
12302 (delete "" values)))))
12304 (defun org-insert-property-drawer ()
12305 "Insert a property drawer into the current entry."
12306 (interactive)
12307 (org-back-to-heading t)
12308 (looking-at outline-regexp)
12309 (let ((indent (if org-adapt-indentation
12310 (- (match-end 0)(match-beginning 0))
12312 (beg (point))
12313 (re (concat "^[ \t]*" org-keyword-time-regexp))
12314 end hiddenp)
12315 (outline-next-heading)
12316 (setq end (point))
12317 (goto-char beg)
12318 (while (re-search-forward re end t))
12319 (setq hiddenp (org-invisible-p))
12320 (end-of-line 1)
12321 (and (equal (char-after) ?\n) (forward-char 1))
12322 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
12323 (if (member (match-string 1) '("CLOCK:" ":END:"))
12324 ;; just skip this line
12325 (beginning-of-line 2)
12326 ;; Drawer start, find the end
12327 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
12328 (beginning-of-line 1)))
12329 (org-skip-over-state-notes)
12330 (skip-chars-backward " \t\n\r")
12331 (if (eq (char-before) ?*) (forward-char 1))
12332 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
12333 (beginning-of-line 0)
12334 (org-indent-to-column indent)
12335 (beginning-of-line 2)
12336 (org-indent-to-column indent)
12337 (beginning-of-line 0)
12338 (if hiddenp
12339 (save-excursion
12340 (org-back-to-heading t)
12341 (hide-entry))
12342 (org-flag-drawer t))))
12344 (defun org-set-property (property value)
12345 "In the current entry, set PROPERTY to VALUE.
12346 When called interactively, this will prompt for a property name, offering
12347 completion on existing and default properties. And then it will prompt
12348 for a value, offering completion either on allowed values (via an inherited
12349 xxx_ALL property) or on existing values in other instances of this property
12350 in the current file."
12351 (interactive
12352 (let* ((completion-ignore-case t)
12353 (keys (org-buffer-property-keys nil t t))
12354 (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
12355 (prop (if (member prop0 keys)
12356 prop0
12357 (or (cdr (assoc (downcase prop0)
12358 (mapcar (lambda (x) (cons (downcase x) x))
12359 keys)))
12360 prop0)))
12361 (cur (org-entry-get nil prop))
12362 (allowed (org-property-get-allowed-values nil prop 'table))
12363 (existing (mapcar 'list (org-property-values prop)))
12364 (val (if allowed
12365 (org-completing-read "Value: " allowed nil 'req-match)
12366 (let (org-completion-use-ido org-completion-use-iswitchb)
12367 (org-completing-read
12368 (concat "Value " (if (and cur (string-match "\\S-" cur))
12369 (concat "[" cur "]") "")
12370 ": ")
12371 existing nil nil "" nil cur)))))
12372 (list prop (if (equal val "") cur val))))
12373 (unless (equal (org-entry-get nil property) value)
12374 (org-entry-put nil property value)))
12376 (defun org-delete-property (property)
12377 "In the current entry, delete PROPERTY."
12378 (interactive
12379 (let* ((completion-ignore-case t)
12380 (prop (org-icompleting-read
12381 "Property: " (org-entry-properties nil 'standard))))
12382 (list prop)))
12383 (message "Property %s %s" property
12384 (if (org-entry-delete nil property)
12385 "deleted"
12386 "was not present in the entry")))
12388 (defun org-delete-property-globally (property)
12389 "Remove PROPERTY globally, from all entries."
12390 (interactive
12391 (let* ((completion-ignore-case t)
12392 (prop (org-icompleting-read
12393 "Globally remove property: "
12394 (mapcar 'list (org-buffer-property-keys)))))
12395 (list prop)))
12396 (save-excursion
12397 (save-restriction
12398 (widen)
12399 (goto-char (point-min))
12400 (let ((cnt 0))
12401 (while (re-search-forward
12402 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
12403 nil t)
12404 (setq cnt (1+ cnt))
12405 (replace-match ""))
12406 (message "Property \"%s\" removed from %d entries" property cnt)))))
12408 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
12410 (defun org-compute-property-at-point ()
12411 "Compute the property at point.
12412 This looks for an enclosing column format, extracts the operator and
12413 then applies it to the property in the column format's scope."
12414 (interactive)
12415 (unless (org-at-property-p)
12416 (error "Not at a property"))
12417 (let ((prop (org-match-string-no-properties 2)))
12418 (org-columns-get-format-and-top-level)
12419 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
12420 (error "No operator defined for property %s" prop))
12421 (org-columns-compute prop)))
12423 (defun org-property-get-allowed-values (pom property &optional table)
12424 "Get allowed values for the property PROPERTY.
12425 When TABLE is non-nil, return an alist that can directly be used for
12426 completion."
12427 (let (vals)
12428 (cond
12429 ((equal property "TODO")
12430 (setq vals (org-with-point-at pom
12431 (append org-todo-keywords-1 '("")))))
12432 ((equal property "PRIORITY")
12433 (let ((n org-lowest-priority))
12434 (while (>= n org-highest-priority)
12435 (push (char-to-string n) vals)
12436 (setq n (1- n)))))
12437 ((member property org-special-properties))
12439 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
12441 (when (and vals (string-match "\\S-" vals))
12442 (setq vals (car (read-from-string (concat "(" vals ")"))))
12443 (setq vals (mapcar (lambda (x)
12444 (cond ((stringp x) x)
12445 ((numberp x) (number-to-string x))
12446 ((symbolp x) (symbol-name x))
12447 (t "???")))
12448 vals)))))
12449 (if table (mapcar 'list vals) vals)))
12451 (defun org-property-previous-allowed-value (&optional previous)
12452 "Switch to the next allowed value for this property."
12453 (interactive)
12454 (org-property-next-allowed-value t))
12456 (defun org-property-next-allowed-value (&optional previous)
12457 "Switch to the next allowed value for this property."
12458 (interactive)
12459 (unless (org-at-property-p)
12460 (error "Not at a property"))
12461 (let* ((key (match-string 2))
12462 (value (match-string 3))
12463 (allowed (or (org-property-get-allowed-values (point) key)
12464 (and (member value '("[ ]" "[-]" "[X]"))
12465 '("[ ]" "[X]"))))
12466 nval)
12467 (unless allowed
12468 (error "Allowed values for this property have not been defined"))
12469 (if previous (setq allowed (reverse allowed)))
12470 (if (member value allowed)
12471 (setq nval (car (cdr (member value allowed)))))
12472 (setq nval (or nval (car allowed)))
12473 (if (equal nval value)
12474 (error "Only one allowed value for this property"))
12475 (org-at-property-p)
12476 (replace-match (concat " :" key ": " nval) t t)
12477 (org-indent-line-function)
12478 (beginning-of-line 1)
12479 (skip-chars-forward " \t")))
12481 (defun org-find-entry-with-id (ident)
12482 "Locate the entry that contains the ID property with exact value IDENT.
12483 IDENT can be a string, a symbol or a number, this function will search for
12484 the string representation of it.
12485 Return the position where this entry starts, or nil if there is no such entry."
12486 (interactive "sID: ")
12487 (let ((id (cond
12488 ((stringp ident) ident)
12489 ((symbol-name ident) (symbol-name ident))
12490 ((numberp ident) (number-to-string ident))
12491 (t (error "IDENT %s must be a string, symbol or number" ident))))
12492 (case-fold-search nil))
12493 (save-excursion
12494 (save-restriction
12495 (widen)
12496 (goto-char (point-min))
12497 (when (re-search-forward
12498 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
12499 nil t)
12500 (org-back-to-heading t)
12501 (point))))))
12503 ;;;; Timestamps
12505 (defvar org-last-changed-timestamp nil)
12506 (defvar org-last-inserted-timestamp nil
12507 "The last time stamp inserted with `org-insert-time-stamp'.")
12508 (defvar org-time-was-given) ; dynamically scoped parameter
12509 (defvar org-end-time-was-given) ; dynamically scoped parameter
12510 (defvar org-ts-what) ; dynamically scoped parameter
12512 (defun org-time-stamp (arg &optional inactive)
12513 "Prompt for a date/time and insert a time stamp.
12514 If the user specifies a time like HH:MM, or if this command is called
12515 with a prefix argument, the time stamp will contain date and time.
12516 Otherwise, only the date will be included. All parts of a date not
12517 specified by the user will be filled in from the current date/time.
12518 So if you press just return without typing anything, the time stamp
12519 will represent the current date/time. If there is already a timestamp
12520 at the cursor, it will be modified."
12521 (interactive "P")
12522 (let* ((ts nil)
12523 (default-time
12524 ;; Default time is either today, or, when entering a range,
12525 ;; the range start.
12526 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
12527 (save-excursion
12528 (re-search-backward
12529 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
12530 (- (point) 20) t)))
12531 (apply 'encode-time (org-parse-time-string (match-string 1)))
12532 (current-time)))
12533 (default-input (and ts (org-get-compact-tod ts)))
12534 org-time-was-given org-end-time-was-given time)
12535 (cond
12536 ((and (org-at-timestamp-p t)
12537 (memq last-command '(org-time-stamp org-time-stamp-inactive))
12538 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
12539 (insert "--")
12540 (setq time (let ((this-command this-command))
12541 (org-read-date arg 'totime nil nil
12542 default-time default-input)))
12543 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
12544 ((org-at-timestamp-p t)
12545 (setq time (let ((this-command this-command))
12546 (org-read-date arg 'totime nil nil default-time default-input)))
12547 (when (org-at-timestamp-p t) ; just to get the match data
12548 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
12549 (replace-match "")
12550 (setq org-last-changed-timestamp
12551 (org-insert-time-stamp
12552 time (or org-time-was-given arg)
12553 inactive nil nil (list org-end-time-was-given))))
12554 (message "Timestamp updated"))
12556 (setq time (let ((this-command this-command))
12557 (org-read-date arg 'totime nil nil default-time default-input)))
12558 (org-insert-time-stamp time (or org-time-was-given arg) inactive
12559 nil nil (list org-end-time-was-given))))))
12561 ;; FIXME: can we use this for something else, like computing time differences?
12562 (defun org-get-compact-tod (s)
12563 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
12564 (let* ((t1 (match-string 1 s))
12565 (h1 (string-to-number (match-string 2 s)))
12566 (m1 (string-to-number (match-string 3 s)))
12567 (t2 (and (match-end 4) (match-string 5 s)))
12568 (h2 (and t2 (string-to-number (match-string 6 s))))
12569 (m2 (and t2 (string-to-number (match-string 7 s))))
12570 dh dm)
12571 (if (not t2)
12573 (setq dh (- h2 h1) dm (- m2 m1))
12574 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
12575 (concat t1 "+" (number-to-string dh)
12576 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
12578 (defun org-time-stamp-inactive (&optional arg)
12579 "Insert an inactive time stamp.
12580 An inactive time stamp is enclosed in square brackets instead of angle
12581 brackets. It is inactive in the sense that it does not trigger agenda entries,
12582 does not link to the calendar and cannot be changed with the S-cursor keys.
12583 So these are more for recording a certain time/date."
12584 (interactive "P")
12585 (org-time-stamp arg 'inactive))
12587 (defvar org-date-ovl (org-make-overlay 1 1))
12588 (org-overlay-put org-date-ovl 'face 'org-warning)
12589 (org-detach-overlay org-date-ovl)
12591 (defvar org-ans1) ; dynamically scoped parameter
12592 (defvar org-ans2) ; dynamically scoped parameter
12594 (defvar org-plain-time-of-day-regexp) ; defined below
12596 (defvar org-overriding-default-time nil) ; dynamically scoped
12597 (defvar org-read-date-overlay nil)
12598 (defvar org-dcst nil) ; dynamically scoped
12599 (defvar org-read-date-history nil)
12600 (defvar org-read-date-final-answer nil)
12602 (defun org-read-date (&optional with-time to-time from-string prompt
12603 default-time default-input)
12604 "Read a date, possibly a time, and make things smooth for the user.
12605 The prompt will suggest to enter an ISO date, but you can also enter anything
12606 which will at least partially be understood by `parse-time-string'.
12607 Unrecognized parts of the date will default to the current day, month, year,
12608 hour and minute. If this command is called to replace a timestamp at point,
12609 of to enter the second timestamp of a range, the default time is taken from the
12610 existing stamp. For example,
12611 3-2-5 --> 2003-02-05
12612 feb 15 --> currentyear-02-15
12613 sep 12 9 --> 2009-09-12
12614 12:45 --> today 12:45
12615 22 sept 0:34 --> currentyear-09-22 0:34
12616 12 --> currentyear-currentmonth-12
12617 Fri --> nearest Friday (today or later)
12618 etc.
12620 Furthermore you can specify a relative date by giving, as the *first* thing
12621 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
12622 change in days weeks, months, years.
12623 With a single plus or minus, the date is relative to today. With a double
12624 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
12625 +4d --> four days from today
12626 +4 --> same as above
12627 +2w --> two weeks from today
12628 ++5 --> five days from default date
12630 The function understands only English month and weekday abbreviations,
12631 but this can be configured with the variables `parse-time-months' and
12632 `parse-time-weekdays'.
12634 While prompting, a calendar is popped up - you can also select the
12635 date with the mouse (button 1). The calendar shows a period of three
12636 months. To scroll it to other months, use the keys `>' and `<'.
12637 If you don't like the calendar, turn it off with
12638 \(setq org-read-date-popup-calendar nil)
12640 With optional argument TO-TIME, the date will immediately be converted
12641 to an internal time.
12642 With an optional argument WITH-TIME, the prompt will suggest to also
12643 insert a time. Note that when WITH-TIME is not set, you can still
12644 enter a time, and this function will inform the calling routine about
12645 this change. The calling routine may then choose to change the format
12646 used to insert the time stamp into the buffer to include the time.
12647 With optional argument FROM-STRING, read from this string instead from
12648 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
12649 the time/date that is used for everything that is not specified by the
12650 user."
12651 (require 'parse-time)
12652 (let* ((org-time-stamp-rounding-minutes
12653 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
12654 (org-dcst org-display-custom-times)
12655 (ct (org-current-time))
12656 (def (or org-overriding-default-time default-time ct))
12657 (defdecode (decode-time def))
12658 (dummy (progn
12659 (when (< (nth 2 defdecode) org-extend-today-until)
12660 (setcar (nthcdr 2 defdecode) -1)
12661 (setcar (nthcdr 1 defdecode) 59)
12662 (setq def (apply 'encode-time defdecode)
12663 defdecode (decode-time def)))))
12664 (calendar-frame-setup nil)
12665 (calendar-move-hook nil)
12666 (calendar-view-diary-initially-flag nil)
12667 (view-diary-entries-initially nil)
12668 (calendar-view-holidays-initially-flag nil)
12669 (view-calendar-holidays-initially nil)
12670 (timestr (format-time-string
12671 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
12672 (prompt (concat (if prompt (concat prompt " ") "")
12673 (format "Date+time [%s]: " timestr)))
12674 ans (org-ans0 "") org-ans1 org-ans2 final)
12676 (cond
12677 (from-string (setq ans from-string))
12678 (org-read-date-popup-calendar
12679 (save-excursion
12680 (save-window-excursion
12681 (calendar)
12682 (calendar-forward-day (- (time-to-days def)
12683 (calendar-absolute-from-gregorian
12684 (calendar-current-date))))
12685 (org-eval-in-calendar nil t)
12686 (let* ((old-map (current-local-map))
12687 (map (copy-keymap calendar-mode-map))
12688 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
12689 (org-defkey map (kbd "RET") 'org-calendar-select)
12690 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
12691 'org-calendar-select-mouse)
12692 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
12693 'org-calendar-select-mouse)
12694 (org-defkey minibuffer-local-map [(meta shift left)]
12695 (lambda () (interactive)
12696 (org-eval-in-calendar '(calendar-backward-month 1))))
12697 (org-defkey minibuffer-local-map [(meta shift right)]
12698 (lambda () (interactive)
12699 (org-eval-in-calendar '(calendar-forward-month 1))))
12700 (org-defkey minibuffer-local-map [(meta shift up)]
12701 (lambda () (interactive)
12702 (org-eval-in-calendar '(calendar-backward-year 1))))
12703 (org-defkey minibuffer-local-map [(meta shift down)]
12704 (lambda () (interactive)
12705 (org-eval-in-calendar '(calendar-forward-year 1))))
12706 (org-defkey minibuffer-local-map [?\e (shift left)]
12707 (lambda () (interactive)
12708 (org-eval-in-calendar '(calendar-backward-month 1))))
12709 (org-defkey minibuffer-local-map [?\e (shift right)]
12710 (lambda () (interactive)
12711 (org-eval-in-calendar '(calendar-forward-month 1))))
12712 (org-defkey minibuffer-local-map [?\e (shift up)]
12713 (lambda () (interactive)
12714 (org-eval-in-calendar '(calendar-backward-year 1))))
12715 (org-defkey minibuffer-local-map [?\e (shift down)]
12716 (lambda () (interactive)
12717 (org-eval-in-calendar '(calendar-forward-year 1))))
12718 (org-defkey minibuffer-local-map [(shift up)]
12719 (lambda () (interactive)
12720 (org-eval-in-calendar '(calendar-backward-week 1))))
12721 (org-defkey minibuffer-local-map [(shift down)]
12722 (lambda () (interactive)
12723 (org-eval-in-calendar '(calendar-forward-week 1))))
12724 (org-defkey minibuffer-local-map [(shift left)]
12725 (lambda () (interactive)
12726 (org-eval-in-calendar '(calendar-backward-day 1))))
12727 (org-defkey minibuffer-local-map [(shift right)]
12728 (lambda () (interactive)
12729 (org-eval-in-calendar '(calendar-forward-day 1))))
12730 (org-defkey minibuffer-local-map ">"
12731 (lambda () (interactive)
12732 (org-eval-in-calendar '(scroll-calendar-left 1))))
12733 (org-defkey minibuffer-local-map "<"
12734 (lambda () (interactive)
12735 (org-eval-in-calendar '(scroll-calendar-right 1))))
12736 (run-hooks 'org-read-date-minibuffer-setup-hook)
12737 (unwind-protect
12738 (progn
12739 (use-local-map map)
12740 (add-hook 'post-command-hook 'org-read-date-display)
12741 (setq org-ans0 (read-string prompt default-input
12742 'org-read-date-history nil))
12743 ;; org-ans0: from prompt
12744 ;; org-ans1: from mouse click
12745 ;; org-ans2: from calendar motion
12746 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
12747 (remove-hook 'post-command-hook 'org-read-date-display)
12748 (use-local-map old-map)
12749 (when org-read-date-overlay
12750 (org-delete-overlay org-read-date-overlay)
12751 (setq org-read-date-overlay nil)))))))
12753 (t ; Naked prompt only
12754 (unwind-protect
12755 (setq ans (read-string prompt default-input
12756 'org-read-date-history timestr))
12757 (when org-read-date-overlay
12758 (org-delete-overlay org-read-date-overlay)
12759 (setq org-read-date-overlay nil)))))
12761 (setq final (org-read-date-analyze ans def defdecode))
12762 (setq org-read-date-final-answer ans)
12764 (if to-time
12765 (apply 'encode-time final)
12766 (if (and (boundp 'org-time-was-given) org-time-was-given)
12767 (format "%04d-%02d-%02d %02d:%02d"
12768 (nth 5 final) (nth 4 final) (nth 3 final)
12769 (nth 2 final) (nth 1 final))
12770 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
12772 (defvar def)
12773 (defvar defdecode)
12774 (defvar with-time)
12775 (defvar org-read-date-analyze-futurep nil)
12776 (defun org-read-date-display ()
12777 "Display the current date prompt interpretation in the minibuffer."
12778 (when org-read-date-display-live
12779 (when org-read-date-overlay
12780 (org-delete-overlay org-read-date-overlay))
12781 (let ((p (point)))
12782 (end-of-line 1)
12783 (while (not (equal (buffer-substring
12784 (max (point-min) (- (point) 4)) (point))
12785 " "))
12786 (insert " "))
12787 (goto-char p))
12788 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
12789 " " (or org-ans1 org-ans2)))
12790 (org-end-time-was-given nil)
12791 (f (org-read-date-analyze ans def defdecode))
12792 (fmts (if org-dcst
12793 org-time-stamp-custom-formats
12794 org-time-stamp-formats))
12795 (fmt (if (or with-time
12796 (and (boundp 'org-time-was-given) org-time-was-given))
12797 (cdr fmts)
12798 (car fmts)))
12799 (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
12800 (when (and org-end-time-was-given
12801 (string-match org-plain-time-of-day-regexp txt))
12802 (setq txt (concat (substring txt 0 (match-end 0)) "-"
12803 org-end-time-was-given
12804 (substring txt (match-end 0)))))
12805 (when org-read-date-analyze-futurep
12806 (setq txt (concat txt " (=>F)")))
12807 (setq org-read-date-overlay
12808 (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
12809 (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
12811 (defun org-read-date-analyze (ans def defdecode)
12812 "Analyse the combined answer of the date prompt."
12813 ;; FIXME: cleanup and comment
12814 (let (delta deltan deltaw deltadef year month day
12815 hour minute second wday pm h2 m2 tl wday1
12816 iso-year iso-weekday iso-week iso-year iso-date futurep)
12817 (setq org-read-date-analyze-futurep nil)
12818 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
12819 (setq ans "+0"))
12821 (when (setq delta (org-read-date-get-relative ans (current-time) def))
12822 (setq ans (replace-match "" t t ans)
12823 deltan (car delta)
12824 deltaw (nth 1 delta)
12825 deltadef (nth 2 delta)))
12827 ;; Check if there is an iso week date in there
12828 ;; If yes, sore the info and postpone interpreting it until the rest
12829 ;; of the parsing is done
12830 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
12831 (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
12832 iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
12833 iso-week (string-to-number (match-string 2 ans)))
12834 (setq ans (replace-match "" t t ans)))
12836 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
12837 (when (string-match
12838 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
12839 (setq year (if (match-end 2)
12840 (string-to-number (match-string 2 ans))
12841 (string-to-number (format-time-string "%Y")))
12842 month (string-to-number (match-string 3 ans))
12843 day (string-to-number (match-string 4 ans)))
12844 (if (< year 100) (setq year (+ 2000 year)))
12845 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
12846 t nil ans)))
12847 ;; Help matching am/pm times, because `parse-time-string' does not do that.
12848 ;; If there is a time with am/pm, and *no* time without it, we convert
12849 ;; so that matching will be successful.
12850 (loop for i from 1 to 2 do ; twice, for end time as well
12851 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
12852 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
12853 (setq hour (string-to-number (match-string 1 ans))
12854 minute (if (match-end 3)
12855 (string-to-number (match-string 3 ans))
12857 pm (equal ?p
12858 (string-to-char (downcase (match-string 4 ans)))))
12859 (if (and (= hour 12) (not pm))
12860 (setq hour 0)
12861 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
12862 (setq ans (replace-match (format "%02d:%02d" hour minute)
12863 t t ans))))
12865 ;; Check if a time range is given as a duration
12866 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
12867 (setq hour (string-to-number (match-string 1 ans))
12868 h2 (+ hour (string-to-number (match-string 3 ans)))
12869 minute (string-to-number (match-string 2 ans))
12870 m2 (+ minute (if (match-end 5) (string-to-number
12871 (match-string 5 ans))0)))
12872 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
12873 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
12874 t t ans)))
12876 ;; Check if there is a time range
12877 (when (boundp 'org-end-time-was-given)
12878 (setq org-time-was-given nil)
12879 (when (and (string-match org-plain-time-of-day-regexp ans)
12880 (match-end 8))
12881 (setq org-end-time-was-given (match-string 8 ans))
12882 (setq ans (concat (substring ans 0 (match-beginning 7))
12883 (substring ans (match-end 7))))))
12885 (setq tl (parse-time-string ans)
12886 day (or (nth 3 tl) (nth 3 defdecode))
12887 month (or (nth 4 tl)
12888 (if (and org-read-date-prefer-future
12889 (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
12890 (prog1 (1+ (nth 4 defdecode)) (setq futurep t))
12891 (nth 4 defdecode)))
12892 year (or (nth 5 tl)
12893 (if (and org-read-date-prefer-future
12894 (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
12895 (prog1 (1+ (nth 5 defdecode)) (setq futurep t))
12896 (nth 5 defdecode)))
12897 hour (or (nth 2 tl) (nth 2 defdecode))
12898 minute (or (nth 1 tl) (nth 1 defdecode))
12899 second (or (nth 0 tl) 0)
12900 wday (nth 6 tl))
12902 (when (and (eq org-read-date-prefer-future 'time)
12903 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
12904 (equal day (nth 3 defdecode))
12905 (equal month (nth 4 defdecode))
12906 (equal year (nth 5 defdecode))
12907 (nth 2 tl)
12908 (or (< (nth 2 tl) (nth 2 defdecode))
12909 (and (= (nth 2 tl) (nth 2 defdecode))
12910 (nth 1 tl)
12911 (< (nth 1 tl) (nth 1 defdecode)))))
12912 (setq day (1+ day)
12913 futurep t))
12915 ;; Special date definitions below
12916 (cond
12917 (iso-week
12918 ;; There was an iso week
12919 (setq futurep nil)
12920 (setq year (or iso-year year)
12921 day (or iso-weekday wday 1)
12922 wday nil ; to make sure that the trigger below does not match
12923 iso-date (calendar-gregorian-from-absolute
12924 (calendar-absolute-from-iso
12925 (list iso-week day year))))
12926 ; FIXME: Should we also push ISO weeks into the future?
12927 ; (when (and org-read-date-prefer-future
12928 ; (not iso-year)
12929 ; (< (calendar-absolute-from-gregorian iso-date)
12930 ; (time-to-days (current-time))))
12931 ; (setq year (1+ year)
12932 ; iso-date (calendar-gregorian-from-absolute
12933 ; (calendar-absolute-from-iso
12934 ; (list iso-week day year)))))
12935 (setq month (car iso-date)
12936 year (nth 2 iso-date)
12937 day (nth 1 iso-date)))
12938 (deltan
12939 (setq futurep nil)
12940 (unless deltadef
12941 (let ((now (decode-time (current-time))))
12942 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
12943 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
12944 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
12945 ((equal deltaw "m") (setq month (+ month deltan)))
12946 ((equal deltaw "y") (setq year (+ year deltan)))))
12947 ((and wday (not (nth 3 tl)))
12948 (setq futurep nil)
12949 ;; Weekday was given, but no day, so pick that day in the week
12950 ;; on or after the derived date.
12951 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
12952 (unless (equal wday wday1)
12953 (setq day (+ day (% (- wday wday1 -7) 7))))))
12954 (if (and (boundp 'org-time-was-given)
12955 (nth 2 tl))
12956 (setq org-time-was-given t))
12957 (if (< year 100) (setq year (+ 2000 year)))
12958 (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
12959 (setq org-read-date-analyze-futurep futurep)
12960 (list second minute hour day month year)))
12962 (defvar parse-time-weekdays)
12964 (defun org-read-date-get-relative (s today default)
12965 "Check string S for special relative date string.
12966 TODAY and DEFAULT are internal times, for today and for a default.
12967 Return shift list (N what def-flag)
12968 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
12969 N is the number of WHATs to shift.
12970 DEF-FLAG is t when a double ++ or -- indicates shift relative to
12971 the DEFAULT date rather than TODAY."
12972 (when (and
12973 (string-match
12974 (concat
12975 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
12976 "\\([0-9]+\\)?"
12977 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
12978 "\\([ \t]\\|$\\)") s)
12979 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
12980 (let* ((dir (if (> (match-end 1) (match-beginning 1))
12981 (string-to-char (substring (match-string 1 s) -1))
12982 ?+))
12983 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
12984 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
12985 (what (if (match-end 3) (match-string 3 s) "d"))
12986 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
12987 (date (if rel default today))
12988 (wday (nth 6 (decode-time date)))
12989 delta)
12990 (if wday1
12991 (progn
12992 (setq delta (mod (+ 7 (- wday1 wday)) 7))
12993 (if (= dir ?-) (setq delta (- delta 7)))
12994 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
12995 (list delta "d" rel))
12996 (list (* n (if (= dir ?-) -1 1)) what rel)))))
12998 (defun org-eval-in-calendar (form &optional keepdate)
12999 "Eval FORM in the calendar window and return to current window.
13000 Also, store the cursor date in variable org-ans2."
13001 (let ((sf (selected-frame))
13002 (sw (selected-window)))
13003 (select-window (get-buffer-window "*Calendar*" t))
13004 (eval form)
13005 (when (and (not keepdate) (calendar-cursor-to-date))
13006 (let* ((date (calendar-cursor-to-date))
13007 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13008 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
13009 (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
13010 (select-window sw)
13011 (org-select-frame-set-input-focus sf)))
13013 (defun org-calendar-select ()
13014 "Return to `org-read-date' with the date currently selected.
13015 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
13016 (interactive)
13017 (when (calendar-cursor-to-date)
13018 (let* ((date (calendar-cursor-to-date))
13019 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13020 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
13021 (if (active-minibuffer-window) (exit-minibuffer))))
13023 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
13024 "Insert a date stamp for the date given by the internal TIME.
13025 WITH-HM means, use the stamp format that includes the time of the day.
13026 INACTIVE means use square brackets instead of angular ones, so that the
13027 stamp will not contribute to the agenda.
13028 PRE and POST are optional strings to be inserted before and after the
13029 stamp.
13030 The command returns the inserted time stamp."
13031 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
13032 stamp)
13033 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
13034 (insert-before-markers (or pre ""))
13035 (insert-before-markers (setq stamp (format-time-string fmt time)))
13036 (when (listp extra)
13037 (setq extra (car extra))
13038 (if (and (stringp extra)
13039 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
13040 (setq extra (format "-%02d:%02d"
13041 (string-to-number (match-string 1 extra))
13042 (string-to-number (match-string 2 extra))))
13043 (setq extra nil)))
13044 (when extra
13045 (backward-char 1)
13046 (insert-before-markers extra)
13047 (forward-char 1))
13048 (insert-before-markers (or post ""))
13049 (setq org-last-inserted-timestamp stamp)))
13051 (defun org-toggle-time-stamp-overlays ()
13052 "Toggle the use of custom time stamp formats."
13053 (interactive)
13054 (setq org-display-custom-times (not org-display-custom-times))
13055 (unless org-display-custom-times
13056 (let ((p (point-min)) (bmp (buffer-modified-p)))
13057 (while (setq p (next-single-property-change p 'display))
13058 (if (and (get-text-property p 'display)
13059 (eq (get-text-property p 'face) 'org-date))
13060 (remove-text-properties
13061 p (setq p (next-single-property-change p 'display))
13062 '(display t))))
13063 (set-buffer-modified-p bmp)))
13064 (if (featurep 'xemacs)
13065 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
13066 (org-restart-font-lock)
13067 (setq org-table-may-need-update t)
13068 (if org-display-custom-times
13069 (message "Time stamps are overlayed with custom format")
13070 (message "Time stamp overlays removed")))
13072 (defun org-display-custom-time (beg end)
13073 "Overlay modified time stamp format over timestamp between BEG and END."
13074 (let* ((ts (buffer-substring beg end))
13075 t1 w1 with-hm tf time str w2 (off 0))
13076 (save-match-data
13077 (setq t1 (org-parse-time-string ts t))
13078 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
13079 (setq off (- (match-end 0) (match-beginning 0)))))
13080 (setq end (- end off))
13081 (setq w1 (- end beg)
13082 with-hm (and (nth 1 t1) (nth 2 t1))
13083 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
13084 time (org-fix-decoded-time t1)
13085 str (org-add-props
13086 (format-time-string
13087 (substring tf 1 -1) (apply 'encode-time time))
13088 nil 'mouse-face 'highlight)
13089 w2 (length str))
13090 (if (not (= w2 w1))
13091 (add-text-properties (1+ beg) (+ 2 beg)
13092 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
13093 (if (featurep 'xemacs)
13094 (progn
13095 (put-text-property beg end 'invisible t)
13096 (put-text-property beg end 'end-glyph (make-glyph str)))
13097 (put-text-property beg end 'display str))))
13099 (defun org-translate-time (string)
13100 "Translate all timestamps in STRING to custom format.
13101 But do this only if the variable `org-display-custom-times' is set."
13102 (when org-display-custom-times
13103 (save-match-data
13104 (let* ((start 0)
13105 (re org-ts-regexp-both)
13106 t1 with-hm inactive tf time str beg end)
13107 (while (setq start (string-match re string start))
13108 (setq beg (match-beginning 0)
13109 end (match-end 0)
13110 t1 (save-match-data
13111 (org-parse-time-string (substring string beg end) t))
13112 with-hm (and (nth 1 t1) (nth 2 t1))
13113 inactive (equal (substring string beg (1+ beg)) "[")
13114 tf (funcall (if with-hm 'cdr 'car)
13115 org-time-stamp-custom-formats)
13116 time (org-fix-decoded-time t1)
13117 str (format-time-string
13118 (concat
13119 (if inactive "[" "<") (substring tf 1 -1)
13120 (if inactive "]" ">"))
13121 (apply 'encode-time time))
13122 string (replace-match str t t string)
13123 start (+ start (length str)))))))
13124 string)
13126 (defun org-fix-decoded-time (time)
13127 "Set 0 instead of nil for the first 6 elements of time.
13128 Don't touch the rest."
13129 (let ((n 0))
13130 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
13132 (defun org-days-to-time (timestamp-string)
13133 "Difference between TIMESTAMP-STRING and now in days."
13134 (- (time-to-days (org-time-string-to-time timestamp-string))
13135 (time-to-days (current-time))))
13137 (defun org-deadline-close (timestamp-string &optional ndays)
13138 "Is the time in TIMESTAMP-STRING close to the current date?"
13139 (setq ndays (or ndays (org-get-wdays timestamp-string)))
13140 (and (< (org-days-to-time timestamp-string) ndays)
13141 (not (org-entry-is-done-p))))
13143 (defun org-get-wdays (ts)
13144 "Get the deadline lead time appropriate for timestring TS."
13145 (cond
13146 ((<= org-deadline-warning-days 0)
13147 ;; 0 or negative, enforce this value no matter what
13148 (- org-deadline-warning-days))
13149 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
13150 ;; lead time is specified.
13151 (floor (* (string-to-number (match-string 1 ts))
13152 (cdr (assoc (match-string 2 ts)
13153 '(("d" . 1) ("w" . 7)
13154 ("m" . 30.4) ("y" . 365.25)))))))
13155 ;; go for the default.
13156 (t org-deadline-warning-days)))
13158 (defun org-calendar-select-mouse (ev)
13159 "Return to `org-read-date' with the date currently selected.
13160 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
13161 (interactive "e")
13162 (mouse-set-point ev)
13163 (when (calendar-cursor-to-date)
13164 (let* ((date (calendar-cursor-to-date))
13165 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13166 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
13167 (if (active-minibuffer-window) (exit-minibuffer))))
13169 (defun org-check-deadlines (ndays)
13170 "Check if there are any deadlines due or past due.
13171 A deadline is considered due if it happens within `org-deadline-warning-days'
13172 days from today's date. If the deadline appears in an entry marked DONE,
13173 it is not shown. The prefix arg NDAYS can be used to test that many
13174 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
13175 (interactive "P")
13176 (let* ((org-warn-days
13177 (cond
13178 ((equal ndays '(4)) 100000)
13179 (ndays (prefix-numeric-value ndays))
13180 (t (abs org-deadline-warning-days))))
13181 (case-fold-search nil)
13182 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
13183 (callback
13184 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
13186 (message "%d deadlines past-due or due within %d days"
13187 (org-occur regexp nil callback)
13188 org-warn-days)))
13190 (defun org-check-before-date (date)
13191 "Check if there are deadlines or scheduled entries before DATE."
13192 (interactive (list (org-read-date)))
13193 (let ((case-fold-search nil)
13194 (regexp (concat "\\<\\(" org-deadline-string
13195 "\\|" org-scheduled-string
13196 "\\) *<\\([^>]+\\)>"))
13197 (callback
13198 (lambda () (time-less-p
13199 (org-time-string-to-time (match-string 2))
13200 (org-time-string-to-time date)))))
13201 (message "%d entries before %s"
13202 (org-occur regexp nil callback) date)))
13204 (defun org-check-after-date (date)
13205 "Check if there are deadlines or scheduled entries after DATE."
13206 (interactive (list (org-read-date)))
13207 (let ((case-fold-search nil)
13208 (regexp (concat "\\<\\(" org-deadline-string
13209 "\\|" org-scheduled-string
13210 "\\) *<\\([^>]+\\)>"))
13211 (callback
13212 (lambda () (not
13213 (time-less-p
13214 (org-time-string-to-time (match-string 2))
13215 (org-time-string-to-time date))))))
13216 (message "%d entries after %s"
13217 (org-occur regexp nil callback) date)))
13219 (defun org-evaluate-time-range (&optional to-buffer)
13220 "Evaluate a time range by computing the difference between start and end.
13221 Normally the result is just printed in the echo area, but with prefix arg
13222 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
13223 If the time range is actually in a table, the result is inserted into the
13224 next column.
13225 For time difference computation, a year is assumed to be exactly 365
13226 days in order to avoid rounding problems."
13227 (interactive "P")
13229 (org-clock-update-time-maybe)
13230 (save-excursion
13231 (unless (org-at-date-range-p t)
13232 (goto-char (point-at-bol))
13233 (re-search-forward org-tr-regexp-both (point-at-eol) t))
13234 (if (not (org-at-date-range-p t))
13235 (error "Not at a time-stamp range, and none found in current line")))
13236 (let* ((ts1 (match-string 1))
13237 (ts2 (match-string 2))
13238 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
13239 (match-end (match-end 0))
13240 (time1 (org-time-string-to-time ts1))
13241 (time2 (org-time-string-to-time ts2))
13242 (t1 (org-float-time time1))
13243 (t2 (org-float-time time2))
13244 (diff (abs (- t2 t1)))
13245 (negative (< (- t2 t1) 0))
13246 ;; (ys (floor (* 365 24 60 60)))
13247 (ds (* 24 60 60))
13248 (hs (* 60 60))
13249 (fy "%dy %dd %02d:%02d")
13250 (fy1 "%dy %dd")
13251 (fd "%dd %02d:%02d")
13252 (fd1 "%dd")
13253 (fh "%02d:%02d")
13254 y d h m align)
13255 (if havetime
13256 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
13258 d (floor (/ diff ds)) diff (mod diff ds)
13259 h (floor (/ diff hs)) diff (mod diff hs)
13260 m (floor (/ diff 60)))
13261 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
13263 d (floor (+ (/ diff ds) 0.5))
13264 h 0 m 0))
13265 (if (not to-buffer)
13266 (message "%s" (org-make-tdiff-string y d h m))
13267 (if (org-at-table-p)
13268 (progn
13269 (goto-char match-end)
13270 (setq align t)
13271 (and (looking-at " *|") (goto-char (match-end 0))))
13272 (goto-char match-end))
13273 (if (looking-at
13274 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
13275 (replace-match ""))
13276 (if negative (insert " -"))
13277 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
13278 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
13279 (insert " " (format fh h m))))
13280 (if align (org-table-align))
13281 (message "Time difference inserted")))))
13283 (defun org-make-tdiff-string (y d h m)
13284 (let ((fmt "")
13285 (l nil))
13286 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
13287 l (push y l)))
13288 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
13289 l (push d l)))
13290 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
13291 l (push h l)))
13292 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
13293 l (push m l)))
13294 (apply 'format fmt (nreverse l))))
13296 (defun org-time-string-to-time (s)
13297 (apply 'encode-time (org-parse-time-string s)))
13298 (defun org-time-string-to-seconds (s)
13299 (org-float-time (org-time-string-to-time s)))
13301 (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
13302 "Convert a time stamp to an absolute day number.
13303 If there is a specifyer for a cyclic time stamp, get the closest date to
13304 DAYNR.
13305 PREFER and SHOW-ALL are passed through to `org-closest-date'.
13306 the variable date is bound by the calendar when this is called."
13307 (cond
13308 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
13309 (if (org-diary-sexp-entry (match-string 1 s) "" date)
13310 daynr
13311 (+ daynr 1000)))
13312 ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
13313 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
13314 (time-to-days (current-time))) (match-string 0 s)
13315 prefer show-all))
13316 (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
13318 (defun org-days-to-iso-week (days)
13319 "Return the iso week number."
13320 (require 'cal-iso)
13321 (car (calendar-iso-from-absolute days)))
13323 (defun org-small-year-to-year (year)
13324 "Convert 2-digit years into 4-digit years.
13325 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
13326 The year 2000 cannot be abbreviated. Any year larger than 99
13327 is returned unchanged."
13328 (if (< year 38)
13329 (setq year (+ 2000 year))
13330 (if (< year 100)
13331 (setq year (+ 1900 year))))
13332 year)
13334 (defun org-time-from-absolute (d)
13335 "Return the time corresponding to date D.
13336 D may be an absolute day number, or a calendar-type list (month day year)."
13337 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
13338 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
13340 (defun org-calendar-holiday ()
13341 "List of holidays, for Diary display in Org-mode."
13342 (require 'holidays)
13343 (let ((hl (funcall
13344 (if (fboundp 'calendar-check-holidays)
13345 'calendar-check-holidays 'check-calendar-holidays) date)))
13346 (if hl (mapconcat 'identity hl "; "))))
13348 (defun org-diary-sexp-entry (sexp entry date)
13349 "Process a SEXP diary ENTRY for DATE."
13350 (require 'diary-lib)
13351 (let ((result (if calendar-debug-sexp
13352 (let ((stack-trace-on-error t))
13353 (eval (car (read-from-string sexp))))
13354 (condition-case nil
13355 (eval (car (read-from-string sexp)))
13356 (error
13357 (beep)
13358 (message "Bad sexp at line %d in %s: %s"
13359 (org-current-line)
13360 (buffer-file-name) sexp)
13361 (sleep-for 2))))))
13362 (cond ((stringp result) result)
13363 ((and (consp result)
13364 (stringp (cdr result))) (cdr result))
13365 (result entry)
13366 (t nil))))
13368 (defun org-diary-to-ical-string (frombuf)
13369 "Get iCalendar entries from diary entries in buffer FROMBUF.
13370 This uses the icalendar.el library."
13371 (let* ((tmpdir (if (featurep 'xemacs)
13372 (temp-directory)
13373 temporary-file-directory))
13374 (tmpfile (make-temp-name
13375 (expand-file-name "orgics" tmpdir)))
13376 buf rtn b e)
13377 (save-excursion
13378 (set-buffer frombuf)
13379 (icalendar-export-region (point-min) (point-max) tmpfile)
13380 (setq buf (find-buffer-visiting tmpfile))
13381 (set-buffer buf)
13382 (goto-char (point-min))
13383 (if (re-search-forward "^BEGIN:VEVENT" nil t)
13384 (setq b (match-beginning 0)))
13385 (goto-char (point-max))
13386 (if (re-search-backward "^END:VEVENT" nil t)
13387 (setq e (match-end 0)))
13388 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
13389 (kill-buffer buf)
13390 (delete-file tmpfile)
13391 rtn))
13393 (defun org-closest-date (start current change prefer show-all)
13394 "Find the date closest to CURRENT that is consistent with START and CHANGE.
13395 When PREFER is `past' return a date that is either CURRENT or past.
13396 When PREFER is `future', return a date that is either CURRENT or future.
13397 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
13398 ;; Make the proper lists from the dates
13399 (catch 'exit
13400 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
13401 dn dw sday cday n1 n2 n0
13402 d m y y1 y2 date1 date2 nmonths nm ny m2)
13404 (setq start (org-date-to-gregorian start)
13405 current (org-date-to-gregorian
13406 (if show-all
13407 current
13408 (time-to-days (current-time))))
13409 sday (calendar-absolute-from-gregorian start)
13410 cday (calendar-absolute-from-gregorian current))
13412 (if (<= cday sday) (throw 'exit sday))
13414 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
13415 (setq dn (string-to-number (match-string 1 change))
13416 dw (cdr (assoc (match-string 2 change) a1)))
13417 (error "Invalid change specifyer: %s" change))
13418 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
13419 (cond
13420 ((eq dw 'day)
13421 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
13422 n2 (+ n1 dn)))
13423 ((eq dw 'year)
13424 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
13425 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
13426 (setq date1 (list m d y1)
13427 n1 (calendar-absolute-from-gregorian date1)
13428 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
13429 n2 (calendar-absolute-from-gregorian date2)))
13430 ((eq dw 'month)
13431 ;; approx number of month between the two dates
13432 (setq nmonths (floor (/ (- cday sday) 30.436875)))
13433 ;; How often does dn fit in there?
13434 (setq d (nth 1 start) m (car start) y (nth 2 start)
13435 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
13436 m (+ m nm)
13437 ny (floor (/ m 12))
13438 y (+ y ny)
13439 m (- m (* ny 12)))
13440 (while (> m 12) (setq m (- m 12) y (1+ y)))
13441 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
13442 (setq m2 (+ m dn) y2 y)
13443 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
13444 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
13445 (while (<= n2 cday)
13446 (setq n1 n2 m m2 y y2)
13447 (setq m2 (+ m dn) y2 y)
13448 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
13449 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
13450 ;; Make sure n1 is the earlier date
13451 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
13452 (if show-all
13453 (cond
13454 ((eq prefer 'past) (if (= cday n2) n2 n1))
13455 ((eq prefer 'future) (if (= cday n1) n1 n2))
13456 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
13457 (cond
13458 ((eq prefer 'past) (if (= cday n2) n2 n1))
13459 ((eq prefer 'future) (if (= cday n1) n1 n2))
13460 (t (if (= cday n1) n1 n2)))))))
13462 (defun org-date-to-gregorian (date)
13463 "Turn any specification of DATE into a gregorian date for the calendar."
13464 (cond ((integerp date) (calendar-gregorian-from-absolute date))
13465 ((and (listp date) (= (length date) 3)) date)
13466 ((stringp date)
13467 (setq date (org-parse-time-string date))
13468 (list (nth 4 date) (nth 3 date) (nth 5 date)))
13469 ((listp date)
13470 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
13472 (defun org-parse-time-string (s &optional nodefault)
13473 "Parse the standard Org-mode time string.
13474 This should be a lot faster than the normal `parse-time-string'.
13475 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
13476 hour and minute fields will be nil if not given."
13477 (if (string-match org-ts-regexp0 s)
13478 (list 0
13479 (if (or (match-beginning 8) (not nodefault))
13480 (string-to-number (or (match-string 8 s) "0")))
13481 (if (or (match-beginning 7) (not nodefault))
13482 (string-to-number (or (match-string 7 s) "0")))
13483 (string-to-number (match-string 4 s))
13484 (string-to-number (match-string 3 s))
13485 (string-to-number (match-string 2 s))
13486 nil nil nil)
13487 (error "Not a standard Org-mode time string: %s" s)))
13489 (defun org-timestamp-up (&optional arg)
13490 "Increase the date item at the cursor by one.
13491 If the cursor is on the year, change the year. If it is on the month or
13492 the day, change that.
13493 With prefix ARG, change by that many units."
13494 (interactive "p")
13495 (org-timestamp-change (prefix-numeric-value arg)))
13497 (defun org-timestamp-down (&optional arg)
13498 "Decrease the date item at the cursor by one.
13499 If the cursor is on the year, change the year. If it is on the month or
13500 the day, change that.
13501 With prefix ARG, change by that many units."
13502 (interactive "p")
13503 (org-timestamp-change (- (prefix-numeric-value arg))))
13505 (defun org-timestamp-up-day (&optional arg)
13506 "Increase the date in the time stamp by one day.
13507 With prefix ARG, change that many days."
13508 (interactive "p")
13509 (if (and (not (org-at-timestamp-p t))
13510 (org-on-heading-p))
13511 (org-todo 'up)
13512 (org-timestamp-change (prefix-numeric-value arg) 'day)))
13514 (defun org-timestamp-down-day (&optional arg)
13515 "Decrease the date in the time stamp by one day.
13516 With prefix ARG, change that many days."
13517 (interactive "p")
13518 (if (and (not (org-at-timestamp-p t))
13519 (org-on-heading-p))
13520 (org-todo 'down)
13521 (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
13523 (defun org-at-timestamp-p (&optional inactive-ok)
13524 "Determine if the cursor is in or at a timestamp."
13525 (interactive)
13526 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
13527 (pos (point))
13528 (ans (or (looking-at tsr)
13529 (save-excursion
13530 (skip-chars-backward "^[<\n\r\t")
13531 (if (> (point) (point-min)) (backward-char 1))
13532 (and (looking-at tsr)
13533 (> (- (match-end 0) pos) -1))))))
13534 (and ans
13535 (boundp 'org-ts-what)
13536 (setq org-ts-what
13537 (cond
13538 ((= pos (match-beginning 0)) 'bracket)
13539 ((= pos (1- (match-end 0))) 'bracket)
13540 ((org-pos-in-match-range pos 2) 'year)
13541 ((org-pos-in-match-range pos 3) 'month)
13542 ((org-pos-in-match-range pos 7) 'hour)
13543 ((org-pos-in-match-range pos 8) 'minute)
13544 ((or (org-pos-in-match-range pos 4)
13545 (org-pos-in-match-range pos 5)) 'day)
13546 ((and (> pos (or (match-end 8) (match-end 5)))
13547 (< pos (match-end 0)))
13548 (- pos (or (match-end 8) (match-end 5))))
13549 (t 'day))))
13550 ans))
13552 (defun org-toggle-timestamp-type ()
13553 "Toggle the type (<active> or [inactive]) of a time stamp."
13554 (interactive)
13555 (when (org-at-timestamp-p t)
13556 (let ((beg (match-beginning 0)) (end (match-end 0))
13557 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
13558 (save-excursion
13559 (goto-char beg)
13560 (while (re-search-forward "[][<>]" end t)
13561 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
13562 t t)))
13563 (message "Timestamp is now %sactive"
13564 (if (equal (char-after beg) ?<) "" "in")))))
13566 (defun org-timestamp-change (n &optional what)
13567 "Change the date in the time stamp at point.
13568 The date will be changed by N times WHAT. WHAT can be `day', `month',
13569 `year', `minute', `second'. If WHAT is not given, the cursor position
13570 in the timestamp determines what will be changed."
13571 (let ((pos (point))
13572 with-hm inactive
13573 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
13574 org-ts-what
13575 extra rem
13576 ts time time0)
13577 (if (not (org-at-timestamp-p t))
13578 (error "Not at a timestamp"))
13579 (if (and (not what) (eq org-ts-what 'bracket))
13580 (org-toggle-timestamp-type)
13581 (if (and (not what) (not (eq org-ts-what 'day))
13582 org-display-custom-times
13583 (get-text-property (point) 'display)
13584 (not (get-text-property (1- (point)) 'display)))
13585 (setq org-ts-what 'day))
13586 (setq org-ts-what (or what org-ts-what)
13587 inactive (= (char-after (match-beginning 0)) ?\[)
13588 ts (match-string 0))
13589 (replace-match "")
13590 (if (string-match
13591 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
13593 (setq extra (match-string 1 ts)))
13594 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
13595 (setq with-hm t))
13596 (setq time0 (org-parse-time-string ts))
13597 (when (and (eq org-ts-what 'minute)
13598 (eq current-prefix-arg nil))
13599 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
13600 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
13601 (setcar (cdr time0) (+ (nth 1 time0)
13602 (if (> n 0) (- rem) (- dm rem))))))
13603 (setq time
13604 (encode-time (or (car time0) 0)
13605 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
13606 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
13607 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
13608 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
13609 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
13610 (nthcdr 6 time0)))
13611 (when (and (member org-ts-what '(hour minute))
13612 extra
13613 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
13614 (setq extra (org-modify-ts-extra
13615 extra
13616 (if (eq org-ts-what 'hour) 2 5)
13617 n dm)))
13618 (when (integerp org-ts-what)
13619 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
13620 (if (eq what 'calendar)
13621 (let ((cal-date (org-get-date-from-calendar)))
13622 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
13623 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
13624 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
13625 (setcar time0 (or (car time0) 0))
13626 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
13627 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
13628 (setq time (apply 'encode-time time0))))
13629 (setq org-last-changed-timestamp
13630 (org-insert-time-stamp time with-hm inactive nil nil extra))
13631 (org-clock-update-time-maybe)
13632 (goto-char pos)
13633 ;; Try to recenter the calendar window, if any
13634 (if (and org-calendar-follow-timestamp-change
13635 (get-buffer-window "*Calendar*" t)
13636 (memq org-ts-what '(day month year)))
13637 (org-recenter-calendar (time-to-days time))))))
13639 (defun org-modify-ts-extra (s pos n dm)
13640 "Change the different parts of the lead-time and repeat fields in timestamp."
13641 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
13642 ng h m new rem)
13643 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
13644 (cond
13645 ((or (org-pos-in-match-range pos 2)
13646 (org-pos-in-match-range pos 3))
13647 (setq m (string-to-number (match-string 3 s))
13648 h (string-to-number (match-string 2 s)))
13649 (if (org-pos-in-match-range pos 2)
13650 (setq h (+ h n))
13651 (setq n (* dm (org-no-warnings (signum n))))
13652 (when (not (= 0 (setq rem (% m dm))))
13653 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
13654 (setq m (+ m n)))
13655 (if (< m 0) (setq m (+ m 60) h (1- h)))
13656 (if (> m 59) (setq m (- m 60) h (1+ h)))
13657 (setq h (min 24 (max 0 h)))
13658 (setq ng 1 new (format "-%02d:%02d" h m)))
13659 ((org-pos-in-match-range pos 6)
13660 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
13661 ((org-pos-in-match-range pos 5)
13662 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
13664 ((org-pos-in-match-range pos 9)
13665 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
13666 ((org-pos-in-match-range pos 8)
13667 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
13669 (when ng
13670 (setq s (concat
13671 (substring s 0 (match-beginning ng))
13673 (substring s (match-end ng))))))
13676 (defun org-recenter-calendar (date)
13677 "If the calendar is visible, recenter it to DATE."
13678 (let* ((win (selected-window))
13679 (cwin (get-buffer-window "*Calendar*" t))
13680 (calendar-move-hook nil))
13681 (when cwin
13682 (select-window cwin)
13683 (calendar-goto-date (if (listp date) date
13684 (calendar-gregorian-from-absolute date)))
13685 (select-window win))))
13687 (defun org-goto-calendar (&optional arg)
13688 "Go to the Emacs calendar at the current date.
13689 If there is a time stamp in the current line, go to that date.
13690 A prefix ARG can be used to force the current date."
13691 (interactive "P")
13692 (let ((tsr org-ts-regexp) diff
13693 (calendar-move-hook nil)
13694 (calendar-view-holidays-initially-flag nil)
13695 (view-calendar-holidays-initially nil)
13696 (calendar-view-diary-initially-flag nil)
13697 (view-diary-entries-initially nil))
13698 (if (or (org-at-timestamp-p)
13699 (save-excursion
13700 (beginning-of-line 1)
13701 (looking-at (concat ".*" tsr))))
13702 (let ((d1 (time-to-days (current-time)))
13703 (d2 (time-to-days
13704 (org-time-string-to-time (match-string 1)))))
13705 (setq diff (- d2 d1))))
13706 (calendar)
13707 (calendar-goto-today)
13708 (if (and diff (not arg)) (calendar-forward-day diff))))
13710 (defun org-get-date-from-calendar ()
13711 "Return a list (month day year) of date at point in calendar."
13712 (with-current-buffer "*Calendar*"
13713 (save-match-data
13714 (calendar-cursor-to-date))))
13716 (defun org-date-from-calendar ()
13717 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
13718 If there is already a time stamp at the cursor position, update it."
13719 (interactive)
13720 (if (org-at-timestamp-p t)
13721 (org-timestamp-change 0 'calendar)
13722 (let ((cal-date (org-get-date-from-calendar)))
13723 (org-insert-time-stamp
13724 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
13726 (defun org-minutes-to-hh:mm-string (m)
13727 "Compute H:MM from a number of minutes."
13728 (let ((h (/ m 60)))
13729 (setq m (- m (* 60 h)))
13730 (format org-time-clocksum-format h m)))
13732 (defun org-hh:mm-string-to-minutes (s)
13733 "Convert a string H:MM to a number of minutes.
13734 If the string is just a number, interprete it as minutes.
13735 In fact, the first hh:mm or number in the string will be taken,
13736 there can be extra stuff in the string.
13737 If no number is found, the return value is 0."
13738 (cond
13739 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
13740 (+ (* (string-to-number (match-string 1 s)) 60)
13741 (string-to-number (match-string 2 s))))
13742 ((string-match "\\([0-9]+\\)" s)
13743 (string-to-number (match-string 1 s)))
13744 (t 0)))
13746 ;;;; Files
13748 (defun org-save-all-org-buffers ()
13749 "Save all Org-mode buffers without user confirmation."
13750 (interactive)
13751 (message "Saving all Org-mode buffers...")
13752 (save-some-buffers t 'org-mode-p)
13753 (when (featurep 'org-id) (org-id-locations-save))
13754 (message "Saving all Org-mode buffers... done"))
13756 (defun org-revert-all-org-buffers ()
13757 "Revert all Org-mode buffers.
13758 Prompt for confirmation when there are unsaved changes.
13759 Be sure you know what you are doing before letting this function
13760 overwrite your changes.
13762 This function is useful in a setup where one tracks org files
13763 with a version control system, to revert on one machine after pulling
13764 changes from another. I believe the procedure must be like this:
13766 1. M-x org-save-all-org-buffers
13767 2. Pull changes from the other machine, resolve conflicts
13768 3. M-x org-revert-all-org-buffers"
13769 (interactive)
13770 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
13771 (error "Abort"))
13772 (save-excursion
13773 (save-window-excursion
13774 (mapc
13775 (lambda (b)
13776 (when (and (with-current-buffer b (org-mode-p))
13777 (with-current-buffer b buffer-file-name))
13778 (switch-to-buffer b)
13779 (revert-buffer t 'no-confirm)))
13780 (buffer-list))
13781 (when (and (featurep 'org-id) org-id-track-globally)
13782 (org-id-locations-load)))))
13784 ;;;; Agenda files
13786 ;;;###autoload
13787 (defun org-iswitchb (&optional arg)
13788 "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
13789 With a prefix argument, restrict available to files.
13790 With two prefix arguments, restrict available buffers to agenda files."
13791 (interactive "P")
13792 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
13793 ((equal arg '(16)) (org-buffer-list 'agenda))
13794 (t (org-buffer-list)))))
13795 (switch-to-buffer
13796 (org-icompleting-read "Org buffer: "
13797 (mapcar 'list (mapcar 'buffer-name blist))
13798 nil t))))
13800 ;;;###autoload
13801 (defalias 'org-ido-switchb 'org-iswitchb)
13803 (defun org-buffer-list (&optional predicate exclude-tmp)
13804 "Return a list of Org buffers.
13805 PREDICATE can be `export', `files' or `agenda'.
13807 export restrict the list to Export buffers.
13808 files restrict the list to buffers visiting Org files.
13809 agenda restrict the list to buffers visiting agenda files.
13811 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
13812 (let* ((bfn nil)
13813 (agenda-files (and (eq predicate 'agenda)
13814 (mapcar 'file-truename (org-agenda-files t))))
13815 (filter
13816 (cond
13817 ((eq predicate 'files)
13818 (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
13819 ((eq predicate 'export)
13820 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
13821 ((eq predicate 'agenda)
13822 (lambda (b)
13823 (with-current-buffer b
13824 (and (eq major-mode 'org-mode)
13825 (setq bfn (buffer-file-name b))
13826 (member (file-truename bfn) agenda-files)))))
13827 (t (lambda (b) (with-current-buffer b
13828 (or (eq major-mode 'org-mode)
13829 (string-match "\*Org .*Export"
13830 (buffer-name b)))))))))
13831 (delq nil
13832 (mapcar
13833 (lambda(b)
13834 (if (and (funcall filter b)
13835 (or (not exclude-tmp)
13836 (not (string-match "tmp" (buffer-name b)))))
13838 nil))
13839 (buffer-list)))))
13841 (defun org-agenda-files (&optional unrestricted archives)
13842 "Get the list of agenda files.
13843 Optional UNRESTRICTED means return the full list even if a restriction
13844 is currently in place.
13845 When ARCHIVES is t, include all archive files hat are really being
13846 used by the agenda files. If ARCHIVE is `ifmode', do this only if
13847 `org-agenda-archives-mode' is t."
13848 (let ((files
13849 (cond
13850 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
13851 ((stringp org-agenda-files) (org-read-agenda-file-list))
13852 ((listp org-agenda-files) org-agenda-files)
13853 (t (error "Invalid value of `org-agenda-files'")))))
13854 (setq files (apply 'append
13855 (mapcar (lambda (f)
13856 (if (file-directory-p f)
13857 (directory-files
13858 f t org-agenda-file-regexp)
13859 (list f)))
13860 files)))
13861 (when org-agenda-skip-unavailable-files
13862 (setq files (delq nil
13863 (mapcar (function
13864 (lambda (file)
13865 (and (file-readable-p file) file)))
13866 files))))
13867 (when (or (eq archives t)
13868 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
13869 (setq files (org-add-archive-files files)))
13870 files))
13872 (defun org-edit-agenda-file-list ()
13873 "Edit the list of agenda files.
13874 Depending on setup, this either uses customize to edit the variable
13875 `org-agenda-files', or it visits the file that is holding the list. In the
13876 latter case, the buffer is set up in a way that saving it automatically kills
13877 the buffer and restores the previous window configuration."
13878 (interactive)
13879 (if (stringp org-agenda-files)
13880 (let ((cw (current-window-configuration)))
13881 (find-file org-agenda-files)
13882 (org-set-local 'org-window-configuration cw)
13883 (org-add-hook 'after-save-hook
13884 (lambda ()
13885 (set-window-configuration
13886 (prog1 org-window-configuration
13887 (kill-buffer (current-buffer))))
13888 (org-install-agenda-files-menu)
13889 (message "New agenda file list installed"))
13890 nil 'local)
13891 (message "%s" (substitute-command-keys
13892 "Edit list and finish with \\[save-buffer]")))
13893 (customize-variable 'org-agenda-files)))
13895 (defun org-store-new-agenda-file-list (list)
13896 "Set new value for the agenda file list and save it correctly."
13897 (if (stringp org-agenda-files)
13898 (let ((f org-agenda-files) b)
13899 (while (setq b (find-buffer-visiting f)) (kill-buffer b))
13900 (with-temp-file f
13901 (insert (mapconcat 'identity list "\n") "\n")))
13902 (let ((org-mode-hook nil) (org-inhibit-startup t)
13903 (org-insert-mode-line-in-empty-file nil))
13904 (setq org-agenda-files list)
13905 (customize-save-variable 'org-agenda-files org-agenda-files))))
13907 (defun org-read-agenda-file-list ()
13908 "Read the list of agenda files from a file."
13909 (when (file-directory-p org-agenda-files)
13910 (error "`org-agenda-files' cannot be a single directory"))
13911 (when (stringp org-agenda-files)
13912 (with-temp-buffer
13913 (insert-file-contents org-agenda-files)
13914 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
13917 ;;;###autoload
13918 (defun org-cycle-agenda-files ()
13919 "Cycle through the files in `org-agenda-files'.
13920 If the current buffer visits an agenda file, find the next one in the list.
13921 If the current buffer does not, find the first agenda file."
13922 (interactive)
13923 (let* ((fs (org-agenda-files t))
13924 (files (append fs (list (car fs))))
13925 (tcf (if buffer-file-name (file-truename buffer-file-name)))
13926 file)
13927 (unless files (error "No agenda files"))
13928 (catch 'exit
13929 (while (setq file (pop files))
13930 (if (equal (file-truename file) tcf)
13931 (when (car files)
13932 (find-file (car files))
13933 (throw 'exit t))))
13934 (find-file (car fs)))
13935 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
13937 (defun org-agenda-file-to-front (&optional to-end)
13938 "Move/add the current file to the top of the agenda file list.
13939 If the file is not present in the list, it is added to the front. If it is
13940 present, it is moved there. With optional argument TO-END, add/move to the
13941 end of the list."
13942 (interactive "P")
13943 (let ((org-agenda-skip-unavailable-files nil)
13944 (file-alist (mapcar (lambda (x)
13945 (cons (file-truename x) x))
13946 (org-agenda-files t)))
13947 (ctf (file-truename buffer-file-name))
13948 x had)
13949 (setq x (assoc ctf file-alist) had x)
13951 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
13952 (if to-end
13953 (setq file-alist (append (delq x file-alist) (list x)))
13954 (setq file-alist (cons x (delq x file-alist))))
13955 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
13956 (org-install-agenda-files-menu)
13957 (message "File %s to %s of agenda file list"
13958 (if had "moved" "added") (if to-end "end" "front"))))
13960 (defun org-remove-file (&optional file)
13961 "Remove current file from the list of files in variable `org-agenda-files'.
13962 These are the files which are being checked for agenda entries.
13963 Optional argument FILE means, use this file instead of the current."
13964 (interactive)
13965 (let* ((org-agenda-skip-unavailable-files nil)
13966 (file (or file buffer-file-name))
13967 (true-file (file-truename file))
13968 (afile (abbreviate-file-name file))
13969 (files (delq nil (mapcar
13970 (lambda (x)
13971 (if (equal true-file
13972 (file-truename x))
13973 nil x))
13974 (org-agenda-files t)))))
13975 (if (not (= (length files) (length (org-agenda-files t))))
13976 (progn
13977 (org-store-new-agenda-file-list files)
13978 (org-install-agenda-files-menu)
13979 (message "Removed file: %s" afile))
13980 (message "File was not in list: %s (not removed)" afile))))
13982 (defun org-file-menu-entry (file)
13983 (vector file (list 'find-file file) t))
13985 (defun org-check-agenda-file (file)
13986 "Make sure FILE exists. If not, ask user what to do."
13987 (when (not (file-exists-p file))
13988 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
13989 (abbreviate-file-name file))
13990 (let ((r (downcase (read-char-exclusive))))
13991 (cond
13992 ((equal r ?r)
13993 (org-remove-file file)
13994 (throw 'nextfile t))
13995 (t (error "Abort"))))))
13997 (defun org-get-agenda-file-buffer (file)
13998 "Get a buffer visiting FILE. If the buffer needs to be created, add
13999 it to the list of buffers which might be released later."
14000 (let ((buf (org-find-base-buffer-visiting file)))
14001 (if buf
14002 buf ; just return it
14003 ;; Make a new buffer and remember it
14004 (setq buf (find-file-noselect file))
14005 (if buf (push buf org-agenda-new-buffers))
14006 buf)))
14008 (defun org-release-buffers (blist)
14009 "Release all buffers in list, asking the user for confirmation when needed.
14010 When a buffer is unmodified, it is just killed. When modified, it is saved
14011 \(if the user agrees) and then killed."
14012 (let (buf file)
14013 (while (setq buf (pop blist))
14014 (setq file (buffer-file-name buf))
14015 (when (and (buffer-modified-p buf)
14016 file
14017 (y-or-n-p (format "Save file %s? " file)))
14018 (with-current-buffer buf (save-buffer)))
14019 (kill-buffer buf))))
14021 (defun org-prepare-agenda-buffers (files)
14022 "Create buffers for all agenda files, protect archived trees and comments."
14023 (interactive)
14024 (let ((pa '(:org-archived t))
14025 (pc '(:org-comment t))
14026 (pall '(:org-archived t :org-comment t))
14027 (inhibit-read-only t)
14028 (rea (concat ":" org-archive-tag ":"))
14029 bmp file re)
14030 (save-excursion
14031 (save-restriction
14032 (while (setq file (pop files))
14033 (catch 'nextfile
14034 (if (bufferp file)
14035 (set-buffer file)
14036 (org-check-agenda-file file)
14037 (set-buffer (org-get-agenda-file-buffer file)))
14038 (widen)
14039 (setq bmp (buffer-modified-p))
14040 (org-refresh-category-properties)
14041 (setq org-todo-keywords-for-agenda
14042 (append org-todo-keywords-for-agenda org-todo-keywords-1))
14043 (setq org-done-keywords-for-agenda
14044 (append org-done-keywords-for-agenda org-done-keywords))
14045 (setq org-todo-keyword-alist-for-agenda
14046 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
14047 (setq org-drawers-for-agenda
14048 (append org-drawers-for-agenda org-drawers))
14049 (setq org-tag-alist-for-agenda
14050 (append org-tag-alist-for-agenda org-tag-alist))
14052 (save-excursion
14053 (remove-text-properties (point-min) (point-max) pall)
14054 (when org-agenda-skip-archived-trees
14055 (goto-char (point-min))
14056 (while (re-search-forward rea nil t)
14057 (if (org-on-heading-p t)
14058 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
14059 (goto-char (point-min))
14060 (setq re (concat "^\\*+ +" org-comment-string "\\>"))
14061 (while (re-search-forward re nil t)
14062 (add-text-properties
14063 (match-beginning 0) (org-end-of-subtree t) pc)))
14064 (set-buffer-modified-p bmp)))))
14065 (setq org-todo-keyword-alist-for-agenda
14066 (org-uniquify org-todo-keyword-alist-for-agenda)
14067 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
14069 ;;;; Embedded LaTeX
14071 (defvar org-cdlatex-mode-map (make-sparse-keymap)
14072 "Keymap for the minor `org-cdlatex-mode'.")
14074 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
14075 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
14076 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
14077 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
14078 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
14080 (defvar org-cdlatex-texmathp-advice-is-done nil
14081 "Flag remembering if we have applied the advice to texmathp already.")
14083 (define-minor-mode org-cdlatex-mode
14084 "Toggle the minor `org-cdlatex-mode'.
14085 This mode supports entering LaTeX environment and math in LaTeX fragments
14086 in Org-mode.
14087 \\{org-cdlatex-mode-map}"
14088 nil " OCDL" nil
14089 (when org-cdlatex-mode (require 'cdlatex))
14090 (unless org-cdlatex-texmathp-advice-is-done
14091 (setq org-cdlatex-texmathp-advice-is-done t)
14092 (defadvice texmathp (around org-math-always-on activate)
14093 "Always return t in org-mode buffers.
14094 This is because we want to insert math symbols without dollars even outside
14095 the LaTeX math segments. If Orgmode thinks that point is actually inside
14096 an embedded LaTeX fragment, let texmathp do its job.
14097 \\[org-cdlatex-mode-map]"
14098 (interactive)
14099 (let (p)
14100 (cond
14101 ((not (org-mode-p)) ad-do-it)
14102 ((eq this-command 'cdlatex-math-symbol)
14103 (setq ad-return-value t
14104 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
14106 (let ((p (org-inside-LaTeX-fragment-p)))
14107 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
14108 (setq ad-return-value t
14109 texmathp-why '("Org-mode embedded math" . 0))
14110 (if p ad-do-it)))))))))
14112 (defun turn-on-org-cdlatex ()
14113 "Unconditionally turn on `org-cdlatex-mode'."
14114 (org-cdlatex-mode 1))
14116 (defun org-inside-LaTeX-fragment-p ()
14117 "Test if point is inside a LaTeX fragment.
14118 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
14119 sequence appearing also before point.
14120 Even though the matchers for math are configurable, this function assumes
14121 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
14122 delimiters are skipped when they have been removed by customization.
14123 The return value is nil, or a cons cell with the delimiter and
14124 and the position of this delimiter.
14126 This function does a reasonably good job, but can locally be fooled by
14127 for example currency specifications. For example it will assume being in
14128 inline math after \"$22.34\". The LaTeX fragment formatter will only format
14129 fragments that are properly closed, but during editing, we have to live
14130 with the uncertainty caused by missing closing delimiters. This function
14131 looks only before point, not after."
14132 (catch 'exit
14133 (let ((pos (point))
14134 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
14135 (lim (progn
14136 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
14137 (point)))
14138 dd-on str (start 0) m re)
14139 (goto-char pos)
14140 (when dodollar
14141 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
14142 re (nth 1 (assoc "$" org-latex-regexps)))
14143 (while (string-match re str start)
14144 (cond
14145 ((= (match-end 0) (length str))
14146 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
14147 ((= (match-end 0) (- (length str) 5))
14148 (throw 'exit nil))
14149 (t (setq start (match-end 0))))))
14150 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
14151 (goto-char pos)
14152 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
14153 (and (match-beginning 2) (throw 'exit nil))
14154 ;; count $$
14155 (while (re-search-backward "\\$\\$" lim t)
14156 (setq dd-on (not dd-on)))
14157 (goto-char pos)
14158 (if dd-on (cons "$$" m))))))
14161 (defun org-try-cdlatex-tab ()
14162 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
14163 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
14164 - inside a LaTeX fragment, or
14165 - after the first word in a line, where an abbreviation expansion could
14166 insert a LaTeX environment."
14167 (when org-cdlatex-mode
14168 (cond
14169 ((save-excursion
14170 (skip-chars-backward "a-zA-Z0-9*")
14171 (skip-chars-backward " \t")
14172 (bolp))
14173 (cdlatex-tab) t)
14174 ((org-inside-LaTeX-fragment-p)
14175 (cdlatex-tab) t)
14176 (t nil))))
14178 (defun org-cdlatex-underscore-caret (&optional arg)
14179 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
14180 Revert to the normal definition outside of these fragments."
14181 (interactive "P")
14182 (if (org-inside-LaTeX-fragment-p)
14183 (call-interactively 'cdlatex-sub-superscript)
14184 (let (org-cdlatex-mode)
14185 (call-interactively (key-binding (vector last-input-event))))))
14187 (defun org-cdlatex-math-modify (&optional arg)
14188 "Execute `cdlatex-math-modify' in LaTeX fragments.
14189 Revert to the normal definition outside of these fragments."
14190 (interactive "P")
14191 (if (org-inside-LaTeX-fragment-p)
14192 (call-interactively 'cdlatex-math-modify)
14193 (let (org-cdlatex-mode)
14194 (call-interactively (key-binding (vector last-input-event))))))
14196 (defvar org-latex-fragment-image-overlays nil
14197 "List of overlays carrying the images of latex fragments.")
14198 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
14200 (defun org-remove-latex-fragment-image-overlays ()
14201 "Remove all overlays with LaTeX fragment images in current buffer."
14202 (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
14203 (setq org-latex-fragment-image-overlays nil))
14205 (defun org-preview-latex-fragment (&optional subtree)
14206 "Preview the LaTeX fragment at point, or all locally or globally.
14207 If the cursor is in a LaTeX fragment, create the image and overlay
14208 it over the source code. If there is no fragment at point, display
14209 all fragments in the current text, from one headline to the next. With
14210 prefix SUBTREE, display all fragments in the current subtree. With a
14211 double prefix `C-u C-u', or when the cursor is before the first headline,
14212 display all fragments in the buffer.
14213 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
14214 (interactive "P")
14215 (org-remove-latex-fragment-image-overlays)
14216 (save-excursion
14217 (save-restriction
14218 (let (beg end at msg)
14219 (cond
14220 ((or (equal subtree '(16))
14221 (not (save-excursion
14222 (re-search-backward (concat "^" outline-regexp) nil t))))
14223 (setq beg (point-min) end (point-max)
14224 msg "Creating images for buffer...%s"))
14225 ((equal subtree '(4))
14226 (org-back-to-heading)
14227 (setq beg (point) end (org-end-of-subtree t)
14228 msg "Creating images for subtree...%s"))
14230 (if (setq at (org-inside-LaTeX-fragment-p))
14231 (goto-char (max (point-min) (- (cdr at) 2)))
14232 (org-back-to-heading))
14233 (setq beg (point) end (progn (outline-next-heading) (point))
14234 msg (if at "Creating image...%s"
14235 "Creating images for entry...%s"))))
14236 (message msg "")
14237 (narrow-to-region beg end)
14238 (goto-char beg)
14239 (org-format-latex
14240 (concat "ltxpng/" (file-name-sans-extension
14241 (file-name-nondirectory
14242 buffer-file-name)))
14243 default-directory 'overlays msg at 'forbuffer)
14244 (message msg "done. Use `C-c C-c' to remove images.")))))
14246 (defvar org-latex-regexps
14247 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
14248 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
14249 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
14250 ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
14251 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
14252 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
14253 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
14254 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
14255 "Regular expressions for matching embedded LaTeX.")
14257 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
14258 "Replace LaTeX fragments with links to an image, and produce images.
14259 Some of the options can be changed using the variable
14260 `org-format-latex-options'."
14261 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
14262 (let* ((prefixnodir (file-name-nondirectory prefix))
14263 (absprefix (expand-file-name prefix dir))
14264 (todir (file-name-directory absprefix))
14265 (opt org-format-latex-options)
14266 (matchers (plist-get opt :matchers))
14267 (re-list org-latex-regexps)
14268 (cnt 0) txt link beg end re e checkdir
14269 executables-checked
14270 m n block linkfile movefile ov)
14271 ;; Check if there are old images files with this prefix, and remove them
14272 (when (file-directory-p todir)
14273 (mapc 'delete-file
14274 (directory-files
14275 todir 'full
14276 (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
14277 ;; Check the different regular expressions
14278 (while (setq e (pop re-list))
14279 (setq m (car e) re (nth 1 e) n (nth 2 e)
14280 block (if (nth 3 e) "\n\n" ""))
14281 (when (member m matchers)
14282 (goto-char (point-min))
14283 (while (re-search-forward re nil t)
14284 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
14285 (not (get-text-property (match-beginning n)
14286 'org-protected))
14287 (or (not overlays)
14288 (not (eq (get-char-property (match-beginning n)
14289 'org-overlay-type)
14290 'org-latex-overlay))))
14291 (setq txt (match-string n)
14292 beg (match-beginning n) end (match-end n)
14293 cnt (1+ cnt)
14294 linkfile (format "%s_%04d.png" prefix cnt)
14295 movefile (format "%s_%04d.png" absprefix cnt)
14296 link (concat block "[[file:" linkfile "]]" block))
14297 (if msg (message msg cnt))
14298 (goto-char beg)
14299 (unless checkdir ; make sure the directory exists
14300 (setq checkdir t)
14301 (or (file-directory-p todir) (make-directory todir)))
14303 (unless executables-checked
14304 (org-check-external-command
14305 "latex" "needed to convert LaTeX fragments to images")
14306 (org-check-external-command
14307 "dvipng" "needed to convert LaTeX fragments to images")
14308 (setq executables-checked t))
14310 (org-create-formula-image
14311 txt movefile opt forbuffer)
14312 (if overlays
14313 (progn
14314 (mapc (lambda (o)
14315 (if (eq (org-overlay-get o 'org-overlay-type)
14316 'org-latex-overlay)
14317 (org-delete-overlay o)))
14318 (org-overlays-in beg end))
14319 (setq ov (org-make-overlay beg end))
14320 (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
14321 (if (featurep 'xemacs)
14322 (progn
14323 (org-overlay-put ov 'invisible t)
14324 (org-overlay-put
14325 ov 'end-glyph
14326 (make-glyph (vector 'png :file movefile))))
14327 (org-overlay-put
14328 ov 'display
14329 (list 'image :type 'png :file movefile :ascent 'center)))
14330 (push ov org-latex-fragment-image-overlays)
14331 (goto-char end))
14332 (delete-region beg end)
14333 (insert link))))))))
14335 (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
14336 ;; This function borrows from Ganesh Swami's latex2png.el
14337 (defun org-create-formula-image (string tofile options buffer)
14338 "This calls dvipng."
14339 (require 'org-latex)
14340 (let* ((tmpdir (if (featurep 'xemacs)
14341 (temp-directory)
14342 temporary-file-directory))
14343 (texfilebase (make-temp-name
14344 (expand-file-name "orgtex" tmpdir)))
14345 (texfile (concat texfilebase ".tex"))
14346 (dvifile (concat texfilebase ".dvi"))
14347 (pngfile (concat texfilebase ".png"))
14348 (fnh (if (featurep 'xemacs)
14349 (font-height (get-face-font 'default))
14350 (face-attribute 'default :height nil)))
14351 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
14352 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
14353 (fg (or (plist-get options (if buffer :foreground :html-foreground))
14354 "Black"))
14355 (bg (or (plist-get options (if buffer :background :html-background))
14356 "Transparent")))
14357 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
14358 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
14359 (with-temp-file texfile
14360 (insert org-format-latex-header
14361 (if org-export-latex-packages-alist
14362 (concat "\n"
14363 (mapconcat (lambda(p)
14364 (if (equal "" (car p))
14365 (format "\\usepackage{%s}" (cadr p))
14366 (format "\\usepackage[%s]{%s}"
14367 (car p) (cadr p))))
14368 org-export-latex-packages-alist "\n"))
14370 "\n\\begin{document}\n" string "\n\\end{document}\n"))
14371 (let ((dir default-directory))
14372 (condition-case nil
14373 (progn
14374 (cd tmpdir)
14375 (call-process "latex" nil nil nil texfile))
14376 (error nil))
14377 (cd dir))
14378 (if (not (file-exists-p dvifile))
14379 (progn (message "Failed to create dvi file from %s" texfile) nil)
14380 (condition-case nil
14381 (call-process "dvipng" nil nil nil
14382 "-fg" fg "-bg" bg
14383 "-D" dpi
14384 ;;"-x" scale "-y" scale
14385 "-T" "tight"
14386 "-o" pngfile
14387 dvifile)
14388 (error nil))
14389 (if (not (file-exists-p pngfile))
14390 (progn (message "Failed to create png file from %s" texfile) nil)
14391 ;; Use the requested file name and clean up
14392 (copy-file pngfile tofile 'replace)
14393 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
14394 (delete-file (concat texfilebase e)))
14395 pngfile))))
14397 (defun org-dvipng-color (attr)
14398 "Return an rgb color specification for dvipng."
14399 (apply 'format "rgb %s %s %s"
14400 (mapcar 'org-normalize-color
14401 (color-values (face-attribute 'default attr nil)))))
14403 (defun org-normalize-color (value)
14404 "Return string to be used as color value for an RGB component."
14405 (format "%g" (/ value 65535.0)))
14407 ;;;; Key bindings
14409 ;; Make `C-c C-x' a prefix key
14410 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
14412 ;; TAB key with modifiers
14413 (org-defkey org-mode-map "\C-i" 'org-cycle)
14414 (org-defkey org-mode-map [(tab)] 'org-cycle)
14415 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
14416 (org-defkey org-mode-map [(meta tab)] 'org-complete)
14417 (org-defkey org-mode-map "\M-\t" 'org-complete)
14418 (org-defkey org-mode-map "\M-\C-i" 'org-complete)
14419 ;; The following line is necessary under Suse GNU/Linux
14420 (unless (featurep 'xemacs)
14421 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
14422 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
14423 (define-key org-mode-map [backtab] 'org-shifttab)
14425 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
14426 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
14427 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
14429 ;; Cursor keys with modifiers
14430 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
14431 (org-defkey org-mode-map [(meta right)] 'org-metaright)
14432 (org-defkey org-mode-map [(meta up)] 'org-metaup)
14433 (org-defkey org-mode-map [(meta down)] 'org-metadown)
14435 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
14436 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
14437 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
14438 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
14440 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
14441 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
14442 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
14443 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
14445 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
14446 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
14448 ;;; Extra keys for tty access.
14449 ;; We only set them when really needed because otherwise the
14450 ;; menus don't show the simple keys
14452 (when (or org-use-extra-keys
14453 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
14454 (not window-system))
14455 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
14456 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
14457 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
14458 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
14459 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
14460 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
14461 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
14462 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
14463 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
14464 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
14465 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
14466 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
14467 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
14468 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
14469 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
14470 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
14471 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
14472 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
14473 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
14474 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
14475 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
14476 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
14477 (org-defkey org-mode-map [?\e (tab)] 'org-complete)
14478 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
14479 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
14480 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
14481 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
14482 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
14484 ;; All the other keys
14486 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
14487 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
14488 (if (boundp 'narrow-map)
14489 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
14490 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
14491 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
14492 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
14493 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
14494 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
14495 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
14496 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
14497 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
14498 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
14499 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
14500 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
14501 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
14502 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
14503 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
14504 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
14505 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
14506 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
14507 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
14508 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
14509 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
14510 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
14511 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
14512 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
14513 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
14514 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
14515 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
14516 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
14517 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
14518 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
14519 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
14520 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
14521 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
14522 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
14523 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
14524 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
14525 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
14526 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
14527 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
14528 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
14529 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
14530 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
14531 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
14532 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
14533 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
14534 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
14535 (org-defkey org-mode-map "\C-c^" 'org-sort)
14536 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
14537 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
14538 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
14539 (org-defkey org-mode-map "\C-m" 'org-return)
14540 (org-defkey org-mode-map "\C-j" 'org-return-indent)
14541 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
14542 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
14543 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
14544 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
14545 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
14546 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
14547 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
14548 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
14549 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
14550 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
14551 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
14552 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
14553 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
14554 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
14555 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
14556 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
14557 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
14558 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
14559 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
14560 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
14562 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
14563 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
14564 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
14565 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
14567 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
14568 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
14569 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
14570 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
14571 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
14572 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
14573 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
14574 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
14575 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
14576 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
14577 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
14578 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
14579 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
14580 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
14581 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
14583 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
14584 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
14585 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
14586 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
14588 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
14590 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
14592 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
14593 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
14595 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
14598 (when (featurep 'xemacs)
14599 (org-defkey org-mode-map 'button3 'popup-mode-menu))
14602 (defvar org-self-insert-command-undo-counter 0)
14604 (defvar org-table-auto-blank-field) ; defined in org-table.el
14605 (defun org-self-insert-command (N)
14606 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
14607 If the cursor is in a table looking at whitespace, the whitespace is
14608 overwritten, and the table is not marked as requiring realignment."
14609 (interactive "p")
14610 (if (and
14611 (org-table-p)
14612 (progn
14613 ;; check if we blank the field, and if that triggers align
14614 (and (featurep 'org-table) org-table-auto-blank-field
14615 (member last-command
14616 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
14617 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
14618 ;; got extra space, this field does not determine column width
14619 (let (org-table-may-need-update) (org-table-blank-field))
14620 ;; no extra space, this field may determine column width
14621 (org-table-blank-field)))
14623 (eq N 1)
14624 (looking-at "[^|\n]* |"))
14625 (let (org-table-may-need-update)
14626 (goto-char (1- (match-end 0)))
14627 (delete-backward-char 1)
14628 (goto-char (match-beginning 0))
14629 (self-insert-command N))
14630 (setq org-table-may-need-update t)
14631 (self-insert-command N)
14632 (org-fix-tags-on-the-fly)
14633 (if org-self-insert-cluster-for-undo
14634 (if (not (eq last-command 'org-self-insert-command))
14635 (setq org-self-insert-command-undo-counter 1)
14636 (if (>= org-self-insert-command-undo-counter 20)
14637 (setq org-self-insert-command-undo-counter 1)
14638 (and (> org-self-insert-command-undo-counter 0)
14639 buffer-undo-list
14640 (not (cadr buffer-undo-list)) ; remove nil entry
14641 (setcdr buffer-undo-list (cddr buffer-undo-list)))
14642 (setq org-self-insert-command-undo-counter
14643 (1+ org-self-insert-command-undo-counter)))))))
14645 (defun org-fix-tags-on-the-fly ()
14646 (when (and (equal (char-after (point-at-bol)) ?*)
14647 (org-on-heading-p))
14648 (org-align-tags-here org-tags-column)))
14650 (defun org-delete-backward-char (N)
14651 "Like `delete-backward-char', insert whitespace at field end in tables.
14652 When deleting backwards, in tables this function will insert whitespace in
14653 front of the next \"|\" separator, to keep the table aligned. The table will
14654 still be marked for re-alignment if the field did fill the entire column,
14655 because, in this case the deletion might narrow the column."
14656 (interactive "p")
14657 (if (and (org-table-p)
14658 (eq N 1)
14659 (string-match "|" (buffer-substring (point-at-bol) (point)))
14660 (looking-at ".*?|"))
14661 (let ((pos (point))
14662 (noalign (looking-at "[^|\n\r]* |"))
14663 (c org-table-may-need-update))
14664 (backward-delete-char N)
14665 (skip-chars-forward "^|")
14666 (insert " ")
14667 (goto-char (1- pos))
14668 ;; noalign: if there were two spaces at the end, this field
14669 ;; does not determine the width of the column.
14670 (if noalign (setq org-table-may-need-update c)))
14671 (backward-delete-char N)
14672 (org-fix-tags-on-the-fly)))
14674 (defun org-delete-char (N)
14675 "Like `delete-char', but insert whitespace at field end in tables.
14676 When deleting characters, in tables this function will insert whitespace in
14677 front of the next \"|\" separator, to keep the table aligned. The table will
14678 still be marked for re-alignment if the field did fill the entire column,
14679 because, in this case the deletion might narrow the column."
14680 (interactive "p")
14681 (if (and (org-table-p)
14682 (not (bolp))
14683 (not (= (char-after) ?|))
14684 (eq N 1))
14685 (if (looking-at ".*?|")
14686 (let ((pos (point))
14687 (noalign (looking-at "[^|\n\r]* |"))
14688 (c org-table-may-need-update))
14689 (replace-match (concat
14690 (substring (match-string 0) 1 -1)
14691 " |"))
14692 (goto-char pos)
14693 ;; noalign: if there were two spaces at the end, this field
14694 ;; does not determine the width of the column.
14695 (if noalign (setq org-table-may-need-update c)))
14696 (delete-char N))
14697 (delete-char N)
14698 (org-fix-tags-on-the-fly)))
14700 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
14701 (put 'org-self-insert-command 'delete-selection t)
14702 (put 'orgtbl-self-insert-command 'delete-selection t)
14703 (put 'org-delete-char 'delete-selection 'supersede)
14704 (put 'org-delete-backward-char 'delete-selection 'supersede)
14705 (put 'org-yank 'delete-selection 'yank)
14707 ;; Make `flyspell-mode' delay after some commands
14708 (put 'org-self-insert-command 'flyspell-delayed t)
14709 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
14710 (put 'org-delete-char 'flyspell-delayed t)
14711 (put 'org-delete-backward-char 'flyspell-delayed t)
14713 ;; Make pabbrev-mode expand after org-mode commands
14714 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
14715 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
14717 ;; How to do this: Measure non-white length of current string
14718 ;; If equal to column width, we should realign.
14720 (defun org-remap (map &rest commands)
14721 "In MAP, remap the functions given in COMMANDS.
14722 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
14723 (let (new old)
14724 (while commands
14725 (setq old (pop commands) new (pop commands))
14726 (if (fboundp 'command-remapping)
14727 (org-defkey map (vector 'remap old) new)
14728 (substitute-key-definition old new map global-map)))))
14730 (when (eq org-enable-table-editor 'optimized)
14731 ;; If the user wants maximum table support, we need to hijack
14732 ;; some standard editing functions
14733 (org-remap org-mode-map
14734 'self-insert-command 'org-self-insert-command
14735 'delete-char 'org-delete-char
14736 'delete-backward-char 'org-delete-backward-char)
14737 (org-defkey org-mode-map "|" 'org-force-self-insert))
14739 (defvar org-ctrl-c-ctrl-c-hook nil
14740 "Hook for functions attaching themselves to `C-c C-c'.
14741 This can be used to add additional functionality to the C-c C-c key which
14742 executes context-dependent commands.
14743 Each function will be called with no arguments. The function must check
14744 if the context is appropriate for it to act. If yes, it should do its
14745 thing and then return a non-nil value. If the context is wrong,
14746 just do nothing and return nil.")
14748 (defvar org-tab-first-hook nil
14749 "Hook for functions to attach themselves to TAB.
14750 See `org-ctrl-c-ctrl-c-hook' for more information.
14751 This hook runs as the first action when TAB is pressed, even before
14752 `org-cycle' messes around with the `outline-regexp' to cater for
14753 inline tasks and plain list item folding.
14754 If any function in this hook returns t, not other actions like table
14755 field motion visibility cycling will be done.")
14757 (defvar org-tab-after-check-for-table-hook nil
14758 "Hook for functions to attach themselves to TAB.
14759 See `org-ctrl-c-ctrl-c-hook' for more information.
14760 This hook runs after it has been established that the cursor is not in a
14761 table, but before checking if the cursor is in a headline or if global cycling
14762 should be done.
14763 If any function in this hook returns t, not other actions like visibility
14764 cycling will be done.")
14766 (defvar org-tab-after-check-for-cycling-hook nil
14767 "Hook for functions to attach themselves to TAB.
14768 See `org-ctrl-c-ctrl-c-hook' for more information.
14769 This hook runs after it has been established that not table field motion and
14770 not visibility should be done because of current context. This is probably
14771 the place where a package like yasnippets can hook in.")
14773 (defvar org-metaleft-hook nil
14774 "Hook for functions attaching themselves to `M-left'.
14775 See `org-ctrl-c-ctrl-c-hook' for more information.")
14776 (defvar org-metaright-hook nil
14777 "Hook for functions attaching themselves to `M-right'.
14778 See `org-ctrl-c-ctrl-c-hook' for more information.")
14779 (defvar org-metaup-hook nil
14780 "Hook for functions attaching themselves to `M-up'.
14781 See `org-ctrl-c-ctrl-c-hook' for more information.")
14782 (defvar org-metadown-hook nil
14783 "Hook for functions attaching themselves to `M-down'.
14784 See `org-ctrl-c-ctrl-c-hook' for more information.")
14785 (defvar org-shiftmetaleft-hook nil
14786 "Hook for functions attaching themselves to `M-S-left'.
14787 See `org-ctrl-c-ctrl-c-hook' for more information.")
14788 (defvar org-shiftmetaright-hook nil
14789 "Hook for functions attaching themselves to `M-S-right'.
14790 See `org-ctrl-c-ctrl-c-hook' for more information.")
14791 (defvar org-shiftmetaup-hook nil
14792 "Hook for functions attaching themselves to `M-S-up'.
14793 See `org-ctrl-c-ctrl-c-hook' for more information.")
14794 (defvar org-shiftmetadown-hook nil
14795 "Hook for functions attaching themselves to `M-S-down'.
14796 See `org-ctrl-c-ctrl-c-hook' for more information.")
14797 (defvar org-metareturn-hook nil
14798 "Hook for functions attaching themselves to `M-RET'.
14799 See `org-ctrl-c-ctrl-c-hook' for more information.")
14801 (defun org-modifier-cursor-error ()
14802 "Throw an error, a modified cursor command was applied in wrong context."
14803 (error "This command is active in special context like tables, headlines or items"))
14805 (defun org-shiftselect-error ()
14806 "Throw an error because Shift-Cursor command was applied in wrong context."
14807 (if (and (boundp 'shift-select-mode) shift-select-mode)
14808 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
14809 (error "This command works only in special context like headlines or timestamps")))
14811 (defun org-call-for-shift-select (cmd)
14812 (let ((this-command-keys-shift-translated t))
14813 (call-interactively cmd)))
14815 (defun org-shifttab (&optional arg)
14816 "Global visibility cycling or move to previous table field.
14817 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
14818 on context.
14819 See the individual commands for more information."
14820 (interactive "P")
14821 (cond
14822 ((org-at-table-p) (call-interactively 'org-table-previous-field))
14823 ((integerp arg)
14824 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
14825 (message "Content view to level: %d" arg)
14826 (org-content (prefix-numeric-value arg2))
14827 (setq org-cycle-global-status 'overview)))
14828 (t (call-interactively 'org-global-cycle))))
14830 (defun org-shiftmetaleft ()
14831 "Promote subtree or delete table column.
14832 Calls `org-promote-subtree', `org-outdent-item',
14833 or `org-table-delete-column', depending on context.
14834 See the individual commands for more information."
14835 (interactive)
14836 (cond
14837 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
14838 ((org-at-table-p) (call-interactively 'org-table-delete-column))
14839 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
14840 ((org-at-item-p) (call-interactively 'org-outdent-item))
14841 (t (org-modifier-cursor-error))))
14843 (defun org-shiftmetaright ()
14844 "Demote subtree or insert table column.
14845 Calls `org-demote-subtree', `org-indent-item',
14846 or `org-table-insert-column', depending on context.
14847 See the individual commands for more information."
14848 (interactive)
14849 (cond
14850 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
14851 ((org-at-table-p) (call-interactively 'org-table-insert-column))
14852 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
14853 ((org-at-item-p) (call-interactively 'org-indent-item))
14854 (t (org-modifier-cursor-error))))
14856 (defun org-shiftmetaup (&optional arg)
14857 "Move subtree up or kill table row.
14858 Calls `org-move-subtree-up' or `org-table-kill-row' or
14859 `org-move-item-up' depending on context. See the individual commands
14860 for more information."
14861 (interactive "P")
14862 (cond
14863 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
14864 ((org-at-table-p) (call-interactively 'org-table-kill-row))
14865 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
14866 ((org-at-item-p) (call-interactively 'org-move-item-up))
14867 (t (org-modifier-cursor-error))))
14869 (defun org-shiftmetadown (&optional arg)
14870 "Move subtree down or insert table row.
14871 Calls `org-move-subtree-down' or `org-table-insert-row' or
14872 `org-move-item-down', depending on context. See the individual
14873 commands for more information."
14874 (interactive "P")
14875 (cond
14876 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
14877 ((org-at-table-p) (call-interactively 'org-table-insert-row))
14878 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
14879 ((org-at-item-p) (call-interactively 'org-move-item-down))
14880 (t (org-modifier-cursor-error))))
14882 (defun org-metaleft (&optional arg)
14883 "Promote heading or move table column to left.
14884 Calls `org-do-promote' or `org-table-move-column', depending on context.
14885 With no specific context, calls the Emacs default `backward-word'.
14886 See the individual commands for more information."
14887 (interactive "P")
14888 (cond
14889 ((run-hook-with-args-until-success 'org-metaleft-hook))
14890 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
14891 ((or (org-on-heading-p)
14892 (and (org-region-active-p)
14893 (save-excursion
14894 (goto-char (region-beginning))
14895 (org-on-heading-p))))
14896 (call-interactively 'org-do-promote))
14897 ((or (org-at-item-p)
14898 (and (org-region-active-p)
14899 (save-excursion
14900 (goto-char (region-beginning))
14901 (org-at-item-p))))
14902 (call-interactively 'org-outdent-item))
14903 (t (call-interactively 'backward-word))))
14905 (defun org-metaright (&optional arg)
14906 "Demote subtree or move table column to right.
14907 Calls `org-do-demote' or `org-table-move-column', depending on context.
14908 With no specific context, calls the Emacs default `forward-word'.
14909 See the individual commands for more information."
14910 (interactive "P")
14911 (cond
14912 ((run-hook-with-args-until-success 'org-metaright-hook))
14913 ((org-at-table-p) (call-interactively 'org-table-move-column))
14914 ((or (org-on-heading-p)
14915 (and (org-region-active-p)
14916 (save-excursion
14917 (goto-char (region-beginning))
14918 (org-on-heading-p))))
14919 (call-interactively 'org-do-demote))
14920 ((or (org-at-item-p)
14921 (and (org-region-active-p)
14922 (save-excursion
14923 (goto-char (region-beginning))
14924 (org-at-item-p))))
14925 (call-interactively 'org-indent-item))
14926 (t (call-interactively 'forward-word))))
14928 (defun org-metaup (&optional arg)
14929 "Move subtree up or move table row up.
14930 Calls `org-move-subtree-up' or `org-table-move-row' or
14931 `org-move-item-up', depending on context. See the individual commands
14932 for more information."
14933 (interactive "P")
14934 (cond
14935 ((run-hook-with-args-until-success 'org-metaup-hook))
14936 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
14937 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
14938 ((org-at-item-p) (call-interactively 'org-move-item-up))
14939 (t (transpose-lines 1) (beginning-of-line -1))))
14941 (defun org-metadown (&optional arg)
14942 "Move subtree down or move table row down.
14943 Calls `org-move-subtree-down' or `org-table-move-row' or
14944 `org-move-item-down', depending on context. See the individual
14945 commands for more information."
14946 (interactive "P")
14947 (cond
14948 ((run-hook-with-args-until-success 'org-metadown-hook))
14949 ((org-at-table-p) (call-interactively 'org-table-move-row))
14950 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
14951 ((org-at-item-p) (call-interactively 'org-move-item-down))
14952 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
14954 (defun org-shiftup (&optional arg)
14955 "Increase item in timestamp or increase priority of current headline.
14956 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
14957 depending on context. See the individual commands for more information."
14958 (interactive "P")
14959 (cond
14960 ((and org-support-shift-select (org-region-active-p))
14961 (org-call-for-shift-select 'previous-line))
14962 ((org-at-timestamp-p t)
14963 (call-interactively (if org-edit-timestamp-down-means-later
14964 'org-timestamp-down 'org-timestamp-up)))
14965 ((and (not (eq org-support-shift-select 'always))
14966 org-enable-priority-commands
14967 (org-on-heading-p))
14968 (call-interactively 'org-priority-up))
14969 ((and (not org-support-shift-select) (org-at-item-p))
14970 (call-interactively 'org-previous-item))
14971 ((org-clocktable-try-shift 'up arg))
14972 (org-support-shift-select
14973 (org-call-for-shift-select 'previous-line))
14974 (t (org-shiftselect-error))))
14976 (defun org-shiftdown (&optional arg)
14977 "Decrease item in timestamp or decrease priority of current headline.
14978 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
14979 depending on context. See the individual commands for more information."
14980 (interactive "P")
14981 (cond
14982 ((and org-support-shift-select (org-region-active-p))
14983 (org-call-for-shift-select 'next-line))
14984 ((org-at-timestamp-p t)
14985 (call-interactively (if org-edit-timestamp-down-means-later
14986 'org-timestamp-up 'org-timestamp-down)))
14987 ((and (not (eq org-support-shift-select 'always))
14988 org-enable-priority-commands
14989 (org-on-heading-p))
14990 (call-interactively 'org-priority-down))
14991 ((and (not org-support-shift-select) (org-at-item-p))
14992 (call-interactively 'org-next-item))
14993 ((org-clocktable-try-shift 'down arg))
14994 (org-support-shift-select
14995 (org-call-for-shift-select 'next-line))
14996 (t (org-shiftselect-error))))
14998 (defun org-shiftright (&optional arg)
14999 "Cycle the thing at point or in the current line, depending on context.
15000 Depending on context, this does one of the following:
15002 - switch a timestamp at point one day into the future
15003 - on a headline, switch to the next TODO keyword.
15004 - on an item, switch entire list to the next bullet type
15005 - on a property line, switch to the next allowed value
15006 - on a clocktable definition line, move time block into the future"
15007 (interactive "P")
15008 (cond
15009 ((and org-support-shift-select (org-region-active-p))
15010 (org-call-for-shift-select 'forward-char))
15011 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
15012 ((and (not (eq org-support-shift-select 'always))
15013 (org-on-heading-p))
15014 (let ((org-inhibit-logging
15015 (not org-treat-S-cursor-todo-selection-as-state-change))
15016 (org-inhibit-blocking
15017 (not org-treat-S-cursor-todo-selection-as-state-change)))
15018 (org-call-with-arg 'org-todo 'right)))
15019 ((or (and org-support-shift-select
15020 (not (eq org-support-shift-select 'always))
15021 (org-at-item-bullet-p))
15022 (and (not org-support-shift-select) (org-at-item-p)))
15023 (org-call-with-arg 'org-cycle-list-bullet nil))
15024 ((and (not (eq org-support-shift-select 'always))
15025 (org-at-property-p))
15026 (call-interactively 'org-property-next-allowed-value))
15027 ((org-clocktable-try-shift 'right arg))
15028 (org-support-shift-select
15029 (org-call-for-shift-select 'forward-char))
15030 (t (org-shiftselect-error))))
15032 (defun org-shiftleft (&optional arg)
15033 "Cycle the thing at point or in the current line, depending on context.
15034 Depending on context, this does one of the following:
15036 - switch a timestamp at point one day into the past
15037 - on a headline, switch to the previous TODO keyword.
15038 - on an item, switch entire list to the previous bullet type
15039 - on a property line, switch to the previous allowed value
15040 - on a clocktable definition line, move time block into the past"
15041 (interactive "P")
15042 (cond
15043 ((and org-support-shift-select (org-region-active-p))
15044 (org-call-for-shift-select 'backward-char))
15045 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
15046 ((and (not (eq org-support-shift-select 'always))
15047 (org-on-heading-p))
15048 (let ((org-inhibit-logging
15049 (not org-treat-S-cursor-todo-selection-as-state-change))
15050 (org-inhibit-blocking
15051 (not org-treat-S-cursor-todo-selection-as-state-change)))
15052 (org-call-with-arg 'org-todo 'left)))
15053 ((or (and org-support-shift-select
15054 (not (eq org-support-shift-select 'always))
15055 (org-at-item-bullet-p))
15056 (and (not org-support-shift-select) (org-at-item-p)))
15057 (org-call-with-arg 'org-cycle-list-bullet 'previous))
15058 ((and (not (eq org-support-shift-select 'always))
15059 (org-at-property-p))
15060 (call-interactively 'org-property-previous-allowed-value))
15061 ((org-clocktable-try-shift 'left arg))
15062 (org-support-shift-select
15063 (org-call-for-shift-select 'backward-char))
15064 (t (org-shiftselect-error))))
15066 (defun org-shiftcontrolright ()
15067 "Switch to next TODO set."
15068 (interactive)
15069 (cond
15070 ((and org-support-shift-select (org-region-active-p))
15071 (org-call-for-shift-select 'forward-word))
15072 ((and (not (eq org-support-shift-select 'always))
15073 (org-on-heading-p))
15074 (org-call-with-arg 'org-todo 'nextset))
15075 (org-support-shift-select
15076 (org-call-for-shift-select 'forward-word))
15077 (t (org-shiftselect-error))))
15079 (defun org-shiftcontrolleft ()
15080 "Switch to previous TODO set."
15081 (interactive)
15082 (cond
15083 ((and org-support-shift-select (org-region-active-p))
15084 (org-call-for-shift-select 'backward-word))
15085 ((and (not (eq org-support-shift-select 'always))
15086 (org-on-heading-p))
15087 (org-call-with-arg 'org-todo 'previousset))
15088 (org-support-shift-select
15089 (org-call-for-shift-select 'backward-word))
15090 (t (org-shiftselect-error))))
15092 (defun org-ctrl-c-ret ()
15093 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
15094 (interactive)
15095 (cond
15096 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
15097 (t (call-interactively 'org-insert-heading))))
15099 (defun org-copy-special ()
15100 "Copy region in table or copy current subtree.
15101 Calls `org-table-copy' or `org-copy-subtree', depending on context.
15102 See the individual commands for more information."
15103 (interactive)
15104 (call-interactively
15105 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
15107 (defun org-cut-special ()
15108 "Cut region in table or cut current subtree.
15109 Calls `org-table-copy' or `org-cut-subtree', depending on context.
15110 See the individual commands for more information."
15111 (interactive)
15112 (call-interactively
15113 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
15115 (defun org-paste-special (arg)
15116 "Paste rectangular region into table, or past subtree relative to level.
15117 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
15118 See the individual commands for more information."
15119 (interactive "P")
15120 (if (org-at-table-p)
15121 (org-table-paste-rectangle)
15122 (org-paste-subtree arg)))
15124 (defun org-edit-special ()
15125 "Call a special editor for the stuff at point.
15126 When at a table, call the formula editor with `org-table-edit-formulas'.
15127 When at the first line of an src example, call `org-edit-src-code'.
15128 When in an #+include line, visit the include file. Otherwise call
15129 `ffap' to visit the file at point."
15130 (interactive)
15131 (cond
15132 ((org-at-table-p)
15133 (call-interactively 'org-table-edit-formulas))
15134 ((save-excursion
15135 (beginning-of-line 1)
15136 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
15137 (find-file (org-trim (match-string 1))))
15138 ((org-edit-src-code))
15139 ((org-edit-fixed-width-region))
15140 (t (call-interactively 'ffap))))
15143 (defun org-ctrl-c-ctrl-c (&optional arg)
15144 "Set tags in headline, or update according to changed information at point.
15146 This command does many different things, depending on context:
15148 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
15149 this is what we do.
15151 - If the cursor is on a statistics cookie, update it.
15153 - If the cursor is in a headline, prompt for tags and insert them
15154 into the current line, aligned to `org-tags-column'. When called
15155 with prefix arg, realign all tags in the current buffer.
15157 - If the cursor is in one of the special #+KEYWORD lines, this
15158 triggers scanning the buffer for these lines and updating the
15159 information.
15161 - If the cursor is inside a table, realign the table. This command
15162 works even if the automatic table editor has been turned off.
15164 - If the cursor is on a #+TBLFM line, re-apply the formulas to
15165 the entire table.
15167 - If the cursor is at a footnote reference or definition, jump to
15168 the corresponding definition or references, respectively.
15170 - If the cursor is a the beginning of a dynamic block, update it.
15172 - If the cursor is inside a table created by the table.el package,
15173 activate that table.
15175 - If the current buffer is a remember buffer, close note and file
15176 it. A prefix argument of 1 files to the default location
15177 without further interaction. A prefix argument of 2 files to
15178 the currently clocking task.
15180 - If the cursor is on a <<<target>>>, update radio targets and corresponding
15181 links in this buffer.
15183 - If the cursor is on a numbered item in a plain list, renumber the
15184 ordered list.
15186 - If the cursor is on a checkbox, toggle it."
15187 (interactive "P")
15188 (let ((org-enable-table-editor t))
15189 (cond
15190 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
15191 org-occur-highlights
15192 org-latex-fragment-image-overlays)
15193 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
15194 (org-remove-occur-highlights)
15195 (org-remove-latex-fragment-image-overlays)
15196 (message "Temporary highlights/overlays removed from current buffer"))
15197 ((and (local-variable-p 'org-finish-function (current-buffer))
15198 (fboundp org-finish-function))
15199 (funcall org-finish-function))
15200 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
15201 ((org-at-property-p)
15202 (call-interactively 'org-property-action))
15203 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
15204 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
15205 (or (org-on-heading-p) (org-at-item-p)))
15206 (call-interactively 'org-update-statistics-cookies))
15207 ((org-on-heading-p) (call-interactively 'org-set-tags))
15208 ((org-at-table.el-p)
15209 (require 'table)
15210 (beginning-of-line 1)
15211 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
15212 (call-interactively 'table-recognize-table))
15213 ((org-at-table-p)
15214 (org-table-maybe-eval-formula)
15215 (if arg
15216 (call-interactively 'org-table-recalculate)
15217 (org-table-maybe-recalculate-line))
15218 (call-interactively 'org-table-align))
15219 ((or (org-footnote-at-reference-p)
15220 (org-footnote-at-definition-p))
15221 (call-interactively 'org-footnote-action))
15222 ((org-at-item-checkbox-p)
15223 (call-interactively 'org-toggle-checkbox))
15224 ((org-at-item-p)
15225 (if arg
15226 (call-interactively 'org-toggle-checkbox)
15227 (call-interactively 'org-maybe-renumber-ordered-list)))
15228 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
15229 ;; Dynamic block
15230 (beginning-of-line 1)
15231 (save-excursion (org-update-dblock)))
15232 ((save-excursion
15233 (beginning-of-line 1)
15234 (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
15235 (cond
15236 ((equal (match-string 1) "TBLFM")
15237 ;; Recalculate the table before this line
15238 (save-excursion
15239 (beginning-of-line 1)
15240 (skip-chars-backward " \r\n\t")
15241 (if (org-at-table-p)
15242 (org-call-with-arg 'org-table-recalculate (or arg t)))))
15244 ; (org-set-regexps-and-options)
15245 ; (org-restart-font-lock)
15246 (let ((org-inhibit-startup t)) (org-mode-restart))
15247 (message "Local setup has been refreshed"))))
15248 ((org-clock-update-time-maybe))
15249 (t (error "C-c C-c can do nothing useful at this location")))))
15251 (defun org-mode-restart ()
15252 "Restart Org-mode, to scan again for special lines.
15253 Also updates the keyword regular expressions."
15254 (interactive)
15255 (org-mode)
15256 (message "Org-mode restarted"))
15258 (defun org-kill-note-or-show-branches ()
15259 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
15260 (interactive)
15261 (if (not org-finish-function)
15262 (call-interactively 'show-branches)
15263 (let ((org-note-abort t))
15264 (funcall org-finish-function))))
15266 (defun org-return (&optional indent)
15267 "Goto next table row or insert a newline.
15268 Calls `org-table-next-row' or `newline', depending on context.
15269 See the individual commands for more information."
15270 (interactive)
15271 (cond
15272 ((bobp) (if indent (newline-and-indent) (newline)))
15273 ((org-at-table-p)
15274 (org-table-justify-field-maybe)
15275 (call-interactively 'org-table-next-row))
15276 ((and org-return-follows-link
15277 (eq (get-text-property (point) 'face) 'org-link))
15278 (call-interactively 'org-open-at-point))
15279 ((and (org-at-heading-p)
15280 (looking-at
15281 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
15282 (org-show-entry)
15283 (end-of-line 1)
15284 (newline))
15285 (t (if indent (newline-and-indent) (newline)))))
15287 (defun org-return-indent ()
15288 "Goto next table row or insert a newline and indent.
15289 Calls `org-table-next-row' or `newline-and-indent', depending on
15290 context. See the individual commands for more information."
15291 (interactive)
15292 (org-return t))
15294 (defun org-ctrl-c-star ()
15295 "Compute table, or change heading status of lines.
15296 Calls `org-table-recalculate' or `org-toggle-heading',
15297 depending on context."
15298 (interactive)
15299 (cond
15300 ((org-at-table-p)
15301 (call-interactively 'org-table-recalculate))
15303 ;; Convert all lines in region to list items
15304 (call-interactively 'org-toggle-heading))))
15306 (defun org-ctrl-c-minus ()
15307 "Insert separator line in table or modify bullet status of line.
15308 Also turns a plain line or a region of lines into list items.
15309 Calls `org-table-insert-hline', `org-toggle-item', or
15310 `org-cycle-list-bullet', depending on context."
15311 (interactive)
15312 (cond
15313 ((org-at-table-p)
15314 (call-interactively 'org-table-insert-hline))
15315 ((org-region-active-p)
15316 (call-interactively 'org-toggle-item))
15317 ((org-in-item-p)
15318 (call-interactively 'org-cycle-list-bullet))
15320 (call-interactively 'org-toggle-item))))
15322 (defun org-toggle-item ()
15323 "Convert headings or normal lines to items, items to normal lines.
15324 If there is no active region, only the current line is considered.
15326 If the first line in the region is a headline, convert all headlines to items.
15328 If the first line in the region is an item, convert all items to normal lines.
15330 If the first line is normal text, add an item bullet to each line."
15331 (interactive)
15332 (let (l2 l beg end)
15333 (if (org-region-active-p)
15334 (setq beg (region-beginning) end (region-end))
15335 (setq beg (point-at-bol)
15336 end (min (1+ (point-at-eol)) (point-max))))
15337 (save-excursion
15338 (goto-char end)
15339 (setq l2 (org-current-line))
15340 (goto-char beg)
15341 (beginning-of-line 1)
15342 (setq l (1- (org-current-line)))
15343 (if (org-at-item-p)
15344 ;; We already have items, de-itemize
15345 (while (< (setq l (1+ l)) l2)
15346 (when (org-at-item-p)
15347 (goto-char (match-beginning 2))
15348 (delete-region (match-beginning 2) (match-end 2))
15349 (and (looking-at "[ \t]+") (replace-match "")))
15350 (beginning-of-line 2))
15351 (if (org-on-heading-p)
15352 ;; Headings, convert to items
15353 (while (< (setq l (1+ l)) l2)
15354 (if (looking-at org-outline-regexp)
15355 (replace-match "- " t t))
15356 (beginning-of-line 2))
15357 ;; normal lines, turn them into items
15358 (while (< (setq l (1+ l)) l2)
15359 (unless (org-at-item-p)
15360 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
15361 (replace-match "\\1- \\2")))
15362 (beginning-of-line 2)))))))
15364 (defun org-toggle-heading (&optional nstars)
15365 "Convert headings to normal text, or items or text to headings.
15366 If there is no active region, only the current line is considered.
15368 If the first line is a heading, remove the stars from all headlines
15369 in the region.
15371 If the first line is a plain list item, turn all plain list items
15372 into headings.
15374 If the first line is a normal line, turn each and every line in the
15375 region into a heading.
15377 When converting a line into a heading, the number of stars is chosen
15378 such that the lines become children of the current entry. However,
15379 when a prefix argument is given, its value determines the number of
15380 stars to add."
15381 (interactive "P")
15382 (let (l2 l itemp beg end)
15383 (if (org-region-active-p)
15384 (setq beg (region-beginning) end (region-end))
15385 (setq beg (point-at-bol)
15386 end (min (1+ (point-at-eol)) (point-max))))
15387 (save-excursion
15388 (goto-char end)
15389 (setq l2 (org-current-line))
15390 (goto-char beg)
15391 (beginning-of-line 1)
15392 (setq l (1- (org-current-line)))
15393 (if (org-on-heading-p)
15394 ;; We already have headlines, de-star them
15395 (while (< (setq l (1+ l)) l2)
15396 (when (org-on-heading-p t)
15397 (and (looking-at outline-regexp) (replace-match "")))
15398 (beginning-of-line 2))
15399 (setq itemp (org-at-item-p))
15400 (let* ((stars
15401 (if nstars
15402 (make-string (prefix-numeric-value current-prefix-arg)
15404 (save-excursion
15405 (if (re-search-backward org-complex-heading-regexp nil t)
15406 (match-string 1) ""))))
15407 (add-stars (cond (nstars "")
15408 ((equal stars "") "*")
15409 (org-odd-levels-only "**")
15410 (t "*")))
15411 (rpl (concat stars add-stars " ")))
15412 (while (< (setq l (1+ l)) l2)
15413 (if itemp
15414 (and (org-at-item-p) (replace-match rpl t t))
15415 (unless (org-on-heading-p)
15416 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
15417 (replace-match (concat rpl (match-string 2))))))
15418 (beginning-of-line 2)))))))
15420 (defun org-meta-return (&optional arg)
15421 "Insert a new heading or wrap a region in a table.
15422 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
15423 See the individual commands for more information."
15424 (interactive "P")
15425 (cond
15426 ((run-hook-with-args-until-success 'org-metareturn-hook))
15427 ((org-at-table-p)
15428 (call-interactively 'org-table-wrap-region))
15429 (t (call-interactively 'org-insert-heading))))
15431 ;;; Menu entries
15433 ;; Define the Org-mode menus
15434 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
15435 '("Tbl"
15436 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
15437 ["Next Field" org-cycle (org-at-table-p)]
15438 ["Previous Field" org-shifttab (org-at-table-p)]
15439 ["Next Row" org-return (org-at-table-p)]
15440 "--"
15441 ["Blank Field" org-table-blank-field (org-at-table-p)]
15442 ["Edit Field" org-table-edit-field (org-at-table-p)]
15443 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
15444 "--"
15445 ("Column"
15446 ["Move Column Left" org-metaleft (org-at-table-p)]
15447 ["Move Column Right" org-metaright (org-at-table-p)]
15448 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
15449 ["Insert Column" org-shiftmetaright (org-at-table-p)])
15450 ("Row"
15451 ["Move Row Up" org-metaup (org-at-table-p)]
15452 ["Move Row Down" org-metadown (org-at-table-p)]
15453 ["Delete Row" org-shiftmetaup (org-at-table-p)]
15454 ["Insert Row" org-shiftmetadown (org-at-table-p)]
15455 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
15456 "--"
15457 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
15458 ("Rectangle"
15459 ["Copy Rectangle" org-copy-special (org-at-table-p)]
15460 ["Cut Rectangle" org-cut-special (org-at-table-p)]
15461 ["Paste Rectangle" org-paste-special (org-at-table-p)]
15462 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
15463 "--"
15464 ("Calculate"
15465 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
15466 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
15467 ["Edit Formulas" org-edit-special (org-at-table-p)]
15468 "--"
15469 ["Recalculate line" org-table-recalculate (org-at-table-p)]
15470 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
15471 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
15472 "--"
15473 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
15474 "--"
15475 ["Sum Column/Rectangle" org-table-sum
15476 (or (org-at-table-p) (org-region-active-p))]
15477 ["Which Column?" org-table-current-column (org-at-table-p)])
15478 ["Debug Formulas"
15479 org-table-toggle-formula-debugger
15480 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
15481 ["Show Col/Row Numbers"
15482 org-table-toggle-coordinate-overlays
15483 :style toggle
15484 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
15485 "--"
15486 ["Create" org-table-create (and (not (org-at-table-p))
15487 org-enable-table-editor)]
15488 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
15489 ["Import from File" org-table-import (not (org-at-table-p))]
15490 ["Export to File" org-table-export (org-at-table-p)]
15491 "--"
15492 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
15494 (easy-menu-define org-org-menu org-mode-map "Org menu"
15495 '("Org"
15496 ("Show/Hide"
15497 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
15498 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
15499 ["Sparse Tree..." org-sparse-tree t]
15500 ["Reveal Context" org-reveal t]
15501 ["Show All" show-all t]
15502 "--"
15503 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
15504 "--"
15505 ["New Heading" org-insert-heading t]
15506 ("Navigate Headings"
15507 ["Up" outline-up-heading t]
15508 ["Next" outline-next-visible-heading t]
15509 ["Previous" outline-previous-visible-heading t]
15510 ["Next Same Level" outline-forward-same-level t]
15511 ["Previous Same Level" outline-backward-same-level t]
15512 "--"
15513 ["Jump" org-goto t])
15514 ("Edit Structure"
15515 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
15516 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
15517 "--"
15518 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
15519 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
15520 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
15521 "--"
15522 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
15523 "--"
15524 ["Promote Heading" org-metaleft (not (org-at-table-p))]
15525 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
15526 ["Demote Heading" org-metaright (not (org-at-table-p))]
15527 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
15528 "--"
15529 ["Sort Region/Children" org-sort (not (org-at-table-p))]
15530 "--"
15531 ["Convert to odd levels" org-convert-to-odd-levels t]
15532 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
15533 ("Editing"
15534 ["Emphasis..." org-emphasize t]
15535 ["Edit Source Example" org-edit-special t]
15536 "--"
15537 ["Footnote new/jump" org-footnote-action t]
15538 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
15539 ("Archive"
15540 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
15541 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
15542 ; :active t :keys "C-u C-c C-x C-a"]
15543 ["Sparse trees open ARCHIVE trees"
15544 (setq org-sparse-tree-open-archived-trees
15545 (not org-sparse-tree-open-archived-trees))
15546 :style toggle :selected org-sparse-tree-open-archived-trees]
15547 ["Cycling opens ARCHIVE trees"
15548 (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
15549 :style toggle :selected org-cycle-open-archived-trees]
15550 "--"
15551 ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
15552 ["Move Subtree to Archive" org-advertized-archive-subtree t]
15553 ; ["Check and Move Children" (org-archive-subtree '(4))
15554 ; :active t :keys "C-u C-c C-x C-s"]
15556 "--"
15557 ("Hyperlinks"
15558 ["Store Link (Global)" org-store-link t]
15559 ["Find existing link to here" org-occur-link-in-agenda-files t]
15560 ["Insert Link" org-insert-link t]
15561 ["Follow Link" org-open-at-point t]
15562 "--"
15563 ["Next link" org-next-link t]
15564 ["Previous link" org-previous-link t]
15565 "--"
15566 ["Descriptive Links"
15567 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
15568 :style radio
15569 :selected (member '(org-link) buffer-invisibility-spec)]
15570 ["Literal Links"
15571 (progn
15572 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
15573 :style radio
15574 :selected (not (member '(org-link) buffer-invisibility-spec))])
15575 "--"
15576 ("TODO Lists"
15577 ["TODO/DONE/-" org-todo t]
15578 ("Select keyword"
15579 ["Next keyword" org-shiftright (org-on-heading-p)]
15580 ["Previous keyword" org-shiftleft (org-on-heading-p)]
15581 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
15582 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
15583 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
15584 ["Show TODO Tree" org-show-todo-tree t]
15585 ["Global TODO list" org-todo-list t]
15586 "--"
15587 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
15588 :selected org-enforce-todo-dependencies :style toggle :active t]
15589 "Settings for tree at point"
15590 ["Do Children sequentially" org-toggle-ordered-property :style radio
15591 :selected (ignore-errors (org-entry-get nil "ORDERED"))
15592 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
15593 ["Do Children parallel" org-toggle-ordered-property :style radio
15594 :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
15595 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
15596 "--"
15597 ["Set Priority" org-priority t]
15598 ["Priority Up" org-shiftup t]
15599 ["Priority Down" org-shiftdown t]
15600 "--"
15601 ["Get news from all feeds" org-feed-update-all t]
15602 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
15603 ["Customize feeds" (customize-variable 'org-feed-alist) t])
15604 ("TAGS and Properties"
15605 ["Set Tags" org-set-tags-command t]
15606 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
15607 "--"
15608 ["Set property" org-set-property t]
15609 ["Column view of properties" org-columns t]
15610 ["Insert Column View DBlock" org-insert-columns-dblock t])
15611 ("Dates and Scheduling"
15612 ["Timestamp" org-time-stamp t]
15613 ["Timestamp (inactive)" org-time-stamp-inactive t]
15614 ("Change Date"
15615 ["1 Day Later" org-shiftright t]
15616 ["1 Day Earlier" org-shiftleft t]
15617 ["1 ... Later" org-shiftup t]
15618 ["1 ... Earlier" org-shiftdown t])
15619 ["Compute Time Range" org-evaluate-time-range t]
15620 ["Schedule Item" org-schedule t]
15621 ["Deadline" org-deadline t]
15622 "--"
15623 ["Custom time format" org-toggle-time-stamp-overlays
15624 :style radio :selected org-display-custom-times]
15625 "--"
15626 ["Goto Calendar" org-goto-calendar t]
15627 ["Date from Calendar" org-date-from-calendar t]
15628 "--"
15629 ["Start/Restart Timer" org-timer-start t]
15630 ["Pause/Continue Timer" org-timer-pause-or-continue t]
15631 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
15632 ["Insert Timer String" org-timer t]
15633 ["Insert Timer Item" org-timer-item t])
15634 ("Logging work"
15635 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
15636 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
15637 ["Clock out" org-clock-out t]
15638 ["Clock cancel" org-clock-cancel t]
15639 "--"
15640 ["Mark as default task" org-clock-mark-default-task t]
15641 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
15642 ["Goto running clock" org-clock-goto t]
15643 "--"
15644 ["Display times" org-clock-display t]
15645 ["Create clock table" org-clock-report t]
15646 "--"
15647 ["Record DONE time"
15648 (progn (setq org-log-done (not org-log-done))
15649 (message "Switching to %s will %s record a timestamp"
15650 (car org-done-keywords)
15651 (if org-log-done "automatically" "not")))
15652 :style toggle :selected org-log-done])
15653 "--"
15654 ["Agenda Command..." org-agenda t]
15655 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
15656 ("File List for Agenda")
15657 ("Special views current file"
15658 ["TODO Tree" org-show-todo-tree t]
15659 ["Check Deadlines" org-check-deadlines t]
15660 ["Timeline" org-timeline t]
15661 ["Tags/Property tree" org-match-sparse-tree t])
15662 "--"
15663 ["Export/Publish..." org-export t]
15664 ("LaTeX"
15665 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
15666 :selected org-cdlatex-mode]
15667 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
15668 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
15669 ["Modify math symbol" org-cdlatex-math-modify
15670 (org-inside-LaTeX-fragment-p)]
15671 ["Insert citation" org-reftex-citation t]
15672 "--"
15673 ["Export LaTeX fragments as images"
15674 (if (featurep 'org-exp)
15675 (setq org-export-with-LaTeX-fragments
15676 (not org-export-with-LaTeX-fragments))
15677 (require 'org-exp))
15678 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
15679 org-export-with-LaTeX-fragments)])
15680 "--"
15681 ("MobileOrg"
15682 ["Push Files and Views" org-mobile-push t]
15683 ["Get Captured and Flagged" org-mobile-pull t]
15684 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
15685 "--"
15686 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
15687 "--"
15688 ("Documentation"
15689 ["Show Version" org-version t]
15690 ["Info Documentation" org-info t])
15691 ("Customize"
15692 ["Browse Org Group" org-customize t]
15693 "--"
15694 ["Expand This Menu" org-create-customize-menu
15695 (fboundp 'customize-menu-create)])
15696 ["Send bug report" org-submit-bug-report t]
15697 "--"
15698 ("Refresh/Reload"
15699 ["Refresh setup current buffer" org-mode-restart t]
15700 ["Reload Org (after update)" org-reload t]
15701 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
15704 (defun org-info (&optional node)
15705 "Read documentation for Org-mode in the info system.
15706 With optional NODE, go directly to that node."
15707 (interactive)
15708 (info (format "(org)%s" (or node ""))))
15710 ;;;###autoload
15711 (defun org-submit-bug-report ()
15712 "Submit a bug report on Org-mode via mail.
15714 Don't hesitate to report any problems or inaccurate documentation.
15716 If you don't have setup sending mail from (X)Emacs, please copy the
15717 output buffer into your mail program, as it gives us important
15718 information about your Org-mode version and configuration."
15719 (interactive)
15720 (require 'reporter)
15721 (org-load-modules-maybe)
15722 (org-require-autoloaded-modules)
15723 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
15724 (reporter-submit-bug-report
15725 "emacs-orgmode@gnu.org"
15726 (org-version)
15727 (let (list)
15728 (save-window-excursion
15729 (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
15730 (delete-other-windows)
15731 (erase-buffer)
15732 (insert "You are about to submit a bug report to the Org-mode mailing list.
15734 We would like to add your full Org-mode and Outline configuration to the
15735 bug report. This greatly simplifies the work of the maintainer and
15736 other experts on the mailing list.
15738 HOWEVER, some variables you have customized may contain private
15739 information. The names of customers, colleagues, or friends, might
15740 appear in the form of file names, tags, todo states, or search strings.
15741 If you answer yes to the prompt, you might want to check and remove
15742 such private information before sending the email.")
15743 (add-text-properties (point-min) (point-max) '(face org-warning))
15744 (when (yes-or-no-p "Include your Org-mode configuration ")
15745 (mapatoms
15746 (lambda (v)
15747 (and (boundp v)
15748 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
15749 (or (and (symbol-value v)
15750 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
15751 (and
15752 (get v 'custom-type) (get v 'standard-value)
15753 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
15754 (push v list)))))
15755 (kill-buffer (get-buffer "*Warn about privacy*"))
15756 list))
15757 nil nil
15758 "Remember to cover the basics, that is, what you expected to happen and
15759 what in fact did happen. You don't know how to make a good report? See
15761 http://orgmode.org/manual/Feedback.html#Feedback
15763 Your bug report will be posted to the Org-mode mailing list.
15764 ------------------------------------------------------------------------")))
15766 (defun org-install-agenda-files-menu ()
15767 (let ((bl (buffer-list)))
15768 (save-excursion
15769 (while bl
15770 (set-buffer (pop bl))
15771 (if (org-mode-p) (setq bl nil)))
15772 (when (org-mode-p)
15773 (easy-menu-change
15774 '("Org") "File List for Agenda"
15775 (append
15776 (list
15777 ["Edit File List" (org-edit-agenda-file-list) t]
15778 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
15779 ["Remove Current File from List" org-remove-file t]
15780 ["Cycle through agenda files" org-cycle-agenda-files t]
15781 ["Occur in all agenda files" org-occur-in-agenda-files t]
15782 "--")
15783 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
15785 ;;;; Documentation
15787 ;;;###autoload
15788 (defun org-require-autoloaded-modules ()
15789 (interactive)
15790 (mapc 'require
15791 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
15792 org-docbook org-exp org-html org-icalendar
15793 org-id org-latex
15794 org-publish org-remember org-table
15795 org-timer org-xoxo)))
15797 ;;;###autoload
15798 (defun org-reload (&optional uncompiled)
15799 "Reload all org lisp files.
15800 With prefix arg UNCOMPILED, load the uncompiled versions."
15801 (interactive "P")
15802 (require 'find-func)
15803 (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
15804 (dir-org (file-name-directory (org-find-library-name "org")))
15805 (dir-org-contrib (ignore-errors
15806 (file-name-directory
15807 (org-find-library-name "org-contribdir"))))
15808 (files
15809 (append (directory-files dir-org t file-re)
15810 (and dir-org-contrib
15811 (directory-files dir-org-contrib t file-re))))
15812 (remove-re (concat (if (featurep 'xemacs)
15813 "org-colview" "org-colview-xemacs")
15814 "\\'")))
15815 (setq files (mapcar 'file-name-sans-extension files))
15816 (setq files (mapcar
15817 (lambda (x) (if (string-match remove-re x) nil x))
15818 files))
15819 (setq files (delq nil files))
15820 (mapc
15821 (lambda (f)
15822 (when (featurep (intern (file-name-nondirectory f)))
15823 (if (and (not uncompiled)
15824 (file-exists-p (concat f ".elc")))
15825 (load (concat f ".elc") nil nil t)
15826 (load (concat f ".el") nil nil t))))
15827 files))
15828 (org-version))
15830 ;;;###autoload
15831 (defun org-customize ()
15832 "Call the customize function with org as argument."
15833 (interactive)
15834 (org-load-modules-maybe)
15835 (org-require-autoloaded-modules)
15836 (customize-browse 'org))
15838 (defun org-create-customize-menu ()
15839 "Create a full customization menu for Org-mode, insert it into the menu."
15840 (interactive)
15841 (org-load-modules-maybe)
15842 (org-require-autoloaded-modules)
15843 (if (fboundp 'customize-menu-create)
15844 (progn
15845 (easy-menu-change
15846 '("Org") "Customize"
15847 `(["Browse Org group" org-customize t]
15848 "--"
15849 ,(customize-menu-create 'org)
15850 ["Set" Custom-set t]
15851 ["Save" Custom-save t]
15852 ["Reset to Current" Custom-reset-current t]
15853 ["Reset to Saved" Custom-reset-saved t]
15854 ["Reset to Standard Settings" Custom-reset-standard t]))
15855 (message "\"Org\"-menu now contains full customization menu"))
15856 (error "Cannot expand menu (outdated version of cus-edit.el)")))
15858 ;;;; Miscellaneous stuff
15860 ;;; Generally useful functions
15862 (defun org-get-at-bol (property)
15863 "Get text property PROPERTY at beginning of line."
15864 (get-text-property (point-at-bol) property))
15866 (defun org-find-text-property-in-string (prop s)
15867 "Return the first non-nil value of property PROP in string S."
15868 (or (get-text-property 0 prop s)
15869 (get-text-property (or (next-single-property-change 0 prop s) 0)
15870 prop s)))
15872 (defun org-display-warning (message) ;; Copied from Emacs-Muse
15873 "Display the given MESSAGE as a warning."
15874 (if (fboundp 'display-warning)
15875 (display-warning 'org message
15876 (if (featurep 'xemacs)
15877 'warning
15878 :warning))
15879 (let ((buf (get-buffer-create "*Org warnings*")))
15880 (with-current-buffer buf
15881 (goto-char (point-max))
15882 (insert "Warning (Org): " message)
15883 (unless (bolp)
15884 (newline)))
15885 (display-buffer buf)
15886 (sit-for 0))))
15888 (defun org-in-commented-line ()
15889 "Is point in a line starting with `#'?"
15890 (equal (char-after (point-at-bol)) ?#))
15892 (defun org-goto-marker-or-bmk (marker &optional bookmark)
15893 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
15894 (if (and marker (marker-buffer marker)
15895 (buffer-live-p (marker-buffer marker)))
15896 (progn
15897 (switch-to-buffer (marker-buffer marker))
15898 (if (or (> marker (point-max)) (< marker (point-min)))
15899 (widen))
15900 (goto-char marker)
15901 (org-show-context 'org-goto))
15902 (if bookmark
15903 (bookmark-jump bookmark)
15904 (error "Cannot find location"))))
15906 (defun org-quote-csv-field (s)
15907 "Quote field for inclusion in CSV material."
15908 (if (string-match "[\",]" s)
15909 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
15912 (defun org-plist-delete (plist property)
15913 "Delete PROPERTY from PLIST.
15914 This is in contrast to merely setting it to 0."
15915 (let (p)
15916 (while plist
15917 (if (not (eq property (car plist)))
15918 (setq p (plist-put p (car plist) (nth 1 plist))))
15919 (setq plist (cddr plist)))
15922 (defun org-force-self-insert (N)
15923 "Needed to enforce self-insert under remapping."
15924 (interactive "p")
15925 (self-insert-command N))
15927 (defun org-string-width (s)
15928 "Compute width of string, ignoring invisible characters.
15929 This ignores character with invisibility property `org-link', and also
15930 characters with property `org-cwidth', because these will become invisible
15931 upon the next fontification round."
15932 (let (b l)
15933 (when (or (eq t buffer-invisibility-spec)
15934 (assq 'org-link buffer-invisibility-spec))
15935 (while (setq b (text-property-any 0 (length s)
15936 'invisible 'org-link s))
15937 (setq s (concat (substring s 0 b)
15938 (substring s (or (next-single-property-change
15939 b 'invisible s) (length s)))))))
15940 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
15941 (setq s (concat (substring s 0 b)
15942 (substring s (or (next-single-property-change
15943 b 'org-cwidth s) (length s))))))
15944 (setq l (string-width s) b -1)
15945 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
15946 (setq l (- l (get-text-property b 'org-dwidth-n s))))
15949 (defun org-get-indentation (&optional line)
15950 "Get the indentation of the current line, interpreting tabs.
15951 When LINE is given, assume it represents a line and compute its indentation."
15952 (if line
15953 (if (string-match "^ *" (org-remove-tabs line))
15954 (match-end 0))
15955 (save-excursion
15956 (beginning-of-line 1)
15957 (skip-chars-forward " \t")
15958 (current-column))))
15960 (defun org-remove-tabs (s &optional width)
15961 "Replace tabulators in S with spaces.
15962 Assumes that s is a single line, starting in column 0."
15963 (setq width (or width tab-width))
15964 (while (string-match "\t" s)
15965 (setq s (replace-match
15966 (make-string
15967 (- (* width (/ (+ (match-beginning 0) width) width))
15968 (match-beginning 0)) ?\ )
15969 t t s)))
15972 (defun org-fix-indentation (line ind)
15973 "Fix indentation in LINE.
15974 IND is a cons cell with target and minimum indentation.
15975 If the current indentation in LINE is smaller than the minimum,
15976 leave it alone. If it is larger than ind, set it to the target."
15977 (let* ((l (org-remove-tabs line))
15978 (i (org-get-indentation l))
15979 (i1 (car ind)) (i2 (cdr ind)))
15980 (if (>= i i2) (setq l (substring line i2)))
15981 (if (> i1 0)
15982 (concat (make-string i1 ?\ ) l)
15983 l)))
15985 (defun org-remove-indentation (code &optional n)
15986 "Remove the maximum common indentation from the lines in CODE.
15987 N may optionally be the number of spaces to remove."
15988 (with-temp-buffer
15989 (insert code)
15990 (org-do-remove-indentation n)
15991 (buffer-string)))
15993 (defun org-do-remove-indentation (&optional n)
15994 "Remove the maximum common indentation from the buffer."
15995 (untabify (point-min) (point-max))
15996 (let ((min 10000) re)
15997 (if n
15998 (setq min n)
15999 (goto-char (point-min))
16000 (while (re-search-forward "^ *[^ \n]" nil t)
16001 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
16002 (unless (or (= min 0) (= min 10000))
16003 (setq re (format "^ \\{%d\\}" min))
16004 (goto-char (point-min))
16005 (while (re-search-forward re nil t)
16006 (replace-match "")
16007 (end-of-line 1))
16008 min)))
16010 (defun org-fill-template (template alist)
16011 "Find each %key of ALIST in TEMPLATE and replace it."
16012 (let (entry key value)
16013 (setq alist (sort (copy-sequence alist)
16014 (lambda (a b) (< (length (car a)) (length (car b))))))
16015 (while (setq entry (pop alist))
16016 (setq template
16017 (replace-regexp-in-string
16018 (concat "%" (regexp-quote (car entry)))
16019 (cdr entry) template t t)))
16020 template))
16022 (defun org-base-buffer (buffer)
16023 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
16024 (if (not buffer)
16025 buffer
16026 (or (buffer-base-buffer buffer)
16027 buffer)))
16029 (defun org-trim (s)
16030 "Remove whitespace at beginning and end of string."
16031 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
16032 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
16035 (defun org-wrap (string &optional width lines)
16036 "Wrap string to either a number of lines, or a width in characters.
16037 If WIDTH is non-nil, the string is wrapped to that width, however many lines
16038 that costs. If there is a word longer than WIDTH, the text is actually
16039 wrapped to the length of that word.
16040 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
16041 many lines, whatever width that takes.
16042 The return value is a list of lines, without newlines at the end."
16043 (let* ((words (org-split-string string "[ \t\n]+"))
16044 (maxword (apply 'max (mapcar 'org-string-width words)))
16045 w ll)
16046 (cond (width
16047 (org-do-wrap words (max maxword width)))
16048 (lines
16049 (setq w maxword)
16050 (setq ll (org-do-wrap words maxword))
16051 (if (<= (length ll) lines)
16053 (setq ll words)
16054 (while (> (length ll) lines)
16055 (setq w (1+ w))
16056 (setq ll (org-do-wrap words w)))
16057 ll))
16058 (t (error "Cannot wrap this")))))
16060 (defun org-do-wrap (words width)
16061 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
16062 (let (lines line)
16063 (while words
16064 (setq line (pop words))
16065 (while (and words (< (+ (length line) (length (car words))) width))
16066 (setq line (concat line " " (pop words))))
16067 (setq lines (push line lines)))
16068 (nreverse lines)))
16070 (defun org-split-string (string &optional separators)
16071 "Splits STRING into substrings at SEPARATORS.
16072 No empty strings are returned if there are matches at the beginning
16073 and end of string."
16074 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
16075 (start 0)
16076 notfirst
16077 (list nil))
16078 (while (and (string-match rexp string
16079 (if (and notfirst
16080 (= start (match-beginning 0))
16081 (< start (length string)))
16082 (1+ start) start))
16083 (< (match-beginning 0) (length string)))
16084 (setq notfirst t)
16085 (or (eq (match-beginning 0) 0)
16086 (and (eq (match-beginning 0) (match-end 0))
16087 (eq (match-beginning 0) start))
16088 (setq list
16089 (cons (substring string start (match-beginning 0))
16090 list)))
16091 (setq start (match-end 0)))
16092 (or (eq start (length string))
16093 (setq list
16094 (cons (substring string start)
16095 list)))
16096 (nreverse list)))
16098 (defun org-quote-vert (s)
16099 "Replace \"|\" with \"\\vert\"."
16100 (while (string-match "|" s)
16101 (setq s (replace-match "\\vert" t t s)))
16104 (defun org-uuidgen-p (s)
16105 "Is S an ID created by UUIDGEN?"
16106 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
16108 (defun org-context ()
16109 "Return a list of contexts of the current cursor position.
16110 If several contexts apply, all are returned.
16111 Each context entry is a list with a symbol naming the context, and
16112 two positions indicating start and end of the context. Possible
16113 contexts are:
16115 :headline anywhere in a headline
16116 :headline-stars on the leading stars in a headline
16117 :todo-keyword on a TODO keyword (including DONE) in a headline
16118 :tags on the TAGS in a headline
16119 :priority on the priority cookie in a headline
16120 :item on the first line of a plain list item
16121 :item-bullet on the bullet/number of a plain list item
16122 :checkbox on the checkbox in a plain list item
16123 :table in an org-mode table
16124 :table-special on a special filed in a table
16125 :table-table in a table.el table
16126 :link on a hyperlink
16127 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
16128 :target on a <<target>>
16129 :radio-target on a <<<radio-target>>>
16130 :latex-fragment on a LaTeX fragment
16131 :latex-preview on a LaTeX fragment with overlayed preview image
16133 This function expects the position to be visible because it uses font-lock
16134 faces as a help to recognize the following contexts: :table-special, :link,
16135 and :keyword."
16136 (let* ((f (get-text-property (point) 'face))
16137 (faces (if (listp f) f (list f)))
16138 (p (point)) clist o)
16139 ;; First the large context
16140 (cond
16141 ((org-on-heading-p t)
16142 (push (list :headline (point-at-bol) (point-at-eol)) clist)
16143 (when (progn
16144 (beginning-of-line 1)
16145 (looking-at org-todo-line-tags-regexp))
16146 (push (org-point-in-group p 1 :headline-stars) clist)
16147 (push (org-point-in-group p 2 :todo-keyword) clist)
16148 (push (org-point-in-group p 4 :tags) clist))
16149 (goto-char p)
16150 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
16151 (if (looking-at "\\[#[A-Z0-9]\\]")
16152 (push (org-point-in-group p 0 :priority) clist)))
16154 ((org-at-item-p)
16155 (push (org-point-in-group p 2 :item-bullet) clist)
16156 (push (list :item (point-at-bol)
16157 (save-excursion (org-end-of-item) (point)))
16158 clist)
16159 (and (org-at-item-checkbox-p)
16160 (push (org-point-in-group p 0 :checkbox) clist)))
16162 ((org-at-table-p)
16163 (push (list :table (org-table-begin) (org-table-end)) clist)
16164 (if (memq 'org-formula faces)
16165 (push (list :table-special
16166 (previous-single-property-change p 'face)
16167 (next-single-property-change p 'face)) clist)))
16168 ((org-at-table-p 'any)
16169 (push (list :table-table) clist)))
16170 (goto-char p)
16172 ;; Now the small context
16173 (cond
16174 ((org-at-timestamp-p)
16175 (push (org-point-in-group p 0 :timestamp) clist))
16176 ((memq 'org-link faces)
16177 (push (list :link
16178 (previous-single-property-change p 'face)
16179 (next-single-property-change p 'face)) clist))
16180 ((memq 'org-special-keyword faces)
16181 (push (list :keyword
16182 (previous-single-property-change p 'face)
16183 (next-single-property-change p 'face)) clist))
16184 ((org-on-target-p)
16185 (push (org-point-in-group p 0 :target) clist)
16186 (goto-char (1- (match-beginning 0)))
16187 (if (looking-at org-radio-target-regexp)
16188 (push (org-point-in-group p 0 :radio-target) clist))
16189 (goto-char p))
16190 ((setq o (car (delq nil
16191 (mapcar
16192 (lambda (x)
16193 (if (memq x org-latex-fragment-image-overlays) x))
16194 (org-overlays-at (point))))))
16195 (push (list :latex-fragment
16196 (org-overlay-start o) (org-overlay-end o)) clist)
16197 (push (list :latex-preview
16198 (org-overlay-start o) (org-overlay-end o)) clist))
16199 ((org-inside-LaTeX-fragment-p)
16200 ;; FIXME: positions wrong.
16201 (push (list :latex-fragment (point) (point)) clist)))
16203 (setq clist (nreverse (delq nil clist)))
16204 clist))
16206 ;; FIXME: Compare with at-regexp-p Do we need both?
16207 (defun org-in-regexp (re &optional nlines visually)
16208 "Check if point is inside a match of regexp.
16209 Normally only the current line is checked, but you can include NLINES extra
16210 lines both before and after point into the search.
16211 If VISUALLY is set, require that the cursor is not after the match but
16212 really on, so that the block visually is on the match."
16213 (catch 'exit
16214 (let ((pos (point))
16215 (eol (point-at-eol (+ 1 (or nlines 0))))
16216 (inc (if visually 1 0)))
16217 (save-excursion
16218 (beginning-of-line (- 1 (or nlines 0)))
16219 (while (re-search-forward re eol t)
16220 (if (and (<= (match-beginning 0) pos)
16221 (>= (+ inc (match-end 0)) pos))
16222 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
16224 (defun org-at-regexp-p (regexp)
16225 "Is point inside a match of REGEXP in the current line?"
16226 (catch 'exit
16227 (save-excursion
16228 (let ((pos (point)) (end (point-at-eol)))
16229 (beginning-of-line 1)
16230 (while (re-search-forward regexp end t)
16231 (if (and (<= (match-beginning 0) pos)
16232 (>= (match-end 0) pos))
16233 (throw 'exit t)))
16234 nil))))
16236 (defun org-occur-in-agenda-files (regexp &optional nlines)
16237 "Call `multi-occur' with buffers for all agenda files."
16238 (interactive "sOrg-files matching: \np")
16239 (let* ((files (org-agenda-files))
16240 (tnames (mapcar 'file-truename files))
16241 (extra org-agenda-text-search-extra-files)
16243 (when (eq (car extra) 'agenda-archives)
16244 (setq extra (cdr extra))
16245 (setq files (org-add-archive-files files)))
16246 (while (setq f (pop extra))
16247 (unless (member (file-truename f) tnames)
16248 (add-to-list 'files f 'append)
16249 (add-to-list 'tnames (file-truename f) 'append)))
16250 (multi-occur
16251 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
16252 regexp)))
16254 (if (boundp 'occur-mode-find-occurrence-hook)
16255 ;; Emacs 23
16256 (add-hook 'occur-mode-find-occurrence-hook
16257 (lambda ()
16258 (when (org-mode-p)
16259 (org-reveal))))
16260 ;; Emacs 22
16261 (defadvice occur-mode-goto-occurrence
16262 (after org-occur-reveal activate)
16263 (and (org-mode-p) (org-reveal)))
16264 (defadvice occur-mode-goto-occurrence-other-window
16265 (after org-occur-reveal activate)
16266 (and (org-mode-p) (org-reveal)))
16267 (defadvice occur-mode-display-occurrence
16268 (after org-occur-reveal activate)
16269 (when (org-mode-p)
16270 (let ((pos (occur-mode-find-occurrence)))
16271 (with-current-buffer (marker-buffer pos)
16272 (save-excursion
16273 (goto-char pos)
16274 (org-reveal)))))))
16276 (defun org-occur-link-in-agenda-files ()
16277 "Create a link and search for it in the agendas.
16278 The link is not stored in `org-stored-links', it is just created
16279 for the search purpose."
16280 (interactive)
16281 (let ((link (condition-case nil
16282 (org-store-link nil)
16283 (error "Unable to create a link to here"))))
16284 (org-occur-in-agenda-files (regexp-quote link))))
16286 (defun org-uniquify (list)
16287 "Remove duplicate elements from LIST."
16288 (let (res)
16289 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
16290 res))
16292 (defun org-delete-all (elts list)
16293 "Remove all elements in ELTS from LIST."
16294 (while elts
16295 (setq list (delete (pop elts) list)))
16296 list)
16298 (defun org-back-over-empty-lines ()
16299 "Move backwards over whitespace, to the beginning of the first empty line.
16300 Returns the number of empty lines passed."
16301 (let ((pos (point)))
16302 (skip-chars-backward " \t\n\r")
16303 (beginning-of-line 2)
16304 (goto-char (min (point) pos))
16305 (count-lines (point) pos)))
16307 (defun org-skip-whitespace ()
16308 (skip-chars-forward " \t\n\r"))
16310 (defun org-point-in-group (point group &optional context)
16311 "Check if POINT is in match-group GROUP.
16312 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
16313 match. If the match group does ot exist or point is not inside it,
16314 return nil."
16315 (and (match-beginning group)
16316 (>= point (match-beginning group))
16317 (<= point (match-end group))
16318 (if context
16319 (list context (match-beginning group) (match-end group))
16320 t)))
16322 (defun org-switch-to-buffer-other-window (&rest args)
16323 "Switch to buffer in a second window on the current frame.
16324 In particular, do not allow pop-up frames."
16325 (let (pop-up-frames special-display-buffer-names special-display-regexps
16326 special-display-function)
16327 (apply 'switch-to-buffer-other-window args)))
16329 (defun org-combine-plists (&rest plists)
16330 "Create a single property list from all plists in PLISTS.
16331 The process starts by copying the first list, and then setting properties
16332 from the other lists. Settings in the last list are the most significant
16333 ones and overrule settings in the other lists."
16334 (let ((rtn (copy-sequence (pop plists)))
16335 p v ls)
16336 (while plists
16337 (setq ls (pop plists))
16338 (while ls
16339 (setq p (pop ls) v (pop ls))
16340 (setq rtn (plist-put rtn p v))))
16341 rtn))
16343 (defun org-move-line-down (arg)
16344 "Move the current line down. With prefix argument, move it past ARG lines."
16345 (interactive "p")
16346 (let ((col (current-column))
16347 beg end pos)
16348 (beginning-of-line 1) (setq beg (point))
16349 (beginning-of-line 2) (setq end (point))
16350 (beginning-of-line (+ 1 arg))
16351 (setq pos (move-marker (make-marker) (point)))
16352 (insert (delete-and-extract-region beg end))
16353 (goto-char pos)
16354 (org-move-to-column col)))
16356 (defun org-move-line-up (arg)
16357 "Move the current line up. With prefix argument, move it past ARG lines."
16358 (interactive "p")
16359 (let ((col (current-column))
16360 beg end pos)
16361 (beginning-of-line 1) (setq beg (point))
16362 (beginning-of-line 2) (setq end (point))
16363 (beginning-of-line (- arg))
16364 (setq pos (move-marker (make-marker) (point)))
16365 (insert (delete-and-extract-region beg end))
16366 (goto-char pos)
16367 (org-move-to-column col)))
16369 (defun org-replace-escapes (string table)
16370 "Replace %-escapes in STRING with values in TABLE.
16371 TABLE is an association list with keys like \"%a\" and string values.
16372 The sequences in STRING may contain normal field width and padding information,
16373 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
16374 so values can contain further %-escapes if they are define later in TABLE."
16375 (let ((case-fold-search nil)
16376 e re rpl)
16377 (while (setq e (pop table))
16378 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
16379 (while (string-match re string)
16380 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
16381 (cdr e)))
16382 (setq string (replace-match rpl t t string))))
16383 string))
16386 (defun org-sublist (list start end)
16387 "Return a section of LIST, from START to END.
16388 Counting starts at 1."
16389 (let (rtn (c start))
16390 (setq list (nthcdr (1- start) list))
16391 (while (and list (<= c end))
16392 (push (pop list) rtn)
16393 (setq c (1+ c)))
16394 (nreverse rtn)))
16396 (defun org-find-base-buffer-visiting (file)
16397 "Like `find-buffer-visiting' but always return the base buffer and
16398 not an indirect buffer."
16399 (let ((buf (or (get-file-buffer file)
16400 (find-buffer-visiting file))))
16401 (if buf
16402 (or (buffer-base-buffer buf) buf)
16403 nil)))
16405 (defun org-image-file-name-regexp (&optional extensions)
16406 "Return regexp matching the file names of images.
16407 If EXTENSIONS is given, only match these."
16408 (if (and (not extensions) (fboundp 'image-file-name-regexp))
16409 (image-file-name-regexp)
16410 (let ((image-file-name-extensions
16411 (or extensions
16412 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
16413 "xbm" "xpm" "pbm" "pgm" "ppm"))))
16414 (concat "\\."
16415 (regexp-opt (nconc (mapcar 'upcase
16416 image-file-name-extensions)
16417 image-file-name-extensions)
16419 "\\'"))))
16421 (defun org-file-image-p (file &optional extensions)
16422 "Return non-nil if FILE is an image."
16423 (save-match-data
16424 (string-match (org-image-file-name-regexp extensions) file)))
16426 (defun org-get-cursor-date ()
16427 "Return the date at cursor in as a time.
16428 This works in the calendar and in the agenda, anywhere else it just
16429 returns the current time."
16430 (let (date day defd)
16431 (cond
16432 ((eq major-mode 'calendar-mode)
16433 (setq date (calendar-cursor-to-date)
16434 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16435 ((eq major-mode 'org-agenda-mode)
16436 (setq day (get-text-property (point) 'day))
16437 (if day
16438 (setq date (calendar-gregorian-from-absolute day)
16439 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
16440 (nth 2 date))))))
16441 (or defd (current-time))))
16443 (defvar org-agenda-action-marker (make-marker)
16444 "Marker pointing to the entry for the next agenda action.")
16446 (defun org-mark-entry-for-agenda-action ()
16447 "Mark the current entry as target of an agenda action.
16448 Agenda actions are actions executed from the agenda with the key `k',
16449 which make use of the date at the cursor."
16450 (interactive)
16451 (move-marker org-agenda-action-marker
16452 (save-excursion (org-back-to-heading t) (point))
16453 (current-buffer))
16454 (message
16455 "Entry marked for action; press `k' at desired date in agenda or calendar"))
16457 ;;; Paragraph filling stuff.
16458 ;; We want this to be just right, so use the full arsenal.
16460 (defun org-indent-line-function ()
16461 "Indent line like previous, but further if previous was headline or item."
16462 (interactive)
16463 (let* ((pos (point))
16464 (itemp (org-at-item-p))
16465 (case-fold-search t)
16466 (org-drawer-regexp (or org-drawer-regexp "\000"))
16467 column bpos bcol tpos tcol bullet btype bullet-type)
16468 ;; Find the previous relevant line
16469 (beginning-of-line 1)
16470 (cond
16471 ((looking-at "#") (setq column 0))
16472 ((looking-at "\\*+ ") (setq column 0))
16473 ((and (looking-at "[ \t]*:END:")
16474 (save-excursion (re-search-backward org-drawer-regexp nil t)))
16475 (save-excursion
16476 (goto-char (1- (match-beginning 1)))
16477 (setq column (current-column))))
16478 ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
16479 (save-excursion
16480 (re-search-backward
16481 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
16482 (setq column (org-get-indentation (match-string 0))))
16484 (beginning-of-line 0)
16485 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
16486 (not (looking-at "[ \t]*:END:"))
16487 (not (looking-at org-drawer-regexp)))
16488 (beginning-of-line 0))
16489 (cond
16490 ((looking-at "\\*+[ \t]+")
16491 (if (not org-adapt-indentation)
16492 (setq column 0)
16493 (goto-char (match-end 0))
16494 (setq column (current-column))))
16495 ((looking-at org-drawer-regexp)
16496 (goto-char (1- (match-beginning 1)))
16497 (setq column (current-column)))
16498 ((looking-at "\\([ \t]*\\):END:")
16499 (goto-char (match-end 1))
16500 (setq column (current-column)))
16501 ((org-in-item-p)
16502 (org-beginning-of-item)
16503 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
16504 (setq bpos (match-beginning 1) tpos (match-end 0)
16505 bcol (progn (goto-char bpos) (current-column))
16506 tcol (progn (goto-char tpos) (current-column))
16507 bullet (match-string 1)
16508 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
16509 (if (> tcol (+ bcol org-description-max-indent))
16510 (setq tcol (+ bcol 5)))
16511 (if (not itemp)
16512 (setq column tcol)
16513 (goto-char pos)
16514 (beginning-of-line 1)
16515 (if (looking-at "\\S-")
16516 (progn
16517 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
16518 (setq bullet (match-string 1)
16519 btype (if (string-match "[0-9]" bullet) "n" bullet))
16520 (setq column (if (equal btype bullet-type) bcol tcol)))
16521 (setq column (org-get-indentation)))))
16522 (t (setq column (org-get-indentation))))))
16523 (goto-char pos)
16524 (if (<= (current-column) (current-indentation))
16525 (org-indent-line-to column)
16526 (save-excursion (org-indent-line-to column)))
16527 (setq column (current-column))
16528 (beginning-of-line 1)
16529 (if (looking-at
16530 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
16531 (replace-match (concat (match-string 1)
16532 (format org-property-format
16533 (match-string 2) (match-string 3)))
16534 t t))
16535 (org-move-to-column column)))
16537 (defun org-set-autofill-regexps ()
16538 (interactive)
16539 ;; In the paragraph separator we include headlines, because filling
16540 ;; text in a line directly attached to a headline would otherwise
16541 ;; fill the headline as well.
16542 (org-set-local 'comment-start-skip "^#+[ \t]*")
16543 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
16544 ;; The paragraph starter includes hand-formatted lists.
16545 (org-set-local
16546 'paragraph-start
16547 (concat
16548 "\f" "\\|"
16549 "[ ]*$" "\\|"
16550 "\\*+ " "\\|"
16551 "[ \t]*#" "\\|"
16552 "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
16553 "[ \t]*[:|]" "\\|"
16554 "\\$\\$" "\\|"
16555 "\\\\\\(begin\\|end\\|[][]\\)"))
16556 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
16557 ;; But only if the user has not turned off tables or fixed-width regions
16558 (org-set-local
16559 'auto-fill-inhibit-regexp
16560 (concat "\\*+ \\|#\\+"
16561 "\\|[ \t]*" org-keyword-time-regexp
16562 (if (or org-enable-table-editor org-enable-fixed-width-editor)
16563 (concat
16564 "\\|[ \t]*["
16565 (if org-enable-table-editor "|" "")
16566 (if org-enable-fixed-width-editor ":" "")
16567 "]"))))
16568 ;; We use our own fill-paragraph function, to make sure that tables
16569 ;; and fixed-width regions are not wrapped. That function will pass
16570 ;; through to `fill-paragraph' when appropriate.
16571 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
16572 ; Adaptive filling: To get full control, first make sure that
16573 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
16574 (org-set-local 'adaptive-fill-regexp "\000")
16575 (org-set-local 'adaptive-fill-function
16576 'org-adaptive-fill-function)
16577 (org-set-local
16578 'align-mode-rules-list
16579 '((org-in-buffer-settings
16580 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
16581 (modes . '(org-mode))))))
16583 (defun org-fill-paragraph (&optional justify)
16584 "Re-align a table, pass through to fill-paragraph if no table."
16585 (let ((table-p (org-at-table-p))
16586 (table.el-p (org-at-table.el-p)))
16587 (cond ((and (equal (char-after (point-at-bol)) ?*)
16588 (save-excursion (goto-char (point-at-bol))
16589 (looking-at outline-regexp)))
16590 t) ; skip headlines
16591 (table.el-p t) ; skip table.el tables
16592 (table-p (org-table-align) t) ; align org-mode tables
16593 (t nil)))) ; call paragraph-fill
16595 ;; For reference, this is the default value of adaptive-fill-regexp
16596 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
16598 (defun org-adaptive-fill-function ()
16599 "Return a fill prefix for org-mode files.
16600 In particular, this makes sure hanging paragraphs for hand-formatted lists
16601 work correctly."
16602 (cond ((looking-at "#[ \t]+")
16603 (match-string 0))
16604 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
16605 (save-excursion
16606 (if (> (match-end 1) (+ (match-beginning 1)
16607 org-description-max-indent))
16608 (goto-char (+ (match-beginning 1) 5))
16609 (goto-char (match-end 0)))
16610 (make-string (current-column) ?\ )))
16611 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
16612 (save-excursion
16613 (goto-char (match-end 0))
16614 (make-string (current-column) ?\ )))
16615 (t nil)))
16617 ;;; Other stuff.
16619 (defun org-toggle-fixed-width-section (arg)
16620 "Toggle the fixed-width export.
16621 If there is no active region, the QUOTE keyword at the current headline is
16622 inserted or removed. When present, it causes the text between this headline
16623 and the next to be exported as fixed-width text, and unmodified.
16624 If there is an active region, this command adds or removes a colon as the
16625 first character of this line. If the first character of a line is a colon,
16626 this line is also exported in fixed-width font."
16627 (interactive "P")
16628 (let* ((cc 0)
16629 (regionp (org-region-active-p))
16630 (beg (if regionp (region-beginning) (point)))
16631 (end (if regionp (region-end)))
16632 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
16633 (case-fold-search nil)
16634 (re "[ \t]*\\(: \\)")
16635 off)
16636 (if regionp
16637 (save-excursion
16638 (goto-char beg)
16639 (setq cc (current-column))
16640 (beginning-of-line 1)
16641 (setq off (looking-at re))
16642 (while (> nlines 0)
16643 (setq nlines (1- nlines))
16644 (beginning-of-line 1)
16645 (cond
16646 (arg
16647 (org-move-to-column cc t)
16648 (insert ": \n")
16649 (forward-line -1))
16650 ((and off (looking-at re))
16651 (replace-match "" t t nil 1))
16652 ((not off) (org-move-to-column cc t) (insert ": ")))
16653 (forward-line 1)))
16654 (save-excursion
16655 (org-back-to-heading)
16656 (if (looking-at (concat outline-regexp
16657 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
16658 (replace-match "" t t nil 1)
16659 (if (looking-at outline-regexp)
16660 (progn
16661 (goto-char (match-end 0))
16662 (insert org-quote-string " "))))))))
16664 (defun org-reftex-citation ()
16665 "Use reftex-citation to insert a citation into the buffer.
16666 This looks for a line like
16668 #+BIBLIOGRAPHY: foo plain option:-d
16670 and derives from it that foo.bib is the bbliography file relevant
16671 for this document. It then installs the necessary environment for RefTeX
16672 to work in this buffer and calls `reftex-citation' to insert a citation
16673 into the buffer.
16675 Export of such citations to both LaTeX and HTML is handled by the contributed
16676 package org-exp-bibtex by Taru Karttunen."
16677 (interactive)
16678 (let ((reftex-docstruct-symbol 'rds)
16679 (reftex-cite-format "\\cite{%l}")
16680 rds bib)
16681 (save-excursion
16682 (save-restriction
16683 (widen)
16684 (let ((case-fold-search t)
16685 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
16686 (if (not (save-excursion
16687 (or (re-search-forward re nil t)
16688 (re-search-backward re nil t))))
16689 (error "No bibliography defined in file")
16690 (setq bib (concat (match-string 1) ".bib")
16691 rds (list (list 'bib bib)))))))
16692 (call-interactively 'reftex-citation)))
16694 ;;;; Functions extending outline functionality
16696 (defun org-beginning-of-line (&optional arg)
16697 "Go to the beginning of the current line. If that is invisible, continue
16698 to a visible line beginning. This makes the function of C-a more intuitive.
16699 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
16700 first attempt, and only move to after the tags when the cursor is already
16701 beyond the end of the headline."
16702 (interactive "P")
16703 (let ((pos (point))
16704 (special (if (consp org-special-ctrl-a/e)
16705 (car org-special-ctrl-a/e)
16706 org-special-ctrl-a/e))
16707 refpos)
16708 (if (org-bound-and-true-p line-move-visual)
16709 (beginning-of-visual-line 1)
16710 (beginning-of-line 1))
16711 (if (and arg (fboundp 'move-beginning-of-line))
16712 (call-interactively 'move-beginning-of-line)
16713 (if (bobp)
16715 (backward-char 1)
16716 (if (org-invisible-p)
16717 (while (and (not (bobp)) (org-invisible-p))
16718 (backward-char 1)
16719 (beginning-of-line 1))
16720 (forward-char 1))))
16721 (when special
16722 (cond
16723 ((and (looking-at org-complex-heading-regexp)
16724 (= (char-after (match-end 1)) ?\ ))
16725 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
16726 (point-at-eol)))
16727 (goto-char
16728 (if (eq special t)
16729 (cond ((> pos refpos) refpos)
16730 ((= pos (point)) refpos)
16731 (t (point)))
16732 (cond ((> pos (point)) (point))
16733 ((not (eq last-command this-command)) (point))
16734 (t refpos)))))
16735 ((org-at-item-p)
16736 (goto-char
16737 (if (eq special t)
16738 (cond ((> pos (match-end 4)) (match-end 4))
16739 ((= pos (point)) (match-end 4))
16740 (t (point)))
16741 (cond ((> pos (point)) (point))
16742 ((not (eq last-command this-command)) (point))
16743 (t (match-end 4))))))))
16744 (org-no-warnings
16745 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
16747 (defun org-end-of-line (&optional arg)
16748 "Go to the end of the line.
16749 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
16750 first attempt, and only move to after the tags when the cursor is already
16751 beyond the end of the headline."
16752 (interactive "P")
16753 (let ((special (if (consp org-special-ctrl-a/e)
16754 (cdr org-special-ctrl-a/e)
16755 org-special-ctrl-a/e)))
16756 (if (or (not special)
16757 (not (org-on-heading-p))
16758 arg)
16759 (call-interactively
16760 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
16761 ((fboundp 'move-end-of-line) 'move-end-of-line)
16762 (t 'end-of-line)))
16763 (let ((pos (point)))
16764 (beginning-of-line 1)
16765 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
16766 (if (eq special t)
16767 (if (or (< pos (match-beginning 1))
16768 (= pos (match-end 0)))
16769 (goto-char (match-beginning 1))
16770 (goto-char (match-end 0)))
16771 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
16772 (goto-char (match-end 0))
16773 (goto-char (match-beginning 1))))
16774 (call-interactively (if (fboundp 'move-end-of-line)
16775 'move-end-of-line
16776 'end-of-line)))))
16777 (org-no-warnings
16778 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
16780 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
16781 (define-key org-mode-map "\C-e" 'org-end-of-line)
16782 (define-key org-mode-map [home] 'org-beginning-of-line)
16783 (define-key org-mode-map [end] 'org-end-of-line)
16785 (defun org-backward-sentence (&optional arg)
16786 "Go to beginning of sentence, or beginning of table field.
16787 This will call `backward-sentence' or `org-table-beginning-of-field',
16788 depending on context."
16789 (interactive "P")
16790 (cond
16791 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
16792 (t (call-interactively 'backward-sentence))))
16794 (defun org-forward-sentence (&optional arg)
16795 "Go to end of sentence, or end of table field.
16796 This will call `forward-sentence' or `org-table-end-of-field',
16797 depending on context."
16798 (interactive "P")
16799 (cond
16800 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
16801 (t (call-interactively 'forward-sentence))))
16803 (define-key org-mode-map "\M-a" 'org-backward-sentence)
16804 (define-key org-mode-map "\M-e" 'org-forward-sentence)
16806 (defun org-kill-line (&optional arg)
16807 "Kill line, to tags or end of line."
16808 (interactive "P")
16809 (cond
16810 ((or (not org-special-ctrl-k)
16811 (bolp)
16812 (not (org-on-heading-p)))
16813 (call-interactively 'kill-line))
16814 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
16815 (kill-region (point) (match-beginning 1))
16816 (org-set-tags nil t))
16817 (t (kill-region (point) (point-at-eol)))))
16819 (define-key org-mode-map "\C-k" 'org-kill-line)
16821 (defun org-yank (&optional arg)
16822 "Yank. If the kill is a subtree, treat it specially.
16823 This command will look at the current kill and check if is a single
16824 subtree, or a series of subtrees[1]. If it passes the test, and if the
16825 cursor is at the beginning of a line or after the stars of a currently
16826 empty headline, then the yank is handled specially. How exactly depends
16827 on the value of the following variables, both set by default.
16829 org-yank-folded-subtrees
16830 When set, the subtree(s) will be folded after insertion, but only
16831 if doing so would now swallow text after the yanked text.
16833 org-yank-adjusted-subtrees
16834 When set, the subtree will be promoted or demoted in order to
16835 fit into the local outline tree structure, which means that the level
16836 will be adjusted so that it becomes the smaller one of the two
16837 *visible* surrounding headings.
16839 Any prefix to this command will cause `yank' to be called directly with
16840 no special treatment. In particular, a simple `C-u' prefix will just
16841 plainly yank the text as it is.
16843 \[1] The test checks if the first non-white line is a heading
16844 and if there are no other headings with fewer stars."
16845 (interactive "P")
16846 (org-yank-generic 'yank arg))
16848 (defun org-yank-generic (command arg)
16849 "Perform some yank-like command.
16851 This function implements the behavior described in the `org-yank'
16852 documentation. However, it has been generalized to work for any
16853 interactive command with similar behavior."
16855 ;; pretend to be command COMMAND
16856 (setq this-command command)
16858 (if arg
16859 (call-interactively command)
16861 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
16862 (and (org-kill-is-subtree-p)
16863 (or (bolp)
16864 (and (looking-at "[ \t]*$")
16865 (string-match
16866 "\\`\\*+\\'"
16867 (buffer-substring (point-at-bol) (point)))))))
16868 swallowp)
16869 (cond
16870 ((and subtreep org-yank-folded-subtrees)
16871 (let ((beg (point))
16872 end)
16873 (if (and subtreep org-yank-adjusted-subtrees)
16874 (org-paste-subtree nil nil 'for-yank)
16875 (call-interactively command))
16877 (setq end (point))
16878 (goto-char beg)
16879 (when (and (bolp) subtreep
16880 (not (setq swallowp
16881 (org-yank-folding-would-swallow-text beg end))))
16882 (or (looking-at outline-regexp)
16883 (re-search-forward (concat "^" outline-regexp) end t))
16884 (while (and (< (point) end) (looking-at outline-regexp))
16885 (hide-subtree)
16886 (org-cycle-show-empty-lines 'folded)
16887 (condition-case nil
16888 (outline-forward-same-level 1)
16889 (error (goto-char end)))))
16890 (when swallowp
16891 (message
16892 "Inserted text not folded because that would swallow text"))
16894 (goto-char end)
16895 (skip-chars-forward " \t\n\r")
16896 (beginning-of-line 1)
16897 (push-mark beg 'nomsg)))
16898 ((and subtreep org-yank-adjusted-subtrees)
16899 (let ((beg (point-at-bol)))
16900 (org-paste-subtree nil nil 'for-yank)
16901 (push-mark beg 'nomsg)))
16903 (call-interactively command))))))
16905 (defun org-yank-folding-would-swallow-text (beg end)
16906 "Would hide-subtree at BEG swallow any text after END?"
16907 (let (level)
16908 (save-excursion
16909 (goto-char beg)
16910 (when (or (looking-at outline-regexp)
16911 (re-search-forward (concat "^" outline-regexp) end t))
16912 (setq level (org-outline-level)))
16913 (goto-char end)
16914 (skip-chars-forward " \t\r\n\v\f")
16915 (if (or (eobp)
16916 (and (bolp) (looking-at org-outline-regexp)
16917 (<= (org-outline-level) level)))
16918 nil ; Nothing would be swallowed
16919 t)))) ; something would swallow
16921 (define-key org-mode-map "\C-y" 'org-yank)
16923 (defun org-invisible-p ()
16924 "Check if point is at a character currently not visible."
16925 ;; Early versions of noutline don't have `outline-invisible-p'.
16926 (if (fboundp 'outline-invisible-p)
16927 (outline-invisible-p)
16928 (get-char-property (point) 'invisible)))
16930 (defun org-invisible-p2 ()
16931 "Check if point is at a character currently not visible."
16932 (save-excursion
16933 (if (and (eolp) (not (bobp))) (backward-char 1))
16934 ;; Early versions of noutline don't have `outline-invisible-p'.
16935 (if (fboundp 'outline-invisible-p)
16936 (outline-invisible-p)
16937 (get-char-property (point) 'invisible))))
16939 (defun org-back-to-heading (&optional invisible-ok)
16940 "Call `outline-back-to-heading', but provide a better error message."
16941 (condition-case nil
16942 (outline-back-to-heading invisible-ok)
16943 (error (error "Before first headline at position %d in buffer %s"
16944 (point) (current-buffer)))))
16946 (defun org-before-first-heading-p ()
16947 "Before first heading?"
16948 (save-excursion
16949 (null (re-search-backward "^\\*+ " nil t))))
16951 (defun org-on-heading-p (&optional ignored)
16952 (outline-on-heading-p t))
16953 (defun org-at-heading-p (&optional ignored)
16954 (outline-on-heading-p t))
16956 (defun org-at-heading-or-item-p ()
16957 (or (org-on-heading-p) (org-at-item-p)))
16959 (defun org-on-target-p ()
16960 (or (org-in-regexp org-radio-target-regexp)
16961 (org-in-regexp org-target-regexp)))
16963 (defun org-up-heading-all (arg)
16964 "Move to the heading line of which the present line is a subheading.
16965 This function considers both visible and invisible heading lines.
16966 With argument, move up ARG levels."
16967 (if (fboundp 'outline-up-heading-all)
16968 (outline-up-heading-all arg) ; emacs 21 version of outline.el
16969 (outline-up-heading arg t))) ; emacs 22 version of outline.el
16971 (defun org-up-heading-safe ()
16972 "Move to the heading line of which the present line is a subheading.
16973 This version will not throw an error. It will return the level of the
16974 headline found, or nil if no higher level is found.
16976 Also, this function will be a lot faster than `outline-up-heading',
16977 because it relies on stars being the outline starters. This can really
16978 make a significant difference in outlines with very many siblings."
16979 (let (start-level re)
16980 (org-back-to-heading t)
16981 (setq start-level (funcall outline-level))
16982 (if (equal start-level 1)
16984 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
16985 (if (re-search-backward re nil t)
16986 (funcall outline-level)))))
16988 (defun org-first-sibling-p ()
16989 "Is this heading the first child of its parents?"
16990 (interactive)
16991 (let ((re (concat "^" outline-regexp))
16992 level l)
16993 (unless (org-at-heading-p t)
16994 (error "Not at a heading"))
16995 (setq level (funcall outline-level))
16996 (save-excursion
16997 (if (not (re-search-backward re nil t))
16999 (setq l (funcall outline-level))
17000 (< l level)))))
17002 (defun org-goto-sibling (&optional previous)
17003 "Goto the next sibling, even if it is invisible.
17004 When PREVIOUS is set, go to the previous sibling instead. Returns t
17005 when a sibling was found. When none is found, return nil and don't
17006 move point."
17007 (let ((fun (if previous 're-search-backward 're-search-forward))
17008 (pos (point))
17009 (re (concat "^" outline-regexp))
17010 level l)
17011 (when (condition-case nil (org-back-to-heading t) (error nil))
17012 (setq level (funcall outline-level))
17013 (catch 'exit
17014 (or previous (forward-char 1))
17015 (while (funcall fun re nil t)
17016 (setq l (funcall outline-level))
17017 (when (< l level) (goto-char pos) (throw 'exit nil))
17018 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
17019 (goto-char pos)
17020 nil))))
17022 (defun org-show-siblings ()
17023 "Show all siblings of the current headline."
17024 (save-excursion
17025 (while (org-goto-sibling) (org-flag-heading nil)))
17026 (save-excursion
17027 (while (org-goto-sibling 'previous)
17028 (org-flag-heading nil))))
17030 (defun org-show-hidden-entry ()
17031 "Show an entry where even the heading is hidden."
17032 (save-excursion
17033 (org-show-entry)))
17035 (defun org-flag-heading (flag &optional entry)
17036 "Flag the current heading. FLAG non-nil means make invisible.
17037 When ENTRY is non-nil, show the entire entry."
17038 (save-excursion
17039 (org-back-to-heading t)
17040 ;; Check if we should show the entire entry
17041 (if entry
17042 (progn
17043 (org-show-entry)
17044 (save-excursion
17045 (and (outline-next-heading)
17046 (org-flag-heading nil))))
17047 (outline-flag-region (max (point-min) (1- (point)))
17048 (save-excursion (outline-end-of-heading) (point))
17049 flag))))
17051 (defun org-get-next-sibling ()
17052 "Move to next heading of the same level, and return point.
17053 If there is no such heading, return nil.
17054 This is like outline-next-sibling, but invisible headings are ok."
17055 (let ((level (funcall outline-level)))
17056 (outline-next-heading)
17057 (while (and (not (eobp)) (> (funcall outline-level) level))
17058 (outline-next-heading))
17059 (if (or (eobp) (< (funcall outline-level) level))
17061 (point))))
17063 (defun org-get-last-sibling ()
17064 "Move to previous heading of the same level, and return point.
17065 If there is no such heading, return nil."
17066 (let ((opoint (point))
17067 (level (funcall outline-level)))
17068 (outline-previous-heading)
17069 (when (and (/= (point) opoint) (outline-on-heading-p t))
17070 (while (and (> (funcall outline-level) level)
17071 (not (bobp)))
17072 (outline-previous-heading))
17073 (if (< (funcall outline-level) level)
17075 (point)))))
17077 (defun org-end-of-subtree (&optional invisible-OK to-heading)
17078 ;; This contains an exact copy of the original function, but it uses
17079 ;; `org-back-to-heading', to make it work also in invisible
17080 ;; trees. And is uses an invisible-OK argument.
17081 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
17082 ;; Furthermore, when used inside Org, finding the end of a large subtree
17083 ;; with many children and grandchildren etc, this can be much faster
17084 ;; than the outline version.
17085 (org-back-to-heading invisible-OK)
17086 (let ((first t)
17087 (level (funcall outline-level)))
17088 (if (and (org-mode-p) (< level 1000))
17089 ;; A true heading (not a plain list item), in Org-mode
17090 ;; This means we can easily find the end by looking
17091 ;; only for the right number of stars. Using a regexp to do
17092 ;; this is so much faster than using a Lisp loop.
17093 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
17094 (forward-char 1)
17095 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
17096 ;; something else, do it the slow way
17097 (while (and (not (eobp))
17098 (or first (> (funcall outline-level) level)))
17099 (setq first nil)
17100 (outline-next-heading)))
17101 (unless to-heading
17102 (if (memq (preceding-char) '(?\n ?\^M))
17103 (progn
17104 ;; Go to end of line before heading
17105 (forward-char -1)
17106 (if (memq (preceding-char) '(?\n ?\^M))
17107 ;; leave blank line before heading
17108 (forward-char -1))))))
17109 (point))
17111 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
17112 "Use Org version in org-mode, for dramatic speed-up."
17113 (if (eq major-mode 'org-mode)
17114 (progn
17115 (org-end-of-subtree nil t)
17116 (unless (eobp) (backward-char 1)))
17117 ad-do-it))
17119 (defun org-forward-same-level (arg &optional invisible-ok)
17120 "Move forward to the arg'th subheading at same level as this one.
17121 Stop at the first and last subheadings of a superior heading."
17122 (interactive "p")
17123 (org-back-to-heading invisible-ok)
17124 (org-on-heading-p)
17125 (let* ((level (- (match-end 0) (match-beginning 0) 1))
17126 (re (format "^\\*\\{1,%d\\} " level))
17128 (forward-char 1)
17129 (while (> arg 0)
17130 (while (and (re-search-forward re nil 'move)
17131 (setq l (- (match-end 0) (match-beginning 0) 1))
17132 (= l level)
17133 (not invisible-ok)
17134 (org-invisible-p))
17135 (if (< l level) (setq arg 1)))
17136 (setq arg (1- arg)))
17137 (beginning-of-line 1)))
17139 (defun org-backward-same-level (arg &optional invisible-ok)
17140 "Move backward to the arg'th subheading at same level as this one.
17141 Stop at the first and last subheadings of a superior heading."
17142 (interactive "p")
17143 (org-back-to-heading)
17144 (org-on-heading-p)
17145 (let* ((level (- (match-end 0) (match-beginning 0) 1))
17146 (re (format "^\\*\\{1,%d\\} " level))
17148 (while (> arg 0)
17149 (while (and (re-search-backward re nil 'move)
17150 (setq l (- (match-end 0) (match-beginning 0) 1))
17151 (= l level)
17152 (not invisible-ok)
17153 (org-invisible-p))
17154 (if (< l level) (setq arg 1)))
17155 (setq arg (1- arg)))))
17157 (defun org-show-subtree ()
17158 "Show everything after this heading at deeper levels."
17159 (outline-flag-region
17160 (point)
17161 (save-excursion
17162 (org-end-of-subtree t t))
17163 nil))
17165 (defun org-show-entry ()
17166 "Show the body directly following this heading.
17167 Show the heading too, if it is currently invisible."
17168 (interactive)
17169 (save-excursion
17170 (condition-case nil
17171 (progn
17172 (org-back-to-heading t)
17173 (outline-flag-region
17174 (max (point-min) (1- (point)))
17175 (save-excursion
17176 (if (re-search-forward
17177 (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
17178 (match-beginning 1)
17179 (point-max)))
17180 nil)
17181 (org-cycle-hide-drawers 'children))
17182 (error nil))))
17184 (defun org-make-options-regexp (kwds &optional extra)
17185 "Make a regular expression for keyword lines."
17186 (concat
17188 "#?[ \t]*\\+\\("
17189 (mapconcat 'regexp-quote kwds "\\|")
17190 (if extra (concat "\\|" extra))
17191 "\\):[ \t]*"
17192 "\\(.*\\)"))
17194 ;; Make isearch reveal the necessary context
17195 (defun org-isearch-end ()
17196 "Reveal context after isearch exits."
17197 (when isearch-success ; only if search was successful
17198 (if (featurep 'xemacs)
17199 ;; Under XEmacs, the hook is run in the correct place,
17200 ;; we directly show the context.
17201 (org-show-context 'isearch)
17202 ;; In Emacs the hook runs *before* restoring the overlays.
17203 ;; So we have to use a one-time post-command-hook to do this.
17204 ;; (Emacs 22 has a special variable, see function `org-mode')
17205 (unless (and (boundp 'isearch-mode-end-hook-quit)
17206 isearch-mode-end-hook-quit)
17207 ;; Only when the isearch was not quitted.
17208 (org-add-hook 'post-command-hook 'org-isearch-post-command
17209 'append 'local)))))
17211 (defun org-isearch-post-command ()
17212 "Remove self from hook, and show context."
17213 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
17214 (org-show-context 'isearch))
17217 ;;;; Integration with and fixes for other packages
17219 ;;; Imenu support
17221 (defvar org-imenu-markers nil
17222 "All markers currently used by Imenu.")
17223 (make-variable-buffer-local 'org-imenu-markers)
17225 (defun org-imenu-new-marker (&optional pos)
17226 "Return a new marker for use by Imenu, and remember the marker."
17227 (let ((m (make-marker)))
17228 (move-marker m (or pos (point)))
17229 (push m org-imenu-markers)
17232 (defun org-imenu-get-tree ()
17233 "Produce the index for Imenu."
17234 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
17235 (setq org-imenu-markers nil)
17236 (let* ((n org-imenu-depth)
17237 (re (concat "^" outline-regexp))
17238 (subs (make-vector (1+ n) nil))
17239 (last-level 0)
17240 m level head)
17241 (save-excursion
17242 (save-restriction
17243 (widen)
17244 (goto-char (point-max))
17245 (while (re-search-backward re nil t)
17246 (setq level (org-reduced-level (funcall outline-level)))
17247 (when (<= level n)
17248 (looking-at org-complex-heading-regexp)
17249 (setq head (org-link-display-format
17250 (org-match-string-no-properties 4))
17251 m (org-imenu-new-marker))
17252 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
17253 (if (>= level last-level)
17254 (push (cons head m) (aref subs level))
17255 (push (cons head (aref subs (1+ level))) (aref subs level))
17256 (loop for i from (1+ level) to n do (aset subs i nil)))
17257 (setq last-level level)))))
17258 (aref subs 1)))
17260 (eval-after-load "imenu"
17261 '(progn
17262 (add-hook 'imenu-after-jump-hook
17263 (lambda ()
17264 (if (eq major-mode 'org-mode)
17265 (org-show-context 'org-goto))))))
17267 (defun org-link-display-format (link)
17268 "Replace a link with either the description, or the link target
17269 if no description is present"
17270 (save-match-data
17271 (if (string-match org-bracket-link-analytic-regexp link)
17272 (replace-match (or (match-string 5 link)
17273 (concat (match-string 1 link)
17274 (match-string 3 link)))
17275 nil nil link)
17276 link)))
17278 ;; Speedbar support
17280 (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
17281 "Overlay marking the agenda restriction line in speedbar.")
17282 (org-overlay-put org-speedbar-restriction-lock-overlay
17283 'face 'org-agenda-restriction-lock)
17284 (org-overlay-put org-speedbar-restriction-lock-overlay
17285 'help-echo "Agendas are currently limited to this item.")
17286 (org-detach-overlay org-speedbar-restriction-lock-overlay)
17288 (defun org-speedbar-set-agenda-restriction ()
17289 "Restrict future agenda commands to the location at point in speedbar.
17290 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
17291 (interactive)
17292 (require 'org-agenda)
17293 (let (p m tp np dir txt)
17294 (cond
17295 ((setq p (text-property-any (point-at-bol) (point-at-eol)
17296 'org-imenu t))
17297 (setq m (get-text-property p 'org-imenu-marker))
17298 (save-excursion
17299 (save-restriction
17300 (set-buffer (marker-buffer m))
17301 (goto-char m)
17302 (org-agenda-set-restriction-lock 'subtree))))
17303 ((setq p (text-property-any (point-at-bol) (point-at-eol)
17304 'speedbar-function 'speedbar-find-file))
17305 (setq tp (previous-single-property-change
17306 (1+ p) 'speedbar-function)
17307 np (next-single-property-change
17308 tp 'speedbar-function)
17309 dir (speedbar-line-directory)
17310 txt (buffer-substring-no-properties (or tp (point-min))
17311 (or np (point-max))))
17312 (save-excursion
17313 (save-restriction
17314 (set-buffer (find-file-noselect
17315 (let ((default-directory dir))
17316 (expand-file-name txt))))
17317 (unless (org-mode-p)
17318 (error "Cannot restrict to non-Org-mode file"))
17319 (org-agenda-set-restriction-lock 'file))))
17320 (t (error "Don't know how to restrict Org-mode's agenda")))
17321 (org-move-overlay org-speedbar-restriction-lock-overlay
17322 (point-at-bol) (point-at-eol))
17323 (setq current-prefix-arg nil)
17324 (org-agenda-maybe-redo)))
17326 (eval-after-load "speedbar"
17327 '(progn
17328 (speedbar-add-supported-extension ".org")
17329 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
17330 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
17331 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
17332 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
17333 (add-hook 'speedbar-visiting-tag-hook
17334 (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
17337 ;;; Fixes and Hacks for problems with other packages
17339 ;; Make flyspell not check words in links, to not mess up our keymap
17340 (defun org-mode-flyspell-verify ()
17341 "Don't let flyspell put overlays at active buttons."
17342 (and (not (get-text-property (point) 'keymap))
17343 (not (get-text-property (point) 'org-no-flyspell))))
17345 (defun org-remove-flyspell-overlays-in (beg end)
17346 "Remove flyspell overlays in region."
17347 (and (org-bound-and-true-p flyspell-mode)
17348 (fboundp 'flyspell-delete-region-overlays)
17349 (flyspell-delete-region-overlays beg end))
17350 (add-text-properties beg end '(org-no-flyspell t)))
17352 ;; Make `bookmark-jump' shows the jump location if it was hidden.
17353 (eval-after-load "bookmark"
17354 '(if (boundp 'bookmark-after-jump-hook)
17355 ;; We can use the hook
17356 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
17357 ;; Hook not available, use advice
17358 (defadvice bookmark-jump (after org-make-visible activate)
17359 "Make the position visible."
17360 (org-bookmark-jump-unhide))))
17362 ;; Make sure saveplace shows the location if it was hidden
17363 (eval-after-load "saveplace"
17364 '(defadvice save-place-find-file-hook (after org-make-visible activate)
17365 "Make the position visible."
17366 (org-bookmark-jump-unhide)))
17368 ;; Make sure ecb shows the location if it was hidden
17369 (eval-after-load "ecb"
17370 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
17371 "Make hierarchy visible when jumping into location from ECB tree buffer."
17372 (if (eq major-mode 'org-mode)
17373 (org-show-context))))
17375 (defun org-bookmark-jump-unhide ()
17376 "Unhide the current position, to show the bookmark location."
17377 (and (org-mode-p)
17378 (or (org-invisible-p)
17379 (save-excursion (goto-char (max (point-min) (1- (point))))
17380 (org-invisible-p)))
17381 (org-show-context 'bookmark-jump)))
17383 ;; Make session.el ignore our circular variable
17384 (eval-after-load "session"
17385 '(add-to-list 'session-globals-exclude 'org-mark-ring))
17387 ;;;; Experimental code
17389 (defun org-closed-in-range ()
17390 "Sparse tree of items closed in a certain time range.
17391 Still experimental, may disappear in the future."
17392 (interactive)
17393 ;; Get the time interval from the user.
17394 (let* ((time1 (org-float-time
17395 (org-read-date nil 'to-time nil "Starting date: ")))
17396 (time2 (org-float-time
17397 (org-read-date nil 'to-time nil "End date:")))
17398 ;; callback function
17399 (callback (lambda ()
17400 (let ((time
17401 (org-float-time
17402 (apply 'encode-time
17403 (org-parse-time-string
17404 (match-string 1))))))
17405 ;; check if time in interval
17406 (and (>= time time1) (<= time time2))))))
17407 ;; make tree, check each match with the callback
17408 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
17410 ;;;; Finish up
17412 (provide 'org)
17414 (run-hooks 'org-load-hook)
17416 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
17418 ;;; org.el ends here