Fix some typos.
[org-mode/org-tableheadings.git] / lisp / org.el
blob9f2944b96d46206237c1e97c1c1cea6dcc6d13e8
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 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 6.06b
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
29 ;; project planning with a fast and effective plain-text system.
31 ;; Org-mode develops organizational tasks around NOTES files that contain
32 ;; information about projects as plain text. Org-mode is implemented on
33 ;; top of outline-mode, which makes it possible to keep the content of
34 ;; large files well structured. Visibility cycling and structure editing
35 ;; help to work with the tree. Tables are easily created with a built-in
36 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
37 ;; and scheduling. It dynamically compiles entries into an agenda that
38 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
39 ;; Plain text URL-like links connect to websites, emails, Usenet
40 ;; messages, BBDB entries, and any files related to the projects. For
41 ;; printing and sharing of notes, an Org-mode file can be exported as a
42 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
43 ;; iCalendar file. It can also serve as a publishing tool for a set of
44 ;; linked webpages.
46 ;; Installation and Activation
47 ;; ---------------------------
48 ;; See the corresponding sections in the manual at
50 ;; http://orgmode.org/org.html#Installation
52 ;; Documentation
53 ;; -------------
54 ;; The documentation of Org-mode can be found in the TeXInfo file. The
55 ;; distribution also contains a PDF version of it. At the homepage of
56 ;; Org-mode, you can read the same text online as HTML. There is also an
57 ;; excellent reference card made by Philip Rooke. This card can be found
58 ;; in the etc/ directory of Emacs 22.
60 ;; A list of recent changes can be found at
61 ;; http://orgmode.org/Changes.html
63 ;;; Code:
65 (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
66 (defvar org-table-formula-constants-local nil
67 "Local version of `org-table-formula-constants'.")
68 (make-variable-buffer-local 'org-table-formula-constants-local)
70 ;;;; Require other packages
72 (eval-when-compile
73 (require 'cl)
74 (require 'gnus-sum)
75 (require 'calendar))
76 ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
77 ;; the file noutline.el being loaded.
78 (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
79 ;; We require noutline, which might be provided in outline.el
80 (require 'outline) (require 'noutline)
81 ;; Other stuff we need.
82 (require 'time-date)
83 (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
84 (require 'easymenu)
86 (require 'org-macs)
87 (require 'org-compat)
88 (require 'org-faces)
90 ;;;; Customization variables
92 ;;; Version
94 (defconst org-version "6.06b"
95 "The version number of the file org.el.")
97 (defun org-version (&optional here)
98 "Show the org-mode version in the echo area.
99 With prefix arg HERE, insert it at point."
100 (interactive "P")
101 (let ((version (format "Org-mode version %s" org-version)))
102 (message version)
103 (if here
104 (insert version))))
106 ;;; Compatibility constants
108 ;;; The custom variables
110 (defgroup org nil
111 "Outline-based notes management and organizer."
112 :tag "Org"
113 :group 'outlines
114 :group 'hypermedia
115 :group 'calendar)
117 (defcustom org-load-hook nil
118 "Hook that is run after org.el has been loaded."
119 :group 'org
120 :type 'hook)
122 (defvar org-modules) ; defined below
123 (defvar org-modules-loaded nil
124 "Have the modules been loaded already?")
126 (defun org-load-modules-maybe (&optional force)
127 "Load all extensions listed in `org-default-extensions'."
128 (when (or force (not org-modules-loaded))
129 (mapc (lambda (ext)
130 (condition-case nil (require ext)
131 (error (message "Problems while trying to load feature `%s'" ext))))
132 org-modules)
133 (setq org-modules-loaded t)))
135 (defun org-set-modules (var value)
136 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
137 (set var value)
138 (when (featurep 'org)
139 (org-load-modules-maybe 'force)))
141 (when (org-bound-and-true-p org-modules)
142 (let ((a (member 'org-infojs org-modules)))
143 (and a (setcar a 'org-jsinfo))))
145 (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl)
146 "Modules that should always be loaded together with org.el.
147 If a description starts with <C>, the file is not part of Emacs
148 and loading it will require that you have downloaded and properly installed
149 the org-mode distribution.
151 You can also use this system to load external packages (i.e. neither Org
152 core modules, not modules from the CONTRIB directory). Just add symbols
153 to the end of the list. If the package is called org-xyz.el, then you need
154 to add the symbol `xyz', and the package must have a call to
156 (provide 'org-xyz)"
157 :group 'org
158 :set 'org-set-modules
159 :type
160 '(set :greedy t
161 (const :tag " bbdb: Links to BBDB entries" org-bbdb)
162 (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
163 (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
164 (const :tag " id: Global id's for identifying entries" org-id)
165 (const :tag " info: Links to Info nodes" org-info)
166 (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
167 (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
168 (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
169 (const :tag " mew Links to Mew folders/messages" org-mew)
170 (const :tag " mhe: Links to MHE folders/messages" org-mhe)
171 (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
172 (const :tag " vm: Links to VM folders/messages" org-vm)
173 (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
174 (const :tag " mouse: Additional mouse support" org-mouse)
176 (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
177 (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
178 (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
179 (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
180 (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
181 (const :tag "C eval: Include command output as text" org-eval)
182 (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
183 (const :tag "C id: Global id's for identifying entries" org-id)
184 (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
185 (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
186 (const :tag "C man: Support for links to manpages in Org-mode" org-man)
187 (const :tag "C mtags: Support for muse-like tags" org-mtags)
188 (const :tag "C panel: Simple routines for us with bad memory" org-panel)
189 (const :tag "C registry: A registry for Org links" org-registry)
190 (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
191 (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
192 (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
193 (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
194 (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
197 (defgroup org-startup nil
198 "Options concerning startup of Org-mode."
199 :tag "Org Startup"
200 :group 'org)
202 (defcustom org-startup-folded t
203 "Non-nil means, entering Org-mode will switch to OVERVIEW.
204 This can also be configured on a per-file basis by adding one of
205 the following lines anywhere in the buffer:
207 #+STARTUP: fold
208 #+STARTUP: nofold
209 #+STARTUP: content"
210 :group 'org-startup
211 :type '(choice
212 (const :tag "nofold: show all" nil)
213 (const :tag "fold: overview" t)
214 (const :tag "content: all headlines" content)))
216 (defcustom org-startup-truncated t
217 "Non-nil means, entering Org-mode will set `truncate-lines'.
218 This is useful since some lines containing links can be very long and
219 uninteresting. Also tables look terrible when wrapped."
220 :group 'org-startup
221 :type 'boolean)
223 (defcustom org-startup-align-all-tables nil
224 "Non-nil means, align all tables when visiting a file.
225 This is useful when the column width in tables is forced with <N> cookies
226 in table fields. Such tables will look correct only after the first re-align.
227 This can also be configured on a per-file basis by adding one of
228 the following lines anywhere in the buffer:
229 #+STARTUP: align
230 #+STARTUP: noalign"
231 :group 'org-startup
232 :type 'boolean)
234 (defcustom org-insert-mode-line-in-empty-file nil
235 "Non-nil means insert the first line setting Org-mode in empty files.
236 When the function `org-mode' is called interactively in an empty file, this
237 normally means that the file name does not automatically trigger Org-mode.
238 To ensure that the file will always be in Org-mode in the future, a
239 line enforcing Org-mode will be inserted into the buffer, if this option
240 has been set."
241 :group 'org-startup
242 :type 'boolean)
244 (defcustom org-replace-disputed-keys nil
245 "Non-nil means use alternative key bindings for some keys.
246 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
247 These keys are also used by other packages like `CUA-mode' or `windmove.el'.
248 If you want to use Org-mode together with one of these other modes,
249 or more generally if you would like to move some Org-mode commands to
250 other keys, set this variable and configure the keys with the variable
251 `org-disputed-keys'.
253 This option is only relevant at load-time of Org-mode, and must be set
254 *before* org.el is loaded. Changing it requires a restart of Emacs to
255 become effective."
256 :group 'org-startup
257 :type 'boolean)
259 (if (fboundp 'defvaralias)
260 (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
262 (defcustom org-disputed-keys
263 '(([(shift up)] . [(meta p)])
264 ([(shift down)] . [(meta n)])
265 ([(shift left)] . [(meta -)])
266 ([(shift right)] . [(meta +)])
267 ([(control shift right)] . [(meta shift +)])
268 ([(control shift left)] . [(meta shift -)]))
269 "Keys for which Org-mode and other modes compete.
270 This is an alist, cars are the default keys, second element specifies
271 the alternative to use when `org-replace-disputed-keys' is t.
273 Keys can be specified in any syntax supported by `define-key'.
274 The value of this option takes effect only at Org-mode's startup,
275 therefore you'll have to restart Emacs to apply it after changing."
276 :group 'org-startup
277 :type 'alist)
279 (defun org-key (key)
280 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
281 Or return the original if not disputed."
282 (if org-replace-disputed-keys
283 (let* ((nkey (key-description key))
284 (x (org-find-if (lambda (x)
285 (equal (key-description (car x)) nkey))
286 org-disputed-keys)))
287 (if x (cdr x) key))
288 key))
290 (defun org-find-if (predicate seq)
291 (catch 'exit
292 (while seq
293 (if (funcall predicate (car seq))
294 (throw 'exit (car seq))
295 (pop seq)))))
297 (defun org-defkey (keymap key def)
298 "Define a key, possibly translated, as returned by `org-key'."
299 (define-key keymap (org-key key) def))
301 (defcustom org-ellipsis nil
302 "The ellipsis to use in the Org-mode outline.
303 When nil, just use the standard three dots. When a string, use that instead,
304 When a face, use the standart 3 dots, but with the specified face.
305 The change affects only Org-mode (which will then use its own display table).
306 Changing this requires executing `M-x org-mode' in a buffer to become
307 effective."
308 :group 'org-startup
309 :type '(choice (const :tag "Default" nil)
310 (face :tag "Face" :value org-warning)
311 (string :tag "String" :value "...#")))
313 (defvar org-display-table nil
314 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
316 (defgroup org-keywords nil
317 "Keywords in Org-mode."
318 :tag "Org Keywords"
319 :group 'org)
321 (defcustom org-deadline-string "DEADLINE:"
322 "String to mark deadline entries.
323 A deadline is this string, followed by a time stamp. Should be a word,
324 terminated by a colon. You can insert a schedule keyword and
325 a timestamp with \\[org-deadline].
326 Changes become only effective after restarting Emacs."
327 :group 'org-keywords
328 :type 'string)
330 (defcustom org-scheduled-string "SCHEDULED:"
331 "String to mark scheduled TODO entries.
332 A schedule is this string, followed by a time stamp. Should be a word,
333 terminated by a colon. You can insert a schedule keyword and
334 a timestamp with \\[org-schedule].
335 Changes become only effective after restarting Emacs."
336 :group 'org-keywords
337 :type 'string)
339 (defcustom org-closed-string "CLOSED:"
340 "String used as the prefix for timestamps logging closing a TODO entry."
341 :group 'org-keywords
342 :type 'string)
344 (defcustom org-clock-string "CLOCK:"
345 "String used as prefix for timestamps clocking work hours on an item."
346 :group 'org-keywords
347 :type 'string)
349 (defcustom org-comment-string "COMMENT"
350 "Entries starting with this keyword will never be exported.
351 An entry can be toggled between COMMENT and normal with
352 \\[org-toggle-comment].
353 Changes become only effective after restarting Emacs."
354 :group 'org-keywords
355 :type 'string)
357 (defcustom org-quote-string "QUOTE"
358 "Entries starting with this keyword will be exported in fixed-width font.
359 Quoting applies only to the text in the entry following the headline, and does
360 not extend beyond the next headline, even if that is lower level.
361 An entry can be toggled between QUOTE and normal with
362 \\[org-toggle-fixed-width-section]."
363 :group 'org-keywords
364 :type 'string)
366 (defconst org-repeat-re
367 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
368 "Regular expression for specifying repeated events.
369 After a match, group 1 contains the repeat expression.")
371 (defgroup org-structure nil
372 "Options concerning the general structure of Org-mode files."
373 :tag "Org Structure"
374 :group 'org)
376 (defgroup org-reveal-location nil
377 "Options about how to make context of a location visible."
378 :tag "Org Reveal Location"
379 :group 'org-structure)
381 (defconst org-context-choice
382 '(choice
383 (const :tag "Always" t)
384 (const :tag "Never" nil)
385 (repeat :greedy t :tag "Individual contexts"
386 (cons
387 (choice :tag "Context"
388 (const agenda)
389 (const org-goto)
390 (const occur-tree)
391 (const tags-tree)
392 (const link-search)
393 (const mark-goto)
394 (const bookmark-jump)
395 (const isearch)
396 (const default))
397 (boolean))))
398 "Contexts for the reveal options.")
400 (defcustom org-show-hierarchy-above '((default . t))
401 "Non-nil means, show full hierarchy when revealing a location.
402 Org-mode often shows locations in an org-mode file which might have
403 been invisible before. When this is set, the hierarchy of headings
404 above the exposed location is shown.
405 Turning this off for example for sparse trees makes them very compact.
406 Instead of t, this can also be an alist specifying this option for different
407 contexts. Valid contexts are
408 agenda when exposing an entry from the agenda
409 org-goto when using the command `org-goto' on key C-c C-j
410 occur-tree when using the command `org-occur' on key C-c /
411 tags-tree when constructing a sparse tree based on tags matches
412 link-search when exposing search matches associated with a link
413 mark-goto when exposing the jump goal of a mark
414 bookmark-jump when exposing a bookmark location
415 isearch when exiting from an incremental search
416 default default for all contexts not set explicitly"
417 :group 'org-reveal-location
418 :type org-context-choice)
420 (defcustom org-show-following-heading '((default . nil))
421 "Non-nil means, show following heading when revealing a location.
422 Org-mode often shows locations in an org-mode file which might have
423 been invisible before. When this is set, the heading following the
424 match is shown.
425 Turning this off for example for sparse trees makes them very compact,
426 but makes it harder to edit the location of the match. In such a case,
427 use the command \\[org-reveal] to show more context.
428 Instead of t, this can also be an alist specifying this option for different
429 contexts. See `org-show-hierarchy-above' for valid contexts."
430 :group 'org-reveal-location
431 :type org-context-choice)
433 (defcustom org-show-siblings '((default . nil) (isearch t))
434 "Non-nil means, show all sibling heading when revealing a location.
435 Org-mode often shows locations in an org-mode file which might have
436 been invisible before. When this is set, the sibling of the current entry
437 heading are all made visible. If `org-show-hierarchy-above' is t,
438 the same happens on each level of the hierarchy above the current entry.
440 By default this is on for the isearch context, off for all other contexts.
441 Turning this off for example for sparse trees makes them very compact,
442 but makes it harder to edit the location of the match. In such a case,
443 use the command \\[org-reveal] to show more context.
444 Instead of t, this can also be an alist specifying this option for different
445 contexts. See `org-show-hierarchy-above' for valid contexts."
446 :group 'org-reveal-location
447 :type org-context-choice)
449 (defcustom org-show-entry-below '((default . nil))
450 "Non-nil means, show the entry below a headline when revealing a location.
451 Org-mode often shows locations in an org-mode file which might have
452 been invisible before. When this is set, the text below the headline that is
453 exposed is also shown.
455 By default this is off for all contexts.
456 Instead of t, this can also be an alist specifying this option for different
457 contexts. See `org-show-hierarchy-above' for valid contexts."
458 :group 'org-reveal-location
459 :type org-context-choice)
461 (defcustom org-indirect-buffer-display 'other-window
462 "How should indirect tree buffers be displayed?
463 This applies to indirect buffers created with the commands
464 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
465 Valid values are:
466 current-window Display in the current window
467 other-window Just display in another window.
468 dedicated-frame Create one new frame, and re-use it each time.
469 new-frame Make a new frame each time. Note that in this case
470 previously-made indirect buffers are kept, and you need to
471 kill these buffers yourself."
472 :group 'org-structure
473 :group 'org-agenda-windows
474 :type '(choice
475 (const :tag "In current window" current-window)
476 (const :tag "In current frame, other window" other-window)
477 (const :tag "Each time a new frame" new-frame)
478 (const :tag "One dedicated frame" dedicated-frame)))
480 (defgroup org-cycle nil
481 "Options concerning visibility cycling in Org-mode."
482 :tag "Org Cycle"
483 :group 'org-structure)
485 (defcustom org-drawers '("PROPERTIES" "CLOCK")
486 "Names of drawers. Drawers are not opened by cycling on the headline above.
487 Drawers only open with a TAB on the drawer line itself. A drawer looks like
488 this:
489 :DRAWERNAME:
490 .....
491 :END:
492 The drawer \"PROPERTIES\" is special for capturing properties through
493 the property API.
495 Drawers can be defined on the per-file basis with a line like:
497 #+DRAWERS: HIDDEN STATE PROPERTIES"
498 :group 'org-structure
499 :type '(repeat (string :tag "Drawer Name")))
501 (defcustom org-cycle-global-at-bob nil
502 "Cycle globally if cursor is at beginning of buffer and not at a headline.
503 This makes it possible to do global cycling without having to use S-TAB or
504 C-u TAB. For this special case to work, the first line of the buffer
505 must not be a headline - it may be empty ot some other text. When used in
506 this way, `org-cycle-hook' is disables temporarily, to make sure the
507 cursor stays at the beginning of the buffer.
508 When this option is nil, don't do anything special at the beginning
509 of the buffer."
510 :group 'org-cycle
511 :type 'boolean)
513 (defcustom org-cycle-emulate-tab t
514 "Where should `org-cycle' emulate TAB.
515 nil Never
516 white Only in completely white lines
517 whitestart Only at the beginning of lines, before the first non-white char
518 t Everywhere except in headlines
519 exc-hl-bol Everywhere except at the start of a headline
520 If TAB is used in a place where it does not emulate TAB, the current subtree
521 visibility is cycled."
522 :group 'org-cycle
523 :type '(choice (const :tag "Never" nil)
524 (const :tag "Only in completely white lines" white)
525 (const :tag "Before first char in a line" whitestart)
526 (const :tag "Everywhere except in headlines" t)
527 (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
530 (defcustom org-cycle-separator-lines 2
531 "Number of empty lines needed to keep an empty line between collapsed trees.
532 If you leave an empty line between the end of a subtree and the following
533 headline, this empty line is hidden when the subtree is folded.
534 Org-mode will leave (exactly) one empty line visible if the number of
535 empty lines is equal or larger to the number given in this variable.
536 So the default 2 means, at least 2 empty lines after the end of a subtree
537 are needed to produce free space between a collapsed subtree and the
538 following headline.
540 Special case: when 0, never leave empty lines in collapsed view."
541 :group 'org-cycle
542 :type 'integer)
543 (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
545 (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
546 org-cycle-hide-drawers
547 org-cycle-show-empty-lines
548 org-optimize-window-after-visibility-change)
549 "Hook that is run after `org-cycle' has changed the buffer visibility.
550 The function(s) in this hook must accept a single argument which indicates
551 the new state that was set by the most recent `org-cycle' command. The
552 argument is a symbol. After a global state change, it can have the values
553 `overview', `content', or `all'. After a local state change, it can have
554 the values `folded', `children', or `subtree'."
555 :group 'org-cycle
556 :type 'hook)
558 (defgroup org-edit-structure nil
559 "Options concerning structure editing in Org-mode."
560 :tag "Org Edit Structure"
561 :group 'org-structure)
563 (defcustom org-odd-levels-only nil
564 "Non-nil means, skip even levels and only use odd levels for the outline.
565 This has the effect that two stars are being added/taken away in
566 promotion/demotion commands. It also influences how levels are
567 handled by the exporters.
568 Changing it requires restart of `font-lock-mode' to become effective
569 for fontification also in regions already fontified.
570 You may also set this on a per-file basis by adding one of the following
571 lines to the buffer:
573 #+STARTUP: odd
574 #+STARTUP: oddeven"
575 :group 'org-edit-structure
576 :group 'org-font-lock
577 :type 'boolean)
579 (defcustom org-adapt-indentation t
580 "Non-nil means, adapt indentation when promoting and demoting.
581 When this is set and the *entire* text in an entry is indented, the
582 indentation is increased by one space in a demotion command, and
583 decreased by one in a promotion command. If any line in the entry
584 body starts at column 0, indentation is not changed at all."
585 :group 'org-edit-structure
586 :type 'boolean)
588 (defcustom org-special-ctrl-a/e nil
589 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
590 When t, `C-a' will bring back the cursor to the beginning of the
591 headline text, i.e. after the stars and after a possible TODO keyword.
592 In an item, this will be the position after the bullet.
593 When the cursor is already at that position, another `C-a' will bring
594 it to the beginning of the line.
595 `C-e' will jump to the end of the headline, ignoring the presence of tags
596 in the headline. A second `C-e' will then jump to the true end of the
597 line, after any tags.
598 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
599 and only a directly following, identical keypress will bring the cursor
600 to the special positions."
601 :group 'org-edit-structure
602 :type '(choice
603 (const :tag "off" nil)
604 (const :tag "after bullet first" t)
605 (const :tag "border first" reversed)))
607 (if (fboundp 'defvaralias)
608 (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
610 (defcustom org-special-ctrl-k nil
611 "Non-nil means `C-k' will behave specially in headlines.
612 When nil, `C-k' will call the default `kill-line' command.
613 When t, the following will happen while the cursor is in the headline:
615 - When the cursor is at the beginning of a headline, kill the entire
616 line and possible the folded subtree below the line.
617 - When in the middle of the headline text, kill the headline up to the tags.
618 - When after the headline text, kill the tags."
619 :group 'org-edit-structure
620 :type 'boolean)
622 (defcustom org-M-RET-may-split-line '((default . t))
623 "Non-nil means, M-RET will split the line at the cursor position.
624 When nil, it will go to the end of the line before making a
625 new line.
626 You may also set this option in a different way for different
627 contexts. Valid contexts are:
629 headline when creating a new headline
630 item when creating a new item
631 table in a table field
632 default the value to be used for all contexts not explicitly
633 customized"
634 :group 'org-structure
635 :group 'org-table
636 :type '(choice
637 (const :tag "Always" t)
638 (const :tag "Never" nil)
639 (repeat :greedy t :tag "Individual contexts"
640 (cons
641 (choice :tag "Context"
642 (const headline)
643 (const item)
644 (const table)
645 (const default))
646 (boolean)))))
649 (defcustom org-blank-before-new-entry '((heading . nil)
650 (plain-list-item . nil))
651 "Should `org-insert-heading' leave a blank line before new heading/item?
652 The value is an alist, with `heading' and `plain-list-item' as car,
653 and a boolean flag as cdr."
654 :group 'org-edit-structure
655 :type '(list
656 (cons (const heading) (boolean))
657 (cons (const plain-list-item) (boolean))))
659 (defcustom org-insert-heading-hook nil
660 "Hook being run after inserting a new heading."
661 :group 'org-edit-structure
662 :type 'hook)
664 (defcustom org-enable-fixed-width-editor t
665 "Non-nil means, lines starting with \":\" are treated as fixed-width.
666 This currently only means, they are never auto-wrapped.
667 When nil, such lines will be treated like ordinary lines.
668 See also the QUOTE keyword."
669 :group 'org-edit-structure
670 :type 'boolean)
672 (defcustom org-goto-auto-isearch t
673 "Non-nil means, typing characters in org-goto starts incremental search."
674 :group 'org-edit-structure
675 :type 'boolean)
677 (defgroup org-sparse-trees nil
678 "Options concerning sparse trees in Org-mode."
679 :tag "Org Sparse Trees"
680 :group 'org-structure)
682 (defcustom org-highlight-sparse-tree-matches t
683 "Non-nil means, highlight all matches that define a sparse tree.
684 The highlights will automatically disappear the next time the buffer is
685 changed by an edit command."
686 :group 'org-sparse-trees
687 :type 'boolean)
689 (defcustom org-remove-highlights-with-change t
690 "Non-nil means, any change to the buffer will remove temporary highlights.
691 Such highlights are created by `org-occur' and `org-clock-display'.
692 When nil, `C-c C-c needs to be used to get rid of the highlights.
693 The highlights created by `org-preview-latex-fragment' always need
694 `C-c C-c' to be removed."
695 :group 'org-sparse-trees
696 :group 'org-time
697 :type 'boolean)
700 (defcustom org-occur-hook '(org-first-headline-recenter)
701 "Hook that is run after `org-occur' has constructed a sparse tree.
702 This can be used to recenter the window to show as much of the structure
703 as possible."
704 :group 'org-sparse-trees
705 :type 'hook)
707 (defgroup org-plain-lists nil
708 "Options concerning plain lists in Org-mode."
709 :tag "Org Plain lists"
710 :group 'org-structure)
712 (defcustom org-cycle-include-plain-lists nil
713 "Non-nil means, include plain lists into visibility cycling.
714 This means that during cycling, plain list items will *temporarily* be
715 interpreted as outline headlines with a level given by 1000+i where i is the
716 indentation of the bullet. In all other operations, plain list items are
717 not seen as headlines. For example, you cannot assign a TODO keyword to
718 such an item."
719 :group 'org-plain-lists
720 :type 'boolean)
722 (defcustom org-plain-list-ordered-item-terminator t
723 "The character that makes a line with leading number an ordered list item.
724 Valid values are ?. and ?\). To get both terminators, use t. While
725 ?. may look nicer, it creates the danger that a line with leading
726 number may be incorrectly interpreted as an item. ?\) therefore is
727 the safe choice."
728 :group 'org-plain-lists
729 :type '(choice (const :tag "dot like in \"2.\"" ?.)
730 (const :tag "paren like in \"2)\"" ?\))
731 (const :tab "both" t)))
733 (defcustom org-empty-line-terminates-plain-lists nil
734 "Non-nil means, an empty line ends all plain list levels.
735 When nil, empty lines are part of the preceeding item."
736 :group 'org-plain-lists
737 :type 'boolean)
739 (defcustom org-auto-renumber-ordered-lists t
740 "Non-nil means, automatically renumber ordered plain lists.
741 Renumbering happens when the sequence have been changed with
742 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
743 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
744 :group 'org-plain-lists
745 :type 'boolean)
747 (defcustom org-provide-checkbox-statistics t
748 "Non-nil means, update checkbox statistics after insert and toggle.
749 When this is set, checkbox statistics is updated each time you either insert
750 a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
751 with \\[org-ctrl-c-ctrl-c\\]."
752 :group 'org-plain-lists
753 :type 'boolean)
755 (defcustom org-description-max-indent 20
756 "Maximum indentation for the second line of a description list.
757 When the indentation would be larger than this, it will become
758 5 characters instead."
759 :group 'org-plain-lists
760 :type 'integer)
762 (defgroup org-imenu-and-speedbar nil
763 "Options concerning imenu and speedbar in Org-mode."
764 :tag "Org Imenu and Speedbar"
765 :group 'org-structure)
767 (defcustom org-imenu-depth 2
768 "The maximum level for Imenu access to Org-mode headlines.
769 This also applied for speedbar access."
770 :group 'org-imenu-and-speedbar
771 :type 'number)
773 (defgroup org-table nil
774 "Options concerning tables in Org-mode."
775 :tag "Org Table"
776 :group 'org)
778 (defcustom org-enable-table-editor 'optimized
779 "Non-nil means, lines starting with \"|\" are handled by the table editor.
780 When nil, such lines will be treated like ordinary lines.
782 When equal to the symbol `optimized', the table editor will be optimized to
783 do the following:
784 - Automatic overwrite mode in front of whitespace in table fields.
785 This makes the structure of the table stay in tact as long as the edited
786 field does not exceed the column width.
787 - Minimize the number of realigns. Normally, the table is aligned each time
788 TAB or RET are pressed to move to another field. With optimization this
789 happens only if changes to a field might have changed the column width.
790 Optimization requires replacing the functions `self-insert-command',
791 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
792 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
793 very good at guessing when a re-align will be necessary, but you can always
794 force one with \\[org-ctrl-c-ctrl-c].
796 If you would like to use the optimized version in Org-mode, but the
797 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
799 This variable can be used to turn on and off the table editor during a session,
800 but in order to toggle optimization, a restart is required.
802 See also the variable `org-table-auto-blank-field'."
803 :group 'org-table
804 :type '(choice
805 (const :tag "off" nil)
806 (const :tag "on" t)
807 (const :tag "on, optimized" optimized)))
809 (defcustom org-table-tab-recognizes-table.el t
810 "Non-nil means, TAB will automatically notice a table.el table.
811 When it sees such a table, it moves point into it and - if necessary -
812 calls `table-recognize-table'."
813 :group 'org-table-editing
814 :type 'boolean)
816 (defgroup org-link nil
817 "Options concerning links in Org-mode."
818 :tag "Org Link"
819 :group 'org)
821 (defvar org-link-abbrev-alist-local nil
822 "Buffer-local version of `org-link-abbrev-alist', which see.
823 The value of this is taken from the #+LINK lines.")
824 (make-variable-buffer-local 'org-link-abbrev-alist-local)
826 (defcustom org-link-abbrev-alist nil
827 "Alist of link abbreviations.
828 The car of each element is a string, to be replaced at the start of a link.
829 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
830 links in Org-mode buffers can have an optional tag after a double colon, e.g.
832 [[linkkey:tag][description]]
834 If REPLACE is a string, the tag will simply be appended to create the link.
835 If the string contains \"%s\", the tag will be inserted there.
837 REPLACE may also be a function that will be called with the tag as the
838 only argument to create the link, which should be returned as a string.
840 See the manual for examples."
841 :group 'org-link
842 :type 'alist)
844 (defcustom org-descriptive-links t
845 "Non-nil means, hide link part and only show description of bracket links.
846 Bracket links are like [[link][descritpion]]. This variable sets the initial
847 state in new org-mode buffers. The setting can then be toggled on a
848 per-buffer basis from the Org->Hyperlinks menu."
849 :group 'org-link
850 :type 'boolean)
852 (defcustom org-link-file-path-type 'adaptive
853 "How the path name in file links should be stored.
854 Valid values are:
856 relative Relative to the current directory, i.e. the directory of the file
857 into which the link is being inserted.
858 absolute Absolute path, if possible with ~ for home directory.
859 noabbrev Absolute path, no abbreviation of home directory.
860 adaptive Use relative path for files in the current directory and sub-
861 directories of it. For other files, use an absolute path."
862 :group 'org-link
863 :type '(choice
864 (const relative)
865 (const absolute)
866 (const noabbrev)
867 (const adaptive)))
869 (defcustom org-activate-links '(bracket angle plain radio tag date)
870 "Types of links that should be activated in Org-mode files.
871 This is a list of symbols, each leading to the activation of a certain link
872 type. In principle, it does not hurt to turn on most link types - there may
873 be a small gain when turning off unused link types. The types are:
875 bracket The recommended [[link][description]] or [[link]] links with hiding.
876 angular Links in angular brackes that may contain whitespace like
877 <bbdb:Carsten Dominik>.
878 plain Plain links in normal text, no whitespace, like http://google.com.
879 radio Text that is matched by a radio target, see manual for details.
880 tag Tag settings in a headline (link to tag search).
881 date Time stamps (link to calendar).
883 Changing this variable requires a restart of Emacs to become effective."
884 :group 'org-link
885 :type '(set (const :tag "Double bracket links (new style)" bracket)
886 (const :tag "Angular bracket links (old style)" angular)
887 (const :tag "Plain text links" plain)
888 (const :tag "Radio target matches" radio)
889 (const :tag "Tags" tag)
890 (const :tag "Timestamps" date)))
892 (defcustom org-make-link-description-function nil
893 "Function to use to generate link descriptions from links. If
894 nil the link location will be used. This function must take two
895 parameters; the first is the link and the second the description
896 org-insert-link has generated, and should return the description
897 to use."
898 :group 'org-link
899 :type 'function)
901 (defgroup org-link-store nil
902 "Options concerning storing links in Org-mode."
903 :tag "Org Store Link"
904 :group 'org-link)
906 (defcustom org-email-link-description-format "Email %c: %.30s"
907 "Format of the description part of a link to an email or usenet message.
908 The following %-excapes will be replaced by corresponding information:
910 %F full \"From\" field
911 %f name, taken from \"From\" field, address if no name
912 %T full \"To\" field
913 %t first name in \"To\" field, address if no name
914 %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
915 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
916 %s subject
917 %m message-id.
919 You may use normal field width specification between the % and the letter.
920 This is for example useful to limit the length of the subject.
922 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
923 :group 'org-link-store
924 :type 'string)
926 (defcustom org-from-is-user-regexp
927 (let (r1 r2)
928 (when (and user-mail-address (not (string= user-mail-address "")))
929 (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
930 (when (and user-full-name (not (string= user-full-name "")))
931 (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
932 (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
933 "Regexp mached against the \"From:\" header of an email or usenet message.
934 It should match if the message is from the user him/herself."
935 :group 'org-link-store
936 :type 'regexp)
938 (defcustom org-context-in-file-links t
939 "Non-nil means, file links from `org-store-link' contain context.
940 A search string will be added to the file name with :: as separator and
941 used to find the context when the link is activated by the command
942 `org-open-at-point'.
943 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
944 negates this setting for the duration of the command."
945 :group 'org-link-store
946 :type 'boolean)
948 (defcustom org-keep-stored-link-after-insertion nil
949 "Non-nil means, keep link in list for entire session.
951 The command `org-store-link' adds a link pointing to the current
952 location to an internal list. These links accumulate during a session.
953 The command `org-insert-link' can be used to insert links into any
954 Org-mode file (offering completion for all stored links). When this
955 option is nil, every link which has been inserted once using \\[org-insert-link]
956 will be removed from the list, to make completing the unused links
957 more efficient."
958 :group 'org-link-store
959 :type 'boolean)
961 (defgroup org-link-follow nil
962 "Options concerning following links in Org-mode."
963 :tag "Org Follow Link"
964 :group 'org-link)
966 (defcustom org-follow-link-hook nil
967 "Hook that is run after a link has been followed."
968 :group 'org-link-follow
969 :type 'hook)
971 (defcustom org-tab-follows-link nil
972 "Non-nil means, on links TAB will follow the link.
973 Needs to be set before org.el is loaded."
974 :group 'org-link-follow
975 :type 'boolean)
977 (defcustom org-return-follows-link nil
978 "Non-nil means, on links RET will follow the link.
979 Needs to be set before org.el is loaded."
980 :group 'org-link-follow
981 :type 'boolean)
983 (defcustom org-mouse-1-follows-link
984 (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
985 "Non-nil means, mouse-1 on a link will follow the link.
986 A longer mouse click will still set point. Does not work on XEmacs.
987 Needs to be set before org.el is loaded."
988 :group 'org-link-follow
989 :type 'boolean)
991 (defcustom org-mark-ring-length 4
992 "Number of different positions to be recorded in the ring
993 Changing this requires a restart of Emacs to work correctly."
994 :group 'org-link-follow
995 :type 'interger)
997 (defcustom org-link-frame-setup
998 '((vm . vm-visit-folder-other-frame)
999 (gnus . gnus-other-frame)
1000 (file . find-file-other-window))
1001 "Setup the frame configuration for following links.
1002 When following a link with Emacs, it may often be useful to display
1003 this link in another window or frame. This variable can be used to
1004 set this up for the different types of links.
1005 For VM, use any of
1006 `vm-visit-folder'
1007 `vm-visit-folder-other-frame'
1008 For Gnus, use any of
1009 `gnus'
1010 `gnus-other-frame'
1011 For FILE, use any of
1012 `find-file'
1013 `find-file-other-window'
1014 `find-file-other-frame'
1015 For the calendar, use the variable `calendar-setup'.
1016 For BBDB, it is currently only possible to display the matches in
1017 another window."
1018 :group 'org-link-follow
1019 :type '(list
1020 (cons (const vm)
1021 (choice
1022 (const vm-visit-folder)
1023 (const vm-visit-folder-other-window)
1024 (const vm-visit-folder-other-frame)))
1025 (cons (const gnus)
1026 (choice
1027 (const gnus)
1028 (const gnus-other-frame)))
1029 (cons (const file)
1030 (choice
1031 (const find-file)
1032 (const find-file-other-window)
1033 (const find-file-other-frame)))))
1035 (defcustom org-display-internal-link-with-indirect-buffer nil
1036 "Non-nil means, use indirect buffer to display infile links.
1037 Activating internal links (from one location in a file to another location
1038 in the same file) normally just jumps to the location. When the link is
1039 activated with a C-u prefix (or with mouse-3), the link is displayed in
1040 another window. When this option is set, the other window actually displays
1041 an indirect buffer clone of the current buffer, to avoid any visibility
1042 changes to the current buffer."
1043 :group 'org-link-follow
1044 :type 'boolean)
1046 (defcustom org-open-non-existing-files nil
1047 "Non-nil means, `org-open-file' will open non-existing files.
1048 When nil, an error will be generated."
1049 :group 'org-link-follow
1050 :type 'boolean)
1052 (defcustom org-open-directory-means-index-dot-org nil
1053 "Non-nil means, a link to a directory really means to index.org.
1054 When nil, following a directory link will run dired or open a finder/explorer
1055 window on that directory."
1056 :group 'org-link-follow
1057 :type 'boolean)
1059 (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
1060 "Function and arguments to call for following mailto links.
1061 This is a list with the first element being a lisp function, and the
1062 remaining elements being arguments to the function. In string arguments,
1063 %a will be replaced by the address, and %s will be replaced by the subject
1064 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1065 :group 'org-link-follow
1066 :type '(choice
1067 (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1068 (const :tag "compose-mail" (compose-mail "%a" "%s"))
1069 (const :tag "message-mail" (message-mail "%a" "%s"))
1070 (cons :tag "other" (function) (repeat :tag "argument" sexp))))
1072 (defcustom org-confirm-shell-link-function 'yes-or-no-p
1073 "Non-nil means, ask for confirmation before executing shell links.
1074 Shell links can be dangerous: just think about a link
1076 [[shell:rm -rf ~/*][Google Search]]
1078 This link would show up in your Org-mode document as \"Google Search\",
1079 but really it would remove your entire home directory.
1080 Therefore we advise against setting this variable to nil.
1081 Just change it to `y-or-n-p' of you want to confirm with a
1082 single keystroke rather than having to type \"yes\"."
1083 :group 'org-link-follow
1084 :type '(choice
1085 (const :tag "with yes-or-no (safer)" yes-or-no-p)
1086 (const :tag "with y-or-n (faster)" y-or-n-p)
1087 (const :tag "no confirmation (dangerous)" nil)))
1089 (defcustom org-confirm-elisp-link-function 'yes-or-no-p
1090 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1091 Elisp links can be dangerous: just think about a link
1093 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1095 This link would show up in your Org-mode document as \"Google Search\",
1096 but really it would remove your entire home directory.
1097 Therefore we advise against setting this variable to nil.
1098 Just change it to `y-or-n-p' of you want to confirm with a
1099 single keystroke rather than having to type \"yes\"."
1100 :group 'org-link-follow
1101 :type '(choice
1102 (const :tag "with yes-or-no (safer)" yes-or-no-p)
1103 (const :tag "with y-or-n (faster)" y-or-n-p)
1104 (const :tag "no confirmation (dangerous)" nil)))
1106 (defconst org-file-apps-defaults-gnu
1107 '((remote . emacs)
1108 (t . mailcap))
1109 "Default file applications on a UNIX or GNU/Linux system.
1110 See `org-file-apps'.")
1112 (defconst org-file-apps-defaults-macosx
1113 '((remote . emacs)
1114 (t . "open %s")
1115 ("ps" . "gv %s")
1116 ("ps.gz" . "gv %s")
1117 ("eps" . "gv %s")
1118 ("eps.gz" . "gv %s")
1119 ("dvi" . "xdvi %s")
1120 ("fig" . "xfig %s"))
1121 "Default file applications on a MacOS X system.
1122 The system \"open\" is known as a default, but we use X11 applications
1123 for some files for which the OS does not have a good default.
1124 See `org-file-apps'.")
1126 (defconst org-file-apps-defaults-windowsnt
1127 (list
1128 '(remote . emacs)
1129 (cons t
1130 (list (if (featurep 'xemacs)
1131 'mswindows-shell-execute
1132 'w32-shell-execute)
1133 "open" 'file)))
1134 "Default file applications on a Windows NT system.
1135 The system \"open\" is used for most files.
1136 See `org-file-apps'.")
1138 (defcustom org-file-apps
1140 ("txt" . emacs)
1141 ("tex" . emacs)
1142 ("ltx" . emacs)
1143 ("org" . emacs)
1144 ("el" . emacs)
1145 ("bib" . emacs)
1147 "External applications for opening `file:path' items in a document.
1148 Org-mode uses system defaults for different file types, but
1149 you can use this variable to set the application for a given file
1150 extension. The entries in this list are cons cells where the car identifies
1151 files and the cdr the corresponding command. Possible values for the
1152 file identifier are
1153 \"ext\" A string identifying an extension
1154 `directory' Matches a directory
1155 `remote' Matches a remote file, accessible through tramp or efs.
1156 Remote files most likely should be visited through Emacs
1157 because external applications cannot handle such paths.
1158 t Default for all remaining files
1160 Possible values for the command are:
1161 `emacs' The file will be visited by the current Emacs process.
1162 `default' Use the default application for this file type.
1163 string A command to be executed by a shell; %s will be replaced
1164 by the path to the file.
1165 sexp A Lisp form which will be evaluated. The file path will
1166 be available in the Lisp variable `file'.
1167 For more examples, see the system specific constants
1168 `org-file-apps-defaults-macosx'
1169 `org-file-apps-defaults-windowsnt'
1170 `org-file-apps-defaults-gnu'."
1171 :group 'org-link-follow
1172 :type '(repeat
1173 (cons (choice :value ""
1174 (string :tag "Extension")
1175 (const :tag "Default for unrecognized files" t)
1176 (const :tag "Remote file" remote)
1177 (const :tag "Links to a directory" directory))
1178 (choice :value ""
1179 (const :tag "Visit with Emacs" emacs)
1180 (const :tag "Use system default" default)
1181 (string :tag "Command")
1182 (sexp :tag "Lisp form")))))
1184 (defgroup org-refile nil
1185 "Options concerning refiling entries in Org-mode."
1186 :tag "Org Remember"
1187 :group 'org)
1189 (defcustom org-directory "~/org"
1190 "Directory with org files.
1191 This directory will be used as default to prompt for org files.
1192 Used by the hooks for remember.el."
1193 :group 'org-refile
1194 :group 'org-remember
1195 :type 'directory)
1197 (defcustom org-default-notes-file "~/.notes"
1198 "Default target for storing notes.
1199 Used by the hooks for remember.el. This can be a string, or nil to mean
1200 the value of `remember-data-file'.
1201 You can set this on a per-template basis with the variable
1202 `org-remember-templates'."
1203 :group 'org-refile
1204 :group 'org-remember
1205 :type '(choice
1206 (const :tag "Default from remember-data-file" nil)
1207 file))
1209 (defcustom org-goto-interface 'outline
1210 "The default interface to be used for `org-goto'.
1211 Allowed vaues are:
1212 outline The interface shows an outline of the relevant file
1213 and the correct heading is found by moving through
1214 the outline or by searching with incremental search.
1215 outline-path-completion Headlines in the current buffer are offered via
1216 completion."
1217 :group 'org-refile
1218 :type '(choice
1219 (const :tag "Outline" outline)
1220 (const :tag "Outline-path-completion" outline-path-completion)))
1222 (defcustom org-reverse-note-order nil
1223 "Non-nil means, store new notes at the beginning of a file or entry.
1224 When nil, new notes will be filed to the end of a file or entry.
1225 This can also be a list with cons cells of regular expressions that
1226 are matched against file names, and values."
1227 :group 'org-remember
1228 :type '(choice
1229 (const :tag "Reverse always" t)
1230 (const :tag "Reverse never" nil)
1231 (repeat :tag "By file name regexp"
1232 (cons regexp boolean))))
1234 (defcustom org-refile-targets nil
1235 "Targets for refiling entries with \\[org-refile].
1236 This is list of cons cells. Each cell contains:
1237 - a specification of the files to be considered, either a list of files,
1238 or a symbol whose function or variable value will be used to retrieve
1239 a file name or a list of file names. Nil means, refile to a different
1240 heading in the current buffer.
1241 - A specification of how to find candidate refile targets. This may be
1242 any of
1243 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1244 This tag has to be present in all target headlines, inheritance will
1245 not be considered.
1246 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1247 todo keyword.
1248 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1249 headlines that are refiling targets.
1250 - a cons cell (:level . N). Any headline of level N is considered a target.
1251 - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
1252 :group 'org-remember
1253 :type '(repeat
1254 (cons
1255 (choice :value org-agenda-files
1256 (const :tag "All agenda files" org-agenda-files)
1257 (const :tag "Current buffer" nil)
1258 (function) (variable) (file))
1259 (choice :tag "Identify target headline by"
1260 (cons :tag "Specific tag" (const :tag) (string))
1261 (cons :tag "TODO keyword" (const :todo) (string))
1262 (cons :tag "Regular expression" (const :regexp) (regexp))
1263 (cons :tag "Level number" (const :level) (integer))
1264 (cons :tag "Max Level number" (const :maxlevel) (integer))))))
1266 (defcustom org-refile-use-outline-path nil
1267 "Non-nil means, provide refile targets as paths.
1268 So a level 3 headline will be available as level1/level2/level3.
1269 When the value is `file', also include the file name (without directory)
1270 into the path. When `full-file-path', include the full file path."
1271 :group 'org-remember
1272 :type '(choice
1273 (const :tag "Not" nil)
1274 (const :tag "Yes" t)
1275 (const :tag "Start with file name" file)
1276 (const :tag "Start with full file path" full-file-path)))
1278 (defgroup org-todo nil
1279 "Options concerning TODO items in Org-mode."
1280 :tag "Org TODO"
1281 :group 'org)
1283 (defgroup org-progress nil
1284 "Options concerning Progress logging in Org-mode."
1285 :tag "Org Progress"
1286 :group 'org-time)
1288 (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
1289 "List of TODO entry keyword sequences and their interpretation.
1290 \\<org-mode-map>This is a list of sequences.
1292 Each sequence starts with a symbol, either `sequence' or `type',
1293 indicating if the keywords should be interpreted as a sequence of
1294 action steps, or as different types of TODO items. The first
1295 keywords are states requiring action - these states will select a headline
1296 for inclusion into the global TODO list Org-mode produces. If one of
1297 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1298 signify that no further action is necessary. If \"|\" is not found,
1299 the last keyword is treated as the only DONE state of the sequence.
1301 The command \\[org-todo] cycles an entry through these states, and one
1302 additional state where no keyword is present. For details about this
1303 cycling, see the manual.
1305 TODO keywords and interpretation can also be set on a per-file basis with
1306 the special #+SEQ_TODO and #+TYP_TODO lines.
1308 Each keyword can optionally specify a character for fast state selection
1309 \(in combination with the variable `org-use-fast-todo-selection')
1310 and specifiers for state change logging, using the same syntax
1311 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1312 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1313 indicates to record a time stamp each time this state is selected.
1315 Each keyword may also specify if a timestamp or a note should be
1316 recorded when entering or leaving the state, by adding additional
1317 characters in the parenthesis after the keyword. This looks like this:
1318 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1319 record only the time of the state change. With X and Y being either
1320 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1321 Y when leaving the state if and only if the *target* state does not
1322 define X. You may omit any of the fast-selection key or X or /Y,
1323 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1325 For backward compatibility, this variable may also be just a list
1326 of keywords - in this case the interptetation (sequence or type) will be
1327 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1328 :group 'org-todo
1329 :group 'org-keywords
1330 :type '(choice
1331 (repeat :tag "Old syntax, just keywords"
1332 (string :tag "Keyword"))
1333 (repeat :tag "New syntax"
1334 (cons
1335 (choice
1336 :tag "Interpretation"
1337 (const :tag "Sequence (cycling hits every state)" sequence)
1338 (const :tag "Type (cycling directly to DONE)" type))
1339 (repeat
1340 (string :tag "Keyword"))))))
1342 (defvar org-todo-keywords-1 nil
1343 "All TODO and DONE keywords active in a buffer.")
1344 (make-variable-buffer-local 'org-todo-keywords-1)
1345 (defvar org-todo-keywords-for-agenda nil)
1346 (defvar org-done-keywords-for-agenda nil)
1347 (defvar org-agenda-contributing-files nil)
1348 (defvar org-not-done-keywords nil)
1349 (make-variable-buffer-local 'org-not-done-keywords)
1350 (defvar org-done-keywords nil)
1351 (make-variable-buffer-local 'org-done-keywords)
1352 (defvar org-todo-heads nil)
1353 (make-variable-buffer-local 'org-todo-heads)
1354 (defvar org-todo-sets nil)
1355 (make-variable-buffer-local 'org-todo-sets)
1356 (defvar org-todo-log-states nil)
1357 (make-variable-buffer-local 'org-todo-log-states)
1358 (defvar org-todo-kwd-alist nil)
1359 (make-variable-buffer-local 'org-todo-kwd-alist)
1360 (defvar org-todo-key-alist nil)
1361 (make-variable-buffer-local 'org-todo-key-alist)
1362 (defvar org-todo-key-trigger nil)
1363 (make-variable-buffer-local 'org-todo-key-trigger)
1365 (defcustom org-todo-interpretation 'sequence
1366 "Controls how TODO keywords are interpreted.
1367 This variable is in principle obsolete and is only used for
1368 backward compatibility, if the interpretation of todo keywords is
1369 not given already in `org-todo-keywords'. See that variable for
1370 more information."
1371 :group 'org-todo
1372 :group 'org-keywords
1373 :type '(choice (const sequence)
1374 (const type)))
1376 (defcustom org-use-fast-todo-selection 'prefix
1377 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1378 This variable describes if and under what circumstances the cycling
1379 mechanism for TODO keywords will be replaced by a single-key, direct
1380 selection scheme.
1382 When nil, fast selection is never used.
1384 When the symbol `prefix', it will be used when `org-todo' is called with
1385 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1386 in an agenda buffer.
1388 When t, fast selection is used by default. In this case, the prefix
1389 argument forces cycling instead.
1391 In all cases, the special interface is only used if access keys have actually
1392 been assigned by the user, i.e. if keywords in the configuration are followed
1393 by a letter in parenthesis, like TODO(t)."
1394 :group 'org-todo
1395 :type '(choice
1396 (const :tag "Never" nil)
1397 (const :tag "By default" t)
1398 (const :tag "Only with C-u C-c C-t" prefix)))
1400 (defcustom org-provide-todo-statistics t
1401 "Non-nil means, update todo statistics after insert and toggle.
1402 When this is set, todo statistics is updated in the parent of the current
1403 entry each time a todo state is changed."
1404 :group 'org-todo
1405 :type 'boolean)
1407 (defcustom org-after-todo-state-change-hook nil
1408 "Hook which is run after the state of a TODO item was changed.
1409 The new state (a string with a TODO keyword, or nil) is available in the
1410 Lisp variable `state'."
1411 :group 'org-todo
1412 :type 'hook)
1414 (defcustom org-log-done nil
1415 "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
1416 When equal to the list (done), also prompt for a closing note.
1417 This can also be configured on a per-file basis by adding one of
1418 the following lines anywhere in the buffer:
1420 #+STARTUP: logdone
1421 #+STARTUP: lognotedone
1422 #+STARTUP: nologdone"
1423 :group 'org-todo
1424 :group 'org-progress
1425 :type '(choice
1426 (const :tag "No logging" nil)
1427 (const :tag "Record CLOSED timestamp" time)
1428 (const :tag "Record CLOSED timestamp with closing note." note)))
1430 ;; Normalize old uses of org-log-done.
1431 (cond
1432 ((eq org-log-done t) (setq org-log-done 'time))
1433 ((and (listp org-log-done) (memq 'done org-log-done))
1434 (setq org-log-done 'note)))
1436 (defcustom org-log-note-clock-out nil
1437 "Non-nil means, record a note when clocking out of an item.
1438 This can also be configured on a per-file basis by adding one of
1439 the following lines anywhere in the buffer:
1441 #+STARTUP: lognoteclock-out
1442 #+STARTUP: nolognoteclock-out"
1443 :group 'org-todo
1444 :group 'org-progress
1445 :type 'boolean)
1447 (defcustom org-log-done-with-time t
1448 "Non-nil means, the CLOSED time stamp will contain date and time.
1449 When nil, only the date will be recorded."
1450 :group 'org-progress
1451 :type 'boolean)
1453 (defcustom org-log-note-headings
1454 '((done . "CLOSING NOTE %t")
1455 (state . "State %-12s %t")
1456 (note . "Note taken on %t")
1457 (clock-out . ""))
1458 "Headings for notes added to entries.
1459 The value is an alist, with the car being a symbol indicating the note
1460 context, and the cdr is the heading to be used. The heading may also be the
1461 empty string.
1462 %t in the heading will be replaced by a time stamp.
1463 %s will be replaced by the new TODO state, in double quotes.
1464 %u will be replaced by the user name.
1465 %U will be replaced by the full user name."
1466 :group 'org-todo
1467 :group 'org-progress
1468 :type '(list :greedy t
1469 (cons (const :tag "Heading when closing an item" done) string)
1470 (cons (const :tag
1471 "Heading when changing todo state (todo sequence only)"
1472 state) string)
1473 (cons (const :tag "Heading when just taking a note" note) string)
1474 (cons (const :tag "Heading when clocking out" clock-out) string)))
1476 (unless (assq 'note org-log-note-headings)
1477 (push '(note . "%t") org-log-note-headings))
1479 (defcustom org-log-states-order-reversed t
1480 "Non-nil means, the latest state change note will be directly after heading.
1481 When nil, the notes will be orderer according to time."
1482 :group 'org-todo
1483 :group 'org-progress
1484 :type 'boolean)
1486 (defcustom org-log-repeat 'time
1487 "Non-nil means, record moving through the DONE state when triggering repeat.
1488 An auto-repeating tasks is immediately switched back to TODO when marked
1489 done. If you are not logging state changes (by adding \"@\" or \"!\" to
1490 the TODO keyword definition, or recording a closing note by setting
1491 `org-log-done', there will be no record of the task moving through DONE.
1492 This variable forces taking a note anyway. Possible values are:
1494 nil Don't force a record
1495 time Record a time stamp
1496 note Record a note
1498 This option can also be set with on a per-file-basis with
1500 #+STARTUP: logrepeat
1501 #+STARTUP: lognoterepeat
1502 #+STARTUP: nologrepeat
1504 You can have local logging settings for a subtree by setting the LOGGING
1505 property to one or more of these keywords."
1506 :group 'org-todo
1507 :group 'org-progress
1508 :type '(choice
1509 (const :tag "Don't force a record" nil)
1510 (const :tag "Force recording the DONE state" time)
1511 (const :tag "Force recording a note with the DONE state" note)))
1514 (defgroup org-priorities nil
1515 "Priorities in Org-mode."
1516 :tag "Org Priorities"
1517 :group 'org-todo)
1519 (defcustom org-highest-priority ?A
1520 "The highest priority of TODO items. A character like ?A, ?B etc.
1521 Must have a smaller ASCII number than `org-lowest-priority'."
1522 :group 'org-priorities
1523 :type 'character)
1525 (defcustom org-lowest-priority ?C
1526 "The lowest priority of TODO items. A character like ?A, ?B etc.
1527 Must have a larger ASCII number than `org-highest-priority'."
1528 :group 'org-priorities
1529 :type 'character)
1531 (defcustom org-default-priority ?B
1532 "The default priority of TODO items.
1533 This is the priority an item get if no explicit priority is given."
1534 :group 'org-priorities
1535 :type 'character)
1537 (defcustom org-priority-start-cycle-with-default t
1538 "Non-nil means, start with default priority when starting to cycle.
1539 When this is nil, the first step in the cycle will be (depending on the
1540 command used) one higher or lower that the default priority."
1541 :group 'org-priorities
1542 :type 'boolean)
1544 (defgroup org-time nil
1545 "Options concerning time stamps and deadlines in Org-mode."
1546 :tag "Org Time"
1547 :group 'org)
1549 (defcustom org-insert-labeled-timestamps-at-point nil
1550 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
1551 When nil, these labeled time stamps are forces into the second line of an
1552 entry, just after the headline. When scheduling from the global TODO list,
1553 the time stamp will always be forced into the second line."
1554 :group 'org-time
1555 :type 'boolean)
1557 (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
1558 "Formats for `format-time-string' which are used for time stamps.
1559 It is not recommended to change this constant.")
1561 (defcustom org-time-stamp-rounding-minutes '(0 5)
1562 "Number of minutes to round time stamps to.
1563 These are two values, the first applies when first creating a time stamp.
1564 The second applies when changing it with the commands `S-up' and `S-down'.
1565 When changing the time stamp, this means that it will change in steps
1566 of N minutes, as given by the second value.
1568 When a setting is 0 or 1, insert the time unmodified. Useful rounding
1569 numbers should be factors of 60, so for example 5, 10, 15.
1571 When this is larger than 1, you can still force an exact time-stamp by using
1572 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
1573 and by using a prefix arg to `S-up/down' to specify the exact number
1574 of minutes to shift."
1575 :group 'org-time
1576 :get '(lambda (var) ; Make sure all entries have 5 elements
1577 (if (integerp (default-value var))
1578 (list (default-value var) 5)
1579 (default-value var)))
1580 :type '(list
1581 (integer :tag "when inserting times")
1582 (integer :tag "when modifying times")))
1584 ;; Normalize old customizations of this variable.
1585 (when (integerp org-time-stamp-rounding-minutes)
1586 (setq org-time-stamp-rounding-minutes
1587 (list org-time-stamp-rounding-minutes
1588 org-time-stamp-rounding-minutes)))
1590 (defcustom org-display-custom-times nil
1591 "Non-nil means, overlay custom formats over all time stamps.
1592 The formats are defined through the variable `org-time-stamp-custom-formats'.
1593 To turn this on on a per-file basis, insert anywhere in the file:
1594 #+STARTUP: customtime"
1595 :group 'org-time
1596 :set 'set-default
1597 :type 'sexp)
1598 (make-variable-buffer-local 'org-display-custom-times)
1600 (defcustom org-time-stamp-custom-formats
1601 '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
1602 "Custom formats for time stamps. See `format-time-string' for the syntax.
1603 These are overlayed over the default ISO format if the variable
1604 `org-display-custom-times' is set. Time like %H:%M should be at the
1605 end of the second format."
1606 :group 'org-time
1607 :type 'sexp)
1609 (defun org-time-stamp-format (&optional long inactive)
1610 "Get the right format for a time string."
1611 (let ((f (if long (cdr org-time-stamp-formats)
1612 (car org-time-stamp-formats))))
1613 (if inactive
1614 (concat "[" (substring f 1 -1) "]")
1615 f)))
1617 (defcustom org-time-clocksum-format "%d:%02d"
1618 "The format string used when creating CLOCKSUM lines, or when
1619 org-mode generates a time duration."
1620 :group 'org-time
1621 :type 'string)
1623 (defcustom org-deadline-warning-days 14
1624 "No. of days before expiration during which a deadline becomes active.
1625 This variable governs the display in sparse trees and in the agenda.
1626 When 0 or negative, it means use this number (the absolute value of it)
1627 even if a deadline has a different individual lead time specified."
1628 :group 'org-time
1629 :group 'org-agenda-daily/weekly
1630 :type 'number)
1632 (defcustom org-read-date-prefer-future t
1633 "Non-nil means, assume future for incomplete date input from user.
1634 This affects the following situations:
1635 1. The user gives a day, but no month.
1636 For example, if today is the 15th, and you enter \"3\", Org-mode will
1637 read this as the third of *next* month. However, if you enter \"17\",
1638 it will be considered as *this* month.
1639 2. The user gives a month but not a year.
1640 For example, if it is april and you enter \"feb 2\", this will be read
1641 as feb 2, *next* year. \"May 5\", however, will be this year.
1643 Currently this does not work for ISO week specifications.
1645 When this option is nil, the current month and year will always be used
1646 as defaults."
1647 :group 'org-time
1648 :type 'boolean)
1650 (defcustom org-read-date-display-live t
1651 "Non-nil means, display current interpretation of date prompt live.
1652 This display will be in an overlay, in the minibuffer."
1653 :group 'org-time
1654 :type 'boolean)
1656 (defcustom org-read-date-popup-calendar t
1657 "Non-nil means, pop up a calendar when prompting for a date.
1658 In the calendar, the date can be selected with mouse-1. However, the
1659 minibuffer will also be active, and you can simply enter the date as well.
1660 When nil, only the minibuffer will be available."
1661 :group 'org-time
1662 :type 'boolean)
1663 (if (fboundp 'defvaralias)
1664 (defvaralias 'org-popup-calendar-for-date-prompt
1665 'org-read-date-popup-calendar))
1667 (defcustom org-extend-today-until 0
1668 "The hour when your day really ends.
1669 This has influence for the following applications:
1670 - When switching the agenda to \"today\". It it is still earlier than
1671 the time given here, the day recognized as TODAY is actually yesterday.
1672 - When a date is read from the user and it is still before the time given
1673 here, the current date and time will be assumed to be yesterday, 23:59.
1675 FIXME:
1676 IMPORTANT: This is still a very experimental feature, it may disappear
1677 again or it may be extended to mean more things."
1678 :group 'org-time
1679 :type 'number)
1681 (defcustom org-edit-timestamp-down-means-later nil
1682 "Non-nil means, S-down will increase the time in a time stamp.
1683 When nil, S-up will increase."
1684 :group 'org-time
1685 :type 'boolean)
1687 (defcustom org-calendar-follow-timestamp-change t
1688 "Non-nil means, make the calendar window follow timestamp changes.
1689 When a timestamp is modified and the calendar window is visible, it will be
1690 moved to the new date."
1691 :group 'org-time
1692 :type 'boolean)
1694 (defgroup org-tags nil
1695 "Options concerning tags in Org-mode."
1696 :tag "Org Tags"
1697 :group 'org)
1699 (defcustom org-tag-alist nil
1700 "List of tags allowed in Org-mode files.
1701 When this list is nil, Org-mode will base TAG input on what is already in the
1702 buffer.
1703 The value of this variable is an alist, the car of each entry must be a
1704 keyword as a string, the cdr may be a character that is used to select
1705 that tag through the fast-tag-selection interface.
1706 See the manual for details."
1707 :group 'org-tags
1708 :type '(repeat
1709 (choice
1710 (cons (string :tag "Tag name")
1711 (character :tag "Access char"))
1712 (const :tag "Start radio group" (:startgroup))
1713 (const :tag "End radio group" (:endgroup)))))
1715 (defvar org-file-tags nil
1716 "List of tags that can be inherited by all entries in the file.
1717 The tags will be inherited if the variable `org-use-tag-inheritance'
1718 says they should be.
1719 This variable is populated from #+TAG lines.")
1721 (defcustom org-use-fast-tag-selection 'auto
1722 "Non-nil means, use fast tag selection scheme.
1723 This is a special interface to select and deselect tags with single keys.
1724 When nil, fast selection is never used.
1725 When the symbol `auto', fast selection is used if and only if selection
1726 characters for tags have been configured, either through the variable
1727 `org-tag-alist' or through a #+TAGS line in the buffer.
1728 When t, fast selection is always used and selection keys are assigned
1729 automatically if necessary."
1730 :group 'org-tags
1731 :type '(choice
1732 (const :tag "Always" t)
1733 (const :tag "Never" nil)
1734 (const :tag "When selection characters are configured" 'auto)))
1736 (defcustom org-fast-tag-selection-single-key nil
1737 "Non-nil means, fast tag selection exits after first change.
1738 When nil, you have to press RET to exit it.
1739 During fast tag selection, you can toggle this flag with `C-c'.
1740 This variable can also have the value `expert'. In this case, the window
1741 displaying the tags menu is not even shown, until you press C-c again."
1742 :group 'org-tags
1743 :type '(choice
1744 (const :tag "No" nil)
1745 (const :tag "Yes" t)
1746 (const :tag "Expert" expert)))
1748 (defvar org-fast-tag-selection-include-todo nil
1749 "Non-nil means, fast tags selection interface will also offer TODO states.
1750 This is an undocumented feature, you should not rely on it.")
1752 (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
1753 "The column to which tags should be indented in a headline.
1754 If this number is positive, it specifies the column. If it is negative,
1755 it means that the tags should be flushright to that column. For example,
1756 -80 works well for a normal 80 character screen."
1757 :group 'org-tags
1758 :type 'integer)
1760 (defcustom org-auto-align-tags t
1761 "Non-nil means, realign tags after pro/demotion of TODO state change.
1762 These operations change the length of a headline and therefore shift
1763 the tags around. With this options turned on, after each such operation
1764 the tags are again aligned to `org-tags-column'."
1765 :group 'org-tags
1766 :type 'boolean)
1768 (defcustom org-use-tag-inheritance t
1769 "Non-nil means, tags in levels apply also for sublevels.
1770 When nil, only the tags directly given in a specific line apply there.
1771 If this option is t, a match early-on in a tree can lead to a large
1772 number of matches in the subtree. If you only want to see the first
1773 match in a tree during a search, check out the variable
1774 `org-tags-match-list-sublevels'.
1776 This may also be a list of tags that should be inherited, or a regexp that
1777 matches tags that should be inherited."
1778 :group 'org-tags
1779 :type '(choice
1780 (const :tag "Not" nil)
1781 (const :tag "Always" t)
1782 (repeat :tag "Specific tags" (string :tag "Tag"))
1783 (regexp :tag "Tags matched by regexp")))
1785 (defun org-tag-inherit-p (tag)
1786 "Check if TAG is one that should be inherited."
1787 (cond
1788 ((eq org-use-tag-inheritance t) t)
1789 ((not org-use-tag-inheritance) nil)
1790 ((stringp org-use-tag-inheritance)
1791 (string-match org-use-tag-inheritance tag))
1792 ((listp org-use-tag-inheritance)
1793 (member tag org-use-tag-inheritance))
1794 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
1796 (defcustom org-tags-match-list-sublevels t
1797 "Non-nil means list also sublevels of headlines matching tag search.
1798 Because of tag inheritance (see variable `org-use-tag-inheritance'),
1799 the sublevels of a headline matching a tag search often also match
1800 the same search. Listing all of them can create very long lists.
1801 Setting this variable to nil causes subtrees of a match to be skipped.
1802 This option is off by default, because inheritance in on. If you turn
1803 inheritance off, you very likely want to turn this option on.
1805 As a special case, if the tag search is restricted to TODO items, the
1806 value of this variable is ignored and sublevels are always checked, to
1807 make sure all corresponding TODO items find their way into the list."
1808 :group 'org-tags
1809 :type 'boolean)
1811 (defvar org-tags-history nil
1812 "History of minibuffer reads for tags.")
1813 (defvar org-last-tags-completion-table nil
1814 "The last used completion table for tags.")
1815 (defvar org-after-tags-change-hook nil
1816 "Hook that is run after the tags in a line have changed.")
1818 (defgroup org-properties nil
1819 "Options concerning properties in Org-mode."
1820 :tag "Org Properties"
1821 :group 'org)
1823 (defcustom org-property-format "%-10s %s"
1824 "How property key/value pairs should be formatted by `indent-line'.
1825 When `indent-line' hits a property definition, it will format the line
1826 according to this format, mainly to make sure that the values are
1827 lined-up with respect to each other."
1828 :group 'org-properties
1829 :type 'string)
1831 (defcustom org-use-property-inheritance nil
1832 "Non-nil means, properties apply also for sublevels.
1834 This setting is chiefly used during property searches. Turning it on can
1835 cause significant overhead when doing a search, which is why it is not
1836 on by default.
1838 When nil, only the properties directly given in the current entry count.
1839 When t, every property is inherited. The value may also be a list of
1840 properties that should have inheritance, or a regular expression matching
1841 properties that should be inherited.
1843 However, note that some special properties use inheritance under special
1844 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
1845 and the properties ending in \"_ALL\" when they are used as descriptor
1846 for valid values of a property.
1848 Note for programmers:
1849 When querying an entry with `org-entry-get', you can control if inheritance
1850 should be used. By default, `org-entry-get' looks only at the local
1851 properties. You can request inheritance by setting the inherit argument
1852 to t (to force inheritance) or to `selective' (to respect the setting
1853 in this variable)."
1854 :group 'org-properties
1855 :type '(choice
1856 (const :tag "Not" nil)
1857 (const :tag "Always" t)
1858 (repeat :tag "Specific properties" (string :tag "Property"))
1859 (regexp :tag "Properties matched by regexp")))
1861 (defun org-property-inherit-p (property)
1862 "Check if PROPERTY is one that should be inherited."
1863 (cond
1864 ((eq org-use-property-inheritance t) t)
1865 ((not org-use-property-inheritance) nil)
1866 ((stringp org-use-property-inheritance)
1867 (string-match org-use-property-inheritance property))
1868 ((listp org-use-property-inheritance)
1869 (member property org-use-property-inheritance))
1870 (t (error "Invalid setting of `org-use-property-inheritance'"))))
1872 (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
1873 "The default column format, if no other format has been defined.
1874 This variable can be set on the per-file basis by inserting a line
1876 #+COLUMNS: %25ITEM ....."
1877 :group 'org-properties
1878 :type 'string)
1880 (defcustom org-columns-ellipses ".."
1881 "The ellipses to be used when a field in column view is truncated.
1882 When this is the empty string, as many characters as possible are shown,
1883 but then there will be no visual indication that the field has been truncated.
1884 When this is a string of length N, the last N characters of a truncated
1885 field are replaced by this string. If the column is narrower than the
1886 ellipses string, only part of the ellipses string will be shown."
1887 :group 'org-properties
1888 :type 'string)
1890 (defcustom org-columns-modify-value-for-display-function nil
1891 "Function that modifies values for display in column view.
1892 For example, it can be used to cut out a certain part from a time stamp.
1893 The function must take 2 argments:
1895 column-title The tite of the column (*not* the property name)
1896 value The value that should be modified.
1898 The function should return the value that should be displayed,
1899 or nil if the normal value should be used."
1900 :group 'org-properties
1901 :type 'function)
1903 (defcustom org-effort-property "Effort"
1904 "The property that is being used to keep track of effort estimates.
1905 Effort estimates given in this property need to have the format H:MM."
1906 :group 'org-properties
1907 :group 'org-progress
1908 :type '(string :tag "Property"))
1910 (defconst org-global-properties-fixed
1911 '(("VISIBILITY_ALL" . "folded children content all"))
1912 "List of property/value pairs that can be inherited by any entry.
1913 These are fixed values, for the preset properties.")
1916 (defcustom org-global-properties nil
1917 "List of property/value pairs that can be inherited by any entry.
1918 You can set buffer-local values for this by adding lines like
1920 #+PROPERTY: NAME VALUE"
1921 :group 'org-properties
1922 :type '(repeat
1923 (cons (string :tag "Property")
1924 (string :tag "Value"))))
1926 (defvar org-file-properties nil
1927 "List of property/value pairs that can be inherited by any entry.
1928 Valid for the current buffer.
1929 This variable is populated from #+PROPERTY lines.")
1930 (make-variable-buffer-local 'org-file-properties)
1932 (defgroup org-agenda nil
1933 "Options concerning agenda views in Org-mode."
1934 :tag "Org Agenda"
1935 :group 'org)
1937 (defvar org-category nil
1938 "Variable used by org files to set a category for agenda display.
1939 Such files should use a file variable to set it, for example
1941 # -*- mode: org; org-category: \"ELisp\"
1943 or contain a special line
1945 #+CATEGORY: ELisp
1947 If the file does not specify a category, then file's base name
1948 is used instead.")
1949 (make-variable-buffer-local 'org-category)
1950 (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
1952 (defcustom org-agenda-files nil
1953 "The files to be used for agenda display.
1954 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
1955 \\[org-remove-file]. You can also use customize to edit the list.
1957 If an entry is a directory, all files in that directory that are matched by
1958 `org-agenda-file-regexp' will be part of the file list.
1960 If the value of the variable is not a list but a single file name, then
1961 the list of agenda files is actually stored and maintained in that file, one
1962 agenda file per line."
1963 :group 'org-agenda
1964 :type '(choice
1965 (repeat :tag "List of files and directories" file)
1966 (file :tag "Store list in a file\n" :value "~/.agenda_files")))
1968 (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
1969 "Regular expression to match files for `org-agenda-files'.
1970 If any element in the list in that variable contains a directory instead
1971 of a normal file, all files in that directory that are matched by this
1972 regular expression will be included."
1973 :group 'org-agenda
1974 :type 'regexp)
1976 (defcustom org-agenda-text-search-extra-files nil
1977 "List of extra files to be searched by text search commands.
1978 These files will be search in addition to the agenda files by the
1979 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
1980 Note that these files will only be searched for text search commands,
1981 not for the other agenda views like todo lists, tag searches or the weekly
1982 agenda. This variable is intended to list notes and possibly archive files
1983 that should also be searched by these two commands.
1984 In fact, if the first element in the list is the symbol `agenda-archives',
1985 than all archive files of all agenda files will be added to the search
1986 scope."
1987 :group 'org-agenda
1988 :type '(set :greedy t
1989 (const :tag "Agenda Archives" agenda-archives)
1990 (repeat :inline t (file))))
1992 (if (fboundp 'defvaralias)
1993 (defvaralias 'org-agenda-multi-occur-extra-files
1994 'org-agenda-text-search-extra-files))
1996 (defcustom org-agenda-skip-unavailable-files nil
1997 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
1998 A nil value means to remove them, after a query, from the list."
1999 :group 'org-agenda
2000 :type 'boolean)
2002 (defcustom org-calendar-to-agenda-key [?c]
2003 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2004 The command `org-calendar-goto-agenda' will be bound to this key. The
2005 default is the character `c' because then `c' can be used to switch back and
2006 forth between agenda and calendar."
2007 :group 'org-agenda
2008 :type 'sexp)
2010 (defcustom org-calendar-agenda-action-key [?k]
2011 "The key to be installed in `calendar-mode-map' for agenda-action.
2012 The command `org-agenda-action' will be bound to this key. The
2013 default is the character `k' because we use the same key in the agenda."
2014 :group 'org-agenda
2015 :type 'sexp)
2017 (eval-after-load "calendar"
2018 '(progn
2019 (org-defkey calendar-mode-map org-calendar-to-agenda-key
2020 'org-calendar-goto-agenda)
2021 (org-defkey calendar-mode-map org-calendar-agenda-action-key
2022 'org-agenda-action)))
2024 (defgroup org-latex nil
2025 "Options for embedding LaTeX code into Org-mode."
2026 :tag "Org LaTeX"
2027 :group 'org)
2029 (defcustom org-format-latex-options
2030 '(:foreground default :background default :scale 1.0
2031 :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
2032 :matchers ("begin" "$" "$$" "\\(" "\\["))
2033 "Options for creating images from LaTeX fragments.
2034 This is a property list with the following properties:
2035 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
2036 `default' means use the foreground of the default face.
2037 :background the background color, or \"Transparent\".
2038 `default' means use the background of the default face.
2039 :scale a scaling factor for the size of the images.
2040 :html-foreground, :html-background, :html-scale
2041 the same numbers for HTML export.
2042 :matchers a list indicating which matchers should be used to
2043 find LaTeX fragments. Valid members of this list are:
2044 \"begin\" find environments
2045 \"$\" find math expressions surrounded by $...$
2046 \"$$\" find math expressions surrounded by $$....$$
2047 \"\\(\" find math expressions surrounded by \\(...\\)
2048 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2049 :group 'org-latex
2050 :type 'plist)
2052 (defcustom org-format-latex-header "\\documentclass{article}
2053 \\usepackage{fullpage} % do not remove
2054 \\usepackage{amssymb}
2055 \\usepackage[usenames]{color}
2056 \\usepackage{amsmath}
2057 \\usepackage{latexsym}
2058 \\usepackage[mathscr]{eucal}
2059 \\pagestyle{empty} % do not remove"
2060 "The document header used for processing LaTeX fragments."
2061 :group 'org-latex
2062 :type 'string)
2065 (defgroup org-font-lock nil
2066 "Font-lock settings for highlighting in Org-mode."
2067 :tag "Org Font Lock"
2068 :group 'org)
2070 (defcustom org-level-color-stars-only nil
2071 "Non-nil means fontify only the stars in each headline.
2072 When nil, the entire headline is fontified.
2073 Changing it requires restart of `font-lock-mode' to become effective
2074 also in regions already fontified."
2075 :group 'org-font-lock
2076 :type 'boolean)
2078 (defcustom org-hide-leading-stars nil
2079 "Non-nil means, hide the first N-1 stars in a headline.
2080 This works by using the face `org-hide' for these stars. This
2081 face is white for a light background, and black for a dark
2082 background. You may have to customize the face `org-hide' to
2083 make this work.
2084 Changing it requires restart of `font-lock-mode' to become effective
2085 also in regions already fontified.
2086 You may also set this on a per-file basis by adding one of the following
2087 lines to the buffer:
2089 #+STARTUP: hidestars
2090 #+STARTUP: showstars"
2091 :group 'org-font-lock
2092 :type 'boolean)
2094 (defcustom org-fontify-done-headline nil
2095 "Non-nil means, change the face of a headline if it is marked DONE.
2096 Normally, only the TODO/DONE keyword indicates the state of a headline.
2097 When this is non-nil, the headline after the keyword is set to the
2098 `org-headline-done' as an additional indication."
2099 :group 'org-font-lock
2100 :type 'boolean)
2102 (defcustom org-fontify-emphasized-text t
2103 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
2104 Changing this variable requires a restart of Emacs to take effect."
2105 :group 'org-font-lock
2106 :type 'boolean)
2108 (defcustom org-highlight-latex-fragments-and-specials nil
2109 "Non-nil means, fontify what is treated specially by the exporters."
2110 :group 'org-font-lock
2111 :type 'boolean)
2113 (defcustom org-hide-emphasis-markers nil
2114 "Non-nil mean font-lock should hide the emphasis marker characters."
2115 :group 'org-font-lock
2116 :type 'boolean)
2118 (defvar org-emph-re nil
2119 "Regular expression for matching emphasis.")
2120 (defvar org-verbatim-re nil
2121 "Regular expression for matching verbatim text.")
2122 (defvar org-emphasis-regexp-components) ; defined just below
2123 (defvar org-emphasis-alist) ; defined just below
2124 (defun org-set-emph-re (var val)
2125 "Set variable and compute the emphasis regular expression."
2126 (set var val)
2127 (when (and (boundp 'org-emphasis-alist)
2128 (boundp 'org-emphasis-regexp-components)
2129 org-emphasis-alist org-emphasis-regexp-components)
2130 (let* ((e org-emphasis-regexp-components)
2131 (pre (car e))
2132 (post (nth 1 e))
2133 (border (nth 2 e))
2134 (body (nth 3 e))
2135 (nl (nth 4 e))
2136 (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
2137 (body1 (concat body "*?"))
2138 (markers (mapconcat 'car org-emphasis-alist ""))
2139 (vmarkers (mapconcat
2140 (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
2141 org-emphasis-alist "")))
2142 ;; make sure special characters appear at the right position in the class
2143 (if (string-match "\\^" markers)
2144 (setq markers (concat (replace-match "" t t markers) "^")))
2145 (if (string-match "-" markers)
2146 (setq markers (concat (replace-match "" t t markers) "-")))
2147 (if (string-match "\\^" vmarkers)
2148 (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
2149 (if (string-match "-" vmarkers)
2150 (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
2151 (if (> nl 0)
2152 (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
2153 (int-to-string nl) "\\}")))
2154 ;; Make the regexp
2155 (setq org-emph-re
2156 (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
2157 "\\("
2158 "\\([" markers "]\\)"
2159 "\\("
2160 "[^" border "]\\|"
2161 "[^" border (if (and nil stacked) markers) "]"
2162 body1
2163 "[^" border (if (and nil stacked) markers) "]"
2164 "\\)"
2165 "\\3\\)"
2166 "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
2167 (setq org-verbatim-re
2168 (concat "\\([" pre "]\\|^\\)"
2169 "\\("
2170 "\\([" vmarkers "]\\)"
2171 "\\("
2172 "[^" border "]\\|"
2173 "[^" border "]"
2174 body1
2175 "[^" border "]"
2176 "\\)"
2177 "\\3\\)"
2178 "\\([" post "]\\|$\\)")))))
2180 (defcustom org-emphasis-regexp-components
2181 '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
2182 "Components used to build the regular expression for emphasis.
2183 This is a list with 6 entries. Terminology: In an emphasis string
2184 like \" *strong word* \", we call the initial space PREMATCH, the final
2185 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
2186 and \"trong wor\" is the body. The different components in this variable
2187 specify what is allowed/forbidden in each part:
2189 pre Chars allowed as prematch. Beginning of line will be allowed too.
2190 post Chars allowed as postmatch. End of line will be allowed too.
2191 border The chars *forbidden* as border characters.
2192 body-regexp A regexp like \".\" to match a body character. Don't use
2193 non-shy groups here, and don't allow newline here.
2194 newline The maximum number of newlines allowed in an emphasis exp.
2196 Use customize to modify this, or restart Emacs after changing it."
2197 :group 'org-font-lock
2198 :set 'org-set-emph-re
2199 :type '(list
2200 (sexp :tag "Allowed chars in pre ")
2201 (sexp :tag "Allowed chars in post ")
2202 (sexp :tag "Forbidden chars in border ")
2203 (sexp :tag "Regexp for body ")
2204 (integer :tag "number of newlines allowed")
2205 (option (boolean :tag "Please ignore this button"))))
2207 (defcustom org-emphasis-alist
2208 `(("*" bold "<b>" "</b>")
2209 ("/" italic "<i>" "</i>")
2210 ("_" underline "<u>" "</u>")
2211 ("=" org-code "<code>" "</code>" verbatim)
2212 ("~" org-verbatim "" "" verbatim)
2213 ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
2214 "<del>" "</del>")
2216 "Special syntax for emphasized text.
2217 Text starting and ending with a special character will be emphasized, for
2218 example *bold*, _underlined_ and /italic/. This variable sets the marker
2219 characters, the face to be used by font-lock for highlighting in Org-mode
2220 Emacs buffers, and the HTML tags to be used for this.
2221 Use customize to modify this, or restart Emacs after changing it."
2222 :group 'org-font-lock
2223 :set 'org-set-emph-re
2224 :type '(repeat
2225 (list
2226 (string :tag "Marker character")
2227 (choice
2228 (face :tag "Font-lock-face")
2229 (plist :tag "Face property list"))
2230 (string :tag "HTML start tag")
2231 (string :tag "HTML end tag")
2232 (option (const verbatim)))))
2234 ;;; Miscellaneous options
2236 (defgroup org-completion nil
2237 "Completion in Org-mode."
2238 :tag "Org Completion"
2239 :group 'org)
2241 (defcustom org-completion-fallback-command 'hippie-expand
2242 "The expansion command called by \\[org-complete] in normal context.
2243 Normal means, no org-mode-specific context."
2244 :group 'org-completion
2245 :type 'function)
2247 ;;; Functions and variables from ther packages
2248 ;; Declared here to avoid compiler warnings
2250 ;; XEmacs only
2251 (defvar outline-mode-menu-heading)
2252 (defvar outline-mode-menu-show)
2253 (defvar outline-mode-menu-hide)
2254 (defvar zmacs-regions) ; XEmacs regions
2256 ;; Emacs only
2257 (defvar mark-active)
2259 ;; Various packages
2260 (declare-function calendar-absolute-from-iso "cal-iso" (date))
2261 (declare-function calendar-forward-day "cal-move" (arg))
2262 (declare-function calendar-goto-date "cal-move" (date))
2263 (declare-function calendar-goto-today "cal-move" ())
2264 (declare-function calendar-iso-from-absolute "cal-iso" (date))
2265 (defvar calc-embedded-close-formula)
2266 (defvar calc-embedded-open-formula)
2267 (declare-function cdlatex-tab "ext:cdlatex" ())
2268 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
2269 (defvar font-lock-unfontify-region-function)
2270 (declare-function iswitchb-mode "iswitchb" (&optional arg))
2271 (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
2272 (defvar iswitchb-temp-buflist)
2273 (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
2274 (declare-function org-agenda-skip "org-agenda" ())
2275 (declare-function org-format-agenda-item "org-agenda"
2276 (extra txt &optional category tags dotime noprefix remove-re))
2277 (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
2278 (declare-function org-agenda-change-all-lines "org-agenda"
2279 (newhead hdmarker &optional fixface))
2280 (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
2281 (declare-function org-agenda-maybe-redo "org-agenda" ())
2282 (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
2283 (beg end))
2284 (declare-function parse-time-string "parse-time" (string))
2285 (declare-function remember "remember" (&optional initial))
2286 (declare-function remember-buffer-desc "remember" ())
2287 (declare-function remember-finalize "remember" ())
2288 (defvar remember-save-after-remembering)
2289 (defvar remember-data-file)
2290 (defvar remember-register)
2291 (defvar remember-buffer)
2292 (defvar remember-handler-functions)
2293 (defvar remember-annotation-functions)
2294 (defvar texmathp-why)
2295 (declare-function speedbar-line-directory "speedbar" (&optional depth))
2296 (declare-function table--at-cell-p "table" (position &optional object at-column))
2298 (defvar w3m-current-url)
2299 (defvar w3m-current-title)
2301 (defvar org-latex-regexps)
2303 ;;; Autoload and prepare some org modules
2305 ;; Some table stuff that needs to be defined here, because it is used
2306 ;; by the functions setting up org-mode or checking for table context.
2308 (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
2309 "Detects an org-type or table-type table.")
2310 (defconst org-table-line-regexp "^[ \t]*|"
2311 "Detects an org-type table line.")
2312 (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
2313 "Detects an org-type table line.")
2314 (defconst org-table-hline-regexp "^[ \t]*|-"
2315 "Detects an org-type table hline.")
2316 (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
2317 "Detects a table-type table hline.")
2318 (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
2319 "Searching from within a table (any type) this finds the first line
2320 outside the table.")
2322 ;; Autoload the functions in org-table.el that are needed by functions here.
2324 (eval-and-compile
2325 (org-autoload "org-table"
2326 '(org-table-align org-table-begin org-table-blank-field
2327 org-table-convert org-table-convert-region org-table-copy-down
2328 org-table-copy-region org-table-create
2329 org-table-create-or-convert-from-region
2330 org-table-create-with-table.el org-table-current-dline
2331 org-table-cut-region org-table-delete-column org-table-edit-field
2332 org-table-edit-formulas org-table-end org-table-eval-formula
2333 org-table-export org-table-field-info
2334 org-table-get-stored-formulas org-table-goto-column
2335 org-table-hline-and-move org-table-import org-table-insert-column
2336 org-table-insert-hline org-table-insert-row org-table-iterate
2337 org-table-justify-field-maybe org-table-kill-row
2338 org-table-maybe-eval-formula org-table-maybe-recalculate-line
2339 org-table-move-column org-table-move-column-left
2340 org-table-move-column-right org-table-move-row
2341 org-table-move-row-down org-table-move-row-up
2342 org-table-next-field org-table-next-row org-table-paste-rectangle
2343 org-table-previous-field org-table-recalculate
2344 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
2345 org-table-toggle-coordinate-overlays
2346 org-table-toggle-formula-debugger org-table-wrap-region
2347 orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
2349 (defun org-at-table-p (&optional table-type)
2350 "Return t if the cursor is inside an org-type table.
2351 If TABLE-TYPE is non-nil, also check for table.el-type tables."
2352 (if org-enable-table-editor
2353 (save-excursion
2354 (beginning-of-line 1)
2355 (looking-at (if table-type org-table-any-line-regexp
2356 org-table-line-regexp)))
2357 nil))
2358 (defsubst org-table-p () (org-at-table-p))
2360 (defun org-at-table.el-p ()
2361 "Return t if and only if we are at a table.el table."
2362 (and (org-at-table-p 'any)
2363 (save-excursion
2364 (goto-char (org-table-begin 'any))
2365 (looking-at org-table1-hline-regexp))))
2366 (defun org-table-recognize-table.el ()
2367 "If there is a table.el table nearby, recognize it and move into it."
2368 (if org-table-tab-recognizes-table.el
2369 (if (org-at-table.el-p)
2370 (progn
2371 (beginning-of-line 1)
2372 (if (looking-at org-table-dataline-regexp)
2374 (if (looking-at org-table1-hline-regexp)
2375 (progn
2376 (beginning-of-line 2)
2377 (if (looking-at org-table-any-border-regexp)
2378 (beginning-of-line -1)))))
2379 (if (re-search-forward "|" (org-table-end t) t)
2380 (progn
2381 (require 'table)
2382 (if (table--at-cell-p (point))
2384 (message "recognizing table.el table...")
2385 (table-recognize-table)
2386 (message "recognizing table.el table...done")))
2387 (error "This should not happen..."))
2389 nil)
2390 nil))
2392 (defun org-at-table-hline-p ()
2393 "Return t if the cursor is inside a hline in a table."
2394 (if org-enable-table-editor
2395 (save-excursion
2396 (beginning-of-line 1)
2397 (looking-at org-table-hline-regexp))
2398 nil))
2400 (defvar org-table-clean-did-remove-column nil)
2402 (defun org-table-map-tables (function)
2403 "Apply FUNCTION to the start of all tables in the buffer."
2404 (save-excursion
2405 (save-restriction
2406 (widen)
2407 (goto-char (point-min))
2408 (while (re-search-forward org-table-any-line-regexp nil t)
2409 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
2410 (beginning-of-line 1)
2411 (if (looking-at org-table-line-regexp)
2412 (save-excursion (funcall function)))
2413 (re-search-forward org-table-any-border-regexp nil 1))))
2414 (message "Mapping tables: done"))
2416 ;; Declare and autoload functions from org-exp.el
2418 (declare-function org-default-export-plist "org-exp")
2419 (declare-function org-infile-export-plist "org-exp")
2420 (declare-function org-get-current-options "org-exp")
2421 (eval-and-compile
2422 (org-autoload "org-exp"
2423 '(org-export org-export-as-ascii org-export-visible
2424 org-insert-export-options-template org-export-as-html-and-open
2425 org-export-as-html-batch org-export-as-html-to-buffer
2426 org-replace-region-by-html org-export-region-as-html
2427 org-export-as-html org-export-icalendar-this-file
2428 org-export-icalendar-all-agenda-files
2429 org-table-clean-before-export
2430 org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
2432 ;; Declare and autoload functions from org-exp.el
2434 (eval-and-compile
2435 (org-autoload "org-exp"
2436 '(org-agenda org-agenda-list org-search-view
2437 org-todo-list org-tags-view org-agenda-list-stuck-projects
2438 org-diary org-agenda-to-appt)))
2440 ;; Autoload org-remember
2442 (eval-and-compile
2443 (org-autoload "org-remember"
2444 '(org-remember-insinuate org-remember-annotation
2445 org-remember-apply-template org-remember org-remember-handler)))
2447 ;; Autoload org-clock.el
2450 (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
2451 (beg end))
2452 (declare-function org-update-mode-line "org-clock" ())
2453 (defvar org-clock-start-time)
2454 (defvar org-clock-marker (make-marker)
2455 "Marker recording the last clock-in.")
2457 (eval-and-compile
2458 (org-autoload
2459 "org-clock"
2460 '(org-clock-in org-clock-out org-clock-cancel
2461 org-clock-goto org-clock-sum org-clock-display
2462 org-remove-clock-overlays org-clock-report
2463 org-clocktable-shift org-dblock-write:clocktable
2464 org-get-clocktable)))
2466 (defun org-clock-update-time-maybe ()
2467 "If this is a CLOCK line, update it and return t.
2468 Otherwise, return nil."
2469 (interactive)
2470 (save-excursion
2471 (beginning-of-line 1)
2472 (skip-chars-forward " \t")
2473 (when (looking-at org-clock-string)
2474 (let ((re (concat "[ \t]*" org-clock-string
2475 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
2476 "\\([ \t]*=>.*\\)?\\)?"))
2477 ts te h m s)
2478 (cond
2479 ((not (looking-at re))
2480 nil)
2481 ((not (match-end 2))
2482 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
2483 (> org-clock-marker (point))
2484 (<= org-clock-marker (point-at-eol)))
2485 ;; The clock is running here
2486 (setq org-clock-start-time
2487 (apply 'encode-time
2488 (org-parse-time-string (match-string 1))))
2489 (org-update-mode-line)))
2491 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
2492 (end-of-line 1)
2493 (setq ts (match-string 1)
2494 te (match-string 3))
2495 (setq s (- (time-to-seconds
2496 (apply 'encode-time (org-parse-time-string te)))
2497 (time-to-seconds
2498 (apply 'encode-time (org-parse-time-string ts))))
2499 h (floor (/ s 3600))
2500 s (- s (* 3600 h))
2501 m (floor (/ s 60))
2502 s (- s (* 60 s)))
2503 (insert " => " (format "%2d:%02d" h m))
2504 t))))))
2506 (defun org-check-running-clock ()
2507 "Check if the current buffer contains the running clock.
2508 If yes, offer to stop it and to save the buffer with the changes."
2509 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
2510 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
2511 (buffer-name))))
2512 (org-clock-out)
2513 (when (y-or-n-p "Save changed buffer?")
2514 (save-buffer))))
2516 (defun org-clocktable-try-shift (dir n)
2517 "Check if this line starts a clock table, if yes, shift the time block."
2518 (when (org-match-line "#\\+BEGIN: clocktable\\>")
2519 (org-clocktable-shift dir n)))
2521 ;; Autoload archiving code
2522 ;; The stuff that is needed for cycling and tags has to be defined here.
2524 (defgroup org-archive nil
2525 "Options concerning archiving in Org-mode."
2526 :tag "Org Archive"
2527 :group 'org-structure)
2529 (defcustom org-archive-location "%s_archive::"
2530 "The location where subtrees should be archived.
2532 Otherwise, the value of this variable is a string, consisting of two
2533 parts, separated by a double-colon.
2535 The first part is a file name - when omitted, archiving happens in the same
2536 file. %s will be replaced by the current file name (without directory part).
2537 Archiving to a different file is useful to keep archived entries from
2538 contributing to the Org-mode Agenda.
2540 The part after the double colon is a headline. The archived entries will be
2541 filed under that headline. When omitted, the subtrees are simply filed away
2542 at the end of the file, as top-level entries.
2544 Here are a few examples:
2545 \"%s_archive::\"
2546 If the current file is Projects.org, archive in file
2547 Projects.org_archive, as top-level trees. This is the default.
2549 \"::* Archived Tasks\"
2550 Archive in the current file, under the top-level headline
2551 \"* Archived Tasks\".
2553 \"~/org/archive.org::\"
2554 Archive in file ~/org/archive.org (absolute path), as top-level trees.
2556 \"basement::** Finished Tasks\"
2557 Archive in file ./basement (relative path), as level 3 trees
2558 below the level 2 heading \"** Finished Tasks\".
2560 You may set this option on a per-file basis by adding to the buffer a
2561 line like
2563 #+ARCHIVE: basement::** Finished Tasks
2565 You may also define it locally for a subtree by setting an ARCHIVE property
2566 in the entry. If such a property is found in an entry, or anywhere up
2567 the hierarchy, it will be used."
2568 :group 'org-archive
2569 :type 'string)
2571 (defcustom org-archive-tag "ARCHIVE"
2572 "The tag that marks a subtree as archived.
2573 An archived subtree does not open during visibility cycling, and does
2574 not contribute to the agenda listings.
2575 After changing this, font-lock must be restarted in the relevant buffers to
2576 get the proper fontification."
2577 :group 'org-archive
2578 :group 'org-keywords
2579 :type 'string)
2581 (defcustom org-agenda-skip-archived-trees t
2582 "Non-nil means, the agenda will skip any items located in archived trees.
2583 An archived tree is a tree marked with the tag ARCHIVE. The use of this
2584 variable is no longer recommended, you should leave it at the value t.
2585 Instead, use the key `v' to cycle the archives-mode in the agenda."
2586 :group 'org-archive
2587 :group 'org-agenda-skip
2588 :type 'boolean)
2590 (defcustom org-cycle-open-archived-trees nil
2591 "Non-nil means, `org-cycle' will open archived trees.
2592 An archived tree is a tree marked with the tag ARCHIVE.
2593 When nil, archived trees will stay folded. You can still open them with
2594 normal outline commands like `show-all', but not with the cycling commands."
2595 :group 'org-archive
2596 :group 'org-cycle
2597 :type 'boolean)
2599 (defcustom org-sparse-tree-open-archived-trees nil
2600 "Non-nil means sparse tree construction shows matches in archived trees.
2601 When nil, matches in these trees are highlighted, but the trees are kept in
2602 collapsed state."
2603 :group 'org-archive
2604 :group 'org-sparse-trees
2605 :type 'boolean)
2607 (defun org-cycle-hide-archived-subtrees (state)
2608 "Re-hide all archived subtrees after a visibility state change."
2609 (when (and (not org-cycle-open-archived-trees)
2610 (not (memq state '(overview folded))))
2611 (save-excursion
2612 (let* ((globalp (memq state '(contents all)))
2613 (beg (if globalp (point-min) (point)))
2614 (end (if globalp (point-max) (org-end-of-subtree t))))
2615 (org-hide-archived-subtrees beg end)
2616 (goto-char beg)
2617 (if (looking-at (concat ".*:" org-archive-tag ":"))
2618 (message "%s" (substitute-command-keys
2619 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
2621 (defun org-force-cycle-archived ()
2622 "Cycle subtree even if it is archived."
2623 (interactive)
2624 (setq this-command 'org-cycle)
2625 (let ((org-cycle-open-archived-trees t))
2626 (call-interactively 'org-cycle)))
2628 (defun org-hide-archived-subtrees (beg end)
2629 "Re-hide all archived subtrees after a visibility state change."
2630 (save-excursion
2631 (let* ((re (concat ":" org-archive-tag ":")))
2632 (goto-char beg)
2633 (while (re-search-forward re end t)
2634 (and (org-on-heading-p) (hide-subtree))
2635 (org-end-of-subtree t)))))
2637 (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
2639 (eval-and-compile
2640 (org-autoload "org-archive"
2641 '(org-add-archive-files org-archive-subtree
2642 org-archive-to-archive-sibling org-toggle-archive-tag)))
2644 ;; Autoload Column View Code
2646 (declare-function org-columns-number-to-string "org-colview")
2647 (declare-function org-columns-get-format-and-top-level "org-colview")
2648 (declare-function org-columns-compute "org-colview")
2650 (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
2651 '(org-columns-number-to-string org-columns-get-format-and-top-level
2652 org-columns-compute org-agenda-columns org-columns-remove-overlays
2653 org-columns org-insert-columns-dblock org-dblock-write:columnview))
2655 ;; Autoload ID code
2657 (org-autoload "org-id"
2658 '(org-id-get-create org-id-new org-id-copy org-id-get
2659 org-id-get-with-outline-path-completion
2660 org-id-get-with-outline-drilling
2661 org-id-goto org-id-find))
2663 ;;; Variables for pre-computed regular expressions, all buffer local
2665 (defvar org-drawer-regexp nil
2666 "Matches first line of a hidden block.")
2667 (make-variable-buffer-local 'org-drawer-regexp)
2668 (defvar org-todo-regexp nil
2669 "Matches any of the TODO state keywords.")
2670 (make-variable-buffer-local 'org-todo-regexp)
2671 (defvar org-not-done-regexp nil
2672 "Matches any of the TODO state keywords except the last one.")
2673 (make-variable-buffer-local 'org-not-done-regexp)
2674 (defvar org-todo-line-regexp nil
2675 "Matches a headline and puts TODO state into group 2 if present.")
2676 (make-variable-buffer-local 'org-todo-line-regexp)
2677 (defvar org-complex-heading-regexp nil
2678 "Matches a headline and puts everything into groups:
2679 group 1: the stars
2680 group 2: The todo keyword, maybe
2681 group 3: Priority cookie
2682 group 4: True headline
2683 group 5: Tags")
2684 (make-variable-buffer-local 'org-complex-heading-regexp)
2685 (defvar org-todo-line-tags-regexp nil
2686 "Matches a headline and puts TODO state into group 2 if present.
2687 Also put tags into group 4 if tags are present.")
2688 (make-variable-buffer-local 'org-todo-line-tags-regexp)
2689 (defvar org-nl-done-regexp nil
2690 "Matches newline followed by a headline with the DONE keyword.")
2691 (make-variable-buffer-local 'org-nl-done-regexp)
2692 (defvar org-looking-at-done-regexp nil
2693 "Matches the DONE keyword a point.")
2694 (make-variable-buffer-local 'org-looking-at-done-regexp)
2695 (defvar org-ds-keyword-length 12
2696 "Maximum length of the Deadline and SCHEDULED keywords.")
2697 (make-variable-buffer-local 'org-ds-keyword-length)
2698 (defvar org-deadline-regexp nil
2699 "Matches the DEADLINE keyword.")
2700 (make-variable-buffer-local 'org-deadline-regexp)
2701 (defvar org-deadline-time-regexp nil
2702 "Matches the DEADLINE keyword together with a time stamp.")
2703 (make-variable-buffer-local 'org-deadline-time-regexp)
2704 (defvar org-deadline-line-regexp nil
2705 "Matches the DEADLINE keyword and the rest of the line.")
2706 (make-variable-buffer-local 'org-deadline-line-regexp)
2707 (defvar org-scheduled-regexp nil
2708 "Matches the SCHEDULED keyword.")
2709 (make-variable-buffer-local 'org-scheduled-regexp)
2710 (defvar org-scheduled-time-regexp nil
2711 "Matches the SCHEDULED keyword together with a time stamp.")
2712 (make-variable-buffer-local 'org-scheduled-time-regexp)
2713 (defvar org-closed-time-regexp nil
2714 "Matches the CLOSED keyword together with a time stamp.")
2715 (make-variable-buffer-local 'org-closed-time-regexp)
2717 (defvar org-keyword-time-regexp nil
2718 "Matches any of the 4 keywords, together with the time stamp.")
2719 (make-variable-buffer-local 'org-keyword-time-regexp)
2720 (defvar org-keyword-time-not-clock-regexp nil
2721 "Matches any of the 3 keywords, together with the time stamp.")
2722 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
2723 (defvar org-maybe-keyword-time-regexp nil
2724 "Matches a timestamp, possibly preceeded by a keyword.")
2725 (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
2726 (defvar org-planning-or-clock-line-re nil
2727 "Matches a line with planning or clock info.")
2728 (make-variable-buffer-local 'org-planning-or-clock-line-re)
2730 (defconst org-plain-time-of-day-regexp
2731 (concat
2732 "\\(\\<[012]?[0-9]"
2733 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2734 "\\(--?"
2735 "\\(\\<[012]?[0-9]"
2736 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2737 "\\)?")
2738 "Regular expression to match a plain time or time range.
2739 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
2740 groups carry important information:
2741 0 the full match
2742 1 the first time, range or not
2743 8 the second time, if it is a range.")
2745 (defconst org-plain-time-extension-regexp
2746 (concat
2747 "\\(\\<[012]?[0-9]"
2748 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2749 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
2750 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
2751 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
2752 groups carry important information:
2753 0 the full match
2754 7 hours of duration
2755 9 minutes of duration")
2757 (defconst org-stamp-time-of-day-regexp
2758 (concat
2759 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
2760 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
2761 "\\(--?"
2762 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
2763 "Regular expression to match a timestamp time or time range.
2764 After a match, the following groups carry important information:
2765 0 the full match
2766 1 date plus weekday, for backreferencing to make sure both times on same day
2767 2 the first time, range or not
2768 4 the second time, if it is a range.")
2770 (defconst org-startup-options
2771 '(("fold" org-startup-folded t)
2772 ("overview" org-startup-folded t)
2773 ("nofold" org-startup-folded nil)
2774 ("showall" org-startup-folded nil)
2775 ("content" org-startup-folded content)
2776 ("hidestars" org-hide-leading-stars t)
2777 ("showstars" org-hide-leading-stars nil)
2778 ("odd" org-odd-levels-only t)
2779 ("oddeven" org-odd-levels-only nil)
2780 ("align" org-startup-align-all-tables t)
2781 ("noalign" org-startup-align-all-tables nil)
2782 ("customtime" org-display-custom-times t)
2783 ("logdone" org-log-done time)
2784 ("lognotedone" org-log-done note)
2785 ("nologdone" org-log-done nil)
2786 ("lognoteclock-out" org-log-note-clock-out t)
2787 ("nolognoteclock-out" org-log-note-clock-out nil)
2788 ("logrepeat" org-log-repeat state)
2789 ("lognoterepeat" org-log-repeat note)
2790 ("nologrepeat" org-log-repeat nil)
2791 ("constcgs" constants-unit-system cgs)
2792 ("constSI" constants-unit-system SI))
2793 "Variable associated with STARTUP options for org-mode.
2794 Each element is a list of three items: The startup options as written
2795 in the #+STARTUP line, the corresponding variable, and the value to
2796 set this variable to if the option is found. An optional forth element PUSH
2797 means to push this value onto the list in the variable.")
2799 (defun org-set-regexps-and-options ()
2800 "Precompute regular expressions for current buffer."
2801 (when (org-mode-p)
2802 (org-set-local 'org-todo-kwd-alist nil)
2803 (org-set-local 'org-todo-key-alist nil)
2804 (org-set-local 'org-todo-key-trigger nil)
2805 (org-set-local 'org-todo-keywords-1 nil)
2806 (org-set-local 'org-done-keywords nil)
2807 (org-set-local 'org-todo-heads nil)
2808 (org-set-local 'org-todo-sets nil)
2809 (org-set-local 'org-todo-log-states nil)
2810 (org-set-local 'org-file-properties nil)
2811 (org-set-local 'org-file-tags nil)
2812 (let ((re (org-make-options-regexp
2813 '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
2814 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
2815 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
2816 (splitre "[ \t]+")
2817 kwds kws0 kwsa key log value cat arch tags const links hw dws
2818 tail sep kws1 prio props ftags drawers
2819 ext-setup-or-nil setup-contents (start 0))
2820 (save-excursion
2821 (save-restriction
2822 (widen)
2823 (goto-char (point-min))
2824 (while (or (and ext-setup-or-nil
2825 (string-match re ext-setup-or-nil start)
2826 (setq start (match-end 0)))
2827 (and (setq ext-setup-or-nil nil start 0)
2828 (re-search-forward re nil t)))
2829 (setq key (upcase (match-string 1 ext-setup-or-nil))
2830 value (org-match-string-no-properties 2 ext-setup-or-nil))
2831 (cond
2832 ((equal key "CATEGORY")
2833 (if (string-match "[ \t]+$" value)
2834 (setq value (replace-match "" t t value)))
2835 (setq cat value))
2836 ((member key '("SEQ_TODO" "TODO"))
2837 (push (cons 'sequence (org-split-string value splitre)) kwds))
2838 ((equal key "TYP_TODO")
2839 (push (cons 'type (org-split-string value splitre)) kwds))
2840 ((equal key "TAGS")
2841 (setq tags (append tags (org-split-string value splitre))))
2842 ((equal key "COLUMNS")
2843 (org-set-local 'org-columns-default-format value))
2844 ((equal key "LINK")
2845 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
2846 (push (cons (match-string 1 value)
2847 (org-trim (match-string 2 value)))
2848 links)))
2849 ((equal key "PRIORITIES")
2850 (setq prio (org-split-string value " +")))
2851 ((equal key "PROPERTY")
2852 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
2853 (push (cons (match-string 1 value) (match-string 2 value))
2854 props)))
2855 ((equal key "FILETAGS")
2856 (when (string-match "\\S-" value)
2857 (setq ftags
2858 (append
2859 ftags
2860 (apply 'append
2861 (mapcar (lambda (x) (org-split-string x ":"))
2862 (org-split-string value)))))))
2863 ((equal key "DRAWERS")
2864 (setq drawers (org-split-string value splitre)))
2865 ((equal key "CONSTANTS")
2866 (setq const (append const (org-split-string value splitre))))
2867 ((equal key "STARTUP")
2868 (let ((opts (org-split-string value splitre))
2869 l var val)
2870 (while (setq l (pop opts))
2871 (when (setq l (assoc l org-startup-options))
2872 (setq var (nth 1 l) val (nth 2 l))
2873 (if (not (nth 3 l))
2874 (set (make-local-variable var) val)
2875 (if (not (listp (symbol-value var)))
2876 (set (make-local-variable var) nil))
2877 (set (make-local-variable var) (symbol-value var))
2878 (add-to-list var val))))))
2879 ((equal key "ARCHIVE")
2880 (string-match " *$" value)
2881 (setq arch (replace-match "" t t value))
2882 (remove-text-properties 0 (length arch)
2883 '(face t fontified t) arch))
2884 ((equal key "SETUPFILE")
2885 (setq setup-contents (org-file-contents
2886 (expand-file-name
2887 (org-remove-double-quotes value))
2888 'noerror))
2889 (if (not ext-setup-or-nil)
2890 (setq ext-setup-or-nil setup-contents start 0)
2891 (setq ext-setup-or-nil
2892 (concat (substring ext-setup-or-nil 0 start)
2893 "\n" setup-contents "\n"
2894 (substring ext-setup-or-nil start)))))
2895 ))))
2896 (when cat
2897 (org-set-local 'org-category (intern cat))
2898 (push (cons "CATEGORY" cat) props))
2899 (when prio
2900 (if (< (length prio) 3) (setq prio '("A" "C" "B")))
2901 (setq prio (mapcar 'string-to-char prio))
2902 (org-set-local 'org-highest-priority (nth 0 prio))
2903 (org-set-local 'org-lowest-priority (nth 1 prio))
2904 (org-set-local 'org-default-priority (nth 2 prio)))
2905 (and props (org-set-local 'org-file-properties (nreverse props)))
2906 (and ftags (org-set-local 'org-file-tags ftags))
2907 (and drawers (org-set-local 'org-drawers drawers))
2908 (and arch (org-set-local 'org-archive-location arch))
2909 (and links (setq org-link-abbrev-alist-local (nreverse links)))
2910 ;; Process the TODO keywords
2911 (unless kwds
2912 ;; Use the global values as if they had been given locally.
2913 (setq kwds (default-value 'org-todo-keywords))
2914 (if (stringp (car kwds))
2915 (setq kwds (list (cons org-todo-interpretation
2916 (default-value 'org-todo-keywords)))))
2917 (setq kwds (reverse kwds)))
2918 (setq kwds (nreverse kwds))
2919 (let (inter kws kw)
2920 (while (setq kws (pop kwds))
2921 (setq inter (pop kws) sep (member "|" kws)
2922 kws0 (delete "|" (copy-sequence kws))
2923 kwsa nil
2924 kws1 (mapcar
2925 (lambda (x)
2926 ;; 1 2
2927 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
2928 (progn
2929 (setq kw (match-string 1 x)
2930 key (and (match-end 2) (match-string 2 x))
2931 log (org-extract-log-state-settings x))
2932 (push (cons kw (and key (string-to-char key))) kwsa)
2933 (and log (push log org-todo-log-states))
2935 (error "Invalid TODO keyword %s" x)))
2936 kws0)
2937 kwsa (if kwsa (append '((:startgroup))
2938 (nreverse kwsa)
2939 '((:endgroup))))
2940 hw (car kws1)
2941 dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
2942 tail (list inter hw (car dws) (org-last dws)))
2943 (add-to-list 'org-todo-heads hw 'append)
2944 (push kws1 org-todo-sets)
2945 (setq org-done-keywords (append org-done-keywords dws nil))
2946 (setq org-todo-key-alist (append org-todo-key-alist kwsa))
2947 (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
2948 (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
2949 (setq org-todo-sets (nreverse org-todo-sets)
2950 org-todo-kwd-alist (nreverse org-todo-kwd-alist)
2951 org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
2952 org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
2953 ;; Process the constants
2954 (when const
2955 (let (e cst)
2956 (while (setq e (pop const))
2957 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
2958 (push (cons (match-string 1 e) (match-string 2 e)) cst)))
2959 (setq org-table-formula-constants-local cst)))
2961 ;; Process the tags.
2962 (when tags
2963 (let (e tgs)
2964 (while (setq e (pop tags))
2965 (cond
2966 ((equal e "{") (push '(:startgroup) tgs))
2967 ((equal e "}") (push '(:endgroup) tgs))
2968 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
2969 (push (cons (match-string 1 e)
2970 (string-to-char (match-string 2 e)))
2971 tgs))
2972 (t (push (list e) tgs))))
2973 (org-set-local 'org-tag-alist nil)
2974 (while (setq e (pop tgs))
2975 (or (and (stringp (car e))
2976 (assoc (car e) org-tag-alist))
2977 (push e org-tag-alist)))))
2979 ;; Compute the regular expressions and other local variables
2980 (if (not org-done-keywords)
2981 (setq org-done-keywords (list (org-last org-todo-keywords-1))))
2982 (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
2983 (length org-scheduled-string)
2984 (length org-clock-string)
2985 (length org-closed-string)))
2986 org-drawer-regexp
2987 (concat "^[ \t]*:\\("
2988 (mapconcat 'regexp-quote org-drawers "\\|")
2989 "\\):[ \t]*$")
2990 org-not-done-keywords
2991 (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
2992 org-todo-regexp
2993 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
2994 "\\|") "\\)\\>")
2995 org-not-done-regexp
2996 (concat "\\<\\("
2997 (mapconcat 'regexp-quote org-not-done-keywords "\\|")
2998 "\\)\\>")
2999 org-todo-line-regexp
3000 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3001 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
3002 "\\)\\>\\)?[ \t]*\\(.*\\)")
3003 org-complex-heading-regexp
3004 (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
3005 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
3006 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
3007 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3008 org-nl-done-regexp
3009 (concat "\n\\*+[ \t]+"
3010 "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
3011 "\\)" "\\>")
3012 org-todo-line-tags-regexp
3013 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3014 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
3015 (org-re
3016 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
3017 org-looking-at-done-regexp
3018 (concat "^" "\\(?:"
3019 (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
3020 "\\>")
3021 org-deadline-regexp (concat "\\<" org-deadline-string)
3022 org-deadline-time-regexp
3023 (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
3024 org-deadline-line-regexp
3025 (concat "\\<\\(" org-deadline-string "\\).*")
3026 org-scheduled-regexp
3027 (concat "\\<" org-scheduled-string)
3028 org-scheduled-time-regexp
3029 (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
3030 org-closed-time-regexp
3031 (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
3032 org-keyword-time-regexp
3033 (concat "\\<\\(" org-scheduled-string
3034 "\\|" org-deadline-string
3035 "\\|" org-closed-string
3036 "\\|" org-clock-string "\\)"
3037 " *[[<]\\([^]>]+\\)[]>]")
3038 org-keyword-time-not-clock-regexp
3039 (concat "\\<\\(" org-scheduled-string
3040 "\\|" org-deadline-string
3041 "\\|" org-closed-string
3042 "\\)"
3043 " *[[<]\\([^]>]+\\)[]>]")
3044 org-maybe-keyword-time-regexp
3045 (concat "\\(\\<\\(" org-scheduled-string
3046 "\\|" org-deadline-string
3047 "\\|" org-closed-string
3048 "\\|" org-clock-string "\\)\\)?"
3049 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
3050 org-planning-or-clock-line-re
3051 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
3052 "\\|" org-deadline-string
3053 "\\|" org-closed-string "\\|" org-clock-string
3054 "\\)\\>\\)")
3056 (org-compute-latex-and-specials-regexp)
3057 (org-set-font-lock-defaults))))
3059 (defun org-file-contents (file &optional noerror)
3060 "Return the contents of FILE, as a string."
3061 (if (or (not file)
3062 (not (file-readable-p file)))
3063 (if noerror
3064 (progn
3065 (message "Cannot read file %s" file)
3066 (ding) (sit-for 2)
3068 (error "Cannot read file %s" file))
3069 (with-temp-buffer
3070 (insert-file-contents file)
3071 (buffer-string))))
3073 (defun org-extract-log-state-settings (x)
3074 "Extract the log state setting from a TODO keyword string.
3075 This will extract info from a string like \"WAIT(w@/!)\"."
3076 (let (kw key log1 log2)
3077 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
3078 (setq kw (match-string 1 x)
3079 key (and (match-end 2) (match-string 2 x))
3080 log1 (and (match-end 3) (match-string 3 x))
3081 log2 (and (match-end 4) (match-string 4 x)))
3082 (and (or log1 log2)
3083 (list kw
3084 (and log1 (if (equal log1 "!") 'time 'note))
3085 (and log2 (if (equal log2 "!") 'time 'note)))))))
3087 (defun org-remove-keyword-keys (list)
3088 "Remove a pair of parenthesis at the end of each string in LIST."
3089 (mapcar (lambda (x)
3090 (if (string-match "(.*)$" x)
3091 (substring x 0 (match-beginning 0))
3093 list))
3095 ;; FIXME: this could be done much better, using second characters etc.
3096 (defun org-assign-fast-keys (alist)
3097 "Assign fast keys to a keyword-key alist.
3098 Respect keys that are already there."
3099 (let (new e k c c1 c2 (char ?a))
3100 (while (setq e (pop alist))
3101 (cond
3102 ((equal e '(:startgroup)) (push e new))
3103 ((equal e '(:endgroup)) (push e new))
3105 (setq k (car e) c2 nil)
3106 (if (cdr e)
3107 (setq c (cdr e))
3108 ;; automatically assign a character.
3109 (setq c1 (string-to-char
3110 (downcase (substring
3111 k (if (= (string-to-char k) ?@) 1 0)))))
3112 (if (or (rassoc c1 new) (rassoc c1 alist))
3113 (while (or (rassoc char new) (rassoc char alist))
3114 (setq char (1+ char)))
3115 (setq c2 c1))
3116 (setq c (or c2 char)))
3117 (push (cons k c) new))))
3118 (nreverse new)))
3120 ;;; Some variables used in various places
3122 (defvar org-window-configuration nil
3123 "Used in various places to store a window configuration.")
3124 (defvar org-finish-function nil
3125 "Function to be called when `C-c C-c' is used.
3126 This is for getting out of special buffers like remember.")
3129 ;; FIXME: Occasionally check by commenting these, to make sure
3130 ;; no other functions uses these, forgetting to let-bind them.
3131 (defvar entry)
3132 (defvar state)
3133 (defvar last-state)
3134 (defvar date)
3135 (defvar description)
3137 ;; Defined somewhere in this file, but used before definition.
3138 (defvar org-html-entities)
3139 (defvar org-struct-menu)
3140 (defvar org-org-menu)
3141 (defvar org-tbl-menu)
3142 (defvar org-agenda-keymap)
3144 ;;;; Define the Org-mode
3146 (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
3147 (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22."))
3150 ;; We use a before-change function to check if a table might need
3151 ;; an update.
3152 (defvar org-table-may-need-update t
3153 "Indicates that a table might need an update.
3154 This variable is set by `org-before-change-function'.
3155 `org-table-align' sets it back to nil.")
3156 (defun org-before-change-function (beg end)
3157 "Every change indicates that a table might need an update."
3158 (setq org-table-may-need-update t))
3159 (defvar org-mode-map)
3160 (defvar org-mode-hook nil)
3161 (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
3162 (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
3163 (defvar org-table-buffer-is-an nil)
3164 (defconst org-outline-regexp "\\*+ ")
3166 ;;;###autoload
3167 (define-derived-mode org-mode outline-mode "Org"
3168 "Outline-based notes management and organizer, alias
3169 \"Carsten's outline-mode for keeping track of everything.\"
3171 Org-mode develops organizational tasks around a NOTES file which
3172 contains information about projects as plain text. Org-mode is
3173 implemented on top of outline-mode, which is ideal to keep the content
3174 of large files well structured. It supports ToDo items, deadlines and
3175 time stamps, which magically appear in the diary listing of the Emacs
3176 calendar. Tables are easily created with a built-in table editor.
3177 Plain text URL-like links connect to websites, emails (VM), Usenet
3178 messages (Gnus), BBDB entries, and any files related to the project.
3179 For printing and sharing of notes, an Org-mode file (or a part of it)
3180 can be exported as a structured ASCII or HTML file.
3182 The following commands are available:
3184 \\{org-mode-map}"
3186 ;; Get rid of Outline menus, they are not needed
3187 ;; Need to do this here because define-derived-mode sets up
3188 ;; the keymap so late. Still, it is a waste to call this each time
3189 ;; we switch another buffer into org-mode.
3190 (if (featurep 'xemacs)
3191 (when (boundp 'outline-mode-menu-heading)
3192 ;; Assume this is Greg's port, it used easymenu
3193 (easy-menu-remove outline-mode-menu-heading)
3194 (easy-menu-remove outline-mode-menu-show)
3195 (easy-menu-remove outline-mode-menu-hide))
3196 (define-key org-mode-map [menu-bar headings] 'undefined)
3197 (define-key org-mode-map [menu-bar hide] 'undefined)
3198 (define-key org-mode-map [menu-bar show] 'undefined))
3200 (org-load-modules-maybe)
3201 (easy-menu-add org-org-menu)
3202 (easy-menu-add org-tbl-menu)
3203 (org-install-agenda-files-menu)
3204 (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
3205 (org-add-to-invisibility-spec '(org-cwidth))
3206 (when (featurep 'xemacs)
3207 (org-set-local 'line-move-ignore-invisible t))
3208 (org-set-local 'outline-regexp org-outline-regexp)
3209 (org-set-local 'outline-level 'org-outline-level)
3210 (when (and org-ellipsis
3211 (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
3212 (fboundp 'make-glyph-code))
3213 (unless org-display-table
3214 (setq org-display-table (make-display-table)))
3215 (set-display-table-slot
3216 org-display-table 4
3217 (vconcat (mapcar
3218 (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
3219 org-ellipsis)))
3220 (if (stringp org-ellipsis) org-ellipsis "..."))))
3221 (setq buffer-display-table org-display-table))
3222 (org-set-regexps-and-options)
3223 ;; Calc embedded
3224 (org-set-local 'calc-embedded-open-mode "# ")
3225 (modify-syntax-entry ?# "<")
3226 (modify-syntax-entry ?@ "w")
3227 (if org-startup-truncated (setq truncate-lines t))
3228 (org-set-local 'font-lock-unfontify-region-function
3229 'org-unfontify-region)
3230 ;; Activate before-change-function
3231 (org-set-local 'org-table-may-need-update t)
3232 (org-add-hook 'before-change-functions 'org-before-change-function nil
3233 'local)
3234 ;; Check for running clock before killing a buffer
3235 (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
3236 ;; Paragraphs and auto-filling
3237 (org-set-autofill-regexps)
3238 (setq indent-line-function 'org-indent-line-function)
3239 (org-update-radio-target-regexp)
3241 ;; Comment characters
3242 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
3243 (org-set-local 'comment-padding " ")
3245 ;; Align options lines
3246 (org-set-local
3247 'align-mode-rules-list
3248 '((org-in-buffer-settings
3249 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
3250 (modes . '(org-mode)))))
3252 ;; Imenu
3253 (org-set-local 'imenu-create-index-function
3254 'org-imenu-get-tree)
3256 ;; Make isearch reveal context
3257 (if (or (featurep 'xemacs)
3258 (not (boundp 'outline-isearch-open-invisible-function)))
3259 ;; Emacs 21 and XEmacs make use of the hook
3260 (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
3261 ;; Emacs 22 deals with this through a special variable
3262 (org-set-local 'outline-isearch-open-invisible-function
3263 (lambda (&rest ignore) (org-show-context 'isearch))))
3265 ;; If empty file that did not turn on org-mode automatically, make it to.
3266 (if (and org-insert-mode-line-in-empty-file
3267 (interactive-p)
3268 (= (point-min) (point-max)))
3269 (insert "# -*- mode: org -*-\n\n"))
3271 (unless org-inhibit-startup
3272 (when org-startup-align-all-tables
3273 (let ((bmp (buffer-modified-p)))
3274 (org-table-map-tables 'org-table-align)
3275 (set-buffer-modified-p bmp)))
3276 (org-set-startup-visibility)))
3278 (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
3280 (defun org-current-time ()
3281 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
3282 (if (> (car org-time-stamp-rounding-minutes) 1)
3283 (let ((r (car org-time-stamp-rounding-minutes))
3284 (time (decode-time)))
3285 (apply 'encode-time
3286 (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
3287 (nthcdr 2 time))))
3288 (current-time)))
3290 ;;;; Font-Lock stuff, including the activators
3292 (defvar org-mouse-map (make-sparse-keymap))
3293 (org-defkey org-mouse-map
3294 (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
3295 (org-defkey org-mouse-map
3296 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
3297 (when org-mouse-1-follows-link
3298 (org-defkey org-mouse-map [follow-link] 'mouse-face))
3299 (when org-tab-follows-link
3300 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
3301 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
3302 (when org-return-follows-link
3303 (org-defkey org-mouse-map [(return)] 'org-open-at-point)
3304 (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
3306 (require 'font-lock)
3308 (defconst org-non-link-chars "]\t\n\r<>")
3309 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
3310 "shell" "elisp"))
3311 (defvar org-link-types-re nil
3312 "Matches a link that has a url-like prefix like \"http:\"")
3313 (defvar org-link-re-with-space nil
3314 "Matches a link with spaces, optional angular brackets around it.")
3315 (defvar org-link-re-with-space2 nil
3316 "Matches a link with spaces, optional angular brackets around it.")
3317 (defvar org-angle-link-re nil
3318 "Matches link with angular brackets, spaces are allowed.")
3319 (defvar org-plain-link-re nil
3320 "Matches plain link, without spaces.")
3321 (defvar org-bracket-link-regexp nil
3322 "Matches a link in double brackets.")
3323 (defvar org-bracket-link-analytic-regexp nil
3324 "Regular expression used to analyze links.
3325 Here is what the match groups contain after a match:
3326 1: http:
3327 2: http
3328 3: path
3329 4: [desc]
3330 5: desc")
3331 (defvar org-any-link-re nil
3332 "Regular expression matching any link.")
3334 (defun org-make-link-regexps ()
3335 "Update the link regular expressions.
3336 This should be called after the variable `org-link-types' has changed."
3337 (setq org-link-types-re
3338 (concat
3339 "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
3340 org-link-re-with-space
3341 (concat
3342 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3343 "\\([^" org-non-link-chars " ]"
3344 "[^" org-non-link-chars "]*"
3345 "[^" org-non-link-chars " ]\\)>?")
3346 org-link-re-with-space2
3347 (concat
3348 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3349 "\\([^" org-non-link-chars " ]"
3350 "[^]\t\n\r]*"
3351 "[^" org-non-link-chars " ]\\)>?")
3352 org-angle-link-re
3353 (concat
3354 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3355 "\\([^" org-non-link-chars " ]"
3356 "[^" org-non-link-chars "]*"
3357 "\\)>")
3358 org-plain-link-re
3359 (concat
3360 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3361 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
3362 org-bracket-link-regexp
3363 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
3364 org-bracket-link-analytic-regexp
3365 (concat
3366 "\\[\\["
3367 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
3368 "\\([^]]+\\)"
3369 "\\]"
3370 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
3371 "\\]")
3372 org-any-link-re
3373 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
3374 org-angle-link-re "\\)\\|\\("
3375 org-plain-link-re "\\)")))
3377 (org-make-link-regexps)
3379 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
3380 "Regular expression for fast time stamp matching.")
3381 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
3382 "Regular expression for fast time stamp matching.")
3383 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3384 "Regular expression matching time strings for analysis.
3385 This one does not require the space after the date, so it can be used
3386 on a string that terminates immediately after the date.")
3387 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3388 "Regular expression matching time strings for analysis.")
3389 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
3390 "Regular expression matching time stamps, with groups.")
3391 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
3392 "Regular expression matching time stamps (also [..]), with groups.")
3393 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
3394 "Regular expression matching a time stamp range.")
3395 (defconst org-tr-regexp-both
3396 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
3397 "Regular expression matching a time stamp range.")
3398 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
3399 org-ts-regexp "\\)?")
3400 "Regular expression matching a time stamp or time stamp range.")
3401 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
3402 org-ts-regexp-both "\\)?")
3403 "Regular expression matching a time stamp or time stamp range.
3404 The time stamps may be either active or inactive.")
3406 (defvar org-emph-face nil)
3408 (defun org-do-emphasis-faces (limit)
3409 "Run through the buffer and add overlays to links."
3410 (let (rtn)
3411 (while (and (not rtn) (re-search-forward org-emph-re limit t))
3412 (if (not (= (char-after (match-beginning 3))
3413 (char-after (match-beginning 4))))
3414 (progn
3415 (setq rtn t)
3416 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
3417 'face
3418 (nth 1 (assoc (match-string 3)
3419 org-emphasis-alist)))
3420 (add-text-properties (match-beginning 2) (match-end 2)
3421 '(font-lock-multiline t))
3422 (when org-hide-emphasis-markers
3423 (add-text-properties (match-end 4) (match-beginning 5)
3424 '(invisible org-link))
3425 (add-text-properties (match-beginning 3) (match-end 3)
3426 '(invisible org-link)))))
3427 (backward-char 1))
3428 rtn))
3430 (defun org-emphasize (&optional char)
3431 "Insert or change an emphasis, i.e. a font like bold or italic.
3432 If there is an active region, change that region to a new emphasis.
3433 If there is no region, just insert the marker characters and position
3434 the cursor between them.
3435 CHAR should be either the marker character, or the first character of the
3436 HTML tag associated with that emphasis. If CHAR is a space, the means
3437 to remove the emphasis of the selected region.
3438 If char is not given (for example in an interactive call) it
3439 will be prompted for."
3440 (interactive)
3441 (let ((eal org-emphasis-alist) e det
3442 (erc org-emphasis-regexp-components)
3443 (prompt "")
3444 (string "") beg end move tag c s)
3445 (if (org-region-active-p)
3446 (setq beg (region-beginning) end (region-end)
3447 string (buffer-substring beg end))
3448 (setq move t))
3450 (while (setq e (pop eal))
3451 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
3452 c (aref tag 0))
3453 (push (cons c (string-to-char (car e))) det)
3454 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
3455 (substring tag 1)))))
3456 (unless char
3457 (message "%s" (concat "Emphasis marker or tag:" prompt))
3458 (setq char (read-char-exclusive)))
3459 (setq char (or (cdr (assoc char det)) char))
3460 (if (equal char ?\ )
3461 (setq s "" move nil)
3462 (unless (assoc (char-to-string char) org-emphasis-alist)
3463 (error "No such emphasis marker: \"%c\"" char))
3464 (setq s (char-to-string char)))
3465 (while (and (> (length string) 1)
3466 (equal (substring string 0 1) (substring string -1))
3467 (assoc (substring string 0 1) org-emphasis-alist))
3468 (setq string (substring string 1 -1)))
3469 (setq string (concat s string s))
3470 (if beg (delete-region beg end))
3471 (unless (or (bolp)
3472 (string-match (concat "[" (nth 0 erc) "\n]")
3473 (char-to-string (char-before (point)))))
3474 (insert " "))
3475 (unless (string-match (concat "[" (nth 1 erc) "\n]")
3476 (char-to-string (char-after (point))))
3477 (insert " ") (backward-char 1))
3478 (insert string)
3479 (and move (backward-char 1))))
3481 (defconst org-nonsticky-props
3482 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
3485 (defun org-activate-plain-links (limit)
3486 "Run through the buffer and add overlays to links."
3487 (catch 'exit
3488 (let (f)
3489 (while (re-search-forward org-plain-link-re limit t)
3490 (setq f (get-text-property (match-beginning 0) 'face))
3491 (if (or (eq f 'org-tag)
3492 (and (listp f) (memq 'org-tag f)))
3494 (add-text-properties (match-beginning 0) (match-end 0)
3495 (list 'mouse-face 'highlight
3496 'rear-nonsticky org-nonsticky-props
3497 'keymap org-mouse-map
3499 (throw 'exit t))))))
3501 (defun org-activate-code (limit)
3502 (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
3503 (unless (get-text-property (match-beginning 1) 'face)
3504 (remove-text-properties (match-beginning 0) (match-end 0)
3505 '(display t invisible t intangible t))
3506 t)))
3508 (defun org-activate-angle-links (limit)
3509 "Run through the buffer and add overlays to links."
3510 (if (re-search-forward org-angle-link-re limit t)
3511 (progn
3512 (add-text-properties (match-beginning 0) (match-end 0)
3513 (list 'mouse-face 'highlight
3514 'rear-nonsticky org-nonsticky-props
3515 'keymap org-mouse-map
3517 t)))
3519 (defun org-activate-bracket-links (limit)
3520 "Run through the buffer and add overlays to bracketed links."
3521 (if (re-search-forward org-bracket-link-regexp limit t)
3522 (let* ((help (concat "LINK: "
3523 (org-match-string-no-properties 1)))
3524 ;; FIXME: above we should remove the escapes.
3525 ;; but that requires another match, protecting match data,
3526 ;; a lot of overhead for font-lock.
3527 (ip (org-maybe-intangible
3528 (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
3529 'keymap org-mouse-map 'mouse-face 'highlight
3530 'font-lock-multiline t 'help-echo help)))
3531 (vp (list 'rear-nonsticky org-nonsticky-props
3532 'keymap org-mouse-map 'mouse-face 'highlight
3533 ' font-lock-multiline t 'help-echo help)))
3534 ;; We need to remove the invisible property here. Table narrowing
3535 ;; may have made some of this invisible.
3536 (remove-text-properties (match-beginning 0) (match-end 0)
3537 '(invisible nil))
3538 (if (match-end 3)
3539 (progn
3540 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
3541 (add-text-properties (match-beginning 3) (match-end 3) vp)
3542 (add-text-properties (match-end 3) (match-end 0) ip))
3543 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
3544 (add-text-properties (match-beginning 1) (match-end 1) vp)
3545 (add-text-properties (match-end 1) (match-end 0) ip))
3546 t)))
3548 (defun org-activate-dates (limit)
3549 "Run through the buffer and add overlays to dates."
3550 (if (re-search-forward org-tsr-regexp-both limit t)
3551 (progn
3552 (add-text-properties (match-beginning 0) (match-end 0)
3553 (list 'mouse-face 'highlight
3554 'rear-nonsticky org-nonsticky-props
3555 'keymap org-mouse-map))
3556 (when org-display-custom-times
3557 (if (match-end 3)
3558 (org-display-custom-time (match-beginning 3) (match-end 3)))
3559 (org-display-custom-time (match-beginning 1) (match-end 1)))
3560 t)))
3562 (defvar org-target-link-regexp nil
3563 "Regular expression matching radio targets in plain text.")
3564 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
3565 "Regular expression matching a link target.")
3566 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
3567 "Regular expression matching a radio target.")
3568 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
3569 "Regular expression matching any target.")
3571 (defun org-activate-target-links (limit)
3572 "Run through the buffer and add overlays to target matches."
3573 (when org-target-link-regexp
3574 (let ((case-fold-search t))
3575 (if (re-search-forward org-target-link-regexp limit t)
3576 (progn
3577 (add-text-properties (match-beginning 0) (match-end 0)
3578 (list 'mouse-face 'highlight
3579 'rear-nonsticky org-nonsticky-props
3580 'keymap org-mouse-map
3581 'help-echo "Radio target link"
3582 'org-linked-text t))
3583 t)))))
3585 (defun org-update-radio-target-regexp ()
3586 "Find all radio targets in this file and update the regular expression."
3587 (interactive)
3588 (when (memq 'radio org-activate-links)
3589 (setq org-target-link-regexp
3590 (org-make-target-link-regexp (org-all-targets 'radio)))
3591 (org-restart-font-lock)))
3593 (defun org-hide-wide-columns (limit)
3594 (let (s e)
3595 (setq s (text-property-any (point) (or limit (point-max))
3596 'org-cwidth t))
3597 (when s
3598 (setq e (next-single-property-change s 'org-cwidth))
3599 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
3600 (goto-char e)
3601 t)))
3603 (defvar org-latex-and-specials-regexp nil
3604 "Regular expression for highlighting export special stuff.")
3605 (defvar org-match-substring-regexp)
3606 (defvar org-match-substring-with-braces-regexp)
3607 (defvar org-export-html-special-string-regexps)
3609 (defun org-compute-latex-and-specials-regexp ()
3610 "Compute regular expression for stuff treated specially by exporters."
3611 (if (not org-highlight-latex-fragments-and-specials)
3612 (org-set-local 'org-latex-and-specials-regexp nil)
3613 (require 'org-exp)
3614 (let*
3615 ((matchers (plist-get org-format-latex-options :matchers))
3616 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
3617 org-latex-regexps)))
3618 (options (org-combine-plists (org-default-export-plist)
3619 (org-infile-export-plist)))
3620 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
3621 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
3622 (org-export-with-TeX-macros (plist-get options :TeX-macros))
3623 (org-export-html-expand (plist-get options :expand-quoted-html))
3624 (org-export-with-special-strings (plist-get options :special-strings))
3625 (re-sub
3626 (cond
3627 ((equal org-export-with-sub-superscripts '{})
3628 (list org-match-substring-with-braces-regexp))
3629 (org-export-with-sub-superscripts
3630 (list org-match-substring-regexp))
3631 (t nil)))
3632 (re-latex
3633 (if org-export-with-LaTeX-fragments
3634 (mapcar (lambda (x) (nth 1 x)) latexs)))
3635 (re-macros
3636 (if org-export-with-TeX-macros
3637 (list (concat "\\\\"
3638 (regexp-opt
3639 (append (mapcar 'car org-html-entities)
3640 (if (boundp 'org-latex-entities)
3641 org-latex-entities nil))
3642 'words))) ; FIXME
3644 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
3645 (re-special (if org-export-with-special-strings
3646 (mapcar (lambda (x) (car x))
3647 org-export-html-special-string-regexps)))
3648 (re-rest
3649 (delq nil
3650 (list
3651 (if org-export-html-expand "@<[^>\n]+>")
3652 ))))
3653 (org-set-local
3654 'org-latex-and-specials-regexp
3655 (mapconcat 'identity (append re-latex re-sub re-macros re-special
3656 re-rest) "\\|")))))
3658 (defun org-do-latex-and-special-faces (limit)
3659 "Run through the buffer and add overlays to links."
3660 (when org-latex-and-specials-regexp
3661 (let (rtn d)
3662 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
3663 limit t))
3664 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
3665 'face))
3666 '(org-code org-verbatim underline)))
3667 (progn
3668 (setq rtn t
3669 d (cond ((member (char-after (1+ (match-beginning 0)))
3670 '(?_ ?^)) 1)
3671 (t 0)))
3672 (font-lock-prepend-text-property
3673 (+ d (match-beginning 0)) (match-end 0)
3674 'face 'org-latex-and-export-specials)
3675 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
3676 '(font-lock-multiline t)))))
3677 rtn)))
3679 (defun org-restart-font-lock ()
3680 "Restart font-lock-mode, to force refontification."
3681 (when (and (boundp 'font-lock-mode) font-lock-mode)
3682 (font-lock-mode -1)
3683 (font-lock-mode 1)))
3685 (defun org-all-targets (&optional radio)
3686 "Return a list of all targets in this file.
3687 With optional argument RADIO, only find radio targets."
3688 (let ((re (if radio org-radio-target-regexp org-target-regexp))
3689 rtn)
3690 (save-excursion
3691 (goto-char (point-min))
3692 (while (re-search-forward re nil t)
3693 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
3694 rtn)))
3696 (defun org-make-target-link-regexp (targets)
3697 "Make regular expression matching all strings in TARGETS.
3698 The regular expression finds the targets also if there is a line break
3699 between words."
3700 (and targets
3701 (concat
3702 "\\<\\("
3703 (mapconcat
3704 (lambda (x)
3705 (while (string-match " +" x)
3706 (setq x (replace-match "\\s-+" t t x)))
3708 targets
3709 "\\|")
3710 "\\)\\>")))
3712 (defun org-activate-tags (limit)
3713 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
3714 (progn
3715 (add-text-properties (match-beginning 1) (match-end 1)
3716 (list 'mouse-face 'highlight
3717 'rear-nonsticky org-nonsticky-props
3718 'keymap org-mouse-map))
3719 t)))
3721 (defun org-outline-level ()
3722 (save-excursion
3723 (looking-at outline-regexp)
3724 (if (match-beginning 1)
3725 (+ (org-get-string-indentation (match-string 1)) 1000)
3726 (1- (- (match-end 0) (match-beginning 0))))))
3728 (defvar org-font-lock-keywords nil)
3730 (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
3731 "Regular expression matching a property line.")
3733 (defvar org-font-lock-hook nil
3734 "Functions to be called for special font lock stuff.")
3736 (defun org-font-lock-hook (limit)
3737 (run-hook-with-args 'org-font-lock-hook limit))
3739 (defun org-set-font-lock-defaults ()
3740 (let* ((em org-fontify-emphasized-text)
3741 (lk org-activate-links)
3742 (org-font-lock-extra-keywords
3743 (list
3744 ;; Call the hook
3745 '(org-font-lock-hook)
3746 ;; Headlines
3747 '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
3748 (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
3749 ;; Table lines
3750 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
3751 (1 'org-table t))
3752 ;; Table internals
3753 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
3754 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
3755 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
3756 ;; Drawers
3757 (list org-drawer-regexp '(0 'org-special-keyword t))
3758 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
3759 ;; Properties
3760 (list org-property-re
3761 '(1 'org-special-keyword t)
3762 '(3 'org-property-value t))
3763 (if org-format-transports-properties-p
3764 '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
3765 ;; Links
3766 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
3767 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
3768 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
3769 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
3770 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
3771 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
3772 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
3773 '(org-hide-wide-columns (0 nil append))
3774 ;; TODO lines
3775 (list (concat "^\\*+[ \t]+" org-todo-regexp)
3776 '(1 (org-get-todo-face 1) t))
3777 ;; DONE
3778 (if org-fontify-done-headline
3779 (list (concat "^[*]+ +\\<\\("
3780 (mapconcat 'regexp-quote org-done-keywords "\\|")
3781 "\\)\\(.*\\)")
3782 '(2 'org-headline-done t))
3783 nil)
3784 ;; Priorities
3785 (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
3786 ;; Special keywords
3787 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
3788 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
3789 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
3790 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
3791 ;; Emphasis
3792 (if em
3793 (if (featurep 'xemacs)
3794 '(org-do-emphasis-faces (0 nil append))
3795 '(org-do-emphasis-faces)))
3796 ;; Checkboxes
3797 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
3798 2 'bold prepend)
3799 (if org-provide-checkbox-statistics
3800 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
3801 (0 (org-get-checkbox-statistics-face) t)))
3802 ;; Description list items
3803 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
3804 2 'bold prepend)
3805 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
3806 '(1 'org-archived prepend))
3807 ;; Specials
3808 '(org-do-latex-and-special-faces)
3809 ;; Code
3810 '(org-activate-code (1 'org-code t))
3811 ;; COMMENT
3812 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
3813 "\\|" org-quote-string "\\)\\>")
3814 '(1 'org-special-keyword t))
3815 '("^#.*" (0 'font-lock-comment-face t))
3817 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
3818 ;; Now set the full font-lock-keywords
3819 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
3820 (org-set-local 'font-lock-defaults
3821 '(org-font-lock-keywords t nil nil backward-paragraph))
3822 (kill-local-variable 'font-lock-keywords) nil))
3824 (defvar org-m nil)
3825 (defvar org-l nil)
3826 (defvar org-f nil)
3827 (defun org-get-level-face (n)
3828 "Get the right face for match N in font-lock matching of healdines."
3829 (setq org-l (- (match-end 2) (match-beginning 1) 1))
3830 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
3831 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
3832 (cond
3833 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
3834 ((eq n 2) org-f)
3835 (t (if org-level-color-stars-only nil org-f))))
3837 (defun org-get-todo-face (kwd)
3838 "Get the right face for a TODO keyword KWD.
3839 If KWD is a number, get the corresponding match group."
3840 (if (numberp kwd) (setq kwd (match-string kwd)))
3841 (or (cdr (assoc kwd org-todo-keyword-faces))
3842 (and (member kwd org-done-keywords) 'org-done)
3843 'org-todo))
3845 (defun org-unfontify-region (beg end &optional maybe_loudly)
3846 "Remove fontification and activation overlays from links."
3847 (font-lock-default-unfontify-region beg end)
3848 (let* ((buffer-undo-list t)
3849 (inhibit-read-only t) (inhibit-point-motion-hooks t)
3850 (inhibit-modification-hooks t)
3851 deactivate-mark buffer-file-name buffer-file-truename)
3852 (remove-text-properties beg end
3853 '(mouse-face t keymap t org-linked-text t
3854 invisible t intangible t))))
3856 ;;;; Visibility cycling, including org-goto and indirect buffer
3858 ;;; Cycling
3860 (defvar org-cycle-global-status nil)
3861 (make-variable-buffer-local 'org-cycle-global-status)
3862 (defvar org-cycle-subtree-status nil)
3863 (make-variable-buffer-local 'org-cycle-subtree-status)
3865 ;;;###autoload
3866 (defun org-cycle (&optional arg)
3867 "Visibility cycling for Org-mode.
3869 - When this function is called with a prefix argument, rotate the entire
3870 buffer through 3 states (global cycling)
3871 1. OVERVIEW: Show only top-level headlines.
3872 2. CONTENTS: Show all headlines of all levels, but no body text.
3873 3. SHOW ALL: Show everything.
3874 When called with two C-c C-u prefixes, switch to the startup visibility,
3875 determined by the variable `org-startup-folded', and by any VISIBILITY
3876 properties in the buffer.
3878 - When point is at the beginning of a headline, rotate the subtree started
3879 by this line through 3 different states (local cycling)
3880 1. FOLDED: Only the main headline is shown.
3881 2. CHILDREN: The main headline and the direct children are shown.
3882 From this state, you can move to one of the children
3883 and zoom in further.
3884 3. SUBTREE: Show the entire subtree, including body text.
3886 - When there is a numeric prefix, go up to a heading with level ARG, do
3887 a `show-subtree' and return to the previous cursor position. If ARG
3888 is negative, go up that many levels.
3890 - When point is not at the beginning of a headline, execute the global
3891 binding for TAB, which is re-indenting the line. See the option
3892 `org-cycle-emulate-tab' for details.
3894 - Special case: if point is at the beginning of the buffer and there is
3895 no headline in line 1, this function will act as if called with prefix arg.
3896 But only if also the variable `org-cycle-global-at-bob' is t."
3897 (interactive "P")
3898 (org-load-modules-maybe)
3899 (let* ((outline-regexp
3900 (if (and (org-mode-p) org-cycle-include-plain-lists)
3901 "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
3902 outline-regexp))
3903 (bob-special (and org-cycle-global-at-bob (bobp)
3904 (not (looking-at outline-regexp))))
3905 (org-cycle-hook
3906 (if bob-special
3907 (delq 'org-optimize-window-after-visibility-change
3908 (copy-sequence org-cycle-hook))
3909 org-cycle-hook))
3910 (pos (point)))
3912 (if (or bob-special (equal arg '(4)))
3913 ;; special case: use global cycling
3914 (setq arg t))
3916 (cond
3918 ((equal arg '(16))
3919 (org-set-startup-visibility)
3920 (message "Startup visibility, plus VISIBILITY properties."))
3922 ((org-at-table-p 'any)
3923 ;; Enter the table or move to the next field in the table
3924 (or (org-table-recognize-table.el)
3925 (progn
3926 (if arg (org-table-edit-field t)
3927 (org-table-justify-field-maybe)
3928 (call-interactively 'org-table-next-field)))))
3930 ((eq arg t) ;; Global cycling
3932 (cond
3933 ((and (eq last-command this-command)
3934 (eq org-cycle-global-status 'overview))
3935 ;; We just created the overview - now do table of contents
3936 ;; This can be slow in very large buffers, so indicate action
3937 (message "CONTENTS...")
3938 (org-content)
3939 (message "CONTENTS...done")
3940 (setq org-cycle-global-status 'contents)
3941 (run-hook-with-args 'org-cycle-hook 'contents))
3943 ((and (eq last-command this-command)
3944 (eq org-cycle-global-status 'contents))
3945 ;; We just showed the table of contents - now show everything
3946 (show-all)
3947 (message "SHOW ALL")
3948 (setq org-cycle-global-status 'all)
3949 (run-hook-with-args 'org-cycle-hook 'all))
3952 ;; Default action: go to overview
3953 (org-overview)
3954 (message "OVERVIEW")
3955 (setq org-cycle-global-status 'overview)
3956 (run-hook-with-args 'org-cycle-hook 'overview))))
3958 ((and org-drawers org-drawer-regexp
3959 (save-excursion
3960 (beginning-of-line 1)
3961 (looking-at org-drawer-regexp)))
3962 ;; Toggle block visibility
3963 (org-flag-drawer
3964 (not (get-char-property (match-end 0) 'invisible))))
3966 ((integerp arg)
3967 ;; Show-subtree, ARG levels up from here.
3968 (save-excursion
3969 (org-back-to-heading)
3970 (outline-up-heading (if (< arg 0) (- arg)
3971 (- (funcall outline-level) arg)))
3972 (org-show-subtree)))
3974 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
3975 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
3976 ;; At a heading: rotate between three different views
3977 (org-back-to-heading)
3978 (let ((goal-column 0) eoh eol eos)
3979 ;; First, some boundaries
3980 (save-excursion
3981 (org-back-to-heading)
3982 (save-excursion
3983 (beginning-of-line 2)
3984 (while (and (not (eobp)) ;; this is like `next-line'
3985 (get-char-property (1- (point)) 'invisible))
3986 (beginning-of-line 2)) (setq eol (point)))
3987 (outline-end-of-heading) (setq eoh (point))
3988 (org-end-of-subtree t)
3989 (unless (eobp)
3990 (skip-chars-forward " \t\n")
3991 (beginning-of-line 1) ; in case this is an item
3993 (setq eos (1- (point))))
3994 ;; Find out what to do next and set `this-command'
3995 (cond
3996 ((= eos eoh)
3997 ;; Nothing is hidden behind this heading
3998 (message "EMPTY ENTRY")
3999 (setq org-cycle-subtree-status nil)
4000 (save-excursion
4001 (goto-char eos)
4002 (outline-next-heading)
4003 (if (org-invisible-p) (org-flag-heading nil))))
4004 ((or (>= eol eos)
4005 (not (string-match "\\S-" (buffer-substring eol eos))))
4006 ;; Entire subtree is hidden in one line: open it
4007 (org-show-entry)
4008 (show-children)
4009 (message "CHILDREN")
4010 (save-excursion
4011 (goto-char eos)
4012 (outline-next-heading)
4013 (if (org-invisible-p) (org-flag-heading nil)))
4014 (setq org-cycle-subtree-status 'children)
4015 (run-hook-with-args 'org-cycle-hook 'children))
4016 ((and (eq last-command this-command)
4017 (eq org-cycle-subtree-status 'children))
4018 ;; We just showed the children, now show everything.
4019 (org-show-subtree)
4020 (message "SUBTREE")
4021 (setq org-cycle-subtree-status 'subtree)
4022 (run-hook-with-args 'org-cycle-hook 'subtree))
4024 ;; Default action: hide the subtree.
4025 (hide-subtree)
4026 (message "FOLDED")
4027 (setq org-cycle-subtree-status 'folded)
4028 (run-hook-with-args 'org-cycle-hook 'folded)))))
4030 ;; TAB emulation and template completion
4031 (buffer-read-only (org-back-to-heading))
4033 ((org-try-structure-completion))
4035 ((org-try-cdlatex-tab))
4037 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
4038 (or (not (bolp))
4039 (not (looking-at outline-regexp))))
4040 (call-interactively (global-key-binding "\t")))
4042 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
4043 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
4044 (or (and (eq org-cycle-emulate-tab 'white)
4045 (= (match-end 0) (point-at-eol)))
4046 (and (eq org-cycle-emulate-tab 'whitestart)
4047 (>= (match-end 0) pos))))
4049 (eq org-cycle-emulate-tab t))
4050 (call-interactively (global-key-binding "\t")))
4052 (t (save-excursion
4053 (org-back-to-heading)
4054 (org-cycle))))))
4056 ;;;###autoload
4057 (defun org-global-cycle (&optional arg)
4058 "Cycle the global visibility. For details see `org-cycle'.
4059 With C-u prefix arg, switch to startup visibility.
4060 With a numeric prefix, show all headlines up to that level."
4061 (interactive "P")
4062 (let ((org-cycle-include-plain-lists
4063 (if (org-mode-p) org-cycle-include-plain-lists nil)))
4064 (cond
4065 ((integerp arg)
4066 (show-all)
4067 (hide-sublevels arg)
4068 (setq org-cycle-global-status 'contents))
4069 ((equal arg '(4))
4070 (org-set-startup-visibility)
4071 (message "Startup visibility, plus VISIBILITY properties."))
4073 (org-cycle '(4))))))
4075 (defun org-set-startup-visibility ()
4076 "Set the visibility required by startup options and properties."
4077 (cond
4078 ((eq org-startup-folded t)
4079 (org-cycle '(4)))
4080 ((eq org-startup-folded 'content)
4081 (let ((this-command 'org-cycle) (last-command 'org-cycle))
4082 (org-cycle '(4)) (org-cycle '(4)))))
4083 (org-set-visibility-according-to-property 'no-cleanup)
4084 (org-cycle-hide-archived-subtrees 'all)
4085 (org-cycle-hide-drawers 'all)
4086 (org-cycle-show-empty-lines 'all))
4088 (defun org-set-visibility-according-to-property (&optional no-cleanup)
4089 "Switch subtree visibilities according to :VISIBILITY: property."
4090 (interactive)
4091 (let (state)
4092 (save-excursion
4093 (goto-char (point-min))
4094 (while (re-search-forward
4095 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
4096 nil t)
4097 (setq state (match-string 1))
4098 (save-excursion
4099 (org-back-to-heading t)
4100 (hide-subtree)
4101 (org-reveal)
4102 (cond
4103 ((equal state '("fold" "folded"))
4104 (hide-subtree))
4105 ((equal state "children")
4106 (org-show-hidden-entry)
4107 (show-children))
4108 ((equal state "content")
4109 (save-excursion
4110 (save-restriction
4111 (org-narrow-to-subtree)
4112 (org-content))))
4113 ((member state '("all" "showall"))
4114 (show-subtree)))))
4115 (unless no-cleanup
4116 (org-cycle-hide-archived-subtrees 'all)
4117 (org-cycle-hide-drawers 'all)
4118 (org-cycle-show-empty-lines 'all)))))
4120 (defun org-overview ()
4121 "Switch to overview mode, shoing only top-level headlines.
4122 Really, this shows all headlines with level equal or greater than the level
4123 of the first headline in the buffer. This is important, because if the
4124 first headline is not level one, then (hide-sublevels 1) gives confusing
4125 results."
4126 (interactive)
4127 (let ((level (save-excursion
4128 (goto-char (point-min))
4129 (if (re-search-forward (concat "^" outline-regexp) nil t)
4130 (progn
4131 (goto-char (match-beginning 0))
4132 (funcall outline-level))))))
4133 (and level (hide-sublevels level))))
4135 (defun org-content (&optional arg)
4136 "Show all headlines in the buffer, like a table of contents.
4137 With numerical argument N, show content up to level N."
4138 (interactive "P")
4139 (save-excursion
4140 ;; Visit all headings and show their offspring
4141 (and (integerp arg) (org-overview))
4142 (goto-char (point-max))
4143 (catch 'exit
4144 (while (and (progn (condition-case nil
4145 (outline-previous-visible-heading 1)
4146 (error (goto-char (point-min))))
4148 (looking-at outline-regexp))
4149 (if (integerp arg)
4150 (show-children (1- arg))
4151 (show-branches))
4152 (if (bobp) (throw 'exit nil))))))
4155 (defun org-optimize-window-after-visibility-change (state)
4156 "Adjust the window after a change in outline visibility.
4157 This function is the default value of the hook `org-cycle-hook'."
4158 (when (get-buffer-window (current-buffer))
4159 (cond
4160 ; ((eq state 'overview) (org-first-headline-recenter 1))
4161 ; ((eq state 'overview) (org-beginning-of-line))
4162 ((eq state 'content) nil)
4163 ((eq state 'all) nil)
4164 ((eq state 'folded) nil)
4165 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
4166 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
4168 (defun org-compact-display-after-subtree-move ()
4169 (let (beg end)
4170 (save-excursion
4171 (if (org-up-heading-safe)
4172 (progn
4173 (hide-subtree)
4174 (show-entry)
4175 (show-children)
4176 (org-cycle-show-empty-lines 'children)
4177 (org-cycle-hide-drawers 'children))
4178 (org-overview)))))
4180 (defun org-cycle-show-empty-lines (state)
4181 "Show empty lines above all visible headlines.
4182 The region to be covered depends on STATE when called through
4183 `org-cycle-hook'. Lisp program can use t for STATE to get the
4184 entire buffer covered. Note that an empty line is only shown if there
4185 are at least `org-cycle-separator-lines' empty lines before the headeline."
4186 (when (> org-cycle-separator-lines 0)
4187 (save-excursion
4188 (let* ((n org-cycle-separator-lines)
4189 (re (cond
4190 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
4191 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
4192 (t (let ((ns (number-to-string (- n 2))))
4193 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
4194 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
4195 beg end)
4196 (cond
4197 ((memq state '(overview contents t))
4198 (setq beg (point-min) end (point-max)))
4199 ((memq state '(children folded))
4200 (setq beg (point) end (progn (org-end-of-subtree t t)
4201 (beginning-of-line 2)
4202 (point)))))
4203 (when beg
4204 (goto-char beg)
4205 (while (re-search-forward re end t)
4206 (if (not (get-char-property (match-end 1) 'invisible))
4207 (outline-flag-region
4208 (match-beginning 1) (match-end 1) nil)))))))
4209 ;; Never hide empty lines at the end of the file.
4210 (save-excursion
4211 (goto-char (point-max))
4212 (outline-previous-heading)
4213 (outline-end-of-heading)
4214 (if (and (looking-at "[ \t\n]+")
4215 (= (match-end 0) (point-max)))
4216 (outline-flag-region (point) (match-end 0) nil))))
4218 (defun org-show-empty-lines-in-parent ()
4219 "Move to the parent and re-show empty lines before visible headlines."
4220 (save-excursion
4221 (let ((context (if (org-up-heading-safe) 'children 'overview)))
4222 (org-cycle-show-empty-lines context))))
4224 (defun org-cycle-hide-drawers (state)
4225 "Re-hide all drawers after a visibility state change."
4226 (when (and (org-mode-p)
4227 (not (memq state '(overview folded))))
4228 (save-excursion
4229 (let* ((globalp (memq state '(contents all)))
4230 (beg (if globalp (point-min) (point)))
4231 (end (if globalp (point-max) (org-end-of-subtree t))))
4232 (goto-char beg)
4233 (while (re-search-forward org-drawer-regexp end t)
4234 (org-flag-drawer t))))))
4236 (defun org-flag-drawer (flag)
4237 (save-excursion
4238 (beginning-of-line 1)
4239 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
4240 (let ((b (match-end 0))
4241 (outline-regexp org-outline-regexp))
4242 (if (re-search-forward
4243 "^[ \t]*:END:"
4244 (save-excursion (outline-next-heading) (point)) t)
4245 (outline-flag-region b (point-at-eol) flag)
4246 (error ":END: line missing"))))))
4248 (defun org-subtree-end-visible-p ()
4249 "Is the end of the current subtree visible?"
4250 (pos-visible-in-window-p
4251 (save-excursion (org-end-of-subtree t) (point))))
4253 (defun org-first-headline-recenter (&optional N)
4254 "Move cursor to the first headline and recenter the headline.
4255 Optional argument N means, put the headline into the Nth line of the window."
4256 (goto-char (point-min))
4257 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
4258 (beginning-of-line)
4259 (recenter (prefix-numeric-value N))))
4261 ;;; Org-goto
4263 (defvar org-goto-window-configuration nil)
4264 (defvar org-goto-marker nil)
4265 (defvar org-goto-map
4266 (let ((map (make-sparse-keymap)))
4267 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
4268 (while (setq cmd (pop cmds))
4269 (substitute-key-definition cmd cmd map global-map)))
4270 (suppress-keymap map)
4271 (org-defkey map "\C-m" 'org-goto-ret)
4272 (org-defkey map [(return)] 'org-goto-ret)
4273 (org-defkey map [(left)] 'org-goto-left)
4274 (org-defkey map [(right)] 'org-goto-right)
4275 (org-defkey map [(control ?g)] 'org-goto-quit)
4276 (org-defkey map "\C-i" 'org-cycle)
4277 (org-defkey map [(tab)] 'org-cycle)
4278 (org-defkey map [(down)] 'outline-next-visible-heading)
4279 (org-defkey map [(up)] 'outline-previous-visible-heading)
4280 (if org-goto-auto-isearch
4281 (if (fboundp 'define-key-after)
4282 (define-key-after map [t] 'org-goto-local-auto-isearch)
4283 nil)
4284 (org-defkey map "q" 'org-goto-quit)
4285 (org-defkey map "n" 'outline-next-visible-heading)
4286 (org-defkey map "p" 'outline-previous-visible-heading)
4287 (org-defkey map "f" 'outline-forward-same-level)
4288 (org-defkey map "b" 'outline-backward-same-level)
4289 (org-defkey map "u" 'outline-up-heading))
4290 (org-defkey map "/" 'org-occur)
4291 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
4292 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
4293 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
4294 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
4295 (org-defkey map "\C-c\C-u" 'outline-up-heading)
4296 map))
4298 (defconst org-goto-help
4299 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
4300 RET=jump to location [Q]uit and return to previous location
4301 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
4303 (defvar org-goto-start-pos) ; dynamically scoped parameter
4305 ;; FIXME: Docstring doe not mention both interfaces
4306 (defun org-goto (&optional alternative-interface)
4307 "Look up a different location in the current file, keeping current visibility.
4309 When you want look-up or go to a different location in a document, the
4310 fastest way is often to fold the entire buffer and then dive into the tree.
4311 This method has the disadvantage, that the previous location will be folded,
4312 which may not be what you want.
4314 This command works around this by showing a copy of the current buffer
4315 in an indirect buffer, in overview mode. You can dive into the tree in
4316 that copy, use org-occur and incremental search to find a location.
4317 When pressing RET or `Q', the command returns to the original buffer in
4318 which the visibility is still unchanged. After RET is will also jump to
4319 the location selected in the indirect buffer and expose the
4320 the headline hierarchy above."
4321 (interactive "P")
4322 (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
4323 (org-refile-use-outline-path t)
4324 (interface
4325 (if (not alternative-interface)
4326 org-goto-interface
4327 (if (eq org-goto-interface 'outline)
4328 'outline-path-completion
4329 'outline)))
4330 (org-goto-start-pos (point))
4331 (selected-point
4332 (if (eq interface 'outline)
4333 (car (org-get-location (current-buffer) org-goto-help))
4334 (nth 3 (org-refile-get-location "Goto: ")))))
4335 (if selected-point
4336 (progn
4337 (org-mark-ring-push org-goto-start-pos)
4338 (goto-char selected-point)
4339 (if (or (org-invisible-p) (org-invisible-p2))
4340 (org-show-context 'org-goto)))
4341 (message "Quit"))))
4343 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
4344 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
4345 (defvar org-goto-local-auto-isearch-map) ; defined below
4347 (defun org-get-location (buf help)
4348 "Let the user select a location in the Org-mode buffer BUF.
4349 This function uses a recursive edit. It returns the selected position
4350 or nil."
4351 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
4352 (isearch-hide-immediately nil)
4353 (isearch-search-fun-function
4354 (lambda () 'org-goto-local-search-headings))
4355 (org-goto-selected-point org-goto-exit-command))
4356 (save-excursion
4357 (save-window-excursion
4358 (delete-other-windows)
4359 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
4360 (switch-to-buffer
4361 (condition-case nil
4362 (make-indirect-buffer (current-buffer) "*org-goto*")
4363 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
4364 (with-output-to-temp-buffer "*Help*"
4365 (princ help))
4366 (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
4367 (setq buffer-read-only nil)
4368 (let ((org-startup-truncated t)
4369 (org-startup-folded nil)
4370 (org-startup-align-all-tables nil))
4371 (org-mode)
4372 (org-overview))
4373 (setq buffer-read-only t)
4374 (if (and (boundp 'org-goto-start-pos)
4375 (integer-or-marker-p org-goto-start-pos))
4376 (let ((org-show-hierarchy-above t)
4377 (org-show-siblings t)
4378 (org-show-following-heading t))
4379 (goto-char org-goto-start-pos)
4380 (and (org-invisible-p) (org-show-context)))
4381 (goto-char (point-min)))
4382 (org-beginning-of-line)
4383 (message "Select location and press RET")
4384 (use-local-map org-goto-map)
4385 (recursive-edit)
4387 (kill-buffer "*org-goto*")
4388 (cons org-goto-selected-point org-goto-exit-command)))
4390 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
4391 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
4392 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
4393 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
4395 (defun org-goto-local-search-headings (string bound noerror)
4396 "Search and make sure that anyq matches are in headlines."
4397 (catch 'return
4398 (while (if isearch-forward
4399 (search-forward string bound noerror)
4400 (search-backward string bound noerror))
4401 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
4402 (and (member :headline context)
4403 (not (member :tags context))))
4404 (throw 'return (point))))))
4406 (defun org-goto-local-auto-isearch ()
4407 "Start isearch."
4408 (interactive)
4409 (goto-char (point-min))
4410 (let ((keys (this-command-keys)))
4411 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
4412 (isearch-mode t)
4413 (isearch-process-search-char (string-to-char keys)))))
4415 (defun org-goto-ret (&optional arg)
4416 "Finish `org-goto' by going to the new location."
4417 (interactive "P")
4418 (setq org-goto-selected-point (point)
4419 org-goto-exit-command 'return)
4420 (throw 'exit nil))
4422 (defun org-goto-left ()
4423 "Finish `org-goto' by going to the new location."
4424 (interactive)
4425 (if (org-on-heading-p)
4426 (progn
4427 (beginning-of-line 1)
4428 (setq org-goto-selected-point (point)
4429 org-goto-exit-command 'left)
4430 (throw 'exit nil))
4431 (error "Not on a heading")))
4433 (defun org-goto-right ()
4434 "Finish `org-goto' by going to the new location."
4435 (interactive)
4436 (if (org-on-heading-p)
4437 (progn
4438 (setq org-goto-selected-point (point)
4439 org-goto-exit-command 'right)
4440 (throw 'exit nil))
4441 (error "Not on a heading")))
4443 (defun org-goto-quit ()
4444 "Finish `org-goto' without cursor motion."
4445 (interactive)
4446 (setq org-goto-selected-point nil)
4447 (setq org-goto-exit-command 'quit)
4448 (throw 'exit nil))
4450 ;;; Indirect buffer display of subtrees
4452 (defvar org-indirect-dedicated-frame nil
4453 "This is the frame being used for indirect tree display.")
4454 (defvar org-last-indirect-buffer nil)
4456 (defun org-tree-to-indirect-buffer (&optional arg)
4457 "Create indirect buffer and narrow it to current subtree.
4458 With numerical prefix ARG, go up to this level and then take that tree.
4459 If ARG is negative, go up that many levels.
4460 If `org-indirect-buffer-display' is not `new-frame', the command removes the
4461 indirect buffer previously made with this command, to avoid proliferation of
4462 indirect buffers. However, when you call the command with a `C-u' prefix, or
4463 when `org-indirect-buffer-display' is `new-frame', the last buffer
4464 is kept so that you can work with several indirect buffers at the same time.
4465 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
4466 requests that a new frame be made for the new buffer, so that the dedicated
4467 frame is not changed."
4468 (interactive "P")
4469 (let ((cbuf (current-buffer))
4470 (cwin (selected-window))
4471 (pos (point))
4472 beg end level heading ibuf)
4473 (save-excursion
4474 (org-back-to-heading t)
4475 (when (numberp arg)
4476 (setq level (org-outline-level))
4477 (if (< arg 0) (setq arg (+ level arg)))
4478 (while (> (setq level (org-outline-level)) arg)
4479 (outline-up-heading 1 t)))
4480 (setq beg (point)
4481 heading (org-get-heading))
4482 (org-end-of-subtree t) (setq end (point)))
4483 (if (and (buffer-live-p org-last-indirect-buffer)
4484 (not (eq org-indirect-buffer-display 'new-frame))
4485 (not arg))
4486 (kill-buffer org-last-indirect-buffer))
4487 (setq ibuf (org-get-indirect-buffer cbuf)
4488 org-last-indirect-buffer ibuf)
4489 (cond
4490 ((or (eq org-indirect-buffer-display 'new-frame)
4491 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
4492 (select-frame (make-frame))
4493 (delete-other-windows)
4494 (switch-to-buffer ibuf)
4495 (org-set-frame-title heading))
4496 ((eq org-indirect-buffer-display 'dedicated-frame)
4497 (raise-frame
4498 (select-frame (or (and org-indirect-dedicated-frame
4499 (frame-live-p org-indirect-dedicated-frame)
4500 org-indirect-dedicated-frame)
4501 (setq org-indirect-dedicated-frame (make-frame)))))
4502 (delete-other-windows)
4503 (switch-to-buffer ibuf)
4504 (org-set-frame-title (concat "Indirect: " heading)))
4505 ((eq org-indirect-buffer-display 'current-window)
4506 (switch-to-buffer ibuf))
4507 ((eq org-indirect-buffer-display 'other-window)
4508 (pop-to-buffer ibuf))
4509 (t (error "Invalid value.")))
4510 (if (featurep 'xemacs)
4511 (save-excursion (org-mode) (turn-on-font-lock)))
4512 (narrow-to-region beg end)
4513 (show-all)
4514 (goto-char pos)
4515 (and (window-live-p cwin) (select-window cwin))))
4517 (defun org-get-indirect-buffer (&optional buffer)
4518 (setq buffer (or buffer (current-buffer)))
4519 (let ((n 1) (base (buffer-name buffer)) bname)
4520 (while (buffer-live-p
4521 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
4522 (setq n (1+ n)))
4523 (condition-case nil
4524 (make-indirect-buffer buffer bname 'clone)
4525 (error (make-indirect-buffer buffer bname)))))
4527 (defun org-set-frame-title (title)
4528 "Set the title of the current frame to the string TITLE."
4529 ;; FIXME: how to name a single frame in XEmacs???
4530 (unless (featurep 'xemacs)
4531 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
4533 ;;;; Structure editing
4535 ;;; Inserting headlines
4537 (defun org-insert-heading (&optional force-heading)
4538 "Insert a new heading or item with same depth at point.
4539 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
4540 If point is at the beginning of a headline, insert a sibling before the
4541 current headline. If point is not at the beginning, do not split the line,
4542 but create the new hedline after the current line."
4543 (interactive "P")
4544 (if (= (buffer-size) 0)
4545 (insert "\n* ")
4546 (when (or force-heading (not (org-insert-item)))
4547 (let* ((head (save-excursion
4548 (condition-case nil
4549 (progn
4550 (org-back-to-heading)
4551 (match-string 0))
4552 (error "*"))))
4553 (blank (cdr (assq 'heading org-blank-before-new-entry)))
4554 pos)
4555 (cond
4556 ((and (org-on-heading-p) (bolp)
4557 (or (bobp)
4558 (save-excursion (backward-char 1) (not (org-invisible-p)))))
4559 ;; insert before the current line
4560 (open-line (if blank 2 1)))
4561 ((and (bolp)
4562 (or (bobp)
4563 (save-excursion
4564 (backward-char 1) (not (org-invisible-p)))))
4565 ;; insert right here
4566 nil)
4568 ;; in the middle of the line
4569 (org-show-entry)
4570 (let ((split
4571 (org-get-alist-option org-M-RET-may-split-line 'headline))
4572 tags pos)
4573 (if (org-on-heading-p)
4574 (progn
4575 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4576 (setq tags (and (match-end 2) (match-string 2)))
4577 (and (match-end 1)
4578 (delete-region (match-beginning 1) (match-end 1)))
4579 (setq pos (point-at-bol))
4580 (or split (end-of-line 1))
4581 (delete-horizontal-space)
4582 (newline (if blank 2 1))
4583 (when tags
4584 (save-excursion
4585 (goto-char pos)
4586 (end-of-line 1)
4587 (insert " " tags)
4588 (org-set-tags nil 'align))))
4589 (or split (end-of-line 1))
4590 (newline (if blank 2 1))))))
4591 (insert head) (just-one-space)
4592 (setq pos (point))
4593 (end-of-line 1)
4594 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
4595 (run-hooks 'org-insert-heading-hook)))))
4597 (defun org-get-heading (&optional no-tags)
4598 "Return the heading of the current entry, without the stars."
4599 (save-excursion
4600 (org-back-to-heading t)
4601 (if (looking-at
4602 (if no-tags
4603 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
4604 "\\*+[ \t]+\\([^\r\n]*\\)"))
4605 (match-string 1) "")))
4607 (defun org-insert-heading-after-current ()
4608 "Insert a new heading with same level as current, after current subtree."
4609 (interactive)
4610 (org-back-to-heading)
4611 (org-insert-heading)
4612 (org-move-subtree-down)
4613 (end-of-line 1))
4615 (defun org-insert-todo-heading (arg)
4616 "Insert a new heading with the same level and TODO state as current heading.
4617 If the heading has no TODO state, or if the state is DONE, use the first
4618 state (TODO by default). Also with prefix arg, force first state."
4619 (interactive "P")
4620 (when (not (org-insert-item 'checkbox))
4621 (org-insert-heading)
4622 (save-excursion
4623 (org-back-to-heading)
4624 (outline-previous-heading)
4625 (looking-at org-todo-line-regexp))
4626 (if (or arg
4627 (not (match-beginning 2))
4628 (member (match-string 2) org-done-keywords))
4629 (insert (car org-todo-keywords-1) " ")
4630 (insert (match-string 2) " "))
4631 (when org-provide-todo-statistics
4632 (org-update-parent-todo-statistics))))
4634 (defun org-insert-subheading (arg)
4635 "Insert a new subheading and demote it.
4636 Works for outline headings and for plain lists alike."
4637 (interactive "P")
4638 (org-insert-heading arg)
4639 (cond
4640 ((org-on-heading-p) (org-do-demote))
4641 ((org-at-item-p) (org-indent-item 1))))
4643 (defun org-insert-todo-subheading (arg)
4644 "Insert a new subheading with TODO keyword or checkbox and demote it.
4645 Works for outline headings and for plain lists alike."
4646 (interactive "P")
4647 (org-insert-todo-heading arg)
4648 (cond
4649 ((org-on-heading-p) (org-do-demote))
4650 ((org-at-item-p) (org-indent-item 1))))
4652 ;;; Promotion and Demotion
4654 (defun org-promote-subtree ()
4655 "Promote the entire subtree.
4656 See also `org-promote'."
4657 (interactive)
4658 (save-excursion
4659 (org-map-tree 'org-promote))
4660 (org-fix-position-after-promote))
4662 (defun org-demote-subtree ()
4663 "Demote the entire subtree. See `org-demote'.
4664 See also `org-promote'."
4665 (interactive)
4666 (save-excursion
4667 (org-map-tree 'org-demote))
4668 (org-fix-position-after-promote))
4671 (defun org-do-promote ()
4672 "Promote the current heading higher up the tree.
4673 If the region is active in `transient-mark-mode', promote all headings
4674 in the region."
4675 (interactive)
4676 (save-excursion
4677 (if (org-region-active-p)
4678 (org-map-region 'org-promote (region-beginning) (region-end))
4679 (org-promote)))
4680 (org-fix-position-after-promote))
4682 (defun org-do-demote ()
4683 "Demote the current heading lower down the tree.
4684 If the region is active in `transient-mark-mode', demote all headings
4685 in the region."
4686 (interactive)
4687 (save-excursion
4688 (if (org-region-active-p)
4689 (org-map-region 'org-demote (region-beginning) (region-end))
4690 (org-demote)))
4691 (org-fix-position-after-promote))
4693 (defun org-fix-position-after-promote ()
4694 "Make sure that after pro/demotion cursor position is right."
4695 (let ((pos (point)))
4696 (when (save-excursion
4697 (beginning-of-line 1)
4698 (looking-at org-todo-line-regexp)
4699 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
4700 (cond ((eobp) (insert " "))
4701 ((eolp) (insert " "))
4702 ((equal (char-after) ?\ ) (forward-char 1))))))
4704 (defun org-reduced-level (l)
4705 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
4707 (defun org-get-valid-level (level &optional change)
4708 "Rectify a level change under the influence of `org-odd-levels-only'
4709 LEVEL is a current level, CHANGE is by how much the level should be
4710 modified. Even if CHANGE is nil, LEVEL may be returned modified because
4711 even level numbers will become the next higher odd number."
4712 (if org-odd-levels-only
4713 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
4714 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
4715 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
4716 (max 1 (+ level change))))
4718 (if (boundp 'define-obsolete-function-alias)
4719 (if (or (featurep 'xemacs) (< emacs-major-version 23))
4720 (define-obsolete-function-alias 'org-get-legal-level
4721 'org-get-valid-level)
4722 (define-obsolete-function-alias 'org-get-legal-level
4723 'org-get-valid-level "23.1")))
4725 (defun org-promote ()
4726 "Promote the current heading higher up the tree.
4727 If the region is active in `transient-mark-mode', promote all headings
4728 in the region."
4729 (org-back-to-heading t)
4730 (let* ((level (save-match-data (funcall outline-level)))
4731 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
4732 (diff (abs (- level (length up-head) -1))))
4733 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
4734 (replace-match up-head nil t)
4735 ;; Fixup tag positioning
4736 (and org-auto-align-tags (org-set-tags nil t))
4737 (if org-adapt-indentation (org-fixup-indentation (- diff)))))
4739 (defun org-demote ()
4740 "Demote the current heading lower down the tree.
4741 If the region is active in `transient-mark-mode', demote all headings
4742 in the region."
4743 (org-back-to-heading t)
4744 (let* ((level (save-match-data (funcall outline-level)))
4745 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
4746 (diff (abs (- level (length down-head) -1))))
4747 (replace-match down-head nil t)
4748 ;; Fixup tag positioning
4749 (and org-auto-align-tags (org-set-tags nil t))
4750 (if org-adapt-indentation (org-fixup-indentation diff))))
4752 (defun org-map-tree (fun)
4753 "Call FUN for every heading underneath the current one."
4754 (org-back-to-heading)
4755 (let ((level (funcall outline-level)))
4756 (save-excursion
4757 (funcall fun)
4758 (while (and (progn
4759 (outline-next-heading)
4760 (> (funcall outline-level) level))
4761 (not (eobp)))
4762 (funcall fun)))))
4764 (defun org-map-region (fun beg end)
4765 "Call FUN for every heading between BEG and END."
4766 (let ((org-ignore-region t))
4767 (save-excursion
4768 (setq end (copy-marker end))
4769 (goto-char beg)
4770 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
4771 (< (point) end))
4772 (funcall fun))
4773 (while (and (progn
4774 (outline-next-heading)
4775 (< (point) end))
4776 (not (eobp)))
4777 (funcall fun)))))
4779 (defun org-fixup-indentation (diff)
4780 "Change the indentation in the current entry by DIFF
4781 However, if any line in the current entry has no indentation, or if it
4782 would end up with no indentation after the change, nothing at all is done."
4783 (save-excursion
4784 (let ((end (save-excursion (outline-next-heading)
4785 (point-marker)))
4786 (prohibit (if (> diff 0)
4787 "^\\S-"
4788 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
4789 col)
4790 (unless (save-excursion (end-of-line 1)
4791 (re-search-forward prohibit end t))
4792 (while (and (< (point) end)
4793 (re-search-forward "^[ \t]+" end t))
4794 (goto-char (match-end 0))
4795 (setq col (current-column))
4796 (if (< diff 0) (replace-match ""))
4797 (indent-to (+ diff col))))
4798 (move-marker end nil))))
4800 (defun org-convert-to-odd-levels ()
4801 "Convert an org-mode file with all levels allowed to one with odd levels.
4802 This will leave level 1 alone, convert level 2 to level 3, level 3 to
4803 level 5 etc."
4804 (interactive)
4805 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
4806 (let ((org-odd-levels-only nil) n)
4807 (save-excursion
4808 (goto-char (point-min))
4809 (while (re-search-forward "^\\*\\*+ " nil t)
4810 (setq n (- (length (match-string 0)) 2))
4811 (while (>= (setq n (1- n)) 0)
4812 (org-demote))
4813 (end-of-line 1))))))
4816 (defun org-convert-to-oddeven-levels ()
4817 "Convert an org-mode file with only odd levels to one with odd and even levels.
4818 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
4819 section with an even level, conversion would destroy the structure of the file. An error
4820 is signaled in this case."
4821 (interactive)
4822 (goto-char (point-min))
4823 ;; First check if there are no even levels
4824 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
4825 (org-show-context t)
4826 (error "Not all levels are odd in this file. Conversion not possible."))
4827 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
4828 (let ((org-odd-levels-only nil) n)
4829 (save-excursion
4830 (goto-char (point-min))
4831 (while (re-search-forward "^\\*\\*+ " nil t)
4832 (setq n (/ (1- (length (match-string 0))) 2))
4833 (while (>= (setq n (1- n)) 0)
4834 (org-promote))
4835 (end-of-line 1))))))
4837 (defun org-tr-level (n)
4838 "Make N odd if required."
4839 (if org-odd-levels-only (1+ (/ n 2)) n))
4841 ;;; Vertical tree motion, cutting and pasting of subtrees
4843 (defun org-move-subtree-up (&optional arg)
4844 "Move the current subtree up past ARG headlines of the same level."
4845 (interactive "p")
4846 (org-move-subtree-down (- (prefix-numeric-value arg))))
4848 (defun org-move-subtree-down (&optional arg)
4849 "Move the current subtree down past ARG headlines of the same level."
4850 (interactive "p")
4851 (setq arg (prefix-numeric-value arg))
4852 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
4853 'outline-get-last-sibling))
4854 (ins-point (make-marker))
4855 (cnt (abs arg))
4856 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
4857 ;; Select the tree
4858 (org-back-to-heading)
4859 (setq beg0 (point))
4860 (save-excursion
4861 (setq ne-beg (org-back-over-empty-lines))
4862 (setq beg (point)))
4863 (save-match-data
4864 (save-excursion (outline-end-of-heading)
4865 (setq folded (org-invisible-p)))
4866 (outline-end-of-subtree))
4867 (outline-next-heading)
4868 (setq ne-end (org-back-over-empty-lines))
4869 (setq end (point))
4870 (goto-char beg0)
4871 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
4872 ;; include less whitespace
4873 (save-excursion
4874 (goto-char beg)
4875 (forward-line (- ne-beg ne-end))
4876 (setq beg (point))))
4877 ;; Find insertion point, with error handling
4878 (while (> cnt 0)
4879 (or (and (funcall movfunc) (looking-at outline-regexp))
4880 (progn (goto-char beg0)
4881 (error "Cannot move past superior level or buffer limit")))
4882 (setq cnt (1- cnt)))
4883 (if (> arg 0)
4884 ;; Moving forward - still need to move over subtree
4885 (progn (org-end-of-subtree t t)
4886 (save-excursion
4887 (org-back-over-empty-lines)
4888 (or (bolp) (newline)))))
4889 (setq ne-ins (org-back-over-empty-lines))
4890 (move-marker ins-point (point))
4891 (setq txt (buffer-substring beg end))
4892 (org-save-markers-in-region beg end)
4893 (delete-region beg end)
4894 (outline-flag-region (1- beg) beg nil)
4895 (outline-flag-region (1- (point)) (point) nil)
4896 (let ((bbb (point)))
4897 (insert-before-markers txt)
4898 (org-reinstall-markers-in-region bbb)
4899 (move-marker ins-point bbb))
4900 (or (bolp) (insert "\n"))
4901 (setq ins-end (point))
4902 (goto-char ins-point)
4903 (org-skip-whitespace)
4904 (when (and (< arg 0)
4905 (org-first-sibling-p)
4906 (> ne-ins ne-beg))
4907 ;; Move whitespace back to beginning
4908 (save-excursion
4909 (goto-char ins-end)
4910 (let ((kill-whole-line t))
4911 (kill-line (- ne-ins ne-beg)) (point)))
4912 (insert (make-string (- ne-ins ne-beg) ?\n)))
4913 (move-marker ins-point nil)
4914 (org-compact-display-after-subtree-move)
4915 (org-show-empty-lines-in-parent)
4916 (unless folded
4917 (org-show-entry)
4918 (show-children)
4919 (org-cycle-hide-drawers 'children))))
4921 (defvar org-subtree-clip ""
4922 "Clipboard for cut and paste of subtrees.
4923 This is actually only a copy of the kill, because we use the normal kill
4924 ring. We need it to check if the kill was created by `org-copy-subtree'.")
4926 (defvar org-subtree-clip-folded nil
4927 "Was the last copied subtree folded?
4928 This is used to fold the tree back after pasting.")
4930 (defun org-cut-subtree (&optional n)
4931 "Cut the current subtree into the clipboard.
4932 With prefix arg N, cut this many sequential subtrees.
4933 This is a short-hand for marking the subtree and then cutting it."
4934 (interactive "p")
4935 (org-copy-subtree n 'cut))
4937 (defun org-copy-subtree (&optional n cut force-store-markers)
4938 "Cut the current subtree into the clipboard.
4939 With prefix arg N, cut this many sequential subtrees.
4940 This is a short-hand for marking the subtree and then copying it.
4941 If CUT is non-nil, actually cut the subtree.
4942 If FORCE-STORE-MARKERS is non-nil, store the relative locations
4943 of some markers in the region, even if CUT is non-nil. This is
4944 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
4945 (interactive "p")
4946 (let (beg end folded (beg0 (point)))
4947 (if (interactive-p)
4948 (org-back-to-heading nil) ; take what looks like a subtree
4949 (org-back-to-heading t)) ; take what is really there
4950 (org-back-over-empty-lines)
4951 (setq beg (point))
4952 (skip-chars-forward " \t\r\n")
4953 (save-match-data
4954 (save-excursion (outline-end-of-heading)
4955 (setq folded (org-invisible-p)))
4956 (condition-case nil
4957 (outline-forward-same-level (1- n))
4958 (error nil))
4959 (org-end-of-subtree t t))
4960 (org-back-over-empty-lines)
4961 (setq end (point))
4962 (goto-char beg0)
4963 (when (> end beg)
4964 (setq org-subtree-clip-folded folded)
4965 (when (or cut force-store-markers)
4966 (org-save-markers-in-region beg end))
4967 (if cut (kill-region beg end) (copy-region-as-kill beg end))
4968 (setq org-subtree-clip (current-kill 0))
4969 (message "%s: Subtree(s) with %d characters"
4970 (if cut "Cut" "Copied")
4971 (length org-subtree-clip)))))
4973 (defun org-paste-subtree (&optional level tree)
4974 "Paste the clipboard as a subtree, with modification of headline level.
4975 The entire subtree is promoted or demoted in order to match a new headline
4976 level. By default, the new level is derived from the visible headings
4977 before and after the insertion point, and taken to be the inferior headline
4978 level of the two. So if the previous visible heading is level 3 and the
4979 next is level 4 (or vice versa), level 4 will be used for insertion.
4980 This makes sure that the subtree remains an independent subtree and does
4981 not swallow low level entries.
4983 You can also force a different level, either by using a numeric prefix
4984 argument, or by inserting the heading marker by hand. For example, if the
4985 cursor is after \"*****\", then the tree will be shifted to level 5.
4987 If you want to insert the tree as is, just use \\[yank].
4989 If optional TREE is given, use this text instead of the kill ring."
4990 (interactive "P")
4991 (unless (org-kill-is-subtree-p tree)
4992 (error "%s"
4993 (substitute-command-keys
4994 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
4995 (let* ((visp (not (org-invisible-p)))
4996 (txt (or tree (and kill-ring (current-kill 0))))
4997 (^re (concat "^\\(" outline-regexp "\\)"))
4998 (re (concat "\\(" outline-regexp "\\)"))
4999 (^re_ (concat "\\(\\*+\\)[ \t]*"))
5001 (old-level (if (string-match ^re txt)
5002 (- (match-end 0) (match-beginning 0) 1)
5003 -1))
5004 (force-level (cond (level (prefix-numeric-value level))
5005 ((string-match
5006 ^re_ (buffer-substring (point-at-bol) (point)))
5007 (- (match-end 1) (match-beginning 1)))
5008 (t nil)))
5009 (previous-level (save-excursion
5010 (condition-case nil
5011 (progn
5012 (outline-previous-visible-heading 1)
5013 (if (looking-at re)
5014 (- (match-end 0) (match-beginning 0) 1)
5016 (error 1))))
5017 (next-level (save-excursion
5018 (condition-case nil
5019 (progn
5020 (or (looking-at outline-regexp)
5021 (outline-next-visible-heading 1))
5022 (if (looking-at re)
5023 (- (match-end 0) (match-beginning 0) 1)
5025 (error 1))))
5026 (new-level (or force-level (max previous-level next-level)))
5027 (shift (if (or (= old-level -1)
5028 (= new-level -1)
5029 (= old-level new-level))
5031 (- new-level old-level)))
5032 (delta (if (> shift 0) -1 1))
5033 (func (if (> shift 0) 'org-demote 'org-promote))
5034 (org-odd-levels-only nil)
5035 beg end)
5036 ;; Remove the forced level indicator
5037 (if force-level
5038 (delete-region (point-at-bol) (point)))
5039 ;; Paste
5040 (beginning-of-line 1)
5041 (org-back-over-empty-lines)
5042 (setq beg (point))
5043 (insert-before-markers txt)
5044 (unless (string-match "\n\\'" txt) (insert "\n"))
5045 (org-reinstall-markers-in-region beg)
5046 (setq end (point))
5047 (goto-char beg)
5048 (skip-chars-forward " \t\n\r")
5049 (setq beg (point))
5050 (if (and (org-invisible-p) visp)
5051 (save-excursion (outline-show-heading)))
5052 ;; Shift if necessary
5053 (unless (= shift 0)
5054 (save-restriction
5055 (narrow-to-region beg end)
5056 (while (not (= shift 0))
5057 (org-map-region func (point-min) (point-max))
5058 (setq shift (+ delta shift)))
5059 (goto-char (point-min))))
5060 (when (interactive-p)
5061 (message "Clipboard pasted as level %d subtree" new-level))
5062 (if (and kill-ring
5063 (eq org-subtree-clip (current-kill 0))
5064 org-subtree-clip-folded)
5065 ;; The tree was folded before it was killed/copied
5066 (hide-subtree))))
5068 (defun org-kill-is-subtree-p (&optional txt)
5069 "Check if the current kill is an outline subtree, or a set of trees.
5070 Returns nil if kill does not start with a headline, or if the first
5071 headline level is not the largest headline level in the tree.
5072 So this will actually accept several entries of equal levels as well,
5073 which is OK for `org-paste-subtree'.
5074 If optional TXT is given, check this string instead of the current kill."
5075 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
5076 (start-level (and kill
5077 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
5078 org-outline-regexp "\\)")
5079 kill)
5080 (- (match-end 2) (match-beginning 2) 1)))
5081 (re (concat "^" org-outline-regexp))
5082 (start (1+ (match-beginning 2))))
5083 (if (not start-level)
5084 (progn
5085 nil) ;; does not even start with a heading
5086 (catch 'exit
5087 (while (setq start (string-match re kill (1+ start)))
5088 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
5089 (throw 'exit nil)))
5090 t))))
5092 (defvar org-markers-to-move nil
5093 "Markers that should be moved with a cut-and-paste operation.
5094 Those markers are stored together with their positions relative to
5095 the start of the region.")
5097 (defun org-save-markers-in-region (beg end)
5098 "Check markers in region.
5099 If these markers are between BEG and END, record their position relative
5100 to BEG, so that after moving the block of text, we can put the markers back
5101 into place.
5102 This function gets called just before an entry or tree gets cut from the
5103 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
5104 called immediately, to move the markers with the entries."
5105 (setq org-markers-to-move nil)
5106 (when (featurep 'org-clock)
5107 (org-clock-save-markers-for-cut-and-paste beg end))
5108 (when (featurep 'org-agenda)
5109 (org-agenda-save-markers-for-cut-and-paste beg end)))
5111 (defun org-check-and-save-marker (marker beg end)
5112 "Check if MARKER is between BEG and END.
5113 If yes, remember the marker and the distance to BEG."
5114 (when (and (marker-buffer marker)
5115 (equal (marker-buffer marker) (current-buffer)))
5116 (if (and (>= marker beg) (< marker end))
5117 (push (cons marker (- marker beg)) org-markers-to-move))))
5119 (defun org-reinstall-markers-in-region (beg)
5120 "Move all remembered markers to their position relative to BEG."
5121 (mapc (lambda (x)
5122 (move-marker (car x) (+ beg (cdr x))))
5123 org-markers-to-move)
5124 (setq org-markers-to-move nil))
5126 (defun org-narrow-to-subtree ()
5127 "Narrow buffer to the current subtree."
5128 (interactive)
5129 (save-excursion
5130 (save-match-data
5131 (narrow-to-region
5132 (progn (org-back-to-heading) (point))
5133 (progn (org-end-of-subtree t) (point))))))
5136 ;;; Outline Sorting
5138 (defun org-sort (with-case)
5139 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
5140 Optional argument WITH-CASE means sort case-sensitively."
5141 (interactive "P")
5142 (if (org-at-table-p)
5143 (org-call-with-arg 'org-table-sort-lines with-case)
5144 (org-call-with-arg 'org-sort-entries-or-items with-case)))
5146 (defun org-sort-remove-invisible (s)
5147 (remove-text-properties 0 (length s) org-rm-props s)
5148 (while (string-match org-bracket-link-regexp s)
5149 (setq s (replace-match (if (match-end 2)
5150 (match-string 3 s)
5151 (match-string 1 s)) t t s)))
5154 (defvar org-priority-regexp) ; defined later in the file
5156 (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
5157 "Sort entries on a certain level of an outline tree.
5158 If there is an active region, the entries in the region are sorted.
5159 Else, if the cursor is before the first entry, sort the top-level items.
5160 Else, the children of the entry at point are sorted.
5162 Sorting can be alphabetically, numerically, and by date/time as given by
5163 the first time stamp in the entry. The command prompts for the sorting
5164 type unless it has been given to the function through the SORTING-TYPE
5165 argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
5166 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
5167 called with point at the beginning of the record. It must return either
5168 a string or a number that should serve as the sorting key for that record.
5170 Comparing entries ignores case by default. However, with an optional argument
5171 WITH-CASE, the sorting considers case as well."
5172 (interactive "P")
5173 (let ((case-func (if with-case 'identity 'downcase))
5174 start beg end stars re re2
5175 txt what tmp plain-list-p)
5176 ;; Find beginning and end of region to sort
5177 (cond
5178 ((org-region-active-p)
5179 ;; we will sort the region
5180 (setq end (region-end)
5181 what "region")
5182 (goto-char (region-beginning))
5183 (if (not (org-on-heading-p)) (outline-next-heading))
5184 (setq start (point)))
5185 ((org-at-item-p)
5186 ;; we will sort this plain list
5187 (org-beginning-of-item-list) (setq start (point))
5188 (org-end-of-item-list) (setq end (point))
5189 (goto-char start)
5190 (setq plain-list-p t
5191 what "plain list"))
5192 ((or (org-on-heading-p)
5193 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
5194 ;; we will sort the children of the current headline
5195 (org-back-to-heading)
5196 (setq start (point)
5197 end (progn (org-end-of-subtree t t)
5198 (org-back-over-empty-lines)
5199 (point))
5200 what "children")
5201 (goto-char start)
5202 (show-subtree)
5203 (outline-next-heading))
5205 ;; we will sort the top-level entries in this file
5206 (goto-char (point-min))
5207 (or (org-on-heading-p) (outline-next-heading))
5208 (setq start (point) end (point-max) what "top-level")
5209 (goto-char start)
5210 (show-all)))
5212 (setq beg (point))
5213 (if (>= beg end) (error "Nothing to sort"))
5215 (unless plain-list-p
5216 (looking-at "\\(\\*+\\)")
5217 (setq stars (match-string 1)
5218 re (concat "^" (regexp-quote stars) " +")
5219 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
5220 txt (buffer-substring beg end))
5221 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
5222 (if (and (not (equal stars "*")) (string-match re2 txt))
5223 (error "Region to sort contains a level above the first entry")))
5225 (unless sorting-type
5226 (message
5227 (if plain-list-p
5228 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
5229 "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
5230 what)
5231 (setq sorting-type (read-char-exclusive))
5233 (and (= (downcase sorting-type) ?f)
5234 (setq getkey-func
5235 (completing-read "Sort using function: "
5236 obarray 'fboundp t nil nil))
5237 (setq getkey-func (intern getkey-func)))
5239 (and (= (downcase sorting-type) ?r)
5240 (setq property
5241 (completing-read "Property: "
5242 (mapcar 'list (org-buffer-property-keys t))
5243 nil t))))
5245 (message "Sorting entries...")
5247 (save-restriction
5248 (narrow-to-region start end)
5250 (let ((dcst (downcase sorting-type))
5251 (now (current-time)))
5252 (sort-subr
5253 (/= dcst sorting-type)
5254 ;; This function moves to the beginning character of the "record" to
5255 ;; be sorted.
5256 (if plain-list-p
5257 (lambda nil
5258 (if (org-at-item-p) t (goto-char (point-max))))
5259 (lambda nil
5260 (if (re-search-forward re nil t)
5261 (goto-char (match-beginning 0))
5262 (goto-char (point-max)))))
5263 ;; This function moves to the last character of the "record" being
5264 ;; sorted.
5265 (if plain-list-p
5266 'org-end-of-item
5267 (lambda nil
5268 (save-match-data
5269 (condition-case nil
5270 (outline-forward-same-level 1)
5271 (error
5272 (goto-char (point-max)))))))
5274 ;; This function returns the value that gets sorted against.
5275 (if plain-list-p
5276 (lambda nil
5277 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
5278 (cond
5279 ((= dcst ?n)
5280 (string-to-number (buffer-substring (match-end 0)
5281 (point-at-eol))))
5282 ((= dcst ?a)
5283 (buffer-substring (match-end 0) (point-at-eol)))
5284 ((= dcst ?t)
5285 (if (re-search-forward org-ts-regexp
5286 (point-at-eol) t)
5287 (org-time-string-to-time (match-string 0))
5288 now))
5289 ((= dcst ?f)
5290 (if getkey-func
5291 (progn
5292 (setq tmp (funcall getkey-func))
5293 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
5294 tmp)
5295 (error "Invalid key function `%s'" getkey-func)))
5296 (t (error "Invalid sorting type `%c'" sorting-type)))))
5297 (lambda nil
5298 (cond
5299 ((= dcst ?n)
5300 (if (looking-at outline-regexp)
5301 (string-to-number (buffer-substring (match-end 0)
5302 (point-at-eol)))
5303 nil))
5304 ((= dcst ?a)
5305 (funcall case-func (buffer-substring (point-at-bol)
5306 (point-at-eol))))
5307 ((= dcst ?t)
5308 (if (re-search-forward org-ts-regexp
5309 (save-excursion
5310 (forward-line 2)
5311 (point)) t)
5312 (org-time-string-to-time (match-string 0))
5313 now))
5314 ((= dcst ?p)
5315 (if (re-search-forward org-priority-regexp (point-at-eol) t)
5316 (string-to-char (match-string 2))
5317 org-default-priority))
5318 ((= dcst ?r)
5319 (or (org-entry-get nil property) ""))
5320 ((= dcst ?o)
5321 (if (looking-at org-complex-heading-regexp)
5322 (- 9999 (length (member (match-string 2)
5323 org-todo-keywords-1)))))
5324 ((= dcst ?f)
5325 (if getkey-func
5326 (progn
5327 (setq tmp (funcall getkey-func))
5328 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
5329 tmp)
5330 (error "Invalid key function `%s'" getkey-func)))
5331 (t (error "Invalid sorting type `%c'" sorting-type)))))
5333 (cond
5334 ((= dcst ?a) 'string<)
5335 ((= dcst ?t) 'time-less-p)
5336 (t nil)))))
5337 (message "Sorting entries...done")))
5339 (defun org-do-sort (table what &optional with-case sorting-type)
5340 "Sort TABLE of WHAT according to SORTING-TYPE.
5341 The user will be prompted for the SORTING-TYPE if the call to this
5342 function does not specify it. WHAT is only for the prompt, to indicate
5343 what is being sorted. The sorting key will be extracted from
5344 the car of the elements of the table.
5345 If WITH-CASE is non-nil, the sorting will be case-sensitive."
5346 (unless sorting-type
5347 (message
5348 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
5349 what)
5350 (setq sorting-type (read-char-exclusive)))
5351 (let ((dcst (downcase sorting-type))
5352 extractfun comparefun)
5353 ;; Define the appropriate functions
5354 (cond
5355 ((= dcst ?n)
5356 (setq extractfun 'string-to-number
5357 comparefun (if (= dcst sorting-type) '< '>)))
5358 ((= dcst ?a)
5359 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
5360 (lambda(x) (downcase (org-sort-remove-invisible x))))
5361 comparefun (if (= dcst sorting-type)
5362 'string<
5363 (lambda (a b) (and (not (string< a b))
5364 (not (string= a b)))))))
5365 ((= dcst ?t)
5366 (setq extractfun
5367 (lambda (x)
5368 (if (string-match org-ts-regexp x)
5369 (time-to-seconds
5370 (org-time-string-to-time (match-string 0 x)))
5372 comparefun (if (= dcst sorting-type) '< '>)))
5373 (t (error "Invalid sorting type `%c'" sorting-type)))
5375 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
5376 table)
5377 (lambda (a b) (funcall comparefun (car a) (car b))))))
5379 ;;; Editing source examples
5381 (defvar org-exit-edit-mode-map (make-sparse-keymap))
5382 (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
5383 (defvar org-edit-src-force-single-line nil)
5384 (defvar org-edit-src-from-org-mode nil)
5386 (define-minor-mode org-exit-edit-mode
5387 "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
5389 (defun org-edit-src-code ()
5390 "Edit the source code example at point.
5391 An indirect buffer is created, and that buffer is then narrowed to the
5392 example at point and switched to the correct language mode. When done,
5393 exit by killing the buffer with \\[org-edit-src-exit]."
5394 (interactive)
5395 (let ((line (org-current-line))
5396 (case-fold-search t)
5397 (msg (substitute-command-keys
5398 "Edit, then exit with C-c ' (C-c and single quote)"))
5399 (info (org-edit-src-find-region-and-lang))
5400 (org-mode-p (eq major-mode 'org-mode))
5401 beg end lang lang-f single)
5402 (if (not info)
5404 (setq beg (nth 0 info)
5405 end (nth 1 info)
5406 lang (nth 2 info)
5407 single (nth 3 info)
5408 lang-f (intern (concat lang "-mode")))
5409 (unless (functionp lang-f)
5410 (error "No such language mode: %s" lang-f))
5411 (goto-line line)
5412 (if (get-buffer "*Org Edit Src Example*")
5413 (kill-buffer "*Org Edit Src Example*"))
5414 (switch-to-buffer (make-indirect-buffer (current-buffer)
5415 "*Org Edit Src Example*"))
5416 (narrow-to-region beg end)
5417 (remove-text-properties beg end '(display nil invisible nil
5418 intangible nil))
5419 (let ((org-inhibit-startup t))
5420 (funcall lang-f))
5421 (set (make-local-variable 'org-edit-src-force-single-line) single)
5422 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
5423 (when org-mode-p
5424 (goto-char (point-min))
5425 (while (re-search-forward "^," nil t)
5426 (replace-match "")))
5427 (goto-line line)
5428 (org-exit-edit-mode)
5429 (org-set-local 'header-line-format msg)
5430 (message "%s" msg)
5431 t)))
5433 (defun org-edit-src-find-region-and-lang ()
5434 "Find the region and language for a local edit.
5435 Return a list with beginning and end of the region, a string representing
5436 the language, a switch telling of the content should be in a single line."
5437 (let ((re-list
5439 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
5440 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
5441 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
5442 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
5443 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
5444 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
5445 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
5446 ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
5447 ("^#\\+begin_example.*\n" "^#\\+end_example" "fundamental")
5448 ("^#\\+html:" "\n" "html" single-line)
5449 ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
5450 ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
5451 ("^#\\+latex:" "\n" "latex" single-line)
5452 ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
5453 ("^#\\+ascii:" "\n" "ascii" single-line)
5455 (pos (point))
5456 re re1 re2 single beg end lang)
5457 (catch 'exit
5458 (while (setq entry (pop re-list))
5459 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
5460 single (nth 3 entry))
5461 (save-excursion
5462 (if (or (looking-at re1)
5463 (re-search-backward re1 nil t))
5464 (progn
5465 (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
5466 (if (and (re-search-forward re2 nil t)
5467 (>= (match-end 0) pos))
5468 (throw 'exit (list beg (match-beginning 0) lang single))))
5469 (if (or (looking-at re2)
5470 (re-search-forward re2 nil t))
5471 (progn
5472 (setq end (match-beginning 0))
5473 (if (and (re-search-backward re1 nil t)
5474 (<= (match-beginning 0) pos))
5475 (throw 'exit
5476 (list (match-end 0) end
5477 (org-edit-src-get-lang lang) single)))))))))))
5479 (defun org-edit-src-get-lang (lang)
5480 "Extract the src language."
5481 (let ((m (match-string 0)))
5482 (cond
5483 ((stringp lang) lang)
5484 ((integerp lang) (match-string lang))
5485 ((and (eq lang lang)
5486 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
5487 (match-string 1 m))
5488 ((and (eq lang lang)
5489 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
5490 (match-string 1 m))
5491 (t "fundamental"))))
5493 (defun org-edit-src-exit ()
5494 "Exit special edit and protect problematic lines."
5495 (interactive)
5496 (unless (buffer-base-buffer (current-buffer))
5497 (error "This is not an indirect buffer, something is wrong..."))
5498 (unless (> (point-min) 1)
5499 (error "This buffer is not narrowed, something is wrong..."))
5500 (goto-char (point-min))
5501 (if (looking-at "[ \t\n]*\n") (replace-match ""))
5502 (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
5503 (when (org-bound-and-true-p org-edit-src-force-single-line)
5504 (goto-char (point-min))
5505 (while (re-search-forward "\n" nil t)
5506 (replace-match " "))
5507 (goto-char (point-min))
5508 (if (looking-at "\\s-*") (replace-match " "))
5509 (if (re-search-forward "\\s-+\\'" nil t)
5510 (replace-match "")))
5511 (when (org-bound-and-true-p org-edit-src-from-org-mode)
5512 (goto-char (point-min))
5513 (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
5514 (replace-match ",\\1"))
5515 (when font-lock-mode
5516 (font-lock-unfontify-region (point-min) (point-max)))
5517 (put-text-property (point-min) (point-max) 'font-lock-fontified t))
5518 (kill-buffer (current-buffer))
5519 (and (org-mode-p) (org-restart-font-lock)))
5521 ;;;; Plain list items, including checkboxes
5523 ;;; Plain list items
5525 (defun org-at-item-p ()
5526 "Is point in a line starting a hand-formatted item?"
5527 (let ((llt org-plain-list-ordered-item-terminator))
5528 (save-excursion
5529 (goto-char (point-at-bol))
5530 (looking-at
5531 (cond
5532 ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5533 ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5534 ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5535 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
5537 (defun org-in-item-p ()
5538 "It the cursor inside a plain list item.
5539 Does not have to be the first line."
5540 (save-excursion
5541 (condition-case nil
5542 (progn
5543 (org-beginning-of-item)
5544 (org-at-item-p)
5546 (error nil))))
5548 (defun org-insert-item (&optional checkbox)
5549 "Insert a new item at the current level.
5550 Return t when things worked, nil when we are not in an item."
5551 (when (save-excursion
5552 (condition-case nil
5553 (progn
5554 (org-beginning-of-item)
5555 (org-at-item-p)
5556 (if (org-invisible-p) (error "Invisible item"))
5558 (error nil)))
5559 (let* ((bul (match-string 0))
5560 (descp (save-excursion (goto-char (match-beginning 0))
5561 (beginning-of-line 1)
5562 (save-match-data
5563 (looking-at "[ \t]*.*? ::"))))
5564 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
5565 (match-end 0)))
5566 (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
5567 pos)
5568 (if descp (setq checkbox nil))
5569 (cond
5570 ((and (org-at-item-p) (<= (point) eow))
5571 ;; before the bullet
5572 (beginning-of-line 1)
5573 (open-line (if blank 2 1)))
5574 ((<= (point) eow)
5575 (beginning-of-line 1))
5577 (unless (org-get-alist-option org-M-RET-may-split-line 'item)
5578 (end-of-line 1)
5579 (delete-horizontal-space))
5580 (newline (if blank 2 1))))
5581 (insert bul
5582 (if checkbox "[ ]" "")
5583 (if descp (concat (if checkbox " " "")
5584 (read-string "Term: ") " :: ") ""))
5585 (just-one-space)
5586 (setq pos (point))
5587 (end-of-line 1)
5588 (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
5589 (org-maybe-renumber-ordered-list)
5590 (and checkbox (org-update-checkbox-count-maybe))
5593 ;;; Checkboxes
5595 (defun org-at-item-checkbox-p ()
5596 "Is point at a line starting a plain-list item with a checklet?"
5597 (and (org-at-item-p)
5598 (save-excursion
5599 (goto-char (match-end 0))
5600 (skip-chars-forward " \t")
5601 (looking-at "\\[[- X]\\]"))))
5603 (defun org-toggle-checkbox (&optional arg)
5604 "Toggle the checkbox in the current line."
5605 (interactive "P")
5606 (catch 'exit
5607 (let (beg end status (firstnew 'unknown))
5608 (cond
5609 ((org-region-active-p)
5610 (setq beg (region-beginning) end (region-end)))
5611 ((org-on-heading-p)
5612 (setq beg (point) end (save-excursion (outline-next-heading) (point))))
5613 ((org-at-item-checkbox-p)
5614 (let ((pos (point)))
5615 (replace-match
5616 (cond (arg "[-]")
5617 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
5618 (t "[ ]"))
5619 t t)
5620 (goto-char pos))
5621 (throw 'exit t))
5622 (t (error "Not at a checkbox or heading, and no active region")))
5623 (save-excursion
5624 (goto-char beg)
5625 (while (< (point) end)
5626 (when (org-at-item-checkbox-p)
5627 (setq status (equal (match-string 0) "[X]"))
5628 (when (eq firstnew 'unknown)
5629 (setq firstnew (not status)))
5630 (replace-match
5631 (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
5632 (beginning-of-line 2)))))
5633 (org-update-checkbox-count-maybe))
5635 (defun org-update-checkbox-count-maybe ()
5636 "Update checkbox statistics unless turned off by user."
5637 (when org-provide-checkbox-statistics
5638 (org-update-checkbox-count)))
5640 (defun org-update-checkbox-count (&optional all)
5641 "Update the checkbox statistics in the current section.
5642 This will find all statistic cookies like [57%] and [6/12] and update them
5643 with the current numbers. With optional prefix argument ALL, do this for
5644 the whole buffer."
5645 (interactive "P")
5646 (save-excursion
5647 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
5648 (beg (condition-case nil
5649 (progn (outline-back-to-heading) (point))
5650 (error (point-min))))
5651 (end (move-marker (make-marker)
5652 (progn (outline-next-heading) (point))))
5653 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
5654 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
5655 (re-find (concat re "\\|" re-box))
5656 beg-cookie end-cookie is-percent c-on c-off lim
5657 eline curr-ind next-ind continue-from startsearch
5658 (cstat 0)
5660 (when all
5661 (goto-char (point-min))
5662 (outline-next-heading)
5663 (setq beg (point) end (point-max)))
5664 (goto-char end)
5665 ;; find each statistic cookie
5666 (while (re-search-backward re-find beg t)
5667 (setq beg-cookie (match-beginning 1)
5668 end-cookie (match-end 1)
5669 cstat (+ cstat (if end-cookie 1 0))
5670 startsearch (point-at-eol)
5671 continue-from (point-at-bol)
5672 is-percent (match-beginning 2)
5673 lim (cond
5674 ((org-on-heading-p) (outline-next-heading) (point))
5675 ((org-at-item-p) (org-end-of-item) (point))
5676 (t nil))
5677 c-on 0
5678 c-off 0)
5679 (when lim
5680 ;; find first checkbox for this cookie and gather
5681 ;; statistics from all that are at this indentation level
5682 (goto-char startsearch)
5683 (if (re-search-forward re-box lim t)
5684 (progn
5685 (org-beginning-of-item)
5686 (setq curr-ind (org-get-indentation))
5687 (setq next-ind curr-ind)
5688 (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
5689 (save-excursion (end-of-line) (setq eline (point)))
5690 (if (re-search-forward re-box eline t)
5691 (if (member (match-string 2) '("[ ]" "[-]"))
5692 (setq c-off (1+ c-off))
5693 (setq c-on (1+ c-on))
5696 (org-end-of-item)
5697 (setq next-ind (org-get-indentation))
5699 (goto-char continue-from)
5700 ;; update cookie
5701 (when end-cookie
5702 (delete-region beg-cookie end-cookie)
5703 (goto-char beg-cookie)
5704 (insert
5705 (if is-percent
5706 (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
5707 (format "[%d/%d]" c-on (+ c-on c-off)))))
5708 ;; update items checkbox if it has one
5709 (when (org-at-item-p)
5710 (org-beginning-of-item)
5711 (when (and (> (+ c-on c-off) 0)
5712 (re-search-forward re-box (point-at-eol) t))
5713 (setq beg-cookie (match-beginning 2)
5714 end-cookie (match-end 2))
5715 (delete-region beg-cookie end-cookie)
5716 (goto-char beg-cookie)
5717 (cond ((= c-off 0) (insert "[X]"))
5718 ((= c-on 0) (insert "[ ]"))
5719 (t (insert "[-]")))
5721 (goto-char continue-from))
5722 (when (interactive-p)
5723 (message "Checkbox satistics updated %s (%d places)"
5724 (if all "in entire file" "in current outline entry") cstat)))))
5726 (defun org-get-checkbox-statistics-face ()
5727 "Select the face for checkbox statistics.
5728 The face will be `org-done' when all relevant boxes are checked. Otherwise
5729 it will be `org-todo'."
5730 (if (match-end 1)
5731 (if (equal (match-string 1) "100%") 'org-done 'org-todo)
5732 (if (and (> (match-end 2) (match-beginning 2))
5733 (equal (match-string 2) (match-string 3)))
5734 'org-done
5735 'org-todo)))
5737 (defun org-get-indentation (&optional line)
5738 "Get the indentation of the current line, interpreting tabs.
5739 When LINE is given, assume it represents a line and compute its indentation."
5740 (if line
5741 (if (string-match "^ *" (org-remove-tabs line))
5742 (match-end 0))
5743 (save-excursion
5744 (beginning-of-line 1)
5745 (skip-chars-forward " \t")
5746 (current-column))))
5748 (defun org-remove-tabs (s &optional width)
5749 "Replace tabulators in S with spaces.
5750 Assumes that s is a single line, starting in column 0."
5751 (setq width (or width tab-width))
5752 (while (string-match "\t" s)
5753 (setq s (replace-match
5754 (make-string
5755 (- (* width (/ (+ (match-beginning 0) width) width))
5756 (match-beginning 0)) ?\ )
5757 t t s)))
5760 (defun org-fix-indentation (line ind)
5761 "Fix indentation in LINE.
5762 IND is a cons cell with target and minimum indentation.
5763 If the current indenation in LINE is smaller than the minimum,
5764 leave it alone. If it is larger than ind, set it to the target."
5765 (let* ((l (org-remove-tabs line))
5766 (i (org-get-indentation l))
5767 (i1 (car ind)) (i2 (cdr ind)))
5768 (if (>= i i2) (setq l (substring line i2)))
5769 (if (> i1 0)
5770 (concat (make-string i1 ?\ ) l)
5771 l)))
5773 (defun org-beginning-of-item ()
5774 "Go to the beginning of the current hand-formatted item.
5775 If the cursor is not in an item, throw an error."
5776 (interactive)
5777 (let ((pos (point))
5778 (limit (save-excursion
5779 (condition-case nil
5780 (progn
5781 (org-back-to-heading)
5782 (beginning-of-line 2) (point))
5783 (error (point-min)))))
5784 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
5785 ind ind1)
5786 (if (org-at-item-p)
5787 (beginning-of-line 1)
5788 (beginning-of-line 1)
5789 (skip-chars-forward " \t")
5790 (setq ind (current-column))
5791 (if (catch 'exit
5792 (while t
5793 (beginning-of-line 0)
5794 (if (or (bobp) (< (point) limit)) (throw 'exit nil))
5796 (if (looking-at "[ \t]*$")
5797 (setq ind1 ind-empty)
5798 (skip-chars-forward " \t")
5799 (setq ind1 (current-column)))
5800 (if (< ind1 ind)
5801 (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
5803 (goto-char pos)
5804 (error "Not in an item")))))
5806 (defun org-end-of-item ()
5807 "Go to the end of the current hand-formatted item.
5808 If the cursor is not in an item, throw an error."
5809 (interactive)
5810 (let* ((pos (point))
5811 ind1
5812 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
5813 (limit (save-excursion (outline-next-heading) (point)))
5814 (ind (save-excursion
5815 (org-beginning-of-item)
5816 (skip-chars-forward " \t")
5817 (current-column)))
5818 (end (catch 'exit
5819 (while t
5820 (beginning-of-line 2)
5821 (if (eobp) (throw 'exit (point)))
5822 (if (>= (point) limit) (throw 'exit (point-at-bol)))
5823 (if (looking-at "[ \t]*$")
5824 (setq ind1 ind-empty)
5825 (skip-chars-forward " \t")
5826 (setq ind1 (current-column)))
5827 (if (<= ind1 ind)
5828 (throw 'exit (point-at-bol)))))))
5829 (if end
5830 (goto-char end)
5831 (goto-char pos)
5832 (error "Not in an item"))))
5834 (defun org-next-item ()
5835 "Move to the beginning of the next item in the current plain list.
5836 Error if not at a plain list, or if this is the last item in the list."
5837 (interactive)
5838 (let (ind ind1 (pos (point)))
5839 (org-beginning-of-item)
5840 (setq ind (org-get-indentation))
5841 (org-end-of-item)
5842 (setq ind1 (org-get-indentation))
5843 (unless (and (org-at-item-p) (= ind ind1))
5844 (goto-char pos)
5845 (error "On last item"))))
5847 (defun org-previous-item ()
5848 "Move to the beginning of the previous item in the current plain list.
5849 Error if not at a plain list, or if this is the first item in the list."
5850 (interactive)
5851 (let (beg ind ind1 (pos (point)))
5852 (org-beginning-of-item)
5853 (setq beg (point))
5854 (setq ind (org-get-indentation))
5855 (goto-char beg)
5856 (catch 'exit
5857 (while t
5858 (beginning-of-line 0)
5859 (if (looking-at "[ \t]*$")
5861 (if (<= (setq ind1 (org-get-indentation)) ind)
5862 (throw 'exit t)))))
5863 (condition-case nil
5864 (if (or (not (org-at-item-p))
5865 (< ind1 (1- ind)))
5866 (error "")
5867 (org-beginning-of-item))
5868 (error (goto-char pos)
5869 (error "On first item")))))
5871 (defun org-first-list-item-p ()
5872 "Is this heading the item in a plain list?"
5873 (unless (org-at-item-p)
5874 (error "Not at a plain list item"))
5875 (org-beginning-of-item)
5876 (= (point) (save-excursion (org-beginning-of-item-list))))
5878 (defun org-move-item-down ()
5879 "Move the plain list item at point down, i.e. swap with following item.
5880 Subitems (items with larger indentation) are considered part of the item,
5881 so this really moves item trees."
5882 (interactive)
5883 (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
5884 (org-beginning-of-item)
5885 (setq beg0 (point))
5886 (save-excursion
5887 (setq ne-beg (org-back-over-empty-lines))
5888 (setq beg (point)))
5889 (goto-char beg0)
5890 (setq ind (org-get-indentation))
5891 (org-end-of-item)
5892 (setq end0 (point))
5893 (setq ind1 (org-get-indentation))
5894 (setq ne-end (org-back-over-empty-lines))
5895 (setq end (point))
5896 (goto-char beg0)
5897 (when (and (org-first-list-item-p) (< ne-end ne-beg))
5898 ;; include less whitespace
5899 (save-excursion
5900 (goto-char beg)
5901 (forward-line (- ne-beg ne-end))
5902 (setq beg (point))))
5903 (goto-char end0)
5904 (if (and (org-at-item-p) (= ind ind1))
5905 (progn
5906 (org-end-of-item)
5907 (org-back-over-empty-lines)
5908 (setq txt (buffer-substring beg end))
5909 (save-excursion
5910 (delete-region beg end))
5911 (setq pos (point))
5912 (insert txt)
5913 (goto-char pos) (org-skip-whitespace)
5914 (org-maybe-renumber-ordered-list))
5915 (goto-char pos)
5916 (error "Cannot move this item further down"))))
5918 (defun org-move-item-up (arg)
5919 "Move the plain list item at point up, i.e. swap with previous item.
5920 Subitems (items with larger indentation) are considered part of the item,
5921 so this really moves item trees."
5922 (interactive "p")
5923 (let (beg beg0 end ind ind1 (pos (point)) txt
5924 ne-beg ne-ins ins-end)
5925 (org-beginning-of-item)
5926 (setq beg0 (point))
5927 (setq ind (org-get-indentation))
5928 (save-excursion
5929 (setq ne-beg (org-back-over-empty-lines))
5930 (setq beg (point)))
5931 (goto-char beg0)
5932 (org-end-of-item)
5933 (org-back-over-empty-lines)
5934 (setq end (point))
5935 (goto-char beg0)
5936 (catch 'exit
5937 (while t
5938 (beginning-of-line 0)
5939 (if (looking-at "[ \t]*$")
5940 (if org-empty-line-terminates-plain-lists
5941 (progn
5942 (goto-char pos)
5943 (error "Cannot move this item further up"))
5944 nil)
5945 (if (<= (setq ind1 (org-get-indentation)) ind)
5946 (throw 'exit t)))))
5947 (condition-case nil
5948 (org-beginning-of-item)
5949 (error (goto-char beg0)
5950 (error "Cannot move this item further up")))
5951 (setq ind1 (org-get-indentation))
5952 (if (and (org-at-item-p) (= ind ind1))
5953 (progn
5954 (setq ne-ins (org-back-over-empty-lines))
5955 (setq txt (buffer-substring beg end))
5956 (save-excursion
5957 (delete-region beg end))
5958 (setq pos (point))
5959 (insert txt)
5960 (setq ins-end (point))
5961 (goto-char pos) (org-skip-whitespace)
5963 (when (and (org-first-list-item-p) (> ne-ins ne-beg))
5964 ;; Move whitespace back to beginning
5965 (save-excursion
5966 (goto-char ins-end)
5967 (let ((kill-whole-line t))
5968 (kill-line (- ne-ins ne-beg)) (point)))
5969 (insert (make-string (- ne-ins ne-beg) ?\n)))
5971 (org-maybe-renumber-ordered-list))
5972 (goto-char pos)
5973 (error "Cannot move this item further up"))))
5975 (defun org-maybe-renumber-ordered-list ()
5976 "Renumber the ordered list at point if setup allows it.
5977 This tests the user option `org-auto-renumber-ordered-lists' before
5978 doing the renumbering."
5979 (interactive)
5980 (when (and org-auto-renumber-ordered-lists
5981 (org-at-item-p))
5982 (if (match-beginning 3)
5983 (org-renumber-ordered-list 1)
5984 (org-fix-bullet-type))))
5986 (defun org-maybe-renumber-ordered-list-safe ()
5987 (condition-case nil
5988 (save-excursion
5989 (org-maybe-renumber-ordered-list))
5990 (error nil)))
5992 (defun org-cycle-list-bullet (&optional which)
5993 "Cycle through the different itemize/enumerate bullets.
5994 This cycle the entire list level through the sequence:
5996 `-' -> `+' -> `*' -> `1.' -> `1)'
5998 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
5999 0 meand `-', 1 means `+' etc."
6000 (interactive "P")
6001 (org-preserve-lc
6002 (org-beginning-of-item-list)
6003 (org-at-item-p)
6004 (beginning-of-line 1)
6005 (let ((current (match-string 0))
6006 (prevp (eq which 'previous))
6007 new)
6008 (setq new (cond
6009 ((and (numberp which)
6010 (nth (1- which) '("-" "+" "*" "1." "1)"))))
6011 ((string-match "-" current) (if prevp "1)" "+"))
6012 ((string-match "\\+" current)
6013 (if prevp "-" (if (looking-at "\\S-") "1." "*")))
6014 ((string-match "\\*" current) (if prevp "+" "1."))
6015 ((string-match "\\." current) (if prevp "*" "1)"))
6016 ((string-match ")" current) (if prevp "1." "-"))
6017 (t (error "This should not happen"))))
6018 (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
6019 (org-fix-bullet-type)
6020 (org-maybe-renumber-ordered-list))))
6022 (defun org-get-string-indentation (s)
6023 "What indentation has S due to SPACE and TAB at the beginning of the string?"
6024 (let ((n -1) (i 0) (w tab-width) c)
6025 (catch 'exit
6026 (while (< (setq n (1+ n)) (length s))
6027 (setq c (aref s n))
6028 (cond ((= c ?\ ) (setq i (1+ i)))
6029 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
6030 (t (throw 'exit t)))))
6033 (defun org-renumber-ordered-list (arg)
6034 "Renumber an ordered plain list.
6035 Cursor needs to be in the first line of an item, the line that starts
6036 with something like \"1.\" or \"2)\"."
6037 (interactive "p")
6038 (unless (and (org-at-item-p)
6039 (match-beginning 3))
6040 (error "This is not an ordered list"))
6041 (let ((line (org-current-line))
6042 (col (current-column))
6043 (ind (org-get-string-indentation
6044 (buffer-substring (point-at-bol) (match-beginning 3))))
6045 ;; (term (substring (match-string 3) -1))
6046 ind1 (n (1- arg))
6047 fmt bob)
6048 ;; find where this list begins
6049 (org-beginning-of-item-list)
6050 (setq bobp (bobp))
6051 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
6052 (setq fmt (concat "%d" (match-string 1)))
6053 (beginning-of-line 0)
6054 ;; walk forward and replace these numbers
6055 (catch 'exit
6056 (while t
6057 (catch 'next
6058 (if bobp (setq bobp nil) (beginning-of-line 2))
6059 (if (eobp) (throw 'exit nil))
6060 (if (looking-at "[ \t]*$") (throw 'next nil))
6061 (skip-chars-forward " \t") (setq ind1 (current-column))
6062 (if (> ind1 ind) (throw 'next t))
6063 (if (< ind1 ind) (throw 'exit t))
6064 (if (not (org-at-item-p)) (throw 'exit nil))
6065 (delete-region (match-beginning 2) (match-end 2))
6066 (goto-char (match-beginning 2))
6067 (insert (format fmt (setq n (1+ n)))))))
6068 (goto-line line)
6069 (org-move-to-column col)))
6071 (defun org-fix-bullet-type ()
6072 "Make sure all items in this list have the same bullet as the firsst item."
6073 (interactive)
6074 (unless (org-at-item-p) (error "This is not a list"))
6075 (let ((line (org-current-line))
6076 (col (current-column))
6077 (ind (current-indentation))
6078 ind1 bullet)
6079 ;; find where this list begins
6080 (org-beginning-of-item-list)
6081 (beginning-of-line 1)
6082 ;; find out what the bullet type is
6083 (looking-at "[ \t]*\\(\\S-+\\)")
6084 (setq bullet (match-string 1))
6085 ;; walk forward and replace these numbers
6086 (beginning-of-line 0)
6087 (catch 'exit
6088 (while t
6089 (catch 'next
6090 (beginning-of-line 2)
6091 (if (eobp) (throw 'exit nil))
6092 (if (looking-at "[ \t]*$") (throw 'next nil))
6093 (skip-chars-forward " \t") (setq ind1 (current-column))
6094 (if (> ind1 ind) (throw 'next t))
6095 (if (< ind1 ind) (throw 'exit t))
6096 (if (not (org-at-item-p)) (throw 'exit nil))
6097 (skip-chars-forward " \t")
6098 (looking-at "\\S-+")
6099 (replace-match bullet))))
6100 (goto-line line)
6101 (org-move-to-column col)
6102 (if (string-match "[0-9]" bullet)
6103 (org-renumber-ordered-list 1))))
6105 (defun org-beginning-of-item-list ()
6106 "Go to the beginning of the current item list.
6107 I.e. to the first item in this list."
6108 (interactive)
6109 (org-beginning-of-item)
6110 (let ((pos (point-at-bol))
6111 (ind (org-get-indentation))
6112 ind1)
6113 ;; find where this list begins
6114 (catch 'exit
6115 (while t
6116 (catch 'next
6117 (beginning-of-line 0)
6118 (if (looking-at "[ \t]*$")
6119 (throw (if (bobp) 'exit 'next) t))
6120 (skip-chars-forward " \t") (setq ind1 (current-column))
6121 (if (or (< ind1 ind)
6122 (and (= ind1 ind)
6123 (not (org-at-item-p)))
6124 (and (= (point-at-bol) (point-min))
6125 (setq pos (point-min))))
6126 (throw 'exit t)
6127 (when (org-at-item-p) (setq pos (point-at-bol)))))))
6128 (goto-char pos)))
6131 (defun org-end-of-item-list ()
6132 "Go to the end of the current item list.
6133 I.e. to the text after the last item."
6134 (interactive)
6135 (org-beginning-of-item)
6136 (let ((pos (point-at-bol))
6137 (ind (org-get-indentation))
6138 ind1)
6139 ;; find where this list begins
6140 (catch 'exit
6141 (while t
6142 (catch 'next
6143 (beginning-of-line 2)
6144 (if (looking-at "[ \t]*$")
6145 (throw (if (eobp) 'exit 'next) t))
6146 (skip-chars-forward " \t") (setq ind1 (current-column))
6147 (if (or (< ind1 ind)
6148 (and (= ind1 ind)
6149 (not (org-at-item-p)))
6150 (eobp))
6151 (progn
6152 (setq pos (point-at-bol))
6153 (throw 'exit t))))))
6154 (goto-char pos)))
6157 (defvar org-last-indent-begin-marker (make-marker))
6158 (defvar org-last-indent-end-marker (make-marker))
6160 (defun org-outdent-item (arg)
6161 "Outdent a local list item."
6162 (interactive "p")
6163 (org-indent-item (- arg)))
6165 (defun org-indent-item (arg)
6166 "Indent a local list item."
6167 (interactive "p")
6168 (unless (org-at-item-p)
6169 (error "Not on an item"))
6170 (save-excursion
6171 (let (beg end ind ind1 tmp delta ind-down ind-up)
6172 (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
6173 (setq beg org-last-indent-begin-marker
6174 end org-last-indent-end-marker)
6175 (org-beginning-of-item)
6176 (setq beg (move-marker org-last-indent-begin-marker (point)))
6177 (org-end-of-item)
6178 (setq end (move-marker org-last-indent-end-marker (point))))
6179 (goto-char beg)
6180 (setq tmp (org-item-indent-positions)
6181 ind (car tmp)
6182 ind-down (nth 2 tmp)
6183 ind-up (nth 1 tmp)
6184 delta (if (> arg 0)
6185 (if ind-down (- ind-down ind) 2)
6186 (if ind-up (- ind-up ind) -2)))
6187 (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
6188 (while (< (point) end)
6189 (beginning-of-line 1)
6190 (skip-chars-forward " \t") (setq ind1 (current-column))
6191 (delete-region (point-at-bol) (point))
6192 (or (eolp) (org-indent-to-column (+ ind1 delta)))
6193 (beginning-of-line 2))))
6194 (org-fix-bullet-type)
6195 (org-maybe-renumber-ordered-list-safe)
6196 (save-excursion
6197 (beginning-of-line 0)
6198 (condition-case nil (org-beginning-of-item) (error nil))
6199 (org-maybe-renumber-ordered-list-safe)))
6201 (defun org-item-indent-positions ()
6202 "Return indentation for plain list items.
6203 This returns a list with three values: The current indentation, the
6204 parent indentation and the indentation a child should habe.
6205 Assumes cursor in item line."
6206 (let* ((bolpos (point-at-bol))
6207 (ind (org-get-indentation))
6208 ind-down ind-up pos)
6209 (save-excursion
6210 (org-beginning-of-item-list)
6211 (skip-chars-backward "\n\r \t")
6212 (when (org-in-item-p)
6213 (org-beginning-of-item)
6214 (setq ind-up (org-get-indentation))))
6215 (setq pos (point))
6216 (save-excursion
6217 (cond
6218 ((and (condition-case nil (progn (org-previous-item) t)
6219 (error nil))
6220 (or (forward-char 1) t)
6221 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
6222 (setq ind-down (org-get-indentation)))
6223 ((and (goto-char pos)
6224 (org-at-item-p))
6225 (goto-char (match-end 0))
6226 (skip-chars-forward " \t")
6227 (setq ind-down (current-column)))))
6228 (list ind ind-up ind-down)))
6230 ;;; The orgstruct minor mode
6232 ;; Define a minor mode which can be used in other modes in order to
6233 ;; integrate the org-mode structure editing commands.
6235 ;; This is really a hack, because the org-mode structure commands use
6236 ;; keys which normally belong to the major mode. Here is how it
6237 ;; works: The minor mode defines all the keys necessary to operate the
6238 ;; structure commands, but wraps the commands into a function which
6239 ;; tests if the cursor is currently at a headline or a plain list
6240 ;; item. If that is the case, the structure command is used,
6241 ;; temporarily setting many Org-mode variables like regular
6242 ;; expressions for filling etc. However, when any of those keys is
6243 ;; used at a different location, function uses `key-binding' to look
6244 ;; up if the key has an associated command in another currently active
6245 ;; keymap (minor modes, major mode, global), and executes that
6246 ;; command. There might be problems if any of the keys is otherwise
6247 ;; used as a prefix key.
6249 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
6250 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
6251 ;; addresses this by checking explicitly for both bindings.
6253 (defvar orgstruct-mode-map (make-sparse-keymap)
6254 "Keymap for the minor `orgstruct-mode'.")
6256 (defvar org-local-vars nil
6257 "List of local variables, for use by `orgstruct-mode'")
6259 ;;;###autoload
6260 (define-minor-mode orgstruct-mode
6261 "Toggle the minor more `orgstruct-mode'.
6262 This mode is for using Org-mode structure commands in other modes.
6263 The following key behave as if Org-mode was active, if the cursor
6264 is on a headline, or on a plain list item (both in the definition
6265 of Org-mode).
6267 M-up Move entry/item up
6268 M-down Move entry/item down
6269 M-left Promote
6270 M-right Demote
6271 M-S-up Move entry/item up
6272 M-S-down Move entry/item down
6273 M-S-left Promote subtree
6274 M-S-right Demote subtree
6275 M-q Fill paragraph and items like in Org-mode
6276 C-c ^ Sort entries
6277 C-c - Cycle list bullet
6278 TAB Cycle item visibility
6279 M-RET Insert new heading/item
6280 S-M-RET Insert new TODO heading / Chekbox item
6281 C-c C-c Set tags / toggle checkbox"
6282 nil " OrgStruct" nil
6283 (org-load-modules-maybe)
6284 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
6286 ;;;###autoload
6287 (defun turn-on-orgstruct ()
6288 "Unconditionally turn on `orgstruct-mode'."
6289 (orgstruct-mode 1))
6291 ;;;###autoload
6292 (defun turn-on-orgstruct++ ()
6293 "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
6294 In addition to setting orgstruct-mode, this also exports all indentation and
6295 autofilling variables from org-mode into the buffer. Note that turning
6296 off orgstruct-mode will *not* remove these additional settings."
6297 (orgstruct-mode 1)
6298 (let (var val)
6299 (mapc
6300 (lambda (x)
6301 (when (string-match
6302 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6303 (symbol-name (car x)))
6304 (setq var (car x) val (nth 1 x))
6305 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
6306 org-local-vars)))
6308 (defun orgstruct-error ()
6309 "Error when there is no default binding for a structure key."
6310 (interactive)
6311 (error "This key has no function outside structure elements"))
6313 (defun orgstruct-setup ()
6314 "Setup orgstruct keymaps."
6315 (let ((nfunc 0)
6316 (bindings
6317 (list
6318 '([(meta up)] org-metaup)
6319 '([(meta down)] org-metadown)
6320 '([(meta left)] org-metaleft)
6321 '([(meta right)] org-metaright)
6322 '([(meta shift up)] org-shiftmetaup)
6323 '([(meta shift down)] org-shiftmetadown)
6324 '([(meta shift left)] org-shiftmetaleft)
6325 '([(meta shift right)] org-shiftmetaright)
6326 '([(shift up)] org-shiftup)
6327 '([(shift down)] org-shiftdown)
6328 '("\C-c\C-c" org-ctrl-c-ctrl-c)
6329 '("\M-q" fill-paragraph)
6330 '("\C-c^" org-sort)
6331 '("\C-c-" org-cycle-list-bullet)))
6332 elt key fun cmd)
6333 (while (setq elt (pop bindings))
6334 (setq nfunc (1+ nfunc))
6335 (setq key (org-key (car elt))
6336 fun (nth 1 elt)
6337 cmd (orgstruct-make-binding fun nfunc key))
6338 (org-defkey orgstruct-mode-map key cmd))
6340 ;; Special treatment needed for TAB and RET
6341 (org-defkey orgstruct-mode-map [(tab)]
6342 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
6343 (org-defkey orgstruct-mode-map "\C-i"
6344 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
6346 (org-defkey orgstruct-mode-map "\M-\C-m"
6347 (orgstruct-make-binding 'org-insert-heading 105
6348 "\M-\C-m" [(meta return)]))
6349 (org-defkey orgstruct-mode-map [(meta return)]
6350 (orgstruct-make-binding 'org-insert-heading 106
6351 [(meta return)] "\M-\C-m"))
6353 (org-defkey orgstruct-mode-map [(shift meta return)]
6354 (orgstruct-make-binding 'org-insert-todo-heading 107
6355 [(meta return)] "\M-\C-m"))
6357 (unless org-local-vars
6358 (setq org-local-vars (org-get-local-variables)))
6362 (defun orgstruct-make-binding (fun n &rest keys)
6363 "Create a function for binding in the structure minor mode.
6364 FUN is the command to call inside a table. N is used to create a unique
6365 command name. KEYS are keys that should be checked in for a command
6366 to execute outside of tables."
6367 (eval
6368 (list 'defun
6369 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
6370 '(arg)
6371 (concat "In Structure, run `" (symbol-name fun) "'.\n"
6372 "Outside of structure, run the binding of `"
6373 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
6374 "'.")
6375 '(interactive "p")
6376 (list 'if
6377 '(org-context-p 'headline 'item)
6378 (list 'org-run-like-in-org-mode (list 'quote fun))
6379 (list 'let '(orgstruct-mode)
6380 (list 'call-interactively
6381 (append '(or)
6382 (mapcar (lambda (k)
6383 (list 'key-binding k))
6384 keys)
6385 '('orgstruct-error))))))))
6387 (defun org-context-p (&rest contexts)
6388 "Check if local context is and of CONTEXTS.
6389 Possible values in the list of contexts are `table', `headline', and `item'."
6390 (let ((pos (point)))
6391 (goto-char (point-at-bol))
6392 (prog1 (or (and (memq 'table contexts)
6393 (looking-at "[ \t]*|"))
6394 (and (memq 'headline contexts)
6395 (looking-at "\\*+"))
6396 (and (memq 'item contexts)
6397 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
6398 (goto-char pos))))
6400 (defun org-get-local-variables ()
6401 "Return a list of all local variables in an org-mode buffer."
6402 (let (varlist)
6403 (with-current-buffer (get-buffer-create "*Org tmp*")
6404 (erase-buffer)
6405 (org-mode)
6406 (setq varlist (buffer-local-variables)))
6407 (kill-buffer "*Org tmp*")
6408 (delq nil
6409 (mapcar
6410 (lambda (x)
6411 (setq x
6412 (if (symbolp x)
6413 (list x)
6414 (list (car x) (list 'quote (cdr x)))))
6415 (if (string-match
6416 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6417 (symbol-name (car x)))
6418 x nil))
6419 varlist))))
6421 ;;;###autoload
6422 (defun org-run-like-in-org-mode (cmd)
6423 (org-load-modules-maybe)
6424 (unless org-local-vars
6425 (setq org-local-vars (org-get-local-variables)))
6426 (eval (list 'let org-local-vars
6427 (list 'call-interactively (list 'quote cmd)))))
6429 ;;;; Archiving
6431 (defun org-get-category (&optional pos)
6432 "Get the category applying to position POS."
6433 (get-text-property (or pos (point)) 'org-category))
6435 (defun org-refresh-category-properties ()
6436 "Refresh category text properties in the buffer."
6437 (let ((def-cat (cond
6438 ((null org-category)
6439 (if buffer-file-name
6440 (file-name-sans-extension
6441 (file-name-nondirectory buffer-file-name))
6442 "???"))
6443 ((symbolp org-category) (symbol-name org-category))
6444 (t org-category)))
6445 beg end cat pos optionp)
6446 (org-unmodified
6447 (save-excursion
6448 (save-restriction
6449 (widen)
6450 (goto-char (point-min))
6451 (put-text-property (point) (point-max) 'org-category def-cat)
6452 (while (re-search-forward
6453 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
6454 (setq pos (match-end 0)
6455 optionp (equal (char-after (match-beginning 0)) ?#)
6456 cat (org-trim (match-string 2)))
6457 (if optionp
6458 (setq beg (point-at-bol) end (point-max))
6459 (org-back-to-heading t)
6460 (setq beg (point) end (org-end-of-subtree t t)))
6461 (put-text-property beg end 'org-category cat)
6462 (goto-char pos)))))))
6465 ;;;; Link Stuff
6467 ;;; Link abbreviations
6469 (defun org-link-expand-abbrev (link)
6470 "Apply replacements as defined in `org-link-abbrev-alist."
6471 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
6472 (let* ((key (match-string 1 link))
6473 (as (or (assoc key org-link-abbrev-alist-local)
6474 (assoc key org-link-abbrev-alist)))
6475 (tag (and (match-end 2) (match-string 3 link)))
6476 rpl)
6477 (if (not as)
6478 link
6479 (setq rpl (cdr as))
6480 (cond
6481 ((symbolp rpl) (funcall rpl tag))
6482 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
6483 (t (concat rpl tag)))))
6484 link))
6486 ;;; Storing and inserting links
6488 (defvar org-insert-link-history nil
6489 "Minibuffer history for links inserted with `org-insert-link'.")
6491 (defvar org-stored-links nil
6492 "Contains the links stored with `org-store-link'.")
6494 (defvar org-store-link-plist nil
6495 "Plist with info about the most recently link created with `org-store-link'.")
6497 (defvar org-link-protocols nil
6498 "Link protocols added to Org-mode using `org-add-link-type'.")
6500 (defvar org-store-link-functions nil
6501 "List of functions that are called to create and store a link.
6502 Each function will be called in turn until one returns a non-nil
6503 value. Each function should check if it is responsible for creating
6504 this link (for example by looking at the major mode).
6505 If not, it must exit and return nil.
6506 If yes, it should return a non-nil value after a calling
6507 `org-store-link-props' with a list of properties and values.
6508 Special properties are:
6510 :type The link prefix. like \"http\". This must be given.
6511 :link The link, like \"http://www.astro.uva.nl/~dominik\".
6512 This is obligatory as well.
6513 :description Optional default description for the second pair
6514 of brackets in an Org-mode link. The user can still change
6515 this when inserting this link into an Org-mode buffer.
6517 In addition to these, any additional properties can be specified
6518 and then used in remember templates.")
6520 (defun org-add-link-type (type &optional follow export)
6521 "Add TYPE to the list of `org-link-types'.
6522 Re-compute all regular expressions depending on `org-link-types'
6524 FOLLOW and EXPORT are two functions.
6526 FOLLOW should take the link path as the single argument and do whatever
6527 is necessary to follow the link, for example find a file or display
6528 a mail message.
6530 EXPORT should format the link path for export to one of the export formats.
6531 It should be a function accepting three arguments:
6533 path the path of the link, the text after the prefix (like \"http:\")
6534 desc the description of the link, if any, nil if there was no descripton
6535 format the export format, a symbol like `html' or `latex'.
6537 The function may use the FORMAT information to return different values
6538 depending on the format. The return value will be put literally into
6539 the exported file.
6540 Org-mode has a built-in default for exporting links. If you are happy with
6541 this default, there is no need to define an export function for the link
6542 type. For a simple example of an export function, see `org-bbdb.el'."
6543 (add-to-list 'org-link-types type t)
6544 (org-make-link-regexps)
6545 (if (assoc type org-link-protocols)
6546 (setcdr (assoc type org-link-protocols) (list follow export))
6547 (push (list type follow export) org-link-protocols)))
6550 ;;;###autoload
6551 (defun org-store-link (arg)
6552 "\\<org-mode-map>Store an org-link to the current location.
6553 This link is added to `org-stored-links' and can later be inserted
6554 into an org-buffer with \\[org-insert-link].
6556 For some link types, a prefix arg is interpreted:
6557 For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
6558 For file links, arg negates `org-context-in-file-links'."
6559 (interactive "P")
6560 (org-load-modules-maybe)
6561 (setq org-store-link-plist nil) ; reset
6562 (let (link cpltxt desc description search txt)
6563 (cond
6565 ((run-hook-with-args-until-success 'org-store-link-functions)
6566 (setq link (plist-get org-store-link-plist :link)
6567 desc (or (plist-get org-store-link-plist :description) link)))
6569 ((eq major-mode 'calendar-mode)
6570 (let ((cd (calendar-cursor-to-date)))
6571 (setq link
6572 (format-time-string
6573 (car org-time-stamp-formats)
6574 (apply 'encode-time
6575 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
6576 nil nil nil))))
6577 (org-store-link-props :type "calendar" :date cd)))
6579 ((eq major-mode 'w3-mode)
6580 (setq cpltxt (url-view-url t)
6581 link (org-make-link cpltxt))
6582 (org-store-link-props :type "w3" :url (url-view-url t)))
6584 ((eq major-mode 'w3m-mode)
6585 (setq cpltxt (or w3m-current-title w3m-current-url)
6586 link (org-make-link w3m-current-url))
6587 (org-store-link-props :type "w3m" :url (url-view-url t)))
6589 ((setq search (run-hook-with-args-until-success
6590 'org-create-file-search-functions))
6591 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
6592 "::" search))
6593 (setq cpltxt (or description link)))
6595 ((eq major-mode 'image-mode)
6596 (setq cpltxt (concat "file:"
6597 (abbreviate-file-name buffer-file-name))
6598 link (org-make-link cpltxt))
6599 (org-store-link-props :type "image" :file buffer-file-name))
6601 ((eq major-mode 'dired-mode)
6602 ;; link to the file in the current line
6603 (setq cpltxt (concat "file:"
6604 (abbreviate-file-name
6605 (expand-file-name
6606 (dired-get-filename nil t))))
6607 link (org-make-link cpltxt)))
6609 ((and buffer-file-name (org-mode-p))
6610 ;; Just link to current headline
6611 (setq cpltxt (concat "file:"
6612 (abbreviate-file-name buffer-file-name)))
6613 ;; Add a context search string
6614 (when (org-xor org-context-in-file-links arg)
6615 ;; Check if we are on a target
6616 (if (org-in-regexp "<<\\(.*?\\)>>")
6617 (setq cpltxt (concat cpltxt "::" (match-string 1)))
6618 (setq txt (cond
6619 ((org-on-heading-p) nil)
6620 ((org-region-active-p)
6621 (buffer-substring (region-beginning) (region-end)))
6622 (t nil)))
6623 (when (or (null txt) (string-match "\\S-" txt))
6624 (setq cpltxt
6625 (concat cpltxt "::"
6626 (condition-case nil
6627 (org-make-org-heading-search-string txt)
6628 (error "")))
6629 desc "NONE"))))
6630 (if (string-match "::\\'" cpltxt)
6631 (setq cpltxt (substring cpltxt 0 -2)))
6632 (setq link (org-make-link cpltxt)))
6634 ((buffer-file-name (buffer-base-buffer))
6635 ;; Just link to this file here.
6636 (setq cpltxt (concat "file:"
6637 (abbreviate-file-name
6638 (buffer-file-name (buffer-base-buffer)))))
6639 ;; Add a context string
6640 (when (org-xor org-context-in-file-links arg)
6641 (setq txt (if (org-region-active-p)
6642 (buffer-substring (region-beginning) (region-end))
6643 (buffer-substring (point-at-bol) (point-at-eol))))
6644 ;; Only use search option if there is some text.
6645 (when (string-match "\\S-" txt)
6646 (setq cpltxt
6647 (concat cpltxt "::" (org-make-org-heading-search-string txt))
6648 desc "NONE")))
6649 (setq link (org-make-link cpltxt)))
6651 ((interactive-p)
6652 (error "Cannot link to a buffer which is not visiting a file"))
6654 (t (setq link nil)))
6656 (if (consp link) (setq cpltxt (car link) link (cdr link)))
6657 (setq link (or link cpltxt)
6658 desc (or desc cpltxt))
6659 (if (equal desc "NONE") (setq desc nil))
6661 (if (and (interactive-p) link)
6662 (progn
6663 (setq org-stored-links
6664 (cons (list link desc) org-stored-links))
6665 (message "Stored: %s" (or desc link)))
6666 (and link (org-make-link-string link desc)))))
6668 (defun org-store-link-props (&rest plist)
6669 "Store link properties, extract names and addresses."
6670 (let (x adr)
6671 (when (setq x (plist-get plist :from))
6672 (setq adr (mail-extract-address-components x))
6673 (plist-put plist :fromname (car adr))
6674 (plist-put plist :fromaddress (nth 1 adr)))
6675 (when (setq x (plist-get plist :to))
6676 (setq adr (mail-extract-address-components x))
6677 (plist-put plist :toname (car adr))
6678 (plist-put plist :toaddress (nth 1 adr))))
6679 (let ((from (plist-get plist :from))
6680 (to (plist-get plist :to)))
6681 (when (and from to org-from-is-user-regexp)
6682 (plist-put plist :fromto
6683 (if (string-match org-from-is-user-regexp from)
6684 (concat "to %t")
6685 (concat "from %f")))))
6686 (setq org-store-link-plist plist))
6688 (defun org-add-link-props (&rest plist)
6689 "Add these properties to the link property list."
6690 (let (key value)
6691 (while plist
6692 (setq key (pop plist) value (pop plist))
6693 (setq org-store-link-plist
6694 (plist-put org-store-link-plist key value)))))
6696 (defun org-email-link-description (&optional fmt)
6697 "Return the description part of an email link.
6698 This takes information from `org-store-link-plist' and formats it
6699 according to FMT (default from `org-email-link-description-format')."
6700 (setq fmt (or fmt org-email-link-description-format))
6701 (let* ((p org-store-link-plist)
6702 (to (plist-get p :toaddress))
6703 (from (plist-get p :fromaddress))
6704 (table
6705 (list
6706 (cons "%c" (plist-get p :fromto))
6707 (cons "%F" (plist-get p :from))
6708 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
6709 (cons "%T" (plist-get p :to))
6710 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
6711 (cons "%s" (plist-get p :subject))
6712 (cons "%m" (plist-get p :message-id)))))
6713 (when (string-match "%c" fmt)
6714 ;; Check if the user wrote this message
6715 (if (and org-from-is-user-regexp from to
6716 (save-match-data (string-match org-from-is-user-regexp from)))
6717 (setq fmt (replace-match "to %t" t t fmt))
6718 (setq fmt (replace-match "from %f" t t fmt))))
6719 (org-replace-escapes fmt table)))
6721 (defun org-make-org-heading-search-string (&optional string heading)
6722 "Make search string for STRING or current headline."
6723 (interactive)
6724 (let ((s (or string (org-get-heading))))
6725 (unless (and string (not heading))
6726 ;; We are using a headline, clean up garbage in there.
6727 (if (string-match org-todo-regexp s)
6728 (setq s (replace-match "" t t s)))
6729 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
6730 (setq s (replace-match "" t t s)))
6731 (setq s (org-trim s))
6732 (if (string-match (concat "^\\(" org-quote-string "\\|"
6733 org-comment-string "\\)") s)
6734 (setq s (replace-match "" t t s)))
6735 (while (string-match org-ts-regexp s)
6736 (setq s (replace-match "" t t s))))
6737 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
6738 (setq s (replace-match " " t t s)))
6739 (or string (setq s (concat "*" s))) ; Add * for headlines
6740 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
6742 (defun org-make-link (&rest strings)
6743 "Concatenate STRINGS."
6744 (apply 'concat strings))
6746 (defun org-make-link-string (link &optional description)
6747 "Make a link with brackets, consisting of LINK and DESCRIPTION."
6748 (unless (string-match "\\S-" link)
6749 (error "Empty link"))
6750 (when (stringp description)
6751 ;; Remove brackets from the description, they are fatal.
6752 (while (string-match "\\[" description)
6753 (setq description (replace-match "{" t t description)))
6754 (while (string-match "\\]" description)
6755 (setq description (replace-match "}" t t description))))
6756 (when (equal (org-link-escape link) description)
6757 ;; No description needed, it is identical
6758 (setq description nil))
6759 (when (and (not description)
6760 (not (equal link (org-link-escape link))))
6761 (setq description (org-extract-attributes link)))
6762 (concat "[[" (org-link-escape link) "]"
6763 (if description (concat "[" description "]") "")
6764 "]"))
6766 (defconst org-link-escape-chars
6767 '((?\ . "%20")
6768 (?\[ . "%5B")
6769 (?\] . "%5D")
6770 (?\340 . "%E0") ; `a
6771 (?\342 . "%E2") ; ^a
6772 (?\347 . "%E7") ; ,c
6773 (?\350 . "%E8") ; `e
6774 (?\351 . "%E9") ; 'e
6775 (?\352 . "%EA") ; ^e
6776 (?\356 . "%EE") ; ^i
6777 (?\364 . "%F4") ; ^o
6778 (?\371 . "%F9") ; `u
6779 (?\373 . "%FB") ; ^u
6780 (?\; . "%3B")
6781 (?? . "%3F")
6782 (?= . "%3D")
6783 (?+ . "%2B")
6785 "Association list of escapes for some characters problematic in links.
6786 This is the list that is used for internal purposes.")
6788 (defconst org-link-escape-chars-browser
6789 '((?\ . "%20")) ; 32 for the SPC char
6790 "Association list of escapes for some characters problematic in links.
6791 This is the list that is used before handing over to the browser.")
6793 (defun org-link-escape (text &optional table)
6794 "Escape charaters in TEXT that are problematic for links."
6795 (setq table (or table org-link-escape-chars))
6796 (when text
6797 (let ((re (mapconcat (lambda (x) (regexp-quote
6798 (char-to-string (car x))))
6799 table "\\|")))
6800 (while (string-match re text)
6801 (setq text
6802 (replace-match
6803 (cdr (assoc (string-to-char (match-string 0 text))
6804 table))
6805 t t text)))
6806 text)))
6808 (defun org-link-unescape (text &optional table)
6809 "Reverse the action of `org-link-escape'."
6810 (setq table (or table org-link-escape-chars))
6811 (when text
6812 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
6813 table "\\|")))
6814 (while (string-match re text)
6815 (setq text
6816 (replace-match
6817 (char-to-string (car (rassoc (match-string 0 text) table)))
6818 t t text)))
6819 text)))
6821 (defun org-xor (a b)
6822 "Exclusive or."
6823 (if a (not b) b))
6825 (defun org-get-header (header)
6826 "Find a header field in the current buffer."
6827 (save-excursion
6828 (goto-char (point-min))
6829 (let ((case-fold-search t) s)
6830 (cond
6831 ((eq header 'from)
6832 (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
6833 (setq s (match-string 1)))
6834 (while (string-match "\"" s)
6835 (setq s (replace-match "" t t s)))
6836 (if (string-match "[<(].*" s)
6837 (setq s (replace-match "" t t s))))
6838 ((eq header 'message-id)
6839 (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
6840 (setq s (match-string 1))))
6841 ((eq header 'subject)
6842 (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
6843 (setq s (match-string 1)))))
6844 (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
6845 (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
6846 s)))
6849 (defun org-fixup-message-id-for-http (s)
6850 "Replace special characters in a message id, so it can be used in an http query."
6851 (while (string-match "<" s)
6852 (setq s (replace-match "%3C" t t s)))
6853 (while (string-match ">" s)
6854 (setq s (replace-match "%3E" t t s)))
6855 (while (string-match "@" s)
6856 (setq s (replace-match "%40" t t s)))
6859 ;;;###autoload
6860 (defun org-insert-link-global ()
6861 "Insert a link like Org-mode does.
6862 This command can be called in any mode to insert a link in Org-mode syntax."
6863 (interactive)
6864 (org-load-modules-maybe)
6865 (org-run-like-in-org-mode 'org-insert-link))
6867 (defun org-insert-link (&optional complete-file link-location)
6868 "Insert a link. At the prompt, enter the link.
6870 Completion can be used to select a link previously stored with
6871 `org-store-link'. When the empty string is entered (i.e. if you just
6872 press RET at the prompt), the link defaults to the most recently
6873 stored link. As SPC triggers completion in the minibuffer, you need to
6874 use M-SPC or C-q SPC to force the insertion of a space character.
6876 You will also be prompted for a description, and if one is given, it will
6877 be displayed in the buffer instead of the link.
6879 If there is already a link at point, this command will allow you to edit link
6880 and description parts.
6882 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
6883 be selected using completion. The path to the file will be relative to the
6884 current directory if the file is in the current directory or a subdirectory.
6885 Otherwise, the link will be the absolute path as completed in the minibuffer
6886 \(i.e. normally ~/path/to/file).
6888 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
6889 the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
6890 of `org-keep-stored-link-after-insertion'.
6892 If `org-make-link-description-function' is non-nil, this function will be
6893 called with the link target, and the result will be the default
6894 link description.
6896 If the LINK-LOCATION parameter is non-nil, this value will be
6897 used as the link location instead of reading one interactively."
6898 (interactive "P")
6899 (let* ((wcf (current-window-configuration))
6900 (region (if (org-region-active-p)
6901 (buffer-substring (region-beginning) (region-end))))
6902 (remove (and region (list (region-beginning) (region-end))))
6903 (desc region)
6904 tmphist ; byte-compile incorrectly complains about this
6905 (link link-location)
6906 entry file)
6907 (cond
6908 (link-location) ; specified by arg, just use it.
6909 ((org-in-regexp org-bracket-link-regexp 1)
6910 ;; We do have a link at point, and we are going to edit it.
6911 (setq remove (list (match-beginning 0) (match-end 0)))
6912 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
6913 (setq link (read-string "Link: "
6914 (org-link-unescape
6915 (org-match-string-no-properties 1)))))
6916 ((or (org-in-regexp org-angle-link-re)
6917 (org-in-regexp org-plain-link-re))
6918 ;; Convert to bracket link
6919 (setq remove (list (match-beginning 0) (match-end 0))
6920 link (read-string "Link: "
6921 (org-remove-angle-brackets (match-string 0)))))
6922 ((equal complete-file '(4))
6923 ;; Completing read for file names.
6924 (setq file (read-file-name "File: "))
6925 (let ((pwd (file-name-as-directory (expand-file-name ".")))
6926 (pwd1 (file-name-as-directory (abbreviate-file-name
6927 (expand-file-name ".")))))
6928 (cond
6929 ((equal complete-file '(16))
6930 (setq link (org-make-link
6931 "file:"
6932 (abbreviate-file-name (expand-file-name file)))))
6933 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
6934 (setq link (org-make-link "file:" (match-string 1 file))))
6935 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
6936 (expand-file-name file))
6937 (setq link (org-make-link
6938 "file:" (match-string 1 (expand-file-name file)))))
6939 (t (setq link (org-make-link "file:" file))))))
6941 ;; Read link, with completion for stored links.
6942 (with-output-to-temp-buffer "*Org Links*"
6943 (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
6944 (when org-stored-links
6945 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
6946 (princ (mapconcat
6947 (lambda (x)
6948 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
6949 (reverse org-stored-links) "\n"))))
6950 (let ((cw (selected-window)))
6951 (select-window (get-buffer-window "*Org Links*"))
6952 (shrink-window-if-larger-than-buffer)
6953 (setq truncate-lines t)
6954 (select-window cw))
6955 ;; Fake a link history, containing the stored links.
6956 (setq tmphist (append (mapcar 'car org-stored-links)
6957 org-insert-link-history))
6958 (unwind-protect
6959 (setq link (org-completing-read
6960 "Link: "
6961 (append
6962 (mapcar (lambda (x) (list (concat (car x) ":")))
6963 (append org-link-abbrev-alist-local org-link-abbrev-alist))
6964 (mapcar (lambda (x) (list (concat x ":")))
6965 org-link-types))
6966 nil nil nil
6967 'tmphist
6968 (or (car (car org-stored-links)))))
6969 (set-window-configuration wcf)
6970 (kill-buffer "*Org Links*"))
6971 (setq entry (assoc link org-stored-links))
6972 (or entry (push link org-insert-link-history))
6973 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
6974 (not org-keep-stored-link-after-insertion))
6975 (setq org-stored-links (delq (assoc link org-stored-links)
6976 org-stored-links)))
6977 (setq desc (or desc (nth 1 entry)))))
6979 (if (string-match org-plain-link-re link)
6980 ;; URL-like link, normalize the use of angular brackets.
6981 (setq link (org-make-link (org-remove-angle-brackets link))))
6983 ;; Check if we are linking to the current file with a search option
6984 ;; If yes, simplify the link by using only the search option.
6985 (when (and buffer-file-name
6986 (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
6987 (let* ((path (match-string 1 link))
6988 (case-fold-search nil)
6989 (search (match-string 2 link)))
6990 (save-match-data
6991 (if (equal (file-truename buffer-file-name) (file-truename path))
6992 ;; We are linking to this same file, with a search option
6993 (setq link search)))))
6995 ;; Check if we can/should use a relative path. If yes, simplify the link
6996 (when (string-match "\\<file:\\(.*\\)" link)
6997 (let* ((path (match-string 1 link))
6998 (origpath path)
6999 (case-fold-search nil))
7000 (cond
7001 ((eq org-link-file-path-type 'absolute)
7002 (setq path (abbreviate-file-name (expand-file-name path))))
7003 ((eq org-link-file-path-type 'noabbrev)
7004 (setq path (expand-file-name path)))
7005 ((eq org-link-file-path-type 'relative)
7006 (setq path (file-relative-name path)))
7008 (save-match-data
7009 (if (string-match (concat "^" (regexp-quote
7010 (file-name-as-directory
7011 (expand-file-name "."))))
7012 (expand-file-name path))
7013 ;; We are linking a file with relative path name.
7014 (setq path (substring (expand-file-name path)
7015 (match-end 0)))))))
7016 (setq link (concat "file:" path))
7017 (if (equal desc origpath)
7018 (setq desc path))))
7020 (if org-make-link-description-function
7021 (setq desc (funcall org-make-link-description-function link desc)))
7023 (setq desc (read-string "Description: " desc))
7024 (unless (string-match "\\S-" desc) (setq desc nil))
7025 (if remove (apply 'delete-region remove))
7026 (insert (org-make-link-string link desc))))
7028 (defun org-completing-read (&rest args)
7029 (let ((minibuffer-local-completion-map
7030 (copy-keymap minibuffer-local-completion-map)))
7031 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
7032 (apply 'completing-read args)))
7034 (defun org-extract-attributes (s)
7035 "Extract the attributes cookie from a string and set as text property."
7036 (let (a attr (start 0))
7037 (save-match-data
7038 (when (string-match "{{\\([^}]+\\)}}$" s)
7039 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
7040 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
7041 (setq key (match-string 1 a) value (match-string 2 a)
7042 start (match-end 0)
7043 attr (plist-put attr (intern key) value))))
7044 (org-add-props s nil 'org-attributes attr))
7047 (defun org-attributes-to-string (plist)
7048 "Format a property list into an HTML attribute list."
7049 (let ((s "") key value)
7050 (while plist
7051 (setq key (pop plist) value (pop plist))
7052 (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
7055 ;;; Opening/following a link
7057 (defvar org-link-search-failed nil)
7059 (defun org-next-link ()
7060 "Move forward to the next link.
7061 If the link is in hidden text, expose it."
7062 (interactive)
7063 (when (and org-link-search-failed (eq this-command last-command))
7064 (goto-char (point-min))
7065 (message "Link search wrapped back to beginning of buffer"))
7066 (setq org-link-search-failed nil)
7067 (let* ((pos (point))
7068 (ct (org-context))
7069 (a (assoc :link ct)))
7070 (if a (goto-char (nth 2 a)))
7071 (if (re-search-forward org-any-link-re nil t)
7072 (progn
7073 (goto-char (match-beginning 0))
7074 (if (org-invisible-p) (org-show-context)))
7075 (goto-char pos)
7076 (setq org-link-search-failed t)
7077 (error "No further link found"))))
7079 (defun org-previous-link ()
7080 "Move backward to the previous link.
7081 If the link is in hidden text, expose it."
7082 (interactive)
7083 (when (and org-link-search-failed (eq this-command last-command))
7084 (goto-char (point-max))
7085 (message "Link search wrapped back to end of buffer"))
7086 (setq org-link-search-failed nil)
7087 (let* ((pos (point))
7088 (ct (org-context))
7089 (a (assoc :link ct)))
7090 (if a (goto-char (nth 1 a)))
7091 (if (re-search-backward org-any-link-re nil t)
7092 (progn
7093 (goto-char (match-beginning 0))
7094 (if (org-invisible-p) (org-show-context)))
7095 (goto-char pos)
7096 (setq org-link-search-failed t)
7097 (error "No further link found"))))
7099 (defun org-find-file-at-mouse (ev)
7100 "Open file link or URL at mouse."
7101 (interactive "e")
7102 (mouse-set-point ev)
7103 (org-open-at-point 'in-emacs))
7105 (defun org-open-at-mouse (ev)
7106 "Open file link or URL at mouse."
7107 (interactive "e")
7108 (mouse-set-point ev)
7109 (org-open-at-point))
7111 (defvar org-window-config-before-follow-link nil
7112 "The window configuration before following a link.
7113 This is saved in case the need arises to restore it.")
7115 (defvar org-open-link-marker (make-marker)
7116 "Marker pointing to the location where `org-open-at-point; was called.")
7118 ;;;###autoload
7119 (defun org-open-at-point-global ()
7120 "Follow a link like Org-mode does.
7121 This command can be called in any mode to follow a link that has
7122 Org-mode syntax."
7123 (interactive)
7124 (org-run-like-in-org-mode 'org-open-at-point))
7126 ;;;###autoload
7127 (defun org-open-link-from-string (s &optional arg)
7128 "Open a link in the string S, as if it was in Org-mode."
7129 (interactive "sLink: \nP")
7130 (with-temp-buffer
7131 (let ((org-inhibit-startup t))
7132 (org-mode)
7133 (insert s)
7134 (goto-char (point-min))
7135 (org-open-at-point arg))))
7137 (defun org-open-at-point (&optional in-emacs)
7138 "Open link at or after point.
7139 If there is no link at point, this function will search forward up to
7140 the end of the current subtree.
7141 Normally, files will be opened by an appropriate application. If the
7142 optional argument IN-EMACS is non-nil, Emacs will visit the file."
7143 (interactive "P")
7144 (org-load-modules-maybe)
7145 (move-marker org-open-link-marker (point))
7146 (setq org-window-config-before-follow-link (current-window-configuration))
7147 (org-remove-occur-highlights nil nil t)
7148 (if (org-at-timestamp-p t)
7149 (org-follow-timestamp-link)
7150 (let (type path link line search (pos (point)))
7151 (catch 'match
7152 (save-excursion
7153 (skip-chars-forward "^]\n\r")
7154 (when (org-in-regexp org-bracket-link-regexp)
7155 (setq link (org-extract-attributes
7156 (org-link-unescape (org-match-string-no-properties 1))))
7157 (while (string-match " *\n *" link)
7158 (setq link (replace-match " " t t link)))
7159 (setq link (org-link-expand-abbrev link))
7160 (cond
7161 ((or (file-name-absolute-p link)
7162 (string-match "^\\.\\.?/" link))
7163 (setq type "file" path link))
7164 ((string-match org-link-re-with-space2 link)
7165 (setq type (match-string 1 link) path (match-string 2 link)))
7166 (t (setq type "thisfile" path link)))
7167 (throw 'match t)))
7169 (when (get-text-property (point) 'org-linked-text)
7170 (setq type "thisfile"
7171 pos (if (get-text-property (1+ (point)) 'org-linked-text)
7172 (1+ (point)) (point))
7173 path (buffer-substring
7174 (previous-single-property-change pos 'org-linked-text)
7175 (next-single-property-change pos 'org-linked-text)))
7176 (throw 'match t))
7178 (save-excursion
7179 (when (or (org-in-regexp org-angle-link-re)
7180 (org-in-regexp org-plain-link-re))
7181 (setq type (match-string 1) path (match-string 2))
7182 (throw 'match t)))
7183 (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
7184 (setq type "tree-match"
7185 path (match-string 1))
7186 (throw 'match t))
7187 (save-excursion
7188 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
7189 (setq type "tags"
7190 path (match-string 1))
7191 (while (string-match ":" path)
7192 (setq path (replace-match "+" t t path)))
7193 (throw 'match t))))
7194 (unless path
7195 (error "No link found"))
7196 ;; Remove any trailing spaces in path
7197 (if (string-match " +\\'" path)
7198 (setq path (replace-match "" t t path)))
7200 (cond
7202 ((assoc type org-link-protocols)
7203 (funcall (nth 1 (assoc type org-link-protocols)) path))
7205 ((equal type "mailto")
7206 (let ((cmd (car org-link-mailto-program))
7207 (args (cdr org-link-mailto-program)) args1
7208 (address path) (subject "") a)
7209 (if (string-match "\\(.*\\)::\\(.*\\)" path)
7210 (setq address (match-string 1 path)
7211 subject (org-link-escape (match-string 2 path))))
7212 (while args
7213 (cond
7214 ((not (stringp (car args))) (push (pop args) args1))
7215 (t (setq a (pop args))
7216 (if (string-match "%a" a)
7217 (setq a (replace-match address t t a)))
7218 (if (string-match "%s" a)
7219 (setq a (replace-match subject t t a)))
7220 (push a args1))))
7221 (apply cmd (nreverse args1))))
7223 ((member type '("http" "https" "ftp" "news"))
7224 (browse-url (concat type ":" (org-link-escape
7225 path org-link-escape-chars-browser))))
7227 ((member type '("message"))
7228 (browse-url (concat type ":" path)))
7230 ((string= type "tags")
7231 (org-tags-view in-emacs path))
7232 ((string= type "thisfile")
7233 (if in-emacs
7234 (switch-to-buffer-other-window
7235 (org-get-buffer-for-internal-link (current-buffer)))
7236 (org-mark-ring-push))
7237 (let ((cmd `(org-link-search
7238 ,path
7239 ,(cond ((equal in-emacs '(4)) 'occur)
7240 ((equal in-emacs '(16)) 'org-occur)
7241 (t nil))
7242 ,pos)))
7243 (condition-case nil (eval cmd)
7244 (error (progn (widen) (eval cmd))))))
7246 ((string= type "tree-match")
7247 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
7249 ((string= type "file")
7250 (if (string-match "::\\([0-9]+\\)\\'" path)
7251 (setq line (string-to-number (match-string 1 path))
7252 path (substring path 0 (match-beginning 0)))
7253 (if (string-match "::\\(.+\\)\\'" path)
7254 (setq search (match-string 1 path)
7255 path (substring path 0 (match-beginning 0)))))
7256 (if (string-match "[*?{]" (file-name-nondirectory path))
7257 (dired path)
7258 (org-open-file path in-emacs line search)))
7260 ((string= type "news")
7261 (require 'org-gnus)
7262 (org-gnus-follow-link path))
7264 ((string= type "shell")
7265 (let ((cmd path))
7266 (if (or (not org-confirm-shell-link-function)
7267 (funcall org-confirm-shell-link-function
7268 (format "Execute \"%s\" in shell? "
7269 (org-add-props cmd nil
7270 'face 'org-warning))))
7271 (progn
7272 (message "Executing %s" cmd)
7273 (shell-command cmd))
7274 (error "Abort"))))
7276 ((string= type "elisp")
7277 (let ((cmd path))
7278 (if (or (not org-confirm-elisp-link-function)
7279 (funcall org-confirm-elisp-link-function
7280 (format "Execute \"%s\" as elisp? "
7281 (org-add-props cmd nil
7282 'face 'org-warning))))
7283 (message "%s => %s" cmd (eval (read cmd)))
7284 (error "Abort"))))
7287 (browse-url-at-point)))))
7288 (move-marker org-open-link-marker nil)
7289 (run-hook-with-args 'org-follow-link-hook))
7291 ;;;; Time estimates
7293 (defun org-get-effort (&optional pom)
7294 "Get the effort estimate for the current entry."
7295 (org-entry-get pom org-effort-property))
7297 ;;; File search
7299 (defvar org-create-file-search-functions nil
7300 "List of functions to construct the right search string for a file link.
7301 These functions are called in turn with point at the location to
7302 which the link should point.
7304 A function in the hook should first test if it would like to
7305 handle this file type, for example by checking the major-mode or
7306 the file extension. If it decides not to handle this file, it
7307 should just return nil to give other functions a chance. If it
7308 does handle the file, it must return the search string to be used
7309 when following the link. The search string will be part of the
7310 file link, given after a double colon, and `org-open-at-point'
7311 will automatically search for it. If special measures must be
7312 taken to make the search successful, another function should be
7313 added to the companion hook `org-execute-file-search-functions',
7314 which see.
7316 A function in this hook may also use `setq' to set the variable
7317 `description' to provide a suggestion for the descriptive text to
7318 be used for this link when it gets inserted into an Org-mode
7319 buffer with \\[org-insert-link].")
7321 (defvar org-execute-file-search-functions nil
7322 "List of functions to execute a file search triggered by a link.
7324 Functions added to this hook must accept a single argument, the
7325 search string that was part of the file link, the part after the
7326 double colon. The function must first check if it would like to
7327 handle this search, for example by checking the major-mode or the
7328 file extension. If it decides not to handle this search, it
7329 should just return nil to give other functions a chance. If it
7330 does handle the search, it must return a non-nil value to keep
7331 other functions from trying.
7333 Each function can access the current prefix argument through the
7334 variable `current-prefix-argument'. Note that a single prefix is
7335 used to force opening a link in Emacs, so it may be good to only
7336 use a numeric or double prefix to guide the search function.
7338 In case this is needed, a function in this hook can also restore
7339 the window configuration before `org-open-at-point' was called using:
7341 (set-window-configuration org-window-config-before-follow-link)")
7343 (defun org-link-search (s &optional type avoid-pos)
7344 "Search for a link search option.
7345 If S is surrounded by forward slashes, it is interpreted as a
7346 regular expression. In org-mode files, this will create an `org-occur'
7347 sparse tree. In ordinary files, `occur' will be used to list matches.
7348 If the current buffer is in `dired-mode', grep will be used to search
7349 in all files. If AVOID-POS is given, ignore matches near that position."
7350 (let ((case-fold-search t)
7351 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
7352 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
7353 (append '(("") (" ") ("\t") ("\n"))
7354 org-emphasis-alist)
7355 "\\|") "\\)"))
7356 (pos (point))
7357 (pre nil) (post nil)
7358 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
7359 (cond
7360 ;; First check if there are any special
7361 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
7362 ;; Now try the builtin stuff
7363 ((save-excursion
7364 (goto-char (point-min))
7365 (and
7366 (re-search-forward
7367 (concat "<<" (regexp-quote s0) ">>") nil t)
7368 (setq type 'dedicated
7369 pos (match-beginning 0))))
7370 ;; There is an exact target for this
7371 (goto-char pos))
7372 ((string-match "^/\\(.*\\)/$" s)
7373 ;; A regular expression
7374 (cond
7375 ((org-mode-p)
7376 (org-occur (match-string 1 s)))
7377 ;;((eq major-mode 'dired-mode)
7378 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
7379 (t (org-do-occur (match-string 1 s)))))
7381 ;; A normal search strings
7382 (when (equal (string-to-char s) ?*)
7383 ;; Anchor on headlines, post may include tags.
7384 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
7385 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
7386 s (substring s 1)))
7387 (remove-text-properties
7388 0 (length s)
7389 '(face nil mouse-face nil keymap nil fontified nil) s)
7390 ;; Make a series of regular expressions to find a match
7391 (setq words (org-split-string s "[ \n\r\t]+")
7393 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
7394 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
7395 "\\)" markers)
7396 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
7397 re2a (concat "[ \t\r\n]" re2a_)
7398 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
7399 re4 (concat "[^a-zA-Z_]" re4_)
7401 re1 (concat pre re2 post)
7402 re3 (concat pre (if pre re4_ re4) post)
7403 re5 (concat pre ".*" re4)
7404 re2 (concat pre re2)
7405 re2a (concat pre (if pre re2a_ re2a))
7406 re4 (concat pre (if pre re4_ re4))
7407 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
7408 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
7409 re5 "\\)"
7411 (cond
7412 ((eq type 'org-occur) (org-occur reall))
7413 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
7414 (t (goto-char (point-min))
7415 (setq type 'fuzzy)
7416 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
7417 (org-search-not-self 1 re1 nil t)
7418 (org-search-not-self 1 re2 nil t)
7419 (org-search-not-self 1 re2a nil t)
7420 (org-search-not-self 1 re3 nil t)
7421 (org-search-not-self 1 re4 nil t)
7422 (org-search-not-self 1 re5 nil t)
7424 (goto-char (match-beginning 1))
7425 (goto-char pos)
7426 (error "No match")))))
7428 ;; Normal string-search
7429 (goto-char (point-min))
7430 (if (search-forward s nil t)
7431 (goto-char (match-beginning 0))
7432 (error "No match"))))
7433 (and (org-mode-p) (org-show-context 'link-search))
7434 type))
7436 (defun org-search-not-self (group &rest args)
7437 "Execute `re-search-forward', but only accept matches that do not
7438 enclose the position of `org-open-link-marker'."
7439 (let ((m org-open-link-marker))
7440 (catch 'exit
7441 (while (apply 're-search-forward args)
7442 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
7443 (goto-char (match-end group))
7444 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
7445 (> (match-beginning 0) (marker-position m))
7446 (< (match-end 0) (marker-position m)))
7447 (save-match-data
7448 (or (not (org-in-regexp
7449 org-bracket-link-analytic-regexp 1))
7450 (not (match-end 4)) ; no description
7451 (and (<= (match-beginning 4) (point))
7452 (>= (match-end 4) (point))))))
7453 (throw 'exit (point))))))))
7455 (defun org-get-buffer-for-internal-link (buffer)
7456 "Return a buffer to be used for displaying the link target of internal links."
7457 (cond
7458 ((not org-display-internal-link-with-indirect-buffer)
7459 buffer)
7460 ((string-match "(Clone)$" (buffer-name buffer))
7461 (message "Buffer is already a clone, not making another one")
7462 ;; we also do not modify visibility in this case
7463 buffer)
7464 (t ; make a new indirect buffer for displaying the link
7465 (let* ((bn (buffer-name buffer))
7466 (ibn (concat bn "(Clone)"))
7467 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
7468 (with-current-buffer ib (org-overview))
7469 ib))))
7471 (defun org-do-occur (regexp &optional cleanup)
7472 "Call the Emacs command `occur'.
7473 If CLEANUP is non-nil, remove the printout of the regular expression
7474 in the *Occur* buffer. This is useful if the regex is long and not useful
7475 to read."
7476 (occur regexp)
7477 (when cleanup
7478 (let ((cwin (selected-window)) win beg end)
7479 (when (setq win (get-buffer-window "*Occur*"))
7480 (select-window win))
7481 (goto-char (point-min))
7482 (when (re-search-forward "match[a-z]+" nil t)
7483 (setq beg (match-end 0))
7484 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
7485 (setq end (1- (match-beginning 0)))))
7486 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
7487 (goto-char (point-min))
7488 (select-window cwin))))
7490 ;;; The mark ring for links jumps
7492 (defvar org-mark-ring nil
7493 "Mark ring for positions before jumps in Org-mode.")
7494 (defvar org-mark-ring-last-goto nil
7495 "Last position in the mark ring used to go back.")
7496 ;; Fill and close the ring
7497 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
7498 (loop for i from 1 to org-mark-ring-length do
7499 (push (make-marker) org-mark-ring))
7500 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
7501 org-mark-ring)
7503 (defun org-mark-ring-push (&optional pos buffer)
7504 "Put the current position or POS into the mark ring and rotate it."
7505 (interactive)
7506 (setq pos (or pos (point)))
7507 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
7508 (move-marker (car org-mark-ring)
7509 (or pos (point))
7510 (or buffer (current-buffer)))
7511 (message "%s"
7512 (substitute-command-keys
7513 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
7515 (defun org-mark-ring-goto (&optional n)
7516 "Jump to the previous position in the mark ring.
7517 With prefix arg N, jump back that many stored positions. When
7518 called several times in succession, walk through the entire ring.
7519 Org-mode commands jumping to a different position in the current file,
7520 or to another Org-mode file, automatically push the old position
7521 onto the ring."
7522 (interactive "p")
7523 (let (p m)
7524 (if (eq last-command this-command)
7525 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
7526 (setq p org-mark-ring))
7527 (setq org-mark-ring-last-goto p)
7528 (setq m (car p))
7529 (switch-to-buffer (marker-buffer m))
7530 (goto-char m)
7531 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
7533 (defun org-remove-angle-brackets (s)
7534 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
7535 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
7537 (defun org-add-angle-brackets (s)
7538 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
7539 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
7541 (defun org-remove-double-quotes (s)
7542 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
7543 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
7546 ;;; Following specific links
7548 (defun org-follow-timestamp-link ()
7549 (cond
7550 ((org-at-date-range-p t)
7551 (let ((org-agenda-start-on-weekday)
7552 (t1 (match-string 1))
7553 (t2 (match-string 2)))
7554 (setq t1 (time-to-days (org-time-string-to-time t1))
7555 t2 (time-to-days (org-time-string-to-time t2)))
7556 (org-agenda-list nil t1 (1+ (- t2 t1)))))
7557 ((org-at-timestamp-p t)
7558 (org-agenda-list nil (time-to-days (org-time-string-to-time
7559 (substring (match-string 1) 0 10)))
7561 (t (error "This should not happen"))))
7564 ;;; Following file links
7565 (defvar org-wait nil)
7566 (defun org-open-file (path &optional in-emacs line search)
7567 "Open the file at PATH.
7568 First, this expands any special file name abbreviations. Then the
7569 configuration variable `org-file-apps' is checked if it contains an
7570 entry for this file type, and if yes, the corresponding command is launched.
7571 If no application is found, Emacs simply visits the file.
7572 With optional argument IN-EMACS, Emacs will visit the file.
7573 Optional LINE specifies a line to go to, optional SEARCH a string to
7574 search for. If LINE or SEARCH is given, the file will always be
7575 opened in Emacs.
7576 If the file does not exist, an error is thrown."
7577 (setq in-emacs (or in-emacs line search))
7578 (let* ((file (if (equal path "")
7579 buffer-file-name
7580 (substitute-in-file-name (expand-file-name path))))
7581 (apps (append org-file-apps (org-default-apps)))
7582 (remp (and (assq 'remote apps) (org-file-remote-p file)))
7583 (dirp (if remp nil (file-directory-p file)))
7584 (file (if (and dirp org-open-directory-means-index-dot-org)
7585 (concat (file-name-as-directory file) "index.org")
7586 file))
7587 (dfile (downcase file))
7588 (old-buffer (current-buffer))
7589 (old-pos (point))
7590 (old-mode major-mode)
7591 ext cmd)
7592 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
7593 (setq ext (match-string 1 dfile))
7594 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
7595 (setq ext (match-string 1 dfile))))
7596 (if in-emacs
7597 (setq cmd 'emacs)
7598 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
7599 (and dirp (cdr (assoc 'directory apps)))
7600 (cdr (assoc ext apps))
7601 (cdr (assoc t apps)))))
7602 (when (eq cmd 'mailcap)
7603 (require 'mailcap)
7604 (mailcap-parse-mailcaps)
7605 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
7606 (command (mailcap-mime-info mime-type)))
7607 (if (stringp command)
7608 (setq cmd command)
7609 (setq cmd 'emacs))))
7610 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
7611 (not (file-exists-p file))
7612 (not org-open-non-existing-files))
7613 (error "No such file: %s" file))
7614 (cond
7615 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
7616 ;; Remove quotes around the file name - we'll use shell-quote-argument.
7617 (while (string-match "['\"]%s['\"]" cmd)
7618 (setq cmd (replace-match "%s" t t cmd)))
7619 (while (string-match "%s" cmd)
7620 (setq cmd (replace-match
7621 (save-match-data
7622 (shell-quote-argument
7623 (convert-standard-filename file)))
7624 t t cmd)))
7625 (save-window-excursion
7626 (start-process-shell-command cmd nil cmd)
7627 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
7629 ((or (stringp cmd)
7630 (eq cmd 'emacs))
7631 (funcall (cdr (assq 'file org-link-frame-setup)) file)
7632 (widen)
7633 (if line (goto-line line)
7634 (if search (org-link-search search))))
7635 ((consp cmd)
7636 (let ((file (convert-standard-filename file)))
7637 (eval cmd)))
7638 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
7639 (and (org-mode-p) (eq old-mode 'org-mode)
7640 (or (not (equal old-buffer (current-buffer)))
7641 (not (equal old-pos (point))))
7642 (org-mark-ring-push old-pos old-buffer))))
7644 (defun org-default-apps ()
7645 "Return the default applications for this operating system."
7646 (cond
7647 ((eq system-type 'darwin)
7648 org-file-apps-defaults-macosx)
7649 ((eq system-type 'windows-nt)
7650 org-file-apps-defaults-windowsnt)
7651 (t org-file-apps-defaults-gnu)))
7653 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
7654 (defun org-file-remote-p (file)
7655 "Test whether FILE specifies a location on a remote system.
7656 Return non-nil if the location is indeed remote.
7658 For example, the filename \"/user@host:/foo\" specifies a location
7659 on the system \"/user@host:\"."
7660 (cond ((fboundp 'file-remote-p)
7661 (file-remote-p file))
7662 ((fboundp 'tramp-handle-file-remote-p)
7663 (tramp-handle-file-remote-p file))
7664 ((and (boundp 'ange-ftp-name-format)
7665 (string-match (car ange-ftp-name-format) file))
7667 (t nil)))
7670 ;;;; Refiling
7672 (defun org-get-org-file ()
7673 "Read a filename, with default directory `org-directory'."
7674 (let ((default (or org-default-notes-file remember-data-file)))
7675 (read-file-name (format "File name [%s]: " default)
7676 (file-name-as-directory org-directory)
7677 default)))
7679 (defun org-notes-order-reversed-p ()
7680 "Check if the current file should receive notes in reversed order."
7681 (cond
7682 ((not org-reverse-note-order) nil)
7683 ((eq t org-reverse-note-order) t)
7684 ((not (listp org-reverse-note-order)) nil)
7685 (t (catch 'exit
7686 (let ((all org-reverse-note-order)
7687 entry)
7688 (while (setq entry (pop all))
7689 (if (string-match (car entry) buffer-file-name)
7690 (throw 'exit (cdr entry))))
7691 nil)))))
7693 (defvar org-refile-target-table nil
7694 "The list of refile targets, created by `org-refile'.")
7696 (defvar org-agenda-new-buffers nil
7697 "Buffers created to visit agenda files.")
7699 (defun org-get-refile-targets (&optional default-buffer)
7700 "Produce a table with refile targets."
7701 (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
7702 targets txt re files f desc descre)
7703 (with-current-buffer (or default-buffer (current-buffer))
7704 (while (setq entry (pop entries))
7705 (setq files (car entry) desc (cdr entry))
7706 (cond
7707 ((null files) (setq files (list (current-buffer))))
7708 ((eq files 'org-agenda-files)
7709 (setq files (org-agenda-files 'unrestricted)))
7710 ((and (symbolp files) (fboundp files))
7711 (setq files (funcall files)))
7712 ((and (symbolp files) (boundp files))
7713 (setq files (symbol-value files))))
7714 (if (stringp files) (setq files (list files)))
7715 (cond
7716 ((eq (car desc) :tag)
7717 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
7718 ((eq (car desc) :todo)
7719 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
7720 ((eq (car desc) :regexp)
7721 (setq descre (cdr desc)))
7722 ((eq (car desc) :level)
7723 (setq descre (concat "^\\*\\{" (number-to-string
7724 (if org-odd-levels-only
7725 (1- (* 2 (cdr desc)))
7726 (cdr desc)))
7727 "\\}[ \t]")))
7728 ((eq (car desc) :maxlevel)
7729 (setq descre (concat "^\\*\\{1," (number-to-string
7730 (if org-odd-levels-only
7731 (1- (* 2 (cdr desc)))
7732 (cdr desc)))
7733 "\\}[ \t]")))
7734 (t (error "Bad refiling target description %s" desc)))
7735 (while (setq f (pop files))
7736 (save-excursion
7737 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
7738 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
7739 (save-excursion
7740 (save-restriction
7741 (widen)
7742 (goto-char (point-min))
7743 (while (re-search-forward descre nil t)
7744 (goto-char (point-at-bol))
7745 (when (looking-at org-complex-heading-regexp)
7746 (setq txt (match-string 4)
7747 re (concat "^" (regexp-quote
7748 (buffer-substring (match-beginning 1)
7749 (match-end 4)))))
7750 (if (match-end 5) (setq re (concat re "[ \t]+"
7751 (regexp-quote
7752 (match-string 5)))))
7753 (setq re (concat re "[ \t]*$"))
7754 (when org-refile-use-outline-path
7755 (setq txt (mapconcat 'identity
7756 (append
7757 (if (eq org-refile-use-outline-path 'file)
7758 (list (file-name-nondirectory
7759 (buffer-file-name (buffer-base-buffer))))
7760 (if (eq org-refile-use-outline-path 'full-file-path)
7761 (list (buffer-file-name (buffer-base-buffer)))))
7762 (org-get-outline-path)
7763 (list txt))
7764 "/")))
7765 (push (list txt f re (point)) targets))
7766 (goto-char (point-at-eol))))))))
7767 (nreverse targets))))
7769 (defun org-get-outline-path ()
7770 "Return the outline path to the current entry, as a list."
7771 (let (rtn)
7772 (save-excursion
7773 (while (org-up-heading-safe)
7774 (when (looking-at org-complex-heading-regexp)
7775 (push (org-match-string-no-properties 4) rtn)))
7776 rtn)))
7778 (defvar org-refile-history nil
7779 "History for refiling operations.")
7781 (defun org-refile (&optional goto default-buffer)
7782 "Move the entry at point to another heading.
7783 The list of target headings is compiled using the information in
7784 `org-refile-targets', which see. This list is created before each use
7785 and will therefore always be up-to-date.
7787 At the target location, the entry is filed as a subitem of the target heading.
7788 Depending on `org-reverse-note-order', the new subitem will either be the
7789 first of the last subitem.
7791 With prefix arg GOTO, the command will only visit the target location,
7792 not actually move anything.
7793 With a double prefix `C-c C-c', go to the location where the last refiling
7794 operation has put the subtree."
7795 (interactive "P")
7796 (let* ((cbuf (current-buffer))
7797 (filename (buffer-file-name (buffer-base-buffer cbuf)))
7798 pos it nbuf file re level reversed)
7799 (if (equal goto '(16))
7800 (org-refile-goto-last-stored)
7801 (when (setq it (org-refile-get-location
7802 (if goto "Goto: " "Refile to: ") default-buffer))
7803 (setq file (nth 1 it)
7804 re (nth 2 it)
7805 pos (nth 3 it))
7806 (setq nbuf (or (find-buffer-visiting file)
7807 (find-file-noselect file)))
7808 (if goto
7809 (progn
7810 (switch-to-buffer nbuf)
7811 (goto-char pos)
7812 (org-show-context 'org-goto))
7813 (org-copy-subtree 1 nil t)
7814 (save-excursion
7815 (set-buffer (setq nbuf (or (find-buffer-visiting file)
7816 (find-file-noselect file))))
7817 (setq reversed (org-notes-order-reversed-p))
7818 (save-excursion
7819 (save-restriction
7820 (widen)
7821 (goto-char pos)
7822 (looking-at outline-regexp)
7823 (setq level (org-get-valid-level (funcall outline-level) 1))
7824 (goto-char
7825 (if reversed
7826 (outline-next-heading)
7827 (or (save-excursion (outline-get-next-sibling))
7828 (org-end-of-subtree t t)
7829 (point-max))))
7830 (bookmark-set "org-refile-last-stored")
7831 (org-paste-subtree level))))
7832 (org-cut-subtree)
7833 (setq org-markers-to-move nil)
7834 (message "Entry refiled to \"%s\"" (car it)))))))
7836 (defun org-refile-goto-last-stored ()
7837 "Go to the location where the last refile was stored."
7838 (interactive)
7839 (bookmark-jump "org-refile-last-stored")
7840 (message "This is the location of the last refile"))
7842 (defun org-refile-get-location (&optional prompt default-buffer)
7843 "Prompt the user for a refile location, using PROMPT."
7844 (let ((org-refile-targets org-refile-targets)
7845 (org-refile-use-outline-path org-refile-use-outline-path))
7846 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
7847 (unless org-refile-target-table
7848 (error "No refile targets"))
7849 (let* ((cbuf (current-buffer))
7850 (cfunc (if org-refile-use-outline-path
7851 'org-olpath-completing-read
7852 'completing-read))
7853 (extra (if org-refile-use-outline-path "/" ""))
7854 (filename (buffer-file-name (buffer-base-buffer cbuf)))
7855 (fname (and filename (file-truename filename)))
7856 (tbl (mapcar
7857 (lambda (x)
7858 (if (not (equal fname (file-truename (nth 1 x))))
7859 (cons (concat (car x) extra " ("
7860 (file-name-nondirectory (nth 1 x)) ")")
7861 (cdr x))
7862 (cons (concat (car x) extra) (cdr x))))
7863 org-refile-target-table))
7864 (completion-ignore-case t))
7865 (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
7866 tbl)))
7868 (defun org-olpath-completing-read (prompt collection &rest args)
7869 "Read an outline path like a file name."
7870 (let ((thetable collection))
7871 (apply
7872 'completing-read prompt
7873 (lambda (string predicate &optional flag)
7874 (let (rtn r s f (l (length string)))
7875 (cond
7876 ((eq flag nil)
7877 ;; try completion
7878 (try-completion string thetable))
7879 ((eq flag t)
7880 ;; all-completions
7881 (setq rtn (all-completions string thetable predicate))
7882 (mapcar
7883 (lambda (x)
7884 (setq r (substring x l))
7885 (if (string-match " ([^)]*)$" x)
7886 (setq f (match-string 0 x))
7887 (setq f ""))
7888 (if (string-match "/" r)
7889 (concat string (substring r 0 (match-end 0)) f)
7891 rtn))
7892 ((eq flag 'lambda)
7893 ;; exact match?
7894 (assoc string thetable)))
7896 args)))
7898 ;;;; Dynamic blocks
7900 (defun org-find-dblock (name)
7901 "Find the first dynamic block with name NAME in the buffer.
7902 If not found, stay at current position and return nil."
7903 (let (pos)
7904 (save-excursion
7905 (goto-char (point-min))
7906 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
7907 nil t)
7908 (match-beginning 0))))
7909 (if pos (goto-char pos))
7910 pos))
7912 (defconst org-dblock-start-re
7913 "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
7914 "Matches the startline of a dynamic block, with parameters.")
7916 (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
7917 "Matches the end of a dyhamic block.")
7919 (defun org-create-dblock (plist)
7920 "Create a dynamic block section, with parameters taken from PLIST.
7921 PLIST must containe a :name entry which is used as name of the block."
7922 (unless (bolp) (newline))
7923 (let ((name (plist-get plist :name)))
7924 (insert "#+BEGIN: " name)
7925 (while plist
7926 (if (eq (car plist) :name)
7927 (setq plist (cddr plist))
7928 (insert " " (prin1-to-string (pop plist)))))
7929 (insert "\n\n#+END:\n")
7930 (beginning-of-line -2)))
7932 (defun org-prepare-dblock ()
7933 "Prepare dynamic block for refresh.
7934 This empties the block, puts the cursor at the insert position and returns
7935 the property list including an extra property :name with the block name."
7936 (unless (looking-at org-dblock-start-re)
7937 (error "Not at a dynamic block"))
7938 (let* ((begdel (1+ (match-end 0)))
7939 (name (org-no-properties (match-string 1)))
7940 (params (append (list :name name)
7941 (read (concat "(" (match-string 3) ")")))))
7942 (unless (re-search-forward org-dblock-end-re nil t)
7943 (error "Dynamic block not terminated"))
7944 (setq params
7945 (append params
7946 (list :content (buffer-substring
7947 begdel (match-beginning 0)))))
7948 (delete-region begdel (match-beginning 0))
7949 (goto-char begdel)
7950 (open-line 1)
7951 params))
7953 (defun org-map-dblocks (&optional command)
7954 "Apply COMMAND to all dynamic blocks in the current buffer.
7955 If COMMAND is not given, use `org-update-dblock'."
7956 (let ((cmd (or command 'org-update-dblock))
7957 pos)
7958 (save-excursion
7959 (goto-char (point-min))
7960 (while (re-search-forward org-dblock-start-re nil t)
7961 (goto-char (setq pos (match-beginning 0)))
7962 (condition-case nil
7963 (funcall cmd)
7964 (error (message "Error during update of dynamic block")))
7965 (goto-char pos)
7966 (unless (re-search-forward org-dblock-end-re nil t)
7967 (error "Dynamic block not terminated"))))))
7969 (defun org-dblock-update (&optional arg)
7970 "User command for updating dynamic blocks.
7971 Update the dynamic block at point. With prefix ARG, update all dynamic
7972 blocks in the buffer."
7973 (interactive "P")
7974 (if arg
7975 (org-update-all-dblocks)
7976 (or (looking-at org-dblock-start-re)
7977 (org-beginning-of-dblock))
7978 (org-update-dblock)))
7980 (defun org-update-dblock ()
7981 "Update the dynamic block at point
7982 This means to empty the block, parse for parameters and then call
7983 the correct writing function."
7984 (save-window-excursion
7985 (let* ((pos (point))
7986 (line (org-current-line))
7987 (params (org-prepare-dblock))
7988 (name (plist-get params :name))
7989 (cmd (intern (concat "org-dblock-write:" name))))
7990 (message "Updating dynamic block `%s' at line %d..." name line)
7991 (funcall cmd params)
7992 (message "Updating dynamic block `%s' at line %d...done" name line)
7993 (goto-char pos))))
7995 (defun org-beginning-of-dblock ()
7996 "Find the beginning of the dynamic block at point.
7997 Error if there is no scuh block at point."
7998 (let ((pos (point))
7999 beg)
8000 (end-of-line 1)
8001 (if (and (re-search-backward org-dblock-start-re nil t)
8002 (setq beg (match-beginning 0))
8003 (re-search-forward org-dblock-end-re nil t)
8004 (> (match-end 0) pos))
8005 (goto-char beg)
8006 (goto-char pos)
8007 (error "Not in a dynamic block"))))
8009 (defun org-update-all-dblocks ()
8010 "Update all dynamic blocks in the buffer.
8011 This function can be used in a hook."
8012 (when (org-mode-p)
8013 (org-map-dblocks 'org-update-dblock)))
8016 ;;;; Completion
8018 (defconst org-additional-option-like-keywords
8019 '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
8020 "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
8021 "BEGIN_EXAMPLE" "END_EXAMPLE"))
8023 (defcustom org-structure-template-alist
8025 ("s" "#+begin_src ?\n\n#+end_src"
8026 "<src lang=\"?\">\n\n</src>")
8027 ("e" "#+begin_example\n?\n#+end_example"
8028 "<example>\n?\n</example>")
8029 ("q" "#+begin_quote\n?\n#+end_quote"
8030 "<quote>\n?\n</quote>")
8031 ("v" "#+begin_verse\n?\n#+end_verse"
8032 "<verse>\n?\n/verse>")
8033 ("l" "#+begin_latex\n?\n#+end_latex"
8034 "<literal style=\"latex\">\n?\n</literal>")
8035 ("L" "#+latex: "
8036 "<literal style=\"latex\">?</literal>")
8037 ("h" "#+begin_html\n?\n#+end_html"
8038 "<literal style=\"html\">\n?\n</literal>")
8039 ("H" "#+html: "
8040 "<literal style=\"html\">?</literal>")
8041 ("a" "#+begin_ascii\n?\n#+end_ascii")
8042 ("A" "#+ascii: ")
8043 ("i" "#+include %file ?"
8044 "<include file=%file markup=\"?\">")
8046 "Structure completion elements.
8047 This is a list of abbreviation keys and values. The value gets inserted
8048 it you type @samp{.} followed by the key and then the completion key,
8049 usually `M-TAB'. %file will be replaced by a file name after prompting
8050 for the file uning completion.
8051 There are two templates for each key, the first uses the original Org syntax,
8052 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
8053 the default when the /org-mtags.el/ module has been loaded. See also the
8054 variable `org-mtags-prefere-muse-templates'.
8055 This is an experimental feature, it is undecided if it is going to stay in."
8056 :group 'org-completion
8057 :type '(repeat
8058 (string :tag "Key")
8059 (string :tag "Template")
8060 (string :tag "Muse Template")))
8062 (defun org-try-structure-completion ()
8063 "Try to complete a structure template before point.
8064 This looks for strings like \"<e\" on an otherwise empty line and
8065 expands them."
8066 (let ((l (buffer-substring (point-at-bol) (point)))
8068 (when (and (looking-at "[ \t]*$")
8069 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
8070 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
8071 (org-complete-expand-structure-template (+ -1 (point-at-bol)
8072 (match-beginning 1)) a)
8073 t)))
8075 (defun org-complete-expand-structure-template (start cell)
8076 "Expand a structure template."
8077 (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
8078 (rpl (nth (if musep 2 1) cell)))
8079 (delete-region start (point))
8080 (when (string-match "\\`#\\+" rpl)
8081 (cond
8082 ((bolp))
8083 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
8084 (delete-region (point-at-bol) (point)))
8085 (t (newline))))
8086 (setq start (point))
8087 (if (string-match "%file" rpl)
8088 (setq rpl (replace-match
8089 (concat
8090 "\""
8091 (save-match-data
8092 (abbreviate-file-name (read-file-name "Include file: ")))
8093 "\"")
8094 t t rpl)))
8095 (insert rpl)
8096 (if (re-search-backward "\\?" start t) (delete-char 1))))
8099 (defun org-complete (&optional arg)
8100 "Perform completion on word at point.
8101 At the beginning of a headline, this completes TODO keywords as given in
8102 `org-todo-keywords'.
8103 If the current word is preceded by a backslash, completes the TeX symbols
8104 that are supported for HTML support.
8105 If the current word is preceded by \"#+\", completes special words for
8106 setting file options.
8107 In the line after \"#+STARTUP:, complete valid keywords.\"
8108 At all other locations, this simply calls the value of
8109 `org-completion-fallback-command'."
8110 (interactive "P")
8111 (org-without-partial-completion
8112 (catch 'exit
8113 (let* ((a nil)
8114 (end (point))
8115 (beg1 (save-excursion
8116 (skip-chars-backward (org-re "[:alnum:]_@"))
8117 (point)))
8118 (beg (save-excursion
8119 (skip-chars-backward "a-zA-Z0-9_:$")
8120 (point)))
8121 (confirm (lambda (x) (stringp (car x))))
8122 (searchhead (equal (char-before beg) ?*))
8123 (struct
8124 (when (and (member (char-before beg1) '(?. ?<))
8125 (setq a (assoc (buffer-substring beg1 (point))
8126 org-structure-template-alist)))
8127 (org-complete-expand-structure-template (1- beg1) a)
8128 (throw 'exit t)))
8129 (tag (and (equal (char-before beg1) ?:)
8130 (equal (char-after (point-at-bol)) ?*)))
8131 (prop (and (equal (char-before beg1) ?:)
8132 (not (equal (char-after (point-at-bol)) ?*))))
8133 (texp (equal (char-before beg) ?\\))
8134 (link (equal (char-before beg) ?\[))
8135 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
8136 beg)
8137 "#+"))
8138 (startup (string-match "^#\\+STARTUP:.*"
8139 (buffer-substring (point-at-bol) (point))))
8140 (completion-ignore-case opt)
8141 (type nil)
8142 (tbl nil)
8143 (table (cond
8144 (opt
8145 (setq type :opt)
8146 (require 'org-exp)
8147 (append
8148 (mapcar
8149 (lambda (x)
8150 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
8151 (cons (match-string 2 x) (match-string 1 x)))
8152 (org-split-string (org-get-current-options) "\n"))
8153 (mapcar 'list org-additional-option-like-keywords)))
8154 (startup
8155 (setq type :startup)
8156 org-startup-options)
8157 (link (append org-link-abbrev-alist-local
8158 org-link-abbrev-alist))
8159 (texp
8160 (setq type :tex)
8161 org-html-entities)
8162 ((string-match "\\`\\*+[ \t]+\\'"
8163 (buffer-substring (point-at-bol) beg))
8164 (setq type :todo)
8165 (mapcar 'list org-todo-keywords-1))
8166 (searchhead
8167 (setq type :searchhead)
8168 (save-excursion
8169 (goto-char (point-min))
8170 (while (re-search-forward org-todo-line-regexp nil t)
8171 (push (list
8172 (org-make-org-heading-search-string
8173 (match-string 3) t))
8174 tbl)))
8175 tbl)
8176 (tag (setq type :tag beg beg1)
8177 (or org-tag-alist (org-get-buffer-tags)))
8178 (prop (setq type :prop beg beg1)
8179 (mapcar 'list (org-buffer-property-keys nil t t)))
8180 (t (progn
8181 (call-interactively org-completion-fallback-command)
8182 (throw 'exit nil)))))
8183 (pattern (buffer-substring-no-properties beg end))
8184 (completion (try-completion pattern table confirm)))
8185 (cond ((eq completion t)
8186 (if (not (assoc (upcase pattern) table))
8187 (message "Already complete")
8188 (if (and (equal type :opt)
8189 (not (member (car (assoc (upcase pattern) table))
8190 org-additional-option-like-keywords)))
8191 (insert (substring (cdr (assoc (upcase pattern) table))
8192 (length pattern)))
8193 (if (memq type '(:tag :prop)) (insert ":")))))
8194 ((null completion)
8195 (message "Can't find completion for \"%s\"" pattern)
8196 (ding))
8197 ((not (string= pattern completion))
8198 (delete-region beg end)
8199 (if (string-match " +$" completion)
8200 (setq completion (replace-match "" t t completion)))
8201 (insert completion)
8202 (if (get-buffer-window "*Completions*")
8203 (delete-window (get-buffer-window "*Completions*")))
8204 (if (assoc completion table)
8205 (if (eq type :todo) (insert " ")
8206 (if (memq type '(:tag :prop)) (insert ":"))))
8207 (if (and (equal type :opt) (assoc completion table))
8208 (message "%s" (substitute-command-keys
8209 "Press \\[org-complete] again to insert example settings"))))
8211 (message "Making completion list...")
8212 (let ((list (sort (all-completions pattern table confirm)
8213 'string<)))
8214 (with-output-to-temp-buffer "*Completions*"
8215 (condition-case nil
8216 ;; Protection needed for XEmacs and emacs 21
8217 (display-completion-list list pattern)
8218 (error (display-completion-list list)))))
8219 (message "Making completion list...%s" "done")))))))
8221 ;;;; TODO, DEADLINE, Comments
8223 (defun org-toggle-comment ()
8224 "Change the COMMENT state of an entry."
8225 (interactive)
8226 (save-excursion
8227 (org-back-to-heading)
8228 (let (case-fold-search)
8229 (if (looking-at (concat outline-regexp
8230 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
8231 (replace-match "" t t nil 1)
8232 (if (looking-at outline-regexp)
8233 (progn
8234 (goto-char (match-end 0))
8235 (insert org-comment-string " ")))))))
8237 (defvar org-last-todo-state-is-todo nil
8238 "This is non-nil when the last TODO state change led to a TODO state.
8239 If the last change removed the TODO tag or switched to DONE, then
8240 this is nil.")
8242 (defvar org-setting-tags nil) ; dynamically skiped
8244 (defun org-parse-local-options (string var)
8245 "Parse STRING for startup setting relevant for variable VAR."
8246 (let ((rtn (symbol-value var))
8247 e opts)
8248 (save-match-data
8249 (if (or (not string) (not (string-match "\\S-" string)))
8251 (setq opts (delq nil (mapcar (lambda (x)
8252 (setq e (assoc x org-startup-options))
8253 (if (eq (nth 1 e) var) e nil))
8254 (org-split-string string "[ \t]+"))))
8255 (if (not opts)
8257 (setq rtn nil)
8258 (while (setq e (pop opts))
8259 (if (not (nth 3 e))
8260 (setq rtn (nth 2 e))
8261 (if (not (listp rtn)) (setq rtn nil))
8262 (push (nth 2 e) rtn)))
8263 rtn)))))
8265 (defvar org-blocker-hook nil
8266 "Hook for functions that are allowed to block a state change.
8268 Each function gets as its single argument a property list, see
8269 `org-trigger-hook' for more information about this list.
8271 If any of the functions in this hook returns nil, the state change
8272 is blocked.")
8274 (defvar org-trigger-hook nil
8275 "Hook for functions that are triggered by a state change.
8277 Each function gets as its single argument a property list with at least
8278 the following elements:
8280 (:type type-of-change :position pos-at-entry-start
8281 :from old-state :to new-state)
8283 Depending on the type, more properties may be present.
8285 This mechanism is currently implemented for:
8287 TODO state changes
8288 ------------------
8289 :type todo-state-change
8290 :from previous state (keyword as a string), or nil
8291 :to new state (keyword as a string), or nil")
8294 (defun org-todo (&optional arg)
8295 "Change the TODO state of an item.
8296 The state of an item is given by a keyword at the start of the heading,
8297 like
8298 *** TODO Write paper
8299 *** DONE Call mom
8301 The different keywords are specified in the variable `org-todo-keywords'.
8302 By default the available states are \"TODO\" and \"DONE\".
8303 So for this example: when the item starts with TODO, it is changed to DONE.
8304 When it starts with DONE, the DONE is removed. And when neither TODO nor
8305 DONE are present, add TODO at the beginning of the heading.
8307 With C-u prefix arg, use completion to determine the new state.
8308 With numeric prefix arg, switch to that state.
8310 For calling through lisp, arg is also interpreted in the following way:
8311 'none -> empty state
8312 \"\"(empty string) -> switch to empty state
8313 'done -> switch to DONE
8314 'nextset -> switch to the next set of keywords
8315 'previousset -> switch to the previous set of keywords
8316 \"WAITING\" -> switch to the specified keyword, but only if it
8317 really is a member of `org-todo-keywords'."
8318 (interactive "P")
8319 (save-excursion
8320 (catch 'exit
8321 (org-back-to-heading)
8322 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
8323 (or (looking-at (concat " +" org-todo-regexp " *"))
8324 (looking-at " *"))
8325 (let* ((match-data (match-data))
8326 (startpos (point-at-bol))
8327 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
8328 (org-log-done org-log-done)
8329 (org-log-repeat org-log-repeat)
8330 (org-todo-log-states org-todo-log-states)
8331 (this (match-string 1))
8332 (hl-pos (match-beginning 0))
8333 (head (org-get-todo-sequence-head this))
8334 (ass (assoc head org-todo-kwd-alist))
8335 (interpret (nth 1 ass))
8336 (done-word (nth 3 ass))
8337 (final-done-word (nth 4 ass))
8338 (last-state (or this ""))
8339 (completion-ignore-case t)
8340 (member (member this org-todo-keywords-1))
8341 (tail (cdr member))
8342 (state (cond
8343 ((and org-todo-key-trigger
8344 (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
8345 (and (not arg) org-use-fast-todo-selection
8346 (not (eq org-use-fast-todo-selection 'prefix)))))
8347 ;; Use fast selection
8348 (org-fast-todo-selection))
8349 ((and (equal arg '(4))
8350 (or (not org-use-fast-todo-selection)
8351 (not org-todo-key-trigger)))
8352 ;; Read a state with completion
8353 (completing-read "State: " (mapcar (lambda(x) (list x))
8354 org-todo-keywords-1)
8355 nil t))
8356 ((eq arg 'right)
8357 (if this
8358 (if tail (car tail) nil)
8359 (car org-todo-keywords-1)))
8360 ((eq arg 'left)
8361 (if (equal member org-todo-keywords-1)
8363 (if this
8364 (nth (- (length org-todo-keywords-1) (length tail) 2)
8365 org-todo-keywords-1)
8366 (org-last org-todo-keywords-1))))
8367 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
8368 (setq arg nil))) ; hack to fall back to cycling
8369 (arg
8370 ;; user or caller requests a specific state
8371 (cond
8372 ((equal arg "") nil)
8373 ((eq arg 'none) nil)
8374 ((eq arg 'done) (or done-word (car org-done-keywords)))
8375 ((eq arg 'nextset)
8376 (or (car (cdr (member head org-todo-heads)))
8377 (car org-todo-heads)))
8378 ((eq arg 'previousset)
8379 (let ((org-todo-heads (reverse org-todo-heads)))
8380 (or (car (cdr (member head org-todo-heads)))
8381 (car org-todo-heads))))
8382 ((car (member arg org-todo-keywords-1)))
8383 ((nth (1- (prefix-numeric-value arg))
8384 org-todo-keywords-1))))
8385 ((null member) (or head (car org-todo-keywords-1)))
8386 ((equal this final-done-word) nil) ;; -> make empty
8387 ((null tail) nil) ;; -> first entry
8388 ((eq interpret 'sequence)
8389 (car tail))
8390 ((memq interpret '(type priority))
8391 (if (eq this-command last-command)
8392 (car tail)
8393 (if (> (length tail) 0)
8394 (or done-word (car org-done-keywords))
8395 nil)))
8396 (t nil)))
8397 (next (if state (concat " " state " ") " "))
8398 (change-plist (list :type 'todo-state-change :from this :to state
8399 :position startpos))
8400 dolog now-done-p)
8401 (when org-blocker-hook
8402 (unless (save-excursion
8403 (save-match-data
8404 (run-hook-with-args-until-failure
8405 'org-blocker-hook change-plist)))
8406 (if (interactive-p)
8407 (error "TODO state change from %s to %s blocked" this state)
8408 ;; fail silently
8409 (message "TODO state change from %s to %s blocked" this state)
8410 (throw 'exit nil))))
8411 (store-match-data match-data)
8412 (replace-match next t t)
8413 (unless (pos-visible-in-window-p hl-pos)
8414 (message "TODO state changed to %s" (org-trim next)))
8415 (unless head
8416 (setq head (org-get-todo-sequence-head state)
8417 ass (assoc head org-todo-kwd-alist)
8418 interpret (nth 1 ass)
8419 done-word (nth 3 ass)
8420 final-done-word (nth 4 ass)))
8421 (when (memq arg '(nextset previousset))
8422 (message "Keyword-Set %d/%d: %s"
8423 (- (length org-todo-sets) -1
8424 (length (memq (assoc state org-todo-sets) org-todo-sets)))
8425 (length org-todo-sets)
8426 (mapconcat 'identity (assoc state org-todo-sets) " ")))
8427 (setq org-last-todo-state-is-todo
8428 (not (member state org-done-keywords)))
8429 (setq now-done-p (and (member state org-done-keywords)
8430 (not (member this org-done-keywords))))
8431 (and logging (org-local-logging logging))
8432 (when (and (or org-todo-log-states org-log-done)
8433 (not (memq arg '(nextset previousset))))
8434 ;; we need to look at recording a time and note
8435 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
8436 (nth 2 (assoc this org-todo-log-states))))
8437 (when (and state
8438 (member state org-not-done-keywords)
8439 (not (member this org-not-done-keywords)))
8440 ;; This is now a todo state and was not one before
8441 ;; If there was a CLOSED time stamp, get rid of it.
8442 (org-add-planning-info nil nil 'closed))
8443 (when (and now-done-p org-log-done)
8444 ;; It is now done, and it was not done before
8445 (org-add-planning-info 'closed (org-current-time))
8446 (if (and (not dolog) (eq 'note org-log-done))
8447 (org-add-log-setup 'done state 'findpos 'note)))
8448 (when (and state dolog)
8449 ;; This is a non-nil state, and we need to log it
8450 (org-add-log-setup 'state state 'findpos dolog)))
8451 ;; Fixup tag positioning
8452 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
8453 (when org-provide-todo-statistics
8454 (org-update-parent-todo-statistics))
8455 (run-hooks 'org-after-todo-state-change-hook)
8456 (if (and arg (not (member state org-done-keywords)))
8457 (setq head (org-get-todo-sequence-head state)))
8458 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
8459 ;; Do we need to trigger a repeat?
8460 (when now-done-p (org-auto-repeat-maybe state))
8461 ;; Fixup cursor location if close to the keyword
8462 (if (and (outline-on-heading-p)
8463 (not (bolp))
8464 (save-excursion (beginning-of-line 1)
8465 (looking-at org-todo-line-regexp))
8466 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
8467 (progn
8468 (goto-char (or (match-end 2) (match-end 1)))
8469 (just-one-space)))
8470 (when org-trigger-hook
8471 (save-excursion
8472 (run-hook-with-args 'org-trigger-hook change-plist)))))))
8474 (defun org-update-parent-todo-statistics ()
8475 "Update any statistics cookie in the parent of the current headline."
8476 (interactive)
8477 (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
8478 level (cnt-all 0) (cnt-done 0) is-percent kwd)
8479 (catch 'exit
8480 (save-excursion
8481 (setq level (org-up-heading-safe))
8482 (unless (and level
8483 (re-search-forward box-re (point-at-eol) t))
8484 (throw 'exit nil))
8485 (setq is-percent (match-end 2))
8486 (save-match-data
8487 (unless (outline-next-heading) (throw 'exit nil))
8488 (while (looking-at org-todo-line-regexp)
8489 (setq kwd (match-string 2))
8490 (and kwd (setq cnt-all (1+ cnt-all)))
8491 (and (member kwd org-done-keywords)
8492 (setq cnt-done (1+ cnt-done)))
8493 (condition-case nil
8494 (outline-forward-same-level 1)
8495 (error (end-of-line 1)))))
8496 (replace-match
8497 (if is-percent
8498 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
8499 (format "[%d/%d]" cnt-done cnt-all)))
8500 (run-hook-with-args 'org-after-todo-statistics-hook
8501 cnt-done (- cnt-all cnt-done))))))
8503 (defvar org-after-todo-statistics-hook nil
8504 "Hook that is called after a TODO statistics cookie has been updated.
8505 Each function is called with two arguments: the number of not-done entries
8506 and the number of done entries.
8508 For example, the following function, when added to this hook, will switch
8509 an entry to DONE when all children are done, and back to TODO when new
8510 entries are set to a TODO status. Note that this hook is only called
8511 when there is a statistics cookie in the headline!
8513 (defun org-summary-todo (n-done n-not-done)
8514 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
8515 (let (org-log-done org-log-states) ; turn off logging
8516 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
8519 (defun org-local-logging (value)
8520 "Get logging settings from a property VALUE."
8521 (let* (words w a)
8522 ;; directly set the variables, they are already local.
8523 (setq org-log-done nil
8524 org-log-repeat nil
8525 org-todo-log-states nil)
8526 (setq words (org-split-string value))
8527 (while (setq w (pop words))
8528 (cond
8529 ((setq a (assoc w org-startup-options))
8530 (and (member (nth 1 a) '(org-log-done org-log-repeat))
8531 (set (nth 1 a) (nth 2 a))))
8532 ((setq a (org-extract-log-state-settings w))
8533 (and (member (car a) org-todo-keywords-1)
8534 (push a org-todo-log-states)))))))
8536 (defun org-get-todo-sequence-head (kwd)
8537 "Return the head of the TODO sequence to which KWD belongs.
8538 If KWD is not set, check if there is a text property remembering the
8539 right sequence."
8540 (let (p)
8541 (cond
8542 ((not kwd)
8543 (or (get-text-property (point-at-bol) 'org-todo-head)
8544 (progn
8545 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
8546 nil (point-at-eol)))
8547 (get-text-property p 'org-todo-head))))
8548 ((not (member kwd org-todo-keywords-1))
8549 (car org-todo-keywords-1))
8550 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
8552 (defun org-fast-todo-selection ()
8553 "Fast TODO keyword selection with single keys.
8554 Returns the new TODO keyword, or nil if no state change should occur."
8555 (let* ((fulltable org-todo-key-alist)
8556 (done-keywords org-done-keywords) ;; needed for the faces.
8557 (maxlen (apply 'max (mapcar
8558 (lambda (x)
8559 (if (stringp (car x)) (string-width (car x)) 0))
8560 fulltable)))
8561 (expert nil)
8562 (fwidth (+ maxlen 3 1 3))
8563 (ncol (/ (- (window-width) 4) fwidth))
8564 tg cnt e c tbl
8565 groups ingroup)
8566 (save-window-excursion
8567 (if expert
8568 (set-buffer (get-buffer-create " *Org todo*"))
8569 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
8570 (erase-buffer)
8571 (org-set-local 'org-done-keywords done-keywords)
8572 (setq tbl fulltable cnt 0)
8573 (while (setq e (pop tbl))
8574 (cond
8575 ((equal e '(:startgroup))
8576 (push '() groups) (setq ingroup t)
8577 (when (not (= cnt 0))
8578 (setq cnt 0)
8579 (insert "\n"))
8580 (insert "{ "))
8581 ((equal e '(:endgroup))
8582 (setq ingroup nil cnt 0)
8583 (insert "}\n"))
8585 (setq tg (car e) c (cdr e))
8586 (if ingroup (push tg (car groups)))
8587 (setq tg (org-add-props tg nil 'face
8588 (org-get-todo-face tg)))
8589 (if (and (= cnt 0) (not ingroup)) (insert " "))
8590 (insert "[" c "] " tg (make-string
8591 (- fwidth 4 (length tg)) ?\ ))
8592 (when (= (setq cnt (1+ cnt)) ncol)
8593 (insert "\n")
8594 (if ingroup (insert " "))
8595 (setq cnt 0)))))
8596 (insert "\n")
8597 (goto-char (point-min))
8598 (if (and (not expert) (fboundp 'fit-window-to-buffer))
8599 (fit-window-to-buffer))
8600 (message "[a-z..]:Set [SPC]:clear")
8601 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
8602 (cond
8603 ((or (= c ?\C-g)
8604 (and (= c ?q) (not (rassoc c fulltable))))
8605 (setq quit-flag t))
8606 ((= c ?\ ) nil)
8607 ((setq e (rassoc c fulltable) tg (car e))
8609 (t (setq quit-flag t))))))
8611 (defun org-entry-is-todo-p ()
8612 (member (org-get-todo-state) org-not-done-keywords))
8614 (defun org-entry-is-done-p ()
8615 (member (org-get-todo-state) org-done-keywords))
8617 (defun org-get-todo-state ()
8618 (save-excursion
8619 (org-back-to-heading t)
8620 (and (looking-at org-todo-line-regexp)
8621 (match-end 2)
8622 (match-string 2))))
8624 (defun org-at-date-range-p (&optional inactive-ok)
8625 "Is the cursor inside a date range?"
8626 (interactive)
8627 (save-excursion
8628 (catch 'exit
8629 (let ((pos (point)))
8630 (skip-chars-backward "^[<\r\n")
8631 (skip-chars-backward "<[")
8632 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
8633 (>= (match-end 0) pos)
8634 (throw 'exit t))
8635 (skip-chars-backward "^<[\r\n")
8636 (skip-chars-backward "<[")
8637 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
8638 (>= (match-end 0) pos)
8639 (throw 'exit t)))
8640 nil)))
8642 (defun org-get-repeat ()
8643 "Check if there is a deadline/schedule with repeater in this entry."
8644 (save-match-data
8645 (save-excursion
8646 (org-back-to-heading t)
8647 (if (re-search-forward
8648 org-repeat-re (save-excursion (outline-next-heading) (point)) t)
8649 (match-string 1)))))
8651 (defvar org-last-changed-timestamp)
8652 (defvar org-last-inserted-timestamp)
8653 (defvar org-log-post-message)
8654 (defvar org-log-note-purpose)
8655 (defvar org-log-note-how)
8656 (defun org-auto-repeat-maybe (done-word)
8657 "Check if the current headline contains a repeated deadline/schedule.
8658 If yes, set TODO state back to what it was and change the base date
8659 of repeating deadline/scheduled time stamps to new date.
8660 This function is run automatically after each state change to a DONE state."
8661 ;; last-state is dynamically scoped into this function
8662 (let* ((repeat (org-get-repeat))
8663 (aa (assoc last-state org-todo-kwd-alist))
8664 (interpret (nth 1 aa))
8665 (head (nth 2 aa))
8666 (whata '(("d" . day) ("m" . month) ("y" . year)))
8667 (msg "Entry repeats: ")
8668 (org-log-done nil)
8669 (org-todo-log-states nil)
8670 (nshiftmax 10) (nshift 0)
8671 re type n what ts mb0 time)
8672 (when repeat
8673 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
8674 (org-todo (if (eq interpret 'type) last-state head))
8675 (when org-log-repeat
8676 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
8677 (memq 'org-add-log-note post-command-hook))
8678 ;; OK, we are already setup for some record
8679 (if (eq org-log-repeat 'note)
8680 ;; make sure we take a note, not only a time stamp
8681 (setq org-log-note-how 'note))
8682 ;; Set up for taking a record
8683 (org-add-log-setup 'state (or done-word (car org-done-keywords))
8684 'findpos org-log-repeat)))
8685 (org-back-to-heading t)
8686 (org-add-planning-info nil nil 'closed)
8687 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
8688 org-deadline-time-regexp "\\)\\|\\("
8689 org-ts-regexp "\\)"))
8690 (while (re-search-forward
8691 re (save-excursion (outline-next-heading) (point)) t)
8692 (setq type (if (match-end 1) org-scheduled-string
8693 (if (match-end 3) org-deadline-string "Plain:"))
8694 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
8695 mb0 (match-beginning 0))
8696 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
8697 (setq n (string-to-number (match-string 2 ts))
8698 what (match-string 3 ts))
8699 (if (equal what "w") (setq n (* n 7) what "d"))
8700 ;; Preparation, see if we need to modify the start date for the change
8701 (when (match-end 1)
8702 (setq time (save-match-data (org-time-string-to-time ts)))
8703 (cond
8704 ((equal (match-string 1 ts) ".")
8705 ;; Shift starting date to today
8706 (org-timestamp-change
8707 (- (time-to-days (current-time)) (time-to-days time))
8708 'day))
8709 ((equal (match-string 1 ts) "+")
8710 (while (or (= nshift 0)
8711 (<= (time-to-days time) (time-to-days (current-time))))
8712 (when (= (incf nshift) nshiftmax)
8713 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
8714 (error "Abort")))
8715 (org-timestamp-change n (cdr (assoc what whata)))
8716 (org-at-timestamp-p t)
8717 (setq ts (match-string 1))
8718 (setq time (save-match-data (org-time-string-to-time ts))))
8719 (org-timestamp-change (- n) (cdr (assoc what whata)))
8720 ;; rematch, so that we have everything in place for the real shift
8721 (org-at-timestamp-p t)
8722 (setq ts (match-string 1))
8723 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
8724 (org-timestamp-change n (cdr (assoc what whata)))
8725 (setq msg (concat msg type org-last-changed-timestamp " "))))
8726 (setq org-log-post-message msg)
8727 (message "%s" msg))))
8729 (defun org-show-todo-tree (arg)
8730 "Make a compact tree which shows all headlines marked with TODO.
8731 The tree will show the lines where the regexp matches, and all higher
8732 headlines above the match.
8733 With a \\[universal-argument] prefix, also show the DONE entries.
8734 With a numeric prefix N, construct a sparse tree for the Nth element
8735 of `org-todo-keywords-1'."
8736 (interactive "P")
8737 (let ((case-fold-search nil)
8738 (kwd-re
8739 (cond ((null arg) org-not-done-regexp)
8740 ((equal arg '(4))
8741 (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
8742 (mapcar 'list org-todo-keywords-1))))
8743 (concat "\\("
8744 (mapconcat 'identity (org-split-string kwd "|") "\\|")
8745 "\\)\\>")))
8746 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
8747 (regexp-quote (nth (1- (prefix-numeric-value arg))
8748 org-todo-keywords-1)))
8749 (t (error "Invalid prefix argument: %s" arg)))))
8750 (message "%d TODO entries found"
8751 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
8753 (defun org-deadline (&optional remove time)
8754 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
8755 With argument REMOVE, remove any deadline from the item.
8756 When TIME is set, it should be an internal time specification, and the
8757 scheduling will use the corresponding date."
8758 (interactive "P")
8759 (if remove
8760 (progn
8761 (org-remove-timestamp-with-keyword org-deadline-string)
8762 (message "Item no longer has a deadline."))
8763 (if (org-get-repeat)
8764 (error "Cannot change deadline on task with repeater, please do that by hand")
8765 (org-add-planning-info 'deadline time 'closed)
8766 (message "Deadline on %s" org-last-inserted-timestamp))))
8768 (defun org-schedule (&optional remove time)
8769 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
8770 With argument REMOVE, remove any scheduling date from the item.
8771 When TIME is set, it should be an internal time specification, and the
8772 scheduling will use the corresponding date."
8773 (interactive "P")
8774 (if remove
8775 (progn
8776 (org-remove-timestamp-with-keyword org-scheduled-string)
8777 (message "Item is no longer scheduled."))
8778 (if (org-get-repeat)
8779 (error "Cannot reschedule task with repeater, please do that by hand")
8780 (org-add-planning-info 'scheduled time 'closed)
8781 (message "Scheduled to %s" org-last-inserted-timestamp))))
8783 (defun org-remove-timestamp-with-keyword (keyword)
8784 "Remove all time stamps with KEYWORD in the current entry."
8785 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
8786 beg)
8787 (save-excursion
8788 (org-back-to-heading t)
8789 (setq beg (point))
8790 (org-end-of-subtree t t)
8791 (while (re-search-backward re beg t)
8792 (replace-match "")
8793 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
8794 (equal (char-before) ?\ ))
8795 (backward-delete-char 1)
8796 (if (string-match "^[ \t]*$" (buffer-substring
8797 (point-at-bol) (point-at-eol)))
8798 (delete-region (point-at-bol)
8799 (min (point-max) (1+ (point-at-eol))))))))))
8801 (defun org-add-planning-info (what &optional time &rest remove)
8802 "Insert new timestamp with keyword in the line directly after the headline.
8803 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
8804 If non is given, the user is prompted for a date.
8805 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
8806 be removed."
8807 (interactive)
8808 (let (org-time-was-given org-end-time-was-given ts
8809 end default-time default-input)
8811 (when (and (not time) (memq what '(scheduled deadline)))
8812 ;; Try to get a default date/time from existing timestamp
8813 (save-excursion
8814 (org-back-to-heading t)
8815 (setq end (save-excursion (outline-next-heading) (point)))
8816 (when (re-search-forward (if (eq what 'scheduled)
8817 org-scheduled-time-regexp
8818 org-deadline-time-regexp)
8819 end t)
8820 (setq ts (match-string 1)
8821 default-time
8822 (apply 'encode-time (org-parse-time-string ts))
8823 default-input (and ts (org-get-compact-tod ts))))))
8824 (when what
8825 ;; If necessary, get the time from the user
8826 (setq time (or time (org-read-date nil 'to-time nil nil
8827 default-time default-input))))
8829 (when (and org-insert-labeled-timestamps-at-point
8830 (member what '(scheduled deadline)))
8831 (insert
8832 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
8833 (org-insert-time-stamp time org-time-was-given
8834 nil nil nil (list org-end-time-was-given))
8835 (setq what nil))
8836 (save-excursion
8837 (save-restriction
8838 (let (col list elt ts buffer-invisibility-spec)
8839 (org-back-to-heading t)
8840 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
8841 (goto-char (match-end 1))
8842 (setq col (current-column))
8843 (goto-char (match-end 0))
8844 (if (eobp) (insert "\n") (forward-char 1))
8845 (if (and (not (looking-at outline-regexp))
8846 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
8847 "[^\r\n]*"))
8848 (not (equal (match-string 1) org-clock-string)))
8849 (narrow-to-region (match-beginning 0) (match-end 0))
8850 (insert-before-markers "\n")
8851 (backward-char 1)
8852 (narrow-to-region (point) (point))
8853 (and org-adapt-indentation (org-indent-to-column col)))
8854 ;; Check if we have to remove something.
8855 (setq list (cons what remove))
8856 (while list
8857 (setq elt (pop list))
8858 (goto-char (point-min))
8859 (when (or (and (eq elt 'scheduled)
8860 (re-search-forward org-scheduled-time-regexp nil t))
8861 (and (eq elt 'deadline)
8862 (re-search-forward org-deadline-time-regexp nil t))
8863 (and (eq elt 'closed)
8864 (re-search-forward org-closed-time-regexp nil t)))
8865 (replace-match "")
8866 (if (looking-at "--+<[^>]+>") (replace-match ""))
8867 (if (looking-at " +") (replace-match ""))))
8868 (goto-char (point-max))
8869 (when what
8870 (insert
8871 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
8872 (cond ((eq what 'scheduled) org-scheduled-string)
8873 ((eq what 'deadline) org-deadline-string)
8874 ((eq what 'closed) org-closed-string))
8875 " ")
8876 (setq ts (org-insert-time-stamp
8877 time
8878 (or org-time-was-given
8879 (and (eq what 'closed) org-log-done-with-time))
8880 (eq what 'closed)
8881 nil nil (list org-end-time-was-given)))
8882 (end-of-line 1))
8883 (goto-char (point-min))
8884 (widen)
8885 (if (and (looking-at "[ \t]+\n")
8886 (equal (char-before) ?\n))
8887 (delete-region (1- (point)) (point-at-eol)))
8888 ts)))))
8890 (defvar org-log-note-marker (make-marker))
8891 (defvar org-log-note-purpose nil)
8892 (defvar org-log-note-state nil)
8893 (defvar org-log-note-how nil)
8894 (defvar org-log-note-window-configuration nil)
8895 (defvar org-log-note-return-to (make-marker))
8896 (defvar org-log-post-message nil
8897 "Message to be displayed after a log note has been stored.
8898 The auto-repeater uses this.")
8900 (defun org-add-note ()
8901 "Add a note to the current entry.
8902 This is done in the same way as adding a state change note."
8903 (interactive)
8904 (org-add-log-setup 'note nil t nil))
8906 (defun org-add-log-setup (&optional purpose state findpos how)
8907 "Set up the post command hook to take a note.
8908 If this is about to TODO state change, the new state is expected in STATE.
8909 When FINDPOS is non-nil, find the correct position for the note in
8910 the current entry. If not, assume that it can be inserted at point."
8911 (save-excursion
8912 (when findpos
8913 (org-back-to-heading t)
8914 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
8915 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
8916 "[^\r\n]*\\)?"))
8917 (goto-char (match-end 0))
8918 (unless org-log-states-order-reversed
8919 (and (= (char-after) ?\n) (forward-char 1))
8920 (org-skip-over-state-notes)
8921 (skip-chars-backward " \t\n\r")))
8922 (move-marker org-log-note-marker (point))
8923 (setq org-log-note-purpose purpose
8924 org-log-note-state state
8925 org-log-note-how how)
8926 (add-hook 'post-command-hook 'org-add-log-note 'append)))
8928 (defun org-skip-over-state-notes ()
8929 "Skip past the list of State notes in an entry."
8930 (if (looking-at "\n[ \t]*- State") (forward-char 1))
8931 (while (looking-at "[ \t]*- State")
8932 (condition-case nil
8933 (org-next-item)
8934 (error (org-end-of-item)))))
8936 (defun org-add-log-note (&optional purpose)
8937 "Pop up a window for taking a note, and add this note later at point."
8938 (remove-hook 'post-command-hook 'org-add-log-note)
8939 (setq org-log-note-window-configuration (current-window-configuration))
8940 (delete-other-windows)
8941 (move-marker org-log-note-return-to (point))
8942 (switch-to-buffer (marker-buffer org-log-note-marker))
8943 (goto-char org-log-note-marker)
8944 (org-switch-to-buffer-other-window "*Org Note*")
8945 (erase-buffer)
8946 (if (memq org-log-note-how '(time state))
8947 (org-store-log-note)
8948 (let ((org-inhibit-startup t)) (org-mode))
8949 (insert (format "# Insert note for %s.
8950 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
8951 (cond
8952 ((eq org-log-note-purpose 'clock-out) "stopped clock")
8953 ((eq org-log-note-purpose 'done) "closed todo item")
8954 ((eq org-log-note-purpose 'state)
8955 (format "state change to \"%s\"" org-log-note-state))
8956 ((eq org-log-note-purpose 'note)
8957 "this entry")
8958 (t (error "This should not happen")))))
8959 (org-set-local 'org-finish-function 'org-store-log-note)))
8961 (defvar org-note-abort nil) ; dynamically scoped
8962 (defun org-store-log-note ()
8963 "Finish taking a log note, and insert it to where it belongs."
8964 (let ((txt (buffer-string))
8965 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
8966 lines ind)
8967 (kill-buffer (current-buffer))
8968 (while (string-match "\\`#.*\n[ \t\n]*" txt)
8969 (setq txt (replace-match "" t t txt)))
8970 (if (string-match "\\s-+\\'" txt)
8971 (setq txt (replace-match "" t t txt)))
8972 (setq lines (org-split-string txt "\n"))
8973 (when (and note (string-match "\\S-" note))
8974 (setq note
8975 (org-replace-escapes
8976 note
8977 (list (cons "%u" (user-login-name))
8978 (cons "%U" user-full-name)
8979 (cons "%t" (format-time-string
8980 (org-time-stamp-format 'long 'inactive)
8981 (current-time)))
8982 (cons "%s" (if org-log-note-state
8983 (concat "\"" org-log-note-state "\"")
8984 "")))))
8985 (if lines (setq note (concat note " \\\\")))
8986 (push note lines))
8987 (when (or current-prefix-arg org-note-abort) (setq lines nil))
8988 (when lines
8989 (save-excursion
8990 (set-buffer (marker-buffer org-log-note-marker))
8991 (save-excursion
8992 (goto-char org-log-note-marker)
8993 (move-marker org-log-note-marker nil)
8994 (end-of-line 1)
8995 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
8996 (indent-relative nil)
8997 (insert "- " (pop lines))
8998 (org-indent-line-function)
8999 (beginning-of-line 1)
9000 (looking-at "[ \t]*")
9001 (setq ind (concat (match-string 0) " "))
9002 (end-of-line 1)
9003 (while lines (insert "\n" ind (pop lines)))))))
9004 (set-window-configuration org-log-note-window-configuration)
9005 (with-current-buffer (marker-buffer org-log-note-return-to)
9006 (goto-char org-log-note-return-to))
9007 (move-marker org-log-note-return-to nil)
9008 (and org-log-post-message (message "%s" org-log-post-message)))
9010 (defun org-sparse-tree (&optional arg)
9011 "Create a sparse tree, prompt for the details.
9012 This command can create sparse trees. You first need to select the type
9013 of match used to create the tree:
9015 t Show entries with a specific TODO keyword.
9016 T Show entries selected by a tags match.
9017 p Enter a property name and its value (both with completion on existing
9018 names/values) and show entries with that property.
9019 r Show entries matching a regular expression
9020 d Show deadlines due within `org-deadline-warning-days'."
9021 (interactive "P")
9022 (let (ans kwd value)
9023 (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
9024 (setq ans (read-char-exclusive))
9025 (cond
9026 ((equal ans ?d)
9027 (call-interactively 'org-check-deadlines))
9028 ((equal ans ?b)
9029 (call-interactively 'org-check-before-date))
9030 ((equal ans ?t)
9031 (org-show-todo-tree '(4)))
9032 ((equal ans ?T)
9033 (call-interactively 'org-tags-sparse-tree))
9034 ((member ans '(?p ?P))
9035 (setq kwd (completing-read "Property: "
9036 (mapcar 'list (org-buffer-property-keys))))
9037 (setq value (completing-read "Value: "
9038 (mapcar 'list (org-property-values kwd))))
9039 (unless (string-match "\\`{.*}\\'" value)
9040 (setq value (concat "\"" value "\"")))
9041 (org-tags-sparse-tree arg (concat kwd "=" value)))
9042 ((member ans '(?r ?R ?/))
9043 (call-interactively 'org-occur))
9044 (t (error "No such sparse tree command \"%c\"" ans)))))
9046 (defvar org-occur-highlights nil
9047 "List of overlays used for occur matches.")
9048 (make-variable-buffer-local 'org-occur-highlights)
9049 (defvar org-occur-parameters nil
9050 "Parameters of the active org-occur calls.
9051 This is a list, each call to org-occur pushes as cons cell,
9052 containing the regular expression and the callback, onto the list.
9053 The list can contain several entries if `org-occur' has been called
9054 several time with the KEEP-PREVIOUS argument. Otherwise, this list
9055 will only contain one set of parameters. When the highlights are
9056 removed (for example with `C-c C-c', or with the next edit (depending
9057 on `org-remove-highlights-with-change'), this variable is emptied
9058 as well.")
9059 (make-variable-buffer-local 'org-occur-parameters)
9061 (defun org-occur (regexp &optional keep-previous callback)
9062 "Make a compact tree which shows all matches of REGEXP.
9063 The tree will show the lines where the regexp matches, and all higher
9064 headlines above the match. It will also show the heading after the match,
9065 to make sure editing the matching entry is easy.
9066 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
9067 call to `org-occur' will be kept, to allow stacking of calls to this
9068 command.
9069 If CALLBACK is non-nil, it is a function which is called to confirm
9070 that the match should indeed be shown."
9071 (interactive "sRegexp: \nP")
9072 (unless keep-previous
9073 (org-remove-occur-highlights nil nil t))
9074 (push (cons regexp callback) org-occur-parameters)
9075 (let ((cnt 0))
9076 (save-excursion
9077 (goto-char (point-min))
9078 (if (or (not keep-previous) ; do not want to keep
9079 (not org-occur-highlights)) ; no previous matches
9080 ;; hide everything
9081 (org-overview))
9082 (while (re-search-forward regexp nil t)
9083 (when (or (not callback)
9084 (save-match-data (funcall callback)))
9085 (setq cnt (1+ cnt))
9086 (when org-highlight-sparse-tree-matches
9087 (org-highlight-new-match (match-beginning 0) (match-end 0)))
9088 (org-show-context 'occur-tree))))
9089 (when org-remove-highlights-with-change
9090 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
9091 nil 'local))
9092 (unless org-sparse-tree-open-archived-trees
9093 (org-hide-archived-subtrees (point-min) (point-max)))
9094 (run-hooks 'org-occur-hook)
9095 (if (interactive-p)
9096 (message "%d match(es) for regexp %s" cnt regexp))
9097 cnt))
9099 (defun org-show-context (&optional key)
9100 "Make sure point and context and visible.
9101 How much context is shown depends upon the variables
9102 `org-show-hierarchy-above', `org-show-following-heading'. and
9103 `org-show-siblings'."
9104 (let ((heading-p (org-on-heading-p t))
9105 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
9106 (following-p (org-get-alist-option org-show-following-heading key))
9107 (entry-p (org-get-alist-option org-show-entry-below key))
9108 (siblings-p (org-get-alist-option org-show-siblings key)))
9109 (catch 'exit
9110 ;; Show heading or entry text
9111 (if (and heading-p (not entry-p))
9112 (org-flag-heading nil) ; only show the heading
9113 (and (or entry-p (org-invisible-p) (org-invisible-p2))
9114 (org-show-hidden-entry))) ; show entire entry
9115 (when following-p
9116 ;; Show next sibling, or heading below text
9117 (save-excursion
9118 (and (if heading-p (org-goto-sibling) (outline-next-heading))
9119 (org-flag-heading nil))))
9120 (when siblings-p (org-show-siblings))
9121 (when hierarchy-p
9122 ;; show all higher headings, possibly with siblings
9123 (save-excursion
9124 (while (and (condition-case nil
9125 (progn (org-up-heading-all 1) t)
9126 (error nil))
9127 (not (bobp)))
9128 (org-flag-heading nil)
9129 (when siblings-p (org-show-siblings))))))))
9131 (defun org-reveal (&optional siblings)
9132 "Show current entry, hierarchy above it, and the following headline.
9133 This can be used to show a consistent set of context around locations
9134 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
9135 not t for the search context.
9137 With optional argument SIBLINGS, on each level of the hierarchy all
9138 siblings are shown. This repairs the tree structure to what it would
9139 look like when opened with hierarchical calls to `org-cycle'."
9140 (interactive "P")
9141 (let ((org-show-hierarchy-above t)
9142 (org-show-following-heading t)
9143 (org-show-siblings (if siblings t org-show-siblings)))
9144 (org-show-context nil)))
9146 (defun org-highlight-new-match (beg end)
9147 "Highlight from BEG to END and mark the highlight is an occur headline."
9148 (let ((ov (org-make-overlay beg end)))
9149 (org-overlay-put ov 'face 'secondary-selection)
9150 (push ov org-occur-highlights)))
9152 (defun org-remove-occur-highlights (&optional beg end noremove)
9153 "Remove the occur highlights from the buffer.
9154 BEG and END are ignored. If NOREMOVE is nil, remove this function
9155 from the `before-change-functions' in the current buffer."
9156 (interactive)
9157 (unless org-inhibit-highlight-removal
9158 (mapc 'org-delete-overlay org-occur-highlights)
9159 (setq org-occur-highlights nil)
9160 (setq org-occur-parameters nil)
9161 (unless noremove
9162 (remove-hook 'before-change-functions
9163 'org-remove-occur-highlights 'local))))
9165 ;;;; Priorities
9167 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
9168 "Regular expression matching the priority indicator.")
9170 (defvar org-remove-priority-next-time nil)
9172 (defun org-priority-up ()
9173 "Increase the priority of the current item."
9174 (interactive)
9175 (org-priority 'up))
9177 (defun org-priority-down ()
9178 "Decrease the priority of the current item."
9179 (interactive)
9180 (org-priority 'down))
9182 (defun org-priority (&optional action)
9183 "Change the priority of an item by ARG.
9184 ACTION can be `set', `up', `down', or a character."
9185 (interactive)
9186 (setq action (or action 'set))
9187 (let (current new news have remove)
9188 (save-excursion
9189 (org-back-to-heading)
9190 (if (looking-at org-priority-regexp)
9191 (setq current (string-to-char (match-string 2))
9192 have t)
9193 (setq current org-default-priority))
9194 (cond
9195 ((or (eq action 'set)
9196 (if (featurep 'xemacs) (characterp action) (integerp action)))
9197 (if (not (eq action 'set))
9198 (setq new action)
9199 (message "Priority %c-%c, SPC to remove: "
9200 org-highest-priority org-lowest-priority)
9201 (setq new (read-char-exclusive)))
9202 (if (and (= (upcase org-highest-priority) org-highest-priority)
9203 (= (upcase org-lowest-priority) org-lowest-priority))
9204 (setq new (upcase new)))
9205 (cond ((equal new ?\ ) (setq remove t))
9206 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
9207 (error "Priority must be between `%c' and `%c'"
9208 org-highest-priority org-lowest-priority))))
9209 ((eq action 'up)
9210 (if (and (not have) (eq last-command this-command))
9211 (setq new org-lowest-priority)
9212 (setq new (if (and org-priority-start-cycle-with-default (not have))
9213 org-default-priority (1- current)))))
9214 ((eq action 'down)
9215 (if (and (not have) (eq last-command this-command))
9216 (setq new org-highest-priority)
9217 (setq new (if (and org-priority-start-cycle-with-default (not have))
9218 org-default-priority (1+ current)))))
9219 (t (error "Invalid action")))
9220 (if (or (< (upcase new) org-highest-priority)
9221 (> (upcase new) org-lowest-priority))
9222 (setq remove t))
9223 (setq news (format "%c" new))
9224 (if have
9225 (if remove
9226 (replace-match "" t t nil 1)
9227 (replace-match news t t nil 2))
9228 (if remove
9229 (error "No priority cookie found in line")
9230 (looking-at org-todo-line-regexp)
9231 (if (match-end 2)
9232 (progn
9233 (goto-char (match-end 2))
9234 (insert " [#" news "]"))
9235 (goto-char (match-beginning 3))
9236 (insert "[#" news "] ")))))
9237 (org-preserve-lc (org-set-tags nil 'align))
9238 (if remove
9239 (message "Priority removed")
9240 (message "Priority of current item set to %s" news))))
9243 (defun org-get-priority (s)
9244 "Find priority cookie and return priority."
9245 (save-match-data
9246 (if (not (string-match org-priority-regexp s))
9247 (* 1000 (- org-lowest-priority org-default-priority))
9248 (* 1000 (- org-lowest-priority
9249 (string-to-char (match-string 2 s)))))))
9251 ;;;; Tags
9253 (defvar org-agenda-archives-mode)
9254 (defun org-scan-tags (action matcher &optional todo-only)
9255 "Scan headline tags with inheritance and produce output ACTION.
9257 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
9258 or `agenda' to produce an entry list for an agenda view. It can also be
9259 a Lisp form or a function that should be called at each matched headline, in
9260 this case the return value is a list of all return values from these calls.
9262 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
9263 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
9264 only lines with a TODO keyword are included in the output."
9265 (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
9266 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
9267 (org-re
9268 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
9269 (props (list 'face 'default
9270 'done-face 'org-done
9271 'undone-face 'default
9272 'mouse-face 'highlight
9273 'org-not-done-regexp org-not-done-regexp
9274 'org-todo-regexp org-todo-regexp
9275 'keymap org-agenda-keymap
9276 'help-echo
9277 (format "mouse-2 or RET jump to org file %s"
9278 (abbreviate-file-name
9279 (or (buffer-file-name (buffer-base-buffer))
9280 (buffer-name (buffer-base-buffer)))))))
9281 (case-fold-search nil)
9282 lspos tags tags-list
9283 (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
9284 (llast 0) rtn rtn1 level category i txt
9285 todo marker entry priority)
9286 (when (not (member action '(agenda sparse-tree)))
9287 (setq action (list 'lambda nil action)))
9288 (save-excursion
9289 (goto-char (point-min))
9290 (when (eq action 'sparse-tree)
9291 (org-overview)
9292 (org-remove-occur-highlights))
9293 (while (re-search-forward re nil t)
9294 (catch :skip
9295 (setq todo (if (match-end 1) (match-string 2))
9296 tags (if (match-end 4) (match-string 4)))
9297 (goto-char (setq lspos (1+ (match-beginning 0))))
9298 (setq level (org-reduced-level (funcall outline-level))
9299 category (org-get-category))
9300 (setq i llast llast level)
9301 ;; remove tag lists from same and sublevels
9302 (while (>= i level)
9303 (when (setq entry (assoc i tags-alist))
9304 (setq tags-alist (delete entry tags-alist)))
9305 (setq i (1- i)))
9306 ;; add the next tags
9307 (when tags
9308 (setq tags (mapcar 'downcase (org-split-string tags ":"))
9309 tags-alist
9310 (cons (cons level tags) tags-alist)))
9311 ;; compile tags for current headline
9312 (setq tags-list
9313 (if org-use-tag-inheritance
9314 (apply 'append (mapcar 'cdr tags-alist))
9315 tags))
9316 (when (and tags org-use-tag-inheritance
9317 (not (eq t org-use-tag-inheritance)))
9318 ;; selective inheritance, remove uninherited ones
9319 (setcdr (car tags-alist)
9320 (org-remove-uniherited-tags (cdar tags-alist))))
9321 (when (and (or (not todo-only) (member todo org-not-done-keywords))
9322 (eval matcher)
9324 (not (member org-archive-tag tags-list))
9325 ;; we have an archive tag, should we use this anyway?
9326 (or (not org-agenda-skip-archived-trees)
9327 (and (eq action 'agenda) org-agenda-archives-mode))))
9328 (unless (eq action 'sparse-tree) (org-agenda-skip))
9330 ;; select this headline
9332 (cond
9333 ((eq action 'sparse-tree)
9334 (and org-highlight-sparse-tree-matches
9335 (org-get-heading) (match-end 0)
9336 (org-highlight-new-match
9337 (match-beginning 0) (match-beginning 1)))
9338 (org-show-context 'tags-tree))
9339 ((eq action 'agenda)
9340 (setq txt (org-format-agenda-item
9342 (concat
9343 (if org-tags-match-list-sublevels
9344 (make-string (1- level) ?.) "")
9345 (org-get-heading))
9346 category tags-list)
9347 priority (org-get-priority txt))
9348 (goto-char lspos)
9349 (setq marker (org-agenda-new-marker))
9350 (org-add-props txt props
9351 'org-marker marker 'org-hd-marker marker 'org-category category
9352 'priority priority 'type "tagsmatch")
9353 (push txt rtn))
9354 ((functionp action)
9355 (save-excursion
9356 (setq rtn1 (funcall action))
9357 (push rtn1 rtn))
9358 (goto-char (point-at-eol)))
9359 (t (error "Invalid action")))
9361 ;; if we are to skip sublevels, jump to end of subtree
9362 (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
9363 (when (and (eq action 'sparse-tree)
9364 (not org-sparse-tree-open-archived-trees))
9365 (org-hide-archived-subtrees (point-min) (point-max)))
9366 (nreverse rtn)))
9368 (defun org-remove-uniherited-tags (tags)
9369 "Remove all tags that are not inherited from the list TAGS."
9370 (cond
9371 ((eq org-use-tag-inheritance t) tags)
9372 ((not org-use-tag-inheritance) nil)
9373 ((stringp org-use-tag-inheritance)
9374 (delq nil (mapcar
9375 (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
9376 tags)))
9377 ((listp org-use-tag-inheritance)
9378 (org-delete-all org-use-tag-inheritance tags))))
9380 (defvar todo-only) ;; dynamically scoped
9382 (defun org-tags-sparse-tree (&optional todo-only match)
9383 "Create a sparse tree according to tags string MATCH.
9384 MATCH can contain positive and negative selection of tags, like
9385 \"+WORK+URGENT-WITHBOSS\".
9386 If optional argument TODO_ONLY is non-nil, only select lines that are
9387 also TODO lines."
9388 (interactive "P")
9389 (org-prepare-agenda-buffers (list (current-buffer)))
9390 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
9392 (defvar org-cached-props nil)
9393 (defun org-cached-entry-get (pom property)
9394 (if (or (eq t org-use-property-inheritance)
9395 (and (stringp org-use-property-inheritance)
9396 (string-match org-use-property-inheritance property))
9397 (and (listp org-use-property-inheritance)
9398 (member property org-use-property-inheritance)))
9399 ;; Caching is not possible, check it directly
9400 (org-entry-get pom property 'inherit)
9401 ;; Get all properties, so that we can do complicated checks easily
9402 (cdr (assoc property (or org-cached-props
9403 (setq org-cached-props
9404 (org-entry-properties pom)))))))
9406 (defun org-global-tags-completion-table (&optional files)
9407 "Return the list of all tags in all agenda buffer/files."
9408 (save-excursion
9409 (org-uniquify
9410 (delq nil
9411 (apply 'append
9412 (mapcar
9413 (lambda (file)
9414 (set-buffer (find-file-noselect file))
9415 (append (org-get-buffer-tags)
9416 (mapcar (lambda (x) (if (stringp (car-safe x))
9417 (list (car-safe x)) nil))
9418 org-tag-alist)))
9419 (if (and files (car files))
9420 files
9421 (org-agenda-files))))))))
9423 (defun org-make-tags-matcher (match)
9424 "Create the TAGS//TODO matcher form for the selection string MATCH."
9425 ;; todo-only is scoped dynamically into this function, and the function
9426 ;; may change it it the matcher asksk for it.
9427 (unless match
9428 ;; Get a new match request, with completion
9429 (let ((org-last-tags-completion-table
9430 (org-global-tags-completion-table)))
9431 (setq match (completing-read
9432 "Match: " 'org-tags-completion-function nil nil nil
9433 'org-tags-history))))
9435 ;; Parse the string and create a lisp form
9436 (let ((match0 match)
9437 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
9438 minus tag mm
9439 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
9440 orterms term orlist re-p str-p level-p level-op
9441 prop-p pn pv po cat-p gv)
9442 (if (string-match "/+" match)
9443 ;; match contains also a todo-matching request
9444 (progn
9445 (setq tagsmatch (substring match 0 (match-beginning 0))
9446 todomatch (substring match (match-end 0)))
9447 (if (string-match "^!" todomatch)
9448 (setq todo-only t todomatch (substring todomatch 1)))
9449 (if (string-match "^\\s-*$" todomatch)
9450 (setq todomatch nil)))
9451 ;; only matching tags
9452 (setq tagsmatch match todomatch nil))
9454 ;; Make the tags matcher
9455 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
9456 (setq tagsmatcher t)
9457 (setq orterms (org-split-string tagsmatch "|") orlist nil)
9458 (while (setq term (pop orterms))
9459 (while (and (equal (substring term -1) "\\") orterms)
9460 (setq term (concat term "|" (pop orterms)))) ; repair bad split
9461 (while (string-match re term)
9462 (setq minus (and (match-end 1)
9463 (equal (match-string 1 term) "-"))
9464 tag (match-string 2 term)
9465 re-p (equal (string-to-char tag) ?{)
9466 level-p (match-end 4)
9467 prop-p (match-end 5)
9468 mm (cond
9469 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
9470 (level-p
9471 (setq level-op (org-op-to-function (match-string 3 term)))
9472 `(,level-op level ,(string-to-number
9473 (match-string 4 term))))
9474 (prop-p
9475 (setq pn (match-string 5 term)
9476 po (match-string 6 term)
9477 pv (match-string 7 term)
9478 cat-p (equal pn "CATEGORY")
9479 re-p (equal (string-to-char pv) ?{)
9480 str-p (equal (string-to-char pv) ?\")
9481 time-p (save-match-data (string-match "^\"<.*>\"$" pv))
9482 pv (if (or re-p str-p) (substring pv 1 -1) pv))
9483 (if time-p (setq pv (org-matcher-time pv)))
9484 (setq po (org-op-to-function po (if time-p 'time str-p)))
9485 (if (equal pn "CATEGORY")
9486 (setq gv '(get-text-property (point) 'org-category))
9487 (setq gv `(org-cached-entry-get nil ,pn)))
9488 (if re-p
9489 (if (eq po 'org<>)
9490 `(not (string-match ,pv (or ,gv "")))
9491 `(string-match ,pv (or ,gv "")))
9492 (if str-p
9493 `(,po (or ,gv "") ,pv)
9494 `(,po (string-to-number (or ,gv ""))
9495 ,(string-to-number pv) ))))
9496 (t `(member ,(downcase tag) tags-list)))
9497 mm (if minus (list 'not mm) mm)
9498 term (substring term (match-end 0)))
9499 (push mm tagsmatcher))
9500 (push (if (> (length tagsmatcher) 1)
9501 (cons 'and tagsmatcher)
9502 (car tagsmatcher))
9503 orlist)
9504 (setq tagsmatcher nil))
9505 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
9506 (setq tagsmatcher
9507 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
9508 ;; Make the todo matcher
9509 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
9510 (setq todomatcher t)
9511 (setq orterms (org-split-string todomatch "|") orlist nil)
9512 (while (setq term (pop orterms))
9513 (while (string-match re term)
9514 (setq minus (and (match-end 1)
9515 (equal (match-string 1 term) "-"))
9516 kwd (match-string 2 term)
9517 re-p (equal (string-to-char kwd) ?{)
9518 term (substring term (match-end 0))
9519 mm (if re-p
9520 `(string-match ,(substring kwd 1 -1) todo)
9521 (list 'equal 'todo kwd))
9522 mm (if minus (list 'not mm) mm))
9523 (push mm todomatcher))
9524 (push (if (> (length todomatcher) 1)
9525 (cons 'and todomatcher)
9526 (car todomatcher))
9527 orlist)
9528 (setq todomatcher nil))
9529 (setq todomatcher (if (> (length orlist) 1)
9530 (cons 'or orlist) (car orlist))))
9532 ;; Return the string and lisp forms of the matcher
9533 (setq matcher (if todomatcher
9534 (list 'and tagsmatcher todomatcher)
9535 tagsmatcher))
9536 (cons match0 matcher)))
9538 (defun org-op-to-function (op &optional stringp)
9539 "Turn an operator into the appropriate function."
9540 (setq op
9541 (cond
9542 ((equal op "<" ) '(< string< org-time<))
9543 ((equal op ">" ) '(> org-string> org-time>))
9544 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
9545 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
9546 ((member op '("=" "==")) '(= string= org-time=))
9547 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
9548 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
9550 (defun org<> (a b) (not (= a b)))
9551 (defun org-string<= (a b) (or (string= a b) (string< a b)))
9552 (defun org-string>= (a b) (not (string< a b)))
9553 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
9554 (defun org-string<> (a b) (not (string= a b)))
9555 (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
9556 (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
9557 (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
9558 (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
9559 (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
9560 (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
9561 (defun org-2ft (s)
9562 "Convert S to a floating point time.
9563 If S is already a number, just return it. If it is a string, parse
9564 it as a time string and apply `float-time' to it. f S is nil, just return 0."
9565 (cond
9566 ((numberp s) s)
9567 ((stringp s)
9568 (condition-case nil
9569 (float-time (apply 'encode-time (org-parse-time-string s)))
9570 (error 0.)))
9571 (t 0.)))
9573 (defun org-matcher-time (s)
9574 (cond
9575 ((equal s "<now>") (float-time))
9576 ((equal s "<today>")
9577 (float-time (append '(0 0 0) (nthcdr 3 (decode-time)))))
9578 (t (org-2ft s))))
9580 (defun org-match-any-p (re list)
9581 "Does re match any element of list?"
9582 (setq list (mapcar (lambda (x) (string-match re x)) list))
9583 (delq nil list))
9585 (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
9586 (defvar org-tags-overlay (org-make-overlay 1 1))
9587 (org-detach-overlay org-tags-overlay)
9589 (defun org-get-tags-at (&optional pos)
9590 "Get a list of all headline tags applicable at POS.
9591 POS defaults to point. If tags are inherited, the list contains
9592 the targets in the same sequence as the headlines appear, i.e.
9593 the tags of the current headline come last."
9594 (interactive)
9595 (let (tags ltags lastpos parent)
9596 (save-excursion
9597 (save-restriction
9598 (widen)
9599 (goto-char (or pos (point)))
9600 (save-match-data
9601 (condition-case nil
9602 (progn
9603 (org-back-to-heading t)
9604 (while (not (equal lastpos (point)))
9605 (setq lastpos (point))
9606 (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
9607 (setq ltags (org-split-string
9608 (org-match-string-no-properties 1) ":"))
9609 (setq tags (append (org-remove-uniherited-tags ltags)
9610 tags)))
9611 (or org-use-tag-inheritance (error ""))
9612 (org-up-heading-all 1)
9613 (setq parent t)))
9614 (error nil))))
9615 (append (org-remove-uniherited-tags org-file-tags) tags))))
9617 (defun org-toggle-tag (tag &optional onoff)
9618 "Toggle the tag TAG for the current line.
9619 If ONOFF is `on' or `off', don't toggle but set to this state."
9620 (unless (org-on-heading-p t) (error "Not on headling"))
9621 (let (res current)
9622 (save-excursion
9623 (beginning-of-line)
9624 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
9625 (point-at-eol) t)
9626 (progn
9627 (setq current (match-string 1))
9628 (replace-match ""))
9629 (setq current ""))
9630 (setq current (nreverse (org-split-string current ":")))
9631 (cond
9632 ((eq onoff 'on)
9633 (setq res t)
9634 (or (member tag current) (push tag current)))
9635 ((eq onoff 'off)
9636 (or (not (member tag current)) (setq current (delete tag current))))
9637 (t (if (member tag current)
9638 (setq current (delete tag current))
9639 (setq res t)
9640 (push tag current))))
9641 (end-of-line 1)
9642 (if current
9643 (progn
9644 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
9645 (org-set-tags nil t))
9646 (delete-horizontal-space))
9647 (run-hooks 'org-after-tags-change-hook))
9648 res))
9650 (defun org-align-tags-here (to-col)
9651 ;; Assumes that this is a headline
9652 (let ((pos (point)) (col (current-column)) ncol tags-l p)
9653 (beginning-of-line 1)
9654 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9655 (< pos (match-beginning 2)))
9656 (progn
9657 (setq tags-l (- (match-end 2) (match-beginning 2)))
9658 (goto-char (match-beginning 1))
9659 (insert " ")
9660 (delete-region (point) (1+ (match-beginning 2)))
9661 (setq ncol (max (1+ (current-column))
9662 (1+ col)
9663 (if (> to-col 0)
9664 to-col
9665 (- (abs to-col) tags-l))))
9666 (setq p (point))
9667 (insert (make-string (- ncol (current-column)) ?\ ))
9668 (setq ncol (current-column))
9669 (when indent-tabs-mode (tabify p (point-at-eol)))
9670 (org-move-to-column (min ncol col) t))
9671 (goto-char pos))))
9673 (defun org-set-tags (&optional arg just-align)
9674 "Set the tags for the current headline.
9675 With prefix ARG, realign all tags in headings in the current buffer."
9676 (interactive "P")
9677 (let* ((re (concat "^" outline-regexp))
9678 (current (org-get-tags-string))
9679 (col (current-column))
9680 (org-setting-tags t)
9681 table current-tags inherited-tags ; computed below when needed
9682 tags p0 c0 c1 rpl)
9683 (if arg
9684 (save-excursion
9685 (goto-char (point-min))
9686 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
9687 (while (re-search-forward re nil t)
9688 (org-set-tags nil t)
9689 (end-of-line 1)))
9690 (message "All tags realigned to column %d" org-tags-column))
9691 (if just-align
9692 (setq tags current)
9693 ;; Get a new set of tags from the user
9694 (save-excursion
9695 (setq table (or org-tag-alist (org-get-buffer-tags))
9696 org-last-tags-completion-table table
9697 current-tags (org-split-string current ":")
9698 inherited-tags (nreverse
9699 (nthcdr (length current-tags)
9700 (nreverse (org-get-tags-at))))
9701 tags
9702 (if (or (eq t org-use-fast-tag-selection)
9703 (and org-use-fast-tag-selection
9704 (delq nil (mapcar 'cdr table))))
9705 (org-fast-tag-selection
9706 current-tags inherited-tags table
9707 (if org-fast-tag-selection-include-todo org-todo-key-alist))
9708 (let ((org-add-colon-after-tag-completion t))
9709 (org-trim
9710 (org-without-partial-completion
9711 (completing-read "Tags: " 'org-tags-completion-function
9712 nil nil current 'org-tags-history)))))))
9713 (while (string-match "[-+&]+" tags)
9714 ;; No boolean logic, just a list
9715 (setq tags (replace-match ":" t t tags))))
9717 (if (string-match "\\`[\t ]*\\'" tags)
9718 (setq tags "")
9719 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
9720 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
9722 ;; Insert new tags at the correct column
9723 (beginning-of-line 1)
9724 (cond
9725 ((and (equal current "") (equal tags "")))
9726 ((re-search-forward
9727 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
9728 (point-at-eol) t)
9729 (if (equal tags "")
9730 (setq rpl "")
9731 (goto-char (match-beginning 0))
9732 (setq c0 (current-column) p0 (point)
9733 c1 (max (1+ c0) (if (> org-tags-column 0)
9734 org-tags-column
9735 (- (- org-tags-column) (length tags))))
9736 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
9737 (replace-match rpl t t)
9738 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
9739 tags)
9740 (t (error "Tags alignment failed")))
9741 (org-move-to-column col)
9742 (unless just-align
9743 (run-hooks 'org-after-tags-change-hook)))))
9745 (defun org-change-tag-in-region (beg end tag off)
9746 "Add or remove TAG for each entry in the region.
9747 This works in the agenda, and also in an org-mode buffer."
9748 (interactive
9749 (list (region-beginning) (region-end)
9750 (let ((org-last-tags-completion-table
9751 (if (org-mode-p)
9752 (org-get-buffer-tags)
9753 (org-global-tags-completion-table))))
9754 (completing-read
9755 "Tag: " 'org-tags-completion-function nil nil nil
9756 'org-tags-history))
9757 (progn
9758 (message "[s]et or [r]emove? ")
9759 (equal (read-char-exclusive) ?r))))
9760 (if (fboundp 'deactivate-mark) (deactivate-mark))
9761 (let ((agendap (equal major-mode 'org-agenda-mode))
9762 l1 l2 m buf pos newhead (cnt 0))
9763 (goto-char end)
9764 (setq l2 (1- (org-current-line)))
9765 (goto-char beg)
9766 (setq l1 (org-current-line))
9767 (loop for l from l1 to l2 do
9768 (goto-line l)
9769 (setq m (get-text-property (point) 'org-hd-marker))
9770 (when (or (and (org-mode-p) (org-on-heading-p))
9771 (and agendap m))
9772 (setq buf (if agendap (marker-buffer m) (current-buffer))
9773 pos (if agendap m (point)))
9774 (with-current-buffer buf
9775 (save-excursion
9776 (save-restriction
9777 (goto-char pos)
9778 (setq cnt (1+ cnt))
9779 (org-toggle-tag tag (if off 'off 'on))
9780 (setq newhead (org-get-heading)))))
9781 (and agendap (org-agenda-change-all-lines newhead m))))
9782 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
9784 (defun org-tags-completion-function (string predicate &optional flag)
9785 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
9786 (confirm (lambda (x) (stringp (car x)))))
9787 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
9788 (setq s1 (match-string 1 string)
9789 s2 (match-string 2 string))
9790 (setq s1 "" s2 string))
9791 (cond
9792 ((eq flag nil)
9793 ;; try completion
9794 (setq rtn (try-completion s2 ctable confirm))
9795 (if (stringp rtn)
9796 (setq rtn
9797 (concat s1 s2 (substring rtn (length s2))
9798 (if (and org-add-colon-after-tag-completion
9799 (assoc rtn ctable))
9800 ":" ""))))
9801 rtn)
9802 ((eq flag t)
9803 ;; all-completions
9804 (all-completions s2 ctable confirm)
9806 ((eq flag 'lambda)
9807 ;; exact match?
9808 (assoc s2 ctable)))
9811 (defun org-fast-tag-insert (kwd tags face &optional end)
9812 "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
9813 (insert (format "%-12s" (concat kwd ":"))
9814 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
9815 (or end "")))
9817 (defun org-fast-tag-show-exit (flag)
9818 (save-excursion
9819 (goto-line 3)
9820 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
9821 (replace-match ""))
9822 (when flag
9823 (end-of-line 1)
9824 (org-move-to-column (- (window-width) 19) t)
9825 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
9827 (defun org-set-current-tags-overlay (current prefix)
9828 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
9829 (if (featurep 'xemacs)
9830 (org-overlay-display org-tags-overlay (concat prefix s)
9831 'secondary-selection)
9832 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
9833 (org-overlay-display org-tags-overlay (concat prefix s)))))
9835 (defun org-fast-tag-selection (current inherited table &optional todo-table)
9836 "Fast tag selection with single keys.
9837 CURRENT is the current list of tags in the headline, INHERITED is the
9838 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
9839 possibly with grouping information. TODO-TABLE is a similar table with
9840 TODO keywords, should these have keys assigned to them.
9841 If the keys are nil, a-z are automatically assigned.
9842 Returns the new tags string, or nil to not change the current settings."
9843 (let* ((fulltable (append table todo-table))
9844 (maxlen (apply 'max (mapcar
9845 (lambda (x)
9846 (if (stringp (car x)) (string-width (car x)) 0))
9847 fulltable)))
9848 (buf (current-buffer))
9849 (expert (eq org-fast-tag-selection-single-key 'expert))
9850 (buffer-tags nil)
9851 (fwidth (+ maxlen 3 1 3))
9852 (ncol (/ (- (window-width) 4) fwidth))
9853 (i-face 'org-done)
9854 (c-face 'org-todo)
9855 tg cnt e c char c1 c2 ntable tbl rtn
9856 ov-start ov-end ov-prefix
9857 (exit-after-next org-fast-tag-selection-single-key)
9858 (done-keywords org-done-keywords)
9859 groups ingroup)
9860 (save-excursion
9861 (beginning-of-line 1)
9862 (if (looking-at
9863 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9864 (setq ov-start (match-beginning 1)
9865 ov-end (match-end 1)
9866 ov-prefix "")
9867 (setq ov-start (1- (point-at-eol))
9868 ov-end (1+ ov-start))
9869 (skip-chars-forward "^\n\r")
9870 (setq ov-prefix
9871 (concat
9872 (buffer-substring (1- (point)) (point))
9873 (if (> (current-column) org-tags-column)
9875 (make-string (- org-tags-column (current-column)) ?\ ))))))
9876 (org-move-overlay org-tags-overlay ov-start ov-end)
9877 (save-window-excursion
9878 (if expert
9879 (set-buffer (get-buffer-create " *Org tags*"))
9880 (delete-other-windows)
9881 (split-window-vertically)
9882 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
9883 (erase-buffer)
9884 (org-set-local 'org-done-keywords done-keywords)
9885 (org-fast-tag-insert "Inherited" inherited i-face "\n")
9886 (org-fast-tag-insert "Current" current c-face "\n\n")
9887 (org-fast-tag-show-exit exit-after-next)
9888 (org-set-current-tags-overlay current ov-prefix)
9889 (setq tbl fulltable char ?a cnt 0)
9890 (while (setq e (pop tbl))
9891 (cond
9892 ((equal e '(:startgroup))
9893 (push '() groups) (setq ingroup t)
9894 (when (not (= cnt 0))
9895 (setq cnt 0)
9896 (insert "\n"))
9897 (insert "{ "))
9898 ((equal e '(:endgroup))
9899 (setq ingroup nil cnt 0)
9900 (insert "}\n"))
9902 (setq tg (car e) c2 nil)
9903 (if (cdr e)
9904 (setq c (cdr e))
9905 ;; automatically assign a character.
9906 (setq c1 (string-to-char
9907 (downcase (substring
9908 tg (if (= (string-to-char tg) ?@) 1 0)))))
9909 (if (or (rassoc c1 ntable) (rassoc c1 table))
9910 (while (or (rassoc char ntable) (rassoc char table))
9911 (setq char (1+ char)))
9912 (setq c2 c1))
9913 (setq c (or c2 char)))
9914 (if ingroup (push tg (car groups)))
9915 (setq tg (org-add-props tg nil 'face
9916 (cond
9917 ((not (assoc tg table))
9918 (org-get-todo-face tg))
9919 ((member tg current) c-face)
9920 ((member tg inherited) i-face)
9921 (t nil))))
9922 (if (and (= cnt 0) (not ingroup)) (insert " "))
9923 (insert "[" c "] " tg (make-string
9924 (- fwidth 4 (length tg)) ?\ ))
9925 (push (cons tg c) ntable)
9926 (when (= (setq cnt (1+ cnt)) ncol)
9927 (insert "\n")
9928 (if ingroup (insert " "))
9929 (setq cnt 0)))))
9930 (setq ntable (nreverse ntable))
9931 (insert "\n")
9932 (goto-char (point-min))
9933 (if (and (not expert) (fboundp 'fit-window-to-buffer))
9934 (fit-window-to-buffer))
9935 (setq rtn
9936 (catch 'exit
9937 (while t
9938 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
9939 (if groups " [!] no groups" " [!]groups")
9940 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
9941 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
9942 (cond
9943 ((= c ?\r) (throw 'exit t))
9944 ((= c ?!)
9945 (setq groups (not groups))
9946 (goto-char (point-min))
9947 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
9948 ((= c ?\C-c)
9949 (if (not expert)
9950 (org-fast-tag-show-exit
9951 (setq exit-after-next (not exit-after-next)))
9952 (setq expert nil)
9953 (delete-other-windows)
9954 (split-window-vertically)
9955 (org-switch-to-buffer-other-window " *Org tags*")
9956 (and (fboundp 'fit-window-to-buffer)
9957 (fit-window-to-buffer))))
9958 ((or (= c ?\C-g)
9959 (and (= c ?q) (not (rassoc c ntable))))
9960 (org-detach-overlay org-tags-overlay)
9961 (setq quit-flag t))
9962 ((= c ?\ )
9963 (setq current nil)
9964 (if exit-after-next (setq exit-after-next 'now)))
9965 ((= c ?\t)
9966 (condition-case nil
9967 (setq tg (completing-read
9968 "Tag: "
9969 (or buffer-tags
9970 (with-current-buffer buf
9971 (org-get-buffer-tags)))))
9972 (quit (setq tg "")))
9973 (when (string-match "\\S-" tg)
9974 (add-to-list 'buffer-tags (list tg))
9975 (if (member tg current)
9976 (setq current (delete tg current))
9977 (push tg current)))
9978 (if exit-after-next (setq exit-after-next 'now)))
9979 ((setq e (rassoc c todo-table) tg (car e))
9980 (with-current-buffer buf
9981 (save-excursion (org-todo tg)))
9982 (if exit-after-next (setq exit-after-next 'now)))
9983 ((setq e (rassoc c ntable) tg (car e))
9984 (if (member tg current)
9985 (setq current (delete tg current))
9986 (loop for g in groups do
9987 (if (member tg g)
9988 (mapc (lambda (x)
9989 (setq current (delete x current)))
9990 g)))
9991 (push tg current))
9992 (if exit-after-next (setq exit-after-next 'now))))
9994 ;; Create a sorted list
9995 (setq current
9996 (sort current
9997 (lambda (a b)
9998 (assoc b (cdr (memq (assoc a ntable) ntable))))))
9999 (if (eq exit-after-next 'now) (throw 'exit t))
10000 (goto-char (point-min))
10001 (beginning-of-line 2)
10002 (delete-region (point) (point-at-eol))
10003 (org-fast-tag-insert "Current" current c-face)
10004 (org-set-current-tags-overlay current ov-prefix)
10005 (while (re-search-forward
10006 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
10007 (setq tg (match-string 1))
10008 (add-text-properties
10009 (match-beginning 1) (match-end 1)
10010 (list 'face
10011 (cond
10012 ((member tg current) c-face)
10013 ((member tg inherited) i-face)
10014 (t (get-text-property (match-beginning 1) 'face))))))
10015 (goto-char (point-min)))))
10016 (org-detach-overlay org-tags-overlay)
10017 (if rtn
10018 (mapconcat 'identity current ":")
10019 nil))))
10021 (defun org-get-tags-string ()
10022 "Get the TAGS string in the current headline."
10023 (unless (org-on-heading-p t)
10024 (error "Not on a heading"))
10025 (save-excursion
10026 (beginning-of-line 1)
10027 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
10028 (org-match-string-no-properties 1)
10029 "")))
10031 (defun org-get-tags ()
10032 "Get the list of tags specified in the current headline."
10033 (org-split-string (org-get-tags-string) ":"))
10035 (defun org-get-buffer-tags ()
10036 "Get a table of all tags used in the buffer, for completion."
10037 (let (tags)
10038 (save-excursion
10039 (goto-char (point-min))
10040 (while (re-search-forward
10041 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
10042 (when (equal (char-after (point-at-bol 0)) ?*)
10043 (mapc (lambda (x) (add-to-list 'tags x))
10044 (org-split-string (org-match-string-no-properties 1) ":")))))
10045 (mapcar 'list tags)))
10047 ;;;; The mapping API
10049 ;;;###autoload
10050 (defun org-map-entries (func &optional match scope &rest skip)
10051 "Call FUNC at each headline selected by MATCH in SCOPE.
10053 FUNC is a function or a lisp form. The function will be called without
10054 arguments, with the cursor positioned at the beginning of the headline.
10055 The return values of all calls to the function will be collected and
10056 returned as a list.
10058 MATCH is a tags/property/todo match as it is used in the agenda tags view.
10059 Only headlines that are matched by this query will be considered during
10060 the iteration. When MATCH is nil or t, all headlines will be
10061 visited by the iteration.
10063 SCOPE determines the scope of this command. It can be any of:
10065 nil The current buffer, respecting the restriction if any
10066 tree The subtree started with the entry at point
10067 file The current buffer, without restriction
10068 file-with-archives
10069 The current buffer, and any archives associated with it
10070 agenda All agenda files
10071 agenda-with-archives
10072 All agenda files with any archive files associated with them
10073 \(file1 file2 ...)
10074 If this is a list, all files in the list will be scanned
10076 The remaining args are treated as settings for the skipping facilities of
10077 the scanner. The following items can be given here:
10079 archive skip trees with the archive tag.
10080 comment skip trees with the COMMENT keyword
10081 function or Emacs Lisp form:
10082 will be used as value for `org-agenda-skip-function', so whenever
10083 the the function returns t, FUNC will not be called for that
10084 entry and search will continue from the point where the
10085 function leaves it."
10086 (let* ((org-agenda-archives-mode nil) ; just to make sure
10087 (org-agenda-skip-archived-trees (memq 'archive skip))
10088 (org-agenda-skip-comment-trees (memq 'comment skip))
10089 (org-agenda-skip-function
10090 (car (org-delete-all '(comment archive) skip)))
10091 (org-tags-match-list-sublevels t)
10092 matcher pos file)
10094 (cond
10095 ((eq match t) (setq matcher t))
10096 ((eq match nil) (setq matcher t))
10097 (t (setq matcher (if match (org-make-tags-matcher match) t))))
10099 (when (eq scope 'tree)
10100 (org-back-to-heading t)
10101 (org-narrow-to-subtree)
10102 (setq scope nil))
10104 (if (not scope)
10105 (progn
10106 (org-prepare-agenda-buffers
10107 (list (buffer-file-name (current-buffer))))
10108 (org-scan-tags func matcher))
10109 ;; Get the right scope
10110 (setq pos (point))
10111 (cond
10112 ((and scope (listp scope) (symbolp (car scope)))
10113 (setq scope (eval scope)))
10114 ((eq scope 'agenda)
10115 (setq scope (org-agenda-files t)))
10116 ((eq scope 'agenda-with-archives)
10117 (setq scope (org-agenda-files t))
10118 (setq scope (org-add-archive-files scope)))
10119 ((eq scope 'file)
10120 (setq scope (list (buffer-file-name))))
10121 ((eq scope 'file-with-archives)
10122 (setq scope (org-add-archive-files (list (buffer-file-name))))))
10123 (org-prepare-agenda-buffers scope)
10124 (while (setq file (pop scope))
10125 (with-current-buffer (org-find-base-buffer-visiting file)
10126 (save-excursion
10127 (save-restriction
10128 (widen)
10129 (goto-char (point-min))
10130 (org-scan-tags func matcher))))))))
10132 ;;;; Properties
10134 ;;; Setting and retrieving properties
10136 (defconst org-special-properties
10137 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
10138 "TIMESTAMP" "TIMESTAMP_IA")
10139 "The special properties valid in Org-mode.
10141 These are properties that are not defined in the property drawer,
10142 but in some other way.")
10144 (defconst org-default-properties
10145 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
10146 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
10147 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
10148 "EXPORT_FILE_NAME" "EXPORT_TITLE")
10149 "Some properties that are used by Org-mode for various purposes.
10150 Being in this list makes sure that they are offered for completion.")
10152 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
10153 "Regular expression matching the first line of a property drawer.")
10155 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
10156 "Regular expression matching the first line of a property drawer.")
10158 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
10159 "Regular expression matching the first line of a property drawer.")
10161 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
10162 "Regular expression matching the first line of a property drawer.")
10164 (defconst org-property-drawer-re
10165 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
10166 org-property-end-re "\\)\n?")
10167 "Matches an entire property drawer.")
10169 (defconst org-clock-drawer-re
10170 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
10171 org-property-end-re "\\)\n?")
10172 "Matches an entire clock drawer.")
10174 (defun org-property-action ()
10175 "Do an action on properties."
10176 (interactive)
10177 (let (c)
10178 (org-at-property-p)
10179 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
10180 (setq c (read-char-exclusive))
10181 (cond
10182 ((equal c ?s)
10183 (call-interactively 'org-set-property))
10184 ((equal c ?d)
10185 (call-interactively 'org-delete-property))
10186 ((equal c ?D)
10187 (call-interactively 'org-delete-property-globally))
10188 ((equal c ?c)
10189 (call-interactively 'org-compute-property-at-point))
10190 (t (error "No such property action %c" c)))))
10192 (defun org-at-property-p ()
10193 "Is the cursor in a property line?"
10194 ;; FIXME: Does not check if we are actually in the drawer.
10195 ;; FIXME: also returns true on any drawers.....
10196 ;; This is used by C-c C-c for property action.
10197 (save-excursion
10198 (beginning-of-line 1)
10199 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
10201 (defun org-get-property-block (&optional beg end force)
10202 "Return the (beg . end) range of the body of the property drawer.
10203 BEG and END can be beginning and end of subtree, if not given
10204 they will be found.
10205 If the drawer does not exist and FORCE is non-nil, create the drawer."
10206 (catch 'exit
10207 (save-excursion
10208 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
10209 (end (or end (progn (outline-next-heading) (point)))))
10210 (goto-char beg)
10211 (if (re-search-forward org-property-start-re end t)
10212 (setq beg (1+ (match-end 0)))
10213 (if force
10214 (save-excursion
10215 (org-insert-property-drawer)
10216 (setq end (progn (outline-next-heading) (point))))
10217 (throw 'exit nil))
10218 (goto-char beg)
10219 (if (re-search-forward org-property-start-re end t)
10220 (setq beg (1+ (match-end 0)))))
10221 (if (re-search-forward org-property-end-re end t)
10222 (setq end (match-beginning 0))
10223 (or force (throw 'exit nil))
10224 (goto-char beg)
10225 (setq end beg)
10226 (org-indent-line-function)
10227 (insert ":END:\n"))
10228 (cons beg end)))))
10230 (defun org-entry-properties (&optional pom which)
10231 "Get all properties of the entry at point-or-marker POM.
10232 This includes the TODO keyword, the tags, time strings for deadline,
10233 scheduled, and clocking, and any additional properties defined in the
10234 entry. The return value is an alist, keys may occur multiple times
10235 if the property key was used several times.
10236 POM may also be nil, in which case the current entry is used.
10237 If WHICH is nil or `all', get all properties. If WHICH is
10238 `special' or `standard', only get that subclass."
10239 (setq which (or which 'all))
10240 (org-with-point-at pom
10241 (let ((clockstr (substring org-clock-string 0 -1))
10242 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
10243 beg end range props sum-props key value string clocksum)
10244 (save-excursion
10245 (when (condition-case nil (org-back-to-heading t) (error nil))
10246 (setq beg (point))
10247 (setq sum-props (get-text-property (point) 'org-summaries))
10248 (setq clocksum (get-text-property (point) :org-clock-minutes))
10249 (outline-next-heading)
10250 (setq end (point))
10251 (when (memq which '(all special))
10252 ;; Get the special properties, like TODO and tags
10253 (goto-char beg)
10254 (when (and (looking-at org-todo-line-regexp) (match-end 2))
10255 (push (cons "TODO" (org-match-string-no-properties 2)) props))
10256 (when (looking-at org-priority-regexp)
10257 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
10258 (when (and (setq value (org-get-tags-string))
10259 (string-match "\\S-" value))
10260 (push (cons "TAGS" value) props))
10261 (when (setq value (org-get-tags-at))
10262 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
10263 props))
10264 (while (re-search-forward org-maybe-keyword-time-regexp end t)
10265 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
10266 string (if (equal key clockstr)
10267 (org-no-properties
10268 (org-trim
10269 (buffer-substring
10270 (match-beginning 3) (goto-char (point-at-eol)))))
10271 (substring (org-match-string-no-properties 3) 1 -1)))
10272 (unless key
10273 (if (= (char-after (match-beginning 3)) ?\[)
10274 (setq key "TIMESTAMP_IA")
10275 (setq key "TIMESTAMP")))
10276 (when (or (equal key clockstr) (not (assoc key props)))
10277 (push (cons key string) props)))
10281 (when (memq which '(all standard))
10282 ;; Get the standard properties, like :PORP: ...
10283 (setq range (org-get-property-block beg end))
10284 (when range
10285 (goto-char (car range))
10286 (while (re-search-forward
10287 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
10288 (cdr range) t)
10289 (setq key (org-match-string-no-properties 1)
10290 value (org-trim (or (org-match-string-no-properties 2) "")))
10291 (unless (member key excluded)
10292 (push (cons key (or value "")) props)))))
10293 (if clocksum
10294 (push (cons "CLOCKSUM"
10295 (org-columns-number-to-string (/ (float clocksum) 60.)
10296 'add_times))
10297 props))
10298 (append sum-props (nreverse props)))))))
10300 (defun org-entry-get (pom property &optional inherit)
10301 "Get value of PROPERTY for entry at point-or-marker POM.
10302 If INHERIT is non-nil and the entry does not have the property,
10303 then also check higher levels of the hierarchy.
10304 If INHERIT is the symbol `selective', use inheritance only if the setting
10305 in `org-use-property-inheritance' selects PROPERTY for inheritance.
10306 If the property is present but empty, the return value is the empty string.
10307 If the property is not present at all, nil is returned."
10308 (org-with-point-at pom
10309 (if (and inherit (if (eq inherit 'selective)
10310 (org-property-inherit-p property)
10312 (org-entry-get-with-inheritance property)
10313 (if (member property org-special-properties)
10314 ;; We need a special property. Use brute force, get all properties.
10315 (cdr (assoc property (org-entry-properties nil 'special)))
10316 (let ((range (org-get-property-block)))
10317 (if (and range
10318 (goto-char (car range))
10319 (re-search-forward
10320 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
10321 (cdr range) t))
10322 ;; Found the property, return it.
10323 (if (match-end 1)
10324 (org-match-string-no-properties 1)
10325 "")))))))
10327 (defun org-property-or-variable-value (var &optional inherit)
10328 "Check if there is a property fixing the value of VAR.
10329 If yes, return this value. If not, return the current value of the variable."
10330 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
10331 (if (and prop (stringp prop) (string-match "\\S-" prop))
10332 (read prop)
10333 (symbol-value var))))
10335 (defun org-entry-delete (pom property)
10336 "Delete the property PROPERTY from entry at point-or-marker POM."
10337 (org-with-point-at pom
10338 (if (member property org-special-properties)
10339 nil ; cannot delete these properties.
10340 (let ((range (org-get-property-block)))
10341 (if (and range
10342 (goto-char (car range))
10343 (re-search-forward
10344 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
10345 (cdr range) t))
10346 (progn
10347 (delete-region (match-beginning 0) (1+ (point-at-eol)))
10349 nil)))))
10351 ;; Multi-values properties are properties that contain multiple values
10352 ;; These values are assumed to be single words, separated by whitespace.
10353 (defun org-entry-add-to-multivalued-property (pom property value)
10354 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
10355 (let* ((old (org-entry-get pom property))
10356 (values (and old (org-split-string old "[ \t]"))))
10357 (unless (member value values)
10358 (setq values (cons value values))
10359 (org-entry-put pom property
10360 (mapconcat 'identity values " ")))))
10362 (defun org-entry-remove-from-multivalued-property (pom property value)
10363 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
10364 (let* ((old (org-entry-get pom property))
10365 (values (and old (org-split-string old "[ \t]"))))
10366 (when (member value values)
10367 (setq values (delete value values))
10368 (org-entry-put pom property
10369 (mapconcat 'identity values " ")))))
10371 (defun org-entry-member-in-multivalued-property (pom property value)
10372 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
10373 (let* ((old (org-entry-get pom property))
10374 (values (and old (org-split-string old "[ \t]"))))
10375 (member value values)))
10377 (defvar org-entry-property-inherited-from (make-marker))
10379 (defun org-entry-get-with-inheritance (property)
10380 "Get entry property, and search higher levels if not present."
10381 (let (tmp)
10382 (save-excursion
10383 (save-restriction
10384 (widen)
10385 (catch 'ex
10386 (while t
10387 (when (setq tmp (org-entry-get nil property))
10388 (org-back-to-heading t)
10389 (move-marker org-entry-property-inherited-from (point))
10390 (throw 'ex tmp))
10391 (or (org-up-heading-safe) (throw 'ex nil)))))
10392 (or tmp
10393 (cdr (assoc property org-file-properties))
10394 (cdr (assoc property org-global-properties))
10395 (cdr (assoc property org-global-properties-fixed))))))
10397 (defun org-entry-put (pom property value)
10398 "Set PROPERTY to VALUE for entry at point-or-marker POM."
10399 (org-with-point-at pom
10400 (org-back-to-heading t)
10401 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
10402 range)
10403 (cond
10404 ((equal property "TODO")
10405 (when (and (stringp value) (string-match "\\S-" value)
10406 (not (member value org-todo-keywords-1)))
10407 (error "\"%s\" is not a valid TODO state" value))
10408 (if (or (not value)
10409 (not (string-match "\\S-" value)))
10410 (setq value 'none))
10411 (org-todo value)
10412 (org-set-tags nil 'align))
10413 ((equal property "PRIORITY")
10414 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
10415 (string-to-char value) ?\ ))
10416 (org-set-tags nil 'align))
10417 ((equal property "SCHEDULED")
10418 (if (re-search-forward org-scheduled-time-regexp end t)
10419 (cond
10420 ((eq value 'earlier) (org-timestamp-change -1 'day))
10421 ((eq value 'later) (org-timestamp-change 1 'day))
10422 (t (call-interactively 'org-schedule)))
10423 (call-interactively 'org-schedule)))
10424 ((equal property "DEADLINE")
10425 (if (re-search-forward org-deadline-time-regexp end t)
10426 (cond
10427 ((eq value 'earlier) (org-timestamp-change -1 'day))
10428 ((eq value 'later) (org-timestamp-change 1 'day))
10429 (t (call-interactively 'org-deadline)))
10430 (call-interactively 'org-deadline)))
10431 ((member property org-special-properties)
10432 (error "The %s property can not yet be set with `org-entry-put'"
10433 property))
10434 (t ; a non-special property
10435 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
10436 (setq range (org-get-property-block beg end 'force))
10437 (goto-char (car range))
10438 (if (re-search-forward
10439 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
10440 (progn
10441 (delete-region (match-beginning 1) (match-end 1))
10442 (goto-char (match-beginning 1)))
10443 (goto-char (cdr range))
10444 (insert "\n")
10445 (backward-char 1)
10446 (org-indent-line-function)
10447 (insert ":" property ":"))
10448 (and value (insert " " value))
10449 (org-indent-line-function)))))))
10451 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
10452 "Get all property keys in the current buffer.
10453 With INCLUDE-SPECIALS, also list the special properties that relect things
10454 like tags and TODO state.
10455 With INCLUDE-DEFAULTS, also include properties that has special meaning
10456 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
10457 With INCLUDE-COLUMNS, also include property names given in COLUMN
10458 formats in the current buffer."
10459 (let (rtn range cfmt cols s p)
10460 (save-excursion
10461 (save-restriction
10462 (widen)
10463 (goto-char (point-min))
10464 (while (re-search-forward org-property-start-re nil t)
10465 (setq range (org-get-property-block))
10466 (goto-char (car range))
10467 (while (re-search-forward
10468 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
10469 (cdr range) t)
10470 (add-to-list 'rtn (org-match-string-no-properties 1)))
10471 (outline-next-heading))))
10473 (when include-specials
10474 (setq rtn (append org-special-properties rtn)))
10476 (when include-defaults
10477 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
10479 (when include-columns
10480 (save-excursion
10481 (save-restriction
10482 (widen)
10483 (goto-char (point-min))
10484 (while (re-search-forward
10485 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
10486 nil t)
10487 (setq cfmt (match-string 2) s 0)
10488 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
10489 cfmt s)
10490 (setq s (match-end 0)
10491 p (match-string 1 cfmt))
10492 (unless (or (equal p "ITEM")
10493 (member p org-special-properties))
10494 (add-to-list 'rtn (match-string 1 cfmt))))))))
10496 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
10498 (defun org-property-values (key)
10499 "Return a list of all values of property KEY."
10500 (save-excursion
10501 (save-restriction
10502 (widen)
10503 (goto-char (point-min))
10504 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
10505 values)
10506 (while (re-search-forward re nil t)
10507 (add-to-list 'values (org-trim (match-string 1))))
10508 (delete "" values)))))
10510 (defun org-insert-property-drawer ()
10511 "Insert a property drawer into the current entry."
10512 (interactive)
10513 (org-back-to-heading t)
10514 (looking-at outline-regexp)
10515 (let ((indent (- (match-end 0)(match-beginning 0)))
10516 (beg (point))
10517 (re (concat "^[ \t]*" org-keyword-time-regexp))
10518 end hiddenp)
10519 (outline-next-heading)
10520 (setq end (point))
10521 (goto-char beg)
10522 (while (re-search-forward re end t))
10523 (setq hiddenp (org-invisible-p))
10524 (end-of-line 1)
10525 (and (equal (char-after) ?\n) (forward-char 1))
10526 (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
10527 (beginning-of-line 2))
10528 (org-skip-over-state-notes)
10529 (skip-chars-backward " \t\n\r")
10530 (if (eq (char-before) ?*) (forward-char 1))
10531 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
10532 (beginning-of-line 0)
10533 (org-indent-to-column indent)
10534 (beginning-of-line 2)
10535 (org-indent-to-column indent)
10536 (beginning-of-line 0)
10537 (if hiddenp
10538 (save-excursion
10539 (org-back-to-heading t)
10540 (hide-entry))
10541 (org-flag-drawer t))))
10543 (defun org-set-property (property value)
10544 "In the current entry, set PROPERTY to VALUE.
10545 When called interactively, this will prompt for a property name, offering
10546 completion on existing and default properties. And then it will prompt
10547 for a value, offering competion either on allowed values (via an inherited
10548 xxx_ALL property) or on existing values in other instances of this property
10549 in the current file."
10550 (interactive
10551 (let* ((completion-ignore-case t)
10552 (keys (org-buffer-property-keys nil t t))
10553 (prop0 (completing-read "Property: " (mapcar 'list keys)))
10554 (prop (if (member prop0 keys)
10555 prop0
10556 (or (cdr (assoc (downcase prop0)
10557 (mapcar (lambda (x) (cons (downcase x) x))
10558 keys)))
10559 prop0)))
10560 (cur (org-entry-get nil prop))
10561 (allowed (org-property-get-allowed-values nil prop 'table))
10562 (existing (mapcar 'list (org-property-values prop)))
10563 (val (if allowed
10564 (org-completing-read "Value: " allowed nil 'req-match)
10565 (org-completing-read
10566 (concat "Value" (if (and cur (string-match "\\S-" cur))
10567 (concat "[" cur "]") "")
10568 ": ")
10569 existing nil nil "" nil cur))))
10570 (list prop (if (equal val "") cur val))))
10571 (unless (equal (org-entry-get nil property) value)
10572 (org-entry-put nil property value)))
10574 (defun org-delete-property (property)
10575 "In the current entry, delete PROPERTY."
10576 (interactive
10577 (let* ((completion-ignore-case t)
10578 (prop (completing-read
10579 "Property: " (org-entry-properties nil 'standard))))
10580 (list prop)))
10581 (message "Property %s %s" property
10582 (if (org-entry-delete nil property)
10583 "deleted"
10584 "was not present in the entry")))
10586 (defun org-delete-property-globally (property)
10587 "Remove PROPERTY globally, from all entries."
10588 (interactive
10589 (let* ((completion-ignore-case t)
10590 (prop (completing-read
10591 "Globally remove property: "
10592 (mapcar 'list (org-buffer-property-keys)))))
10593 (list prop)))
10594 (save-excursion
10595 (save-restriction
10596 (widen)
10597 (goto-char (point-min))
10598 (let ((cnt 0))
10599 (while (re-search-forward
10600 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
10601 nil t)
10602 (setq cnt (1+ cnt))
10603 (replace-match ""))
10604 (message "Property \"%s\" removed from %d entries" property cnt)))))
10606 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
10608 (defun org-compute-property-at-point ()
10609 "Compute the property at point.
10610 This looks for an enclosing column format, extracts the operator and
10611 then applies it to the proerty in the column format's scope."
10612 (interactive)
10613 (unless (org-at-property-p)
10614 (error "Not at a property"))
10615 (let ((prop (org-match-string-no-properties 2)))
10616 (org-columns-get-format-and-top-level)
10617 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
10618 (error "No operator defined for property %s" prop))
10619 (org-columns-compute prop)))
10621 (defun org-property-get-allowed-values (pom property &optional table)
10622 "Get allowed values for the property PROPERTY.
10623 When TABLE is non-nil, return an alist that can directly be used for
10624 completion."
10625 (let (vals)
10626 (cond
10627 ((equal property "TODO")
10628 (setq vals (org-with-point-at pom
10629 (append org-todo-keywords-1 '("")))))
10630 ((equal property "PRIORITY")
10631 (let ((n org-lowest-priority))
10632 (while (>= n org-highest-priority)
10633 (push (char-to-string n) vals)
10634 (setq n (1- n)))))
10635 ((member property org-special-properties))
10637 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
10639 (when (and vals (string-match "\\S-" vals))
10640 (setq vals (car (read-from-string (concat "(" vals ")"))))
10641 (setq vals (mapcar (lambda (x)
10642 (cond ((stringp x) x)
10643 ((numberp x) (number-to-string x))
10644 ((symbolp x) (symbol-name x))
10645 (t "???")))
10646 vals)))))
10647 (if table (mapcar 'list vals) vals)))
10649 (defun org-property-previous-allowed-value (&optional previous)
10650 "Switch to the next allowed value for this property."
10651 (interactive)
10652 (org-property-next-allowed-value t))
10654 (defun org-property-next-allowed-value (&optional previous)
10655 "Switch to the next allowed value for this property."
10656 (interactive)
10657 (unless (org-at-property-p)
10658 (error "Not at a property"))
10659 (let* ((key (match-string 2))
10660 (value (match-string 3))
10661 (allowed (or (org-property-get-allowed-values (point) key)
10662 (and (member value '("[ ]" "[-]" "[X]"))
10663 '("[ ]" "[X]"))))
10664 nval)
10665 (unless allowed
10666 (error "Allowed values for this property have not been defined"))
10667 (if previous (setq allowed (reverse allowed)))
10668 (if (member value allowed)
10669 (setq nval (car (cdr (member value allowed)))))
10670 (setq nval (or nval (car allowed)))
10671 (if (equal nval value)
10672 (error "Only one allowed value for this property"))
10673 (org-at-property-p)
10674 (replace-match (concat " :" key ": " nval) t t)
10675 (org-indent-line-function)
10676 (beginning-of-line 1)
10677 (skip-chars-forward " \t")))
10679 (defun org-find-entry-with-id (ident)
10680 "Locate the entry that contains the ID property with exact value IDENT.
10681 IDENT can be a string, a symbol or a number, this function will search for
10682 the string representation of it.
10683 Return the position where this entry starts, or nil if there is no such entry."
10684 (let ((id (cond
10685 ((stringp ident) ident)
10686 ((symbol-name ident) (symbol-name ident))
10687 ((numberp ident) (number-to-string ident))
10688 (t (error "IDENT %s must be a string, symbol or number" ident))))
10689 (case-fold-search nil))
10690 (save-excursion
10691 (save-restriction
10692 (widen)
10693 (goto-char (point-min))
10694 (when (re-search-forward
10695 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
10696 nil t)
10697 (org-back-to-heading)
10698 (point))))))
10700 ;;;; Timestamps
10702 (defvar org-last-changed-timestamp nil)
10703 (defvar org-last-inserted-timestamp nil
10704 "The last time stamp inserted with `org-insert-time-stamp'.")
10705 (defvar org-time-was-given) ; dynamically scoped parameter
10706 (defvar org-end-time-was-given) ; dynamically scoped parameter
10707 (defvar org-ts-what) ; dynamically scoped parameter
10709 (defun org-time-stamp (arg)
10710 "Prompt for a date/time and insert a time stamp.
10711 If the user specifies a time like HH:MM, or if this command is called
10712 with a prefix argument, the time stamp will contain date and time.
10713 Otherwise, only the date will be included. All parts of a date not
10714 specified by the user will be filled in from the current date/time.
10715 So if you press just return without typing anything, the time stamp
10716 will represent the current date/time. If there is already a timestamp
10717 at the cursor, it will be modified."
10718 (interactive "P")
10719 (let* ((ts nil)
10720 (default-time
10721 ;; Default time is either today, or, when entering a range,
10722 ;; the range start.
10723 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
10724 (save-excursion
10725 (re-search-backward
10726 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
10727 (- (point) 20) t)))
10728 (apply 'encode-time (org-parse-time-string (match-string 1)))
10729 (current-time)))
10730 (default-input (and ts (org-get-compact-tod ts)))
10731 org-time-was-given org-end-time-was-given time)
10732 (cond
10733 ((and (org-at-timestamp-p)
10734 (eq last-command 'org-time-stamp)
10735 (eq this-command 'org-time-stamp))
10736 (insert "--")
10737 (setq time (let ((this-command this-command))
10738 (org-read-date arg 'totime nil nil default-time default-input)))
10739 (org-insert-time-stamp time (or org-time-was-given arg)))
10740 ((org-at-timestamp-p)
10741 (setq time (let ((this-command this-command))
10742 (org-read-date arg 'totime nil nil default-time default-input)))
10743 (when (org-at-timestamp-p) ; just to get the match data
10744 (replace-match "")
10745 (setq org-last-changed-timestamp
10746 (org-insert-time-stamp
10747 time (or org-time-was-given arg)
10748 nil nil nil (list org-end-time-was-given))))
10749 (message "Timestamp updated"))
10751 (setq time (let ((this-command this-command))
10752 (org-read-date arg 'totime nil nil default-time default-input)))
10753 (org-insert-time-stamp time (or org-time-was-given arg)
10754 nil nil nil (list org-end-time-was-given))))))
10756 ;; FIXME: can we use this for something else, like computing time differences?
10757 (defun org-get-compact-tod (s)
10758 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
10759 (let* ((t1 (match-string 1 s))
10760 (h1 (string-to-number (match-string 2 s)))
10761 (m1 (string-to-number (match-string 3 s)))
10762 (t2 (and (match-end 4) (match-string 5 s)))
10763 (h2 (and t2 (string-to-number (match-string 6 s))))
10764 (m2 (and t2 (string-to-number (match-string 7 s))))
10765 dh dm)
10766 (if (not t2)
10768 (setq dh (- h2 h1) dm (- m2 m1))
10769 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
10770 (concat t1 "+" (number-to-string dh)
10771 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
10773 (defun org-time-stamp-inactive (&optional arg)
10774 "Insert an inactive time stamp.
10775 An inactive time stamp is enclosed in square brackets instead of angle
10776 brackets. It is inactive in the sense that it does not trigger agenda entries,
10777 does not link to the calendar and cannot be changed with the S-cursor keys.
10778 So these are more for recording a certain time/date."
10779 (interactive "P")
10780 (let (org-time-was-given org-end-time-was-given time)
10781 (setq time (org-read-date arg 'totime))
10782 (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
10783 nil nil (list org-end-time-was-given))))
10785 (defvar org-date-ovl (org-make-overlay 1 1))
10786 (org-overlay-put org-date-ovl 'face 'org-warning)
10787 (org-detach-overlay org-date-ovl)
10789 (defvar org-ans1) ; dynamically scoped parameter
10790 (defvar org-ans2) ; dynamically scoped parameter
10792 (defvar org-plain-time-of-day-regexp) ; defined below
10794 (defvar org-overriding-default-time nil) ; dynamically scoped
10795 (defvar org-read-date-overlay nil)
10796 (defvar org-dcst nil) ; dynamically scoped
10798 (defun org-read-date (&optional with-time to-time from-string prompt
10799 default-time default-input)
10800 "Read a date, possibly a time, and make things smooth for the user.
10801 The prompt will suggest to enter an ISO date, but you can also enter anything
10802 which will at least partially be understood by `parse-time-string'.
10803 Unrecognized parts of the date will default to the current day, month, year,
10804 hour and minute. If this command is called to replace a timestamp at point,
10805 of to enter the second timestamp of a range, the default time is taken from the
10806 existing stamp. For example,
10807 3-2-5 --> 2003-02-05
10808 feb 15 --> currentyear-02-15
10809 sep 12 9 --> 2009-09-12
10810 12:45 --> today 12:45
10811 22 sept 0:34 --> currentyear-09-22 0:34
10812 12 --> currentyear-currentmonth-12
10813 Fri --> nearest Friday (today or later)
10814 etc.
10816 Furthermore you can specify a relative date by giving, as the *first* thing
10817 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
10818 change in days weeks, months, years.
10819 With a single plus or minus, the date is relative to today. With a double
10820 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
10821 +4d --> four days from today
10822 +4 --> same as above
10823 +2w --> two weeks from today
10824 ++5 --> five days from default date
10826 The function understands only English month and weekday abbreviations,
10827 but this can be configured with the variables `parse-time-months' and
10828 `parse-time-weekdays'.
10830 While prompting, a calendar is popped up - you can also select the
10831 date with the mouse (button 1). The calendar shows a period of three
10832 months. To scroll it to other months, use the keys `>' and `<'.
10833 If you don't like the calendar, turn it off with
10834 \(setq org-read-date-popup-calendar nil)
10836 With optional argument TO-TIME, the date will immediately be converted
10837 to an internal time.
10838 With an optional argument WITH-TIME, the prompt will suggest to also
10839 insert a time. Note that when WITH-TIME is not set, you can still
10840 enter a time, and this function will inform the calling routine about
10841 this change. The calling routine may then choose to change the format
10842 used to insert the time stamp into the buffer to include the time.
10843 With optional argument FROM-STRING, read from this string instead from
10844 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
10845 the time/date that is used for everything that is not specified by the
10846 user."
10847 (require 'parse-time)
10848 (let* ((org-time-stamp-rounding-minutes
10849 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
10850 (org-dcst org-display-custom-times)
10851 (ct (org-current-time))
10852 (def (or org-overriding-default-time default-time ct))
10853 (defdecode (decode-time def))
10854 (dummy (progn
10855 (when (< (nth 2 defdecode) org-extend-today-until)
10856 (setcar (nthcdr 2 defdecode) -1)
10857 (setcar (nthcdr 1 defdecode) 59)
10858 (setq def (apply 'encode-time defdecode)
10859 defdecode (decode-time def)))))
10860 (calendar-move-hook nil)
10861 (calendar-view-diary-initially-flag nil)
10862 (view-diary-entries-initially nil)
10863 (calendar-view-holidays-initially-flag nil)
10864 (view-calendar-holidays-initially nil)
10865 (timestr (format-time-string
10866 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
10867 (prompt (concat (if prompt (concat prompt " ") "")
10868 (format "Date+time [%s]: " timestr)))
10869 ans (org-ans0 "") org-ans1 org-ans2 final)
10871 (cond
10872 (from-string (setq ans from-string))
10873 (org-read-date-popup-calendar
10874 (save-excursion
10875 (save-window-excursion
10876 (calendar)
10877 (calendar-forward-day (- (time-to-days def)
10878 (calendar-absolute-from-gregorian
10879 (calendar-current-date))))
10880 (org-eval-in-calendar nil t)
10881 (let* ((old-map (current-local-map))
10882 (map (copy-keymap calendar-mode-map))
10883 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
10884 (org-defkey map (kbd "RET") 'org-calendar-select)
10885 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
10886 'org-calendar-select-mouse)
10887 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
10888 'org-calendar-select-mouse)
10889 (org-defkey minibuffer-local-map [(meta shift left)]
10890 (lambda () (interactive)
10891 (org-eval-in-calendar '(calendar-backward-month 1))))
10892 (org-defkey minibuffer-local-map [(meta shift right)]
10893 (lambda () (interactive)
10894 (org-eval-in-calendar '(calendar-forward-month 1))))
10895 (org-defkey minibuffer-local-map [(meta shift up)]
10896 (lambda () (interactive)
10897 (org-eval-in-calendar '(calendar-backward-year 1))))
10898 (org-defkey minibuffer-local-map [(meta shift down)]
10899 (lambda () (interactive)
10900 (org-eval-in-calendar '(calendar-forward-year 1))))
10901 (org-defkey minibuffer-local-map [(shift up)]
10902 (lambda () (interactive)
10903 (org-eval-in-calendar '(calendar-backward-week 1))))
10904 (org-defkey minibuffer-local-map [(shift down)]
10905 (lambda () (interactive)
10906 (org-eval-in-calendar '(calendar-forward-week 1))))
10907 (org-defkey minibuffer-local-map [(shift left)]
10908 (lambda () (interactive)
10909 (org-eval-in-calendar '(calendar-backward-day 1))))
10910 (org-defkey minibuffer-local-map [(shift right)]
10911 (lambda () (interactive)
10912 (org-eval-in-calendar '(calendar-forward-day 1))))
10913 (org-defkey minibuffer-local-map ">"
10914 (lambda () (interactive)
10915 (org-eval-in-calendar '(scroll-calendar-left 1))))
10916 (org-defkey minibuffer-local-map "<"
10917 (lambda () (interactive)
10918 (org-eval-in-calendar '(scroll-calendar-right 1))))
10919 (unwind-protect
10920 (progn
10921 (use-local-map map)
10922 (add-hook 'post-command-hook 'org-read-date-display)
10923 (setq org-ans0 (read-string prompt default-input nil nil))
10924 ;; org-ans0: from prompt
10925 ;; org-ans1: from mouse click
10926 ;; org-ans2: from calendar motion
10927 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
10928 (remove-hook 'post-command-hook 'org-read-date-display)
10929 (use-local-map old-map)
10930 (when org-read-date-overlay
10931 (org-delete-overlay org-read-date-overlay)
10932 (setq org-read-date-overlay nil)))))))
10934 (t ; Naked prompt only
10935 (unwind-protect
10936 (setq ans (read-string prompt default-input nil timestr))
10937 (when org-read-date-overlay
10938 (org-delete-overlay org-read-date-overlay)
10939 (setq org-read-date-overlay nil)))))
10941 (setq final (org-read-date-analyze ans def defdecode))
10943 (if to-time
10944 (apply 'encode-time final)
10945 (if (and (boundp 'org-time-was-given) org-time-was-given)
10946 (format "%04d-%02d-%02d %02d:%02d"
10947 (nth 5 final) (nth 4 final) (nth 3 final)
10948 (nth 2 final) (nth 1 final))
10949 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
10950 (defvar def)
10951 (defvar defdecode)
10952 (defvar with-time)
10953 (defun org-read-date-display ()
10954 "Display the currrent date prompt interpretation in the minibuffer."
10955 (when org-read-date-display-live
10956 (when org-read-date-overlay
10957 (org-delete-overlay org-read-date-overlay))
10958 (let ((p (point)))
10959 (end-of-line 1)
10960 (while (not (equal (buffer-substring
10961 (max (point-min) (- (point) 4)) (point))
10962 " "))
10963 (insert " "))
10964 (goto-char p))
10965 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
10966 " " (or org-ans1 org-ans2)))
10967 (org-end-time-was-given nil)
10968 (f (org-read-date-analyze ans def defdecode))
10969 (fmts (if org-dcst
10970 org-time-stamp-custom-formats
10971 org-time-stamp-formats))
10972 (fmt (if (or with-time
10973 (and (boundp 'org-time-was-given) org-time-was-given))
10974 (cdr fmts)
10975 (car fmts)))
10976 (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
10977 (when (and org-end-time-was-given
10978 (string-match org-plain-time-of-day-regexp txt))
10979 (setq txt (concat (substring txt 0 (match-end 0)) "-"
10980 org-end-time-was-given
10981 (substring txt (match-end 0)))))
10982 (setq org-read-date-overlay
10983 (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
10984 (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
10986 (defun org-read-date-analyze (ans def defdecode)
10987 "Analyze the combined answer of the date prompt."
10988 ;; FIXME: cleanup and comment
10989 (let (delta deltan deltaw deltadef year month day
10990 hour minute second wday pm h2 m2 tl wday1
10991 iso-year iso-weekday iso-week iso-year iso-date)
10993 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
10994 (setq ans "+0"))
10996 (when (setq delta (org-read-date-get-relative ans (current-time) def))
10997 (setq ans (replace-match "" t t ans)
10998 deltan (car delta)
10999 deltaw (nth 1 delta)
11000 deltadef (nth 2 delta)))
11002 ;; Check if there is an iso week date in there
11003 ;; If yes, sore the info and ostpone interpreting it until the rest
11004 ;; of the parsing is done
11005 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
11006 (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
11007 iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
11008 iso-week (string-to-number (match-string 2 ans)))
11009 (setq ans (replace-match "" t t ans)))
11011 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
11012 (when (string-match
11013 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
11014 (setq year (if (match-end 2)
11015 (string-to-number (match-string 2 ans))
11016 (string-to-number (format-time-string "%Y")))
11017 month (string-to-number (match-string 3 ans))
11018 day (string-to-number (match-string 4 ans)))
11019 (if (< year 100) (setq year (+ 2000 year)))
11020 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
11021 t nil ans)))
11022 ;; Help matching am/pm times, because `parse-time-string' does not do that.
11023 ;; If there is a time with am/pm, and *no* time without it, we convert
11024 ;; so that matching will be successful.
11025 (loop for i from 1 to 2 do ; twice, for end time as well
11026 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
11027 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
11028 (setq hour (string-to-number (match-string 1 ans))
11029 minute (if (match-end 3)
11030 (string-to-number (match-string 3 ans))
11032 pm (equal ?p
11033 (string-to-char (downcase (match-string 4 ans)))))
11034 (if (and (= hour 12) (not pm))
11035 (setq hour 0)
11036 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
11037 (setq ans (replace-match (format "%02d:%02d" hour minute)
11038 t t ans))))
11040 ;; Check if a time range is given as a duration
11041 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
11042 (setq hour (string-to-number (match-string 1 ans))
11043 h2 (+ hour (string-to-number (match-string 3 ans)))
11044 minute (string-to-number (match-string 2 ans))
11045 m2 (+ minute (if (match-end 5) (string-to-number
11046 (match-string 5 ans))0)))
11047 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
11048 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
11049 t t ans)))
11051 ;; Check if there is a time range
11052 (when (boundp 'org-end-time-was-given)
11053 (setq org-time-was-given nil)
11054 (when (and (string-match org-plain-time-of-day-regexp ans)
11055 (match-end 8))
11056 (setq org-end-time-was-given (match-string 8 ans))
11057 (setq ans (concat (substring ans 0 (match-beginning 7))
11058 (substring ans (match-end 7))))))
11060 (setq tl (parse-time-string ans)
11061 day (or (nth 3 tl) (nth 3 defdecode))
11062 month (or (nth 4 tl)
11063 (if (and org-read-date-prefer-future
11064 (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
11065 (1+ (nth 4 defdecode))
11066 (nth 4 defdecode)))
11067 year (or (nth 5 tl)
11068 (if (and org-read-date-prefer-future
11069 (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
11070 (1+ (nth 5 defdecode))
11071 (nth 5 defdecode)))
11072 hour (or (nth 2 tl) (nth 2 defdecode))
11073 minute (or (nth 1 tl) (nth 1 defdecode))
11074 second (or (nth 0 tl) 0)
11075 wday (nth 6 tl))
11077 ;; Special date definitions below
11078 (cond
11079 (iso-week
11080 ;; There was an iso week
11081 (setq year (or iso-year year)
11082 day (or iso-weekday wday 1)
11083 wday nil ; to make sure that the trigger below does not match
11084 iso-date (calendar-gregorian-from-absolute
11085 (calendar-absolute-from-iso
11086 (list iso-week day year))))
11087 ; FIXME: Should we also push ISO weeks into the future?
11088 ; (when (and org-read-date-prefer-future
11089 ; (not iso-year)
11090 ; (< (calendar-absolute-from-gregorian iso-date)
11091 ; (time-to-days (current-time))))
11092 ; (setq year (1+ year)
11093 ; iso-date (calendar-gregorian-from-absolute
11094 ; (calendar-absolute-from-iso
11095 ; (list iso-week day year)))))
11096 (setq month (car iso-date)
11097 year (nth 2 iso-date)
11098 day (nth 1 iso-date)))
11099 (deltan
11100 (unless deltadef
11101 (let ((now (decode-time (current-time))))
11102 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
11103 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
11104 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
11105 ((equal deltaw "m") (setq month (+ month deltan)))
11106 ((equal deltaw "y") (setq year (+ year deltan)))))
11107 ((and wday (not (nth 3 tl)))
11108 ;; Weekday was given, but no day, so pick that day in the week
11109 ;; on or after the derived date.
11110 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
11111 (unless (equal wday wday1)
11112 (setq day (+ day (% (- wday wday1 -7) 7))))))
11113 (if (and (boundp 'org-time-was-given)
11114 (nth 2 tl))
11115 (setq org-time-was-given t))
11116 (if (< year 100) (setq year (+ 2000 year)))
11117 (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
11118 (list second minute hour day month year)))
11120 (defvar parse-time-weekdays)
11122 (defun org-read-date-get-relative (s today default)
11123 "Check string S for special relative date string.
11124 TODAY and DEFAULT are internal times, for today and for a default.
11125 Return shift list (N what def-flag)
11126 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
11127 N is the number of WHATs to shift.
11128 DEF-FLAG is t when a double ++ or -- indicates shift relative to
11129 the DEFAULT date rather than TODAY."
11130 (when (and
11131 (string-match
11132 (concat
11133 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
11134 "\\([0-9]+\\)?"
11135 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
11136 "\\([ \t]\\|$\\)") s)
11137 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
11138 (let* ((dir (if (> (match-end 1) (match-beginning 1))
11139 (string-to-char (substring (match-string 1 s) -1))
11140 ?+))
11141 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
11142 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
11143 (what (if (match-end 3) (match-string 3 s) "d"))
11144 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
11145 (date (if rel default today))
11146 (wday (nth 6 (decode-time date)))
11147 delta)
11148 (if wday1
11149 (progn
11150 (setq delta (mod (+ 7 (- wday1 wday)) 7))
11151 (if (= dir ?-) (setq delta (- delta 7)))
11152 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
11153 (list delta "d" rel))
11154 (list (* n (if (= dir ?-) -1 1)) what rel)))))
11156 (defun org-eval-in-calendar (form &optional keepdate)
11157 "Eval FORM in the calendar window and return to current window.
11158 Also, store the cursor date in variable org-ans2."
11159 (let ((sw (selected-window)))
11160 (select-window (get-buffer-window "*Calendar*"))
11161 (eval form)
11162 (when (and (not keepdate) (calendar-cursor-to-date))
11163 (let* ((date (calendar-cursor-to-date))
11164 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
11165 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
11166 (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
11167 (select-window sw)))
11169 ; ;; Update the prompt to show new default date
11170 ; (save-excursion
11171 ; (goto-char (point-min))
11172 ; (when (and org-ans2
11173 ; (re-search-forward "\\[[-0-9]+\\]" nil t)
11174 ; (get-text-property (match-end 0) 'field))
11175 ; (let ((inhibit-read-only t))
11176 ; (replace-match (concat "[" org-ans2 "]") t t)
11177 ; (add-text-properties (point-min) (1+ (match-end 0))
11178 ; (text-properties-at (1+ (point-min)))))))))
11180 (defun org-calendar-select ()
11181 "Return to `org-read-date' with the date currently selected.
11182 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
11183 (interactive)
11184 (when (calendar-cursor-to-date)
11185 (let* ((date (calendar-cursor-to-date))
11186 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
11187 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
11188 (if (active-minibuffer-window) (exit-minibuffer))))
11190 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
11191 "Insert a date stamp for the date given by the internal TIME.
11192 WITH-HM means, use the stamp format that includes the time of the day.
11193 INACTIVE means use square brackets instead of angular ones, so that the
11194 stamp will not contribute to the agenda.
11195 PRE and POST are optional strings to be inserted before and after the
11196 stamp.
11197 The command returns the inserted time stamp."
11198 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
11199 stamp)
11200 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
11201 (insert-before-markers (or pre ""))
11202 (insert-before-markers (setq stamp (format-time-string fmt time)))
11203 (when (listp extra)
11204 (setq extra (car extra))
11205 (if (and (stringp extra)
11206 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
11207 (setq extra (format "-%02d:%02d"
11208 (string-to-number (match-string 1 extra))
11209 (string-to-number (match-string 2 extra))))
11210 (setq extra nil)))
11211 (when extra
11212 (backward-char 1)
11213 (insert-before-markers extra)
11214 (forward-char 1))
11215 (insert-before-markers (or post ""))
11216 (setq org-last-inserted-timestamp stamp)))
11218 (defun org-toggle-time-stamp-overlays ()
11219 "Toggle the use of custom time stamp formats."
11220 (interactive)
11221 (setq org-display-custom-times (not org-display-custom-times))
11222 (unless org-display-custom-times
11223 (let ((p (point-min)) (bmp (buffer-modified-p)))
11224 (while (setq p (next-single-property-change p 'display))
11225 (if (and (get-text-property p 'display)
11226 (eq (get-text-property p 'face) 'org-date))
11227 (remove-text-properties
11228 p (setq p (next-single-property-change p 'display))
11229 '(display t))))
11230 (set-buffer-modified-p bmp)))
11231 (if (featurep 'xemacs)
11232 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
11233 (org-restart-font-lock)
11234 (setq org-table-may-need-update t)
11235 (if org-display-custom-times
11236 (message "Time stamps are overlayed with custom format")
11237 (message "Time stamp overlays removed")))
11239 (defun org-display-custom-time (beg end)
11240 "Overlay modified time stamp format over timestamp between BEG and END."
11241 (let* ((ts (buffer-substring beg end))
11242 t1 w1 with-hm tf time str w2 (off 0))
11243 (save-match-data
11244 (setq t1 (org-parse-time-string ts t))
11245 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
11246 (setq off (- (match-end 0) (match-beginning 0)))))
11247 (setq end (- end off))
11248 (setq w1 (- end beg)
11249 with-hm (and (nth 1 t1) (nth 2 t1))
11250 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
11251 time (org-fix-decoded-time t1)
11252 str (org-add-props
11253 (format-time-string
11254 (substring tf 1 -1) (apply 'encode-time time))
11255 nil 'mouse-face 'highlight)
11256 w2 (length str))
11257 (if (not (= w2 w1))
11258 (add-text-properties (1+ beg) (+ 2 beg)
11259 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
11260 (if (featurep 'xemacs)
11261 (progn
11262 (put-text-property beg end 'invisible t)
11263 (put-text-property beg end 'end-glyph (make-glyph str)))
11264 (put-text-property beg end 'display str))))
11266 (defun org-translate-time (string)
11267 "Translate all timestamps in STRING to custom format.
11268 But do this only if the variable `org-display-custom-times' is set."
11269 (when org-display-custom-times
11270 (save-match-data
11271 (let* ((start 0)
11272 (re org-ts-regexp-both)
11273 t1 with-hm inactive tf time str beg end)
11274 (while (setq start (string-match re string start))
11275 (setq beg (match-beginning 0)
11276 end (match-end 0)
11277 t1 (save-match-data
11278 (org-parse-time-string (substring string beg end) t))
11279 with-hm (and (nth 1 t1) (nth 2 t1))
11280 inactive (equal (substring string beg (1+ beg)) "[")
11281 tf (funcall (if with-hm 'cdr 'car)
11282 org-time-stamp-custom-formats)
11283 time (org-fix-decoded-time t1)
11284 str (format-time-string
11285 (concat
11286 (if inactive "[" "<") (substring tf 1 -1)
11287 (if inactive "]" ">"))
11288 (apply 'encode-time time))
11289 string (replace-match str t t string)
11290 start (+ start (length str)))))))
11291 string)
11293 (defun org-fix-decoded-time (time)
11294 "Set 0 instead of nil for the first 6 elements of time.
11295 Don't touch the rest."
11296 (let ((n 0))
11297 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
11299 (defun org-days-to-time (timestamp-string)
11300 "Difference between TIMESTAMP-STRING and now in days."
11301 (- (time-to-days (org-time-string-to-time timestamp-string))
11302 (time-to-days (current-time))))
11304 (defun org-deadline-close (timestamp-string &optional ndays)
11305 "Is the time in TIMESTAMP-STRING close to the current date?"
11306 (setq ndays (or ndays (org-get-wdays timestamp-string)))
11307 (and (< (org-days-to-time timestamp-string) ndays)
11308 (not (org-entry-is-done-p))))
11310 (defun org-get-wdays (ts)
11311 "Get the deadline lead time appropriate for timestring TS."
11312 (cond
11313 ((<= org-deadline-warning-days 0)
11314 ;; 0 or negative, enforce this value no matter what
11315 (- org-deadline-warning-days))
11316 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
11317 ;; lead time is specified.
11318 (floor (* (string-to-number (match-string 1 ts))
11319 (cdr (assoc (match-string 2 ts)
11320 '(("d" . 1) ("w" . 7)
11321 ("m" . 30.4) ("y" . 365.25)))))))
11322 ;; go for the default.
11323 (t org-deadline-warning-days)))
11325 (defun org-calendar-select-mouse (ev)
11326 "Return to `org-read-date' with the date currently selected.
11327 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
11328 (interactive "e")
11329 (mouse-set-point ev)
11330 (when (calendar-cursor-to-date)
11331 (let* ((date (calendar-cursor-to-date))
11332 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
11333 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
11334 (if (active-minibuffer-window) (exit-minibuffer))))
11336 (defun org-check-deadlines (ndays)
11337 "Check if there are any deadlines due or past due.
11338 A deadline is considered due if it happens within `org-deadline-warning-days'
11339 days from today's date. If the deadline appears in an entry marked DONE,
11340 it is not shown. The prefix arg NDAYS can be used to test that many
11341 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
11342 (interactive "P")
11343 (let* ((org-warn-days
11344 (cond
11345 ((equal ndays '(4)) 100000)
11346 (ndays (prefix-numeric-value ndays))
11347 (t (abs org-deadline-warning-days))))
11348 (case-fold-search nil)
11349 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
11350 (callback
11351 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
11353 (message "%d deadlines past-due or due within %d days"
11354 (org-occur regexp nil callback)
11355 org-warn-days)))
11357 (defun org-check-before-date (date)
11358 "Check if there are deadlines or scheduled entries before DATE."
11359 (interactive (list (org-read-date)))
11360 (let ((case-fold-search nil)
11361 (regexp (concat "\\<\\(" org-deadline-string
11362 "\\|" org-scheduled-string
11363 "\\) *<\\([^>]+\\)>"))
11364 (callback
11365 (lambda () (time-less-p
11366 (org-time-string-to-time (match-string 2))
11367 (org-time-string-to-time date)))))
11368 (message "%d entries before %s"
11369 (org-occur regexp nil callback) date)))
11371 (defun org-evaluate-time-range (&optional to-buffer)
11372 "Evaluate a time range by computing the difference between start and end.
11373 Normally the result is just printed in the echo area, but with prefix arg
11374 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
11375 If the time range is actually in a table, the result is inserted into the
11376 next column.
11377 For time difference computation, a year is assumed to be exactly 365
11378 days in order to avoid rounding problems."
11379 (interactive "P")
11381 (org-clock-update-time-maybe)
11382 (save-excursion
11383 (unless (org-at-date-range-p t)
11384 (goto-char (point-at-bol))
11385 (re-search-forward org-tr-regexp-both (point-at-eol) t))
11386 (if (not (org-at-date-range-p t))
11387 (error "Not at a time-stamp range, and none found in current line")))
11388 (let* ((ts1 (match-string 1))
11389 (ts2 (match-string 2))
11390 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
11391 (match-end (match-end 0))
11392 (time1 (org-time-string-to-time ts1))
11393 (time2 (org-time-string-to-time ts2))
11394 (t1 (time-to-seconds time1))
11395 (t2 (time-to-seconds time2))
11396 (diff (abs (- t2 t1)))
11397 (negative (< (- t2 t1) 0))
11398 ;; (ys (floor (* 365 24 60 60)))
11399 (ds (* 24 60 60))
11400 (hs (* 60 60))
11401 (fy "%dy %dd %02d:%02d")
11402 (fy1 "%dy %dd")
11403 (fd "%dd %02d:%02d")
11404 (fd1 "%dd")
11405 (fh "%02d:%02d")
11406 y d h m align)
11407 (if havetime
11408 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
11410 d (floor (/ diff ds)) diff (mod diff ds)
11411 h (floor (/ diff hs)) diff (mod diff hs)
11412 m (floor (/ diff 60)))
11413 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
11415 d (floor (+ (/ diff ds) 0.5))
11416 h 0 m 0))
11417 (if (not to-buffer)
11418 (message "%s" (org-make-tdiff-string y d h m))
11419 (if (org-at-table-p)
11420 (progn
11421 (goto-char match-end)
11422 (setq align t)
11423 (and (looking-at " *|") (goto-char (match-end 0))))
11424 (goto-char match-end))
11425 (if (looking-at
11426 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
11427 (replace-match ""))
11428 (if negative (insert " -"))
11429 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
11430 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
11431 (insert " " (format fh h m))))
11432 (if align (org-table-align))
11433 (message "Time difference inserted")))))
11435 (defun org-make-tdiff-string (y d h m)
11436 (let ((fmt "")
11437 (l nil))
11438 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
11439 l (push y l)))
11440 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
11441 l (push d l)))
11442 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
11443 l (push h l)))
11444 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
11445 l (push m l)))
11446 (apply 'format fmt (nreverse l))))
11448 (defun org-time-string-to-time (s)
11449 (apply 'encode-time (org-parse-time-string s)))
11451 (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
11452 "Convert a time stamp to an absolute day number.
11453 If there is a specifyer for a cyclic time stamp, get the closest date to
11454 DAYNR.
11455 PREFER and SHOW_ALL are passed through to `org-closest-date'."
11456 (cond
11457 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
11458 (if (org-diary-sexp-entry (match-string 1 s) "" date)
11459 daynr
11460 (+ daynr 1000)))
11461 ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
11462 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
11463 (time-to-days (current-time))) (match-string 0 s)
11464 prefer show-all))
11465 (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
11467 (defun org-days-to-iso-week (days)
11468 "Return the iso week number."
11469 (require 'cal-iso)
11470 (car (calendar-iso-from-absolute days)))
11472 (defun org-small-year-to-year (year)
11473 "Convert 2-digit years into 4-digit years.
11474 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
11475 The year 2000 cannot be abbreviated. Any year lager than 99
11476 is retrned unchanged."
11477 (if (< year 38)
11478 (setq year (+ 2000 year))
11479 (if (< year 100)
11480 (setq year (+ 1900 year))))
11481 year)
11483 (defun org-time-from-absolute (d)
11484 "Return the time corresponding to date D.
11485 D may be an absolute day number, or a calendar-type list (month day year)."
11486 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
11487 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
11489 (defun org-calendar-holiday ()
11490 "List of holidays, for Diary display in Org-mode."
11491 (require 'holidays)
11492 (let ((hl (funcall
11493 (if (fboundp 'calendar-check-holidays)
11494 'calendar-check-holidays 'check-calendar-holidays) date)))
11495 (if hl (mapconcat 'identity hl "; "))))
11497 (defun org-diary-sexp-entry (sexp entry date)
11498 "Process a SEXP diary ENTRY for DATE."
11499 (require 'diary-lib)
11500 (let ((result (if calendar-debug-sexp
11501 (let ((stack-trace-on-error t))
11502 (eval (car (read-from-string sexp))))
11503 (condition-case nil
11504 (eval (car (read-from-string sexp)))
11505 (error
11506 (beep)
11507 (message "Bad sexp at line %d in %s: %s"
11508 (org-current-line)
11509 (buffer-file-name) sexp)
11510 (sleep-for 2))))))
11511 (cond ((stringp result) result)
11512 ((and (consp result)
11513 (stringp (cdr result))) (cdr result))
11514 (result entry)
11515 (t nil))))
11517 (defun org-diary-to-ical-string (frombuf)
11518 "Get iCalendar entries from diary entries in buffer FROMBUF.
11519 This uses the icalendar.el library."
11520 (let* ((tmpdir (if (featurep 'xemacs)
11521 (temp-directory)
11522 temporary-file-directory))
11523 (tmpfile (make-temp-name
11524 (expand-file-name "orgics" tmpdir)))
11525 buf rtn b e)
11526 (save-excursion
11527 (set-buffer frombuf)
11528 (icalendar-export-region (point-min) (point-max) tmpfile)
11529 (setq buf (find-buffer-visiting tmpfile))
11530 (set-buffer buf)
11531 (goto-char (point-min))
11532 (if (re-search-forward "^BEGIN:VEVENT" nil t)
11533 (setq b (match-beginning 0)))
11534 (goto-char (point-max))
11535 (if (re-search-backward "^END:VEVENT" nil t)
11536 (setq e (match-end 0)))
11537 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
11538 (kill-buffer buf)
11539 (delete-file tmpfile)
11540 rtn))
11542 (defun org-closest-date (start current change prefer show-all)
11543 "Find the date closest to CURRENT that is consistent with START and CHANGE.
11544 When PREFER is `past' return a date that is either CURRENT or past.
11545 When PREFER is `future', return a date that is either CURRENT or future.
11546 When SHOW-ALL is nil, only return the current occurence of a time stamp."
11547 ;; Make the proper lists from the dates
11548 (catch 'exit
11549 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
11550 dn dw sday cday n1 n2
11551 d m y y1 y2 date1 date2 nmonths nm ny m2)
11553 (setq start (org-date-to-gregorian start)
11554 current (org-date-to-gregorian
11555 (if show-all
11556 current
11557 (time-to-days (current-time))))
11558 sday (calendar-absolute-from-gregorian start)
11559 cday (calendar-absolute-from-gregorian current))
11561 (if (<= cday sday) (throw 'exit sday))
11563 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
11564 (setq dn (string-to-number (match-string 1 change))
11565 dw (cdr (assoc (match-string 2 change) a1)))
11566 (error "Invalid change specifyer: %s" change))
11567 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
11568 (cond
11569 ((eq dw 'day)
11570 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
11571 n2 (+ n1 dn)))
11572 ((eq dw 'year)
11573 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
11574 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
11575 (setq date1 (list m d y1)
11576 n1 (calendar-absolute-from-gregorian date1)
11577 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
11578 n2 (calendar-absolute-from-gregorian date2)))
11579 ((eq dw 'month)
11580 ;; approx number of month between the two dates
11581 (setq nmonths (floor (/ (- cday sday) 30.436875)))
11582 ;; How often does dn fit in there?
11583 (setq d (nth 1 start) m (car start) y (nth 2 start)
11584 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
11585 m (+ m nm)
11586 ny (floor (/ m 12))
11587 y (+ y ny)
11588 m (- m (* ny 12)))
11589 (while (> m 12) (setq m (- m 12) y (1+ y)))
11590 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
11591 (setq m2 (+ m dn) y2 y)
11592 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
11593 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
11594 (while (<= n2 cday)
11595 (setq n1 n2 m m2 y y2)
11596 (setq m2 (+ m dn) y2 y)
11597 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
11598 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
11599 (if show-all
11600 (cond
11601 ((eq prefer 'past) n1)
11602 ((eq prefer 'future) (if (= cday n1) n1 n2))
11603 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
11604 (cond
11605 ((eq prefer 'past) n1)
11606 ((eq prefer 'future) (if (= cday n1) n1 n2))
11607 (t (if (= cday n1) n1 n2)))))))
11609 (defun org-date-to-gregorian (date)
11610 "Turn any specification of DATE into a gregorian date for the calendar."
11611 (cond ((integerp date) (calendar-gregorian-from-absolute date))
11612 ((and (listp date) (= (length date) 3)) date)
11613 ((stringp date)
11614 (setq date (org-parse-time-string date))
11615 (list (nth 4 date) (nth 3 date) (nth 5 date)))
11616 ((listp date)
11617 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
11619 (defun org-parse-time-string (s &optional nodefault)
11620 "Parse the standard Org-mode time string.
11621 This should be a lot faster than the normal `parse-time-string'.
11622 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
11623 hour and minute fields will be nil if not given."
11624 (if (string-match org-ts-regexp0 s)
11625 (list 0
11626 (if (or (match-beginning 8) (not nodefault))
11627 (string-to-number (or (match-string 8 s) "0")))
11628 (if (or (match-beginning 7) (not nodefault))
11629 (string-to-number (or (match-string 7 s) "0")))
11630 (string-to-number (match-string 4 s))
11631 (string-to-number (match-string 3 s))
11632 (string-to-number (match-string 2 s))
11633 nil nil nil)
11634 (make-list 9 0)))
11636 (defun org-timestamp-up (&optional arg)
11637 "Increase the date item at the cursor by one.
11638 If the cursor is on the year, change the year. If it is on the month or
11639 the day, change that.
11640 With prefix ARG, change by that many units."
11641 (interactive "p")
11642 (org-timestamp-change (prefix-numeric-value arg)))
11644 (defun org-timestamp-down (&optional arg)
11645 "Decrease the date item at the cursor by one.
11646 If the cursor is on the year, change the year. If it is on the month or
11647 the day, change that.
11648 With prefix ARG, change by that many units."
11649 (interactive "p")
11650 (org-timestamp-change (- (prefix-numeric-value arg))))
11652 (defun org-timestamp-up-day (&optional arg)
11653 "Increase the date in the time stamp by one day.
11654 With prefix ARG, change that many days."
11655 (interactive "p")
11656 (if (and (not (org-at-timestamp-p t))
11657 (org-on-heading-p))
11658 (org-todo 'up)
11659 (org-timestamp-change (prefix-numeric-value arg) 'day)))
11661 (defun org-timestamp-down-day (&optional arg)
11662 "Decrease the date in the time stamp by one day.
11663 With prefix ARG, change that many days."
11664 (interactive "p")
11665 (if (and (not (org-at-timestamp-p t))
11666 (org-on-heading-p))
11667 (org-todo 'down)
11668 (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
11670 (defun org-at-timestamp-p (&optional inactive-ok)
11671 "Determine if the cursor is in or at a timestamp."
11672 (interactive)
11673 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
11674 (pos (point))
11675 (ans (or (looking-at tsr)
11676 (save-excursion
11677 (skip-chars-backward "^[<\n\r\t")
11678 (if (> (point) (point-min)) (backward-char 1))
11679 (and (looking-at tsr)
11680 (> (- (match-end 0) pos) -1))))))
11681 (and ans
11682 (boundp 'org-ts-what)
11683 (setq org-ts-what
11684 (cond
11685 ((= pos (match-beginning 0)) 'bracket)
11686 ((= pos (1- (match-end 0))) 'bracket)
11687 ((org-pos-in-match-range pos 2) 'year)
11688 ((org-pos-in-match-range pos 3) 'month)
11689 ((org-pos-in-match-range pos 7) 'hour)
11690 ((org-pos-in-match-range pos 8) 'minute)
11691 ((or (org-pos-in-match-range pos 4)
11692 (org-pos-in-match-range pos 5)) 'day)
11693 ((and (> pos (or (match-end 8) (match-end 5)))
11694 (< pos (match-end 0)))
11695 (- pos (or (match-end 8) (match-end 5))))
11696 (t 'day))))
11697 ans))
11699 (defun org-toggle-timestamp-type ()
11700 "Toggle the type (<active> or [inactive]) of a time stamp."
11701 (interactive)
11702 (when (org-at-timestamp-p t)
11703 (save-excursion
11704 (goto-char (match-beginning 0))
11705 (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
11706 (goto-char (1- (match-end 0)))
11707 (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
11708 (message "Timestamp is now %sactive"
11709 (if (equal (char-before) ?>) "in" ""))))
11711 (defun org-timestamp-change (n &optional what)
11712 "Change the date in the time stamp at point.
11713 The date will be changed by N times WHAT. WHAT can be `day', `month',
11714 `year', `minute', `second'. If WHAT is not given, the cursor position
11715 in the timestamp determines what will be changed."
11716 (let ((pos (point))
11717 with-hm inactive
11718 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
11719 org-ts-what
11720 extra rem
11721 ts time time0)
11722 (if (not (org-at-timestamp-p t))
11723 (error "Not at a timestamp"))
11724 (if (and (not what) (eq org-ts-what 'bracket))
11725 (org-toggle-timestamp-type)
11726 (if (and (not what) (not (eq org-ts-what 'day))
11727 org-display-custom-times
11728 (get-text-property (point) 'display)
11729 (not (get-text-property (1- (point)) 'display)))
11730 (setq org-ts-what 'day))
11731 (setq org-ts-what (or what org-ts-what)
11732 inactive (= (char-after (match-beginning 0)) ?\[)
11733 ts (match-string 0))
11734 (replace-match "")
11735 (if (string-match
11736 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
11738 (setq extra (match-string 1 ts)))
11739 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
11740 (setq with-hm t))
11741 (setq time0 (org-parse-time-string ts))
11742 (when (and (eq org-ts-what 'minute)
11743 (eq current-prefix-arg nil))
11744 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
11745 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
11746 (setcar (cdr time0) (+ (nth 1 time0)
11747 (if (> n 0) (- rem) (- dm rem))))))
11748 (setq time
11749 (encode-time (or (car time0) 0)
11750 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
11751 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
11752 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
11753 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
11754 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
11755 (nthcdr 6 time0)))
11756 (when (integerp org-ts-what)
11757 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
11758 (if (eq what 'calendar)
11759 (let ((cal-date (org-get-date-from-calendar)))
11760 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
11761 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
11762 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
11763 (setcar time0 (or (car time0) 0))
11764 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
11765 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
11766 (setq time (apply 'encode-time time0))))
11767 (setq org-last-changed-timestamp
11768 (org-insert-time-stamp time with-hm inactive nil nil extra))
11769 (org-clock-update-time-maybe)
11770 (goto-char pos)
11771 ;; Try to recenter the calendar window, if any
11772 (if (and org-calendar-follow-timestamp-change
11773 (get-buffer-window "*Calendar*" t)
11774 (memq org-ts-what '(day month year)))
11775 (org-recenter-calendar (time-to-days time))))))
11777 (defun org-modify-ts-extra (s pos n dm)
11778 "Change the different parts of the lead-time and repeat fields in timestamp."
11779 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
11780 ng h m new rem)
11781 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
11782 (cond
11783 ((or (org-pos-in-match-range pos 2)
11784 (org-pos-in-match-range pos 3))
11785 (setq m (string-to-number (match-string 3 s))
11786 h (string-to-number (match-string 2 s)))
11787 (if (org-pos-in-match-range pos 2)
11788 (setq h (+ h n))
11789 (setq n (* dm (org-no-warnings (signum n))))
11790 (when (not (= 0 (setq rem (% m dm))))
11791 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
11792 (setq m (+ m n)))
11793 (if (< m 0) (setq m (+ m 60) h (1- h)))
11794 (if (> m 59) (setq m (- m 60) h (1+ h)))
11795 (setq h (min 24 (max 0 h)))
11796 (setq ng 1 new (format "-%02d:%02d" h m)))
11797 ((org-pos-in-match-range pos 6)
11798 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
11799 ((org-pos-in-match-range pos 5)
11800 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
11802 ((org-pos-in-match-range pos 9)
11803 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
11804 ((org-pos-in-match-range pos 8)
11805 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
11807 (when ng
11808 (setq s (concat
11809 (substring s 0 (match-beginning ng))
11811 (substring s (match-end ng))))))
11814 (defun org-recenter-calendar (date)
11815 "If the calendar is visible, recenter it to DATE."
11816 (let* ((win (selected-window))
11817 (cwin (get-buffer-window "*Calendar*" t))
11818 (calendar-move-hook nil))
11819 (when cwin
11820 (select-window cwin)
11821 (calendar-goto-date (if (listp date) date
11822 (calendar-gregorian-from-absolute date)))
11823 (select-window win))))
11825 (defun org-goto-calendar (&optional arg)
11826 "Go to the Emacs calendar at the current date.
11827 If there is a time stamp in the current line, go to that date.
11828 A prefix ARG can be used to force the current date."
11829 (interactive "P")
11830 (let ((tsr org-ts-regexp) diff
11831 (calendar-move-hook nil)
11832 (calendar-view-holidays-initially-flag nil)
11833 (view-calendar-holidays-initially nil)
11834 (calendar-view-diary-initially-flag nil)
11835 (view-diary-entries-initially nil))
11836 (if (or (org-at-timestamp-p)
11837 (save-excursion
11838 (beginning-of-line 1)
11839 (looking-at (concat ".*" tsr))))
11840 (let ((d1 (time-to-days (current-time)))
11841 (d2 (time-to-days
11842 (org-time-string-to-time (match-string 1)))))
11843 (setq diff (- d2 d1))))
11844 (calendar)
11845 (calendar-goto-today)
11846 (if (and diff (not arg)) (calendar-forward-day diff))))
11848 (defun org-get-date-from-calendar ()
11849 "Return a list (month day year) of date at point in calendar."
11850 (with-current-buffer "*Calendar*"
11851 (save-match-data
11852 (calendar-cursor-to-date))))
11854 (defun org-date-from-calendar ()
11855 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
11856 If there is already a time stamp at the cursor position, update it."
11857 (interactive)
11858 (if (org-at-timestamp-p t)
11859 (org-timestamp-change 0 'calendar)
11860 (let ((cal-date (org-get-date-from-calendar)))
11861 (org-insert-time-stamp
11862 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
11864 (defun org-minutes-to-hh:mm-string (m)
11865 "Compute H:MM from a number of minutes."
11866 (let ((h (/ m 60)))
11867 (setq m (- m (* 60 h)))
11868 (format org-time-clocksum-format h m)))
11870 (defun org-hh:mm-string-to-minutes (s)
11871 "Convert a string H:MM to a number of minutes."
11872 (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
11873 (+ (* (string-to-number (match-string 1 s)) 60)
11874 (string-to-number (match-string 2 s)))
11877 ;;;; Agenda files
11879 ;;;###autoload
11880 (defun org-iswitchb (&optional arg)
11881 "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
11882 With a prefix argument, restrict available to files.
11883 With two prefix arguments, restrict available buffers to agenda files.
11885 Due to some yet unresolved reason, the global function
11886 `iswitchb-mode' needs to be active for this function to work."
11887 (interactive "P")
11888 (require 'iswitchb)
11889 (let ((enabled iswitchb-mode) blist)
11890 (or enabled (iswitchb-mode 1))
11891 (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
11892 ((equal arg '(16)) (org-buffer-list 'agenda))
11893 (t (org-buffer-list))))
11894 (unwind-protect
11895 (let ((iswitchb-make-buflist-hook
11896 (lambda ()
11897 (setq iswitchb-temp-buflist
11898 (mapcar 'buffer-name blist)))))
11899 (switch-to-buffer
11900 (iswitchb-read-buffer
11901 "Switch-to: " nil t))
11902 (or enabled (iswitchb-mode -1))))))
11904 (defun org-buffer-list (&optional predicate exclude-tmp)
11905 "Return a list of Org buffers.
11906 PREDICATE can be `export', `files' or `agenda'.
11908 export restrict the list to Export buffers.
11909 files restrict the list to buffers visiting Org files.
11910 agenda restrict the list to buffers visiting agenda files.
11912 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
11913 (let* ((bfn nil)
11914 (agenda-files (and (eq predicate 'agenda)
11915 (mapcar 'file-truename (org-agenda-files t))))
11916 (filter
11917 (cond
11918 ((eq predicate 'files)
11919 (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
11920 ((eq predicate 'export)
11921 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
11922 ((eq predicate 'agenda)
11923 (lambda (b)
11924 (with-current-buffer b
11925 (and (eq major-mode 'org-mode)
11926 (setq bfn (buffer-file-name b))
11927 (member (file-truename bfn) agenda-files)))))
11928 (t (lambda (b) (with-current-buffer b
11929 (or (eq major-mode 'org-mode)
11930 (string-match "\*Org .*Export"
11931 (buffer-name b)))))))))
11932 (delq nil
11933 (mapcar
11934 (lambda(b)
11935 (if (and (funcall filter b)
11936 (or (not exclude-tmp)
11937 (not (string-match "tmp" (buffer-name b)))))
11939 nil))
11940 (buffer-list)))))
11942 (defun org-agenda-files (&optional unrestricted archives)
11943 "Get the list of agenda files.
11944 Optional UNRESTRICTED means return the full list even if a restriction
11945 is currently in place.
11946 When ARCHIVES is t, include all archive files hat are really being
11947 used by the agenda files. If ARCHIVE is `ifmode', do this only if
11948 `org-agenda-archives-mode' is t."
11949 (let ((files
11950 (cond
11951 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
11952 ((stringp org-agenda-files) (org-read-agenda-file-list))
11953 ((listp org-agenda-files) org-agenda-files)
11954 (t (error "Invalid value of `org-agenda-files'")))))
11955 (setq files (apply 'append
11956 (mapcar (lambda (f)
11957 (if (file-directory-p f)
11958 (directory-files
11959 f t org-agenda-file-regexp)
11960 (list f)))
11961 files)))
11962 (when org-agenda-skip-unavailable-files
11963 (setq files (delq nil
11964 (mapcar (function
11965 (lambda (file)
11966 (and (file-readable-p file) file)))
11967 files))))
11968 (when (or (eq archives t)
11969 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
11970 (setq files (org-add-archive-files files)))
11971 files))
11973 (defun org-edit-agenda-file-list ()
11974 "Edit the list of agenda files.
11975 Depending on setup, this either uses customize to edit the variable
11976 `org-agenda-files', or it visits the file that is holding the list. In the
11977 latter case, the buffer is set up in a way that saving it automatically kills
11978 the buffer and restores the previous window configuration."
11979 (interactive)
11980 (if (stringp org-agenda-files)
11981 (let ((cw (current-window-configuration)))
11982 (find-file org-agenda-files)
11983 (org-set-local 'org-window-configuration cw)
11984 (org-add-hook 'after-save-hook
11985 (lambda ()
11986 (set-window-configuration
11987 (prog1 org-window-configuration
11988 (kill-buffer (current-buffer))))
11989 (org-install-agenda-files-menu)
11990 (message "New agenda file list installed"))
11991 nil 'local)
11992 (message "%s" (substitute-command-keys
11993 "Edit list and finish with \\[save-buffer]")))
11994 (customize-variable 'org-agenda-files)))
11996 (defun org-store-new-agenda-file-list (list)
11997 "Set new value for the agenda file list and save it correcly."
11998 (if (stringp org-agenda-files)
11999 (let ((f org-agenda-files) b)
12000 (while (setq b (find-buffer-visiting f)) (kill-buffer b))
12001 (with-temp-file f
12002 (insert (mapconcat 'identity list "\n") "\n")))
12003 (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
12004 (setq org-agenda-files list)
12005 (customize-save-variable 'org-agenda-files org-agenda-files))))
12007 (defun org-read-agenda-file-list ()
12008 "Read the list of agenda files from a file."
12009 (when (file-directory-p org-agenda-files)
12010 (error "`org-agenda-files' cannot be a single directory"))
12011 (when (stringp org-agenda-files)
12012 (with-temp-buffer
12013 (insert-file-contents org-agenda-files)
12014 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
12017 ;;;###autoload
12018 (defun org-cycle-agenda-files ()
12019 "Cycle through the files in `org-agenda-files'.
12020 If the current buffer visits an agenda file, find the next one in the list.
12021 If the current buffer does not, find the first agenda file."
12022 (interactive)
12023 (let* ((fs (org-agenda-files t))
12024 (files (append fs (list (car fs))))
12025 (tcf (if buffer-file-name (file-truename buffer-file-name)))
12026 file)
12027 (unless files (error "No agenda files"))
12028 (catch 'exit
12029 (while (setq file (pop files))
12030 (if (equal (file-truename file) tcf)
12031 (when (car files)
12032 (find-file (car files))
12033 (throw 'exit t))))
12034 (find-file (car fs)))
12035 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
12037 (defun org-agenda-file-to-front (&optional to-end)
12038 "Move/add the current file to the top of the agenda file list.
12039 If the file is not present in the list, it is added to the front. If it is
12040 present, it is moved there. With optional argument TO-END, add/move to the
12041 end of the list."
12042 (interactive "P")
12043 (let ((org-agenda-skip-unavailable-files nil)
12044 (file-alist (mapcar (lambda (x)
12045 (cons (file-truename x) x))
12046 (org-agenda-files t)))
12047 (ctf (file-truename buffer-file-name))
12048 x had)
12049 (setq x (assoc ctf file-alist) had x)
12051 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
12052 (if to-end
12053 (setq file-alist (append (delq x file-alist) (list x)))
12054 (setq file-alist (cons x (delq x file-alist))))
12055 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
12056 (org-install-agenda-files-menu)
12057 (message "File %s to %s of agenda file list"
12058 (if had "moved" "added") (if to-end "end" "front"))))
12060 (defun org-remove-file (&optional file)
12061 "Remove current file from the list of files in variable `org-agenda-files'.
12062 These are the files which are being checked for agenda entries.
12063 Optional argument FILE means, use this file instead of the current."
12064 (interactive)
12065 (let* ((org-agenda-skip-unavailable-files nil)
12066 (file (or file buffer-file-name))
12067 (true-file (file-truename file))
12068 (afile (abbreviate-file-name file))
12069 (files (delq nil (mapcar
12070 (lambda (x)
12071 (if (equal true-file
12072 (file-truename x))
12073 nil x))
12074 (org-agenda-files t)))))
12075 (if (not (= (length files) (length (org-agenda-files t))))
12076 (progn
12077 (org-store-new-agenda-file-list files)
12078 (org-install-agenda-files-menu)
12079 (message "Removed file: %s" afile))
12080 (message "File was not in list: %s (not removed)" afile))))
12082 (defun org-file-menu-entry (file)
12083 (vector file (list 'find-file file) t))
12085 (defun org-check-agenda-file (file)
12086 "Make sure FILE exists. If not, ask user what to do."
12087 (when (not (file-exists-p file))
12088 (message "non-existent file %s. [R]emove from list or [A]bort?"
12089 (abbreviate-file-name file))
12090 (let ((r (downcase (read-char-exclusive))))
12091 (cond
12092 ((equal r ?r)
12093 (org-remove-file file)
12094 (throw 'nextfile t))
12095 (t (error "Abort"))))))
12097 (defun org-get-agenda-file-buffer (file)
12098 "Get a buffer visiting FILE. If the buffer needs to be created, add
12099 it to the list of buffers which might be released later."
12100 (let ((buf (org-find-base-buffer-visiting file)))
12101 (if buf
12102 buf ; just return it
12103 ;; Make a new buffer and remember it
12104 (setq buf (find-file-noselect file))
12105 (if buf (push buf org-agenda-new-buffers))
12106 buf)))
12108 (defun org-release-buffers (blist)
12109 "Release all buffers in list, asking the user for confirmation when needed.
12110 When a buffer is unmodified, it is just killed. When modified, it is saved
12111 \(if the user agrees) and then killed."
12112 (let (buf file)
12113 (while (setq buf (pop blist))
12114 (setq file (buffer-file-name buf))
12115 (when (and (buffer-modified-p buf)
12116 file
12117 (y-or-n-p (format "Save file %s? " file)))
12118 (with-current-buffer buf (save-buffer)))
12119 (kill-buffer buf))))
12121 (defun org-prepare-agenda-buffers (files)
12122 "Create buffers for all agenda files, protect archived trees and comments."
12123 (interactive)
12124 (let ((pa '(:org-archived t))
12125 (pc '(:org-comment t))
12126 (pall '(:org-archived t :org-comment t))
12127 (inhibit-read-only t)
12128 (rea (concat ":" org-archive-tag ":"))
12129 bmp file re)
12130 (save-excursion
12131 (save-restriction
12132 (while (setq file (pop files))
12133 (if (bufferp file)
12134 (set-buffer file)
12135 (org-check-agenda-file file)
12136 (set-buffer (org-get-agenda-file-buffer file)))
12137 (widen)
12138 (setq bmp (buffer-modified-p))
12139 (org-refresh-category-properties)
12140 (setq org-todo-keywords-for-agenda
12141 (append org-todo-keywords-for-agenda org-todo-keywords-1))
12142 (setq org-done-keywords-for-agenda
12143 (append org-done-keywords-for-agenda org-done-keywords))
12144 (save-excursion
12145 (remove-text-properties (point-min) (point-max) pall)
12146 (when org-agenda-skip-archived-trees
12147 (goto-char (point-min))
12148 (while (re-search-forward rea nil t)
12149 (if (org-on-heading-p t)
12150 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
12151 (goto-char (point-min))
12152 (setq re (concat "^\\*+ +" org-comment-string "\\>"))
12153 (while (re-search-forward re nil t)
12154 (add-text-properties
12155 (match-beginning 0) (org-end-of-subtree t) pc)))
12156 (set-buffer-modified-p bmp))))))
12158 ;;;; Embedded LaTeX
12160 (defvar org-cdlatex-mode-map (make-sparse-keymap)
12161 "Keymap for the minor `org-cdlatex-mode'.")
12163 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
12164 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
12165 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
12166 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
12167 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
12169 (defvar org-cdlatex-texmathp-advice-is-done nil
12170 "Flag remembering if we have applied the advice to texmathp already.")
12172 (define-minor-mode org-cdlatex-mode
12173 "Toggle the minor `org-cdlatex-mode'.
12174 This mode supports entering LaTeX environment and math in LaTeX fragments
12175 in Org-mode.
12176 \\{org-cdlatex-mode-map}"
12177 nil " OCDL" nil
12178 (when org-cdlatex-mode (require 'cdlatex))
12179 (unless org-cdlatex-texmathp-advice-is-done
12180 (setq org-cdlatex-texmathp-advice-is-done t)
12181 (defadvice texmathp (around org-math-always-on activate)
12182 "Always return t in org-mode buffers.
12183 This is because we want to insert math symbols without dollars even outside
12184 the LaTeX math segments. If Orgmode thinks that point is actually inside
12185 en embedded LaTeX fragement, let texmathp do its job.
12186 \\[org-cdlatex-mode-map]"
12187 (interactive)
12188 (let (p)
12189 (cond
12190 ((not (org-mode-p)) ad-do-it)
12191 ((eq this-command 'cdlatex-math-symbol)
12192 (setq ad-return-value t
12193 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
12195 (let ((p (org-inside-LaTeX-fragment-p)))
12196 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
12197 (setq ad-return-value t
12198 texmathp-why '("Org-mode embedded math" . 0))
12199 (if p ad-do-it)))))))))
12201 (defun turn-on-org-cdlatex ()
12202 "Unconditionally turn on `org-cdlatex-mode'."
12203 (org-cdlatex-mode 1))
12205 (defun org-inside-LaTeX-fragment-p ()
12206 "Test if point is inside a LaTeX fragment.
12207 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
12208 sequence appearing also before point.
12209 Even though the matchers for math are configurable, this function assumes
12210 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
12211 delimiters are skipped when they have been removed by customization.
12212 The return value is nil, or a cons cell with the delimiter and
12213 and the position of this delimiter.
12215 This function does a reasonably good job, but can locally be fooled by
12216 for example currency specifications. For example it will assume being in
12217 inline math after \"$22.34\". The LaTeX fragment formatter will only format
12218 fragments that are properly closed, but during editing, we have to live
12219 with the uncertainty caused by missing closing delimiters. This function
12220 looks only before point, not after."
12221 (catch 'exit
12222 (let ((pos (point))
12223 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
12224 (lim (progn
12225 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
12226 (point)))
12227 dd-on str (start 0) m re)
12228 (goto-char pos)
12229 (when dodollar
12230 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
12231 re (nth 1 (assoc "$" org-latex-regexps)))
12232 (while (string-match re str start)
12233 (cond
12234 ((= (match-end 0) (length str))
12235 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
12236 ((= (match-end 0) (- (length str) 5))
12237 (throw 'exit nil))
12238 (t (setq start (match-end 0))))))
12239 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
12240 (goto-char pos)
12241 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
12242 (and (match-beginning 2) (throw 'exit nil))
12243 ;; count $$
12244 (while (re-search-backward "\\$\\$" lim t)
12245 (setq dd-on (not dd-on)))
12246 (goto-char pos)
12247 (if dd-on (cons "$$" m))))))
12250 (defun org-try-cdlatex-tab ()
12251 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
12252 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
12253 - inside a LaTeX fragment, or
12254 - after the first word in a line, where an abbreviation expansion could
12255 insert a LaTeX environment."
12256 (when org-cdlatex-mode
12257 (cond
12258 ((save-excursion
12259 (skip-chars-backward "a-zA-Z0-9*")
12260 (skip-chars-backward " \t")
12261 (bolp))
12262 (cdlatex-tab) t)
12263 ((org-inside-LaTeX-fragment-p)
12264 (cdlatex-tab) t)
12265 (t nil))))
12267 (defun org-cdlatex-underscore-caret (&optional arg)
12268 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
12269 Revert to the normal definition outside of these fragments."
12270 (interactive "P")
12271 (if (org-inside-LaTeX-fragment-p)
12272 (call-interactively 'cdlatex-sub-superscript)
12273 (let (org-cdlatex-mode)
12274 (call-interactively (key-binding (vector last-input-event))))))
12276 (defun org-cdlatex-math-modify (&optional arg)
12277 "Execute `cdlatex-math-modify' in LaTeX fragments.
12278 Revert to the normal definition outside of these fragments."
12279 (interactive "P")
12280 (if (org-inside-LaTeX-fragment-p)
12281 (call-interactively 'cdlatex-math-modify)
12282 (let (org-cdlatex-mode)
12283 (call-interactively (key-binding (vector last-input-event))))))
12285 (defvar org-latex-fragment-image-overlays nil
12286 "List of overlays carrying the images of latex fragments.")
12287 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
12289 (defun org-remove-latex-fragment-image-overlays ()
12290 "Remove all overlays with LaTeX fragment images in current buffer."
12291 (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
12292 (setq org-latex-fragment-image-overlays nil))
12294 (defun org-preview-latex-fragment (&optional subtree)
12295 "Preview the LaTeX fragment at point, or all locally or globally.
12296 If the cursor is in a LaTeX fragment, create the image and overlay
12297 it over the source code. If there is no fragment at point, display
12298 all fragments in the current text, from one headline to the next. With
12299 prefix SUBTREE, display all fragments in the current subtree. With a
12300 double prefix `C-u C-u', or when the cursor is before the first headline,
12301 display all fragments in the buffer.
12302 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
12303 (interactive "P")
12304 (org-remove-latex-fragment-image-overlays)
12305 (save-excursion
12306 (save-restriction
12307 (let (beg end at msg)
12308 (cond
12309 ((or (equal subtree '(16))
12310 (not (save-excursion
12311 (re-search-backward (concat "^" outline-regexp) nil t))))
12312 (setq beg (point-min) end (point-max)
12313 msg "Creating images for buffer...%s"))
12314 ((equal subtree '(4))
12315 (org-back-to-heading)
12316 (setq beg (point) end (org-end-of-subtree t)
12317 msg "Creating images for subtree...%s"))
12319 (if (setq at (org-inside-LaTeX-fragment-p))
12320 (goto-char (max (point-min) (- (cdr at) 2)))
12321 (org-back-to-heading))
12322 (setq beg (point) end (progn (outline-next-heading) (point))
12323 msg (if at "Creating image...%s"
12324 "Creating images for entry...%s"))))
12325 (message msg "")
12326 (narrow-to-region beg end)
12327 (goto-char beg)
12328 (org-format-latex
12329 (concat "ltxpng/" (file-name-sans-extension
12330 (file-name-nondirectory
12331 buffer-file-name)))
12332 default-directory 'overlays msg at 'forbuffer)
12333 (message msg "done. Use `C-c C-c' to remove images.")))))
12335 (defvar org-latex-regexps
12336 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
12337 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
12338 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
12339 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
12340 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
12341 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
12342 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
12343 "Regular expressions for matching embedded LaTeX.")
12345 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
12346 "Replace LaTeX fragments with links to an image, and produce images."
12347 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
12348 (let* ((prefixnodir (file-name-nondirectory prefix))
12349 (absprefix (expand-file-name prefix dir))
12350 (todir (file-name-directory absprefix))
12351 (opt org-format-latex-options)
12352 (matchers (plist-get opt :matchers))
12353 (re-list org-latex-regexps)
12354 (cnt 0) txt link beg end re e checkdir
12355 m n block linkfile movefile ov)
12356 ;; Check if there are old images files with this prefix, and remove them
12357 (when (file-directory-p todir)
12358 (mapc 'delete-file
12359 (directory-files
12360 todir 'full
12361 (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
12362 ;; Check the different regular expressions
12363 (while (setq e (pop re-list))
12364 (setq m (car e) re (nth 1 e) n (nth 2 e)
12365 block (if (nth 3 e) "\n\n" ""))
12366 (when (member m matchers)
12367 (goto-char (point-min))
12368 (while (re-search-forward re nil t)
12369 (when (or (not at) (equal (cdr at) (match-beginning n)))
12370 (setq txt (match-string n)
12371 beg (match-beginning n) end (match-end n)
12372 cnt (1+ cnt)
12373 linkfile (format "%s_%04d.png" prefix cnt)
12374 movefile (format "%s_%04d.png" absprefix cnt)
12375 link (concat block "[[file:" linkfile "]]" block))
12376 (if msg (message msg cnt))
12377 (goto-char beg)
12378 (unless checkdir ; make sure the directory exists
12379 (setq checkdir t)
12380 (or (file-directory-p todir) (make-directory todir)))
12381 (org-create-formula-image
12382 txt movefile opt forbuffer)
12383 (if overlays
12384 (progn
12385 (setq ov (org-make-overlay beg end))
12386 (if (featurep 'xemacs)
12387 (progn
12388 (org-overlay-put ov 'invisible t)
12389 (org-overlay-put
12390 ov 'end-glyph
12391 (make-glyph (vector 'png :file movefile))))
12392 (org-overlay-put
12393 ov 'display
12394 (list 'image :type 'png :file movefile :ascent 'center)))
12395 (push ov org-latex-fragment-image-overlays)
12396 (goto-char end))
12397 (delete-region beg end)
12398 (insert link))))))))
12400 ;; This function borrows from Ganesh Swami's latex2png.el
12401 (defun org-create-formula-image (string tofile options buffer)
12402 (let* ((tmpdir (if (featurep 'xemacs)
12403 (temp-directory)
12404 temporary-file-directory))
12405 (texfilebase (make-temp-name
12406 (expand-file-name "orgtex" tmpdir)))
12407 (texfile (concat texfilebase ".tex"))
12408 (dvifile (concat texfilebase ".dvi"))
12409 (pngfile (concat texfilebase ".png"))
12410 (fnh (if (featurep 'xemacs)
12411 (font-height (get-face-font 'default))
12412 (face-attribute 'default :height nil)))
12413 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
12414 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
12415 (fg (or (plist-get options (if buffer :foreground :html-foreground))
12416 "Black"))
12417 (bg (or (plist-get options (if buffer :background :html-background))
12418 "Transparent")))
12419 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
12420 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
12421 (with-temp-file texfile
12422 (insert org-format-latex-header
12423 "\n\\begin{document}\n" string "\n\\end{document}\n"))
12424 (let ((dir default-directory))
12425 (condition-case nil
12426 (progn
12427 (cd tmpdir)
12428 (call-process "latex" nil nil nil texfile))
12429 (error nil))
12430 (cd dir))
12431 (if (not (file-exists-p dvifile))
12432 (progn (message "Failed to create dvi file from %s" texfile) nil)
12433 (condition-case nil
12434 (call-process "dvipng" nil nil nil
12435 "-E" "-fg" fg "-bg" bg
12436 "-D" dpi
12437 ;;"-x" scale "-y" scale
12438 "-T" "tight"
12439 "-o" pngfile
12440 dvifile)
12441 (error nil))
12442 (if (not (file-exists-p pngfile))
12443 (progn (message "Failed to create png file from %s" texfile) nil)
12444 ;; Use the requested file name and clean up
12445 (copy-file pngfile tofile 'replace)
12446 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
12447 (delete-file (concat texfilebase e)))
12448 pngfile))))
12450 (defun org-dvipng-color (attr)
12451 "Return an rgb color specification for dvipng."
12452 (apply 'format "rgb %s %s %s"
12453 (mapcar 'org-normalize-color
12454 (color-values (face-attribute 'default attr nil)))))
12456 (defun org-normalize-color (value)
12457 "Return string to be used as color value for an RGB component."
12458 (format "%g" (/ value 65535.0)))
12461 ;;;; Key bindings
12463 ;; Make `C-c C-x' a prefix key
12464 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
12466 ;; TAB key with modifiers
12467 (org-defkey org-mode-map "\C-i" 'org-cycle)
12468 (org-defkey org-mode-map [(tab)] 'org-cycle)
12469 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
12470 (org-defkey org-mode-map [(meta tab)] 'org-complete)
12471 (org-defkey org-mode-map "\M-\t" 'org-complete)
12472 (org-defkey org-mode-map "\M-\C-i" 'org-complete)
12473 ;; The following line is necessary under Suse GNU/Linux
12474 (unless (featurep 'xemacs)
12475 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
12476 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
12477 (define-key org-mode-map [backtab] 'org-shifttab)
12479 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
12480 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
12481 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
12483 ;; Cursor keys with modifiers
12484 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
12485 (org-defkey org-mode-map [(meta right)] 'org-metaright)
12486 (org-defkey org-mode-map [(meta up)] 'org-metaup)
12487 (org-defkey org-mode-map [(meta down)] 'org-metadown)
12489 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
12490 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
12491 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
12492 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
12494 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
12495 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
12496 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
12497 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
12499 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
12500 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
12502 ;;; Extra keys for tty access.
12503 ;; We only set them when really needed because otherwise the
12504 ;; menus don't show the simple keys
12506 (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
12507 (not window-system))
12508 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
12509 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
12510 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
12511 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
12512 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
12513 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
12514 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
12515 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
12516 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
12517 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
12518 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
12519 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
12520 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
12521 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
12522 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
12523 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
12524 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
12525 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
12526 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
12527 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
12528 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
12529 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
12531 ;; All the other keys
12533 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
12534 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
12535 (if (boundp 'narrow-map)
12536 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
12537 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
12538 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
12539 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
12540 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
12541 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
12542 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
12543 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
12544 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
12545 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
12546 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
12547 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
12548 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
12549 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
12550 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
12551 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
12552 (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
12553 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
12554 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
12555 (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
12556 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
12557 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
12558 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
12559 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
12560 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
12561 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
12562 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
12563 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
12564 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
12565 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
12566 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
12567 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
12568 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
12569 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
12570 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
12571 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
12572 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
12573 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
12574 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
12575 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
12576 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
12577 (org-defkey org-mode-map "\C-c^" 'org-sort)
12578 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
12579 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
12580 (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
12581 (org-defkey org-mode-map "\C-m" 'org-return)
12582 (org-defkey org-mode-map "\C-j" 'org-return-indent)
12583 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
12584 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
12585 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
12586 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
12587 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
12588 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
12589 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
12590 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
12591 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
12592 (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
12593 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
12594 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
12595 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
12596 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
12597 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
12599 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
12600 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
12601 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
12602 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
12604 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
12605 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
12606 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
12607 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
12608 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
12609 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
12610 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
12611 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
12612 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
12613 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
12614 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
12615 (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
12617 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
12619 (when (featurep 'xemacs)
12620 (org-defkey org-mode-map 'button3 'popup-mode-menu))
12622 (defvar org-table-auto-blank-field) ; defined in org-table.el
12623 (defun org-self-insert-command (N)
12624 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
12625 If the cursor is in a table looking at whitespace, the whitespace is
12626 overwritten, and the table is not marked as requiring realignment."
12627 (interactive "p")
12628 (if (and (org-table-p)
12629 (progn
12630 ;; check if we blank the field, and if that triggers align
12631 (and (featurep 'org-table) org-table-auto-blank-field
12632 (member last-command
12633 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
12634 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
12635 ;; got extra space, this field does not determine column width
12636 (let (org-table-may-need-update) (org-table-blank-field))
12637 ;; no extra space, this field may determine column width
12638 (org-table-blank-field)))
12640 (eq N 1)
12641 (looking-at "[^|\n]* |"))
12642 (let (org-table-may-need-update)
12643 (goto-char (1- (match-end 0)))
12644 (delete-backward-char 1)
12645 (goto-char (match-beginning 0))
12646 (self-insert-command N))
12647 (setq org-table-may-need-update t)
12648 (self-insert-command N)
12649 (org-fix-tags-on-the-fly)))
12651 (defun org-fix-tags-on-the-fly ()
12652 (when (and (equal (char-after (point-at-bol)) ?*)
12653 (org-on-heading-p))
12654 (org-align-tags-here org-tags-column)))
12656 (defun org-delete-backward-char (N)
12657 "Like `delete-backward-char', insert whitespace at field end in tables.
12658 When deleting backwards, in tables this function will insert whitespace in
12659 front of the next \"|\" separator, to keep the table aligned. The table will
12660 still be marked for re-alignment if the field did fill the entire column,
12661 because, in this case the deletion might narrow the column."
12662 (interactive "p")
12663 (if (and (org-table-p)
12664 (eq N 1)
12665 (string-match "|" (buffer-substring (point-at-bol) (point)))
12666 (looking-at ".*?|"))
12667 (let ((pos (point))
12668 (noalign (looking-at "[^|\n\r]* |"))
12669 (c org-table-may-need-update))
12670 (backward-delete-char N)
12671 (skip-chars-forward "^|")
12672 (insert " ")
12673 (goto-char (1- pos))
12674 ;; noalign: if there were two spaces at the end, this field
12675 ;; does not determine the width of the column.
12676 (if noalign (setq org-table-may-need-update c)))
12677 (backward-delete-char N)
12678 (org-fix-tags-on-the-fly)))
12680 (defun org-delete-char (N)
12681 "Like `delete-char', but insert whitespace at field end in tables.
12682 When deleting characters, in tables this function will insert whitespace in
12683 front of the next \"|\" separator, to keep the table aligned. The table will
12684 still be marked for re-alignment if the field did fill the entire column,
12685 because, in this case the deletion might narrow the column."
12686 (interactive "p")
12687 (if (and (org-table-p)
12688 (not (bolp))
12689 (not (= (char-after) ?|))
12690 (eq N 1))
12691 (if (looking-at ".*?|")
12692 (let ((pos (point))
12693 (noalign (looking-at "[^|\n\r]* |"))
12694 (c org-table-may-need-update))
12695 (replace-match (concat
12696 (substring (match-string 0) 1 -1)
12697 " |"))
12698 (goto-char pos)
12699 ;; noalign: if there were two spaces at the end, this field
12700 ;; does not determine the width of the column.
12701 (if noalign (setq org-table-may-need-update c)))
12702 (delete-char N))
12703 (delete-char N)
12704 (org-fix-tags-on-the-fly)))
12706 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
12707 (put 'org-self-insert-command 'delete-selection t)
12708 (put 'orgtbl-self-insert-command 'delete-selection t)
12709 (put 'org-delete-char 'delete-selection 'supersede)
12710 (put 'org-delete-backward-char 'delete-selection 'supersede)
12712 ;; Make `flyspell-mode' delay after some commands
12713 (put 'org-self-insert-command 'flyspell-delayed t)
12714 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
12715 (put 'org-delete-char 'flyspell-delayed t)
12716 (put 'org-delete-backward-char 'flyspell-delayed t)
12718 ;; Make pabbrev-mode expand after org-mode commands
12719 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
12720 (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
12722 ;; How to do this: Measure non-white length of current string
12723 ;; If equal to column width, we should realign.
12725 (defun org-remap (map &rest commands)
12726 "In MAP, remap the functions given in COMMANDS.
12727 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
12728 (let (new old)
12729 (while commands
12730 (setq old (pop commands) new (pop commands))
12731 (if (fboundp 'command-remapping)
12732 (org-defkey map (vector 'remap old) new)
12733 (substitute-key-definition old new map global-map)))))
12735 (when (eq org-enable-table-editor 'optimized)
12736 ;; If the user wants maximum table support, we need to hijack
12737 ;; some standard editing functions
12738 (org-remap org-mode-map
12739 'self-insert-command 'org-self-insert-command
12740 'delete-char 'org-delete-char
12741 'delete-backward-char 'org-delete-backward-char)
12742 (org-defkey org-mode-map "|" 'org-force-self-insert))
12744 (defun org-shiftcursor-error ()
12745 "Throw an error because Shift-Cursor command was applied in wrong context."
12746 (error "This command is active in special context like tables, headlines or timestamps"))
12748 (defun org-shifttab (&optional arg)
12749 "Global visibility cycling or move to previous table field.
12750 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
12751 on context.
12752 See the individual commands for more information."
12753 (interactive "P")
12754 (cond
12755 ((org-at-table-p) (call-interactively 'org-table-previous-field))
12756 ((integerp arg)
12757 (message "Content view to level: %d" arg)
12758 (org-content (prefix-numeric-value arg))
12759 (setq org-cycle-global-status 'overview))
12760 (t (call-interactively 'org-global-cycle))))
12762 (defun org-shiftmetaleft ()
12763 "Promote subtree or delete table column.
12764 Calls `org-promote-subtree', `org-outdent-item',
12765 or `org-table-delete-column', depending on context.
12766 See the individual commands for more information."
12767 (interactive)
12768 (cond
12769 ((org-at-table-p) (call-interactively 'org-table-delete-column))
12770 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
12771 ((org-at-item-p) (call-interactively 'org-outdent-item))
12772 (t (org-shiftcursor-error))))
12774 (defun org-shiftmetaright ()
12775 "Demote subtree or insert table column.
12776 Calls `org-demote-subtree', `org-indent-item',
12777 or `org-table-insert-column', depending on context.
12778 See the individual commands for more information."
12779 (interactive)
12780 (cond
12781 ((org-at-table-p) (call-interactively 'org-table-insert-column))
12782 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
12783 ((org-at-item-p) (call-interactively 'org-indent-item))
12784 (t (org-shiftcursor-error))))
12786 (defun org-shiftmetaup (&optional arg)
12787 "Move subtree up or kill table row.
12788 Calls `org-move-subtree-up' or `org-table-kill-row' or
12789 `org-move-item-up' depending on context. See the individual commands
12790 for more information."
12791 (interactive "P")
12792 (cond
12793 ((org-at-table-p) (call-interactively 'org-table-kill-row))
12794 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
12795 ((org-at-item-p) (call-interactively 'org-move-item-up))
12796 (t (org-shiftcursor-error))))
12797 (defun org-shiftmetadown (&optional arg)
12798 "Move subtree down or insert table row.
12799 Calls `org-move-subtree-down' or `org-table-insert-row' or
12800 `org-move-item-down', depending on context. See the individual
12801 commands for more information."
12802 (interactive "P")
12803 (cond
12804 ((org-at-table-p) (call-interactively 'org-table-insert-row))
12805 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
12806 ((org-at-item-p) (call-interactively 'org-move-item-down))
12807 (t (org-shiftcursor-error))))
12809 (defun org-metaleft (&optional arg)
12810 "Promote heading or move table column to left.
12811 Calls `org-do-promote' or `org-table-move-column', depending on context.
12812 With no specific context, calls the Emacs default `backward-word'.
12813 See the individual commands for more information."
12814 (interactive "P")
12815 (cond
12816 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
12817 ((or (org-on-heading-p) (org-region-active-p))
12818 (call-interactively 'org-do-promote))
12819 ((org-at-item-p) (call-interactively 'org-outdent-item))
12820 (t (call-interactively 'backward-word))))
12822 (defun org-metaright (&optional arg)
12823 "Demote subtree or move table column to right.
12824 Calls `org-do-demote' or `org-table-move-column', depending on context.
12825 With no specific context, calls the Emacs default `forward-word'.
12826 See the individual commands for more information."
12827 (interactive "P")
12828 (cond
12829 ((org-at-table-p) (call-interactively 'org-table-move-column))
12830 ((or (org-on-heading-p) (org-region-active-p))
12831 (call-interactively 'org-do-demote))
12832 ((org-at-item-p) (call-interactively 'org-indent-item))
12833 (t (call-interactively 'forward-word))))
12835 (defun org-metaup (&optional arg)
12836 "Move subtree up or move table row up.
12837 Calls `org-move-subtree-up' or `org-table-move-row' or
12838 `org-move-item-up', depending on context. See the individual commands
12839 for more information."
12840 (interactive "P")
12841 (cond
12842 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
12843 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
12844 ((org-at-item-p) (call-interactively 'org-move-item-up))
12845 (t (transpose-lines 1) (beginning-of-line -1))))
12847 (defun org-metadown (&optional arg)
12848 "Move subtree down or move table row down.
12849 Calls `org-move-subtree-down' or `org-table-move-row' or
12850 `org-move-item-down', depending on context. See the individual
12851 commands for more information."
12852 (interactive "P")
12853 (cond
12854 ((org-at-table-p) (call-interactively 'org-table-move-row))
12855 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
12856 ((org-at-item-p) (call-interactively 'org-move-item-down))
12857 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
12859 (defun org-shiftup (&optional arg)
12860 "Increase item in timestamp or increase priority of current headline.
12861 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
12862 depending on context. See the individual commands for more information."
12863 (interactive "P")
12864 (cond
12865 ((org-at-timestamp-p t)
12866 (call-interactively (if org-edit-timestamp-down-means-later
12867 'org-timestamp-down 'org-timestamp-up)))
12868 ((org-on-heading-p) (call-interactively 'org-priority-up))
12869 ((org-at-item-p) (call-interactively 'org-previous-item))
12870 ((org-clocktable-try-shift 'up arg))
12871 (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
12873 (defun org-shiftdown (&optional arg)
12874 "Decrease item in timestamp or decrease priority of current headline.
12875 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
12876 depending on context. See the individual commands for more information."
12877 (interactive "P")
12878 (cond
12879 ((org-at-timestamp-p t)
12880 (call-interactively (if org-edit-timestamp-down-means-later
12881 'org-timestamp-up 'org-timestamp-down)))
12882 ((org-on-heading-p) (call-interactively 'org-priority-down))
12883 ((org-clocktable-try-shift 'down arg))
12884 (t (call-interactively 'org-next-item))))
12886 (defun org-shiftright (&optional arg)
12887 "Next TODO keyword or timestamp one day later, depending on context."
12888 (interactive "P")
12889 (cond
12890 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
12891 ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
12892 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
12893 ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
12894 ((org-clocktable-try-shift 'right arg))
12895 (t (org-shiftcursor-error))))
12897 (defun org-shiftleft (&optional arg)
12898 "Previous TODO keyword or timestamp one day earlier, depending on context."
12899 (interactive "P")
12900 (cond
12901 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
12902 ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
12903 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
12904 ((org-at-property-p)
12905 (call-interactively 'org-property-previous-allowed-value))
12906 ((org-clocktable-try-shift 'left arg))
12907 (t (org-shiftcursor-error))))
12909 (defun org-shiftcontrolright ()
12910 "Switch to next TODO set."
12911 (interactive)
12912 (cond
12913 ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
12914 (t (org-shiftcursor-error))))
12916 (defun org-shiftcontrolleft ()
12917 "Switch to previous TODO set."
12918 (interactive)
12919 (cond
12920 ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
12921 (t (org-shiftcursor-error))))
12923 (defun org-ctrl-c-ret ()
12924 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
12925 (interactive)
12926 (cond
12927 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
12928 (t (call-interactively 'org-insert-heading))))
12930 (defun org-copy-special ()
12931 "Copy region in table or copy current subtree.
12932 Calls `org-table-copy' or `org-copy-subtree', depending on context.
12933 See the individual commands for more information."
12934 (interactive)
12935 (call-interactively
12936 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
12938 (defun org-cut-special ()
12939 "Cut region in table or cut current subtree.
12940 Calls `org-table-copy' or `org-cut-subtree', depending on context.
12941 See the individual commands for more information."
12942 (interactive)
12943 (call-interactively
12944 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
12946 (defun org-paste-special (arg)
12947 "Paste rectangular region into table, or past subtree relative to level.
12948 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
12949 See the individual commands for more information."
12950 (interactive "P")
12951 (if (org-at-table-p)
12952 (org-table-paste-rectangle)
12953 (org-paste-subtree arg)))
12955 (defun org-edit-special ()
12956 "Call a special editor for the stuff at point.
12957 When at a table, call the formula editor with `org-table-edit-formulas'.
12958 When at the first line of an src example, call `org-edit-src-code'.
12959 When in an #+include line, visit the include file. Otherwise call
12960 `ffap' to visit the file at point."
12961 (interactive)
12962 (cond
12963 ((org-at-table-p)
12964 (call-interactively 'org-table-edit-formulas))
12965 ((save-excursion
12966 (beginning-of-line 1)
12967 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
12968 (find-file (org-trim (match-string 1))))
12969 ((org-edit-src-code))
12970 (t (call-interactively 'ffap))))
12972 (defun org-ctrl-c-ctrl-c (&optional arg)
12973 "Set tags in headline, or update according to changed information at point.
12975 This command does many different things, depending on context:
12977 - If the cursor is in a headline, prompt for tags and insert them
12978 into the current line, aligned to `org-tags-column'. When called
12979 with prefix arg, realign all tags in the current buffer.
12981 - If the cursor is in one of the special #+KEYWORD lines, this
12982 triggers scanning the buffer for these lines and updating the
12983 information.
12985 - If the cursor is inside a table, realign the table. This command
12986 works even if the automatic table editor has been turned off.
12988 - If the cursor is on a #+TBLFM line, re-apply the formulas to
12989 the entire table.
12991 - If the cursor is a the beginning of a dynamic block, update it.
12993 - If the cursor is inside a table created by the table.el package,
12994 activate that table.
12996 - If the current buffer is a remember buffer, close note and file it.
12997 with a prefix argument, file it without further interaction to the default
12998 location.
13000 - If the cursor is on a <<<target>>>, update radio targets and corresponding
13001 links in this buffer.
13003 - If the cursor is on a numbered item in a plain list, renumber the
13004 ordered list.
13006 - If the cursor is on a checkbox, toggle it."
13007 (interactive "P")
13008 (let ((org-enable-table-editor t))
13009 (cond
13010 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
13011 org-occur-highlights
13012 org-latex-fragment-image-overlays)
13013 (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
13014 (org-remove-occur-highlights)
13015 (org-remove-latex-fragment-image-overlays)
13016 (message "Temporary highlights/overlays removed from current buffer"))
13017 ((and (local-variable-p 'org-finish-function (current-buffer))
13018 (fboundp org-finish-function))
13019 (funcall org-finish-function))
13020 ((org-at-property-p)
13021 (call-interactively 'org-property-action))
13022 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
13023 ((org-on-heading-p) (call-interactively 'org-set-tags))
13024 ((org-at-table.el-p)
13025 (require 'table)
13026 (beginning-of-line 1)
13027 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
13028 (call-interactively 'table-recognize-table))
13029 ((org-at-table-p)
13030 (org-table-maybe-eval-formula)
13031 (if arg
13032 (call-interactively 'org-table-recalculate)
13033 (org-table-maybe-recalculate-line))
13034 (call-interactively 'org-table-align))
13035 ((org-at-item-checkbox-p)
13036 (call-interactively 'org-toggle-checkbox))
13037 ((org-at-item-p)
13038 (call-interactively 'org-maybe-renumber-ordered-list))
13039 ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
13040 ;; Dynamic block
13041 (beginning-of-line 1)
13042 (org-update-dblock))
13043 ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
13044 (cond
13045 ((equal (match-string 1) "TBLFM")
13046 ;; Recalculate the table before this line
13047 (save-excursion
13048 (beginning-of-line 1)
13049 (skip-chars-backward " \r\n\t")
13050 (if (org-at-table-p)
13051 (org-call-with-arg 'org-table-recalculate t))))
13053 ; (org-set-regexps-and-options)
13054 ; (org-restart-font-lock)
13055 (let ((org-inhibit-startup t)) (org-mode-restart))
13056 (message "Local setup has been refreshed"))))
13057 (t (error "C-c C-c can do nothing useful at this location.")))))
13059 (defun org-mode-restart ()
13060 "Restart Org-mode, to scan again for special lines.
13061 Also updates the keyword regular expressions."
13062 (interactive)
13063 (org-mode)
13064 (message "Org-mode restarted"))
13066 (defun org-kill-note-or-show-branches ()
13067 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
13068 (interactive)
13069 (if (not org-finish-function)
13070 (call-interactively 'show-branches)
13071 (let ((org-note-abort t))
13072 (funcall org-finish-function))))
13074 (defun org-return (&optional indent)
13075 "Goto next table row or insert a newline.
13076 Calls `org-table-next-row' or `newline', depending on context.
13077 See the individual commands for more information."
13078 (interactive)
13079 (cond
13080 ((bobp) (if indent (newline-and-indent) (newline)))
13081 ((and (org-at-heading-p)
13082 (looking-at
13083 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
13084 (org-show-entry)
13085 (end-of-line 1)
13086 (newline))
13087 ((org-at-table-p)
13088 (org-table-justify-field-maybe)
13089 (call-interactively 'org-table-next-row))
13090 (t (if indent (newline-and-indent) (newline)))))
13092 (defun org-return-indent ()
13093 "Goto next table row or insert a newline and indent.
13094 Calls `org-table-next-row' or `newline-and-indent', depending on
13095 context. See the individual commands for more information."
13096 (interactive)
13097 (org-return t))
13099 (defun org-ctrl-c-star ()
13100 "Compute table, or change heading status of lines.
13101 Calls `org-table-recalculate' or `org-toggle-region-headings',
13102 depending on context. This will also turn a plain list item or a normal
13103 line into a subheading."
13104 (interactive)
13105 (cond
13106 ((org-at-table-p)
13107 (call-interactively 'org-table-recalculate))
13108 ((org-region-active-p)
13109 ;; Convert all lines in region to list items
13110 (call-interactively 'org-toggle-region-headings))
13111 ((org-on-heading-p)
13112 (org-toggle-region-headings (point-at-bol)
13113 (min (1+ (point-at-eol)) (point-max))))
13114 ((org-at-item-p)
13115 ;; Convert to heading
13116 (let ((level (save-match-data
13117 (save-excursion
13118 (condition-case nil
13119 (progn
13120 (org-back-to-heading t)
13121 (funcall outline-level))
13122 (error 0))))))
13123 (replace-match
13124 (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
13125 (t (org-toggle-region-headings (point-at-bol)
13126 (min (1+ (point-at-eol)) (point-max))))))
13128 (defun org-ctrl-c-minus ()
13129 "Insert separator line in table or modify bullet status of line.
13130 Also turns a plain line or a region of lines into list items.
13131 Calls `org-table-insert-hline', `org-toggle-region-items', or
13132 `org-cycle-list-bullet', depending on context."
13133 (interactive)
13134 (cond
13135 ((org-at-table-p)
13136 (call-interactively 'org-table-insert-hline))
13137 ((org-on-heading-p)
13138 ;; Convert to item
13139 (save-excursion
13140 (beginning-of-line 1)
13141 (if (looking-at "\\*+ ")
13142 (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
13143 ((org-region-active-p)
13144 ;; Convert all lines in region to list items
13145 (call-interactively 'org-toggle-region-items))
13146 ((org-in-item-p)
13147 (call-interactively 'org-cycle-list-bullet))
13148 (t (org-toggle-region-items (point-at-bol)
13149 (min (1+ (point-at-eol)) (point-max))))))
13151 (defun org-toggle-region-items (beg end)
13152 "Convert all lines in region to list items.
13153 If the first line is already an item, convert all list items in the region
13154 to normal lines."
13155 (interactive "r")
13156 (let (l2 l)
13157 (save-excursion
13158 (goto-char end)
13159 (setq l2 (org-current-line))
13160 (goto-char beg)
13161 (beginning-of-line 1)
13162 (setq l (1- (org-current-line)))
13163 (if (org-at-item-p)
13164 ;; We already have items, de-itemize
13165 (while (< (setq l (1+ l)) l2)
13166 (when (org-at-item-p)
13167 (goto-char (match-beginning 2))
13168 (delete-region (match-beginning 2) (match-end 2))
13169 (and (looking-at "[ \t]+") (replace-match "")))
13170 (beginning-of-line 2))
13171 (while (< (setq l (1+ l)) l2)
13172 (unless (org-at-item-p)
13173 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
13174 (replace-match "\\1- \\2")))
13175 (beginning-of-line 2))))))
13177 (defun org-toggle-region-headings (beg end)
13178 "Convert all lines in region to list items.
13179 If the first line is already an item, convert all list items in the region
13180 to normal lines."
13181 (interactive "r")
13182 (let (l2 l)
13183 (save-excursion
13184 (goto-char end)
13185 (setq l2 (org-current-line))
13186 (goto-char beg)
13187 (beginning-of-line 1)
13188 (setq l (1- (org-current-line)))
13189 (if (org-on-heading-p)
13190 ;; We already have headlines, de-star them
13191 (while (< (setq l (1+ l)) l2)
13192 (when (org-on-heading-p t)
13193 (and (looking-at outline-regexp) (replace-match "")))
13194 (beginning-of-line 2))
13195 (let* ((stars (save-excursion
13196 (re-search-backward org-complex-heading-regexp nil t)
13197 (or (match-string 1) "*")))
13198 (add-stars (if org-odd-levels-only "**" "*"))
13199 (rpl (concat stars add-stars " \\2")))
13200 (while (< (setq l (1+ l)) l2)
13201 (unless (org-on-heading-p)
13202 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
13203 (replace-match rpl)))
13204 (beginning-of-line 2)))))))
13206 (defun org-meta-return (&optional arg)
13207 "Insert a new heading or wrap a region in a table.
13208 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
13209 See the individual commands for more information."
13210 (interactive "P")
13211 (cond
13212 ((org-at-table-p)
13213 (call-interactively 'org-table-wrap-region))
13214 (t (call-interactively 'org-insert-heading))))
13216 ;;; Menu entries
13218 ;; Define the Org-mode menus
13219 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
13220 '("Tbl"
13221 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
13222 ["Next Field" org-cycle (org-at-table-p)]
13223 ["Previous Field" org-shifttab (org-at-table-p)]
13224 ["Next Row" org-return (org-at-table-p)]
13225 "--"
13226 ["Blank Field" org-table-blank-field (org-at-table-p)]
13227 ["Edit Field" org-table-edit-field (org-at-table-p)]
13228 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
13229 "--"
13230 ("Column"
13231 ["Move Column Left" org-metaleft (org-at-table-p)]
13232 ["Move Column Right" org-metaright (org-at-table-p)]
13233 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
13234 ["Insert Column" org-shiftmetaright (org-at-table-p)])
13235 ("Row"
13236 ["Move Row Up" org-metaup (org-at-table-p)]
13237 ["Move Row Down" org-metadown (org-at-table-p)]
13238 ["Delete Row" org-shiftmetaup (org-at-table-p)]
13239 ["Insert Row" org-shiftmetadown (org-at-table-p)]
13240 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
13241 "--"
13242 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
13243 ("Rectangle"
13244 ["Copy Rectangle" org-copy-special (org-at-table-p)]
13245 ["Cut Rectangle" org-cut-special (org-at-table-p)]
13246 ["Paste Rectangle" org-paste-special (org-at-table-p)]
13247 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
13248 "--"
13249 ("Calculate"
13250 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
13251 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
13252 ["Edit Formulas" org-edit-special (org-at-table-p)]
13253 "--"
13254 ["Recalculate line" org-table-recalculate (org-at-table-p)]
13255 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
13256 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
13257 "--"
13258 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
13259 "--"
13260 ["Sum Column/Rectangle" org-table-sum
13261 (or (org-at-table-p) (org-region-active-p))]
13262 ["Which Column?" org-table-current-column (org-at-table-p)])
13263 ["Debug Formulas"
13264 org-table-toggle-formula-debugger
13265 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
13266 ["Show Col/Row Numbers"
13267 org-table-toggle-coordinate-overlays
13268 :style toggle
13269 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
13270 "--"
13271 ["Create" org-table-create (and (not (org-at-table-p))
13272 org-enable-table-editor)]
13273 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
13274 ["Import from File" org-table-import (not (org-at-table-p))]
13275 ["Export to File" org-table-export (org-at-table-p)]
13276 "--"
13277 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
13279 (easy-menu-define org-org-menu org-mode-map "Org menu"
13280 '("Org"
13281 ("Show/Hide"
13282 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
13283 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
13284 ["Sparse Tree..." org-sparse-tree t]
13285 ["Reveal Context" org-reveal t]
13286 ["Show All" show-all t]
13287 "--"
13288 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
13289 "--"
13290 ["New Heading" org-insert-heading t]
13291 ("Navigate Headings"
13292 ["Up" outline-up-heading t]
13293 ["Next" outline-next-visible-heading t]
13294 ["Previous" outline-previous-visible-heading t]
13295 ["Next Same Level" outline-forward-same-level t]
13296 ["Previous Same Level" outline-backward-same-level t]
13297 "--"
13298 ["Jump" org-goto t])
13299 ("Edit Structure"
13300 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
13301 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
13302 "--"
13303 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
13304 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
13305 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
13306 "--"
13307 ["Promote Heading" org-metaleft (not (org-at-table-p))]
13308 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
13309 ["Demote Heading" org-metaright (not (org-at-table-p))]
13310 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
13311 "--"
13312 ["Sort Region/Children" org-sort (not (org-at-table-p))]
13313 "--"
13314 ["Convert to odd levels" org-convert-to-odd-levels t]
13315 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
13316 ("Editing"
13317 ["Emphasis..." org-emphasize t]
13318 ["Edit Source Example" org-edit-special t])
13319 ("Archive"
13320 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
13321 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
13322 ; :active t :keys "C-u C-c C-x C-a"]
13323 ["Sparse trees open ARCHIVE trees"
13324 (setq org-sparse-tree-open-archived-trees
13325 (not org-sparse-tree-open-archived-trees))
13326 :style toggle :selected org-sparse-tree-open-archived-trees]
13327 ["Cycling opens ARCHIVE trees"
13328 (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
13329 :style toggle :selected org-cycle-open-archived-trees]
13330 "--"
13331 ["Move Subtree to Archive" org-advertized-archive-subtree t]
13332 ; ["Check and Move Children" (org-archive-subtree '(4))
13333 ; :active t :keys "C-u C-c C-x C-s"]
13335 "--"
13336 ("TODO Lists"
13337 ["TODO/DONE/-" org-todo t]
13338 ("Select keyword"
13339 ["Next keyword" org-shiftright (org-on-heading-p)]
13340 ["Previous keyword" org-shiftleft (org-on-heading-p)]
13341 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
13342 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
13343 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
13344 ["Show TODO Tree" org-show-todo-tree t]
13345 ["Global TODO list" org-todo-list t]
13346 "--"
13347 ["Set Priority" org-priority t]
13348 ["Priority Up" org-shiftup t]
13349 ["Priority Down" org-shiftdown t])
13350 ("TAGS and Properties"
13351 ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
13352 ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
13353 "--"
13354 ["Set property" 'org-set-property t]
13355 ["Column view of properties" org-columns t]
13356 ["Insert Column View DBlock" org-insert-columns-dblock t])
13357 ("Dates and Scheduling"
13358 ["Timestamp" org-time-stamp t]
13359 ["Timestamp (inactive)" org-time-stamp-inactive t]
13360 ("Change Date"
13361 ["1 Day Later" org-shiftright t]
13362 ["1 Day Earlier" org-shiftleft t]
13363 ["1 ... Later" org-shiftup t]
13364 ["1 ... Earlier" org-shiftdown t])
13365 ["Compute Time Range" org-evaluate-time-range t]
13366 ["Schedule Item" org-schedule t]
13367 ["Deadline" org-deadline t]
13368 "--"
13369 ["Custom time format" org-toggle-time-stamp-overlays
13370 :style radio :selected org-display-custom-times]
13371 "--"
13372 ["Goto Calendar" org-goto-calendar t]
13373 ["Date from Calendar" org-date-from-calendar t])
13374 ("Logging work"
13375 ["Clock in" org-clock-in t]
13376 ["Clock out" org-clock-out t]
13377 ["Clock cancel" org-clock-cancel t]
13378 ["Goto running clock" org-clock-goto t]
13379 ["Display times" org-clock-display t]
13380 ["Create clock table" org-clock-report t]
13381 "--"
13382 ["Record DONE time"
13383 (progn (setq org-log-done (not org-log-done))
13384 (message "Switching to %s will %s record a timestamp"
13385 (car org-done-keywords)
13386 (if org-log-done "automatically" "not")))
13387 :style toggle :selected org-log-done])
13388 "--"
13389 ["Agenda Command..." org-agenda t]
13390 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
13391 ("File List for Agenda")
13392 ("Special views current file"
13393 ["TODO Tree" org-show-todo-tree t]
13394 ["Check Deadlines" org-check-deadlines t]
13395 ["Timeline" org-timeline t]
13396 ["Tags Tree" org-tags-sparse-tree t])
13397 "--"
13398 ("Hyperlinks"
13399 ["Store Link (Global)" org-store-link t]
13400 ["Insert Link" org-insert-link t]
13401 ["Follow Link" org-open-at-point t]
13402 "--"
13403 ["Next link" org-next-link t]
13404 ["Previous link" org-previous-link t]
13405 "--"
13406 ["Descriptive Links"
13407 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
13408 :style radio
13409 :selected (member '(org-link) buffer-invisibility-spec)]
13410 ["Literal Links"
13411 (progn
13412 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
13413 :style radio
13414 :selected (not (member '(org-link) buffer-invisibility-spec))])
13415 "--"
13416 ["Export/Publish..." org-export t]
13417 ("LaTeX"
13418 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
13419 :selected org-cdlatex-mode]
13420 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
13421 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
13422 ["Modify math symbol" org-cdlatex-math-modify
13423 (org-inside-LaTeX-fragment-p)]
13424 ["Export LaTeX fragments as images"
13425 (if (featurep 'org-exp)
13426 (setq org-export-with-LaTeX-fragments
13427 (not org-export-with-LaTeX-fragments))
13428 (require 'org-exp))
13429 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
13430 org-export-with-LaTeX-fragments)])
13431 "--"
13432 ("Documentation"
13433 ["Show Version" org-version t]
13434 ["Info Documentation" org-info t])
13435 ("Customize"
13436 ["Browse Org Group" org-customize t]
13437 "--"
13438 ["Expand This Menu" org-create-customize-menu
13439 (fboundp 'customize-menu-create)])
13440 "--"
13441 ["Refresh setup" org-mode-restart t]
13444 (defun org-info (&optional node)
13445 "Read documentation for Org-mode in the info system.
13446 With optional NODE, go directly to that node."
13447 (interactive)
13448 (info (format "(org)%s" (or node ""))))
13450 (defun org-install-agenda-files-menu ()
13451 (let ((bl (buffer-list)))
13452 (save-excursion
13453 (while bl
13454 (set-buffer (pop bl))
13455 (if (org-mode-p) (setq bl nil)))
13456 (when (org-mode-p)
13457 (easy-menu-change
13458 '("Org") "File List for Agenda"
13459 (append
13460 (list
13461 ["Edit File List" (org-edit-agenda-file-list) t]
13462 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
13463 ["Remove Current File from List" org-remove-file t]
13464 ["Cycle through agenda files" org-cycle-agenda-files t]
13465 ["Occur in all agenda files" org-occur-in-agenda-files t]
13466 "--")
13467 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
13469 ;;;; Documentation
13471 ;;;###autoload
13472 (defun org-require-autoloaded-modules ()
13473 (interactive)
13474 (mapc 'require
13475 '(org-agenda org-archive org-clock org-colview
13476 org-exp org-id org-export-latex org-publish
13477 org-remember org-table)))
13479 ;;;###autoload
13480 (defun org-customize ()
13481 "Call the customize function with org as argument."
13482 (interactive)
13483 (org-load-modules-maybe)
13484 (org-require-autoloaded-modules)
13485 (customize-browse 'org))
13487 (defun org-create-customize-menu ()
13488 "Create a full customization menu for Org-mode, insert it into the menu."
13489 (interactive)
13490 (org-load-modules-maybe)
13491 (org-require-autoloaded-modules)
13492 (if (fboundp 'customize-menu-create)
13493 (progn
13494 (easy-menu-change
13495 '("Org") "Customize"
13496 `(["Browse Org group" org-customize t]
13497 "--"
13498 ,(customize-menu-create 'org)
13499 ["Set" Custom-set t]
13500 ["Save" Custom-save t]
13501 ["Reset to Current" Custom-reset-current t]
13502 ["Reset to Saved" Custom-reset-saved t]
13503 ["Reset to Standard Settings" Custom-reset-standard t]))
13504 (message "\"Org\"-menu now contains full customization menu"))
13505 (error "Cannot expand menu (outdated version of cus-edit.el)")))
13507 ;;;; Miscellaneous stuff
13509 ;;; Generally useful functions
13511 (defun org-display-warning (message) ;; Copied from Emacs-Muse
13512 "Display the given MESSAGE as a warning."
13513 (if (fboundp 'display-warning)
13514 (display-warning 'org message
13515 (if (featurep 'xemacs)
13516 'warning
13517 :warning))
13518 (let ((buf (get-buffer-create "*Org warnings*")))
13519 (with-current-buffer buf
13520 (goto-char (point-max))
13521 (insert "Warning (Org): " message)
13522 (unless (bolp)
13523 (newline)))
13524 (display-buffer buf)
13525 (sit-for 0))))
13527 (defun org-goto-marker-or-bmk (marker &optional bookmark)
13528 "Go to MARKER, widen if necesary. When marker is not live, try BOOKMARK."
13529 (if (and marker (marker-buffer marker)
13530 (buffer-live-p (marker-buffer marker)))
13531 (progn
13532 (switch-to-buffer (marker-buffer marker))
13533 (if (or (> marker (point-max)) (< marker (point-min)))
13534 (widen))
13535 (goto-char marker))
13536 (if bookmark
13537 (bookmark-jump bookmark)
13538 (error "Cannot find location"))))
13540 (defun org-quote-csv-field (s)
13541 "Quote field for inclusion in CSV material."
13542 (if (string-match "[\",]" s)
13543 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
13546 (defun org-plist-delete (plist property)
13547 "Delete PROPERTY from PLIST.
13548 This is in contrast to merely setting it to 0."
13549 (let (p)
13550 (while plist
13551 (if (not (eq property (car plist)))
13552 (setq p (plist-put p (car plist) (nth 1 plist))))
13553 (setq plist (cddr plist)))
13556 (defun org-force-self-insert (N)
13557 "Needed to enforce self-insert under remapping."
13558 (interactive "p")
13559 (self-insert-command N))
13561 (defun org-string-width (s)
13562 "Compute width of string, ignoring invisible characters.
13563 This ignores character with invisibility property `org-link', and also
13564 characters with property `org-cwidth', because these will become invisible
13565 upon the next fontification round."
13566 (let (b l)
13567 (when (or (eq t buffer-invisibility-spec)
13568 (assq 'org-link buffer-invisibility-spec))
13569 (while (setq b (text-property-any 0 (length s)
13570 'invisible 'org-link s))
13571 (setq s (concat (substring s 0 b)
13572 (substring s (or (next-single-property-change
13573 b 'invisible s) (length s)))))))
13574 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
13575 (setq s (concat (substring s 0 b)
13576 (substring s (or (next-single-property-change
13577 b 'org-cwidth s) (length s))))))
13578 (setq l (string-width s) b -1)
13579 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
13580 (setq l (- l (get-text-property b 'org-dwidth-n s))))
13583 (defun org-base-buffer (buffer)
13584 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
13585 (if (not buffer)
13586 buffer
13587 (or (buffer-base-buffer buffer)
13588 buffer)))
13590 (defun org-trim (s)
13591 "Remove whitespace at beginning and end of string."
13592 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
13593 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
13596 (defun org-wrap (string &optional width lines)
13597 "Wrap string to either a number of lines, or a width in characters.
13598 If WIDTH is non-nil, the string is wrapped to that width, however many lines
13599 that costs. If there is a word longer than WIDTH, the text is actually
13600 wrapped to the length of that word.
13601 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
13602 many lines, whatever width that takes.
13603 The return value is a list of lines, without newlines at the end."
13604 (let* ((words (org-split-string string "[ \t\n]+"))
13605 (maxword (apply 'max (mapcar 'org-string-width words)))
13606 w ll)
13607 (cond (width
13608 (org-do-wrap words (max maxword width)))
13609 (lines
13610 (setq w maxword)
13611 (setq ll (org-do-wrap words maxword))
13612 (if (<= (length ll) lines)
13614 (setq ll words)
13615 (while (> (length ll) lines)
13616 (setq w (1+ w))
13617 (setq ll (org-do-wrap words w)))
13618 ll))
13619 (t (error "Cannot wrap this")))))
13621 (defun org-do-wrap (words width)
13622 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
13623 (let (lines line)
13624 (while words
13625 (setq line (pop words))
13626 (while (and words (< (+ (length line) (length (car words))) width))
13627 (setq line (concat line " " (pop words))))
13628 (setq lines (push line lines)))
13629 (nreverse lines)))
13631 (defun org-split-string (string &optional separators)
13632 "Splits STRING into substrings at SEPARATORS.
13633 No empty strings are returned if there are matches at the beginning
13634 and end of string."
13635 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
13636 (start 0)
13637 notfirst
13638 (list nil))
13639 (while (and (string-match rexp string
13640 (if (and notfirst
13641 (= start (match-beginning 0))
13642 (< start (length string)))
13643 (1+ start) start))
13644 (< (match-beginning 0) (length string)))
13645 (setq notfirst t)
13646 (or (eq (match-beginning 0) 0)
13647 (and (eq (match-beginning 0) (match-end 0))
13648 (eq (match-beginning 0) start))
13649 (setq list
13650 (cons (substring string start (match-beginning 0))
13651 list)))
13652 (setq start (match-end 0)))
13653 (or (eq start (length string))
13654 (setq list
13655 (cons (substring string start)
13656 list)))
13657 (nreverse list)))
13659 (defun org-context ()
13660 "Return a list of contexts of the current cursor position.
13661 If several contexts apply, all are returned.
13662 Each context entry is a list with a symbol naming the context, and
13663 two positions indicating start and end of the context. Possible
13664 contexts are:
13666 :headline anywhere in a headline
13667 :headline-stars on the leading stars in a headline
13668 :todo-keyword on a TODO keyword (including DONE) in a headline
13669 :tags on the TAGS in a headline
13670 :priority on the priority cookie in a headline
13671 :item on the first line of a plain list item
13672 :item-bullet on the bullet/number of a plain list item
13673 :checkbox on the checkbox in a plain list item
13674 :table in an org-mode table
13675 :table-special on a special filed in a table
13676 :table-table in a table.el table
13677 :link on a hyperlink
13678 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
13679 :target on a <<target>>
13680 :radio-target on a <<<radio-target>>>
13681 :latex-fragment on a LaTeX fragment
13682 :latex-preview on a LaTeX fragment with overlayed preview image
13684 This function expects the position to be visible because it uses font-lock
13685 faces as a help to recognize the following contexts: :table-special, :link,
13686 and :keyword."
13687 (let* ((f (get-text-property (point) 'face))
13688 (faces (if (listp f) f (list f)))
13689 (p (point)) clist o)
13690 ;; First the large context
13691 (cond
13692 ((org-on-heading-p t)
13693 (push (list :headline (point-at-bol) (point-at-eol)) clist)
13694 (when (progn
13695 (beginning-of-line 1)
13696 (looking-at org-todo-line-tags-regexp))
13697 (push (org-point-in-group p 1 :headline-stars) clist)
13698 (push (org-point-in-group p 2 :todo-keyword) clist)
13699 (push (org-point-in-group p 4 :tags) clist))
13700 (goto-char p)
13701 (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
13702 (if (looking-at "\\[#[A-Z0-9]\\]")
13703 (push (org-point-in-group p 0 :priority) clist)))
13705 ((org-at-item-p)
13706 (push (org-point-in-group p 2 :item-bullet) clist)
13707 (push (list :item (point-at-bol)
13708 (save-excursion (org-end-of-item) (point)))
13709 clist)
13710 (and (org-at-item-checkbox-p)
13711 (push (org-point-in-group p 0 :checkbox) clist)))
13713 ((org-at-table-p)
13714 (push (list :table (org-table-begin) (org-table-end)) clist)
13715 (if (memq 'org-formula faces)
13716 (push (list :table-special
13717 (previous-single-property-change p 'face)
13718 (next-single-property-change p 'face)) clist)))
13719 ((org-at-table-p 'any)
13720 (push (list :table-table) clist)))
13721 (goto-char p)
13723 ;; Now the small context
13724 (cond
13725 ((org-at-timestamp-p)
13726 (push (org-point-in-group p 0 :timestamp) clist))
13727 ((memq 'org-link faces)
13728 (push (list :link
13729 (previous-single-property-change p 'face)
13730 (next-single-property-change p 'face)) clist))
13731 ((memq 'org-special-keyword faces)
13732 (push (list :keyword
13733 (previous-single-property-change p 'face)
13734 (next-single-property-change p 'face)) clist))
13735 ((org-on-target-p)
13736 (push (org-point-in-group p 0 :target) clist)
13737 (goto-char (1- (match-beginning 0)))
13738 (if (looking-at org-radio-target-regexp)
13739 (push (org-point-in-group p 0 :radio-target) clist))
13740 (goto-char p))
13741 ((setq o (car (delq nil
13742 (mapcar
13743 (lambda (x)
13744 (if (memq x org-latex-fragment-image-overlays) x))
13745 (org-overlays-at (point))))))
13746 (push (list :latex-fragment
13747 (org-overlay-start o) (org-overlay-end o)) clist)
13748 (push (list :latex-preview
13749 (org-overlay-start o) (org-overlay-end o)) clist))
13750 ((org-inside-LaTeX-fragment-p)
13751 ;; FIXME: positions wrong.
13752 (push (list :latex-fragment (point) (point)) clist)))
13754 (setq clist (nreverse (delq nil clist)))
13755 clist))
13757 ;; FIXME: Compare with at-regexp-p Do we need both?
13758 (defun org-in-regexp (re &optional nlines visually)
13759 "Check if point is inside a match of regexp.
13760 Normally only the current line is checked, but you can include NLINES extra
13761 lines both before and after point into the search.
13762 If VISUALLY is set, require that the cursor is not after the match but
13763 really on, so that the block visually is on the match."
13764 (catch 'exit
13765 (let ((pos (point))
13766 (eol (point-at-eol (+ 1 (or nlines 0))))
13767 (inc (if visually 1 0)))
13768 (save-excursion
13769 (beginning-of-line (- 1 (or nlines 0)))
13770 (while (re-search-forward re eol t)
13771 (if (and (<= (match-beginning 0) pos)
13772 (>= (+ inc (match-end 0)) pos))
13773 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
13775 (defun org-at-regexp-p (regexp)
13776 "Is point inside a match of REGEXP in the current line?"
13777 (catch 'exit
13778 (save-excursion
13779 (let ((pos (point)) (end (point-at-eol)))
13780 (beginning-of-line 1)
13781 (while (re-search-forward regexp end t)
13782 (if (and (<= (match-beginning 0) pos)
13783 (>= (match-end 0) pos))
13784 (throw 'exit t)))
13785 nil))))
13787 (defun org-occur-in-agenda-files (regexp &optional nlines)
13788 "Call `multi-occur' with buffers for all agenda files."
13789 (interactive "sOrg-files matching: \np")
13790 (let* ((files (org-agenda-files))
13791 (tnames (mapcar 'file-truename files))
13792 (extra org-agenda-text-search-extra-files)
13794 (when (eq (car extra) 'agenda-archives)
13795 (setq extra (cdr extra))
13796 (setq files (org-add-archive-files files)))
13797 (while (setq f (pop extra))
13798 (unless (member (file-truename f) tnames)
13799 (add-to-list 'files f 'append)
13800 (add-to-list 'tnames (file-truename f) 'append)))
13801 (multi-occur
13802 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
13803 regexp)))
13805 (if (boundp 'occur-mode-find-occurrence-hook)
13806 ;; Emacs 23
13807 (add-hook 'occur-mode-find-occurrence-hook
13808 (lambda ()
13809 (when (org-mode-p)
13810 (org-reveal))))
13811 ;; Emacs 22
13812 (defadvice occur-mode-goto-occurrence
13813 (after org-occur-reveal activate)
13814 (and (org-mode-p) (org-reveal)))
13815 (defadvice occur-mode-goto-occurrence-other-window
13816 (after org-occur-reveal activate)
13817 (and (org-mode-p) (org-reveal)))
13818 (defadvice occur-mode-display-occurrence
13819 (after org-occur-reveal activate)
13820 (when (org-mode-p)
13821 (let ((pos (occur-mode-find-occurrence)))
13822 (with-current-buffer (marker-buffer pos)
13823 (save-excursion
13824 (goto-char pos)
13825 (org-reveal)))))))
13827 (defun org-uniquify (list)
13828 "Remove duplicate elements from LIST."
13829 (let (res)
13830 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
13831 res))
13833 (defun org-delete-all (elts list)
13834 "Remove all elements in ELTS from LIST."
13835 (while elts
13836 (setq list (delete (pop elts) list)))
13837 list)
13839 (defun org-back-over-empty-lines ()
13840 "Move backwards over witespace, to the beginning of the first empty line.
13841 Returns the number of empty lines passed."
13842 (let ((pos (point)))
13843 (skip-chars-backward " \t\n\r")
13844 (beginning-of-line 2)
13845 (goto-char (min (point) pos))
13846 (count-lines (point) pos)))
13848 (defun org-skip-whitespace ()
13849 (skip-chars-forward " \t\n\r"))
13851 (defun org-point-in-group (point group &optional context)
13852 "Check if POINT is in match-group GROUP.
13853 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
13854 match. If the match group does ot exist or point is not inside it,
13855 return nil."
13856 (and (match-beginning group)
13857 (>= point (match-beginning group))
13858 (<= point (match-end group))
13859 (if context
13860 (list context (match-beginning group) (match-end group))
13861 t)))
13863 (defun org-switch-to-buffer-other-window (&rest args)
13864 "Switch to buffer in a second window on the current frame.
13865 In particular, do not allow pop-up frames."
13866 (let (pop-up-frames special-display-buffer-names special-display-regexps
13867 special-display-function)
13868 (apply 'switch-to-buffer-other-window args)))
13870 (defun org-combine-plists (&rest plists)
13871 "Create a single property list from all plists in PLISTS.
13872 The process starts by copying the first list, and then setting properties
13873 from the other lists. Settings in the last list are the most significant
13874 ones and overrule settings in the other lists."
13875 (let ((rtn (copy-sequence (pop plists)))
13876 p v ls)
13877 (while plists
13878 (setq ls (pop plists))
13879 (while ls
13880 (setq p (pop ls) v (pop ls))
13881 (setq rtn (plist-put rtn p v))))
13882 rtn))
13884 (defun org-move-line-down (arg)
13885 "Move the current line down. With prefix argument, move it past ARG lines."
13886 (interactive "p")
13887 (let ((col (current-column))
13888 beg end pos)
13889 (beginning-of-line 1) (setq beg (point))
13890 (beginning-of-line 2) (setq end (point))
13891 (beginning-of-line (+ 1 arg))
13892 (setq pos (move-marker (make-marker) (point)))
13893 (insert (delete-and-extract-region beg end))
13894 (goto-char pos)
13895 (org-move-to-column col)))
13897 (defun org-move-line-up (arg)
13898 "Move the current line up. With prefix argument, move it past ARG lines."
13899 (interactive "p")
13900 (let ((col (current-column))
13901 beg end pos)
13902 (beginning-of-line 1) (setq beg (point))
13903 (beginning-of-line 2) (setq end (point))
13904 (beginning-of-line (- arg))
13905 (setq pos (move-marker (make-marker) (point)))
13906 (insert (delete-and-extract-region beg end))
13907 (goto-char pos)
13908 (org-move-to-column col)))
13910 (defun org-replace-escapes (string table)
13911 "Replace %-escapes in STRING with values in TABLE.
13912 TABLE is an association list with keys like \"%a\" and string values.
13913 The sequences in STRING may contain normal field width and padding information,
13914 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
13915 so values can contain further %-escapes if they are define later in TABLE."
13916 (let ((case-fold-search nil)
13917 e re rpl)
13918 (while (setq e (pop table))
13919 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
13920 (while (string-match re string)
13921 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
13922 (cdr e)))
13923 (setq string (replace-match rpl t t string))))
13924 string))
13927 (defun org-sublist (list start end)
13928 "Return a section of LIST, from START to END.
13929 Counting starts at 1."
13930 (let (rtn (c start))
13931 (setq list (nthcdr (1- start) list))
13932 (while (and list (<= c end))
13933 (push (pop list) rtn)
13934 (setq c (1+ c)))
13935 (nreverse rtn)))
13937 (defun org-find-base-buffer-visiting (file)
13938 "Like `find-buffer-visiting' but alway return the base buffer and
13939 not an indirect buffer."
13940 (let ((buf (find-buffer-visiting file)))
13941 (if buf
13942 (or (buffer-base-buffer buf) buf)
13943 nil)))
13945 (defun org-image-file-name-regexp ()
13946 "Return regexp matching the file names of images."
13947 (if (fboundp 'image-file-name-regexp)
13948 (image-file-name-regexp)
13949 (let ((image-file-name-extensions
13950 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
13951 "xbm" "xpm" "pbm" "pgm" "ppm")))
13952 (concat "\\."
13953 (regexp-opt (nconc (mapcar 'upcase
13954 image-file-name-extensions)
13955 image-file-name-extensions)
13957 "\\'"))))
13959 (defun org-file-image-p (file)
13960 "Return non-nil if FILE is an image."
13961 (save-match-data
13962 (string-match (org-image-file-name-regexp) file)))
13964 (defun org-get-cursor-date ()
13965 "Return the date at cursor in as a time.
13966 This works in the calendar and in the agenda, anywhere else it just
13967 returns the current time."
13968 (let (date day defd)
13969 (cond
13970 ((eq major-mode 'calendar-mode)
13971 (setq date (calendar-cursor-to-date)
13972 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13973 ((eq major-mode 'org-agenda-mode)
13974 (setq day (get-text-property (point) 'day))
13975 (if day
13976 (setq date (calendar-gregorian-from-absolute day)
13977 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
13978 (nth 2 date))))))
13979 (or defd (current-time))))
13981 (defvar org-agenda-action-marker (make-marker)
13982 "Marker pointing to the entry for the next agenda action.")
13984 (defun org-mark-entry-for-agenda-action ()
13985 "Mark the current entry as target of an agenda action.
13986 Agenda actions are actions executed from the agenda with the key `k',
13987 which make use of the date at the cursor."
13988 (interactive)
13989 (move-marker org-agenda-action-marker
13990 (save-excursion (org-back-to-heading t) (point))
13991 (current-buffer))
13992 (message
13993 "Entry marked for action; press `k' at desired date in agenda or calendar"))
13995 ;;; Paragraph filling stuff.
13996 ;; We want this to be just right, so use the full arsenal.
13998 (defun org-indent-line-function ()
13999 "Indent line like previous, but further if previous was headline or item."
14000 (interactive)
14001 (let* ((pos (point))
14002 (itemp (org-at-item-p))
14003 column bpos bcol tpos tcol bullet btype bullet-type)
14004 ;; Find the previous relevant line
14005 (beginning-of-line 1)
14006 (cond
14007 ((looking-at "#") (setq column 0))
14008 ((looking-at "\\*+ ") (setq column 0))
14010 (beginning-of-line 0)
14011 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
14012 (beginning-of-line 0))
14013 (cond
14014 ((looking-at "\\*+[ \t]+")
14015 (if (not org-adapt-indentation)
14016 (setq column 0)
14017 (goto-char (match-end 0))
14018 (setq column (current-column))))
14019 ((org-in-item-p)
14020 (org-beginning-of-item)
14021 ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
14022 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
14023 (setq bpos (match-beginning 1) tpos (match-end 0)
14024 bcol (progn (goto-char bpos) (current-column))
14025 tcol (progn (goto-char tpos) (current-column))
14026 bullet (match-string 1)
14027 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
14028 (if (> tcol (+ bcol org-description-max-indent))
14029 (setq tcol (+ bcol 5)))
14030 (if (not itemp)
14031 (setq column tcol)
14032 (goto-char pos)
14033 (beginning-of-line 1)
14034 (if (looking-at "\\S-")
14035 (progn
14036 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
14037 (setq bullet (match-string 1)
14038 btype (if (string-match "[0-9]" bullet) "n" bullet))
14039 (setq column (if (equal btype bullet-type) bcol tcol)))
14040 (setq column (org-get-indentation)))))
14041 (t (setq column (org-get-indentation))))))
14042 (goto-char pos)
14043 (if (<= (current-column) (current-indentation))
14044 (org-indent-line-to column)
14045 (save-excursion (org-indent-line-to column)))
14046 (setq column (current-column))
14047 (beginning-of-line 1)
14048 (if (looking-at
14049 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
14050 (replace-match (concat "\\1" (format org-property-format
14051 (match-string 2) (match-string 3)))
14052 t nil))
14053 (org-move-to-column column)))
14055 (defun org-set-autofill-regexps ()
14056 (interactive)
14057 ;; In the paragraph separator we include headlines, because filling
14058 ;; text in a line directly attached to a headline would otherwise
14059 ;; fill the headline as well.
14060 (org-set-local 'comment-start-skip "^#+[ \t]*")
14061 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
14062 ;; The paragraph starter includes hand-formatted lists.
14063 (org-set-local 'paragraph-start
14064 "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
14065 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
14066 ;; But only if the user has not turned off tables or fixed-width regions
14067 (org-set-local
14068 'auto-fill-inhibit-regexp
14069 (concat "\\*+ \\|#\\+"
14070 "\\|[ \t]*" org-keyword-time-regexp
14071 (if (or org-enable-table-editor org-enable-fixed-width-editor)
14072 (concat
14073 "\\|[ \t]*["
14074 (if org-enable-table-editor "|" "")
14075 (if org-enable-fixed-width-editor ":" "")
14076 "]"))))
14077 ;; We use our own fill-paragraph function, to make sure that tables
14078 ;; and fixed-width regions are not wrapped. That function will pass
14079 ;; through to `fill-paragraph' when appropriate.
14080 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
14081 ; Adaptive filling: To get full control, first make sure that
14082 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
14083 (org-set-local 'adaptive-fill-regexp "\000")
14084 (org-set-local 'adaptive-fill-function
14085 'org-adaptive-fill-function)
14086 (org-set-local
14087 'align-mode-rules-list
14088 '((org-in-buffer-settings
14089 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
14090 (modes . '(org-mode))))))
14092 (defun org-fill-paragraph (&optional justify)
14093 "Re-align a table, pass through to fill-paragraph if no table."
14094 (let ((table-p (org-at-table-p))
14095 (table.el-p (org-at-table.el-p)))
14096 (cond ((and (equal (char-after (point-at-bol)) ?*)
14097 (save-excursion (goto-char (point-at-bol))
14098 (looking-at outline-regexp)))
14099 t) ; skip headlines
14100 (table.el-p t) ; skip table.el tables
14101 (table-p (org-table-align) t) ; align org-mode tables
14102 (t nil)))) ; call paragraph-fill
14104 ;; For reference, this is the default value of adaptive-fill-regexp
14105 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
14107 (defun org-adaptive-fill-function ()
14108 "Return a fill prefix for org-mode files.
14109 In particular, this makes sure hanging paragraphs for hand-formatted lists
14110 work correctly."
14111 (cond ((looking-at "#[ \t]+")
14112 (match-string 0))
14113 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
14114 (save-excursion
14115 (if (> (match-end 1) (+ (match-beginning 1)
14116 org-description-max-indent))
14117 (goto-char (+ (match-beginning 1) 5))
14118 (goto-char (match-end 0)))
14119 (make-string (current-column) ?\ )))
14120 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
14121 (save-excursion
14122 (goto-char (match-end 0))
14123 (make-string (current-column) ?\ )))
14124 (t nil)))
14126 ;;; Other stuff.
14128 (defun org-toggle-fixed-width-section (arg)
14129 "Toggle the fixed-width export.
14130 If there is no active region, the QUOTE keyword at the current headline is
14131 inserted or removed. When present, it causes the text between this headline
14132 and the next to be exported as fixed-width text, and unmodified.
14133 If there is an active region, this command adds or removes a colon as the
14134 first character of this line. If the first character of a line is a colon,
14135 this line is also exported in fixed-width font."
14136 (interactive "P")
14137 (let* ((cc 0)
14138 (regionp (org-region-active-p))
14139 (beg (if regionp (region-beginning) (point)))
14140 (end (if regionp (region-end)))
14141 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
14142 (case-fold-search nil)
14143 (re "[ \t]*\\(:\\)")
14144 off)
14145 (if regionp
14146 (save-excursion
14147 (goto-char beg)
14148 (setq cc (current-column))
14149 (beginning-of-line 1)
14150 (setq off (looking-at re))
14151 (while (> nlines 0)
14152 (setq nlines (1- nlines))
14153 (beginning-of-line 1)
14154 (cond
14155 (arg
14156 (org-move-to-column cc t)
14157 (insert ":\n")
14158 (forward-line -1))
14159 ((and off (looking-at re))
14160 (replace-match "" t t nil 1))
14161 ((not off) (org-move-to-column cc t) (insert ":")))
14162 (forward-line 1)))
14163 (save-excursion
14164 (org-back-to-heading)
14165 (if (looking-at (concat outline-regexp
14166 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
14167 (replace-match "" t t nil 1)
14168 (if (looking-at outline-regexp)
14169 (progn
14170 (goto-char (match-end 0))
14171 (insert org-quote-string " "))))))))
14173 ;;;; Functions extending outline functionality
14175 (defun org-beginning-of-line (&optional arg)
14176 "Go to the beginning of the current line. If that is invisible, continue
14177 to a visible line beginning. This makes the function of C-a more intuitive.
14178 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
14179 first attempt, and only move to after the tags when the cursor is already
14180 beyond the end of the headline."
14181 (interactive "P")
14182 (let ((pos (point)) refpos)
14183 (beginning-of-line 1)
14184 (if (bobp)
14186 (backward-char 1)
14187 (if (org-invisible-p)
14188 (while (and (not (bobp)) (org-invisible-p))
14189 (backward-char 1)
14190 (beginning-of-line 1))
14191 (forward-char 1)))
14192 (when org-special-ctrl-a/e
14193 (cond
14194 ((and (looking-at org-complex-heading-regexp)
14195 (= (char-after (match-end 1)) ?\ ))
14196 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
14197 (point-at-eol)))
14198 (goto-char
14199 (if (eq org-special-ctrl-a/e t)
14200 (cond ((> pos refpos) refpos)
14201 ((= pos (point)) refpos)
14202 (t (point)))
14203 (cond ((> pos (point)) (point))
14204 ((not (eq last-command this-command)) (point))
14205 (t refpos)))))
14206 ((org-at-item-p)
14207 (goto-char
14208 (if (eq org-special-ctrl-a/e t)
14209 (cond ((> pos (match-end 4)) (match-end 4))
14210 ((= pos (point)) (match-end 4))
14211 (t (point)))
14212 (cond ((> pos (point)) (point))
14213 ((not (eq last-command this-command)) (point))
14214 (t (match-end 4))))))))
14215 (org-no-warnings
14216 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
14218 (defun org-end-of-line (&optional arg)
14219 "Go to the end of the line.
14220 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
14221 first attempt, and only move to after the tags when the cursor is already
14222 beyond the end of the headline."
14223 (interactive "P")
14224 (if (or (not org-special-ctrl-a/e)
14225 (not (org-on-heading-p)))
14226 (end-of-line arg)
14227 (let ((pos (point)))
14228 (beginning-of-line 1)
14229 (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
14230 (if (eq org-special-ctrl-a/e t)
14231 (if (or (< pos (match-beginning 1))
14232 (= pos (match-end 0)))
14233 (goto-char (match-beginning 1))
14234 (goto-char (match-end 0)))
14235 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
14236 (goto-char (match-end 0))
14237 (goto-char (match-beginning 1))))
14238 (end-of-line arg))))
14239 (org-no-warnings
14240 (and (featurep 'xemacs) (setq zmacs-region-stays t))))
14243 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
14244 (define-key org-mode-map "\C-e" 'org-end-of-line)
14246 (defun org-kill-line (&optional arg)
14247 "Kill line, to tags or end of line."
14248 (interactive "P")
14249 (cond
14250 ((or (not org-special-ctrl-k)
14251 (bolp)
14252 (not (org-on-heading-p)))
14253 (call-interactively 'kill-line))
14254 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
14255 (kill-region (point) (match-beginning 1))
14256 (org-set-tags nil t))
14257 (t (kill-region (point) (point-at-eol)))))
14259 (define-key org-mode-map "\C-k" 'org-kill-line)
14261 (defun org-invisible-p ()
14262 "Check if point is at a character currently not visible."
14263 ;; Early versions of noutline don't have `outline-invisible-p'.
14264 (if (fboundp 'outline-invisible-p)
14265 (outline-invisible-p)
14266 (get-char-property (point) 'invisible)))
14268 (defun org-invisible-p2 ()
14269 "Check if point is at a character currently not visible."
14270 (save-excursion
14271 (if (and (eolp) (not (bobp))) (backward-char 1))
14272 ;; Early versions of noutline don't have `outline-invisible-p'.
14273 (if (fboundp 'outline-invisible-p)
14274 (outline-invisible-p)
14275 (get-char-property (point) 'invisible))))
14277 (defalias 'org-back-to-heading 'outline-back-to-heading)
14278 (defalias 'org-on-heading-p 'outline-on-heading-p)
14279 (defalias 'org-at-heading-p 'outline-on-heading-p)
14280 (defun org-at-heading-or-item-p ()
14281 (or (org-on-heading-p) (org-at-item-p)))
14283 (defun org-on-target-p ()
14284 (or (org-in-regexp org-radio-target-regexp)
14285 (org-in-regexp org-target-regexp)))
14287 (defun org-up-heading-all (arg)
14288 "Move to the heading line of which the present line is a subheading.
14289 This function considers both visible and invisible heading lines.
14290 With argument, move up ARG levels."
14291 (if (fboundp 'outline-up-heading-all)
14292 (outline-up-heading-all arg) ; emacs 21 version of outline.el
14293 (outline-up-heading arg t))) ; emacs 22 version of outline.el
14295 (defun org-up-heading-safe ()
14296 "Move to the heading line of which the present line is a subheading.
14297 This version will not throw an error. It will return the level of the
14298 headline found, or nil if no higher level is found."
14299 (let ((pos (point)) start-level level
14300 (re (concat "^" outline-regexp)))
14301 (catch 'exit
14302 (outline-back-to-heading t)
14303 (setq start-level (funcall outline-level))
14304 (if (equal start-level 1) (throw 'exit nil))
14305 (while (re-search-backward re nil t)
14306 (setq level (funcall outline-level))
14307 (if (< level start-level) (throw 'exit level)))
14308 nil)))
14310 (defun org-first-sibling-p ()
14311 "Is this heading the first child of its parents?"
14312 (interactive)
14313 (let ((re (concat "^" outline-regexp))
14314 level l)
14315 (unless (org-at-heading-p t)
14316 (error "Not at a heading"))
14317 (setq level (funcall outline-level))
14318 (save-excursion
14319 (if (not (re-search-backward re nil t))
14321 (setq l (funcall outline-level))
14322 (< l level)))))
14324 (defun org-goto-sibling (&optional previous)
14325 "Goto the next sibling, even if it is invisible.
14326 When PREVIOUS is set, go to the previous sibling instead. Returns t
14327 when a sibling was found. When none is found, return nil and don't
14328 move point."
14329 (let ((fun (if previous 're-search-backward 're-search-forward))
14330 (pos (point))
14331 (re (concat "^" outline-regexp))
14332 level l)
14333 (when (condition-case nil (org-back-to-heading t) (error nil))
14334 (setq level (funcall outline-level))
14335 (catch 'exit
14336 (or previous (forward-char 1))
14337 (while (funcall fun re nil t)
14338 (setq l (funcall outline-level))
14339 (when (< l level) (goto-char pos) (throw 'exit nil))
14340 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
14341 (goto-char pos)
14342 nil))))
14344 (defun org-show-siblings ()
14345 "Show all siblings of the current headline."
14346 (save-excursion
14347 (while (org-goto-sibling) (org-flag-heading nil)))
14348 (save-excursion
14349 (while (org-goto-sibling 'previous)
14350 (org-flag-heading nil))))
14352 (defun org-show-hidden-entry ()
14353 "Show an entry where even the heading is hidden."
14354 (save-excursion
14355 (org-show-entry)))
14357 (defun org-flag-heading (flag &optional entry)
14358 "Flag the current heading. FLAG non-nil means make invisible.
14359 When ENTRY is non-nil, show the entire entry."
14360 (save-excursion
14361 (org-back-to-heading t)
14362 ;; Check if we should show the entire entry
14363 (if entry
14364 (progn
14365 (org-show-entry)
14366 (save-excursion
14367 (and (outline-next-heading)
14368 (org-flag-heading nil))))
14369 (outline-flag-region (max (point-min) (1- (point)))
14370 (save-excursion (outline-end-of-heading) (point))
14371 flag))))
14373 (defun org-end-of-subtree (&optional invisible-OK to-heading)
14374 ;; This is an exact copy of the original function, but it uses
14375 ;; `org-back-to-heading', to make it work also in invisible
14376 ;; trees. And is uses an invisible-OK argument.
14377 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
14378 (org-back-to-heading invisible-OK)
14379 (let ((first t)
14380 (level (funcall outline-level)))
14381 (while (and (not (eobp))
14382 (or first (> (funcall outline-level) level)))
14383 (setq first nil)
14384 (outline-next-heading))
14385 (unless to-heading
14386 (if (memq (preceding-char) '(?\n ?\^M))
14387 (progn
14388 ;; Go to end of line before heading
14389 (forward-char -1)
14390 (if (memq (preceding-char) '(?\n ?\^M))
14391 ;; leave blank line before heading
14392 (forward-char -1))))))
14393 (point))
14395 (defun org-show-subtree ()
14396 "Show everything after this heading at deeper levels."
14397 (outline-flag-region
14398 (point)
14399 (save-excursion
14400 (outline-end-of-subtree) (outline-next-heading) (point))
14401 nil))
14403 (defun org-show-entry ()
14404 "Show the body directly following this heading.
14405 Show the heading too, if it is currently invisible."
14406 (interactive)
14407 (save-excursion
14408 (condition-case nil
14409 (progn
14410 (org-back-to-heading t)
14411 (outline-flag-region
14412 (max (point-min) (1- (point)))
14413 (save-excursion
14414 (re-search-forward
14415 (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
14416 (or (match-beginning 1) (point-max)))
14417 nil))
14418 (error nil))))
14420 (defun org-make-options-regexp (kwds)
14421 "Make a regular expression for keyword lines."
14422 (concat
14424 "#?[ \t]*\\+\\("
14425 (mapconcat 'regexp-quote kwds "\\|")
14426 "\\):[ \t]*"
14427 "\\(.+\\)"))
14429 ;; Make isearch reveal the necessary context
14430 (defun org-isearch-end ()
14431 "Reveal context after isearch exits."
14432 (when isearch-success ; only if search was successful
14433 (if (featurep 'xemacs)
14434 ;; Under XEmacs, the hook is run in the correct place,
14435 ;; we directly show the context.
14436 (org-show-context 'isearch)
14437 ;; In Emacs the hook runs *before* restoring the overlays.
14438 ;; So we have to use a one-time post-command-hook to do this.
14439 ;; (Emacs 22 has a special variable, see function `org-mode')
14440 (unless (and (boundp 'isearch-mode-end-hook-quit)
14441 isearch-mode-end-hook-quit)
14442 ;; Only when the isearch was not quitted.
14443 (org-add-hook 'post-command-hook 'org-isearch-post-command
14444 'append 'local)))))
14446 (defun org-isearch-post-command ()
14447 "Remove self from hook, and show context."
14448 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
14449 (org-show-context 'isearch))
14452 ;;;; Integration with and fixes for other packages
14454 ;;; Imenu support
14456 (defvar org-imenu-markers nil
14457 "All markers currently used by Imenu.")
14458 (make-variable-buffer-local 'org-imenu-markers)
14460 (defun org-imenu-new-marker (&optional pos)
14461 "Return a new marker for use by Imenu, and remember the marker."
14462 (let ((m (make-marker)))
14463 (move-marker m (or pos (point)))
14464 (push m org-imenu-markers)
14467 (defun org-imenu-get-tree ()
14468 "Produce the index for Imenu."
14469 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
14470 (setq org-imenu-markers nil)
14471 (let* ((n org-imenu-depth)
14472 (re (concat "^" outline-regexp))
14473 (subs (make-vector (1+ n) nil))
14474 (last-level 0)
14475 m tree level head)
14476 (save-excursion
14477 (save-restriction
14478 (widen)
14479 (goto-char (point-max))
14480 (while (re-search-backward re nil t)
14481 (setq level (org-reduced-level (funcall outline-level)))
14482 (when (<= level n)
14483 (looking-at org-complex-heading-regexp)
14484 (setq head (org-match-string-no-properties 4)
14485 m (org-imenu-new-marker))
14486 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
14487 (if (>= level last-level)
14488 (push (cons head m) (aref subs level))
14489 (push (cons head (aref subs (1+ level))) (aref subs level))
14490 (loop for i from (1+ level) to n do (aset subs i nil)))
14491 (setq last-level level)))))
14492 (aref subs 1)))
14494 (eval-after-load "imenu"
14495 '(progn
14496 (add-hook 'imenu-after-jump-hook
14497 (lambda ()
14498 (if (eq major-mode 'org-mode)
14499 (org-show-context 'org-goto))))))
14501 ;; Speedbar support
14503 (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
14504 "Overlay marking the agenda restriction line in speedbar.")
14505 (org-overlay-put org-speedbar-restriction-lock-overlay
14506 'face 'org-agenda-restriction-lock)
14507 (org-overlay-put org-speedbar-restriction-lock-overlay
14508 'help-echo "Agendas are currently limited to this item.")
14509 (org-detach-overlay org-speedbar-restriction-lock-overlay)
14511 (defun org-speedbar-set-agenda-restriction ()
14512 "Restrict future agenda commands to the location at point in speedbar.
14513 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
14514 (interactive)
14515 (require 'org-agenda)
14516 (let (p m tp np dir txt w)
14517 (cond
14518 ((setq p (text-property-any (point-at-bol) (point-at-eol)
14519 'org-imenu t))
14520 (setq m (get-text-property p 'org-imenu-marker))
14521 (save-excursion
14522 (save-restriction
14523 (set-buffer (marker-buffer m))
14524 (goto-char m)
14525 (org-agenda-set-restriction-lock 'subtree))))
14526 ((setq p (text-property-any (point-at-bol) (point-at-eol)
14527 'speedbar-function 'speedbar-find-file))
14528 (setq tp (previous-single-property-change
14529 (1+ p) 'speedbar-function)
14530 np (next-single-property-change
14531 tp 'speedbar-function)
14532 dir (speedbar-line-directory)
14533 txt (buffer-substring-no-properties (or tp (point-min))
14534 (or np (point-max))))
14535 (save-excursion
14536 (save-restriction
14537 (set-buffer (find-file-noselect
14538 (let ((default-directory dir))
14539 (expand-file-name txt))))
14540 (unless (org-mode-p)
14541 (error "Cannot restrict to non-Org-mode file"))
14542 (org-agenda-set-restriction-lock 'file))))
14543 (t (error "Don't know how to restrict Org-mode's agenda")))
14544 (org-move-overlay org-speedbar-restriction-lock-overlay
14545 (point-at-bol) (point-at-eol))
14546 (setq current-prefix-arg nil)
14547 (org-agenda-maybe-redo)))
14549 (eval-after-load "speedbar"
14550 '(progn
14551 (speedbar-add-supported-extension ".org")
14552 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
14553 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
14554 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
14555 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
14556 (add-hook 'speedbar-visiting-tag-hook
14557 (lambda () (org-show-context 'org-goto)))))
14560 ;;; Fixes and Hacks for problems with other packages
14562 ;; Make flyspell not check words in links, to not mess up our keymap
14563 (defun org-mode-flyspell-verify ()
14564 "Don't let flyspell put overlays at active buttons."
14565 (not (get-text-property (point) 'keymap)))
14567 ;; Make `bookmark-jump' show the jump location if it was hidden.
14568 (eval-after-load "bookmark"
14569 '(if (boundp 'bookmark-after-jump-hook)
14570 ;; We can use the hook
14571 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
14572 ;; Hook not available, use advice
14573 (defadvice bookmark-jump (after org-make-visible activate)
14574 "Make the position visible."
14575 (org-bookmark-jump-unhide))))
14577 (defun org-bookmark-jump-unhide ()
14578 "Unhide the current position, to show the bookmark location."
14579 (and (org-mode-p)
14580 (or (org-invisible-p)
14581 (save-excursion (goto-char (max (point-min) (1- (point))))
14582 (org-invisible-p)))
14583 (org-show-context 'bookmark-jump)))
14585 ;; Make session.el ignore our circular variable
14586 (eval-after-load "session"
14587 '(add-to-list 'session-globals-exclude 'org-mark-ring))
14589 ;;;; Experimental code
14591 (defun org-closed-in-range ()
14592 "Sparse tree of items closed in a certain time range.
14593 Still experimental, may disappear in the future."
14594 (interactive)
14595 ;; Get the time interval from the user.
14596 (let* ((time1 (time-to-seconds
14597 (org-read-date nil 'to-time nil "Starting date: ")))
14598 (time2 (time-to-seconds
14599 (org-read-date nil 'to-time nil "End date:")))
14600 ;; callback function
14601 (callback (lambda ()
14602 (let ((time
14603 (time-to-seconds
14604 (apply 'encode-time
14605 (org-parse-time-string
14606 (match-string 1))))))
14607 ;; check if time in interval
14608 (and (>= time time1) (<= time time2))))))
14609 ;; make tree, check each match with the callback
14610 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
14613 ;;;; Finish up
14615 (provide 'org)
14617 (run-hooks 'org-load-hook)
14619 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
14621 ;;; org.el ends here