]> code.delx.au - gnu-emacs/blob - lisp/textmodes/org.el
(org-promote, org-evaluate-time-range, org-agenda-next-date-line,
[gnu-emacs] / lisp / textmodes / org.el
1 ;;; org.el --- Outline-based notes management and organizer
2 ;; Carstens outline-mode for keeping track of everything.
3 ;; Copyright (c) 2004, 2005 Free Software Foundation
4 ;;
5 ;; Author: Carsten Dominik <dominik at science dot uva dot nl>
6 ;; Keywords: outlines, hypermedia, calendar
7 ;; Homepage: http://www.astro.uva.nl/~dominik/Tools/org/
8 ;; Version: 3.11
9 ;;
10 ;; This file is part of GNU Emacs.
11 ;;
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.
16
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.
21
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., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;
28 ;;; Commentary:
29 ;;
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.
32 ;;
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.
43 ;;
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.
50 ;;
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)
54 ;;
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:
58 ;;
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")
65 ;;
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:
68 ;;
69 ;; MY PROJECTS -*- mode: org; -*-
70 ;;
71 ;; which will select Org-mode for this buffer no matter what the file's
72 ;; name is.
73 ;;
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.
80 ;;
81 ;; Changes:
82 ;; -------
83 ;; Version 3.11
84 ;; - Links inserted with C-c C-l are now by default enclosed in angle
85 ;; brackets. See the new variable `org-link-format'.
86 ;; - ">" terminates a link, this is a way to have several links in a line.
87 ;; - Archiving of finished tasks.
88 ;; - C-<up>/<down> bindings removed, to allow access to paragraph commands.
89 ;; - Compatibility with CUA-mode (see variable `org-CUA-compatible').
90 ;; - Compatibility problems with viper-mode fixed.
91 ;; - Improved html export of tables.
92 ;; - Various clean-up changes.
93 ;;
94 ;; Version 3.10
95 ;; - Using `define-derived-mode' to derive `org-mode' from `outline-mode'.
96 ;;
97 ;; Version 3.09
98 ;; - Time-of-day specifications in agenda are extracted and placed
99 ;; into the prefix. Timed entries can be placed into a time grid for
100 ;; day.
101 ;;
102 ;; Version 3.08
103 ;; - "|" no longer allowed as part of a link, to allow links in tables.
104 ;; - The prefix of items in the agenda buffer can be configured.
105 ;; - Cleanup.
106 ;;
107 ;; Version 3.07
108 ;; - Some folding incinsistencies removed.
109 ;; - BBDB links to company-only entries.
110 ;; - Bug fixes and global cleanup.
111 ;;
112 ;; Version 3.06
113 ;; - M-S-RET inserts a new TODO heading.
114 ;; - New startup option `content'.
115 ;; - Better visual response when TODO items in agenda change status.
116 ;; - Window positioning after visibility state changes optimized and made
117 ;; configurable. See `org-cycle-hook' and `org-occur-hook'.
118 ;;
119 ;; Version 3.05
120 ;; - Agenda entries from the diary are linked to the diary file, so
121 ;; adding and editing diary entries can be done directly from the agenda.
122 ;; - Many calendar/diary commands available directly from agenda.
123 ;; - Field copying in tables with S-RET does increment.
124 ;; - C-c C-x C-v extracts the visible part of the buffer for printing.
125 ;; - Moving subtrees up and down preserves the whitespace at the tree end.
126 ;;
127 ;; Version 3.04
128 ;; - Table editor optimized to need fewer realignments, and to keep
129 ;; table shape when typing in fields.
130 ;; - A new minor mode, orgtbl-mode, introduces the Org-mode table editor
131 ;; into arbitrary major modes.
132 ;; - Fixed bug with realignment in XEmacs.
133 ;; - Startup options can be set with special #+STARTUP line.
134 ;; - Heading following a match in org-occur can be suppressed.
135 ;;
136 ;; Version 3.03
137 ;; - Copyright transfer to the FSF.
138 ;; - Effect of C-u and C-u C-u in org-timeline swapped.
139 ;; - Timeline now always contains today, and `.' jumps to it.
140 ;; - Table editor:
141 ;; - cut and paste of rectangular regions in tables
142 ;; - command to convert org-mode table to table.el table and back
143 ;; - command to treat several cells like a paragraph and fill it
144 ;; - command to convert a buffer region to a table
145 ;; - import/export tables as tab-separated files (exchange with Excel)
146 ;; - Agenda:
147 ;; - Sorting mechanism for agenda items rewritten from scratch.
148 ;; - Sorting fully configurable.
149 ;; - Entries specifying a time are sorted together.
150 ;; - Completion also covers option keywords after `#-'.
151 ;; - Bug fixes.
152 ;;
153 ;; Version 3.01
154 ;; - New reference card, thanks to Philip Rooke for creating it.
155 ;; - Single file agenda renamed to "Timeline". It no longer shows
156 ;; warnings about upcoming deadlines/overdue scheduled items.
157 ;; That functionality is now limited to the (multifile) agenda.
158 ;; - When reading a date, the calendar can be manipulated with keys.
159 ;; - Link support for RMAIL and Wanderlust (from planner.el, untested).
160 ;; - Minor bug fixes and documentation improvements.
161
162 ;;; Code:
163
164 (eval-when-compile (require 'cl) (require 'calendar))
165 (require 'outline)
166 (require 'time-date)
167 (require 'easymenu)
168
169 ;;; Customization variables
170
171 (defvar org-version "3.11"
172 "The version number of the file org.el.")
173 (defun org-version ()
174 (interactive)
175 (message "Org-mode version %s" org-version))
176
177 ;; The following two constants are for compatibility with different Emacs
178 ;; versions (Emacs versus XEmacs) and with different versions of outline.el.
179 ;; The compatibility code in org.el is based on these two constants.
180 (defconst org-xemacs-p (featurep 'xemacs)
181 "Are we running xemacs?")
182 (defconst org-noutline-p (featurep 'noutline)
183 "Are we using the new outline mode?")
184
185 (defgroup org nil
186 "Outline-based notes management and organizer "
187 :tag "Org"
188 :group 'outlines
189 :group 'hypermedia
190 :group 'calendar)
191
192 (defgroup org-startup nil
193 "Options concerning startup of Org-mode."
194 :tag "Org Startup"
195 :group 'org)
196
197 (defcustom org-CUA-compatible nil
198 "Non-nil means use alternative key bindings for S-<cursor movement>.
199 Org-mode used S-<cursor movement> for changing timestamps and priorities.
200 S-<cursor movement> is also used for example by `CUA-mode' to select text.
201 If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
202 alternative bindings. Setting this variable to t will replace the following
203 keys both in Org-mode and in the Org-agenda buffer.
204
205 S-RET -> C-S-RET
206 S-up -> M-p
207 S-down -> M-n
208 S-left -> M--
209 S-right -> M-+
210
211 If you do not like the alternative keys, take a look at the variable
212 `org-disputed-keys'.
213
214 This option is only relevant at load-time of Org-mode. Changing it requires
215 a restart of Emacs to become effective."
216 :group 'org-startup
217 :type 'boolean)
218
219 (defvar org-disputed-keys
220 '((S-up [(shift up)] [(meta ?p)])
221 (S-down [(shift down)] [(meta ?n)])
222 (S-left [(shift left)] [(meta ?-)])
223 (S-right [(shift right)] [(meta ?+)])
224 (S-return [(shift return)] [(control shift return)]))
225 "Keys for which Org-mode and other modes compete.
226 This is an alist, cars are symbols for lookup, 1st element is the default key,
227 second element will be used when `org-CUA-compatible' is t.")
228
229 (defun org-key (key)
230 "Select a key according to `org-CUA-compatible'."
231 (nth (if org-CUA-compatible 2 1)
232 (or (assq key org-disputed-keys)
233 (error "Invalid Key %s in `org-key'" key))))
234
235 (defcustom org-startup-folded t
236 "Non-nil means, entering Org-mode will switch to OVERVIEW.
237 This can also be configured on a per-file basis by adding one of
238 the following lines anywhere in the buffer:
239
240 #+STARTUP: fold
241 #+STARTUP: nofold
242 #+STARTUP: content"
243 :group 'org-startup
244 :type '(choice
245 (const :tag "nofold: show all" nil)
246 (const :tag "fold: overview" t)
247 (const :tag "content: all headlines" content)))
248
249 (defcustom org-startup-truncated t
250 "Non-nil means, entering Org-mode will set `truncate-lines'.
251 This is useful since some lines containing links can be very long and
252 uninteresting. Also tables look terrible when wrapped."
253 :group 'org-startup
254 :type 'boolean)
255
256 (defcustom org-startup-with-deadline-check nil
257 "Non-nil means, entering Org-mode will run the deadline check.
258 This means, if you start editing an org file, you will get an
259 immediate reminder of any due deadlines.
260 This can also be configured on a per-file basis by adding one of
261 the following lines anywhere in the buffer:
262
263 #+STARTUP: dlcheck
264 #+STARTUP: nodlcheck"
265 :group 'org-startup
266 :type 'boolean)
267
268 (defcustom org-insert-mode-line-in-empty-file nil
269 "Non-nil means insert the first line setting Org-mode in empty files.
270 When the function `org-mode' is called interactively in an empty file, this
271 normally means that the file name does not automatically trigger Org-mode.
272 To ensure that the file will always be in Org-mode in the future, a
273 line enforcing Org-mode will be inserted into the buffer, if this option
274 has been set."
275 :group 'org-startup
276 :type 'boolean)
277
278 (defgroup org-keywords nil
279 "Options concerning TODO items in Org-mode."
280 :tag "Org Keywords"
281 :group 'org)
282
283 (defcustom org-todo-keywords '("TODO" "DONE")
284 "List of TODO entry keywords.
285 \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
286 considered to mean that the entry is \"done\". All the other mean that
287 action is required, and will make the entry show up in todo lists, diaries
288 etc.
289 The command \\[org-todo] cycles an entry through these states, and an
290 additional state where no keyword is present. For details about this
291 cycling, see also the variable `org-todo-interpretation'
292 Changes become only effective after restarting Emacs."
293 :group 'org-keywords
294 :type '(repeat (string :tag "Keyword")))
295
296 (defcustom org-todo-interpretation 'sequence
297 "Controls how TODO keywords are interpreted.
298 \\<org-mode-map>Possible values are `sequence' and `type'.
299 This variable is only relevant if `org-todo-keywords' contains more than two
300 states. There are two ways how these keywords can be used:
301
302 - As a sequence in the process of working on a TODO item, for example
303 (setq org-todo-keywords '(\"TODO\" \"STARTED\" \"VERIFY\" \"DONE\")
304 org-todo-interpretation 'sequence)
305
306 - As different types of TODO items, for example
307 (setq org-todo-keywords '(\"URGENT\" \"RELAXED\" \"REMIND\" \"FOR_TOM\" \"DONE\")
308 org-todo-interpretation 'type)
309
310 When the states are interpreted as a sequence, \\[org-todo] always cycles
311 to the next state, in order to walk through all different states. So with
312 \\[org-todo], you turn an empty entry into the state TODO. When you started
313 working on the item, you use \\[org-todo] again to switch it to \"STARTED\",
314 later to VERIFY and finally to DONE.
315
316 When the states are interpreted as types, \\[org-todo] still cycles through
317 when it is called several times in direct succession, in order to initially
318 select the type. However, if not called immediately after a previous
319 \\[org-todo], it switches from each type directly to DONE. So with the
320 above example, you could use `\\[org-todo] \\[org-todo]' to label an entry
321 RELAXED. If you later return to this entry and press \\[org-todo] again,
322 RELAXED will not be changed REMIND, but directly to DONE.
323
324 You can create a large number of types. To initially select a
325 type, it is then best to use \\[universal-argument] \\[org-todo] in order to specify the
326 type with completion. Of course, you can also type the keyword
327 directly into the buffer. M-TAB completes TODO keywords at the
328 beginning of a headline."
329 :group 'org-keywords
330 :type '(choice (const sequence)
331 (const type)))
332
333 (defcustom org-default-priority ?B
334 "The default priority of TODO items.
335 This is the priority an item get if no explicit priority is given."
336 :group 'org-keywords
337 :type 'character)
338
339 (defcustom org-lowest-priority ?C
340 "The lowest priority of TODO items. A character like ?A, ?B etc."
341 :group 'org-keywords
342 :type 'character)
343
344 (defcustom org-deadline-string "DEADLINE:"
345 "String to mark deadline entries.
346 A deadline is this string, followed by a time stamp. Should be a word,
347 terminated by a colon. You can insert a schedule keyword and
348 a timestamp with \\[org-deadline].
349 Changes become only effective after restarting Emacs."
350 :group 'org-keywords
351 :type 'string)
352
353 (defcustom org-scheduled-string "SCHEDULED:"
354 "String to mark scheduled TODO entries.
355 A schedule is this string, followed by a time stamp. Should be a word,
356 terminated by a colon. You can insert a schedule keyword and
357 a timestamp with \\[org-schedule].
358 Changes become only effective after restarting Emacs."
359 :group 'org-keywords
360 :type 'string)
361
362 (defcustom org-comment-string "COMMENT"
363 "Entries starting with this keyword will never be exported.
364 An entry can be toggled between COMMENT and normal with
365 \\[org-toggle-comment].
366 Changes become only effective after restarting Emacs."
367 :group 'org-keywords
368 :type 'string)
369
370 (defcustom org-after-todo-state-change-hook nil
371 "Hook which is run after the state of a TODO item was changed.
372 The new state (a string with a todo keyword, or nil) is available in the
373 Lisp variable `state'."
374 :group 'org-keywords
375 :type 'hook)
376
377 ;; Variables for pre-computed regular expressions, all buffer local
378 (defvar org-todo-kwd-priority-p nil
379 "Do TODO items have priorities?")
380 (make-variable-buffer-local 'org-todo-kwd-priority-p)
381 (defvar org-todo-kwd-max-priority nil
382 "Maximum priority of TODO items.")
383 (make-variable-buffer-local 'org-todo-kwd-max-priority)
384 (defvar org-ds-keyword-length 12
385 "Maximum length of the Deadline and SCHEDULED keywords.")
386 (make-variable-buffer-local 'org-ds-keyword-length)
387 (defvar org-done-string nil
388 "The last string in `org-todo-keywords', indicating an item is DONE.")
389 (make-variable-buffer-local 'org-done-string)
390 (defvar org-todo-regexp nil
391 "Matches any of the TODO state keywords.")
392 (make-variable-buffer-local 'org-todo-regexp)
393 (defvar org-not-done-regexp nil
394 "Matches any of the TODO state keywords except the last one.")
395 (make-variable-buffer-local 'org-not-done-regexp)
396 (defvar org-todo-line-regexp nil
397 "Matches a headline and puts TODO state into group 2 if present.")
398 (make-variable-buffer-local 'org-todo-line-regexp)
399 (defvar org-nl-done-regexp nil
400 "Matches newline followed by a headline with the DONE keyword.")
401 (make-variable-buffer-local 'org-nl-done-regexp)
402 (defvar org-looking-at-done-regexp nil
403 "Matches the DONE keyword a point.")
404 (make-variable-buffer-local 'org-looking-at-done-regexp)
405 (defvar org-deadline-regexp nil
406 "Matches the DEADLINE keyword.")
407 (make-variable-buffer-local 'org-deadline-regexp)
408 (defvar org-deadline-time-regexp nil
409 "Matches the DEADLINE keyword together with a time stamp.")
410 (make-variable-buffer-local 'org-deadline-time-regexp)
411 (defvar org-deadline-line-regexp nil
412 "Matches the DEADLINE keyword and the rest of the line.")
413 (make-variable-buffer-local 'org-deadline-line-regexp)
414 (defvar org-scheduled-regexp nil
415 "Matches the SCHEDULED keyword.")
416 (make-variable-buffer-local 'org-scheduled-regexp)
417 (defvar org-scheduled-time-regexp nil
418 "Matches the SCHEDULED keyword together with a time stamp.")
419 (make-variable-buffer-local 'org-scheduled-time-regexp)
420
421 (defvar org-category nil
422 "Variable used by org files to set a category for agenda display.
423 Such files should use a file variable to set it, for example
424
425 -*- mode: org; org-category: \"ELisp\"
426
427 or contain a special line
428
429 #+CATEGORY: ELisp
430
431 If the file does not specify a category, then file's base name
432 is used instead.")
433
434 (defun org-set-regexps-and-options ()
435 "Precompute regular expressions for current buffer."
436 (when (eq major-mode 'org-mode)
437 (let ((re (org-make-options-regexp
438 '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
439 "STARTUP" "ARCHIVE")))
440 (splitre "[ \t]+")
441 kwds int key value cat arch)
442 (save-excursion
443 (save-restriction
444 (widen)
445 (goto-char (point-min))
446 (while (re-search-forward re nil t)
447 (setq key (match-string 1) value (match-string 2))
448 (cond
449 ((equal key "CATEGORY")
450 (if (string-match "[ \t]+$" value)
451 (setq value (replace-match "" t t value)))
452 (setq cat (intern value)))
453 ((equal key "SEQ_TODO")
454 (setq int 'sequence
455 kwds (append kwds (org-split-string value splitre))))
456 ((equal key "PRI_TODO")
457 (setq int 'priority
458 kwds (append kwds (org-split-string value splitre))))
459 ((equal key "TYP_TODO")
460 (setq int 'type
461 kwds (append kwds (org-split-string value splitre))))
462 ((equal key "STARTUP")
463 (let ((opts (org-split-string value splitre))
464 (set '(("fold" org-startup-folded t)
465 ("nofold" org-startup-folded nil)
466 ("content" org-startup-folded content)
467 ("dlcheck" org-startup-with-deadline-check t)
468 ("nodlcheck" org-startup-with-deadline-check nil)))
469 l var val)
470 (while (setq l (assoc (pop opts) set))
471 (setq var (nth 1 l) val (nth 2 l))
472 (set (make-local-variable var) val))))
473 ((equal key "ARCHIVE")
474 (string-match " *$" value)
475 (setq arch (replace-match "" t t value))
476 (remove-text-properties 0 (length arch)
477 '(face t fontified t) arch)))
478 )))
479 (and cat (set (make-local-variable 'org-category) cat))
480 (and kwds (set (make-local-variable 'org-todo-keywords) kwds))
481 (and arch (set (make-local-variable 'org-archive-location) arch))
482 (and int (set (make-local-variable 'org-todo-interpretation) int)))
483 ;; Compute the regular expressions and other local variables
484 (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
485 org-todo-kwd-max-priority (1- (length org-todo-keywords))
486 org-ds-keyword-length (+ 2 (max (length org-deadline-string)
487 (length org-scheduled-string)))
488 org-done-string
489 (nth (1- (length org-todo-keywords)) org-todo-keywords)
490 org-todo-regexp
491 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
492 "\\|") "\\)\\>")
493 org-not-done-regexp
494 (concat "\\<\\("
495 (mapconcat 'regexp-quote
496 (nreverse (cdr (reverse org-todo-keywords)))
497 "\\|")
498 "\\)\\>")
499 org-todo-line-regexp
500 (concat "^\\(\\*+\\)[ \t]*\\("
501 (mapconcat 'regexp-quote org-todo-keywords "\\|")
502 "\\)? *\\(.*\\)")
503 org-nl-done-regexp
504 (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
505 org-looking-at-done-regexp (concat "^" org-done-string "\\>")
506 org-deadline-regexp (concat "\\<" org-deadline-string)
507 org-deadline-time-regexp
508 (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
509 org-deadline-line-regexp
510 (concat "\\<\\(" org-deadline-string "\\).*")
511 org-scheduled-regexp
512 (concat "\\<" org-scheduled-string)
513 org-scheduled-time-regexp
514 (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
515 (org-set-font-lock-defaults)))
516
517 (defgroup org-time nil
518 "Options concerning time stamps and deadlines in Org-mode."
519 :tag "Org Time"
520 :group 'org)
521
522 (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
523 "Formats for `format-time-string' which are used for time stamps.
524 It is not recommended to change this constant.")
525
526
527 (defcustom org-deadline-warning-days 30
528 "No. of days before expiration during which a deadline becomes active.
529 This variable governs the display in the org file."
530 :group 'org-time
531 :type 'number)
532
533 (defcustom org-popup-calendar-for-date-prompt t
534 "Non-nil means, pop up a calendar when prompting for a date.
535 In the calendar, the date can be selected with mouse-1. However, the
536 minibuffer will also be active, and you can simply enter the date as well.
537 When nil, only the minibuffer will be available."
538 :group 'org-time
539 :type 'number)
540
541 (defcustom org-calendar-follow-timestamp-change t
542 "Non-nil means, make the calendar window follow timestamp changes.
543 When a timestamp is modified and the calendar window is visible, it will be
544 moved to the new date."
545 :group 'org-time
546 :type 'boolean)
547
548 (defgroup org-agenda nil
549 "Options concerning agenda display Org-mode."
550 :tag "Org Agenda"
551 :group 'org)
552
553 (defcustom org-agenda-files nil
554 "A list of org files for agenda/diary display.
555 Entries are added to this list with \\[org-add-file] and removed with
556 \\[org-remove-file]. You can also use customize to edit the list."
557 :group 'org-agenda
558 :type '(repeat file))
559
560 (defcustom org-select-timeline-window t
561 "Non-nil means, after creating a timeline, move cursor into Timeline window.
562 When nil, cursor will remain in the current window."
563 :group 'org-agenda
564 :type 'boolean)
565
566 (defcustom org-select-agenda-window t
567 "Non-nil means, after creating an agenda, move cursor into Agenda window.
568 When nil, cursor will remain in the current window."
569 :group 'org-agenda
570 :type 'boolean)
571
572 (defcustom org-fit-agenda-window t
573 "Non-nil means, change window size of agenda to fit content."
574 :group 'org-agenda
575 :type 'boolean)
576
577 (defcustom org-agenda-show-all-dates t
578 "Non-nil means, `org-agenda' shows every day in the selected range.
579 When nil, only the days which actually have entries are shown."
580 :group 'org-agenda
581 :type 'boolean)
582
583 ;; FIXME: First day of month works only for current month because it would
584 ;; require a variable ndays treatment.
585 (defcustom org-agenda-start-on-weekday 1
586 "Non-nil means, start the overview always on the specified weekday.
587 0 Denotes Sunday, 1 denotes Monday etc.
588 When nil, always start on the current day."
589 :group 'org-agenda
590 :type '(choice (const :tag "Today" nil)
591 (const :tag "First day of month" t)
592 (number :tag "Weekday No.")))
593
594 (defcustom org-agenda-ndays 7
595 "Number of days to include in overview display."
596 :group 'org-agenda
597 :type 'number)
598
599 (defcustom org-agenda-include-all-todo t
600 "Non-nil means, the agenda will always contain all TODO entries.
601 When nil, date-less entries will only be shown if `org-agenda' is called
602 with a prefix argument.
603 When non-nil, the TODO entries will be listed at the top of the agenda, before
604 the entries for specific days."
605 :group 'org-agenda
606 :type 'boolean)
607
608 (defcustom org-agenda-include-diary nil
609 "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
610 :group 'org-agenda
611 :type 'boolean)
612
613 (defcustom org-calendar-to-agenda-key [?c]
614 "The key to be installed in `calendar-mode-map' for switching to the agenda.
615 The command `org-calendar-goto-agenda' will be bound to this key. The
616 default is the character `c' because then`c' can be used to switch back and
617 force between agenda and calendar."
618 :group 'org-agenda
619 :type 'sexp)
620
621 (defcustom org-agenda-sorting-strategy '(time-up category-keep priority-down)
622 "Sorting structure for the agenda items of a single day.
623 This is a list of symbols which will be used in sequence to determine
624 if an entry should be listed before another entry. The following
625 symbols are recognized.
626
627 time-up Put entries with time-of-day indications first, early first
628 time-down Put entries with time-of-day indications first, late first
629 category-keep Keep the default order of categories, corresponding to the
630 sequence in `org-agenda-files'.
631 category-up Sort alphabetically by category, A-Z.
632 category-down Sort alphabetically by category, Z-A.
633 priority-up Sort numerically by priority, high priority last.
634 priority-down Sort numerically by priority, high priority first.
635
636 The different possibilities will be tried in sequence, and testing stops
637 if one comparison returns a \"not-equal\". For example, the default
638 '(time-up category-keep priority-down)
639 means: Pull out all entries having a specified time of day and sort them,
640 in order to make a time schedule for the current day the first thing in the
641 agenda listing for the day. Of the entries without a time indication, keep
642 the grouped in categories, don't sort the categories, but keep them in
643 the sequence given in `org-agenda-files'. Within each category sort by
644 priority.
645
646 Leaving out `category-keep' would mean that items will be sorted across
647 categories by priority."
648 :group 'org-agenda
649 :type '(repeat
650 (choice
651 (const time-up)
652 (const time-down)
653 (const category-keep)
654 (const category-up)
655 (const category-down)
656 (const priority-up)
657 (const priority-down))))
658
659 (defcustom org-agenda-prefix-format " %-12:c%?-12t% s"
660 "Format specification for the prefix of items in the agenda buffer.
661 This format works similar to a printf format, with the following meaning:
662
663 %c the category of the item, \"Diary\" for entries from the diary, or
664 as given by the CATEGORY keyword or derived from the file name.
665 %t the time-of-day specification if one applies to the entry, in the
666 format HH:MM
667 %s Scheduling/Deadline information, a short string
668
669 All specifiers work basically like the standard `%s' of printf, but may
670 contain two additional characters: A question mark just after the `%' and
671 a whitespace/punctuation character just before the final letter.
672
673 If the first character after `%' is a question mark, the entire field
674 will only be included if the corresponding value applies to the
675 current entry. This is useful for fields which should have fixed
676 width when present, but zero width when absent. For example,
677 \"%?-12t\" will result in a 12 character time field if a time of the
678 day is specified, but will completely disappear in entries which do
679 not contain a time.
680
681 If there is punctuation or whitespace character just before the final
682 format letter, this character will be appended to the field value if
683 the value is not empty. For example, the format \"%-12:c\" leads to
684 \"Diary: \" if the category is \"Diary\". If the category were be
685 empty, no additional colon would be interted.
686
687 The default value of this option is \" %-12:c%?-12t% s\", meaning:
688 - Indent the line with two space characters
689 - Give the category in a 12 chars wide field, padded with whitespace on
690 the right (because of `-'). Append a colon if there is a category
691 (because of `:').
692 - If there is a time-of-day, put it into a 12 chars wide field. If no
693 time, don't put in an empty field, just skip it (because of '?').
694 - Finally, put the scheduling information and append a whitespace.
695
696 As another example, if you don't want the time-of-day of entries in
697 the prefix, you could use:
698
699 (setq org-agenda-prefix-format \" %-11:c% s\")
700
701 See also the variable `org-agenda-remove-times-when-in-prefix'."
702 :type 'string
703 :group 'org-agenda)
704
705 (defcustom org-timeline-prefix-format " % s"
706 "Like `org-agenda-prefix-format', but for the timeline of a single file."
707 :type 'string
708 :group 'org-agenda)
709
710 (defvar org-prefix-format-compiled nil
711 "The compiled version of the most recently used prefix format.
712 Depending on which command was used last, this may be the compiled version
713 of `org-agenda-prefix-format' or `org-timeline-prefix-format'.")
714
715 (defcustom org-agenda-use-time-grid t
716 "Non-nil means, show a time grid in the agenda schedule.
717 A time grid is a set of lines for specific times (like every two hours between
718 8:00 and 20:00. The items scheduled for a day at specific times are
719 sorted in between these lines.
720 For deails about when the grid will be shown, and what it will look like, see
721 the variable `org-agenda-time-grid'."
722 :group 'org-agenda
723 :type 'boolean)
724
725 (defcustom org-agenda-time-grid
726 '((daily today require-timed)
727 "----------------"
728 (800 1000 1200 1400 1600 1800 2000))
729
730 "The settings for time grid for agenda display.
731 This is a list of three items. The first item is again a list. It contains
732 symbols specifying conditions when the grid should be displayed:
733
734 daily if the agenda shows a single day
735 weekly if the agenda shows an entire week
736 today show grid on current date, independent of daily/weekly display
737 require-timed show grid only if at least on item has a time specification
738
739 The second item is a string which will be places behing the grid time.
740
741 The third item is a list of integers, indicating the times that should have
742 a grid line."
743 :group 'org-agenda
744 :type
745 '(list
746 (set :greedy t :tag "Grid Display Options"
747 (const :tag "Show grid in single day agenda display" daily)
748 (const :tag "Show grid in weekly agenda display" weekly)
749 (const :tag "Always show grid for today" today)
750 (const :tag "Show grid only if any timed entries are present"
751 require-timed)
752 (const :tag "Skip grid times already present in an entry"
753 remove-match))
754 (string :tag "Grid String")
755 (repeat :tag "Grid Times" (integer :tag "Time"))))
756
757 (defcustom org-agenda-remove-times-when-in-prefix t
758 "Non-nil means, remove duplicate time specifications in agenda items.
759 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
760 time-of-day specification in a headline or diary entry is extracted and
761 placed into the prefix. If this option is non-nil, the original specification
762 \(a timestamp or -range, or just a plain time(range) specification like
763 11:30-4pm) will be removed for agenda display. This makes the agenda less
764 cluttered.
765 The option can be t or nil. It may also be the symbol `beg', indicating
766 that the time should only be removed what it is located at the beginning of
767 the headline/diary entry."
768 :group 'org-agenda
769 :type '(choice
770 (const :tag "Always" t)
771 (const :tag "Never" nil)
772 (const :tag "When at beginning of entry" beg)))
773
774 (defcustom org-sort-agenda-notime-is-late t
775 "Non-nil means, items without time are considered late.
776 This is only relevant for sorting. When t, items which have no explicit
777 time like 15:30 will be considered as 24:01, i.e. later than any items which
778 do have a time. When nil, the default time is before 0:00. You can use this
779 option to decide if the schedule for today should come before or after timeless
780 agenda entries."
781 :group 'org-agenda
782 :type 'boolean)
783
784 (defgroup org-structure nil
785 "Options concerning structure editing in Org-mode."
786 :tag "Org Structure"
787 :group 'org)
788
789 (defcustom org-cycle-hook '(org-optimize-window-after-visibility-change)
790 "Hook that is run after `org-cycle' has changed the buffer visibility.
791 The function(s) in this hook must accept a single argument which indicates
792 the new state that was set by the most recent `org-cycle' command. The
793 argument is a symbol. After a global state change, it can have the values
794 `overview', `content', or `all'. After a local state change, it can have
795 the values `folded', `children', or `subtree'."
796 :group 'org-structure
797 :type 'hook)
798
799 (defcustom org-occur-hook '(org-first-headline-recenter)
800 "Hook that is run after `org-occur' has constructed a sparse tree.
801 This can be used to recenter the window to show as much of the structure
802 as possible."
803 :group 'org-structure
804 :type 'hook)
805
806 (defcustom org-adapt-indentation t
807 "Non-nil means, adapt indentation when promoting and demoting.
808 When this is set and the *entire* text in an entry is indented, the
809 indentation is increased by one space in a demotion command, and
810 decreased by one in a promotion command. If any line in the entry
811 body starts at column 0, indentation is not changed at all."
812 :group 'org-structure
813 :type 'boolean)
814
815 (defcustom org-cycle-emulate-tab t
816 "Where should `org-cycle' emulate TAB.
817 nil Never
818 white Only in completely white lines
819 t Everywhere except in headlines"
820 :group 'org-structure
821 :type '(choice (const :tag "Never" nil)
822 (const :tag "Only in completely white lines" white)
823 (const :tag "Everywhere except in headlines" t)
824 ))
825
826 (defcustom org-show-following-heading t
827 "Non-nil means, show heading following match in `org-occur'.
828 When doing an `org-occur' it is useful to show the headline which
829 follows the match, even if they do not match the regexp. This makes it
830 easier to edit directly inside the sparse tree. However, if you use
831 org-occur mainly as an overview, the following headlines are
832 unnecessary clutter."
833 :group 'org-structure
834 :type 'boolean)
835
836 (defcustom org-archive-location "%s_archive::"
837 "The location where subtrees should be archived.
838 This string consists of two parts, separated by a double-colon.
839
840 The first part is a file name - when omitted, archiving happens in the same
841 file. %s will be replaced by the current file name (without directory part).
842 Archiving to a different file is useful to keep archived entries from
843 contributing to the Org-mode Agenda.
844
845 The part after the double colon is a headline. The archived entries will be
846 filed under that headline. When omitted, the subtrees are simply filed away
847 at the end of the file, as top-level entries.
848
849 Here are a few examples:
850 \"%s_archive::\"
851 If the current file is Projects.org, archive in file
852 Projects.org_archive, as top-level trees. This is the default.
853
854 \"::* Archived Tasks\"
855 Archive in the current file, under the top-level headline
856 \"* Archived Tasks\".
857
858 \"~/org/archive.org::\"
859 Archive in file ~/org/archive.org (absolute path), as top-level trees.
860
861 \"basement::** Finished Tasks\"
862 Archive in file ./basement (relative path), as level 3 trees
863 below the level 2 heading \"** Finished Tasks\".
864
865 You may set this option on a per-file basis by adding to the buffer a
866 line like
867
868 #+ARCHIVE: basement::** Finished Tasks"
869 :group 'org-structure
870 :type 'string)
871
872 (defcustom org-archive-mark-done t
873 "Non-nil means, mark archived entries as DONE."
874 :group 'org-structure
875 :type 'boolean)
876
877 (defcustom org-archive-stamp-time t
878 "Non-nil means, add a time stamp to archived entries.
879 The time stamp will be added directly after the TODO state keyword in the
880 first line, so it is probably best to use this in combinations with
881 `org-archive-mark-done'."
882 :group 'org-structure
883 :type 'boolean)
884
885 (defgroup org-link nil
886 "Options concerning links in Org-mode."
887 :tag "Org Link"
888 :group 'org)
889
890 (defcustom org-link-format "<%s>"
891 "Default format for linkes in the buffer.
892 This is a format string for printf, %s will be replaced by the link text.
893 If you want to make sure that your link is always properly terminated,
894 include angle brackets into this format, like \"<%s>\". Some people also
895 recommend an additional URL: prefix, so the format would be \"<URL:%s>\"."
896 :group 'org-link
897 :type '(choice
898 (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
899 (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
900 (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
901 (string :tag "Other" :value "<%s>")))
902
903 (defcustom org-allow-space-in-links t
904 "Non-nil means, file names in links may contain space characters.
905 When nil, it becomes possible to put several links into a line.
906 Note that in tables, a link never extends accross fields, so in a table
907 it is always possible to put several links into a line.
908 Changing this varable requires a re-launch of Emacs of become effective."
909 :group 'org-link
910 :type 'boolean)
911
912 (defcustom org-line-numbers-in-file-links t
913 "Non-nil means, file links from `org-store-link' contain line numbers.
914 The line number will be added to the file name with :NNN and interpreted
915 by the command `org-open-at-point'.
916 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
917 negates this setting for the duration of the command."
918 :group 'org-link
919 :type 'boolean)
920
921 (defcustom org-keep-stored-link-after-insertion nil
922 "Non-nil means, keep link in list for entire session.
923
924 The command `org-store-link' adds a link pointing to the current
925 location to an internal list. These links accumulate during a session.
926 The command `org-insert-link' can be used to insert links into any
927 Org-mode file (offering completion for all stored links). When this
928 option is nil, every link which has been inserted once using \\[org-insert-link]
929 will be removed from the list, to make completing the unused links
930 more efficient."
931 :group 'org-link
932 :type 'boolean)
933
934 (defcustom org-link-frame-setup
935 '((vm . vm-visit-folder-other-frame)
936 (gnus . gnus-other-frame)
937 (file . find-file-other-window))
938 "Setup the frame configuration for following links.
939 When following a link with Emacs, it may often be useful to display
940 this link in another window or frame. This variable can be used to
941 set this up for the different types of links.
942 For VM, use any of
943 `vm-visit-folder'
944 `vm-visit-folder-other-frame'
945 For Gnus, use any of
946 `gnus'
947 `gnus-other-frame'
948 For FILE, use any of
949 `find-file'
950 `find-file-other-window'
951 `find-file-other-frame'
952 For the calendar, use the variable `calendar-setup'.
953 For BBDB, it is currently only possible to display the matches in
954 another window."
955 :group 'org-link
956 :type '(list
957 (cons (const vm)
958 (choice
959 (const vm-visit-folder)
960 (const vm-visit-folder-other-window)
961 (const vm-visit-folder-other-frame)))
962 (cons (const gnus)
963 (choice
964 (const gnus)
965 (const gnus-other-frame)))
966 (cons (const file)
967 (choice
968 (const find-file)
969 (const find-file-other-window)
970 (const find-file-other-frame)))))
971
972 (defcustom org-usenet-links-prefer-google nil
973 "Non-nil means, `org-store-link' will create web links to google groups.
974 When nil, Gnus will be used for such links.
975 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
976 negates this setting for the duration of the command."
977 :group 'org-link
978 :type 'boolean)
979
980 (defcustom org-open-non-existing-files nil
981 "Non-nil means, `org-open-file' will open non-existing file.
982 When nil, an error will be generated."
983 :group 'org-link
984 :type 'boolean)
985
986 (defcustom org-confirm-shell-links t
987 "Non-nil means, ask for confirmation before executing shell links.
988 The default is true, to keep new users from shooting into their own foot."
989 :group 'org-link
990 :type 'boolean)
991
992 (defconst org-file-apps-defaults-linux
993 '((t . emacs)
994 ("jpg" . "xv %s")
995 ("gif" . "xv %s")
996 ("ppm" . "xv %s")
997 ("pgm" . "xv %s")
998 ("pbm" . "xv %s")
999 ("tif" . "xv %s")
1000 ("png" . "xv %s")
1001 ("ps" . "gv %s")
1002 ("ps.gz" . "gv %s")
1003 ("eps" . "gv %s")
1004 ("eps.gz" . "gv %s")
1005 ("dvi" . "xdvi %s")
1006 ("mpeg" . "plaympeg %s")
1007 ("mp3" . "plaympeg %s")
1008 ("fig" . "xfig %s")
1009 ("pdf" . "acroread %s")
1010 ("doc" . "soffice %s")
1011 ("ppt" . "soffice %s")
1012 ("pps" . "soffice %s")
1013 ("html" . "netscape -remote openURL(%s,new-window)")
1014 ("htm" . "netscape -remote openURL(%s,new-window)")
1015 ("xs" . "soffice %s"))
1016 "Default file applications on a UNIX/LINUX system.
1017 See `org-file-apps'.")
1018
1019 (defconst org-file-apps-defaults-macosx
1020 '((t . "open %s")
1021 ("ps" . "gv %s")
1022 ("ps.gz" . "gv %s")
1023 ("eps" . "gv %s")
1024 ("eps.gz" . "gv %s")
1025 ("dvi" . "xdvi %s")
1026 ("fig" . "xfig %s"))
1027 "Default file applications on a MacOS X system.
1028 The system \"open\" is known as a default, but we use X11 applications
1029 for some files for which the OS does not have a good default.
1030 See `org-file-apps'.")
1031
1032 (defconst org-file-apps-defaults-windowsnt
1033 '((t . (w32-shell-execute "open" file)))
1034 "Default file applications on a Windows NT system.
1035 The system \"open\" is used for most files.
1036 See `org-file-apps'.")
1037
1038 (defcustom org-file-apps
1039 '(
1040 ("txt" . emacs)
1041 ("tex" . emacs)
1042 ("ltx" . emacs)
1043 ("org" . emacs)
1044 ("el" . emacs)
1045 )
1046 "External applications for opening `file:path' items in a document.
1047 Org-mode uses system defaults for different file types, but
1048 you can use this variable to set the application for a given file
1049 extension. The entries in this list are cons cells with a file extension
1050 and the corresponding command. Possible values for the command are:
1051 `emacs' The file will be visited by the current Emacs process.
1052 `default' Use the default application for this file type.
1053 string A command to be executed by a shell; %s will be replaced
1054 by the path to the file.
1055 sexp A Lisp form which will be evaluated. The file path will
1056 be available in the Lisp variable `file'.
1057 For more examples, see the system specific constants
1058 `org-file-apps-defaults-macosx'
1059 `org-file-apps-defaults-windowsnt'
1060 `org-file-apps-defaults-linux'."
1061 :group 'org-link
1062 :type '(repeat
1063 (cons (string :tag "Extension")
1064 (choice :value ""
1065 (const :tag "Visit with Emacs" 'emacs)
1066 (const :tag "Use system default" 'default)
1067 (string :tag "Command")
1068 (sexp :tag "Lisp form")))))
1069
1070
1071 (defgroup org-remember nil
1072 "Options concerning interaction with remember.el."
1073 :tag "Org Remember"
1074 :group 'org)
1075
1076 (defcustom org-directory "~/org"
1077 "Directory with org files.
1078 This directory will be used as default to prompt for org files.
1079 Used by the hooks for remember.el."
1080 :group 'org-remember
1081 :type 'directory)
1082
1083 (defcustom org-default-notes-file "~/.notes"
1084 "Default target for storing notes.
1085 Used by the hooks for remember.el. This can be a string, or nil to mean
1086 the value of `remember-data-file'."
1087 :group 'org-remember
1088 :type '(choice
1089 (const :tag "Default from remember-data-file" nil)
1090 file))
1091
1092 (defcustom org-reverse-note-order nil
1093 "Non-nil means, store new notes at the beginning of a file or entry.
1094 When nil, new notes will be filed to the end of a file or entry."
1095 :group 'org-remember
1096 :type '(choice
1097 (const :tag "Reverse always" t)
1098 (const :tag "Reverse never" nil)
1099 (repeat :tag "By file name regexp"
1100 (cons regexp boolean))))
1101
1102 (defgroup org-table nil
1103 "Options concerning tables in Org-mode."
1104 :tag "Org Table"
1105 :group 'org)
1106
1107 (defcustom org-enable-table-editor 'optimized
1108 "Non-nil means, lines starting with \"|\" are handled by the table editor.
1109 When nil, such lines will be treated like ordinary lines.
1110
1111 When equal to the symbol `optimized', the table editor will be optimized to
1112 do the following
1113 - Use automatic overwrite mode in front of whitespace in table fields.
1114 This make the structure of the table stay in tact as long as the edited
1115 field does not exceed the column width.
1116 - Minimize the number of realigns. Normally, the table is aligned each time
1117 TAB or RET are pressed to move to another field. With optimization this
1118 happens only if changes to a field might have changed the column width.
1119 Optimization requires replacing the functions `self-insert-command',
1120 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1121 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1122 very good at guessing when a re-align will be necessary, but you can always
1123 force one with `C-c C-c'.
1124
1125 If you would like to use the optimized version in Org-mode, but the
1126 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1127
1128 This variable can be used to turn on and off the table editor during a session,
1129 but in order to toggle optimization, a restart is required."
1130 :group 'org-table
1131 :type '(choice
1132 (const :tag "off" nil)
1133 (const :tag "on" t)
1134 (const :tag "on, optimized" optimized)))
1135
1136 (defcustom org-table-default-size "5x2"
1137 "The default size for newly created tables, Columns x Rows."
1138 :group 'org-table
1139 :type 'string)
1140
1141 (defcustom org-table-automatic-realign t
1142 "Non-nil means, automatically re-align table when pressing TAB or RETURN.
1143 When nil, aligning is only done with \\[org-table-align], or after column
1144 removal/insertion."
1145 :group 'org-table
1146 :type 'boolean)
1147
1148 (defcustom org-table-spaces-around-separators '(1 . 1)
1149 "The number of spaces to be placed before and after separators."
1150 :group 'org-table
1151 :type '(cons (number :tag "Before \"|\"") (number :tag " After \"|\"")))
1152
1153 (defcustom org-table-spaces-around-invisible-separators '(1 . 2)
1154 "The number of spaces to be placed before and after separators.
1155 This option applies when the column separators have been made invisible."
1156 :group 'org-table
1157 :type '(cons (number :tag "Before \"|\"") (number :tag " After \"|\"")))
1158
1159 (defcustom org-table-number-regexp "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$"
1160 "Regular expression for recognizing numbers in table columns.
1161 If a table column contains mostly numbers, it will be aligned to the
1162 right. If not, it will be aligned to the left.
1163
1164 The default value of this option is a regular expression which allows
1165 anything which looks remotely like a number as used in scientific
1166 context. For example, all of the following will be considered a
1167 number:
1168 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
1169
1170 Other options offered by the customize interface are more restrictive."
1171 :group 'org-table
1172 :type '(choice
1173 (const :tag "Positive Integers"
1174 "^[0-9]+$")
1175 (const :tag "Integers"
1176 "^[-+]?[0-9]+$")
1177 (const :tag "Floating Point Numbers"
1178 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
1179 (const :tag "Floating Point Number or Integer"
1180 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
1181 (const :tag "Exponential, Floating point, Integer"
1182 "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
1183 (const :tag "Very General Number-Like"
1184 "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$")
1185 (string :tag "Regexp:")))
1186
1187 (defcustom org-table-number-fraction 0.5
1188 "Fraction of numbers in a column required to make the column align right.
1189 In a column all non-white fields are considered. If at least this
1190 fraction of fields is matched by `org-table-number-fraction',
1191 alignment to the right border applies."
1192 :group 'org-table
1193 :type 'number)
1194
1195 (defcustom org-export-highlight-first-table-line t
1196 "Non-nil means, highlight the first table line.
1197 In HTML export, this means use <th> instead of <td>.
1198 In tables created with table.el, this applies to the first table line.
1199 In Org-mode tables, all lines before the first horizontal separator
1200 line will be formatted with <th> tags."
1201 :group 'org-table
1202 :type 'boolean)
1203
1204 (defcustom org-table-copy-increment t
1205 "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
1206 :group 'org-table
1207 :type 'boolean)
1208
1209 (defcustom org-table-tab-recognizes-table.el t
1210 "Non-nil means, TAB will automatically notice a table.el table.
1211 When it sees such a table, it moves point into it and - if necessary -
1212 calls `table-recognize-table'."
1213 :group 'org-table
1214 :type 'boolean)
1215
1216 (defcustom org-export-prefer-native-exporter-for-tables nil
1217 "Non-nil means, always export tables created with table.el natively.
1218 Natively means, use the HTML code generator in table.el.
1219 When nil, Org-mode's own HTML generator is used when possible (i.e. if
1220 the table does not use row- or column-spanning). This has the
1221 advantage, that the automatic HTML conversions for math symbols and
1222 sub/superscripts can be applied. Org-mode's HTML generator is also
1223 much faster."
1224 :group 'org-table
1225 :type 'boolean)
1226
1227 (defcustom org-enable-fixed-width-editor t
1228 "Non-nil means, lines starting with \":\" are treated as fixed-width.
1229 This currently only means, they are never auto-wrapped.
1230 When nil, such lines will be treated like ordinary lines."
1231 :group 'org-table
1232 :type 'boolean)
1233
1234 (defgroup org-export nil
1235 "Options for exporting org-listings."
1236 :tag "Org Export"
1237 :group 'org)
1238
1239 (defcustom org-export-language-setup
1240 '(("en" "Author" "Date" "Table of Contents")
1241 ("da" "Ophavsmand" "Dato" "Indhold")
1242 ("de" "Autor" "Datum" "Inhaltsverzeichnis")
1243 ("es" "Autor" "Fecha" "\xccndice")
1244 ("fr" "Auteur" "Date" "Table des Mati\xe8res")
1245 ("it" "Autore" "Data" "Indice")
1246 ("nl" "Auteur" "Datum" "Inhoudsopgave")
1247 ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
1248 ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
1249 "Terms used in export text, translated to different languages.
1250 Use the variable `org-export-default-language' to set the language,
1251 or use the +OPTION lines for a per-file setting."
1252 :group 'org-export
1253 :type '(repeat
1254 (list
1255 (string :tag "HTML language tag")
1256 (string :tag "Author")
1257 (string :tag "Date")
1258 (string :tag "Table of Contents"))))
1259
1260 (defcustom org-export-default-language "en"
1261 "The default language of HTML export, as a string.
1262 This should have an association in `org-export-language-setup'"
1263 :group 'org-export
1264 :type 'string)
1265
1266 (defcustom org-export-headline-levels 3
1267 "The last level which is still exported as a headline.
1268 Inferior levels will produce itemize lists when exported.
1269 Note that a numeric prefix argument to an exporter function overrides
1270 this setting.
1271
1272 This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
1273 :group 'org-export
1274 :type 'number)
1275
1276 (defcustom org-export-with-section-numbers t
1277 "Non-nil means, add section numbers to headlines when exporting.
1278
1279 This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
1280 :group 'org-export
1281 :type 'boolean)
1282
1283 (defcustom org-export-with-toc t
1284 "Non-nil means, create a table of contents in exported files.
1285 The TOC contains headlines with levels up to`org-export-headline-levels'.
1286
1287 Headlines which contain any TODO items will be marked with \"(*)\" in
1288 ASCII export, and with red color in HTML output.
1289
1290 In HTML output, the TOC will be clickable.
1291
1292 This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
1293 :group 'org-export
1294 :type 'boolean)
1295
1296 (defcustom org-export-preserve-breaks nil
1297 "Non-nil means, preserve all line breaks when exporting.
1298 Normally, in HTML output paragraphs will be reformatted. In ASCII
1299 export, line breaks will always be preserved, regardless of this variable.
1300
1301 This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
1302 :group 'org-export
1303 :type 'boolean)
1304
1305 (defcustom org-export-html-inline-images t
1306 "Non-nil means, inline images into exported HTML pages.
1307 The link will still be to the original location of the image file.
1308 So if you are moving the page, lets say to your public HTML site,
1309 you will have to move the image and maybe change the link."
1310 :group 'org-export
1311 :type 'boolean)
1312
1313 (defcustom org-export-html-expand t
1314 "Non-nil means, for HTML export, treat @<...> as HTML tag.
1315 When nil, these tags will be exported as plain text and therefore
1316 not be interpreted by a browser.
1317
1318 This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
1319 :group 'org-export
1320 :type 'boolean)
1321
1322 (defcustom org-export-with-fixed-width t
1323 "Non-nil means, lines starting with \":\" will be in fixed width font.
1324 This can be used to have pre-formatted text, fragments of code etc. For
1325 example
1326 : ;; Some Lisp examples
1327 : (while (defc cnt)
1328 : (ding))
1329 will be looking just like this in also HTML. In ASCII export, this option
1330 has no effect.
1331
1332 This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
1333 :group 'org-export
1334 :type 'boolean)
1335
1336 (defcustom org-export-with-tables t
1337 "If non-nil, lines starting with \"|\" define a table.
1338 For example:
1339
1340 | Name | Address | Birthday |
1341 |-------------+----------+-----------|
1342 | Arthur Dent | England | 29.2.2100 |
1343
1344 In ASCII export, this option has no effect.
1345
1346 This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
1347 :group 'org-export
1348 :type 'boolean)
1349
1350 (defcustom org-export-html-table-tag
1351 "<table border=1 cellspacing=0 cellpadding=6>"
1352 "The HTML tag used to start a table.
1353 This must be a <table> tag, but you may change the options like
1354 borders and spacing."
1355 :group 'org-export
1356 :type 'string)
1357
1358 (defcustom org-export-with-emphasize t
1359 "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
1360 If the export target supports emphasizing text, the word will be
1361 typeset in bold, italic, or underlined, respectively. Works only for
1362 single words, but you can say: I *really* *mean* *this*.
1363 In ASCII export, this option has no effect.
1364
1365 This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
1366 :group 'org-export
1367 :type 'boolean)
1368
1369 (defcustom org-match-sexp-depth 3
1370 "Number of stacked braces for sub/superscript matching.
1371 This has to be set before loading org.el to be effective."
1372 :group 'org-export
1373 :type 'integer)
1374
1375 ;; FIXME: Should () parens be removed as well in sub/superscripts?
1376 (defcustom org-export-with-sub-superscripts t
1377 "Non-nil means, interpret \"_\" and \"^\" for export.
1378 When this option is turned on, you can use TeX-like syntax for sub- and
1379 superscripts. Several characters after \"_\" or \"^\" will be
1380 considered as a single item - so grouping with {} is normally not
1381 needed. For example, the following things will be parsed as single
1382 sub- or superscripts.
1383
1384 10^24 or 10^tau several digits will be considered 1 item
1385 10^-12 or 10^-tau a leading sign with digits or a word
1386 x^2-y^3 will be read as x^2 - y^3, because items are
1387 terminated by almost any nonword/nondigit char.
1388 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
1389
1390 Still, ambiguity is possible - so when in doubt use {} to enclose the
1391 sub/superscript.
1392 In ASCII export, this option has no effect.
1393
1394 This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
1395 :group 'org-export
1396 :type 'boolean)
1397
1398 (defcustom org-export-with-TeX-macros t
1399 "Non-nil means, interpret simple TeX-like macros when exporting.
1400 For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
1401 No only real TeX macros will work here, but the standard HTML entities
1402 for math can be used as macro names as well. For a list of supported
1403 names in HTML export, see the constant `org-html-entities'.
1404 In ASCII export, this option has no effect.
1405
1406 This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
1407 :group 'org-export
1408 :type 'boolean)
1409
1410 (defcustom org-export-html-with-timestamp nil
1411 "If non-nil, write `org-export-html-html-helper-timestamp'
1412 into the exported html text. Otherwise, the buffer will just be saved
1413 to a file."
1414 :group 'org-export
1415 :type 'boolean)
1416
1417 (defcustom org-export-html-html-helper-timestamp
1418 "<br><br><hr><p><!-- hhmts start --> <!-- hhmts end -->\n"
1419 "The HTML tag used as timestamp delimiter for HTML-helper-mode."
1420 :group 'org-export
1421 :type 'string)
1422
1423 (defcustom org-export-ascii-show-new-buffer t
1424 "Non-nil means, popup buffer containing the exported ASCII text.
1425 Otherwise the buffer will just be saved to a file and stay hidden."
1426 :group 'org-export
1427 :type 'boolean)
1428
1429 (defcustom org-export-html-show-new-buffer nil
1430 "Non-nil means, popup buffer containing the exported html text.
1431 Otherwise, the buffer will just be saved to a file and stay hidden."
1432 :group 'org-export
1433 :type 'boolean)
1434
1435
1436 (defgroup org-faces nil
1437 "Faces for highlighting in Org-mode."
1438 :tag "Org Faces"
1439 :group 'org)
1440
1441 (defface org-level-1 ;; font-lock-function-name-face
1442 '((((type tty) (class color)) (:foreground "blue" :weight bold))
1443 (((class color) (background light)) (:foreground "Blue"))
1444 (((class color) (background dark)) (:foreground "LightSkyBlue"))
1445 (t (:inverse-video t :bold t)))
1446 "Face used for level 1 headlines."
1447 :group 'org-faces)
1448
1449 (defface org-level-2 ;; font-lock-variable-name-face
1450 '((((type tty) (class color)) (:foreground "yellow" :weight light))
1451 (((class color) (background light)) (:foreground "DarkGoldenrod"))
1452 (((class color) (background dark)) (:foreground "LightGoldenrod"))
1453 (t (:bold t :italic t)))
1454 "Face used for level 2 headlines."
1455 :group 'org-faces)
1456
1457 (defface org-level-3 ;; font-lock-keyword-face
1458 '((((type tty) (class color)) (:foreground "cyan" :weight bold))
1459 (((class color) (background light)) (:foreground "Purple"))
1460 (((class color) (background dark)) (:foreground "Cyan"))
1461 (t (:bold t)))
1462 "Face used for level 3 headlines."
1463 :group 'org-faces)
1464
1465 (defface org-level-4 ;; font-lock-comment-face
1466 '((((type tty pc) (class color) (background light)) (:foreground "red"))
1467 (((type tty pc) (class color) (background dark)) (:foreground "red1"))
1468 (((class color) (background light)) (:foreground "Firebrick"))
1469 (((class color) (background dark)) (:foreground "chocolate1"))
1470 (t (:bold t :italic t)))
1471 "Face used for level 4 headlines."
1472 :group 'org-faces)
1473
1474 (defface org-level-5 ;; font-lock-type-face
1475 '((((type tty) (class color)) (:foreground "green"))
1476 (((class color) (background light)) (:foreground "ForestGreen"))
1477 (((class color) (background dark)) (:foreground "PaleGreen"))
1478 (t (:bold t :underline t)))
1479 "Face used for level 5 headlines."
1480 :group 'org-faces)
1481
1482 (defface org-level-6 ;; font-lock-constant-face
1483 '((((type tty) (class color)) (:foreground "magenta"))
1484 (((class color) (background light)) (:foreground "CadetBlue"))
1485 (((class color) (background dark)) (:foreground "Aquamarine"))
1486 (t (:bold t :underline t)))
1487 "Face used for level 6 headlines."
1488 :group 'org-faces)
1489
1490 (defface org-level-7 ;; font-lock-builtin-face
1491 '((((type tty) (class color)) (:foreground "blue" :weight light))
1492 (((class color) (background light)) (:foreground "Orchid"))
1493 (((class color) (background dark)) (:foreground "LightSteelBlue"))
1494 (t (:bold t)))
1495 "Face used for level 7 headlines."
1496 :group 'org-faces)
1497
1498 (defface org-level-8 ;; font-lock-string-face
1499 '((((type tty) (class color)) (:foreground "green"))
1500 (((class color) (background light)) (:foreground "RosyBrown"))
1501 (((class color) (background dark)) (:foreground "LightSalmon"))
1502 (t (:italic t)))
1503 "Face used for level 8 headlines."
1504 :group 'org-faces)
1505
1506 (defface org-warning ;; font-lock-warning-face
1507 '((((type tty) (class color)) (:foreground "red"))
1508 (((class color) (background light)) (:foreground "Red" :bold t))
1509 (((class color) (background dark)) (:foreground "Red1" :bold t))
1510 ; (((class color) (background dark)) (:foreground "Pink" :bold t))
1511 (t (:inverse-video t :bold t)))
1512 "Face for deadlines and TODO keywords."
1513 :group 'org-faces)
1514
1515 (defcustom org-fontify-done-headline nil
1516 "Non-nil means, change the face of a headline if it is marked DONE.
1517 Normally, only the TODO/DONE keyword indicates the state of a headline.
1518 When this is non-nil, the headline after the keyword is set to the
1519 `org-headline-done' as an additional indication."
1520 :group 'org-faces
1521 :type 'boolean)
1522
1523 (defface org-headline-done ;; font-lock-string-face
1524 '((((type tty) (class color)) (:foreground "green"))
1525 (((class color) (background light)) (:foreground "RosyBrown"))
1526 (((class color) (background dark)) (:foreground "LightSalmon"))
1527 (t (:italic t)))
1528 "Face used to indicate that a headline is DONE. See also the variable
1529 `org-fontify-done-headline'."
1530 :group 'org-faces)
1531
1532 ;; Inheritance does not yet work for xemacs. So we just copy...
1533
1534 (defface org-deadline-announce
1535 '((((type tty) (class color)) (:foreground "blue" :weight bold))
1536 (((class color) (background light)) (:foreground "Blue"))
1537 (((class color) (background dark)) (:foreground "LightSkyBlue"))
1538 (t (:inverse-video t :bold t)))
1539 "Face for upcoming deadlines."
1540 :group 'org-faces)
1541
1542 (defface org-scheduled-today
1543 '((((type tty) (class color)) (:foreground "green"))
1544 (((class color) (background light)) (:foreground "DarkGreen"))
1545 (((class color) (background dark)) (:foreground "PaleGreen"))
1546 (t (:bold t :underline t)))
1547 "Face for items scheduled for a certain day."
1548 :group 'org-faces)
1549
1550 (defface org-scheduled-previously
1551 '((((type tty pc) (class color) (background light)) (:foreground "red"))
1552 (((type tty pc) (class color) (background dark)) (:foreground "red1"))
1553 (((class color) (background light)) (:foreground "Firebrick"))
1554 (((class color) (background dark)) (:foreground "chocolate1"))
1555 (t (:bold t :italic t)))
1556 "Face for items scheduled previously, and not yet done."
1557 :group 'org-faces)
1558
1559 (defface org-link
1560 '((((type tty) (class color)) (:foreground "cyan" :weight bold))
1561 (((class color) (background light)) (:foreground "Purple"))
1562 (((class color) (background dark)) (:foreground "Cyan"))
1563 (t (:bold t)))
1564 "Face for links."
1565 :group 'org-faces)
1566
1567 (defface org-done ;; font-lock-type-face
1568 '((((type tty) (class color)) (:foreground "green"))
1569 (((class color) (background light)) (:foreground "ForestGreen" :bold t))
1570 (((class color) (background dark)) (:foreground "PaleGreen" :bold t))
1571 (t (:bold t :underline t)))
1572 "Face used for DONE."
1573 :group 'org-faces)
1574
1575 (defface org-table ;; font-lock-function-name-face
1576 '((((type tty) (class color)) (:foreground "blue" :weight bold))
1577 (((class color) (background light)) (:foreground "Blue"))
1578 (((class color) (background dark)) (:foreground "LightSkyBlue"))
1579 (t (:inverse-video t :bold t)))
1580 "Face used for tables."
1581 :group 'org-faces)
1582
1583 (defface org-time-grid ;; font-lock-variable-name-face
1584 '((((type tty) (class color)) (:foreground "yellow" :weight light))
1585 (((class color) (background light)) (:foreground "DarkGoldenrod"))
1586 (((class color) (background dark)) (:foreground "LightGoldenrod"))
1587 (t (:bold t :italic t)))
1588 "Face used for level 2 headlines."
1589 :group 'org-faces)
1590
1591 (defvar org-level-faces
1592 '(
1593 org-level-1
1594 org-level-2
1595 org-level-3
1596 org-level-4
1597 org-level-5
1598 org-level-6
1599 org-level-7
1600 org-level-8
1601 ))
1602 (defvar org-n-levels (length org-level-faces))
1603
1604 ;; Tell the compiler about dynamically scoped variables,
1605 ;; and variables from other packages
1606 (eval-when-compile
1607 (defvar zmacs-regions)
1608 (defvar original-date)
1609 (defvar org-transient-mark-mode)
1610 (defvar org-old-auto-fill-inhibit-regexp)
1611 (defvar orgtbl-mode-menu)
1612 (defvar org-html-entities)
1613 (defvar org-goto-start-pos)
1614 (defvar org-cursor-color)
1615 (defvar org-time-was-given)
1616 (defvar org-ts-what)
1617 (defvar mark-active)
1618 (defvar timecnt)
1619 (defvar levels-open)
1620 (defvar title)
1621 (defvar author)
1622 (defvar email)
1623 (defvar text)
1624 (defvar entry)
1625 (defvar date)
1626 (defvar language)
1627 (defvar options)
1628 (defvar ans1)
1629 (defvar ans2)
1630 (defvar starting-day)
1631 (defvar include-all-loc)
1632 (defvar vm-message-pointer)
1633 (defvar vm-folder-directory)
1634 (defvar wl-summary-buffer-elmo-folder)
1635 (defvar wl-summary-buffer-folder-name)
1636 (defvar gnus-group-name)
1637 (defvar gnus-article-current)
1638 (defvar w3m-current-url)
1639 (defvar org-selected-point)
1640 (defvar calendar-mode-map)
1641 (defvar remember-save-after-remembering)
1642 (defvar remember-data-file))
1643
1644
1645 ;;; Define the mode
1646
1647 (defvar org-mode-map (copy-keymap outline-mode-map)
1648 "Keymap for Org-mode.")
1649
1650 (defvar org-struct-menu)
1651 (defvar org-org-menu)
1652
1653 ;; We use a before-change function to check if a table might need
1654 ;; an update.
1655 (defvar org-table-may-need-update t
1656 "Indicates of a table might need an update.
1657 This variable is set by `org-before-change-function'. `org-table-align'
1658 sets it back to nil.")
1659
1660 (defvar org-mode-hook nil)
1661 (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
1662
1663
1664 ;;;###autoload
1665 (define-derived-mode org-mode outline-mode "Org"
1666 "Outline-based notes management and organizer, alias
1667 \"Carstens outline-mode for keeping track of everything.\"
1668
1669 Org-mode develops organizational tasks around a NOTES file which
1670 contains information about projects as plain text. Org-mode is
1671 implemented on top of outline-mode, which is ideal to keep the content
1672 of large files well structured. It supports ToDo items, deadlines and
1673 time stamps, which magically appear in the diary listing of the Emacs
1674 calendar. Tables are easily created with a built-in table editor.
1675 Plain text URL-like links connect to websites, emails (VM), Usenet
1676 messages (Gnus), BBDB entries, and any files related to the project.
1677 For printing and sharing of notes, an Org-mode file (or a part of it)
1678 can be exported as a structured ASCII or HTML file.
1679
1680 The following commands are available:
1681
1682 \\{org-mode-map}"
1683 (easy-menu-add org-org-menu)
1684 (org-install-agenda-files-menu)
1685 (setq outline-regexp "\\*+")
1686 (if org-startup-truncated (setq truncate-lines t))
1687 (org-set-regexps-and-options)
1688 (set (make-local-variable 'font-lock-unfontify-region-function)
1689 'org-unfontify-region)
1690 ;; Activate before-change-function
1691 (set (make-local-variable 'org-table-may-need-update) t)
1692 (make-local-hook 'before-change-functions) ;; needed for XEmacs
1693 (add-hook 'before-change-functions 'org-before-change-function nil
1694 'local)
1695 ;; Paragraph regular expressions
1696 (set (make-local-variable 'paragraph-separate) "\f\\|[ ]*$")
1697 (set (make-local-variable 'paragraph-start) "\f\\|[ ]*$\\|\\([*\f]+\\)")
1698 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
1699 (set (make-local-variable 'auto-fill-inhibit-regexp)
1700 (concat "\\*"
1701 (if (or org-enable-table-editor org-enable-fixed-width-editor)
1702 (concat
1703 "\\|[ \t]*["
1704 (if org-enable-table-editor "|" "")
1705 (if org-enable-fixed-width-editor ":" "")
1706 "]"))))
1707 (set (make-local-variable 'fill-paragraph-function) 'org-fill-paragraph)
1708 (if (and org-insert-mode-line-in-empty-file
1709 (interactive-p)
1710 (= (point-min) (point-max)))
1711 (insert " -*- mode: org -*-\n\n"))
1712 (unless org-inhibit-startup
1713 (if org-startup-with-deadline-check
1714 (call-interactively 'org-check-deadlines)
1715 (cond
1716 ((eq org-startup-folded t)
1717 (org-cycle '(4)))
1718 ((eq org-startup-folded 'content)
1719 (let ((this-command 'org-cycle) (last-command 'org-cycle))
1720 (org-cycle '(4)) (org-cycle '(4))))))))
1721
1722 (defun org-fill-paragraph (&optional justify)
1723 "Re-align a table, pass through to fill-paragraph if no table."
1724 (save-excursion
1725 (beginning-of-line 1)
1726 (looking-at "\\s-*\\(|\\|\\+-+\\)")))
1727
1728 ;;; Font-Lock stuff
1729
1730 (defvar org-mouse-map (make-sparse-keymap))
1731 (define-key org-mouse-map
1732 (if org-xemacs-p [button2] [mouse-2]) 'org-open-at-mouse)
1733 (define-key org-mouse-map
1734 (if org-xemacs-p [button3] [mouse-3]) 'org-find-file-at-mouse)
1735
1736 (require 'font-lock)
1737
1738 (defconst org-non-link-chars "\t\n\r|<>\000")
1739 (defconst org-link-regexp
1740 (if org-allow-space-in-links
1741 (concat
1742 "\\(https?\\|ftp\\|mailto\\|file\\|news\\|bbdb\\|vm\\|wl\\|rmail\\|gnus\\|shell\\):\\([^" org-non-link-chars "]+[^ " org-non-link-chars "]\\)")
1743 (concat
1744 "\\(https?\\|ftp\\|mailto\\|file\\|news\\|bbdb\\|vm\\|wl\\|rmail\\|gnus\\|shell\\):\\([^ " org-non-link-chars "]+\\)")
1745 )
1746 "Regular expression for matching links.")
1747 (defconst org-link-maybe-angles-regexp
1748 (concat "<?\\(" org-link-regexp "\\)>?")
1749 "Matches a link and optionally surrounding angle brackets.")
1750 (defconst org-protected-link-regexp
1751 (concat "\000" org-link-regexp "\000")
1752 "Matches a link and optionally surrounding angle brackets.")
1753
1754 (defconst org-ts-lengths
1755 (cons (length (format-time-string (car org-time-stamp-formats)))
1756 (length (format-time-string (cdr org-time-stamp-formats))))
1757 "This holds the lengths of the two different time formats.")
1758 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*\\)>"
1759 "Regular expression for fast time stamp matching.")
1760 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
1761 "Regular expression matching time strings for analysis.")
1762 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
1763 "Regular expression matching time stamps, with groups.")
1764 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
1765 "Regular expression matching a time stamp range.")
1766 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
1767 org-ts-regexp "\\)?")
1768 "Regular expression matching a time stamp or time stamp range.")
1769
1770 (defun org-activate-links (limit)
1771 "Run through the buffer and add overlays to links."
1772 (if (re-search-forward org-link-regexp limit t)
1773 (progn
1774 (add-text-properties (match-beginning 0) (match-end 0)
1775 (list 'mouse-face 'highlight
1776 'keymap org-mouse-map))
1777 t)))
1778
1779 (defun org-activate-dates (limit)
1780 "Run through the buffer and add overlays to dates."
1781 (if (re-search-forward org-tsr-regexp limit t)
1782 (progn
1783 (add-text-properties (match-beginning 0) (match-end 0)
1784 (list 'mouse-face 'highlight
1785 'keymap org-mouse-map))
1786 t)))
1787
1788 (defun org-font-lock-level ()
1789 (save-excursion
1790 (org-back-to-heading t)
1791 (- (match-end 0) (match-beginning 0))))
1792
1793 (defvar org-font-lock-keywords nil)
1794
1795 (defun org-set-font-lock-defaults ()
1796 (let ((org-font-lock-extra-keywords
1797 (list
1798 '(org-activate-links (0 'org-link))
1799 '(org-activate-dates (0 'org-link))
1800 (list (concat "^\\*+[ \t]*" org-not-done-regexp)
1801 '(1 'org-warning t))
1802 (list (concat "\\[#[A-Z]\\]") '(0 'org-warning t))
1803 (list (concat "\\<" org-deadline-string) '(0 'org-warning t))
1804 (list (concat "\\<" org-scheduled-string) '(0 'org-warning t))
1805 ;; '("\\(\\s-\\|^\\)\\(\\*\\([a-zA-Z]+\\)\\*\\)\\([^a-zA-Z*]\\|$\\)"
1806 ;; (3 'bold))
1807 ;; '("\\(\\s-\\|^\\)\\(/\\([a-zA-Z]+\\)/\\)\\([^a-zA-Z*]\\|$\\)"
1808 ;; (3 'italic))
1809 ;; '("\\(\\s-\\|^\\)\\(_\\([a-zA-Z]+\\)_\\)\\([^a-zA-Z*]\\|$\\)"
1810 ;; (3 'underline))
1811 '("\\<FIXME\\>" (0 'org-warning t))
1812 (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string "\\)\\>")
1813 '(1 'org-warning t))
1814 '("^#.*" (0 'font-lock-comment-face t))
1815 (if org-fontify-done-headline
1816 (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
1817 '(1 'org-done t) '(2 'org-headline-done t))
1818 (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
1819 '(1 'org-done t)))
1820 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
1821 (1 'org-table t))
1822 '("^[ \t]*\\(:.*\\)" (1 'org-table t)))))
1823 (set (make-local-variable 'org-font-lock-keywords)
1824 (append
1825 (if org-noutline-p ; FIXME: I am not sure if eval will work
1826 ; on XEmacs if noutline is ever ported
1827 '((eval . (list "^\\(\\*+\\).*"
1828 0 '(nth
1829 (% (- (match-end 1) (match-beginning 1) 1)
1830 org-n-levels)
1831 org-level-faces)
1832 nil t)))
1833 '(("^\\(\\(\\*+\\)[^\r\n]*\\)[\n\r]"
1834 (1 (nth (% (- (match-end 2) (match-beginning 2) 1)
1835 org-n-levels)
1836 org-level-faces)
1837 nil t))))
1838 org-font-lock-extra-keywords))
1839 (set (make-local-variable 'font-lock-defaults)
1840 '(org-font-lock-keywords t nil nil backward-paragraph))
1841 (kill-local-variable 'font-lock-keywords) nil))
1842
1843 (defun org-unfontify-region (beg end &optional maybe_loudly)
1844 "Remove fontification and activation overlays from links."
1845 (font-lock-default-unfontify-region beg end)
1846 (let* ((buffer-undo-list t)
1847 (inhibit-read-only t) (inhibit-point-motion-hooks t)
1848 (inhibit-modification-hooks t)
1849 deactivate-mark buffer-file-name buffer-file-truename)
1850 (remove-text-properties beg end '(mouse-face nil keymap nil))))
1851
1852 ;;; Visibility cycling
1853
1854 (defvar org-cycle-global-status nil)
1855 (defvar org-cycle-subtree-status nil)
1856 (defun org-cycle (&optional arg)
1857 "Visibility cycling for Org-mode.
1858
1859 - When this function is called with a prefix argument, rotate the entire
1860 buffer through 3 states (global cycling)
1861 1. OVERVIEW: Show only top-level headlines.
1862 2. CONTENTS: Show all headlines of all levels, but no body text.
1863 3. SHOW ALL: Show everything.
1864
1865 - When point is at the beginning of a headline, rotate the subtree started
1866 by this line through 3 different states (local cycling)
1867 1. FOLDED: Only the main headline is shown.
1868 2. CHILDREN: The main headline and the direct children are shown. From
1869 this state, you can move to one of the children and
1870 zoom in further.
1871 3. SUBTREE: Show the entire subtree, including body text.
1872
1873 - When there is a numeric prefix, go ARG levels up and do a `show-subtree',
1874 keeping cursor position.
1875
1876 - When point is not at the beginning of a headline, execute
1877 `indent-relative', like TAB normally does. See the option
1878 `org-cycle-emulate-tab' for details.
1879
1880 - Special case: if point is the the beginning of the buffer and there is
1881 no headline in line 1, this function will act as if called with prefix arg."
1882 (interactive "P")
1883
1884 (if (or (and (bobp) (not (looking-at outline-regexp)))
1885 (equal arg '(4)))
1886 ;; special case: use global cycling
1887 (setq arg t))
1888
1889 (cond
1890
1891 ((org-at-table-p 'any)
1892 ;; Enter the table or move to the next field in the table
1893 (or (org-table-recognize-table.el)
1894 (progn
1895 (org-table-justify-field-maybe)
1896 (org-table-next-field))))
1897
1898 ((eq arg t) ;; Global cycling
1899
1900 (cond
1901 ((and (eq last-command this-command)
1902 (eq org-cycle-global-status 'overview))
1903 ;; We just created the overview - now do table of contents
1904 ;; This can be slow in very large buffers, so indicate action
1905 (message "CONTENTS...")
1906 (save-excursion
1907 ;; Visit all headings and show their offspring
1908 (goto-char (point-max))
1909 (catch 'exit
1910 (while (and (progn (condition-case nil
1911 (outline-previous-visible-heading 1)
1912 (error (goto-char (point-min))))
1913 t)
1914 (looking-at outline-regexp))
1915 (show-branches)
1916 (if (bobp) (throw 'exit nil))))
1917 (message "CONTENTS...done"))
1918 (setq org-cycle-global-status 'contents)
1919 (run-hook-with-args 'org-cycle-hook 'contents))
1920
1921 ((and (eq last-command this-command)
1922 (eq org-cycle-global-status 'contents))
1923 ;; We just showed the table of contents - now show everything
1924 (show-all)
1925 (message "SHOW ALL")
1926 (setq org-cycle-global-status 'all)
1927 (run-hook-with-args 'org-cycle-hook 'all))
1928
1929 (t
1930 ;; Default action: go to overview
1931 (hide-sublevels 1)
1932 (message "OVERVIEW")
1933 (setq org-cycle-global-status 'overview)
1934 (run-hook-with-args 'org-cycle-hook 'overview))))
1935
1936 ((integerp arg)
1937 ;; Show-subtree, ARG levels up from here.
1938 (save-excursion
1939 (org-back-to-heading)
1940 (outline-up-heading arg)
1941 (org-show-subtree)))
1942
1943 ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
1944 ;; At a heading: rotate between three different views
1945 (org-back-to-heading)
1946 (let ((goal-column 0) eoh eol eos)
1947 ;; First, some boundaries
1948 (save-excursion
1949 (org-back-to-heading)
1950 (save-excursion
1951 (beginning-of-line 2)
1952 (while (and (not (eobp)) ;; this is like `next-line'
1953 (get-char-property (1- (point)) 'invisible))
1954 (beginning-of-line 2)) (setq eol (point)))
1955 (outline-end-of-heading) (setq eoh (point))
1956 (outline-end-of-subtree) (setq eos (point))
1957 (outline-next-heading))
1958 ;; Find out what to do next and set `this-command'
1959 (cond
1960 ((= eos eoh)
1961 ;; Nothing is hidden behind this heading
1962 (message "EMPTY ENTRY")
1963 (setq org-cycle-subtree-status nil))
1964 ((>= eol eos)
1965 ;; Entire subtree is hidden in one line: open it
1966 (org-show-entry)
1967 (show-children)
1968 (message "CHILDREN")
1969 (setq org-cycle-subtree-status 'children)
1970 (run-hook-with-args 'org-cycle-hook 'children))
1971 ((and (eq last-command this-command)
1972 (eq org-cycle-subtree-status 'children))
1973 ;; We just showed the children, now show everything.
1974 (org-show-subtree)
1975 (message "SUBTREE")
1976 (setq org-cycle-subtree-status 'subtree)
1977 (run-hook-with-args 'org-cycle-hook 'subtree))
1978 (t
1979 ;; Default action: hide the subtree.
1980 (hide-subtree)
1981 (message "FOLDED")
1982 (setq org-cycle-subtree-status 'folded)
1983 (run-hook-with-args 'org-cycle-hook 'folded)))))
1984
1985 ;; TAB emulation
1986 (buffer-read-only (org-back-to-heading))
1987 ((if (and (eq org-cycle-emulate-tab 'white)
1988 (save-excursion (beginning-of-line 1) (looking-at "[ \t]+$")))
1989 t
1990 (eq org-cycle-emulate-tab t))
1991 (if (and (looking-at "[ \n\r\t]")
1992 (string-match "^[ \t]*$" (buffer-substring
1993 (point-at-bol) (point))))
1994 (progn
1995 (beginning-of-line 1)
1996 (and (looking-at "[ \t]+") (replace-match ""))))
1997 (indent-relative))
1998
1999 (t (save-excursion
2000 (org-back-to-heading)
2001 (org-cycle)))))
2002
2003 (defun org-optimize-window-after-visibility-change (state)
2004 "Adjust the window after a change in outline visibility.
2005 This function is the default value of the hook `org-cycle-hook'."
2006 (cond
2007 ((eq state 'overview) (org-first-headline-recenter 1))
2008 ((eq state 'content) nil)
2009 ((eq state 'all) nil)
2010 ((eq state 'folded) nil)
2011 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
2012 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1)))))
2013
2014 (defun org-subtree-end-visible-p ()
2015 "Is the end of the current subtree visible?"
2016 (pos-visible-in-window-p
2017 (save-excursion (outline-end-of-subtree) (point))))
2018
2019 (defun org-first-headline-recenter (&optional N)
2020 "Move cursor to the first headline and recenter the headline.
2021 Optional argument N means, put the headline into the Nth line of the window."
2022 (goto-char (point-min))
2023 (when (re-search-forward (concat "^" outline-regexp) nil t)
2024 (beginning-of-line)
2025 (recenter (prefix-numeric-value N))))
2026
2027 (defvar org-goto-window-configuration nil)
2028 (defvar org-goto-marker nil)
2029 (defvar org-goto-map (make-sparse-keymap))
2030 (let ((cmds '(isearch-forward isearch-backward)) cmd)
2031 (while (setq cmd (pop cmds))
2032 (substitute-key-definition cmd cmd org-goto-map global-map)))
2033 (define-key org-goto-map "\C-m" 'org-goto-ret)
2034 (define-key org-goto-map [(left)] 'org-goto-left)
2035 (define-key org-goto-map [(right)] 'org-goto-right)
2036 (define-key org-goto-map [(?q)] 'org-goto-quit)
2037 (define-key org-goto-map [(control ?g)] 'org-goto-quit)
2038 (define-key org-goto-map "\C-i" 'org-cycle)
2039 (define-key org-goto-map [(down)] 'outline-next-visible-heading)
2040 (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
2041 (define-key org-goto-map "n" 'outline-next-visible-heading)
2042 (define-key org-goto-map "p" 'outline-previous-visible-heading)
2043 (define-key org-goto-map "f" 'outline-forward-same-level)
2044 (define-key org-goto-map "b" 'outline-backward-same-level)
2045 (define-key org-goto-map "u" 'outline-up-heading)
2046 (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
2047 (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
2048 (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
2049 (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
2050 (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
2051 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2052 (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
2053
2054 (defconst org-goto-help
2055 "Select a location to jump to, press RET
2056 \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
2057
2058 (defun org-goto ()
2059 "Go to a different location of the document, keeping current visibility.
2060
2061 When you want to go to a different location in a document, the fastest way
2062 is often to fold the entire buffer and then dive into the tree. This
2063 method has the disadvantage, that the previous location will be folded,
2064 which may not be what you want.
2065
2066 This command works around this by showing a copy of the current buffer in
2067 overview mode. You can dive into the tree in that copy, to find the
2068 location you want to reach. When pressing RET, the command returns to the
2069 original buffer in which the visibility is still unchanged. It then jumps
2070 to the new location, making it and the headline hierarchy above it visible."
2071 (interactive)
2072 (let* ((org-goto-start-pos (point))
2073 (selected-point
2074 (org-get-location (current-buffer) org-goto-help)))
2075 (if selected-point
2076 (progn
2077 (goto-char selected-point)
2078 (if (org-invisible-p) (org-show-hierarchy-above)))
2079 (error "Quit"))))
2080
2081 (defun org-get-location (buf help)
2082 "Let the user select a location in the Org-mode buffer BUF.
2083 This function uses a recursive edit. It returns the selected position
2084 or nil."
2085 (let (org-selected-point)
2086 (save-excursion
2087 (save-window-excursion
2088 (delete-other-windows)
2089 (switch-to-buffer (get-buffer-create "*org-goto*"))
2090 (with-output-to-temp-buffer "*Help*"
2091 (princ help))
2092 (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
2093 (setq buffer-read-only nil)
2094 (erase-buffer)
2095 (insert-buffer buf)
2096 (let ((org-startup-truncated t)
2097 (org-startup-folded t)
2098 (org-startup-with-deadline-check nil))
2099 (org-mode))
2100 (setq buffer-read-only t)
2101 (if (boundp 'org-goto-start-pos)
2102 (goto-char org-goto-start-pos)
2103 (goto-char (point-min)))
2104 (org-beginning-of-line)
2105 (message "Select location and press RET")
2106 ;; now we make sure that during selection, ony very few keys work
2107 ;; and that it is impossible to switch to another window.
2108 (let ((gm (current-global-map))
2109 (overriding-local-map org-goto-map))
2110 (unwind-protect
2111 (progn
2112 (use-global-map org-goto-map)
2113 (recursive-edit))
2114 (use-global-map gm)))))
2115 (kill-buffer "*org-goto*")
2116 org-selected-point))
2117
2118 ;; FIXME: It may not be a good idea to temper with the prefix argument...
2119 (defun org-goto-ret (&optional arg)
2120 "Finish org-goto by going to the new location."
2121 (interactive "P")
2122 (setq org-selected-point (point)
2123 current-prefix-arg arg)
2124 (throw 'exit nil))
2125
2126 (defun org-goto-left ()
2127 "Finish org-goto by going to the new location."
2128 (interactive)
2129 (if (org-on-heading-p)
2130 (progn
2131 (beginning-of-line 1)
2132 (setq org-selected-point (point)
2133 current-prefix-arg (- (match-end 0) (match-beginning 0)))
2134 (throw 'exit nil))
2135 (error "Not on a heading")))
2136
2137 (defun org-goto-right ()
2138 "Finish org-goto by going to the new location."
2139 (interactive)
2140 (if (org-on-heading-p)
2141 (progn
2142 (outline-end-of-subtree)
2143 (or (eobp) (forward-char 1))
2144 (setq org-selected-point (point)
2145 current-prefix-arg (- (match-end 0) (match-beginning 0)))
2146 (throw 'exit nil))
2147 (error "Not on a heading")))
2148
2149 (defun org-goto-quit ()
2150 "Finish org-goto without cursor motion."
2151 (interactive)
2152 (setq org-selected-point nil)
2153 (throw 'exit nil))
2154
2155 ;;; Promotion, Demotion, Inserting new headlines
2156
2157 (defvar org-ignore-region nil
2158 "To temporarily disable the active region.")
2159
2160 (defun org-insert-heading ()
2161 "Insert a new heading with same depth at point."
2162 (interactive)
2163 (let* ((head (save-excursion
2164 (condition-case nil
2165 (org-back-to-heading)
2166 (error (outline-next-heading)))
2167 (prog1 (match-string 0)
2168 (funcall outline-level)))))
2169 (unless (bolp) (newline))
2170 (insert head)
2171 (unless (eolp)
2172 (save-excursion (newline-and-indent)))
2173 (unless (equal (char-before) ?\ )
2174 (insert " "))
2175 (run-hooks 'org-insert-heading-hook)))
2176
2177 (defun org-insert-todo-heading (arg)
2178 "Insert a new heading with the same level and TODO state as current heading.
2179 If the heading has no TODO state, or if the state is DONE, use the first
2180 state (TODO by default). Also with prefix arg, force first state."
2181 (interactive "P")
2182 (org-insert-heading)
2183 (save-excursion
2184 (org-back-to-heading)
2185 (outline-previous-heading)
2186 (looking-at org-todo-line-regexp))
2187 (if (or arg
2188 (not (match-beginning 2))
2189 (equal (match-string 2) org-done-string))
2190 (insert (car org-todo-keywords) " ")
2191 (insert (match-string 2) " ")))
2192
2193 (defun org-promote-subtree ()
2194 "Promote the entire subtree.
2195 See also `org-promote'."
2196 (interactive)
2197 (save-excursion
2198 (org-map-tree 'org-promote)))
2199
2200 (defun org-demote-subtree ()
2201 "Demote the entire subtree. See `org-demote'.
2202 See also `org-promote'."
2203 (interactive)
2204 (save-excursion
2205 (org-map-tree 'org-demote)))
2206
2207 (defun org-do-promote ()
2208 "Promote the current heading higher up the tree.
2209 If the region is active in `transient-mark-mode', promote all headings
2210 in the region."
2211 (interactive)
2212 (save-excursion
2213 (if (org-region-active-p)
2214 (org-map-region 'org-promote (region-beginning) (region-end))
2215 (org-promote)))
2216 (org-fix-position-after-promote))
2217
2218 (defun org-do-demote ()
2219 "Demote the current heading lower down the tree.
2220 If the region is active in `transient-mark-mode', demote all headings
2221 in the region."
2222 (interactive)
2223 (save-excursion
2224 (if (org-region-active-p)
2225 (org-map-region 'org-demote (region-beginning) (region-end))
2226 (org-demote)))
2227 (org-fix-position-after-promote))
2228
2229 (defun org-fix-position-after-promote ()
2230 "Make sure that after pro/demotion cursor position is right."
2231 (and (equal (char-after) ?\ )
2232 (equal (char-before) ?*)
2233 (forward-char 1)))
2234
2235 (defun org-promote ()
2236 "Promote the current heading higher up the tree.
2237 If the region is active in `transient-mark-mode', promote all headings
2238 in the region."
2239 (org-back-to-heading t)
2240 (let* ((level (save-match-data (funcall outline-level)))
2241 (up-head (make-string (1- level) ?*)))
2242 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
2243 (replace-match up-head nil t)
2244 (if org-adapt-indentation
2245 (org-fixup-indentation "^ " "" "^ ?\\S-"))))
2246
2247 (defun org-demote ()
2248 "Demote the current heading lower down the tree.
2249 If the region is active in `transient-mark-mode', demote all headings
2250 in the region."
2251 (org-back-to-heading t)
2252 (let* ((level (save-match-data (funcall outline-level)))
2253 (down-head (make-string (1+ level) ?*)))
2254 (replace-match down-head nil t)
2255 (if org-adapt-indentation
2256 (org-fixup-indentation "^ " " " "^\\S-"))))
2257
2258 (defun org-map-tree (fun)
2259 "Call FUN for every heading underneath the current one."
2260 (org-back-to-heading)
2261 (let ((level (outline-level)))
2262 (save-excursion
2263 (funcall fun)
2264 (while (and (progn
2265 (outline-next-heading)
2266 (> (funcall outline-level) level))
2267 (not (eobp)))
2268 (funcall fun)))))
2269
2270 (defun org-map-region (fun beg end)
2271 "Call FUN for every heading between BEG and END."
2272 (let ((org-ignore-region t))
2273 (save-excursion
2274 (setq end (copy-marker end))
2275 (goto-char beg)
2276 ;; (if (fboundp 'deactivate-mark) (deactivate-mark))
2277 ;; (if (fboundp 'zmacs-deactivate-region) (zmacs-deactivate-region))
2278 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
2279 (< (point) end))
2280 (funcall fun))
2281 (while (and (progn
2282 (outline-next-heading)
2283 (< (point) end))
2284 (not (eobp)))
2285 (funcall fun)))))
2286
2287 (defun org-fixup-indentation (from to prohibit)
2288 "Change the indentation in the current entry by re-replacing FROM with TO.
2289 However, if the regexp PROHIBIT matches at all, don't do anything.
2290 This is being used to change indentation along with the length of the
2291 heading marker. But if there are any lines which are not indented, nothing
2292 is changed at all."
2293 (save-excursion
2294 (let ((end (save-excursion (outline-next-heading)
2295 (point-marker))))
2296 (unless (save-excursion (re-search-forward prohibit end t))
2297 (while (re-search-forward from end t)
2298 (replace-match to)
2299 (beginning-of-line 2)))
2300 (move-marker end nil))))
2301
2302 ;;; Vertical tree motion, cutting and pasting of subtrees
2303
2304 (defun org-move-subtree-up (&optional arg)
2305 "Move the current subtree up past ARG headlines of the same level."
2306 (interactive "p")
2307 (org-move-subtree-down (- (prefix-numeric-value arg))))
2308
2309 (defun org-move-subtree-down (&optional arg)
2310 "Move the current subtree down past ARG headlines of the same level."
2311 (interactive "p")
2312 (setq arg (prefix-numeric-value arg))
2313 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
2314 'outline-get-last-sibling))
2315 (ins-point (make-marker))
2316 (cnt (abs arg))
2317 beg end txt folded)
2318 ;; Select the tree
2319 (org-back-to-heading)
2320 (setq beg (point))
2321 (save-match-data
2322 (save-excursion (outline-end-of-heading)
2323 (setq folded (org-invisible-p)))
2324 (outline-end-of-subtree))
2325 (outline-next-heading)
2326 (setq end (point))
2327 ;; Find insertion point, with error handling
2328 (goto-char beg)
2329 (while (> cnt 0)
2330 (or (and (funcall movfunc) (looking-at outline-regexp))
2331 (progn (goto-char beg)
2332 (error "Cannot move past superior level or buffer limit")))
2333 (setq cnt (1- cnt)))
2334 (if (> arg 0)
2335 ;; Moving forward - still need to move over subtree
2336 (progn (outline-end-of-subtree)
2337 (outline-next-heading)
2338 (if (not (or (looking-at (concat "^" outline-regexp))
2339 (bolp)))
2340 (newline))))
2341 (move-marker ins-point (point))
2342 (setq txt (buffer-substring beg end))
2343 (delete-region beg end)
2344 (insert txt)
2345 (goto-char ins-point)
2346 (if folded (hide-subtree))
2347 (move-marker ins-point nil)))
2348
2349 (defvar org-subtree-clip ""
2350 "Clipboard for cut and paste of subtrees.
2351 This is actually only a copy of the kill, because we use the normal kill
2352 ring. We need it to check if the kill was created by `org-copy-subtree'.")
2353
2354 (defvar org-subtree-clip-folded nil
2355 "Was the last copied subtree folded?
2356 This is used to fold the tree back after pasting.")
2357
2358 (defun org-cut-subtree ()
2359 "Cut the current subtree into the clipboard.
2360 This is a short-hand for marking the subtree and then cutting it."
2361 (interactive)
2362 (org-copy-subtree 'cut))
2363
2364 (defun org-copy-subtree (&optional cut)
2365 "Cut the current subtree into the clipboard.
2366 This is a short-hand for marking the subtree and then copying it.
2367 If CUT is non nil, actually cut the subtree."
2368 (interactive)
2369 (let (beg end folded)
2370 (org-back-to-heading)
2371 (setq beg (point))
2372 (save-match-data
2373 (save-excursion (outline-end-of-heading)
2374 (setq folded (org-invisible-p)))
2375 (outline-end-of-subtree))
2376 (if (equal (char-after) ?\n) (forward-char 1))
2377 (setq end (point))
2378 (goto-char beg)
2379 (when (> end beg)
2380 (setq org-subtree-clip-folded folded)
2381 (if cut (kill-region beg end) (copy-region-as-kill beg end))
2382 (setq org-subtree-clip (current-kill 0))
2383 (message "%s: Subtree with %d characters"
2384 (if cut "Cut" "Copied")
2385 (length org-subtree-clip)))))
2386
2387 (defun org-paste-subtree (&optional level tree)
2388 "Paste the clipboard as a subtree, with modification of headline level.
2389 The entire subtree is promoted or demoted in order to match a new headline
2390 level. By default, the new level is derived from the visible headings
2391 before and after the insertion point, and taken to be the inferior headline
2392 level of the two. So if the previous visible heading is level 3 and the
2393 next is level 4 (or vice versa), level 4 will be used for insertion.
2394 This makes sure that the subtree remains an independent subtree and does
2395 not swallow low level entries.
2396
2397 You can also force a different level, either by using a numeric prefix
2398 argument, or by inserting the heading marker by hand. For example, if the
2399 cursor is after \"*****\", then the tree will be shifted to level 5.
2400
2401 If you want to insert the tree as is, just use \\[yank].
2402
2403 If optional TREE is given, use this text instead of the kill ring."
2404 (interactive "P")
2405 (unless (org-kill-is-subtree-p tree)
2406 (error
2407 (substitute-command-keys
2408 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
2409 (let* ((txt (or tree (current-kill 0)))
2410 (^re (concat "^\\(" outline-regexp "\\)"))
2411 (re (concat "\\(" outline-regexp "\\)"))
2412 (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
2413
2414 (old-level (if (string-match ^re txt)
2415 (- (match-end 0) (match-beginning 0))
2416 -1))
2417 (force-level (cond (level (prefix-numeric-value level))
2418 ((string-match
2419 ^re_ (buffer-substring (point-at-bol) (point)))
2420 (- (match-end 0) (match-beginning 0)))
2421 (t nil)))
2422 (previous-level (save-excursion
2423 (condition-case nil
2424 (progn
2425 (outline-previous-visible-heading 1)
2426 (if (looking-at re)
2427 (- (match-end 0) (match-beginning 0))
2428 1))
2429 (error 1))))
2430 (next-level (save-excursion
2431 (condition-case nil
2432 (progn
2433 (outline-next-visible-heading 1)
2434 (if (looking-at re)
2435 (- (match-end 0) (match-beginning 0))
2436 1))
2437 (error 1))))
2438 (new-level (or force-level (max previous-level next-level)))
2439 (shift (if (or (= old-level -1)
2440 (= new-level -1)
2441 (= old-level new-level))
2442 0
2443 (- new-level old-level)))
2444 (shift1 shift)
2445 (delta (if (> shift 0) -1 1))
2446 (func (if (> shift 0) 'org-demote 'org-promote))
2447 beg end)
2448 ;; Remove the forces level indicator
2449 (if force-level
2450 (delete-region (point-at-bol) (point)))
2451 ;; Make sure we start at the beginning of an empty line
2452 (if (not (bolp)) (insert "\n"))
2453 (if (not (looking-at "[ \t]*$"))
2454 (progn (insert "\n") (backward-char 1)))
2455 ;; Paste
2456 (setq beg (point))
2457 (insert txt)
2458 (setq end (point))
2459 (goto-char beg)
2460 ;; Shift if necessary
2461 (if (= shift 0)
2462 (message "Pasted at level %d, without shift" new-level)
2463 (save-restriction
2464 (narrow-to-region beg end)
2465 (while (not (= shift 0))
2466 (org-map-region func (point-min) (point-max))
2467 (setq shift (+ delta shift)))
2468 (goto-char (point-min))
2469 (message "Pasted at level %d, with shift by %d levels"
2470 new-level shift1)))
2471 (if (and (eq org-subtree-clip (current-kill 0))
2472 org-subtree-clip-folded)
2473 ;; The tree was folded before it was killed/copied
2474 (hide-subtree))))
2475
2476 (defun org-kill-is-subtree-p (&optional txt)
2477 "Check if the current kill is an outline subtree, or a set of trees.
2478 Returns nil if kill does not start with a headline, or if the first
2479 headline level is not the largest headline level in the tree.
2480 So this will actually accept several entries of equal levels as well,
2481 which is OK for `org-paste-subtree'.
2482 If optional TXT is given, check this string instead of the current kill."
2483 (let* ((kill (or txt (current-kill 0) ""))
2484 (start-level (and (string-match (concat "\\`" outline-regexp) kill)
2485 (- (match-end 0) (match-beginning 0))))
2486 (re (concat "^" outline-regexp))
2487 (start 1))
2488 (if (not start-level)
2489 nil ;; does not even start with a heading
2490 (catch 'exit
2491 (while (setq start (string-match re kill (1+ start)))
2492 (if (< (- (match-end 0) (match-beginning 0)) start-level)
2493 (throw 'exit nil)))
2494 t))))
2495
2496 (defun org-archive-subtree ()
2497 "Move the current subtree to the archive.
2498 The archive can be a certain top-level heading in the current file, or in
2499 a different file. The tree will be moved to that location, the subtree
2500 heading be marked DONE, and the current time will be added."
2501 (interactive)
2502 ;; Save all relevant TODO keyword-relatex variables
2503 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
2504 (tr-org-todo-keywords org-todo-keywords)
2505 (tr-org-todo-interpretation org-todo-interpretation)
2506 (tr-org-done-string org-done-string)
2507 (tr-org-todo-regexp org-todo-regexp)
2508 (tr-org-todo-line-regexp org-todo-line-regexp)
2509 (this-buffer (current-buffer))
2510 file heading buffer level newfile-p)
2511 (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
2512 (progn
2513 (setq file (format (match-string 1 org-archive-location)
2514 (file-name-nondirectory (buffer-file-name)))
2515 heading (match-string 2 org-archive-location)))
2516 (error "Invalid `org-archive-location'"))
2517 (if (> (length file) 0)
2518 (setq newfile-p (not (file-exists-p file))
2519 buffer (find-file-noselect file))
2520 (setq buffer (current-buffer)))
2521 (unless buffer
2522 (error "Cannot access file \"%s\"" file))
2523 (if (and (> (length heading) 0)
2524 (string-match "^\\*+" heading))
2525 (setq level (match-end 0))
2526 (setq heading nil level 0))
2527 (save-excursion
2528 (org-copy-subtree) ; We first only copy, in case something goes wrong
2529 (set-buffer buffer)
2530 ;; Enforce org-mode for the archive buffer
2531 (if (not (eq major-mode 'org-mode))
2532 ;; Force the mode for future visits.
2533 (let ((org-insert-mode-line-in-empty-file t))
2534 (call-interactively 'org-mode)))
2535 (when newfile-p
2536 (goto-char (point-max))
2537 (insert (format "\nArchived entries from file %s\n\n"
2538 (buffer-file-name this-buffer))))
2539 ;; Force the TODO keywords of the original buffer
2540 (let ((org-todo-line-regexp tr-org-todo-line-regexp)
2541 (org-todo-keywords tr-org-todo-keywords)
2542 (org-todo-interpretation tr-org-todo-interpretation)
2543 (org-done-string tr-org-done-string)
2544 (org-todo-regexp tr-org-todo-regexp)
2545 (org-todo-line-regexp tr-org-todo-line-regexp))
2546 (goto-char (point-min))
2547 (if heading
2548 (progn
2549 (if (re-search-forward
2550 (concat "\\(^\\|\r\\)"
2551 (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
2552 nil t)
2553 (goto-char (match-end 0))
2554 ;; Heading not found, just insert it at the end
2555 (goto-char (point-max))
2556 (or (bolp) (insert "\n"))
2557 (insert "\n" heading "\n")
2558 (end-of-line 0))
2559 ;; Make the heading visible, and the following as well
2560 (let ((org-show-following-heading t)) (org-show-hierarchy-above))
2561 (if (re-search-forward
2562 (concat "^" (regexp-quote (make-string level ?*)) "[ \t]")
2563 nil t)
2564 (progn (goto-char (match-beginning 0)) (insert "\n")
2565 (beginning-of-line 0))
2566 (goto-char (point-max)) (insert "\n")))
2567 (goto-char (point-max)) (insert "\n"))
2568 ;; Paste
2569 (org-paste-subtree (1+ level))
2570 ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
2571 (if org-archive-mark-done
2572 (org-todo (length org-todo-keywords)))
2573 ;; Move cursor to right after the TODO keyword
2574 (when org-archive-stamp-time
2575 (beginning-of-line 1)
2576 (looking-at org-todo-line-regexp)
2577 (goto-char (or (match-end 2) (match-beginning 3)))
2578 (insert "(" (format-time-string (cdr org-time-stamp-formats)
2579 (current-time))
2580 ")"))
2581 ;; Save the buffer, if it is not the same buffer.
2582 (if (not (eq this-buffer buffer)) (save-buffer))))
2583 ;; Here we are back in the original buffer. Everything seems to have
2584 ;; worked. So now cut the tree and finish up.
2585 (org-cut-subtree)
2586 (if (looking-at "[ \t]*$") (kill-line))
2587 (message "Subtree archived %s"
2588 (if (eq this-buffer buffer)
2589 (concat "under heading: " heading)
2590 (concat "in file: " (abbreviate-file-name file))))))
2591
2592 ;;; Completion
2593
2594 (defun org-complete (&optional arg)
2595 "Perform completion on word at point.
2596 At the beginning of a headline, this completes TODO keywords as given in
2597 `org-todo-keywords'.
2598 If the current word is preceded by a backslash, completes the TeX symbols
2599 that are supported for HTML support.
2600 If the current word is preceded by \"#+\", completes special words for
2601 setting file options.
2602 At all other locations, this simply calls `ispell-complete-word'."
2603 (interactive "P")
2604 (catch 'exit
2605 (let* ((end (point))
2606 (beg (save-excursion
2607 (if (equal (char-before (point)) ?\ ) (backward-char 1))
2608 (skip-chars-backward "a-zA-Z0-9_:")
2609 (point)))
2610 (texp (equal (char-before beg) ?\\))
2611 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
2612 beg)
2613 "#+"))
2614 (pattern (buffer-substring-no-properties beg end))
2615 (completion-ignore-case opt)
2616 (type nil)
2617 (table (cond
2618 (opt
2619 (setq type :opt)
2620 (mapcar (lambda (x)
2621 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
2622 (cons (match-string 2 x) (match-string 1 x)))
2623 (org-split-string (org-get-current-options) "\n")))
2624 (texp
2625 (setq type :tex)
2626 org-html-entities)
2627 ((string-match "\\`\\*+[ \t]*\\'"
2628 (buffer-substring (point-at-bol) beg))
2629 (setq type :todo)
2630 (mapcar 'list org-todo-keywords))
2631 (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
2632 (completion (try-completion pattern table)))
2633 (cond ((eq completion t)
2634 (if (equal type :opt)
2635 (insert (substring (cdr (assoc (upcase pattern) table))
2636 (length pattern)))))
2637 ((null completion)
2638 (message "Can't find completion for \"%s\"" pattern)
2639 (ding))
2640 ((not (string= pattern completion))
2641 (delete-region beg end)
2642 (if (string-match " +$" completion)
2643 (setq completion (replace-match "" t t completion)))
2644 (insert completion)
2645 (if (get-buffer-window "*Completions*")
2646 (delete-window (get-buffer-window "*Completions*")))
2647 (if (and (eq type :todo)
2648 (assoc completion table))
2649 (insert " "))
2650 (if (and (equal type :opt) (assoc completion table))
2651 (message (substitute-command-keys
2652 "Press \\[org-complete] again to insert example settings"))))
2653 (t
2654 (message "Making completion list...")
2655 (let ((list (sort (all-completions pattern table) 'string<)))
2656 (with-output-to-temp-buffer "*Completions*"
2657 (display-completion-list list)))
2658 (message "Making completion list...%s" "done"))))))
2659
2660 ;;; Comments, TODO and DEADLINE
2661
2662 (defun org-toggle-comment ()
2663 "Change the COMMENT state of an entry."
2664 (interactive)
2665 (save-excursion
2666 (org-back-to-heading)
2667 (if (looking-at (concat outline-regexp
2668 "\\( +\\<" org-comment-string "\\>\\)"))
2669 (replace-match "" t t nil 1)
2670 (if (looking-at outline-regexp)
2671 (progn
2672 (goto-char (match-end 0))
2673 (insert " " org-comment-string))))))
2674
2675 (defvar org-last-todo-state-is-todo nil
2676 "This is non-nil when the last TODO state change led to a TODO state.
2677 If the last change removed the TODO tag or switched to DONE, then
2678 this is nil.")
2679
2680 (defun org-todo (&optional arg)
2681 "Change the TODO state of an item.
2682 The state of an item is given by a keyword at the start of the heading,
2683 like
2684 *** TODO Write paper
2685 *** DONE Call mom
2686
2687 The different keywords are specified in the variable `org-todo-keywords'. By
2688 default the available states are \"TODO\" and \"DONE\".
2689 So for this example: when the item starts with TODO, it is changed to DONE.
2690 When it starts with DONE, the DONE is removed. And when neither TODO nor
2691 DONE are present, add TODO at the beginning of the heading.
2692
2693 With prefix arg, use completion to determined the new state. With numeric
2694 prefix arg, switch to that state."
2695 (interactive "P")
2696 (save-excursion
2697 (org-back-to-heading)
2698 (if (looking-at outline-regexp) (goto-char (match-end 0)))
2699 (or (looking-at (concat " +" org-todo-regexp " *"))
2700 (looking-at " *"))
2701 (let* ((this (match-string 1))
2702 (completion-ignore-case t)
2703 (member (member this org-todo-keywords))
2704 (tail (cdr member))
2705 (state (cond
2706 ((equal arg '(4))
2707 ;; Read a state with completion
2708 (completing-read "State: " (mapcar (lambda(x) (list x))
2709 org-todo-keywords)
2710 nil t))
2711 (arg
2712 ;; user requests a specific state
2713 (nth (1- (prefix-numeric-value arg))
2714 org-todo-keywords))
2715 ((null member) (car org-todo-keywords))
2716 ((null tail) nil) ;; -> first entry
2717 ((eq org-todo-interpretation 'sequence)
2718 (car tail))
2719 ((memq org-todo-interpretation '(type priority))
2720 (if (eq this-command last-command)
2721 (car tail)
2722 (if (> (length tail) 0) org-done-string nil)))
2723 (t nil)))
2724 (next (if state (concat " " state " ") " ")))
2725 (replace-match next t t)
2726 (setq org-last-todo-state-is-todo
2727 (not (equal state org-done-string)))
2728 (run-hooks 'org-after-todo-state-change-hook)))
2729 ;; Fixup cursor location if close to the keyword
2730 (if (and (outline-on-heading-p)
2731 (not (bolp))
2732 (save-excursion (beginning-of-line 1)
2733 (looking-at org-todo-line-regexp))
2734 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
2735 (progn
2736 (goto-char (or (match-end 2) (match-end 1)))
2737 (just-one-space))))
2738
2739 (defun org-show-todo-tree (arg)
2740 "Make a compact tree which shows all headlines marked with TODO.
2741 The tree will show the lines where the regexp matches, and all higher
2742 headlines above the match."
2743 (interactive "P")
2744 (let ((case-fold-search nil)
2745 (kwd-re (if arg org-todo-regexp org-not-done-regexp)))
2746 (message "%d TODO entries found"
2747 (org-occur (concat "^" outline-regexp " +" kwd-re )))))
2748
2749 (defun org-deadline ()
2750 "Insert the DEADLINE: string to make a deadline.
2751 A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
2752 to modify it to the correct date."
2753 (interactive)
2754 (insert
2755 org-deadline-string " "
2756 (format-time-string (car org-time-stamp-formats)
2757 (org-read-date nil 'to-time)))
2758 (message (substitute-command-keys
2759 "Use \\[org-timestamp-up-day] and \\[org-timestamp-down-day] to change the date.")))
2760
2761 (defun org-schedule ()
2762 "Insert the SCHEDULED: string to schedule a TODO item.
2763 A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
2764 to modify it to the correct date."
2765 (interactive)
2766 (insert
2767 org-scheduled-string " "
2768 (format-time-string (car org-time-stamp-formats)
2769 (org-read-date nil 'to-time)))
2770 (message (substitute-command-keys
2771 "Use \\[org-timestamp-up-day] and \\[org-timestamp-down-day] to change the date.")))
2772
2773
2774 (defun org-occur (regexp &optional callback)
2775 "Make a compact tree which shows all matches of REGEXP.
2776 The tree will show the lines where the regexp matches, and all higher
2777 headlines above the match. It will also show the heading after the match,
2778 to make sure editing the matching entry is easy.
2779 if CALLBACK is non-nil, it is a function which is called to confirm
2780 that the match should indeed be shown."
2781 (interactive "sRegexp: ")
2782 (setq regexp (org-check-occur-regexp regexp))
2783 (let ((cnt 0))
2784 (save-excursion
2785 (goto-char (point-min))
2786 (hide-sublevels 1)
2787 (while (re-search-forward regexp nil t)
2788 (when (or (not callback)
2789 (funcall callback))
2790 (setq cnt (1+ cnt))
2791 (org-show-hierarchy-above))))
2792 (run-hooks 'org-occur-hook)
2793 (if (interactive-p)
2794 (message "%d match(es) for regexp %s" cnt regexp))
2795 cnt))
2796
2797 (defun org-show-hierarchy-above ()
2798 "Make sure point and the headings hierarchy above is visible."
2799 (if (org-on-heading-p t)
2800 (org-flag-heading nil) ; only show the heading
2801 (org-show-hidden-entry)) ; show entire entry
2802 (save-excursion
2803 (and org-show-following-heading
2804 (outline-next-heading)
2805 (org-flag-heading nil))) ; show the next heading
2806 (save-excursion ; show all higher headings
2807 (while (condition-case nil
2808 (progn (org-up-heading-all 1) t)
2809 (error nil))
2810 (org-flag-heading nil))))
2811
2812 ;;; Priorities
2813
2814 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
2815 "Regular expression matching the priority indicator.")
2816
2817 (defvar org-remove-priority-next-time nil)
2818
2819 (defun org-priority-up ()
2820 "Increase the priority of the current item."
2821 (interactive)
2822 (org-priority 'up))
2823
2824 (defun org-priority-down ()
2825 "Decrease the priority of the current item."
2826 (interactive)
2827 (org-priority 'down))
2828
2829 (defun org-priority (&optional action)
2830 "Change the priority of an item by ARG.
2831 ACTION can be set, up, or down."
2832 (interactive)
2833 (setq action (or action 'set))
2834 (let (current new news have remove)
2835 (save-excursion
2836 (org-back-to-heading)
2837 (if (looking-at org-priority-regexp)
2838 (setq current (string-to-char (match-string 2))
2839 have t)
2840 (setq current org-default-priority))
2841 (cond
2842 ((eq action 'set)
2843 (message (format "Priority A-%c, SPC to remove: " org-lowest-priority))
2844 (setq new (read-char-exclusive))
2845 (cond ((equal new ?\ ) (setq remove t))
2846 ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
2847 (error "Priority must be between `%c' and `%c'"
2848 ?A org-lowest-priority))))
2849 ((eq action 'up)
2850 (setq new (1- current)))
2851 ((eq action 'down)
2852 (setq new (1+ current)))
2853 (t (error "Invalid action")))
2854 (setq new (min (max ?A (upcase new)) org-lowest-priority))
2855 (setq news (format "%c" new))
2856 (if have
2857 (if remove
2858 (replace-match "" t t nil 1)
2859 (replace-match news t t nil 2))
2860 (if remove
2861 (error "No priority cookie found in line")
2862 (looking-at org-todo-line-regexp)
2863 (if (match-end 2)
2864 (progn
2865 (goto-char (match-end 2))
2866 (insert " [#" news "]"))
2867 (goto-char (match-beginning 3))
2868 (insert "[#" news "] ")))))
2869 (if remove
2870 (message "Priority removed")
2871 (message "Priority of current item set to %s" news))))
2872
2873
2874 (defun org-get-priority (s)
2875 "Find priority cookie and return priority."
2876 (save-match-data
2877 (if (not (string-match org-priority-regexp s))
2878 (* 1000 (- org-lowest-priority org-default-priority))
2879 (* 1000 (- org-lowest-priority
2880 (string-to-char (match-string 2 s)))))))
2881
2882 ;;; Timestamps
2883
2884 (defvar org-last-changed-timestamp nil)
2885
2886 (defun org-time-stamp (arg)
2887 "Prompt for a date/time and insert a time stamp.
2888 If the user specifies a time like HH:MM, or if this command is called
2889 with a prefix argument, the time stamp will contain date and time.
2890 Otherwise, only the date will be included. All parts of a date not
2891 specified by the user will be filled in from the current date/time.
2892 So if you press just return without typing anything, the time stamp
2893 will represent the current date/time. If there is already a timestamp
2894 at the cursor, it will be modified."
2895 (interactive "P")
2896 (let ((fmt (if arg (cdr org-time-stamp-formats)
2897 (car org-time-stamp-formats)))
2898 (org-time-was-given nil)
2899 time)
2900 (cond
2901 ((and (org-at-timestamp-p)
2902 (eq last-command 'org-time-stamp)
2903 (eq this-command 'org-time-stamp))
2904 (insert "--")
2905 (setq time (let ((this-command this-command))
2906 (org-read-date arg 'totime)))
2907 (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
2908 (insert (format-time-string fmt time)))
2909 ((org-at-timestamp-p)
2910 (setq time (let ((this-command this-command))
2911 (org-read-date arg 'totime)))
2912 (and (org-at-timestamp-p) (replace-match
2913 (setq org-last-changed-timestamp
2914 (format-time-string fmt time))
2915 t t))
2916 (message "Timestamp updated"))
2917 (t
2918 (setq time (let ((this-command this-command))
2919 (org-read-date arg 'totime)))
2920 (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
2921 (insert (format-time-string fmt time))))))
2922
2923 ;;; FIXME: Make the function take "Fri" as "next friday"
2924 (defun org-read-date (&optional with-time to-time)
2925 "Read a date and make things smooth for the user.
2926 The prompt will suggest to enter an ISO date, but you can also enter anything
2927 which will at least partially be understood by `parse-time-string'.
2928 Unrecognized parts of the date will default to the current day, month ,year,
2929 hour and minute. For example,
2930 3-2-5 --> 2003-02-05
2931 feb 15 --> currentyear-02-15
2932 sep 12 9 --> 2009-09-12
2933 12:45 --> today 12:45
2934 22 sept 0:34 --> currentyear-09-22 0:34
2935 12 --> currentyear-currentmonth-12
2936 etc.
2937 The function understands only English month and weekday abbreviations,
2938 but this can be configured with the variables `parse-time-months' and
2939 `parse-time-weekdays'.
2940
2941 While prompting, a calendar is popped up - you can also select the
2942 date with the mouse (button 1). The calendar shows a period of three
2943 month. To scroll it to other months, use the keys `>' and `<'.
2944 If you don't like the calendar, turn it off with
2945 \(setq org-popup-calendar-for-date-prompt nil).
2946
2947 With optional argument TO-TIME, the date will immediately be converted
2948 to an internal time.
2949 With an optional argument WITH-TIME, the prompt will suggest to also
2950 insert a time. Note that when WITH-TIME is not set, you can still
2951 enter a time, and this function will inform the calling routine about
2952 this change. The calling routine may then choose to change the format
2953 used to insert the time stamp into the buffer to include the time."
2954 (let* ((default-time
2955 ;; Default time is either today, or, when entering a range,
2956 ;; the range start.
2957 (if (save-excursion
2958 (re-search-backward
2959 (concat org-ts-regexp "--\\=")
2960 (- (point) 20) t))
2961 (apply
2962 'encode-time
2963 (mapcar (lambda(x) (or x 0)) ;; FIXME: Problem with timezone?
2964 (parse-time-string (match-string 1))))
2965 (current-time)))
2966 (timestr (format-time-string
2967 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
2968 (prompt (format "YYYY-MM-DD [%s]: " timestr))
2969 ans ans1 ans2
2970 second minute hour day month year tl)
2971
2972 (if org-popup-calendar-for-date-prompt
2973 ;; Also show a calendar for date selection
2974 ;; Copied (with modifications) from planner.el by John Wiegley
2975 (save-excursion
2976 (save-window-excursion
2977 (calendar)
2978 (calendar-forward-day (- (time-to-days default-time)
2979 (calendar-absolute-from-gregorian
2980 (calendar-current-date))))
2981 (let* ((old-map (current-local-map))
2982 (map (copy-keymap calendar-mode-map))
2983 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
2984 (define-key map (kbd "RET") 'org-calendar-select)
2985 (define-key map (if org-xemacs-p [button1] [mouse-1])
2986 'org-calendar-select)
2987 (define-key minibuffer-local-map [(meta shift left)]
2988 (lambda () (interactive)
2989 (org-eval-in-calendar '(calendar-backward-month 1))))
2990 (define-key minibuffer-local-map [(meta shift right)]
2991 (lambda () (interactive)
2992 (org-eval-in-calendar '(calendar-forward-month 1))))
2993 (define-key minibuffer-local-map [(shift up)]
2994 (lambda () (interactive)
2995 (org-eval-in-calendar '(calendar-backward-week 1))))
2996 (define-key minibuffer-local-map [(shift down)]
2997 (lambda () (interactive)
2998 (org-eval-in-calendar '(calendar-forward-week 1))))
2999 (define-key minibuffer-local-map [(shift left)]
3000 (lambda () (interactive)
3001 (org-eval-in-calendar '(calendar-backward-day 1))))
3002 (define-key minibuffer-local-map [(shift right)]
3003 (lambda () (interactive)
3004 (org-eval-in-calendar '(calendar-forward-day 1))))
3005 (define-key minibuffer-local-map ">"
3006 (lambda () (interactive)
3007 (org-eval-in-calendar '(scroll-calendar-left 1))))
3008 (define-key minibuffer-local-map "<"
3009 (lambda () (interactive)
3010 (org-eval-in-calendar '(scroll-calendar-right 1))))
3011 (unwind-protect
3012 (progn
3013 (use-local-map map)
3014 (setq ans (read-string prompt "" nil nil))
3015 (setq ans (or ans1 ans2 ans)))
3016 (use-local-map old-map)))))
3017 ;; Naked prompt only
3018 (setq ans (read-string prompt "" nil timestr)))
3019
3020 (if (string-match
3021 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
3022 (progn
3023 (setq year (if (match-end 2)
3024 (string-to-number (match-string 2 ans))
3025 (string-to-number (format-time-string "%Y")))
3026 month (string-to-number (match-string 3 ans))
3027 day (string-to-number (match-string 4 ans)))
3028 (if (< year 100) (setq year (+ 2000 year)))
3029 (setq ans (replace-match (format "%04d-%02d-%02d" year month day)
3030 t t ans))))
3031 (setq tl (parse-time-string ans)
3032 year (or (nth 5 tl) (string-to-number (format-time-string "%Y")))
3033 month (or (nth 4 tl) (string-to-number (format-time-string "%m")))
3034 day (or (nth 3 tl) (string-to-number (format-time-string "%d")))
3035 hour (or (nth 2 tl) (string-to-number (format-time-string "%H")))
3036 minute (or (nth 1 tl) (string-to-number (format-time-string "%M")))
3037 second (or (nth 0 tl) 0))
3038 (if (and (boundp 'org-time-was-given)
3039 (nth 2 tl))
3040 (setq org-time-was-given t))
3041 (if (< year 100) (setq year (+ 2000 year)))
3042 (if to-time
3043 (encode-time second minute hour day month year)
3044 (if (or (nth 1 tl) (nth 2 tl))
3045 (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
3046 (format "%04d-%02d-%02d" year month day)))))
3047
3048 (defun org-eval-in-calendar (form)
3049 "Eval FORM in the calendar window and return to current window.
3050 Also, store the cursor date in variable ans2."
3051 (let ((sw (selected-window)))
3052 (select-window (get-buffer-window "*Calendar*"))
3053 (eval form)
3054 (when (calendar-cursor-to-date)
3055 (let* ((date (calendar-cursor-to-date))
3056 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
3057 (setq ans2 (format-time-string "%Y-%m-%d" time))))
3058 (select-window sw)))
3059
3060 (defun org-calendar-select ()
3061 "Return to `org-read-date' with the date currently selected.
3062 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
3063 (interactive)
3064 (when (calendar-cursor-to-date)
3065 (let* ((date (calendar-cursor-to-date))
3066 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
3067 (setq ans1 (format-time-string "%Y-%m-%d" time)))
3068 (if (active-minibuffer-window) (exit-minibuffer))))
3069
3070 (defun org-check-deadlines (ndays)
3071 "Check if there are any deadlines due or past due.
3072 A deadline is considered due if it happens within `org-deadline-warning-days'
3073 days from today's date. If the deadline appears in an entry marked DONE,
3074 it is not shown. The prefix arg NDAYS can be used to test that many
3075 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
3076 (interactive "P")
3077 (let* ((org-warn-days
3078 (cond
3079 ((equal ndays '(4)) 100000)
3080 (ndays (prefix-numeric-value ndays))
3081 (t org-deadline-warning-days)))
3082 (case-fold-search nil)
3083 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
3084 (callback
3085 (lambda ()
3086 (and (let ((d1 (time-to-days (current-time)))
3087 (d2 (time-to-days
3088 (org-time-string-to-time (match-string 1)))))
3089 (< (- d2 d1) org-warn-days))
3090 (not (org-entry-is-done-p))))))
3091 (message "%d deadlines past-due or due within %d days"
3092 (org-occur regexp callback)
3093 org-warn-days)))
3094
3095 (defun org-evaluate-time-range (&optional to-buffer)
3096 "Evaluate a time range by computing the difference between start and end.
3097 Normally the result is just printed in the echo area, but with prefix arg
3098 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
3099 If the time range is actually in a table, the result is inserted into the
3100 next column.
3101 For time difference computation, a year is assumed to be exactly 365
3102 days in order to avoid rounding problems."
3103 (interactive "P")
3104 (save-excursion
3105 (unless (org-at-date-range-p)
3106 (goto-char (point-at-bol))
3107 (re-search-forward org-tr-regexp (point-at-eol) t))
3108 (if (not (org-at-date-range-p))
3109 (error "Not at a time-stamp range, and none found in current line")))
3110 (let* ((ts1 (match-string 1))
3111 (ts2 (match-string 2))
3112 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
3113 (match-end (match-end 0))
3114 (time1 (org-time-string-to-time ts1))
3115 (time2 (org-time-string-to-time ts2))
3116 (t1 (time-to-seconds time1))
3117 (t2 (time-to-seconds time2))
3118 (diff (abs (- t2 t1)))
3119 (negative (< (- t2 t1) 0))
3120 ;; (ys (floor (* 365 24 60 60)))
3121 (ds (* 24 60 60))
3122 (hs (* 60 60))
3123 (fy "%dy %dd %02d:%02d")
3124 (fy1 "%dy %dd")
3125 (fd "%dd %02d:%02d")
3126 (fd1 "%dd")
3127 (fh "%02d:%02d")
3128 y d h m align)
3129 ;; FIXME: Should I re-introduce years, make year refer to same date?
3130 ;; This would be the only useful way to have years, actually.
3131 (if havetime
3132 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
3133 y 0
3134 d (floor (/ diff ds)) diff (mod diff ds)
3135 h (floor (/ diff hs)) diff (mod diff hs)
3136 m (floor (/ diff 60)))
3137 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
3138 y 0
3139 d (floor (+ (/ diff ds) 0.5))
3140 h 0 m 0))
3141 (if (not to-buffer)
3142 (message (org-make-tdiff-string y d h m))
3143 (when (org-at-table-p)
3144 (goto-char match-end)
3145 (setq align t)
3146 (and (looking-at " *|") (goto-char (match-end 0))))
3147 (if (looking-at
3148 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
3149 (replace-match ""))
3150 (if negative (insert " -"))
3151 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
3152 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
3153 (insert " " (format fh h m))))
3154 (if align (org-table-align))
3155 (message "Time difference inserted"))))
3156
3157 (defun org-make-tdiff-string (y d h m)
3158 (let ((fmt "")
3159 (l nil))
3160 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
3161 l (push y l)))
3162 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
3163 l (push d l)))
3164 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
3165 l (push h l)))
3166 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
3167 l (push m l)))
3168 (apply 'format fmt (nreverse l))))
3169
3170 (defun org-time-string-to-time (s)
3171 (apply 'encode-time (org-parse-time-string s)))
3172
3173 (defun org-parse-time-string (s &optional nodefault)
3174 "Parse the standard Org-mode time string.
3175 This should be a lot faster than the normal `parse-time-string'.
3176 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
3177 hour and minute fields will be nil if not given."
3178 (if (string-match org-ts-regexp1 s)
3179 (list 0
3180 (if (or (match-beginning 8) (not nodefault))
3181 (string-to-number (or (match-string 8 s) "0")))
3182 (if (or (match-beginning 7) (not nodefault))
3183 (string-to-number (or (match-string 7 s) "0")))
3184 (string-to-number (match-string 4 s))
3185 (string-to-number (match-string 3 s))
3186 (string-to-number (match-string 2 s))
3187 nil nil nil)
3188 (make-list 9 0)))
3189
3190 (defun org-timestamp-up (&optional arg)
3191 "Increase the date item at the cursor by one.
3192 If the cursor is on the year, change the year. If it is on the month or
3193 the day, change that.
3194 With prefix ARG, change by that many units."
3195 (interactive "p")
3196 (org-timestamp-change (prefix-numeric-value arg)))
3197
3198 (defun org-timestamp-down (&optional arg)
3199 "Decrease the date item at the cursor by one.
3200 If the cursor is on the year, change the year. If it is on the month or
3201 the day, change that.
3202 With prefix ARG, change by that many units."
3203 (interactive "p")
3204 (org-timestamp-change (- (prefix-numeric-value arg))))
3205
3206 (defun org-timestamp-up-day (&optional arg)
3207 "Increase the date in the time stamp by one day.
3208 With prefix ARG, change that many days."
3209 (interactive "p")
3210 (org-timestamp-change (prefix-numeric-value arg) 'day))
3211
3212 (defun org-timestamp-down-day (&optional arg)
3213 "Decrease the date in the time stamp by one day.
3214 With prefix ARG, change that many days."
3215 (interactive "p")
3216 (org-timestamp-change (- (prefix-numeric-value arg)) 'day))
3217
3218 (defsubst org-pos-in-match-range (pos n)
3219 (and (match-beginning n)
3220 (<= (match-beginning n) pos)
3221 (>= (match-end n) pos)))
3222
3223 (defun org-at-timestamp-p ()
3224 "Determine if the cursor is or at a timestamp."
3225 (interactive)
3226 (let* ((tsr org-ts-regexp2)
3227 (pos (point))
3228 (ans (or (looking-at tsr)
3229 (save-excursion
3230 (skip-chars-backward "^<\n\r\t")
3231 (if (> (point) 1) (backward-char 1))
3232 (and (looking-at tsr)
3233 (> (- (match-end 0) pos) -1))))))
3234 (and (boundp 'org-ts-what)
3235 (setq org-ts-what
3236 (cond
3237 ((org-pos-in-match-range pos 2) 'year)
3238 ((org-pos-in-match-range pos 3) 'month)
3239 ((org-pos-in-match-range pos 7) 'hour)
3240 ((org-pos-in-match-range pos 8) 'minute)
3241 ((or (org-pos-in-match-range pos 4)
3242 (org-pos-in-match-range pos 5)) 'day)
3243 (t 'day))))
3244 ans))
3245
3246 (defun org-timestamp-change (n &optional what)
3247 "Change the date in the time stamp at point.
3248 The date will be changed by N times WHAT. WHAT can be `day', `month',
3249 `year', `minute', `second'. If WHAT is not given, the cursor position
3250 in the timestamp determines what will be changed."
3251 (let ((fmt (car org-time-stamp-formats))
3252 org-ts-what
3253 (pos (point))
3254 ts time time0)
3255 (if (not (org-at-timestamp-p))
3256 (error "Not at a timestamp"))
3257 (setq org-ts-what (or what org-ts-what))
3258 (setq fmt (if (<= (abs (- (cdr org-ts-lengths)
3259 (- (match-end 0) (match-beginning 0))))
3260 1)
3261 (cdr org-time-stamp-formats)
3262 (car org-time-stamp-formats)))
3263 (setq ts (match-string 0))
3264 (replace-match "")
3265 (setq time0 (org-parse-time-string ts))
3266 (setq time
3267 (apply 'encode-time
3268 (append
3269 (list (or (car time0) 0))
3270 (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
3271 (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
3272 (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
3273 (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
3274 (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
3275 (nthcdr 6 time0))))
3276 (if (eq what 'calendar)
3277 (let ((cal-date
3278 (save-excursion
3279 (save-match-data
3280 (set-buffer "*Calendar*")
3281 (calendar-cursor-to-date)))))
3282 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
3283 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
3284 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
3285 (setcar time0 (or (car time0) 0))
3286 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
3287 (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
3288 (setq time (apply 'encode-time time0))))
3289 (insert (setq org-last-changed-timestamp (format-time-string fmt time)))
3290 (goto-char pos)
3291 ;; Try to recenter the calendar window, if any
3292 (if (and org-calendar-follow-timestamp-change
3293 (get-buffer-window "*Calendar*" t)
3294 (memq org-ts-what '(day month year)))
3295 (org-recenter-calendar (time-to-days time)))))
3296
3297 (defun org-recenter-calendar (date)
3298 "If the calendar is visible, recenter it to DATE."
3299 (let* ((win (selected-window))
3300 (cwin (get-buffer-window "*Calendar*" t)))
3301 (when cwin
3302 (select-window cwin)
3303 (calendar-goto-date (if (listp date) date
3304 (calendar-gregorian-from-absolute date)))
3305 (select-window win))))
3306
3307 (defun org-goto-calendar (&optional arg)
3308 "Go to the Emacs calendar at the current date.
3309 If there is a time stamp in the current line, go to that date.
3310 A prefix ARG can be used force the current date."
3311 (interactive "P")
3312 (let ((tsr org-ts-regexp) diff)
3313 (if (or (org-at-timestamp-p)
3314 (save-excursion
3315 (beginning-of-line 1)
3316 (looking-at (concat ".*" tsr))))
3317 (let ((d1 (time-to-days (current-time)))
3318 (d2 (time-to-days
3319 (org-time-string-to-time (match-string 1)))))
3320 (setq diff (- d2 d1))))
3321 (calendar)
3322 (calendar-goto-today)
3323 (if (and diff (not arg)) (calendar-forward-day diff))))
3324
3325 (defun org-date-from-calendar ()
3326 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
3327 If there is already a time stamp at the cursor position, update it."
3328 (interactive)
3329 (org-timestamp-change 0 'calendar))
3330
3331 ;;; Agenda, and Diary Integration
3332
3333 ;;; Define the mode
3334
3335 (defvar org-agenda-mode-map (make-sparse-keymap)
3336 "Keymap for `org-agenda-mode'.")
3337
3338 (defvar org-agenda-menu)
3339 (defvar org-agenda-follow-mode nil)
3340 (defvar org-agenda-buffer-name "*Org Agenda*")
3341 (defvar org-agenda-redo-command nil)
3342 (defvar org-agenda-mode-hook nil)
3343
3344 ;;;###autoload
3345 (defun org-agenda-mode ()
3346 "Mode for time-sorted view on action items in Org-mode files.
3347
3348 The following commands are available:
3349
3350 \\{org-agenda-mode-map}"
3351 (interactive)
3352 (kill-all-local-variables)
3353 (setq major-mode 'org-agenda-mode)
3354 (setq mode-name "Org-Agenda")
3355 (use-local-map org-agenda-mode-map)
3356 (easy-menu-add org-agenda-menu)
3357 (if org-startup-truncated (setq truncate-lines t))
3358 (add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
3359 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
3360 (setq org-agenda-follow-mode nil)
3361 (easy-menu-change
3362 '("Agenda") "Agenda Files"
3363 (append
3364 (list
3365 ["Edit File List" (customize-variable 'org-agenda-files) t]
3366 "--")
3367 (mapcar 'org-file-menu-entry org-agenda-files)))
3368 (org-agenda-set-mode-name)
3369 (apply
3370 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
3371 org-agenda-mode-hook))
3372
3373 (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
3374 (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
3375 (define-key org-agenda-mode-map " " 'org-agenda-show)
3376 (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
3377 (define-key org-agenda-mode-map "o" 'delete-other-windows)
3378 (define-key org-agenda-mode-map "l" 'org-agenda-recenter)
3379 (define-key org-agenda-mode-map "t" 'org-agenda-todo)
3380 (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
3381 (define-key org-agenda-mode-map "w" 'org-agenda-toggle-week-view)
3382 (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
3383 (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
3384
3385 (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
3386 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
3387 (while l (define-key org-agenda-mode-map
3388 (int-to-string (pop l)) 'digit-argument)))
3389
3390 (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
3391 (define-key org-agenda-mode-map "d" 'org-agenda-toggle-diary)
3392 (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
3393 (define-key org-agenda-mode-map "r" 'org-agenda-redo)
3394 (define-key org-agenda-mode-map "q" 'org-agenda-quit)
3395 (define-key org-agenda-mode-map "x" 'org-agenda-exit)
3396 (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
3397 (define-key org-agenda-mode-map "n" 'next-line)
3398 (define-key org-agenda-mode-map "p" 'previous-line)
3399 (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
3400 (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
3401 (define-key org-agenda-mode-map "," 'org-agenda-priority)
3402 (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
3403 (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
3404 (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
3405 (eval-after-load "calendar"
3406 '(define-key calendar-mode-map org-calendar-to-agenda-key
3407 'org-calendar-goto-agenda))
3408 (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
3409 (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
3410 (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
3411 (define-key org-agenda-mode-map "s" 'org-agenda-sunrise-sunset)
3412 (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
3413 (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
3414 (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
3415 (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
3416 (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
3417 (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
3418 (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
3419 (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
3420 (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
3421
3422 (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
3423 "Local keymap for agenda entries from Org-mode.")
3424
3425 (define-key org-agenda-keymap
3426 (if org-xemacs-p [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
3427 (define-key org-agenda-keymap
3428 (if org-xemacs-p [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
3429
3430 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
3431 '("Agenda"
3432 ("Agenda Files")
3433 "--"
3434 ["Show" org-agenda-show t]
3435 ["Go To (other window)" org-agenda-goto t]
3436 ["Go To (one window)" org-agenda-switch-to t]
3437 ["Follow Mode" org-agenda-follow-mode
3438 :style toggle :selected org-agenda-follow-mode :active t]
3439 "--"
3440 ["Cycle TODO" org-agenda-todo t]
3441 ("Reschedule"
3442 ["Reschedule +1 day" org-agenda-date-later t]
3443 ["Reschedule -1 day" org-agenda-date-earlier t]
3444 "--"
3445 ["Reschedule to ..." org-agenda-date-prompt t])
3446 ("Priority"
3447 ["Set Priority" org-agenda-priority t]
3448 ["Increase Priority" org-agenda-priority-up t]
3449 ["Decrease Priority" org-agenda-priority-down t]
3450 ["Show Priority" org-agenda-show-priority t])
3451 "--"
3452 ["Rebuild buffer" org-agenda-redo t]
3453 ["Goto Today" org-agenda-goto-today t]
3454 ["Next Dates" org-agenda-later (local-variable-p 'starting-day)]
3455 ["Previous Dates" org-agenda-earlier (local-variable-p 'starting-day)]
3456 "--"
3457 ["Week/Day View" org-agenda-toggle-week-view
3458 (local-variable-p 'starting-day)]
3459 ["Include Diary" org-agenda-toggle-diary
3460 :style toggle :selected org-agenda-include-diary :active t]
3461 ["Use Time Grid" org-agenda-toggle-time-grid
3462 :style toggle :selected org-agenda-use-time-grid :active t]
3463 "--"
3464 ["New Diary Entry" org-agenda-diary-entry t]
3465 ("Calendar Commands"
3466 ["Goto Calendar" org-agenda-goto-calendar t]
3467 ["Phases of the Moon" org-agenda-phases-of-moon t]
3468 ["Sunrise/Sunset" org-agenda-sunrise-sunset t]
3469 ["Holidays" org-agenda-holidays t]
3470 ["Convert" org-agenda-convert-date t])
3471 "--"
3472 ["Quit" org-agenda-quit t]
3473 ["Exit and Release Buffers" org-agenda-exit t]
3474 ))
3475
3476 (defvar org-agenda-markers nil
3477 "List of all currently active markers created by `org-agenda'.")
3478 (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
3479 "Creation time of the last agenda marker.")
3480
3481 (defun org-agenda-new-marker (&optional pos)
3482 "Return a new agenda marker.
3483 Org-mode keeps a list of these markers and resets them when they are
3484 no longer in use."
3485 (let ((m (copy-marker (or pos (point)))))
3486 (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
3487 (push m org-agenda-markers)
3488 m))
3489
3490 (defun org-agenda-maybe-reset-markers (&optional force)
3491 "Reset markers created by `org-agenda'. But only if they are old enough."
3492 (if (or force
3493 (> (- (time-to-seconds (current-time))
3494 org-agenda-last-marker-time)
3495 5))
3496 (while org-agenda-markers
3497 (move-marker (pop org-agenda-markers) nil))))
3498
3499 (defvar org-agenda-new-buffers nil
3500 "Buffers created to visit agenda files.")
3501
3502 (defun org-get-agenda-file-buffer (file)
3503 "Get a buffer visiting FILE. If the buffer needs to be created, add
3504 it to the list of buffers which might be released later."
3505 (let ((buf (find-buffer-visiting file)))
3506 (if buf
3507 buf ; just return it
3508 ;; Make a new buffer and remember it
3509 (setq buf (find-file-noselect file))
3510 (if buf (push buf org-agenda-new-buffers))
3511 buf)))
3512
3513 (defun org-release-buffers (blist)
3514 "Release all buffers in list, asking the user for confirmation when needed.
3515 When a buffer is unmodified, it is just killed. When modified, it is saved
3516 \(if the user agrees) and then killed."
3517 (let (buf file)
3518 (while (setq buf (pop blist))
3519 (setq file (buffer-file-name buf))
3520 (when (and (buffer-modified-p buf)
3521 file
3522 (y-or-n-p (format "Save file %s? " file)))
3523 (with-current-buffer buf (save-buffer)))
3524 (kill-buffer buf))))
3525
3526 (defvar org-respect-restriction nil) ; Dynamically-scoped param.
3527
3528 (defun org-timeline (&optional include-all)
3529 "Show a time-sorted view of the entries in the current org file.
3530 Only entries with a time stamp of today or later will be listed. With
3531 one \\[universal-argument] prefix argument, past entries will also be listed.
3532 With two \\[universal-argument] prefixes, all unfinished TODO items will also be shown,
3533 under the current date.
3534 If the buffer contains an active region, only check the region for
3535 dates."
3536 (interactive "P")
3537 (require 'calendar)
3538 (org-agenda-maybe-reset-markers 'force)
3539 (org-compile-prefix-format org-timeline-prefix-format)
3540 (let* ((dopast include-all)
3541 (dotodo (equal include-all '(16)))
3542 (entry (buffer-file-name))
3543 (org-agenda-files (list (buffer-file-name)))
3544 (date (calendar-current-date))
3545 (win (selected-window))
3546 (pos1 (point))
3547 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3548 (end (if (org-region-active-p) (region-end) (point-max)))
3549 (day-numbers (org-get-all-dates beg end 'no-ranges
3550 t)) ; always include today
3551 (today (time-to-days (current-time)))
3552 (org-respect-restriction t)
3553 (past t)
3554 s e rtn d)
3555 (setq org-agenda-redo-command
3556 (list 'progn
3557 (list 'switch-to-buffer-other-window (current-buffer))
3558 (list 'org-timeline include-all)))
3559 (if (not dopast)
3560 ;; Remove past dates from the list of dates.
3561 (setq day-numbers (delq nil (mapcar (lambda(x)
3562 (if (>= x today) x nil))
3563 day-numbers))))
3564 (switch-to-buffer-other-window
3565 (get-buffer-create org-agenda-buffer-name))
3566 (setq buffer-read-only nil)
3567 (erase-buffer)
3568 (org-agenda-mode) (setq buffer-read-only nil)
3569 (while (setq d (pop day-numbers))
3570 (if (and (>= d today)
3571 dopast
3572 past)
3573 (progn
3574 (setq past nil)
3575 (insert (make-string 79 ?-) "\n")))
3576 (setq date (calendar-gregorian-from-absolute d))
3577 (setq s (point))
3578 (if dotodo
3579 (setq rtn (org-agenda-get-day-entries
3580 entry date :todo :timestamp))
3581 (setq rtn (org-agenda-get-day-entries entry date :timestamp)))
3582 (if (or rtn (equal d today))
3583 (progn
3584 (insert (calendar-day-name date) " "
3585 (number-to-string (extract-calendar-day date)) " "
3586 (calendar-month-name (extract-calendar-month date)) " "
3587 (number-to-string (extract-calendar-year date)) "\n")
3588 (put-text-property s (1- (point)) 'face
3589 'org-link)
3590 (if (equal d today)
3591 (put-text-property s (1- (point)) 'org-today t))
3592 (insert (org-finalize-agenda-entries rtn) "\n")
3593 (put-text-property s (1- (point)) 'day d))))
3594 (goto-char (point-min))
3595 (setq buffer-read-only t)
3596 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3597 (point-min)))
3598 (when (not org-select-timeline-window)
3599 (select-window win)
3600 (goto-char pos1))))
3601
3602 ;;;###autoload
3603 (defun org-agenda (&optional include-all start-day ndays)
3604 "Produce a weekly view from all files in variable `org-agenda-files'.
3605 The view will be for the current week, but from the overview buffer you
3606 will be able to go to other weeks.
3607 With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
3608 also be shown, under the current date.
3609 START-DAY defaults to TODAY, or to the most recent match for the weekday
3610 given in `org-agenda-start-on-weekday'.
3611 NDAYS defaults to `org-agenda-ndays'."
3612 (interactive "P")
3613 (org-agenda-maybe-reset-markers 'force)
3614 (org-compile-prefix-format org-agenda-prefix-format)
3615 (require 'calendar)
3616 (let* ((org-agenda-start-on-weekday
3617 (if (or (equal ndays 1)
3618 (and (null ndays) (equal 1 org-agenda-ndays)))
3619 nil org-agenda-start-on-weekday))
3620 (files (copy-sequence org-agenda-files))
3621 (win (selected-window))
3622 (today (time-to-days (current-time)))
3623 (sd (or start-day today))
3624 (start (if (or (null org-agenda-start-on-weekday)
3625 (< org-agenda-ndays 7))
3626 sd
3627 (let* ((nt (calendar-day-of-week
3628 (calendar-gregorian-from-absolute sd)))
3629 (n1 org-agenda-start-on-weekday)
3630 (d (- nt n1)))
3631 (- sd (+ (if (< d 0) 7 0) d)))))
3632 (day-numbers (list start))
3633 (inhibit-redisplay t)
3634 s e rtn rtnall file date d start-pos end-pos todayp nd)
3635 (setq org-agenda-redo-command
3636 (list 'org-agenda include-all start-day ndays))
3637 ;; Make the list of days
3638 (setq ndays (or ndays org-agenda-ndays)
3639 nd ndays)
3640 (while (> ndays 1)
3641 (push (1+ (car day-numbers)) day-numbers)
3642 (setq ndays (1- ndays)))
3643 (setq day-numbers (nreverse day-numbers))
3644 (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
3645 (progn
3646 (delete-other-windows)
3647 (switch-to-buffer-other-window
3648 (get-buffer-create org-agenda-buffer-name))))
3649 (setq buffer-read-only nil)
3650 (erase-buffer)
3651 (org-agenda-mode) (setq buffer-read-only nil)
3652 (set (make-local-variable 'starting-day) (car day-numbers))
3653 (set (make-local-variable 'include-all-loc) include-all)
3654 (when (and (or include-all org-agenda-include-all-todo)
3655 (member today day-numbers))
3656 (setq files org-agenda-files
3657 rtnall nil)
3658 (while (setq file (pop files))
3659 (catch 'nextfile
3660 (org-check-agenda-file file)
3661 (setq date (calendar-gregorian-from-absolute today)
3662 rtn (org-agenda-get-day-entries
3663 file date :todo))
3664 (setq rtnall (append rtnall rtn))))
3665 (when rtnall
3666 (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
3667 (add-text-properties (point-min) (1- (point))
3668 (list 'face 'org-link))
3669 (insert (org-finalize-agenda-entries rtnall) "\n")))
3670 (while (setq d (pop day-numbers))
3671 (setq date (calendar-gregorian-from-absolute d)
3672 s (point))
3673 (if (or (setq todayp (= d today))
3674 (and (not start-pos) (= d sd)))
3675 (setq start-pos (point))
3676 (if (and start-pos (not end-pos))
3677 (setq end-pos (point))))
3678 (setq files org-agenda-files
3679 rtnall nil)
3680 (while (setq file (pop files))
3681 (catch 'nextfile
3682 (org-check-agenda-file file)
3683 (setq rtn (org-agenda-get-day-entries file date))
3684 (setq rtnall (append rtnall rtn))))
3685 (if org-agenda-include-diary
3686 (progn
3687 (require 'diary-lib)
3688 (setq rtn (org-get-entries-from-diary date))
3689 (setq rtnall (append rtnall rtn))))
3690 (if (or rtnall org-agenda-show-all-dates)
3691 (progn
3692 (insert (format "%-9s %2d %s %4d\n"
3693 (calendar-day-name date)
3694 (extract-calendar-day date)
3695 (calendar-month-name (extract-calendar-month date))
3696 (extract-calendar-year date)))
3697 (put-text-property s (1- (point)) 'face
3698 'org-link)
3699 (if rtnall (insert
3700 (org-finalize-agenda-entries ;; FIXME: condition needed
3701 (org-agenda-add-time-grid-maybe
3702 rtnall nd todayp))
3703 "\n"))
3704 (put-text-property s (1- (point)) 'day d))))
3705 (goto-char (point-min))
3706 (setq buffer-read-only t)
3707 (if org-fit-agenda-window
3708 (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
3709 (/ (frame-height) 2)))
3710 (unless (and (pos-visible-in-window-p (point-min))
3711 (pos-visible-in-window-p (point-max)))
3712 (goto-char (1- (point-max)))
3713 (recenter -1)
3714 (if (not (pos-visible-in-window-p (or start-pos 1)))
3715 (progn
3716 (goto-char (or start-pos 1))
3717 (recenter 1))))
3718 (goto-char (or start-pos 1))
3719 (if (not org-select-agenda-window) (select-window win))
3720 (message "")))
3721
3722 (defun org-check-agenda-file (file)
3723 "Make sure FILE exists. If not, ask user what to do."
3724 ;; FIXME: this does not correctly change the menus
3725 ;; Could probably be fixed by explicitly going to the buffer.
3726 (when (not (file-exists-p file))
3727 (message "non-existent file %s. [R]emove from agenda-files or [A]bort?"
3728 file)
3729 (let ((r (downcase (read-char-exclusive))))
3730 (cond
3731 ((equal r ?r)
3732 (org-remove-file file)
3733 (throw 'nextfile t))
3734 (t (error "Abort"))))))
3735
3736 (defun org-agenda-quit ()
3737 "Exit agenda by removing the window or the buffer."
3738 (interactive)
3739 (let ((buf (current-buffer)))
3740 (if (not (one-window-p)) (delete-window))
3741 (kill-buffer buf)
3742 (org-agenda-maybe-reset-markers 'force)))
3743
3744 (defun org-agenda-exit ()
3745 "Exit agenda by removing the window or the buffer.
3746 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
3747 Org-mode buffers visited directly by the user will not be touched."
3748 (interactive)
3749 (org-release-buffers org-agenda-new-buffers)
3750 (setq org-agenda-new-buffers nil)
3751 (org-agenda-quit))
3752
3753 (defun org-agenda-redo ()
3754 "Rebuild Agenda."
3755 (interactive)
3756 (eval org-agenda-redo-command))
3757
3758 (defun org-agenda-goto-today ()
3759 "Go to today."
3760 (interactive)
3761 (if (boundp 'starting-day)
3762 (let ((cmd (car org-agenda-redo-command))
3763 (iall (nth 1 org-agenda-redo-command))
3764 (nday (nth 3 org-agenda-redo-command)))
3765 (eval (list cmd iall nil nday)))
3766 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3767 (point-min)))))
3768
3769 (defun org-agenda-later (arg)
3770 "Go forward in time by `org-agenda-ndays' days.
3771 With prefix ARG, go forward that many times `org-agenda-ndays'."
3772 (interactive "p")
3773 (unless (boundp 'starting-day)
3774 (error "Not allowed"))
3775 (org-agenda (if (boundp 'include-all-loc) include-all-loc nil)
3776 (+ starting-day (* arg org-agenda-ndays))))
3777
3778 (defun org-agenda-earlier (arg)
3779 "Go back in time by `org-agenda-ndays' days.
3780 With prefix ARG, go back that many times `org-agenda-ndays'."
3781 (interactive "p")
3782 (unless (boundp 'starting-day)
3783 (error "Not allowed"))
3784 (org-agenda (if (boundp 'include-all-loc) include-all-loc nil)
3785 (- starting-day (* arg org-agenda-ndays))))
3786
3787 (defun org-agenda-toggle-week-view ()
3788 "Toggle weekly/daily view for aagenda."
3789 (interactive)
3790 (unless (boundp 'starting-day)
3791 (error "Not allowed"))
3792 (setq org-agenda-ndays
3793 (if (equal org-agenda-ndays 1) 7 1))
3794 (org-agenda include-all-loc
3795 (or (get-text-property (point) 'day)
3796 starting-day))
3797 (org-agenda-set-mode-name)
3798 (message "Switched to %s view"
3799 (if (equal org-agenda-ndays 1) "day" "week")))
3800
3801 (defun org-agenda-next-date-line (&optional arg)
3802 "Jump to the next line indicating a date in agenda buffer."
3803 (interactive "p")
3804 (beginning-of-line 1)
3805 (if (looking-at "^\\S-") (forward-char 1))
3806 (if (not (re-search-forward "^\\S-" nil t arg))
3807 (progn
3808 (backward-char 1)
3809 (error "No next date after this line in this buffer")))
3810 (goto-char (match-beginning 0)))
3811
3812 (defun org-agenda-previous-date-line (&optional arg)
3813 "Jump to the next line indicating a date in agenda buffer."
3814 (interactive "p")
3815 (beginning-of-line 1)
3816 (if (not (re-search-backward "^\\S-" nil t arg))
3817 (error "No previous date before this line in this buffer")))
3818
3819 ;; Initialize the highlight
3820 (defvar org-hl (funcall (if org-xemacs-p 'make-extent 'make-overlay) 1 1))
3821 (funcall (if org-xemacs-p 'set-extent-property 'overlay-put) org-hl
3822 'face 'highlight)
3823
3824 (defun org-highlight (begin end &optional buffer)
3825 "Highlight a region with overlay."
3826 (funcall (if org-xemacs-p 'set-extent-endpoints 'move-overlay)
3827 org-hl begin end (or buffer (current-buffer))))
3828
3829 (defun org-unhighlight ()
3830 "Detach overlay INDEX."
3831 (funcall (if org-xemacs-p 'detach-extent 'delete-overlay) org-hl))
3832
3833
3834 (defun org-agenda-follow-mode ()
3835 "Toggle follow mode in an agenda buffer."
3836 (interactive)
3837 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
3838 (org-agenda-set-mode-name)
3839 (message "Follow mode is %s"
3840 (if org-agenda-follow-mode "on" "off")))
3841
3842 (defun org-agenda-toggle-diary ()
3843 "Toggle follow mode in an agenda buffer."
3844 (interactive)
3845 (setq org-agenda-include-diary (not org-agenda-include-diary))
3846 (org-agenda-redo)
3847 (org-agenda-set-mode-name)
3848 (message "Diary inclusion turned %s"
3849 (if org-agenda-include-diary "on" "off")))
3850
3851 (defun org-agenda-toggle-time-grid ()
3852 "Toggle follow mode in an agenda buffer."
3853 (interactive)
3854 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
3855 (org-agenda-redo)
3856 (org-agenda-set-mode-name)
3857 (message "Time-grid turned %s"
3858 (if org-agenda-use-time-grid "on" "off")))
3859
3860 (defun org-agenda-set-mode-name ()
3861 "Set the mode name to indicate all the small mode settings."
3862 (setq mode-name
3863 (concat "Org-Agenda"
3864 (if (equal org-agenda-ndays 1) " Day" "")
3865 (if (equal org-agenda-ndays 7) " Week" "")
3866 (if org-agenda-follow-mode " Follow" "")
3867 (if org-agenda-include-diary " Diary" "")
3868 (if org-agenda-use-time-grid " Grid" "")))
3869 (force-mode-line-update))
3870
3871 (defun org-agenda-post-command-hook ()
3872 (and (eolp) (not (bolp)) (backward-char 1))
3873 (if (and org-agenda-follow-mode
3874 (get-text-property (point) 'org-marker))
3875 (org-agenda-show)))
3876
3877 (defvar org-disable-diary nil) ;Dynamically-scoped param.
3878
3879 (defun org-get-entries-from-diary (date)
3880 "Get the (Emacs Calendar) diary entries for DATE."
3881 (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
3882 (diary-display-hook '(fancy-diary-display))
3883 (list-diary-entries-hook
3884 (cons 'org-diary-default-entry list-diary-entries-hook))
3885 entries
3886 (org-disable-diary t))
3887 (save-excursion
3888 (save-window-excursion
3889 (list-diary-entries date 1)))
3890 (if (not (get-buffer fancy-diary-buffer))
3891 (setq entries nil)
3892 (with-current-buffer fancy-diary-buffer
3893 (setq buffer-read-only nil)
3894 (if (= (point-max) 1)
3895 ;; No entries
3896 (setq entries nil)
3897 ;; Omit the date and other unnecessary stuff
3898 (org-agenda-cleanup-fancy-diary)
3899 ;; Add prefix to each line and extend the text properties
3900 (if (= (point-max) 1)
3901 (setq entries nil)
3902 (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
3903 (set-buffer-modified-p nil)
3904 (kill-buffer fancy-diary-buffer)))
3905 (when entries
3906 (setq entries (org-split-string entries "\n"))
3907 (setq entries
3908 (mapcar
3909 (lambda (x)
3910 (setq x (org-format-agenda-item "" x "Diary" 'time))
3911 ;; Extend the text properties to the beginning of the line
3912 (add-text-properties
3913 0 (length x)
3914 (text-properties-at (1- (length x)) x)
3915 x)
3916 x)
3917 entries)))))
3918
3919 (defun org-agenda-cleanup-fancy-diary ()
3920 "Remove unwanted stuff in buffer created by fancy-diary-display.
3921 This gets rid of the date, the underline under the date, and
3922 the dummy entry installed by `org-mode' to ensure non-empty diary for each
3923 date. Itt also removes lines that contain only whitespace."
3924 (goto-char (point-min))
3925 (if (looking-at ".*?:[ \t]*")
3926 (progn
3927 (replace-match "")
3928 (re-search-forward "\n=+$" nil t)
3929 (replace-match "")
3930 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
3931 (re-search-forward "\n=+$" nil t)
3932 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
3933 (goto-char (point-min))
3934 (while (re-search-forward "^ +\n" nil t)
3935 (replace-match ""))
3936 (goto-char (point-min))
3937 (if (re-search-forward "^Org-mode dummy\n?" nil t)
3938 (replace-match "")))
3939
3940 ;; Advise the add-to-diary-list function to allow org to jump to
3941 ;; diary entries. Wrapped into eval-after-load to avoid loading
3942 ;; advice unnecessarily
3943 (eval-after-load "diary-lib"
3944 '(defadvice add-to-diary-list (before org-mark-diary-entry activate)
3945 "Make the position visible."
3946 (if (and org-disable-diary ;; called from org-agenda
3947 (stringp string)
3948 (buffer-file-name))
3949 (add-text-properties
3950 0 (length string)
3951 (list 'mouse-face 'highlight
3952 'keymap org-agenda-keymap
3953 'help-echo
3954 (format
3955 "mouse-2 or RET jump to diary file %s"
3956 (abbreviate-file-name (buffer-file-name)))
3957 'org-agenda-diary-link t
3958 'org-marker (org-agenda-new-marker (point-at-bol)))
3959 string))))
3960
3961 (defun org-diary-default-entry ()
3962 "Add a dummy entry to the diary.
3963 Needed to avoid empty dates which mess up holiday display."
3964 ;; Catch the error if dealing with the new add-to-diary-alist
3965 (condition-case nil
3966 (add-to-diary-list original-date "Org-mode dummy" "")
3967 (error
3968 (add-to-diary-list original-date "Org-mode dummy" "" nil))))
3969
3970 (defun org-add-file (&optional file)
3971 "Add current file to the list of files in variable `org-agenda-files'.
3972 These are the files which are being checked for agenda entries.
3973 Optional argument FILE means, use this file instead of the current.
3974 It is possible (but not recommended) to add this function to the
3975 `org-mode-hook'."
3976 (interactive)
3977 (catch 'exit
3978 (let* ((file (or file (buffer-file-name)
3979 (if (interactive-p)
3980 (error "Buffer is not visiting a file")
3981 (throw 'exit nil))))
3982 (true-file (file-truename file))
3983 (afile (abbreviate-file-name file))
3984 (present (delq nil (mapcar
3985 (lambda (x)
3986 (equal true-file (file-truename x)))
3987 org-agenda-files))))
3988 (if (not present)
3989 (progn
3990 (setq org-agenda-files
3991 (cons afile org-agenda-files))
3992 ;; Make sure custom.el does not end up with Org-mode
3993 (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
3994 (customize-save-variable 'org-agenda-files org-agenda-files))
3995 (org-install-agenda-files-menu)
3996 (message "Added file: %s" afile))
3997 (message "File was already in list: %s" afile)))))
3998
3999 (defun org-remove-file (&optional file)
4000 "Remove current file from the list of files in variable `org-agenda-files'.
4001 These are the files which are being checked for agenda entries.
4002 Optional argument FILE means, use this file instead of the current."
4003 (interactive)
4004 (let* ((file (or file (buffer-file-name)))
4005 (true-file (file-truename file))
4006 (afile (abbreviate-file-name file))
4007 (files (delq nil (mapcar
4008 (lambda (x)
4009 (if (equal true-file
4010 (file-truename x))
4011 nil x))
4012 org-agenda-files))))
4013 (if (not (= (length files) (length org-agenda-files)))
4014 (progn
4015 (setq org-agenda-files files)
4016 (customize-save-variable 'org-agenda-files org-agenda-files)
4017 (org-install-agenda-files-menu)
4018 (message "Removed file: %s" afile))
4019 (message "File was not in list: %s" afile))))
4020
4021 (defun org-file-menu-entry (file)
4022 (vector file (list 'find-file file) t))
4023
4024 (defun org-get-all-dates (beg end &optional no-ranges force-today)
4025 "Return a list of all relevant day numbers from BEG to END buffer positions.
4026 If NO-RANGES is non-nil, include only the start and end dates of a range,
4027 not every single day in the range. If FORCE-TODAY is non-nil, make
4028 sure that TODAY is included in the list."
4029 (let (dates date day day1 day2 ts1 ts2)
4030 (if force-today
4031 (setq dates (list (time-to-days (current-time)))))
4032 (save-excursion
4033 (goto-char beg)
4034 (while (re-search-forward org-ts-regexp end t)
4035 (setq day (time-to-days (org-time-string-to-time
4036 (substring (match-string 1) 0 10))))
4037 (or (memq day dates) (push day dates)))
4038 (unless no-ranges
4039 (goto-char beg)
4040 (while (re-search-forward org-tr-regexp end t)
4041 (setq ts1 (substring (match-string 1) 0 10)
4042 ts2 (substring (match-string 2) 0 10)
4043 day1 (time-to-days (org-time-string-to-time ts1))
4044 day2 (time-to-days (org-time-string-to-time ts2)))
4045 (while (< (setq day1 (1+ day1)) day2)
4046 (or (memq day1 dates) (push day1 dates)))))
4047 (sort dates '<))))
4048
4049 ;;;###autoload
4050 (defun org-diary (&rest args)
4051 "Return diary information from org-files.
4052 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4053 It accesses org files and extracts information from those files to be
4054 listed in the diary. The function accepts arguments specifying what
4055 items should be listed. The following arguments are allowed:
4056
4057 :timestamp List the headlines of items containing a date stamp or
4058 date range matching the selected date. Deadlines will
4059 also be listed, on the expiration day.
4060
4061 :deadline List any deadlines past due, or due within
4062 `org-deadline-warning-days'. The listing occurs only
4063 in the diary for *today*, not at any other date. If
4064 an entry is marked DONE, it is no longer listed.
4065
4066 :scheduled List all items which are scheduled for the given date.
4067 The diary for *today* also contains items which were
4068 scheduled earlier and are not yet marked DONE.
4069
4070 :todo List all TODO items from the org-file. This may be a
4071 long list - so this is not turned on by default.
4072 Like deadlines, these entries only show up in the
4073 diary for *today*, not at any other date.
4074
4075 The call in the diary file should look like this:
4076
4077 &%%(org-diary) ~/path/to/some/orgfile.org
4078
4079 Use a separate line for each org file to check. Or, if you omit the file name,
4080 all files listed in `org-agenda-files' will be checked automatically:
4081
4082 &%%(org-diary)
4083
4084 If you don't give any arguments (as in the example above), the default
4085 arguments (:deadline :scheduled :timestamp) are used. So the example above may
4086 also be written as
4087
4088 &%%(org-diary :deadline :timestamp :scheduled)
4089
4090 The function expects the lisp variables `entry' and `date' to be provided
4091 by the caller, because this is how the calendar works. Don't use this
4092 function from a program - use `org-agenda-get-day-entries' instead."
4093 (org-agenda-maybe-reset-markers)
4094 (org-compile-agenda-prefix-format org-agenda-prefix-format)
4095 (setq args (or args '(:deadline :scheduled :timestamp)))
4096 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4097 (list entry)
4098 org-agenda-files))
4099 file rtn results)
4100 ;; If this is called during org-agenda, don't return any entries to
4101 ;; the calendar. Org Agenda will list these entries itself.
4102 (if org-disable-diary (setq files nil))
4103 (while (setq file (pop files))
4104 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4105 (setq results (append results rtn)))
4106 (concat (org-finalize-agenda-entries results) "\n")))
4107
4108 (defun org-agenda-get-day-entries (file date &rest args)
4109 "Does the work for `org-diary' and `org-agenda'.
4110 FILE is the path to a file to be checked for entries. DATE is date like
4111 the one returned by `calendar-current-date'. ARGS are symbols indicating
4112 which kind of entries should be extracted. For details about these, see
4113 the documentation of `org-diary'."
4114 (setq args (or args '(:deadline :scheduled :timestamp)))
4115 (let* ((org-startup-with-deadline-check nil)
4116 (org-startup-folded nil)
4117 (buffer (if (file-exists-p file)
4118 (org-get-agenda-file-buffer file)
4119 (error "No such file %s" file)))
4120 arg results rtn)
4121 (if (not buffer)
4122 ;; If file does not exist, make sure an error message ends up in diary
4123 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4124 (with-current-buffer buffer
4125 (unless (eq major-mode 'org-mode)
4126 (error "Agenda file %s is not in `org-mode'" file))
4127 (let ((case-fold-search nil))
4128 (save-excursion
4129 (save-restriction
4130 (if org-respect-restriction
4131 (if (org-region-active-p)
4132 ;; Respect a region to restrict search
4133 (narrow-to-region (region-beginning) (region-end)))
4134 ;; If we work for the calendar or many files,
4135 ;; get rid of any restriction
4136 (widen))
4137 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4138 (while (setq arg (pop args))
4139 (cond
4140 ((and (eq arg :todo)
4141 (equal date (calendar-current-date)))
4142 (setq rtn (org-agenda-get-todos))
4143 (setq results (append results rtn)))
4144 ((eq arg :timestamp)
4145 (setq rtn (org-agenda-get-blocks))
4146 (setq results (append results rtn))
4147 (setq rtn (org-agenda-get-timestamps))
4148 (setq results (append results rtn)))
4149 ((eq arg :scheduled)
4150 (setq rtn (org-agenda-get-scheduled))
4151 (setq results (append results rtn)))
4152 ((and (eq arg :deadline)
4153 (equal date (calendar-current-date)))
4154 (setq rtn (org-agenda-get-deadlines))
4155 (setq results (append results rtn))))))))
4156 results))))
4157
4158 (defun org-entry-is-done-p ()
4159 "Is the current entry marked DONE?"
4160 (save-excursion
4161 (and (re-search-backward "[\r\n]\\*" nil t)
4162 (looking-at org-nl-done-regexp))))
4163
4164 (defun org-at-date-range-p ()
4165 "Is the cursor inside a date range?"
4166 (interactive)
4167 (save-excursion
4168 (catch 'exit
4169 (let ((pos (point)))
4170 (skip-chars-backward "^<\r\n")
4171 (skip-chars-backward "<")
4172 (and (looking-at org-tr-regexp)
4173 (>= (match-end 0) pos)
4174 (throw 'exit t))
4175 (skip-chars-backward "^<\r\n")
4176 (skip-chars-backward "<")
4177 (and (looking-at org-tr-regexp)
4178 (>= (match-end 0) pos)
4179 (throw 'exit t)))
4180 nil)))
4181
4182 (defun org-agenda-get-todos ()
4183 "Return the TODO information for agenda display."
4184 (let* ((props (list 'face nil
4185 'done-face 'org-done
4186 'mouse-face 'highlight
4187 'keymap org-agenda-keymap
4188 'help-echo
4189 (format "mouse-2 or RET jump to org file %s"
4190 (abbreviate-file-name (buffer-file-name)))))
4191 (regexp (concat "[\n\r]\\*+ *\\(" org-not-done-regexp
4192 "[^\n\r]*\\)"))
4193 marker priority
4194 ee txt)
4195 (goto-char (point-min))
4196 (while (re-search-forward regexp nil t)
4197 (goto-char (match-beginning 1))
4198 (setq marker (org-agenda-new-marker (point-at-bol))
4199 txt (org-format-agenda-item "" (match-string 1))
4200 priority
4201 (+ (org-get-priority txt)
4202 (if org-todo-kwd-priority-p
4203 (- org-todo-kwd-max-priority -2
4204 (length
4205 (member (match-string 2) org-todo-keywords)))
4206 1)))
4207 (add-text-properties
4208 0 (length txt) (append (list 'org-marker marker 'org-hd-marker marker
4209 'priority priority)
4210 props)
4211 txt)
4212 (push txt ee)
4213 (goto-char (match-end 1)))
4214 (nreverse ee)))
4215
4216 (defconst org-agenda-no-heading-message
4217 "No heading for this item in buffer or region")
4218
4219 (defun org-agenda-get-timestamps ()
4220 "Return the date stamp information for agenda display."
4221 (let* ((props (list 'face nil
4222 'mouse-face 'highlight
4223 'keymap org-agenda-keymap
4224 'help-echo
4225 (format "mouse-2 or RET jump to org file %s"
4226 (abbreviate-file-name (buffer-file-name)))))
4227 (regexp (regexp-quote
4228 (substring
4229 (format-time-string
4230 (car org-time-stamp-formats)
4231 (apply 'encode-time ; DATE bound by calendar
4232 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4233 0 11)))
4234 marker hdmarker deadlinep scheduledp donep tmp priority
4235 ee txt timestr)
4236 (goto-char (point-min))
4237 (while (re-search-forward regexp nil t)
4238 (if (not (save-match-data (org-at-date-range-p)))
4239 (progn
4240 (setq marker (org-agenda-new-marker (match-beginning 0))
4241 tmp (buffer-substring (max (point-min)
4242 (- (match-beginning 0)
4243 org-ds-keyword-length))
4244 (match-beginning 0))
4245 timestr (buffer-substring (match-beginning 0) (point-at-eol))
4246 deadlinep (string-match org-deadline-regexp tmp)
4247 scheduledp (string-match org-scheduled-regexp tmp)
4248 donep (org-entry-is-done-p))
4249 (if (string-match ">" timestr)
4250 ;; substring should only run to end of time stamp
4251 (setq timestr (substring timestr 0 (match-end 0))))
4252 (save-excursion
4253 (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
4254 (progn
4255 (goto-char (match-end 1))
4256 (setq hdmarker (org-agenda-new-marker))
4257 (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
4258 (setq txt (org-format-agenda-item
4259 (format "%s%s"
4260 (if deadlinep "Deadline: " "")
4261 (if scheduledp "Scheduled: " ""))
4262 (match-string 1) nil timestr)))
4263 (setq txt org-agenda-no-heading-message))
4264 (setq priority (org-get-priority txt))
4265 (add-text-properties
4266 0 (length txt) (append (list 'org-marker marker
4267 'org-hd-marker hdmarker) props)
4268 txt)
4269 (if deadlinep
4270 (add-text-properties
4271 0 (length txt)
4272 (list 'face
4273 (if donep 'org-done 'org-warning)
4274 'undone-face 'org-warning
4275 'done-face 'org-done
4276 'priority (+ 100 priority))
4277 txt)
4278 (if scheduledp
4279 (add-text-properties
4280 0 (length txt)
4281 (list 'face 'org-scheduled-today
4282 'undone-face 'org-scheduled-today
4283 'done-face 'org-done
4284 priority (+ 99 priority))
4285 txt)
4286 (add-text-properties
4287 0 (length txt)
4288 (list 'priority priority) txt)))
4289 (push txt ee))
4290 (outline-next-heading))))
4291 (nreverse ee)))
4292
4293 (defun org-agenda-get-deadlines ()
4294 "Return the deadline information for agenda display."
4295 (let* ((wdays org-deadline-warning-days)
4296 (props (list 'mouse-face 'highlight
4297 'keymap org-agenda-keymap
4298 'help-echo
4299 (format "mouse-2 or RET jump to org file %s"
4300 (abbreviate-file-name (buffer-file-name)))))
4301 (regexp org-deadline-time-regexp)
4302 (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
4303 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4304 d2 diff pos pos1
4305 ee txt head)
4306 (goto-char (point-min))
4307 (while (re-search-forward regexp nil t)
4308 (setq pos (1- (match-beginning 1))
4309 d2 (time-to-days
4310 (org-time-string-to-time (match-string 1)))
4311 diff (- d2 d1))
4312 ;; When to show a deadline in the calendar:
4313 ;; If the expiration is within wdays warning time.
4314 ;; Past-due deadlines are only shown on the current date
4315 (if (and (< diff wdays) todayp (not (= diff 0)))
4316 (save-excursion
4317 (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
4318 (progn
4319 (goto-char (match-end 0))
4320 (setq pos1 (match-end 1))
4321 (setq head (buffer-substring-no-properties
4322 (point)
4323 (progn (skip-chars-forward "^\r\n")
4324 (point))))
4325 (if (string-match org-looking-at-done-regexp head)
4326 (setq txt nil)
4327 (setq txt (org-format-agenda-item
4328 (format "In %3d d.: " diff) head))))
4329 (setq txt org-agenda-no-heading-message))
4330 (when txt
4331 (add-text-properties
4332 0 (length txt)
4333 (append
4334 (list 'org-marker (org-agenda-new-marker pos)
4335 'org-hd-marker (org-agenda-new-marker pos1)
4336 'priority (+ (- 10 diff) (org-get-priority txt))
4337 'face (cond ((<= diff 0) 'org-warning)
4338 ((<= diff 5) 'org-scheduled-previously)
4339 (t nil))
4340 'undone-face (cond
4341 ((<= diff 0) 'org-warning)
4342 ((<= diff 5) 'org-scheduled-previously)
4343 (t nil))
4344 'done-face 'org-done)
4345 props)
4346 txt)
4347 (push txt ee)))))
4348 ee))
4349
4350 (defun org-agenda-get-scheduled ()
4351 "Return the scheduled information for agenda display."
4352 (let* ((props (list 'face 'org-scheduled-previously
4353 'undone-face 'org-scheduled-previously
4354 'done-face 'org-done
4355 'mouse-face 'highlight
4356 'keymap org-agenda-keymap
4357 'help-echo
4358 (format "mouse-2 or RET jump to org file %s"
4359 (abbreviate-file-name (buffer-file-name)))))
4360 (regexp org-scheduled-time-regexp)
4361 (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
4362 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4363 d2 diff pos pos1
4364 ee txt head)
4365 (goto-char (point-min))
4366 (while (re-search-forward regexp nil t)
4367 (setq pos (1- (match-beginning 1))
4368 d2 (time-to-days
4369 (org-time-string-to-time (match-string 1)))
4370 diff (- d2 d1))
4371 ;; When to show a scheduled item in the calendar:
4372 ;; If it is on or past the date.
4373 (if (and (< diff 0) todayp)
4374 (save-excursion
4375 (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
4376 (progn
4377 (goto-char (match-end 0))
4378 (setq pos1 (match-end 1))
4379 (setq head (buffer-substring-no-properties
4380 (point)
4381 (progn (skip-chars-forward "^\r\n") (point))))
4382 (if (string-match org-looking-at-done-regexp head)
4383 (setq txt nil)
4384 (setq txt (org-format-agenda-item
4385 (format "Sched.%2dx: " (- 1 diff)) head))))
4386 (setq txt org-agenda-no-heading-message))
4387 (when txt
4388 (add-text-properties
4389 0 (length txt)
4390 (append (list 'org-marker (org-agenda-new-marker pos)
4391 'org-hd-marker (org-agenda-new-marker pos1)
4392 'priority (+ (- 5 diff) (org-get-priority txt)))
4393 props) txt)
4394 (push txt ee)))))
4395 ee))
4396
4397 (defun org-agenda-get-blocks ()
4398 "Return the date-range information for agenda display."
4399 (let* ((props (list 'face nil
4400 'mouse-face 'highlight
4401 'keymap org-agenda-keymap
4402 'help-echo
4403 (format "mouse-2 or RET jump to org file %s"
4404 (abbreviate-file-name (buffer-file-name)))))
4405 (regexp org-tr-regexp)
4406 (d0 (calendar-absolute-from-gregorian date))
4407 marker hdmarker ee txt d1 d2 s1 s2 timestr)
4408 (goto-char (point-min))
4409 (while (re-search-forward regexp nil t)
4410 (setq timestr (match-string 0)
4411 s1 (match-string 1)
4412 s2 (match-string 2)
4413 d1 (time-to-days (org-time-string-to-time s1))
4414 d2 (time-to-days (org-time-string-to-time s2)))
4415 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
4416 ;; Only allow days between the limits, because the normal
4417 ;; date stamps will catch the limits.
4418 (save-excursion
4419 (setq marker (org-agenda-new-marker (point)))
4420 (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
4421 (progn
4422 (setq hdmarker (org-agenda-new-marker (match-end 1)))
4423 (goto-char (match-end 1))
4424 (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
4425 (setq txt (org-format-agenda-item
4426 (format (if (= d1 d2) "" "(%d/%d): ")
4427 (1+ (- d0 d1)) (1+ (- d2 d1)))
4428 (match-string 1) nil (if (= d0 d1) timestr))))
4429 (setq txt org-agenda-no-heading-message))
4430 (add-text-properties
4431 0 (length txt) (append (list 'org-marker marker
4432 'org-hd-marker hdmarker
4433 'priority (org-get-priority txt))
4434 props)
4435 txt)
4436 (push txt ee)))
4437 (outline-next-heading))
4438 ;; Sort the entries by expiration date.
4439 (nreverse ee)))
4440
4441
4442
4443 (defconst org-plain-time-of-day-regexp
4444 (concat
4445 "\\(\\<[012]?[0-9]"
4446 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4447 "\\(--?"
4448 "\\(\\<[012]?[0-9]"
4449 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4450 "\\)?")
4451 "Regular expression to match a plain time or time range.
4452 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4453 groups carry important information:
4454 0 the full match
4455 1 the first time, range or not
4456 8 the second time, if it is a range.")
4457
4458 (defconst org-stamp-time-of-day-regexp
4459 (concat
4460 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +[a-zA-Z]+ +\\)"
4461 "\\([012][0-9]:[0-5][0-9]\\)>"
4462 "\\(--?"
4463 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4464 "Regular expression to match a timestamp time or time range.
4465 After a match, the following groups carry important information:
4466 0 the full match
4467 1 date plus weekday, for backreferencing to make sure both times on same day
4468 2 the first time, range or not
4469 4 the second time, if it is a range.")
4470
4471 (defvar org-prefix-has-time nil
4472 "A flag, set by `org-compile-prefix-format'.
4473 The flag is set if the currently compiled format contains a `%t'.")
4474
4475 (defun org-format-agenda-item (extra txt &optional category dotime noprefix)
4476 "Format TXT to be inserted into the agenda buffer.
4477 In particular, it adds the prefix and corresponding text properties. EXTRA
4478 must be a string and replaces the `%s' specifier in the prefix format.
4479 CATEGORY (string, symbol or nil) may be used to overule the default
4480 category taken from local variable or file name. It will replace the `%c'
4481 specifier in the format. DOTIME, when non-nil, indicates that a
4482 time-of-day should be extracted from TXT for sorting of this entry, and for
4483 the `%t' specifier in the format. When DOTIME is a string, this string is
4484 searched for a time before TXT is. NOPREFIX is a flag and indicates that
4485 only the correctly processes TXT should be returned - this is used by
4486 `org-agenda-change-all-lines'."
4487 (save-match-data
4488 ;; Diary entries sometimes have extra whitespace at the beginning
4489 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
4490 (let* ((category (or category
4491 org-category
4492 (if (buffer-file-name)
4493 (file-name-sans-extension
4494 (file-name-nondirectory (buffer-file-name)))
4495 "")))
4496 time ;; needed for the eval of the prefix format
4497 (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
4498 (time-of-day (and dotime (org-get-time-of-day ts)))
4499 stamp plain s0 s1 s2 rtn)
4500 (when (and dotime time-of-day org-prefix-has-time)
4501 ;; Extract starting and ending time and move them to prefix
4502 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
4503 (setq plain (string-match org-plain-time-of-day-regexp ts)))
4504 (setq s0 (match-string 0 ts)
4505 s1 (match-string (if plain 1 2) ts)
4506 s2 (match-string (if plain 8 4) ts))
4507
4508 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
4509 ;; them, we might want to remove them there to avoid duplication.
4510 ;; The user can turn this off with a variable.
4511 (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
4512 (string-match (concat (regexp-quote s0) " *") txt)
4513 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
4514 (= (match-beginning 0) 0)
4515 t))
4516 (setq txt (replace-match "" nil nil txt))))
4517 ;; Normalize the time(s) to 24 hour
4518 (if s1 (setq s1 (org-get-time-of-day s1 'string)))
4519 (if s2 (setq s2 (org-get-time-of-day s2 'string))))
4520
4521 ;; Create the final string
4522 (if noprefix
4523 (setq rtn txt)
4524 ;; Prepare the variables needed in the eval of the compiled format
4525 (setq time (cond (s2 (concat s1 "-" s2))
4526 (s1 (concat s1 "......"))
4527 (t ""))
4528 extra (or extra "")
4529 category (if (symbolp category) (symbol-name category) category))
4530 ;; Evaluate the compiled format
4531 (setq rtn (concat (eval org-prefix-format-compiled) txt)))
4532
4533 ;; And finally add the text properties
4534 (add-text-properties
4535 0 (length rtn) (list 'category (downcase category)
4536 'prefix-length (- (length rtn) (length txt))
4537 'time-of-day time-of-day
4538 'dotime dotime)
4539 rtn)
4540 rtn)))
4541
4542 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
4543 (catch 'exit
4544 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
4545 ((and todayp (member 'today (car org-agenda-time-grid))))
4546 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
4547 ((member 'weekly (car org-agenda-time-grid)))
4548 (t (throw 'exit list)))
4549 (let* ((have (delq nil (mapcar
4550 (lambda (x) (get-text-property 1 'time-of-day x))
4551 list)))
4552 (string (nth 1 org-agenda-time-grid))
4553 (gridtimes (nth 2 org-agenda-time-grid))
4554 (req (car org-agenda-time-grid))
4555 (remove (member 'remove-match req))
4556 new time)
4557 (if (and (member 'require-timed req) (not have))
4558 ;; don't show empty grid
4559 (throw 'exit list))
4560 (while (setq time (pop gridtimes))
4561 (unless (and remove (member time have))
4562 (setq time (int-to-string time))
4563 (push (org-format-agenda-item
4564 nil string "" ;; FIXME: put a category?
4565 (concat (substring time 0 -2) ":" (substring time -2)))
4566 new)
4567 (put-text-property
4568 1 (length (car new)) 'face 'org-time-grid (car new))))
4569 (if (member 'time-up org-agenda-sorting-strategy)
4570 (append new list)
4571 (append list new)))))
4572
4573 (defun org-compile-prefix-format (format)
4574 "Compile the prefix format into a Lisp form that can be evaluated.
4575 The resulting form is returned and stored in the variable
4576 `org-prefix-format-compiled'."
4577 (setq org-prefix-has-time nil)
4578 (let ((start 0) varform vars var (s format) c f opt)
4579 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
4580 s start)
4581 (setq var (cdr (assoc (match-string 4 s)
4582 '(("c" . category) ("t" . time) ("s" . extra))))
4583 c (or (match-string 3 s) "")
4584 opt (match-beginning 1)
4585 start (1+ (match-beginning 0)))
4586 (if (equal var 'time) (setq org-prefix-has-time t))
4587 (setq f (concat "%" (match-string 2 s) "s"))
4588 (if opt
4589 (setq varform
4590 `(if (equal "" ,var)
4591 ""
4592 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
4593 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
4594 (setq s (replace-match "%s" t nil s))
4595 (push varform vars))
4596 (setq vars (nreverse vars))
4597 (setq org-prefix-format-compiled `(format ,s ,@vars))))
4598
4599 (defun org-get-time-of-day (s &optional string)
4600 "Check string S for a time of day.
4601 If found, return it as a military time number between 0 and 2400.
4602 If not found, return nil.
4603 The optional STRING argument forces conversion into a 5 character wide string
4604 HH:MM."
4605 (save-match-data
4606 (when
4607 (or
4608 (string-match
4609 "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
4610 (string-match
4611 "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
4612 (let* ((t0 (+ (* 100
4613 (+ (string-to-number (match-string 1 s))
4614 (if (and (match-beginning 4)
4615 (equal (downcase (match-string 4 s)) "pm"))
4616 12 0)))
4617 (if (match-beginning 3)
4618 (string-to-number (match-string 3 s))
4619 0)))
4620 (t1 (concat " " (int-to-string t0))))
4621 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
4622
4623 (defun org-finalize-agenda-entries (list)
4624 "Sort and concatenate the agenda items."
4625 (mapconcat 'identity (sort list 'org-entries-lessp) "\n"))
4626
4627 (defsubst org-cmp-priority (a b)
4628 "Compare the priorities of string a and b."
4629 (let ((pa (or (get-text-property 1 'priority a) 0))
4630 (pb (or (get-text-property 1 'priority b) 0)))
4631 (cond ((> pa pb) +1)
4632 ((< pa pb) -1)
4633 (t nil))))
4634
4635 (defsubst org-cmp-category (a b)
4636 "Compare the string values of categories of strings a and b."
4637 (let ((ca (or (get-text-property 1 'category a) ""))
4638 (cb (or (get-text-property 1 'category b) "")))
4639 (cond ((string-lessp ca cb) -1)
4640 ((string-lessp cb ca) +1)
4641 (t nil))))
4642
4643 (defsubst org-cmp-time (a b)
4644 "Compare the time-of-day values of strings a and b."
4645 (let* ((def (if org-sort-agenda-notime-is-late 2401 -1))
4646 (ta (or (get-text-property 1 'time-of-day a) def))
4647 (tb (or (get-text-property 1 'time-of-day b) def)))
4648 (cond ((< ta tb) -1)
4649 ((< tb ta) +1)
4650 (t nil))))
4651
4652 (defun org-entries-lessp (a b)
4653 "Predicate for sorting agenda entries."
4654 ;; The following variables will be used when the form is evaluated.
4655 (let* ((time-up (org-cmp-time a b))
4656 (time-down (if time-up (- time-up) nil))
4657 (priority-up (org-cmp-priority a b))
4658 (priority-down (if priority-up (- priority-up) nil))
4659 (category-up (org-cmp-category a b))
4660 (category-down (if category-up (- category-up) nil))
4661 (category-keep (if category-up +1 nil))) ; FIXME +1 or -1?
4662 (cdr (assoc
4663 (eval (cons 'or org-agenda-sorting-strategy))
4664 '((-1 . t) (1 . nil) (nil . nil))))))
4665
4666 (defun org-agenda-show-priority ()
4667 "Show the priority of the current item.
4668 This priority is composed of the main priority given with the [#A] cookies,
4669 and by additional input from the age of a schedules or deadline entry."
4670 (interactive)
4671 (let* ((pri (get-text-property (point-at-bol) 'priority)))
4672 (message "Priority is %d" (if pri pri -1000))))
4673
4674 (defun org-agenda-goto (&optional highlight)
4675 "Go to the Org-mode file which contains the item at point."
4676 (interactive)
4677 (let* ((marker (or (get-text-property (point) 'org-marker)
4678 (org-agenda-error)))
4679 (buffer (marker-buffer marker))
4680 (pos (marker-position marker)))
4681 (switch-to-buffer-other-window buffer)
4682 (widen)
4683 (goto-char pos)
4684 (when (eq major-mode 'org-mode)
4685 (org-show-hidden-entry)
4686 (save-excursion
4687 (and (outline-next-heading)
4688 (org-flag-heading nil)))) ; show the next heading
4689 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
4690
4691 (defun org-agenda-switch-to ()
4692 "Go to the Org-mode file which contains the item at point."
4693 (interactive)
4694 (let* ((marker (or (get-text-property (point) 'org-marker)
4695 (org-agenda-error)))
4696 (buffer (marker-buffer marker))
4697 (pos (marker-position marker)))
4698 (switch-to-buffer buffer)
4699 (delete-other-windows)
4700 (widen)
4701 (goto-char pos)
4702 (when (eq major-mode 'org-mode)
4703 (org-show-hidden-entry)
4704 (save-excursion
4705 (and (outline-next-heading)
4706 (org-flag-heading nil)))))) ; show the next heading
4707
4708 (defun org-agenda-goto-mouse (ev)
4709 "Go to the Org-mode file which contains the item at the mouse click."
4710 (interactive "e")
4711 (mouse-set-point ev)
4712 (org-agenda-goto))
4713
4714 (defun org-agenda-show ()
4715 "Display the Org-mode file which contains the item at point."
4716 (interactive)
4717 (let ((win (selected-window)))
4718 (org-agenda-goto t)
4719 (select-window win)))
4720
4721 (defun org-agenda-recenter (arg)
4722 "Display the Org-mode file which contains the item at point and recenter."
4723 (interactive "P")
4724 (let ((win (selected-window)))
4725 (org-agenda-goto t)
4726 (recenter arg)
4727 (select-window win)))
4728
4729 (defun org-agenda-show-mouse (ev)
4730 "Display the Org-mode file which contains the item at the mouse click."
4731 (interactive "e")
4732 (mouse-set-point ev)
4733 (org-agenda-show))
4734
4735 (defun org-agenda-check-no-diary ()
4736 "Check if the entry is a diary link and abort if yes."
4737 (if (get-text-property (point) 'org-agenda-diary-link)
4738 (org-agenda-error)))
4739
4740 (defun org-agenda-error ()
4741 (error "Command not allowed in this line"))
4742
4743 (defvar org-last-heading-marker (make-marker)
4744 "Marker pointing to the headline that last changed its TODO state
4745 by a remote command from the agenda.")
4746
4747 (defun org-agenda-todo ()
4748 "Cycle TODO state of line at point, also in Org-mode file.
4749 This changes the line at point, all other lines in the agenda referring to
4750 the same tree node, and the headline of the tree node in the Org-mode file."
4751 (interactive)
4752 (org-agenda-check-no-diary)
4753 (let* ((col (current-column))
4754 (marker (or (get-text-property (point) 'org-marker)
4755 (org-agenda-error)))
4756 (buffer (marker-buffer marker))
4757 (pos (marker-position marker))
4758 (hdmarker (get-text-property (point) 'org-hd-marker))
4759 (buffer-read-only nil)
4760 newhead)
4761 (with-current-buffer buffer
4762 (widen)
4763 (goto-char pos)
4764 (org-show-hidden-entry)
4765 (save-excursion
4766 (and (outline-next-heading)
4767 (org-flag-heading nil))) ; show the next heading
4768 (org-todo)
4769 (forward-char 1)
4770 (setq newhead (org-get-heading))
4771 (save-excursion
4772 (org-back-to-heading)
4773 (move-marker org-last-heading-marker (point))))
4774 (beginning-of-line 1)
4775 (save-excursion
4776 (org-agenda-change-all-lines newhead hdmarker 'fixface))
4777 (move-to-column col)))
4778
4779 (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
4780 "Change all lines in the agenda buffer which match hdmarker.
4781 The new content of the line will be NEWHEAD (as modified by
4782 `org-format-agenda-item'). HDMARKER is checked with
4783 `equal' against all `org-hd-marker' text properties in the file.
4784 If FIXFACE is non-nil, the face of each item is modified acording to
4785 the new TODO state."
4786 (let* (props m pl undone-face done-face finish new dotime)
4787 ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
4788 (save-excursion
4789 (goto-char (point-max))
4790 (beginning-of-line 1)
4791 (while (not finish)
4792 (setq finish (bobp))
4793 (when (and (setq m (get-text-property (point) 'org-hd-marker))
4794 (equal m hdmarker))
4795 (setq props (text-properties-at (point))
4796 dotime (get-text-property (point) 'dotime)
4797 new (org-format-agenda-item "x" newhead "x" dotime 'noprefix)
4798 pl (get-text-property (point) 'prefix-length)
4799 undone-face (get-text-property (point) 'undone-face)
4800 done-face (get-text-property (point) 'done-face))
4801 (move-to-column pl)
4802 (if (looking-at ".*")
4803 (progn
4804 (replace-match new t t)
4805 (beginning-of-line 1)
4806 (add-text-properties (point-at-bol) (point-at-eol) props)
4807 (if fixface
4808 (add-text-properties
4809 (point-at-bol) (point-at-eol)
4810 (list 'face
4811 (if org-last-todo-state-is-todo
4812 undone-face done-face))))
4813 (beginning-of-line 1))
4814 (error "Line update did not work")))
4815 (beginning-of-line 0)))))
4816
4817 (defun org-agenda-priority-up ()
4818 "Increase the priority of line at point, also in Org-mode file."
4819 (interactive)
4820 (org-agenda-priority 'up))
4821
4822 (defun org-agenda-priority-down ()
4823 "Decrease the priority of line at point, also in Org-mode file."
4824 (interactive)
4825 (org-agenda-priority 'down))
4826
4827 (defun org-agenda-priority (&optional force-direction)
4828 "Set the priority of line at point, also in Org-mode file.
4829 This changes the line at point, all other lines in the agenda referring to
4830 the same tree node, and the headline of the tree node in the Org-mode file."
4831 (interactive)
4832 (org-agenda-check-no-diary)
4833 (let* ((marker (or (get-text-property (point) 'org-marker)
4834 (org-agenda-error)))
4835 (buffer (marker-buffer marker))
4836 (pos (marker-position marker))
4837 (hdmarker (get-text-property (point) 'org-hd-marker))
4838 (buffer-read-only nil)
4839 newhead)
4840 (with-current-buffer buffer
4841 (widen)
4842 (goto-char pos)
4843 (org-show-hidden-entry)
4844 (save-excursion
4845 (and (outline-next-heading)
4846 (org-flag-heading nil))) ; show the next heading
4847 (funcall 'org-priority force-direction)
4848 (end-of-line 1)
4849 (setq newhead (org-get-heading)))
4850 (org-agenda-change-all-lines newhead hdmarker)
4851 (beginning-of-line 1)))
4852
4853 (defun org-agenda-date-later (arg &optional what)
4854 "Change the date of this item to one day later."
4855 (interactive "p")
4856 (org-agenda-check-no-diary)
4857 (let* ((marker (or (get-text-property (point) 'org-marker)
4858 (org-agenda-error)))
4859 (buffer (marker-buffer marker))
4860 (pos (marker-position marker)))
4861 (with-current-buffer buffer
4862 (widen)
4863 (goto-char pos)
4864 (if (not (org-at-timestamp-p))
4865 (error "Cannot find time stamp"))
4866 (org-timestamp-change arg (or what 'day))
4867 (message "Time stamp changed to %s" org-last-changed-timestamp))))
4868
4869 (defun org-agenda-date-earlier (arg &optional what)
4870 "Change the date of this item to one day earlier."
4871 (interactive "p")
4872 (org-agenda-date-later (- arg) what))
4873
4874 (defun org-agenda-date-prompt (arg)
4875 "Change the date of this item. Date is prompted for, with default today.
4876 The prefix ARG is passed to the `org-time-stamp' command and can therefore
4877 be used to request time specification in the time stamp."
4878 (interactive "P")
4879 (org-agenda-check-no-diary)
4880 (let* ((marker (or (get-text-property (point) 'org-marker)
4881 (org-agenda-error)))
4882 (buffer (marker-buffer marker))
4883 (pos (marker-position marker)))
4884 (with-current-buffer buffer
4885 (widen)
4886 (goto-char pos)
4887 (if (not (org-at-timestamp-p))
4888 (error "Cannot find time stamp"))
4889 (org-time-stamp arg)
4890 (message "Time stamp changed to %s" org-last-changed-timestamp))))
4891
4892 (defun org-get-heading ()
4893 "Return the heading of the current entry, without the stars."
4894 (save-excursion
4895 (if (and (re-search-backward "[\r\n]\\*" nil t)
4896 (looking-at "[\r\n]\\*+[ \t]+\\(.*\\)"))
4897 (match-string 1)
4898 "")))
4899
4900 (defun org-agenda-diary-entry ()
4901 "Make a diary entry, like the `i' command from the calendar.
4902 All the standard commands work: block, weekly etc"
4903 (interactive)
4904 (require 'diary-lib)
4905 (let* ((char (progn
4906 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
4907 (read-char-exclusive)))
4908 (cmd (cdr (assoc char
4909 '((?d . insert-diary-entry)
4910 (?w . insert-weekly-diary-entry)
4911 (?m . insert-monthly-diary-entry)
4912 (?y . insert-yearly-diary-entry)
4913 (?a . insert-anniversary-diary-entry)
4914 (?b . insert-block-diary-entry)
4915 (?c . insert-cyclic-diary-entry)))))
4916 (oldf (symbol-function 'calendar-cursor-to-date))
4917 (point (point))
4918 (mark (or (mark t) (point))))
4919 (unless cmd
4920 (error "No command associated with <%c>" char))
4921 (unless (and (get-text-property point 'day)
4922 (or (not (equal ?b char))
4923 (get-text-property mark 'day)))
4924 (error "Don't know which date to use for diary entry"))
4925 ;; We implement this by hacking the `calendar-cursor-to-date' function
4926 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
4927 (let ((calendar-mark-ring
4928 (list (calendar-gregorian-from-absolute
4929 (or (get-text-property mark 'day)
4930 (get-text-property point 'day))))))
4931 (unwind-protect
4932 (progn
4933 (fset 'calendar-cursor-to-date
4934 (lambda (&optional error)
4935 (calendar-gregorian-from-absolute
4936 (get-text-property point 'day))))
4937 (call-interactively cmd))
4938 (fset 'calendar-cursor-to-date oldf)))))
4939
4940
4941 (defun org-agenda-execute-calendar-command (cmd)
4942 "Execute a calendar command from the agenda, with the date associated to
4943 the cursor position."
4944 (require 'diary-lib)
4945 (unless (get-text-property (point) 'day)
4946 (error "Don't know which date to use for calendar command"))
4947 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
4948 (point (point))
4949 (date (calendar-gregorian-from-absolute
4950 (get-text-property point 'day)))
4951 (displayed-day (extract-calendar-day date))
4952 (displayed-month (extract-calendar-month date))
4953 (displayed-year (extract-calendar-year date)))
4954 (unwind-protect
4955 (progn
4956 (fset 'calendar-cursor-to-date
4957 (lambda (&optional error)
4958 (calendar-gregorian-from-absolute
4959 (get-text-property point 'day))))
4960 (call-interactively cmd))
4961 (fset 'calendar-cursor-to-date oldf))))
4962
4963 (defun org-agenda-phases-of-moon ()
4964 "Display the phases of the moon for the 3 months around the cursor date."
4965 (interactive)
4966 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
4967
4968 (defun org-agenda-holidays ()
4969 "Display the holidays for the 3 months around the cursor date."
4970 (interactive)
4971 (org-agenda-execute-calendar-command 'list-calendar-holidays))
4972
4973 (defun org-agenda-sunrise-sunset (arg)
4974 "Display sunrise and sunset for the cursor date.
4975 Latitude and longitude can be specified with the variables
4976 `calendar-latitude' and `calendar-longitude'. When called with prefix
4977 argument, latitude and longitude will be prompted for."
4978 (interactive "P")
4979 (let ((calendar-longitude (if arg nil calendar-longitude))
4980 (calendar-latitude (if arg nil calendar-latitude))
4981 (calendar-location-name
4982 (if arg "the given coordinates" calendar-location-name)))
4983 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
4984
4985 (defun org-agenda-goto-calendar ()
4986 "Open the Emacs calendar with the date at the cursor."
4987 (interactive)
4988 (let* ((day (or (get-text-property (point) 'day)
4989 (error "Don't know which date to open in calendar")))
4990 (date (calendar-gregorian-from-absolute day)))
4991 (calendar)
4992 (calendar-goto-date date)))
4993
4994 (defun org-calendar-goto-agenda ()
4995 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
4996 This is a command that has to be installed in `calendar-mode-map'."
4997 (interactive)
4998 (org-agenda nil (calendar-absolute-from-gregorian
4999 (calendar-cursor-to-date))))
5000
5001 (defun org-agenda-convert-date ()
5002 (interactive)
5003 (let ((day (get-text-property (point) 'day))
5004 date s)
5005 (unless day
5006 (error "Don't know which date to convert"))
5007 (setq date (calendar-gregorian-from-absolute day))
5008 (setq s (concat
5009 "Gregorian: " (calendar-date-string date) "\n"
5010 "ISO: " (calendar-iso-date-string date) "\n"
5011 "Day of Yr: " (calendar-day-of-year-string date) "\n"
5012 "Julian: " (calendar-julian-date-string date) "\n"
5013 "Astron. JD: " (calendar-astro-date-string date)
5014 " (Julian date number at noon UTC)\n"
5015 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
5016 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
5017 "French: " (calendar-french-date-string date) "\n"
5018 "Mayan: " (calendar-mayan-date-string date) "\n"
5019 "Coptic: " (calendar-coptic-date-string date) "\n"
5020 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
5021 "Persian: " (calendar-persian-date-string date) "\n"
5022 "Chinese: " (calendar-chinese-date-string date) "\n"))
5023 (with-output-to-temp-buffer "*Dates*"
5024 (princ s))
5025 (fit-window-to-buffer (get-buffer-window "*Dates*"))))
5026
5027 ;;; Link Stuff
5028
5029 (defun org-find-file-at-mouse (ev)
5030 "Open file link or URL at mouse."
5031 (interactive "e")
5032 (mouse-set-point ev)
5033 (org-open-at-point 'in-emacs))
5034
5035 (defun org-open-at-mouse (ev)
5036 "Open file link or URL at mouse."
5037 (interactive "e")
5038 (mouse-set-point ev)
5039 (org-open-at-point))
5040
5041 (defun org-open-at-point (&optional in-emacs)
5042 "Open link at or after point.
5043 If there is no link at point, this function will search forward up to
5044 the end of the current subtree.
5045 Normally, files will be opened by an appropriate application. If the
5046 optional argument IN-EMACS is non-nil, Emacs will visit the file."
5047 (interactive "P")
5048 (if (org-at-timestamp-p)
5049 (org-agenda nil (time-to-days (org-time-string-to-time
5050 (substring (match-string 1) 0 10)))
5051 1)
5052 (let (type path line (pos (point)))
5053 (save-excursion
5054 (skip-chars-backward
5055 (concat (if org-allow-space-in-links "^" "^ ")
5056 org-non-link-chars))
5057 (if (re-search-forward
5058 org-link-regexp
5059 (save-excursion
5060 (condition-case nil
5061 (progn (outline-end-of-subtree) (max pos (point)))
5062 (error (end-of-line 1) (point))))
5063 t)
5064 (setq type (match-string 1)
5065 path (match-string 2)))
5066 (unless path
5067 (error "No link found"))
5068 ;; Remove any trailing spaces in path
5069 (if (string-match " +\\'" path)
5070 (setq path (replace-match "" t t path)))
5071
5072 (cond
5073
5074 ((string= type "file")
5075 (if (string-match ":\\([0-9]+\\)\\'" path)
5076 (setq line (string-to-number (match-string 1 path))
5077 path (substring path 0 (match-beginning 0))))
5078 (org-open-file path in-emacs line))
5079
5080 ((string= type "news")
5081 (org-follow-gnus-link path))
5082
5083 ((string= type "bbdb")
5084 (org-follow-bbdb-link path))
5085
5086 ((string= type "gnus")
5087 (let (group article)
5088 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
5089 (error "Error in Gnus link"))
5090 (setq group (match-string 1 path)
5091 article (match-string 3 path))
5092 (org-follow-gnus-link group article)))
5093
5094 ((string= type "vm")
5095 (let (folder article)
5096 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
5097 (error "Error in VM link"))
5098 (setq folder (match-string 1 path)
5099 article (match-string 3 path))
5100 ;; in-emacs is the prefix arg, will be interpreted as read-only
5101 (org-follow-vm-link folder article in-emacs)))
5102
5103 ((string= type "wl")
5104 (let (folder article)
5105 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
5106 (error "Error in Wanderlust link"))
5107 (setq folder (match-string 1 path)
5108 article (match-string 3 path))
5109 (org-follow-wl-link folder article)))
5110
5111 ((string= type "rmail")
5112 (let (folder article)
5113 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
5114 (error "Error in RMAIL link"))
5115 (setq folder (match-string 1 path)
5116 article (match-string 3 path))
5117 (org-follow-rmail-link folder article)))
5118
5119 ((string= type "shell")
5120 (let ((cmd path))
5121 (while (string-match "@{" cmd)
5122 (setq cmd (replace-match "<" t t cmd)))
5123 (while (string-match "@}" cmd)
5124 (setq cmd (replace-match ">" t t cmd)))
5125 (if (or (not org-confirm-shell-links)
5126 (yes-or-no-p (format "Execute \"%s\" in the shell? " cmd)))
5127 (shell-command cmd)
5128 (error "Abort"))))
5129
5130 (t
5131 (browse-url-at-point)))))))
5132
5133 (defun org-follow-bbdb-link (name)
5134 "Follow a BBDB link to NAME."
5135 (require 'bbdb)
5136 (let ((inhibit-redisplay t))
5137 (catch 'exit
5138 ;; Exact match on name
5139 (bbdb-name (concat "\\`" name "\\'") nil)
5140 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
5141 ;; Exact match on name
5142 (bbdb-company (concat "\\`" name "\\'") nil)
5143 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
5144 ;; Partial match on name
5145 (bbdb-name name nil)
5146 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
5147 ;; Partial match on company
5148 (bbdb-company name nil)
5149 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
5150 ;; General match including network address and notes
5151 (bbdb name nil)
5152 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
5153 (delete-window (get-buffer-window "*BBDB*"))
5154 (error "No matching BBDB record")))))
5155
5156 (defun org-follow-gnus-link (&optional group article)
5157 "Follow a Gnus link to GROUP and ARTICLE."
5158 (require 'gnus)
5159 (funcall (cdr (assq 'gnus org-link-frame-setup)))
5160 (if group (gnus-fetch-group group))
5161 (if article
5162 (or (gnus-summary-goto-article article nil 'force)
5163 (if (fboundp 'gnus-summary-insert-cached-articles)
5164 (progn
5165 (gnus-summary-insert-cached-articles)
5166 (gnus-summary-goto-article article nil 'force))
5167 (message "Message could not be found.")))))
5168
5169 (defun org-follow-vm-link (&optional folder article readonly)
5170 "Follow a VM link to FOLDER and ARTICLE."
5171 (require 'vm)
5172 (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
5173 ;; ange-ftp or efs or tramp access
5174 (let ((user (or (match-string 1 folder) (user-login-name)))
5175 (host (match-string 2 folder))
5176 (file (match-string 3 folder)))
5177 (cond
5178 ((featurep 'tramp)
5179 ;; use tramp to access the file
5180 (if org-xemacs-p
5181 (setq folder (format "[%s@%s]%s" user host file))
5182 (setq folder (format "/%s@%s:%s" user host file))))
5183 (t
5184 ;; use ange-ftp or efs
5185 (require (if org-xemacs-p 'efs 'ange-ftp))
5186 (setq folder (format "/%s@%s:%s" user host file))))))
5187 (when folder
5188 (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
5189 (sit-for 0.1)
5190 (when article
5191 (vm-select-folder-buffer)
5192 (widen)
5193 (let ((case-fold-search t))
5194 (goto-char (point-min))
5195 (if (not (re-search-forward
5196 (concat "^" "message-id: *" (regexp-quote article))))
5197 (error "Could not find the specified message in this folder"))
5198 (vm-isearch-update)
5199 (vm-isearch-narrow)
5200 (vm-beginning-of-message)
5201 (vm-summarize)))))
5202
5203 (defun org-follow-wl-link (folder article)
5204 "Follow a Wanderlust link to FOLDER and ARTICLE."
5205 (wl-summary-goto-folder-subr folder 'no-sync t nil t)
5206 (if article (wl-summary-jump-to-msg-by-message-id article))
5207 (wl-summary-redisplay))
5208
5209 (defun org-follow-rmail-link (folder article)
5210 "Follow an RMAIL link to FOLDER and ARTICLE."
5211 (let (message-number)
5212 (save-excursion
5213 (save-window-excursion
5214 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
5215 (setq message-number
5216 (save-restriction
5217 (widen)
5218 (goto-char (point-max))
5219 (if (re-search-backward
5220 (concat "^Message-ID:\\s-+" (regexp-quote
5221 (or article "")))
5222 nil t)
5223 (rmail-what-message))))))
5224 (if message-number
5225 (progn
5226 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
5227 (rmail-show-message message-number)
5228 message-number)
5229 (error "Message not found"))))
5230
5231 (defun org-open-file (path &optional in-emacs line)
5232 "Open the file at PATH.
5233 First, this expands any special file name abbreviations. Then the
5234 configuration variable `org-file-apps' is checked if it contains an
5235 entry for this file type, and if yes, the corresponding command is launched.
5236 If no application is found, Emacs simply visits the file.
5237 With optional argument IN-EMACS, Emacs will visit the file.
5238 If the file does not exist, an error is thrown."
5239 (let* ((file (convert-standard-filename (org-expand-file-name path)))
5240 (dfile (downcase file))
5241 ext cmd apps)
5242 (if (and (not (file-exists-p file))
5243 (not org-open-non-existing-files))
5244 (error "No such file: %s" file))
5245 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
5246 (setq ext (match-string 1 dfile))
5247 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
5248 (setq ext (match-string 1 dfile))))
5249 (setq apps (append org-file-apps (org-default-apps)))
5250 (if in-emacs
5251 (setq cmd 'emacs)
5252 (setq cmd (or (cdr (assoc ext apps))
5253 (cdr (assoc t apps)))))
5254 (cond
5255 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
5256 (setq cmd (format cmd (concat "\"" file "\"")))
5257 (save-window-excursion
5258 (shell-command (concat cmd " & &"))))
5259 ((or (stringp cmd)
5260 (eq cmd 'emacs))
5261 (funcall (cdr (assq 'file org-link-frame-setup)) file)
5262 (if line (goto-line line)))
5263 ((consp cmd)
5264 (eval cmd))
5265 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))))
5266
5267 (defun org-default-apps ()
5268 "Return the default applications for this operating system."
5269 (cond
5270 ((eq system-type 'darwin)
5271 org-file-apps-defaults-macosx)
5272 ((eq system-type 'windows-nt)
5273 org-file-apps-defaults-windowsnt)
5274 ((eq system-type 'linux)
5275 org-file-apps-defaults-linux)
5276 (t org-file-apps-defaults-linux)))
5277
5278 (defun org-expand-file-name (path)
5279 "Replace special path abbreviations and expand the file name."
5280 (expand-file-name path))
5281
5282
5283 (defvar org-insert-link-history nil
5284 "Minibuffer history for links inserted with `org-insert-link'.")
5285
5286 (defvar org-stored-links nil
5287 "Contains the links stored with `org-store-link'.")
5288
5289 ;;;###autoload
5290 (defun org-store-link (arg)
5291 "\\<org-mode-map>Store an org-link to the current location.
5292 This link can later be inserted into an org-buffer with
5293 \\[org-insert-link].
5294 For some link types, a prefix arg is interpreted:
5295 For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
5296 For file links, arg negates `org-line-numbers-in-file-links'."
5297 (interactive "P")
5298 (let (link cpltxt)
5299 (cond
5300
5301 ((eq major-mode 'bbdb-mode)
5302 (setq cpltxt (concat
5303 "bbdb:"
5304 (or (bbdb-record-name (bbdb-current-record))
5305 (bbdb-record-company (bbdb-current-record))))
5306 link (org-make-link cpltxt)))
5307
5308 ((eq major-mode 'calendar-mode)
5309 (let ((cd (calendar-cursor-to-date)))
5310 (setq link
5311 (format-time-string
5312 (car org-time-stamp-formats)
5313 (apply 'encode-time
5314 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
5315 nil nil nil))))))
5316
5317 ((or (eq major-mode 'vm-summary-mode)
5318 (eq major-mode 'vm-presentation-mode))
5319 (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
5320 (vm-follow-summary-cursor)
5321 (save-excursion
5322 (vm-select-folder-buffer)
5323 (let* ((message (car vm-message-pointer))
5324 (folder (buffer-file-name))
5325 (subject (vm-su-subject message))
5326 (author (vm-su-full-name message))
5327 (message-id (vm-su-message-id message)))
5328 (setq folder (abbreviate-file-name folder))
5329 (if (string-match (concat "^" (regexp-quote vm-folder-directory))
5330 folder)
5331 (setq folder (replace-match "" t t folder)))
5332 (setq cpltxt (concat author " on: " subject))
5333 (setq link (concat cpltxt "\n "
5334 (org-make-link
5335 "vm:" folder "#" message-id))))))
5336
5337 ((eq major-mode 'wl-summary-mode)
5338 (let* ((msgnum (wl-summary-message-number))
5339 (message-id (elmo-message-field wl-summary-buffer-elmo-folder
5340 msgnum 'message-id))
5341 (wl-message-entity (elmo-msgdb-overview-get-entity
5342 msgnum (wl-summary-buffer-msgdb)))
5343 (author (wl-summary-line-from)) ; FIXME: how to get author name?
5344 (subject "???")) ; FIXME: How to get subject of email?
5345 (setq cpltxt (concat author " on: " subject))
5346 (setq link (concat cpltxt "\n "
5347 (org-make-link
5348 "wl:" wl-summary-buffer-folder-name
5349 "#" message-id)))))
5350
5351 ((eq major-mode 'rmail-mode)
5352 (save-excursion
5353 (save-restriction
5354 (rmail-narrow-to-non-pruned-header)
5355 (let ((folder (buffer-file-name))
5356 (message-id (mail-fetch-field "message-id"))
5357 (author (mail-fetch-field "from"))
5358 (subject (mail-fetch-field "subject")))
5359 (setq cpltxt (concat author " on: " subject))
5360 (setq link (concat cpltxt "\n "
5361 (org-make-link
5362 "rmail:" folder "#" message-id)))))))
5363
5364 ((eq major-mode 'gnus-group-mode)
5365 (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
5366 (gnus-group-group-name)) ; version
5367 ((fboundp 'gnus-group-name)
5368 (gnus-group-name))
5369 (t "???"))))
5370 (setq cpltxt (concat
5371 (if (org-xor arg org-usenet-links-prefer-google)
5372 "http://groups.google.com/groups?group="
5373 "gnus:")
5374 group)
5375 link (org-make-link cpltxt))))
5376
5377 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
5378 (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
5379 (gnus-summary-beginning-of-article)
5380 (let* ((group (car gnus-article-current))
5381 (article (cdr gnus-article-current))
5382 (header (gnus-summary-article-header article))
5383 (author (mail-header-from header))
5384 (message-id (mail-header-id header))
5385 (date (mail-header-date header))
5386 (subject (gnus-summary-subject-string)))
5387 (setq cpltxt (concat author " on: " subject))
5388 (if (org-xor arg org-usenet-links-prefer-google)
5389 (setq link
5390 (concat
5391 cpltxt "\n "
5392 (format "http://groups.google.com/groups?as_umsgid=%s"
5393 (org-fixup-message-id-for-http message-id))))
5394 (setq link (concat cpltxt "\n"
5395 (org-make-link
5396 "gnus:" group
5397 "#" (number-to-string article)))))))
5398
5399 ((eq major-mode 'w3-mode)
5400 (setq cpltxt (url-view-url t)
5401 link (org-make-link cpltxt)))
5402 ((eq major-mode 'w3m-mode)
5403 (setq cpltxt w3m-current-url
5404 link (org-make-link cpltxt)))
5405
5406 ((buffer-file-name)
5407 ;; Just link to this file here.
5408 (setq cpltxt (concat "file:"
5409 (abbreviate-file-name (buffer-file-name))))
5410 ;; Add the line number?
5411 (if (org-xor org-line-numbers-in-file-links arg)
5412 (setq cpltxt
5413 (concat cpltxt
5414 ":" (int-to-string
5415 (+ (if (bolp) 1 0) (count-lines
5416 (point-min) (point)))))))
5417 (setq link (org-make-link cpltxt)))
5418
5419 ((interactive-p)
5420 (error "Cannot link to a buffer which is not visiting a file"))
5421
5422 (t (setq link nil)))
5423
5424 (if (and (interactive-p) link)
5425 (progn
5426 (setq org-stored-links
5427 (cons (cons (or cpltxt link) link) org-stored-links))
5428 (message "Stored: %s" (or cpltxt link)))
5429 link)))
5430
5431 (defun org-make-link (&rest strings)
5432 "Concatenate STRINGS, format resulting string with `org-link-format'."
5433 (format org-link-format (apply 'concat strings)))
5434
5435 (defun org-xor (a b)
5436 "Exclusive or."
5437 (if a (not b) b))
5438
5439 (defun org-get-header (header)
5440 "Find a header field in the current buffer."
5441 (save-excursion
5442 (goto-char (point-min))
5443 (let ((case-fold-search t) s)
5444 (cond
5445 ((eq header 'from)
5446 (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
5447 (setq s (match-string 1)))
5448 (while (string-match "\"" s)
5449 (setq s (replace-match "" t t s)))
5450 (if (string-match "[<(].*" s)
5451 (setq s (replace-match "" t t s))))
5452 ((eq header 'message-id)
5453 (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
5454 (setq s (match-string 1))))
5455 ((eq header 'subject)
5456 (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
5457 (setq s (match-string 1)))))
5458 (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
5459 (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
5460 s)))
5461
5462
5463 (defun org-fixup-message-id-for-http (s)
5464 "Replace special characters in a message id, so it can be used in an http query."
5465 (while (string-match "<" s)
5466 (setq s (replace-match "%3C" t t s)))
5467 (while (string-match ">" s)
5468 (setq s (replace-match "%3E" t t s)))
5469 (while (string-match "@" s)
5470 (setq s (replace-match "%40" t t s)))
5471 s)
5472
5473 (defun org-insert-link (&optional complete-file)
5474 "Insert a link. At the prompt, enter the link.
5475
5476 Completion can be used to select a link previously stored with
5477 `org-store-link'. When the empty string is entered (i.e. if you just
5478 press RET at the prompt), the link defaults to the most recently
5479 stored link. As SPC triggers completion in the minibuffer, you need to
5480 use M-SPC or C-q SPC to force the insertion of a space character.
5481
5482 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
5483 selected using completion. The path to the file will be relative to
5484 the current directory if the file is in the current directory or a
5485 subdirectory. Otherwise, the link will be the absolute path as
5486 completed in the minibuffer (i.e. normally ~/path/to/file).
5487
5488 With two \\[universal-argument] prefixes, enforce an absolute path even if the file
5489 is in the current directory or below."
5490 (interactive "P")
5491 (let ((link (if complete-file
5492 (read-file-name "File: ")
5493 (completing-read
5494 "Link: " org-stored-links nil nil nil
5495 org-insert-link-history
5496 (or (car (car org-stored-links))))))
5497 linktxt matched)
5498 (if (or (not link) (equal link ""))
5499 (error "No links available"))
5500 (if complete-file
5501 (let ((pwd (file-name-as-directory (expand-file-name "."))))
5502 (cond
5503 ((equal complete-file '(16))
5504 (insert
5505 (org-make-link
5506 "file:" (abbreviate-file-name (expand-file-name link)))))
5507 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
5508 (expand-file-name link))
5509 (insert
5510 (org-make-link
5511 "file:" (match-string 1 (expand-file-name link)))))
5512 (t (insert (org-make-link "file:" link)))))
5513 (setq linktxt (cdr (assoc link org-stored-links)))
5514 (if (not org-keep-stored-link-after-insertion)
5515 (setq org-stored-links (delq (assoc link org-stored-links)
5516 org-stored-links)))
5517 (if (not linktxt) (setq link (org-make-link link)))
5518 (let ((lines (org-split-string (or linktxt link) "\n")))
5519 (insert (car lines))
5520 (setq matched (string-match org-link-regexp (car lines)))
5521 (setq lines (cdr lines))
5522 (while lines
5523 (insert "\n")
5524 (if (save-excursion
5525 (beginning-of-line 0)
5526 (looking-at "[ \t]+\\S-"))
5527 (indent-relative))
5528 (setq matched (or matched
5529 (string-match org-link-regexp (car lines))))
5530 (insert (car lines))
5531 (setq lines (cdr lines))))
5532 (unless matched
5533 (error "Add link type: http(s),ftp,mailto,file,news,bbdb,vm,wl,rmail,gnus, or shell")))))
5534
5535 ;;; Hooks for remember.el
5536 ;;;###autoload
5537 (defun org-remember-annotation ()
5538 "Return a link to the current location as an annotation for remember.el.
5539 If you are using Org-mode files as target for data storage with
5540 remember.el, then the annotations should include a link compatible with the
5541 conventions in Org-mode. This function returns such a link."
5542 (org-store-link nil))
5543
5544 (defconst org-remember-help
5545 "Select a destination location for the note.
5546 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
5547 RET at beg-of-buf -> Append to file as level 2 headline
5548 RET on headline -> Store as sublevel entry to current headline
5549 <left>/<right> -> before/after current headline, same headings level")
5550
5551 ;;;###autoload
5552 (defun org-remember-handler ()
5553 "Store stuff from remember.el into an org file.
5554 First prompts for an org file. If the user just presses return, the value
5555 of `org-default-notes-file' is used.
5556 Then the command offers the headings tree of the selected file in order to
5557 file the text at a specific location.
5558 You can either immediately press RET to get the note appended to the
5559 file. Or you can use vertical cursor motion and visibility cycling (TAB) to
5560 find a better place. Then press RET or <left> or <right> in insert the note.
5561
5562 Key Cursor position Note gets inserted
5563 -----------------------------------------------------------------------------
5564 RET buffer-start as level 2 heading at end of file
5565 RET on headline as sublevel of the heading at cursor
5566 RET no heading at cursor position, level taken from context.
5567 Or use prefix arg to specify level manually.
5568 <left> on headline as same level, before current heading
5569 <right> on headline as same level, after current heading
5570
5571 So the fastest way to store the note is to press RET RET to append it to
5572 the default file. This way your current train of thought is not
5573 interrupted, in accordance with the principles of remember.el. But with
5574 little extra effort, you can push it directly to the correct location.
5575
5576 Before being stored away, the function ensures that the text has a
5577 headline, i.e. a first line that starts with a \"*\". If not, a headline
5578 is constructed from the current date and some additional data.
5579
5580 If the variable `org-adapt-indentation' is non-nil, the entire text is
5581 also indented so that it starts in the same column as the headline
5582 \(i.e. after the stars).
5583
5584 See also the variable `org-reverse-note-order'."
5585 (catch 'quit
5586 (let* ((txt (buffer-substring (point-min) (point-max)))
5587 (fastp current-prefix-arg)
5588 (file (if fastp org-default-notes-file (org-get-org-file)))
5589 (visiting (find-buffer-visiting file))
5590 (org-startup-with-deadline-check nil)
5591 (org-startup-folded nil)
5592 spos level indent reversed)
5593 ;; Modify text so that it becomes a nice subtree which can be inserted
5594 ;; into an org tree.
5595 (let* ((lines (split-string txt "\n"))
5596 (first (car lines))
5597 (lines (cdr lines)))
5598 (if (string-match "^\\*+" first)
5599 ;; Is already a headline
5600 (setq indent (make-string (- (match-end 0) (match-beginning 0)
5601 -1) ?\ ))
5602 ;; We need to add a headline: Use time and first buffer line
5603 (setq lines (cons first lines)
5604 first (concat "* " (current-time-string)
5605 " (" (remember-buffer-desc) ")")
5606 indent " "))
5607 (if org-adapt-indentation
5608 (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
5609 (setq txt (concat first "\n"
5610 (mapconcat 'identity lines "\n"))))
5611 ;; Find the file
5612 (if (not visiting)
5613 (find-file-noselect file))
5614 (with-current-buffer (get-file-buffer file)
5615 (setq reversed (org-notes-order-reversed-p))
5616 (save-excursion
5617 (save-restriction
5618 (widen)
5619 ;; Ask the User for a location
5620 (setq spos (if fastp 1 (org-get-location
5621 (current-buffer)
5622 org-remember-help)))
5623 (if (not spos) (throw 'quit nil)) ; return nil to show we did
5624 ; not handle this note
5625 (goto-char spos)
5626 (cond ((bobp)
5627 ;; Put it at the start or end, as level 2
5628 (save-restriction
5629 (widen)
5630 (goto-char (if reversed (point-min) (point-max)))
5631 (if (not (bolp)) (newline))
5632 (org-paste-subtree (or current-prefix-arg 2) txt)))
5633 ((and (org-on-heading-p nil) (not current-prefix-arg))
5634 ;; Put it below this entry, at the beg/end of the subtree
5635 (org-back-to-heading)
5636 (setq level (outline-level))
5637 (if reversed
5638 (outline-end-of-heading)
5639 (outline-end-of-subtree))
5640 (if (not (bolp)) (newline))
5641 (beginning-of-line 1)
5642 (org-paste-subtree (1+ level) txt))
5643 (t
5644 ;; Put it right there, with automatic level determined by
5645 ;; org-paste-subtree or from prefix arg
5646 (org-paste-subtree current-prefix-arg txt)))
5647 (when remember-save-after-remembering
5648 (save-buffer)
5649 (if (not visiting) (kill-buffer (current-buffer)))))))))
5650 t) ;; return t to indicate that we took care of this note.
5651
5652 (defun org-get-org-file ()
5653 "Read a filename, with default directory `org-directory'."
5654 (let ((default (or org-default-notes-file remember-data-file)))
5655 (read-file-name (format "File name [%s]: " default)
5656 (file-name-as-directory org-directory)
5657 default)))
5658
5659 (defun org-notes-order-reversed-p ()
5660 "Check if the current file should receive notes in reversed order."
5661 (cond
5662 ((not org-reverse-note-order) nil)
5663 ((eq t org-reverse-note-order) t)
5664 ((not (listp org-reverse-note-order)) nil)
5665 (t (catch 'exit
5666 (let ((all org-reverse-note-order)
5667 entry)
5668 (while (setq entry (pop all))
5669 (if (string-match (car entry) (buffer-file-name))
5670 (throw 'exit (cdr entry))))
5671 nil)))))
5672
5673 ;;; Tables
5674
5675 ;; Watch out: Here we are talking about two different kind of tables.
5676 ;; Most of the code is for the tables created with the Org-mode table editor.
5677 ;; Sometimes, we talk about tables created and edited with the table.el
5678 ;; Emacs package. We call the former org-type tables, and the latter
5679 ;; table.el-type tables.
5680
5681
5682 (defun org-before-change-function (beg end)
5683 "Every change indicates that a table might need an update."
5684 (setq org-table-may-need-update t))
5685
5686 (defconst org-table-line-regexp "^[ \t]*|"
5687 "Detects an org-type table line.")
5688 (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
5689 "Detects an org-type table line.")
5690 (defconst org-table-hline-regexp "^[ \t]*|-"
5691 "Detects an org-type table hline.")
5692 (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
5693 "Detects a table-type table hline.")
5694 (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
5695 "Detects an org-type or table-type table.")
5696 (defconst org-table-border-regexp "^[ \t]*[^| \t]"
5697 "Searching from within a table (any type) this finds the first line
5698 outside the table.")
5699 (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
5700 "Searching from within a table (any type) this finds the first line
5701 outside the table.")
5702
5703 (defun org-table-create-with-table.el ()
5704 "Use the table.el package to insert a new table.
5705 If there is already a table at point, convert between Org-mode tables
5706 and table.el tables."
5707 (interactive)
5708 (require 'table)
5709 (cond
5710 ((org-at-table.el-p)
5711 (if (y-or-n-p "Convert table to Org-mode table? ")
5712 (org-table-convert)))
5713 ((org-at-table-p)
5714 (if (y-or-n-p "Convert table to table.el table? ")
5715 (org-table-convert)))
5716 (t (call-interactively 'table-insert))))
5717
5718 (defun org-table-create (&optional size)
5719 "Query for a size and insert a table skeleton.
5720 SIZE is a string Columns x Rows like for example \"3x2\"."
5721 (interactive "P")
5722 (unless size
5723 (setq size (read-string
5724 (concat "Table size Columns x Rows [e.g. "
5725 org-table-default-size "]: ")
5726 "" nil org-table-default-size)))
5727
5728 (let* ((pos (point))
5729 (indent (make-string (current-column) ?\ ))
5730 (split (org-split-string size " *x *"))
5731 (rows (string-to-number (nth 1 split)))
5732 (columns (string-to-number (car split)))
5733 (line (concat (apply 'concat indent "|" (make-list columns " |"))
5734 "\n")))
5735 (if (string-match "^[ \t]*$" (buffer-substring-no-properties
5736 (point-at-bol) (point)))
5737 (beginning-of-line 1)
5738 (newline))
5739 ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
5740 (dotimes (i rows) (insert line))
5741 (goto-char pos)
5742 (if (> rows 1)
5743 ;; Insert a hline after the first row.
5744 (progn
5745 (end-of-line 1)
5746 (insert "\n|-")
5747 (goto-char pos)))
5748 (org-table-align)))
5749
5750 (defun org-table-convert-region (beg0 end0 nspace)
5751 "Convert region to a table.
5752 The region goes from BEG0 to END0, but these borders will be moved
5753 slightly, to make sure a beginning of line in the first line is included.
5754 When NSPACE is non-nil, it indicates the minimum number of spaces that
5755 separate columns (default: just one space)"
5756 (let* ((beg (min beg0 end0))
5757 (end (max beg0 end0))
5758 (tabsep t)
5759 re)
5760 (goto-char beg)
5761 (beginning-of-line 1)
5762 (setq beg (move-marker (make-marker) (point)))
5763 (goto-char end)
5764 (if (bolp) (backward-char 1) (end-of-line 1))
5765 (setq end (move-marker (make-marker) (point)))
5766 ;; Lets see if this is tab-separated material. If every nonempty line
5767 ;; contains a tab, we will assume that it is tab-separated material
5768 (if nspace
5769 (setq tabsep nil)
5770 (goto-char beg)
5771 (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
5772 (if nspace (setq tabsep nil))
5773 (if tabsep
5774 (setq re "^\\|\t")
5775 (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
5776 (max 1 (prefix-numeric-value nspace)))))
5777 (goto-char beg)
5778 (while (re-search-forward re end t)
5779 (replace-match "|" t t))
5780 (goto-char beg)
5781 (insert " ")
5782 (org-table-align)))
5783
5784 (defun org-table-import (file arg)
5785 "Import FILE as a table.
5786 The file is assumed to be tab-separated. Such files can be produced by most
5787 spreadsheet and database applications. If no tabs (at least one per line)
5788 are found, lines will be split on whitespace into fields."
5789 (interactive "f\nP")
5790 (or (bolp) (newline))
5791 (let ((beg (point))
5792 (pm (point-max)))
5793 (insert-file-contents file)
5794 (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
5795
5796 (defun org-table-export ()
5797 "Export table as a tab-separated file.
5798 Such a file can be imported into a spreadsheet program like Excel."
5799 (interactive)
5800 (let* ((beg (org-table-begin))
5801 (end (org-table-end))
5802 (table (buffer-substring beg end))
5803 (file (read-file-name "Export table to: "))
5804 buf)
5805 (unless (or (not (file-exists-p file))
5806 (y-or-n-p (format "Overwrite file %s? " file)))
5807 (error "Abort"))
5808 (with-current-buffer (find-file-noselect file)
5809 (setq buf (current-buffer))
5810 (erase-buffer)
5811 (fundamental-mode)
5812 (insert table)
5813 (goto-char (point-min))
5814 (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
5815 (replace-match "" t t)
5816 (end-of-line 1))
5817 (goto-char (point-min))
5818 (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
5819 (replace-match "" t t)
5820 (goto-char (min (1+ (point)) (point-max))))
5821 (goto-char (point-min))
5822 (while (re-search-forward "^-[-+]*$" nil t)
5823 (replace-match "")
5824 (if (looking-at "\n")
5825 (delete-char 1)))
5826 (goto-char (point-min))
5827 (while (re-search-forward "[ \t]*|[ \t]*" nil t)
5828 (replace-match "\t" t t))
5829 (save-buffer))
5830 (kill-buffer buf)))
5831
5832 (defvar org-table-aligned-begin-marker (make-marker)
5833 "Marker at the beginning of the table last aligned.
5834 Used to check if cursor still is in that table, to minimize realignment.")
5835 (defvar org-table-aligned-end-marker (make-marker)
5836 "Marker at the end of the table last aligned.
5837 Used to check if cursor still is in that table, to minimize realignment.")
5838 (defvar org-table-last-alignment nil
5839 "List of flags for flushright alignment, from the last re-alignment.
5840 This is being used to correctly align a single field after TAB or RET.")
5841 ;; FIXME: The following is currently not used.
5842 (defvar org-table-last-column-widths nil
5843 "List of max width of fields in each column.
5844 This is being used to correctly align a single field after TAB or RET.")
5845
5846
5847 (defun org-table-align ()
5848 "Align the table at point by aligning all vertical bars."
5849 (interactive)
5850 (let* (
5851 ;; Limits of table
5852 (beg (org-table-begin))
5853 (end (org-table-end))
5854 ;; Current cursor position
5855 (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
5856 (colpos (org-table-current-column))
5857 (winstart (window-start))
5858 text lines (new "") lengths l typenums ty fields maxfields i
5859 column
5860 (indent "") cnt frac
5861 rfmt hfmt
5862 (spaces (if (org-in-invisibility-spec-p '(org-table))
5863 org-table-spaces-around-invisible-separators
5864 org-table-spaces-around-separators))
5865 (sp1 (car spaces))
5866 (sp2 (cdr spaces))
5867 (rfmt1 (concat
5868 (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
5869 (hfmt1 (concat
5870 (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
5871 emptystrings)
5872 (untabify beg end)
5873 ;; (message "Aligning table...")
5874 ;; Get the rows
5875 (setq lines (org-split-string
5876 (buffer-substring-no-properties beg end) "\n"))
5877 ;; Store the indentation of the first line
5878 (if (string-match "^ *" (car lines))
5879 (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
5880 ;; Mark the hlines
5881 (setq lines (mapcar (lambda (l) (if (string-match "^ *|-" l) nil l))
5882 lines))
5883 ;; Get the data fields
5884 (setq fields (mapcar
5885 (lambda (l)
5886 (org-split-string l " *| *"))
5887 (delq nil (copy-sequence lines))))
5888 ;; How many fields in the longest line?
5889 (condition-case nil
5890 (setq maxfields (apply 'max (mapcar 'length fields)))
5891 (error
5892 (kill-region beg end)
5893 (org-table-create org-table-default-size)
5894 (error "Empty table - created default table")))
5895 ;; A list of empty string to fill any short rows on output
5896 (setq emptystrings (make-list maxfields ""))
5897 ;; Get the maximum length of a field and the most common datatype
5898 ;; for each column
5899 (setq i -1)
5900 (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
5901 (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
5902 ;; maximum length
5903 (push (apply 'max 1 (mapcar 'length column)) lengths)
5904 ;; compute the fraction stepwise, ignoring empty fields
5905 (setq cnt 0 frac 0.0)
5906 (mapcar
5907 (lambda (x)
5908 (if (equal x "")
5909 nil
5910 (setq frac ( / (+ (* frac cnt)
5911 (if (string-match org-table-number-regexp x) 1 0))
5912 (setq cnt (1+ cnt))))))
5913 column)
5914 (push (>= frac org-table-number-fraction) typenums))
5915 (setq lengths (nreverse lengths)
5916 typenums (nreverse typenums))
5917 (setq org-table-last-alignment typenums
5918 org-table-last-column-widths lengths)
5919 ;; Compute the formats needed for output of the table
5920 (setq rfmt (concat indent "|") hfmt (concat indent "|"))
5921 (while (setq l (pop lengths))
5922 (setq ty (if (pop typenums) "" "-")) ; number types flushright
5923 (setq rfmt (concat rfmt (format rfmt1 ty l))
5924 hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
5925 (setq rfmt (concat rfmt "\n")
5926 hfmt (concat (substring hfmt 0 -1) "|\n"))
5927 ;; Produce the new table
5928 ;;(while lines
5929 ;; (setq l (pop lines))
5930 ;; (if l
5931 ;; (setq new (concat new (apply 'format rfmt
5932 ;; (append (pop fields) emptystrings))))
5933 ;; (setq new (concat new hfmt))))
5934 (setq new (mapconcat
5935 (lambda (l)
5936 (if l (apply 'format rfmt
5937 (append (pop fields) emptystrings))
5938 hfmt))
5939 lines ""))
5940 ;; Replace the old one
5941 (delete-region beg end)
5942 (move-marker end nil)
5943 (move-marker org-table-aligned-begin-marker (point))
5944 (insert new)
5945 (move-marker org-table-aligned-end-marker (point))
5946 ;; Try to move to the old location (approximately)
5947 (goto-line linepos)
5948 (set-window-start (selected-window) winstart 'noforce)
5949 (org-table-goto-column colpos)
5950 (setq org-table-may-need-update nil)
5951 (if (org-in-invisibility-spec-p '(org-table))
5952 (org-table-add-invisible-to-vertical-lines))
5953 ))
5954
5955 (defun org-table-begin (&optional table-type)
5956 "Find the beginning of the table and return its position.
5957 With argument TABLE-TYPE, go to the beginning of a table.el-type table."
5958 (save-excursion
5959 (if (not (re-search-backward
5960 (if table-type org-table-any-border-regexp
5961 org-table-border-regexp)
5962 nil t))
5963 (error "Can't find beginning of table")
5964 (goto-char (match-beginning 0))
5965 (beginning-of-line 2)
5966 (point))))
5967
5968 (defun org-table-end (&optional table-type)
5969 "Find the end of the table and return its position.
5970 With argument TABLE-TYPE, go to the end of a table.el-type table."
5971 (save-excursion
5972 (if (not (re-search-forward
5973 (if table-type org-table-any-border-regexp
5974 org-table-border-regexp)
5975 nil t))
5976 (goto-char (point-max))
5977 (goto-char (match-beginning 0)))
5978 (point-marker)))
5979
5980 (defun org-table-justify-field-maybe ()
5981 "Justify the current field, text to left, number to right."
5982 (cond
5983 (org-table-may-need-update) ; Realignment will happen anyway, don't bother
5984 ((org-at-table-hline-p)
5985 ;; This is pretty stupid, but I don't know how to deal with hlines
5986 (setq org-table-may-need-update t))
5987 ((or (not (equal (marker-buffer org-table-aligned-begin-marker)
5988 (current-buffer)))
5989 (< (point) org-table-aligned-begin-marker)
5990 (>= (point) org-table-aligned-end-marker))
5991 ;; This is not the same table, force a full re-align
5992 (setq org-table-may-need-update t))
5993 (t ;; realign the current field, based on previous full realign
5994 (let* ((pos (point)) s org-table-may-need-update
5995 (col (org-table-current-column))
5996 (num (nth (1- col) org-table-last-alignment))
5997 l f)
5998 (when (> col 0)
5999 (skip-chars-backward "^|\n")
6000 (if (looking-at " *\\([^|\n]*?\\) *|")
6001 (progn
6002 (setq s (match-string 1)
6003 l (max 1 (- (match-end 0) (match-beginning 0) 3)))
6004 (setq f (format (if num " %%%ds |" " %%-%ds |") l))
6005 (replace-match (format f s t t)))
6006 (setq org-table-may-need-update t))
6007 (goto-char pos))))))
6008
6009 (defun org-table-next-field ()
6010 "Go to the next field in the current table.
6011 Before doing so, re-align the table if necessary."
6012 (interactive)
6013 (if (and org-table-automatic-realign
6014 org-table-may-need-update)
6015 (org-table-align))
6016 (if (org-at-table-hline-p)
6017 (end-of-line 1))
6018 (condition-case nil
6019 (progn
6020 (re-search-forward "|" (org-table-end))
6021 (if (looking-at "[ \t]*$")
6022 (re-search-forward "|" (org-table-end)))
6023 (if (looking-at "-")
6024 (progn
6025 (beginning-of-line 0)
6026 (org-table-insert-row 'below))
6027 (if (looking-at " ") (forward-char 1))))
6028 (error
6029 (org-table-insert-row 'below))))
6030
6031 (defun org-table-previous-field ()
6032 "Go to the previous field in the table.
6033 Before doing so, re-align the table if necessary."
6034 (interactive)
6035 (if (and org-table-automatic-realign
6036 org-table-may-need-update)
6037 (org-table-align))
6038 (if (org-at-table-hline-p)
6039 (end-of-line 1))
6040 (re-search-backward "|" (org-table-begin))
6041 (re-search-backward "|" (org-table-begin))
6042 (while (looking-at "|\\(-\\|[ \t]*$\\)")
6043 (re-search-backward "|" (org-table-begin)))
6044 (if (looking-at "| ?")
6045 (goto-char (match-end 0))))
6046
6047 (defun org-table-next-row ()
6048 "Go to the next row (same column) in the current table.
6049 Before doing so, re-align the table if necessary."
6050 (interactive)
6051 (if (or (looking-at "[ \t]*$")
6052 (save-excursion (skip-chars-backward " \t") (bolp)))
6053 (newline)
6054 (if (and org-table-automatic-realign
6055 org-table-may-need-update)
6056 (org-table-align))
6057 (let ((col (org-table-current-column)))
6058 (beginning-of-line 2)
6059 (if (or (not (org-at-table-p))
6060 (org-at-table-hline-p))
6061 (progn
6062 (beginning-of-line 0)
6063 (org-table-insert-row 'below)))
6064 (org-table-goto-column col)
6065 (skip-chars-backward "^|\n\r")
6066 (if (looking-at " ") (forward-char 1)))))
6067
6068 (defun org-table-copy-down (n)
6069 "Copy a field down in the current column.
6070 If the field at the cursor is empty, copy into it the content of the nearest
6071 non-empty field above. With argument N, use the Nth non-empty field.
6072 If the current field is not empty, it is copied down to the next row, and
6073 the cursor is moved with it. Therefore, repeating this command causes the
6074 column to be filled row-by-row.
6075 If the variable `org-table-copy-increment' is non-nil and the field is an
6076 integer, it will be incremented while copying."
6077 (interactive "p")
6078 (let* ((colpos (org-table-current-column))
6079 (field (org-table-get-field))
6080 (non-empty (string-match "[^ \t]" field))
6081 (beg (org-table-begin))
6082 txt)
6083 (org-table-check-inside-data-field)
6084 (if non-empty (progn (org-table-next-row) (org-table-blank-field)))
6085 (if (save-excursion
6086 (setq txt
6087 (catch 'exit
6088 (while (progn (beginning-of-line 1)
6089 (re-search-backward org-table-dataline-regexp
6090 beg t))
6091 (org-table-goto-column colpos t)
6092 (if (and (looking-at
6093 "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
6094 (= (setq n (1- n)) 0))
6095 (throw 'exit (match-string 1)))))))
6096 (progn
6097 (if (and org-table-copy-increment
6098 (string-match "^[0-9]+$" txt))
6099 (setq txt (format "%d" (+ (string-to-int txt) 1))))
6100 (insert txt)
6101 (org-table-align))
6102 (error "No non-empty field found"))))
6103
6104 (defun org-table-check-inside-data-field ()
6105 "Is point inside a table data field?
6106 I.e. not on a hline or before the first or after the last column?"
6107 (if (or (not (org-at-table-p))
6108 (= (org-table-current-column) 0)
6109 (org-at-table-hline-p)
6110 (looking-at "[ \t]*$"))
6111 (error "Not in table data field")))
6112
6113 (defvar org-table-clip nil
6114 "Clipboard for table regions.")
6115
6116 (defun org-table-blank-field ()
6117 "Blank the current table field or active region."
6118 (interactive)
6119 (org-table-check-inside-data-field)
6120 (if (and (interactive-p) (org-region-active-p))
6121 (let (org-table-clip)
6122 (org-table-cut-region))
6123 (skip-chars-backward "^|")
6124 (backward-char 1)
6125 (if (looking-at "|[^|]+")
6126 (let* ((pos (match-beginning 0))
6127 (match (match-string 0))
6128 (len (length match)))
6129 (replace-match (concat "|" (make-string (1- len) ?\ )))
6130 (goto-char (+ 2 pos))
6131 (substring match 1)))))
6132
6133 (defun org-table-get-field (&optional n replace)
6134 "Return the value of the field in column N of current row.
6135 N defaults to current field.
6136 If REPLACE is a string, replace field with this value. The return value
6137 is always the old value."
6138 (and n (org-table-goto-column n))
6139 (skip-chars-backward "^|")
6140 (backward-char 1)
6141 (if (looking-at "|[^|\r\n]*")
6142 (let* ((pos (match-beginning 0))
6143 (val (buffer-substring (1+ pos) (match-end 0))))
6144 (if replace
6145 (replace-match (concat "|" replace)))
6146 (goto-char (+ 2 pos))
6147 val)))
6148
6149 (defun org-table-current-column ()
6150 "Find out which column we are in.
6151 When called interactively, column is also displayed in echo area."
6152 (interactive)
6153 (if (interactive-p) (org-table-check-inside-data-field))
6154 (save-excursion
6155 (let ((cnt 0) (pos (point)))
6156 (beginning-of-line 1)
6157 (while (search-forward "|" pos t)
6158 (setq cnt (1+ cnt)))
6159 (if (interactive-p) (message "This is table column %d" cnt))
6160 cnt)))
6161
6162 (defun org-table-goto-column (n &optional on-delim force)
6163 "Move the cursor to the Nth column in the current table line.
6164 With optional argument ON-DELIM, stop with point before the left delimiter
6165 of the field.
6166 If there are less than N fields, just go to after the last delimiter.
6167 However, when FORCE is non-nil, create new columns if necessary."
6168 (let ((pos (point-at-eol)))
6169 (beginning-of-line 1)
6170 (when (> n 0)
6171 (while (and (> (setq n (1- n)) -1)
6172 (or (search-forward "|" pos t)
6173 (and force
6174 (progn (end-of-line 1)
6175 (skip-chars-backward "^|")
6176 (insert " |")
6177 (backward-char 2) t)))))
6178 (when (and force (not (looking-at ".*|")))
6179 (save-excursion (end-of-line 1) (insert "|")))
6180 (if on-delim
6181 (backward-char 1)
6182 (if (looking-at " ") (forward-char 1))))))
6183
6184 (defun org-at-table-p (&optional table-type)
6185 "Return t if the cursor is inside an org-type table.
6186 If TABLE-TYPE is non-nil, also chack for table.el-type tables."
6187 (if org-enable-table-editor
6188 (save-excursion
6189 (beginning-of-line 1)
6190 (looking-at (if table-type org-table-any-line-regexp
6191 org-table-line-regexp)))
6192 nil))
6193
6194 (defun org-table-recognize-table.el ()
6195 "If there is a table.el table nearby, recognize it and move into it."
6196 (if org-table-tab-recognizes-table.el
6197 (if (org-at-table.el-p)
6198 (progn
6199 (beginning-of-line 1)
6200 (if (looking-at org-table-dataline-regexp)
6201 nil
6202 (if (looking-at org-table1-hline-regexp)
6203 (progn
6204 (beginning-of-line 2)
6205 (if (looking-at org-table-any-border-regexp)
6206 (beginning-of-line -1)))))
6207 (if (re-search-forward "|" (org-table-end t) t)
6208 (progn
6209 (require 'table)
6210 (if (table--at-cell-p (point))
6211 t
6212 (message "recognizing table.el table...")
6213 (table-recognize-table)
6214 (message "recognizing table.el table...done")))
6215 (error "This should not happen..."))
6216 t)
6217 nil)
6218 nil))
6219
6220 (defun org-at-table.el-p ()
6221 "Return t if the cursor is inside a table.el-type table."
6222 (save-excursion
6223 (if (org-at-table-p 'any)
6224 (progn
6225 (goto-char (org-table-begin 'any))
6226 (looking-at org-table1-hline-regexp))
6227 nil)))
6228
6229 (defun org-at-table-hline-p ()
6230 "Return t if the cursor is inside a hline in a table."
6231 (if org-enable-table-editor
6232 (save-excursion
6233 (beginning-of-line 1)
6234 (looking-at org-table-hline-regexp))
6235 nil))
6236
6237 (defun org-table-insert-column ()
6238 "Insert a new column into the table."
6239 (interactive)
6240 (if (not (org-at-table-p))
6241 (error "Not at a table"))
6242 (org-table-find-dataline)
6243 (let* ((col (max 1 (org-table-current-column)))
6244 (beg (org-table-begin))
6245 (end (org-table-end))
6246 ;; Current cursor position
6247 (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
6248 (colpos col))
6249 (goto-char beg)
6250 (while (< (point) end)
6251 (if (org-at-table-hline-p)
6252 nil
6253 (org-table-goto-column col t)
6254 (insert "| "))
6255 (beginning-of-line 2))
6256 (move-marker end nil)
6257 (goto-line linepos)
6258 (org-table-goto-column colpos))
6259 (org-table-align))
6260
6261 (defun org-table-find-dataline ()
6262 "Find a dataline in the current table, which is needed for column commands."
6263 (if (and (org-at-table-p)
6264 (not (org-at-table-hline-p)))
6265 t
6266 (let ((col (current-column))
6267 (end (org-table-end)))
6268 (move-to-column col)
6269 (while (and (< (point) end)
6270 (or (not (= (current-column) col))
6271 (org-at-table-hline-p)))
6272 (beginning-of-line 2)
6273 (move-to-column col))
6274 (if (and (org-at-table-p)
6275 (not (org-at-table-hline-p)))
6276 t
6277 (error
6278 "Please position cursor in a data line for column operations")))))
6279
6280 (defun org-table-delete-column ()
6281 "Delete a column into the table."
6282 (interactive)
6283 (if (not (org-at-table-p))
6284 (error "Not at a table"))
6285 (org-table-find-dataline)
6286 (org-table-check-inside-data-field)
6287 (let* ((col (org-table-current-column))
6288 (beg (org-table-begin))
6289 (end (org-table-end))
6290 ;; Current cursor position
6291 (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
6292 (colpos col))
6293 (goto-char beg)
6294 (while (< (point) end)
6295 (if (org-at-table-hline-p)
6296 nil
6297 (org-table-goto-column col t)
6298 (and (looking-at "|[^|\n]+|")
6299 (replace-match "|")))
6300 (beginning-of-line 2))
6301 (move-marker end nil)
6302 (goto-line linepos)
6303 (org-table-goto-column colpos))
6304 (org-table-align))
6305
6306 (defun org-table-move-column-right ()
6307 "Move column to the right."
6308 (interactive)
6309 (org-table-move-column nil))
6310 (defun org-table-move-column-left ()
6311 "Move column to the left."
6312 (interactive)
6313 (org-table-move-column 'left))
6314
6315 (defun org-table-move-column (&optional left)
6316 "Move the current column to the right. With arg LEFT, move to the left."
6317 (interactive "P")
6318 (if (not (org-at-table-p))
6319 (error "Not at a table"))
6320 (org-table-find-dataline)
6321 (org-table-check-inside-data-field)
6322 (let* ((col (org-table-current-column))
6323 (col1 (if left (1- col) col))
6324 (beg (org-table-begin))
6325 (end (org-table-end))
6326 ;; Current cursor position
6327 (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
6328 (colpos (if left (1- col) (1+ col))))
6329 (if (and left (= col 1))
6330 (error "Cannot move column further left"))
6331 (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
6332 (error "Cannot move column further right"))
6333 (goto-char beg)
6334 (while (< (point) end)
6335 (if (org-at-table-hline-p)
6336 nil
6337 (org-table-goto-column col1 t)
6338 (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
6339 (replace-match "|\\2|\\1|")))
6340 (beginning-of-line 2))
6341 (move-marker end nil)
6342 (goto-line linepos)
6343 (org-table-goto-column colpos))
6344 (org-table-align))
6345
6346 (defun org-table-move-row-down ()
6347 "Move table row down."
6348 (interactive)
6349 (org-table-move-row nil))
6350 (defun org-table-move-row-up ()
6351 "Move table row up."
6352 (interactive)
6353 (org-table-move-row 'up))
6354
6355 (defun org-table-move-row (&optional up)
6356 "Move the current table line down. With arg UP, move it up."
6357 (interactive "P")
6358 (let ((col (current-column))
6359 (pos (point))
6360 (tonew (if up 0 2))
6361 txt)
6362 (beginning-of-line tonew)
6363 (if (not (org-at-table-p))
6364 (progn
6365 (goto-char pos)
6366 (error "Cannot move row further")))
6367 (goto-char pos)
6368 (beginning-of-line 1)
6369 (setq pos (point))
6370 (setq txt (buffer-substring (point) (1+ (point-at-eol))))
6371 (delete-region (point) (1+ (point-at-eol)))
6372 (beginning-of-line tonew)
6373 (insert txt)
6374 (beginning-of-line 0)
6375 (move-to-column col)))
6376
6377 (defun org-table-insert-row (&optional arg)
6378 "Insert a new row above the current line into the table.
6379 With prefix ARG, insert below the current line."
6380 (interactive "P")
6381 (if (not (org-at-table-p))
6382 (error "Not at a table"))
6383 (let ((line (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
6384 (if (string-match "^[ \t]*|-" line)
6385 (setq line (mapcar (lambda (x) (if (member x '(?| ?+)) ?| ?\ )) line))
6386 (setq line (mapcar (lambda (x) (if (equal x ?|) ?| ?\ )) line)))
6387 (beginning-of-line (if arg 2 1))
6388 (let (org-table-may-need-update)
6389 (apply 'insert-before-markers line)
6390 (insert-before-markers "\n"))
6391 (beginning-of-line 0)
6392 (re-search-forward "| ?" (point-at-eol) t)
6393 (and org-table-may-need-update (org-table-align))))
6394
6395 (defun org-table-insert-hline (&optional arg)
6396 "Insert a horizontal-line below the current line into the table.
6397 With prefix ARG, insert above the current line."
6398 (interactive "P")
6399 (if (not (org-at-table-p))
6400 (error "Not at a table"))
6401 (let ((line (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
6402 (col (current-column))
6403 start)
6404 (if (string-match "^[ \t]*|-" line)
6405 (setq line
6406 (mapcar (lambda (x) (if (member x '(?| ?+))
6407 (prog1 (if start ?+ ?|) (setq start t))
6408 (if start ?- ?\ )))
6409 line))
6410 (setq line
6411 (mapcar (lambda (x) (if (equal x ?|)
6412 (prog1 (if start ?+ ?|) (setq start t))
6413 (if start ?- ?\ )))
6414 line)))
6415 (beginning-of-line (if arg 1 2))
6416 (apply 'insert line)
6417 (if (equal (char-before (point)) ?+)
6418 (progn (backward-delete-char 1) (insert "|")))
6419 (insert "\n")
6420 (beginning-of-line 0)
6421 (move-to-column col)))
6422
6423 (defun org-table-kill-row ()
6424 "Delete the current row or horizontal line from the table."
6425 (interactive)
6426 (if (not (org-at-table-p))
6427 (error "Not at a table"))
6428 (let ((col (current-column)))
6429 (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
6430 (if (not (org-at-table-p)) (beginning-of-line 0))
6431 (move-to-column col)))
6432
6433
6434 (defun org-table-cut-region ()
6435 "Copy region in table to the clipboard and blank all relevant fields."
6436 (interactive)
6437 (org-table-copy-region 'cut))
6438
6439 (defun org-table-copy-region (&optional cut)
6440 "Copy rectangular region in table to clipboard.
6441 A special clipboard is used which can only be accessed
6442 with `org-table-paste-rectangle'"
6443 (interactive "P")
6444 (unless (org-region-active-p) (error "No active region"))
6445 (let* ((beg (region-beginning))
6446 (end (region-end))
6447 l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
6448 region cols
6449 (rpl (if cut " " nil)))
6450 (goto-char beg)
6451 (org-table-check-inside-data-field)
6452 (setq l01 (count-lines (point-min) (point))
6453 c01 (org-table-current-column))
6454 (goto-char end)
6455 (org-table-check-inside-data-field)
6456 (setq l02 (count-lines (point-min) (point))
6457 c02 (org-table-current-column))
6458 (setq l1 (min l01 l02) l2 (max l01 l02)
6459 c1 (min c01 c02) c2 (max c01 c02))
6460 (catch 'exit
6461 (while t
6462 (catch 'nextline
6463 (if (> l1 l2) (throw 'exit t))
6464 (goto-line l1)
6465 (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
6466 (setq cols nil ic1 c1 ic2 c2)
6467 (while (< ic1 (1+ ic2))
6468 (push (org-table-get-field ic1 rpl) cols)
6469 (setq ic1 (1+ ic1)))
6470 (push (nreverse cols) region)
6471 (setq l1 (1+ l1)))))
6472 (setq org-table-clip (nreverse region))
6473 (if cut (org-table-align))))
6474
6475 (defun org-table-paste-rectangle ()
6476 "Paste a rectangular region into a table.
6477 The upper right corner ends up in the current field. All involved fields
6478 will be overwritten. If the rectangle does not fit into the present table,
6479 the table is enlarged as needed. The process ignores horizontal separator
6480 lines."
6481 (interactive)
6482 (unless (and org-table-clip (listp org-table-clip))
6483 (error "First cut/copy a region to paste!"))
6484 (org-table-check-inside-data-field)
6485 (let* ((clip org-table-clip)
6486 (line (count-lines (point-min) (point)))
6487 (col (org-table-current-column))
6488 (org-enable-table-editor t)
6489 (org-table-automatic-realign nil)
6490 c cols field)
6491 (while (setq cols (pop clip))
6492 (while (org-at-table-hline-p) (beginning-of-line 2))
6493 (if (not (org-at-table-p))
6494 (progn (end-of-line 0) (org-table-next-field)))
6495 (setq c col)
6496 (while (setq field (pop cols))
6497 (org-table-goto-column c nil 'force)
6498 (org-table-get-field nil field)
6499 (setq c (1+ c)))
6500 (beginning-of-line 2))
6501 (goto-line line)
6502 (org-table-goto-column col)
6503 (org-table-align)))
6504
6505 (defun org-table-convert ()
6506 "Convert from `org-mode' table to table.el and back.
6507 Obviously, this only works within limits. When an Org-mode table is
6508 converted to table.el, all horizontal separator lines get lost, because
6509 table.el uses these as cell boundaries and has no notion of horizontal lines.
6510 A table.el table can be converted to an Org-mode table only if it does not
6511 do row or column spanning. Multiline cells will become multiple cells.
6512 Beware, Org-mode does not test if the table can be successfully converted - it
6513 blindly applies a recipe that works for simple tables."
6514 (interactive)
6515 (require 'table)
6516 (if (org-at-table.el-p)
6517 ;; convert to Org-mode table
6518 (let ((beg (move-marker (make-marker) (org-table-begin t)))
6519 (end (move-marker (make-marker) (org-table-end t))))
6520 (table-unrecognize-region beg end)
6521 (goto-char beg)
6522 (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
6523 (replace-match ""))
6524 (goto-char beg))
6525 (if (org-at-table-p)
6526 ;; convert to table.el table
6527 (let ((beg (move-marker (make-marker) (org-table-begin)))
6528 (end (move-marker (make-marker) (org-table-end))))
6529 ;; first, get rid of all horizontal lines
6530 (goto-char beg)
6531 (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
6532 (replace-match ""))
6533 ;; insert a hline before first
6534 (goto-char beg)
6535 (org-table-insert-hline 'above)
6536 ;; insert a hline after each line
6537 (while (progn (beginning-of-line 2) (< (point) end))
6538 (org-table-insert-hline))
6539 (goto-char beg)
6540 (setq end (move-marker end (org-table-end)))
6541 ;; replace "+" at beginning and ending of hlines
6542 (while (re-search-forward "^\\([ \t]*\\)|-" end t)
6543 (replace-match "\\1+-"))
6544 (goto-char beg)
6545 (while (re-search-forward "-|[ \t]*$" end t)
6546 (replace-match "-+"))
6547 (goto-char beg)))))
6548
6549 (defun org-table-wrap-region (arg)
6550 "Wrap several fields in a column like a paragraph.
6551 This is useful if you'd like to spread the contents of a field over several
6552 lines, in order to keep the table compact.
6553
6554 If there is an active region, and both point and mark are in the same column,
6555 the text in the column is wrapped to minimum width for the given number of
6556 lines. Generally, this makes the table more compact. A prefix ARG may be
6557 used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
6558 formats the selected text to two lines. If the region was longer than 2
6559 lines, the remaining lines remain empty. A negative prefix argument reduces
6560 the current number of lines by that amount. The wrapped text is pasted back
6561 into the table. If you formatted it to more lines than it was before, fields
6562 further down in the table get overwritten - so you might need to make space in
6563 the table first.
6564
6565 If there is no region, the current field is split at the cursor position and
6566 the text fragment to the right of the cursor is prepended to the field one
6567 line down.
6568
6569 If there is no region, but you specify a prefix ARG, the current field gets
6570 blank, and the content is appended to the field above."
6571 (interactive "P")
6572 (org-table-check-inside-data-field)
6573 (if (org-region-active-p)
6574 ;; There is a region: fill as a paragraph
6575 (let ((beg (region-beginning))
6576 nlines)
6577 (org-table-cut-region)
6578 (if (> (length (car org-table-clip)) 1)
6579 (error "Region must be limited to single column"))
6580 (setq nlines (if arg
6581 (if (< arg 1)
6582 (+ (length org-table-clip) arg)
6583 arg)
6584 (length org-table-clip)))
6585 (setq org-table-clip
6586 (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
6587 nil nlines)))
6588 (goto-char beg)
6589 (org-table-paste-rectangle))
6590 ;; No region, split the current field at point
6591 (if arg
6592 ;; combine with field above
6593 (let ((s (org-table-blank-field))
6594 (col (org-table-current-column)))
6595 (beginning-of-line 0)
6596 (while (org-at-table-hline-p) (beginning-of-line 0))
6597 (org-table-goto-column col)
6598 (skip-chars-forward "^|")
6599 (skip-chars-backward " ")
6600 (insert " " (org-trim s))
6601 (org-table-align))
6602 ;; split field
6603 (when (looking-at "\\([^|]+\\)+|")
6604 (let ((s (match-string 1)))
6605 (replace-match " |")
6606 (goto-char (match-beginning 0))
6607 (org-table-next-row)
6608 (insert (org-trim s) " ")
6609 (org-table-align))))))
6610
6611 (defun org-trim (s)
6612 "Remove whitespace at beginning and end of string."
6613 (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
6614 (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s))))
6615
6616 (defun org-wrap (string &optional width lines)
6617 "Wrap string to either a number of lines, or a width in characters.
6618 If WIDTH is non-nil, the string is wrapped to that width, however many lines
6619 that costs. If there is a word longer than WIDTH, the text is actually
6620 wrapped to the length of that word.
6621 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
6622 many lines, whatever width that takes.
6623 The return value is a list of lines, without newlines at the end."
6624 (let* ((words (org-split-string string "[ \t\n]+"))
6625 (maxword (apply 'max (mapcar 'length words)))
6626 w ll)
6627 (cond (width
6628 (org-do-wrap words (max maxword width)))
6629 (lines
6630 (setq w maxword)
6631 (setq ll (org-do-wrap words maxword))
6632 (if (<= (length ll) lines)
6633 ll
6634 (setq ll words)
6635 (while (> (length ll) lines)
6636 (setq w (1+ w))
6637 (setq ll (org-do-wrap words w)))
6638 ll))
6639 (t (error "Cannot wrap this")))))
6640
6641
6642 (defun org-do-wrap (words width)
6643 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
6644 (let (lines line)
6645 (while words
6646 (setq line (pop words))
6647 (while (and words (< (+ (length line) (length (car words))) width))
6648 (setq line (concat line " " (pop words))))
6649 (setq lines (push line lines)))
6650 (nreverse lines)))
6651
6652 ;; FIXME: I think I can make this more efficient
6653 (defun org-split-string (string &optional separators)
6654 "Splits STRING into substrings at SEPARATORS.
6655 No empty strings are returned if there are matches at the beginning
6656 and end of string."
6657 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
6658 (start 0)
6659 notfirst
6660 (list nil))
6661 (while (and (string-match rexp string
6662 (if (and notfirst
6663 (= start (match-beginning 0))
6664 (< start (length string)))
6665 (1+ start) start))
6666 (< (match-beginning 0) (length string)))
6667 (setq notfirst t)
6668 (or (eq (match-beginning 0) 0)
6669 (and (eq (match-beginning 0) (match-end 0))
6670 (eq (match-beginning 0) start))
6671 (setq list
6672 (cons (substring string start (match-beginning 0))
6673 list)))
6674 (setq start (match-end 0)))
6675 (or (eq start (length string))
6676 (setq list
6677 (cons (substring string start)
6678 list)))
6679 (nreverse list)))
6680
6681 (defun org-table-add-invisible-to-vertical-lines ()
6682 "Add an `invisible' property to vertical lines of current table."
6683 (interactive)
6684 (let* ((beg (org-table-begin))
6685 (end (org-table-end))
6686 (end1))
6687 (save-excursion
6688 (goto-char beg)
6689 (while (< (point) end)
6690 (setq end1 (point-at-eol))
6691 (if (looking-at org-table-dataline-regexp)
6692 (while (re-search-forward "|" end1 t)
6693 (add-text-properties (1- (point)) (point)
6694 '(invisible org-table)))
6695 (while (re-search-forward "[+|]" end1 t)
6696 (add-text-properties (1- (point)) (point)
6697 '(invisible org-table))))
6698 (beginning-of-line 2)))))
6699
6700 (defun org-table-toggle-vline-visibility (&optional arg)
6701 "Toggle the visibility of table vertical lines.
6702 The effect is immediate and on all tables in the file.
6703 With prefix ARG, make lines invisible when ARG is positive, make lines
6704 visible when ARG is not positive"
6705 (interactive "P")
6706 (let ((action (cond
6707 ((and arg (> (prefix-numeric-value arg) 0)) 'on)
6708 ((and arg (< (prefix-numeric-value arg) 1)) 'off)
6709 (t (if (org-in-invisibility-spec-p '(org-table))
6710 'off
6711 'on)))))
6712 (if (eq action 'off)
6713 (progn
6714 (org-remove-from-invisibility-spec '(org-table))
6715 (org-table-map-tables 'org-table-align)
6716 (message "Vertical table lines visible")
6717 (if (org-at-table-p)
6718 (org-table-align)))
6719 (org-add-to-invisibility-spec '(org-table))
6720 (org-table-map-tables 'org-table-align)
6721 (message "Vertical table lines invisible"))
6722 (redraw-frame (selected-frame))))
6723
6724 (defun org-table-map-tables (function)
6725 "Apply FUNCTION to the start of all tables in the buffer."
6726 (save-excursion
6727 (save-restriction
6728 (widen)
6729 (goto-char (point-min))
6730 (while (re-search-forward org-table-any-line-regexp nil t)
6731 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
6732 (beginning-of-line 1)
6733 (if (looking-at org-table-line-regexp)
6734 (save-excursion (funcall function)))
6735 (re-search-forward org-table-any-border-regexp nil 1)))))
6736
6737 (defun org-table-sum ()
6738 "Sum numbers in region of current table column.
6739 The result will be displayed in the echo area, and will be available
6740 as kill to be inserted with \\[yank].
6741
6742 If there is an active region, it is interpreted as a rectangle and all
6743 numbers in that rectangle will be summed. If there is no active
6744 region and point is located in a table column, sum all numbers in that
6745 column.
6746
6747 If at least one number looks like a time HH:MM or HH:MM:SS, all other
6748 numbers are assumed to be times as well (in decimal hours) and the
6749 numbers are added as such."
6750 (interactive)
6751 (save-excursion
6752 (let (beg end col (timecnt 0) diff h m s)
6753 (if (org-region-active-p)
6754 (setq beg (region-beginning) end (region-end))
6755 (setq col (org-table-current-column))
6756 (goto-char (org-table-begin))
6757 (unless (re-search-forward "^[ \t]*|[^-]" nil t)
6758 (error "No table data"))
6759 (org-table-goto-column col)
6760 (skip-chars-backward "^|")
6761 (setq beg (point))
6762 (goto-char (org-table-end))
6763 (unless (re-search-backward "^[ \t]*|[^-]" nil t)
6764 (error "No table data"))
6765 (org-table-goto-column col)
6766 (skip-chars-forward "^|")
6767 (setq end (point)))
6768 (let* ((l1 (progn (goto-char beg)
6769 (+ (if (bolp) 1 0) (count-lines (point-min) (point)))))
6770 (l2 (progn (goto-char end)
6771 (+ (if (bolp) 1 0) (count-lines (point-min) (point)))))
6772 (items (if (= l1 l2)
6773 (split-string (buffer-substring beg end))
6774 (split-string
6775 (mapconcat 'identity (extract-rectangle beg end) " "))))
6776 (numbers (delq nil (mapcar 'org-table-get-number-for-summing
6777 items)))
6778 (res (apply '+ numbers))
6779 (sres (if (= timecnt 0)
6780 (format "%g" res)
6781 (setq diff (* 3600 res)
6782 h (floor (/ diff 3600)) diff (mod diff 3600)
6783 m (floor (/ diff 60)) diff (mod diff 60)
6784 s diff)
6785 (format "%d:%02d:%02d" h m s))))
6786 (kill-new sres)
6787 (message (substitute-command-keys
6788 (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
6789 (length numbers) sres)))))))
6790
6791 (defun org-table-get-number-for-summing (s)
6792 (let (n)
6793 (if (string-match "^ *|? *" s)
6794 (setq s (replace-match "" nil nil s)))
6795 (if (string-match " *|? *$" s)
6796 (setq s (replace-match "" nil nil s)))
6797 (setq n (string-to-number s))
6798 (cond
6799 ((and (string-match "0" s)
6800 (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
6801 ((string-match "\\`[ \t]+\\'" s) nil)
6802 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
6803 (let ((h (string-to-number (or (match-string 1 s) "0")))
6804 (m (string-to-number (or (match-string 2 s) "0")))
6805 (s (string-to-number (or (match-string 4 s) "0"))))
6806 (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
6807 (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
6808 ((equal n 0) nil)
6809 (t n))))
6810
6811 (defvar org-table-current-formula nil)
6812 (defvar org-table-formula-history nil)
6813 (defun org-table-get-formula (current)
6814 (if (and current (not (equal "" org-table-current-formula)))
6815 org-table-current-formula
6816 (setq org-table-current-formula
6817 (read-string
6818 "Formula [last]: " "" 'org-table-formula-history
6819 org-table-current-formula))))
6820
6821 (defun org-this-word ()
6822 ;; Get the current word
6823 (save-excursion
6824 (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
6825 (end (progn (skip-chars-forward "^ \t\n") (point))))
6826 (buffer-substring-no-properties beg end))))
6827
6828 (defun org-table-eval-formula (&optional ndown)
6829 "Replace the table field value at the cursor by the result of a calculation.
6830
6831 This function makes use of Dave Gillespie's calc package, arguably the most
6832 exciting program ever written for GNU Emacs. So you need to have calc
6833 installed in order to use this function.
6834
6835 In a table, this command replaces the value in the current field with the
6836 result of a formula. While nowhere near the computation options of a
6837 spreadsheet program, this is still very useful. Note that there is no
6838 automatic updating of a calculated field, nor will the field remember the
6839 formula. The command needs to be applied again after changing input
6840 fields.
6841
6842 When called, the command first prompts for a formula, which is read in the
6843 minibuffer. Previously entered formulae are available through the history
6844 list, and the last used formula is the default, reachable by simply
6845 pressing RET.
6846
6847 The formula can be any algebraic expression understood by the calc package.
6848 Before evaluation, variable substitution takes place: \"$\" is replaced by
6849 the field the cursor is currently in, and $1..$n reference the fields in
6850 the current row. Values from a *different* row can *not* be referenced
6851 here, so the command supports only horizontal computing. The formula can
6852 contain an optional printf format specifier after a semicolon, to reformat
6853 the result.
6854
6855 A few examples for formulae:
6856 $1+$2 Sum of first and second field
6857 $1+$2;%.2f Same, and format result to two digits after dec.point
6858 exp($2)+exp($1) Math functions can be used
6859 $;%.1f Reformat current cell to 1 digit after dec.point
6860 ($3-32)*5/9 degrees F -> C conversion
6861
6862 When called with a raw \\[universal-argument] prefix, the formula is applied to the current
6863 field, and to the same same column in all following rows, until reaching a
6864 horizontal line or the end of the table. When the command is called with a
6865 numeric prefix argument (like M-3 or C-7 or \\[universal-argument] 24), the formula is applied
6866 to the current row, and to the following n-1 rows (but not beyond a
6867 separator line)."
6868 (interactive "P")
6869 (setq ndown (if (equal ndown '(4)) 10000 (prefix-numeric-value ndown)))
6870 (require 'calc)
6871 (org-table-check-inside-data-field)
6872 (let* (fields
6873 (org-table-automatic-realign nil)
6874 (down (> ndown 1))
6875 (formula (org-table-get-formula nil))
6876 (n0 (org-table-current-column))
6877 n form fmt x ev)
6878 (if (string-match ";" formula)
6879 (let ((tmp (org-split-string formula ";")))
6880 (setq formula (car tmp) fmt (nth 1 tmp))))
6881 (while (> ndown 0)
6882 (setq fields (org-split-string
6883 (concat " " (buffer-substring
6884 (point-at-bol) (point-at-eol))) "|"))
6885 (setq ndown (1- ndown))
6886 (setq form (copy-sequence formula))
6887 (while (string-match "\\$\\([0-9]+\\)?" form)
6888 (setq n (if (match-beginning 1)
6889 (string-to-int (match-string 1 form))
6890 n0)
6891 x (nth n fields))
6892 (unless x (error "Invalid field specifier \"%s\""
6893 (match-string 0 form)))
6894 (if (equal (string-to-number x) 0) (setq x "0"))
6895 (setq form (replace-match x t t form)))
6896 (setq ev (calc-eval (list form) 'num))
6897 (if (listp ev)
6898 (error "Invalid expression: %s (%s at %d)" form (nth 1 ev) (car ev)))
6899 (org-table-blank-field)
6900 (if fmt
6901 (insert (format fmt (string-to-number ev)))
6902 (insert ev))
6903 (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
6904 (call-interactively 'org-return)
6905 (setq ndown 0)))
6906 (org-table-align)))
6907
6908 ;;; The orgtbl minor mode
6909
6910 ;; Define a minor mode which can be used in other modes in order to
6911 ;; integrate the org-mode table editor.
6912
6913 ;; This is really a hack, because the org-mode table editor uses several
6914 ;; keys which normally belong to the major mode, for example the TAB and
6915 ;; RET keys. Here is how it works: The minor mode defines all the keys
6916 ;; necessary to operate the table editor, but wraps the commands into a
6917 ;; function which tests if the cursor is currently inside a table. If that
6918 ;; is the case, the table editor command is executed. However, when any of
6919 ;; those keys is used outside a table, the function uses `key-binding' to
6920 ;; look up if the key has an associated command in another currently active
6921 ;; keymap (minor modes, major mode, global), and executes that command.
6922 ;; There might be problems if any of the keys used by the table editor is
6923 ;; otherwise used as a prefix key.
6924
6925 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
6926 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
6927 ;; addresses this by checking explicitly for both bindings.
6928
6929 ;; The optimized version (see variable `orgtbl-optimized') takes over
6930 ;; all keys which are bound to `self-insert-command' in the *global map*.
6931 ;; Some modes bind other commands to simple characters, for example
6932 ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
6933 ;; active, this binding is ignored inside tables and replaced with a
6934 ;; modified self-insert.
6935
6936 (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
6937 "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
6938 In the optimized version, the table editor takes over all simple keys that
6939 normally just insert a character. In tables, the characters are inserted
6940 in a way to minimize disturbing the table structure (i.e. in overwrite mode
6941 for empty fields). Outside tables, the correct binding of the keys is
6942 restored.
6943
6944 The default for this option is t if the optimized version is also used in
6945 Org-mode. See the variable `org-enable-table-editor' for details. Changing
6946 this variable requires a restart of Emacs to become effective."
6947 :group 'org-table
6948 :type 'boolean)
6949
6950 (defvar orgtbl-mode nil
6951 "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
6952 table editor in arbitrary modes.")
6953 (make-variable-buffer-local 'orgtbl-mode)
6954
6955 (defvar orgtbl-mode-map (make-sparse-keymap)
6956 "Keymap for `orgtbl-mode'.")
6957
6958 ;;;###autoload
6959 (defun turn-on-orgtbl ()
6960 "Unconditionally turn on `orgtbl-mode'."
6961 (orgtbl-mode 1))
6962
6963 ;;;###autoload
6964 (defun orgtbl-mode (&optional arg)
6965 "The `org-mode' table editor as a minor mode for use in other modes."
6966 (interactive)
6967 (if (eq major-mode 'org-mode)
6968 ;; Exit without error, in case some hook functions calls this
6969 ;; by accident in org-mode.
6970 (message "Orgtbl-mode is not useful in org-mode, command ignored")
6971 (setq orgtbl-mode
6972 (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
6973 (if orgtbl-mode
6974 (progn
6975 (set (make-local-variable (quote org-table-may-need-update)) t)
6976 (make-local-hook (quote before-change-functions))
6977 (add-hook 'before-change-functions 'org-before-change-function
6978 nil 'local)
6979 (set (make-local-variable 'org-old-auto-fill-inhibit-regexp)
6980 auto-fill-inhibit-regexp)
6981 (set (make-local-variable 'auto-fill-inhibit-regexp)
6982 (if auto-fill-inhibit-regexp
6983 (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
6984 "[ \t]*|"))
6985 (easy-menu-add orgtbl-mode-menu)
6986 (run-hooks 'orgtbl-mode-hook))
6987 (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
6988 (remove-hook 'before-change-functions 'org-before-change-function t)
6989 (easy-menu-remove orgtbl-mode-menu)
6990 (force-mode-line-update 'all))))
6991
6992 ;; Install it as a minor mode.
6993 (put 'orgtbl-mode :included t)
6994 (put 'orgtbl-mode :menu-tag "Org Table Mode")
6995 (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
6996
6997 (defun orgtbl-make-binding (fun &rest keys)
6998 "Create a function for binding in the table minor mode."
6999 (list 'lambda '(arg)
7000 (concat "Run `" (symbol-name fun) "' or the default binding.")
7001 '(interactive "p")
7002 (list 'if
7003 '(org-at-table-p)
7004 (list 'call-interactively (list 'quote fun))
7005 (list 'let '(orgtbl-mode)
7006 (list 'call-interactively
7007 (append '(or)
7008 (mapcar (lambda (k)
7009 (list 'key-binding k))
7010 keys)
7011 '('orgtbl-error)))))))
7012
7013 (defun orgtbl-error ()
7014 "Error when there is no default binding for a table key."
7015 (interactive)
7016 (error "This key is has no function outside tables"))
7017
7018 ;; Keybindings for the minor mode
7019 (let ((bindings
7020 (list
7021 '([(meta shift left)] org-table-delete-column)
7022 '([(meta left)] org-table-move-column-left)
7023 '([(meta right)] org-table-move-column-right)
7024 '([(meta shift right)] org-table-insert-column)
7025 '([(meta shift up)] org-table-kill-row)
7026 '([(meta shift down)] org-table-insert-row)
7027 '([(meta up)] org-table-move-row-up)
7028 '([(meta down)] org-table-move-row-down)
7029 '("\C-c\C-w" org-table-cut-region)
7030 '("\C-c\M-w" org-table-copy-region)
7031 '("\C-c\C-y" org-table-paste-rectangle)
7032 '("\C-c-" org-table-insert-hline)
7033 '([(shift tab)] org-table-previous-field)
7034 '("\C-c\C-c" org-table-align)
7035 '("\C-m" org-table-next-row)
7036 (list (org-key 'S-return) 'org-table-copy-down)
7037 '([(meta return)] org-table-wrap-region)
7038 '("\C-c\C-q" org-table-wrap-region)
7039 '("\C-c?" org-table-current-column)
7040 '("\C-c " org-table-blank-field)
7041 '("\C-c+" org-table-sum)
7042 '("\C-c|" org-table-toggle-vline-visibility)
7043 '("\C-c=" org-table-eval-formula)))
7044 elt key fun cmd)
7045 (while (setq elt (pop bindings))
7046 (setq key (car elt)
7047 fun (nth 1 elt)
7048 cmd (orgtbl-make-binding fun key))
7049 (define-key orgtbl-mode-map key cmd)))
7050
7051 ;; Special treatment needed for TAB and RET
7052
7053 (define-key orgtbl-mode-map [(return)]
7054 (orgtbl-make-binding 'orgtbl-ret [(return)] "\C-m"))
7055 (define-key orgtbl-mode-map "\C-m"
7056 (orgtbl-make-binding 'orgtbl-ret "\C-m" [(return)]))
7057 (define-key orgtbl-mode-map [(tab)]
7058 (orgtbl-make-binding 'orgtbl-tab [(tab)] "\C-i"))
7059 (define-key orgtbl-mode-map "\C-i"
7060 (orgtbl-make-binding 'orgtbl-tab "\C-i" [(tab)]))
7061
7062 (when orgtbl-optimized
7063 ;; If the user wants maximum table support, we need to hijack
7064 ;; some standard editing functions
7065 (substitute-key-definition 'self-insert-command 'orgtbl-self-insert-command
7066 orgtbl-mode-map global-map)
7067 (substitute-key-definition 'delete-char 'orgtbl-delete-char
7068 orgtbl-mode-map global-map)
7069 (substitute-key-definition 'delete-backward-char 'orgtbl-delete-backward-char
7070 orgtbl-mode-map global-map)
7071 (define-key org-mode-map "|" 'self-insert-command))
7072
7073 (defun orgtbl-tab ()
7074 "Justification and field motion for `orgtbl-mode'."
7075 (interactive)
7076 (org-table-justify-field-maybe)
7077 (org-table-next-field))
7078
7079 (defun orgtbl-ret ()
7080 "Justification and field motion for `orgtbl-mode'."
7081 (interactive)
7082 (org-table-justify-field-maybe)
7083 (org-table-next-row))
7084
7085 (defun orgtbl-self-insert-command (N)
7086 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
7087 If the cursor is in a table looking at whitespace, the whitespace is
7088 overwritten, and the table is not marked as requiring realignment."
7089 (interactive "p")
7090 (if (and (org-at-table-p)
7091 (eq N 1)
7092 (looking-at "[^|\n]* +|"))
7093 (let (org-table-may-need-update)
7094 (goto-char (1- (match-end 0)))
7095 (delete-backward-char 1)
7096 (goto-char (match-beginning 0))
7097 (self-insert-command N))
7098 (setq org-table-may-need-update t)
7099 (let (orgtbl-mode)
7100 (call-interactively (key-binding (vector last-input-event))))))
7101
7102 (defun orgtbl-delete-backward-char (N)
7103 "Like `delete-backward-char', insert whitespace at field end in tables.
7104 When deleting backwards, in tables this function will insert whitespace in
7105 front of the next \"|\" separator, to keep the table aligned. The table will
7106 still be marked for re-alignment, because a narrow field may lead to a
7107 reduced column width."
7108 (interactive "p")
7109 (if (and (org-at-table-p)
7110 (eq N 1)
7111 (looking-at ".*?|"))
7112 (let ((pos (point)))
7113 (backward-delete-char N)
7114 (skip-chars-forward "^|")
7115 (insert " ")
7116 (goto-char (1- pos)))
7117 (message "%s" last-input-event) (sit-for 1)
7118 (delete-backward-char N)))
7119
7120 (defun orgtbl-delete-char (N)
7121 "Like `delete-char', but insert whitespace at field end in tables.
7122 When deleting characters, in tables this function will insert whitespace in
7123 front of the next \"|\" separator, to keep the table aligned. The table
7124 will still be marked for re-alignment, because a narrow field may lead to
7125 a reduced column width."
7126 (interactive "p")
7127 (if (and (org-at-table-p)
7128 (eq N 1))
7129 (if (looking-at ".*?|")
7130 (let ((pos (point)))
7131 (replace-match (concat
7132 (substring (match-string 0) 1 -1)
7133 " |"))
7134 (goto-char pos)))
7135 (delete-char N)))
7136
7137 (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
7138 '("Tbl"
7139 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
7140 ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
7141 ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
7142 ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
7143 "--"
7144 ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
7145 ["Copy Field from Above"
7146 org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
7147 "--"
7148 ("Column"
7149 ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
7150 ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
7151 ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
7152 ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
7153 ("Row"
7154 ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
7155 ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
7156 ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
7157 ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
7158 "--"
7159 ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
7160 ("Rectangle"
7161 ["Copy Rectangle" org-copy-special :active (org-at-table-p) :keys "C-c M-w"]
7162 ["Cut Rectangle" org-cut-special :active (org-at-table-p) :keys "C-c C-w"]
7163 ["Paste Rectangle" org-paste-special :active (org-at-table-p) :keys "C-c C-y"]
7164 ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p) :keys "C-c C-q"])
7165 "--"
7166 ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
7167 ["Sum Column/Rectangle" org-table-sum
7168 :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
7169 ["Eval Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
7170 ))
7171
7172 ;;; Exporting
7173
7174 (defconst org-level-max 20)
7175
7176 (defun org-export-find-first-heading-line (list)
7177 "Remove all lines from LIST which are before the first headline."
7178 (let ((orig-list list)
7179 (re (concat "^" outline-regexp)))
7180 (while (and list
7181 (not (string-match re (car list))))
7182 (pop list))
7183 (or list orig-list)))
7184
7185 (defun org-skip-comments (lines)
7186 "Skip lines starting with \"#\" and subtrees starting with COMMENT."
7187 (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
7188 (re2 "^\\(\\*+\\)[ \t\n\r]")
7189 rtn line level)
7190 (while (setq line (pop lines))
7191 (cond
7192 ((and (string-match re1 line)
7193 (setq level (- (match-end 1) (match-beginning 1))))
7194 ;; Beginning of a COMMENT subtree. Skip it.
7195 (while (and (setq line (pop lines))
7196 (or (not (string-match re2 line))
7197 (> (- (match-end 1) (match-beginning 1)) level))))
7198 (setq lines (cons line lines)))
7199 ((string-match "^#" line)
7200 ;; an ordinary comment line
7201 )
7202 (t (setq rtn (cons line rtn)))))
7203 (nreverse rtn)))
7204
7205 ;; ASCII
7206
7207 (defconst org-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
7208 "Characters for underlining headings in ASCII export.")
7209
7210 (defconst org-html-entities
7211 '(("nbsp")
7212 ("iexcl")
7213 ("cent")
7214 ("pound")
7215 ("curren")
7216 ("yen")
7217 ("brvbar")
7218 ("sect")
7219 ("uml")
7220 ("copy")
7221 ("ordf")
7222 ("laquo")
7223 ("not")
7224 ("shy")
7225 ("reg")
7226 ("macr")
7227 ("deg")
7228 ("plusmn")
7229 ("sup2")
7230 ("sup3")
7231 ("acute")
7232 ("micro")
7233 ("para")
7234 ("middot")
7235 ("odot"."o")
7236 ("star"."*")
7237 ("cedil")
7238 ("sup1")
7239 ("ordm")
7240 ("raquo")
7241 ("frac14")
7242 ("frac12")
7243 ("frac34")
7244 ("iquest")
7245 ("Agrave")
7246 ("Aacute")
7247 ("Acirc")
7248 ("Atilde")
7249 ("Auml")
7250 ("Aring") ("AA"."&Aring;")
7251 ("AElig")
7252 ("Ccedil")
7253 ("Egrave")
7254 ("Eacute")
7255 ("Ecirc")
7256 ("Euml")
7257 ("Igrave")
7258 ("Iacute")
7259 ("Icirc")
7260 ("Iuml")
7261 ("ETH")
7262 ("Ntilde")
7263 ("Ograve")
7264 ("Oacute")
7265 ("Ocirc")
7266 ("Otilde")
7267 ("Ouml")
7268 ("times")
7269 ("Oslash")
7270 ("Ugrave")
7271 ("Uacute")
7272 ("Ucirc")
7273 ("Uuml")
7274 ("Yacute")
7275 ("THORN")
7276 ("szlig")
7277 ("agrave")
7278 ("aacute")
7279 ("acirc")
7280 ("atilde")
7281 ("auml")
7282 ("aring")
7283 ("aelig")
7284 ("ccedil")
7285 ("egrave")
7286 ("eacute")
7287 ("ecirc")
7288 ("euml")
7289 ("igrave")
7290 ("iacute")
7291 ("icirc")
7292 ("iuml")
7293 ("eth")
7294 ("ntilde")
7295 ("ograve")
7296 ("oacute")
7297 ("ocirc")
7298 ("otilde")
7299 ("ouml")
7300 ("divide")
7301 ("oslash")
7302 ("ugrave")
7303 ("uacute")
7304 ("ucirc")
7305 ("uuml")
7306 ("yacute")
7307 ("thorn")
7308 ("yuml")
7309 ("fnof")
7310 ("Alpha")
7311 ("Beta")
7312 ("Gamma")
7313 ("Delta")
7314 ("Epsilon")
7315 ("Zeta")
7316 ("Eta")
7317 ("Theta")
7318 ("Iota")
7319 ("Kappa")
7320 ("Lambda")
7321 ("Mu")
7322 ("Nu")
7323 ("Xi")
7324 ("Omicron")
7325 ("Pi")
7326 ("Rho")
7327 ("Sigma")
7328 ("Tau")
7329 ("Upsilon")
7330 ("Phi")
7331 ("Chi")
7332 ("Psi")
7333 ("Omega")
7334 ("alpha")
7335 ("beta")
7336 ("gamma")
7337 ("delta")
7338 ("epsilon")
7339 ("varepsilon"."&epsilon;")
7340 ("zeta")
7341 ("eta")
7342 ("theta")
7343 ("iota")
7344 ("kappa")
7345 ("lambda")
7346 ("mu")
7347 ("nu")
7348 ("xi")
7349 ("omicron")
7350 ("pi")
7351 ("rho")
7352 ("sigmaf") ("varsigma"."&sigmaf;")
7353 ("sigma")
7354 ("tau")
7355 ("upsilon")
7356 ("phi")
7357 ("chi")
7358 ("psi")
7359 ("omega")
7360 ("thetasym") ("vartheta"."&thetasym;")
7361 ("upsih")
7362 ("piv")
7363 ("bull") ("bullet"."&bull;")
7364 ("hellip") ("dots"."&hellip;")
7365 ("prime")
7366 ("Prime")
7367 ("oline")
7368 ("frasl")
7369 ("weierp")
7370 ("image")
7371 ("real")
7372 ("trade")
7373 ("alefsym")
7374 ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
7375 ("uarr") ("uparrow"."&uarr;")
7376 ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
7377 ("darr")("downarrow"."&darr;")
7378 ("harr") ("leftrightarrow"."&harr;")
7379 ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
7380 ("lArr") ("Leftarrow"."&lArr;")
7381 ("uArr") ("Uparrow"."&uArr;")
7382 ("rArr") ("Rightarrow"."&rArr;")
7383 ("dArr") ("Downarrow"."&dArr;")
7384 ("hArr") ("Leftrightarrow"."&hArr;")
7385 ("forall")
7386 ("part") ("partial"."&part;")
7387 ("exist") ("exists"."&exist;")
7388 ("empty") ("emptyset"."&empty;")
7389 ("nabla")
7390 ("isin") ("in"."&isin;")
7391 ("notin")
7392 ("ni")
7393 ("prod")
7394 ("sum")
7395 ("minus")
7396 ("lowast") ("ast"."&lowast;")
7397 ("radic")
7398 ("prop") ("proptp"."&prop;")
7399 ("infin") ("infty"."&infin;")
7400 ("ang") ("angle"."&ang;")
7401 ("and") ("vee"."&and;")
7402 ("or") ("wedge"."&or;")
7403 ("cap")
7404 ("cup")
7405 ("int")
7406 ("there4")
7407 ("sim")
7408 ("cong") ("simeq"."&cong;")
7409 ("asymp")("approx"."&asymp;")
7410 ("ne") ("neq"."&ne;")
7411 ("equiv")
7412 ("le")
7413 ("ge")
7414 ("sub") ("subset"."&sub;")
7415 ("sup") ("supset"."&sup;")
7416 ("nsub")
7417 ("sube")
7418 ("supe")
7419 ("oplus")
7420 ("otimes")
7421 ("perp")
7422 ("sdot") ("cdot"."&sdot;")
7423 ("lceil")
7424 ("rceil")
7425 ("lfloor")
7426 ("rfloor")
7427 ("lang")
7428 ("rang")
7429 ("loz") ("Diamond"."&loz;")
7430 ("spades") ("spadesuit"."&spades;")
7431 ("clubs") ("clubsuit"."&clubs;")
7432 ("hearts") ("diamondsuit"."&hearts;")
7433 ("diams") ("diamondsuit"."&diams;")
7434 ("quot")
7435 ("amp")
7436 ("lt")
7437 ("gt")
7438 ("OElig")
7439 ("oelig")
7440 ("Scaron")
7441 ("scaron")
7442 ("Yuml")
7443 ("circ")
7444 ("tilde")
7445 ("ensp")
7446 ("emsp")
7447 ("thinsp")
7448 ("zwnj")
7449 ("zwj")
7450 ("lrm")
7451 ("rlm")
7452 ("ndash")
7453 ("mdash")
7454 ("lsquo")
7455 ("rsquo")
7456 ("sbquo")
7457 ("ldquo")
7458 ("rdquo")
7459 ("bdquo")
7460 ("dagger")
7461 ("Dagger")
7462 ("permil")
7463 ("lsaquo")
7464 ("rsaquo")
7465 ("euro")
7466
7467 ("arccos"."arccos")
7468 ("arcsin"."arcsin")
7469 ("arctan"."arctan")
7470 ("arg"."arg")
7471 ("cos"."cos")
7472 ("cosh"."cosh")
7473 ("cot"."cot")
7474 ("coth"."coth")
7475 ("csc"."csc")
7476 ("deg"."deg")
7477 ("det"."det")
7478 ("dim"."dim")
7479 ("exp"."exp")
7480 ("gcd"."gcd")
7481 ("hom"."hom")
7482 ("inf"."inf")
7483 ("ker"."ker")
7484 ("lg"."lg")
7485 ("lim"."lim")
7486 ("liminf"."liminf")
7487 ("limsup"."limsup")
7488 ("ln"."ln")
7489 ("log"."log")
7490 ("max"."max")
7491 ("min"."min")
7492 ("Pr"."Pr")
7493 ("sec"."sec")
7494 ("sin"."sin")
7495 ("sinh"."sinh")
7496 ("sup"."sup")
7497 ("tan"."tan")
7498 ("tanh"."tanh")
7499 )
7500 "Entities for TeX->HTML translation.
7501 Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
7502 \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
7503 In that case, \"\\ent\" will be translated to \"&other;\".
7504 The list contains HTML entities for Latin-1, Greek and other symbols.
7505 It is supplemented by a number of commonly used TeX macros with appropriate
7506 translations.")
7507
7508 (defvar org-last-level nil) ; dynamically scoped variable
7509
7510 (defun org-export-as-ascii (arg)
7511 "Export the outline as a pretty ASCII file.
7512 If there is an active region, export only the region.
7513 The prefix ARG specifies how many levels of the outline should become
7514 underlined headlines. The default is 3."
7515 (interactive "P")
7516 (setq-default org-todo-line-regexp org-todo-line-regexp)
7517 (let* ((region
7518 (buffer-substring
7519 (if (org-region-active-p) (region-beginning) (point-min))
7520 (if (org-region-active-p) (region-end) (point-max))))
7521 (lines (org-export-find-first-heading-line
7522 (org-skip-comments (org-split-string region "[\r\n]"))))
7523 (org-startup-with-deadline-check nil)
7524 (level 0) line txt
7525 (umax nil)
7526 (case-fold-search nil)
7527 (filename (concat (file-name-sans-extension (buffer-file-name))
7528 ".txt"))
7529 (buffer (find-file-noselect filename))
7530 (levels-open (make-vector org-level-max nil))
7531 (date (format-time-string "%Y/%m/%d" (current-time)))
7532 (time (format-time-string "%X" (current-time)))
7533 (author user-full-name)
7534 (title (buffer-name))
7535 (options nil)
7536 (email user-mail-address)
7537 (language org-export-default-language)
7538 (text nil)
7539 (todo nil)
7540 (lang-words nil))
7541
7542 (setq org-last-level 1)
7543 (org-init-section-numbers)
7544
7545 (find-file-noselect filename)
7546
7547 ;; Search for the export key lines
7548 (org-parse-key-lines)
7549
7550 (setq lang-words (or (assoc language org-export-language-setup)
7551 (assoc "en" org-export-language-setup)))
7552 (if org-export-ascii-show-new-buffer
7553 (switch-to-buffer-other-window buffer)
7554 (set-buffer buffer))
7555 (erase-buffer)
7556 (fundamental-mode)
7557 (if options (org-parse-export-options options))
7558 (setq umax (if arg (prefix-numeric-value arg)
7559 org-export-headline-levels))
7560
7561 ;; File header
7562 (if title (org-insert-centered title ?=))
7563 (insert "\n")
7564 (if (or author email)
7565 (insert (concat (nth 1 lang-words) ": " (or author "")
7566 (if email (concat " <" email ">") "")
7567 "\n")))
7568 (if (and date time)
7569 (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
7570 (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
7571
7572 (insert "\n\n")
7573
7574 (if org-export-with-toc
7575 (progn
7576 (insert (nth 3 lang-words) "\n"
7577 (make-string (length (nth 3 lang-words)) ?=) "\n")
7578 (mapcar '(lambda (line)
7579 (if (string-match org-todo-line-regexp
7580 line)
7581 ;; This is a headline
7582 (progn
7583 (setq level (- (match-end 1) (match-beginning 1))
7584 txt (match-string 3 line)
7585 todo
7586 (or (and (match-beginning 2)
7587 (not (equal (match-string 2 line)
7588 org-done-string)))
7589 ; TODO, not DONE
7590 (and (= level umax)
7591 (org-search-todo-below
7592 line lines level))))
7593 (setq txt (org-html-expand-for-ascii txt))
7594
7595 (if org-export-with-section-numbers
7596 (setq txt (concat (org-section-number level)
7597 " " txt)))
7598 (if (<= level umax)
7599 (progn
7600 (insert
7601 (make-string (* (1- level) 4) ?\ )
7602 (format (if todo "%s (*)\n" "%s\n") txt))
7603 (setq org-last-level level))
7604 ))))
7605 lines)))
7606
7607 (org-init-section-numbers)
7608 (while (setq line (pop lines))
7609 ;; Remove the quoted HTML tags.
7610 (setq line (org-html-expand-for-ascii line))
7611 (cond
7612 ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
7613 ;; a Headline
7614 (setq level (- (match-end 1) (match-beginning 1))
7615 txt (match-string 2 line))
7616 (org-ascii-level-start level txt umax))
7617 (t (insert line "\n"))))
7618 (normal-mode)
7619 (save-buffer)
7620 (goto-char (point-min))))
7621
7622 (defun org-search-todo-below (line lines level)
7623 "Search the subtree below LINE for any TODO entries."
7624 (let ((rest (cdr (memq line lines)))
7625 (re org-todo-line-regexp)
7626 line lv todo)
7627 (catch 'exit
7628 (while (setq line (pop rest))
7629 (if (string-match re line)
7630 (progn
7631 (setq lv (- (match-end 1) (match-beginning 1))
7632 todo (and (match-beginning 2)
7633 (not (equal (match-string 2 line)
7634 org-done-string))))
7635 ; TODO, not DONE
7636 (if (<= lv level) (throw 'exit nil))
7637 (if todo (throw 'exit t))))))))
7638
7639 ;; FIXME: Try to handle <b> and <i> as faces via text properties.
7640 ;; FIXME: Can I implement *bold*,/italic/ and _underline_ for ASCII export?
7641 (defun org-html-expand-for-ascii (line)
7642 "Handle quoted HTML for ASCII export."
7643 (if org-export-html-expand
7644 (while (string-match "@<[^<>\n]*>" line)
7645 ;; We just remove the tags for now.
7646 (setq line (replace-match "" nil nil line))))
7647 line)
7648
7649 (defun org-insert-centered (s &optional underline)
7650 "Insert the string S centered and underline it with character UNDERLINE."
7651 (let ((ind (max (/ (- 80 (length s)) 2) 0)))
7652 (insert (make-string ind ?\ ) s "\n")
7653 (if underline
7654 (insert (make-string ind ?\ )
7655 (make-string (length s) underline)
7656 "\n"))))
7657
7658 (defun org-ascii-level-start (level title umax)
7659 "Insert a new level in ASCII export."
7660 (let (char)
7661 (if (> level umax)
7662 (insert (make-string (* 2 (- level umax 1)) ?\ ) "* " title "\n")
7663 (if (or (not (equal (char-before) ?\n))
7664 (not (equal (char-before (1- (point))) ?\n)))
7665 (insert "\n"))
7666 (setq char (nth (- umax level) (reverse org-ascii-underline)))
7667 (if org-export-with-section-numbers
7668 (setq title (concat (org-section-number level) " " title)))
7669 (insert title "\n" (make-string (string-width title) char) "\n"))))
7670
7671 (defun org-export-copy-visible ()
7672 "Copy the visible part of the buffer to another buffer, for printing.
7673 Also removes the first line of the buffer if it specifies a mode,
7674 and all options lines."
7675 (interactive)
7676 (let* ((filename (concat (file-name-sans-extension (buffer-file-name))
7677 ".txt"))
7678 (buffer (find-file-noselect filename))
7679 (ore (concat
7680 (org-make-options-regexp
7681 '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
7682 "STARTUP" "ARCHIVE"
7683 "TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"))
7684 (if org-noutline-p "\\(\n\\|$\\)" "")))
7685 s e)
7686 (with-current-buffer buffer
7687 (erase-buffer)
7688 (text-mode))
7689 (save-excursion
7690 (setq s (goto-char (point-min)))
7691 (while (not (= (point) (point-max)))
7692 (goto-char (org-find-invisible))
7693 (append-to-buffer buffer s (point))
7694 (setq s (goto-char (org-find-visible)))))
7695 (switch-to-buffer-other-window buffer)
7696 (newline)
7697 (goto-char (point-min))
7698 (if (looking-at ".*-\\*- mode:.*\n")
7699 (replace-match ""))
7700 (while (re-search-forward ore nil t)
7701 (replace-match ""))
7702 (goto-char (point-min))))
7703
7704 (defun org-find-visible ()
7705 (if (featurep 'noutline)
7706 (let ((s (point)))
7707 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
7708 (get-char-property s 'invisible)))
7709 s)
7710 (skip-chars-forward "^\n")
7711 (point)))
7712 (defun org-find-invisible ()
7713 (if (featurep 'noutline)
7714 (let ((s (point)))
7715 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
7716 (not (get-char-property s 'invisible))))
7717 s)
7718 (skip-chars-forward "^\r")
7719 (point)))
7720
7721 ;; HTML
7722
7723 (defun org-get-current-options ()
7724 "Return a string with current options as keyword options.
7725 Does include HTML export options as well as TODO and CATEGORY stuff."
7726 (format
7727 "#+TITLE: %s
7728 #+AUTHOR: %s
7729 #+EMAIL: %s
7730 #+LANGUAGE: %s
7731 #+TEXT: Some descriptive text to be emitted. Several lines OK.
7732 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s
7733 #+CATEGORY: %s
7734 #+SEQ_TODO: %s
7735 #+TYP_TODO: %s
7736 #+STARTUP: %s %s
7737 #+ARCHIVE: %s
7738 "
7739 (buffer-name) (user-full-name) user-mail-address org-export-default-language
7740 org-export-headline-levels
7741 org-export-with-section-numbers
7742 org-export-with-toc
7743 org-export-preserve-breaks
7744 org-export-html-expand
7745 org-export-with-fixed-width
7746 org-export-with-tables
7747 org-export-with-sub-superscripts
7748 org-export-with-emphasize
7749 org-export-with-TeX-macros
7750 (file-name-nondirectory (buffer-file-name))
7751 (if (equal org-todo-interpretation 'sequence)
7752 (mapconcat 'identity org-todo-keywords " ")
7753 "TODO FEEDBACK VERIFY DONE")
7754 (if (equal org-todo-interpretation 'type)
7755 (mapconcat 'identity org-todo-keywords " ")
7756 "Me Jason Marie DONE")
7757 (cdr (assoc org-startup-folded
7758 '((nil . "nofold")(t . "fold")(content . "content"))))
7759 (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
7760 org-archive-location
7761 ))
7762
7763 (defun org-insert-export-options-template ()
7764 "Insert into the buffer a template with information for exporting."
7765 (interactive)
7766 (if (not (bolp)) (newline))
7767 (let ((s (org-get-current-options)))
7768 (and (string-match "#\\+CATEGORY" s)
7769 (setq s (substring s 0 (match-beginning 0))))
7770 (insert s)))
7771
7772 (defun org-toggle-fixed-width-section (arg)
7773 "Toggle the fixed-width indicator at the beginning of lines in the region.
7774 If there is no active region, only acts on the current line.
7775 If the first non-white character in the first line of the region is a
7776 vertical bar \"|\", then the command removes the bar from all lines in
7777 the region. If the first character is not a bar, the command adds a
7778 bar to all lines, in the column given by the beginning of the region.
7779
7780 If there is a numerical prefix ARG, create ARG new lines starting with \"|\"."
7781 (interactive "P")
7782 (let* ((cc 0)
7783 (regionp (org-region-active-p))
7784 (beg (if regionp (region-beginning) (point)))
7785 (end (if regionp (region-end)))
7786 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
7787 (re "[ \t]*\\(:\\)")
7788 off)
7789 (save-excursion
7790 (goto-char beg)
7791 (setq cc (current-column))
7792 (beginning-of-line 1)
7793 (setq off (looking-at re))
7794 (while (> nlines 0)
7795 (setq nlines (1- nlines))
7796 (beginning-of-line 1)
7797 (cond
7798 (arg
7799 (move-to-column cc t)
7800 (insert ":\n")
7801 (forward-line -1))
7802 ((and off (looking-at re))
7803 (replace-match "" t t nil 1))
7804 ((not off) (move-to-column cc t) (insert ":")))
7805 (forward-line 1)))))
7806
7807 (defun org-export-as-html-and-open (arg)
7808 "Export the outline as HTML and immediately open it with a browser.
7809 If there is an active region, export only the region.
7810 The prefix ARG specifies how many levels of the outline should become
7811 headlines. The default is 3. Lower levels will become bulleted lists."
7812 (interactive "P")
7813 (org-export-as-html arg 'hidden)
7814 (org-open-file (buffer-file-name)))
7815
7816 (defun org-export-as-html-batch ()
7817 "Call `org-export-as-html', may be used in batch processing as
7818 emacs --batch
7819 --load=$HOME/lib/emacs/org.el
7820 --eval \"(setq org-export-headline-levels 2)\"
7821 --visit=MyFile --funcall org-export-as-html-batch"
7822 (org-export-as-html org-export-headline-levels 'hidden))
7823
7824 (defun org-export-as-html (arg &optional hidden)
7825 "Export the outline as a pretty HTML file.
7826 If there is an active region, export only the region.
7827 The prefix ARG specifies how many levels of the outline should become
7828 headlines. The default is 3. Lower levels will become bulleted lists."
7829 (interactive "P")
7830 (setq-default org-todo-line-regexp org-todo-line-regexp)
7831 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
7832 (setq-default org-done-string org-done-string)
7833 (let* ((region-p (org-region-active-p))
7834 (region
7835 (buffer-substring
7836 (if region-p (region-beginning) (point-min))
7837 (if region-p (region-end) (point-max))))
7838 (all_lines
7839 (org-skip-comments (org-split-string region "[\r\n]")))
7840 (lines (org-export-find-first-heading-line all_lines))
7841 (level 0) (line "") (origline "") txt todo
7842 (umax nil)
7843 (filename (concat (file-name-sans-extension (buffer-file-name))
7844 ".html"))
7845 (buffer (find-file-noselect filename))
7846 (levels-open (make-vector org-level-max nil))
7847 (date (format-time-string "%Y/%m/%d" (current-time)))
7848 (time (format-time-string "%X" (current-time)))
7849 (author user-full-name)
7850 (title (buffer-name))
7851 (options nil)
7852 (email user-mail-address)
7853 (language org-export-default-language)
7854 (text nil)
7855 (lang-words nil)
7856 (head-count 0) cnt
7857 (start 0)
7858 table-open type
7859 table-buffer table-orig-buffer
7860 )
7861 (message "Exporting...")
7862
7863 (setq org-last-level 1)
7864 (org-init-section-numbers)
7865
7866 ;; Search for the export key lines
7867 (org-parse-key-lines)
7868 (setq lang-words (or (assoc language org-export-language-setup)
7869 (assoc "en" org-export-language-setup)))
7870
7871 ;; Switch to the output buffer
7872 (if (or hidden (not org-export-html-show-new-buffer))
7873 (set-buffer buffer)
7874 (switch-to-buffer-other-window buffer))
7875 (erase-buffer)
7876 (fundamental-mode)
7877 (let ((case-fold-search nil))
7878 (if options (org-parse-export-options options))
7879 (setq umax (if arg (prefix-numeric-value arg)
7880 org-export-headline-levels))
7881
7882 ;; File header
7883 (insert (format
7884 "<html lang=\"%s\"><head>
7885 <title>%s</title>
7886 <meta http-equiv=\"Content-Type\" content=\"text/html\">
7887 <meta name=generator content=\"Org-mode\">
7888 <meta name=generated content=\"%s %s\">
7889 <meta name=author content=\"%s\">
7890 </head><body>
7891 "
7892 language (org-html-expand title) date time author))
7893 (if title (insert (concat "<H1 align=\"center\">"
7894 (org-html-expand title) "</H1>\n")))
7895 (if author (insert (concat (nth 1 lang-words) ": " author "\n")))
7896 (if email (insert (concat "<a href=\"mailto:" email "\">&lt;"
7897 email "&gt;</a>\n")))
7898 (if (or author email) (insert "<br>\n"))
7899 (if (and date time) (insert (concat (nth 2 lang-words) ": "
7900 date " " time "<br>\n")))
7901 (if text (insert (concat "<p>\n" (org-html-expand text))))
7902 (if org-export-with-toc
7903 (progn
7904 (insert (format "<H2>%s</H2>\n" (nth 3 lang-words)))
7905 (insert "<ul>\n")
7906 (mapcar '(lambda (line)
7907 (if (string-match org-todo-line-regexp line)
7908 ;; This is a headline
7909 (progn
7910 (setq level (- (match-end 1) (match-beginning 1))
7911 txt (save-match-data
7912 (org-html-expand
7913 (match-string 3 line)))
7914 todo
7915 (or (and (match-beginning 2)
7916 (not (equal (match-string 2 line)
7917 org-done-string)))
7918 ; TODO, not DONE
7919 (and (= level umax)
7920 (org-search-todo-below
7921 line lines level))))
7922 (if org-export-with-section-numbers
7923 (setq txt (concat (org-section-number level)
7924 " " txt)))
7925 (if (<= level umax)
7926 (progn
7927 (setq head-count (+ head-count 1))
7928 (if (> level org-last-level)
7929 (progn
7930 (setq cnt (- level org-last-level))
7931 (while (>= (setq cnt (1- cnt)) 0)
7932 (insert "<ul>"))
7933 (insert "\n")))
7934 (if (< level org-last-level)
7935 (progn
7936 (setq cnt (- org-last-level level))
7937 (while (>= (setq cnt (1- cnt)) 0)
7938 (insert "</ul>"))
7939 (insert "\n")))
7940 (insert
7941 (format
7942 (if todo
7943 "<li><a href=\"#sec-%d\"><span style='color:red'>%s</span></a></li>\n"
7944 "<li><a href=\"#sec-%d\">%s</a></li>\n")
7945 head-count txt))
7946 (setq org-last-level level))
7947 ))))
7948 lines)
7949 (while (> org-last-level 0)
7950 (setq org-last-level (1- org-last-level))
7951 (insert "</ul>\n"))
7952 ))
7953 (setq head-count 0)
7954 (org-init-section-numbers)
7955 (while (setq line (pop lines) origline line)
7956 ;; Protect the links
7957 (setq start 0)
7958 (while (string-match org-link-maybe-angles-regexp line start)
7959 (setq start (match-end 0))
7960 (setq line (replace-match
7961 (concat "\000" (match-string 1 line) "\000")
7962 t t line)))
7963
7964 ;; replace "<" and ">" by "&lt;" and "&gt;"
7965 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
7966 (setq line (org-html-expand line))
7967
7968 ;; Verbatim lines
7969 (if (and org-export-with-fixed-width
7970 (string-match "^[ \t]*:\\(.*\\)" line))
7971 (progn
7972 (let ((l (match-string 1 line)))
7973 (while (string-match " " l)
7974 (setq l (replace-match "&nbsp;" t t l)))
7975 (insert "\n<span style='font-family:Courier'>"
7976 l "</span>"
7977 (if (and lines
7978 (not (string-match "^[ \t]+\\(:.*\\)"
7979 (car lines))))
7980 "<br>\n" "\n"))))
7981 (setq start 0)
7982 (while (string-match org-protected-link-regexp line start)
7983 (setq start (- (match-end 0) 2))
7984 (setq type (match-string 1 line))
7985 (cond
7986 ((member type '("http" "https" "ftp" "mailto" "news"))
7987 ;; standard URL
7988 (setq line (replace-match
7989 ; "<a href=\"\\1:\\2\">&lt;\\1:\\2&gt;</a>"
7990 "<a href=\"\\1:\\2\">\\1:\\2</a>"
7991 nil nil line)))
7992 ((string= type "file")
7993 ;; FILE link
7994 (let* ((filename (match-string 2 line))
7995 (abs-p (file-name-absolute-p filename))
7996 (thefile (if abs-p (expand-file-name filename) filename))
7997 (thefile (save-match-data
7998 (if (string-match ":[0-9]+$" thefile)
7999 (replace-match "" t t thefile)
8000 thefile)))
8001 (file-is-image-p
8002 (save-match-data
8003 (string-match (org-image-file-name-regexp) thefile))))
8004 (setq line (replace-match
8005 (if (and org-export-html-inline-images
8006 file-is-image-p)
8007 (concat "<img src=\"" thefile "\"/>")
8008 (concat "<a href=\"" thefile "\">\\1:\\2</a>"))
8009 nil nil line))))
8010
8011 ((member type '("bbdb" "vm" "wl" "rmail" "gnus" "shell"))
8012 (setq line (replace-match
8013 "<i>&lt;\\1:\\2&gt;</i>" nil nil line)))))
8014
8015 ;; TODO items
8016 (if (and (string-match org-todo-line-regexp line)
8017 (match-beginning 2))
8018 (if (equal (match-string 2 line) org-done-string)
8019 (setq line (replace-match
8020 "<span style='color:green'>\\2</span>"
8021 nil nil line 2))
8022 (setq line (replace-match "<span style='color:red'>\\2</span>"
8023 nil nil line 2))))
8024
8025 ;; DEADLINES
8026 (if (string-match org-deadline-line-regexp line)
8027 (progn
8028 (if (save-match-data
8029 (string-match "<a href"
8030 (substring line 0 (match-beginning 0))))
8031 nil ; Don't do the replacement - it is inside a link
8032 (setq line (replace-match "<span style='color:red'>\\&</span>"
8033 nil nil line 1)))))
8034
8035 (cond
8036 ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
8037 ;; This is a headline
8038 (setq level (- (match-end 1) (match-beginning 1))
8039 txt (match-string 2 line))
8040 (if (<= level umax) (setq head-count (+ head-count 1)))
8041 (org-html-level-start level txt umax
8042 (and org-export-with-toc (<= level umax))
8043 head-count))
8044
8045 ((and org-export-with-tables
8046 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
8047 (if (not table-open)
8048 ;; New table starts
8049 (setq table-open t table-buffer nil table-orig-buffer nil))
8050 ;; Accumulate lines
8051 (setq table-buffer (cons line table-buffer)
8052 table-orig-buffer (cons origline table-orig-buffer))
8053 (when (or (not lines)
8054 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
8055 (car lines))))
8056 (setq table-open nil
8057 table-buffer (nreverse table-buffer)
8058 table-orig-buffer (nreverse table-orig-buffer))
8059 (insert (org-format-table-html table-buffer table-orig-buffer))))
8060 (t
8061 ;; Normal lines
8062 ;; Lines starting with "-", and empty lines make new paragraph.
8063 (if (string-match "^ *-\\|^[ \t]*$" line) (insert "<p>"))
8064 (insert line (if org-export-preserve-breaks "<br>\n" "\n"))))
8065 ))
8066 (if org-export-html-with-timestamp
8067 (insert org-export-html-html-helper-timestamp))
8068 (insert "</body>\n</html>\n")
8069 (normal-mode)
8070 (save-buffer)
8071 (goto-char (point-min)))))
8072
8073 (defun org-format-table-html (lines olines)
8074 "Find out which HTML converter to use and return the HTML code."
8075 (if (string-match "^[ \t]*|" (car lines))
8076 ;; A normal org table
8077 (org-format-org-table-html lines)
8078 ;; Table made by table.el - test for spanning
8079 (let* ((hlines (delq nil (mapcar
8080 (lambda (x)
8081 (if (string-match "^[ \t]*\\+-" x) x
8082 nil))
8083 lines)))
8084 (first (car hlines))
8085 (ll (and (string-match "\\S-+" first)
8086 (match-string 0 first)))
8087 (re (concat "^[ \t]*" (regexp-quote ll)))
8088 (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
8089 hlines))))
8090 (if (and (not spanning)
8091 (not org-export-prefer-native-exporter-for-tables))
8092 ;; We can use my own converter with HTML conversions
8093 (org-format-table-table-html lines)
8094 ;; Need to use the code generator in table.el, with the original text.
8095 (org-format-table-table-html-using-table-generate-source olines)))))
8096
8097 (defun org-format-org-table-html (lines)
8098 "Format a table into html."
8099 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
8100 (setq lines (nreverse lines))
8101 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
8102 (setq lines (nreverse lines))
8103 (let ((head (and org-export-highlight-first-table-line
8104 (delq nil (mapcar
8105 (lambda (x) (string-match "^[ \t]*|-" x))
8106 (cdr lines)))))
8107 line fields html)
8108 (setq html (concat org-export-html-table-tag "\n"))
8109 (while (setq line (pop lines))
8110 (catch 'next-line
8111 (if (string-match "^[ \t]*|-" line)
8112 (progn
8113 (setq head nil) ;; head ends here, first time around
8114 ;; ignore this line
8115 (throw 'next-line t)))
8116 ;; Break the line into fields
8117 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
8118 (setq html (concat
8119 html
8120 "<tr>"
8121 (mapconcat (lambda (x)
8122 (if head
8123 (concat "<th>" x "</th>")
8124 (concat "<td valign=\"top\">" x "</td>")))
8125 fields "")
8126 "</tr>\n"))))
8127 (setq html (concat html "</table>\n"))
8128 html))
8129
8130 (defun org-fake-empty-table-line (line)
8131 "Replace everything except \"|\" with spaces."
8132 (let ((i (length line))
8133 (newstr (copy-sequence line)))
8134 (while (> i 0)
8135 (setq i (1- i))
8136 (if (not (eq (aref newstr i) ?|))
8137 (aset newstr i ?\ )))
8138 newstr))
8139
8140 (defun org-format-table-table-html (lines)
8141 "Format a table generated by table.el into html.
8142 This conversion does *not* use `table-generate-source' from table.el.
8143 This has the advantage that Org-mode's HTML conversions can be used.
8144 But it has the disadvantage, that no cell- or row-spanning is allowed."
8145 (let (line field-buffer
8146 (head org-export-highlight-first-table-line)
8147 fields html empty)
8148 (setq html (concat org-export-html-table-tag "\n"))
8149 (while (setq line (pop lines))
8150 (setq empty "&nbsp")
8151 (catch 'next-line
8152 (if (string-match "^[ \t]*\\+-" line)
8153 (progn
8154 (if field-buffer
8155 (progn
8156 (setq html (concat
8157 html
8158 "<tr>"
8159 (mapconcat
8160 (lambda (x)
8161 (if (equal x "") (setq x empty))
8162 (if head
8163 (concat "<th valign=\"top\">" x
8164 "</th>\n")
8165 (concat "<td valign=\"top\">" x
8166 "</td>\n")))
8167 field-buffer "\n")
8168 "</tr>\n"))
8169 (setq head nil)
8170 (setq field-buffer nil)))
8171 ;; Ignore this line
8172 (throw 'next-line t)))
8173 ;; Break the line into fields and store the fields
8174 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
8175 (if field-buffer
8176 (setq field-buffer (mapcar
8177 (lambda (x)
8178 (concat x "<br>" (pop fields)))
8179 field-buffer))
8180 (setq field-buffer fields))))
8181 (setq html (concat html "</table>\n"))
8182 html))
8183
8184 (defun org-format-table-table-html-using-table-generate-source (lines)
8185 "Format a table into html, using `table-generate-source' from table.el.
8186 This has the advantage that cell- or row-spanning is allowed.
8187 But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
8188 (require 'table)
8189 (with-current-buffer (get-buffer-create " org-tmp1 ")
8190 (erase-buffer)
8191 (insert (mapconcat 'identity lines "\n"))
8192 (goto-char (point-min))
8193 (if (not (re-search-forward "|[^+]" nil t))
8194 (error "Error processing table"))
8195 (table-recognize-table)
8196 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
8197 (table-generate-source 'html " org-tmp2 ")
8198 (set-buffer " org-tmp2 ")
8199 (buffer-substring (point-min) (point-max))))
8200
8201 (defun org-html-expand (string)
8202 "Prepare STRING for HTML export. Applies all active conversions."
8203 ;; First check if there is a link in the line - if yes, apply conversions
8204 ;; only before the start of the link.
8205 (let* ((m (string-match org-link-regexp string))
8206 (s (if m (substring string 0 m) string))
8207 (r (if m (substring string m) "")))
8208 ;; convert < to &lt; and > to &gt;
8209 (while (string-match "<" s)
8210 (setq s (replace-match "&lt;" t t s)))
8211 (while (string-match ">" s)
8212 (setq s (replace-match "&gt;" t t s)))
8213 (if org-export-html-expand
8214 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
8215 (setq s (replace-match "<\\1>" nil nil s))))
8216 (if org-export-with-emphasize
8217 (setq s (org-export-html-convert-emphasize s)))
8218 (if org-export-with-sub-superscripts
8219 (setq s (org-export-html-convert-sub-super s)))
8220 (if org-export-with-TeX-macros
8221 (let ((start 0) wd ass)
8222 (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
8223 (setq wd (match-string 1 s))
8224 (if (setq ass (assoc wd org-html-entities))
8225 (setq s (replace-match (or (cdr ass)
8226 (concat "&" (car ass) ";"))
8227 t t s))
8228 (setq start (+ start (length wd)))))))
8229 (concat s r)))
8230
8231 (defun org-create-multibrace-regexp (left right n)
8232 "Create a regular expression which will match a balanced sexp.
8233 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
8234 as single character strings.
8235 The regexp returned will match the entire expression including the
8236 delimiters. It will also define a single group which contains the
8237 match except for the outermost delimiters. The maximum depth of
8238 stacked delimiters is N. Escaping delimiters is not possible."
8239 (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
8240 (or "\\|")
8241 (re nothing)
8242 (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
8243 (while (> n 1)
8244 (setq n (1- n)
8245 re (concat re or next)
8246 next (concat "\\(?:" nothing left next right "\\)+" nothing)))
8247 (concat left "\\(" re "\\)" right)))
8248
8249 (defvar org-match-substring-regexp
8250 (concat
8251 "\\([^\\]\\)\\([_^]\\)\\("
8252 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
8253 "\\|"
8254 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
8255 "\\|"
8256 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
8257 "The regular expression matching a sub- or superscript.")
8258
8259 (defun org-export-html-convert-sub-super (string)
8260 "Convert sub- and superscripts in STRING to HTML."
8261 (let (key c)
8262 (while (string-match org-match-substring-regexp string)
8263 (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
8264 (setq c (or (match-string 8 string)
8265 (match-string 6 string)
8266 (match-string 5 string)))
8267 (setq string (replace-match
8268 (concat (match-string 1 string)
8269 "<" key ">" c "</" key ">")
8270 t t string)))
8271 (while (string-match "\\\\\\([_^]\\)" string)
8272 (setq string (replace-match (match-string 1 string) t t string))))
8273 string)
8274
8275 (defun org-export-html-convert-emphasize (string)
8276 (while (string-match
8277 "\\(\\s-\\|^\\)\\(\\*\\([a-zA-Z]+\\)\\*\\)\\([^a-zA-Z*]\\|$\\)"
8278 string)
8279 (setq string (replace-match
8280 (concat "<b>" (match-string 3 string) "</b>")
8281 t t string 2)))
8282 (while (string-match
8283 "\\(\\s-\\|^\\)\\(/\\([a-zA-Z]+\\)/\\)\\([^a-zA-Z*]\\|$\\)"
8284 string)
8285 (setq string (replace-match
8286 (concat "<i>" (match-string 3 string) "</i>")
8287 t t string 2)))
8288 (while (string-match
8289 "\\(\\s-\\|^\\)\\(_\\([a-zA-Z]+\\)_\\)\\([^a-zA-Z*]\\|$\\)"
8290 string)
8291 (setq string (replace-match
8292 (concat "<u>" (match-string 3 string) "</u>")
8293 t t string 2)))
8294 string)
8295
8296 (defun org-parse-key-lines ()
8297 "Find the special key lines with the information for exporters."
8298 (save-excursion
8299 (goto-char 0)
8300 (let ((re (org-make-options-regexp
8301 '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
8302 key)
8303 (while (re-search-forward re nil t)
8304 (setq key (match-string 1))
8305 (cond ((string-equal key "TITLE")
8306 (setq title (match-string 2)))
8307 ((string-equal key "AUTHOR")
8308 (setq author (match-string 2)))
8309 ((string-equal key "EMAIL")
8310 (setq email (match-string 2)))
8311 ((string-equal key "LANGUAGE")
8312 (setq language (match-string 2)))
8313 ((string-equal key "TEXT")
8314 (setq text (concat text "\n" (match-string 2))))
8315 ((string-equal key "OPTIONS")
8316 (setq options (match-string 2))))))))
8317
8318 (defun org-parse-export-options (s)
8319 "Parse the export options line."
8320 (let ((op '(("H" . org-export-headline-levels)
8321 ("num" . org-export-with-section-numbers)
8322 ("toc" . org-export-with-toc)
8323 ("\\n" . org-export-preserve-breaks)
8324 ("@" . org-export-html-expand)
8325 (":" . org-export-with-fixed-width)
8326 ("|" . org-export-with-tables)
8327 ("^" . org-export-with-sub-superscripts)
8328 ("*" . org-export-with-emphasize)
8329 ("TeX" . org-export-with-TeX-macros)))
8330 o)
8331 (while (setq o (pop op))
8332 (if (string-match (concat (regexp-quote (car o)) ":\\([^ \t\n\r;,.]*\\)")
8333 s)
8334 (set (make-local-variable (cdr o))
8335 (car (read-from-string (match-string 1 s))))))))
8336
8337 (defun org-html-level-start (level title umax with-toc head-count)
8338 "Insert a new level in HTML export."
8339 (let ((l (1+ (max level umax))))
8340 (while (<= l org-level-max)
8341 (if (aref levels-open (1- l))
8342 (progn
8343 (org-html-level-close l)
8344 (aset levels-open (1- l) nil)))
8345 (setq l (1+ l)))
8346 (if (> level umax)
8347 (progn
8348 (if (aref levels-open (1- level))
8349 (insert "<li>" title "<p>\n")
8350 (aset levels-open (1- level) t)
8351 (insert "<ul><li>" title "<p>\n")))
8352 (if org-export-with-section-numbers
8353 (setq title (concat (org-section-number level) " " title)))
8354 (setq level (+ level 1))
8355 (if with-toc
8356 (insert (format "\n<H%d><a name=\"sec-%d\">%s</a></H%d>\n"
8357 level head-count title level))
8358 (insert (format "\n<H%d>%s</H%d>\n" level title level))))))
8359
8360 (defun org-html-level-close (&rest args)
8361 "Terminate one level in HTML export."
8362 (insert "</ul>"))
8363
8364
8365 ;; Variable holding the vector with section numbers
8366 (defvar org-section-numbers (make-vector org-level-max 0))
8367
8368 (defun org-init-section-numbers ()
8369 "Initialize the vector for the section numbers."
8370 (let* ((level -1)
8371 (numbers (nreverse (org-split-string "" "\\.")))
8372 (depth (1- (length org-section-numbers)))
8373 (i depth) number-string)
8374 (while (>= i 0)
8375 (if (> i level)
8376 (aset org-section-numbers i 0)
8377 (setq number-string (or (car numbers) "0"))
8378 (if (string-match "\\`[A-Z]\\'" number-string)
8379 (aset org-section-numbers i
8380 (- (string-to-char number-string) ?A -1))
8381 (aset org-section-numbers i (string-to-int number-string)))
8382 (pop numbers))
8383 (setq i (1- i)))))
8384
8385 (defun org-section-number (&optional level)
8386 "Return a string with the current section number.
8387 When LEVEL is non-nil, increase section numbers on that level."
8388 (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
8389 (when level
8390 (when (> level -1)
8391 (aset org-section-numbers
8392 level (1+ (aref org-section-numbers level))))
8393 (setq idx (1+ level))
8394 (while (<= idx depth)
8395 (if (not (= idx 1))
8396 (aset org-section-numbers idx 0))
8397 (setq idx (1+ idx))))
8398 (setq idx 0)
8399 (while (<= idx depth)
8400 (setq n (aref org-section-numbers idx))
8401 (setq string (concat string (if (not (string= string "")) "." "")
8402 (int-to-string n)))
8403 (setq idx (1+ idx)))
8404 (save-match-data
8405 (if (string-match "\\`\\([@0]\\.\\)+" string)
8406 (setq string (replace-match "" nil nil string)))
8407 (if (string-match "\\(\\.0\\)+\\'" string)
8408 (setq string (replace-match "" nil nil string))))
8409 string))
8410
8411
8412 ;;; Key bindings
8413
8414 ;; - Bindings in Org-mode map are currently
8415 ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
8416 ;; abcd fgh j lmnopqrstuvwxyz ? # -+ /= [] ; |,.<> \t necessary bindings
8417 ;; e (?) useful from outline-mode
8418 ;; i k @ expendable from outline-mode
8419 ;; 0123456789 ! $%^& * ()_{} " ~`' free
8420
8421 (define-key org-mode-map "\C-i" 'org-cycle)
8422 (define-key org-mode-map [(meta tab)] 'org-complete)
8423 (define-key org-mode-map "\M-\C-i" 'org-complete)
8424 (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
8425 (define-key org-mode-map [(meta left)] 'org-metaleft)
8426 (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
8427 (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
8428 (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
8429 (define-key org-mode-map [(meta right)] 'org-metaright)
8430 (define-key org-mode-map [(meta up)] 'org-metaup)
8431 (define-key org-mode-map [(meta down)] 'org-metadown)
8432 ;(define-key org-mode-map "\C-c\C-h\C-w" 'org-cut-subtree)
8433 ;(define-key org-mode-map "\C-c\C-h\M-w" 'org-copy-subtree)
8434 ;(define-key org-mode-map "\C-c\C-h\C-y" 'org-paste-subtree)
8435 (define-key org-mode-map "\C-c\C-h\C-w" 'org-cut-special)
8436 (define-key org-mode-map "\C-c\C-h\M-w" 'org-copy-special)
8437 (define-key org-mode-map "\C-c\C-h\C-y" 'org-paste-special)
8438 (define-key org-mode-map "\C-c$" 'org-archive-subtree)
8439 (define-key org-mode-map "\C-c\C-j" 'org-goto)
8440 (define-key org-mode-map "\C-c\C-t" 'org-todo)
8441 (define-key org-mode-map "\C-c\C-s" 'org-schedule)
8442 (define-key org-mode-map "\C-c\C-d" 'org-deadline)
8443 (define-key org-mode-map "\C-c;" 'org-toggle-comment)
8444 (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
8445 (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
8446 (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
8447 (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
8448 (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
8449 (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
8450 (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
8451 (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
8452 (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
8453 (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
8454 (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
8455 (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
8456 (define-key org-mode-map "\C-c>" 'org-goto-calendar)
8457 (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
8458 (define-key org-mode-map "\C-c[" 'org-add-file)
8459 (define-key org-mode-map "\C-c]" 'org-remove-file)
8460 (define-key org-mode-map "\C-c\C-r" 'org-timeline)
8461 (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
8462 (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
8463 (define-key org-mode-map (org-key 'S-left) 'org-timestamp-down-day)
8464 (define-key org-mode-map (org-key 'S-right) 'org-timestamp-up-day)
8465 (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
8466 ;; The following line is e.g. necessary for German keyboards under Suse Linux
8467 (unless org-xemacs-p
8468 (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
8469 (define-key org-mode-map [(shift tab)] 'org-shifttab)
8470 (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
8471 (define-key org-mode-map "\C-m" 'org-return)
8472 (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
8473 (define-key org-mode-map [(meta return)] 'org-meta-return)
8474 (define-key org-mode-map "\C-c?" 'org-table-current-column)
8475 (define-key org-mode-map "\C-c " 'org-table-blank-field)
8476 (define-key org-mode-map "\C-c+" 'org-table-sum)
8477 (define-key org-mode-map "\C-c|" 'org-table-toggle-vline-visibility)
8478 (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
8479 (define-key org-mode-map "\C-c#" 'org-table-create-with-table.el)
8480 (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
8481 (define-key org-mode-map "\C-c\C-xa" 'org-export-as-ascii)
8482 (define-key org-mode-map "\C-c\C-x\C-a" 'org-export-as-ascii)
8483 (define-key org-mode-map "\C-c\C-xv" 'org-export-copy-visible)
8484 (define-key org-mode-map "\C-c\C-x\C-v" 'org-export-copy-visible)
8485 ;(define-key org-mode-map "\C-c\C-xo" 'org-export-as-opml)
8486 ;(define-key org-mode-map "\C-c\C-x\C-o" 'org-export-as-opml)
8487 (define-key org-mode-map "\C-c\C-xt" 'org-insert-export-options-template)
8488 (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
8489 (define-key org-mode-map "\C-c\C-xh" 'org-export-as-html)
8490 (define-key org-mode-map "\C-c\C-x\C-h" 'org-export-as-html-and-open)
8491
8492 (defsubst org-table-p ()
8493 (if (and (eq major-mode 'org-mode) font-lock-mode)
8494 (eq (get-text-property (point) 'face) 'org-table)
8495 ;; (save-match-data (org-at-table-p)))) ; FIXME: OK to not use this?
8496 (org-at-table-p)))
8497
8498
8499 (defun org-self-insert-command (N)
8500 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
8501 If the cursor is in a table looking at whitespace, the whitespace is
8502 overwritten, and the table is not marked as requiring realignment."
8503 (interactive "p")
8504 (if (and (org-table-p)
8505 (eq N 1)
8506 (looking-at "[^|\n]* +|"))
8507 (let (org-table-may-need-update)
8508 (goto-char (1- (match-end 0)))
8509 (delete-backward-char 1)
8510 (goto-char (match-beginning 0))
8511 (self-insert-command N))
8512 (setq org-table-may-need-update t)
8513 (self-insert-command N)))
8514
8515 ;; FIXME:
8516 ;; The following two functions might still be optimized to trigger
8517 ;; re-alignment less frequently.
8518
8519 (defun org-delete-backward-char (N)
8520 "Like `delete-backward-char', insert whitespace at field end in tables.
8521 When deleting backwards, in tables this function will insert whitespace in
8522 front of the next \"|\" separator, to keep the table aligned. The table will
8523 still be marked for re-alignment, because a narrow field may lead to a
8524 reduced column width."
8525 (interactive "p")
8526 (if (and (org-table-p)
8527 (eq N 1)
8528 (looking-at ".*?|"))
8529 (let ((pos (point)))
8530 (backward-delete-char N)
8531 (skip-chars-forward "^|")
8532 (insert " ")
8533 (goto-char (1- pos)))
8534 (backward-delete-char N)))
8535
8536 (defun org-delete-char (N)
8537 "Like `delete-char', but insert whitespace at field end in tables.
8538 When deleting characters, in tables this function will insert whitespace in
8539 front of the next \"|\" separator, to keep the table aligned. The table
8540 will still be marked for re-alignment, because a narrow field may lead to
8541 a reduced column width."
8542 (interactive "p")
8543 (if (and (org-table-p)
8544 (eq N 1))
8545 (if (looking-at ".*?|")
8546 (let ((pos (point)))
8547 (replace-match (concat
8548 (substring (match-string 0) 1 -1)
8549 " |"))
8550 (goto-char pos)))
8551 (delete-char N)))
8552
8553 ;; How to do this: Measure non-white length of current string
8554 ;; If equal to column width, we should realign.
8555
8556 (when (eq org-enable-table-editor 'optimized)
8557 ;; If the user wants maximum table support, we need to hijack
8558 ;; some standard editing functions
8559 (substitute-key-definition 'self-insert-command 'org-self-insert-command
8560 org-mode-map global-map)
8561 (substitute-key-definition 'delete-char 'org-delete-char
8562 org-mode-map global-map)
8563 (substitute-key-definition 'delete-backward-char 'org-delete-backward-char
8564 org-mode-map global-map)
8565 (define-key org-mode-map "|" 'self-insert-command))
8566
8567 (defun org-shiftcursor-error ()
8568 "Throw an error because Shift-Cursor command was applied in wrong context."
8569 (error "This command is only active in tables and on headlines"))
8570
8571 (defun org-shifttab ()
8572 "Call `(org-cycle t)' or `org-table-previous-field'."
8573 (interactive)
8574 (cond
8575 ((org-at-table-p) (org-table-previous-field))
8576 (t (org-cycle '(4)))))
8577
8578 (defun org-shiftmetaleft ()
8579 "Call `org-promote-subtree' or `org-table-delete-column'."
8580 (interactive)
8581 (cond
8582 ((org-at-table-p) (org-table-delete-column))
8583 ((org-on-heading-p) (org-promote-subtree))
8584 (t (org-shiftcursor-error))))
8585
8586 (defun org-shiftmetaright ()
8587 "Call `org-demote-subtree' or `org-table-insert-column'."
8588 (interactive)
8589 (cond
8590 ((org-at-table-p) (org-table-insert-column))
8591 ((org-on-heading-p) (org-demote-subtree))
8592 (t (org-shiftcursor-error))))
8593
8594 (defun org-shiftmetaup (&optional arg)
8595 "Call `org-move-subtree-up' or `org-table-kill-row'."
8596 (interactive "P")
8597 (cond
8598 ((org-at-table-p) (org-table-kill-row))
8599 ((org-on-heading-p) (org-move-subtree-up arg))
8600 (t (org-shiftcursor-error))))
8601 (defun org-shiftmetadown (&optional arg)
8602 "Call `org-move-subtree-down' or `org-table-insert-row'."
8603 (interactive "P")
8604 (cond
8605 ((org-at-table-p) (org-table-insert-row arg))
8606 ((org-on-heading-p) (org-move-subtree-down arg))
8607 (t (org-shiftcursor-error))))
8608
8609 (defun org-metaleft (&optional arg)
8610 "Call `org-do-promote' or `org-table-move-column' to left."
8611 (interactive "P")
8612 (cond
8613 ((org-at-table-p) (org-table-move-column 'left))
8614 ((or (org-on-heading-p) (org-region-active-p)) (org-do-promote))
8615 (t (backward-word (prefix-numeric-value arg)))))
8616
8617 (defun org-metaright (&optional arg)
8618 "Call `org-do-demote' or `org-table-move-column' to right."
8619 (interactive "P")
8620 (cond
8621 ((org-at-table-p) (org-table-move-column nil))
8622 ((or (org-on-heading-p) (org-region-active-p)) (org-do-demote))
8623 (t (forward-word (prefix-numeric-value arg)))))
8624
8625 (defun org-metaup (&optional arg)
8626 "Call `org-move-subtree-up' or `org-table-move-row' up."
8627 (interactive "P")
8628 (cond
8629 ((org-at-table-p) (org-table-move-row 'up))
8630 ((org-on-heading-p) (org-move-subtree-up arg))
8631 (t (org-shiftcursor-error))))
8632
8633 (defun org-metadown (&optional arg)
8634 "Call `org-move-subtree-down' or `org-table-move-row' down."
8635 (interactive "P")
8636 (cond
8637 ((org-at-table-p) (org-table-move-row nil))
8638 ((org-on-heading-p) (org-move-subtree-down arg))
8639 (t (org-shiftcursor-error))))
8640
8641 (defun org-shiftup (&optional arg)
8642 "Call `org-timestamp-up' or `org-priority-up'."
8643 (interactive "P")
8644 (cond
8645 ((org-at-timestamp-p) (org-timestamp-up arg))
8646 (t (org-priority-up))))
8647
8648 (defun org-shiftdown (&optional arg)
8649 "Call `org-timestamp-down' or `org-priority-down'."
8650 (interactive "P")
8651 (cond
8652 ((org-at-timestamp-p) (org-timestamp-down arg))
8653 (t (org-priority-down))))
8654
8655 (defun org-copy-special ()
8656 "Call either `org-table-copy' or `org-copy-subtree'."
8657 (interactive)
8658 (if (org-at-table-p)
8659 (org-table-copy-region)
8660 (org-copy-subtree)))
8661
8662 (defun org-cut-special ()
8663 "Call either `org-table-copy' or `org-cut-subtree'."
8664 (interactive)
8665 (if (org-at-table-p)
8666 (org-table-cut-region)
8667 (org-cut-subtree)))
8668
8669 (defun org-paste-special (arg)
8670 "Call either `org-table-paste-rectangle' or `org-paste-subtree'."
8671 (interactive "P")
8672 (if (org-at-table-p)
8673 (org-table-paste-rectangle)
8674 (org-paste-subtree arg)))
8675
8676 (defun org-ctrl-c-ctrl-c (&optional arg)
8677 "Call realign table, or recognize a table.el table.
8678 When the cursor is inside a table created by the table.el package,
8679 activate that table. Otherwise, if the cursor is at a normal table
8680 created with org.el, re-align that table. This command works even if
8681 the automatic table editor has been turned off."
8682 (interactive "P")
8683 (let ((org-enable-table-editor t))
8684 (cond
8685 ((org-at-table.el-p)
8686 (require 'table)
8687 (beginning-of-line 1)
8688 (re-search-forward "|" (save-excursion (end-of-line 2) (point))) ;FIXME: line-end-position?
8689 (table-recognize-table))
8690 ((org-at-table-p)
8691 (org-table-align))
8692 ((save-excursion (beginning-of-line 1) (looking-at "#\\+[A-Z]+"))
8693 (let ((org-inhibit-startup t)) (org-mode)))
8694 ((org-region-active-p)
8695 (org-table-convert-region (region-beginning) (region-end) arg))
8696 ((and (region-beginning) (region-end))
8697 (if (y-or-n-p "Convert inactive region to table? ")
8698 (org-table-convert-region (region-beginning) (region-end) arg)
8699 (error "Abort")))
8700 (t (error "No table at point, and no region to make one")))))
8701
8702 (defun org-return ()
8703 "Call `org-table-next-row' or `newline'."
8704 (interactive)
8705 (cond
8706 ((org-at-table-p)
8707 (org-table-justify-field-maybe)
8708 (org-table-next-row))
8709 (t (newline))))
8710
8711 (defun org-meta-return (&optional arg)
8712 "Call `org-insert-heading' or `org-table-wrap-region'."
8713 (interactive "P")
8714 (cond
8715 ((org-at-table-p)
8716 (org-table-wrap-region arg))
8717 (t (org-insert-heading))))
8718
8719 ;;; Menu entries
8720
8721 ;; First, remove the outline menus. Org-mode does not neede these commands.
8722 (if org-xemacs-p
8723 (add-hook 'org-mode-hook
8724 (lambda ()
8725 (delete-menu-item '("Headings"))
8726 (delete-menu-item '("Show"))
8727 (delete-menu-item '("Hide"))
8728 (set-menubar-dirty-flag)))
8729 (setq org-mode-map (delq (assoc 'menu-bar (cdr org-mode-map))
8730 org-mode-map)))
8731
8732 ;; Define the Org-mode menus
8733 (easy-menu-define org-org-menu org-mode-map "Org menu"
8734 '("Org"
8735 ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
8736 ["Sparse Tree" org-occur t]
8737 ["Show All" show-all t]
8738 "--"
8739 ["New Heading" org-insert-heading t]
8740 ("Navigate Headings"
8741 ["Up" outline-up-heading t]
8742 ["Next" outline-next-visible-heading t]
8743 ["Previous" outline-previous-visible-heading t]
8744 ["Next Same Level" outline-forward-same-level t]
8745 ["Previous Same Level" outline-backward-same-level t]
8746 "--"
8747 ["Jump" org-goto t])
8748 ("Edit Structure"
8749 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
8750 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
8751 "--"
8752 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
8753 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
8754 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
8755 "--"
8756 ["Promote Heading" org-metaleft (not (org-at-table-p))]
8757 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
8758 ["Demote Heading" org-metaright (not (org-at-table-p))]
8759 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
8760 "--"
8761 ["Archive Subtree" org-archive-subtree t])
8762 "--"
8763 ("TODO Lists"
8764 ["TODO/DONE/-" org-todo t]
8765 ["Show TODO Tree" org-show-todo-tree t]
8766 "--"
8767 ["Set Priority" org-priority t]
8768 ["Priority Up" org-shiftup t]
8769 ["Priority Down" org-shiftdown t])
8770 ("Dates and Scheduling"
8771 ["Timestamp" org-time-stamp t]
8772 ("Change Date"
8773 ["1 Day Later" org-timestamp-up-day t]
8774 ["1 Day Earlier" org-timestamp-down-day t]
8775 ["1 ... Later" org-shiftup t]
8776 ["1 ... Earlier" org-shiftdown t])
8777 ["Compute Time Range" org-evaluate-time-range t]
8778 ["Schedule Item" org-schedule t]
8779 ["Deadline" org-deadline t]
8780 "--"
8781 ["Goto Calendar" org-goto-calendar t]
8782 ["Date from Calendar" org-date-from-calendar t])
8783 "--"
8784 ("Timeline/Agenda"
8785 ["Show TODO Tree this File" org-show-todo-tree t]
8786 ["Check Deadlines this File" org-check-deadlines t]
8787 ["Timeline Current File" org-timeline t]
8788 "--"
8789 ["Agenda" org-agenda t])
8790 ("File List for Agenda")
8791 "--"
8792 ("Hyperlinks"
8793 ["Store Link (Global)" org-store-link t]
8794 ["Insert Link" org-insert-link t]
8795 ["Follow Link" org-open-at-point t])
8796 "--"
8797 ("Table"
8798 ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
8799 ["Next Field" org-cycle (org-at-table-p)]
8800 ["Previous Field" org-shifttab (org-at-table-p)]
8801 ["Next Row" org-return (org-at-table-p)]
8802 "--"
8803 ["Blank Field" org-table-blank-field (org-at-table-p)]
8804 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
8805 "--"
8806 ("Column"
8807 ["Move Column Left" org-metaleft (org-at-table-p)]
8808 ["Move Column Right" org-metaright (org-at-table-p)]
8809 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
8810 ["Insert Column" org-shiftmetaright (org-at-table-p)])
8811 ("Row"
8812 ["Move Row Up" org-metaup (org-at-table-p)]
8813 ["Move Row Down" org-metadown (org-at-table-p)]
8814 ["Delete Row" org-shiftmetaup (org-at-table-p)]
8815 ["Insert Row" org-shiftmetadown (org-at-table-p)]
8816 "--"
8817 ["Insert Hline" org-table-insert-hline (org-at-table-p)])
8818 ("Rectangle"
8819 ["Copy Rectangle" org-copy-special (org-at-table-p)]
8820 ["Cut Rectangle" org-cut-special (org-at-table-p)]
8821 ["Paste Rectangle" org-paste-special (org-at-table-p)]
8822 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
8823 "--"
8824 ["Which Column?" org-table-current-column (org-at-table-p)]
8825 ["Sum Column/Rectangle" org-table-sum
8826 (or (org-at-table-p) (org-region-active-p))]
8827 ["Eval Formula" org-table-eval-formula (org-at-table-p)]
8828 "--"
8829 ["Invisible Vlines" org-table-toggle-vline-visibility
8830 :style toggle :selected (org-in-invisibility-spec-p '(org-table))]
8831 "--"
8832 ["Create" org-table-create (and (not (org-at-table-p))
8833 org-enable-table-editor)]
8834 ["Convert Region" org-ctrl-c-ctrl-c (not (org-at-table-p 'any))]
8835 ["Import from File" org-table-import (not (org-at-table-p))]
8836 ["Export to File" org-table-export (org-at-table-p)]
8837 "--"
8838 ["Create/Convert from/to table.el" org-table-create-with-table.el t])
8839 "--"
8840 ("Export"
8841 ["ASCII" org-export-as-ascii t]
8842 ["Extract Visible Text" org-export-copy-visible t]
8843 ["HTML" org-export-as-html t]
8844 ["HTML and Open" org-export-as-html-and-open t]
8845 ; ["OPML" org-export-as-opml nil]
8846 "--"
8847 ["Option Template" org-insert-export-options-template t]
8848 ["Toggle Fixed Width" org-toggle-fixed-width-section t])
8849 "--"
8850 ("Documentation"
8851 ["Show Version" org-version t]
8852 ["Info Documentation" org-info t])
8853 ("Customize"
8854 ["Browse Org Group" org-customize t]
8855 "--"
8856 ["Build Full Customize Menu" org-create-customize-menu
8857 (fboundp 'customize-menu-create)])
8858 ))
8859
8860 (defun org-info (&optional node)
8861 "Read documentation for Org-mode in the info system.
8862 With optional NODE, go directly to that node."
8863 (interactive)
8864 (require 'info)
8865 (Info-goto-node (format "(org)%s" (or node ""))))
8866
8867 (defun org-install-agenda-files-menu ()
8868 (easy-menu-change
8869 '("Org") "File List for Agenda"
8870 (append
8871 (list
8872 ["Edit File List" (customize-variable 'org-agenda-files) t]
8873 ["Add Current File to List" org-add-file t]
8874 ["Remove Current File from List" org-remove-file t]
8875 "--")
8876 (mapcar 'org-file-menu-entry org-agenda-files))))
8877
8878 ;;; Documentation
8879
8880 (defun org-customize ()
8881 "Call the customize function with org as argument."
8882 (interactive)
8883 (customize-browse 'org))
8884
8885 (defun org-create-customize-menu ()
8886 "Create a full customization menu for Org-mode, insert it into the menu."
8887 (interactive)
8888 (if (fboundp 'customize-menu-create)
8889 (progn
8890 (easy-menu-change
8891 '("Org") "Customize"
8892 `(["Browse Org group" org-customize t]
8893 "--"
8894 ,(customize-menu-create 'org)
8895 ["Set" Custom-set t]
8896 ["Save" Custom-save t]
8897 ["Reset to Current" Custom-reset-current t]
8898 ["Reset to Saved" Custom-reset-saved t]
8899 ["Reset to Standard Settings" Custom-reset-standard t]))
8900 (message "\"Org\"-menu now contains full customization menu"))
8901 (error "Cannot expand menu (outdated version of cus-edit.el)")))
8902
8903 ;;; Miscellaneous stuff
8904
8905 (defun org-move-line-down (arg)
8906 "Move the current line down. With prefix argument, move it past ARG lines."
8907 (interactive "p")
8908 (let ((col (current-column))
8909 beg end pos)
8910 (beginning-of-line 1) (setq beg (point))
8911 (beginning-of-line 2) (setq end (point))
8912 (beginning-of-line (+ 1 arg))
8913 (setq pos (move-marker (make-marker) (point)))
8914 (insert (delete-and-extract-region beg end))
8915 (goto-char pos)
8916 (move-to-column col)))
8917
8918 (defun org-move-line-up (arg)
8919 "Move the current line up. With prefix argument, move it past ARG lines."
8920 (interactive "p")
8921 (let ((col (current-column))
8922 beg end pos)
8923 (beginning-of-line 1) (setq beg (point))
8924 (beginning-of-line 2) (setq end (point))
8925 (beginning-of-line (- arg))
8926 (setq pos (move-marker (make-marker) (point)))
8927 (insert (delete-and-extract-region beg end))
8928 (goto-char pos)
8929 (move-to-column col)))
8930
8931 ;; Functions needed for Emacs/XEmacs region compatibility
8932
8933 (defun org-region-active-p ()
8934 "Is `transient-mark-mode' on and the region active?
8935 Works on both Emacs and XEmacs."
8936 (if org-ignore-region
8937 nil
8938 (if org-xemacs-p
8939 (and zmacs-regions (region-active-p))
8940 (and transient-mark-mode mark-active))))
8941
8942 (defun org-add-to-invisibility-spec (arg)
8943 "Add elements to `buffer-invisibility-spec'.
8944 See documentation for `buffer-invisibility-spec' for the kind of elements
8945 that can be added."
8946 (cond
8947 ((fboundp 'add-to-invisibility-spec)
8948 (add-to-invisibility-spec arg))
8949 ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
8950 (setq buffer-invisibility-spec (list arg)))
8951 (t
8952 (setq buffer-invisibility-spec
8953 (cons arg buffer-invisibility-spec)))))
8954
8955 (defun org-remove-from-invisibility-spec (arg)
8956 "Remove elements from `buffer-invisibility-spec'."
8957 (if (fboundp 'remove-from-invisibility-spec)
8958 (remove-from-invisibility-spec arg)
8959 (if (consp buffer-invisibility-spec)
8960 (setq buffer-invisibility-spec
8961 (delete arg buffer-invisibility-spec)))))
8962
8963 (defun org-in-invisibility-spec-p (arg)
8964 "Is ARG a member of `buffer-invisibility-spec'?."
8965 (if (consp buffer-invisibility-spec)
8966 (member arg buffer-invisibility-spec)
8967 nil))
8968
8969 (defun org-image-file-name-regexp ()
8970 "Return regexp matching the file names of images."
8971 (if (fboundp 'image-file-name-regexp)
8972 (image-file-name-regexp)
8973 (let ((image-file-name-extensions
8974 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
8975 "xbm" "xpm" "pbm" "pgm" "ppm")))
8976 (concat "\\."
8977 (regexp-opt (nconc (mapcar 'upcase
8978 image-file-name-extensions)
8979 image-file-name-extensions)
8980 t)
8981 "\\'"))))
8982
8983 ;; Functions needed for compatibility with old outline.el
8984
8985 ;; The following functions capture almost the entire compatibility code
8986 ;; between the different versions of outline-mode. The only other place
8987 ;; where this is important are the font-lock-keywords. Search for
8988 ;; `org-noutline-p' to find it.
8989
8990 ;; C-a should go to the beginning of a *visible* line, also in the
8991 ;; new outline.el. I guess this should be patched into Emacs?
8992 (defun org-beginning-of-line ()
8993 "Go to the beginning of the current line. If that is invisible, continue
8994 to a visible line beginning. This makes the function of C-a more intuitive."
8995 (interactive)
8996 (beginning-of-line 1)
8997 (if (bobp)
8998 nil
8999 (backward-char 1)
9000 (if (org-invisible-p)
9001 (while (and (not (bobp)) (org-invisible-p))
9002 (backward-char 1)
9003 (beginning-of-line 1))
9004 (forward-char 1))))
9005 (when org-noutline-p
9006 (define-key org-mode-map "\C-a" 'org-beginning-of-line))
9007
9008 (defun org-invisible-p ()
9009 "Check if point is at a character currently not visible."
9010 (if org-noutline-p
9011 ;; Early versions of noutline don't have `outline-invisible-p'.
9012 (if (fboundp 'outline-invisible-p)
9013 (outline-invisible-p)
9014 (get-char-property (point) 'invisible))
9015 (save-excursion
9016 (skip-chars-backward "^\r\n")
9017 (equal (char-before) ?\r))))
9018
9019 (defun org-back-to-heading (&optional invisible-ok)
9020 "Move to previous heading line, or beg of this line if it's a heading.
9021 Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
9022 (if org-noutline-p
9023 (outline-back-to-heading invisible-ok)
9024 (if (looking-at outline-regexp)
9025 t
9026 (if (re-search-backward (concat (if invisible-ok "\\([\r\n]\\|^\\)" "^")
9027 outline-regexp)
9028 nil t)
9029 (if invisible-ok
9030 (progn (goto-char (match-end 1))
9031 (looking-at outline-regexp)))
9032 (error "Before first heading")))))
9033
9034 (defun org-on-heading-p (&optional invisible-ok)
9035 "Return t if point is on a (visible) heading line.
9036 If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
9037 (if org-noutline-p
9038 (outline-on-heading-p 'invisible-ok)
9039 (save-excursion
9040 (skip-chars-backward "^\n\r")
9041 (and (looking-at outline-regexp)
9042 (or invisible-ok
9043 (bobp)
9044 (equal (char-before) ?\n))))))
9045
9046 (defun org-up-heading-all (arg)
9047 "Move to the heading line of which the present line is a subheading.
9048 This function considers both visible and invisible heading lines.
9049 With argument, move up ARG levels."
9050 (if org-noutline-p
9051 (if (fboundp 'outline-up-heading-all)
9052 (outline-up-heading-all arg) ; emacs 21 version of outline.el
9053 (outline-up-heading arg t)) ; emacs 22 version of outline.el
9054 (org-back-to-heading t)
9055 (looking-at outline-regexp)
9056 (if (<= (- (match-end 0) (match-beginning 0)) arg)
9057 (error "Cannot move up %d levels" arg)
9058 (re-search-backward
9059 (concat "[\n\r]" (regexp-quote
9060 (make-string (- (match-end 0) (match-beginning 0) arg)
9061 ?*))
9062 "[^*]"))
9063 (forward-char 1))))
9064
9065 (defun org-show-hidden-entry ()
9066 "Show an entry where even the heading is hidden."
9067 (save-excursion
9068 (if (not org-noutline-p)
9069 (progn
9070 (org-back-to-heading t)
9071 (org-flag-heading nil)))
9072 (org-show-entry)))
9073
9074 (defun org-check-occur-regexp (regexp)
9075 "If REGEXP starts with \"^\", modify it to check for \\r as well.
9076 Of course, only for the old outline mode."
9077 (if org-noutline-p
9078 regexp
9079 (if (string-match "^\\^" regexp)
9080 (concat "[\n\r]" (substring regexp 1))
9081 regexp)))
9082
9083 (defun org-flag-heading (flag &optional entry)
9084 "Flag the current heading. FLAG non-nil means make invisible.
9085 When ENTRY is non-nil, show the entire entry."
9086 (save-excursion
9087 (org-back-to-heading t)
9088 (if (not org-noutline-p)
9089 ;; Make the current headline visible
9090 (outline-flag-region (max 1 (1- (point))) (point) (if flag ?\r ?\n)))
9091 ;; Check if we should show the entire entry
9092 (if entry
9093 (progn
9094 (org-show-entry)
9095 (save-excursion ;; FIXME: Is this the fix for points in the -|
9096 ;; middle of text? |
9097 (and (outline-next-heading) ;; |
9098 (org-flag-heading nil)))) ; show the next heading _|
9099 (outline-flag-region (max 1 (1- (point)))
9100 (save-excursion (outline-end-of-heading) (point))
9101 (if org-noutline-p
9102 flag
9103 (if flag ?\r ?\n))))))
9104
9105 (defun org-show-subtree ()
9106 "Show everything after this heading at deeper levels."
9107 (outline-flag-region
9108 (point)
9109 (save-excursion
9110 (outline-end-of-subtree) (outline-next-heading) (point))
9111 (if org-noutline-p nil ?\n)))
9112
9113 (defun org-show-entry ()
9114 "Show the body directly following this heading.
9115 Show the heading too, if it is currently invisible."
9116 (interactive)
9117 (save-excursion
9118 (org-back-to-heading t)
9119 (outline-flag-region
9120 (1- (point))
9121 (save-excursion
9122 (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
9123 (or (match-beginning 1) (point-max)))
9124 (if org-noutline-p nil ?\n))))
9125
9126
9127 (defun org-make-options-regexp (kwds)
9128 "Make a regular expression for keyword lines."
9129 (concat
9130 (if org-noutline-p "^" "[\n\r]")
9131 "#?[ \t]*\\+\\("
9132 (mapconcat 'regexp-quote kwds "\\|")
9133 "\\):[ \t]*"
9134 (if org-noutline-p "\\(.+\\)" "\\([^\n\r]+\\)")))
9135
9136 ;; Advise the bookmark-jump function to make jump position visible
9137 ;; Wrapped into eval-after-load to avoid loading advice unnecessarily
9138 (eval-after-load "bookmark"
9139 '(defadvice bookmark-jump (after org-make-visible activate)
9140 "Make the position visible."
9141 (and (eq major-mode 'org-mode)
9142 (org-invisible-p)
9143 (org-show-hierarchy-above))))
9144
9145 ;;; Finish up
9146
9147 (provide 'org)
9148
9149 (run-hooks 'org-load-hook)
9150
9151 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
9152 ;;; org.el ends here