Release 4.28
[org-mode/org-mode-NeilSmithlineMods.git] / org.el
blobeb195165151e31cfd5f1ea2d47fa6b3157b2f970
1 ;;; org.el --- Outline-based notes management and organize
2 ;; Carstens outline-mode for keeping track of everything.
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Carsten Dominik <dominik at science dot uva dot nl>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://www.astro.uva.nl/~dominik/Tools/org/
8 ;; Version: 4.28
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 2, or (at your option)
15 ;; 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Commentary:
30 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
31 ;; project planning with a fast and effective plain-text system.
33 ;; Org-mode develops organizational tasks around a NOTES file that contains
34 ;; information about projects as plain text. Org-mode is implemented on top
35 ;; of outline-mode - ideal to keep the content of large files well structured.
36 ;; It supports ToDo items, deadlines and time stamps, which can be extracted
37 ;; to create a daily/weekly agenda that also integrates the diary of the Emacs
38 ;; calendar. Tables are easily created with a built-in table editor. Plain
39 ;; text URL-like links connect to websites, emails (VM, RMAIL, WANDERLUST),
40 ;; Usenet messages (Gnus), BBDB entries, and any files related to the
41 ;; projects. For printing and sharing of notes, an Org-mode file (or a part
42 ;; of it) can be exported as a structured ASCII file, or as HTML.
44 ;; Installation
45 ;; ------------
46 ;; If Org-mode is part of the Emacs distribution or an XEmacs package, you
47 ;; only need to copy the following lines to your .emacs file. The last two
48 ;; lines define *global* keys for the commands `org-store-link' and
49 ;; `org-agenda' - please choose suitable keys yourself.
51 ;; (add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
52 ;; (define-key global-map "\C-cl" 'org-store-link)
53 ;; (define-key global-map "\C-ca" 'org-agenda)
55 ;; If you have downloaded Org-mode from the Web, you must byte-compile
56 ;; org.el and put it on your load path. In addition to the Emacs Lisp
57 ;; lines above, you also need to add the following lines to .emacs:
59 ;; (autoload 'org-mode "org" "Org mode" t)
60 ;; (autoload 'org-diary "org" "Diary entries from Org mode")
61 ;; (autoload 'org-agenda "org" "Multi-file agenda from Org mode" t)
62 ;; (autoload 'org-store-link "org" "Store a link to the current location" t)
63 ;; (autoload 'orgtbl-mode "org" "Org tables as a minor mode" t)
64 ;; (autoload 'turn-on-orgtbl "org" "Org tables as a minor mode")
66 ;; This setup will put all files with extension ".org" into Org-mode. As
67 ;; an alternative, make the first line of a file look like this:
69 ;; MY PROJECTS -*- mode: org; -*-
71 ;; which will select Org-mode for this buffer no matter what the file's
72 ;; name is.
74 ;; Documentation
75 ;; -------------
76 ;; The documentation of Org-mode can be found in the TeXInfo file. The
77 ;; distribution also contains a PDF version of it. At the homepage of
78 ;; Org-mode, you can read the same text online as HTML. There is also an
79 ;; excellent reference card made by Philip Rooke. This card can be found
80 ;; in the etc/ directory of Emacs 22.
82 ;; Changes since version 4.10:
83 ;; ---------------------------
84 ;; Version 4.28
85 ;; - Bug fixes.
87 ;; Version 4.27
88 ;; - HTML exporter generalized to receive external options.
89 ;; As part of the process, author, email and date have been moved to the
90 ;; end of the HTML file.
91 ;; - Support for customizable file search in file links.
92 ;; - BibTeX database links as first application of the above.
93 ;; - New option `org-agenda-todo-list-sublevels' to turn off listing TODO
94 ;; entries that are sublevels of another TODO entry.
96 ;; Version 4.26
97 ;; - Bug fixes.
99 ;; Version 4.25
100 ;; - Revision of the font-lock faces section, with better tty support.
101 ;; - TODO keywords in Agenda buffer are fontified.
102 ;; - Export converts links between .org files to links between .html files.
103 ;; - Better support for bold/italic/underline emphasis.
105 ;; Version 4.24
106 ;; - Bug fixes.
108 ;; Version 4.23
109 ;; - Bug fixes.
111 ;; Version 4.22
112 ;; - Bug fixes.
113 ;; - In agenda buffer, mouse-1 no longer follows link.
114 ;; See `org-agenda-mouse-1-follows-link' and `org-mouse-1-follows-link'.
116 ;; Version 4.20
117 ;; - Links use now the [[link][description]] format by default.
118 ;; When inserting links, the user is prompted for a description.
119 ;; - If a link has a description, only the description is displayed
120 ;; the link part is hidden. Use C-c C-l to edit the link part.
121 ;; - TAGS are now bold, but in the same color as the headline.
122 ;; - The width of a table column can be limited by using a field "<N>".
123 ;; - New structure for the customization tree.
124 ;; - Bug fixes.
126 ;; Version 4.13
127 ;; - The list of agenda files can be maintainted in an external file.
128 ;; - Bug fixes.
130 ;; Version 4.12
131 ;; - Templates for remember buffer. Note that the remember setup changes.
132 ;; To set up templates, see `org-remember-templates'.
133 ;; - The time in new time stamps can be rounded, see new option
134 ;; `org-time-stamp-rounding-minutes'.
135 ;; - Bug fixes (there are *always* more bugs).
137 ;;; Code:
139 (eval-when-compile
140 (require 'cl)
141 (require 'calendar))
142 (require 'outline)
143 (require 'time-date)
144 (require 'easymenu)
146 (defvar calc-embedded-close-formula) ; defined by the calc package
147 (defvar calc-embedded-open-formula) ; defined by the calc package
148 (defvar font-lock-unfontify-region-function) ; defined by font-lock.el
150 ;;; Customization variables
152 (defvar org-version "4.28"
153 "The version number of the file org.el.")
154 (defun org-version ()
155 (interactive)
156 (message "Org-mode version %s" org-version))
158 ;; The following constant is for compatibility with different versions
159 ;; of outline.el.
160 (defconst org-noutline-p (featurep 'noutline)
161 "Are we using the new outline mode?")
162 (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
163 (defconst org-format-transports-properties-p
164 (let ((x "a"))
165 (add-text-properties 0 1 '(test t) x)
166 (get-text-property 0 'test (format "%s" x)))
167 "Does format transport text properties?")
169 (defgroup org nil
170 "Outline-based notes management and organizer."
171 :tag "Org"
172 :group 'outlines
173 :group 'hypermedia
174 :group 'calendar)
176 (defgroup org-startup nil
177 "Options concerning startup of Org-mode."
178 :tag "Org Startup"
179 :group 'org)
181 (defcustom org-startup-folded t
182 "Non-nil means, entering Org-mode will switch to OVERVIEW.
183 This can also be configured on a per-file basis by adding one of
184 the following lines anywhere in the buffer:
186 #+STARTUP: fold
187 #+STARTUP: nofold
188 #+STARTUP: content"
189 :group 'org-startup
190 :type '(choice
191 (const :tag "nofold: show all" nil)
192 (const :tag "fold: overview" t)
193 (const :tag "content: all headlines" content)))
195 (defcustom org-startup-truncated t
196 "Non-nil means, entering Org-mode will set `truncate-lines'.
197 This is useful since some lines containing links can be very long and
198 uninteresting. Also tables look terrible when wrapped."
199 :group 'org-startup
200 :type 'boolean)
202 (defcustom org-startup-align-all-tables nil
203 "Non-nil means, align all tables when visiting a file.
204 This is useful when the column width in tables is forced with <N> cookies
205 in table fields. Such tables will look correct only after the first re-align.
206 This can also be configured on a per-file basis by adding one of
207 the following lines anywhere in the buffer:
208 #+STARTUP: align
209 #+STARTUP: noalign"
210 :group 'org-startup
211 :type 'boolean)
213 (defcustom org-startup-with-deadline-check nil
214 "Non-nil means, entering Org-mode will run the deadline check.
215 This means, if you start editing an org file, you will get an
216 immediate reminder of any due deadlines.
217 This can also be configured on a per-file basis by adding one of
218 the following lines anywhere in the buffer:
219 #+STARTUP: dlcheck
220 #+STARTUP: nodlcheck"
221 :group 'org-startup
222 :type 'boolean)
224 (defcustom org-insert-mode-line-in-empty-file nil
225 "Non-nil means insert the first line setting Org-mode in empty files.
226 When the function `org-mode' is called interactively in an empty file, this
227 normally means that the file name does not automatically trigger Org-mode.
228 To ensure that the file will always be in Org-mode in the future, a
229 line enforcing Org-mode will be inserted into the buffer, if this option
230 has been set."
231 :group 'org-startup
232 :type 'boolean)
234 (defcustom org-CUA-compatible nil
235 "Non-nil means use alternative key bindings for S-<cursor movement>.
236 Org-mode used S-<cursor movement> for changing timestamps and priorities.
237 S-<cursor movement> is also used for example by `CUA-mode' to select text.
238 If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
239 alternative bindings. Setting this variable to t will replace the following
240 keys both in Org-mode and in the Org-agenda buffer.
242 S-RET -> C-S-RET
243 S-up -> M-p
244 S-down -> M-n
245 S-left -> M--
246 S-right -> M-+
248 If you do not like the alternative keys, take a look at the variable
249 `org-disputed-keys'.
251 This option is only relevant at load-time of Org-mode. Changing it requires
252 a restart of Emacs to become effective."
253 :group 'org-startup
254 :type 'boolean)
256 (defvar org-disputed-keys
257 '((S-up [(shift up)] [(meta ?p)])
258 (S-down [(shift down)] [(meta ?n)])
259 (S-left [(shift left)] [(meta ?-)])
260 (S-right [(shift right)] [(meta ?+)])
261 (S-return [(shift return)] [(control shift return)]))
262 "Keys for which Org-mode and other modes compete.
263 This is an alist, cars are symbols for lookup, 1st element is the default key,
264 second element will be used when `org-CUA-compatible' is t.")
266 (defun org-key (key)
267 "Select a key according to `org-CUA-compatible'."
268 (nth (if org-CUA-compatible 2 1)
269 (or (assq key org-disputed-keys)
270 (error "Invalid Key %s in `org-key'" key))))
272 (defcustom org-ellipsis nil
273 "The ellipsis to use in the Org-mode outline.
274 When nil, just use the standard three dots. When a string, use that instead,
275 and just in Org-mode (which will then use its own display table).
276 Changing this requires executing `M-x org-mode' in a buffer to become
277 effective."
278 :group 'org-startup
279 :type '(choice (const :tag "Default" nil)
280 (string :tag "String" :value "...#")))
282 (defvar org-display-table nil
283 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
285 (defgroup org-keywords nil
286 "Keywords in Org-mode."
287 :tag "Org Keywords"
288 :group 'org)
290 (defcustom org-deadline-string "DEADLINE:"
291 "String to mark deadline entries.
292 A deadline is this string, followed by a time stamp. Should be a word,
293 terminated by a colon. You can insert a schedule keyword and
294 a timestamp with \\[org-deadline].
295 Changes become only effective after restarting Emacs."
296 :group 'org-keywords
297 :type 'string)
299 (defcustom org-scheduled-string "SCHEDULED:"
300 "String to mark scheduled TODO entries.
301 A schedule is this string, followed by a time stamp. Should be a word,
302 terminated by a colon. You can insert a schedule keyword and
303 a timestamp with \\[org-schedule].
304 Changes become only effective after restarting Emacs."
305 :group 'org-keywords
306 :type 'string)
308 (defcustom org-closed-string "CLOSED:"
309 "String used as the prefix for timestamps logging closing a TODO entry."
310 :group 'org-keywords
311 :type 'string)
313 (defcustom org-comment-string "COMMENT"
314 "Entries starting with this keyword will never be exported.
315 An entry can be toggled between COMMENT and normal with
316 \\[org-toggle-comment].
317 Changes become only effective after restarting Emacs."
318 :group 'org-keywords
319 :type 'string)
321 (defcustom org-quote-string "QUOTE"
322 "Entries starting with this keyword will be exported in fixed-width font.
323 Quoting applies only to the text in the entry following the headline, and does
324 not extend beyond the next headline, even if that is lower level.
325 An entry can be toggled between QUOTE and normal with
326 \\[org-toggle-fixed-width-section]."
327 :group 'org-keywords
328 :type 'string)
330 (defgroup org-structure nil
331 "Options concerning the general structure of Org-mode files."
332 :tag "Org Structure"
333 :group 'org)
335 (defgroup org-cycle nil
336 "Options concerning visibility cycling in Org-mode."
337 :tag "Org Cycle"
338 :group 'org-structure)
340 (defcustom org-cycle-emulate-tab t
341 "Where should `org-cycle' emulate TAB.
342 nil Never
343 white Only in completely white lines
344 t Everywhere except in headlines"
345 :group 'org-cycle
346 :type '(choice (const :tag "Never" nil)
347 (const :tag "Only in completely white lines" white)
348 (const :tag "Everywhere except in headlines" t)
351 (defcustom org-cycle-hook '(org-optimize-window-after-visibility-change)
352 "Hook that is run after `org-cycle' has changed the buffer visibility.
353 The function(s) in this hook must accept a single argument which indicates
354 the new state that was set by the most recent `org-cycle' command. The
355 argument is a symbol. After a global state change, it can have the values
356 `overview', `content', or `all'. After a local state change, it can have
357 the values `folded', `children', or `subtree'."
358 :group 'org-cycle
359 :type 'hook)
361 (defgroup org-edit-structure nil
362 "Options concerning structure editing in Org-mode."
363 :tag "Org Edit Structure"
364 :group 'org-structure)
366 (defcustom org-odd-levels-only nil
367 "Non-nil means, skip even levels and only use odd levels for the outline.
368 This has the effect that two stars are being added/taken away in
369 promotion/demotion commands. It also influences how levels are
370 handled by the exporters.
371 Changing it requires restart of `font-lock-mode' to become effective
372 for fontification also in regions already fontified.
373 You may also set this on a per-file basis by adding one of the following
374 lines to the buffer:
376 #+STARTUP: odd
377 #+STARTUP: oddeven"
378 :group 'org-edit-structure
379 :group 'org-font-lock
380 :type 'boolean)
382 (defcustom org-adapt-indentation t
383 "Non-nil means, adapt indentation when promoting and demoting.
384 When this is set and the *entire* text in an entry is indented, the
385 indentation is increased by one space in a demotion command, and
386 decreased by one in a promotion command. If any line in the entry
387 body starts at column 0, indentation is not changed at all."
388 :group 'org-edit-structure
389 :type 'boolean)
391 (defcustom org-insert-heading-hook nil
392 "Hook being run after inserting a new heading."
393 :group 'org-edit-structure
394 :type 'boolean)
396 (defcustom org-enable-fixed-width-editor t
397 "Non-nil means, lines starting with \":\" are treated as fixed-width.
398 This currently only means, they are never auto-wrapped.
399 When nil, such lines will be treated like ordinary lines.
400 See also the QUOTE keyword."
401 :group 'org-edit-structure
402 :type 'boolean)
404 (defgroup org-sparse-trees nil
405 "Options concerning sparse trees in Org-mode."
406 :tag "Org Sparse Trees"
407 :group 'org-structure)
409 (defcustom org-highlight-sparse-tree-matches t
410 "Non-nil means, highlight all matches that define a sparse tree.
411 The highlights will automatically disappear the next time the buffer is
412 changed by an edit command."
413 :group 'org-sparse-trees
414 :type 'boolean)
416 (defcustom org-show-hierarchy-above t
417 "Non-nil means, show full hierarchy when showing a spot in the tree.
418 Turning this off makes sparse trees more compact, but also less clear."
419 :group 'org-sparse-trees
420 :type 'boolean)
422 (defcustom org-show-following-heading t
423 "Non-nil means, show heading following match in `org-occur'.
424 When doing an `org-occur' it is useful to show the headline which
425 follows the match, even if they do not match the regexp. This makes it
426 easier to edit directly inside the sparse tree. However, if you use
427 `org-occur' mainly as an overview, the following headlines are
428 unnecessary clutter."
429 :group 'org-sparse-trees
430 :type 'boolean)
432 (defcustom org-occur-hook '(org-first-headline-recenter)
433 "Hook that is run after `org-occur' has constructed a sparse tree.
434 This can be used to recenter the window to show as much of the structure
435 as possible."
436 :group 'org-sparse-trees
437 :type 'hook)
439 (defgroup org-plain-lists nil
440 "Options concerning plain lists in Org-mode."
441 :tag "Org Plain lists"
442 :group 'org-structure)
444 (defcustom org-cycle-include-plain-lists nil
445 "Non-nil means, include plain lists into visibility cycling.
446 This means that during cycling, plain list items will *temporarily* be
447 interpreted as outline headlines with a level given by 1000+i where i is the
448 indentation of the bullet. In all other operations, plain list items are
449 not seen as headlines. For example, you cannot assign a TODO keyword to
450 such an item."
451 :group 'org-plain-lists
452 :type 'boolean)
455 (defcustom org-plain-list-ordered-item-terminator t
456 "The character that makes a line with leading number an ordered list item.
457 Valid values are ?. and ?\). To get both terminators, use t. While
458 ?. may look nicer, it creates the danger that a line with leading
459 number may be incorrectly interpreted as an item. ?\) therefore is
460 the safe choice."
461 :group 'org-plain-lists
462 :type '(choice (const :tag "dot like in \"2.\"" ?.)
463 (const :tag "paren like in \"2)\"" ?\))
464 (const :tab "both" t)))
466 (defcustom org-auto-renumber-ordered-lists t
467 "Non-nil means, automatically renumber ordered plain lists.
468 Renumbering happens when the sequence have been changed with
469 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
470 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
471 :group 'org-plain-lists
472 :type 'boolean)
474 (defgroup org-archive nil
475 "Options concerning archiving in Org-mode."
476 :tag "Org Archive"
477 :group 'org-structure)
479 (defcustom org-archive-location "%s_archive::"
480 "The location where subtrees should be archived.
481 This string consists of two parts, separated by a double-colon.
483 The first part is a file name - when omitted, archiving happens in the same
484 file. %s will be replaced by the current file name (without directory part).
485 Archiving to a different file is useful to keep archived entries from
486 contributing to the Org-mode Agenda.
488 The part after the double colon is a headline. The archived entries will be
489 filed under that headline. When omitted, the subtrees are simply filed away
490 at the end of the file, as top-level entries.
492 Here are a few examples:
493 \"%s_archive::\"
494 If the current file is Projects.org, archive in file
495 Projects.org_archive, as top-level trees. This is the default.
497 \"::* Archived Tasks\"
498 Archive in the current file, under the top-level headline
499 \"* Archived Tasks\".
501 \"~/org/archive.org::\"
502 Archive in file ~/org/archive.org (absolute path), as top-level trees.
504 \"basement::** Finished Tasks\"
505 Archive in file ./basement (relative path), as level 3 trees
506 below the level 2 heading \"** Finished Tasks\".
508 You may set this option on a per-file basis by adding to the buffer a
509 line like
511 #+ARCHIVE: basement::** Finished Tasks"
512 :group 'org-archive
513 :type 'string)
515 (defcustom org-archive-mark-done t
516 "Non-nil means, mark archived entries as DONE."
517 :group 'org-archive
518 :type 'boolean)
520 (defcustom org-archive-stamp-time t
521 "Non-nil means, add a time stamp to archived entries.
522 The time stamp will be added directly after the TODO state keyword in the
523 first line, so it is probably best to use this in combinations with
524 `org-archive-mark-done'."
525 :group 'org-archive
526 :type 'boolean)
528 (defgroup org-table nil
529 "Options concerning tables in Org-mode."
530 :tag "Org Table"
531 :group 'org)
533 (defcustom org-enable-table-editor 'optimized
534 "Non-nil means, lines starting with \"|\" are handled by the table editor.
535 When nil, such lines will be treated like ordinary lines.
537 When equal to the symbol `optimized', the table editor will be optimized to
538 do the following:
539 - Use automatic overwrite mode in front of whitespace in table fields.
540 This make the structure of the table stay in tact as long as the edited
541 field does not exceed the column width.
542 - Minimize the number of realigns. Normally, the table is aligned each time
543 TAB or RET are pressed to move to another field. With optimization this
544 happens only if changes to a field might have changed the column width.
545 Optimization requires replacing the functions `self-insert-command',
546 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
547 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
548 very good at guessing when a re-align will be necessary, but you can always
549 force one with \\[org-ctrl-c-ctrl-c].
551 If you would like to use the optimized version in Org-mode, but the
552 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
554 This variable can be used to turn on and off the table editor during a session,
555 but in order to toggle optimization, a restart is required.
557 See also the variable `org-table-auto-blank-field'."
558 :group 'org-table
559 :type '(choice
560 (const :tag "off" nil)
561 (const :tag "on" t)
562 (const :tag "on, optimized" optimized)))
564 (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
565 "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
566 In the optimized version, the table editor takes over all simple keys that
567 normally just insert a character. In tables, the characters are inserted
568 in a way to minimize disturbing the table structure (i.e. in overwrite mode
569 for empty fields). Outside tables, the correct binding of the keys is
570 restored.
572 The default for this option is t if the optimized version is also used in
573 Org-mode. See the variable `org-enable-table-editor' for details. Changing
574 this variable requires a restart of Emacs to become effective."
575 :group 'org-table
576 :type 'boolean)
578 (defgroup org-table-settings nil
579 "Settings for tables in Org-mode."
580 :tag "Org Table Settings"
581 :group 'org-table)
583 (defcustom org-table-default-size "5x2"
584 "The default size for newly created tables, Columns x Rows."
585 :group 'org-table-settings
586 :type 'string)
588 (defcustom org-table-number-regexp "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$"
589 "Regular expression for recognizing numbers in table columns.
590 If a table column contains mostly numbers, it will be aligned to the
591 right. If not, it will be aligned to the left.
593 The default value of this option is a regular expression which allows
594 anything which looks remotely like a number as used in scientific
595 context. For example, all of the following will be considered a
596 number:
597 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
599 Other options offered by the customize interface are more restrictive."
600 :group 'org-table-settings
601 :type '(choice
602 (const :tag "Positive Integers"
603 "^[0-9]+$")
604 (const :tag "Integers"
605 "^[-+]?[0-9]+$")
606 (const :tag "Floating Point Numbers"
607 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
608 (const :tag "Floating Point Number or Integer"
609 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
610 (const :tag "Exponential, Floating point, Integer"
611 "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
612 (const :tag "Very General Number-Like"
613 "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$")
614 (string :tag "Regexp:")))
616 (defcustom org-table-number-fraction 0.5
617 "Fraction of numbers in a column required to make the column align right.
618 In a column all non-white fields are considered. If at least this
619 fraction of fields is matched by `org-table-number-fraction',
620 alignment to the right border applies."
621 :group 'org-table-settings
622 :type 'number)
624 (defgroup org-table-editing nil
625 "Bahavior of tables during editing in Org-mode."
626 :tag "Org Table Editing"
627 :group 'org-table)
629 (defcustom org-table-automatic-realign t
630 "Non-nil means, automatically re-align table when pressing TAB or RETURN.
631 When nil, aligning is only done with \\[org-table-align], or after column
632 removal/insertion."
633 :group 'org-table-editing
634 :type 'boolean)
636 (defcustom org-table-limit-column-width t ;kw
637 "Non-nil means, allow to limit the width of table columns with <N> fields."
638 :group 'org-table-editing
639 :type 'boolean)
641 (defcustom org-table-auto-blank-field t
642 "Non-nil means, automatically blank table field when starting to type into it.
643 This only happens when typing immediately after a field motion
644 command (TAB, S-TAB or RET).
645 Only relevant when `org-enable-table-editor' is equal to `optimized'."
646 :group 'org-table-editing
647 :type 'boolean)
649 (defcustom org-table-tab-jumps-over-hlines t
650 "Non-nil means, tab in the last column of a table with jump over a hline.
651 If a horizontal separator line is following the current line,
652 `org-table-next-field' can either create a new row before that line, or jump
653 over the line. When this option is nil, a new line will be created before
654 this line."
655 :group 'org-table-editing
656 :type 'boolean)
658 (defcustom org-table-tab-recognizes-table.el t
659 "Non-nil means, TAB will automatically notice a table.el table.
660 When it sees such a table, it moves point into it and - if necessary -
661 calls `table-recognize-table'."
662 :group 'org-table-editing
663 :type 'boolean)
665 (defgroup org-table-calculation nil
666 "Options concerning tables in Org-mode."
667 :tag "Org Table Calculation"
668 :group 'org-table)
670 (defcustom org-table-copy-increment t
671 "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
672 :group 'org-table-calculation
673 :type 'boolean)
675 (defcustom org-calc-default-modes
676 '(calc-internal-prec 12
677 calc-float-format (float 5)
678 calc-angle-mode deg
679 calc-prefer-frac nil
680 calc-symbolic-mode nil
681 calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
682 calc-display-working-message t
684 "List with Calc mode settings for use in calc-eval for table formulas.
685 The list must contain alternating symbols (Calc modes variables and values).
686 Don't remove any of the default settings, just change the values. Org-mode
687 relies on the variables to be present in the list."
688 :group 'org-table-calculation
689 :type 'plist)
691 (defcustom org-table-formula-evaluate-inline t
692 "Non-nil means, TAB and RET evaluate a formula in current table field.
693 If the current field starts with an equal sign, it is assumed to be a formula
694 which should be evaluated as described in the manual and in the documentation
695 string of the command `org-table-eval-formula'. This feature requires the
696 Emacs calc package.
697 When this variable is nil, formula calculation is only available through
698 the command \\[org-table-eval-formula]."
699 :group 'org-table-calculation
700 :type 'boolean)
703 (defcustom org-table-formula-use-constants t
704 "Non-nil means, interpret constants in formulas in tables.
705 A constant looks like `$c' or `$Grav' and will be replaced before evaluation
706 by the value given in `org-table-formula-constants', or by a value obtained
707 from the `constants.el' package."
708 :group 'org-table-calculation
709 :type 'boolean)
711 (defcustom org-table-formula-constants nil
712 "Alist with constant names and values, for use in table formulas.
713 The car of each element is a name of a constant, without the `$' before it.
714 The cdr is the value as a string. For example, if you'd like to use the
715 speed of light in a formula, you would configure
717 (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
719 and then use it in an equation like `$1*$c'."
720 :group 'org-table-calculation
721 :type '(repeat
722 (cons (string :tag "name")
723 (string :tag "value"))))
725 (defcustom org-table-formula-numbers-only nil
726 "Non-nil means, calculate only with numbers in table formulas.
727 Then all input fields will be converted to a number, and the result
728 must also be a number. When nil, calc's full potential is available
729 in table calculations, including symbolics etc."
730 :group 'org-table-calculation
731 :type 'boolean)
733 (defcustom org-table-allow-automatic-line-recalculation t
734 "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
735 Automatically means, when TAB or RET or C-c C-c are pressed in the line."
736 :group 'org-table-calculation
737 :type 'boolean)
739 (defgroup org-link nil
740 "Options concerning links in Org-mode."
741 :tag "Org Link"
742 :group 'org)
744 (defcustom org-descriptive-links t
745 "Non-nil means, hide link part and only show description of bracket links.
746 Bracket links are like [[link][descritpion]]. This variable sets the initial
747 state in new org-mode buffers. The setting can then be toggled on a
748 per-buffer basis from the Org->Hyperlinks menu."
749 :group 'org-link
750 :type 'boolean)
752 (defcustom org-link-style 'bracket
753 "The style of links to be inserted with \\[org-insert-link].
754 Possible values are:
755 bracket [[link][description]]. This is recommended
756 plain Description \\n link. The old way, no longer recommended."
757 :group 'org-link
758 :type '(choice
759 (const :tag "Bracket (recommended)" bracket)
760 (const :tag "Plain (no longer recommended)" plain)))
762 (defcustom org-link-format "%s"
763 "Default format for external, URL-like linkes in the buffer.
764 This is a format string for printf, %s will be replaced by the link text.
765 The recommended value is just \"%s\", since links will be protected by
766 enclosing them in double brackets. If you prefer plain links (see variable
767 `org-link-style'), \"<%s>\" is useful. Some people also recommend an
768 additional URL: prefix, so the format would be \"<URL:%s>\"."
769 :group 'org-link
770 :type '(choice
771 (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
772 (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
773 (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
774 (string :tag "Other" :value "<%s>")))
776 (defcustom org-activate-links '(bracket angle plain radio tag date)
777 "Types of links that should be activated in Org-mode files.
778 This is a list of symbols, each leading to the activation of a certain link
779 type. In principle, it does not hurt to turn on most link types - there may
780 be a small gain when turning off unused link types. The types are:
782 bracket The recommended [[link][description]] or [[link]] links with hiding.
783 angular Links in angular brackes that may contain whitespace like
784 <bbdb:Carsten Dominik>.
785 plain Plain links in normal text, no whitespace, like http://google.com.
786 radio Text that is matched by a radio target, see manual for details.
787 tag Tag settings in a headline (link to tag search).
788 date Time stamps (link to calendar).
789 camel CamelCase words defining text searches.
791 Changing this variable requires a restart of Emacs to become effective."
792 :group 'org-link
793 :type '(set (const :tag "Double bracket links (new style)" bracket)
794 (const :tag "Angular bracket links (old style)" angular)
795 (const :tag "plain text links" plain)
796 (const :tag "Radio target matches" radio)
797 (const :tag "Tags" tag)
798 (const :tag "Timestamps" date)
799 (const :tag "CamelCase words" camel)))
801 (defgroup org-link-store nil
802 "Options concerning storing links in Org-mode"
803 :tag "Org Store Link"
804 :group 'org-link)
806 (defcustom org-context-in-file-links t
807 "Non-nil means, file links from `org-store-link' contain context.
808 A search string will be added to the file name with :: as separator and
809 used to find the context when the link is activated by the command
810 `org-open-at-point'.
811 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
812 negates this setting for the duration of the command."
813 :group 'org-link-store
814 :type 'boolean)
816 (defcustom org-file-link-context-use-camel-case nil
817 "Non-nil means, use CamelCase to store a search context in a file link.
818 When nil, the search string simply consists of the words of the string.
819 CamelCase is deprecated, and support for it may be dropped in the future."
820 :group 'org-link-store
821 :type 'boolean)
823 (defcustom org-keep-stored-link-after-insertion nil
824 "Non-nil means, keep link in list for entire session.
826 The command `org-store-link' adds a link pointing to the current
827 location to an internal list. These links accumulate during a session.
828 The command `org-insert-link' can be used to insert links into any
829 Org-mode file (offering completion for all stored links). When this
830 option is nil, every link which has been inserted once using \\[org-insert-link]
831 will be removed from the list, to make completing the unused links
832 more efficient."
833 :group 'org-link-store
834 :type 'boolean)
836 (defcustom org-usenet-links-prefer-google nil
837 "Non-nil means, `org-store-link' will create web links to Google groups.
838 When nil, Gnus will be used for such links.
839 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
840 negates this setting for the duration of the command."
841 :group 'org-link-store
842 :type 'boolean)
844 (defgroup org-link-follow nil
845 "Options concerning following links in Org-mode"
846 :tag "Org Follow Link"
847 :group 'org-link)
849 (defcustom org-tab-follows-link nil
850 "Non-nil means, on links TAB will follow the link.
851 Needs to be set before org.el is loaded."
852 :group 'org-link-follow
853 :type 'boolean)
855 (defcustom org-return-follows-link nil
856 "Non-nil means, on links RET will follow the link.
857 Needs to be set before org.el is loaded."
858 :group 'org-link-follow
859 :type 'boolean)
861 (defcustom org-mouse-1-follows-link t
862 "Non-nil means, mouse-1 on a link will follow the link.
863 A longer mouse click will still set point. Does not wortk on XEmacs.
864 Needs to be set before org.el is loaded."
865 :group 'org-link-follow
866 :type 'boolean)
868 (defcustom org-mark-ring-length 4
869 "Number of different positions to be recorded in the ring
870 Changing this requires a restart of Emacs to work correctly."
871 :group 'org-link-follow
872 :type 'interger)
874 (defcustom org-link-frame-setup
875 '((vm . vm-visit-folder-other-frame)
876 (gnus . gnus-other-frame)
877 (file . find-file-other-window))
878 "Setup the frame configuration for following links.
879 When following a link with Emacs, it may often be useful to display
880 this link in another window or frame. This variable can be used to
881 set this up for the different types of links.
882 For VM, use any of
883 `vm-visit-folder'
884 `vm-visit-folder-other-frame'
885 For Gnus, use any of
886 `gnus'
887 `gnus-other-frame'
888 For FILE, use any of
889 `find-file'
890 `find-file-other-window'
891 `find-file-other-frame'
892 For the calendar, use the variable `calendar-setup'.
893 For BBDB, it is currently only possible to display the matches in
894 another window."
895 :group 'org-link-follow
896 :type '(list
897 (cons (const vm)
898 (choice
899 (const vm-visit-folder)
900 (const vm-visit-folder-other-window)
901 (const vm-visit-folder-other-frame)))
902 (cons (const gnus)
903 (choice
904 (const gnus)
905 (const gnus-other-frame)))
906 (cons (const file)
907 (choice
908 (const find-file)
909 (const find-file-other-window)
910 (const find-file-other-frame)))))
912 (defcustom org-open-non-existing-files nil
913 "Non-nil means, `org-open-file' will open non-existing file.
914 When nil, an error will be generated."
915 :group 'org-link-follow
916 :type 'boolean)
918 (defcustom org-confirm-shell-links 'yes-or-no-p
919 "Non-nil means, ask for confirmation before executing shell links.
920 Shell links can be dangerous, just thing about a link
922 [[shell:rm -rf ~/*][Google Search]]
924 This link would show up in your Org-mode document as \"Google Search\"
925 but really it would remove your entire home directory. Dangerous indeed.
926 Therefore I *definitely* advise agains setting this varaiable to nil.
927 Just change it to `y-or-n-p' of you want to confirm with a single key press
928 rather than having to type \"yes\"."
929 :group 'org-link-follow
930 :type '(choice
931 (const :tag "with yes-or-no (safer)" yes-or-no-p)
932 (const :tag "with y-or-n (faster)" y-or-n-p)
933 (const :tag "no confirmation (dangerous)" nil)))
935 (defconst org-file-apps-defaults-gnu
936 '((t . mailcap))
937 "Default file applications on a UNIX or GNU/Linux system.
938 See `org-file-apps'.")
940 (defconst org-file-apps-defaults-macosx
941 '((t . "open %s")
942 ("ps" . "gv %s")
943 ("ps.gz" . "gv %s")
944 ("eps" . "gv %s")
945 ("eps.gz" . "gv %s")
946 ("dvi" . "xdvi %s")
947 ("fig" . "xfig %s"))
948 "Default file applications on a MacOS X system.
949 The system \"open\" is known as a default, but we use X11 applications
950 for some files for which the OS does not have a good default.
951 See `org-file-apps'.")
953 (defconst org-file-apps-defaults-windowsnt
954 '((t . (w32-shell-execute "open" file)))
955 "Default file applications on a Windows NT system.
956 The system \"open\" is used for most files.
957 See `org-file-apps'.")
959 (defcustom org-file-apps
961 ("txt" . emacs)
962 ("tex" . emacs)
963 ("ltx" . emacs)
964 ("org" . emacs)
965 ("el" . emacs)
966 ("bib" . emacs)
968 "External applications for opening `file:path' items in a document.
969 Org-mode uses system defaults for different file types, but
970 you can use this variable to set the application for a given file
971 extension. The entries in this list are cons cells with a file extension
972 and the corresponding command. Possible values for the command are:
973 `emacs' The file will be visited by the current Emacs process.
974 `default' Use the default application for this file type.
975 string A command to be executed by a shell; %s will be replaced
976 by the path to the file.
977 sexp A Lisp form which will be evaluated. The file path will
978 be available in the Lisp variable `file'.
979 For more examples, see the system specific constants
980 `org-file-apps-defaults-macosx'
981 `org-file-apps-defaults-windowsnt'
982 `org-file-apps-defaults-gnu'."
983 :group 'org-link-follow
984 :type '(repeat
985 (cons (choice :value ""
986 (string :tag "Extension")
987 (const :tag "Default for unrecognized files" t)
988 (const :tag "Links to a directory" directory))
989 (choice :value ""
990 (const :tag "Visit with Emacs" emacs)
991 (const :tag "Use system default" default)
992 (string :tag "Command")
993 (sexp :tag "Lisp form")))))
995 (defcustom org-mhe-search-all-folders nil
996 "Non-nil means, that the search for the mh-message will be extended to
997 all folders if the message cannot be found in the folder given in the link.
998 Searching all folders is very effective with one of the search engines
999 supported by MH-E, but will be slow with pick."
1000 :group 'org-link-follow
1001 :type 'boolean)
1003 (defgroup org-remember nil
1004 "Options concerning interaction with remember.el."
1005 :tag "Org Remember"
1006 :group 'org)
1008 (defcustom org-directory "~/org"
1009 "Directory with org files.
1010 This directory will be used as default to prompt for org files.
1011 Used by the hooks for remember.el."
1012 :group 'org-remember
1013 :type 'directory)
1015 (defcustom org-default-notes-file "~/.notes"
1016 "Default target for storing notes.
1017 Used by the hooks for remember.el. This can be a string, or nil to mean
1018 the value of `remember-data-file'."
1019 :group 'org-remember
1020 :type '(choice
1021 (const :tag "Default from remember-data-file" nil)
1022 file))
1024 (defcustom org-remember-templates nil
1025 "Templates for the creation of remember buffers.
1026 When nil, just let remember make the buffer.
1027 When not nil, this is a list of 3-element lists. In each entry, the first
1028 element is a character, a unique key to select this template.
1029 The second element is the template. The third element is optional and can
1030 specify a destination file for remember items created with this template.
1031 The default file is given by `org-default-notes-file'.
1033 The template specifies the structure of the remember buffer. It should have
1034 a first line starting with a star, to act as the org-mode headline.
1035 Furthermore, the following %-escapes will be replaced with content:
1036 %t time stamp, date only
1037 %T time stamp with date and time
1038 %u inactive time stamp, date only
1039 %U inactive time stamp with date and time
1040 %n user name
1041 %a annotation, normally the link created with org-store-link
1042 %i initial content, the region when remember is called with C-u.
1043 If %i is indented, the entire inserted text will be indented as well.
1044 %? This will be removed, and the cursor placed at this position."
1045 :group 'org-remember
1046 :type '(repeat :tag "enabled"
1047 (list :value (?a "\n" nil)
1048 (character :tag "Selection Key")
1049 (string :tag "Template")
1050 (file :tag "Destination file (optional)"))))
1052 (defcustom org-reverse-note-order nil
1053 "Non-nil means, store new notes at the beginning of a file or entry.
1054 When nil, new notes will be filed to the end of a file or entry."
1055 :group 'org-remember
1056 :type '(choice
1057 (const :tag "Reverse always" t)
1058 (const :tag "Reverse never" nil)
1059 (repeat :tag "By file name regexp"
1060 (cons regexp boolean))))
1062 (defgroup org-todo nil
1063 "Options concerning TODO items in Org-mode."
1064 :tag "Org TODO"
1065 :group 'org)
1067 (defcustom org-todo-keywords '("TODO" "DONE")
1068 "List of TODO entry keywords.
1069 \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
1070 considered to mean that the entry is \"done\". All the other mean that
1071 action is required, and will make the entry show up in todo lists, diaries
1072 etc.
1073 The command \\[org-todo] cycles an entry through these states, and an
1074 additional state where no keyword is present. For details about this
1075 cycling, see also the variable `org-todo-interpretation'
1076 Changes become only effective after restarting Emacs."
1077 :group 'org-todo
1078 :group 'org-keywords
1079 :type '(repeat (string :tag "Keyword")))
1081 (defcustom org-todo-interpretation 'sequence
1082 "Controls how TODO keywords are interpreted.
1083 This variable is only relevant if `org-todo-keywords' contains more than two
1084 states. \\<org-mode-map>Possible values are `sequence' and `type'.
1086 When `sequence', \\[org-todo] will always switch to the next state in the
1087 `org-todo-keywords' list. When `type', \\[org-todo] only cycles from state
1088 to state when executed several times in direct succession. Otherwise, it
1089 switches directly to DONE from any state.
1090 See the manual for more information."
1091 :group 'org-todo
1092 :group 'org-keywords
1093 :type '(choice (const sequence)
1094 (const type)))
1096 (defcustom org-after-todo-state-change-hook nil
1097 "Hook which is run after the state of a TODO item was changed.
1098 The new state (a string with a TODO keyword, or nil) is available in the
1099 Lisp variable `state'."
1100 :group 'org-todo
1101 :type 'hook)
1103 (defcustom org-log-done nil
1104 "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
1105 When the state of an entry is changed from nothing to TODO, remove a previous
1106 closing date."
1107 :group 'org-todo
1108 :type 'boolean)
1110 (defgroup org-priorities nil
1111 "Priorities in Org-mode."
1112 :tag "Org Priorities"
1113 :group 'org-todo)
1115 (defcustom org-default-priority ?B
1116 "The default priority of TODO items.
1117 This is the priority an item get if no explicit priority is given."
1118 :group 'org-priorities
1119 :type 'character)
1121 (defcustom org-lowest-priority ?C
1122 "The lowest priority of TODO items. A character like ?A, ?B etc."
1123 :group 'org-priorities
1124 :type 'character)
1126 (defgroup org-time nil
1127 "Options concerning time stamps and deadlines in Org-mode."
1128 :tag "Org Time"
1129 :group 'org)
1131 (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
1132 "Formats for `format-time-string' which are used for time stamps.
1133 It is not recommended to change this constant.")
1135 (defcustom org-time-stamp-rounding-minutes 0
1136 "Number of minutes to round time stamps to upon insertion.
1137 When zero, insert the time unmodified. Useful rounding numbers
1138 should be factors of 60, so for example 5, 10, 15.
1139 When this is not zero, you can still force an exact time-stamp by using
1140 a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
1141 :group 'org-time
1142 :type 'integer)
1144 (defcustom org-deadline-warning-days 30
1145 "No. of days before expiration during which a deadline becomes active.
1146 This variable governs the display in the org file."
1147 :group 'org-time
1148 :type 'number)
1150 (defcustom org-popup-calendar-for-date-prompt t
1151 "Non-nil means, pop up a calendar when prompting for a date.
1152 In the calendar, the date can be selected with mouse-1. However, the
1153 minibuffer will also be active, and you can simply enter the date as well.
1154 When nil, only the minibuffer will be available."
1155 :group 'org-time
1156 :type 'boolean)
1158 (defcustom org-calendar-follow-timestamp-change t
1159 "Non-nil means, make the calendar window follow timestamp changes.
1160 When a timestamp is modified and the calendar window is visible, it will be
1161 moved to the new date."
1162 :group 'org-time
1163 :type 'boolean)
1165 (defgroup org-tags nil
1166 "Options concerning tags in Org-mode."
1167 :tag "Org Tags"
1168 :group 'org)
1170 (defcustom org-tags-column 48
1171 "The column to which tags should be indented in a headline.
1172 If this number is positive, it specifies the column. If it is negative,
1173 it means that the tags should be flushright to that column. For example,
1174 -79 works well for a normal 80 character screen."
1175 :group 'org-tags
1176 :type 'integer)
1178 (defcustom org-auto-align-tags t
1179 "Non-nil means, realign tags after pro/demotion of TODO state change.
1180 These operations change the length of a headline and therefore shift
1181 the tags around. With this options turned on, after each such operation
1182 the tags are again aligned to `org-tags-column'."
1183 :group 'org-tags
1184 :type 'boolean)
1186 (defcustom org-use-tag-inheritance t
1187 "Non-nil means, tags in levels apply also for sublevels.
1188 When nil, only the tags directly given in a specific line apply there.
1189 If you turn off this option, you very likely want to turn on the
1190 companion option `org-tags-match-list-sublevels'."
1191 :group 'org-tags
1192 :type 'boolean)
1194 (defcustom org-tags-match-list-sublevels nil
1195 "Non-nil means list also sublevels of headlines matching tag search.
1196 Because of tag inheritance (see variable `org-use-tag-inheritance'),
1197 the sublevels of a headline matching a tag search often also match
1198 the same search. Listing all of them can create very long lists.
1199 Setting this variable to nil causes subtrees of a match to be skipped.
1200 This option is off by default, because inheritance in on. If you turn
1201 inheritance off, you very likely want to turn this option on.
1203 As a special case, if the tag search is restricted to TODO items, the
1204 value of this variable is ignored and sublevels are always checked, to
1205 make sure all corresponding TODO items find their way into the list."
1206 :group 'org-tags
1207 :type 'boolean)
1209 (defvar org-tags-history nil
1210 "History of minibuffer reads for tags.")
1211 (defvar org-last-tags-completion-table nil
1212 "The last used completion table for tags.")
1214 (defgroup org-agenda nil
1215 "Options concerning agenda display Org-mode."
1216 :tag "Org Agenda"
1217 :group 'org)
1219 (defvar org-category nil
1220 "Variable used by org files to set a category for agenda display.
1221 Such files should use a file variable to set it, for example
1223 -*- mode: org; org-category: \"ELisp\"
1225 or contain a special line
1227 #+CATEGORY: ELisp
1229 If the file does not specify a category, then file's base name
1230 is used instead.")
1231 (make-variable-buffer-local 'org-category)
1233 (defcustom org-agenda-files nil
1234 "The files to be used for agenda display.
1235 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
1236 \\[org-remove-file]. You can also use customize to edit the list.
1238 If the value of the variable is not a list but a single file name, then
1239 the list of agenda files is actually stored and maintained in that file, one
1240 agenda file per line."
1241 :group 'org-agenda
1242 :type '(choice
1243 (repeat :tag "List of files" file)
1244 (file :tag "Store list in a file\n" :value "~/.agenda_files")))
1246 (defcustom org-agenda-custom-commands '(("w" todo "WAITING"))
1247 "Custom commands for the agenda.
1248 These commands will be offered on the splash screen displayed by the
1249 agenda dispatcher \\[org-agenda]. Each entry is a list of 3 items:
1251 key The key (a single char as a string) to be associated with the command.
1252 type The command type, any of the following symbols:
1253 todo Entries with a specific TODO keyword, in all agenda files.
1254 tags Tags match in all agenda files.
1255 tags-todo Tags match in all agenda files, TODO entries only.
1256 todo-tree Sparse tree of specific TODO keyword in *current* file.
1257 tags-tree Sparse tree with all tags matches in *current* file.
1258 occur-tree Occur sparse tree for current file.
1259 match What to search for:
1260 - a single keyword for TODO keyword searches
1261 - a tags match expression for tags searches
1262 - a regular expression for occur searches"
1263 :group 'org-agenda
1264 :type '(repeat
1265 (list (string :tag "Key")
1266 (choice :tag "Type"
1267 (const :tag "Tags search in all agenda files" tags)
1268 (const :tag "Tags search of TODO entries, all agenda files" tags-todo)
1269 (const :tag "TODO keyword search in all agenda files" todo)
1270 (const :tag "Tags sparse tree in current buffer" tags-tree)
1271 (const :tag "TODO keyword tree in current buffer" todo-tree)
1272 (const :tag "Occur tree in current buffer" occur-tree))
1273 (string :tag "Match"))))
1275 ;; Fixme: Need a way to toggle this variable, maybe a mode in the
1276 ;; agenda buffer?
1277 (defcustom org-agenda-todo-list-sublevels t
1278 "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
1279 When nil, the sublevels of a TODO entry are not checked, resulting in
1280 potentially much shorter TODO lists."
1281 :group 'org-agenda
1282 :group 'org-todo
1283 :type 'boolean)
1285 (defcustom org-agenda-include-all-todo nil
1286 "Non-nil means, the agenda will always contain all TODO entries.
1287 When nil, date-less entries will only be shown if `org-agenda' is called
1288 with a prefix argument.
1289 When non-nil, the TODO entries will be listed at the top of the agenda, before
1290 the entries for specific days."
1291 :group 'org-agenda
1292 :type 'boolean)
1294 (defcustom org-agenda-include-diary nil
1295 "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
1296 :group 'org-agenda
1297 :type 'boolean)
1299 (defcustom org-calendar-to-agenda-key [?c]
1300 "The key to be installed in `calendar-mode-map' for switching to the agenda.
1301 The command `org-calendar-goto-agenda' will be bound to this key. The
1302 default is the character `c' because then `c' can be used to switch back and
1303 forth between agenda and calendar."
1304 :group 'org-agenda
1305 :type 'sexp)
1307 (defgroup org-agenda-setup nil
1308 "Options concerning setting up the Agenda window in Org Mode."
1309 :tag "Org Agenda Window Setup"
1310 :group 'org-agenda)
1312 (defcustom org-agenda-mouse-1-follows-link nil
1313 "Non-nil means, mouse-1 on a link will follow the link in the agenda.
1314 A longer mouse click will still set point. Does not wortk on XEmacs.
1315 Needs to be set before org.el is loaded."
1316 :group 'org-agenda-setup
1317 :type 'boolean)
1319 (defcustom org-agenda-start-with-follow-mode t
1320 "The initial value of follwo-mode in a newly created agenda window."
1321 :group 'org-agenda-setup
1322 :type 'boolean)
1324 (defcustom org-select-timeline-window t
1325 "Non-nil means, after creating a timeline, move cursor into Timeline window.
1326 When nil, cursor will remain in the current window."
1327 :group 'org-agenda-setup
1328 :type 'boolean)
1330 (defcustom org-select-agenda-window t
1331 "Non-nil means, after creating an agenda, move cursor into Agenda window.
1332 When nil, cursor will remain in the current window."
1333 :group 'org-agenda-setup
1334 :type 'boolean)
1336 (defcustom org-fit-agenda-window t
1337 "Non-nil means, change window size of agenda to fit content."
1338 :group 'org-agenda-setup
1339 :type 'boolean)
1341 (defgroup org-agenda-display nil
1342 "Options concerning what to display initially in Agenda."
1343 :tag "Org Agenda Display"
1344 :group 'org-agenda)
1346 (defcustom org-agenda-show-all-dates t
1347 "Non-nil means, `org-agenda' shows every day in the selected range.
1348 When nil, only the days which actually have entries are shown."
1349 :group 'org-agenda-display
1350 :type 'boolean)
1352 (defcustom org-agenda-start-on-weekday 1
1353 "Non-nil means, start the overview always on the specified weekday.
1354 0 denotes Sunday, 1 denotes Monday etc.
1355 When nil, always start on the current day."
1356 :group 'org-agenda-display
1357 :type '(choice (const :tag "Today" nil)
1358 (number :tag "Weekday No.")))
1360 (defcustom org-agenda-ndays 7
1361 "Number of days to include in overview display.
1362 Should be 1 or 7."
1363 :group 'org-agenda-display
1364 :type 'number)
1366 (defcustom org-agenda-use-time-grid t
1367 "Non-nil means, show a time grid in the agenda schedule.
1368 A time grid is a set of lines for specific times (like every two hours between
1369 8:00 and 20:00). The items scheduled for a day at specific times are
1370 sorted in between these lines.
1371 For details about when the grid will be shown, and what it will look like, see
1372 the variable `org-agenda-time-grid'."
1373 :group 'org-agenda-display
1374 :type 'boolean)
1376 (defcustom org-agenda-time-grid
1377 '((daily today require-timed)
1378 "----------------"
1379 (800 1000 1200 1400 1600 1800 2000))
1381 "The settings for time grid for agenda display.
1382 This is a list of three items. The first item is again a list. It contains
1383 symbols specifying conditions when the grid should be displayed:
1385 daily if the agenda shows a single day
1386 weekly if the agenda shows an entire week
1387 today show grid on current date, independent of daily/weekly display
1388 require-timed show grid only if at least on item has a time specification
1390 The second item is a string which will be places behing the grid time.
1392 The third item is a list of integers, indicating the times that should have
1393 a grid line."
1394 :group 'org-agenda-display
1395 :type
1396 '(list
1397 (set :greedy t :tag "Grid Display Options"
1398 (const :tag "Show grid in single day agenda display" daily)
1399 (const :tag "Show grid in weekly agenda display" weekly)
1400 (const :tag "Always show grid for today" today)
1401 (const :tag "Show grid only if any timed entries are present"
1402 require-timed)
1403 (const :tag "Skip grid times already present in an entry"
1404 remove-match))
1405 (string :tag "Grid String")
1406 (repeat :tag "Grid Times" (integer :tag "Time"))))
1408 (defcustom org-agenda-sorting-strategy '(time-up category-keep priority-down)
1409 "Sorting structure for the agenda items of a single day.
1410 This is a list of symbols which will be used in sequence to determine
1411 if an entry should be listed before another entry. The following
1412 symbols are recognized:
1414 time-up Put entries with time-of-day indications first, early first
1415 time-down Put entries with time-of-day indications first, late first
1416 category-keep Keep the default order of categories, corresponding to the
1417 sequence in `org-agenda-files'.
1418 category-up Sort alphabetically by category, A-Z.
1419 category-down Sort alphabetically by category, Z-A.
1420 priority-up Sort numerically by priority, high priority last.
1421 priority-down Sort numerically by priority, high priority first.
1423 The different possibilities will be tried in sequence, and testing stops
1424 if one comparison returns a \"not-equal\". For example, the default
1425 '(time-up category-keep priority-down)
1426 means: Pull out all entries having a specified time of day and sort them,
1427 in order to make a time schedule for the current day the first thing in the
1428 agenda listing for the day. Of the entries without a time indication, keep
1429 the grouped in categories, don't sort the categories, but keep them in
1430 the sequence given in `org-agenda-files'. Within each category sort by
1431 priority.
1433 Leaving out `category-keep' would mean that items will be sorted across
1434 categories by priority."
1435 :group 'org-agenda-display
1436 :type '(repeat
1437 (choice
1438 (const time-up)
1439 (const time-down)
1440 (const category-keep)
1441 (const category-up)
1442 (const category-down)
1443 (const priority-up)
1444 (const priority-down))))
1446 (defcustom org-sort-agenda-notime-is-late t
1447 "Non-nil means, items without time are considered late.
1448 This is only relevant for sorting. When t, items which have no explicit
1449 time like 15:30 will be considered as 24:01, i.e. later than any items which
1450 do have a time. When nil, the default time is before 0:00. You can use this
1451 option to decide if the schedule for today should come before or after timeless
1452 agenda entries."
1453 :group 'org-agenda-display
1454 :type 'boolean)
1457 (defgroup org-agenda-prefix nil
1458 "Options concerning the entry prefix in the Org-mode agenda display."
1459 :tag "Org Agenda Prefix"
1460 :group 'org-agenda)
1462 (defcustom org-agenda-prefix-format " %-12:c%?-12t% s"
1463 "Format specification for the prefix of items in the agenda buffer.
1464 This format works similar to a printf format, with the following meaning:
1466 %c the category of the item, \"Diary\" for entries from the diary, or
1467 as given by the CATEGORY keyword or derived from the file name.
1468 %T the first tag of the item.
1469 %t the time-of-day specification if one applies to the entry, in the
1470 format HH:MM
1471 %s Scheduling/Deadline information, a short string
1473 All specifiers work basically like the standard `%s' of printf, but may
1474 contain two additional characters: A question mark just after the `%' and
1475 a whitespace/punctuation character just before the final letter.
1477 If the first character after `%' is a question mark, the entire field
1478 will only be included if the corresponding value applies to the
1479 current entry. This is useful for fields which should have fixed
1480 width when present, but zero width when absent. For example,
1481 \"%?-12t\" will result in a 12 character time field if a time of the
1482 day is specified, but will completely disappear in entries which do
1483 not contain a time.
1485 If there is punctuation or whitespace character just before the final
1486 format letter, this character will be appended to the field value if
1487 the value is not empty. For example, the format \"%-12:c\" leads to
1488 \"Diary: \" if the category is \"Diary\". If the category were be
1489 empty, no additional colon would be interted.
1491 The default value of this option is \" %-12:c%?-12t% s\", meaning:
1492 - Indent the line with two space characters
1493 - Give the category in a 12 chars wide field, padded with whitespace on
1494 the right (because of `-'). Append a colon if there is a category
1495 (because of `:').
1496 - If there is a time-of-day, put it into a 12 chars wide field. If no
1497 time, don't put in an empty field, just skip it (because of '?').
1498 - Finally, put the scheduling information and append a whitespace.
1500 As another example, if you don't want the time-of-day of entries in
1501 the prefix, you could use:
1503 (setq org-agenda-prefix-format \" %-11:c% s\")
1505 See also the variables `org-agenda-remove-times-when-in-prefix' and
1506 `org-agenda-remove-tags-when-in-prefix'."
1507 :type 'string
1508 :group 'org-agenda-prefix)
1510 (defcustom org-timeline-prefix-format " % s"
1511 "Like `org-agenda-prefix-format', but for the timeline of a single file."
1512 :type 'string
1513 :group 'org-agenda-prefix)
1515 (defvar org-prefix-format-compiled nil
1516 "The compiled version of the most recently used prefix format.
1517 Depending on which command was used last, this may be the compiled version
1518 of `org-agenda-prefix-format' or `org-timeline-prefix-format'.")
1520 ;; FIXME: There seem to be situations where this does no work.
1521 (defcustom org-agenda-remove-times-when-in-prefix t
1522 "Non-nil means, remove duplicate time specifications in agenda items.
1523 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1524 time-of-day specification in a headline or diary entry is extracted and
1525 placed into the prefix. If this option is non-nil, the original specification
1526 \(a timestamp or -range, or just a plain time(range) specification like
1527 11:30-4pm) will be removed for agenda display. This makes the agenda less
1528 cluttered.
1529 The option can be t or nil. It may also be the symbol `beg', indicating
1530 that the time should only be removed what it is located at the beginning of
1531 the headline/diary entry."
1532 :group 'org-agenda-prefix
1533 :type '(choice
1534 (const :tag "Always" t)
1535 (const :tag "Never" nil)
1536 (const :tag "When at beginning of entry" beg)))
1538 (defcustom org-agenda-remove-tags-when-in-prefix nil
1539 "Non-nil means, remove the tags from the headline copy in the agenda.
1540 When this is the symbol `prefix', only remove tags when
1541 `org-agenda-prefix-format' contains a `%T' specifier."
1542 :group 'org-agenda-prefix
1543 :type '(choice
1544 (const :tag "Always" t)
1545 (const :tag "Never" nil)
1546 (const :tag "When prefix format contains %T" prefix)))
1548 (defgroup org-export nil
1549 "Options for exporting org-listings."
1550 :tag "Org Export"
1551 :group 'org)
1553 (defgroup org-export-general nil
1554 "General options for exporting Org-mode files."
1555 :tag "Org Export General"
1556 :group 'org-export)
1558 (defcustom org-export-publishing-directory "."
1559 "Path to the location where exported files should be located.
1560 This path may be relative to the directory where the Org-mode file lives.
1561 The default is to put them into the same directory as the Org-mode file."
1562 :group 'org-export-general
1563 :type 'directory)
1565 (defcustom org-export-language-setup
1566 '(("en" "Author" "Date" "Table of Contents")
1567 ("da" "Ophavsmand" "Dato" "Indhold")
1568 ("de" "Autor" "Datum" "Inhaltsverzeichnis")
1569 ("es" "Autor" "Fecha" "\xccndice")
1570 ("fr" "Auteur" "Date" "Table des Mati\xe8res")
1571 ("it" "Autore" "Data" "Indice")
1572 ("nl" "Auteur" "Datum" "Inhoudsopgave")
1573 ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
1574 ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
1575 "Terms used in export text, translated to different languages.
1576 Use the variable `org-export-default-language' to set the language,
1577 or use the +OPTION lines for a per-file setting."
1578 :group 'org-export-general
1579 :type '(repeat
1580 (list
1581 (string :tag "HTML language tag")
1582 (string :tag "Author")
1583 (string :tag "Date")
1584 (string :tag "Table of Contents"))))
1586 (defcustom org-export-default-language "en"
1587 "The default language of HTML export, as a string.
1588 This should have an association in `org-export-language-setup'."
1589 :group 'org-export-general
1590 :type 'string)
1592 (defcustom org-export-headline-levels 3
1593 "The last level which is still exported as a headline.
1594 Inferior levels will produce itemize lists when exported.
1595 Note that a numeric prefix argument to an exporter function overrides
1596 this setting.
1598 This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
1599 :group 'org-export-general
1600 :type 'number)
1602 (defcustom org-export-with-section-numbers t
1603 "Non-nil means, add section numbers to headlines when exporting.
1605 This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
1606 :group 'org-export-general
1607 :type 'boolean)
1609 (defcustom org-export-with-toc t
1610 "Non-nil means, create a table of contents in exported files.
1611 The TOC contains headlines with levels up to`org-export-headline-levels'.
1613 Headlines which contain any TODO items will be marked with \"(*)\" in
1614 ASCII export, and with red color in HTML output.
1616 In HTML output, the TOC will be clickable.
1618 This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
1619 :group 'org-export-general
1620 :type 'boolean)
1622 (defcustom org-export-mark-todo-in-toc nil
1623 "Non-nil means, mark TOC lines that contain any open TODO items."
1624 :group 'org-export-general
1625 :type 'boolean)
1627 (defcustom org-export-preserve-breaks nil
1628 "Non-nil means, preserve all line breaks when exporting.
1629 Normally, in HTML output paragraphs will be reformatted. In ASCII
1630 export, line breaks will always be preserved, regardless of this variable.
1632 This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
1633 :group 'org-export-general
1634 :type 'boolean)
1636 (defcustom org-export-with-tags t
1637 "Nil means, do not export tags, just remove them from headlines."
1638 :group 'org-export-general
1639 :type 'boolean)
1641 (defcustom org-export-with-timestamps t
1642 "Nil means, do not export timestamps and associated keywords."
1643 :group 'org-export-general
1644 :type 'boolean)
1646 (defgroup org-export-translation nil
1647 "Options for translating special ascii sequences for the export backends."
1648 :tag "Org Export Translation"
1649 :group 'org-export)
1651 (defcustom org-export-with-emphasize t
1652 "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
1653 If the export target supports emphasizing text, the word will be
1654 typeset in bold, italic, or underlined, respectively. Works only for
1655 single words, but you can say: I *really* *mean* *this*.
1656 Not all export backends support this.
1658 This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
1659 :group 'org-export-translation
1660 :type 'boolean)
1662 (defcustom org-export-with-sub-superscripts t
1663 "Non-nil means, interpret \"_\" and \"^\" for export.
1664 When this option is turned on, you can use TeX-like syntax for sub- and
1665 superscripts. Several characters after \"_\" or \"^\" will be
1666 considered as a single item - so grouping with {} is normally not
1667 needed. For example, the following things will be parsed as single
1668 sub- or superscripts.
1670 10^24 or 10^tau several digits will be considered 1 item.
1671 10^-12 or 10^-tau a leading sign with digits or a word
1672 x^2-y^3 will be read as x^2 - y^3, because items are
1673 terminated by almost any nonword/nondigit char.
1674 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
1676 Still, ambiguity is possible - so when in doubt use {} to enclose the
1677 sub/superscript.
1678 Not all export backends support this, but HTML does.
1680 This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
1681 :group 'org-export-translation
1682 :type 'boolean)
1684 (defcustom org-export-with-TeX-macros t
1685 "Non-nil means, interpret simple TeX-like macros when exporting.
1686 For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
1687 No only real TeX macros will work here, but the standard HTML entities
1688 for math can be used as macro names as well. For a list of supported
1689 names in HTML export, see the constant `org-html-entities'.
1690 Not all export backends support this.
1692 This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
1693 :group 'org-export-translation
1694 :type 'boolean)
1696 (defcustom org-export-with-fixed-width t
1697 "Non-nil means, lines starting with \":\" will be in fixed width font.
1698 This can be used to have pre-formatted text, fragments of code etc. For
1699 example:
1700 : ;; Some Lisp examples
1701 : (while (defc cnt)
1702 : (ding))
1703 will be looking just like this in also HTML. See also the QUOTE keyword.
1704 Not all export backends support this.
1706 This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
1707 :group 'org-export-translation
1708 :type 'boolean)
1710 (defcustom org-match-sexp-depth 3
1711 "Number of stacked braces for sub/superscript matching.
1712 This has to be set before loading org.el to be effective."
1713 :group 'org-export-translation
1714 :type 'integer)
1716 (defgroup org-export-tables nil
1717 "Options for exporting tables in Org-mode."
1718 :tag "Org Export Tables"
1719 :group 'org-export)
1721 (defcustom org-export-with-tables t
1722 "If non-nil, lines starting with \"|\" define a table.
1723 For example:
1725 | Name | Address | Birthday |
1726 |-------------+----------+-----------|
1727 | Arthur Dent | England | 29.2.2100 |
1729 Not all export backends support this.
1731 This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
1732 :group 'org-export-tables
1733 :type 'boolean)
1735 (defcustom org-export-highlight-first-table-line t
1736 "Non-nil means, highlight the first table line.
1737 In HTML export, this means use <th> instead of <td>.
1738 In tables created with table.el, this applies to the first table line.
1739 In Org-mode tables, all lines before the first horizontal separator
1740 line will be formatted with <th> tags."
1741 :group 'org-export-tables
1742 :type 'boolean)
1744 (defcustom org-export-table-remove-special-lines t
1745 "Remove special lines and marking characters in calculating tables.
1746 This removes the special marking character column from tables that are set
1747 up for spreadsheet calculations. It also removes the entire lines
1748 marked with `!', `_', or `^'. The lines with `$' are kept, because
1749 the values of constants may be useful to have."
1750 :group 'org-export-tables
1751 :type 'boolean)
1753 (defcustom org-export-prefer-native-exporter-for-tables nil
1754 "Non-nil means, always export tables created with table.el natively.
1755 Natively means, use the HTML code generator in table.el.
1756 When nil, Org-mode's own HTML generator is used when possible (i.e. if
1757 the table does not use row- or column-spanning). This has the
1758 advantage, that the automatic HTML conversions for math symbols and
1759 sub/superscripts can be applied. Org-mode's HTML generator is also
1760 much faster."
1761 :group 'org-export-tables
1762 :type 'boolean)
1764 (defgroup org-export-ascii nil
1765 "Options specific for ASCII export of Org-mode files."
1766 :tag "Org Export ASCII"
1767 :group 'org-export)
1769 (defcustom org-export-ascii-show-new-buffer t
1770 "Non-nil means, popup buffer containing the exported ASCII text.
1771 Otherwise the buffer will just be saved to a file and stay hidden."
1772 :group 'org-export-ascii
1773 :type 'boolean)
1775 (defgroup org-export-xml nil
1776 "Options specific for XML export of Org-mode files."
1777 :tag "Org Export XML"
1778 :group 'org-export)
1780 (defcustom org-export-xml-type 'xoxo ;kw, if we have only one.
1781 "The kind of XML to be produced by the XML exporter.
1782 Allowed values are:
1783 xoxo The XOXO exporter."
1784 :group 'org-export-xml
1785 :type '(choice
1786 (const :tag "XOXO" xoxo)))
1788 (defgroup org-export-html nil
1789 "Options specific for HTML export of Org-mode files."
1790 :tag "Org Export HTML"
1791 :group 'org-export)
1793 (defcustom org-export-html-style
1794 "<style type=\"text/css\">
1795 html {
1796 font-family: Times, serif;
1797 font-size: 12pt;
1799 .title { text-align: center; }
1800 .todo, .deadline { color: red; }
1801 .done { color: green; }
1802 .target { background-color: lavender; }
1803 pre {
1804 border: 1pt solid #AEBDCC;
1805 background-color: #F3F5F7;
1806 padding: 5pt;
1807 font-family: courier, monospace;
1809 table { border-collapse: collapse; }
1810 td, th {
1811 vertical-align: top;
1812 border: 1pt solid #ADB9CC;
1814 </style>"
1815 "The default style specification for exported HTML files.
1816 Since there are different ways of setting style information, this variable
1817 needs to contain the full HTML structure to provide a style, including the
1818 surrounding HTML tags. The style specifications should include definitions
1819 for new classes todo, done, title, and deadline. For example, legal values
1820 would be:
1822 <style type=\"text/css\">
1823 p { font-weight: normal; color: gray; }
1824 h1 { color: black; }
1825 .title { text-align: center; }
1826 .todo, .deadline { color: red; }
1827 .done { color: green; }
1828 </style>
1830 or, if you want to keep the style in a file,
1832 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
1834 As the value of this option simply gets inserted into the HTML <head> header,
1835 you can \"misuse\" it to add arbitrary text to the header."
1836 :group 'org-export-html
1837 :type 'string)
1839 (defcustom org-export-html-link-org-files-as-html t
1840 "Non-nil means, make file links to `file.org' point to `file.html'.
1841 When org-mode is exporting an org-mode file to HTML, links to
1842 non-html files are directly put into a href tag in HTML.
1843 However, links to other Org-mode files (recognized by the
1844 extension `.org.) should become links to the corresponding html
1845 file, assuming that the linked org-mode file will also be
1846 converted to HTML.
1847 When nil, the links still point to the plain `.org' file."
1848 :group 'org-export-html
1849 :type 'boolean)
1851 (defcustom org-export-html-inline-images t
1852 "Non-nil means, inline images into exported HTML pages.
1853 The link will still be to the original location of the image file.
1854 So if you are moving the page, lets say to your public HTML site,
1855 you will have to move the image and maybe change the link."
1856 :group 'org-export-html
1857 :type 'boolean)
1859 (defcustom org-export-html-expand t
1860 "Non-nil means, for HTML export, treat @<...> as HTML tag.
1861 When nil, these tags will be exported as plain text and therefore
1862 not be interpreted by a browser.
1864 This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
1865 :group 'org-export-html
1866 :type 'boolean)
1868 (defcustom org-export-html-table-tag
1869 "<table border=1 cellspacing=0 cellpadding=6>"
1870 "The HTML tag used to start a table.
1871 This must be a <table> tag, but you may change the options like
1872 borders and spacing."
1873 :group 'org-export-html
1874 :type 'string)
1876 (defcustom org-export-html-with-timestamp nil
1877 "If non-nil, write `org-export-html-html-helper-timestamp'
1878 into the exported HTML text. Otherwise, the buffer will just be saved
1879 to a file."
1880 :group 'org-export-html
1881 :type 'boolean)
1883 (defcustom org-export-html-html-helper-timestamp
1884 "<br><br><hr><p><!-- hhmts start --> <!-- hhmts end -->\n"
1885 "The HTML tag used as timestamp delimiter for HTML-helper-mode."
1886 :group 'org-export-html
1887 :type 'string)
1889 (defcustom org-export-html-show-new-buffer nil
1890 "Non-nil means, popup buffer containing the exported html text.
1891 Otherwise, the buffer will just be saved to a file and stay hidden."
1892 :group 'org-export-html
1893 :type 'boolean)
1895 (defgroup org-export-icalendar nil
1896 "Options specific for iCalendar export of Org-mode files."
1897 :tag "Org Export iCalendar"
1898 :group 'org-export)
1900 (defcustom org-combined-agenda-icalendar-file "~/org.ics"
1901 "The file name for the iCalendar file covering all agenda files.
1902 This file is created with the command \\[org-export-icalendar-all-agenda-files]."
1903 :group 'org-export-icalendar
1904 :type 'file)
1906 (defcustom org-icalendar-include-todo nil
1907 "Non-nil means, export to iCalendar files should also cover TODO items."
1908 :group 'org-export-icalendar
1909 :type 'boolean)
1911 (defcustom org-icalendar-combined-name "OrgMode"
1912 "Calendar name for the combined iCalendar representing all agenda files."
1913 :group 'org-export-icalendar
1914 :type 'string)
1916 (defgroup org-font-lock nil
1917 "Font-lock settings for highlighting in Org-mode."
1918 :tag "Org Font Lock"
1919 :group 'org)
1921 (defcustom org-level-color-stars-only nil
1922 "Non-nil means fontify only the stars in each headline.
1923 When nil, the entire headline is fontified.
1924 Changing it requires restart of `font-lock-mode' to become effective
1925 also in regions already fontified."
1926 :group 'org-font-lock
1927 :type 'boolean)
1929 (defcustom org-hide-leading-stars nil
1930 "Non-nil means, hide the first N-1 stars in a headline.
1931 This works by using the face `org-hide' for these stars. This
1932 face is white for a light background, and black for a dark
1933 background. You may have to customize the face `org-hide' to
1934 make this work.
1935 Changing it requires restart of `font-lock-mode' to become effective
1936 also in regions already fontified.
1937 You may also set this on a per-file basis by adding one of the following
1938 lines to the buffer:
1940 #+STARTUP: hidestars
1941 #+STARTUP: showstars"
1942 :group 'org-font-lock
1943 :type 'boolean)
1945 (defcustom org-fontify-done-headline nil
1946 "Non-nil means, change the face of a headline if it is marked DONE.
1947 Normally, only the TODO/DONE keyword indicates the state of a headline.
1948 When this is non-nil, the headline after the keyword is set to the
1949 `org-headline-done' as an additional indication."
1950 :group 'org-font-lock
1951 :type 'boolean)
1953 (defcustom org-fontify-emphasized-text t
1954 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
1955 Changing this variable requires a restart of Emacs to take effect."
1956 :group 'org-font-lock
1957 :type 'boolean)
1959 (defgroup org-faces nil
1960 "Faces in Org-mode."
1961 :tag "Org Faces"
1962 :group 'org-font-lock)
1964 (defun org-compatible-face (specs)
1965 "Make a compatible face specification.
1966 XEmacs and Emacs 21 do not know about the `min-colors' attribute.
1967 For them we convert a (min-colors 8) entry to a `tty' entry and move it
1968 to the top of the list. The `min-colors' attribute will be removed from
1969 any other entries, and any resulting duplicates will be removed entirely."
1970 (if (or (featurep 'xemacs) (< emacs-major-version 22))
1971 (let (r e a)
1972 (while (setq e (pop specs))
1973 (cond
1974 ((memq (car e) '(t default)) (push e r))
1975 ((setq a (member '(min-colors 8) (car e)))
1976 (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
1977 (cdr e)))))
1978 ((setq a (assq 'min-colors (car e)))
1979 (setq e (cons (delq a (car e)) (cdr e)))
1980 (or (assoc (car e) r) (push e r)))
1981 (t (or (assoc (car e) r) (push e r)))))
1982 (nreverse r))
1983 specs))
1985 (defface org-hide
1986 '((((background light)) (:foreground "white"))
1987 (((background dark)) (:foreground "black")))
1988 "Face used to hide leading stars in headlines.
1989 The forground color of this face should be equal to the background
1990 color of the frame."
1991 :group 'org-faces)
1993 (defface org-level-1 ;; font-lock-function-name-face
1994 (org-compatible-face
1995 '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
1996 (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
1997 (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
1998 (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
1999 (((class color) (min-colors 8)) (:foreground "blue" :bold t))
2000 (t (:bold t))))
2001 "Face used for level 1 headlines."
2002 :group 'org-faces)
2004 (defface org-level-2 ;; font-lock-variable-name-face
2005 (org-compatible-face
2006 '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
2007 (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
2008 (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
2009 (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
2010 (t (:bold t))))
2011 "Face used for level 2 headlines."
2012 :group 'org-faces)
2014 (defface org-level-3 ;; font-lock-keyword-face
2015 (org-compatible-face
2016 '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
2017 (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
2018 (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
2019 (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
2020 (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
2021 (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
2022 (t (:bold t))))
2023 "Face used for level 3 headlines."
2024 :group 'org-faces)
2026 (defface org-level-4 ;; font-lock-comment-face
2027 (org-compatible-face
2028 '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
2029 (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
2030 (((class color) (min-colors 16) (background light)) (:foreground "red"))
2031 (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
2032 (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
2033 (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
2034 (t (:bold t))))
2035 "Face used for level 4 headlines."
2036 :group 'org-faces)
2038 (defface org-level-5 ;; font-lock-type-face
2039 (org-compatible-face
2040 '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
2041 (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
2042 (((class color) (min-colors 8)) (:foreground "green"))))
2043 "Face used for level 5 headlines."
2044 :group 'org-faces)
2046 (defface org-level-6 ;; font-lock-constant-face
2047 (org-compatible-face
2048 '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
2049 (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
2050 (((class color) (min-colors 8)) (:foreground "magenta"))))
2051 "Face used for level 6 headlines."
2052 :group 'org-faces)
2054 (defface org-level-7 ;; font-lock-builtin-face
2055 (org-compatible-face
2056 '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
2057 (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
2058 (((class color) (min-colors 8)) (:foreground "blue")))) ;; FIXME: for dark bg?
2059 "Face used for level 7 headlines."
2060 :group 'org-faces)
2062 (defface org-level-8 ;; font-lock-string-face
2063 (org-compatible-face
2064 '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
2065 (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
2066 (((class color) (min-colors 8)) (:foreground "green"))))
2067 "Face used for level 8 headlines."
2068 :group 'org-faces)
2070 (defface org-special-keyword ;; font-lock-string-face
2071 (org-compatible-face
2072 '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
2073 (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
2074 (t (:italic t))))
2075 "Face used for special keywords."
2076 :group 'org-faces)
2078 (defface org-warning ;; font-lock-warning-face
2079 (org-compatible-face
2080 '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
2081 (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
2082 (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
2083 (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
2084 (t (:bold t))))
2085 "Face for deadlines and TODO keywords."
2086 :group 'org-faces)
2088 (defface org-headline-done ;; font-lock-string-face
2089 (org-compatible-face
2090 '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
2091 (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
2092 (((class color) (min-colors 8) (background light)) (:bold nil))))
2093 "Face used to indicate that a headline is DONE.
2094 This face is only used if `org-fontify-done-headline' is set."
2095 :group 'org-faces)
2097 (defface org-link
2098 '((((class color) (background light)) (:foreground "Purple" :underline t))
2099 (((class color) (background dark)) (:foreground "Cyan" :underline t))
2100 (t (:underline t)))
2101 "Face for links."
2102 :group 'org-faces)
2104 (defface org-date
2105 '((((class color) (background light)) (:foreground "Purple" :underline t))
2106 (((class color) (background dark)) (:foreground "Cyan" :underline t))
2107 (t (:underline t)))
2108 "Face for links."
2109 :group 'org-faces)
2111 (defface org-tag
2112 '((t (:bold t)))
2113 "Face for tags."
2114 :group 'org-faces)
2116 (defface org-todo ;; font-lock-warning-face
2117 (org-compatible-face
2118 '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
2119 (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
2120 (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
2121 (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
2122 (t (:inverse-video t :bold t))))
2123 "Face for TODO keywords."
2124 :group 'org-faces)
2126 (defface org-done ;; font-lock-type-face
2127 (org-compatible-face
2128 '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
2129 (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
2130 (((class color) (min-colors 8)) (:foreground "green"))
2131 (t (:bold t))))
2132 "Face used for DONE."
2133 :group 'org-faces)
2135 (defface org-table ;; font-lock-function-name-face
2136 (org-compatible-face
2137 '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
2138 (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
2139 (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
2140 (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
2141 (((class color) (min-colors 8) (background light)) (:foreground "blue"))
2142 (((class color) (min-colors 8) (background dark)))))
2143 "Face used for tables."
2144 :group 'org-faces)
2146 (defface org-formula
2147 (org-compatible-face
2148 '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
2149 (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
2150 (((class color) (min-colors 8) (background light)) (:foreground "red"))
2151 (((class color) (min-colors 8) (background dark)) (:foreground "red"))
2152 (t (:bold t :italic t))))
2153 "Face for formulas."
2154 :group 'org-faces)
2156 (defface org-scheduled-today
2157 (org-compatible-face
2158 '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
2159 (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
2160 (((class color) (min-colors 8)) (:foreground "green"))
2161 (t (:bold t :italic t))))
2162 "Face for items scheduled for a certain day."
2163 :group 'org-faces)
2165 (defface org-scheduled-previously
2166 (org-compatible-face
2167 '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
2168 (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
2169 (((class color) (min-colors 8) (background light)) (:foreground "red"))
2170 (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
2171 (t (:bold t))))
2172 "Face for items scheduled previously, and not yet done."
2173 :group 'org-faces)
2175 (defface org-time-grid ;; font-lock-variable-name-face
2176 (org-compatible-face
2177 '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
2178 (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
2179 (((class color) (min-colors 8)) (:foreground "yellow" :weight light)))) ; FIXME: turn off???
2180 "Face used for time grids."
2181 :group 'org-faces)
2183 (defconst org-level-faces
2184 '(org-level-1 org-level-2 org-level-3 org-level-4
2185 org-level-5 org-level-6 org-level-7 org-level-8
2187 (defconst org-n-levels (length org-level-faces))
2189 (defconst org-bold-re
2190 (if (featurep 'xemacs)
2191 "\\([ ]\\|^\\)\\(\\*\\(\\w[a-zA-Z0-9-_ ]*?\\w\\)\\*\\)\\([ ,.]\\|$\\)"
2192 "\\([ ]\\|^\\)\\(\\*\\(\\w[[:word:] -_]*?\\w\\)\\*\\)\\([ ,.]\\|$\\)")
2193 "Regular expression for bold emphasis.")
2194 (defconst org-italic-re
2195 (if (featurep 'xemacs)
2196 "\\([ ]\\|^\\)\\(/\\(\\w[a-zA-Z0-9-_ ]*?\\w\\)/\\)\\([ ,.]\\|$\\)"
2197 "\\([ ]\\|^\\)\\(/\\(\\w[[:word:] -_]*?\\w\\)/\\)\\([ ,.]\\|$\\)")
2198 "Regular expression for italic emphasis.")
2199 (defconst org-underline-re
2200 (if (featurep 'xemacs)
2201 "\\([ ]\\|^\\)\\(_\\(\\w[a-zA-Z0-9-_ ]*?\\w\\)_\\)\\([ ,.]\\|$\\)"
2202 "\\([ ]\\|^\\)\\(_\\(\\w[[:word:] -_]*?\\w\\)_\\)\\([ ,.]\\|$\\)")
2203 "Regular expression for underline emphasis.")
2205 ;; Variables for pre-computed regular expressions, all buffer local
2206 (defvar org-done-string nil
2207 "The last string in `org-todo-keywords', indicating an item is DONE.")
2208 (make-variable-buffer-local 'org-done-string)
2209 (defvar org-todo-regexp nil
2210 "Matches any of the TODO state keywords.")
2211 (make-variable-buffer-local 'org-todo-regexp)
2212 (defvar org-not-done-regexp nil
2213 "Matches any of the TODO state keywords except the last one.")
2214 (make-variable-buffer-local 'org-not-done-regexp)
2215 (defvar org-todo-line-regexp nil
2216 "Matches a headline and puts TODO state into group 2 if present.")
2217 (make-variable-buffer-local 'org-todo-line-regexp)
2218 (defvar org-nl-done-regexp nil
2219 "Matches newline followed by a headline with the DONE keyword.")
2220 (make-variable-buffer-local 'org-nl-done-regexp)
2221 (defvar org-looking-at-done-regexp nil
2222 "Matches the DONE keyword a point.")
2223 (make-variable-buffer-local 'org-looking-at-done-regexp)
2224 (defvar org-todo-kwd-priority-p nil
2225 "Do TODO items have priorities?")
2226 (make-variable-buffer-local 'org-todo-kwd-priority-p)
2227 (defvar org-todo-kwd-max-priority nil
2228 "Maximum priority of TODO items.")
2229 (make-variable-buffer-local 'org-todo-kwd-max-priority)
2230 (defvar org-ds-keyword-length 12
2231 "Maximum length of the Deadline and SCHEDULED keywords.")
2232 (make-variable-buffer-local 'org-ds-keyword-length)
2233 (defvar org-deadline-regexp nil
2234 "Matches the DEADLINE keyword.")
2235 (make-variable-buffer-local 'org-deadline-regexp)
2236 (defvar org-deadline-time-regexp nil
2237 "Matches the DEADLINE keyword together with a time stamp.")
2238 (make-variable-buffer-local 'org-deadline-time-regexp)
2239 (defvar org-deadline-line-regexp nil
2240 "Matches the DEADLINE keyword and the rest of the line.")
2241 (make-variable-buffer-local 'org-deadline-line-regexp)
2242 (defvar org-scheduled-regexp nil
2243 "Matches the SCHEDULED keyword.")
2244 (make-variable-buffer-local 'org-scheduled-regexp)
2245 (defvar org-scheduled-time-regexp nil
2246 "Matches the SCHEDULED keyword together with a time stamp.")
2247 (make-variable-buffer-local 'org-scheduled-time-regexp)
2248 (defvar org-closed-time-regexp nil
2249 "Matches the CLOSED keyword together with a time stamp.")
2250 (make-variable-buffer-local 'org-closed-time-regexp)
2252 (defvar org-keyword-time-regexp nil
2253 "Matches any of the 3 keywords, together with the time stamp.")
2254 (make-variable-buffer-local 'org-keyword-time-regexp)
2256 (defun org-set-regexps-and-options ()
2257 "Precompute regular expressions for current buffer."
2258 (when (eq major-mode 'org-mode)
2259 (let ((re (org-make-options-regexp
2260 '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
2261 "STARTUP" "ARCHIVE")))
2262 (splitre "[ \t]+")
2263 kwds int key value cat arch)
2264 (save-excursion
2265 (save-restriction
2266 (widen)
2267 (goto-char (point-min))
2268 (while (re-search-forward re nil t)
2269 (setq key (match-string 1) value (match-string 2))
2270 (cond
2271 ((equal key "CATEGORY")
2272 (if (string-match "[ \t]+$" value)
2273 (setq value (replace-match "" t t value)))
2274 (setq cat (intern value)))
2275 ((equal key "SEQ_TODO")
2276 (setq int 'sequence
2277 kwds (append kwds (org-split-string value splitre))))
2278 ((equal key "PRI_TODO")
2279 (setq int 'priority
2280 kwds (append kwds (org-split-string value splitre))))
2281 ((equal key "TYP_TODO")
2282 (setq int 'type
2283 kwds (append kwds (org-split-string value splitre))))
2284 ((equal key "STARTUP")
2285 (let ((opts (org-split-string value splitre))
2286 (set '(("fold" org-startup-folded t)
2287 ("overview" org-startup-folded t)
2288 ("nofold" org-startup-folded nil)
2289 ("showall" org-startup-folded nil)
2290 ("content" org-startup-folded content)
2291 ("hidestars" org-hide-leading-stars t)
2292 ("showstars" org-hide-leading-stars nil)
2293 ("odd" org-odd-levels-only t)
2294 ("oddeven" org-odd-levels-only nil)
2295 ("align" org-startup-align-all-tables t)
2296 ("noalign" org-startup-align-all-tables nil)
2297 ("dlcheck" org-startup-with-deadline-check t)
2298 ("nodlcheck" org-startup-with-deadline-check nil)))
2299 l var val)
2300 (while (setq l (assoc (pop opts) set))
2301 (setq var (nth 1 l) val (nth 2 l))
2302 (set (make-local-variable var) val))))
2303 ((equal key "ARCHIVE")
2304 (string-match " *$" value)
2305 (setq arch (replace-match "" t t value))
2306 (remove-text-properties 0 (length arch)
2307 '(face t fontified t) arch)))
2309 (and cat (set (make-local-variable 'org-category) cat))
2310 (and kwds (set (make-local-variable 'org-todo-keywords) kwds))
2311 (and arch (set (make-local-variable 'org-archive-location) arch))
2312 (and int (set (make-local-variable 'org-todo-interpretation) int)))
2313 ;; Compute the regular expressions and other local variables
2314 (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
2315 org-todo-kwd-max-priority (1- (length org-todo-keywords))
2316 org-ds-keyword-length (+ 2 (max (length org-deadline-string)
2317 (length org-scheduled-string)))
2318 org-done-string
2319 (nth (1- (length org-todo-keywords)) org-todo-keywords)
2320 org-todo-regexp
2321 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
2322 "\\|") "\\)\\>")
2323 org-not-done-regexp
2324 (concat "\\<\\("
2325 (mapconcat 'regexp-quote
2326 (nreverse (cdr (reverse org-todo-keywords)))
2327 "\\|")
2328 "\\)\\>")
2329 org-todo-line-regexp
2330 (concat "^\\(\\*+\\)[ \t]*\\("
2331 (mapconcat 'regexp-quote org-todo-keywords "\\|")
2332 "\\)? *\\(.*\\)")
2333 org-nl-done-regexp
2334 (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
2335 org-looking-at-done-regexp (concat "^" org-done-string "\\>")
2336 org-deadline-regexp (concat "\\<" org-deadline-string)
2337 org-deadline-time-regexp
2338 (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
2339 org-deadline-line-regexp
2340 (concat "\\<\\(" org-deadline-string "\\).*")
2341 org-scheduled-regexp
2342 (concat "\\<" org-scheduled-string)
2343 org-scheduled-time-regexp
2344 (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
2345 org-closed-time-regexp
2346 (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
2347 org-keyword-time-regexp
2348 (concat "\\<\\(" org-scheduled-string
2349 "\\|" org-deadline-string
2350 "\\|" org-closed-string "\\)"
2351 " *[[<]\\([^]>]+\\)[]>]")) ;; FIXME: is this correct?
2353 (org-set-font-lock-defaults)))
2355 ;; Tell the compiler about dynamically scoped variables,
2356 ;; and variables from other packages
2357 (defvar zmacs-regions) ; XEmacs regions
2358 (defvar original-date) ; dynamically scoped in calendar
2359 (defvar org-old-auto-fill-inhibit-regexp) ; local variable used by `orgtbl-mode'
2360 (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
2361 (defvar org-html-entities) ; defined later in this file
2362 (defvar org-goto-start-pos) ; dynamically scoped parameter
2363 (defvar org-time-was-given) ; dynamically scoped parameter
2364 (defvar org-ts-what) ; dynamically scoped parameter
2365 (defvar mark-active) ; Emacs only, not available in XEmacs.
2366 (defvar timecnt) ; dynamically scoped parameter
2367 (defvar levels-open) ; dynamically scoped parameter
2368 (defvar entry) ; dynamically scoped parameter
2369 (defvar date) ; dynamically scoped parameter
2370 (defvar description) ; dynamically scoped parameter
2371 (defvar ans1) ; dynamically scoped parameter
2372 (defvar ans2) ; dynamically scoped parameter
2373 (defvar starting-day) ; local variable
2374 (defvar include-all-loc) ; local variable
2375 (defvar vm-message-pointer) ; from vm
2376 (defvar vm-folder-directory) ; from vm
2377 (defvar wl-summary-buffer-elmo-folder) ; from wanderlust
2378 (defvar wl-summary-buffer-folder-name) ; from wanderlust
2379 (defvar gnus-group-name) ; from gnus
2380 (defvar gnus-article-current) ; from gnus
2381 (defvar w3m-current-url) ; from w3m
2382 (defvar mh-progs) ; from MH-E
2383 (defvar mh-current-folder) ; from MH-E
2384 (defvar mh-show-folder-buffer) ; from MH-E
2385 (defvar mh-index-folder) ; from MH-E
2386 (defvar mh-searcher) ; from MH-E
2387 (defvar org-selected-point) ; dynamically scoped parameter
2388 (defvar calendar-mode-map) ; from calendar.el
2389 (defvar last-arg) ; local variable
2390 (defvar remember-save-after-remembering) ; from remember.el
2391 (defvar remember-data-file) ; from remember.el
2392 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
2393 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
2394 (defvar orgtbl-mode) ; defined later in this file
2395 ;;; Define the mode
2397 (defvar org-mode-map
2398 (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
2399 (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.")
2400 (copy-keymap outline-mode-map))
2401 "Keymap for Org-mode.")
2403 (defvar org-struct-menu) ; defined later in this file
2404 (defvar org-org-menu) ; defined later in this file
2405 (defvar org-tbl-menu) ; defined later in this file
2407 ;; We use a before-change function to check if a table might need
2408 ;; an update.
2409 (defvar org-table-may-need-update t
2410 "Indicates that a table might need an update.
2411 This variable is set by `org-before-change-function'.
2412 `org-table-align' sets it back to nil.")
2413 (defvar org-mode-hook nil)
2414 (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
2415 (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
2418 ;;;###autoload
2419 (define-derived-mode org-mode outline-mode "Org"
2420 "Outline-based notes management and organizer, alias
2421 \"Carsten's outline-mode for keeping track of everything.\"
2423 Org-mode develops organizational tasks around a NOTES file which
2424 contains information about projects as plain text. Org-mode is
2425 implemented on top of outline-mode, which is ideal to keep the content
2426 of large files well structured. It supports ToDo items, deadlines and
2427 time stamps, which magically appear in the diary listing of the Emacs
2428 calendar. Tables are easily created with a built-in table editor.
2429 Plain text URL-like links connect to websites, emails (VM), Usenet
2430 messages (Gnus), BBDB entries, and any files related to the project.
2431 For printing and sharing of notes, an Org-mode file (or a part of it)
2432 can be exported as a structured ASCII or HTML file.
2434 The following commands are available:
2436 \\{org-mode-map}"
2437 (easy-menu-add org-org-menu)
2438 (easy-menu-add org-tbl-menu)
2439 (org-install-agenda-files-menu)
2440 (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
2441 (org-add-to-invisibility-spec '(org-cwidth))
2442 (setq outline-regexp "\\*+")
2443 ;;(setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
2444 (setq outline-level 'org-outline-level)
2445 (when (and org-ellipsis (stringp org-ellipsis))
2446 (unless org-display-table
2447 (setq org-display-table (make-display-table)))
2448 (set-display-table-slot org-display-table
2449 4 (string-to-vector org-ellipsis))
2450 (setq buffer-display-table org-display-table))
2451 (org-set-regexps-and-options)
2452 (if org-startup-truncated (setq truncate-lines t))
2453 (set (make-local-variable 'font-lock-unfontify-region-function)
2454 'org-unfontify-region)
2455 ;; Activate before-change-function
2456 (set (make-local-variable 'org-table-may-need-update) t)
2457 (org-add-hook 'before-change-functions 'org-before-change-function nil
2458 'local)
2459 ;; Paragraphs and auto-filling
2460 (org-set-autofill-regexps)
2461 (org-update-radio-target-regexp)
2462 ;; Settings for Calc embedded mode
2463 (set (make-local-variable 'calc-embedded-open-formula) "|\\|\n")
2464 (set (make-local-variable 'calc-embedded-close-formula) "|\\|\n")
2465 (if (and org-insert-mode-line-in-empty-file
2466 (interactive-p)
2467 (= (point-min) (point-max)))
2468 (insert " -*- mode: org -*-\n\n"))
2470 ;; Get rid of Outline menus, they are not needed
2471 ;; Need to do this here because define-derived-mode sets up
2472 ;; the keymap so late.
2473 (if (featurep 'xemacs)
2474 (progn
2475 (delete-menu-item '("Headings"))
2476 (delete-menu-item '("Show"))
2477 (delete-menu-item '("Hide"))
2478 (set-menubar-dirty-flag))
2479 (define-key org-mode-map [menu-bar headings] 'undefined)
2480 (define-key org-mode-map [menu-bar hide] 'undefined)
2481 (define-key org-mode-map [menu-bar show] 'undefined))
2483 (unless org-inhibit-startup
2484 (if org-startup-align-all-tables
2485 (org-table-map-tables 'org-table-align))
2486 (if org-startup-with-deadline-check
2487 (call-interactively 'org-check-deadlines)
2488 (cond
2489 ((eq org-startup-folded t)
2490 (org-cycle '(4)))
2491 ((eq org-startup-folded 'content)
2492 (let ((this-command 'org-cycle) (last-command 'org-cycle))
2493 (org-cycle '(4)) (org-cycle '(4))))))))
2495 (defsubst org-current-line (&optional pos)
2496 (+ (if (bolp) 1 0) (count-lines (point-min) (or pos (point)))))
2498 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
2499 mouse-map t)
2500 "Properties to remove when a string without properties is wanted.")
2502 (defsubst org-match-string-no-properties (num &optional string)
2503 (if (featurep 'xemacs)
2504 (let ((s (match-string num string)))
2505 (remove-text-properties 0 (length s) org-rm-props s)
2507 (match-string-no-properties num string)))
2509 (defsubst org-no-properties (s)
2510 (remove-text-properties 0 (length s) org-rm-props s)
2513 (defun org-current-time ()
2514 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
2515 (if (> org-time-stamp-rounding-minutes 0)
2516 (let ((r org-time-stamp-rounding-minutes)
2517 (time (decode-time)))
2518 (apply 'encode-time
2519 (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
2520 (nthcdr 2 time))))
2521 (current-time)))
2523 (defun org-add-props (string plist &rest props)
2524 "Add text properties to entire string, from beginning to end.
2525 PLIST may be a list of properties, PROPS are individual properties and values
2526 that will be added to PLIST. Returns the string that was modified."
2527 (add-text-properties
2528 0 (length string) (if props (append plist props) plist) string)
2529 string)
2530 (put 'org-add-props 'lisp-indent-function 2)
2533 ;;; Font-Lock stuff
2535 (defvar org-mouse-map (make-sparse-keymap))
2536 (define-key org-mouse-map
2537 (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
2538 (define-key org-mouse-map
2539 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
2540 (when org-mouse-1-follows-link
2541 (define-key org-mouse-map [follow-link] 'mouse-face))
2542 (when org-tab-follows-link
2543 (define-key org-mouse-map [(tab)] 'org-open-at-point)
2544 (define-key org-mouse-map "\C-i" 'org-open-at-point))
2545 (when org-return-follows-link
2546 (define-key org-mouse-map [(return)] 'org-open-at-point)
2547 (define-key org-mouse-map "\C-m" 'org-open-at-point))
2549 (require 'font-lock)
2551 (defconst org-non-link-chars "]\t\n\r<>")
2552 (defconst org-link-types '("https?" "ftp" "mailto" "file" "news" "bbdb" "vm"
2553 "wl" "mhe" "rmail" "gnus" "shell"))
2554 (defconst org-link-re-with-space
2555 (concat
2556 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
2557 "\\([^" org-non-link-chars " ]"
2558 "[^" org-non-link-chars "]*"
2559 "[^" org-non-link-chars " ]\\)>?")
2560 "Matches a link with spaces, optional angular brackets around it.")
2562 (defconst org-link-re-with-space2
2563 (concat
2564 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
2565 "\\([^" org-non-link-chars " ]"
2566 "[^]\t\n\r]*"
2567 "[^" org-non-link-chars " ]\\)>?")
2568 "Matches a link with spaces, optional angular brackets around it.")
2570 (defconst org-angle-link-re
2571 (concat
2572 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
2573 "\\([^" org-non-link-chars " ]"
2574 "[^" org-non-link-chars "]*"
2575 "\\)>")
2576 "Matches link with angular brackets, spaces are allowed.")
2577 (defconst org-plain-link-re
2578 (concat
2579 "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
2580 "\\([^]\t\n\r<>,;() ]+\\)")
2581 "Matches plain link, without spaces.")
2583 (defconst org-bracket-link-regexp
2584 "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
2585 "Matches a link in double brackets.")
2587 (defconst org-bracket-link-analytic-regexp
2588 (concat
2589 "\\[\\["
2590 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
2591 "\\([^]]+\\)"
2592 "\\]"
2593 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
2594 "\\]"))
2595 ; 1: http:
2596 ; 2: http
2597 ; 3: path
2598 ; 4: [desc]
2599 ; 5: desc
2602 (defconst org-ts-lengths
2603 (cons (length (format-time-string (car org-time-stamp-formats)))
2604 (length (format-time-string (cdr org-time-stamp-formats))))
2605 "This holds the lengths of the two different time formats.")
2606 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
2607 "Regular expression for fast time stamp matching.")
2608 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
2609 "Regular expression for fast time stamp matching.")
2610 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
2611 "Regular expression matching time strings for analysis.")
2612 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
2613 "Regular expression matching time stamps, with groups.")
2614 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
2615 "Regular expression matching a time stamp range.")
2616 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
2617 org-ts-regexp "\\)?")
2618 "Regular expression matching a time stamp or time stamp range.")
2620 (defun org-activate-plain-links (limit)
2621 "Run through the buffer and add overlays to links."
2622 (if (re-search-forward org-plain-link-re limit t)
2623 (progn
2624 (add-text-properties (match-beginning 0) (match-end 0)
2625 (list 'mouse-face 'highlight
2626 'keymap org-mouse-map
2628 t)))
2630 (defun org-activate-angle-links (limit)
2631 "Run through the buffer and add overlays to links."
2632 (if (re-search-forward org-angle-link-re limit t)
2633 (progn
2634 (add-text-properties (match-beginning 0) (match-end 0)
2635 (list 'mouse-face 'highlight
2636 'keymap org-mouse-map
2638 t)))
2640 (defun org-activate-bracket-links (limit)
2641 "Run through the buffer and add overlays to bracketed links."
2642 (if (re-search-forward org-bracket-link-regexp limit t)
2643 (let* ((help (concat "LINK: "
2644 (org-match-string-no-properties 1)))
2645 ;; FIXME: above we should remove the escapes.
2646 (ip (list 'invisible 'org-link 'intangible t 'rear-nonsticky t
2647 'keymap org-mouse-map 'mouse-face 'highlight
2648 'help-echo help))
2649 (vp (list 'rear-nonsticky t
2650 'keymap org-mouse-map 'mouse-face 'highlight
2651 'help-echo help)))
2652 ;; We need to remove the invisible property here. Table narrowing
2653 ;; may have made some of this invisible.
2654 (remove-text-properties (match-beginning 0) (match-end 0)
2655 '(invisible nil))
2656 (if (match-end 3)
2657 (progn
2658 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
2659 (add-text-properties (match-beginning 3) (match-end 3) vp)
2660 (add-text-properties (match-end 3) (match-end 0) ip))
2661 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
2662 (add-text-properties (match-beginning 1) (match-end 1) vp)
2663 (add-text-properties (match-end 1) (match-end 0) ip))
2664 t)))
2666 (defun org-activate-dates (limit)
2667 "Run through the buffer and add overlays to dates."
2668 (if (re-search-forward org-tsr-regexp limit t)
2669 (progn
2670 (add-text-properties (match-beginning 0) (match-end 0)
2671 (list 'mouse-face 'highlight
2672 'keymap org-mouse-map))
2673 t)))
2675 (defvar org-target-link-regexp nil
2676 "Regular expression matching radio targets in plain text.")
2677 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
2678 "Regular expression matching a link target.")
2679 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
2680 "Regular expression matching a link target.")
2682 (defun org-activate-target-links (limit)
2683 "Run through the buffer and add overlays to target matches."
2684 (when org-target-link-regexp
2685 (let ((case-fold-search t))
2686 (if (re-search-forward org-target-link-regexp limit t)
2687 (progn
2688 (add-text-properties (match-beginning 0) (match-end 0)
2689 (list 'mouse-face 'highlight
2690 'keymap org-mouse-map
2691 'help-echo "Radio target link"
2692 'org-linked-text t))
2693 t)))))
2695 (defun org-update-radio-target-regexp ()
2696 "Find all radio targets in this file and update the regular expression."
2697 (interactive)
2698 (when (memq 'radio org-activate-links)
2699 (setq org-target-link-regexp
2700 (org-make-target-link-regexp (org-all-targets 'radio)))
2701 (org-restart-font-lock)))
2703 (defun org-hide-wide-columns (limit)
2704 (let (s e)
2705 (setq s (text-property-any (point) (or limit (point-max))
2706 'org-cwidth t))
2707 (when s
2708 (setq e (next-single-property-change s 'org-cwidth))
2709 (add-text-properties s e '(invisible org-cwidth intangible t))
2710 (goto-char e)
2711 t)))
2713 (defun org-restart-font-lock ()
2714 "Restart font-lock-mode, to force refontification."
2715 (when (and (boundp 'font-lock-mode) font-lock-mode)
2716 (font-lock-mode -1)
2717 (font-lock-mode 1)))
2719 (defun org-all-targets (&optional radio)
2720 "Return a list of all targets in this file.
2721 With optional argument RADIO, only find radio targets."
2722 (let ((re (if radio org-radio-target-regexp org-target-regexp))
2723 rtn)
2724 (save-excursion
2725 (goto-char (point-min))
2726 (while (re-search-forward re nil t)
2727 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
2728 rtn)))
2730 (defun org-make-target-link-regexp (targets)
2731 "Make regular expression matching all strings in TARGETS.
2732 The regular expression finds the targets also if there is a line break
2733 between words."
2734 (and targets
2735 (concat
2736 "\\<\\("
2737 (mapconcat
2738 (lambda (x)
2739 (while (string-match " +" x)
2740 (setq x (replace-match "\\s-+" t t x)))
2742 targets
2743 "\\|")
2744 "\\)\\>")))
2746 (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
2747 "Matches CamelCase words, possibly with a star before it.")
2749 (defun org-activate-camels (limit)
2750 "Run through the buffer and add overlays to dates."
2751 (if (re-search-forward org-camel-regexp limit t)
2752 (progn
2753 (add-text-properties (match-beginning 0) (match-end 0)
2754 (list 'mouse-face 'highlight
2755 'keymap org-mouse-map))
2756 t)))
2758 (defun org-activate-tags (limit)
2759 (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
2760 (progn
2761 (add-text-properties (match-beginning 1) (match-end 1)
2762 (list 'mouse-face 'highlight
2763 'keymap org-mouse-map))
2764 t)))
2766 (defun org-font-lock-level ()
2767 (save-excursion
2768 (org-back-to-heading t)
2769 (- (match-end 0) (match-beginning 0))))
2771 (defun org-outline-level ()
2772 (save-excursion
2773 (looking-at outline-regexp)
2774 (if (match-beginning 1)
2775 (+ (org-get-string-indentation (match-string 1)) 1000)
2776 (- (match-end 0) (match-beginning 0)))))
2778 (defvar org-font-lock-keywords nil)
2780 (defun org-set-font-lock-defaults ()
2781 (let* ((em org-fontify-emphasized-text)
2782 (lk org-activate-links)
2783 (org-font-lock-extra-keywords
2784 (list
2785 '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
2786 (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
2787 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
2788 (1 'org-table))
2789 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
2790 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
2791 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
2792 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
2793 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
2794 (if (memq 'camel lk) '(org-activate-camels (0 'org-link t)))
2795 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
2796 (if org-table-limit-column-width
2797 '(org-hide-wide-columns (0 nil append)))
2798 (list (concat "^\\*+[ \t]*" org-not-done-regexp)
2799 '(1 'org-todo t))
2800 (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
2801 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
2802 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
2803 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
2804 ; (if em '("\\(\\W\\|^\\)\\(\\*\\w+\\*\\)\\(\\W\\|$\\)" 2 'bold prepend))
2805 ; (if em '("\\(\\W\\|^\\)\\(/\\w+/\\)\\(\\W\\|$\\)" 2 'italic prepend))
2806 ; (if em '("\\(\\W\\|^\\)\\(_\\w+_\\)\\(\\W\\|$\\)" 2 'underline prepend))
2807 (if em (list org-bold-re 2 ''bold 'prepend))
2808 (if em (list org-italic-re 2 ''italic 'prepend))
2809 (if em (list org-underline-re 2 ''underline 'prepend))
2810 (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
2811 "\\|" org-quote-string "\\)\\>")
2812 '(1 'org-special-keyword t))
2813 '("^#.*" (0 'font-lock-comment-face t))
2814 (if org-fontify-done-headline
2815 (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
2816 '(1 'org-done t) '(2 'org-headline-done t))
2817 (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
2818 '(1 'org-done t)))
2819 '("^[ \t]*\\(:.*\\)" (1 'org-table t))
2820 '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
2821 '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
2822 (if org-format-transports-properties-p
2823 '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
2825 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
2826 ;; Now set the full font-lock-keywords
2827 (set (make-local-variable 'org-font-lock-keywords)
2828 org-font-lock-extra-keywords)
2829 (set (make-local-variable 'font-lock-defaults)
2830 '(org-font-lock-keywords t nil nil backward-paragraph))
2831 (kill-local-variable 'font-lock-keywords) nil))
2833 (defvar org-m nil)
2834 (defvar org-l nil)
2835 (defvar org-f nil)
2836 (defun org-get-level-face (n)
2837 "Get the right face for match N in font-lock matching of healdines."
2838 (setq org-l (- (match-end 2) (match-beginning 1)))
2839 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
2840 ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
2841 (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
2842 (cond
2843 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
2844 ((eq n 2) org-f)
2845 (t (if org-level-color-stars-only nil org-f))))
2847 (defun org-unfontify-region (beg end &optional maybe_loudly)
2848 "Remove fontification and activation overlays from links."
2849 (font-lock-default-unfontify-region beg end)
2850 (let* ((buffer-undo-list t)
2851 (inhibit-read-only t) (inhibit-point-motion-hooks t)
2852 (inhibit-modification-hooks t)
2853 deactivate-mark buffer-file-name buffer-file-truename)
2854 (remove-text-properties beg end
2855 '(mouse-face nil keymap nil org-linked-text nil
2856 invisible nil intangible nil))))
2857 ;;; Visibility cycling
2859 (defvar org-cycle-global-status nil)
2860 (defvar org-cycle-subtree-status nil)
2861 (defun org-cycle (&optional arg)
2862 "Visibility cycling for Org-mode.
2864 - When this function is called with a prefix argument, rotate the entire
2865 buffer through 3 states (global cycling)
2866 1. OVERVIEW: Show only top-level headlines.
2867 2. CONTENTS: Show all headlines of all levels, but no body text.
2868 3. SHOW ALL: Show everything.
2870 - When point is at the beginning of a headline, rotate the subtree started
2871 by this line through 3 different states (local cycling)
2872 1. FOLDED: Only the main headline is shown.
2873 2. CHILDREN: The main headline and the direct children are shown.
2874 From this state, you can move to one of the children
2875 and zoom in further.
2876 3. SUBTREE: Show the entire subtree, including body text.
2878 - When there is a numeric prefix, go up to a heading with level ARG, do
2879 a `show-subtree' and return to the previous cursor position. If ARG
2880 is negative, go up that many levels.
2882 - When point is not at the beginning of a headline, execute
2883 `indent-relative', like TAB normally does. See the option
2884 `org-cycle-emulate-tab' for details.
2886 - Special case: if point is the the beginning of the buffer and there is
2887 no headline in line 1, this function will act as if called with prefix arg."
2888 (interactive "P")
2890 (if (or (and (bobp) (not (looking-at outline-regexp)))
2891 (equal arg '(4)))
2892 ;; special case: use global cycling
2893 (setq arg t))
2895 (let ((outline-regexp
2896 (if org-cycle-include-plain-lists
2897 "\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) "
2898 outline-regexp)))
2900 (cond
2902 ((org-at-table-p 'any)
2903 ;; Enter the table or move to the next field in the table
2904 (or (org-table-recognize-table.el)
2905 (progn
2906 (if arg (org-table-edit-field t)
2907 (org-table-justify-field-maybe)
2908 (org-table-next-field)))))
2910 ((eq arg t) ;; Global cycling
2912 (cond
2913 ((and (eq last-command this-command)
2914 (eq org-cycle-global-status 'overview))
2915 ;; We just created the overview - now do table of contents
2916 ;; This can be slow in very large buffers, so indicate action
2917 (message "CONTENTS...")
2918 (save-excursion
2919 ;; Visit all headings and show their offspring
2920 (goto-char (point-max))
2921 (catch 'exit
2922 (while (and (progn (condition-case nil
2923 (outline-previous-visible-heading 1)
2924 (error (goto-char (point-min))))
2926 (looking-at outline-regexp))
2927 (show-branches)
2928 (if (bobp) (throw 'exit nil))))
2929 (message "CONTENTS...done"))
2930 (setq org-cycle-global-status 'contents)
2931 (run-hook-with-args 'org-cycle-hook 'contents))
2933 ((and (eq last-command this-command)
2934 (eq org-cycle-global-status 'contents))
2935 ;; We just showed the table of contents - now show everything
2936 (show-all)
2937 (message "SHOW ALL")
2938 (setq org-cycle-global-status 'all)
2939 (run-hook-with-args 'org-cycle-hook 'all))
2942 ;; Default action: go to overview
2943 (hide-sublevels 1)
2944 (message "OVERVIEW")
2945 (setq org-cycle-global-status 'overview)
2946 (run-hook-with-args 'org-cycle-hook 'overview))))
2948 ((integerp arg)
2949 ;; Show-subtree, ARG levels up from here.
2950 (save-excursion
2951 (org-back-to-heading)
2952 (outline-up-heading (if (< arg 0) (- arg)
2953 (- (funcall outline-level) arg)))
2954 (org-show-subtree)))
2956 ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
2957 ;; At a heading: rotate between three different views
2958 (org-back-to-heading)
2959 (let ((goal-column 0) eoh eol eos)
2960 ;; First, some boundaries
2961 (save-excursion
2962 (org-back-to-heading)
2963 (save-excursion
2964 (beginning-of-line 2)
2965 (while (and (not (eobp)) ;; this is like `next-line'
2966 (get-char-property (1- (point)) 'invisible))
2967 (beginning-of-line 2)) (setq eol (point)))
2968 (outline-end-of-heading) (setq eoh (point))
2969 (org-end-of-subtree t) (setq eos (point))
2970 (outline-next-heading))
2971 ;; Find out what to do next and set `this-command'
2972 (cond
2973 ((= eos eoh)
2974 ;; Nothing is hidden behind this heading
2975 (message "EMPTY ENTRY")
2976 (setq org-cycle-subtree-status nil))
2977 ((>= eol eos)
2978 ;; Entire subtree is hidden in one line: open it
2979 (org-show-entry)
2980 (show-children)
2981 (message "CHILDREN")
2982 (setq org-cycle-subtree-status 'children)
2983 (run-hook-with-args 'org-cycle-hook 'children))
2984 ((and (eq last-command this-command)
2985 (eq org-cycle-subtree-status 'children))
2986 ;; We just showed the children, now show everything.
2987 (org-show-subtree)
2988 (message "SUBTREE")
2989 (setq org-cycle-subtree-status 'subtree)
2990 (run-hook-with-args 'org-cycle-hook 'subtree))
2992 ;; Default action: hide the subtree.
2993 (hide-subtree)
2994 (message "FOLDED")
2995 (setq org-cycle-subtree-status 'folded)
2996 (run-hook-with-args 'org-cycle-hook 'folded)))))
2998 ;; TAB emulation
2999 (buffer-read-only (org-back-to-heading))
3000 ((if (and (eq org-cycle-emulate-tab 'white)
3001 (save-excursion (beginning-of-line 1) (looking-at "[ \t]+$")))
3003 (eq org-cycle-emulate-tab t))
3004 (if (and (looking-at "[ \n\r\t]")
3005 (string-match "^[ \t]*$" (buffer-substring
3006 (point-at-bol) (point))))
3007 (progn
3008 (beginning-of-line 1)
3009 (and (looking-at "[ \t]+") (replace-match ""))))
3010 (indent-relative))
3012 (t (save-excursion
3013 (org-back-to-heading)
3014 (org-cycle))))))
3016 (defun org-optimize-window-after-visibility-change (state)
3017 "Adjust the window after a change in outline visibility.
3018 This function is the default value of the hook `org-cycle-hook'."
3019 (when (get-buffer-window (current-buffer))
3020 (cond
3021 ((eq state 'overview) (org-first-headline-recenter 1))
3022 ((eq state 'content) nil)
3023 ((eq state 'all) nil)
3024 ((eq state 'folded) nil)
3025 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
3026 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
3028 (defun org-subtree-end-visible-p ()
3029 "Is the end of the current subtree visible?"
3030 (pos-visible-in-window-p
3031 (save-excursion (org-end-of-subtree t) (point))))
3033 (defun org-first-headline-recenter (&optional N)
3034 "Move cursor to the first headline and recenter the headline.
3035 Optional argument N means, put the headline into the Nth line of the window."
3036 (goto-char (point-min))
3037 (when (re-search-forward (concat "^" outline-regexp) nil t)
3038 (beginning-of-line)
3039 (recenter (prefix-numeric-value N))))
3041 (defvar org-goto-window-configuration nil)
3042 (defvar org-goto-marker nil)
3043 (defvar org-goto-map (make-sparse-keymap))
3044 (let ((cmds '(isearch-forward isearch-backward)) cmd)
3045 (while (setq cmd (pop cmds))
3046 (substitute-key-definition cmd cmd org-goto-map global-map)))
3047 (define-key org-goto-map "\C-m" 'org-goto-ret)
3048 (define-key org-goto-map [(left)] 'org-goto-left)
3049 (define-key org-goto-map [(right)] 'org-goto-right)
3050 (define-key org-goto-map [(?q)] 'org-goto-quit)
3051 (define-key org-goto-map [(control ?g)] 'org-goto-quit)
3052 (define-key org-goto-map "\C-i" 'org-cycle)
3053 (define-key org-goto-map [(tab)] 'org-cycle)
3054 (define-key org-goto-map [(down)] 'outline-next-visible-heading)
3055 (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
3056 (define-key org-goto-map "n" 'outline-next-visible-heading)
3057 (define-key org-goto-map "p" 'outline-previous-visible-heading)
3058 (define-key org-goto-map "f" 'outline-forward-same-level)
3059 (define-key org-goto-map "b" 'outline-backward-same-level)
3060 (define-key org-goto-map "u" 'outline-up-heading)
3061 (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
3062 (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
3063 (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
3064 (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
3065 (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
3066 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
3067 (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
3069 (defconst org-goto-help
3070 "Select a location to jump to, press RET
3071 \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
3073 (defun org-goto ()
3074 "Go to a different location of the document, keeping current visibility.
3076 When you want to go to a different location in a document, the fastest way
3077 is often to fold the entire buffer and then dive into the tree. This
3078 method has the disadvantage, that the previous location will be folded,
3079 which may not be what you want.
3081 This command works around this by showing a copy of the current buffer in
3082 overview mode. You can dive into the tree in that copy, to find the
3083 location you want to reach. When pressing RET, the command returns to the
3084 original buffer in which the visibility is still unchanged. It then jumps
3085 to the new location, making it and the headline hierarchy above it visible."
3086 (interactive)
3087 (let* ((org-goto-start-pos (point))
3088 (selected-point
3089 (org-get-location (current-buffer) org-goto-help)))
3090 (if selected-point
3091 (progn
3092 (org-mark-ring-push org-goto-start-pos)
3093 (goto-char selected-point)
3094 (if (or (org-invisible-p) (org-invisible-p2))
3095 (org-show-hierarchy-above)))
3096 (error "Quit"))))
3098 (defun org-get-location (buf help)
3099 "Let the user select a location in the Org-mode buffer BUF.
3100 This function uses a recursive edit. It returns the selected position
3101 or nil."
3102 (let (org-selected-point)
3103 (save-excursion
3104 (save-window-excursion
3105 (delete-other-windows)
3106 (switch-to-buffer (get-buffer-create "*org-goto*"))
3107 (with-output-to-temp-buffer "*Help*"
3108 (princ help))
3109 (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
3110 (setq buffer-read-only nil)
3111 (erase-buffer)
3112 (insert-buffer-substring buf)
3113 (let ((org-startup-truncated t)
3114 (org-startup-folded t)
3115 (org-startup-align-all-tables nil)
3116 (org-startup-with-deadline-check nil))
3117 (org-mode))
3118 (setq buffer-read-only t)
3119 (if (boundp 'org-goto-start-pos)
3120 (goto-char org-goto-start-pos)
3121 (goto-char (point-min)))
3122 (org-beginning-of-line)
3123 (message "Select location and press RET")
3124 ;; now we make sure that during selection, ony very few keys work
3125 ;; and that it is impossible to switch to another window.
3126 (let ((gm (current-global-map))
3127 (overriding-local-map org-goto-map))
3128 (unwind-protect
3129 (progn
3130 (use-global-map org-goto-map)
3131 (recursive-edit))
3132 (use-global-map gm)))))
3133 (kill-buffer "*org-goto*")
3134 org-selected-point))
3136 ;; FIXME: It may not be a good idea to temper with the prefix argument...
3137 (defun org-goto-ret (&optional arg)
3138 "Finish `org-goto' by going to the new location."
3139 (interactive "P")
3140 (setq org-selected-point (point)
3141 current-prefix-arg arg)
3142 (throw 'exit nil))
3144 (defun org-goto-left ()
3145 "Finish `org-goto' by going to the new location."
3146 (interactive)
3147 (if (org-on-heading-p)
3148 (progn
3149 (beginning-of-line 1)
3150 (setq org-selected-point (point)
3151 current-prefix-arg (- (match-end 0) (match-beginning 0)))
3152 (throw 'exit nil))
3153 (error "Not on a heading")))
3155 (defun org-goto-right ()
3156 "Finish `org-goto' by going to the new location."
3157 (interactive)
3158 (if (org-on-heading-p)
3159 (progn
3160 (outline-end-of-subtree)
3161 (or (eobp) (forward-char 1))
3162 (setq org-selected-point (point)
3163 current-prefix-arg (- (match-end 0) (match-beginning 0)))
3164 (throw 'exit nil))
3165 (error "Not on a heading")))
3167 (defun org-goto-quit ()
3168 "Finish `org-goto' without cursor motion."
3169 (interactive)
3170 (setq org-selected-point nil)
3171 (throw 'exit nil))
3173 ;;; Promotion, Demotion, Inserting new headlines
3175 (defvar org-ignore-region nil
3176 "To temporarily disable the active region.")
3178 (defun org-insert-heading (&optional force-heading)
3179 "Insert a new heading or item with same depth at point."
3180 (interactive "P")
3181 (if (= (buffer-size) 0)
3182 (insert "\n* ")
3183 (when (or force-heading (not (org-insert-item)))
3184 (let* ((head (save-excursion
3185 (condition-case nil
3186 (org-back-to-heading)
3187 (error (outline-next-heading)))
3188 (prog1 (match-string 0)
3189 (funcall outline-level)))))
3190 (cond
3191 ((and (org-on-heading-p) (bolp)
3192 (save-excursion (backward-char 1) (not (org-invisible-p))))
3193 (open-line 1))
3194 ((bolp) nil)
3195 (t (newline)))
3196 (insert head)
3197 (just-one-space)
3198 (run-hooks 'org-insert-heading-hook)))))
3200 (defun org-insert-item ()
3201 "Insert a new item at the current level.
3202 Return t when things worked, nil when we are not in an item."
3203 (when (save-excursion
3204 (condition-case nil
3205 (progn
3206 (org-beginning-of-item)
3207 (org-at-item-p)
3209 (error nil)))
3210 (let* ((bul (match-string 0))
3211 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
3212 (match-end 0)))
3213 (eowcol (save-excursion (goto-char eow) (current-column))))
3214 (cond
3215 ((and (org-at-item-p) (<= (point) eow))
3216 ;; before the bullet
3217 (beginning-of-line 1)
3218 (open-line 1))
3219 ((<= (point) eow)
3220 (beginning-of-line 1))
3221 (t (newline)))
3222 (insert bul)
3223 (just-one-space))
3224 (org-maybe-renumber-ordered-list)
3227 (defun org-insert-todo-heading (arg)
3228 "Insert a new heading with the same level and TODO state as current heading.
3229 If the heading has no TODO state, or if the state is DONE, use the first
3230 state (TODO by default). Also with prefix arg, force first state."
3231 (interactive "P")
3232 (org-insert-heading)
3233 (save-excursion
3234 (org-back-to-heading)
3235 (outline-previous-heading)
3236 (looking-at org-todo-line-regexp))
3237 (if (or arg
3238 (not (match-beginning 2))
3239 (equal (match-string 2) org-done-string))
3240 (insert (car org-todo-keywords) " ")
3241 (insert (match-string 2) " ")))
3243 (defun org-promote-subtree ()
3244 "Promote the entire subtree.
3245 See also `org-promote'."
3246 (interactive)
3247 (save-excursion
3248 (org-map-tree 'org-promote)))
3250 (defun org-demote-subtree ()
3251 "Demote the entire subtree. See `org-demote'.
3252 See also `org-promote'."
3253 (interactive)
3254 (save-excursion
3255 (org-map-tree 'org-demote)))
3257 (defun org-do-promote ()
3258 "Promote the current heading higher up the tree.
3259 If the region is active in `transient-mark-mode', promote all headings
3260 in the region."
3261 (interactive)
3262 (save-excursion
3263 (if (org-region-active-p)
3264 (org-map-region 'org-promote (region-beginning) (region-end))
3265 (org-promote)))
3266 (org-fix-position-after-promote))
3268 (defun org-do-demote ()
3269 "Demote the current heading lower down the tree.
3270 If the region is active in `transient-mark-mode', demote all headings
3271 in the region."
3272 (interactive)
3273 (save-excursion
3274 (if (org-region-active-p)
3275 (org-map-region 'org-demote (region-beginning) (region-end))
3276 (org-demote)))
3277 (org-fix-position-after-promote))
3279 (defun org-fix-position-after-promote ()
3280 "Make sure that after pro/demotion cursor position is right."
3281 (and (equal (char-after) ?\ )
3282 (equal (char-before) ?*)
3283 (forward-char 1)))
3285 (defun org-get-legal-level (level change)
3286 "Rectify a level change under the influence of `org-odd-levels-only'
3287 LEVEL is a current level, CHANGE is by how much the level should be
3288 modified. Even if CHANGE is nil, LEVEL may be returned modified because
3289 even level numbers will become the next higher odd number."
3290 (if org-odd-levels-only
3291 (cond ((not change) (1+ (* 2 (/ level 2))))
3292 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
3293 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
3294 (max 1 (+ level change))))
3296 (defun org-promote ()
3297 "Promote the current heading higher up the tree.
3298 If the region is active in `transient-mark-mode', promote all headings
3299 in the region."
3300 (org-back-to-heading t)
3301 (let* ((level (save-match-data (funcall outline-level)))
3302 (up-head (make-string (org-get-legal-level level -1) ?*))
3303 (diff (abs (- level (length up-head)))))
3304 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
3305 (replace-match up-head nil t)
3306 ;; Fixup tag positioning
3307 (and org-auto-align-tags (org-set-tags nil t))
3308 (if org-adapt-indentation
3309 (org-fixup-indentation (if (> diff 1) "^ " "^ ") ""
3310 (if (> diff 1) "^ ? ?\\S-" "^ ?\\S-")))))
3312 (defun org-demote ()
3313 "Demote the current heading lower down the tree.
3314 If the region is active in `transient-mark-mode', demote all headings
3315 in the region."
3316 (org-back-to-heading t)
3317 (let* ((level (save-match-data (funcall outline-level)))
3318 (down-head (make-string (org-get-legal-level level 1) ?*))
3319 (diff (abs (- level (length down-head)))))
3320 (replace-match down-head nil t)
3321 ;; Fixup tag positioning
3322 (and org-auto-align-tags (org-set-tags nil t))
3323 (if org-adapt-indentation
3324 (org-fixup-indentation "^ " (if (> diff 1) " " " ") "^\\S-"))))
3326 (defun org-map-tree (fun)
3327 "Call FUN for every heading underneath the current one."
3328 (org-back-to-heading)
3329 (let ((level (funcall outline-level)))
3330 (save-excursion
3331 (funcall fun)
3332 (while (and (progn
3333 (outline-next-heading)
3334 (> (funcall outline-level) level))
3335 (not (eobp)))
3336 (funcall fun)))))
3338 (defun org-map-region (fun beg end)
3339 "Call FUN for every heading between BEG and END."
3340 (let ((org-ignore-region t))
3341 (save-excursion
3342 (setq end (copy-marker end))
3343 (goto-char beg)
3344 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
3345 (< (point) end))
3346 (funcall fun))
3347 (while (and (progn
3348 (outline-next-heading)
3349 (< (point) end))
3350 (not (eobp)))
3351 (funcall fun)))))
3353 (defun org-fixup-indentation (from to prohibit)
3354 "Change the indentation in the current entry by re-replacing FROM with TO.
3355 However, if the regexp PROHIBIT matches at all, don't do anything.
3356 This is being used to change indentation along with the length of the
3357 heading marker. But if there are any lines which are not indented, nothing
3358 is changed at all."
3359 (save-excursion
3360 (let ((end (save-excursion (outline-next-heading)
3361 (point-marker))))
3362 (unless (save-excursion (re-search-forward prohibit end t))
3363 (while (re-search-forward from end t)
3364 (replace-match to)
3365 (beginning-of-line 2)))
3366 (move-marker end nil))))
3368 ;;; Vertical tree motion, cutting and pasting of subtrees
3370 (defun org-move-subtree-up (&optional arg)
3371 "Move the current subtree up past ARG headlines of the same level."
3372 (interactive "p")
3373 (org-move-subtree-down (- (prefix-numeric-value arg))))
3375 (defun org-move-subtree-down (&optional arg)
3376 "Move the current subtree down past ARG headlines of the same level."
3377 (interactive "p")
3378 (setq arg (prefix-numeric-value arg))
3379 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
3380 'outline-get-last-sibling))
3381 (ins-point (make-marker))
3382 (cnt (abs arg))
3383 beg end txt folded)
3384 ;; Select the tree
3385 (org-back-to-heading)
3386 (setq beg (point))
3387 (save-match-data
3388 (save-excursion (outline-end-of-heading)
3389 (setq folded (org-invisible-p)))
3390 (outline-end-of-subtree))
3391 (outline-next-heading)
3392 (setq end (point))
3393 ;; Find insertion point, with error handling
3394 (goto-char beg)
3395 (while (> cnt 0)
3396 (or (and (funcall movfunc) (looking-at outline-regexp))
3397 (progn (goto-char beg)
3398 (error "Cannot move past superior level or buffer limit")))
3399 (setq cnt (1- cnt)))
3400 (if (> arg 0)
3401 ;; Moving forward - still need to move over subtree
3402 (progn (outline-end-of-subtree)
3403 (outline-next-heading)
3404 (if (not (or (looking-at (concat "^" outline-regexp))
3405 (bolp)))
3406 (newline))))
3407 (move-marker ins-point (point))
3408 (setq txt (buffer-substring beg end))
3409 (delete-region beg end)
3410 (insert txt)
3411 (goto-char ins-point)
3412 (if folded (hide-subtree))
3413 (move-marker ins-point nil)))
3415 (defvar org-subtree-clip ""
3416 "Clipboard for cut and paste of subtrees.
3417 This is actually only a copy of the kill, because we use the normal kill
3418 ring. We need it to check if the kill was created by `org-copy-subtree'.")
3420 (defvar org-subtree-clip-folded nil
3421 "Was the last copied subtree folded?
3422 This is used to fold the tree back after pasting.")
3424 (defun org-cut-subtree ()
3425 "Cut the current subtree into the clipboard.
3426 This is a short-hand for marking the subtree and then cutting it."
3427 (interactive)
3428 (org-copy-subtree 'cut))
3430 (defun org-copy-subtree (&optional cut)
3431 "Cut the current subtree into the clipboard.
3432 This is a short-hand for marking the subtree and then copying it.
3433 If CUT is non nil, actually cut the subtree."
3434 (interactive)
3435 (let (beg end folded)
3436 (org-back-to-heading)
3437 (setq beg (point))
3438 (save-match-data
3439 (save-excursion (outline-end-of-heading)
3440 (setq folded (org-invisible-p)))
3441 (outline-end-of-subtree))
3442 (if (equal (char-after) ?\n) (forward-char 1))
3443 (setq end (point))
3444 (goto-char beg)
3445 (when (> end beg)
3446 (setq org-subtree-clip-folded folded)
3447 (if cut (kill-region beg end) (copy-region-as-kill beg end))
3448 (setq org-subtree-clip (current-kill 0))
3449 (message "%s: Subtree with %d characters"
3450 (if cut "Cut" "Copied")
3451 (length org-subtree-clip)))))
3453 (defun org-paste-subtree (&optional level tree)
3454 "Paste the clipboard as a subtree, with modification of headline level.
3455 The entire subtree is promoted or demoted in order to match a new headline
3456 level. By default, the new level is derived from the visible headings
3457 before and after the insertion point, and taken to be the inferior headline
3458 level of the two. So if the previous visible heading is level 3 and the
3459 next is level 4 (or vice versa), level 4 will be used for insertion.
3460 This makes sure that the subtree remains an independent subtree and does
3461 not swallow low level entries.
3463 You can also force a different level, either by using a numeric prefix
3464 argument, or by inserting the heading marker by hand. For example, if the
3465 cursor is after \"*****\", then the tree will be shifted to level 5.
3467 If you want to insert the tree as is, just use \\[yank].
3469 If optional TREE is given, use this text instead of the kill ring."
3470 (interactive "P")
3471 (unless (org-kill-is-subtree-p tree)
3472 (error
3473 (substitute-command-keys
3474 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
3475 (let* ((txt (or tree (current-kill 0)))
3476 (^re (concat "^\\(" outline-regexp "\\)"))
3477 (re (concat "\\(" outline-regexp "\\)"))
3478 (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
3480 (old-level (if (string-match ^re txt)
3481 (- (match-end 0) (match-beginning 0))
3482 -1))
3483 (force-level (cond (level (prefix-numeric-value level))
3484 ((string-match
3485 ^re_ (buffer-substring (point-at-bol) (point)))
3486 (- (match-end 0) (match-beginning 0)))
3487 (t nil)))
3488 (previous-level (save-excursion
3489 (condition-case nil
3490 (progn
3491 (outline-previous-visible-heading 1)
3492 (if (looking-at re)
3493 (- (match-end 0) (match-beginning 0))
3495 (error 1))))
3496 (next-level (save-excursion
3497 (condition-case nil
3498 (progn
3499 (outline-next-visible-heading 1)
3500 (if (looking-at re)
3501 (- (match-end 0) (match-beginning 0))
3503 (error 1))))
3504 (new-level (or force-level (max previous-level next-level)))
3505 (shift (if (or (= old-level -1)
3506 (= new-level -1)
3507 (= old-level new-level))
3509 (- new-level old-level)))
3510 (shift1 shift)
3511 (delta (if (> shift 0) -1 1))
3512 (func (if (> shift 0) 'org-demote 'org-promote))
3513 (org-odd-levels-only nil)
3514 beg end)
3515 ;; Remove the forces level indicator
3516 (if force-level
3517 (delete-region (point-at-bol) (point)))
3518 ;; Make sure we start at the beginning of an empty line
3519 (if (not (bolp)) (insert "\n"))
3520 (if (not (looking-at "[ \t]*$"))
3521 (progn (insert "\n") (backward-char 1)))
3522 ;; Paste
3523 (setq beg (point))
3524 (insert txt)
3525 (setq end (point))
3526 (goto-char beg)
3527 ;; Shift if necessary
3528 (if (= shift 0)
3529 (message "Pasted at level %d, without shift" new-level)
3530 (save-restriction
3531 (narrow-to-region beg end)
3532 (while (not (= shift 0))
3533 (org-map-region func (point-min) (point-max))
3534 (setq shift (+ delta shift)))
3535 (goto-char (point-min))
3536 (message "Pasted at level %d, with shift by %d levels"
3537 new-level shift1)))
3538 (if (and (eq org-subtree-clip (current-kill 0))
3539 org-subtree-clip-folded)
3540 ;; The tree was folded before it was killed/copied
3541 (hide-subtree))))
3543 (defun org-kill-is-subtree-p (&optional txt)
3544 "Check if the current kill is an outline subtree, or a set of trees.
3545 Returns nil if kill does not start with a headline, or if the first
3546 headline level is not the largest headline level in the tree.
3547 So this will actually accept several entries of equal levels as well,
3548 which is OK for `org-paste-subtree'.
3549 If optional TXT is given, check this string instead of the current kill."
3550 (let* ((kill (or txt (current-kill 0) ""))
3551 (start-level (and (string-match (concat "\\`" outline-regexp) kill)
3552 (- (match-end 0) (match-beginning 0))))
3553 (re (concat "^" outline-regexp))
3554 (start 1))
3555 (if (not start-level)
3556 nil ;; does not even start with a heading
3557 (catch 'exit
3558 (while (setq start (string-match re kill (1+ start)))
3559 (if (< (- (match-end 0) (match-beginning 0)) start-level)
3560 (throw 'exit nil)))
3561 t))))
3563 ;;; Plain list items
3565 (defun org-at-item-p ()
3566 "Is point in a line starting a hand-formatted item?"
3567 (let ((llt org-plain-list-ordered-item-terminator))
3568 (save-excursion
3569 (goto-char (point-at-bol))
3570 (looking-at
3571 (cond
3572 ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
3573 ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
3574 ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
3575 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
3577 (defun org-get-indentation ()
3578 "Get the indentation of the current line, interpreting tabs."
3579 (save-excursion
3580 (beginning-of-line 1)
3581 (skip-chars-forward " \t")
3582 (current-column)))
3584 (defun org-beginning-of-item ()
3585 "Go to the beginning of the current hand-formatted item.
3586 If the cursor is not in an item, throw an error."
3587 (let ((pos (point))
3588 (limit (save-excursion (org-back-to-heading)
3589 (beginning-of-line 2) (point)))
3590 ind ind1)
3591 (if (org-at-item-p)
3592 (beginning-of-line 1)
3593 (beginning-of-line 1)
3594 (skip-chars-forward " \t")
3595 (setq ind (current-column))
3596 (if (catch 'exit
3597 (while t
3598 (beginning-of-line 0)
3599 (if (< (point) limit) (throw 'exit nil))
3600 (unless (looking-at " \t]*$")
3601 (skip-chars-forward " \t")
3602 (setq ind1 (current-column))
3603 (if (< ind1 ind)
3604 (throw 'exit (org-at-item-p))))))
3606 (goto-char pos)
3607 (error "Not in an item")))))
3609 (defun org-end-of-item ()
3610 "Go to the end of the current hand-formatted item.
3611 If the cursor is not in an item, throw an error."
3612 (let ((pos (point))
3613 (limit (save-excursion (outline-next-heading) (point)))
3614 (ind (save-excursion
3615 (org-beginning-of-item)
3616 (skip-chars-forward " \t")
3617 (current-column)))
3618 ind1)
3619 (if (catch 'exit
3620 (while t
3621 (beginning-of-line 2)
3622 (if (>= (point) limit) (throw 'exit t))
3623 (unless (looking-at "[ \t]*$")
3624 (skip-chars-forward " \t")
3625 (setq ind1 (current-column))
3626 (if (<= ind1 ind) (throw 'exit t)))))
3627 (beginning-of-line 1)
3628 (goto-char pos)
3629 (error "Not in an item"))))
3631 (defun org-move-item-down (arg)
3632 "Move the plain list item at point down, i.e. swap with following item.
3633 Subitems (items with larger indentation) are considered part of the item,
3634 so this really moves item trees."
3635 (interactive "p")
3636 (let (beg end ind ind1 (pos (point)) txt)
3637 (org-beginning-of-item)
3638 (setq beg (point))
3639 (setq ind (org-get-indentation))
3640 (org-end-of-item)
3641 (setq end (point))
3642 (setq ind1 (org-get-indentation))
3643 (if (and (org-at-item-p) (= ind ind1))
3644 (progn
3645 (org-end-of-item)
3646 (setq txt (buffer-substring beg end))
3647 (save-excursion
3648 (delete-region beg end))
3649 (setq pos (point))
3650 (insert txt)
3651 (goto-char pos)
3652 (org-maybe-renumber-ordered-list))
3653 (goto-char pos)
3654 (error "Cannot move this item further down"))))
3656 (defun org-move-item-up (arg)
3657 "Move the plain list item at point up, i.e. swap with previous item.
3658 Subitems (items with larger indentation) are considered part of the item,
3659 so this really moves item trees."
3660 (interactive "p")
3661 (let (beg end ind ind1 (pos (point)) txt)
3662 (org-beginning-of-item)
3663 (setq beg (point))
3664 (setq ind (org-get-indentation))
3665 (org-end-of-item)
3666 (setq end (point))
3667 (goto-char beg)
3668 (catch 'exit
3669 (while t
3670 (beginning-of-line 0)
3671 (if (looking-at "[ \t]*$")
3673 (if (<= (setq ind1 (org-get-indentation)) ind)
3674 (throw 'exit t)))))
3675 (condition-case nil
3676 (org-beginning-of-item)
3677 (error (goto-char beg)
3678 (error "Cannot move this item further up")))
3679 (setq ind1 (org-get-indentation))
3680 (if (and (org-at-item-p) (= ind ind1))
3681 (progn
3682 (setq txt (buffer-substring beg end))
3683 (save-excursion
3684 (delete-region beg end))
3685 (setq pos (point))
3686 (insert txt)
3687 (goto-char pos)
3688 (org-maybe-renumber-ordered-list))
3689 (goto-char pos)
3690 (error "Cannot move this item further up"))))
3692 (defun org-maybe-renumber-ordered-list ()
3693 "Renumber the ordered list at point if setup allows it.
3694 This tests the user option `org-auto-renumber-ordered-lists' before
3695 doing the renumbering."
3696 (and org-auto-renumber-ordered-lists
3697 (org-at-item-p)
3698 (match-beginning 3)
3699 (org-renumber-ordered-list 1)))
3701 (defun org-get-string-indentation (s)
3702 "What indentation has S due to SPACE and TAB at the beginning of the string?"
3703 (let ((n -1) (i 0) (w tab-width) c)
3704 (catch 'exit
3705 (while (< (setq n (1+ n)) (length s))
3706 (setq c (aref s n))
3707 (cond ((= c ?\ ) (setq i (1+ i)))
3708 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
3709 (t (throw 'exit t)))))
3712 (defun org-renumber-ordered-list (arg)
3713 "Renumber an ordered plain list.
3714 Cursor next to be in the first line of an item, the line that starts
3715 with something like \"1.\" or \"2)\"."
3716 (interactive "p")
3717 (unless (and (org-at-item-p)
3718 (match-beginning 3))
3719 (error "This is not an ordered list"))
3720 (let ((line (org-current-line))
3721 (col (current-column))
3722 (ind (org-get-string-indentation
3723 (buffer-substring (point-at-bol) (match-beginning 3))))
3724 ;; (term (substring (match-string 3) -1))
3725 ind1 (n (1- arg)))
3726 ;; find where this list begins
3727 (catch 'exit
3728 (while t
3729 (catch 'next
3730 (beginning-of-line 0)
3731 (if (looking-at "[ \t]*$") (throw 'next t))
3732 (skip-chars-forward " \t") (setq ind1 (current-column))
3733 (if (or (< ind1 ind)
3734 (and (= ind1 ind)
3735 (not (org-at-item-p))))
3736 (throw 'exit t)))))
3737 ;; Walk forward and replace these numbers
3738 (catch 'exit
3739 (while t
3740 (catch 'next
3741 (beginning-of-line 2)
3742 (if (eobp) (throw 'exit nil))
3743 (if (looking-at "[ \t]*$") (throw 'next nil))
3744 (skip-chars-forward " \t") (setq ind1 (current-column))
3745 (if (> ind1 ind) (throw 'next t))
3746 (if (< ind1 ind) (throw 'exit t))
3747 (if (not (org-at-item-p)) (throw 'exit nil))
3748 (if (not (match-beginning 3))
3749 (error "unordered bullet in ordered list. Press \\[undo] to recover"))
3750 (delete-region (match-beginning 3) (1- (match-end 3)))
3751 (goto-char (match-beginning 3))
3752 (insert (format "%d" (setq n (1+ n)))))))
3753 (goto-line line)
3754 (move-to-column col)))
3756 (defvar org-last-indent-begin-marker (make-marker))
3757 (defvar org-last-indent-end-marker (make-marker))
3759 (defun org-outdent-item (arg)
3760 "Outdent a local list item."
3761 (interactive "p")
3762 (org-indent-item (- arg)))
3764 (defun org-indent-item (arg)
3765 "Indent a local list item."
3766 (interactive "p")
3767 (unless (org-at-item-p)
3768 (error "Not on an item"))
3769 (let (beg end ind ind1)
3770 (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
3771 (setq beg org-last-indent-begin-marker
3772 end org-last-indent-end-marker)
3773 (org-beginning-of-item)
3774 (setq beg (move-marker org-last-indent-begin-marker (point)))
3775 (org-end-of-item)
3776 (setq end (move-marker org-last-indent-end-marker (point))))
3777 (goto-char beg)
3778 (skip-chars-forward " \t") (setq ind (current-column))
3779 (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
3780 (while (< (point) end)
3781 (beginning-of-line 1)
3782 (skip-chars-forward " \t") (setq ind1 (current-column))
3783 (delete-region (point-at-bol) (point))
3784 (indent-to-column (+ ind1 arg))
3785 (beginning-of-line 2))
3786 (goto-char beg)))
3788 ;;; Archiving
3790 (defun org-archive-subtree ()
3791 "Move the current subtree to the archive.
3792 The archive can be a certain top-level heading in the current file, or in
3793 a different file. The tree will be moved to that location, the subtree
3794 heading be marked DONE, and the current time will be added."
3795 (interactive)
3796 ;; Save all relevant TODO keyword-relatex variables
3797 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
3798 (tr-org-todo-keywords org-todo-keywords)
3799 (tr-org-todo-interpretation org-todo-interpretation)
3800 (tr-org-done-string org-done-string)
3801 (tr-org-todo-regexp org-todo-regexp)
3802 (tr-org-todo-line-regexp org-todo-line-regexp)
3803 (this-buffer (current-buffer))
3804 file heading buffer level newfile-p)
3805 (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
3806 (progn
3807 (setq file (format (match-string 1 org-archive-location)
3808 (file-name-nondirectory buffer-file-name))
3809 heading (match-string 2 org-archive-location)))
3810 (error "Invalid `org-archive-location'"))
3811 (if (> (length file) 0)
3812 (setq newfile-p (not (file-exists-p file))
3813 buffer (find-file-noselect file))
3814 (setq buffer (current-buffer)))
3815 (unless buffer
3816 (error "Cannot access file \"%s\"" file))
3817 (if (and (> (length heading) 0)
3818 (string-match "^\\*+" heading))
3819 (setq level (match-end 0))
3820 (setq heading nil level 0))
3821 (save-excursion
3822 ;; We first only copy, in case something goes wrong
3823 ;; we need to protect this-command, to avoid kill-region sets it,
3824 ;; which would lead to duplication of subtrees
3825 (let (this-command) (org-copy-subtree))
3826 (set-buffer buffer)
3827 ;; Enforce org-mode for the archive buffer
3828 (if (not (eq major-mode 'org-mode))
3829 ;; Force the mode for future visits.
3830 (let ((org-insert-mode-line-in-empty-file t))
3831 (call-interactively 'org-mode)))
3832 (when newfile-p
3833 (goto-char (point-max))
3834 (insert (format "\nArchived entries from file %s\n\n"
3835 (buffer-file-name this-buffer))))
3836 ;; Force the TODO keywords of the original buffer
3837 (let ((org-todo-line-regexp tr-org-todo-line-regexp)
3838 (org-todo-keywords tr-org-todo-keywords)
3839 (org-todo-interpretation tr-org-todo-interpretation)
3840 (org-done-string tr-org-done-string)
3841 (org-todo-regexp tr-org-todo-regexp)
3842 (org-todo-line-regexp tr-org-todo-line-regexp))
3843 (goto-char (point-min))
3844 (if heading
3845 (progn
3846 (if (re-search-forward
3847 (concat "\\(^\\|\r\\)"
3848 (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
3849 nil t)
3850 (goto-char (match-end 0))
3851 ;; Heading not found, just insert it at the end
3852 (goto-char (point-max))
3853 (or (bolp) (insert "\n"))
3854 (insert "\n" heading "\n")
3855 (end-of-line 0))
3856 ;; Make the heading visible, and the following as well
3857 (let ((org-show-following-heading t)) (org-show-hierarchy-above))
3858 (if (re-search-forward
3859 (concat "^" (regexp-quote (make-string level ?*)) "[ \t]")
3860 nil t)
3861 (progn (goto-char (match-beginning 0)) (insert "\n")
3862 (beginning-of-line 0))
3863 (goto-char (point-max)) (insert "\n")))
3864 (goto-char (point-max)) (insert "\n"))
3865 ;; Paste
3866 (org-paste-subtree (1+ level))
3867 ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
3868 (if org-archive-mark-done
3869 (org-todo (length org-todo-keywords)))
3870 ;; Move cursor to right after the TODO keyword
3871 (when org-archive-stamp-time
3872 (beginning-of-line 1)
3873 (looking-at org-todo-line-regexp)
3874 (goto-char (or (match-end 2) (match-beginning 3)))
3875 (insert "(" (format-time-string (cdr org-time-stamp-formats)
3876 (org-current-time))
3877 ")"))
3878 ;; Save the buffer, if it is not the same buffer.
3879 (if (not (eq this-buffer buffer)) (save-buffer))))
3880 ;; Here we are back in the original buffer. Everything seems to have
3881 ;; worked. So now cut the tree and finish up.
3882 (let (this-command) (org-cut-subtree))
3883 (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
3884 (message "Subtree archived %s"
3885 (if (eq this-buffer buffer)
3886 (concat "under heading: " heading)
3887 (concat "in file: " (abbreviate-file-name file))))))
3889 ;;; Completion
3891 (defun org-complete (&optional arg)
3892 "Perform completion on word at point.
3893 At the beginning of a headline, this completes TODO keywords as given in
3894 `org-todo-keywords'.
3895 If the current word is preceded by a backslash, completes the TeX symbols
3896 that are supported for HTML support.
3897 If the current word is preceded by \"#+\", completes special words for
3898 setting file options.
3899 At all other locations, this simply calls `ispell-complete-word'."
3900 (interactive "P")
3901 (catch 'exit
3902 (let* ((end (point))
3903 (beg1 (save-excursion
3904 (if (equal (char-before (point)) ?\ ) (backward-char 1))
3905 (skip-chars-backward "a-zA-Z_@0-9")
3906 (point)))
3907 (beg (save-excursion
3908 (if (equal (char-before (point)) ?\ ) (backward-char 1))
3909 (skip-chars-backward "a-zA-Z0-9_:$")
3910 (point)))
3911 (camel (equal (char-before beg) ?*))
3912 (tag (equal (char-before beg1) ?:))
3913 (texp (equal (char-before beg) ?\\))
3914 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
3915 beg)
3916 "#+"))
3917 (completion-ignore-case opt)
3918 (type nil)
3919 (tbl nil)
3920 (table (cond
3921 (opt
3922 (setq type :opt)
3923 (mapcar (lambda (x)
3924 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
3925 (cons (match-string 2 x) (match-string 1 x)))
3926 (org-split-string (org-get-current-options) "\n")))
3927 (texp
3928 (setq type :tex)
3929 org-html-entities)
3930 ((string-match "\\`\\*+[ \t]*\\'"
3931 (buffer-substring (point-at-bol) beg))
3932 (setq type :todo)
3933 (mapcar 'list org-todo-keywords))
3934 (camel
3935 (setq type :camel)
3936 (save-excursion
3937 (goto-char (point-min))
3938 (while (re-search-forward org-todo-line-regexp nil t)
3939 (push (list
3940 (if org-file-link-context-use-camel-case
3941 (org-make-org-heading-camel (match-string 3) t)
3942 (org-make-org-heading-search-string
3943 (match-string 3) t)))
3944 tbl)))
3945 tbl)
3946 (tag (setq type :tag beg beg1)
3947 (org-get-buffer-tags))
3948 (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
3949 (pattern (buffer-substring-no-properties beg end))
3950 (completion (try-completion pattern table)))
3951 (cond ((eq completion t)
3952 (if (equal type :opt)
3953 (insert (substring (cdr (assoc (upcase pattern) table))
3954 (length pattern)))))
3955 ((null completion)
3956 (message "Can't find completion for \"%s\"" pattern)
3957 (ding))
3958 ((not (string= pattern completion))
3959 (delete-region beg end)
3960 (if (string-match " +$" completion)
3961 (setq completion (replace-match "" t t completion)))
3962 (insert completion)
3963 (if (get-buffer-window "*Completions*")
3964 (delete-window (get-buffer-window "*Completions*")))
3965 (if (assoc completion table)
3966 (if (eq type :todo) (insert " ")
3967 (if (eq type :tag) (insert ":"))))
3968 (if (and (equal type :opt) (assoc completion table))
3969 (message "%s" (substitute-command-keys
3970 "Press \\[org-complete] again to insert example settings"))))
3972 (message "Making completion list...")
3973 (let ((list (sort (all-completions pattern table) 'string<)))
3974 (with-output-to-temp-buffer "*Completions*"
3975 (condition-case nil
3976 ;; Protection needed for XEmacs and emacs 21
3977 (display-completion-list list pattern)
3978 (error (display-completion-list list)))))
3979 (message "Making completion list...%s" "done"))))))
3981 ;;; Comments, TODO and DEADLINE
3983 (defun org-toggle-comment ()
3984 "Change the COMMENT state of an entry."
3985 (interactive)
3986 (save-excursion
3987 (org-back-to-heading)
3988 (if (looking-at (concat outline-regexp
3989 "\\( +\\<" org-comment-string "\\>\\)"))
3990 (replace-match "" t t nil 1)
3991 (if (looking-at outline-regexp)
3992 (progn
3993 (goto-char (match-end 0))
3994 (insert " " org-comment-string))))))
3996 (defvar org-last-todo-state-is-todo nil
3997 "This is non-nil when the last TODO state change led to a TODO state.
3998 If the last change removed the TODO tag or switched to DONE, then
3999 this is nil.")
4001 (defun org-todo (&optional arg)
4002 "Change the TODO state of an item.
4003 The state of an item is given by a keyword at the start of the heading,
4004 like
4005 *** TODO Write paper
4006 *** DONE Call mom
4008 The different keywords are specified in the variable `org-todo-keywords'.
4009 By default the available states are \"TODO\" and \"DONE\".
4010 So for this example: when the item starts with TODO, it is changed to DONE.
4011 When it starts with DONE, the DONE is removed. And when neither TODO nor
4012 DONE are present, add TODO at the beginning of the heading.
4014 With prefix arg, use completion to determine the new state. With numeric
4015 prefix arg, switch to that state."
4016 (interactive "P")
4017 (save-excursion
4018 (org-back-to-heading)
4019 (if (looking-at outline-regexp) (goto-char (match-end 0)))
4020 (or (looking-at (concat " +" org-todo-regexp " *"))
4021 (looking-at " *"))
4022 (let* ((this (match-string 1))
4023 (completion-ignore-case t)
4024 (member (member this org-todo-keywords))
4025 (tail (cdr member))
4026 (state (cond
4027 ((equal arg '(4))
4028 ;; Read a state with completion
4029 (completing-read "State: " (mapcar (lambda(x) (list x))
4030 org-todo-keywords)
4031 nil t))
4032 ((eq arg 'right)
4033 (if this
4034 (if tail (car tail) nil)
4035 (car org-todo-keywords)))
4036 ((eq arg 'left)
4037 (if (equal member org-todo-keywords)
4039 (if this
4040 (nth (- (length org-todo-keywords) (length tail) 2)
4041 org-todo-keywords)
4042 org-done-string)))
4043 (arg
4044 ;; user requests a specific state
4045 (nth (1- (prefix-numeric-value arg))
4046 org-todo-keywords))
4047 ((null member) (car org-todo-keywords))
4048 ((null tail) nil) ;; -> first entry
4049 ((eq org-todo-interpretation 'sequence)
4050 (car tail))
4051 ((memq org-todo-interpretation '(type priority))
4052 (if (eq this-command last-command)
4053 (car tail)
4054 (if (> (length tail) 0) org-done-string nil)))
4055 (t nil)))
4056 (next (if state (concat " " state " ") " ")))
4057 (replace-match next t t)
4058 (setq org-last-todo-state-is-todo
4059 (not (equal state org-done-string)))
4060 (when org-log-done
4061 (if (equal state org-done-string)
4062 (org-log-done)
4063 (if (not this)
4064 (org-log-done t))))
4065 ;; Fixup tag positioning
4066 (and org-auto-align-tags (org-set-tags nil t))
4067 (run-hooks 'org-after-todo-state-change-hook)))
4068 ;; Fixup cursor location if close to the keyword
4069 (if (and (outline-on-heading-p)
4070 (not (bolp))
4071 (save-excursion (beginning-of-line 1)
4072 (looking-at org-todo-line-regexp))
4073 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
4074 (progn
4075 (goto-char (or (match-end 2) (match-end 1)))
4076 (just-one-space))))
4078 (defun org-log-done (&optional undone)
4079 "Add a time stamp logging that a TODO entry has been closed.
4080 When UNDONE is non-nil, remove such a time stamp again."
4081 (interactive)
4082 (let (beg end col)
4083 (save-excursion
4084 (org-back-to-heading t)
4085 (setq beg (point))
4086 (looking-at (concat outline-regexp " *"))
4087 (goto-char (match-end 0))
4088 (setq col (current-column))
4089 (outline-next-heading)
4090 (setq end (point))
4091 (goto-char beg)
4092 (when (re-search-forward (concat
4093 "[\r\n]\\([ \t]*"
4094 (regexp-quote org-closed-string)
4095 " *\\[.*?\\][^\n\r]*[\n\r]?\\)") end t)
4096 (delete-region (match-beginning 1) (match-end 1)))
4097 (unless undone
4098 (org-back-to-heading t)
4099 (skip-chars-forward "^\n\r")
4100 (goto-char (min (1+ (point)) (point-max)))
4101 (when (not (member (char-before) '(?\r ?\n)))
4102 (insert "\n"))
4103 (indent-to col)
4104 (insert org-closed-string " "
4105 (format-time-string
4106 (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
4107 (org-current-time))
4108 "\n")))))
4110 (defun org-show-todo-tree (arg)
4111 "Make a compact tree which shows all headlines marked with TODO.
4112 The tree will show the lines where the regexp matches, and all higher
4113 headlines above the match.
4114 With \\[universal-argument] prefix, also show the DONE entries.
4115 With a numeric prefix N, construct a sparse tree for the Nth element
4116 of `org-todo-keywords'."
4117 (interactive "P")
4118 (let ((case-fold-search nil)
4119 (kwd-re
4120 (cond ((null arg) org-not-done-regexp)
4121 ((equal arg '(4)) org-todo-regexp)
4122 ((<= (prefix-numeric-value arg) (length org-todo-keywords))
4123 (regexp-quote (nth (1- (prefix-numeric-value arg))
4124 org-todo-keywords)))
4125 (t (error "Invalid prefix argument: %s" arg)))))
4126 (message "%d TODO entries found"
4127 (org-occur (concat "^" outline-regexp " +" kwd-re )))))
4129 (defun org-deadline ()
4130 "Insert the DEADLINE: string to make a deadline.
4131 A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
4132 to modify it to the correct date."
4133 (interactive)
4134 (insert
4135 org-deadline-string " "
4136 (format-time-string (car org-time-stamp-formats)
4137 (org-read-date nil 'to-time)))
4138 (message "%s" (substitute-command-keys
4139 "Use \\[org-timestamp-up-day] and \\[org-timestamp-down-day] to change the date.")))
4141 (defun org-schedule ()
4142 "Insert the SCHEDULED: string to schedule a TODO item.
4143 A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
4144 to modify it to the correct date."
4145 (interactive)
4146 (insert
4147 org-scheduled-string " "
4148 (format-time-string (car org-time-stamp-formats)
4149 (org-read-date nil 'to-time)))
4150 (message "%s" (substitute-command-keys
4151 "Use \\[org-timestamp-up-day] and \\[org-timestamp-down-day] to change the date.")))
4153 (defun org-add-planning-info (what &optional time &rest remove)
4154 "Insert new timestamp with keyword in the line directly after the headline.
4155 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
4156 If non is given, the user is prompted for a date.
4157 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
4158 be removed."
4159 (interactive)
4160 (save-excursion
4161 (let (beg end col list elt)
4162 (org-show-entry) ; Avoid this.
4163 (org-back-to-heading t)
4164 (setq beg (point))
4165 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
4166 (goto-char (match-end 1))
4167 (setq col (current-column))
4168 (goto-char (1+ (match-end 0)))
4169 (if (and (not (looking-at outline-regexp))
4170 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
4171 "[^\r\n]*")))
4172 (narrow-to-region (match-beginning 0) (match-end 0))
4173 (insert "\n")
4174 (backward-char 1)
4175 (narrow-to-region (point) (point))
4176 (indent-to-column col))
4177 ;; Check if we have to remove something.
4178 (setq list (cons what remove))
4179 (while list
4180 (setq elt (pop list))
4181 (goto-char (point-min))
4182 (when (or (and (eq elt 'scheduled)
4183 (re-search-forward org-scheduled-time-regexp nil t))
4184 (and (eq elt 'deadline)
4185 (re-search-forward org-deadline-time-regexp nil t))
4186 (and (eq elt 'closed)
4187 (re-search-forward org-closed-time-regexp nil t)))
4188 (replace-match "")
4189 (if (looking-at " +") (replace-match ""))))
4190 (goto-char (point-max))
4191 (when what
4192 (insert
4193 (if (not (equal (char-before) ?\ )) " " "")
4194 (cond ((eq what 'scheduled) org-scheduled-string)
4195 ((eq what 'deadline) org-deadline-string)
4196 ((eq what 'closed) org-closed-string))
4197 " ")
4198 (insert
4199 (format-time-string
4200 (if (eq what 'closed)
4201 (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
4202 (car org-time-stamp-formats))
4203 (or time (org-read-date nil 'to-time)))))
4204 (goto-char (point-min))
4205 (widen)
4206 (if (looking-at "[ \t]+\r?\n")
4207 (replace-match "")))))
4209 (defun org-occur (regexp &optional callback)
4210 "Make a compact tree which shows all matches of REGEXP.
4211 The tree will show the lines where the regexp matches, and all higher
4212 headlines above the match. It will also show the heading after the match,
4213 to make sure editing the matching entry is easy.
4214 If CALLBACK is non-nil, it is a function which is called to confirm
4215 that the match should indeed be shown."
4216 (interactive "sRegexp: ")
4217 (org-remove-occur-highlights nil nil t)
4218 (setq regexp (org-check-occur-regexp regexp))
4219 (let ((cnt 0))
4220 (save-excursion
4221 (goto-char (point-min))
4222 (hide-sublevels 1)
4223 (while (re-search-forward regexp nil t)
4224 (when (or (not callback)
4225 (save-match-data (funcall callback)))
4226 (setq cnt (1+ cnt))
4227 (org-highlight-new-match (match-beginning 0) (match-end 0))
4228 (org-show-hierarchy-above))))
4229 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
4230 nil 'local)
4231 (run-hooks 'org-occur-hook)
4232 (if (interactive-p)
4233 (message "%d match(es) for regexp %s" cnt regexp))
4234 cnt))
4236 (defun org-show-hierarchy-above ()
4237 "Make sure point and the headings hierarchy above is visible."
4238 (catch 'exit
4239 (if (org-on-heading-p t)
4240 (org-flag-heading nil) ; only show the heading
4241 (and (or (org-invisible-p) (org-invisible-p2))
4242 (org-show-hidden-entry))) ; show entire entry
4243 (save-excursion
4244 (and org-show-following-heading
4245 (outline-next-heading)
4246 (org-flag-heading nil))) ; show the next heading
4247 (when org-show-hierarchy-above
4248 (save-excursion ; show all higher headings
4249 (while (and (condition-case nil
4250 (progn (org-up-heading-all 1) t)
4251 (error nil))
4252 (not (bobp)))
4253 (org-flag-heading nil))))))
4255 ;; Overlay compatibility functions
4256 (defun org-make-overlay (beg end &optional buffer)
4257 (if (featurep 'xemacs)
4258 (make-extent beg end buffer)
4259 (make-overlay beg end buffer)))
4260 (defun org-delete-overlay (ovl)
4261 (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
4262 (defun org-detatch-overlay (ovl)
4263 (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
4264 (defun org-move-overlay (ovl beg end &optional buffer)
4265 (if (featurep 'xemacs)
4266 (set-extent-endpoints ovl beg end buffer)
4267 (move-overlay ovl beg end buffer)))
4268 (defun org-overlay-put (ovl prop value)
4269 (if (featurep 'xemacs)
4270 (set-extent-property ovl prop value)
4271 (overlay-put ovl prop value)))
4273 (defvar org-occur-highlights nil)
4274 (defun org-highlight-new-match (beg end)
4275 "Highlight from BEG to END and mark the highlight is an occur headline."
4276 (let ((ov (org-make-overlay beg end)))
4277 (org-overlay-put ov 'face 'secondary-selection)
4278 (push ov org-occur-highlights)))
4280 (defun org-remove-occur-highlights (&optional beg end noremove)
4281 "Remove the occur highlights from the buffer.
4282 BEG and END are ignored. If NOREMOVE is nil, remove this function
4283 from the `before-change-functions' in the current buffer."
4284 (interactive)
4285 (mapc 'org-delete-overlay org-occur-highlights)
4286 (setq org-occur-highlights nil)
4287 (unless noremove
4288 (remove-hook 'before-change-functions
4289 'org-remove-occur-highlights 'local)))
4291 ;;; Priorities
4293 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
4294 "Regular expression matching the priority indicator.")
4296 (defvar org-remove-priority-next-time nil)
4298 (defun org-priority-up ()
4299 "Increase the priority of the current item."
4300 (interactive)
4301 (org-priority 'up))
4303 (defun org-priority-down ()
4304 "Decrease the priority of the current item."
4305 (interactive)
4306 (org-priority 'down))
4308 (defun org-priority (&optional action)
4309 "Change the priority of an item by ARG.
4310 ACTION can be set, up, or down."
4311 (interactive)
4312 (setq action (or action 'set))
4313 (let (current new news have remove)
4314 (save-excursion
4315 (org-back-to-heading)
4316 (if (looking-at org-priority-regexp)
4317 (setq current (string-to-char (match-string 2))
4318 have t)
4319 (setq current org-default-priority))
4320 (cond
4321 ((eq action 'set)
4322 (message "Priority A-%c, SPC to remove: " org-lowest-priority)
4323 (setq new (read-char-exclusive))
4324 (cond ((equal new ?\ ) (setq remove t))
4325 ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
4326 (error "Priority must be between `%c' and `%c'"
4327 ?A org-lowest-priority))))
4328 ((eq action 'up)
4329 (setq new (1- current)))
4330 ((eq action 'down)
4331 (setq new (1+ current)))
4332 (t (error "Invalid action")))
4333 (setq new (min (max ?A (upcase new)) org-lowest-priority))
4334 (setq news (format "%c" new))
4335 (if have
4336 (if remove
4337 (replace-match "" t t nil 1)
4338 (replace-match news t t nil 2))
4339 (if remove
4340 (error "No priority cookie found in line")
4341 (looking-at org-todo-line-regexp)
4342 (if (match-end 2)
4343 (progn
4344 (goto-char (match-end 2))
4345 (insert " [#" news "]"))
4346 (goto-char (match-beginning 3))
4347 (insert "[#" news "] ")))))
4348 (if remove
4349 (message "Priority removed")
4350 (message "Priority of current item set to %s" news))))
4353 (defun org-get-priority (s)
4354 "Find priority cookie and return priority."
4355 (save-match-data
4356 (if (not (string-match org-priority-regexp s))
4357 (* 1000 (- org-lowest-priority org-default-priority))
4358 (* 1000 (- org-lowest-priority
4359 (string-to-char (match-string 2 s)))))))
4361 ;;; Timestamps
4363 (defvar org-last-changed-timestamp nil)
4365 (defun org-time-stamp (arg)
4366 "Prompt for a date/time and insert a time stamp.
4367 If the user specifies a time like HH:MM, or if this command is called
4368 with a prefix argument, the time stamp will contain date and time.
4369 Otherwise, only the date will be included. All parts of a date not
4370 specified by the user will be filled in from the current date/time.
4371 So if you press just return without typing anything, the time stamp
4372 will represent the current date/time. If there is already a timestamp
4373 at the cursor, it will be modified."
4374 (interactive "P")
4375 (let ((fmt (if arg (cdr org-time-stamp-formats)
4376 (car org-time-stamp-formats)))
4377 (org-time-was-given nil)
4378 time)
4379 (cond
4380 ((and (org-at-timestamp-p)
4381 (eq last-command 'org-time-stamp)
4382 (eq this-command 'org-time-stamp))
4383 (insert "--")
4384 (setq time (let ((this-command this-command))
4385 (org-read-date arg 'totime)))
4386 (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
4387 (insert (format-time-string fmt time)))
4388 ((org-at-timestamp-p)
4389 (setq time (let ((this-command this-command))
4390 (org-read-date arg 'totime)))
4391 (and (org-at-timestamp-p) (replace-match
4392 (setq org-last-changed-timestamp
4393 (format-time-string fmt time))
4394 t t))
4395 (message "Timestamp updated"))
4397 (setq time (let ((this-command this-command))
4398 (org-read-date arg 'totime)))
4399 (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
4400 (insert (format-time-string fmt time))))))
4402 (defun org-time-stamp-inactive (&optional arg)
4403 "Insert an inactive time stamp.
4404 An inactive time stamp is enclosed in square brackets instead of angle
4405 brackets. It is inactive in the sense that it does not trigger agenda entries,
4406 does not link to the calendar and cannot be changed with the S-cursor keys.
4407 So these are more for recording a certain time/date."
4408 (interactive "P")
4409 (let ((fmt (if arg (cdr org-time-stamp-formats)
4410 (car org-time-stamp-formats)))
4411 (org-time-was-given nil)
4412 time)
4413 (setq time (org-read-date arg 'totime))
4414 (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
4415 (setq fmt (concat "[" (substring fmt 1 -1) "]"))
4416 (insert (format-time-string fmt time))))
4418 (defvar org-date-ovl (org-make-overlay 1 1))
4419 (org-overlay-put org-date-ovl 'face 'org-warning)
4420 (org-detatch-overlay org-date-ovl)
4422 (defun org-read-date (&optional with-time to-time)
4423 "Read a date and make things smooth for the user.
4424 The prompt will suggest to enter an ISO date, but you can also enter anything
4425 which will at least partially be understood by `parse-time-string'.
4426 Unrecognized parts of the date will default to the current day, month, year,
4427 hour and minute. For example,
4428 3-2-5 --> 2003-02-05
4429 feb 15 --> currentyear-02-15
4430 sep 12 9 --> 2009-09-12
4431 12:45 --> today 12:45
4432 22 sept 0:34 --> currentyear-09-22 0:34
4433 12 --> currentyear-currentmonth-12
4434 Fri --> nearest Friday (today or later)
4435 etc.
4436 The function understands only English month and weekday abbreviations,
4437 but this can be configured with the variables `parse-time-months' and
4438 `parse-time-weekdays'.
4440 While prompting, a calendar is popped up - you can also select the
4441 date with the mouse (button 1). The calendar shows a period of three
4442 months. To scroll it to other months, use the keys `>' and `<'.
4443 If you don't like the calendar, turn it off with
4444 \(setq org-popup-calendar-for-date-prompt nil)
4446 With optional argument TO-TIME, the date will immediately be converted
4447 to an internal time.
4448 With an optional argument WITH-TIME, the prompt will suggest to also
4449 insert a time. Note that when WITH-TIME is not set, you can still
4450 enter a time, and this function will inform the calling routine about
4451 this change. The calling routine may then choose to change the format
4452 used to insert the time stamp into the buffer to include the time."
4453 (require 'parse-time)
4454 (let* ((org-time-stamp-rounding-minutes
4455 (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
4456 (ct (org-current-time))
4457 (default-time
4458 ;; Default time is either today, or, when entering a range,
4459 ;; the range start.
4460 (if (save-excursion
4461 (re-search-backward
4462 (concat org-ts-regexp "--\\=") ; FIXME: exactly two minuses?
4463 (- (point) 20) t))
4464 (apply
4465 'encode-time
4466 (mapcar (lambda(x) (or x 0))
4467 (parse-time-string (match-string 1))))
4468 ct))
4469 (calendar-move-hook nil)
4470 (view-diary-entries-initially nil)
4471 (timestr (format-time-string
4472 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
4473 (prompt (format "YYYY-MM-DD [%s]: " timestr))
4474 ans ans1 ans2
4475 second minute hour day month year tl wday wday1)
4477 (if org-popup-calendar-for-date-prompt
4478 (save-excursion
4479 (save-window-excursion
4480 (calendar)
4481 (calendar-forward-day (- (time-to-days default-time)
4482 (calendar-absolute-from-gregorian
4483 (calendar-current-date))))
4484 (org-eval-in-calendar nil)
4485 (let* ((old-map (current-local-map))
4486 (map (copy-keymap calendar-mode-map))
4487 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
4488 (define-key map (kbd "RET") 'org-calendar-select)
4489 (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
4490 'org-calendar-select-mouse)
4491 (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
4492 'org-calendar-select-mouse)
4493 (define-key minibuffer-local-map [(meta shift left)]
4494 (lambda () (interactive)
4495 (org-eval-in-calendar '(calendar-backward-month 1))))
4496 (define-key minibuffer-local-map [(meta shift right)]
4497 (lambda () (interactive)
4498 (org-eval-in-calendar '(calendar-forward-month 1))))
4499 (define-key minibuffer-local-map [(shift up)]
4500 (lambda () (interactive)
4501 (org-eval-in-calendar '(calendar-backward-week 1))))
4502 (define-key minibuffer-local-map [(shift down)]
4503 (lambda () (interactive)
4504 (org-eval-in-calendar '(calendar-forward-week 1))))
4505 (define-key minibuffer-local-map [(shift left)]
4506 (lambda () (interactive)
4507 (org-eval-in-calendar '(calendar-backward-day 1))))
4508 (define-key minibuffer-local-map [(shift right)]
4509 (lambda () (interactive)
4510 (org-eval-in-calendar '(calendar-forward-day 1))))
4511 (define-key minibuffer-local-map ">"
4512 (lambda () (interactive)
4513 (org-eval-in-calendar '(scroll-calendar-left 1))))
4514 (define-key minibuffer-local-map "<"
4515 (lambda () (interactive)
4516 (org-eval-in-calendar '(scroll-calendar-right 1))))
4517 (unwind-protect
4518 (progn
4519 (use-local-map map)
4520 (setq ans (read-string prompt "" nil nil))
4521 (if (not (string-match "\\S-" ans)) (setq ans nil))
4522 (setq ans (or ans1 ans ans2)))
4523 (use-local-map old-map)))))
4524 ;; Naked prompt only
4525 (setq ans (read-string prompt "" nil timestr)))
4526 (org-detatch-overlay org-date-ovl)
4528 (if (string-match
4529 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
4530 (progn
4531 (setq year (if (match-end 2)
4532 (string-to-number (match-string 2 ans))
4533 (string-to-number (format-time-string "%Y")))
4534 month (string-to-number (match-string 3 ans))
4535 day (string-to-number (match-string 4 ans)))
4536 (if (< year 100) (setq year (+ 2000 year)))
4537 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
4538 t nil ans))))
4539 (setq tl (parse-time-string ans)
4540 year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
4541 month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
4542 day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
4543 hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
4544 minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
4545 second (or (nth 0 tl) 0)
4546 wday (nth 6 tl))
4547 (when (and wday (not (nth 3 tl)))
4548 ;; Weekday was given, but no day, so pick that day in the week
4549 ;; on or after the derived date.
4550 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
4551 (unless (equal wday wday1)
4552 (setq day (+ day (% (- wday wday1 -7) 7)))))
4553 (if (and (boundp 'org-time-was-given)
4554 (nth 2 tl))
4555 (setq org-time-was-given t))
4556 (if (< year 100) (setq year (+ 2000 year)))
4557 (if to-time
4558 (encode-time second minute hour day month year)
4559 (if (or (nth 1 tl) (nth 2 tl))
4560 (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
4561 (format "%04d-%02d-%02d" year month day)))))
4563 (defun org-eval-in-calendar (form)
4564 "Eval FORM in the calendar window and return to current window.
4565 Also, store the cursor date in variable ans2."
4566 (let ((sw (selected-window)))
4567 (select-window (get-buffer-window "*Calendar*"))
4568 (eval form)
4569 (when (calendar-cursor-to-date)
4570 (let* ((date (calendar-cursor-to-date))
4571 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4572 (setq ans2 (format-time-string "%Y-%m-%d" time))))
4573 (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
4574 (select-window sw)))
4576 (defun org-calendar-select ()
4577 "Return to `org-read-date' with the date currently selected.
4578 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
4579 (interactive)
4580 (when (calendar-cursor-to-date)
4581 (let* ((date (calendar-cursor-to-date))
4582 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4583 (setq ans1 (format-time-string "%Y-%m-%d" time)))
4584 (if (active-minibuffer-window) (exit-minibuffer))))
4586 (defun org-calendar-select-mouse (ev)
4587 "Return to `org-read-date' with the date currently selected.
4588 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
4589 (interactive "e")
4590 (mouse-set-point ev)
4591 (when (calendar-cursor-to-date)
4592 (let* ((date (calendar-cursor-to-date))
4593 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4594 (setq ans1 (format-time-string "%Y-%m-%d" time)))
4595 (if (active-minibuffer-window) (exit-minibuffer))))
4597 (defun org-check-deadlines (ndays)
4598 "Check if there are any deadlines due or past due.
4599 A deadline is considered due if it happens within `org-deadline-warning-days'
4600 days from today's date. If the deadline appears in an entry marked DONE,
4601 it is not shown. The prefix arg NDAYS can be used to test that many
4602 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
4603 (interactive "P")
4604 (let* ((org-warn-days
4605 (cond
4606 ((equal ndays '(4)) 100000)
4607 (ndays (prefix-numeric-value ndays))
4608 (t org-deadline-warning-days)))
4609 (case-fold-search nil)
4610 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
4611 (callback
4612 (lambda ()
4613 (and (let ((d1 (time-to-days (current-time)))
4614 (d2 (time-to-days
4615 (org-time-string-to-time (match-string 1)))))
4616 (< (- d2 d1) org-warn-days))
4617 (not (org-entry-is-done-p))))))
4618 (message "%d deadlines past-due or due within %d days"
4619 (org-occur regexp callback)
4620 org-warn-days)))
4622 (defun org-evaluate-time-range (&optional to-buffer)
4623 "Evaluate a time range by computing the difference between start and end.
4624 Normally the result is just printed in the echo area, but with prefix arg
4625 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
4626 If the time range is actually in a table, the result is inserted into the
4627 next column.
4628 For time difference computation, a year is assumed to be exactly 365
4629 days in order to avoid rounding problems."
4630 (interactive "P")
4631 (save-excursion
4632 (unless (org-at-date-range-p)
4633 (goto-char (point-at-bol))
4634 (re-search-forward org-tr-regexp (point-at-eol) t))
4635 (if (not (org-at-date-range-p))
4636 (error "Not at a time-stamp range, and none found in current line")))
4637 (let* ((ts1 (match-string 1))
4638 (ts2 (match-string 2))
4639 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
4640 (match-end (match-end 0))
4641 (time1 (org-time-string-to-time ts1))
4642 (time2 (org-time-string-to-time ts2))
4643 (t1 (time-to-seconds time1))
4644 (t2 (time-to-seconds time2))
4645 (diff (abs (- t2 t1)))
4646 (negative (< (- t2 t1) 0))
4647 ;; (ys (floor (* 365 24 60 60)))
4648 (ds (* 24 60 60))
4649 (hs (* 60 60))
4650 (fy "%dy %dd %02d:%02d")
4651 (fy1 "%dy %dd")
4652 (fd "%dd %02d:%02d")
4653 (fd1 "%dd")
4654 (fh "%02d:%02d")
4655 y d h m align)
4656 (if havetime
4657 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
4659 d (floor (/ diff ds)) diff (mod diff ds)
4660 h (floor (/ diff hs)) diff (mod diff hs)
4661 m (floor (/ diff 60)))
4662 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
4664 d (floor (+ (/ diff ds) 0.5))
4665 h 0 m 0))
4666 (if (not to-buffer)
4667 (message (org-make-tdiff-string y d h m))
4668 (when (org-at-table-p)
4669 (goto-char match-end)
4670 (setq align t)
4671 (and (looking-at " *|") (goto-char (match-end 0))))
4672 (if (looking-at
4673 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
4674 (replace-match ""))
4675 (if negative (insert " -"))
4676 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
4677 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
4678 (insert " " (format fh h m))))
4679 (if align (org-table-align))
4680 (message "Time difference inserted"))))
4682 (defun org-make-tdiff-string (y d h m)
4683 (let ((fmt "")
4684 (l nil))
4685 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
4686 l (push y l)))
4687 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
4688 l (push d l)))
4689 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
4690 l (push h l)))
4691 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
4692 l (push m l)))
4693 (apply 'format fmt (nreverse l))))
4695 (defun org-time-string-to-time (s)
4696 (apply 'encode-time (org-parse-time-string s)))
4698 (defun org-parse-time-string (s &optional nodefault)
4699 "Parse the standard Org-mode time string.
4700 This should be a lot faster than the normal `parse-time-string'.
4701 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
4702 hour and minute fields will be nil if not given."
4703 (if (string-match org-ts-regexp1 s)
4704 (list 0
4705 (if (or (match-beginning 8) (not nodefault))
4706 (string-to-number (or (match-string 8 s) "0")))
4707 (if (or (match-beginning 7) (not nodefault))
4708 (string-to-number (or (match-string 7 s) "0")))
4709 (string-to-number (match-string 4 s))
4710 (string-to-number (match-string 3 s))
4711 (string-to-number (match-string 2 s))
4712 nil nil nil)
4713 (make-list 9 0)))
4715 (defun org-timestamp-up (&optional arg)
4716 "Increase the date item at the cursor by one.
4717 If the cursor is on the year, change the year. If it is on the month or
4718 the day, change that.
4719 With prefix ARG, change by that many units."
4720 (interactive "p")
4721 (org-timestamp-change (prefix-numeric-value arg)))
4723 (defun org-timestamp-down (&optional arg)
4724 "Decrease the date item at the cursor by one.
4725 If the cursor is on the year, change the year. If it is on the month or
4726 the day, change that.
4727 With prefix ARG, change by that many units."
4728 (interactive "p")
4729 (org-timestamp-change (- (prefix-numeric-value arg))))
4731 (defun org-timestamp-up-day (&optional arg)
4732 "Increase the date in the time stamp by one day.
4733 With prefix ARG, change that many days."
4734 (interactive "p")
4735 (if (and (not (org-at-timestamp-p))
4736 (org-on-heading-p))
4737 (org-todo 'up)
4738 (org-timestamp-change (prefix-numeric-value arg) 'day)))
4740 (defun org-timestamp-down-day (&optional arg)
4741 "Decrease the date in the time stamp by one day.
4742 With prefix ARG, change that many days."
4743 (interactive "p")
4744 (if (and (not (org-at-timestamp-p))
4745 (org-on-heading-p))
4746 (org-todo 'down)
4747 (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
4749 (defsubst org-pos-in-match-range (pos n)
4750 (and (match-beginning n)
4751 (<= (match-beginning n) pos)
4752 (>= (match-end n) pos)))
4754 (defun org-at-timestamp-p ()
4755 "Determine if the cursor is in or at a timestamp."
4756 (interactive)
4757 (let* ((tsr org-ts-regexp2)
4758 (pos (point))
4759 (ans (or (looking-at tsr)
4760 (save-excursion
4761 (skip-chars-backward "^<\n\r\t")
4762 (if (> (point) 1) (backward-char 1))
4763 (and (looking-at tsr)
4764 (> (- (match-end 0) pos) -1))))))
4765 (and (boundp 'org-ts-what)
4766 (setq org-ts-what
4767 (cond
4768 ((org-pos-in-match-range pos 2) 'year)
4769 ((org-pos-in-match-range pos 3) 'month)
4770 ((org-pos-in-match-range pos 7) 'hour)
4771 ((org-pos-in-match-range pos 8) 'minute)
4772 ((or (org-pos-in-match-range pos 4)
4773 (org-pos-in-match-range pos 5)) 'day)
4774 (t 'day))))
4775 ans))
4777 (defun org-timestamp-change (n &optional what)
4778 "Change the date in the time stamp at point.
4779 The date will be changed by N times WHAT. WHAT can be `day', `month',
4780 `year', `minute', `second'. If WHAT is not given, the cursor position
4781 in the timestamp determines what will be changed."
4782 (let ((fmt (car org-time-stamp-formats))
4783 org-ts-what
4784 (pos (point))
4785 ts time time0)
4786 (if (not (org-at-timestamp-p))
4787 (error "Not at a timestamp"))
4788 (setq org-ts-what (or what org-ts-what))
4789 (setq fmt (if (<= (abs (- (cdr org-ts-lengths)
4790 (- (match-end 0) (match-beginning 0))))
4792 (cdr org-time-stamp-formats)
4793 (car org-time-stamp-formats)))
4794 (setq ts (match-string 0))
4795 (replace-match "")
4796 (setq time0 (org-parse-time-string ts))
4797 (setq time
4798 (apply 'encode-time
4799 (append
4800 (list (or (car time0) 0))
4801 (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
4802 (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
4803 (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
4804 (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
4805 (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
4806 (nthcdr 6 time0))))
4807 (if (eq what 'calendar)
4808 (let ((cal-date
4809 (save-excursion
4810 (save-match-data
4811 (set-buffer "*Calendar*")
4812 (calendar-cursor-to-date)))))
4813 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
4814 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
4815 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
4816 (setcar time0 (or (car time0) 0))
4817 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
4818 (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
4819 (setq time (apply 'encode-time time0))))
4820 (insert (setq org-last-changed-timestamp (format-time-string fmt time)))
4821 (goto-char pos)
4822 ;; Try to recenter the calendar window, if any
4823 (if (and org-calendar-follow-timestamp-change
4824 (get-buffer-window "*Calendar*" t)
4825 (memq org-ts-what '(day month year)))
4826 (org-recenter-calendar (time-to-days time)))))
4828 (defun org-recenter-calendar (date)
4829 "If the calendar is visible, recenter it to DATE."
4830 (let* ((win (selected-window))
4831 (cwin (get-buffer-window "*Calendar*" t))
4832 (calendar-move-hook nil))
4833 (when cwin
4834 (select-window cwin)
4835 (calendar-goto-date (if (listp date) date
4836 (calendar-gregorian-from-absolute date)))
4837 (select-window win))))
4839 (defun org-goto-calendar (&optional arg)
4840 "Go to the Emacs calendar at the current date.
4841 If there is a time stamp in the current line, go to that date.
4842 A prefix ARG can be used to force the current date."
4843 (interactive "P")
4844 (let ((tsr org-ts-regexp) diff
4845 (calendar-move-hook nil)
4846 (view-diary-entries-initially nil))
4847 (if (or (org-at-timestamp-p)
4848 (save-excursion
4849 (beginning-of-line 1)
4850 (looking-at (concat ".*" tsr))))
4851 (let ((d1 (time-to-days (current-time)))
4852 (d2 (time-to-days
4853 (org-time-string-to-time (match-string 1)))))
4854 (setq diff (- d2 d1))))
4855 (calendar)
4856 (calendar-goto-today)
4857 (if (and diff (not arg)) (calendar-forward-day diff))))
4859 (defun org-date-from-calendar ()
4860 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
4861 If there is already a time stamp at the cursor position, update it."
4862 (interactive)
4863 (org-timestamp-change 0 'calendar))
4865 ;;; Agenda, and Diary Integration
4867 ;;; Define the mode
4869 (defvar org-agenda-mode-map (make-sparse-keymap)
4870 "Keymap for `org-agenda-mode'.")
4872 (defvar org-agenda-menu) ; defined later in this file.
4873 (defvar org-agenda-follow-mode nil)
4874 (defvar org-agenda-show-log nil)
4875 (defvar org-agenda-buffer-name "*Org Agenda*")
4876 (defvar org-agenda-redo-command nil)
4877 (defvar org-agenda-mode-hook nil)
4878 (defvar org-agenda-type nil)
4879 (defvar org-agenda-force-single-file nil)
4881 ;;;###autoload
4882 (defun org-agenda-mode ()
4883 "Mode for time-sorted view on action items in Org-mode files.
4885 The following commands are available:
4887 \\{org-agenda-mode-map}"
4888 (interactive)
4889 (kill-all-local-variables)
4890 (setq major-mode 'org-agenda-mode)
4891 (setq mode-name "Org-Agenda")
4892 (use-local-map org-agenda-mode-map)
4893 (easy-menu-add org-agenda-menu)
4894 (if org-startup-truncated (setq truncate-lines t))
4895 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
4896 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
4897 (unless org-agenda-keep-modes
4898 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
4899 org-agenda-show-log nil))
4900 (easy-menu-change
4901 '("Agenda") "Agenda Files"
4902 (append
4903 (list
4904 (vector
4905 (if (get 'org-agenda-files 'org-restrict)
4906 "Restricted to single file"
4907 "Edit File List")
4908 '(org-edit-agenda-file-list)
4909 (not (get 'org-agenda-files 'org-restrict)))
4910 "--")
4911 (mapcar 'org-file-menu-entry (org-agenda-files))))
4912 (org-agenda-set-mode-name)
4913 (apply
4914 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
4915 (list 'org-agenda-mode-hook)))
4917 (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
4918 (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
4919 (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
4920 (define-key org-agenda-mode-map " " 'org-agenda-show)
4921 (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
4922 (define-key org-agenda-mode-map "o" 'delete-other-windows)
4923 (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
4924 (define-key org-agenda-mode-map "t" 'org-agenda-todo)
4925 (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
4926 (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
4927 (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
4928 (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
4929 (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
4930 (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
4931 (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
4932 (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
4934 (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
4935 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
4936 (while l (define-key org-agenda-mode-map
4937 (int-to-string (pop l)) 'digit-argument)))
4939 (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
4940 (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
4941 (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
4942 (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
4943 (define-key org-agenda-mode-map "r" 'org-agenda-redo)
4944 (define-key org-agenda-mode-map "q" 'org-agenda-quit)
4945 (define-key org-agenda-mode-map "x" 'org-agenda-exit)
4946 (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
4947 (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
4948 (define-key org-agenda-mode-map "n" 'next-line)
4949 (define-key org-agenda-mode-map "p" 'previous-line)
4950 (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
4951 (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
4952 (define-key org-agenda-mode-map "," 'org-agenda-priority)
4953 (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
4954 (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
4955 (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
4956 (eval-after-load "calendar"
4957 '(define-key calendar-mode-map org-calendar-to-agenda-key
4958 'org-calendar-goto-agenda))
4959 (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
4960 (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
4961 (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
4962 (define-key org-agenda-mode-map "s" 'org-agenda-sunrise-sunset)
4963 (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
4964 (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
4965 (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
4966 (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
4967 (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
4968 (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
4969 (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
4970 (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
4971 (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
4972 (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
4973 (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
4974 (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
4975 (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
4976 "Local keymap for agenda entries from Org-mode.")
4978 (define-key org-agenda-keymap
4979 (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
4980 (define-key org-agenda-keymap
4981 (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
4982 (when org-agenda-mouse-1-follows-link
4983 (define-key org-agenda-keymap [follow-link] 'mouse-face))
4984 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
4985 '("Agenda"
4986 ("Agenda Files")
4987 "--"
4988 ["Show" org-agenda-show t]
4989 ["Go To (other window)" org-agenda-goto t]
4990 ["Go To (one window)" org-agenda-switch-to t]
4991 ["Follow Mode" org-agenda-follow-mode
4992 :style toggle :selected org-agenda-follow-mode :active t]
4993 "--"
4994 ["Cycle TODO" org-agenda-todo t]
4995 ("Tags"
4996 ["Show all Tags" org-agenda-show-tags t]
4997 ["Set Tags" org-agenda-set-tags t])
4998 ("Reschedule"
4999 ["Reschedule +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
5000 ["Reschedule -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
5001 "--"
5002 ["Reschedule to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
5003 ("Priority"
5004 ["Set Priority" org-agenda-priority t]
5005 ["Increase Priority" org-agenda-priority-up t]
5006 ["Decrease Priority" org-agenda-priority-down t]
5007 ["Show Priority" org-agenda-show-priority t])
5008 "--"
5009 ;; ["New agenda command" org-agenda t]
5010 ["Rebuild buffer" org-agenda-redo t]
5011 "--"
5012 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
5013 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
5014 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
5015 "--"
5016 ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
5017 :style radio :selected (equal org-agenda-ndays 1)]
5018 ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
5019 :style radio :selected (equal org-agenda-ndays 7)]
5020 "--"
5021 ["Show Logbook entries" org-agenda-log-mode
5022 :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
5023 ["Include Diary" org-agenda-toggle-diary
5024 :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
5025 ["Use Time Grid" org-agenda-toggle-time-grid
5026 :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)]
5027 "--"
5028 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
5029 ("Calendar Commands"
5030 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
5031 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
5032 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
5033 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
5034 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)])
5035 ["Create iCalendar file" org-export-icalendar-combine-agenda-files t]
5036 "--"
5037 ["Quit" org-agenda-quit t]
5038 ["Exit and Release Buffers" org-agenda-exit t]
5041 ;;;###autoload
5042 (defun org-agenda (arg)
5043 "Dispatch agenda commands to collect entries to the agenda buffer.
5044 Prompts for a character to select a command. Any prefix arg will be passed
5045 on to the selected command. The default selections are:
5047 a Call `org-agenda' to display the agenda for the current day or week.
5048 t Call `org-todo-list' to display the global todo list.
5049 T Call `org-todo-list' to display the global todo list, select only
5050 entries with a specific TODO keyword (the user gets a prompt).
5051 m Call `org-tags-view' to display headlines with tags matching
5052 a condition (the user is prompted for the condition).
5053 M Like `m', but select only TODO entries, no ordinary headlines.
5055 More commands can be added by configuring the variable
5056 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
5057 searches can be pre-defined in this way.
5059 If the current buffer is in Org-mode and visiting a file, you can also
5060 first press `1' to indicate that the agenda should be temporarily (until the
5061 next use of \\[org-agenda]) restricted to the current file."
5062 (interactive "P")
5063 (catch 'exit
5064 (let ((restrict-ok (and buffer-file-name (eq major-mode 'org-mode)))
5065 (custom org-agenda-custom-commands)
5066 c entry key type string)
5067 (put 'org-agenda-files 'org-restrict nil)
5068 (save-window-excursion
5069 (delete-other-windows)
5070 (switch-to-buffer-other-window " *Agenda Commands*")
5071 (erase-buffer)
5072 (insert
5073 "Press key for an agenda command:
5074 --------------------------------
5075 a Agenda for current week or day
5076 t List of all TODO entries T Entries with special TODO kwd
5077 m Match a TAGS query M Like m, but only TODO entries
5078 C Configure your own agenda commands")
5079 (while (setq entry (pop custom))
5080 (setq key (car entry) type (nth 1 entry) string (nth 2 entry))
5081 (insert (format "\n%-4s%-14s: %s"
5083 (cond
5084 ((eq type 'tags) "Tags query")
5085 ((eq type 'todo) "TODO keyword")
5086 ((eq type 'tags-tree) "Tags tree")
5087 ((eq type 'todo-tree) "TODO kwd tree")
5088 ((eq type 'occur-tree) "Occur tree")
5089 (t "???"))
5090 (org-add-props string nil 'face 'org-warning))))
5091 (goto-char (point-min))
5092 (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
5093 (message "Press key for agenda command%s"
5094 (if restrict-ok ", or [1] to restrict to current file" ""))
5095 (setq c (read-char-exclusive))
5096 (message "")
5097 (when (equal c ?1)
5098 (if restrict-ok
5099 (put 'org-agenda-files 'org-restrict (list buffer-file-name))
5100 (error "Cannot restrict agenda to current buffer"))
5101 (message "Press key for agenda command%s"
5102 (if restrict-ok " (restricted to current file)" ""))
5103 (setq c (read-char-exclusive))
5104 (message "")))
5105 (require 'calendar) ; FIXME: can we avoid this for some commands?
5106 ;; For example the todo list should not need it (but does...)
5107 (cond
5108 ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
5109 ((equal c ?a) (call-interactively 'org-agenda-list))
5110 ((equal c ?t) (call-interactively 'org-todo-list))
5111 ((equal c ?T)
5112 (setq current-prefix-arg (or arg '(4)))
5113 (call-interactively 'org-todo-list))
5114 ((equal c ?m) (call-interactively 'org-tags-view))
5115 ((equal c ?M)
5116 (setq current-prefix-arg (or arg '(4)))
5117 (call-interactively 'org-tags-view))
5118 ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
5119 (setq type (nth 1 entry) string (nth 2 entry))
5120 (cond
5121 ((eq type 'tags)
5122 (org-tags-view current-prefix-arg string))
5123 ((eq type 'tags-todo)
5124 (org-tags-view '(4) string))
5125 ((eq type 'todo)
5126 (org-todo-list string))
5127 ((eq type 'tags-tree)
5128 (org-check-for-org-mode)
5129 (org-tags-sparse-tree current-prefix-arg string))
5130 ((eq type 'todo-tree)
5131 (org-check-for-org-mode)
5132 (org-occur (concat "^" outline-regexp "[ \t]*"
5133 (regexp-quote string) "\\>")))
5134 ((eq type 'occur-tree)
5135 (org-check-for-org-mode)
5136 (org-occur string))
5137 (t (error "Invalid custom agenda command type %s" type))))
5138 (t (error "Invalid key"))))))
5140 (defun org-check-for-org-mode ()
5141 "Make sure current buffer is in org-mode. Error if not."
5142 (or (eq major-mode 'org-mode)
5143 (error "Cannot execute org-mode agenda command on buffer in %s."
5144 major-mode)))
5146 (defun org-fit-agenda-window ()
5147 "Fit the window to the buffer size."
5148 (and org-fit-agenda-window
5149 (fboundp 'fit-window-to-buffer)
5150 (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
5151 (/ (frame-height) 2))))
5153 (defun org-agenda-files (&optional unrestricted)
5154 "Get the list of agenda files.
5155 Optional UNRESTRICTED means return the full list even if a restriction
5156 is currently in place."
5157 (cond
5158 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
5159 ((stringp org-agenda-files) (org-read-agenda-file-list))
5160 ((listp org-agenda-files) org-agenda-files)
5161 (t (error "Invalid value of `org-agenda-files'"))))
5163 (defvar org-window-configuration)
5165 (defun org-edit-agenda-file-list ()
5166 "Edit the list of agenda files.
5167 Depending on setup, this either uses customize to edit the variable
5168 `org-agenda-files', or it visits the file that is holding the list. In the
5169 latter case, the buffer is set up in a way that saving it automatically kills
5170 the buffer and restores the previous window configuration."
5171 (interactive)
5172 (if (stringp org-agenda-files)
5173 (let ((cw (current-window-configuration)))
5174 (find-file org-agenda-files)
5175 (set (make-local-variable 'org-window-configuration) cw)
5176 (org-add-hook 'after-save-hook
5177 (lambda ()
5178 (set-window-configuration
5179 (prog1 org-window-configuration
5180 (kill-buffer (current-buffer))))
5181 (org-install-agenda-files-menu)
5182 (message "New agenda file list installed"))
5183 nil 'local)
5184 (message (substitute-command-keys
5185 "Edit list and finish with \\[save-buffer]")))
5186 (customize-variable 'org-agenda-files)))
5188 (defun org-store-new-agenda-file-list (list)
5189 "Set new value for the agenda file list and save it correcly."
5190 (if (stringp org-agenda-files)
5191 (let ((f org-agenda-files) b)
5192 (while (setq b (find-buffer-visiting f)) (kill-buffer b))
5193 (with-temp-file f
5194 (insert (mapconcat 'identity list "\n") "\n")))
5195 (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
5196 (setq org-agenda-files list)
5197 (customize-save-variable 'org-agenda-files org-agenda-files))))
5199 (defun org-read-agenda-file-list ()
5200 "Read the list of agenda files from a file."
5201 (when (stringp org-agenda-files)
5202 (with-temp-buffer
5203 (insert-file-contents org-agenda-files)
5204 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
5206 (defvar org-agenda-markers nil
5207 "List of all currently active markers created by `org-agenda'.")
5208 (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
5209 "Creation time of the last agenda marker.")
5211 (defun org-agenda-new-marker (&optional pos)
5212 "Return a new agenda marker.
5213 Org-mode keeps a list of these markers and resets them when they are
5214 no longer in use."
5215 (let ((m (copy-marker (or pos (point)))))
5216 (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
5217 (push m org-agenda-markers)
5220 (defun org-agenda-maybe-reset-markers (&optional force)
5221 "Reset markers created by `org-agenda'. But only if they are old enough."
5222 (if (or force
5223 (> (- (time-to-seconds (current-time))
5224 org-agenda-last-marker-time)
5226 (while org-agenda-markers
5227 (move-marker (pop org-agenda-markers) nil))))
5229 (defvar org-agenda-new-buffers nil
5230 "Buffers created to visit agenda files.")
5232 (defun org-get-agenda-file-buffer (file)
5233 "Get a buffer visiting FILE. If the buffer needs to be created, add
5234 it to the list of buffers which might be released later."
5235 (let ((buf (find-buffer-visiting file)))
5236 (if buf
5237 buf ; just return it
5238 ;; Make a new buffer and remember it
5239 (setq buf (find-file-noselect file))
5240 (if buf (push buf org-agenda-new-buffers))
5241 buf)))
5243 (defun org-release-buffers (blist)
5244 "Release all buffers in list, asking the user for confirmation when needed.
5245 When a buffer is unmodified, it is just killed. When modified, it is saved
5246 \(if the user agrees) and then killed."
5247 (let (buf file)
5248 (while (setq buf (pop blist))
5249 (setq file (buffer-file-name buf))
5250 (when (and (buffer-modified-p buf)
5251 file
5252 (y-or-n-p (format "Save file %s? " file)))
5253 (with-current-buffer buf (save-buffer)))
5254 (kill-buffer buf))))
5256 (defvar org-respect-restriction nil) ; Dynamically-scoped param.
5258 (defun org-timeline (&optional include-all keep-modes)
5259 "Show a time-sorted view of the entries in the current org file.
5260 Only entries with a time stamp of today or later will be listed. With
5261 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
5262 under the current date.
5263 If the buffer contains an active region, only check the region for
5264 dates."
5265 (interactive "P")
5266 (require 'calendar)
5267 (org-agenda-maybe-reset-markers 'force)
5268 (org-compile-prefix-format org-timeline-prefix-format)
5269 (let* ((dopast t)
5270 (dotodo include-all)
5271 (doclosed org-agenda-show-log)
5272 (org-agenda-keep-modes keep-modes)
5273 (entry buffer-file-name)
5274 (org-agenda-files (list buffer-file-name))
5275 (date (calendar-current-date))
5276 (win (selected-window))
5277 (pos1 (point))
5278 (beg (if (org-region-active-p) (region-beginning) (point-min)))
5279 (end (if (org-region-active-p) (region-end) (point-max)))
5280 (day-numbers (org-get-all-dates beg end 'no-ranges
5281 t doclosed)) ; always include today
5282 (today (time-to-days (current-time)))
5283 (org-respect-restriction t)
5284 (past t)
5285 args
5286 s e rtn d)
5287 (setq org-agenda-redo-command
5288 (list 'progn
5289 (list 'switch-to-buffer-other-window (current-buffer))
5290 (list 'org-timeline (list 'quote include-all) t)))
5291 (if (not dopast)
5292 ;; Remove past dates from the list of dates.
5293 (setq day-numbers (delq nil (mapcar (lambda(x)
5294 (if (>= x today) x nil))
5295 day-numbers))))
5296 (switch-to-buffer-other-window
5297 (get-buffer-create org-agenda-buffer-name))
5298 (setq buffer-read-only nil)
5299 (erase-buffer)
5300 (org-agenda-mode) (setq buffer-read-only nil)
5301 (set (make-local-variable 'org-agenda-type) 'timeline)
5302 (if doclosed (push :closed args))
5303 (push :timestamp args)
5304 (if dotodo (push :todo args))
5305 (while (setq d (pop day-numbers))
5306 (if (and (>= d today)
5307 dopast
5308 past)
5309 (progn
5310 (setq past nil)
5311 (insert (make-string 79 ?-) "\n")))
5312 (setq date (calendar-gregorian-from-absolute d))
5313 (setq s (point))
5314 (setq rtn (apply 'org-agenda-get-day-entries
5315 entry date args))
5316 (if (or rtn (equal d today))
5317 (progn
5318 (insert (calendar-day-name date) " "
5319 (number-to-string (extract-calendar-day date)) " "
5320 (calendar-month-name (extract-calendar-month date)) " "
5321 (number-to-string (extract-calendar-year date)) "\n")
5322 (put-text-property s (1- (point)) 'face
5323 'org-level-3)
5324 (if (equal d today)
5325 (put-text-property s (1- (point)) 'org-today t))
5326 (insert (org-finalize-agenda-entries rtn) "\n")
5327 (put-text-property s (1- (point)) 'day d))))
5328 (goto-char (point-min))
5329 (setq buffer-read-only t)
5330 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
5331 (point-min)))
5332 (when (not org-select-timeline-window)
5333 (select-window win)
5334 (goto-char pos1))))
5336 ;;;###autoload
5337 (defun org-agenda-list (&optional include-all start-day ndays keep-modes)
5338 "Produce a weekly view from all files in variable `org-agenda-files'.
5339 The view will be for the current week, but from the overview buffer you
5340 will be able to go to other weeks.
5341 With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
5342 also be shown, under the current date.
5343 With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
5344 on the days are also shown. See the variable `org-log-done' for how
5345 to turn on logging.
5346 START-DAY defaults to TODAY, or to the most recent match for the weekday
5347 given in `org-agenda-start-on-weekday'.
5348 NDAYS defaults to `org-agenda-ndays'."
5349 (interactive "P")
5350 (org-agenda-maybe-reset-markers 'force)
5351 (org-compile-prefix-format org-agenda-prefix-format)
5352 (require 'calendar)
5353 (let* ((org-agenda-start-on-weekday
5354 (if (or (equal ndays 1)
5355 (and (null ndays) (equal 1 org-agenda-ndays)))
5356 nil org-agenda-start-on-weekday))
5357 (org-agenda-keep-modes keep-modes)
5358 (thefiles (org-agenda-files))
5359 (files thefiles)
5360 (win (selected-window))
5361 (today (time-to-days (current-time)))
5362 (sd (or start-day today))
5363 (start (if (or (null org-agenda-start-on-weekday)
5364 (< org-agenda-ndays 7))
5366 (let* ((nt (calendar-day-of-week
5367 (calendar-gregorian-from-absolute sd)))
5368 (n1 org-agenda-start-on-weekday)
5369 (d (- nt n1)))
5370 (- sd (+ (if (< d 0) 7 0) d)))))
5371 (day-numbers (list start))
5372 (inhibit-redisplay t)
5373 s e rtn rtnall file date d start-pos end-pos todayp nd)
5374 (setq org-agenda-redo-command
5375 (list 'org-agenda-list (list 'quote include-all) start-day ndays t))
5376 ;; Make the list of days
5377 (setq ndays (or ndays org-agenda-ndays)
5378 nd ndays)
5379 (while (> ndays 1)
5380 (push (1+ (car day-numbers)) day-numbers)
5381 (setq ndays (1- ndays)))
5382 (setq day-numbers (nreverse day-numbers))
5383 (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
5384 (progn
5385 (delete-other-windows)
5386 (switch-to-buffer-other-window
5387 (get-buffer-create org-agenda-buffer-name))))
5388 (setq buffer-read-only nil)
5389 (erase-buffer)
5390 (org-agenda-mode) (setq buffer-read-only nil)
5391 (set (make-local-variable 'org-agenda-type) 'agenda)
5392 (set (make-local-variable 'starting-day) (car day-numbers))
5393 (set (make-local-variable 'include-all-loc) include-all)
5394 (when (and (or include-all org-agenda-include-all-todo)
5395 (member today day-numbers))
5396 (setq files thefiles
5397 rtnall nil)
5398 (while (setq file (pop files))
5399 (catch 'nextfile
5400 (org-check-agenda-file file)
5401 (setq date (calendar-gregorian-from-absolute today)
5402 rtn (org-agenda-get-day-entries
5403 file date :todo))
5404 (setq rtnall (append rtnall rtn))))
5405 (when rtnall
5406 (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
5407 (add-text-properties (point-min) (1- (point))
5408 (list 'face 'org-level-3))
5409 (insert (org-finalize-agenda-entries rtnall) "\n")))
5410 (while (setq d (pop day-numbers))
5411 (setq date (calendar-gregorian-from-absolute d)
5412 s (point))
5413 (if (or (setq todayp (= d today))
5414 (and (not start-pos) (= d sd)))
5415 (setq start-pos (point))
5416 (if (and start-pos (not end-pos))
5417 (setq end-pos (point))))
5418 (setq files thefiles
5419 rtnall nil)
5420 (while (setq file (pop files))
5421 (catch 'nextfile
5422 (org-check-agenda-file file)
5423 (if org-agenda-show-log
5424 (setq rtn (org-agenda-get-day-entries
5425 file date
5426 :deadline :scheduled :timestamp :closed))
5427 (setq rtn (org-agenda-get-day-entries
5428 file date
5429 :deadline :scheduled :timestamp)))
5430 (setq rtnall (append rtnall rtn))))
5431 (if org-agenda-include-diary
5432 (progn
5433 (require 'diary-lib)
5434 (setq rtn (org-get-entries-from-diary date))
5435 (setq rtnall (append rtnall rtn))))
5436 (if (or rtnall org-agenda-show-all-dates)
5437 (progn
5438 (insert (format "%-9s %2d %s %4d\n"
5439 (calendar-day-name date)
5440 (extract-calendar-day date)
5441 (calendar-month-name (extract-calendar-month date))
5442 (extract-calendar-year date)))
5443 (put-text-property s (1- (point)) 'face
5444 'org-level-3)
5445 (if rtnall (insert
5446 (org-finalize-agenda-entries
5447 (org-agenda-add-time-grid-maybe
5448 rtnall nd todayp))
5449 "\n"))
5450 (put-text-property s (1- (point)) 'day d))))
5451 (goto-char (point-min))
5452 (setq buffer-read-only t)
5453 (org-fit-agenda-window)
5454 (unless (and (pos-visible-in-window-p (point-min))
5455 (pos-visible-in-window-p (point-max)))
5456 (goto-char (1- (point-max)))
5457 (recenter -1)
5458 (if (not (pos-visible-in-window-p (or start-pos 1)))
5459 (progn
5460 (goto-char (or start-pos 1))
5461 (recenter 1))))
5462 (goto-char (or start-pos 1))
5463 (if (not org-select-agenda-window) (select-window win))
5464 (message "")))
5466 (defvar org-select-this-todo-keyword nil)
5468 ;;;###autoload
5469 (defun org-todo-list (arg &optional keep-modes)
5470 "Show all TODO entries from all agenda file in a single list.
5471 The prefix arg can be used to select a specific TODO keyword and limit
5472 the list to these. When using \\[universal-argument], you will be prompted
5473 for a keyword. A numeric prefix directly selects the Nth keyword in
5474 `org-todo-keywords'."
5475 (interactive "P")
5476 (org-agenda-maybe-reset-markers 'force)
5477 (org-compile-prefix-format org-agenda-prefix-format)
5478 (let* ((org-agenda-keep-modes keep-modes)
5479 (today (time-to-days (current-time)))
5480 (date (calendar-gregorian-from-absolute today))
5481 (win (selected-window))
5482 (kwds org-todo-keywords)
5483 (completion-ignore-case t)
5484 (org-select-this-todo-keyword
5485 (if (stringp arg) arg
5486 (and arg (integerp arg) (> arg 0)
5487 (nth (1- arg) org-todo-keywords))))
5488 rtn rtnall files file pos)
5489 (when (equal arg '(4))
5490 (setq org-select-this-todo-keyword
5491 (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
5492 nil t)))
5493 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
5494 (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
5495 (progn
5496 (delete-other-windows)
5497 (switch-to-buffer-other-window
5498 (get-buffer-create org-agenda-buffer-name))))
5499 (setq buffer-read-only nil)
5500 (erase-buffer)
5501 (org-agenda-mode) (setq buffer-read-only nil)
5502 (set (make-local-variable 'org-agenda-type) 'todo)
5503 (set (make-local-variable 'last-arg) arg)
5504 (set (make-local-variable 'org-todo-keywords) kwds)
5505 (set (make-local-variable 'org-agenda-redo-command)
5506 '(org-todo-list (or current-prefix-arg last-arg) t))
5507 (setq files (org-agenda-files)
5508 rtnall nil)
5509 (while (setq file (pop files))
5510 (catch 'nextfile
5511 (org-check-agenda-file file)
5512 (setq rtn (org-agenda-get-day-entries file date :todo))
5513 (setq rtnall (append rtnall rtn))))
5514 (insert "Global list of TODO items of type: ")
5515 (add-text-properties (point-min) (1- (point))
5516 (list 'face 'org-level-3))
5517 (setq pos (point))
5518 (insert (or org-select-this-todo-keyword "ALL") "\n")
5519 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
5520 (setq pos (point))
5521 (insert
5522 "Available with `N r': (0)ALL "
5523 (let ((n 0))
5524 (mapconcat (lambda (x)
5525 (format "(%d)%s" (setq n (1+ n)) x))
5526 org-todo-keywords " "))
5527 "\n")
5528 (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
5529 (when rtnall
5530 (insert (org-finalize-agenda-entries rtnall) "\n"))
5531 (goto-char (point-min))
5532 (setq buffer-read-only t)
5533 (org-fit-agenda-window)
5534 (if (not org-select-agenda-window) (select-window win))))
5536 (defun org-check-agenda-file (file)
5537 "Make sure FILE exists. If not, ask user what to do."
5538 (when (not (file-exists-p file))
5539 (message "non-existent file %s. [R]emove from list or [A]bort?"
5540 (abbreviate-file-name file))
5541 (let ((r (downcase (read-char-exclusive))))
5542 (cond
5543 ((equal r ?r)
5544 (org-remove-file file)
5545 (throw 'nextfile t))
5546 (t (error "Abort"))))))
5548 (defun org-agenda-check-type (error &rest types)
5549 "Check if agenda buffer is of allowed type.
5550 If ERROR is non-nil, throw an error, otherwise just return nil."
5551 (if (memq org-agenda-type types)
5553 (if error
5554 (error "Now allowed in %s-type agenda buffers" org-agenda-type)
5555 nil)))
5557 (defun org-agenda-quit ()
5558 "Exit agenda by removing the window or the buffer."
5559 (interactive)
5560 (let ((buf (current-buffer)))
5561 (if (not (one-window-p)) (delete-window))
5562 (kill-buffer buf)
5563 (org-agenda-maybe-reset-markers 'force)))
5565 (defun org-agenda-exit ()
5566 "Exit agenda by removing the window or the buffer.
5567 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
5568 Org-mode buffers visited directly by the user will not be touched."
5569 (interactive)
5570 (org-release-buffers org-agenda-new-buffers)
5571 (setq org-agenda-new-buffers nil)
5572 (org-agenda-quit))
5574 (defun org-agenda-redo ()
5575 "Rebuild Agenda.
5576 When this is the global TODO list, a prefix argument will be interpreted."
5577 (interactive)
5578 (message "Rebuilding agenda buffer...")
5579 (eval org-agenda-redo-command)
5580 (message "Rebuilding agenda buffer...done"))
5582 (defun org-agenda-goto-today ()
5583 "Go to today."
5584 (interactive)
5585 (org-agenda-check-type t 'timeline 'agenda)
5586 (if (boundp 'starting-day)
5587 (let ((cmd (car org-agenda-redo-command))
5588 (iall (nth 1 org-agenda-redo-command))
5589 (nday (nth 3 org-agenda-redo-command))
5590 (keep (nth 4 org-agenda-redo-command)))
5591 (eval (list cmd iall nil nday keep)))
5592 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
5593 (point-min)))))
5595 (defun org-agenda-later (arg)
5596 "Go forward in time by `org-agenda-ndays' days.
5597 With prefix ARG, go forward that many times `org-agenda-ndays'."
5598 (interactive "p")
5599 (org-agenda-check-type t 'agenda)
5600 (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
5601 (+ starting-day (* arg org-agenda-ndays)) nil t))
5603 (defun org-agenda-earlier (arg)
5604 "Go back in time by `org-agenda-ndays' days.
5605 With prefix ARG, go back that many times `org-agenda-ndays'."
5606 (interactive "p")
5607 (org-agenda-check-type t 'agenda)
5608 (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
5609 (- starting-day (* arg org-agenda-ndays)) nil t))
5611 (defun org-agenda-week-view ()
5612 "Switch to weekly view for agenda."
5613 (interactive)
5614 (org-agenda-check-type t 'agenda)
5615 (setq org-agenda-ndays 7)
5616 (org-agenda-list include-all-loc
5617 (or (get-text-property (point) 'day)
5618 starting-day)
5619 nil t)
5620 (org-agenda-set-mode-name)
5621 (message "Switched to week view"))
5623 (defun org-agenda-day-view ()
5624 "Switch to daily view for agenda."
5625 (interactive)
5626 (org-agenda-check-type t 'agenda)
5627 (setq org-agenda-ndays 1)
5628 (org-agenda-list include-all-loc
5629 (or (get-text-property (point) 'day)
5630 starting-day)
5631 nil t)
5632 (org-agenda-set-mode-name)
5633 (message "Switched to day view"))
5635 (defun org-agenda-next-date-line (&optional arg)
5636 "Jump to the next line indicating a date in agenda buffer."
5637 (interactive "p")
5638 (org-agenda-check-type t 'agenda 'timeline)
5639 (beginning-of-line 1)
5640 (if (looking-at "^\\S-") (forward-char 1))
5641 (if (not (re-search-forward "^\\S-" nil t arg))
5642 (progn
5643 (backward-char 1)
5644 (error "No next date after this line in this buffer")))
5645 (goto-char (match-beginning 0)))
5647 (defun org-agenda-previous-date-line (&optional arg)
5648 "Jump to the previous line indicating a date in agenda buffer."
5649 (interactive "p")
5650 (org-agenda-check-type t 'agenda 'timeline)
5651 (beginning-of-line 1)
5652 (if (not (re-search-backward "^\\S-" nil t arg))
5653 (error "No previous date before this line in this buffer")))
5655 ;; Initialize the highlight
5656 (defvar org-hl (org-make-overlay 1 1))
5657 (org-overlay-put org-hl 'face 'highlight)
5659 (defun org-highlight (begin end &optional buffer)
5660 "Highlight a region with overlay."
5661 (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
5662 org-hl begin end (or buffer (current-buffer))))
5664 (defun org-unhighlight ()
5665 "Detach overlay INDEX."
5666 (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
5669 (defun org-agenda-follow-mode ()
5670 "Toggle follow mode in an agenda buffer."
5671 (interactive)
5672 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
5673 (org-agenda-set-mode-name)
5674 (message "Follow mode is %s"
5675 (if org-agenda-follow-mode "on" "off")))
5677 (defun org-agenda-log-mode ()
5678 "Toggle log mode in an agenda buffer."
5679 (interactive)
5680 (org-agenda-check-type t 'agenda 'timeline)
5681 (setq org-agenda-show-log (not org-agenda-show-log))
5682 (org-agenda-set-mode-name)
5683 (org-agenda-redo)
5684 (message "Log mode is %s"
5685 (if org-agenda-show-log "on" "off")))
5687 (defun org-agenda-toggle-diary ()
5688 "Toggle diary inclusion in an agenda buffer."
5689 (interactive)
5690 (org-agenda-check-type t 'agenda)
5691 (setq org-agenda-include-diary (not org-agenda-include-diary))
5692 (org-agenda-redo)
5693 (org-agenda-set-mode-name)
5694 (message "Diary inclusion turned %s"
5695 (if org-agenda-include-diary "on" "off")))
5697 (defun org-agenda-toggle-time-grid ()
5698 "Toggle time grid in an agenda buffer."
5699 (interactive)
5700 (org-agenda-check-type t 'agenda)
5701 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
5702 (org-agenda-redo)
5703 (org-agenda-set-mode-name)
5704 (message "Time-grid turned %s"
5705 (if org-agenda-use-time-grid "on" "off")))
5707 (defun org-agenda-set-mode-name ()
5708 "Set the mode name to indicate all the small mode settings."
5709 (setq mode-name
5710 (concat "Org-Agenda"
5711 (if (equal org-agenda-ndays 1) " Day" "")
5712 (if (equal org-agenda-ndays 7) " Week" "")
5713 (if org-agenda-follow-mode " Follow" "")
5714 (if org-agenda-include-diary " Diary" "")
5715 (if org-agenda-use-time-grid " Grid" "")
5716 (if org-agenda-show-log " Log" "")))
5717 (force-mode-line-update))
5719 (defun org-agenda-post-command-hook ()
5720 (and (eolp) (not (bolp)) (backward-char 1))
5721 (if (and org-agenda-follow-mode
5722 (get-text-property (point) 'org-marker))
5723 (org-agenda-show)))
5725 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5727 (defun org-get-entries-from-diary (date)
5728 "Get the (Emacs Calendar) diary entries for DATE."
5729 (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
5730 (diary-display-hook '(fancy-diary-display))
5731 (list-diary-entries-hook
5732 (cons 'org-diary-default-entry list-diary-entries-hook))
5733 (diary-file-name-prefix-function nil) ; turn this feature off
5734 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5735 entries
5736 (org-disable-agenda-to-diary t))
5737 (save-excursion
5738 (save-window-excursion
5739 (list-diary-entries date 1))) ;; Keep this name for now, compatibility
5740 (if (not (get-buffer fancy-diary-buffer))
5741 (setq entries nil)
5742 (with-current-buffer fancy-diary-buffer
5743 (setq buffer-read-only nil)
5744 (if (= (point-max) 1)
5745 ;; No entries
5746 (setq entries nil)
5747 ;; Omit the date and other unnecessary stuff
5748 (org-agenda-cleanup-fancy-diary)
5749 ;; Add prefix to each line and extend the text properties
5750 (if (= (point-max) 1)
5751 (setq entries nil)
5752 (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
5753 (set-buffer-modified-p nil)
5754 (kill-buffer fancy-diary-buffer)))
5755 (when entries
5756 (setq entries (org-split-string entries "\n"))
5757 (setq entries
5758 (mapcar
5759 (lambda (x)
5760 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
5761 ;; Extend the text properties to the beginning of the line
5762 (org-add-props x (text-properties-at (1- (length x)) x)))
5763 entries)))))
5765 (defun org-agenda-cleanup-fancy-diary ()
5766 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5767 This gets rid of the date, the underline under the date, and
5768 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5769 date. It also removes lines that contain only whitespace."
5770 (goto-char (point-min))
5771 (if (looking-at ".*?:[ \t]*")
5772 (progn
5773 (replace-match "")
5774 (re-search-forward "\n=+$" nil t)
5775 (replace-match "")
5776 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5777 (re-search-forward "\n=+$" nil t)
5778 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5779 (goto-char (point-min))
5780 (while (re-search-forward "^ +\n" nil t)
5781 (replace-match ""))
5782 (goto-char (point-min))
5783 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5784 (replace-match "")))
5786 ;; Make sure entries from the diary have the right text properties.
5787 (eval-after-load "diary-lib"
5788 '(if (boundp 'diary-modify-entry-list-string-function)
5789 ;; We can rely on the hook, nothing to do
5791 ;; Hook not avaiable, must use advice to make this work
5792 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5793 "Make the position visible."
5794 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5795 (stringp string)
5796 buffer-file-name)
5797 (setq string (org-modify-diary-entry-string string))))))
5799 (defun org-modify-diary-entry-string (string)
5800 "Add text properties to string, allowing org-mode to act on it."
5801 (org-add-props string nil
5802 'mouse-face 'highlight
5803 'keymap org-agenda-keymap
5804 'help-echo (format "mouse-2 or RET jump to diary file %s"
5805 (abbreviate-file-name buffer-file-name))
5806 'org-agenda-diary-link t
5807 'org-marker (org-agenda-new-marker (point-at-bol))))
5809 (defun org-diary-default-entry ()
5810 "Add a dummy entry to the diary.
5811 Needed to avoid empty dates which mess up holiday display."
5812 ;; Catch the error if dealing with the new add-to-diary-alist
5813 (when org-disable-agenda-to-diary
5814 (condition-case nil
5815 (add-to-diary-list original-date "Org-mode dummy" "")
5816 (error
5817 (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5819 (defun org-cycle-agenda-files ()
5820 "Cycle through the files in `org-agenda-files'.
5821 If the current buffer visits an agenda file, find the next one in the list.
5822 If the current buffer does not, find the first agenda file."
5823 (interactive)
5824 (let* ((fs (org-agenda-files t))
5825 (files (append fs (list (car fs))))
5826 (tcf (if buffer-file-name (file-truename buffer-file-name)))
5827 file)
5828 (unless files (error "No agenda files"))
5829 (catch 'exit
5830 (while (setq file (pop files))
5831 (if (equal (file-truename file) tcf)
5832 (when (car files)
5833 (find-file (car files))
5834 (throw 'exit t))))
5835 (find-file (car fs)))))
5837 (defun org-agenda-file-to-end ()
5838 "Move/add the current file to the end of the agenda file list.
5839 If the file is not present in the list, it is appended to the list. If it is
5840 present, it is moved there."
5841 (interactive)
5842 (org-agenda-file-to-front 'to-end))
5844 (defun org-agenda-file-to-front (&optional to-end)
5845 "Move/add the current file to the top of the agenda file list.
5846 If the file is not present in the list, it is added to the front. If it is
5847 present, it is moved there. With optional argument TO-END, add/move to the
5848 end of the list."
5849 (interactive "P")
5850 (let ((file-alist (mapcar (lambda (x)
5851 (cons (file-truename x) x))
5852 (org-agenda-files t)))
5853 (ctf (file-truename buffer-file-name))
5854 x had)
5855 (setq x (assoc ctf file-alist) had x)
5857 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
5858 (if to-end
5859 (setq file-alist (append (delq x file-alist) (list x)))
5860 (setq file-alist (cons x (delq x file-alist))))
5861 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
5862 (org-install-agenda-files-menu)
5863 (message "File %s to %s of agenda file list"
5864 (if had "moved" "added") (if to-end "end" "front"))))
5866 (defun org-remove-file (&optional file)
5867 "Remove current file from the list of files in variable `org-agenda-files'.
5868 These are the files which are being checked for agenda entries.
5869 Optional argument FILE means, use this file instead of the current."
5870 (interactive)
5871 (let* ((file (or file buffer-file-name))
5872 (true-file (file-truename file))
5873 (afile (abbreviate-file-name file))
5874 (files (delq nil (mapcar
5875 (lambda (x)
5876 (if (equal true-file
5877 (file-truename x))
5878 nil x))
5879 (org-agenda-files t)))))
5880 (if (not (= (length files) (length (org-agenda-files t))))
5881 (progn
5882 (org-store-new-agenda-file-list files)
5883 (org-install-agenda-files-menu)
5884 (message "Removed file: %s" afile))
5885 (message "File was not in list: %s" afile))))
5887 (defun org-file-menu-entry (file)
5888 (vector file (list 'find-file file) t))
5890 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive)
5891 "Return a list of all relevant day numbers from BEG to END buffer positions.
5892 If NO-RANGES is non-nil, include only the start and end dates of a range,
5893 not every single day in the range. If FORCE-TODAY is non-nil, make
5894 sure that TODAY is included in the list. If INACTIVE is non-nil, also
5895 inactive time stamps (those in square brackets) are included."
5896 (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
5897 dates date day day1 day2 ts1 ts2)
5898 (if force-today
5899 (setq dates (list (time-to-days (current-time)))))
5900 (save-excursion
5901 (goto-char beg)
5902 (while (re-search-forward re end t)
5903 (setq day (time-to-days (org-time-string-to-time
5904 (substring (match-string 1) 0 10))))
5905 (or (memq day dates) (push day dates)))
5906 (unless no-ranges
5907 (goto-char beg)
5908 (while (re-search-forward org-tr-regexp end t)
5909 (setq ts1 (substring (match-string 1) 0 10)
5910 ts2 (substring (match-string 2) 0 10)
5911 day1 (time-to-days (org-time-string-to-time ts1))
5912 day2 (time-to-days (org-time-string-to-time ts2)))
5913 (while (< (setq day1 (1+ day1)) day2)
5914 (or (memq day1 dates) (push day1 dates)))))
5915 (sort dates '<))))
5917 ;;;###autoload
5918 (defun org-diary (&rest args)
5919 "Return diary information from org-files.
5920 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5921 It accesses org files and extracts information from those files to be
5922 listed in the diary. The function accepts arguments specifying what
5923 items should be listed. The following arguments are allowed:
5925 :timestamp List the headlines of items containing a date stamp or
5926 date range matching the selected date. Deadlines will
5927 also be listed, on the expiration day.
5929 :deadline List any deadlines past due, or due within
5930 `org-deadline-warning-days'. The listing occurs only
5931 in the diary for *today*, not at any other date. If
5932 an entry is marked DONE, it is no longer listed.
5934 :scheduled List all items which are scheduled for the given date.
5935 The diary for *today* also contains items which were
5936 scheduled earlier and are not yet marked DONE.
5938 :todo List all TODO items from the org-file. This may be a
5939 long list - so this is not turned on by default.
5940 Like deadlines, these entries only show up in the
5941 diary for *today*, not at any other date.
5943 The call in the diary file should look like this:
5945 &%%(org-diary) ~/path/to/some/orgfile.org
5947 Use a separate line for each org file to check. Or, if you omit the file name,
5948 all files listed in `org-agenda-files' will be checked automatically:
5950 &%%(org-diary)
5952 If you don't give any arguments (as in the example above), the default
5953 arguments (:deadline :scheduled :timestamp) are used. So the example above may
5954 also be written as
5956 &%%(org-diary :deadline :timestamp :scheduled)
5958 The function expects the lisp variables `entry' and `date' to be provided
5959 by the caller, because this is how the calendar works. Don't use this
5960 function from a program - use `org-agenda-get-day-entries' instead."
5961 (org-agenda-maybe-reset-markers)
5962 (org-compile-prefix-format org-agenda-prefix-format)
5963 (setq args (or args '(:deadline :scheduled :timestamp)))
5964 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5965 (list entry)
5966 (org-agenda-files t)))
5967 file rtn results)
5968 ;; If this is called during org-agenda, don't return any entries to
5969 ;; the calendar. Org Agenda will list these entries itself.
5970 (if org-disable-agenda-to-diary (setq files nil))
5971 (while (setq file (pop files))
5972 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5973 (setq results (append results rtn)))
5974 (if results
5975 (concat (org-finalize-agenda-entries results) "\n"))))
5976 (defvar org-category-table nil)
5977 (defun org-get-category-table ()
5978 "Get the table of categories and positions in current buffer."
5979 (let (tbl)
5980 (save-excursion
5981 (goto-char (point-min))
5982 (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
5983 (push (cons (point) (org-trim (match-string 2))) tbl)))
5984 tbl))
5985 (defun org-get-category (&optional pos)
5986 "Get the category applying to position POS."
5987 (if (not org-category-table)
5988 (cond
5989 ((null org-category)
5990 (setq org-category
5991 (if buffer-file-name
5992 (file-name-sans-extension
5993 (file-name-nondirectory buffer-file-name))
5994 "???")))
5995 ((symbolp org-category) (symbol-name org-category))
5996 (t org-category))
5997 (let ((tbl org-category-table)
5998 (pos (or pos (point))))
5999 (while (and tbl (> (caar tbl) pos))
6000 (pop tbl))
6001 (or (cdar tbl) (cdr (nth (1- (length org-category-table))
6002 org-category-table))))))
6004 (defun org-agenda-get-day-entries (file date &rest args)
6005 "Does the work for `org-diary' and `org-agenda'.
6006 FILE is the path to a file to be checked for entries. DATE is date like
6007 the one returned by `calendar-current-date'. ARGS are symbols indicating
6008 which kind of entries should be extracted. For details about these, see
6009 the documentation of `org-diary'."
6010 (setq args (or args '(:deadline :scheduled :timestamp)))
6011 (let* ((org-startup-with-deadline-check nil)
6012 (org-startup-folded nil)
6013 (org-startup-align-all-tables nil)
6014 (buffer (if (file-exists-p file)
6015 (org-get-agenda-file-buffer file)
6016 (error "No such file %s" file)))
6017 arg results rtn)
6018 (if (not buffer)
6019 ;; If file does not exist, make sure an error message ends up in diary
6020 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
6021 (with-current-buffer buffer
6022 (unless (eq major-mode 'org-mode)
6023 (error "Agenda file %s is not in `org-mode'" file))
6024 (setq org-category-table (org-get-category-table))
6025 (let ((case-fold-search nil))
6026 (save-excursion
6027 (save-restriction
6028 (if org-respect-restriction
6029 (if (org-region-active-p)
6030 ;; Respect a region to restrict search
6031 (narrow-to-region (region-beginning) (region-end)))
6032 ;; If we work for the calendar or many files,
6033 ;; get rid of any restriction
6034 (widen))
6035 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
6036 (while (setq arg (pop args))
6037 (cond
6038 ((and (eq arg :todo)
6039 (equal date (calendar-current-date)))
6040 (setq rtn (org-agenda-get-todos))
6041 (setq results (append results rtn)))
6042 ((eq arg :timestamp)
6043 (setq rtn (org-agenda-get-blocks))
6044 (setq results (append results rtn))
6045 (setq rtn (org-agenda-get-timestamps))
6046 (setq results (append results rtn)))
6047 ((eq arg :scheduled)
6048 (setq rtn (org-agenda-get-scheduled))
6049 (setq results (append results rtn)))
6050 ((eq arg :closed)
6051 (setq rtn (org-agenda-get-closed))
6052 (setq results (append results rtn)))
6053 ((and (eq arg :deadline)
6054 (equal date (calendar-current-date)))
6055 (setq rtn (org-agenda-get-deadlines))
6056 (setq results (append results rtn))))))))
6057 results))))
6059 (defun org-entry-is-done-p ()
6060 "Is the current entry marked DONE?"
6061 (save-excursion
6062 (and (re-search-backward "[\r\n]\\*" nil t)
6063 (looking-at org-nl-done-regexp))))
6065 (defun org-at-date-range-p ()
6066 "Is the cursor inside a date range?"
6067 (interactive)
6068 (save-excursion
6069 (catch 'exit
6070 (let ((pos (point)))
6071 (skip-chars-backward "^<\r\n")
6072 (skip-chars-backward "<")
6073 (and (looking-at org-tr-regexp)
6074 (>= (match-end 0) pos)
6075 (throw 'exit t))
6076 (skip-chars-backward "^<\r\n")
6077 (skip-chars-backward "<")
6078 (and (looking-at org-tr-regexp)
6079 (>= (match-end 0) pos)
6080 (throw 'exit t)))
6081 nil)))
6083 (defun org-agenda-get-todos ()
6084 "Return the TODO information for agenda display."
6085 (let* ((props (list 'face nil
6086 'done-face 'org-done
6087 'org-not-done-regexp org-not-done-regexp
6088 'mouse-face 'highlight
6089 'keymap org-agenda-keymap
6090 'help-echo
6091 (format "mouse-2 or RET jump to org file %s"
6092 (abbreviate-file-name buffer-file-name))))
6093 (regexp (concat "[\n\r]\\*+ *\\("
6094 (if org-select-this-todo-keyword
6095 (concat "\\<\\(" org-select-this-todo-keyword
6096 "\\)\\>")
6097 org-not-done-regexp)
6098 "[^\n\r]*\\)"))
6099 marker priority category tags
6100 ee txt)
6101 (goto-char (point-min))
6102 (while (re-search-forward regexp nil t)
6103 (goto-char (match-beginning 1))
6104 (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
6105 category (org-get-category)
6106 tags (org-get-tags-at (point))
6107 txt (org-format-agenda-item "" (match-string 1) category tags)
6108 priority
6109 (+ (org-get-priority txt)
6110 (if org-todo-kwd-priority-p
6111 (- org-todo-kwd-max-priority -2
6112 (length
6113 (member (match-string 2) org-todo-keywords)))
6114 1)))
6115 (org-add-props txt props
6116 'org-marker marker 'org-hd-marker marker
6117 'priority priority 'category category)
6118 (push txt ee)
6119 (if org-agenda-todo-list-sublevels
6120 (goto-char (match-end 1))
6121 (org-end-of-subtree 'invisible)))
6122 (nreverse ee)))
6124 (defconst org-agenda-no-heading-message
6125 "No heading for this item in buffer or region.")
6127 (defun org-agenda-get-timestamps ()
6128 "Return the date stamp information for agenda display."
6129 (let* ((props (list 'face nil
6130 'org-not-done-regexp org-not-done-regexp
6131 'mouse-face 'highlight
6132 'keymap org-agenda-keymap
6133 'help-echo
6134 (format "mouse-2 or RET jump to org file %s"
6135 (abbreviate-file-name buffer-file-name))))
6136 (regexp (regexp-quote
6137 (substring
6138 (format-time-string
6139 (car org-time-stamp-formats)
6140 (apply 'encode-time ; DATE bound by calendar
6141 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
6142 0 11)))
6143 marker hdmarker deadlinep scheduledp donep tmp priority category
6144 ee txt timestr tags)
6145 (goto-char (point-min))
6146 (while (re-search-forward regexp nil t)
6147 (if (not (save-match-data (org-at-date-range-p)))
6148 (progn
6149 (setq marker (org-agenda-new-marker (match-beginning 0))
6150 category (org-get-category (match-beginning 0))
6151 tmp (buffer-substring (max (point-min)
6152 (- (match-beginning 0)
6153 org-ds-keyword-length))
6154 (match-beginning 0))
6155 timestr (buffer-substring (match-beginning 0) (point-at-eol))
6156 deadlinep (string-match org-deadline-regexp tmp)
6157 scheduledp (string-match org-scheduled-regexp tmp)
6158 donep (org-entry-is-done-p))
6159 (if (string-match ">" timestr)
6160 ;; substring should only run to end of time stamp
6161 (setq timestr (substring timestr 0 (match-end 0))))
6162 (save-excursion
6163 (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
6164 (progn
6165 (goto-char (match-end 1))
6166 (setq hdmarker (org-agenda-new-marker)
6167 tags (org-get-tags-at))
6168 (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
6169 (setq txt (org-format-agenda-item
6170 (format "%s%s"
6171 (if deadlinep "Deadline: " "")
6172 (if scheduledp "Scheduled: " ""))
6173 (match-string 1) category tags timestr)))
6174 (setq txt org-agenda-no-heading-message))
6175 (setq priority (org-get-priority txt))
6176 (org-add-props txt props
6177 'org-marker marker 'org-hd-marker hdmarker)
6178 (if deadlinep
6179 (org-add-props txt nil
6180 'face (if donep 'org-done 'org-warning)
6181 'undone-face 'org-warning 'done-face 'org-done
6182 'category category 'priority (+ 100 priority))
6183 (if scheduledp
6184 (org-add-props txt nil
6185 'face 'org-scheduled-today
6186 'undone-face 'org-scheduled-today 'done-face 'org-done
6187 'category category 'priority (+ 99 priority))
6188 (org-add-props txt nil 'priority priority 'category category)))
6189 (push txt ee))
6190 (outline-next-heading))))
6191 (nreverse ee)))
6193 (defun org-agenda-get-closed ()
6194 "Return the logged TODO entries for agenda display."
6195 (let* ((props (list 'mouse-face 'highlight
6196 'org-not-done-regexp org-not-done-regexp
6197 'keymap org-agenda-keymap
6198 'help-echo
6199 (format "mouse-2 or RET jump to org file %s"
6200 (abbreviate-file-name buffer-file-name))))
6201 (regexp (concat
6202 "\\<" org-closed-string " *\\["
6203 (regexp-quote
6204 (substring
6205 (format-time-string
6206 (car org-time-stamp-formats)
6207 (apply 'encode-time ; DATE bound by calendar
6208 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
6209 1 11))))
6210 marker hdmarker priority category tags
6211 ee txt timestr)
6212 (goto-char (point-min))
6213 (while (re-search-forward regexp nil t)
6214 (if (not (save-match-data (org-at-date-range-p)))
6215 (progn
6216 (setq marker (org-agenda-new-marker (match-beginning 0))
6217 category (org-get-category (match-beginning 0))
6218 timestr (buffer-substring (match-beginning 0) (point-at-eol))
6219 ;; donep (org-entry-is-done-p)
6221 (if (string-match "\\]" timestr)
6222 ;; substring should only run to end of time stamp
6223 (setq timestr (substring timestr 0 (match-end 0))))
6224 (save-excursion
6225 (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
6226 (progn
6227 (goto-char (match-end 1))
6228 (setq hdmarker (org-agenda-new-marker)
6229 tags (org-get-tags-at))
6230 (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
6231 (setq txt (org-format-agenda-item
6232 "Closed: "
6233 (match-string 1) category tags timestr)))
6234 (setq txt org-agenda-no-heading-message))
6235 (setq priority 100000)
6236 (org-add-props txt props
6237 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
6238 'priority priority 'category category
6239 'undone-face 'org-warning 'done-face 'org-done)
6240 (push txt ee))
6241 (outline-next-heading))))
6242 (nreverse ee)))
6244 (defun org-agenda-get-deadlines ()
6245 "Return the deadline information for agenda display."
6246 (let* ((wdays org-deadline-warning-days)
6247 (props (list 'mouse-face 'highlight
6248 'org-not-done-regexp org-not-done-regexp
6249 'keymap org-agenda-keymap
6250 'help-echo
6251 (format "mouse-2 or RET jump to org file %s"
6252 (abbreviate-file-name buffer-file-name))))
6253 (regexp org-deadline-time-regexp)
6254 (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
6255 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6256 d2 diff pos pos1 category tags
6257 ee txt head)
6258 (goto-char (point-min))
6259 (while (re-search-forward regexp nil t)
6260 (setq pos (1- (match-beginning 1))
6261 d2 (time-to-days
6262 (org-time-string-to-time (match-string 1)))
6263 diff (- d2 d1))
6264 ;; When to show a deadline in the calendar:
6265 ;; If the expiration is within wdays warning time.
6266 ;; Past-due deadlines are only shown on the current date
6267 (if (and (< diff wdays) todayp (not (= diff 0)))
6268 (save-excursion
6269 (setq category (org-get-category))
6270 (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
6271 (progn
6272 (goto-char (match-end 0))
6273 (setq pos1 (match-end 1))
6274 (setq tags (org-get-tags-at pos1))
6275 (setq head (buffer-substring-no-properties
6276 (point)
6277 (progn (skip-chars-forward "^\r\n")
6278 (point))))
6279 (if (string-match org-looking-at-done-regexp head)
6280 (setq txt nil)
6281 (setq txt (org-format-agenda-item
6282 (format "In %3d d.: " diff) head category tags))))
6283 (setq txt org-agenda-no-heading-message))
6284 (when txt
6285 (org-add-props txt props
6286 'org-marker (org-agenda-new-marker pos)
6287 'org-hd-marker (org-agenda-new-marker pos1)
6288 'priority (+ (- 10 diff) (org-get-priority txt))
6289 'category category
6290 'face (cond ((<= diff 0) 'org-warning)
6291 ((<= diff 5) 'org-scheduled-previously)
6292 (t nil))
6293 'undone-face (cond
6294 ((<= diff 0) 'org-warning)
6295 ((<= diff 5) 'org-scheduled-previously)
6296 (t nil))
6297 'done-face 'org-done)
6298 (push txt ee)))))
6299 ee))
6301 (defun org-agenda-get-scheduled ()
6302 "Return the scheduled information for agenda display."
6303 (let* ((props (list 'face 'org-scheduled-previously
6304 'org-not-done-regexp org-not-done-regexp
6305 'undone-face 'org-scheduled-previously
6306 'done-face 'org-done
6307 'mouse-face 'highlight
6308 'keymap org-agenda-keymap
6309 'help-echo
6310 (format "mouse-2 or RET jump to org file %s"
6311 (abbreviate-file-name buffer-file-name))))
6312 (regexp org-scheduled-time-regexp)
6313 (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
6314 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6315 d2 diff pos pos1 category tags
6316 ee txt head)
6317 (goto-char (point-min))
6318 (while (re-search-forward regexp nil t)
6319 (setq pos (1- (match-beginning 1))
6320 d2 (time-to-days
6321 (org-time-string-to-time (match-string 1)))
6322 diff (- d2 d1))
6323 ;; When to show a scheduled item in the calendar:
6324 ;; If it is on or past the date.
6325 (if (and (< diff 0) todayp)
6326 (save-excursion
6327 (setq category (org-get-category))
6328 (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
6329 (progn
6330 (goto-char (match-end 0))
6331 (setq pos1 (match-end 1))
6332 (setq tags (org-get-tags-at))
6333 (setq head (buffer-substring-no-properties
6334 (point)
6335 (progn (skip-chars-forward "^\r\n") (point))))
6336 (if (string-match org-looking-at-done-regexp head)
6337 (setq txt nil)
6338 (setq txt (org-format-agenda-item
6339 (format "Sched.%2dx: " (- 1 diff)) head
6340 category tags))))
6341 (setq txt org-agenda-no-heading-message))
6342 (when txt
6343 (org-add-props txt props
6344 'org-marker (org-agenda-new-marker pos)
6345 'org-hd-marker (org-agenda-new-marker pos1)
6346 'priority (+ (- 5 diff) (org-get-priority txt))
6347 'category category)
6348 (push txt ee)))))
6349 ee))
6351 (defun org-agenda-get-blocks ()
6352 "Return the date-range information for agenda display."
6353 (let* ((props (list 'face nil
6354 'org-not-done-regexp org-not-done-regexp
6355 'mouse-face 'highlight
6356 'keymap org-agenda-keymap
6357 'help-echo
6358 (format "mouse-2 or RET jump to org file %s"
6359 (abbreviate-file-name buffer-file-name))))
6360 (regexp org-tr-regexp)
6361 (d0 (calendar-absolute-from-gregorian date))
6362 marker hdmarker ee txt d1 d2 s1 s2 timestr category tags)
6363 (goto-char (point-min))
6364 (while (re-search-forward regexp nil t)
6365 (setq timestr (match-string 0)
6366 s1 (match-string 1)
6367 s2 (match-string 2)
6368 d1 (time-to-days (org-time-string-to-time s1))
6369 d2 (time-to-days (org-time-string-to-time s2)))
6370 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6371 ;; Only allow days between the limits, because the normal
6372 ;; date stamps will catch the limits.
6373 (save-excursion
6374 (setq marker (org-agenda-new-marker (point)))
6375 (setq category (org-get-category))
6376 (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
6377 (progn
6378 (setq hdmarker (org-agenda-new-marker (match-end 1)))
6379 (goto-char (match-end 1))
6380 (setq tags (org-get-tags-at))
6381 (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
6382 (setq txt (org-format-agenda-item
6383 (format (if (= d1 d2) "" "(%d/%d): ")
6384 (1+ (- d0 d1)) (1+ (- d2 d1)))
6385 (match-string 1) category tags
6386 (if (= d0 d1) timestr))))
6387 (setq txt org-agenda-no-heading-message))
6388 (org-add-props txt props
6389 'org-marker marker 'org-hd-marker hdmarker
6390 'priority (org-get-priority txt) 'category category)
6391 (push txt ee)))
6392 (outline-next-heading))
6393 ;; Sort the entries by expiration date.
6394 (nreverse ee)))
6396 (defconst org-plain-time-of-day-regexp
6397 (concat
6398 "\\(\\<[012]?[0-9]"
6399 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
6400 "\\(--?"
6401 "\\(\\<[012]?[0-9]"
6402 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
6403 "\\)?")
6404 "Regular expression to match a plain time or time range.
6405 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
6406 groups carry important information:
6407 0 the full match
6408 1 the first time, range or not
6409 8 the second time, if it is a range.")
6411 (defconst org-stamp-time-of-day-regexp
6412 (concat
6413 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +[a-zA-Z]+ +\\)"
6414 "\\([012][0-9]:[0-5][0-9]\\)>"
6415 "\\(--?"
6416 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
6417 "Regular expression to match a timestamp time or time range.
6418 After a match, the following groups carry important information:
6419 0 the full match
6420 1 date plus weekday, for backreferencing to make sure both times on same day
6421 2 the first time, range or not
6422 4 the second time, if it is a range.")
6424 (defvar org-prefix-has-time nil
6425 "A flag, set by `org-compile-prefix-format'.
6426 The flag is set if the currently compiled format contains a `%t'.")
6427 (defvar org-prefix-has-tag nil
6428 "A flag, set by `org-compile-prefix-format'.
6429 The flag is set if the currently compiled format contains a `%T'.")
6431 (defun org-format-agenda-item (extra txt &optional category tags dotime noprefix)
6432 "Format TXT to be inserted into the agenda buffer.
6433 In particular, it adds the prefix and corresponding text properties. EXTRA
6434 must be a string and replaces the `%s' specifier in the prefix format.
6435 CATEGORY (string, symbol or nil) may be used to overrule the default
6436 category taken from local variable or file name. It will replace the `%c'
6437 specifier in the format. DOTIME, when non-nil, indicates that a
6438 time-of-day should be extracted from TXT for sorting of this entry, and for
6439 the `%t' specifier in the format. When DOTIME is a string, this string is
6440 searched for a time before TXT is. NOPREFIX is a flag and indicates that
6441 only the correctly processes TXT should be returned - this is used by
6442 `org-agenda-change-all-lines'. TAG can be the tag of the headline."
6443 (save-match-data
6444 ;; Diary entries sometimes have extra whitespace at the beginning
6445 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
6446 (let* ((category (or category
6447 org-category
6448 (if buffer-file-name
6449 (file-name-sans-extension
6450 (file-name-nondirectory buffer-file-name))
6451 "")))
6452 (tag (if tags (nth (1- (length tags)) tags) ""))
6453 time ;; needed for the eval of the prefix format
6454 (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
6455 (time-of-day (and dotime (org-get-time-of-day ts)))
6456 stamp plain s0 s1 s2 rtn)
6457 (when (and dotime time-of-day org-prefix-has-time)
6458 ;; Extract starting and ending time and move them to prefix
6459 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6460 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6461 (setq s0 (match-string 0 ts)
6462 s1 (match-string (if plain 1 2) ts)
6463 s2 (match-string (if plain 8 4) ts))
6465 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6466 ;; them, we might want to remove them there to avoid duplication.
6467 ;; The user can turn this off with a variable.
6468 (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
6469 (string-match (concat (regexp-quote s0) " *") txt)
6470 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6471 (= (match-beginning 0) 0)
6473 (setq txt (replace-match "" nil nil txt))))
6474 ;; Normalize the time(s) to 24 hour
6475 (if s1 (setq s1 (org-get-time-of-day s1 'string)))
6476 (if s2 (setq s2 (org-get-time-of-day s2 'string))))
6478 (when (and (or (eq org-agenda-remove-tags-when-in-prefix t)
6479 (and org-agenda-remove-tags-when-in-prefix
6480 org-prefix-has-tag))
6481 (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" txt))
6482 (setq txt (replace-match "" t t txt)))
6484 ;; Create the final string
6485 (if noprefix
6486 (setq rtn txt)
6487 ;; Prepare the variables needed in the eval of the compiled format
6488 (setq time (cond (s2 (concat s1 "-" s2))
6489 (s1 (concat s1 "......"))
6490 (t ""))
6491 extra (or extra "")
6492 category (if (symbolp category) (symbol-name category) category))
6493 ;; Evaluate the compiled format
6494 (setq rtn (concat (eval org-prefix-format-compiled) txt)))
6496 ;; And finally add the text properties
6497 (org-add-props rtn nil
6498 'category (downcase category) 'tags tags
6499 'prefix-length (- (length rtn) (length txt))
6500 'time-of-day time-of-day
6501 'dotime dotime))))
6503 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6504 (catch 'exit
6505 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6506 ((and todayp (member 'today (car org-agenda-time-grid))))
6507 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6508 ((member 'weekly (car org-agenda-time-grid)))
6509 (t (throw 'exit list)))
6510 (let* ((have (delq nil (mapcar
6511 (lambda (x) (get-text-property 1 'time-of-day x))
6512 list)))
6513 (string (nth 1 org-agenda-time-grid))
6514 (gridtimes (nth 2 org-agenda-time-grid))
6515 (req (car org-agenda-time-grid))
6516 (remove (member 'remove-match req))
6517 new time)
6518 (if (and (member 'require-timed req) (not have))
6519 ;; don't show empty grid
6520 (throw 'exit list))
6521 (while (setq time (pop gridtimes))
6522 (unless (and remove (member time have))
6523 (setq time (int-to-string time))
6524 (push (org-format-agenda-item
6525 nil string "" nil
6526 (concat (substring time 0 -2) ":" (substring time -2)))
6527 new)
6528 (put-text-property
6529 1 (length (car new)) 'face 'org-time-grid (car new))))
6530 (if (member 'time-up org-agenda-sorting-strategy)
6531 (append new list)
6532 (append list new)))))
6534 (defun org-compile-prefix-format (format)
6535 "Compile the prefix format into a Lisp form that can be evaluated.
6536 The resulting form is returned and stored in the variable
6537 `org-prefix-format-compiled'."
6538 (setq org-prefix-has-time nil org-prefix-has-tag nil)
6539 (let ((start 0) varform vars var (s format)e c f opt)
6540 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
6541 s start)
6542 (setq var (cdr (assoc (match-string 4 s)
6543 '(("c" . category) ("t" . time) ("s" . extra)
6544 ("T" . tag))))
6545 c (or (match-string 3 s) "")
6546 opt (match-beginning 1)
6547 start (1+ (match-beginning 0)))
6548 (if (equal var 'time) (setq org-prefix-has-time t))
6549 (if (equal var 'tag) (setq org-prefix-has-tag t))
6550 (setq f (concat "%" (match-string 2 s) "s"))
6551 (if opt
6552 (setq varform
6553 `(if (equal "" ,var)
6555 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6556 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
6557 (setq s (replace-match "%s" t nil s))
6558 (push varform vars))
6559 (setq vars (nreverse vars))
6560 (setq org-prefix-format-compiled `(format ,s ,@vars))))
6562 (defun org-get-time-of-day (s &optional string)
6563 "Check string S for a time of day.
6564 If found, return it as a military time number between 0 and 2400.
6565 If not found, return nil.
6566 The optional STRING argument forces conversion into a 5 character wide string
6567 HH:MM."
6568 (save-match-data
6569 (when
6571 (string-match
6572 "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6573 (string-match
6574 "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6575 (let* ((t0 (+ (* 100
6576 (+ (string-to-number (match-string 1 s))
6577 (if (and (match-beginning 4)
6578 (equal (downcase (match-string 4 s)) "pm"))
6579 12 0)))
6580 (if (match-beginning 3)
6581 (string-to-number (match-string 3 s))
6582 0)))
6583 (t1 (concat " "
6584 (if (< t0 100) "0" "") (if (< t0 10) "0" "")
6585 (int-to-string t0))))
6586 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6588 (defun org-finalize-agenda-entries (list)
6589 "Sort and concatenate the agenda items."
6590 (setq list (mapcar 'org-agenda-highlight-todo list))
6591 (mapconcat 'identity (sort list 'org-entries-lessp) "\n"))
6593 (defun org-agenda-highlight-todo (x)
6594 (let (re)
6595 (if (eq x 'line)
6596 (save-excursion
6597 (beginning-of-line 1)
6598 (setq re (get-text-property (point) 'org-not-done-regexp))
6599 (goto-char (+ (point) (get-text-property (point) 'prefix-length)))
6600 (and (looking-at (concat "[ \t]*" re))
6601 (add-text-properties (match-beginning 0) (match-end 0)
6602 '(face org-todo))))
6603 (setq re (get-text-property 0 'org-not-done-regexp x))
6604 (and re (string-match re x)
6605 (add-text-properties (match-beginning 0) (match-end 0)
6606 '(face org-todo) x))
6607 x)))
6609 (defsubst org-cmp-priority (a b)
6610 "Compare the priorities of string A and B."
6611 (let ((pa (or (get-text-property 1 'priority a) 0))
6612 (pb (or (get-text-property 1 'priority b) 0)))
6613 (cond ((> pa pb) +1)
6614 ((< pa pb) -1)
6615 (t nil))))
6617 (defsubst org-cmp-category (a b)
6618 "Compare the string values of categories of strings A and B."
6619 (let ((ca (or (get-text-property 1 'category a) ""))
6620 (cb (or (get-text-property 1 'category b) "")))
6621 (cond ((string-lessp ca cb) -1)
6622 ((string-lessp cb ca) +1)
6623 (t nil))))
6625 (defsubst org-cmp-time (a b)
6626 "Compare the time-of-day values of strings A and B."
6627 (let* ((def (if org-sort-agenda-notime-is-late 2401 -1))
6628 (ta (or (get-text-property 1 'time-of-day a) def))
6629 (tb (or (get-text-property 1 'time-of-day b) def)))
6630 (cond ((< ta tb) -1)
6631 ((< tb ta) +1)
6632 (t nil))))
6634 (defun org-entries-lessp (a b)
6635 "Predicate for sorting agenda entries."
6636 ;; The following variables will be used when the form is evaluated.
6637 (let* ((time-up (org-cmp-time a b))
6638 (time-down (if time-up (- time-up) nil))
6639 (priority-up (org-cmp-priority a b))
6640 (priority-down (if priority-up (- priority-up) nil))
6641 (category-up (org-cmp-category a b))
6642 (category-down (if category-up (- category-up) nil))
6643 (category-keep (if category-up +1 nil)))
6644 (cdr (assoc
6645 (eval (cons 'or org-agenda-sorting-strategy))
6646 '((-1 . t) (1 . nil) (nil . nil))))))
6648 (defun org-agenda-show-priority ()
6649 "Show the priority of the current item.
6650 This priority is composed of the main priority given with the [#A] cookies,
6651 and by additional input from the age of a schedules or deadline entry."
6652 (interactive)
6653 (let* ((pri (get-text-property (point-at-bol) 'priority)))
6654 (message "Priority is %d" (if pri pri -1000))))
6656 (defun org-agenda-show-tags ()
6657 "Show the tags applicable to the current item."
6658 (interactive)
6659 (let* ((tags (get-text-property (point-at-bol) 'tags)))
6660 (if tags
6661 (message "Tags are :%s:" (mapconcat 'identity tags ":"))
6662 (message "No tags associated with this line"))))
6664 (defun org-agenda-goto (&optional highlight)
6665 "Go to the Org-mode file which contains the item at point."
6666 (interactive)
6667 (let* ((marker (or (get-text-property (point) 'org-marker)
6668 (org-agenda-error)))
6669 (buffer (marker-buffer marker))
6670 (pos (marker-position marker)))
6671 (switch-to-buffer-other-window buffer)
6672 (widen)
6673 (goto-char pos)
6674 (when (eq major-mode 'org-mode)
6675 (org-show-hidden-entry)
6676 (save-excursion
6677 (and (outline-next-heading)
6678 (org-flag-heading nil)))) ; show the next heading
6679 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6681 (defun org-agenda-switch-to ()
6682 "Go to the Org-mode file which contains the item at point."
6683 (interactive)
6684 (let* ((marker (or (get-text-property (point) 'org-marker)
6685 (org-agenda-error)))
6686 (buffer (marker-buffer marker))
6687 (pos (marker-position marker)))
6688 (switch-to-buffer buffer)
6689 (delete-other-windows)
6690 (widen)
6691 (goto-char pos)
6692 (when (eq major-mode 'org-mode)
6693 (org-show-hidden-entry)
6694 (save-excursion
6695 (and (outline-next-heading)
6696 (org-flag-heading nil)))))) ; show the next heading
6698 (defun org-agenda-goto-mouse (ev)
6699 "Go to the Org-mode file which contains the item at the mouse click."
6700 (interactive "e")
6701 (mouse-set-point ev)
6702 (org-agenda-goto))
6704 (defun org-agenda-show ()
6705 "Display the Org-mode file which contains the item at point."
6706 (interactive)
6707 (let ((win (selected-window)))
6708 (org-agenda-goto t)
6709 (select-window win)))
6711 (defun org-agenda-recenter (arg)
6712 "Display the Org-mode file which contains the item at point and recenter."
6713 (interactive "P")
6714 (let ((win (selected-window)))
6715 (org-agenda-goto t)
6716 (recenter arg)
6717 (select-window win)))
6719 (defun org-agenda-show-mouse (ev)
6720 "Display the Org-mode file which contains the item at the mouse click."
6721 (interactive "e")
6722 (mouse-set-point ev)
6723 (org-agenda-show))
6725 (defun org-agenda-check-no-diary ()
6726 "Check if the entry is a diary link and abort if yes."
6727 (if (get-text-property (point) 'org-agenda-diary-link)
6728 (org-agenda-error)))
6730 (defun org-agenda-error ()
6731 (error "Command not allowed in this line"))
6733 (defvar org-last-heading-marker (make-marker)
6734 "Marker pointing to the headline that last changed its TODO state
6735 by a remote command from the agenda.")
6737 (defun org-agenda-todo (&optional arg)
6738 "Cycle TODO state of line at point, also in Org-mode file.
6739 This changes the line at point, all other lines in the agenda referring to
6740 the same tree node, and the headline of the tree node in the Org-mode file."
6741 (interactive "P")
6742 (org-agenda-check-no-diary)
6743 (let* ((col (current-column))
6744 (marker (or (get-text-property (point) 'org-marker)
6745 (org-agenda-error)))
6746 (buffer (marker-buffer marker))
6747 (pos (marker-position marker))
6748 (hdmarker (get-text-property (point) 'org-hd-marker))
6749 (buffer-read-only nil)
6750 newhead)
6751 (with-current-buffer buffer
6752 (widen)
6753 (goto-char pos)
6754 (org-show-hidden-entry)
6755 (save-excursion
6756 (and (outline-next-heading)
6757 (org-flag-heading nil))) ; show the next heading
6758 (org-todo arg)
6759 (and (bolp) (forward-char 1))
6760 (setq newhead (org-get-heading))
6761 (save-excursion
6762 (org-back-to-heading)
6763 (move-marker org-last-heading-marker (point))))
6764 (beginning-of-line 1)
6765 (save-excursion
6766 (org-agenda-change-all-lines newhead hdmarker 'fixface))
6767 (move-to-column col)))
6769 (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
6770 "Change all lines in the agenda buffer which match HDMARKER.
6771 The new content of the line will be NEWHEAD (as modified by
6772 `org-format-agenda-item'). HDMARKER is checked with
6773 `equal' against all `org-hd-marker' text properties in the file.
6774 If FIXFACE is non-nil, the face of each item is modified acording to
6775 the new TODO state."
6776 (let* (props m pl undone-face done-face finish new dotime cat tags)
6777 ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
6778 (save-excursion
6779 (goto-char (point-max))
6780 (beginning-of-line 1)
6781 (while (not finish)
6782 (setq finish (bobp))
6783 (when (and (setq m (get-text-property (point) 'org-hd-marker))
6784 (equal m hdmarker))
6785 (setq props (text-properties-at (point))
6786 dotime (get-text-property (point) 'dotime)
6787 cat (get-text-property (point) 'category)
6788 tags (get-text-property (point) 'tags)
6789 new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
6790 pl (get-text-property (point) 'prefix-length)
6791 undone-face (get-text-property (point) 'undone-face)
6792 done-face (get-text-property (point) 'done-face))
6793 (move-to-column pl)
6794 (if (looking-at ".*")
6795 (progn
6796 (replace-match new t t)
6797 (beginning-of-line 1)
6798 (add-text-properties (point-at-bol) (point-at-eol) props)
6799 (when fixface
6800 (add-text-properties
6801 (point-at-bol) (point-at-eol)
6802 (list 'face
6803 (if org-last-todo-state-is-todo
6804 undone-face done-face)))
6805 (org-agenda-highlight-todo 'line))
6806 (beginning-of-line 1))
6807 (error "Line update did not work")))
6808 (beginning-of-line 0)))))
6810 (defun org-agenda-priority-up ()
6811 "Increase the priority of line at point, also in Org-mode file."
6812 (interactive)
6813 (org-agenda-priority 'up))
6815 (defun org-agenda-priority-down ()
6816 "Decrease the priority of line at point, also in Org-mode file."
6817 (interactive)
6818 (org-agenda-priority 'down))
6820 (defun org-agenda-priority (&optional force-direction)
6821 "Set the priority of line at point, also in Org-mode file.
6822 This changes the line at point, all other lines in the agenda referring to
6823 the same tree node, and the headline of the tree node in the Org-mode file."
6824 (interactive)
6825 (org-agenda-check-no-diary)
6826 (let* ((marker (or (get-text-property (point) 'org-marker)
6827 (org-agenda-error)))
6828 (buffer (marker-buffer marker))
6829 (pos (marker-position marker))
6830 (hdmarker (get-text-property (point) 'org-hd-marker))
6831 (buffer-read-only nil)
6832 newhead)
6833 (with-current-buffer buffer
6834 (widen)
6835 (goto-char pos)
6836 (org-show-hidden-entry)
6837 (save-excursion
6838 (and (outline-next-heading)
6839 (org-flag-heading nil))) ; show the next heading
6840 (funcall 'org-priority force-direction)
6841 (end-of-line 1)
6842 (setq newhead (org-get-heading)))
6843 (org-agenda-change-all-lines newhead hdmarker)
6844 (beginning-of-line 1)))
6846 (defun org-get-tags-at (&optional pos)
6847 "Get a list of all headline targs applicable at POS.
6848 POS defaults to point. If tags are inherited, the list contains
6849 the targets in the same sequence as the headlines appear, i.e.
6850 the tags of the current headline come last."
6851 (interactive)
6852 (let (tags)
6853 (save-excursion
6854 (goto-char (or pos (point)))
6855 (save-match-data
6856 (org-back-to-heading t)
6857 (condition-case nil
6858 (while t
6859 (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
6860 (setq tags (append (org-split-string (match-string 1) ":") tags)))
6861 (or org-use-tag-inheritance (error ""))
6862 (org-up-heading-all 1))
6863 (error nil))))
6864 (message "%s" tags)
6865 tags))
6867 (defun org-agenda-set-tags ()
6868 "Set tags for the current headline."
6869 (interactive)
6870 (org-agenda-check-no-diary)
6871 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6872 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
6873 (org-agenda-error)))
6874 (buffer (marker-buffer hdmarker))
6875 (pos (marker-position hdmarker))
6876 (buffer-read-only nil)
6877 newhead)
6878 (with-current-buffer buffer
6879 (widen)
6880 (goto-char pos)
6881 (org-show-hidden-entry)
6882 (save-excursion
6883 (and (outline-next-heading)
6884 (org-flag-heading nil))) ; show the next heading
6885 (call-interactively 'org-set-tags)
6886 (end-of-line 1)
6887 (setq newhead (org-get-heading)))
6888 (org-agenda-change-all-lines newhead hdmarker)
6889 (beginning-of-line 1)))
6891 (defun org-agenda-date-later (arg &optional what)
6892 "Change the date of this item to one day later."
6893 (interactive "p")
6894 (org-agenda-check-type t 'agenda 'timeline)
6895 (org-agenda-check-no-diary)
6896 (let* ((marker (or (get-text-property (point) 'org-marker)
6897 (org-agenda-error)))
6898 (buffer (marker-buffer marker))
6899 (pos (marker-position marker)))
6900 (with-current-buffer buffer
6901 (widen)
6902 (goto-char pos)
6903 (if (not (org-at-timestamp-p))
6904 (error "Cannot find time stamp"))
6905 (org-timestamp-change arg (or what 'day))
6906 (message "Time stamp changed to %s" org-last-changed-timestamp))))
6908 (defun org-agenda-date-earlier (arg &optional what)
6909 "Change the date of this item to one day earlier."
6910 (interactive "p")
6911 (org-agenda-date-later (- arg) what))
6913 (defun org-agenda-date-prompt (arg)
6914 "Change the date of this item. Date is prompted for, with default today.
6915 The prefix ARG is passed to the `org-time-stamp' command and can therefore
6916 be used to request time specification in the time stamp."
6917 (interactive "P")
6918 (org-agenda-check-type t 'agenda 'timeline)
6919 (org-agenda-check-no-diary)
6920 (let* ((marker (or (get-text-property (point) 'org-marker)
6921 (org-agenda-error)))
6922 (buffer (marker-buffer marker))
6923 (pos (marker-position marker)))
6924 (with-current-buffer buffer
6925 (widen)
6926 (goto-char pos)
6927 (if (not (org-at-timestamp-p))
6928 (error "Cannot find time stamp"))
6929 (org-time-stamp arg)
6930 (message "Time stamp changed to %s" org-last-changed-timestamp))))
6932 (defun org-get-heading ()
6933 "Return the heading of the current entry, without the stars."
6934 (save-excursion
6935 (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
6936 (if (and (re-search-backward "[\r\n]\\*" nil t)
6937 (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
6938 (match-string 1)
6939 "")))
6941 (defun org-agenda-diary-entry ()
6942 "Make a diary entry, like the `i' command from the calendar.
6943 All the standard commands work: block, weekly etc."
6944 (interactive)
6945 (org-agenda-check-type t 'agenda 'timeline)
6946 (require 'diary-lib)
6947 (let* ((char (progn
6948 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
6949 (read-char-exclusive)))
6950 (cmd (cdr (assoc char
6951 '((?d . insert-diary-entry)
6952 (?w . insert-weekly-diary-entry)
6953 (?m . insert-monthly-diary-entry)
6954 (?y . insert-yearly-diary-entry)
6955 (?a . insert-anniversary-diary-entry)
6956 (?b . insert-block-diary-entry)
6957 (?c . insert-cyclic-diary-entry)))))
6958 (oldf (symbol-function 'calendar-cursor-to-date))
6959 (point (point))
6960 (mark (or (mark t) (point))))
6961 (unless cmd
6962 (error "No command associated with <%c>" char))
6963 (unless (and (get-text-property point 'day)
6964 (or (not (equal ?b char))
6965 (get-text-property mark 'day)))
6966 (error "Don't know which date to use for diary entry"))
6967 ;; We implement this by hacking the `calendar-cursor-to-date' function
6968 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
6969 (let ((calendar-mark-ring
6970 (list (calendar-gregorian-from-absolute
6971 (or (get-text-property mark 'day)
6972 (get-text-property point 'day))))))
6973 (unwind-protect
6974 (progn
6975 (fset 'calendar-cursor-to-date
6976 (lambda (&optional error)
6977 (calendar-gregorian-from-absolute
6978 (get-text-property point 'day))))
6979 (call-interactively cmd))
6980 (fset 'calendar-cursor-to-date oldf)))))
6983 (defun org-agenda-execute-calendar-command (cmd)
6984 "Execute a calendar command from the agenda, with the date associated to
6985 the cursor position."
6986 (org-agenda-check-type t 'agenda 'timeline)
6987 (require 'diary-lib)
6988 (unless (get-text-property (point) 'day)
6989 (error "Don't know which date to use for calendar command"))
6990 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
6991 (point (point))
6992 (date (calendar-gregorian-from-absolute
6993 (get-text-property point 'day)))
6994 (displayed-day (extract-calendar-day date))
6995 (displayed-month (extract-calendar-month date))
6996 (displayed-year (extract-calendar-year date)))
6997 (unwind-protect
6998 (progn
6999 (fset 'calendar-cursor-to-date
7000 (lambda (&optional error)
7001 (calendar-gregorian-from-absolute
7002 (get-text-property point 'day))))
7003 (call-interactively cmd))
7004 (fset 'calendar-cursor-to-date oldf))))
7006 (defun org-agenda-phases-of-moon ()
7007 "Display the phases of the moon for the 3 months around the cursor date."
7008 (interactive)
7009 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
7011 (defun org-agenda-holidays ()
7012 "Display the holidays for the 3 months around the cursor date."
7013 (interactive)
7014 (org-agenda-execute-calendar-command 'list-calendar-holidays))
7016 (defun org-agenda-sunrise-sunset (arg)
7017 "Display sunrise and sunset for the cursor date.
7018 Latitude and longitude can be specified with the variables
7019 `calendar-latitude' and `calendar-longitude'. When called with prefix
7020 argument, latitude and longitude will be prompted for."
7021 (interactive "P")
7022 (let ((calendar-longitude (if arg nil calendar-longitude))
7023 (calendar-latitude (if arg nil calendar-latitude))
7024 (calendar-location-name
7025 (if arg "the given coordinates" calendar-location-name)))
7026 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
7028 (defun org-agenda-goto-calendar ()
7029 "Open the Emacs calendar with the date at the cursor."
7030 (interactive)
7031 (org-agenda-check-type t 'agenda 'timeline)
7032 (let* ((day (or (get-text-property (point) 'day)
7033 (error "Don't know which date to open in calendar")))
7034 (date (calendar-gregorian-from-absolute day))
7035 (calendar-move-hook nil)
7036 (view-diary-entries-initially nil))
7037 (calendar)
7038 (calendar-goto-date date)))
7040 (defun org-calendar-goto-agenda ()
7041 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
7042 This is a command that has to be installed in `calendar-mode-map'."
7043 (interactive)
7044 (org-agenda-list nil (calendar-absolute-from-gregorian
7045 (calendar-cursor-to-date))
7046 nil t))
7048 (defun org-agenda-convert-date ()
7049 (interactive)
7050 (org-agenda-check-type t 'agenda 'timeline)
7051 (let ((day (get-text-property (point) 'day))
7052 date s)
7053 (unless day
7054 (error "Don't know which date to convert"))
7055 (setq date (calendar-gregorian-from-absolute day))
7056 (setq s (concat
7057 "Gregorian: " (calendar-date-string date) "\n"
7058 "ISO: " (calendar-iso-date-string date) "\n"
7059 "Day of Yr: " (calendar-day-of-year-string date) "\n"
7060 "Julian: " (calendar-julian-date-string date) "\n"
7061 "Astron. JD: " (calendar-astro-date-string date)
7062 " (Julian date number at noon UTC)\n"
7063 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
7064 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
7065 "French: " (calendar-french-date-string date) "\n"
7066 "Mayan: " (calendar-mayan-date-string date) "\n"
7067 "Coptic: " (calendar-coptic-date-string date) "\n"
7068 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
7069 "Persian: " (calendar-persian-date-string date) "\n"
7070 "Chinese: " (calendar-chinese-date-string date) "\n"))
7071 (with-output-to-temp-buffer "*Dates*"
7072 (princ s))
7073 (if (fboundp 'fit-window-to-buffer)
7074 (fit-window-to-buffer (get-buffer-window "*Dates*")))))
7076 ;;; Tags
7078 (defun org-scan-tags (action matcher &optional todo-only)
7079 "Scan headline tags with inheritance and produce output ACTION.
7080 ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
7081 evaluated, testing if a given set of tags qualifies a headline for
7082 inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
7083 are included in the output."
7084 (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
7085 (mapconcat 'regexp-quote
7086 (nreverse (cdr (reverse org-todo-keywords)))
7087 "\\|")
7088 "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*[\n\r]"))
7089 (props (list 'face nil
7090 'done-face 'org-done
7091 'undone-face nil
7092 'mouse-face 'highlight
7093 'keymap org-agenda-keymap
7094 'help-echo
7095 (format "mouse-2 or RET jump to org file %s"
7096 (abbreviate-file-name buffer-file-name))))
7097 lspos
7098 tags tags-list tags-alist (llast 0) rtn level category i txt
7099 todo marker)
7101 (save-excursion
7102 (goto-char (point-min))
7103 (when (eq action 'sparse-tree) (hide-sublevels 1))
7104 (while (re-search-forward re nil t)
7105 (setq todo (if (match-end 1) (match-string 2))
7106 tags (if (match-end 4) (match-string 4)))
7107 (goto-char (setq lspos (1+ (match-beginning 0))))
7108 (setq level (funcall outline-level)
7109 category (org-get-category))
7110 (setq i llast llast level)
7111 ;; remove tag lists from same and sublevels
7112 (while (>= i level)
7113 (when (setq entry (assoc i tags-alist))
7114 (setq tags-alist (delete entry tags-alist)))
7115 (setq i (1- i)))
7116 ;; add the nex tags
7117 (when tags
7118 (setq tags (mapcar 'downcase (org-split-string tags ":"))
7119 tags-alist
7120 (cons (cons level tags) tags-alist)))
7121 ;; compile tags for current headline
7122 (setq tags-list
7123 (if org-use-tag-inheritance
7124 (apply 'append (mapcar 'cdr tags-alist))
7125 tags))
7126 (when (and (or (not todo-only) todo)
7127 (eval matcher))
7128 ;; list this headline
7129 (if (eq action 'sparse-tree)
7130 (progn
7131 (org-show-hierarchy-above))
7132 (setq txt (org-format-agenda-item
7134 (concat
7135 (if org-tags-match-list-sublevels
7136 (make-string (1- level) ?.) "")
7137 (org-get-heading))
7138 category tags-list))
7139 (goto-char lspos)
7140 (setq marker (org-agenda-new-marker))
7141 (org-add-props txt props
7142 'org-marker marker 'org-hd-marker marker 'category category)
7143 (push txt rtn))
7144 ;; if we are to skip sublevels, jump to end of subtree
7145 (point)
7146 (or org-tags-match-list-sublevels (org-end-of-subtree)))))
7147 (nreverse rtn)))
7149 (defun org-tags-sparse-tree (&optional arg match)
7150 "Create a sparse tree according to tags search string MATCH.
7151 MATCH can contain positive and negative selection of tags, like
7152 \"+WORK+URGENT-WITHBOSS\"."
7153 (interactive "P")
7154 (let ((org-show-following-heading nil)
7155 (org-show-hierarchy-above nil))
7156 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)))))
7158 (defun org-make-tags-matcher (match)
7159 "Create the TAGS matcher form for the tags-selecting string MATCH."
7160 (unless match
7161 ;; Get a new match request, with completion
7162 (setq org-last-tags-completion-table
7163 (or (org-get-buffer-tags)
7164 org-last-tags-completion-table))
7165 (setq match (completing-read
7166 "Tags: " 'org-tags-completion-function nil nil nil
7167 'org-tags-history)))
7168 ;; parse the string and create a lisp form
7169 (let ((match0 match) minus tag mm matcher orterms term orlist)
7170 (setq orterms (org-split-string match "|"))
7171 (while (setq term (pop orterms))
7172 (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
7173 (setq minus (and (match-end 1)
7174 (equal (match-string 1 term) "-"))
7175 tag (match-string 2 term)
7176 term (substring term (match-end 0))
7177 mm (list 'member (downcase tag) 'tags-list)
7178 mm (if minus (list 'not mm) mm))
7179 (push mm matcher))
7180 (push (if (> (length matcher) 1) (cons 'and matcher) (car matcher))
7181 orlist)
7182 (setq matcher nil))
7183 (setq matcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
7184 ;; Return the string and lisp forms of the matcher
7185 (cons match0 matcher)))
7187 ;;;###autoload
7188 (defun org-tags-view (&optional todo-only match keep-modes)
7189 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
7190 The prefix arg TODO-ONLY limits the search to TODO entries."
7191 (interactive "P")
7192 (org-agenda-maybe-reset-markers 'force)
7193 (org-compile-prefix-format org-agenda-prefix-format)
7194 (let* ((org-agenda-keep-modes keep-modes)
7195 (org-tags-match-list-sublevels
7196 (if todo-only t org-tags-match-list-sublevels))
7197 (win (selected-window))
7198 (completion-ignore-case t)
7199 rtn rtnall files file pos matcher
7200 buffer)
7201 (setq matcher (org-make-tags-matcher match)
7202 match (car matcher) matcher (cdr matcher))
7203 (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
7204 (progn
7205 (delete-other-windows)
7206 (switch-to-buffer-other-window
7207 (get-buffer-create org-agenda-buffer-name))))
7208 (setq buffer-read-only nil)
7209 (erase-buffer)
7210 (org-agenda-mode) (setq buffer-read-only nil)
7211 (set (make-local-variable 'org-agenda-type) 'tags)
7212 (set (make-local-variable 'org-agenda-redo-command)
7213 (list 'org-tags-view (list 'quote todo-only)
7214 (list 'if 'current-prefix-arg nil match) t))
7215 (setq files (org-agenda-files)
7216 rtnall nil)
7217 (while (setq file (pop files))
7218 (catch 'nextfile
7219 (org-check-agenda-file file)
7220 (setq buffer (if (file-exists-p file)
7221 (org-get-agenda-file-buffer file)
7222 (error "No such file %s" file)))
7223 (if (not buffer)
7224 ;; If file does not exist, merror message to agenda
7225 (setq rtn (list
7226 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
7227 rtnall (append rtnall rtn))
7228 (with-current-buffer buffer
7229 (unless (eq major-mode 'org-mode)
7230 (error "Agenda file %s is not in `org-mode'" file))
7231 (save-excursion
7232 (save-restriction
7233 (if org-respect-restriction
7234 (if (org-region-active-p)
7235 ;; Respect a region to restrict search
7236 (narrow-to-region (region-beginning) (region-end)))
7237 ;; If we work for the calendar or many files,
7238 ;; get rid of any restriction
7239 (widen))
7240 (setq rtn (org-scan-tags 'agenda matcher todo-only))
7241 (setq rtnall (append rtnall rtn))))))))
7242 (insert "Headlines with TAGS match: ")
7243 (add-text-properties (point-min) (1- (point))
7244 (list 'face 'org-level-3))
7245 (setq pos (point))
7246 (insert match "\n")
7247 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
7248 (setq pos (point))
7249 (insert "Press `C-u r' to search again with new search string\n")
7250 (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
7251 (when rtnall
7252 (insert (mapconcat 'identity rtnall "\n")))
7253 (goto-char (point-min))
7254 (setq buffer-read-only t)
7255 (org-fit-agenda-window)
7256 (if (not org-select-agenda-window) (select-window win))))
7258 (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
7259 (defun org-set-tags (&optional arg just-align)
7260 "Set the tags for the current headline.
7261 With prefix ARG, realign all tags in headings in the current buffer."
7262 (interactive)
7263 (let* (;(inherit (org-get-inherited-tags))
7264 (re (concat "^" outline-regexp))
7265 (col (current-column))
7266 (current (org-get-tags))
7267 tags hd empty invis)
7268 (if arg
7269 (save-excursion
7270 (goto-char (point-min))
7271 (while (re-search-forward re nil t)
7272 (org-set-tags nil t))
7273 (message "All tags realigned to column %d" org-tags-column))
7274 (if just-align
7275 (setq tags current)
7276 (setq org-last-tags-completion-table
7277 (or (org-get-buffer-tags)
7278 org-last-tags-completion-table))
7279 (setq tags
7280 (let ((org-add-colon-after-tag-completion t))
7281 (completing-read "Tags: " 'org-tags-completion-function
7282 nil nil current 'org-tags-history)))
7283 (while (string-match "[-+&]+" tags)
7284 (setq tags (replace-match ":" t t tags))))
7285 ;; FIXME: still optimize this by not checking when JUST-ALIGN?
7286 (unless (setq empty (string-match "\\`[\t ]*\\'" tags))
7287 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
7288 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
7289 (if (equal current "")
7290 (progn
7291 (end-of-line 1)
7292 (or empty (insert-before-markers " ")))
7293 (beginning-of-line 1)
7294 (setq invis (org-invisible-p))
7295 (looking-at (concat "\\(.*\\)\\(" (regexp-quote current) "\\)[ \t]*"))
7296 (setq hd (match-string 1))
7297 (delete-region (match-beginning 0) (match-end 0))
7298 (insert-before-markers (org-trim hd) (if empty "" " ")))
7299 (unless (equal tags "")
7300 (move-to-column (max (current-column)
7301 (if (> org-tags-column 0)
7302 org-tags-column
7303 (- (- org-tags-column) (length tags))))
7305 (insert-before-markers tags)
7306 (if (and (not invis) (org-invisible-p))
7307 (outline-flag-region (point-at-bol) (point) nil)))
7308 (move-to-column col))))
7310 (defun org-tags-completion-function (string predicate &optional flag)
7311 (let (s1 s2 rtn (ctable org-last-tags-completion-table))
7312 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
7313 (setq s1 (match-string 1 string)
7314 s2 (match-string 2 string))
7315 (setq s1 "" s2 string))
7316 (cond
7317 ((eq flag nil)
7318 ;; try completion
7319 (setq rtn (try-completion s2 ctable))
7320 (if (stringp rtn)
7321 (concat s1 s2 (substring rtn (length s2))
7322 (if (and org-add-colon-after-tag-completion
7323 (assoc rtn ctable))
7324 ":" "")))
7326 ((eq flag t)
7327 ;; all-completions
7328 (all-completions s2 ctable)
7330 ((eq flag 'lambda)
7331 ;; exact match?
7332 (assoc s2 ctable)))
7335 (defun org-get-tags ()
7336 "Get the TAGS string in the current headline."
7337 (unless (org-on-heading-p)
7338 (error "Not on a heading"))
7339 (save-excursion
7340 (beginning-of-line 1)
7341 (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
7342 (org-match-string-no-properties 1)
7343 "")))
7345 (defun org-get-buffer-tags ()
7346 "Get a table of all tags used in the buffer, for completion."
7347 (let (tags)
7348 (save-excursion
7349 (goto-char (point-min))
7350 (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
7351 (mapc (lambda (x) (add-to-list 'tags x))
7352 (org-split-string (org-match-string-no-properties 1) ":"))))
7353 (mapcar 'list tags)))
7355 ;;; Link Stuff
7357 (defvar org-create-file-search-functions nil
7358 "List of functions to construct the right search string for a file link.
7359 These functions are called in turn with point at the location to
7360 which the link should point.
7362 A function in the hook should first test if it would like to
7363 handle this file type, for example by checking the major-mode or
7364 the file extension. If it decides not to handle this file, it
7365 should just return nil to give other functions a chance. If it
7366 does handle the file, it must return the search string to be used
7367 when following the link. The search string will be part of the
7368 file link, given after a double colon, and `org-open-at-point'
7369 will automatically search for it. If special measures must be
7370 taken to make the search successful, another function should be
7371 added to the companion hook `org-execute-file-search-functions',
7372 which see.
7374 A function in this hook may also use `setq' to set the variable
7375 `description' to provide a suggestion for the descriptive text to
7376 be used for this link when it gets inserted into an Org-mode
7377 buffer with \\[org-insert-link].")
7379 (defvar org-execute-file-search-functions nil
7380 "List of functions to execute a file search triggered by a link.
7382 Functions added to this hook must accept a single argument, the
7383 search string that was part of the file link, the part after the
7384 double colon. The function must first check if it would like to
7385 handle this search, for example by checking the major-mode or the
7386 file extension. If it decides not to handle this search, it
7387 should just return nil to give other functions a chance. If it
7388 does handle the search, it must return a non-nil value to keep
7389 other functions from trying.
7391 Each function can access the current prefix argument through the
7392 variable `current-prefix-argument'. Note that a single prefix is
7393 used to force opening a link in Emacs, so it may be good to only
7394 use a numeric or double prefix to guide the search function.
7396 In case this is needed, a function in this hook can also restore
7397 the window configuration before `org-open-at-point' was called using:
7399 (set-window-configuration org-window-config-before-follow-link)")
7401 (defun org-find-file-at-mouse (ev)
7402 "Open file link or URL at mouse."
7403 (interactive "e")
7404 (mouse-set-point ev)
7405 (org-open-at-point 'in-emacs))
7407 (defun org-open-at-mouse (ev)
7408 "Open file link or URL at mouse."
7409 (interactive "e")
7410 (mouse-set-point ev)
7411 (org-open-at-point))
7413 (defvar org-window-config-before-follow-link nil
7414 "The window configuration before following a link.
7415 This is saved in case the need arises to restore it.")
7417 (defun org-open-at-point (&optional in-emacs)
7418 "Open link at or after point.
7419 If there is no link at point, this function will search forward up to
7420 the end of the current subtree.
7421 Normally, files will be opened by an appropriate application. If the
7422 optional argument IN-EMACS is non-nil, Emacs will visit the file."
7423 (interactive "P")
7424 (setq org-window-config-before-follow-link (current-window-configuration))
7425 (org-remove-occur-highlights nil nil t)
7426 (if (org-at-timestamp-p)
7427 (org-agenda-list nil (time-to-days (org-time-string-to-time
7428 (substring (match-string 1) 0 10)))
7430 (let (type path link line search (pos (point)))
7431 (catch 'match
7432 (save-excursion
7433 (skip-chars-forward "^]\n\r")
7434 (when (and (re-search-backward "\\[\\[" nil t)
7435 (looking-at org-bracket-link-regexp)
7436 (<= (match-beginning 0) pos)
7437 (>= (match-end 0) pos))
7438 (setq link (org-link-unescape (org-match-string-no-properties 1)))
7439 (while (string-match " *\n *" link)
7440 (setq link (replace-match " " t t link)))
7441 (if (string-match org-link-re-with-space2 link)
7442 (setq type (match-string 1 link)
7443 path (match-string 2 link))
7444 (setq type "thisfile"
7445 path link))
7446 (throw 'match t)))
7448 (when (get-text-property (point) 'org-linked-text)
7449 (setq type "thisfile"
7450 pos (if (get-text-property (1+ (point)) 'org-linked-text)
7451 (1+ (point)) (point))
7452 path (buffer-substring
7453 (previous-single-property-change pos 'org-linked-text)
7454 (next-single-property-change pos 'org-linked-text)))
7455 (throw 'match t))
7457 (save-excursion
7458 (skip-chars-backward (concat "^[]" org-non-link-chars " "))
7459 (if (equal (char-before) ?<) (backward-char 1))
7460 (when (or (looking-at org-angle-link-re)
7461 (looking-at org-plain-link-re)
7462 (and (or (re-search-forward org-angle-link-re (point-at-eol) t)
7463 (re-search-forward org-plain-link-re (point-at-eol) t))
7464 (<= (match-beginning 0) pos)
7465 (>= (match-end 0) pos)))
7466 (setq type (match-string 1)
7467 path (match-string 2))
7468 (throw 'match t)))
7469 (save-excursion
7470 (skip-chars-backward "^ \t\n\r")
7471 (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
7472 (setq type "tags"
7473 path (match-string 1))
7474 (while (string-match ":" path)
7475 (setq path (replace-match "+" t t path)))
7476 (throw 'match t)))
7477 (save-excursion
7478 (skip-chars-backward "a-zA-Z_")
7479 (when (and (memq 'camel org-activate-links)
7480 (looking-at org-camel-regexp))
7481 (setq type "camel" path (match-string 0))
7482 (if (equal (char-before) ?*)
7483 (setq path (concat "*" path))))
7484 (throw 'match t)))
7485 (unless path
7486 (error "No link found"))
7487 ;; Remove any trailing spaces in path
7488 (if (string-match " +\\'" path)
7489 (setq path (replace-match "" t t path)))
7491 (cond
7493 ((member type '("http" "https" "ftp" "mailto" "news"))
7494 (browse-url (concat type ":" path)))
7496 ((string= type "tags")
7497 (org-tags-view in-emacs path))
7498 ((or (string= type "camel")
7499 (string= type "thisfile"))
7500 (org-mark-ring-push)
7501 (org-link-search
7502 path
7503 (cond ((equal in-emacs '(4)) 'occur)
7504 ((equal in-emacs '(16)) 'org-occur)
7505 (t nil))))
7507 ((string= type "file")
7508 (if (string-match "::?\\([0-9]+\\)\\'" path) ;; second : optional
7509 ;; FIXME: It is unsafe to allow a single colon.
7510 (setq line (string-to-number (match-string 1 path))
7511 path (substring path 0 (match-beginning 0)))
7512 (if (string-match "::\\(.+\\)\\'" path)
7513 (setq search (match-string 1 path)
7514 path (substring path 0 (match-beginning 0)))))
7515 (org-open-file path in-emacs line search))
7517 ((string= type "news")
7518 (org-follow-gnus-link path))
7520 ((string= type "bbdb")
7521 (org-follow-bbdb-link path))
7523 ((string= type "gnus")
7524 (let (group article)
7525 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
7526 (error "Error in Gnus link"))
7527 (setq group (match-string 1 path)
7528 article (match-string 3 path))
7529 (org-follow-gnus-link group article)))
7531 ((string= type "vm")
7532 (let (folder article)
7533 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
7534 (error "Error in VM link"))
7535 (setq folder (match-string 1 path)
7536 article (match-string 3 path))
7537 ;; in-emacs is the prefix arg, will be interpreted as read-only
7538 (org-follow-vm-link folder article in-emacs)))
7540 ((string= type "wl")
7541 (let (folder article)
7542 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
7543 (error "Error in Wanderlust link"))
7544 (setq folder (match-string 1 path)
7545 article (match-string 3 path))
7546 (org-follow-wl-link folder article)))
7548 ((string= type "mhe")
7549 (let (folder article)
7550 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
7551 (error "Error in MHE link"))
7552 (setq folder (match-string 1 path)
7553 article (match-string 3 path))
7554 (org-follow-mhe-link folder article)))
7556 ((string= type "rmail")
7557 (let (folder article)
7558 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
7559 (error "Error in RMAIL link"))
7560 (setq folder (match-string 1 path)
7561 article (match-string 3 path))
7562 (org-follow-rmail-link folder article)))
7564 ((string= type "shell")
7565 (let ((cmd path))
7566 (while (string-match "@{" cmd)
7567 (setq cmd (replace-match "<" t t cmd)))
7568 (while (string-match "@}" cmd)
7569 (setq cmd (replace-match ">" t t cmd)))
7570 (if (or (not org-confirm-shell-links)
7571 (funcall org-confirm-shell-links
7572 (format "Execute \"%s\" in shell? "
7573 (org-add-props cmd nil
7574 'face 'org-warning))))
7575 (progn
7576 (message "Executing %s" cmd)
7577 (shell-command cmd))
7578 (error "Abort"))))
7581 (browse-url-at-point))))))
7583 (defun org-link-search (s &optional type)
7584 "Search for a link search option.
7585 When S is a CamelCaseWord, search for a target, or for a sentence containing
7586 the words. If S is surrounded by forward slashes, it is interpreted as a
7587 regular expression. In org-mode files, this will create an `org-occur'
7588 sparse tree. In ordinary files, `occur' will be used to list matches.
7589 If the current buffer is in `dired-mode', grep will be used to search
7590 in all files."
7591 (let ((case-fold-search t)
7592 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
7593 (pos (point))
7594 (pre "") (post "")
7595 words re0 re1 re2 re3 re4 re5 re2a reall camel)
7596 (cond
7597 ;; First check if there are any special
7598 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
7599 ;; Now try the builtin stuff
7600 ((save-excursion
7601 (goto-char (point-min))
7602 (and
7603 (re-search-forward
7604 (concat "<<" (regexp-quote s0) ">>") nil t)
7605 (setq pos (match-beginning 0))))
7606 ;; There is an exact target for this
7607 (goto-char pos))
7608 ((string-match "^/\\(.*\\)/$" s)
7609 ;; A regular expression
7610 (cond
7611 ((eq major-mode 'org-mode)
7612 (org-occur (match-string 1 s)))
7613 ;;((eq major-mode 'dired-mode)
7614 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
7615 (t (org-do-occur (match-string 1 s)))))
7616 ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
7618 ;; A camel or a normal search string
7619 (when (equal (string-to-char s) ?*)
7620 ;; Anchor on headlines, post may include tags.
7621 (setq pre "^\\*+[ \t]*\\(?:\\sw+\\)?[ \t]*"
7622 post "[ \t]*\\(?:[ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
7623 s (substring s 1)))
7624 (remove-text-properties
7625 0 (length s)
7626 '(face nil mouse-face nil keymap nil fontified nil) s)
7627 ;; Make a series of regular expressions to find a match
7628 (setq words
7629 (if camel
7630 (org-camel-to-words s)
7631 (org-split-string s "[ \n\r\t]+"))
7632 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
7633 ;; FIXME: The word delimiters in the following are not quite correct.
7634 ; re2 (concat "\\<" (mapconcat 'downcase words "[ \t]+") "\\>")
7635 ; re2a (concat "\\<" (mapconcat 'downcase words "[ \t\r\n]+") "\\>")
7636 ; re4 (concat "\\<" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\>")
7637 re2 (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t]+") "\\)[ \t\r\n]")
7638 re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
7639 re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
7640 re1 (concat pre re2 post)
7641 re3 (concat pre re4 post)
7642 re5 (concat pre ".*" re4)
7643 re2 (concat pre re2)
7644 re2a (concat pre re2a)
7645 re4 (concat pre re4)
7646 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
7647 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
7648 re5 "\\)"
7650 (cond
7651 ((eq type 'org-occur) (org-occur reall))
7652 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
7653 (t (goto-char (point-min))
7654 (if (or (org-search-not-link re0 nil t)
7655 (org-search-not-link re1 nil t)
7656 (org-search-not-link re2 nil t)
7657 (org-search-not-link re2a nil t)
7658 (org-search-not-link re3 nil t)
7659 (org-search-not-link re4 nil t)
7660 (org-search-not-link re5 nil t)
7662 (goto-char (match-beginning 1)) ;; Fixme: does every re have group 1?
7663 (goto-char pos)
7664 (error "No match")))))
7666 ;; Normal string-search
7667 (goto-char (point-min))
7668 (if (search-forward s nil t)
7669 (goto-char (match-beginning 0))
7670 (error "No match"))))
7671 (and (eq major-mode 'org-mode) (org-show-hierarchy-above))))
7673 (defun org-search-not-link (&rest args)
7674 "Execute `re-search-forward', but only accept matches that are not a link."
7675 (catch 'exit
7676 (let ((pos (point)) p1)
7677 (while (apply 're-search-forward args)
7678 (setq p1 (point))
7679 (if (not (save-match-data
7680 (and (re-search-backward "\\[\\[" nil t)
7681 (looking-at org-bracket-link-regexp)
7682 (<= (match-beginning 0) p1)
7683 (>= (match-end 0) p1))))
7684 (progn (goto-char (match-end 0))
7685 (throw 'exit (point)))
7686 (goto-char (match-end 0)))))))
7688 (defun org-do-occur (regexp &optional cleanup)
7689 "Call the Emacs command `occur'.
7690 If CLEANUP is non-nil, remove the printout of the regular expression
7691 in the *Occur* buffer. This is useful if the regex is long and not useful
7692 to read."
7693 (occur regexp)
7694 (when cleanup
7695 (let ((cwin (selected-window)) win beg end)
7696 (when (setq win (get-buffer-window "*Occur*"))
7697 (select-window win))
7698 (goto-char (point-min))
7699 (when (re-search-forward "match[a-z]+" nil t)
7700 (setq beg (match-end 0))
7701 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
7702 (setq end (1- (match-beginning 0)))))
7703 (and beg end (let ((buffer-read-only)) (delete-region beg end)))
7704 (goto-char (point-min))
7705 (select-window cwin))))
7707 (defvar org-mark-ring nil
7708 "Mark ring for positions before jumps in Org-mode.")
7709 (defvar org-mark-ring-last-goto nil
7710 "Last position in the mark ring used to go back.")
7711 ;; Fill and close the ring
7712 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
7713 (loop for i from 1 to org-mark-ring-length do
7714 (push (make-marker) org-mark-ring))
7715 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
7716 org-mark-ring)
7718 (defun org-mark-ring-push (&optional pos buffer)
7719 "Put the current position or POS into the mark ring and rotate it."
7720 (interactive)
7721 (setq pos (or pos (point)))
7722 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
7723 (move-marker (car org-mark-ring)
7724 (or pos (point))
7725 (or buffer (current-buffer)))
7726 (message
7727 (substitute-command-keys
7728 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
7730 (defun org-mark-ring-goto (&optional n)
7731 "Jump to the previous position in the mark ring.
7732 With prefix arg N, jump back that many stored positions. When
7733 called several times in succession, walk through the entire ring.
7734 Org-mode commands jumping to a different position in the current file,
7735 or to another Org-mode file, automatically push the old position
7736 onto the ring."
7737 (interactive "p")
7738 (let (p m)
7739 (if (eq last-command this-command)
7740 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
7741 (setq p org-mark-ring))
7742 (setq org-mark-ring-last-goto p)
7743 (setq m (car p))
7744 (switch-to-buffer (marker-buffer m))
7745 (goto-char m)
7746 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-hierarchy-above))))
7748 (defun org-camel-to-words (s)
7749 "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
7750 (let ((case-fold-search nil)
7751 words)
7752 (while (string-match "[a-z][A-Z]" s)
7753 (push (substring s 0 (1+ (match-beginning 0))) words)
7754 (setq s (substring s (1+ (match-beginning 0)))))
7755 (nreverse (cons s words))))
7757 (defun org-remove-angle-brackets (s)
7758 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
7759 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
7761 (defun org-add-angle-brackets (s)
7762 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
7763 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
7766 (defun org-follow-bbdb-link (name)
7767 "Follow a BBDB link to NAME."
7768 (require 'bbdb)
7769 (let ((inhibit-redisplay t)
7770 (bbdb-electric-p nil))
7771 (catch 'exit
7772 ;; Exact match on name
7773 (bbdb-name (concat "\\`" name "\\'") nil)
7774 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
7775 ;; Exact match on name
7776 (bbdb-company (concat "\\`" name "\\'") nil)
7777 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
7778 ;; Partial match on name
7779 (bbdb-name name nil)
7780 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
7781 ;; Partial match on company
7782 (bbdb-company name nil)
7783 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
7784 ;; General match including network address and notes
7785 (bbdb name nil)
7786 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
7787 (delete-window (get-buffer-window "*BBDB*"))
7788 (error "No matching BBDB record")))))
7790 (defun org-follow-gnus-link (&optional group article)
7791 "Follow a Gnus link to GROUP and ARTICLE."
7792 (require 'gnus)
7793 (funcall (cdr (assq 'gnus org-link-frame-setup)))
7794 (if group (gnus-fetch-group group))
7795 (if article
7796 (or (gnus-summary-goto-article article nil 'force)
7797 (if (fboundp 'gnus-summary-insert-cached-articles)
7798 (progn
7799 (gnus-summary-insert-cached-articles)
7800 (gnus-summary-goto-article article nil 'force))
7801 (message "Message could not be found.")))))
7803 (defun org-follow-vm-link (&optional folder article readonly)
7804 "Follow a VM link to FOLDER and ARTICLE."
7805 (require 'vm)
7806 (setq article (org-add-angle-brackets article))
7807 (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
7808 ;; ange-ftp or efs or tramp access
7809 (let ((user (or (match-string 1 folder) (user-login-name)))
7810 (host (match-string 2 folder))
7811 (file (match-string 3 folder)))
7812 (cond
7813 ((featurep 'tramp)
7814 ;; use tramp to access the file
7815 (if (featurep 'xemacs)
7816 (setq folder (format "[%s@%s]%s" user host file))
7817 (setq folder (format "/%s@%s:%s" user host file))))
7819 ;; use ange-ftp or efs
7820 (require (if (featurep 'xemacs) 'efs 'ange-ftp))
7821 (setq folder (format "/%s@%s:%s" user host file))))))
7822 (when folder
7823 (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
7824 (sit-for 0.1)
7825 (when article
7826 (vm-select-folder-buffer)
7827 (widen)
7828 (let ((case-fold-search t))
7829 (goto-char (point-min))
7830 (if (not (re-search-forward
7831 (concat "^" "message-id: *" (regexp-quote article))))
7832 (error "Could not find the specified message in this folder"))
7833 (vm-isearch-update)
7834 (vm-isearch-narrow)
7835 (vm-beginning-of-message)
7836 (vm-summarize)))))
7838 (defun org-follow-wl-link (folder article)
7839 "Follow a Wanderlust link to FOLDER and ARTICLE."
7840 (setq article (org-add-angle-brackets article))
7841 (wl-summary-goto-folder-subr folder 'no-sync t nil t)
7842 (if article (wl-summary-jump-to-msg-by-message-id article ">"))
7843 (wl-summary-redisplay))
7845 (defun org-follow-rmail-link (folder article)
7846 "Follow an RMAIL link to FOLDER and ARTICLE."
7847 (setq article (org-add-angle-brackets article))
7848 (let (message-number)
7849 (save-excursion
7850 (save-window-excursion
7851 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
7852 (setq message-number
7853 (save-restriction
7854 (widen)
7855 (goto-char (point-max))
7856 (if (re-search-backward
7857 (concat "^Message-ID:\\s-+" (regexp-quote
7858 (or article "")))
7859 nil t)
7860 (rmail-what-message))))))
7861 (if message-number
7862 (progn
7863 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
7864 (rmail-show-message message-number)
7865 message-number)
7866 (error "Message not found"))))
7868 ;; mh-e integration based on planner-mode
7869 (defun org-mhe-get-message-real-folder ()
7870 "Return the name of the current message real folder, so if you use
7871 sequences, it will now work."
7872 (save-excursion
7873 (let* ((folder
7874 (if (equal major-mode 'mh-folder-mode)
7875 mh-current-folder
7876 ;; Refer to the show buffer
7877 mh-show-folder-buffer))
7878 (end-index
7879 (if (boundp 'mh-index-folder)
7880 (min (length mh-index-folder) (length folder))))
7882 ;; a simple test on mh-index-data does not work, because
7883 ;; mh-index-data is always nil in a show buffer.
7884 (if (and (boundp 'mh-index-folder)
7885 (string= mh-index-folder (substring folder 0 end-index)))
7886 (if (equal major-mode 'mh-show-mode)
7887 (save-window-excursion
7888 (when (buffer-live-p (get-buffer folder))
7889 (progn
7890 (pop-to-buffer folder)
7891 (org-mhe-get-message-folder-from-index)
7894 (org-mhe-get-message-folder-from-index)
7896 folder
7900 (defun org-mhe-get-message-folder-from-index ()
7901 "Returns the name of the message folder in a index folder buffer."
7902 (save-excursion
7903 (mh-index-previous-folder)
7904 (re-search-forward "^\\(+.*\\)$" nil t)
7905 (message (match-string 1))))
7907 (defun org-mhe-get-message-folder ()
7908 "Return the name of the current message folder. Be careful if you
7909 use sequences."
7910 (save-excursion
7911 (if (equal major-mode 'mh-folder-mode)
7912 mh-current-folder
7913 ;; Refer to the show buffer
7914 mh-show-folder-buffer)))
7916 (defun org-mhe-get-message-num ()
7917 "Return the number of the current message. Be careful if you
7918 use sequences."
7919 (save-excursion
7920 (if (equal major-mode 'mh-folder-mode)
7921 (mh-get-msg-num nil)
7922 ;; Refer to the show buffer
7923 (mh-show-buffer-message-number))))
7925 (defun org-mhe-get-header (header)
7926 "Return a header of the message in folder mode. This will create a
7927 show buffer for the corresponding message. If you have a more clever
7928 idea..."
7929 (let* ((folder (org-mhe-get-message-folder))
7930 (num (org-mhe-get-message-num))
7931 (buffer (get-buffer-create (concat "show-" folder)))
7932 (header-field))
7933 (with-current-buffer buffer
7934 (mh-display-msg num folder)
7935 (if (equal major-mode 'mh-folder-mode)
7936 (mh-header-display)
7937 (mh-show-header-display))
7938 (set-buffer buffer)
7939 (setq header-field (mh-get-header-field header))
7940 (if (equal major-mode 'mh-folder-mode)
7941 (mh-show)
7942 (mh-show-show))
7943 header-field)))
7945 (defun org-follow-mhe-link (folder article)
7946 "Follow an MHE link to FOLDER and ARTICLE.
7947 If ARTICLE is nil FOLDER is shown. If the configuration variable
7948 `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
7949 ARTICLE is searched in all folders. Indexed searches (swish++,
7950 namazu, and others supported by MH-E) will always search in all
7951 folders."
7952 (require 'mh-e)
7953 (require 'mh-search)
7954 (require 'mh-utils)
7955 (mh-find-path)
7956 (if (not article)
7957 (mh-visit-folder (mh-normalize-folder-name folder))
7958 (setq article (org-add-angle-brackets article))
7959 (mh-search-choose)
7960 (if (equal mh-searcher 'pick)
7961 (progn
7962 (mh-search folder (list "--message-id" article))
7963 (when (and org-mhe-search-all-folders
7964 (not (org-mhe-get-message-real-folder)))
7965 (kill-this-buffer)
7966 (mh-search "+" (list "--message-id" article))))
7967 (mh-search "+" article))
7968 (if (org-mhe-get-message-real-folder)
7969 (mh-show-msg 1)
7970 (kill-this-buffer)
7971 (error "Message not found"))))
7973 ;; BibTeX links
7975 ;; Use the custom search meachnism to construct and use search strings for
7976 ;; file links to BibTeX database entries.
7978 (defun org-create-file-search-in-bibtex ()
7979 "Create the search string and description for a BibTeX database entry."
7980 (when (eq major-mode 'bibtex-mode)
7981 ;; yes, we want to construct this search string.
7982 ;; Make a good description for this entry, using names, year and the title
7983 ;; Put it into the `description' variable which is dynamically scoped.
7984 (let ((bibtex-autokey-names 1)
7985 (bibtex-autokey-names-stretch 1)
7986 (bibtex-autokey-name-case-convert-function 'identity)
7987 (bibtex-autokey-name-separator " & ")
7988 (bibtex-autokey-additional-names " et al.")
7989 (bibtex-autokey-year-length 4)
7990 (bibtex-autokey-name-year-separator " ")
7991 (bibtex-autokey-titlewords 3)
7992 (bibtex-autokey-titleword-separator " ")
7993 (bibtex-autokey-titleword-case-convert-function 'identity)
7994 (bibtex-autokey-titleword-length 'infty)
7995 (bibtex-autokey-year-title-separator ": "))
7996 (setq description (bibtex-generate-autokey)))
7997 ;; Now parse the entry, get the key and return it.
7998 (save-excursion
7999 (bibtex-beginning-of-entry)
8000 (cdr (assoc "=key=" (bibtex-parse-entry))))))
8002 (defun org-execute-file-search-in-bibtex (s)
8003 "Find the link search string S as a key for a database entry."
8004 (when (eq major-mode 'bibtex-mode)
8005 ;; Yes, we want to do the search in this file.
8006 ;; We construct a regexp that searches for "@entrytype{" followed by the key
8007 (goto-char (point-min))
8008 (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
8009 (regexp-quote s) "[ \t\n]*,") nil t)
8010 (goto-char (match-beginning 0)))
8011 (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
8012 ;; Use double prefix to indicate that any web link should be browsed
8013 (let ((b (current-buffer)) (p (point)))
8014 ;; Restore the window configuration because we just use the web link
8015 (set-window-configuration org-window-config-before-follow-link)
8016 (save-excursion (set-buffer b) (goto-char p)
8017 (bibtex-url)))
8018 (recenter 0)) ; Move entry start to beginning of window
8019 ;; return t to indicate that the search is done.
8022 ;; Finally add the functions to the right hooks.
8023 (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
8024 (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
8026 ;; end of Bibtex link setup
8028 (defun org-upgrade-old-links (&optional query-description)
8029 "Transfer old <...> style links to new [[...]] style links.
8030 With arg query-description, ask at each match for a description text to use
8031 for this link."
8032 (interactive (list (y-or-n-p "Would you like to be queried for a description at each link?")))
8033 (save-excursion
8034 (goto-char (point-min))
8035 (let ((re (concat "\\([^[]\\)<\\("
8036 "\\(" (mapconcat 'identity org-link-types "\\|")
8037 "\\):"
8038 "[^" org-non-link-chars "]+\\)>"))
8039 l1 l2 (cnt 0))
8040 (while (re-search-forward re nil t)
8041 (setq cnt (1+ cnt)
8042 l1 (org-match-string-no-properties 2)
8043 l2 (save-match-data (org-link-escape l1)))
8044 (when query-description (setq l1 (read-string "Desc: " l1)))
8045 (if (equal l1 l2)
8046 (replace-match (concat (match-string 1) "[[" l1 "]]") t t)
8047 (replace-match (concat (match-string 1) "[[" l2 "][" l1 "]]") t t)))
8048 (message "%d matches have beed treated" cnt))))
8050 (defun org-open-file (path &optional in-emacs line search)
8051 "Open the file at PATH.
8052 First, this expands any special file name abbreviations. Then the
8053 configuration variable `org-file-apps' is checked if it contains an
8054 entry for this file type, and if yes, the corresponding command is launched.
8055 If no application is found, Emacs simply visits the file.
8056 With optional argument IN-EMACS, Emacs will visit the file.
8057 Optional LINE specifies a line to go to, optional SEARCH a string to
8058 search for. If LINE or SEARCH is given, the file will always be
8059 opened in Emacs.
8060 If the file does not exist, an error is thrown."
8061 (setq in-emacs (or in-emacs line search))
8062 (let* ((file (if (equal path "")
8063 buffer-file-name
8064 (convert-standard-filename (org-expand-file-name path))))
8065 (dirp (file-directory-p file))
8066 (dfile (downcase file))
8067 (old-buffer (current-buffer))
8068 (old-pos (point))
8069 (old-mode major-mode)
8070 ext cmd apps)
8071 (if (and (not (file-exists-p file))
8072 (not org-open-non-existing-files))
8073 (error "No such file: %s" file))
8074 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
8075 (setq ext (match-string 1 dfile))
8076 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
8077 (setq ext (match-string 1 dfile))))
8078 (setq apps (append org-file-apps (org-default-apps)))
8079 (if in-emacs
8080 (setq cmd 'emacs)
8081 (setq cmd (or (and dirp (cdr (assoc 'directory apps)))
8082 (cdr (assoc ext apps))
8083 (cdr (assoc t apps)))))
8084 (when (eq cmd 'mailcap)
8085 (require 'mailcap)
8086 (mailcap-parse-mailcaps)
8087 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
8088 (command (mailcap-mime-info mime-type)))
8089 (if (stringp command)
8090 (setq cmd command)
8091 (setq cmd 'emacs))))
8092 (cond
8093 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
8094 ;; Normalize use of quote, this can vary.
8095 (if (string-match "['\"]%s['\"]" cmd)
8096 (setq cmd (replace-match "'%s'" t t cmd)))
8097 (setq cmd (format cmd file))
8098 (save-window-excursion
8099 (shell-command (concat cmd " &"))))
8100 ((or (stringp cmd)
8101 (eq cmd 'emacs))
8102 (unless (equal (file-truename file) (file-truename (or buffer-file-name "")))
8103 (funcall (cdr (assq 'file org-link-frame-setup)) file))
8104 (if line (goto-line line)
8105 (if search (org-link-search search))))
8106 ((consp cmd)
8107 (eval cmd))
8108 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
8109 (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
8110 (or (not (equal old-buffer (current-buffer)))
8111 (not (equal old-pos (point))))
8112 (org-mark-ring-push old-pos old-buffer))))
8114 (defun org-default-apps ()
8115 "Return the default applications for this operating system."
8116 (cond
8117 ((eq system-type 'darwin)
8118 org-file-apps-defaults-macosx)
8119 ((eq system-type 'windows-nt)
8120 org-file-apps-defaults-windowsnt)
8121 (t org-file-apps-defaults-gnu)))
8123 (defun org-expand-file-name (path)
8124 "Replace special path abbreviations and expand the file name."
8125 (expand-file-name path))
8128 (defvar org-insert-link-history nil
8129 "Minibuffer history for links inserted with `org-insert-link'.")
8131 (defvar org-stored-links nil
8132 "Contains the links stored with `org-store-link'.")
8134 ;;;###autoload
8135 (defun org-store-link (arg)
8136 "\\<org-mode-map>Store an org-link to the current location.
8137 This link can later be inserted into an org-buffer with
8138 \\[org-insert-link].
8139 For some link types, a prefix arg is interpreted:
8140 For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
8141 For file links, arg negates `org-context-in-file-links'."
8142 (interactive "P")
8143 (let (link cpltxt desc description search txt (pos (point)))
8144 (cond
8146 ((eq major-mode 'bbdb-mode)
8147 (setq cpltxt (concat
8148 "bbdb:"
8149 (or (bbdb-record-name (bbdb-current-record))
8150 (bbdb-record-company (bbdb-current-record))))
8151 link (org-make-link cpltxt)))
8153 ((eq major-mode 'calendar-mode)
8154 (let ((cd (calendar-cursor-to-date)))
8155 (setq link
8156 (format-time-string
8157 (car org-time-stamp-formats)
8158 (apply 'encode-time
8159 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
8160 nil nil nil))))))
8162 ((or (eq major-mode 'vm-summary-mode)
8163 (eq major-mode 'vm-presentation-mode))
8164 (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
8165 (vm-follow-summary-cursor)
8166 (save-excursion
8167 (vm-select-folder-buffer)
8168 (let* ((message (car vm-message-pointer))
8169 (folder buffer-file-name)
8170 (subject (vm-su-subject message))
8171 (author (vm-su-full-name message))
8172 (message-id (vm-su-message-id message)))
8173 (setq message-id (org-remove-angle-brackets message-id))
8174 (setq folder (abbreviate-file-name folder))
8175 (if (string-match (concat "^" (regexp-quote vm-folder-directory))
8176 folder)
8177 (setq folder (replace-match "" t t folder)))
8178 (setq cpltxt (concat author " on: " subject))
8179 (setq link (org-make-link "vm:" folder "#" message-id)))))
8181 ((eq major-mode 'wl-summary-mode)
8182 (let* ((msgnum (wl-summary-message-number))
8183 (message-id (elmo-message-field wl-summary-buffer-elmo-folder
8184 msgnum 'message-id))
8185 (wl-message-entity (elmo-msgdb-overview-get-entity
8186 msgnum (wl-summary-buffer-msgdb)))
8187 (author (wl-summary-line-from)) ; FIXME: correct?
8188 (subject "???")) ; FIXME:
8189 (setq message-id (org-remove-angle-brackets message-id))
8190 (setq cpltxt (concat author " on: " subject))
8191 (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
8192 "#" message-id))))
8194 ((or (equal major-mode 'mh-folder-mode)
8195 (equal major-mode 'mh-show-mode))
8196 (let ((from-header (org-mhe-get-header "From:"))
8197 (to-header (org-mhe-get-header "To:"))
8198 (subject (org-mhe-get-header "Subject:")))
8199 (setq cpltxt (concat from-header " on: " subject))
8200 (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
8201 (org-remove-angle-brackets
8202 (org-mhe-get-header "Message-Id:"))))))
8204 ((eq major-mode 'rmail-mode)
8205 (save-excursion
8206 (save-restriction
8207 (rmail-narrow-to-non-pruned-header)
8208 (let ((folder buffer-file-name)
8209 (message-id (mail-fetch-field "message-id"))
8210 (author (mail-fetch-field "from"))
8211 (subject (mail-fetch-field "subject")))
8212 (setq message-id (org-remove-angle-brackets message-id))
8213 (setq cpltxt (concat author " on: " subject))
8214 (setq link (org-make-link "rmail:" folder "#" message-id))))))
8216 ((eq major-mode 'gnus-group-mode)
8217 (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
8218 (gnus-group-group-name)) ; version
8219 ((fboundp 'gnus-group-name)
8220 (gnus-group-name))
8221 (t "???"))))
8222 (setq cpltxt (concat
8223 (if (org-xor arg org-usenet-links-prefer-google)
8224 "http://groups.google.com/groups?group="
8225 "gnus:")
8226 group)
8227 link (org-make-link cpltxt))))
8229 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
8230 (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
8231 (gnus-summary-beginning-of-article)
8232 (let* ((group (car gnus-article-current))
8233 (article (cdr gnus-article-current))
8234 (header (gnus-summary-article-header article))
8235 (author (mail-header-from header))
8236 (message-id (mail-header-id header))
8237 (date (mail-header-date header))
8238 (subject (gnus-summary-subject-string)))
8239 (setq cpltxt (concat author " on: " subject))
8240 (if (org-xor arg org-usenet-links-prefer-google)
8241 (setq link
8242 (concat
8243 cpltxt "\n "
8244 (format "http://groups.google.com/groups?as_umsgid=%s"
8245 (org-fixup-message-id-for-http message-id))))
8246 (setq link (org-make-link "gnus:" group
8247 "#" (number-to-string article))))))
8249 ((eq major-mode 'w3-mode)
8250 (setq cpltxt (url-view-url t)
8251 link (org-make-link cpltxt)))
8252 ((eq major-mode 'w3m-mode)
8253 (setq cpltxt w3m-current-url
8254 link (org-make-link cpltxt)))
8256 ((setq search (run-hook-with-args-until-success
8257 'org-create-file-search-functions))
8258 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
8259 "::" search))
8260 (setq cpltxt (or description link))) ;; FIXME: is this the best way?
8262 ((eq major-mode 'org-mode)
8263 ;; Just link to current headline
8264 (setq cpltxt (concat "file:"
8265 (abbreviate-file-name buffer-file-name)))
8266 ;; Add a context search string
8267 (when (org-xor org-context-in-file-links arg)
8268 ;; Check if we are on a target
8269 (if (save-excursion
8270 (skip-chars-forward "^>\n\r")
8271 (and (re-search-backward "<<" nil t)
8272 (looking-at "<<\\(.*?\\)>>")
8273 (<= (match-beginning 0) pos)
8274 (>= (match-end 0) pos)))
8275 (setq cpltxt (concat cpltxt "::" (match-string 1)))
8276 (setq txt (cond
8277 ((org-on-heading-p) nil)
8278 ((org-region-active-p)
8279 (buffer-substring (region-beginning) (region-end)))
8280 (t (buffer-substring (point-at-bol) (point-at-eol)))))
8281 (setq cpltxt
8282 (concat cpltxt "::"
8283 (if org-file-link-context-use-camel-case
8284 (org-make-org-heading-camel txt)
8285 (org-make-org-heading-search-string txt)))
8286 desc "NONE")))
8287 (if (string-match "::\\'" cpltxt)
8288 (setq cpltxt (substring cpltxt 0 -2)))
8289 (setq link (org-make-link cpltxt)))
8291 (buffer-file-name
8292 ;; Just link to this file here.
8293 (setq cpltxt (concat "file:"
8294 (abbreviate-file-name buffer-file-name)))
8295 ;; Add a context string
8296 (when (org-xor org-context-in-file-links arg)
8297 (setq txt (if (org-region-active-p)
8298 (buffer-substring (region-beginning) (region-end))
8299 (buffer-substring (point-at-bol) (point-at-eol))))
8300 (setq cpltxt
8301 (concat cpltxt "::"
8302 (if org-file-link-context-use-camel-case
8303 (org-make-org-heading-camel txt)
8304 (org-make-org-heading-search-string txt)))
8305 desc "NONE"))
8306 (setq link (org-make-link cpltxt)))
8308 ((interactive-p)
8309 (error "Cannot link to a buffer which is not visiting a file"))
8311 (t (setq link nil)))
8313 (if (consp link) (setq cpltxt (car link) link (cdr link)))
8314 (setq link (or link cpltxt)
8315 desc (or desc cpltxt))
8316 (if (equal desc "NONE") (setq desc nil))
8318 (if (and (interactive-p) link)
8319 (progn
8320 (setq org-stored-links
8321 (cons (list cpltxt link desc) org-stored-links))
8322 (message "Stored: %s" (or cpltxt link)))
8323 (org-make-link-string link desc))))
8325 (defun org-make-org-heading-search-string (&optional string heading)
8326 "Make search string for STRING or current headline."
8327 (interactive)
8328 (let ((s (or string (org-get-heading))))
8329 (unless (and string (not heading))
8330 ;; We are using a headline, clean up garbage in there.
8331 (if (string-match org-todo-regexp s)
8332 (setq s (replace-match "" t t s)))
8333 (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
8334 (setq s (replace-match "" t t s)))
8335 (setq s (org-trim s))
8336 (if (string-match (concat "^\\(" org-quote-string "\\|"
8337 org-comment-string "\\)") s)
8338 (setq s (replace-match "" t t s)))
8339 (while (string-match org-ts-regexp s)
8340 (setq s (replace-match "" t t s))))
8341 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
8342 (setq s (replace-match " " t t s)))
8343 (or string (setq s (concat "*" s))) ; Add * for headlines
8344 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
8346 (defun org-make-org-heading-camel (&optional string heading)
8347 "Make a CamelCase string for STRING or the current headline."
8348 (interactive)
8349 (let ((s (or string (org-get-heading))))
8350 (unless (and string (not heading))
8351 ;; We are using a headline, clean up garbage in there.
8352 (if (string-match org-todo-regexp s)
8353 (setq s (replace-match "" t t s)))
8354 (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
8355 (setq s (replace-match "" t t s)))
8356 (setq s (org-trim s))
8357 (if (string-match (concat "^\\(" org-quote-string "\\|"
8358 org-comment-string "\\)") s)
8359 (setq s (replace-match "" t t s)))
8360 (while (string-match org-ts-regexp s)
8361 (setq s (replace-match "" t t s))))
8362 (while (string-match "[^a-zA-Z_ \t]+" s)
8363 (setq s (replace-match " " t t s)))
8364 (or string (setq s (concat "*" s))) ; Add * for headlines
8365 (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
8367 (defun org-make-link (&rest strings)
8368 "Concatenate STRINGS, format resulting string with `org-link-format'."
8369 (format org-link-format (apply 'concat strings)))
8371 (defun org-make-link-string (link &optional description)
8372 "Make a link with brackets, consisting of LINK and DESCRIPTION."
8373 (if (eq org-link-style 'plain)
8374 (if (equal description link)
8375 link
8376 (concat description "\n" link))
8377 (when (stringp description)
8378 ;; Remove brackets from the description, they are fatal.
8379 (while (string-match "\\[\\|\\]" description)
8380 (setq description (replace-match "" t t description))))
8381 (when (equal (org-link-escape link) description)
8382 ;; No description needed, it is identical
8383 (setq description nil))
8384 (when (and (not description)
8385 (not (equal link (org-link-escape link))))
8386 (setq description link))
8387 (concat "[[" (org-link-escape link) "]"
8388 (if description (concat "[" description "]") "")
8389 "]")))
8391 (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
8392 "Association list of escapes for some characters problematic in links.")
8394 (defun org-link-escape (text)
8395 "Escape charaters in TEXT that are problematic for links."
8396 (when text
8397 (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
8398 org-link-escape-chars "\\|")))
8399 (while (string-match re text)
8400 (setq text
8401 (replace-match
8402 (cdr (assoc (match-string 0 text) org-link-escape-chars))
8403 t t text)))
8404 text)))
8406 (defun org-link-unescape (text)
8407 "Reverse the action of `org-link-escape'."
8408 (when text
8409 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
8410 org-link-escape-chars "\\|")))
8411 (while (string-match re text)
8412 (setq text
8413 (replace-match
8414 (car (rassoc (match-string 0 text) org-link-escape-chars))
8415 t t text)))
8416 text)))
8418 (defun org-xor (a b)
8419 "Exclusive or."
8420 (if a (not b) b))
8422 (defun org-get-header (header)
8423 "Find a header field in the current buffer."
8424 (save-excursion
8425 (goto-char (point-min))
8426 (let ((case-fold-search t) s)
8427 (cond
8428 ((eq header 'from)
8429 (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
8430 (setq s (match-string 1)))
8431 (while (string-match "\"" s)
8432 (setq s (replace-match "" t t s)))
8433 (if (string-match "[<(].*" s)
8434 (setq s (replace-match "" t t s))))
8435 ((eq header 'message-id)
8436 (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
8437 (setq s (match-string 1))))
8438 ((eq header 'subject)
8439 (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
8440 (setq s (match-string 1)))))
8441 (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
8442 (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
8443 s)))
8446 (defun org-fixup-message-id-for-http (s)
8447 "Replace special characters in a message id, so it can be used in an http query."
8448 (while (string-match "<" s)
8449 (setq s (replace-match "%3C" t t s)))
8450 (while (string-match ">" s)
8451 (setq s (replace-match "%3E" t t s)))
8452 (while (string-match "@" s)
8453 (setq s (replace-match "%40" t t s)))
8456 (defun org-insert-link (&optional complete-file)
8457 "Insert a link. At the prompt, enter the link.
8459 Completion can be used to select a link previously stored with
8460 `org-store-link'. When the empty string is entered (i.e. if you just
8461 press RET at the prompt), the link defaults to the most recently
8462 stored link. As SPC triggers completion in the minibuffer, you need to
8463 use M-SPC or C-q SPC to force the insertion of a space character.
8465 You will also be prompted for a description, and if one is given, it will
8466 be displayed in the buffer instead of the link.
8468 If there is already a link at point, this command will allow you to edit link
8469 and description parts.
8471 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
8472 selected using completion. The path to the file will be relative to
8473 the current directory if the file is in the current directory or a
8474 subdirectory. Otherwise, the link will be the absolute path as
8475 completed in the minibuffer (i.e. normally ~/path/to/file).
8477 With two \\[universal-argument] prefixes, enforce an absolute path even if the file
8478 is in the current directory or below."
8479 (interactive "P")
8480 (let (link desc entry remove file (pos (point)))
8481 (cond
8482 ((save-excursion
8483 (skip-chars-forward "^]\n\r")
8484 (and (re-search-backward "\\[\\[" nil t)
8485 (looking-at org-bracket-link-regexp)
8486 (<= (match-beginning 0) pos)
8487 (>= (match-end 0) pos)))
8488 ;; We do have a link at point, and we are going to edit it.
8489 (setq remove (list (match-beginning 0) (match-end 0)))
8490 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
8491 (setq link (read-string "Link: "
8492 (org-link-unescape
8493 (org-match-string-no-properties 1)))))
8494 (complete-file
8495 ;; Completing read for file names.
8496 (setq file (read-file-name "File: "))
8497 (let ((pwd (file-name-as-directory (expand-file-name ".")))
8498 (pwd1 (file-name-as-directory (abbreviate-file-name
8499 (expand-file-name ".")))))
8500 (cond
8501 ((equal complete-file '(16))
8502 (setq link (org-make-link
8503 "file:"
8504 (abbreviate-file-name (expand-file-name file)))))
8505 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
8506 (setq link (org-make-link "file:" (match-string 1 file))))
8507 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
8508 (expand-file-name file))
8509 (setq link (org-make-link
8510 "file:" (match-string 1 (expand-file-name file)))))
8511 (t (setq link (org-make-link "file:" file))))))
8513 ;; Read link, with completion for stored links.
8514 (setq link (org-completing-read
8515 "Link: " org-stored-links nil nil nil
8516 org-insert-link-history
8517 (or (car (car org-stored-links)))))
8518 (setq entry (assoc link org-stored-links))
8519 (if (not org-keep-stored-link-after-insertion)
8520 (setq org-stored-links (delq (assoc link org-stored-links)
8521 org-stored-links)))
8522 (setq link (if entry (nth 1 entry) link)
8523 desc (or desc (nth 2 entry)))))
8525 (if (string-match org-plain-link-re link)
8526 ;; URL-like link, normalize the use of angular brackets.
8527 (setq link (org-make-link (org-remove-angle-brackets link))))
8529 ;; Check if we are linking to the current file. If yes, simplify the link.
8530 (when (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link)
8531 (let* ((path (match-string 1 link))
8532 (case-fold-search nil)
8533 (search (match-string 2 link)))
8534 (when (save-match-data
8535 (equal (file-truename buffer-file-name)
8536 (file-truename path)))
8537 ;; We are linking to this same file, with a search option
8538 (setq link search))))
8539 (setq desc (read-string "Description: " desc))
8540 (unless (string-match "\\S-" desc) (setq desc nil))
8541 (if remove (apply 'delete-region remove))
8542 (insert (org-make-link-string link desc))))
8544 (defun org-completing-read (&rest args)
8545 (let ((minibuffer-local-completion-map
8546 (copy-keymap minibuffer-local-completion-map)))
8547 (define-key minibuffer-local-completion-map " " 'self-insert-command)
8548 (apply 'completing-read args)))
8550 ;;; Hooks for remember.el
8552 (defvar org-finish-function nil)
8554 ;;;###autoload
8555 (defun org-remember-annotation ()
8556 "Return a link to the current location as an annotation for remember.el.
8557 If you are using Org-mode files as target for data storage with
8558 remember.el, then the annotations should include a link compatible with the
8559 conventions in Org-mode. This function returns such a link."
8560 (org-store-link nil))
8562 (defconst org-remember-help
8563 "Select a destination location for the note.
8564 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
8565 RET at beg-of-buf -> Append to file as level 2 headline
8566 RET on headline -> Store as sublevel entry to current headline
8567 <left>/<right> -> before/after current headline, same headings level")
8569 ;;;###autoload
8570 (defun org-remember-apply-template ()
8571 "Initialize *remember* buffer with template, invode `org-mode'.
8572 This function should be placed into `remember-mode-hook' and in fact requires
8573 to be run from that hook to fucntion properly."
8574 (if org-remember-templates
8575 (let* ((entry (if (= (length org-remember-templates) 1)
8576 (cdar org-remember-templates)
8577 (message "Select template: %s"
8578 (mapconcat
8579 (lambda (x) (char-to-string (car x)))
8580 org-remember-templates " "))
8581 (cdr (assoc (read-char-exclusive) org-remember-templates))))
8582 (tpl (if (consp (cdr entry)) (cadr entry) (cdr entry)))
8583 (file (if (consp (cdr entry)) (nth 2 entry)))
8584 (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
8585 (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
8586 (v-u (concat "[" (substring v-t 1 -1) "]"))
8587 (v-U (concat "[" (substring v-T 1 -1) "]"))
8588 (v-a annotation) ; defined in `remember-mode'
8589 (v-i initial) ; defined in `remember-mode'
8590 (v-n user-full-name)
8592 (unless tpl (setq tpl "") (message "No template") (ding))
8593 (insert tpl) (goto-char (point-min))
8594 (while (re-search-forward "%\\([tTuTai]\\)" nil t)
8595 (when (and initial (equal (match-string 0) "%i"))
8596 (save-match-data
8597 (let* ((lead (buffer-substring
8598 (point-at-bol) (match-beginning 0))))
8599 (setq v-i (mapconcat 'identity
8600 (org-split-string initial "\n")
8601 (concat "\n" lead))))))
8602 (replace-match
8603 (or (eval (intern (concat "v-" (match-string 1)))) "")
8604 t t))
8605 (let ((org-startup-folded nil)
8606 (org-startup-with-deadline-check nil))
8607 (org-mode))
8608 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
8609 (set (make-local-variable 'org-default-notes-file) file))
8610 (goto-char (point-min))
8611 (if (re-search-forward "%\\?" nil t) (replace-match "")))
8612 (let ((org-startup-folded nil)
8613 (org-startup-with-deadline-check nil))
8614 (org-mode)))
8615 (set (make-local-variable 'org-finish-function) 'remember-buffer))
8617 ;;;###autoload
8618 (defun org-remember-handler ()
8619 "Store stuff from remember.el into an org file.
8620 First prompts for an org file. If the user just presses return, the value
8621 of `org-default-notes-file' is used.
8622 Then the command offers the headings tree of the selected file in order to
8623 file the text at a specific location.
8624 You can either immediately press RET to get the note appended to the
8625 file, or you can use vertical cursor motion and visibility cycling (TAB) to
8626 find a better place. Then press RET or <left> or <right> in insert the note.
8628 Key Cursor position Note gets inserted
8629 -----------------------------------------------------------------------------
8630 RET buffer-start as level 2 heading at end of file
8631 RET on headline as sublevel of the heading at cursor
8632 RET no heading at cursor position, level taken from context.
8633 Or use prefix arg to specify level manually.
8634 <left> on headline as same level, before current heading
8635 <right> on headline as same level, after current heading
8637 So the fastest way to store the note is to press RET RET to append it to
8638 the default file. This way your current train of thought is not
8639 interrupted, in accordance with the principles of remember.el. But with
8640 little extra effort, you can push it directly to the correct location.
8642 Before being stored away, the function ensures that the text has a
8643 headline, i.e. a first line that starts with a \"*\". If not, a headline
8644 is constructed from the current date and some additional data.
8646 If the variable `org-adapt-indentation' is non-nil, the entire text is
8647 also indented so that it starts in the same column as the headline
8648 \(i.e. after the stars).
8650 See also the variable `org-reverse-note-order'."
8651 (catch 'quit
8652 (let* ((txt (buffer-substring (point-min) (point-max)))
8653 (fastp current-prefix-arg)
8654 (file (if fastp org-default-notes-file (org-get-org-file)))
8655 (visiting (find-buffer-visiting file))
8656 (org-startup-with-deadline-check nil)
8657 (org-startup-folded nil)
8658 (org-startup-align-all-tables nil)
8659 spos level indent reversed)
8660 ;; Modify text so that it becomes a nice subtree which can be inserted
8661 ;; into an org tree.
8662 (let* ((lines (split-string txt "\n"))
8663 first)
8664 ;; remove empty lines at the beginning
8665 (while (and lines (string-match "^[ \t]*\n" (car lines)))
8666 (setq lines (cdr lines)))
8667 (setq first (car lines) lines (cdr lines))
8668 (if (string-match "^\\*+" first)
8669 ;; Is already a headline
8670 (setq indent nil)
8671 ;; We need to add a headline: Use time and first buffer line
8672 (setq lines (cons first lines)
8673 first (concat "* " (current-time-string)
8674 " (" (remember-buffer-desc) ")")
8675 indent " "))
8676 (if (and org-adapt-indentation indent)
8677 (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
8678 (setq txt (concat first "\n"
8679 (mapconcat 'identity lines "\n"))))
8680 ;; Find the file
8681 (if (not visiting)
8682 (find-file-noselect file))
8683 (with-current-buffer (get-file-buffer file)
8684 (save-excursion (and (goto-char (point-min))
8685 (not (re-search-forward "^\\* " nil t))
8686 (insert "\n* Notes\n")))
8687 (setq reversed (org-notes-order-reversed-p))
8688 (save-excursion
8689 (save-restriction
8690 (widen)
8691 ;; Ask the User for a location
8692 (setq spos (if fastp 1 (org-get-location
8693 (current-buffer)
8694 org-remember-help)))
8695 (if (not spos) (throw 'quit nil)) ; return nil to show we did
8696 ; not handle this note
8697 (goto-char spos)
8698 (cond ((bobp)
8699 ;; Put it at the start or end, as level 2
8700 (save-restriction
8701 (widen)
8702 (goto-char (if reversed (point-min) (point-max)))
8703 (if (not (bolp)) (newline))
8704 (org-paste-subtree 2 txt)))
8705 ((and (org-on-heading-p nil) (not current-prefix-arg))
8706 ;; Put it below this entry, at the beg/end of the subtree
8707 (org-back-to-heading)
8708 (setq level (funcall outline-level))
8709 (if reversed
8710 (outline-end-of-heading)
8711 (outline-end-of-subtree))
8712 (if (not (bolp)) (newline))
8713 (beginning-of-line 1)
8714 (org-paste-subtree (1+ level) txt))
8716 ;; Put it right there, with automatic level determined by
8717 ;; org-paste-subtree or from prefix arg
8718 (org-paste-subtree current-prefix-arg txt)))
8719 (when remember-save-after-remembering
8720 (save-buffer)
8721 (if (not visiting) (kill-buffer (current-buffer)))))))))
8722 t) ;; return t to indicate that we took care of this note.
8724 (defun org-get-org-file ()
8725 "Read a filename, with default directory `org-directory'."
8726 (let ((default (or org-default-notes-file remember-data-file)))
8727 (read-file-name (format "File name [%s]: " default)
8728 (file-name-as-directory org-directory)
8729 default)))
8731 (defun org-notes-order-reversed-p ()
8732 "Check if the current file should receive notes in reversed order."
8733 (cond
8734 ((not org-reverse-note-order) nil)
8735 ((eq t org-reverse-note-order) t)
8736 ((not (listp org-reverse-note-order)) nil)
8737 (t (catch 'exit
8738 (let ((all org-reverse-note-order)
8739 entry)
8740 (while (setq entry (pop all))
8741 (if (string-match (car entry) buffer-file-name)
8742 (throw 'exit (cdr entry))))
8743 nil)))))
8745 ;;; Tables
8747 ;; Watch out: Here we are talking about two different kind of tables.
8748 ;; Most of the code is for the tables created with the Org-mode table editor.
8749 ;; Sometimes, we talk about tables created and edited with the table.el
8750 ;; Emacs package. We call the former org-type tables, and the latter
8751 ;; table.el-type tables.
8754 (defun org-before-change-function (beg end)
8755 "Every change indicates that a table might need an update."
8756 (setq org-table-may-need-update t))
8758 (defconst org-table-line-regexp "^[ \t]*|"
8759 "Detects an org-type table line.")
8760 (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
8761 "Detects an org-type table line.")
8762 (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
8763 "Detects a table line marked for automatic recalculation.")
8764 (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
8765 "Detects a table line marked for automatic recalculation.")
8766 (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
8767 "Detects a table line marked for automatic recalculation.")
8768 (defconst org-table-hline-regexp "^[ \t]*|-"
8769 "Detects an org-type table hline.")
8770 (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
8771 "Detects a table-type table hline.")
8772 (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
8773 "Detects an org-type or table-type table.")
8774 (defconst org-table-border-regexp "^[ \t]*[^| \t]"
8775 "Searching from within a table (any type) this finds the first line
8776 outside the table.")
8777 (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
8778 "Searching from within a table (any type) this finds the first line
8779 outside the table.")
8781 (defun org-table-create-with-table.el ()
8782 "Use the table.el package to insert a new table.
8783 If there is already a table at point, convert between Org-mode tables
8784 and table.el tables."
8785 (interactive)
8786 (require 'table)
8787 (cond
8788 ((org-at-table.el-p)
8789 (if (y-or-n-p "Convert table to Org-mode table? ")
8790 (org-table-convert)))
8791 ((org-at-table-p)
8792 (if (y-or-n-p "Convert table to table.el table? ")
8793 (org-table-convert)))
8794 (t (call-interactively 'table-insert))))
8796 (defun org-table-create-or-convert-from-region (arg)
8797 "Convert region to table, or create an empty table.
8798 If there is an active region, convert it to a table. If there is no such
8799 region, create an empty table."
8800 (interactive "P")
8801 (if (org-region-active-p)
8802 (org-table-convert-region (region-beginning) (region-end) arg)
8803 (org-table-create arg)))
8805 (defun org-table-create (&optional size)
8806 "Query for a size and insert a table skeleton.
8807 SIZE is a string Columns x Rows like for example \"3x2\"."
8808 (interactive "P")
8809 (unless size
8810 (setq size (read-string
8811 (concat "Table size Columns x Rows [e.g. "
8812 org-table-default-size "]: ")
8813 "" nil org-table-default-size)))
8815 (let* ((pos (point))
8816 (indent (make-string (current-column) ?\ ))
8817 (split (org-split-string size " *x *"))
8818 (rows (string-to-number (nth 1 split)))
8819 (columns (string-to-number (car split)))
8820 (line (concat (apply 'concat indent "|" (make-list columns " |"))
8821 "\n")))
8822 (if (string-match "^[ \t]*$" (buffer-substring-no-properties
8823 (point-at-bol) (point)))
8824 (beginning-of-line 1)
8825 (newline))
8826 ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
8827 (dotimes (i rows) (insert line))
8828 (goto-char pos)
8829 (if (> rows 1)
8830 ;; Insert a hline after the first row.
8831 (progn
8832 (end-of-line 1)
8833 (insert "\n|-")
8834 (goto-char pos)))
8835 (org-table-align)))
8837 (defun org-table-convert-region (beg0 end0 &optional nspace)
8838 "Convert region to a table.
8839 The region goes from BEG0 to END0, but these borders will be moved
8840 slightly, to make sure a beginning of line in the first line is included.
8841 When NSPACE is non-nil, it indicates the minimum number of spaces that
8842 separate columns (default: just one space)."
8843 (interactive "rP")
8844 (let* ((beg (min beg0 end0))
8845 (end (max beg0 end0))
8846 (tabsep t)
8848 (goto-char beg)
8849 (beginning-of-line 1)
8850 (setq beg (move-marker (make-marker) (point)))
8851 (goto-char end)
8852 (if (bolp) (backward-char 1) (end-of-line 1))
8853 (setq end (move-marker (make-marker) (point)))
8854 ;; Lets see if this is tab-separated material. If every nonempty line
8855 ;; contains a tab, we will assume that it is tab-separated material
8856 (if nspace
8857 (setq tabsep nil)
8858 (goto-char beg)
8859 (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
8860 (if nspace (setq tabsep nil))
8861 (if tabsep
8862 (setq re "^\\|\t")
8863 (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
8864 (max 1 (prefix-numeric-value nspace)))))
8865 (goto-char beg)
8866 (while (re-search-forward re end t)
8867 (replace-match "|" t t))
8868 (goto-char beg)
8869 (insert " ")
8870 (org-table-align)))
8872 (defun org-table-import (file arg)
8873 "Import FILE as a table.
8874 The file is assumed to be tab-separated. Such files can be produced by most
8875 spreadsheet and database applications. If no tabs (at least one per line)
8876 are found, lines will be split on whitespace into fields."
8877 (interactive "f\nP")
8878 (or (bolp) (newline))
8879 (let ((beg (point))
8880 (pm (point-max)))
8881 (insert-file-contents file)
8882 (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
8884 (defun org-table-export ()
8885 "Export table as a tab-separated file.
8886 Such a file can be imported into a spreadsheet program like Excel."
8887 (interactive)
8888 (let* ((beg (org-table-begin))
8889 (end (org-table-end))
8890 (table (buffer-substring beg end))
8891 (file (read-file-name "Export table to: "))
8892 buf)
8893 (unless (or (not (file-exists-p file))
8894 (y-or-n-p (format "Overwrite file %s? " file)))
8895 (error "Abort"))
8896 (with-current-buffer (find-file-noselect file)
8897 (setq buf (current-buffer))
8898 (erase-buffer)
8899 (fundamental-mode)
8900 (insert table)
8901 (goto-char (point-min))
8902 (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
8903 (replace-match "" t t)
8904 (end-of-line 1))
8905 (goto-char (point-min))
8906 (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
8907 (replace-match "" t t)
8908 (goto-char (min (1+ (point)) (point-max))))
8909 (goto-char (point-min))
8910 (while (re-search-forward "^-[-+]*$" nil t)
8911 (replace-match "")
8912 (if (looking-at "\n")
8913 (delete-char 1)))
8914 (goto-char (point-min))
8915 (while (re-search-forward "[ \t]*|[ \t]*" nil t)
8916 (replace-match "\t" t t))
8917 (save-buffer))
8918 (kill-buffer buf)))
8920 (defvar org-table-aligned-begin-marker (make-marker)
8921 "Marker at the beginning of the table last aligned.
8922 Used to check if cursor still is in that table, to minimize realignment.")
8923 (defvar org-table-aligned-end-marker (make-marker)
8924 "Marker at the end of the table last aligned.
8925 Used to check if cursor still is in that table, to minimize realignment.")
8926 (defvar org-table-last-alignment nil
8927 "List of flags for flushright alignment, from the last re-alignment.
8928 This is being used to correctly align a single field after TAB or RET.")
8929 (defvar org-table-last-column-widths nil
8930 "List of max width of fields in each column.
8931 This is being used to correctly align a single field after TAB or RET.")
8933 (defvar org-last-recalc-line nil)
8934 (defconst org-narrow-column-arrow "=>"
8935 "Used as display property in narrowed table columns.")
8937 (defun org-table-align ()
8938 "Align the table at point by aligning all vertical bars."
8939 (interactive)
8940 (let* (
8941 ;; Limits of table
8942 (beg (org-table-begin))
8943 (end (org-table-end))
8944 ;; Current cursor position
8945 (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
8946 (colpos (org-table-current-column))
8947 (winstart (window-start))
8948 text lines (new "") lengths l typenums ty fields maxfields i
8949 column
8950 (indent "") cnt frac
8951 rfmt hfmt
8952 (spaces '(1 . 1))
8953 (sp1 (car spaces))
8954 (sp2 (cdr spaces))
8955 (rfmt1 (concat
8956 (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
8957 (hfmt1 (concat
8958 (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
8959 emptystrings xx links narrow fmax fmin f1 len c e)
8960 (untabify beg end)
8961 (remove-text-properties beg end '(org-cwidth t display t))
8962 ;; Check if we have links
8963 (goto-char beg)
8964 (setq links (re-search-forward org-bracket-link-regexp end t))
8965 ;; Make sure the link properties are right FIXME: Can this be optimized????
8966 (when links (goto-char beg) (while (org-activate-bracket-links end)))
8967 ;; Check if we are narrowing any columns
8968 (goto-char beg)
8969 (setq narrow (and org-format-transports-properties-p
8970 (re-search-forward "<[0-9]+>" end t)))
8971 ;; Get the rows
8972 (setq lines (org-split-string
8973 (buffer-substring beg end) "\n"))
8974 ;; Store the indentation of the first line
8975 (if (string-match "^ *" (car lines))
8976 (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
8977 ;; Mark the hlines by setting the corresponding element to nil
8978 ;; At the same time, we remove trailing space.
8979 (setq lines (mapcar (lambda (l)
8980 (if (string-match "^ *|-" l)
8982 (if (string-match "[ \t]+$" l)
8983 (substring l 0 (match-beginning 0))
8984 l)))
8985 lines))
8986 ;; Get the data fields by splitting the lines.
8987 (setq fields (mapcar
8988 (lambda (l)
8989 (org-split-string l " *| *"))
8990 (delq nil (copy-sequence lines))))
8991 ;; How many fields in the longest line?
8992 (condition-case nil
8993 (setq maxfields (apply 'max (mapcar 'length fields)))
8994 (error
8995 (kill-region beg end)
8996 (org-table-create org-table-default-size)
8997 (error "Empty table - created default table")))
8998 ;; A list of empty string to fill any short rows on output
8999 (setq emptystrings (make-list maxfields ""))
9000 ;; Check for special formatting.
9001 (setq i -1)
9002 (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
9003 (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
9004 ;; Check if there is an explicit width specified
9005 (when (and org-table-limit-column-width narrow)
9006 (setq c column fmax nil)
9007 (while c
9008 (setq e (pop c))
9009 (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
9010 (setq fmax (string-to-number (match-string 1 e)) c nil)))
9011 ;; Find fields that are wider than fmax, and shorten them
9012 (when fmax
9013 (loop for xx in column do
9014 (when (and (stringp xx)
9015 (> (org-string-width xx) fmax))
9016 (org-add-props xx nil
9017 'help-echo
9018 (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
9019 (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
9020 (unless (> f1 1)
9021 (error "Cannot narrow field starting with wide link \"%s\""
9022 (match-string 0 xx)))
9023 (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
9024 (add-text-properties (- f1 2) f1
9025 (list 'display org-narrow-column-arrow)
9026 xx)))))
9027 ;; Get the maximum width for each column
9028 (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
9029 ;; Get the fraction of numbers, to decide about alignment of the column
9030 (setq cnt 0 frac 0.0)
9031 (loop for x in column do
9032 (if (equal x "")
9034 (setq frac ( / (+ (* frac cnt)
9035 (if (string-match org-table-number-regexp x) 1 0))
9036 (setq cnt (1+ cnt))))))
9037 (push (>= frac org-table-number-fraction) typenums))
9038 (setq lengths (nreverse lengths) typenums (nreverse typenums))
9040 ;; Store the alignment of this table, for later editing of single fields
9041 (setq org-table-last-alignment typenums
9042 org-table-last-column-widths lengths)
9044 ;; With invisible characters, `format' does not get the field width right
9045 ;; So we need to make these fields wide by hand.
9046 (when links
9047 (loop for i from 0 upto (1- maxfields) do
9048 (setq len (nth i lengths))
9049 (loop for j from 0 upto (1- (length fields)) do
9050 (setq c (nthcdr i (car (nthcdr j fields))))
9051 (if (and (stringp (car c))
9052 (string-match org-bracket-link-regexp (car c))
9053 (< (org-string-width (car c)) len))
9054 (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
9056 ;; Compute the formats needed for output of the table
9057 (setq rfmt (concat indent "|") hfmt (concat indent "|"))
9058 (while (setq l (pop lengths))
9059 (setq ty (if (pop typenums) "" "-")) ; number types flushright
9060 (setq rfmt (concat rfmt (format rfmt1 ty l))
9061 hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
9062 (setq rfmt (concat rfmt "\n")
9063 hfmt (concat (substring hfmt 0 -1) "|\n"))
9065 (setq new (mapconcat
9066 (lambda (l)
9067 (if l (apply 'format rfmt
9068 (append (pop fields) emptystrings))
9069 hfmt))
9070 lines ""))
9071 ;; Replace the old one
9072 (delete-region beg end)
9073 (move-marker end nil)
9074 (move-marker org-table-aligned-begin-marker (point))
9075 (insert new)
9076 (move-marker org-table-aligned-end-marker (point))
9077 (when (and orgtbl-mode (not (eq major-mode 'org-mode)))
9078 (goto-char org-table-aligned-begin-marker)
9079 (while (org-hide-wide-columns org-table-aligned-end-marker)))
9080 ;; Try to move to the old location (approximately)
9081 (goto-line linepos)
9082 (set-window-start (selected-window) winstart 'noforce)
9083 (org-table-goto-column colpos)
9084 (setq org-table-may-need-update nil)
9087 (defun org-string-width (s)
9088 "Compute width of string, ignoring invisible characters.
9089 This ignores character with invisibility property `org-link', and also
9090 characters with property `org-cwidth', because these will become invisible
9091 upon the next fontification round."
9092 (let (b)
9093 (when (or (eq t buffer-invisibility-spec)
9094 (assq 'org-link buffer-invisibility-spec))
9095 (while (setq b (text-property-any 0 (length s)
9096 'invisible 'org-link s))
9097 (setq s (concat (substring s 0 b)
9098 (substring s (or (next-single-property-change
9099 b 'invisible s) (length s)))))))
9100 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
9101 (setq s (concat (substring s 0 b)
9102 (substring s (or (next-single-property-change
9103 b 'org-cwidth s) (length s))))))
9104 (string-width s)))
9106 (defun org-table-begin (&optional table-type)
9107 "Find the beginning of the table and return its position.
9108 With argument TABLE-TYPE, go to the beginning of a table.el-type table."
9109 (save-excursion
9110 (if (not (re-search-backward
9111 (if table-type org-table-any-border-regexp
9112 org-table-border-regexp)
9113 nil t))
9114 ; FIXME: OK to just use beginning-of-buffer?
9115 ; (error "Can't find beginning of table")
9116 (progn (goto-char (point-min)) (point))
9117 (goto-char (match-beginning 0))
9118 (beginning-of-line 2)
9119 (point))))
9121 (defun org-table-end (&optional table-type)
9122 "Find the end of the table and return its position.
9123 With argument TABLE-TYPE, go to the end of a table.el-type table."
9124 (save-excursion
9125 (if (not (re-search-forward
9126 (if table-type org-table-any-border-regexp
9127 org-table-border-regexp)
9128 nil t))
9129 (goto-char (point-max))
9130 (goto-char (match-beginning 0)))
9131 (point-marker)))
9133 (defun org-table-justify-field-maybe (&optional new)
9134 "Justify the current field, text to left, number to right.
9135 Optional argument NEW may specify text to replace the current field content."
9136 (cond
9137 ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
9138 ((org-at-table-hline-p))
9139 ((and (not new)
9140 (or (not (equal (marker-buffer org-table-aligned-begin-marker)
9141 (current-buffer)))
9142 (< (point) org-table-aligned-begin-marker)
9143 (>= (point) org-table-aligned-end-marker)))
9144 ;; This is not the same table, force a full re-align
9145 (setq org-table-may-need-update t))
9146 (t ;; realign the current field, based on previous full realign
9147 (let* ((pos (point)) s
9148 (col (org-table-current-column))
9149 (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
9150 l f n o e)
9151 (when (> col 0)
9152 (skip-chars-backward "^|\n")
9153 (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
9154 (progn
9155 (setq s (match-string 1)
9156 o (match-string 0)
9157 l (max 1 (- (match-end 0) (match-beginning 0) 3))
9158 e (not (= (match-beginning 2) (match-end 2))))
9159 (setq f (format (if num " %%%ds %s" " %%-%ds %s")
9160 l (if e "|" (setq org-table-may-need-update t) ""))
9161 n (format f s))
9162 (if new
9163 (if (<= (length new) l) ;; FIXME: length -> str-width?
9164 (setq n (format f new t t)) ;; FIXME: t t?
9165 (setq n (concat new "|") org-table-may-need-update t)))
9166 (or (equal n o)
9167 (let (org-table-may-need-update)
9168 (replace-match n))))
9169 (setq org-table-may-need-update t))
9170 (goto-char pos))))))
9172 (defun org-table-next-field ()
9173 "Go to the next field in the current table, creating new lines as needed.
9174 Before doing so, re-align the table if necessary."
9175 (interactive)
9176 (org-table-maybe-eval-formula)
9177 (org-table-maybe-recalculate-line)
9178 (if (and org-table-automatic-realign
9179 org-table-may-need-update)
9180 (org-table-align))
9181 (let ((end (org-table-end)))
9182 (if (org-at-table-hline-p)
9183 (end-of-line 1))
9184 (condition-case nil
9185 (progn
9186 (re-search-forward "|" end)
9187 (if (looking-at "[ \t]*$")
9188 (re-search-forward "|" end))
9189 (if (and (looking-at "-")
9190 org-table-tab-jumps-over-hlines
9191 (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
9192 (goto-char (match-beginning 1)))
9193 (if (looking-at "-")
9194 (progn
9195 (beginning-of-line 0)
9196 (org-table-insert-row 'below))
9197 (if (looking-at " ") (forward-char 1))))
9198 (error
9199 (org-table-insert-row 'below)))))
9201 (defun org-table-previous-field ()
9202 "Go to the previous field in the table.
9203 Before doing so, re-align the table if necessary."
9204 (interactive)
9205 (org-table-justify-field-maybe)
9206 (org-table-maybe-recalculate-line)
9207 (if (and org-table-automatic-realign
9208 org-table-may-need-update)
9209 (org-table-align))
9210 (if (org-at-table-hline-p)
9211 (end-of-line 1))
9212 (re-search-backward "|" (org-table-begin))
9213 (re-search-backward "|" (org-table-begin))
9214 (while (looking-at "|\\(-\\|[ \t]*$\\)")
9215 (re-search-backward "|" (org-table-begin)))
9216 (if (looking-at "| ?")
9217 (goto-char (match-end 0))))
9219 (defun org-table-next-row ()
9220 "Go to the next row (same column) in the current table.
9221 Before doing so, re-align the table if necessary."
9222 (interactive)
9223 (org-table-maybe-eval-formula)
9224 (org-table-maybe-recalculate-line)
9225 (if (or (looking-at "[ \t]*$")
9226 (save-excursion (skip-chars-backward " \t") (bolp)))
9227 (newline)
9228 (if (and org-table-automatic-realign
9229 org-table-may-need-update)
9230 (org-table-align))
9231 (let ((col (org-table-current-column)))
9232 (beginning-of-line 2)
9233 (if (or (not (org-at-table-p))
9234 (org-at-table-hline-p))
9235 (progn
9236 (beginning-of-line 0)
9237 (org-table-insert-row 'below)))
9238 (org-table-goto-column col)
9239 (skip-chars-backward "^|\n\r")
9240 (if (looking-at " ") (forward-char 1)))))
9242 (defun org-table-copy-down (n)
9243 "Copy a field down in the current column.
9244 If the field at the cursor is empty, copy into it the content of the nearest
9245 non-empty field above. With argument N, use the Nth non-empty field.
9246 If the current field is not empty, it is copied down to the next row, and
9247 the cursor is moved with it. Therefore, repeating this command causes the
9248 column to be filled row-by-row.
9249 If the variable `org-table-copy-increment' is non-nil and the field is an
9250 integer, it will be incremented while copying."
9251 (interactive "p")
9252 (let* ((colpos (org-table-current-column))
9253 (field (org-table-get-field))
9254 (non-empty (string-match "[^ \t]" field))
9255 (beg (org-table-begin))
9256 txt)
9257 (org-table-check-inside-data-field)
9258 (if non-empty
9259 (progn
9260 (setq txt (org-trim field))
9261 (org-table-next-row)
9262 (org-table-blank-field))
9263 (save-excursion
9264 (setq txt
9265 (catch 'exit
9266 (while (progn (beginning-of-line 1)
9267 (re-search-backward org-table-dataline-regexp
9268 beg t))
9269 (org-table-goto-column colpos t)
9270 (if (and (looking-at
9271 "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
9272 (= (setq n (1- n)) 0))
9273 (throw 'exit (match-string 1))))))))
9274 (if txt
9275 (progn
9276 (if (and org-table-copy-increment
9277 (string-match "^[0-9]+$" txt))
9278 (setq txt (format "%d" (+ (string-to-number txt) 1))))
9279 (insert txt)
9280 (org-table-maybe-recalculate-line)
9281 (org-table-align))
9282 (error "No non-empty field found"))))
9284 (defun org-table-check-inside-data-field ()
9285 "Is point inside a table data field?
9286 I.e. not on a hline or before the first or after the last column?
9287 This actually throws an error, so it aborts the current command."
9288 (if (or (not (org-at-table-p))
9289 (= (org-table-current-column) 0)
9290 (org-at-table-hline-p)
9291 (looking-at "[ \t]*$"))
9292 (error "Not in table data field")))
9294 (defvar org-table-clip nil
9295 "Clipboard for table regions.")
9297 (defun org-table-blank-field ()
9298 "Blank the current table field or active region."
9299 (interactive)
9300 (org-table-check-inside-data-field)
9301 (if (and (interactive-p) (org-region-active-p))
9302 (let (org-table-clip)
9303 (org-table-cut-region (region-beginning) (region-end)))
9304 (skip-chars-backward "^|")
9305 (backward-char 1)
9306 (if (looking-at "|[^|\n]+")
9307 (let* ((pos (match-beginning 0))
9308 (match (match-string 0))
9309 (len (org-string-width match)))
9310 (replace-match (concat "|" (make-string (1- len) ?\ )))
9311 (goto-char (+ 2 pos))
9312 (substring match 1)))))
9314 (defun org-table-get-field (&optional n replace)
9315 "Return the value of the field in column N of current row.
9316 N defaults to current field.
9317 If REPLACE is a string, replace field with this value. The return value
9318 is always the old value."
9319 (and n (org-table-goto-column n))
9320 (skip-chars-backward "^|\n")
9321 (backward-char 1)
9322 (if (looking-at "|[^|\r\n]*")
9323 (let* ((pos (match-beginning 0))
9324 (val (buffer-substring (1+ pos) (match-end 0))))
9325 (if replace
9326 (replace-match (concat "|" replace)))
9327 (goto-char (min (point-at-eol) (+ 2 pos)))
9328 val)
9329 (forward-char 1) ""))
9331 (defun org-table-current-column ()
9332 "Find out which column we are in.
9333 When called interactively, column is also displayed in echo area."
9334 (interactive)
9335 (if (interactive-p) (org-table-check-inside-data-field))
9336 (save-excursion
9337 (let ((cnt 0) (pos (point)))
9338 (beginning-of-line 1)
9339 (while (search-forward "|" pos t)
9340 (setq cnt (1+ cnt)))
9341 (if (interactive-p) (message "This is table column %d" cnt))
9342 cnt)))
9344 (defun org-table-goto-column (n &optional on-delim force)
9345 "Move the cursor to the Nth column in the current table line.
9346 With optional argument ON-DELIM, stop with point before the left delimiter
9347 of the field.
9348 If there are less than N fields, just go to after the last delimiter.
9349 However, when FORCE is non-nil, create new columns if necessary."
9350 (interactive "p")
9351 (let ((pos (point-at-eol)))
9352 (beginning-of-line 1)
9353 (when (> n 0)
9354 (while (and (> (setq n (1- n)) -1)
9355 (or (search-forward "|" pos t)
9356 (and force
9357 (progn (end-of-line 1)
9358 (skip-chars-backward "^|")
9359 (insert " | "))))))
9360 ; (backward-char 2) t)))))
9361 (when (and force (not (looking-at ".*|")))
9362 (save-excursion (end-of-line 1) (insert " | ")))
9363 (if on-delim
9364 (backward-char 1)
9365 (if (looking-at " ") (forward-char 1))))))
9367 (defun org-at-table-p (&optional table-type)
9368 "Return t if the cursor is inside an org-type table.
9369 If TABLE-TYPE is non-nil, also check for table.el-type tables."
9370 (if org-enable-table-editor
9371 (save-excursion
9372 (beginning-of-line 1)
9373 (looking-at (if table-type org-table-any-line-regexp
9374 org-table-line-regexp)))
9375 nil))
9377 (defun org-at-table.el-p ()
9378 "Return t if and only if we are at a table.el table."
9379 (and (org-at-table-p 'any)
9380 (save-excursion
9381 (goto-char (org-table-begin 'any))
9382 (looking-at org-table1-hline-regexp))))
9384 (defun org-table-recognize-table.el ()
9385 "If there is a table.el table nearby, recognize it and move into it."
9386 (if org-table-tab-recognizes-table.el
9387 (if (org-at-table.el-p)
9388 (progn
9389 (beginning-of-line 1)
9390 (if (looking-at org-table-dataline-regexp)
9392 (if (looking-at org-table1-hline-regexp)
9393 (progn
9394 (beginning-of-line 2)
9395 (if (looking-at org-table-any-border-regexp)
9396 (beginning-of-line -1)))))
9397 (if (re-search-forward "|" (org-table-end t) t)
9398 (progn
9399 (require 'table)
9400 (if (table--at-cell-p (point))
9402 (message "recognizing table.el table...")
9403 (table-recognize-table)
9404 (message "recognizing table.el table...done")))
9405 (error "This should not happen..."))
9407 nil)
9408 nil))
9410 (defun org-at-table-hline-p ()
9411 "Return t if the cursor is inside a hline in a table."
9412 (if org-enable-table-editor
9413 (save-excursion
9414 (beginning-of-line 1)
9415 (looking-at org-table-hline-regexp))
9416 nil))
9418 (defun org-table-insert-column ()
9419 "Insert a new column into the table."
9420 (interactive)
9421 (if (not (org-at-table-p))
9422 (error "Not at a table"))
9423 (org-table-find-dataline)
9424 (let* ((col (max 1 (org-table-current-column)))
9425 (beg (org-table-begin))
9426 (end (org-table-end))
9427 ;; Current cursor position
9428 (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
9429 (colpos col))
9430 (goto-char beg)
9431 (while (< (point) end)
9432 (if (org-at-table-hline-p)
9434 (org-table-goto-column col t)
9435 (insert "| "))
9436 (beginning-of-line 2))
9437 (move-marker end nil)
9438 (goto-line linepos)
9439 (org-table-goto-column colpos)
9440 (org-table-align)
9441 (org-table-modify-formulas 'insert col)))
9443 (defun org-table-find-dataline ()
9444 "Find a dataline in the current table, which is needed for column commands."
9445 (if (and (org-at-table-p)
9446 (not (org-at-table-hline-p)))
9448 (let ((col (current-column))
9449 (end (org-table-end)))
9450 (move-to-column col)
9451 (while (and (< (point) end)
9452 (or (not (= (current-column) col))
9453 (org-at-table-hline-p)))
9454 (beginning-of-line 2)
9455 (move-to-column col))
9456 (if (and (org-at-table-p)
9457 (not (org-at-table-hline-p)))
9459 (error
9460 "Please position cursor in a data line for column operations")))))
9462 (defun org-table-delete-column ()
9463 "Delete a column into the table."
9464 (interactive)
9465 (if (not (org-at-table-p))
9466 (error "Not at a table"))
9467 (org-table-find-dataline)
9468 (org-table-check-inside-data-field)
9469 (let* ((col (org-table-current-column))
9470 (beg (org-table-begin))
9471 (end (org-table-end))
9472 ;; Current cursor position
9473 (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
9474 (colpos col))
9475 (goto-char beg)
9476 (while (< (point) end)
9477 (if (org-at-table-hline-p)
9479 (org-table-goto-column col t)
9480 (and (looking-at "|[^|\n]+|")
9481 (replace-match "|")))
9482 (beginning-of-line 2))
9483 (move-marker end nil)
9484 (goto-line linepos)
9485 (org-table-goto-column colpos)
9486 (org-table-align)
9487 (org-table-modify-formulas 'remove col)))
9489 (defun org-table-move-column-right ()
9490 "Move column to the right."
9491 (interactive)
9492 (org-table-move-column nil))
9493 (defun org-table-move-column-left ()
9494 "Move column to the left."
9495 (interactive)
9496 (org-table-move-column 'left))
9498 (defun org-table-move-column (&optional left)
9499 "Move the current column to the right. With arg LEFT, move to the left."
9500 (interactive "P")
9501 (if (not (org-at-table-p))
9502 (error "Not at a table"))
9503 (org-table-find-dataline)
9504 (org-table-check-inside-data-field)
9505 (let* ((col (org-table-current-column))
9506 (col1 (if left (1- col) col))
9507 (beg (org-table-begin))
9508 (end (org-table-end))
9509 ;; Current cursor position
9510 (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
9511 (colpos (if left (1- col) (1+ col))))
9512 (if (and left (= col 1))
9513 (error "Cannot move column further left"))
9514 (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
9515 (error "Cannot move column further right"))
9516 (goto-char beg)
9517 (while (< (point) end)
9518 (if (org-at-table-hline-p)
9520 (org-table-goto-column col1 t)
9521 (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
9522 (replace-match "|\\2|\\1|")))
9523 (beginning-of-line 2))
9524 (move-marker end nil)
9525 (goto-line linepos)
9526 (org-table-goto-column colpos)
9527 (org-table-align)
9528 (org-table-modify-formulas 'swap col (if left (1- col) (1+ col)))))
9530 (defun org-table-move-row-down ()
9531 "Move table row down."
9532 (interactive)
9533 (org-table-move-row nil))
9534 (defun org-table-move-row-up ()
9535 "Move table row up."
9536 (interactive)
9537 (org-table-move-row 'up))
9539 (defun org-table-move-row (&optional up)
9540 "Move the current table line down. With arg UP, move it up."
9541 (interactive "P")
9542 (let ((col (current-column))
9543 (pos (point))
9544 (tonew (if up 0 2))
9545 txt)
9546 (beginning-of-line tonew)
9547 (if (not (org-at-table-p))
9548 (progn
9549 (goto-char pos)
9550 (error "Cannot move row further")))
9551 (goto-char pos)
9552 (beginning-of-line 1)
9553 (setq pos (point))
9554 (setq txt (buffer-substring (point) (1+ (point-at-eol))))
9555 (delete-region (point) (1+ (point-at-eol)))
9556 (beginning-of-line tonew)
9557 (insert txt)
9558 (beginning-of-line 0)
9559 (move-to-column col)))
9561 (defun org-table-insert-row (&optional arg)
9562 "Insert a new row above the current line into the table.
9563 With prefix ARG, insert below the current line."
9564 (interactive "P")
9565 (if (not (org-at-table-p))
9566 (error "Not at a table"))
9567 (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
9568 (new (org-table-clean-line line)))
9569 ;; Fix the first field if necessary
9570 (if (string-match "^[ \t]*| *[#$] *|" line)
9571 (setq new (replace-match (match-string 0 line) t t new)))
9572 (beginning-of-line (if arg 2 1))
9573 (let (org-table-may-need-update) (insert-before-markers new "\n"))
9574 (beginning-of-line 0)
9575 (re-search-forward "| ?" (point-at-eol) t)
9576 (and org-table-may-need-update (org-table-align))))
9578 (defun org-table-insert-hline (&optional arg)
9579 "Insert a horizontal-line below the current line into the table.
9580 With prefix ARG, insert above the current line."
9581 (interactive "P")
9582 (if (not (org-at-table-p))
9583 (error "Not at a table"))
9584 (let ((line (org-table-clean-line
9585 (buffer-substring (point-at-bol) (point-at-eol))))
9586 (col (current-column)))
9587 (while (string-match "|\\( +\\)|" line)
9588 (setq line (replace-match
9589 (concat "+" (make-string (- (match-end 1) (match-beginning 1))
9590 ?-) "|") t t line)))
9591 (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
9592 (beginning-of-line (if arg 1 2))
9593 (insert line "\n")
9594 (beginning-of-line (if arg 1 -1))
9595 (move-to-column col)))
9597 (defun org-table-clean-line (s)
9598 "Convert a table line S into a string with only \"|\" and space.
9599 In particular, this does handle wide and invisible characters."
9600 (if (string-match "^[ \t]*|-" s)
9601 ;; It's a hline, just map the characters
9602 (setq s (mapcar (lambda (x) (if (member x '(?| ?+)) ?| ?\ )) s))
9603 (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
9604 (setq s (replace-match
9605 (concat "|" (make-string (org-string-width (match-string 1 s))
9606 ?\ ) "|")
9607 t t s)))
9610 (defun org-table-kill-row ()
9611 "Delete the current row or horizontal line from the table."
9612 (interactive)
9613 (if (not (org-at-table-p))
9614 (error "Not at a table"))
9615 (let ((col (current-column)))
9616 (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
9617 (if (not (org-at-table-p)) (beginning-of-line 0))
9618 (move-to-column col)))
9620 (defun org-table-sort-lines (beg end numericp)
9621 "Sort table lines in region.
9622 Point and mark define the first and last line to include. Both point and
9623 mark should be in the column that is used for sorting. For example, to
9624 sort according to column 3, put the mark in the first line to sort, in
9625 table column 3. Put point into the last line to be included in the sorting,
9626 also in table column 3. The command will prompt for the sorting method
9627 \(n for numerical, a for alphanumeric)."
9628 (interactive "r\nsSorting method: [n]=numeric [a]=alpha: ")
9629 (setq numericp (string-match "[nN]" numericp))
9630 (org-table-align) ;; Just to be safe
9631 (let* (bcol ecol cmp column lns)
9632 (goto-char beg)
9633 (org-table-check-inside-data-field)
9634 (setq column (org-table-current-column)
9635 beg (move-marker (make-marker) (point-at-bol)))
9636 (goto-char end)
9637 (org-table-check-inside-data-field)
9638 (setq end (move-marker (make-marker) (1+ (point-at-eol))))
9639 (untabify beg end)
9640 (goto-char beg)
9641 (org-table-goto-column column)
9642 (skip-chars-backward "^|")
9643 (setq bcol (current-column))
9644 (org-table-goto-column (1+ column))
9645 (skip-chars-backward "^|")
9646 (setq ecol (1- (current-column)))
9647 (setq cmp (if numericp
9648 (lambda (a b) (< (car a) (car b)))
9649 (lambda (a b) (string< (car a) (car b)))))
9650 (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
9651 (org-split-string (buffer-substring beg end) "\n")))
9652 (if numericp
9653 (setq lns (mapcar (lambda(x)
9654 (cons (string-to-number (car x)) (cdr x)))
9655 lns)))
9656 (delete-region beg end)
9657 (move-marker beg nil)
9658 (move-marker end nil)
9659 (insert (mapconcat 'cdr (setq lns (sort lns cmp)) "\n") "\n")
9660 (message "%d lines sorted %s based on column %d"
9661 (length lns)
9662 (if numericp "numerically" "alphabetically") column)))
9664 (defun org-table-cut-region (beg end)
9665 "Copy region in table to the clipboard and blank all relevant fields."
9666 (interactive "r")
9667 (org-table-copy-region beg end 'cut))
9669 (defun org-table-copy-region (beg end &optional cut)
9670 "Copy rectangular region in table to clipboard.
9671 A special clipboard is used which can only be accessed
9672 with `org-table-paste-rectangle'."
9673 (interactive "rP")
9674 (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
9675 region cols
9676 (rpl (if cut " " nil)))
9677 (goto-char beg)
9678 (org-table-check-inside-data-field)
9679 (setq l01 (count-lines (point-min) (point))
9680 c01 (org-table-current-column))
9681 (goto-char end)
9682 (org-table-check-inside-data-field)
9683 (setq l02 (count-lines (point-min) (point))
9684 c02 (org-table-current-column))
9685 (setq l1 (min l01 l02) l2 (max l01 l02)
9686 c1 (min c01 c02) c2 (max c01 c02))
9687 (catch 'exit
9688 (while t
9689 (catch 'nextline
9690 (if (> l1 l2) (throw 'exit t))
9691 (goto-line l1)
9692 (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
9693 (setq cols nil ic1 c1 ic2 c2)
9694 (while (< ic1 (1+ ic2))
9695 (push (org-table-get-field ic1 rpl) cols)
9696 (setq ic1 (1+ ic1)))
9697 (push (nreverse cols) region)
9698 (setq l1 (1+ l1)))))
9699 (setq org-table-clip (nreverse region))
9700 (if cut (org-table-align))
9701 org-table-clip))
9703 (defun org-table-paste-rectangle ()
9704 "Paste a rectangular region into a table.
9705 The upper right corner ends up in the current field. All involved fields
9706 will be overwritten. If the rectangle does not fit into the present table,
9707 the table is enlarged as needed. The process ignores horizontal separator
9708 lines."
9709 (interactive)
9710 (unless (and org-table-clip (listp org-table-clip))
9711 (error "First cut/copy a region to paste!"))
9712 (org-table-check-inside-data-field)
9713 (let* ((clip org-table-clip)
9714 (line (count-lines (point-min) (point)))
9715 (col (org-table-current-column))
9716 (org-enable-table-editor t)
9717 (org-table-automatic-realign nil)
9718 c cols field)
9719 (while (setq cols (pop clip))
9720 (while (org-at-table-hline-p) (beginning-of-line 2))
9721 (if (not (org-at-table-p))
9722 (progn (end-of-line 0) (org-table-next-field)))
9723 (setq c col)
9724 (while (setq field (pop cols))
9725 (org-table-goto-column c nil 'force)
9726 (org-table-get-field nil field)
9727 (setq c (1+ c)))
9728 (beginning-of-line 2))
9729 (goto-line line)
9730 (org-table-goto-column col)
9731 (org-table-align)))
9733 (defun org-table-convert ()
9734 "Convert from `org-mode' table to table.el and back.
9735 Obviously, this only works within limits. When an Org-mode table is
9736 converted to table.el, all horizontal separator lines get lost, because
9737 table.el uses these as cell boundaries and has no notion of horizontal lines.
9738 A table.el table can be converted to an Org-mode table only if it does not
9739 do row or column spanning. Multiline cells will become multiple cells.
9740 Beware, Org-mode does not test if the table can be successfully converted - it
9741 blindly applies a recipe that works for simple tables."
9742 (interactive)
9743 (require 'table)
9744 (if (org-at-table.el-p)
9745 ;; convert to Org-mode table
9746 (let ((beg (move-marker (make-marker) (org-table-begin t)))
9747 (end (move-marker (make-marker) (org-table-end t))))
9748 (table-unrecognize-region beg end)
9749 (goto-char beg)
9750 (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
9751 (replace-match ""))
9752 (goto-char beg))
9753 (if (org-at-table-p)
9754 ;; convert to table.el table
9755 (let ((beg (move-marker (make-marker) (org-table-begin)))
9756 (end (move-marker (make-marker) (org-table-end))))
9757 ;; first, get rid of all horizontal lines
9758 (goto-char beg)
9759 (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
9760 (replace-match ""))
9761 ;; insert a hline before first
9762 (goto-char beg)
9763 (org-table-insert-hline 'above)
9764 (beginning-of-line -1)
9765 ;; insert a hline after each line
9766 (while (progn (beginning-of-line 3) (< (point) end))
9767 (org-table-insert-hline))
9768 (goto-char beg)
9769 (setq end (move-marker end (org-table-end)))
9770 ;; replace "+" at beginning and ending of hlines
9771 (while (re-search-forward "^\\([ \t]*\\)|-" end t)
9772 (replace-match "\\1+-"))
9773 (goto-char beg)
9774 (while (re-search-forward "-|[ \t]*$" end t)
9775 (replace-match "-+"))
9776 (goto-char beg)))))
9778 (defun org-table-wrap-region (arg)
9779 "Wrap several fields in a column like a paragraph.
9780 This is useful if you'd like to spread the contents of a field over several
9781 lines, in order to keep the table compact.
9783 If there is an active region, and both point and mark are in the same column,
9784 the text in the column is wrapped to minimum width for the given number of
9785 lines. Generally, this makes the table more compact. A prefix ARG may be
9786 used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
9787 formats the selected text to two lines. If the region was longer than two
9788 lines, the remaining lines remain empty. A negative prefix argument reduces
9789 the current number of lines by that amount. The wrapped text is pasted back
9790 into the table. If you formatted it to more lines than it was before, fields
9791 further down in the table get overwritten - so you might need to make space in
9792 the table first.
9794 If there is no region, the current field is split at the cursor position and
9795 the text fragment to the right of the cursor is prepended to the field one
9796 line down.
9798 If there is no region, but you specify a prefix ARG, the current field gets
9799 blank, and the content is appended to the field above."
9800 (interactive "P")
9801 (org-table-check-inside-data-field)
9802 (if (org-region-active-p)
9803 ;; There is a region: fill as a paragraph
9804 (let ((beg (region-beginning))
9805 nlines)
9806 (org-table-cut-region (region-beginning) (region-end))
9807 (if (> (length (car org-table-clip)) 1)
9808 (error "Region must be limited to single column"))
9809 (setq nlines (if arg
9810 (if (< arg 1)
9811 (+ (length org-table-clip) arg)
9812 arg)
9813 (length org-table-clip)))
9814 (setq org-table-clip
9815 (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
9816 nil nlines)))
9817 (goto-char beg)
9818 (org-table-paste-rectangle))
9819 ;; No region, split the current field at point
9820 (if arg
9821 ;; combine with field above
9822 (let ((s (org-table-blank-field))
9823 (col (org-table-current-column)))
9824 (beginning-of-line 0)
9825 (while (org-at-table-hline-p) (beginning-of-line 0))
9826 (org-table-goto-column col)
9827 (skip-chars-forward "^|")
9828 (skip-chars-backward " ")
9829 (insert " " (org-trim s))
9830 (org-table-align))
9831 ;; split field
9832 (when (looking-at "\\([^|]+\\)+|")
9833 (let ((s (match-string 1)))
9834 (replace-match " |")
9835 (goto-char (match-beginning 0))
9836 (org-table-next-row)
9837 (insert (org-trim s) " ")
9838 (org-table-align))))))
9840 (defvar org-field-marker nil)
9842 (defun org-table-edit-field (arg)
9843 "Edit table field in a different window.
9844 This is mainly useful for fields that contain hidden parts.
9845 When called with a \\[universal-argument] prefix, just make the full field visible so that
9846 it can be edited in place."
9847 (interactive "P")
9848 (if arg
9849 (let ((b (save-excursion (skip-chars-backward "^|") (point)))
9850 (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
9851 (remove-text-properties b e '(org-cwidth t invisible t
9852 display t intangible t))
9853 (if (and (boundp 'font-lock-mode) font-lock-mode)
9854 (font-lock-fontify-block)))
9855 (let ((pos (move-marker (make-marker) (point)))
9856 (field (org-table-get-field))
9857 (cw (current-window-configuration))
9859 (switch-to-buffer-other-window "*Org tmp*")
9860 (erase-buffer)
9861 (insert "#\n# Edit field and finish with C-c C-c\n#\n")
9862 (org-mode)
9863 (goto-char (setq p (point-max)))
9864 (insert (org-trim field))
9865 (remove-text-properties p (point-max)
9866 '(invisible t org-cwidth t display t
9867 intangible t))
9868 (goto-char p)
9869 (set (make-local-variable 'org-finish-function)
9870 'org-table-finish-edit-field)
9871 (set (make-local-variable 'org-window-configuration) cw)
9872 (set (make-local-variable 'org-field-marker) pos)
9873 (message "Edit and finish with C-c C-c"))))
9875 (defun org-table-finish-edit-field ()
9876 "Finish editing a table data field.
9877 Remove all newline characters, insert the result into the table, realign
9878 the table and kill the editing buffer."
9879 (let ((pos org-field-marker)
9880 (cw org-window-configuration)
9881 (cb (current-buffer))
9882 text)
9883 (goto-char (point-min))
9884 (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
9885 (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
9886 (replace-match " "))
9887 (setq text (org-trim (buffer-string)))
9888 (set-window-configuration cw)
9889 (kill-buffer cb)
9890 (select-window (get-buffer-window (marker-buffer pos)))
9891 (goto-char pos)
9892 (move-marker pos nil)
9893 (org-table-check-inside-data-field)
9894 (org-table-get-field nil text)
9895 (org-table-align)
9896 (message "New field value inserted")))
9898 (defun org-trim (s)
9899 "Remove whitespace at beginning and end of string."
9900 (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
9901 (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
9904 (defun org-wrap (string &optional width lines)
9905 "Wrap string to either a number of lines, or a width in characters.
9906 If WIDTH is non-nil, the string is wrapped to that width, however many lines
9907 that costs. If there is a word longer than WIDTH, the text is actually
9908 wrapped to the length of that word.
9909 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
9910 many lines, whatever width that takes.
9911 The return value is a list of lines, without newlines at the end."
9912 (let* ((words (org-split-string string "[ \t\n]+"))
9913 (maxword (apply 'max (mapcar 'org-string-width words)))
9914 w ll)
9915 (cond (width
9916 (org-do-wrap words (max maxword width)))
9917 (lines
9918 (setq w maxword)
9919 (setq ll (org-do-wrap words maxword))
9920 (if (<= (length ll) lines)
9922 (setq ll words)
9923 (while (> (length ll) lines)
9924 (setq w (1+ w))
9925 (setq ll (org-do-wrap words w)))
9926 ll))
9927 (t (error "Cannot wrap this")))))
9930 (defun org-do-wrap (words width)
9931 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
9932 (let (lines line)
9933 (while words
9934 (setq line (pop words))
9935 (while (and words (< (+ (length line) (length (car words))) width))
9936 (setq line (concat line " " (pop words))))
9937 (setq lines (push line lines)))
9938 (nreverse lines)))
9940 (defun org-split-string (string &optional separators)
9941 "Splits STRING into substrings at SEPARATORS.
9942 No empty strings are returned if there are matches at the beginning
9943 and end of string."
9944 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
9945 (start 0)
9946 notfirst
9947 (list nil))
9948 (while (and (string-match rexp string
9949 (if (and notfirst
9950 (= start (match-beginning 0))
9951 (< start (length string)))
9952 (1+ start) start))
9953 (< (match-beginning 0) (length string)))
9954 (setq notfirst t)
9955 (or (eq (match-beginning 0) 0)
9956 (and (eq (match-beginning 0) (match-end 0))
9957 (eq (match-beginning 0) start))
9958 (setq list
9959 (cons (substring string start (match-beginning 0))
9960 list)))
9961 (setq start (match-end 0)))
9962 (or (eq start (length string))
9963 (setq list
9964 (cons (substring string start)
9965 list)))
9966 (nreverse list)))
9968 (defun org-table-map-tables (function)
9969 "Apply FUNCTION to the start of all tables in the buffer."
9970 (save-excursion
9971 (save-restriction
9972 (widen)
9973 (goto-char (point-min))
9974 (while (re-search-forward org-table-any-line-regexp nil t)
9975 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
9976 (beginning-of-line 1)
9977 (if (looking-at org-table-line-regexp)
9978 (save-excursion (funcall function)))
9979 (re-search-forward org-table-any-border-regexp nil 1))))
9980 (message "Mapping tables: done"))
9982 (defun org-table-sum (&optional beg end nlast)
9983 "Sum numbers in region of current table column.
9984 The result will be displayed in the echo area, and will be available
9985 as kill to be inserted with \\[yank].
9987 If there is an active region, it is interpreted as a rectangle and all
9988 numbers in that rectangle will be summed. If there is no active
9989 region and point is located in a table column, sum all numbers in that
9990 column.
9992 If at least one number looks like a time HH:MM or HH:MM:SS, all other
9993 numbers are assumed to be times as well (in decimal hours) and the
9994 numbers are added as such.
9996 If NLAST is a number, only the NLAST fields will actually be summed."
9997 (interactive)
9998 (save-excursion
9999 (let (col (timecnt 0) diff h m s org-table-clip)
10000 (cond
10001 ((and beg end)) ; beg and end given explicitly
10002 ((org-region-active-p)
10003 (setq beg (region-beginning) end (region-end)))
10005 (setq col (org-table-current-column))
10006 (goto-char (org-table-begin))
10007 (unless (re-search-forward "^[ \t]*|[^-]" nil t)
10008 (error "No table data"))
10009 (org-table-goto-column col)
10010 ;not needed? (skip-chars-backward "^|")
10011 (setq beg (point))
10012 (goto-char (org-table-end))
10013 (unless (re-search-backward "^[ \t]*|[^-]" nil t)
10014 (error "No table data"))
10015 (org-table-goto-column col)
10016 ;not needed? (skip-chars-forward "^|")
10017 (setq end (point))))
10018 (let* ((items (apply 'append (org-table-copy-region beg end)))
10019 (items1 (cond ((not nlast) items)
10020 ((>= nlast (length items)) items)
10021 (t (setq items (reverse items))
10022 (setcdr (nthcdr (1- nlast) items) nil)
10023 (nreverse items))))
10024 (numbers (delq nil (mapcar 'org-table-get-number-for-summing
10025 items1)))
10026 (res (apply '+ numbers))
10027 (sres (if (= timecnt 0)
10028 (format "%g" res)
10029 (setq diff (* 3600 res)
10030 h (floor (/ diff 3600)) diff (mod diff 3600)
10031 m (floor (/ diff 60)) diff (mod diff 60)
10032 s diff)
10033 (format "%d:%02d:%02d" h m s))))
10034 (kill-new sres)
10035 (if (interactive-p)
10036 (message "%s"
10037 (substitute-command-keys
10038 (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
10039 (length numbers) sres))))
10040 sres))))
10042 (defun org-table-get-number-for-summing (s)
10043 (let (n)
10044 (if (string-match "^ *|? *" s)
10045 (setq s (replace-match "" nil nil s)))
10046 (if (string-match " *|? *$" s)
10047 (setq s (replace-match "" nil nil s)))
10048 (setq n (string-to-number s))
10049 (cond
10050 ((and (string-match "0" s)
10051 (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
10052 ((string-match "\\`[ \t]+\\'" s) nil)
10053 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
10054 (let ((h (string-to-number (or (match-string 1 s) "0")))
10055 (m (string-to-number (or (match-string 2 s) "0")))
10056 (s (string-to-number (or (match-string 4 s) "0"))))
10057 (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
10058 (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
10059 ((equal n 0) nil)
10060 (t n))))
10062 (defun org-table-get-vertical-vector (desc &optional tbeg col)
10063 "Get a calc vector from a column, accorting to descriptor DESC.
10064 Optional arguments TBEG and COL can give the beginning of the table and
10065 the current column, to avoid unnecessary parsing."
10066 (save-excursion
10067 (or tbeg (setq tbeg (org-table-begin)))
10068 (or col (setq col (org-table-current-column)))
10069 (let (beg end nn n n1 n2 l (thisline (org-current-line)) hline-list)
10070 (cond
10071 ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc)
10072 (setq n1 (- (match-end 1) (match-beginning 1)))
10073 (if (match-beginning 3)
10074 (setq n2 (- (match-end 2) (match-beginning 3))))
10075 (setq n (if n2 (max n1 n2) n1))
10076 (setq n1 (if n2 (min n1 n2)))
10077 (setq nn n)
10078 (while (and (> nn 0)
10079 (re-search-backward org-table-hline-regexp tbeg t))
10080 (push (org-current-line) hline-list)
10081 (setq nn (1- nn)))
10082 (setq hline-list (nreverse hline-list))
10083 (goto-line (nth (1- n) hline-list))
10084 (when (re-search-forward org-table-dataline-regexp)
10085 (org-table-goto-column col)
10086 (setq beg (point)))
10087 (goto-line (if n1 (nth (1- n1) hline-list) thisline))
10088 (when (re-search-backward org-table-dataline-regexp)
10089 (org-table-goto-column col)
10090 (setq end (point)))
10091 (setq l (apply 'append (org-table-copy-region beg end)))
10092 (concat "[" (mapconcat (lambda (x) (setq x (org-trim x))
10093 (if (equal x "") "0" x))
10094 l ",") "]"))
10095 ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc)
10096 (setq n1 (string-to-number (match-string 1 desc))
10097 n2 (string-to-number (match-string 2 desc)))
10098 (beginning-of-line 1)
10099 (save-excursion
10100 (when (re-search-backward org-table-dataline-regexp tbeg t n1)
10101 (org-table-goto-column col)
10102 (setq beg (point))))
10103 (when (re-search-backward org-table-dataline-regexp tbeg t n2)
10104 (org-table-goto-column col)
10105 (setq end (point)))
10106 (setq l (apply 'append (org-table-copy-region beg end)))
10107 (concat "[" (mapconcat
10108 (lambda (x) (setq x (org-trim x))
10109 (if (equal x "") "0" x))
10110 l ",") "]"))
10111 ((string-match "\\([0-9]+\\)" desc)
10112 (beginning-of-line 1)
10113 (when (re-search-backward org-table-dataline-regexp tbeg t
10114 (string-to-number (match-string 0 desc)))
10115 (org-table-goto-column col)
10116 (org-trim (org-table-get-field))))))))
10118 (defvar org-table-formula-history nil)
10120 (defvar org-table-column-names nil
10121 "Alist with column names, derived from the `!' line.")
10122 (defvar org-table-column-name-regexp nil
10123 "Regular expression matching the current column names.")
10124 (defvar org-table-local-parameters nil
10125 "Alist with parameter names, derived from the `$' line.")
10126 (defvar org-table-named-field-locations nil
10127 "Alist with locations of named fields.")
10129 (defun org-table-get-formula (&optional equation named)
10130 "Read a formula from the minibuffer, offer stored formula as default."
10131 (let* ((name (car (rassoc (list (org-current-line)
10132 (org-table-current-column))
10133 org-table-named-field-locations)))
10134 (scol (if named
10135 (if name name
10136 (error "Not in a named field"))
10137 (int-to-string (org-table-current-column))))
10138 (dummy (and name (not named)
10139 (not (y-or-n-p "Replace named-field formula with column equation? " ))
10140 (error "Abort")))
10141 (org-table-may-need-update nil)
10142 (stored-list (org-table-get-stored-formulas))
10143 (stored (cdr (assoc scol stored-list)))
10144 (eq (cond
10145 ((and stored equation (string-match "^ *=? *$" equation))
10146 stored)
10147 ((stringp equation)
10148 equation)
10149 (t (read-string
10150 (format "%s formula $%s=" (if named "Field" "Column") scol)
10151 (or stored "") 'org-table-formula-history
10152 ;stored
10153 ))))
10154 mustsave)
10155 (when (not (string-match "\\S-" eq))
10156 ;; remove formula
10157 (setq stored-list (delq (assoc scol stored-list) stored-list))
10158 (org-table-store-formulas stored-list)
10159 (error "Formula removed"))
10160 (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
10161 (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
10162 (if (and name (not named))
10163 ;; We set the column equation, delete the named one.
10164 (setq stored-list (delq (assoc name stored-list) stored-list)
10165 mustsave t))
10166 (if stored
10167 (setcdr (assoc scol stored-list) eq)
10168 (setq stored-list (cons (cons scol eq) stored-list)))
10169 (if (or mustsave (not (equal stored eq)))
10170 (org-table-store-formulas stored-list))
10171 eq))
10173 (defun org-table-store-formulas (alist)
10174 "Store the list of formulas below the current table."
10175 (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
10176 (save-excursion
10177 (goto-char (org-table-end))
10178 (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
10179 (delete-region (point) (match-end 0)))
10180 (insert "#+TBLFM: "
10181 (mapconcat (lambda (x)
10182 (concat "$" (car x) "=" (cdr x)))
10183 alist "::")
10184 "\n")))
10186 (defun org-table-get-stored-formulas ()
10187 "Return an alist with the t=stored formulas directly after current table."
10188 (interactive)
10189 (let (scol eq eq-alist strings string seen)
10190 (save-excursion
10191 (goto-char (org-table-end))
10192 (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
10193 (setq strings (org-split-string (match-string 2) " *:: *"))
10194 (while (setq string (pop strings))
10195 (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string)
10196 (setq scol (match-string 1 string)
10197 eq (match-string 2 string)
10198 eq-alist (cons (cons scol eq) eq-alist))
10199 (if (member scol seen)
10200 (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
10201 (push scol seen))))))
10202 (nreverse eq-alist)))
10204 (defun org-table-modify-formulas (action &rest columns)
10205 "Modify the formulas stored below the current table.
10206 ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
10207 expected, for the other actions only a single column number is needed."
10208 (let ((list (org-table-get-stored-formulas))
10209 (nmax (length (org-split-string
10210 (buffer-substring (point-at-bol) (point-at-eol))
10211 "|")))
10212 col col1 col2 scol si sc1 sc2)
10213 (cond
10214 ((null list)) ; No action needed if there are no stored formulas
10215 ((eq action 'remove)
10216 (setq col (car columns)
10217 scol (int-to-string col))
10218 (org-table-replace-in-formulas list scol "INVALID")
10219 (if (assoc scol list) (setq list (delq (assoc scol list) list)))
10220 (loop for i from (1+ col) upto nmax by 1 do
10221 (setq si (int-to-string i))
10222 (org-table-replace-in-formulas list si (int-to-string (1- i)))
10223 (if (assoc si list) (setcar (assoc si list)
10224 (int-to-string (1- i))))))
10225 ((eq action 'insert)
10226 (setq col (car columns))
10227 (loop for i from nmax downto col by 1 do
10228 (setq si (int-to-string i))
10229 (org-table-replace-in-formulas list si (int-to-string (1+ i)))
10230 (if (assoc si list) (setcar (assoc si list)
10231 (int-to-string (1+ i))))))
10232 ((eq action 'swap)
10233 (setq col1 (car columns) col2 (nth 1 columns)
10234 sc1 (int-to-string col1) sc2 (int-to-string col2))
10235 ;; Hopefully, ZqZtZ will never be a name in a table
10236 (org-table-replace-in-formulas list sc1 "ZqZtZ")
10237 (org-table-replace-in-formulas list sc2 sc1)
10238 (org-table-replace-in-formulas list "ZqZtZ" sc2)
10239 (if (assoc sc1 list) (setcar (assoc sc1 list) "ZqZtZ"))
10240 (if (assoc sc2 list) (setcar (assoc sc2 list) sc1))
10241 (if (assoc "ZqZtZ" list) (setcar (assoc "ZqZtZ" list) sc2)))
10242 (t (error "Invalid action in `org-table-modify-formulas'")))
10243 (if list (org-table-store-formulas list))))
10245 (defun org-table-replace-in-formulas (list s1 s2)
10246 (let (elt re s)
10247 (setq s1 (concat "$" (if (integerp s1) (int-to-string s1) s1))
10248 s2 (concat "$" (if (integerp s2) (int-to-string s2) s2))
10249 re (concat (regexp-quote s1) "\\>"))
10250 (while (setq elt (pop list))
10251 (setq s (cdr elt))
10252 (while (string-match re s)
10253 (setq s (replace-match s2 t t s)))
10254 (setcdr elt s))))
10256 (defun org-table-get-specials ()
10257 "Get the column names and local parameters for this table."
10258 (save-excursion
10259 (let ((beg (org-table-begin)) (end (org-table-end))
10260 names name fields fields1 field cnt c v line col)
10261 (setq org-table-column-names nil
10262 org-table-local-parameters nil
10263 org-table-named-field-locations nil)
10264 (goto-char beg)
10265 (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
10266 (setq names (org-split-string (match-string 1) " *| *")
10267 cnt 1)
10268 (while (setq name (pop names))
10269 (setq cnt (1+ cnt))
10270 (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
10271 (push (cons name (int-to-string cnt)) org-table-column-names))))
10272 (setq org-table-column-names (nreverse org-table-column-names))
10273 (setq org-table-column-name-regexp
10274 (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
10275 (goto-char beg)
10276 (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
10277 (setq fields (org-split-string (match-string 1) " *| *"))
10278 (while (setq field (pop fields))
10279 (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
10280 (push (cons (match-string 1 field) (match-string 2 field))
10281 org-table-local-parameters))))
10282 (goto-char beg)
10283 (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
10284 (setq c (match-string 1)
10285 fields (org-split-string (match-string 2) " *| *"))
10286 (save-excursion
10287 (beginning-of-line (if (equal c "_") 2 0))
10288 (setq line (org-current-line) col 1)
10289 (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
10290 (setq fields1 (org-split-string (match-string 1) " *| *"))))
10291 (while (and fields1 (setq field (pop fields)))
10292 (setq v (pop fields1) col (1+ col))
10293 (when (and (stringp field) (stringp v)
10294 (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
10295 (push (cons field v) org-table-local-parameters)
10296 (push (list field line col) org-table-named-field-locations)))))))
10298 (defun org-this-word ()
10299 ;; Get the current word
10300 (save-excursion
10301 (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
10302 (end (progn (skip-chars-forward "^ \t\n") (point))))
10303 (buffer-substring-no-properties beg end))))
10305 (defun org-table-maybe-eval-formula ()
10306 "Check if the current field starts with \"=\" or \":=\".
10307 If yes, store the formula and apply it."
10308 ;; We already know we are in a table. Get field will only return a formula
10309 ;; when appropriate. It might return a separator line, but no problem.
10310 (when org-table-formula-evaluate-inline
10311 (let* ((field (org-trim (or (org-table-get-field) "")))
10312 named eq)
10313 (when (string-match "^:?=\\(.*\\)" field)
10314 (setq named (equal (string-to-char field) ?:)
10315 eq (match-string 1 field))
10316 (if (fboundp 'calc-eval)
10317 (org-table-eval-formula (if named '(4) nil) eq))))))
10319 (defvar org-recalc-commands nil
10320 "List of commands triggering the recalculation of a line.
10321 Will be filled automatically during use.")
10323 (defvar org-recalc-marks
10324 '((" " . "Unmarked: no special line, no automatic recalculation")
10325 ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
10326 ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
10327 ("!" . "Column name definition line. Reference in formula as $name.")
10328 ("$" . "Parameter definition line name=value. Reference in formula as $name.")
10329 ("_" . "Names for values in row below this one.")
10330 ("^" . "Names for values in row above this one.")))
10332 (defun org-table-rotate-recalc-marks (&optional newchar)
10333 "Rotate the recalculation mark in the first column.
10334 If in any row, the first field is not consistent with a mark,
10335 insert a new column for the markers.
10336 When there is an active region, change all the lines in the region,
10337 after prompting for the marking character.
10338 After each change, a message will be displayed indicating the meaning
10339 of the new mark."
10340 (interactive)
10341 (unless (org-at-table-p) (error "Not at a table"))
10342 (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
10343 (beg (org-table-begin))
10344 (end (org-table-end))
10345 (l (org-current-line))
10346 (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
10347 (l2 (if (org-region-active-p) (org-current-line (region-end))))
10348 (have-col
10349 (save-excursion
10350 (goto-char beg)
10351 (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
10352 (col (org-table-current-column))
10353 (forcenew (car (assoc newchar org-recalc-marks)))
10354 epos new)
10355 (when l1
10356 (message "Change region to what mark? Type # * ! $ or SPC: ")
10357 (setq newchar (char-to-string (read-char-exclusive))
10358 forcenew (car (assoc newchar org-recalc-marks))))
10359 (if (and newchar (not forcenew))
10360 (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
10361 newchar))
10362 (if l1 (goto-line l1))
10363 (save-excursion
10364 (beginning-of-line 1)
10365 (unless (looking-at org-table-dataline-regexp)
10366 (error "Not at a table data line")))
10367 (unless have-col
10368 (org-table-goto-column 1)
10369 (org-table-insert-column)
10370 (org-table-goto-column (1+ col)))
10371 (setq epos (point-at-eol))
10372 (save-excursion
10373 (beginning-of-line 1)
10374 (org-table-get-field
10375 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
10376 (concat " "
10377 (setq new (or forcenew
10378 (cadr (member (match-string 1) marks))))
10379 " ")
10380 " # ")))
10381 (if (and l1 l2)
10382 (progn
10383 (goto-line l1)
10384 (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
10385 (and (looking-at org-table-dataline-regexp)
10386 (org-table-get-field 1 (concat " " new " "))))
10387 (goto-line l1)))
10388 (if (not (= epos (point-at-eol))) (org-table-align))
10389 (goto-line l)
10390 (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
10392 (defun org-table-maybe-recalculate-line ()
10393 "Recompute the current line if marked for it, and if we haven't just done it."
10394 (interactive)
10395 (and org-table-allow-automatic-line-recalculation
10396 (not (and (memq last-command org-recalc-commands)
10397 (equal org-last-recalc-line (org-current-line))))
10398 (save-excursion (beginning-of-line 1)
10399 (looking-at org-table-auto-recalculate-regexp))
10400 (fboundp 'calc-eval)
10401 (org-table-recalculate) t))
10403 (defvar org-table-formula-debug nil
10404 "Non-nil means, debug table formulas.
10405 When nil, simply write \"#ERROR\" in corrupted fields.")
10407 (defvar modes)
10408 (defsubst org-set-calc-mode (var &optional value)
10409 (if (stringp var)
10410 (setq var (assoc var '(("D" calc-angle-mode deg)
10411 ("R" calc-angle-mode rad)
10412 ("F" calc-prefer-frac t)
10413 ("S" calc-symbolic-mode t)))
10414 value (nth 2 var) var (nth 1 var)))
10415 (if (memq var modes)
10416 (setcar (cdr (memq var modes)) value)
10417 (cons var (cons value modes)))
10418 modes)
10420 (defun org-table-eval-formula (&optional arg equation
10421 suppress-align suppress-const
10422 suppress-store)
10423 "Replace the table field value at the cursor by the result of a calculation.
10425 This function makes use of Dave Gillespie's Calc package, in my view the
10426 most exciting program ever written for GNU Emacs. So you need to have Calc
10427 installed in order to use this function.
10429 In a table, this command replaces the value in the current field with the
10430 result of a formula. It also installs the formula as the \"current\" column
10431 formula, by storing it in a special line below the table. When called
10432 with a `C-u' prefix, the current field must ba a named field, and the
10433 formula is installed as valid in only this specific field.
10435 When called, the command first prompts for a formula, which is read in
10436 the minibuffer. Previously entered formulas are available through the
10437 history list, and the last used formula is offered as a default.
10438 These stored formulas are adapted correctly when moving, inserting, or
10439 deleting columns with the corresponding commands.
10441 The formula can be any algebraic expression understood by the Calc package.
10442 For details, see the Org-mode manual.
10444 This function can also be called from Lisp programs and offers
10445 additional arguments: EQUATION can be the formula to apply. If this
10446 argument is given, the user will not be prompted. SUPPRESS-ALIGN is
10447 used to speed-up recursive calls by by-passing unnecessary aligns.
10448 SUPPRESS-CONST suppresses the interpretation of constants in the
10449 formula, assuming that this has been done already outside the function.
10450 SUPPRESS-STORE means the formula should not be stored, either because
10451 it is already stored, or because it is a modified equation that should
10452 not overwrite the stored one."
10453 (interactive "P")
10454 (require 'calc)
10455 (org-table-check-inside-data-field)
10456 (org-table-get-specials)
10457 (let* (fields
10458 (ndown (if (integerp arg) arg 1))
10459 (org-table-automatic-realign nil)
10460 (case-fold-search nil)
10461 (down (> ndown 1))
10462 (formula (if (and equation suppress-store)
10463 equation
10464 (org-table-get-formula equation (equal arg '(4)))))
10465 (n0 (org-table-current-column))
10466 (modes (copy-sequence org-calc-default-modes))
10467 n form fmt x ev orig c)
10468 ;; Parse the format string. Since we have a lot of modes, this is
10469 ;; a lot of work. However, I think calc still uses most of the time.
10470 (if (string-match ";" formula)
10471 (let ((tmp (org-split-string formula ";")))
10472 (setq formula (car tmp)
10473 fmt (concat (cdr (assoc "%" org-table-local-parameters))
10474 (nth 1 tmp)))
10475 (while (string-match "[pnfse]\\(-?[0-9]+\\)" fmt)
10476 (setq c (string-to-char (match-string 1 fmt))
10477 n (string-to-number (or (match-string 1 fmt) "")))
10478 (if (= c ?p) (setq modes (org-set-calc-mode 'calc-internal-prec n))
10479 (setq modes (org-set-calc-mode
10480 'calc-float-format
10481 (list (cdr (assoc c '((?n . float) (?f . fix)
10482 (?s . sci) (?e . eng))))
10483 n))))
10484 (setq fmt (replace-match "" t t fmt)))
10485 (while (string-match "[DRFS]" fmt)
10486 (setq modes (org-set-calc-mode (match-string 0 fmt)))
10487 (setq fmt (replace-match "" t t fmt)))
10488 (unless (string-match "\\S-" fmt)
10489 (setq fmt nil))))
10490 (if (and (not suppress-const) org-table-formula-use-constants)
10491 (setq formula (org-table-formula-substitute-names formula)))
10492 (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
10493 (while (> ndown 0)
10494 (setq fields (org-split-string
10495 (buffer-substring
10496 (point-at-bol) (point-at-eol)) " *| *"))
10497 (if org-table-formula-numbers-only
10498 (setq fields (mapcar
10499 (lambda (x) (number-to-string (string-to-number x)))
10500 fields)))
10501 (setq ndown (1- ndown))
10502 (setq form (copy-sequence formula))
10503 ;; Insert the references to fields in same row
10504 (while (string-match "\\$\\([0-9]+\\)?" form)
10505 (setq n (if (match-beginning 1)
10506 (string-to-number (match-string 1 form))
10508 x (nth (1- n) fields))
10509 (unless x (error "Invalid field specifier \"%s\""
10510 (match-string 0 form)))
10511 (if (equal x "") (setq x "0"))
10512 (setq form (replace-match (concat "(" x ")") t t form)))
10513 ;; Insert ranges in current column
10514 (while (string-match "\\&[-I0-9]+" form)
10515 (setq form (replace-match
10516 (save-match-data
10517 (org-table-get-vertical-vector (match-string 0 form)
10518 nil n0))
10519 t t form)))
10520 (setq ev (calc-eval (cons form modes)
10521 (if org-table-formula-numbers-only 'num)))
10523 (when org-table-formula-debug
10524 (with-output-to-temp-buffer "*Help*"
10525 (princ (format "Substitution history of formula
10526 Orig: %s
10527 $xyz-> %s
10528 $1-> %s\n" orig formula form))
10529 (if (listp ev)
10530 (princ (format " %s^\nError: %s"
10531 (make-string (car ev) ?\-) (nth 1 ev)))
10532 (princ (format "Result: %s\nFormat: %s\nFinal: %s"
10533 ev (or fmt "NONE")
10534 (if fmt (format fmt (string-to-number ev)) ev)))))
10535 (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
10536 (unless (and (interactive-p) (not ndown))
10537 (unless (let (inhibit-redisplay)
10538 (y-or-n-p "Debugging Formula. Continue to next? "))
10539 (org-table-align)
10540 (error "Abort"))
10541 (delete-window (get-buffer-window "*Help*"))
10542 (message "")))
10543 (if (listp ev) (setq fmt nil ev "#ERROR"))
10544 (org-table-justify-field-maybe
10545 (if fmt (format fmt (string-to-number ev)) ev))
10546 (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
10547 (call-interactively 'org-return)
10548 (setq ndown 0)))
10549 (and down (org-table-maybe-recalculate-line))
10550 (or suppress-align (and org-table-may-need-update
10551 (org-table-align)))))
10553 (defun org-table-recalculate (&optional all noalign)
10554 "Recalculate the current table line by applying all stored formulas.
10555 With prefix arg ALL, do this for all lines in the table."
10556 (interactive "P")
10557 (or (memq this-command org-recalc-commands)
10558 (setq org-recalc-commands (cons this-command org-recalc-commands)))
10559 (unless (org-at-table-p) (error "Not at a table"))
10560 (org-table-get-specials)
10561 (let* ((eqlist (sort (org-table-get-stored-formulas)
10562 (lambda (a b) (string< (car a) (car b)))))
10563 (inhibit-redisplay t)
10564 (line-re org-table-dataline-regexp)
10565 (thisline (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
10566 (thiscol (org-table-current-column))
10567 beg end entry eqlnum eqlname eql (cnt 0) eq a name)
10568 ;; Insert constants in all formulas
10569 (setq eqlist
10570 (mapcar (lambda (x)
10571 (setcdr x (org-table-formula-substitute-names (cdr x)))
10573 eqlist))
10574 ;; Split the equation list
10575 (while (setq eq (pop eqlist))
10576 (if (<= (string-to-char (car eq)) ?9)
10577 (push eq eqlnum)
10578 (push eq eqlname)))
10579 (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
10580 (if all
10581 (progn
10582 (setq end (move-marker (make-marker) (1+ (org-table-end))))
10583 (goto-char (setq beg (org-table-begin)))
10584 (if (re-search-forward org-table-calculate-mark-regexp end t)
10585 ;; This is a table with marked lines, only compute selected lines
10586 (setq line-re org-table-recalculate-regexp)
10587 ;; Move forward to the first non-header line
10588 (if (and (re-search-forward org-table-dataline-regexp end t)
10589 (re-search-forward org-table-hline-regexp end t)
10590 (re-search-forward org-table-dataline-regexp end t))
10591 (setq beg (match-beginning 0))
10592 nil))) ;; just leave beg where it is
10593 (setq beg (point-at-bol)
10594 end (move-marker (make-marker) (1+ (point-at-eol)))))
10595 (goto-char beg)
10596 (and all (message "Re-applying formulas to full table..."))
10597 (while (re-search-forward line-re end t)
10598 (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
10599 ;; Unprotected line, recalculate
10600 (and all (message "Re-applying formulas to full table...(line %d)"
10601 (setq cnt (1+ cnt))))
10602 (setq org-last-recalc-line (org-current-line))
10603 (setq eql eqlnum)
10604 (while (setq entry (pop eql))
10605 (goto-line org-last-recalc-line)
10606 (org-table-goto-column (string-to-number (car entry)) nil 'force)
10607 (org-table-eval-formula nil (cdr entry) 'noalign 'nocst 'nostore))))
10608 (goto-line thisline)
10609 (org-table-goto-column thiscol)
10610 (or noalign (and org-table-may-need-update (org-table-align))
10611 (and all (message "Re-applying formulas to %d lines...done" cnt)))
10612 ;; Now do the names fields
10613 (while (setq eq (pop eqlname))
10614 (setq name (car eq)
10615 a (assoc name org-table-named-field-locations))
10616 (when a
10617 (message "Re-applying formula to named field: %s" name)
10618 (goto-line (nth 1 a))
10619 (org-table-goto-column (nth 2 a))
10620 (org-table-eval-formula nil (cdr eq) 'noalign 'nocst 'nostore)))
10621 ;; back to initial position
10622 (goto-line thisline)
10623 (org-table-goto-column thiscol)
10624 (or noalign (and org-table-may-need-update (org-table-align))
10625 (and all (message "Re-applying formulas...done")))))
10627 (defun org-table-formula-substitute-names (f)
10628 "Replace $const with values in string F."
10629 (let ((start 0) a n1 n2 nn1 nn2 s (f1 f))
10630 ;; First, check for column names
10631 (while (setq start (string-match org-table-column-name-regexp f start))
10632 (setq start (1+ start))
10633 (setq a (assoc (match-string 1 f) org-table-column-names))
10634 (setq f (replace-match (concat "$" (cdr a)) t t f)))
10635 ;; Expand ranges to vectors
10636 (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f)
10637 (setq n1 (string-to-number (match-string 1 f))
10638 n2 (string-to-number (match-string 2 f))
10639 nn1 (1+ (min n1 n2)) nn2 (max n1 n2)
10640 s (concat "[($" (number-to-string (1- nn1)) ")"))
10641 (loop for i from nn1 upto nn2 do
10642 (setq s (concat s ",($" (int-to-string i) ")")))
10643 (setq s (concat s "]"))
10644 (if (< n2 n1) (setq s (concat "rev(" s ")")))
10645 (setq f (replace-match s t t f)))
10646 ;; Parameters and constants
10647 (setq start 0)
10648 (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
10649 (setq start (1+ start))
10650 (if (setq a (save-match-data
10651 (org-table-get-constant (match-string 1 f))))
10652 (setq f (replace-match (concat "(" a ")") t t f))))
10653 (if org-table-formula-debug
10654 (put-text-property 0 (length f) :orig-formula f1 f))
10657 (defun org-table-get-constant (const)
10658 "Find the value for a parameter or constant in a formula.
10659 Parameters get priority."
10660 (or (cdr (assoc const org-table-local-parameters))
10661 (cdr (assoc const org-table-formula-constants))
10662 (and (fboundp 'constants-get) (constants-get const))
10663 "#UNDEFINED_NAME"))
10665 (defvar org-edit-formulas-map (make-sparse-keymap))
10666 (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
10667 (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
10668 (define-key org-edit-formulas-map "\C-c?" 'org-show-variable)
10670 (defvar org-pos)
10672 (defun org-table-edit-formulas ()
10673 "Edit the formulas of the current table in a separate buffer."
10674 (interactive)
10675 (unless (org-at-table-p)
10676 (error "Not at a table"))
10677 (org-table-get-specials)
10678 (let ((eql (org-table-get-stored-formulas))
10679 (pos (move-marker (make-marker) (point)))
10680 (wc (current-window-configuration))
10681 entry loc s)
10682 (switch-to-buffer-other-window "*Edit Formulas*")
10683 (erase-buffer)
10684 (fundamental-mode)
10685 (set (make-local-variable 'org-pos) pos)
10686 (set (make-local-variable 'org-window-configuration) wc)
10687 (use-local-map org-edit-formulas-map)
10688 (setq s "# Edit formulas and finish with `C-c C-c'.
10689 # Use `C-u C-c C-c' to also appy them immediately to the entire table.
10690 # Use `C-c ?' to get information about $name at point.
10691 # To cancel editing, press `C-c C-q'.\n")
10692 (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
10693 (insert s)
10694 (while (setq entry (pop eql))
10695 (when (setq loc (assoc (car entry) org-table-named-field-locations))
10696 (setq s (format "# Named formula, referring to column %d in line %d\n"
10697 (nth 2 loc) (nth 1 loc)))
10698 (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
10699 (insert s))
10700 (setq s (concat "$" (car entry) "=" (cdr entry) "\n"))
10701 (remove-text-properties 0 (length s) '(face nil) s)
10702 (insert s))
10703 (goto-char (point-min))
10704 (message "Edit formulas and finish with `C-c C-c'.")))
10706 (defun org-show-variable ()
10707 "Show the location/value of the $ expression at point."
10708 (interactive)
10709 (let (var (pos org-pos) (win (selected-window)) e)
10710 (save-excursion
10711 (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
10712 (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
10713 (setq var (match-string 1))
10714 (error "No variable at point")))
10715 (cond
10716 ((setq e (assoc var org-table-named-field-locations))
10717 (switch-to-buffer-other-window (marker-buffer pos))
10718 (goto-line (nth 1 e))
10719 (org-table-goto-column (nth 2 e))
10720 (select-window win)
10721 (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
10722 ((setq e (assoc var org-table-column-names))
10723 (switch-to-buffer-other-window (marker-buffer pos))
10724 (goto-char pos)
10725 (goto-char (org-table-begin))
10726 (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
10727 (org-table-end) t)
10728 (progn
10729 (goto-char (match-beginning 1))
10730 (message "Named column (column %s)" (cdr e)))
10731 (error "Column name not found"))
10732 (select-window win))
10733 ((string-match "^[0-9]$" var)
10734 ;; column number
10735 (switch-to-buffer-other-window (marker-buffer pos))
10736 (goto-char pos)
10737 (goto-char (org-table-begin))
10738 (recenter 1)
10739 (if (re-search-forward org-table-dataline-regexp
10740 (org-table-end) t)
10741 (progn
10742 (goto-char (match-beginning 0))
10743 (org-table-goto-column (string-to-number var))
10744 (message "Column %s" var))
10745 (error "Column name not found"))
10746 (select-window win))
10747 ((setq e (assoc var org-table-local-parameters))
10748 (switch-to-buffer-other-window (marker-buffer pos))
10749 (goto-char pos)
10750 (goto-char (org-table-begin))
10751 (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
10752 (progn
10753 (goto-char (match-beginning 1))
10754 (message "Local parameter."))
10755 (error "Parameter not found"))
10756 (select-window win))
10758 (cond
10759 ((setq e (assoc var org-table-formula-constants))
10760 (message "Constant: $%s=%s in `org-table-formula-constants'." var (cdr e)))
10761 ((setq e (and (fboundp 'constants-get) (constants-get var)))
10762 (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
10763 (t (error "Undefined name $%s" var)))))))
10765 (defun org-finish-edit-formulas (&optional arg)
10766 "Parse the buffer for formula definitions and install them.
10767 With prefix ARG, apply the new formulas to the table."
10768 (interactive "P")
10769 (let ((pos org-pos) eql)
10770 (goto-char (point-min))
10771 (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t)
10772 (push (cons (match-string 1) (match-string 2)) eql))
10773 (set-window-configuration org-window-configuration)
10774 (select-window (get-buffer-window (marker-buffer pos)))
10775 (goto-char pos)
10776 (unless (org-at-table-p)
10777 (error "Lost table position - cannot install formulae"))
10778 (org-table-store-formulas eql)
10779 (move-marker pos nil)
10780 (kill-buffer "*Edit Formulas*")
10781 (if arg
10782 (org-table-recalculate 'all)
10783 (message "New formulas installed - press C-u C-c C-c to apply."))))
10785 (defun org-abort-edit-formulas ()
10786 "Abort editing formulas, without installing the changes."
10787 (interactive)
10788 (let ((pos org-pos))
10789 (set-window-configuration org-window-configuration)
10790 (select-window (get-buffer-window (marker-buffer pos)))
10791 (goto-char pos)
10792 (message "Formula editing aborted without installing changes")))
10794 ;;; The orgtbl minor mode
10796 ;; Define a minor mode which can be used in other modes in order to
10797 ;; integrate the org-mode table editor.
10799 ;; This is really a hack, because the org-mode table editor uses several
10800 ;; keys which normally belong to the major mode, for example the TAB and
10801 ;; RET keys. Here is how it works: The minor mode defines all the keys
10802 ;; necessary to operate the table editor, but wraps the commands into a
10803 ;; function which tests if the cursor is currently inside a table. If that
10804 ;; is the case, the table editor command is executed. However, when any of
10805 ;; those keys is used outside a table, the function uses `key-binding' to
10806 ;; look up if the key has an associated command in another currently active
10807 ;; keymap (minor modes, major mode, global), and executes that command.
10808 ;; There might be problems if any of the keys used by the table editor is
10809 ;; otherwise used as a prefix key.
10811 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
10812 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
10813 ;; addresses this by checking explicitly for both bindings.
10815 ;; The optimized version (see variable `orgtbl-optimized') takes over
10816 ;; all keys which are bound to `self-insert-command' in the *global map*.
10817 ;; Some modes bind other commands to simple characters, for example
10818 ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
10819 ;; active, this binding is ignored inside tables and replaced with a
10820 ;; modified self-insert.
10822 (defvar orgtbl-mode nil
10823 "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
10824 table editor in arbitrary modes.")
10825 (make-variable-buffer-local 'orgtbl-mode)
10827 (defvar orgtbl-mode-map (make-keymap)
10828 "Keymap for `orgtbl-mode'.")
10830 ;;;###autoload
10831 (defun turn-on-orgtbl ()
10832 "Unconditionally turn on `orgtbl-mode'."
10833 (orgtbl-mode 1))
10835 ;;;###autoload
10836 (defun orgtbl-mode (&optional arg)
10837 "The `org-mode' table editor as a minor mode for use in other modes."
10838 (interactive)
10839 (if (eq major-mode 'org-mode)
10840 ;; Exit without error, in case some hook functions calls this
10841 ;; by accident in org-mode.
10842 (message "Orgtbl-mode is not useful in org-mode, command ignored")
10843 (setq orgtbl-mode
10844 (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
10845 (if orgtbl-mode
10846 (progn
10847 (and (orgtbl-setup) (defun orgtbl-setup () nil))
10848 ;; Make sure we are first in minor-mode-map-alist
10849 (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
10850 (and c (setq minor-mode-map-alist
10851 (cons c (delq c minor-mode-map-alist)))))
10852 (set (make-local-variable (quote org-table-may-need-update)) t)
10853 (org-add-hook 'before-change-functions 'org-before-change-function
10854 nil 'local)
10855 (set (make-local-variable 'org-old-auto-fill-inhibit-regexp)
10856 auto-fill-inhibit-regexp)
10857 (set (make-local-variable 'auto-fill-inhibit-regexp)
10858 (if auto-fill-inhibit-regexp
10859 (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
10860 "[ \t]*|"))
10861 (org-add-to-invisibility-spec '(org-cwidth))
10862 (easy-menu-add orgtbl-mode-menu)
10863 (run-hooks 'orgtbl-mode-hook))
10864 (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
10865 (org-cleanup-narrow-column-properties)
10866 (org-remove-from-invisibility-spec '(org-cwidth))
10867 (remove-hook 'before-change-functions 'org-before-change-function t)
10868 (easy-menu-remove orgtbl-mode-menu)
10869 (force-mode-line-update 'all))))
10871 (defun org-cleanup-narrow-column-properties ()
10872 "Remove all properties related to narrow-column invisibility."
10873 (let ((s 1))
10874 (while (setq s (text-property-any s (point-max)
10875 'display org-narrow-column-arrow))
10876 (remove-text-properties s (1+ s) '(display t)))
10877 (setq s 1)
10878 (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
10879 (remove-text-properties s (1+ s) '(org-cwidth t)))
10880 (setq s 1)
10881 (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
10882 (remove-text-properties s (1+ s) '(invisible t)))))
10884 ;; Install it as a minor mode.
10885 (put 'orgtbl-mode :included t)
10886 (put 'orgtbl-mode :menu-tag "Org Table Mode")
10887 (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
10889 (defun orgtbl-make-binding (fun n &rest keys)
10890 "Create a function for binding in the table minor mode.
10891 FUN is the command to call inside a table. N is used to create a unique
10892 command name. KEYS are keys that should be checked in for a command
10893 to execute outside of tables."
10894 (eval
10895 (list 'defun
10896 (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
10897 '(arg)
10898 (concat "In tables, run `" (symbol-name fun) "'.\n"
10899 "Outside of tables, run the binding of `"
10900 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
10901 "'.")
10902 '(interactive "p")
10903 (list 'if
10904 '(org-at-table-p)
10905 (list 'call-interactively (list 'quote fun))
10906 (list 'let '(orgtbl-mode)
10907 (list 'call-interactively
10908 (append '(or)
10909 (mapcar (lambda (k)
10910 (list 'key-binding k))
10911 keys)
10912 '('orgtbl-error))))))))
10914 (defun orgtbl-error ()
10915 "Error when there is no default binding for a table key."
10916 (interactive)
10917 (error "This key is has no function outside tables"))
10919 (defun orgtbl-setup ()
10920 "Setup orgtbl keymaps."
10921 (let ((nfunc 0)
10922 (bindings
10923 (list
10924 '([(meta shift left)] org-table-delete-column)
10925 '([(meta left)] org-table-move-column-left)
10926 '([(meta right)] org-table-move-column-right)
10927 '([(meta shift right)] org-table-insert-column)
10928 '([(meta shift up)] org-table-kill-row)
10929 '([(meta shift down)] org-table-insert-row)
10930 '([(meta up)] org-table-move-row-up)
10931 '([(meta down)] org-table-move-row-down)
10932 '("\C-c\C-w" org-table-cut-region)
10933 '("\C-c\M-w" org-table-copy-region)
10934 '("\C-c\C-y" org-table-paste-rectangle)
10935 '("\C-c-" org-table-insert-hline)
10936 ; '([(shift tab)] org-table-previous-field)
10937 '("\C-m" org-table-next-row)
10938 (list (org-key 'S-return) 'org-table-copy-down)
10939 '([(meta return)] org-table-wrap-region)
10940 '("\C-c\C-q" org-table-wrap-region)
10941 '("\C-c?" org-table-current-column)
10942 '("\C-c " org-table-blank-field)
10943 '("\C-c+" org-table-sum)
10944 '("\C-c=" org-table-eval-formula)
10945 '("\C-c'" org-table-edit-formulas)
10946 '("\C-c`" org-table-edit-field)
10947 '("\C-c*" org-table-recalculate)
10948 '("\C-c|" org-table-create-or-convert-from-region)
10949 '("\C-c^" org-table-sort-lines)
10950 '([(control ?#)] org-table-rotate-recalc-marks)))
10951 elt key fun cmd)
10952 (while (setq elt (pop bindings))
10953 (setq nfunc (1+ nfunc))
10954 (setq key (car elt)
10955 fun (nth 1 elt)
10956 cmd (orgtbl-make-binding fun nfunc key))
10957 (define-key orgtbl-mode-map key cmd))
10958 ;; Special treatment needed for TAB and RET
10959 (define-key orgtbl-mode-map [(return)]
10960 (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
10961 (define-key orgtbl-mode-map "\C-m"
10962 (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
10963 (define-key orgtbl-mode-map [(tab)]
10964 (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
10965 (define-key orgtbl-mode-map "\C-i"
10966 (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
10967 (define-key orgtbl-mode-map "\C-i"
10968 (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
10969 (define-key orgtbl-mode-map "\C-c\C-c"
10970 (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
10971 (when orgtbl-optimized
10972 ;; If the user wants maximum table support, we need to hijack
10973 ;; some standard editing functions
10974 (org-remap orgtbl-mode-map
10975 'self-insert-command 'orgtbl-self-insert-command
10976 'delete-char 'org-delete-char
10977 'delete-backward-char 'org-delete-backward-char)
10978 (define-key orgtbl-mode-map "|" 'org-force-self-insert))
10979 (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
10980 '("OrgTbl"
10981 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
10982 ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
10983 ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
10984 ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
10985 "--"
10986 ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
10987 ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
10988 ["Copy Field from Above"
10989 org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
10990 "--"
10991 ("Column"
10992 ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
10993 ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
10994 ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
10995 ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"]
10996 "--"
10997 ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :active (org-at-table-p) :selected org-table-limit-column-width :style toggle])
10998 ("Row"
10999 ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
11000 ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
11001 ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
11002 ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
11003 ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
11004 "--"
11005 ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
11006 ("Rectangle"
11007 ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
11008 ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
11009 ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
11010 ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
11011 "--"
11012 ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
11013 ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
11014 ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
11015 ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
11016 ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
11017 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
11018 ["Sum Column/Rectangle" org-table-sum
11019 :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
11020 ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
11021 ["Debug Formulas"
11022 (setq org-table-formula-debug (not org-table-formula-debug))
11023 :style toggle :selected org-table-formula-debug]
11027 (defun orgtbl-tab (arg)
11028 "Justification and field motion for `orgtbl-mode'."
11029 (interactive "P")
11030 (if arg (org-table-edit-field t)
11031 (org-table-justify-field-maybe)
11032 (org-table-next-field)))
11034 (defun orgtbl-ret ()
11035 "Justification and field motion for `orgtbl-mode'."
11036 (interactive)
11037 (org-table-justify-field-maybe)
11038 (org-table-next-row))
11040 (defun orgtbl-self-insert-command (N)
11041 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
11042 If the cursor is in a table looking at whitespace, the whitespace is
11043 overwritten, and the table is not marked as requiring realignment."
11044 (interactive "p")
11045 (if (and (org-at-table-p)
11047 (and org-table-auto-blank-field
11048 (member last-command
11049 '(orgtbl-hijacker-command-100
11050 orgtbl-hijacker-command-101
11051 orgtbl-hijacker-command-102
11052 orgtbl-hijacker-command-103
11053 orgtbl-hijacker-command-104
11054 orgtbl-hijacker-command-105))
11055 (org-table-blank-field))
11057 (eq N 1)
11058 (looking-at "[^|\n]* +|"))
11059 (let (org-table-may-need-update)
11060 (goto-char (1- (match-end 0)))
11061 (delete-backward-char 1)
11062 (goto-char (match-beginning 0))
11063 (self-insert-command N))
11064 (setq org-table-may-need-update t)
11065 (let (orgtbl-mode)
11066 (call-interactively (key-binding (vector last-input-event))))))
11068 (defun org-force-self-insert (N)
11069 "Needed to enforce self-insert under remapping."
11070 (interactive "p")
11071 (self-insert-command N))
11073 ;;; Exporting
11075 (defconst org-level-max 20)
11077 (defvar org-export-html-preamble nil
11078 "Preamble, to be inserted just after <body>. Set by publishing functions.")
11079 (defvar org-export-html-postamble nil
11080 "Preamble, to be inserted just before </body>. Set by publishing functions.")
11081 (defvar org-export-html-auto-preamble t
11082 "Should default preamble be inserted? Set by publishing functions.")
11083 (defvar org-export-html-auto-postamble t
11084 "Should default postamble be inserted? Set by publishing functions.")
11086 (defconst org-export-plist-vars
11087 '((:language . org-export-default-language)
11088 (:headline-levels . org-export-headline-levels)
11089 (:section-numbers . org-export-with-section-numbers)
11090 (:table-of-contents . org-export-with-toc)
11091 (:emphasize . org-export-with-emphasize)
11092 (:sub-superscript . org-export-with-sub-superscripts)
11093 (:TeX-macros . org-export-with-TeX-macros)
11094 (:fixed-width . org-export-with-fixed-width)
11095 (:tables . org-export-with-tables)
11096 (:table-auto-headline . org-export-highlight-first-table-line)
11097 (:style . org-export-html-style)
11098 (:convert-org-links . org-export-html-link-org-files-as-html)
11099 (:inline-images . org-export-html-inline-images)
11100 (:expand-quoted-html . org-export-html-expand)
11101 (:timestamp . org-export-html-with-timestamp)
11102 (:publishing-directory . org-export-publishing-directory)
11103 (:preamble . org-export-html-preamble)
11104 (:postamble . org-export-html-postamble)
11105 (:auto-preamble . org-export-html-auto-preamble)
11106 (:auto-postamble . org-export-html-auto-postamble)
11107 (:author . user-full-name)
11108 (:email . user-mail-address)))
11110 (defun org-default-export-plist ()
11111 "Return the property list with default settings for the export variables."
11112 (let ((l org-export-plist-vars) rtn e)
11113 (while (setq e (pop l))
11114 (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
11115 rtn))
11117 (defun org-infile-export-plist ()
11118 "Return the property list with file-local settings for export."
11119 (save-excursion
11120 (goto-char 0)
11121 (let ((re (org-make-options-regexp
11122 '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
11123 (text nil)
11124 p key val text options)
11125 (while (re-search-forward re nil t)
11126 (setq key (org-match-string-no-properties 1)
11127 val (org-match-string-no-properties 2))
11128 (cond
11129 ((string-equal key "TITLE") (setq p (plist-put p :title val)))
11130 ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
11131 ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
11132 ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
11133 ((string-equal key "TEXT")
11134 (setq text (if text (concat text "\n" val) val)))
11135 ((string-equal key "OPTIONS") (setq options val))))
11136 (setq p (plist-put p :text text))
11137 (when options
11138 (let ((op '(("H" . :headline-levels)
11139 ("num" . :section-numbers)
11140 ("toc" . :table-of-contents)
11141 ("\\n" . :preserve-breaks)
11142 ("@" . :expand-quoted-html)
11143 (":" . :fixed-width)
11144 ("|" . :tables)
11145 ("^" . :sub-superscript)
11146 ("*" . :emphasize)
11147 ("TeX" . :TeX-macros)))
11149 (while (setq o (pop op))
11150 (if (string-match (concat (regexp-quote (car o))
11151 ":\\([^ \t\n\r;,.]*\\)")
11152 options)
11153 (setq p (plist-put p (cdr o)
11154 (car (read-from-string
11155 (match-string 1 options)))))))))
11156 p)))
11158 (defun org-combine-plists (&rest plists)
11159 "Create a single property list from all plists in PLISTS.
11160 The process starts by copying the last list, and then setting properties
11161 from the other lists. Settings in the first list are the most significant
11162 ones and overrule settings in the other lists."
11163 (let ((rtn (copy-sequence (pop plists)))
11164 p v ls)
11165 (while plists
11166 (setq ls (pop plists))
11167 (while ls
11168 (setq p (pop ls) v (pop ls))
11169 (setq rtn (plist-put rtn p v))))
11170 rtn))
11172 (defun org-export-find-first-heading-line (list)
11173 "Remove all lines from LIST which are before the first headline."
11174 (let ((orig-list list)
11175 (re (concat "^" outline-regexp)))
11176 (while (and list
11177 (not (string-match re (car list))))
11178 (pop list))
11179 (or list orig-list)))
11181 (defun org-skip-comments (lines)
11182 "Skip lines starting with \"#\" and subtrees starting with COMMENT."
11183 (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
11184 (re2 "^\\(\\*+\\)[ \t\n\r]")
11185 rtn line level)
11186 (while (setq line (pop lines))
11187 (cond
11188 ((and (string-match re1 line)
11189 (setq level (- (match-end 1) (match-beginning 1))))
11190 ;; Beginning of a COMMENT subtree. Skip it.
11191 (while (and (setq line (pop lines))
11192 (or (not (string-match re2 line))
11193 (> (- (match-end 1) (match-beginning 1)) level))))
11194 (setq lines (cons line lines)))
11195 ((string-match "^#" line)
11196 ;; an ordinary comment line
11198 ((and org-export-table-remove-special-lines
11199 (string-match "^[ \t]*| *[!_^] *|" line))
11200 ;; a special table line that should be removed
11202 (t (setq rtn (cons line rtn)))))
11203 (nreverse rtn)))
11205 ;; ASCII
11207 (defconst org-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
11208 "Characters for underlining headings in ASCII export.")
11210 (defconst org-html-entities
11211 '(("nbsp")
11212 ("iexcl")
11213 ("cent")
11214 ("pound")
11215 ("curren")
11216 ("yen")
11217 ("brvbar")
11218 ("sect")
11219 ("uml")
11220 ("copy")
11221 ("ordf")
11222 ("laquo")
11223 ("not")
11224 ("shy")
11225 ("reg")
11226 ("macr")
11227 ("deg")
11228 ("plusmn")
11229 ("sup2")
11230 ("sup3")
11231 ("acute")
11232 ("micro")
11233 ("para")
11234 ("middot")
11235 ("odot"."o")
11236 ("star"."*")
11237 ("cedil")
11238 ("sup1")
11239 ("ordm")
11240 ("raquo")
11241 ("frac14")
11242 ("frac12")
11243 ("frac34")
11244 ("iquest")
11245 ("Agrave")
11246 ("Aacute")
11247 ("Acirc")
11248 ("Atilde")
11249 ("Auml")
11250 ("Aring") ("AA"."&Aring;")
11251 ("AElig")
11252 ("Ccedil")
11253 ("Egrave")
11254 ("Eacute")
11255 ("Ecirc")
11256 ("Euml")
11257 ("Igrave")
11258 ("Iacute")
11259 ("Icirc")
11260 ("Iuml")
11261 ("ETH")
11262 ("Ntilde")
11263 ("Ograve")
11264 ("Oacute")
11265 ("Ocirc")
11266 ("Otilde")
11267 ("Ouml")
11268 ("times")
11269 ("Oslash")
11270 ("Ugrave")
11271 ("Uacute")
11272 ("Ucirc")
11273 ("Uuml")
11274 ("Yacute")
11275 ("THORN")
11276 ("szlig")
11277 ("agrave")
11278 ("aacute")
11279 ("acirc")
11280 ("atilde")
11281 ("auml")
11282 ("aring")
11283 ("aelig")
11284 ("ccedil")
11285 ("egrave")
11286 ("eacute")
11287 ("ecirc")
11288 ("euml")
11289 ("igrave")
11290 ("iacute")
11291 ("icirc")
11292 ("iuml")
11293 ("eth")
11294 ("ntilde")
11295 ("ograve")
11296 ("oacute")
11297 ("ocirc")
11298 ("otilde")
11299 ("ouml")
11300 ("divide")
11301 ("oslash")
11302 ("ugrave")
11303 ("uacute")
11304 ("ucirc")
11305 ("uuml")
11306 ("yacute")
11307 ("thorn")
11308 ("yuml")
11309 ("fnof")
11310 ("Alpha")
11311 ("Beta")
11312 ("Gamma")
11313 ("Delta")
11314 ("Epsilon")
11315 ("Zeta")
11316 ("Eta")
11317 ("Theta")
11318 ("Iota")
11319 ("Kappa")
11320 ("Lambda")
11321 ("Mu")
11322 ("Nu")
11323 ("Xi")
11324 ("Omicron")
11325 ("Pi")
11326 ("Rho")
11327 ("Sigma")
11328 ("Tau")
11329 ("Upsilon")
11330 ("Phi")
11331 ("Chi")
11332 ("Psi")
11333 ("Omega")
11334 ("alpha")
11335 ("beta")
11336 ("gamma")
11337 ("delta")
11338 ("epsilon")
11339 ("varepsilon"."&epsilon;")
11340 ("zeta")
11341 ("eta")
11342 ("theta")
11343 ("iota")
11344 ("kappa")
11345 ("lambda")
11346 ("mu")
11347 ("nu")
11348 ("xi")
11349 ("omicron")
11350 ("pi")
11351 ("rho")
11352 ("sigmaf") ("varsigma"."&sigmaf;")
11353 ("sigma")
11354 ("tau")
11355 ("upsilon")
11356 ("phi")
11357 ("chi")
11358 ("psi")
11359 ("omega")
11360 ("thetasym") ("vartheta"."&thetasym;")
11361 ("upsih")
11362 ("piv")
11363 ("bull") ("bullet"."&bull;")
11364 ("hellip") ("dots"."&hellip;")
11365 ("prime")
11366 ("Prime")
11367 ("oline")
11368 ("frasl")
11369 ("weierp")
11370 ("image")
11371 ("real")
11372 ("trade")
11373 ("alefsym")
11374 ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
11375 ("uarr") ("uparrow"."&uarr;")
11376 ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
11377 ("darr")("downarrow"."&darr;")
11378 ("harr") ("leftrightarrow"."&harr;")
11379 ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
11380 ("lArr") ("Leftarrow"."&lArr;")
11381 ("uArr") ("Uparrow"."&uArr;")
11382 ("rArr") ("Rightarrow"."&rArr;")
11383 ("dArr") ("Downarrow"."&dArr;")
11384 ("hArr") ("Leftrightarrow"."&hArr;")
11385 ("forall")
11386 ("part") ("partial"."&part;")
11387 ("exist") ("exists"."&exist;")
11388 ("empty") ("emptyset"."&empty;")
11389 ("nabla")
11390 ("isin") ("in"."&isin;")
11391 ("notin")
11392 ("ni")
11393 ("prod")
11394 ("sum")
11395 ("minus")
11396 ("lowast") ("ast"."&lowast;")
11397 ("radic")
11398 ("prop") ("proptp"."&prop;")
11399 ("infin") ("infty"."&infin;")
11400 ("ang") ("angle"."&ang;")
11401 ("and") ("vee"."&and;")
11402 ("or") ("wedge"."&or;")
11403 ("cap")
11404 ("cup")
11405 ("int")
11406 ("there4")
11407 ("sim")
11408 ("cong") ("simeq"."&cong;")
11409 ("asymp")("approx"."&asymp;")
11410 ("ne") ("neq"."&ne;")
11411 ("equiv")
11412 ("le")
11413 ("ge")
11414 ("sub") ("subset"."&sub;")
11415 ("sup") ("supset"."&sup;")
11416 ("nsub")
11417 ("sube")
11418 ("supe")
11419 ("oplus")
11420 ("otimes")
11421 ("perp")
11422 ("sdot") ("cdot"."&sdot;")
11423 ("lceil")
11424 ("rceil")
11425 ("lfloor")
11426 ("rfloor")
11427 ("lang")
11428 ("rang")
11429 ("loz") ("Diamond"."&loz;")
11430 ("spades") ("spadesuit"."&spades;")
11431 ("clubs") ("clubsuit"."&clubs;")
11432 ("hearts") ("diamondsuit"."&hearts;")
11433 ("diams") ("diamondsuit"."&diams;")
11434 ("quot")
11435 ("amp")
11436 ("lt")
11437 ("gt")
11438 ("OElig")
11439 ("oelig")
11440 ("Scaron")
11441 ("scaron")
11442 ("Yuml")
11443 ("circ")
11444 ("tilde")
11445 ("ensp")
11446 ("emsp")
11447 ("thinsp")
11448 ("zwnj")
11449 ("zwj")
11450 ("lrm")
11451 ("rlm")
11452 ("ndash")
11453 ("mdash")
11454 ("lsquo")
11455 ("rsquo")
11456 ("sbquo")
11457 ("ldquo")
11458 ("rdquo")
11459 ("bdquo")
11460 ("dagger")
11461 ("Dagger")
11462 ("permil")
11463 ("lsaquo")
11464 ("rsaquo")
11465 ("euro")
11467 ("arccos"."arccos")
11468 ("arcsin"."arcsin")
11469 ("arctan"."arctan")
11470 ("arg"."arg")
11471 ("cos"."cos")
11472 ("cosh"."cosh")
11473 ("cot"."cot")
11474 ("coth"."coth")
11475 ("csc"."csc")
11476 ("deg"."deg")
11477 ("det"."det")
11478 ("dim"."dim")
11479 ("exp"."exp")
11480 ("gcd"."gcd")
11481 ("hom"."hom")
11482 ("inf"."inf")
11483 ("ker"."ker")
11484 ("lg"."lg")
11485 ("lim"."lim")
11486 ("liminf"."liminf")
11487 ("limsup"."limsup")
11488 ("ln"."ln")
11489 ("log"."log")
11490 ("max"."max")
11491 ("min"."min")
11492 ("Pr"."Pr")
11493 ("sec"."sec")
11494 ("sin"."sin")
11495 ("sinh"."sinh")
11496 ("sup"."sup")
11497 ("tan"."tan")
11498 ("tanh"."tanh")
11500 "Entities for TeX->HTML translation.
11501 Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
11502 \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
11503 In that case, \"\\ent\" will be translated to \"&other;\".
11504 The list contains HTML entities for Latin-1, Greek and other symbols.
11505 It is supplemented by a number of commonly used TeX macros with appropriate
11506 translations. There is currently no way for users to extend this.")
11508 (defun org-cleaned-string-for-export (string)
11509 "Cleanup a buffer substring so that links can be created safely."
11510 (interactive)
11511 (let* ((cb (current-buffer))
11512 (re-radio (and org-target-link-regexp
11513 (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
11514 (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
11515 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
11516 rtn)
11517 (save-excursion
11518 (set-buffer (get-buffer-create " org-mode-tmp"))
11519 (erase-buffer)
11520 (insert string)
11521 (org-mode)
11522 ;; Find targets in comments and move them out of comments,
11523 ;; but mark them as targets that should be invisible
11524 (goto-char (point-min))
11525 (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
11526 (replace-match "\\1(INVISIBLE)"))
11527 ;; Find matches for radio targets and turn them into internal links
11528 (goto-char (point-min))
11529 (when re-radio
11530 (while (re-search-forward re-radio nil t)
11531 (replace-match "\\1[[\\2]]")))
11532 ;; Find all links that contain a newline and put them into a single line
11533 (goto-char (point-min))
11534 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
11535 (replace-match "\\1 \\3")
11536 (goto-char (match-beginning 0)))
11537 ;; Normalize links: Convert angle and plain links into bracket links
11538 (goto-char (point-min))
11539 (while (re-search-forward re-plain-link nil t)
11540 (replace-match
11541 (concat
11542 (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
11543 t t))
11544 (goto-char (point-min))
11545 (while (re-search-forward re-angle-link nil t)
11546 (replace-match
11547 (concat
11548 (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
11549 t t))
11551 ;; Remove comments
11552 (goto-char (point-min))
11553 (while (re-search-forward "^#.*\n?" nil t)
11554 (replace-match ""))
11555 (setq rtn (buffer-string)))
11556 (kill-buffer " org-mode-tmp")
11557 rtn))
11559 (defun org-solidify-link-text (s &optional alist)
11560 "Take link text and make a safe target out of it."
11561 (save-match-data
11562 (let* ((rtn
11563 (mapconcat
11564 'identity
11565 (org-split-string s "[ \t\r\n]+") "--"))
11566 (a (assoc rtn alist)))
11567 (or (cdr a) rtn))))
11569 (defun org-convert-to-odd-levels ()
11570 "Convert an org-mode file with all levels allowed to one with odd levels.
11571 This will leave level 1 alone, convert level 2 to level 3, level 3 to
11572 level 5 etc."
11573 (interactive)
11574 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
11575 (let ((org-odd-levels-only nil) n)
11576 (save-excursion
11577 (goto-char (point-min))
11578 (while (re-search-forward "^\\*\\*+" nil t)
11579 (setq n (1- (length (match-string 0))))
11580 (while (>= (setq n (1- n)) 0)
11581 (org-demote))
11582 (end-of-line 1))))))
11585 (defun org-convert-to-oddeven-levels ()
11586 "Convert an org-mode file with only odd levels to one with odd and even levels.
11587 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
11588 section with an even level, conversion would destroy the structure of the file. An error
11589 is signaled in this case."
11590 (interactive)
11591 (goto-char (point-min))
11592 ;; First check if there are no even levels
11593 (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
11594 (org-show-hierarchy-above)
11595 (error "Not all levels are odd in this file. Conversion not possible."))
11596 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
11597 (let ((org-odd-levels-only nil) n)
11598 (save-excursion
11599 (goto-char (point-min))
11600 (while (re-search-forward "^\\*\\*+" nil t)
11601 (setq n (/ (length (match-string 0)) 2))
11602 (while (>= (setq n (1- n)) 0)
11603 (org-promote))
11604 (end-of-line 1))))))
11606 (defun org-tr-level (n)
11607 "Make N odd if required."
11608 (if org-odd-levels-only (1+ (/ n 2)) n))
11610 (defvar org-last-level nil) ; dynamically scoped variable
11612 (defun org-export-as-ascii (arg)
11613 "Export the outline as a pretty ASCII file.
11614 If there is an active region, export only the region.
11615 The prefix ARG specifies how many levels of the outline should become
11616 underlined headlines. The default is 3."
11617 (interactive "P")
11618 (setq-default org-todo-line-regexp org-todo-line-regexp)
11619 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
11620 (org-infile-export-plist)))
11621 (region
11622 (buffer-substring
11623 (if (org-region-active-p) (region-beginning) (point-min))
11624 (if (org-region-active-p) (region-end) (point-max))))
11625 (lines (org-export-find-first-heading-line
11626 (org-skip-comments
11627 (org-split-string
11628 (org-cleaned-string-for-export region)
11629 "[\r\n]"))))
11630 (org-startup-with-deadline-check nil)
11631 (level 0) line txt
11632 (umax nil)
11633 (case-fold-search nil)
11634 (filename (concat (file-name-as-directory
11635 (plist-get opt-plist :publishing-directory))
11636 (file-name-sans-extension
11637 (file-name-nondirectory buffer-file-name))
11638 ".txt"))
11639 (buffer (find-file-noselect filename))
11640 (levels-open (make-vector org-level-max nil))
11641 (odd org-odd-levels-only)
11642 (date (format-time-string "%Y/%m/%d" (current-time)))
11643 (time (format-time-string "%X" (org-current-time)))
11644 (author (plist-get opt-plist :author))
11645 (title (or (plist-get opt-plist :title)
11646 (file-name-sans-extension
11647 (file-name-nondirectory buffer-file-name))))
11648 (options nil)
11649 (email (plist-get opt-plist :email))
11650 (language (plist-get opt-plist :language))
11651 (text nil)
11652 (todo nil)
11653 (lang-words nil))
11655 (setq org-last-level 1)
11656 (org-init-section-numbers)
11658 (find-file-noselect filename)
11660 (setq lang-words (or (assoc language org-export-language-setup)
11661 (assoc "en" org-export-language-setup)))
11662 (if org-export-ascii-show-new-buffer
11663 (switch-to-buffer-other-window buffer)
11664 (set-buffer buffer))
11665 (erase-buffer)
11666 (fundamental-mode)
11667 ;; create local variables for all options, to make sure all called
11668 ;; functions get the correct information
11669 (mapcar (lambda (x)
11670 (set (make-local-variable (cdr x))
11671 (plist-get opt-plist (car x))))
11672 org-export-plist-vars)
11673 (set (make-local-variable 'org-odd-levels-only) odd)
11674 (setq umax (if arg (prefix-numeric-value arg)
11675 org-export-headline-levels))
11677 ;; File header
11678 (if title (org-insert-centered title ?=))
11679 (insert "\n")
11680 (if (or author email)
11681 (insert (concat (nth 1 lang-words) ": " (or author "")
11682 (if email (concat " <" email ">") "")
11683 "\n")))
11684 (if (and date time)
11685 (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
11686 (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
11688 (insert "\n\n")
11690 (if org-export-with-toc
11691 (progn
11692 (insert (nth 3 lang-words) "\n"
11693 (make-string (length (nth 3 lang-words)) ?=) "\n")
11694 (mapcar '(lambda (line)
11695 (if (string-match org-todo-line-regexp
11696 line)
11697 ;; This is a headline
11698 (progn
11699 (setq level (- (match-end 1) (match-beginning 1))
11700 level (org-tr-level level)
11701 txt (match-string 3 line)
11702 todo
11703 (or (and org-export-mark-todo-in-toc
11704 (match-beginning 2)
11705 (not (equal (match-string 2 line)
11706 org-done-string)))
11707 ; TODO, not DONE
11708 (and org-export-mark-todo-in-toc
11709 (= level umax)
11710 (org-search-todo-below
11711 line lines level))))
11712 (setq txt (org-html-expand-for-ascii txt))
11714 (if org-export-with-section-numbers
11715 (setq txt (concat (org-section-number level)
11716 " " txt)))
11717 (if (<= level umax)
11718 (progn
11719 (insert
11720 (make-string (* (1- level) 4) ?\ )
11721 (format (if todo "%s (*)\n" "%s\n") txt))
11722 (setq org-last-level level))
11723 ))))
11724 lines)))
11726 (org-init-section-numbers)
11727 (while (setq line (pop lines))
11728 ;; Remove the quoted HTML tags.
11729 (setq line (org-html-expand-for-ascii line))
11730 ;; Remove targets
11731 (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
11732 (setq line (replace-match "" t t line)))
11733 ;; Replace internal links
11734 (while (string-match org-bracket-link-regexp line)
11735 (setq line (replace-match
11736 (if (match-end 3) "[\\3]" "[\\1]")
11737 t nil line)))
11738 (cond
11739 ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
11740 ;; a Headline
11741 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
11742 txt (match-string 2 line))
11743 (org-ascii-level-start level txt umax))
11744 (t (insert line "\n"))))
11745 (normal-mode)
11746 (save-buffer)
11747 (goto-char (point-min))))
11749 (defun org-search-todo-below (line lines level)
11750 "Search the subtree below LINE for any TODO entries."
11751 (let ((rest (cdr (memq line lines)))
11752 (re org-todo-line-regexp)
11753 line lv todo)
11754 (catch 'exit
11755 (while (setq line (pop rest))
11756 (if (string-match re line)
11757 (progn
11758 (setq lv (- (match-end 1) (match-beginning 1))
11759 todo (and (match-beginning 2)
11760 (not (equal (match-string 2 line)
11761 org-done-string))))
11762 ; TODO, not DONE
11763 (if (<= lv level) (throw 'exit nil))
11764 (if todo (throw 'exit t))))))))
11766 ;; FIXME: Try to handle <b> and <i> as faces via text properties.
11767 ;; We could also implement *bold*,/italic/ and _underline_ for ASCII export
11768 (defun org-html-expand-for-ascii (line)
11769 "Handle quoted HTML for ASCII export."
11770 (if org-export-html-expand
11771 (while (string-match "@<[^<>\n]*>" line)
11772 ;; We just remove the tags for now.
11773 (setq line (replace-match "" nil nil line))))
11774 line)
11776 (defun org-insert-centered (s &optional underline)
11777 "Insert the string S centered and underline it with character UNDERLINE."
11778 (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
11779 (insert (make-string ind ?\ ) s "\n")
11780 (if underline
11781 (insert (make-string ind ?\ )
11782 (make-string (string-width s) underline)
11783 "\n"))))
11785 (defun org-ascii-level-start (level title umax)
11786 "Insert a new level in ASCII export."
11787 (let (char)
11788 (if (> level umax)
11789 (insert (make-string (* 2 (- level umax 1)) ?\ ) "* " title "\n")
11790 (if (or (not (equal (char-before) ?\n))
11791 (not (equal (char-before (1- (point))) ?\n)))
11792 (insert "\n"))
11793 (setq char (nth (- umax level) (reverse org-ascii-underline)))
11794 (if org-export-with-section-numbers
11795 (setq title (concat (org-section-number level) " " title)))
11796 (insert title "\n" (make-string (string-width title) char) "\n"))))
11798 (defun org-export-copy-visible ()
11799 "Copy the visible part of the buffer to another buffer, for printing.
11800 Also removes the first line of the buffer if it specifies a mode,
11801 and all options lines."
11802 (interactive)
11803 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
11804 (org-infile-export-plist)))
11805 (filename (concat (file-name-as-directory
11806 (plist-get opt-plist :publishing-directory))
11807 (file-name-sans-extension
11808 (file-name-nondirectory buffer-file-name))
11809 ".txt"))
11810 (buffer (find-file-noselect filename))
11811 (ore (concat
11812 (org-make-options-regexp
11813 '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
11814 "STARTUP" "ARCHIVE"
11815 "TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"))
11816 (if org-noutline-p "\\(\n\\|$\\)" "")))
11817 s e)
11818 (with-current-buffer buffer
11819 (erase-buffer)
11820 (text-mode))
11821 (save-excursion
11822 (setq s (goto-char (point-min)))
11823 (while (not (= (point) (point-max)))
11824 (goto-char (org-find-invisible))
11825 (append-to-buffer buffer s (point))
11826 (setq s (goto-char (org-find-visible)))))
11827 (switch-to-buffer-other-window buffer)
11828 (newline)
11829 (goto-char (point-min))
11830 (if (looking-at ".*-\\*- mode:.*\n")
11831 (replace-match ""))
11832 (while (re-search-forward ore nil t)
11833 (replace-match ""))
11834 (goto-char (point-min))))
11836 (defun org-find-visible ()
11837 (if (featurep 'noutline)
11838 (let ((s (point)))
11839 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
11840 (get-char-property s 'invisible)))
11842 (skip-chars-forward "^\n")
11843 (point)))
11844 (defun org-find-invisible ()
11845 (if (featurep 'noutline)
11846 (let ((s (point)))
11847 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
11848 (not (get-char-property s 'invisible))))
11850 (skip-chars-forward "^\r")
11851 (point)))
11853 ;; HTML
11855 (defun org-get-current-options ()
11856 "Return a string with current options as keyword options.
11857 Does include HTML export options as well as TODO and CATEGORY stuff."
11858 (format
11859 "#+TITLE: %s
11860 #+AUTHOR: %s
11861 #+EMAIL: %s
11862 #+LANGUAGE: %s
11863 #+TEXT: Some descriptive text to be emitted. Several lines OK.
11864 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s
11865 #+CATEGORY: %s
11866 #+SEQ_TODO: %s
11867 #+TYP_TODO: %s
11868 #+STARTUP: %s %s %s %s %s
11869 #+ARCHIVE: %s
11871 (buffer-name) (user-full-name) user-mail-address org-export-default-language
11872 org-export-headline-levels
11873 org-export-with-section-numbers
11874 org-export-with-toc
11875 org-export-preserve-breaks
11876 org-export-html-expand
11877 org-export-with-fixed-width
11878 org-export-with-tables
11879 org-export-with-sub-superscripts
11880 org-export-with-emphasize
11881 org-export-with-TeX-macros
11882 (file-name-nondirectory buffer-file-name)
11883 (if (equal org-todo-interpretation 'sequence)
11884 (mapconcat 'identity org-todo-keywords " ")
11885 "TODO FEEDBACK VERIFY DONE")
11886 (if (equal org-todo-interpretation 'type)
11887 (mapconcat 'identity org-todo-keywords " ")
11888 "Me Jason Marie DONE")
11889 (cdr (assoc org-startup-folded
11890 '((nil . "showall") (t . "overview") (content . "content"))))
11891 (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
11892 (if org-odd-levels-only "odd" "oddeven")
11893 (if org-hide-leading-stars "hidestars" "showstars")
11894 (if org-startup-align-all-tables "align" "noalign")
11895 org-archive-location
11898 (defun org-insert-export-options-template ()
11899 "Insert into the buffer a template with information for exporting."
11900 (interactive)
11901 (if (not (bolp)) (newline))
11902 (let ((s (org-get-current-options)))
11903 (and (string-match "#\\+CATEGORY" s)
11904 (setq s (substring s 0 (match-beginning 0))))
11905 (insert s)))
11907 (defun org-toggle-fixed-width-section (arg)
11908 "Toggle the fixed-width export.
11909 If there is no active region, the QUOTE keyword at the current headline is
11910 inserted or removed. When present, it causes the text between this headline
11911 and the next to be exported as fixed-width text, and unmodified.
11912 If there is an active region, this command adds or removes a colon as the
11913 first character of this line. If the first character of a line is a colon,
11914 this line is also exported in fixed-width font."
11915 (interactive "P")
11916 (let* ((cc 0)
11917 (regionp (org-region-active-p))
11918 (beg (if regionp (region-beginning) (point)))
11919 (end (if regionp (region-end)))
11920 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
11921 (re "[ \t]*\\(:\\)")
11922 off)
11923 (if regionp
11924 (save-excursion
11925 (goto-char beg)
11926 (setq cc (current-column))
11927 (beginning-of-line 1)
11928 (setq off (looking-at re))
11929 (while (> nlines 0)
11930 (setq nlines (1- nlines))
11931 (beginning-of-line 1)
11932 (cond
11933 (arg
11934 (move-to-column cc t)
11935 (insert ":\n")
11936 (forward-line -1))
11937 ((and off (looking-at re))
11938 (replace-match "" t t nil 1))
11939 ((not off) (move-to-column cc t) (insert ":")))
11940 (forward-line 1)))
11941 (save-excursion
11942 (org-back-to-heading)
11943 (if (looking-at (concat outline-regexp
11944 "\\( +\\<" org-quote-string "\\>\\)"))
11945 (replace-match "" t t nil 1)
11946 (if (looking-at outline-regexp)
11947 (progn
11948 (goto-char (match-end 0))
11949 (insert " " org-quote-string))))))))
11951 (defun org-export-as-html-and-open (arg)
11952 "Export the outline as HTML and immediately open it with a browser.
11953 If there is an active region, export only the region.
11954 The prefix ARG specifies how many levels of the outline should become
11955 headlines. The default is 3. Lower levels will become bulleted lists."
11956 (interactive "P")
11957 (org-export-as-html arg 'hidden)
11958 (org-open-file buffer-file-name))
11960 (defun org-export-as-html-batch ()
11961 "Call `org-export-as-html', may be used in batch processing as
11962 emacs --batch
11963 --load=$HOME/lib/emacs/org.el
11964 --eval \"(setq org-export-headline-levels 2)\"
11965 --visit=MyFile --funcall org-export-as-html-batch"
11966 (org-export-as-html org-export-headline-levels 'hidden))
11968 (defun org-export-as-html (arg &optional hidden ext-plist)
11969 "Export the outline as a pretty HTML file.
11970 If there is an active region, export only the region.
11971 The prefix ARG specifies how many levels of the outline should become
11972 headlines. The default is 3. Lower levels will become bulleted lists.
11973 When HIDDEN is non-nil, don't display the HTML buffer.
11974 EXT-PLIST is a property list with external parameters overriding
11975 org-mode's default settings, but still inferior to file-local settings."
11976 (interactive "P")
11977 (setq-default org-todo-line-regexp org-todo-line-regexp)
11978 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
11979 (setq-default org-done-string org-done-string)
11980 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
11981 ext-plist
11982 (org-infile-export-plist)))
11984 (style (plist-get opt-plist :style))
11985 (odd org-odd-levels-only)
11986 (region-p (org-region-active-p))
11987 (region
11988 (buffer-substring
11989 (if region-p (region-beginning) (point-min))
11990 (if region-p (region-end) (point-max))))
11991 (all_lines
11992 (org-skip-comments (org-split-string
11993 (org-cleaned-string-for-export region)
11994 "[\r\n]")))
11995 (lines (org-export-find-first-heading-line all_lines))
11996 (level 0) (line "") (origline "") txt todo
11997 (umax nil)
11998 (filename (concat (file-name-as-directory
11999 (plist-get opt-plist :publishing-directory))
12000 (file-name-sans-extension
12001 (file-name-nondirectory buffer-file-name))
12002 ".html"))
12003 (buffer (find-file-noselect filename))
12004 (levels-open (make-vector org-level-max nil))
12005 (date (format-time-string "%Y/%m/%d" (current-time)))
12006 (time (format-time-string "%X" (org-current-time)))
12007 (author (plist-get opt-plist :author))
12008 (title (or (plist-get opt-plist :title)
12009 (file-name-sans-extension
12010 (file-name-nondirectory buffer-file-name))))
12011 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
12012 (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
12013 (inquote nil)
12014 (infixed nil)
12015 (in-local-list nil)
12016 (local-list-num nil)
12017 (local-list-indent nil)
12018 (llt org-plain-list-ordered-item-terminator)
12019 (email (plist-get opt-plist :email))
12020 (language (plist-get opt-plist :language))
12021 (text (plist-get opt-plist :text))
12022 (lang-words nil)
12023 (target-alist nil) tg
12024 (head-count 0) cnt
12025 (start 0)
12026 ;; FIXME: The following returns always nil under XEmacs
12027 (coding-system (and (fboundp 'coding-system-get)
12028 (boundp 'buffer-file-coding-system)
12029 buffer-file-coding-system))
12030 (coding-system-for-write (or coding-system coding-system-for-write))
12031 (save-buffer-coding-system (or coding-system save-buffer-coding-system))
12032 (charset (and coding-system
12033 (coding-system-get coding-system 'mime-charset)))
12034 table-open type
12035 table-buffer table-orig-buffer
12036 ind start-is-num starter
12037 rpl path desc desc1 desc2 link
12039 (message "Exporting...")
12041 (setq org-last-level 1)
12042 (org-init-section-numbers)
12044 ;; Get the language-dependent settings
12045 (setq lang-words (or (assoc language org-export-language-setup)
12046 (assoc "en" org-export-language-setup)))
12048 ;; Switch to the output buffer
12049 (if (or hidden (not org-export-html-show-new-buffer))
12050 (set-buffer buffer)
12051 (switch-to-buffer-other-window buffer))
12052 (erase-buffer)
12053 (fundamental-mode)
12054 (let ((case-fold-search nil)
12055 (org-odd-levels-only odd))
12056 ;; create local variables for all options, to make sure all called
12057 ;; functions get the correct information
12058 (mapcar (lambda (x)
12059 (set (make-local-variable (cdr x))
12060 (plist-get opt-plist (car x))))
12061 org-export-plist-vars)
12062 (setq umax (if arg (prefix-numeric-value arg)
12063 org-export-headline-levels))
12065 ;; File header
12066 (insert (format
12067 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"
12068 \"http://www.w3.org/TR/REC-html40/loose.dtd\">
12069 <html lang=\"%s\"><head>
12070 <title>%s</title>
12071 <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\">
12072 <meta name=generator content=\"Org-mode\">
12073 <meta name=generated content=\"%s %s\">
12074 <meta name=author content=\"%s\">
12076 </head><body>
12078 language (org-html-expand title) (or charset "iso-8859-1")
12079 date time author style))
12082 (insert (or (plist-get opt-plist :preamble) ""))
12084 (when (plist-get opt-plist :auto-preamble)
12085 (if title (insert (concat "<H1 class=\"title\">"
12086 (org-html-expand title) "</H1>\n")))
12087 ; (if author (insert (concat (nth 1 lang-words) ": " author "\n")))
12088 ; (if email (insert (concat "<a href=\"mailto:" email "\">&lt;"
12089 ; email "&gt;</a>\n")))
12090 ; (if (or author email) (insert "<br>\n"))
12091 ; (if (and date time) (insert (concat (nth 2 lang-words) ": "
12092 ; date " " time "<br>\n")))
12093 (if text (insert (concat "<p>\n" (org-html-expand text)))))
12095 (if org-export-with-toc
12096 (progn
12097 (insert (format "<H2>%s</H2>\n" (nth 3 lang-words)))
12098 (insert "<ul>\n")
12099 (setq lines
12100 (mapcar '(lambda (line)
12101 (if (string-match org-todo-line-regexp line)
12102 ;; This is a headline
12103 (progn
12104 (setq level (- (match-end 1) (match-beginning 1))
12105 level (org-tr-level level)
12106 txt (save-match-data
12107 (org-html-expand
12108 (match-string 3 line)))
12109 todo
12110 (or (and org-export-mark-todo-in-toc
12111 (match-beginning 2)
12112 (not (equal (match-string 2 line)
12113 org-done-string)))
12114 ; TODO, not DONE
12115 (and org-export-mark-todo-in-toc
12116 (= level umax)
12117 (org-search-todo-below
12118 line lines level))))
12119 (if org-export-with-section-numbers
12120 (setq txt (concat (org-section-number level)
12121 " " txt)))
12122 (if (<= level umax)
12123 (progn
12124 (setq head-count (+ head-count 1))
12125 (if (> level org-last-level)
12126 (progn
12127 (setq cnt (- level org-last-level))
12128 (while (>= (setq cnt (1- cnt)) 0)
12129 (insert "<ul>"))
12130 (insert "\n")))
12131 (if (< level org-last-level)
12132 (progn
12133 (setq cnt (- org-last-level level))
12134 (while (>= (setq cnt (1- cnt)) 0)
12135 (insert "</ul>"))
12136 (insert "\n")))
12137 ;; Check for targets
12138 (while (string-match org-target-regexp line)
12139 (setq tg (match-string 1 line)
12140 line (replace-match
12141 (concat "@<span class=\"target\">" tg "@</span> ")
12142 t t line))
12143 (push (cons (org-solidify-link-text tg)
12144 (format "sec-%d" head-count))
12145 target-alist))
12146 (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
12147 (setq txt (replace-match "" t t txt)))
12148 (insert
12149 (format
12150 (if todo
12151 "<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>\n"
12152 "<li><a href=\"#sec-%d\">%s</a>\n")
12153 head-count txt))
12155 (setq org-last-level level))
12157 line)
12158 lines))
12159 (while (> org-last-level 0)
12160 (setq org-last-level (1- org-last-level))
12161 (insert "</ul>\n"))
12163 (setq head-count 0)
12164 (org-init-section-numbers)
12166 (while (setq line (pop lines) origline line)
12167 (catch 'nextline
12169 ;; end of quote section?
12170 (when (and inquote (string-match "^\\*+" line))
12171 (insert "</pre>\n")
12172 (setq inquote nil))
12173 ;; inside a quote section?
12174 (when inquote
12175 (insert (org-html-protect line) "\n")
12176 (throw 'nextline nil))
12178 ;; verbatim lines
12179 (when (and org-export-with-fixed-width
12180 (string-match "^[ \t]*:\\(.*\\)" line))
12181 (when (not infixed)
12182 (setq infixed t)
12183 (insert "<pre>\n"))
12184 (insert (org-html-protect (match-string 1 line)) "\n")
12185 (when (and lines
12186 (not (string-match "^[ \t]*\\(:.*\\)"
12187 (car lines))))
12188 (setq infixed nil)
12189 (insert "</pre>\n"))
12190 (throw 'nextline nil))
12193 ;; make targets to anchors
12194 (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
12195 (cond
12196 ((match-end 2)
12197 (setq line (replace-match
12198 (concat "@<a name=\""
12199 (org-solidify-link-text (match-string 1 line))
12200 "\">\\nbsp@</a>")
12201 t t line)))
12202 ((and org-export-with-toc (equal (string-to-char line) ?*))
12203 (setq line (replace-match
12204 (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
12205 ; (concat "@<i>" (match-string 1 line) "@</i> ")
12206 t t line)))
12208 (setq line (replace-match
12209 (concat "@<a name=\""
12210 (org-solidify-link-text (match-string 1 line))
12211 "\" class=\"target\">" (match-string 1 line) "@</a> ")
12212 t t line)))))
12214 (setq line (org-html-handle-time-stamps line))
12216 ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
12217 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
12218 (setq line (org-html-expand line))
12220 ;; Format the links
12221 (setq start 0)
12222 (while (string-match org-bracket-link-analytic-regexp line start)
12223 (setq start (match-beginning 0))
12224 (setq type (if (match-end 2) (match-string 2 line) "internal"))
12225 (setq path (match-string 3 line))
12226 (setq desc1 (if (match-end 5) (match-string 5 line))
12227 desc2 (if (match-end 2) (concat type ":" path) path)
12228 desc (or desc1 desc2))
12229 (cond
12230 ((equal type "internal")
12231 (setq rpl
12232 (concat
12233 "<a href=\"#"
12234 (org-solidify-link-text path target-alist)
12235 "\">" desc "</a>")))
12236 ((member type '("http" "https" "ftp" "mailto" "news"))
12237 ;; standard URL
12238 (setq link (concat type ":" path))
12239 (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
12240 ((string= type "file")
12241 ;; FILE link
12242 (let* ((filename path)
12243 (abs-p (file-name-absolute-p filename))
12244 thefile file-is-image-p search)
12245 (save-match-data
12246 (if (string-match "::\\(.*\\)" filename)
12247 (setq search (match-string 1 filename)
12248 filename (replace-match "" nil nil filename)))
12249 (setq file-is-image-p
12250 (string-match (org-image-file-name-regexp) filename))
12251 (setq thefile (if abs-p (expand-file-name filename) filename))
12252 (when (and org-export-html-link-org-files-as-html
12253 (string-match "\\.org$" thefile))
12254 (setq thefile (concat (substring thefile 0
12255 (match-beginning 0))
12256 ".html"))
12257 (if (and search
12258 ;; make sure this is can be used as target search
12259 (not (string-match "^[0-9]*$" search))
12260 (not (string-match "^\\*" search))
12261 (not (string-match "^/.*/$" search)))
12262 (setq thefile (concat thefile "#"
12263 (org-solidify-link-text
12264 (org-link-unescape search)))))
12265 (when (string-match "^file:" desc)
12266 (setq desc (replace-match "" t t desc))
12267 (if (string-match "\\.org$" desc)
12268 (setq desc (replace-match "" t t desc))))))
12269 (setq rpl (if (and org-export-html-inline-images
12270 file-is-image-p)
12271 (concat "<img src=\"" thefile "\"/>")
12272 (concat "<a href=\"" thefile "\">" desc "</a>")))))
12273 ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell"))
12274 (setq rpl (concat "<i>&lt;" type ":"
12275 (save-match-data (org-link-unescape path))
12276 "&gt;</i>"))))
12277 (setq line (replace-match rpl t t line)
12278 start (+ start (length rpl))))
12279 ;; TODO items
12280 (if (and (string-match org-todo-line-regexp line)
12281 (match-beginning 2))
12282 (if (equal (match-string 2 line) org-done-string)
12283 (setq line (replace-match
12284 "<span class=\"done\">\\2</span>"
12285 nil nil line 2))
12286 (setq line (replace-match "<span class=\"todo\">\\2</span>"
12287 nil nil line 2))))
12289 ;; DEADLINES
12290 (if (string-match org-deadline-line-regexp line)
12291 (progn
12292 (if (save-match-data
12293 (string-match "<a href"
12294 (substring line 0 (match-beginning 0))))
12295 nil ; Don't do the replacement - it is inside a link
12296 (setq line (replace-match "<span class=\"deadline\">\\&</span>"
12297 nil nil line 1)))))
12298 (cond
12299 ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
12300 ;; This is a headline
12301 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
12302 txt (match-string 2 line))
12303 (if (string-match quote-re0 txt)
12304 (setq txt (replace-match "" t t txt)))
12305 (if (<= level umax) (setq head-count (+ head-count 1)))
12306 (when in-local-list
12307 ;; Close any local lists before inserting a new header line
12308 (while local-list-num
12309 (insert (if (car local-list-num) "</ol>\n" "</ul>"))
12310 (pop local-list-num))
12311 (setq local-list-indent nil
12312 in-local-list nil))
12313 (org-html-level-start level txt umax
12314 (and org-export-with-toc (<= level umax))
12315 head-count)
12316 ;; QUOTES
12317 (when (string-match quote-re line)
12318 (insert "<pre>")
12319 (setq inquote t)))
12321 ((and org-export-with-tables
12322 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
12323 (if (not table-open)
12324 ;; New table starts
12325 (setq table-open t table-buffer nil table-orig-buffer nil))
12326 ;; Accumulate lines
12327 (setq table-buffer (cons line table-buffer)
12328 table-orig-buffer (cons origline table-orig-buffer))
12329 (when (or (not lines)
12330 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
12331 (car lines))))
12332 (setq table-open nil
12333 table-buffer (nreverse table-buffer)
12334 table-orig-buffer (nreverse table-orig-buffer))
12335 (insert (org-format-table-html table-buffer table-orig-buffer))))
12337 ;; Normal lines
12338 (when (and (string-match
12339 (cond
12340 ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+[.)]\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
12341 ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+\\.\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
12342 ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+)\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
12343 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
12344 line))
12345 (setq ind (org-get-string-indentation line)
12346 start-is-num (match-beginning 4)
12347 starter (if (match-beginning 2) (match-string 2 line))
12348 line (substring line (match-beginning 5)))
12349 (unless (string-match "[^ \t]" line)
12350 ;; empty line. Pretend indentation is large.
12351 (setq ind (1+ (or (car local-list-indent) 1))))
12352 (while (and in-local-list
12353 (or (and (= ind (car local-list-indent))
12354 (not starter))
12355 (< ind (car local-list-indent))))
12356 (insert (if (car local-list-num) "</ol>\n" "</ul>"))
12357 (pop local-list-num) (pop local-list-indent)
12358 (setq in-local-list local-list-indent))
12359 (cond
12360 ((and starter
12361 (or (not in-local-list)
12362 (> ind (car local-list-indent))))
12363 ;; Start new (level of ) list
12364 (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
12365 (push start-is-num local-list-num)
12366 (push ind local-list-indent)
12367 (setq in-local-list t))
12368 (starter
12369 ;; continue current list
12370 (insert "<li>\n"))))
12371 ;; Empty lines start a new paragraph. If hand-formatted lists
12372 ;; are not fully interpreted, lines starting with "-", "+", "*"
12373 ;; also start a new paragraph.
12374 (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (insert "<p>"))
12375 (insert line (if org-export-preserve-breaks "<br>\n" "\n"))))
12378 ;; Properly close all local lists and other lists
12379 (when inquote (insert "</pre>\n"))
12380 (when in-local-list
12381 ;; Close any local lists before inserting a new header line
12382 (while local-list-num
12383 (insert (if (car local-list-num) "</ol>\n" "</ul>"))
12384 (pop local-list-num))
12385 (setq local-list-indent nil
12386 in-local-list nil))
12387 (org-html-level-start 1 nil umax
12388 (and org-export-with-toc (<= level umax))
12389 head-count)
12391 (when (plist-get opt-plist :auto-postamble)
12392 (if author (insert (concat (nth 1 lang-words) ": " author "\n")))
12393 (if email (insert (concat "<a href=\"mailto:" email "\">&lt;"
12394 email "&gt;</a>\n")))
12395 (if (or author email) (insert "<br>\n"))
12396 (if (and date time) (insert (concat (nth 2 lang-words) ": "
12397 date " " time "<br>\n"))))
12399 (if org-export-html-with-timestamp
12400 (insert org-export-html-html-helper-timestamp))
12401 (insert (or (plist-get opt-plist :postamble) ""))
12402 (insert "</body>\n</html>\n")
12403 (normal-mode)
12404 (save-buffer)
12405 (goto-char (point-min)))))
12407 (defun org-format-table-html (lines olines)
12408 "Find out which HTML converter to use and return the HTML code."
12409 (if (string-match "^[ \t]*|" (car lines))
12410 ;; A normal org table
12411 (org-format-org-table-html lines)
12412 ;; Table made by table.el - test for spanning
12413 (let* ((hlines (delq nil (mapcar
12414 (lambda (x)
12415 (if (string-match "^[ \t]*\\+-" x) x
12416 nil))
12417 lines)))
12418 (first (car hlines))
12419 (ll (and (string-match "\\S-+" first)
12420 (match-string 0 first)))
12421 (re (concat "^[ \t]*" (regexp-quote ll)))
12422 (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
12423 hlines))))
12424 (if (and (not spanning)
12425 (not org-export-prefer-native-exporter-for-tables))
12426 ;; We can use my own converter with HTML conversions
12427 (org-format-table-table-html lines)
12428 ;; Need to use the code generator in table.el, with the original text.
12429 (org-format-table-table-html-using-table-generate-source olines)))))
12431 (defun org-format-org-table-html (lines)
12432 "Format a table into HTML."
12433 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
12434 (setq lines (nreverse lines))
12435 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
12436 (setq lines (nreverse lines))
12437 (when org-export-table-remove-special-lines
12438 ;; Check if the table has a marking column. If yes remove the
12439 ;; column and the special lines
12440 (let* ((special
12441 (not
12442 (memq nil
12443 (mapcar
12444 (lambda (x)
12445 (or (string-match "^[ \t]*|-" x)
12446 (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
12447 lines)))))
12448 (if special
12449 (setq lines
12450 (delq nil
12451 (mapcar
12452 (lambda (x)
12453 (if (string-match "^[ \t]*| *[!_^] *|" x)
12454 nil ; ignore this line
12455 (and (or (string-match "^[ \t]*|-+\\+" x)
12456 (string-match "^[ \t]*|[^|]*|" x))
12457 (replace-match "|" t t x))))
12458 lines))))))
12460 (let ((head (and org-export-highlight-first-table-line
12461 (delq nil (mapcar
12462 (lambda (x) (string-match "^[ \t]*|-" x))
12463 (cdr lines)))))
12464 line fields html)
12465 (setq html (concat org-export-html-table-tag "\n"))
12466 (while (setq line (pop lines))
12467 (catch 'next-line
12468 (if (string-match "^[ \t]*|-" line)
12469 (progn
12470 (setq head nil) ;; head ends here, first time around
12471 ;; ignore this line
12472 (throw 'next-line t)))
12473 ;; Break the line into fields
12474 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
12475 (setq html (concat
12476 html
12477 "<tr>"
12478 (mapconcat (lambda (x)
12479 (if head
12480 (concat "<th>" x "</th>")
12481 (concat "<td>" x "</td>")))
12482 fields "")
12483 "</tr>\n"))))
12484 (setq html (concat html "</table>\n"))
12485 html))
12487 (defun org-fake-empty-table-line (line)
12488 "Replace everything except \"|\" with spaces."
12489 (let ((i (length line))
12490 (newstr (copy-sequence line)))
12491 (while (> i 0)
12492 (setq i (1- i))
12493 (if (not (eq (aref newstr i) ?|))
12494 (aset newstr i ?\ )))
12495 newstr))
12497 (defun org-format-table-table-html (lines)
12498 "Format a table generated by table.el into HTML.
12499 This conversion does *not* use `table-generate-source' from table.el.
12500 This has the advantage that Org-mode's HTML conversions can be used.
12501 But it has the disadvantage, that no cell- or row-spanning is allowed."
12502 (let (line field-buffer
12503 (head org-export-highlight-first-table-line)
12504 fields html empty)
12505 (setq html (concat org-export-html-table-tag "\n"))
12506 (while (setq line (pop lines))
12507 (setq empty "&nbsp")
12508 (catch 'next-line
12509 (if (string-match "^[ \t]*\\+-" line)
12510 (progn
12511 (if field-buffer
12512 (progn
12513 (setq html (concat
12514 html
12515 "<tr>"
12516 (mapconcat
12517 (lambda (x)
12518 (if (equal x "") (setq x empty))
12519 (if head
12520 (concat "<th>" x "</th>\n")
12521 (concat "<td>" x "</td>\n")))
12522 field-buffer "\n")
12523 "</tr>\n"))
12524 (setq head nil)
12525 (setq field-buffer nil)))
12526 ;; Ignore this line
12527 (throw 'next-line t)))
12528 ;; Break the line into fields and store the fields
12529 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
12530 (if field-buffer
12531 (setq field-buffer (mapcar
12532 (lambda (x)
12533 (concat x "<br>" (pop fields)))
12534 field-buffer))
12535 (setq field-buffer fields))))
12536 (setq html (concat html "</table>\n"))
12537 html))
12539 (defun org-format-table-table-html-using-table-generate-source (lines)
12540 "Format a table into html, using `table-generate-source' from table.el.
12541 This has the advantage that cell- or row-spanning is allowed.
12542 But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
12543 (require 'table)
12544 (with-current-buffer (get-buffer-create " org-tmp1 ")
12545 (erase-buffer)
12546 (insert (mapconcat 'identity lines "\n"))
12547 (goto-char (point-min))
12548 (if (not (re-search-forward "|[^+]" nil t))
12549 (error "Error processing table"))
12550 (table-recognize-table)
12551 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
12552 (table-generate-source 'html " org-tmp2 ")
12553 (set-buffer " org-tmp2 ")
12554 (buffer-substring (point-min) (point-max))))
12556 (defun org-html-handle-time-stamps (s)
12557 "FIXME: Format time stamps, or remove them"
12560 (defun org-html-protect (s)
12561 ;; convert & to &amp;, < to &lt; and > to &gt;
12562 (let ((start 0))
12563 (while (string-match "&" s start)
12564 (setq s (replace-match "&amp;" t t s)
12565 start (1+ (match-beginning 0))))
12566 (while (string-match "<" s)
12567 (setq s (replace-match "&lt;" t t s)))
12568 (while (string-match ">" s)
12569 (setq s (replace-match "&gt;" t t s))))
12572 (defun org-html-expand (string)
12573 "Prepare STRING for HTML export. Applies all active conversions.
12574 If there are links in the string, don't modify these."
12575 (let* (m s l res)
12576 (while (setq m (string-match org-bracket-link-regexp string))
12577 (setq s (substring string 0 m)
12578 l (match-string 0 string)
12579 string (substring string (match-end 0)))
12580 (push (org-html-do-expand s) res)
12581 (push l res))
12582 (push (org-html-do-expand string) res)
12583 (apply 'concat (nreverse res))))
12585 (defun org-html-do-expand (s)
12586 "Apply all active conversions to translate special ASCII to HTML."
12587 (setq s (org-html-protect s))
12588 (if org-export-html-expand
12589 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
12590 (setq s (replace-match "<\\1>" nil nil s))))
12591 (if org-export-with-emphasize
12592 (setq s (org-export-html-convert-emphasize s)))
12593 (if org-export-with-sub-superscripts
12594 (setq s (org-export-html-convert-sub-super s)))
12595 (if org-export-with-TeX-macros
12596 (let ((start 0) wd ass)
12597 (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
12598 (setq wd (match-string 1 s))
12599 (if (setq ass (assoc wd org-html-entities))
12600 (setq s (replace-match (or (cdr ass)
12601 (concat "&" (car ass) ";"))
12602 t t s))
12603 (setq start (+ start (length wd)))))))
12606 (defun org-create-multibrace-regexp (left right n)
12607 "Create a regular expression which will match a balanced sexp.
12608 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
12609 as single character strings.
12610 The regexp returned will match the entire expression including the
12611 delimiters. It will also define a single group which contains the
12612 match except for the outermost delimiters. The maximum depth of
12613 stacked delimiters is N. Escaping delimiters is not possible."
12614 (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
12615 (or "\\|")
12616 (re nothing)
12617 (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
12618 (while (> n 1)
12619 (setq n (1- n)
12620 re (concat re or next)
12621 next (concat "\\(?:" nothing left next right "\\)+" nothing)))
12622 (concat left "\\(" re "\\)" right)))
12624 (defvar org-match-substring-regexp
12625 (concat
12626 "\\([^\\]\\)\\([_^]\\)\\("
12627 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
12628 "\\|"
12629 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
12630 "\\|"
12631 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
12632 "The regular expression matching a sub- or superscript.")
12634 (defun org-export-html-convert-sub-super (string)
12635 "Convert sub- and superscripts in STRING to HTML."
12636 (let (key c)
12637 (while (string-match org-match-substring-regexp string)
12638 (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
12639 (setq c (or (match-string 8 string)
12640 (match-string 6 string)
12641 (match-string 5 string)))
12642 (setq string (replace-match
12643 (concat (match-string 1 string)
12644 "<" key ">" c "</" key ">")
12645 t t string)))
12646 (while (string-match "\\\\\\([_^]\\)" string)
12647 (setq string (replace-match (match-string 1 string) t t string))))
12648 string)
12650 (defun org-export-html-convert-emphasize (string)
12651 (while (string-match org-italic-re string)
12652 (setq string (replace-match "\\1<i>\\3</i>\\4" t nil string)))
12653 (while (string-match org-bold-re string)
12654 (setq string (replace-match "\\1<b>\\3</b>\\4" t nil string)))
12655 (while (string-match org-underline-re string)
12656 (setq string (replace-match "\\1<u>\\3</u>\\4" t nil string)))
12657 string)
12659 (defun org-html-level-start (level title umax with-toc head-count)
12660 "Insert a new level in HTML export.
12661 When TITLE is nil, just close all open levels."
12662 (let ((l (1+ (max level umax))))
12663 (while (<= l org-level-max)
12664 (if (aref levels-open (1- l))
12665 (progn
12666 (org-html-level-close l)
12667 (aset levels-open (1- l) nil)))
12668 (setq l (1+ l)))
12669 (when title
12670 ;; If title is nil, this means this function is called to close
12671 ;; all levels, so the rest is done only if title is given
12672 (if (> level umax)
12673 (progn
12674 (if (aref levels-open (1- level))
12675 (insert "<li>" title "<p>\n")
12676 (aset levels-open (1- level) t)
12677 (insert "<ul><li>" title "<p>\n")))
12678 (if org-export-with-section-numbers
12679 (setq title (concat (org-section-number level) " " title)))
12680 (setq level (+ level 1))
12681 (if with-toc
12682 (insert (format "\n<H%d><a name=\"sec-%d\">%s</a></H%d>\n"
12683 level head-count title level))
12684 (insert (format "\n<H%d>%s</H%d>\n" level title level)))))))
12686 (defun org-html-level-close (&rest args)
12687 "Terminate one level in HTML export."
12688 (insert "</ul>"))
12690 ;; Variable holding the vector with section numbers
12691 (defvar org-section-numbers (make-vector org-level-max 0))
12693 (defun org-init-section-numbers ()
12694 "Initialize the vector for the section numbers."
12695 (let* ((level -1)
12696 (numbers (nreverse (org-split-string "" "\\.")))
12697 (depth (1- (length org-section-numbers)))
12698 (i depth) number-string)
12699 (while (>= i 0)
12700 (if (> i level)
12701 (aset org-section-numbers i 0)
12702 (setq number-string (or (car numbers) "0"))
12703 (if (string-match "\\`[A-Z]\\'" number-string)
12704 (aset org-section-numbers i
12705 (- (string-to-char number-string) ?A -1))
12706 (aset org-section-numbers i (string-to-number number-string)))
12707 (pop numbers))
12708 (setq i (1- i)))))
12710 (defun org-section-number (&optional level)
12711 "Return a string with the current section number.
12712 When LEVEL is non-nil, increase section numbers on that level."
12713 (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
12714 (when level
12715 (when (> level -1)
12716 (aset org-section-numbers
12717 level (1+ (aref org-section-numbers level))))
12718 (setq idx (1+ level))
12719 (while (<= idx depth)
12720 (if (not (= idx 1))
12721 (aset org-section-numbers idx 0))
12722 (setq idx (1+ idx))))
12723 (setq idx 0)
12724 (while (<= idx depth)
12725 (setq n (aref org-section-numbers idx))
12726 (setq string (concat string (if (not (string= string "")) "." "")
12727 (int-to-string n)))
12728 (setq idx (1+ idx)))
12729 (save-match-data
12730 (if (string-match "\\`\\([@0]\\.\\)+" string)
12731 (setq string (replace-match "" nil nil string)))
12732 (if (string-match "\\(\\.0\\)+\\'" string)
12733 (setq string (replace-match "" nil nil string))))
12734 string))
12737 (defun org-export-icalendar-this-file ()
12738 "Export current file as an iCalendar file.
12739 The iCalendar file will be located in the same directory as the Org-mode
12740 file, but with extension `.ics'."
12741 (interactive)
12742 (org-export-icalendar nil buffer-file-name))
12744 (defun org-export-as-xml ()
12745 "Export current buffer as XOXO XML buffer."
12746 (interactive)
12747 (cond ((eq org-export-xml-type 'xoxo)
12748 (org-export-as-xoxo (current-buffer)))))
12750 (defun org-export-as-xoxo-insert-into (buffer &rest output)
12751 (with-current-buffer buffer
12752 (apply 'insert output)))
12754 (defun org-export-as-xoxo (&optional buffer)
12755 "Export the org buffer as XOXO.
12756 The XOXO buffer is named *xoxo-<source buffer name>*"
12757 (interactive (list (current-buffer)))
12758 ;; A quickie abstraction
12760 ;; Output everything as XOXO
12761 (with-current-buffer (get-buffer buffer)
12762 (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
12763 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
12764 (org-infile-export-plist)))
12765 (filename (concat (file-name-as-directory
12766 (plist-get opt-plist :publishing-directory))
12767 (file-name-sans-extension
12768 (file-name-nondirectory buffer-file-name))
12769 ".xml"))
12770 (out (find-file-noselect filename))
12771 (last-level 1)
12772 (hanging-li nil))
12773 ;; Check the output buffer is empty.
12774 (with-current-buffer out (erase-buffer))
12775 ;; Kick off the output
12776 (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
12777 (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
12778 (let* ((hd (match-string-no-properties 1))
12779 (level (length hd))
12780 (text (concat
12781 (match-string-no-properties 2)
12782 (save-excursion
12783 (goto-char (match-end 0))
12784 (let ((str ""))
12785 (catch 'loop
12786 (while 't
12787 (forward-line)
12788 (if (looking-at "^[ \t]\\(.*\\)")
12789 (setq str (concat str (match-string-no-properties 1)))
12790 (throw 'loop str)))))))))
12792 ;; Handle level rendering
12793 (cond
12794 ((> level last-level)
12795 (org-export-as-xoxo-insert-into out "\n<ol>\n"))
12797 ((< level last-level)
12798 (dotimes (- (- last-level level) 1)
12799 (if hanging-li
12800 (org-export-as-xoxo-insert-into out "</li>\n"))
12801 (org-export-as-xoxo-insert-into out "</ol>\n"))
12802 (when hanging-li
12803 (org-export-as-xoxo-insert-into out "</li>\n")
12804 (setq hanging-li nil)))
12806 ((equal level last-level)
12807 (if hanging-li
12808 (org-export-as-xoxo-insert-into out "</li>\n")))
12811 (setq last-level level)
12813 ;; And output the new li
12814 (setq hanging-li 't)
12815 (if (equal ?+ (elt text 0))
12816 (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
12817 (org-export-as-xoxo-insert-into out "<li>" text))))
12819 ;; Finally finish off the ol
12820 (dotimes (- last-level 1)
12821 (if hanging-li
12822 (org-export-as-xoxo-insert-into out "</li>\n"))
12823 (org-export-as-xoxo-insert-into out "</ol>\n"))
12825 ;; Finish the buffer off and clean it up.
12826 (switch-to-buffer-other-window out)
12827 (indent-region (point-min) (point-max) nil)
12828 (save-buffer)
12829 (goto-char (point-min))
12832 ;;;###autoload
12833 (defun org-export-icalendar-all-agenda-files ()
12834 "Export all files in `org-agenda-files' to iCalendar .ics files.
12835 Each iCalendar file will be located in the same directory as the Org-mode
12836 file, but with extension `.ics'."
12837 (interactive)
12838 (apply 'org-export-icalendar nil (org-agenda-files t)))
12840 ;;;###autoload
12841 (defun org-export-icalendar-combine-agenda-files ()
12842 "Export all files in `org-agenda-files' to a single combined iCalendar file.
12843 The file is stored under the name `org-combined-agenda-icalendar-file'."
12844 (interactive)
12845 (apply 'org-export-icalendar t (org-agenda-files t)))
12847 (defun org-export-icalendar (combine &rest files)
12848 "Create iCalendar files for all elements of FILES.
12849 If COMBINE is non-nil, combine all calendar entries into a single large
12850 file and store it under the name `org-combined-agenda-icalendar-file'."
12851 (save-excursion
12852 (let* (file ical-file ical-buffer category started org-agenda-new-buffers)
12853 (when combine
12854 (setq ical-file org-combined-agenda-icalendar-file
12855 ical-buffer (org-get-agenda-file-buffer ical-file))
12856 (set-buffer ical-buffer) (erase-buffer))
12857 (while (setq file (pop files))
12858 (catch 'nextfile
12859 (org-check-agenda-file file)
12860 (unless combine
12861 (setq ical-file (concat (file-name-sans-extension file) ".ics"))
12862 (setq ical-buffer (org-get-agenda-file-buffer ical-file))
12863 (set-buffer ical-buffer) (erase-buffer))
12864 (set-buffer (org-get-agenda-file-buffer file))
12865 (setq category (or org-category
12866 (file-name-sans-extension
12867 (file-name-nondirectory buffer-file-name))))
12868 (if (symbolp category) (setq category (symbol-name category)))
12869 (let ((standard-output ical-buffer))
12870 (if combine
12871 (and (not started) (setq started t)
12872 (org-start-icalendar-file org-icalendar-combined-name))
12873 (org-start-icalendar-file category))
12874 (org-print-icalendar-entries combine category)
12875 (when (or (and combine (not files)) (not combine))
12876 (org-finish-icalendar-file)
12877 (set-buffer ical-buffer)
12878 (save-buffer)
12879 (run-hooks 'org-after-save-iCalendar-file-hook)))))
12880 (org-release-buffers org-agenda-new-buffers))))
12882 (defvar org-after-save-iCalendar-file-hook nil
12883 "Hook run after an iCalendar file has been saved.
12884 The iCalendar buffer is still current when this hook is run.
12885 A good way to use this is to tell a desktop calenndar application to re-read
12886 the iCalendar file.")
12888 (defun org-print-icalendar-entries (&optional combine category)
12889 "Print iCalendar entries for the current Org-mode file to `standard-output'.
12890 When COMBINE is non nil, add the category to each line."
12891 (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
12892 (dts (org-ical-ts-to-string
12893 (format-time-string (cdr org-time-stamp-formats) (current-time))
12894 "DTSTART"))
12895 hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri)
12896 (save-excursion
12897 (goto-char (point-min))
12898 (while (re-search-forward org-ts-regexp nil t)
12899 (setq pos (match-beginning 0)
12900 ts (match-string 0)
12901 inc t
12902 hd (org-get-heading))
12903 (if (looking-at re2)
12904 (progn
12905 (goto-char (match-end 0))
12906 (setq ts2 (match-string 1) inc nil))
12907 (setq ts2 ts
12908 tmp (buffer-substring (max (point-min)
12909 (- pos org-ds-keyword-length))
12910 pos)
12911 deadlinep (string-match org-deadline-regexp tmp)
12912 scheduledp (string-match org-scheduled-regexp tmp)
12913 ;; donep (org-entry-is-done-p)
12915 (if (or (string-match org-tr-regexp hd)
12916 (string-match org-ts-regexp hd))
12917 (setq hd (replace-match "" t t hd)))
12918 (if combine
12919 (setq hd (concat hd " (category " category ")")))
12920 (if deadlinep (setq hd (concat "DL: " hd " This is a deadline")))
12921 (if scheduledp (setq hd (concat "S: " hd " Scheduled for this date")))
12922 (princ (format "BEGIN:VEVENT
12925 SUMMARY:%s
12926 END:VEVENT\n"
12927 (org-ical-ts-to-string ts "DTSTART")
12928 (org-ical-ts-to-string ts2 "DTEND" inc)
12929 hd)))
12930 (when org-icalendar-include-todo
12931 (goto-char (point-min))
12932 (while (re-search-forward org-todo-line-regexp nil t)
12933 (setq state (match-string 1))
12934 (unless (equal state org-done-string)
12935 (setq hd (match-string 3))
12936 (if (string-match org-priority-regexp hd)
12937 (setq pri (string-to-char (match-string 2 hd))
12938 hd (concat (substring hd 0 (match-beginning 1))
12939 (substring hd (- (match-end 1)))))
12940 (setq pri org-default-priority))
12941 (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
12942 (- org-lowest-priority ?A))))))
12944 (princ (format "BEGIN:VTODO
12946 SUMMARY:%s
12947 SEQUENCE:1
12948 PRIORITY:%d
12949 END:VTODO\n"
12950 dts hd pri))))))))
12952 (defun org-start-icalendar-file (name)
12953 "Start an iCalendar file by inserting the header."
12954 (let ((user user-full-name)
12955 (name (or name "unknown"))
12956 (timezone (cadr (current-time-zone))))
12957 (princ
12958 (format "BEGIN:VCALENDAR
12959 VERSION:2.0
12960 X-WR-CALNAME:%s
12961 PRODID:-//%s//Emacs with Org-mode//EN
12962 X-WR-TIMEZONE:%s
12963 CALSCALE:GREGORIAN\n" name user timezone))))
12965 (defun org-finish-icalendar-file ()
12966 "Finish an iCalendar file by inserting the END statement."
12967 (princ "END:VCALENDAR\n"))
12969 (defun org-ical-ts-to-string (s keyword &optional inc)
12970 "Take a time string S and convert it to iCalendar format.
12971 KEYWORD is added in front, to make a complete line like DTSTART....
12972 When INC is non-nil, increase the hour by two (if time string contains
12973 a time), or the day by one (if it does not contain a time)."
12974 (let ((t1 (org-parse-time-string s 'nodefault))
12975 t2 fmt have-time time)
12976 (if (and (car t1) (nth 1 t1) (nth 2 t1))
12977 (setq t2 t1 have-time t)
12978 (setq t2 (org-parse-time-string s)))
12979 (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
12980 (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
12981 (when inc
12982 (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
12983 (setq time (encode-time s mi h d m y)))
12984 (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
12985 (concat keyword (format-time-string fmt time))))
12988 ;;; Key bindings
12990 ;; - Bindings in Org-mode map are currently
12991 ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
12992 ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
12993 ;; e (?) useful from outline-mode
12994 ;; i k @ expendable from outline-mode
12995 ;; 0123456789 % & ()_{} " ` free
12997 ;; Make `C-c C-x' a prefix key
12998 (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
13000 ;; TAB key with modifiers
13001 (define-key org-mode-map "\C-i" 'org-cycle)
13002 (define-key org-mode-map [(tab)] 'org-cycle)
13003 (define-key org-mode-map [(meta tab)] 'org-complete)
13004 (define-key org-mode-map "\M-\C-i" 'org-complete) ; for tty emacs
13005 ;; The following line is necessary under Suse GNU/Linux
13006 (unless (featurep 'xemacs)
13007 (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
13008 (define-key org-mode-map [(shift tab)] 'org-shifttab)
13010 (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
13011 (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down) ; tty
13012 (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
13013 (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading) ; tty
13014 (define-key org-mode-map [(meta return)] 'org-meta-return)
13015 (define-key org-mode-map "\C-c\C-xm" 'org-meta-return) ; tty emacs
13016 (define-key org-mode-map [?\e (return)] 'org-meta-return) ; tty emacs
13018 ;; Cursor keys with modifiers
13019 (define-key org-mode-map [(meta left)] 'org-metaleft)
13020 (define-key org-mode-map [?\e (left)] 'org-metaleft) ; for tty emacs
13021 (define-key org-mode-map "\C-c\C-xl" 'org-metaleft) ; for tty emacs
13022 (define-key org-mode-map [(meta right)] 'org-metaright)
13023 (define-key org-mode-map [?\e (right)] 'org-metaright) ; for tty emacs
13024 (define-key org-mode-map "\C-c\C-xr" 'org-metaright) ; for tty emacs
13025 (define-key org-mode-map [(meta up)] 'org-metaup)
13026 (define-key org-mode-map [?\e (up)] 'org-metaup) ; for tty emacs
13027 (define-key org-mode-map "\C-c\C-xu" 'org-metaup) ; for tty emacs
13028 (define-key org-mode-map [(meta down)] 'org-metadown)
13029 (define-key org-mode-map [?\e (down)] 'org-metadown) ; for tty emacs
13030 (define-key org-mode-map "\C-c\C-xd" 'org-metadown) ; for tty emacs
13032 (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
13033 (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft) ; tty
13034 (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
13035 (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright) ; tty
13036 (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
13037 (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup) ; tty
13038 (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
13039 (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown) ; tty
13040 (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
13041 (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
13042 (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
13043 (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
13044 (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
13045 (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
13046 (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
13047 (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright)
13049 ;; All the other keys
13051 (define-key org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
13052 (define-key org-mode-map "\C-c$" 'org-archive-subtree)
13053 (define-key org-mode-map "\C-c\C-j" 'org-goto)
13054 (define-key org-mode-map "\C-c\C-t" 'org-todo)
13055 (define-key org-mode-map "\C-c\C-s" 'org-schedule)
13056 (define-key org-mode-map "\C-c\C-d" 'org-deadline)
13057 (define-key org-mode-map "\C-c;" 'org-toggle-comment)
13058 (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
13059 (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
13060 (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
13061 (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
13062 (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
13063 (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
13064 (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
13065 (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
13066 (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
13067 (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
13068 (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
13069 (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
13070 (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
13071 (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
13072 (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
13073 (define-key org-mode-map "\C-c>" 'org-goto-calendar)
13074 (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
13075 (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
13076 (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
13077 (define-key org-mode-map "\C-c]" 'org-remove-file)
13078 (define-key org-mode-map "\C-c\C-r" 'org-timeline)
13079 (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
13080 (define-key org-mode-map "\C-c^" 'org-table-sort-lines)
13081 (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
13082 (define-key org-mode-map "\C-m" 'org-return)
13083 (define-key org-mode-map "\C-c?" 'org-table-current-column)
13084 (define-key org-mode-map "\C-c " 'org-table-blank-field)
13085 (define-key org-mode-map "\C-c+" 'org-table-sum)
13086 (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
13087 (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
13088 (define-key org-mode-map "\C-c`" 'org-table-edit-field)
13089 (define-key org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
13090 (define-key org-mode-map "\C-c*" 'org-table-recalculate)
13091 (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
13092 (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
13093 (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
13094 (define-key org-mode-map "\C-c\C-xa" 'org-export-as-ascii)
13095 (define-key org-mode-map "\C-c\C-x\C-a" 'org-export-as-ascii)
13096 (define-key org-mode-map "\C-c\C-xv" 'org-export-copy-visible)
13097 (define-key org-mode-map "\C-c\C-x\C-v" 'org-export-copy-visible)
13098 ;; OPML support is only an option for the future
13099 ;(define-key org-mode-map "\C-c\C-xo" 'org-export-as-opml)
13100 ;(define-key org-mode-map "\C-c\C-x\C-o" 'org-export-as-opml)
13101 (define-key org-mode-map "\C-c\C-xi" 'org-export-icalendar-this-file)
13102 (define-key org-mode-map "\C-c\C-x\C-i" 'org-export-icalendar-all-agenda-files)
13103 (define-key org-mode-map "\C-c\C-xc" 'org-export-icalendar-combine-agenda-files)
13104 (define-key org-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
13105 (define-key org-mode-map "\C-c\C-xt" 'org-insert-export-options-template)
13106 (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
13107 (define-key org-mode-map "\C-c\C-xh" 'org-export-as-html)
13108 (define-key org-mode-map "\C-c\C-xx" 'org-export-as-xml)
13109 (define-key org-mode-map "\C-c\C-x\C-x" 'org-export-as-xml)
13110 (define-key org-mode-map "\C-c\C-xb" 'org-export-as-html-and-open)
13111 (define-key org-mode-map "\C-c\C-x\C-b" 'org-export-as-html-and-open)
13113 (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
13114 (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
13115 (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
13116 (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
13118 (defsubst org-table-p () (org-at-table-p))
13120 (defun org-self-insert-command (N)
13121 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
13122 If the cursor is in a table looking at whitespace, the whitespace is
13123 overwritten, and the table is not marked as requiring realignment."
13124 (interactive "p")
13125 (if (and (org-table-p)
13126 (progn
13127 ;; check if we blank the field, and if that triggers align
13128 (and org-table-auto-blank-field
13129 (member last-command
13130 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
13131 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
13132 ;; got extra space, this field does not determine column width
13133 (let (org-table-may-need-update) (org-table-blank-field))
13134 ;; no extra space, this field may determine column width
13135 (org-table-blank-field)))
13137 (eq N 1)
13138 (looking-at "[^|\n]* |"))
13139 (let (org-table-may-need-update)
13140 (goto-char (1- (match-end 0)))
13141 (delete-backward-char 1)
13142 (goto-char (match-beginning 0))
13143 (self-insert-command N))
13144 (setq org-table-may-need-update t)
13145 (self-insert-command N)))
13147 (defun org-delete-backward-char (N)
13148 "Like `delete-backward-char', insert whitespace at field end in tables.
13149 When deleting backwards, in tables this function will insert whitespace in
13150 front of the next \"|\" separator, to keep the table aligned. The table will
13151 still be marked for re-alignment if the field did fill the entire column,
13152 because, in this case the deletion might narrow the column."
13153 (interactive "p")
13154 (if (and (org-table-p)
13155 (eq N 1)
13156 (string-match "|" (buffer-substring (point-at-bol) (point)))
13157 (looking-at ".*?|"))
13158 (let ((pos (point))
13159 (noalign (looking-at "[^|\n\r]* |"))
13160 (c org-table-may-need-update))
13161 (backward-delete-char N)
13162 (skip-chars-forward "^|")
13163 (insert " ")
13164 (goto-char (1- pos))
13165 ;; noalign: if there were two spaces at the end, this field
13166 ;; does not determine the width of the column.
13167 (if noalign (setq org-table-may-need-update c)))
13168 (backward-delete-char N)))
13170 (defun org-delete-char (N)
13171 "Like `delete-char', but insert whitespace at field end in tables.
13172 When deleting characters, in tables this function will insert whitespace in
13173 front of the next \"|\" separator, to keep the table aligned. The table will
13174 still be marked for re-alignment if the field did fill the entire column,
13175 because, in this case the deletion might narrow the column."
13176 (interactive "p")
13177 (if (and (org-table-p)
13178 (not (bolp))
13179 (not (= (char-after) ?|))
13180 (eq N 1))
13181 (if (looking-at ".*?|")
13182 (let ((pos (point))
13183 (noalign (looking-at "[^|\n\r]* |"))
13184 (c org-table-may-need-update))
13185 (replace-match (concat
13186 (substring (match-string 0) 1 -1)
13187 " |"))
13188 (goto-char pos)
13189 ;; noalign: if there were two spaces at the end, this field
13190 ;; does not determine the width of the column.
13191 (if noalign (setq org-table-may-need-update c)))
13192 (delete-char N))
13193 (delete-char N)))
13195 ;; How to do this: Measure non-white length of current string
13196 ;; If equal to column width, we should realign.
13198 (defun org-remap (map &rest commands)
13199 "In MAP, remap the functions given in COMMANDS.
13200 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
13201 (let (new old)
13202 (while commands
13203 (setq old (pop commands) new (pop commands))
13204 (if (fboundp 'command-remapping)
13205 (define-key map (vector 'remap old) new)
13206 (substitute-key-definition old new map global-map)))))
13208 (when (eq org-enable-table-editor 'optimized)
13209 ;; If the user wants maximum table support, we need to hijack
13210 ;; some standard editing functions
13211 (org-remap org-mode-map
13212 'self-insert-command 'org-self-insert-command
13213 'delete-char 'org-delete-char
13214 'delete-backward-char 'org-delete-backward-char)
13215 (define-key org-mode-map "|" 'org-force-self-insert))
13217 (defun org-shiftcursor-error ()
13218 "Throw an error because Shift-Cursor command was applied in wrong context."
13219 (error "This command is active in special context like tables, headlines or timestamps"))
13221 (defun org-shifttab ()
13222 "Global visibility cycling or move to previous table field.
13223 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
13224 on context.
13225 See the individual commands for more information."
13226 (interactive)
13227 (cond
13228 ((org-at-table-p) (org-table-previous-field))
13229 (t (org-cycle '(4)))))
13231 (defun org-shiftmetaleft ()
13232 "Promote subtree or delete table column.
13233 Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
13234 See the individual commands for more information."
13235 (interactive)
13236 (cond
13237 ((org-at-table-p) (org-table-delete-column))
13238 ((org-on-heading-p) (org-promote-subtree))
13239 ((org-at-item-p) (call-interactively 'org-outdent-item))
13240 (t (org-shiftcursor-error))))
13242 (defun org-shiftmetaright ()
13243 "Demote subtree or insert table column.
13244 Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
13245 See the individual commands for more information."
13246 (interactive)
13247 (cond
13248 ((org-at-table-p) (org-table-insert-column))
13249 ((org-on-heading-p) (org-demote-subtree))
13250 ((org-at-item-p) (call-interactively 'org-indent-item))
13251 (t (org-shiftcursor-error))))
13253 (defun org-shiftmetaup (&optional arg)
13254 "Move subtree up or kill table row.
13255 Calls `org-move-subtree-up' or `org-table-kill-row' or
13256 `org-move-item-up' depending on context. See the individual commands
13257 for more information."
13258 (interactive "P")
13259 (cond
13260 ((org-at-table-p) (org-table-kill-row))
13261 ((org-on-heading-p) (org-move-subtree-up arg))
13262 ((org-at-item-p) (org-move-item-up arg))
13263 (t (org-shiftcursor-error))))
13264 (defun org-shiftmetadown (&optional arg)
13265 "Move subtree down or insert table row.
13266 Calls `org-move-subtree-down' or `org-table-insert-row' or
13267 `org-move-item-down', depending on context. See the individual
13268 commands for more information."
13269 (interactive "P")
13270 (cond
13271 ((org-at-table-p) (org-table-insert-row arg))
13272 ((org-on-heading-p) (org-move-subtree-down arg))
13273 ((org-at-item-p) (org-move-item-down arg))
13274 (t (org-shiftcursor-error))))
13276 (defun org-metaleft (&optional arg)
13277 "Promote heading or move table column to left.
13278 Calls `org-do-promote' or `org-table-move-column', depending on context.
13279 With no specific context, calls the Emacs default `backward-word'.
13280 See the individual commands for more information."
13281 (interactive "P")
13282 (cond
13283 ((org-at-table-p) (org-table-move-column 'left))
13284 ((or (org-on-heading-p) (org-region-active-p)) (org-do-promote))
13285 (t (backward-word (prefix-numeric-value arg)))))
13287 (defun org-metaright (&optional arg)
13288 "Demote subtree or move table column to right.
13289 Calls `org-do-demote' or `org-table-move-column', depending on context.
13290 With no specific context, calls the Emacs default `forward-word'.
13291 See the individual commands for more information."
13292 (interactive "P")
13293 (cond
13294 ((org-at-table-p) (org-table-move-column nil))
13295 ((or (org-on-heading-p) (org-region-active-p)) (org-do-demote))
13296 (t (forward-word (prefix-numeric-value arg)))))
13298 (defun org-metaup (&optional arg)
13299 "Move subtree up or move table row up.
13300 Calls `org-move-subtree-up' or `org-table-move-row' or
13301 `org-move-item-up', depending on context. See the individual commands
13302 for more information."
13303 (interactive "P")
13304 (cond
13305 ((org-at-table-p) (org-table-move-row 'up))
13306 ((org-on-heading-p) (org-move-subtree-up arg))
13307 ((org-at-item-p) (org-move-item-up arg))
13308 (t (org-shiftcursor-error))))
13310 (defun org-metadown (&optional arg)
13311 "Move subtree down or move table row down.
13312 Calls `org-move-subtree-down' or `org-table-move-row' or
13313 `org-move-item-down', depending on context. See the individual
13314 commands for more information."
13315 (interactive "P")
13316 (cond
13317 ((org-at-table-p) (org-table-move-row nil))
13318 ((org-on-heading-p) (org-move-subtree-down arg))
13319 ((org-at-item-p) (org-move-item-down arg))
13320 (t (org-shiftcursor-error))))
13322 (defun org-shiftup (&optional arg)
13323 "Increase item in timestamp or increase priority of current item.
13324 Calls `org-timestamp-up' or `org-priority-up', depending on context.
13325 See the individual commands for more information."
13326 (interactive "P")
13327 (cond
13328 ((org-at-timestamp-p) (org-timestamp-up arg))
13329 (t (org-priority-up))))
13331 (defun org-shiftdown (&optional arg)
13332 "Decrease item in timestamp or decrease priority of current item.
13333 Calls `org-timestamp-down' or `org-priority-down', depending on context.
13334 See the individual commands for more information."
13335 (interactive "P")
13336 (cond
13337 ((org-at-timestamp-p) (org-timestamp-down arg))
13338 (t (org-priority-down))))
13340 (defun org-shiftright ()
13341 "Next TODO keyword or timestamp one day later, depending on context."
13342 (interactive)
13343 (cond
13344 ((org-at-timestamp-p) (org-timestamp-up-day))
13345 ((org-on-heading-p) (org-todo 'right))
13346 (t (org-shiftcursor-error))))
13348 (defun org-shiftleft ()
13349 "Previous TODO keyword or timestamp one day earlier, depending on context."
13350 (interactive)
13351 (cond
13352 ((org-at-timestamp-p) (org-timestamp-down-day))
13353 ((org-on-heading-p) (org-todo 'left))
13354 (t (org-shiftcursor-error))))
13356 (defun org-copy-special ()
13357 "Copy region in table or copy current subtree.
13358 Calls `org-table-copy' or `org-copy-subtree', depending on context.
13359 See the individual commands for more information."
13360 (interactive)
13361 (call-interactively
13362 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
13364 (defun org-cut-special ()
13365 "Cut region in table or cut current subtree.
13366 Calls `org-table-copy' or `org-cut-subtree', depending on context.
13367 See the individual commands for more information."
13368 (interactive)
13369 (call-interactively
13370 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
13372 (defun org-paste-special (arg)
13373 "Paste rectangular region into table, or past subtree relative to level.
13374 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
13375 See the individual commands for more information."
13376 (interactive "P")
13377 (if (org-at-table-p)
13378 (org-table-paste-rectangle)
13379 (org-paste-subtree arg)))
13381 (defun org-ctrl-c-ctrl-c (&optional arg)
13382 "Set tags in headline, or update according to changed information at point.
13384 This command does many different things, depending on context:
13386 - If the cursor is in a headline, prompt for tags and insert them
13387 into the current line, aligned to `org-tags-column'. When called
13388 with prefix arg, realign all tags in the current buffer.
13390 - If the cursor is in one of the special #+KEYWORD lines, this
13391 triggers scanning the buffer for these lines and updating the
13392 information.
13394 - If the cursor is inside a table, realign the table. This command
13395 works even if the automatic table editor has been turned off.
13397 - If the cursor is on a #+TBLFM line, re-apply the formulas to
13398 the entire table.
13400 - If the cursor is inside a table created by the table.el package,
13401 activate that table.
13403 - If the current buffer is a remember buffer, close note and file it.
13404 with a prefix argument, file it without further interaction to the default
13405 location.
13407 - If the cursor is on a <<<target>>>, update radio targets and corresponding
13408 links in this buffer.
13410 - If the cursor is on a numbered item in a plain list, renumber the
13411 ordered list."
13412 (interactive "P")
13413 (let ((org-enable-table-editor t))
13414 (cond
13415 ((and (local-variable-p 'org-finish-function (current-buffer))
13416 (fboundp org-finish-function))
13417 (funcall org-finish-function))
13418 ((org-on-target-p) (org-update-radio-target-regexp))
13419 ((org-on-heading-p) (org-set-tags arg))
13420 ((org-at-table.el-p)
13421 (require 'table)
13422 (beginning-of-line 1)
13423 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
13424 (table-recognize-table))
13425 ((org-at-table-p)
13426 (org-table-maybe-eval-formula)
13427 (if arg
13428 (org-table-recalculate t)
13429 (org-table-maybe-recalculate-line))
13430 (org-table-align))
13431 ((org-at-item-p)
13432 (org-renumber-ordered-list (prefix-numeric-value arg)))
13433 ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
13434 (cond
13435 ((equal (match-string 1) "TBLFM")
13436 ;; Recalculate the table before this line
13437 (save-excursion
13438 (beginning-of-line 1)
13439 (skip-chars-backward " \r\n\t")
13440 (if (org-at-table-p) (org-table-recalculate t))))
13442 (org-mode-restart))))
13443 (t (error "C-c C-c can do nothing useful at this location.")))))
13445 (defun org-mode-restart ()
13446 "Restart Org-mode, to scan again for special lines.
13447 Also updates the keyword regular expressions."
13448 (interactive)
13449 (let ((org-inhibit-startup t)) (org-mode))
13450 (message "Org-mode restarted to refresh keyword and special line setup"))
13452 (defun org-return ()
13453 "Goto next table row or insert a newline.
13454 Calls `org-table-next-row' or `newline', depending on context.
13455 See the individual commands for more information."
13456 (interactive)
13457 (cond
13458 ((org-at-table-p)
13459 (org-table-justify-field-maybe)
13460 (org-table-next-row))
13461 (t (newline))))
13463 (defun org-meta-return (&optional arg)
13464 "Insert a new heading or wrap a region in a table.
13465 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
13466 See the individual commands for more information."
13467 (interactive "P")
13468 (cond
13469 ((org-at-table-p)
13470 (org-table-wrap-region arg))
13471 (t (org-insert-heading arg))))
13473 ;;; Menu entries
13475 ;; Define the Org-mode menus
13476 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
13477 '("Tbl"
13478 ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
13479 ["Next Field" org-cycle (org-at-table-p)]
13480 ["Previous Field" org-shifttab (org-at-table-p)]
13481 ["Next Row" org-return (org-at-table-p)]
13482 "--"
13483 ["Blank Field" org-table-blank-field (org-at-table-p)]
13484 ["Edit Field" org-table-edit-field (org-at-table-p)]
13485 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
13486 "--"
13487 ("Column"
13488 ["Move Column Left" org-metaleft (org-at-table-p)]
13489 ["Move Column Right" org-metaright (org-at-table-p)]
13490 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
13491 ["Insert Column" org-shiftmetaright (org-at-table-p)]
13492 "--"
13493 ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :active (org-at-table-p) :selected org-table-limit-column-width :style toggle])
13494 ("Row"
13495 ["Move Row Up" org-metaup (org-at-table-p)]
13496 ["Move Row Down" org-metadown (org-at-table-p)]
13497 ["Delete Row" org-shiftmetaup (org-at-table-p)]
13498 ["Insert Row" org-shiftmetadown (org-at-table-p)]
13499 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
13500 "--"
13501 ["Insert Hline" org-table-insert-hline (org-at-table-p)])
13502 ("Rectangle"
13503 ["Copy Rectangle" org-copy-special (org-at-table-p)]
13504 ["Cut Rectangle" org-cut-special (org-at-table-p)]
13505 ["Paste Rectangle" org-paste-special (org-at-table-p)]
13506 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
13507 "--"
13508 ("Calculate"
13509 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
13510 ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
13511 ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
13512 "--"
13513 ["Recalculate line" org-table-recalculate (org-at-table-p)]
13514 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
13515 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
13516 "--"
13517 ["Sum Column/Rectangle" org-table-sum
13518 (or (org-at-table-p) (org-region-active-p))]
13519 ["Which Column?" org-table-current-column (org-at-table-p)])
13520 ["Debug Formulas"
13521 (setq org-table-formula-debug (not org-table-formula-debug))
13522 :style toggle :selected org-table-formula-debug]
13523 "--"
13524 ["Create" org-table-create (and (not (org-at-table-p))
13525 org-enable-table-editor)]
13526 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
13527 ["Import from File" org-table-import (not (org-at-table-p))]
13528 ["Export to File" org-table-export (org-at-table-p)]
13529 "--"
13530 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
13532 (easy-menu-define org-org-menu org-mode-map "Org menu"
13533 '("Org"
13534 ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
13535 ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
13536 ["Sparse Tree" org-occur t]
13537 ["Show All" show-all t]
13538 "--"
13539 ["New Heading" org-insert-heading t]
13540 ("Navigate Headings"
13541 ["Up" outline-up-heading t]
13542 ["Next" outline-next-visible-heading t]
13543 ["Previous" outline-previous-visible-heading t]
13544 ["Next Same Level" outline-forward-same-level t]
13545 ["Previous Same Level" outline-backward-same-level t]
13546 "--"
13547 ["Jump" org-goto t])
13548 ("Edit Structure"
13549 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
13550 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
13551 "--"
13552 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
13553 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
13554 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
13555 "--"
13556 ["Promote Heading" org-metaleft (not (org-at-table-p))]
13557 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
13558 ["Demote Heading" org-metaright (not (org-at-table-p))]
13559 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
13560 "--"
13561 ["Archive Subtree" org-archive-subtree t]
13562 "--"
13563 ["Convert to odd levels" org-convert-to-odd-levels t]
13564 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
13565 "--"
13566 ("TODO Lists"
13567 ["TODO/DONE/-" org-todo t]
13568 ["Show TODO Tree" org-show-todo-tree t]
13569 ["Global TODO list" org-todo-list t]
13570 "--"
13571 ["Set Priority" org-priority t]
13572 ["Priority Up" org-shiftup t]
13573 ["Priority Down" org-shiftdown t])
13574 ("Dates and Scheduling"
13575 ["Timestamp" org-time-stamp t]
13576 ["Timestamp (inactive)" org-time-stamp-inactive t]
13577 ("Change Date"
13578 ["1 Day Later" org-timestamp-up-day t]
13579 ["1 Day Earlier" org-timestamp-down-day t]
13580 ["1 ... Later" org-shiftup t]
13581 ["1 ... Earlier" org-shiftdown t])
13582 ["Compute Time Range" org-evaluate-time-range t]
13583 ["Schedule Item" org-schedule t]
13584 ["Deadline" org-deadline t]
13585 "--"
13586 ["Goto Calendar" org-goto-calendar t]
13587 ["Date from Calendar" org-date-from-calendar t])
13588 "--"
13589 ["Agenda Command" org-agenda t]
13590 ("File List for Agenda")
13591 ("Special views current file"
13592 ["TODO Tree" org-show-todo-tree t]
13593 ["Check Deadlines" org-check-deadlines t]
13594 ["Timeline" org-timeline t]
13595 ["Tags Tree" org-tags-sparse-tree t])
13596 "--"
13597 ("Hyperlinks"
13598 ["Store Link (Global)" org-store-link t]
13599 ["Insert Link" org-insert-link t]
13600 ["Follow Link" org-open-at-point t]
13601 "--"
13602 ["Descriptive Links"
13603 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
13604 :style radio :selected (member '(org-link) buffer-invisibility-spec)]
13605 ["Literal Links"
13606 (progn
13607 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
13608 :style radio :selected (not (member '(org-link) buffer-invisibility-spec))]
13609 "--"
13610 ["Upgrade all <link> to [[link][desc]]" org-upgrade-old-links
13611 (save-excursion (goto-char (point-min))
13612 (re-search-forward "<[a-z]+:" nil t))])
13613 "--"
13614 ("Export"
13615 ["ASCII" org-export-as-ascii t]
13616 ["Extract Visible Text" org-export-copy-visible t]
13617 ["HTML" org-export-as-html t]
13618 ["HTML and Open" org-export-as-html-and-open t]
13619 ["XML (XOXO)" org-export-as-xml t]
13620 "--"
13621 ["iCalendar this file" org-export-icalendar-this-file t]
13622 ["iCalendar all agenda files" org-export-icalendar-all-agenda-files
13623 :active t :keys "C-c C-x C-i"]
13624 ["iCalendar combined" org-export-icalendar-combine-agenda-files t]
13625 "--"
13626 ["Option Template" org-insert-export-options-template t]
13627 ["Toggle Fixed Width" org-toggle-fixed-width-section t])
13628 ("Publish"
13629 ["Current File" org-publish-current-file t]
13630 ["Current Project" org-publish-current-project t]
13631 ["Project..." org-publish t]
13632 ["All Projects" org-publish-all t])
13633 "--"
13634 ("Documentation"
13635 ["Show Version" org-version t]
13636 ["Info Documentation" org-info t])
13637 ("Customize"
13638 ["Browse Org Group" org-customize t]
13639 "--"
13640 ["Expand This Menu" org-create-customize-menu
13641 (fboundp 'customize-menu-create)])
13642 "--"
13643 ["Refresh setup" org-mode-restart t]
13646 (defun org-info (&optional node)
13647 "Read documentation for Org-mode in the info system.
13648 With optional NODE, go directly to that node."
13649 (interactive)
13650 (require 'info)
13651 (Info-goto-node (format "(org)%s" (or node ""))))
13653 (defun org-install-agenda-files-menu ()
13654 (let ((bl (buffer-list)))
13655 (save-excursion
13656 (while bl
13657 (set-buffer (pop bl))
13658 (if (eq major-mode 'org-mode) (setq bl nil)))
13659 (when (eq major-mode 'org-mode)
13660 (easy-menu-change
13661 '("Org") "File List for Agenda"
13662 (append
13663 (list
13664 ["Edit File List" (org-edit-agenda-file-list) t]
13665 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
13666 ["Remove Current File from List" org-remove-file t]
13667 ["Cycle through agenda files" org-cycle-agenda-files t]
13668 "--")
13669 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
13671 ;;; Documentation
13673 (defun org-customize ()
13674 "Call the customize function with org as argument."
13675 (interactive)
13676 (customize-browse 'org))
13678 (defun org-create-customize-menu ()
13679 "Create a full customization menu for Org-mode, insert it into the menu."
13680 (interactive)
13681 (if (fboundp 'customize-menu-create)
13682 (progn
13683 (easy-menu-change
13684 '("Org") "Customize"
13685 `(["Browse Org group" org-customize t]
13686 "--"
13687 ,(customize-menu-create 'org)
13688 ["Set" Custom-set t]
13689 ["Save" Custom-save t]
13690 ["Reset to Current" Custom-reset-current t]
13691 ["Reset to Saved" Custom-reset-saved t]
13692 ["Reset to Standard Settings" Custom-reset-standard t]))
13693 (message "\"Org\"-menu now contains full customization menu"))
13694 (error "Cannot expand menu (outdated version of cus-edit.el)")))
13696 ;;; Miscellaneous stuff
13698 (defun org-move-line-down (arg)
13699 "Move the current line down. With prefix argument, move it past ARG lines."
13700 (interactive "p")
13701 (let ((col (current-column))
13702 beg end pos)
13703 (beginning-of-line 1) (setq beg (point))
13704 (beginning-of-line 2) (setq end (point))
13705 (beginning-of-line (+ 1 arg))
13706 (setq pos (move-marker (make-marker) (point)))
13707 (insert (delete-and-extract-region beg end))
13708 (goto-char pos)
13709 (move-to-column col)))
13711 (defun org-move-line-up (arg)
13712 "Move the current line up. With prefix argument, move it past ARG lines."
13713 (interactive "p")
13714 (let ((col (current-column))
13715 beg end pos)
13716 (beginning-of-line 1) (setq beg (point))
13717 (beginning-of-line 2) (setq end (point))
13718 (beginning-of-line (- arg))
13719 (setq pos (move-marker (make-marker) (point)))
13720 (insert (delete-and-extract-region beg end))
13721 (goto-char pos)
13722 (move-to-column col)))
13724 ;; Paragraph filling stuff.
13725 ;; We want this to be just right, so use the full arsenal.
13726 ;; FIXME: This very likely does not work correctly for XEmacs, because the
13727 ;; filladapt package works slightly differently.
13729 (defun org-set-autofill-regexps ()
13730 (interactive)
13731 ;; In the paragraph separator we include headlines, because filling
13732 ;; text in a line directly attached to a headline would otherwise
13733 ;; fill the headline as well.
13734 (set (make-local-variable 'paragraph-separate) "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
13735 ;; The paragraph starter includes hand-formatted lists.
13736 (set (make-local-variable 'paragraph-start)
13737 "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*]\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
13738 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
13739 ;; But only if the user has not turned off tables or fixed-width regions
13740 (set (make-local-variable 'auto-fill-inhibit-regexp)
13741 (concat "\\*\\|#"
13742 (if (or org-enable-table-editor org-enable-fixed-width-editor)
13743 (concat
13744 "\\|[ \t]*["
13745 (if org-enable-table-editor "|" "")
13746 (if org-enable-fixed-width-editor ":" "")
13747 "]"))))
13748 ;; We use our own fill-paragraph function, to make sure that tables
13749 ;; and fixed-width regions are not wrapped. That function will pass
13750 ;; through to `fill-paragraph' when appropriate.
13751 (set (make-local-variable 'fill-paragraph-function) 'org-fill-paragraph)
13752 ;; Adaptive filling: To get full control, first make sure that
13753 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
13754 (set (make-local-variable 'adaptive-fill-regexp) "\000")
13755 (set (make-local-variable 'adaptive-fill-function)
13756 'org-adaptive-fill-function))
13758 (defun org-fill-paragraph (&optional justify)
13759 "Re-align a table, pass through to fill-paragraph if no table."
13760 (let ((table-p (org-at-table-p))
13761 (table.el-p (org-at-table.el-p)))
13762 (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
13763 (table.el-p t) ; skip table.el tables
13764 (table-p (org-table-align) t) ; align org-mode tables
13765 (t nil)))) ; call paragraph-fill
13767 ;; For reference, this is the default value of adaptive-fill-regexp
13768 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
13770 (defun org-adaptive-fill-function ()
13771 "Return a fill prefix for org-mode files.
13772 In particular, this makes sure hanging paragraphs for hand-formatted lists
13773 work correctly."
13774 (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
13775 (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
13777 ;; Functions needed for Emacs/XEmacs region compatibility
13779 (defun org-add-hook (hook function &optional append local)
13780 "Add-hook, compatible with both Emacsen."
13781 (if (and local (featurep 'xemacs))
13782 (add-local-hook hook function append)
13783 (add-hook hook function append local)))
13785 (defun org-region-active-p ()
13786 "Is `transient-mark-mode' on and the region active?
13787 Works on both Emacs and XEmacs."
13788 (if org-ignore-region
13790 (if (featurep 'xemacs)
13791 (and zmacs-regions (region-active-p))
13792 (and transient-mark-mode mark-active))))
13794 (defun org-add-to-invisibility-spec (arg)
13795 "Add elements to `buffer-invisibility-spec'.
13796 See documentation for `buffer-invisibility-spec' for the kind of elements
13797 that can be added."
13798 (cond
13799 ((fboundp 'add-to-invisibility-spec)
13800 (add-to-invisibility-spec arg))
13801 ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
13802 (setq buffer-invisibility-spec (list arg)))
13804 (setq buffer-invisibility-spec
13805 (cons arg buffer-invisibility-spec)))))
13807 (defun org-remove-from-invisibility-spec (arg)
13808 "Remove elements from `buffer-invisibility-spec'."
13809 (if (fboundp 'remove-from-invisibility-spec)
13810 (remove-from-invisibility-spec arg)
13811 (if (consp buffer-invisibility-spec)
13812 (setq buffer-invisibility-spec
13813 (delete arg buffer-invisibility-spec)))))
13815 (defun org-in-invisibility-spec-p (arg)
13816 "Is ARG a member of `buffer-invisibility-spec'?"
13817 (if (consp buffer-invisibility-spec)
13818 (member arg buffer-invisibility-spec)
13819 nil))
13821 (defun org-image-file-name-regexp ()
13822 "Return regexp matching the file names of images."
13823 (if (fboundp 'image-file-name-regexp)
13824 (image-file-name-regexp)
13825 (let ((image-file-name-extensions
13826 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
13827 "xbm" "xpm" "pbm" "pgm" "ppm")))
13828 (concat "\\."
13829 (regexp-opt (nconc (mapcar 'upcase
13830 image-file-name-extensions)
13831 image-file-name-extensions)
13833 "\\'"))))
13835 ;; Functions needed for compatibility with old outline.el.
13837 ;; Programming for the old outline.el (that uses selective display
13838 ;; instead of `invisible' text properties) is a nightmare, mostly
13839 ;; because regular expressions can no longer be anchored at
13840 ;; beginning/end of line. Therefore a number of function need special
13841 ;; treatment when the old outline.el is being used.
13843 ;; The following functions capture almost the entire compatibility code
13844 ;; between the different versions of outline-mode. The only other
13845 ;; places where this is important are the font-lock-keywords, and in
13846 ;; `org-export-copy-visible'. Search for `org-noutline-p' to find them.
13848 ;; C-a should go to the beginning of a *visible* line, also in the
13849 ;; new outline.el. I guess this should be patched into Emacs?
13850 (defun org-beginning-of-line ()
13851 "Go to the beginning of the current line. If that is invisible, continue
13852 to a visible line beginning. This makes the function of C-a more intuitive."
13853 (interactive)
13854 (beginning-of-line 1)
13855 (if (bobp)
13857 (backward-char 1)
13858 (if (org-invisible-p)
13859 (while (and (not (bobp)) (org-invisible-p))
13860 (backward-char 1)
13861 (beginning-of-line 1))
13862 (forward-char 1))))
13864 (when org-noutline-p
13865 (define-key org-mode-map "\C-a" 'org-beginning-of-line))
13866 ;; FIXME: should I use substitute-key-definition to reach other bindings
13867 ;; of beginning-of-line?
13869 (defun org-invisible-p ()
13870 "Check if point is at a character currently not visible."
13871 (if org-noutline-p
13872 ;; Early versions of noutline don't have `outline-invisible-p'.
13873 (if (fboundp 'outline-invisible-p)
13874 (outline-invisible-p)
13875 (get-char-property (point) 'invisible))
13876 (save-excursion
13877 (skip-chars-backward "^\r\n")
13878 (equal (char-before) ?\r))))
13880 (defun org-invisible-p2 ()
13881 "Check if point is at a character currently not visible."
13882 (save-excursion
13883 (if org-noutline-p
13884 (progn
13885 (if (and (eolp) (not (bobp))) (backward-char 1))
13886 ;; Early versions of noutline don't have `outline-invisible-p'.
13887 (if (fboundp 'outline-invisible-p)
13888 (outline-invisible-p)
13889 (get-char-property (point) 'invisible)))
13890 (skip-chars-backward "^\r\n")
13891 (equal (char-before) ?\r))))
13893 (defun org-back-to-heading (&optional invisible-ok)
13894 "Move to previous heading line, or beg of this line if it's a heading.
13895 Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
13896 (if org-noutline-p
13897 (outline-back-to-heading invisible-ok)
13898 (if (and (memq (char-before) '(?\n ?\r))
13899 (looking-at outline-regexp))
13901 (if (re-search-backward (concat (if invisible-ok "\\([\r\n]\\|^\\)" "^")
13902 outline-regexp)
13903 nil t)
13904 (if invisible-ok
13905 (progn (goto-char (match-end 1))
13906 (looking-at outline-regexp)))
13907 (error "Before first heading")))))
13909 (defun org-on-heading-p (&optional invisible-ok)
13910 "Return t if point is on a (visible) heading line.
13911 If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
13912 (if org-noutline-p
13913 (outline-on-heading-p 'invisible-ok)
13914 (save-excursion
13915 (skip-chars-backward "^\n\r")
13916 (and (looking-at outline-regexp)
13917 (or invisible-ok
13918 (bobp)
13919 (equal (char-before) ?\n))))))
13921 (defun org-on-target-p ()
13922 (let ((pos (point)))
13923 (save-excursion
13924 (skip-chars-forward "<")
13925 (and (re-search-backward "<<" nil t)
13926 (or (looking-at org-radio-target-regexp)
13927 (looking-at org-target-regexp))
13928 (<= (match-beginning 0) pos)
13929 (>= (1+ (match-end 0)) pos)))))
13931 (defun org-up-heading-all (arg)
13932 "Move to the heading line of which the present line is a subheading.
13933 This function considers both visible and invisible heading lines.
13934 With argument, move up ARG levels."
13935 (if org-noutline-p
13936 (if (fboundp 'outline-up-heading-all)
13937 (outline-up-heading-all arg) ; emacs 21 version of outline.el
13938 (outline-up-heading arg t)) ; emacs 22 version of outline.el
13939 (org-back-to-heading t)
13940 (looking-at outline-regexp)
13941 (if (<= (- (match-end 0) (match-beginning 0)) arg)
13942 (error "Cannot move up %d levels" arg)
13943 (re-search-backward
13944 (concat "[\n\r]" (regexp-quote
13945 (make-string (- (match-end 0) (match-beginning 0) arg)
13946 ?*))
13947 "[^*]"))
13948 (forward-char 1))))
13950 (defun org-show-hidden-entry ()
13951 "Show an entry where even the heading is hidden."
13952 (save-excursion
13953 (if (not org-noutline-p)
13954 (progn
13955 (org-back-to-heading t)
13956 (org-flag-heading nil)))
13957 (org-show-entry)))
13959 (defun org-check-occur-regexp (regexp)
13960 "If REGEXP starts with \"^\", modify it to check for \\r as well.
13961 Of course, only for the old outline mode."
13962 (if org-noutline-p
13963 regexp
13964 (if (string-match "^\\^" regexp)
13965 (concat "[\n\r]" (substring regexp 1))
13966 regexp)))
13968 (defun org-flag-heading (flag &optional entry)
13969 "Flag the current heading. FLAG non-nil means make invisible.
13970 When ENTRY is non-nil, show the entire entry."
13971 (save-excursion
13972 (org-back-to-heading t)
13973 (if (not org-noutline-p)
13974 ;; Make the current headline visible
13975 (outline-flag-region (max 1 (1- (point))) (point) (if flag ?\r ?\n)))
13976 ;; Check if we should show the entire entry
13977 (if entry
13978 (progn
13979 (org-show-entry)
13980 (save-excursion ;; FIXME: Is this the fix for points in the -|
13981 ;; middle of text? |
13982 (and (outline-next-heading) ;; |
13983 (org-flag-heading nil)))) ; show the next heading _|
13984 (outline-flag-region (max 1 (1- (point)))
13985 (save-excursion (outline-end-of-heading) (point))
13986 (if org-noutline-p
13987 flag
13988 (if flag ?\r ?\n))))))
13990 (defun org-end-of-subtree (&optional invisible-OK)
13991 ;; This is an exact copy of the original function, but it uses
13992 ;; `org-back-to-heading', to make it work also in invisible
13993 ;; trees. And is uses an invisible-OK argument.
13994 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
13995 (org-back-to-heading invisible-OK)
13996 (let ((first t)
13997 (level (funcall outline-level)))
13998 (while (and (not (eobp))
13999 (or first (> (funcall outline-level) level)))
14000 (setq first nil)
14001 (outline-next-heading))
14002 (if (memq (preceding-char) '(?\n ?\^M))
14003 (progn
14004 ;; Go to end of line before heading
14005 (forward-char -1)
14006 (if (memq (preceding-char) '(?\n ?\^M))
14007 ;; leave blank line before heading
14008 (forward-char -1))))))
14010 (defun org-show-subtree ()
14011 "Show everything after this heading at deeper levels."
14012 (outline-flag-region
14013 (point)
14014 (save-excursion
14015 (outline-end-of-subtree) (outline-next-heading) (point))
14016 (if org-noutline-p nil ?\n)))
14018 (defun org-show-entry ()
14019 "Show the body directly following this heading.
14020 Show the heading too, if it is currently invisible."
14021 (interactive)
14022 (save-excursion
14023 (org-back-to-heading t)
14024 (outline-flag-region
14025 (1- (point))
14026 (save-excursion
14027 (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
14028 (or (match-beginning 1) (point-max)))
14029 (if org-noutline-p nil ?\n))))
14031 (defun org-make-options-regexp (kwds)
14032 "Make a regular expression for keyword lines."
14033 (concat
14034 (if org-noutline-p "^" "[\n\r]")
14035 "#?[ \t]*\\+\\("
14036 (mapconcat 'regexp-quote kwds "\\|")
14037 "\\):[ \t]*"
14038 (if org-noutline-p "\\(.+\\)" "\\([^\n\r]+\\)")))
14040 ;; Make `bookmark-jump' show the jump location if it was hidden.
14041 (eval-after-load "bookmark"
14042 '(if (boundp 'bookmark-after-jump-hook)
14043 ;; We can use the hook
14044 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
14045 ;; Hook not available, use advice
14046 (defadvice bookmark-jump (after org-make-visible activate)
14047 "Make the position visible."
14048 (org-bookmark-jump-unhide))))
14050 (defun org-bookmark-jump-unhide ()
14051 "Unhide the current position, to show the bookmark location."
14052 (and (eq major-mode 'org-mode)
14053 (or (org-invisible-p)
14054 (save-excursion (goto-char (max (point-min) (1- (point))))
14055 (org-invisible-p)))
14056 (org-show-hierarchy-above)))
14058 ;;; Finish up
14060 (provide 'org)
14062 (run-hooks 'org-load-hook)
14064 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
14065 ;;; org.el ends here