1 ;;; org.el --- Outline-based notes management and organize
2 ;; Carstens outline-mode for keeping track of everything.
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <dominik at science dot uva dot nl>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://www.astro.uva.nl/~dominik/Tools/org/
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
31 ;; project planning with a fast and effective plain-text system.
33 ;; Org-mode develops organizational tasks around a NOTES file that contains
34 ;; information about projects as plain text. Org-mode is implemented on top
35 ;; of outline-mode - ideal to keep the content of large files well structured.
36 ;; It supports ToDo items, deadlines and time stamps, which can be extracted
37 ;; to create a daily/weekly agenda that also integrates the diary of the Emacs
38 ;; calendar. Tables are easily created with a built-in table editor. Plain
39 ;; text URL-like links connect to websites, emails (VM, RMAIL, WANDERLUST),
40 ;; Usenet messages (Gnus), BBDB entries, and any files related to the
41 ;; projects. For printing and sharing of notes, an Org-mode file (or a part
42 ;; of it) can be exported as a structured ASCII file, or as HTML.
46 ;; If Org-mode is part of the Emacs distribution or an XEmacs package, you
47 ;; only need to copy the following lines to your .emacs file. The last two
48 ;; lines define *global* keys for the commands `org-store-link' and
49 ;; `org-agenda' - please choose suitable keys yourself.
51 ;; (add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
52 ;; (define-key global-map "\C-cl" 'org-store-link)
53 ;; (define-key global-map "\C-ca" 'org-agenda)
55 ;; If you have downloaded Org-mode from the Web, you must byte-compile
56 ;; org.el and put it on your load path. In addition to the Emacs Lisp
57 ;; lines above, you also need to add the following lines to .emacs:
59 ;; (autoload 'org-mode "org" "Org mode" t)
60 ;; (autoload 'org-diary "org" "Diary entries from Org mode")
61 ;; (autoload 'org-agenda "org" "Multi-file agenda from Org mode" t)
62 ;; (autoload 'org-store-link "org" "Store a link to the current location" t)
63 ;; (autoload 'orgtbl-mode "org" "Org tables as a minor mode" t)
64 ;; (autoload 'turn-on-orgtbl "org" "Org tables as a minor mode")
66 ;; This setup will put all files with extension ".org" into Org-mode. As
67 ;; an alternative, make the first line of a file look like this:
69 ;; MY PROJECTS -*- mode: org; -*-
71 ;; which will select Org-mode for this buffer no matter what the file's
76 ;; The documentation of Org-mode can be found in the TeXInfo file. The
77 ;; distribution also contains a PDF version of it. At the homepage of
78 ;; Org-mode, you can read the same text online as HTML. There is also an
79 ;; excellent reference card made by Philip Rooke. This card can be found
80 ;; in the etc/ directory of Emacs 22.
82 ;; Changes since version 4.00:
83 ;; ---------------------------
86 ;; - In agenda buffer, mouse-1 no longer follows link.
87 ;; See `org-agenda-mouse-1-follows-link' and `org-mouse-1-follows-link'.
90 ;; - Links use now the [[link][description]] format by default.
91 ;; When inserting links, the user is prompted for a description.
92 ;; - If a link has a description, only the description is displayed
93 ;; the link part is hidden. Use C-c C-l to edit the link part.
94 ;; - TAGS are now bold, but in the same color as the headline.
95 ;; - The width of a table column can be limited by using a field "<N>".
96 ;; - New structure for the customization tree.
100 ;; - The list of agenda files can be maintainted in an external file.
104 ;; - Templates for remember buffer. Note that the remember setup changes.
105 ;; To set up templates, see `org-remember-templates'.
106 ;; - The time in new time stamps can be rounded, see new option
107 ;; `org-time-stamp-rounding-minutes'.
108 ;; - Bug fixes (there are *always* more bugs).
115 ;; - Small improvements to font-lock support.
116 ;; - MHE support finalized.
120 ;; - Improved MHE support
124 ;; - Leading stars in headlines can be hidden, so make the outline look
126 ;; - Mouse-1 can be used to follow links.
129 ;; - HTML exporter treats targeted internal links.
133 ;; - Changes to internal link system (thanks to David Wainberg for ideas).
134 ;; - in-file links: [[Search String]] instead of <file:::Search String>
135 ;; - automatic links to "radio targets".
136 ;; - CamelCase not longer active by default, configure org-activate-camels
137 ;; if you want to turn it back on.
138 ;; - After following a link, `C-c &' jumps back to it.
139 ;; - MH-E link support (thanks to Thomas Baumann).
140 ;; - Special table lines are no longer exported.
141 ;; - Bug fixes and minor improvements.
144 ;; - Cleanup tags display in agenda.
148 ;; - Table alignment fixed for use with wide characters.
149 ;; - `C-c -' leaves cursor in current table line.
150 ;; - The current TAG can be incorporated into the agenda prefix.
151 ;; See option `org-agenda-prefix-format' for details.
154 ;; - Minor bug fixes and improvements around tag searches.
155 ;; - XEmacs compatibility fixes.
158 ;; - Tags can also be set remotely from agenda buffer.
159 ;; - Boolean logic for tag searches.
160 ;; - Additional agenda commands can be configured through the variable
161 ;; `org-agenda-custom-commands'.
162 ;; - Minor bug fixes.
173 (defvar calc-embedded-close-formula
) ; defined by the calc package
174 (defvar calc-embedded-open-formula
) ; defined by the calc package
175 (defvar font-lock-unfontify-region-function
) ; defined by font-lock.el
177 ;;; Customization variables
179 (defvar org-version
"4.22"
180 "The version number of the file org.el.")
181 (defun org-version ()
183 (message "Org-mode version %s" org-version
))
185 ;; The following constant is for compatibility with different versions
187 (defconst org-noutline-p
(featurep 'noutline
)
188 "Are we using the new outline mode?")
189 (defconst org-xemacs-p
(featurep 'xemacs
))
190 (defconst org-format-transports-properties-p
192 (add-text-properties 0 1 '(test t
) x
)
193 (get-text-property 0 'test
(format "%s" x
)))
194 "Does format transport text properties?")
197 "Outline-based notes management and organizer."
203 (defgroup org-startup nil
204 "Options concerning startup of Org-mode."
208 (defcustom org-startup-folded t
209 "Non-nil means, entering Org-mode will switch to OVERVIEW.
210 This can also be configured on a per-file basis by adding one of
211 the following lines anywhere in the buffer:
218 (const :tag
"nofold: show all" nil
)
219 (const :tag
"fold: overview" t
)
220 (const :tag
"content: all headlines" content
)))
222 (defcustom org-startup-truncated t
223 "Non-nil means, entering Org-mode will set `truncate-lines'.
224 This is useful since some lines containing links can be very long and
225 uninteresting. Also tables look terrible when wrapped."
229 (defcustom org-startup-align-all-tables nil
230 "Non-nil means, align all tables when visiting a file.
231 This is useful when the column width in tables is forced with <N> cookies
232 in table fields. Such tables will look correct only after the first re-align."
236 (defcustom org-startup-with-deadline-check nil
237 "Non-nil means, entering Org-mode will run the deadline check.
238 This means, if you start editing an org file, you will get an
239 immediate reminder of any due deadlines.
240 This can also be configured on a per-file basis by adding one of
241 the following lines anywhere in the buffer:
244 #+STARTUP: nodlcheck"
248 (defcustom org-insert-mode-line-in-empty-file nil
249 "Non-nil means insert the first line setting Org-mode in empty files.
250 When the function `org-mode' is called interactively in an empty file, this
251 normally means that the file name does not automatically trigger Org-mode.
252 To ensure that the file will always be in Org-mode in the future, a
253 line enforcing Org-mode will be inserted into the buffer, if this option
258 (defcustom org-CUA-compatible nil
259 "Non-nil means use alternative key bindings for S-<cursor movement>.
260 Org-mode used S-<cursor movement> for changing timestamps and priorities.
261 S-<cursor movement> is also used for example by `CUA-mode' to select text.
262 If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
263 alternative bindings. Setting this variable to t will replace the following
264 keys both in Org-mode and in the Org-agenda buffer.
272 If you do not like the alternative keys, take a look at the variable
275 This option is only relevant at load-time of Org-mode. Changing it requires
276 a restart of Emacs to become effective."
280 (defvar org-disputed-keys
281 '((S-up [(shift up
)] [(meta ?p
)])
282 (S-down [(shift down
)] [(meta ?n
)])
283 (S-left [(shift left
)] [(meta ?-
)])
284 (S-right [(shift right
)] [(meta ?
+)])
285 (S-return [(shift return
)] [(control shift return
)]))
286 "Keys for which Org-mode and other modes compete.
287 This is an alist, cars are symbols for lookup, 1st element is the default key,
288 second element will be used when `org-CUA-compatible' is t.")
291 "Select a key according to `org-CUA-compatible'."
292 (nth (if org-CUA-compatible
2 1)
293 (or (assq key org-disputed-keys
)
294 (error "Invalid Key %s in `org-key'" key
))))
296 (defcustom org-ellipsis nil
297 "The ellipsis to use in the Org-mode outline.
298 When nil, just use the standard three dots. When a string, use that instead,
299 and just in Org-mode (which will then use its own display table).
300 Changing this requires executing `M-x org-mode' in a buffer to become
303 :type
'(choice (const :tag
"Default" nil
)
304 (string :tag
"String" :value
"...#")))
306 (defvar org-display-table nil
307 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
309 (defgroup org-keywords nil
310 "Keywords in Org-mode."
314 (defcustom org-deadline-string
"DEADLINE:"
315 "String to mark deadline entries.
316 A deadline is this string, followed by a time stamp. Should be a word,
317 terminated by a colon. You can insert a schedule keyword and
318 a timestamp with \\[org-deadline].
319 Changes become only effective after restarting Emacs."
323 (defcustom org-scheduled-string
"SCHEDULED:"
324 "String to mark scheduled TODO entries.
325 A schedule is this string, followed by a time stamp. Should be a word,
326 terminated by a colon. You can insert a schedule keyword and
327 a timestamp with \\[org-schedule].
328 Changes become only effective after restarting Emacs."
332 (defcustom org-closed-string
"CLOSED:"
333 "String used as the prefix for timestamps logging closing a TODO entry."
337 (defcustom org-comment-string
"COMMENT"
338 "Entries starting with this keyword will never be exported.
339 An entry can be toggled between COMMENT and normal with
340 \\[org-toggle-comment].
341 Changes become only effective after restarting Emacs."
345 (defcustom org-quote-string
"QUOTE"
346 "Entries starting with this keyword will be exported in fixed-width font.
347 Quoting applies only to the text in the entry following the headline, and does
348 not extend beyond the next headline, even if that is lower level.
349 An entry can be toggled between QUOTE and normal with
350 \\[org-toggle-fixed-width-section]."
354 (defgroup org-structure nil
355 "Options concerning the general structure of Org-mode files."
359 (defgroup org-cycle nil
360 "Options concerning visibility cycling in Org-mode."
362 :group
'org-structure
)
364 (defcustom org-cycle-emulate-tab t
365 "Where should `org-cycle' emulate TAB.
367 white Only in completely white lines
368 t Everywhere except in headlines"
370 :type
'(choice (const :tag
"Never" nil
)
371 (const :tag
"Only in completely white lines" white
)
372 (const :tag
"Everywhere except in headlines" t
)
375 (defcustom org-cycle-hook
'(org-optimize-window-after-visibility-change)
376 "Hook that is run after `org-cycle' has changed the buffer visibility.
377 The function(s) in this hook must accept a single argument which indicates
378 the new state that was set by the most recent `org-cycle' command. The
379 argument is a symbol. After a global state change, it can have the values
380 `overview', `content', or `all'. After a local state change, it can have
381 the values `folded', `children', or `subtree'."
385 (defgroup org-edit-structure nil
386 "Options concerning structure editing in Org-mode."
387 :tag
"Org Edit Structure"
388 :group
'org-structure
)
390 (defcustom org-odd-levels-only nil
391 "Non-nil means, skip even levels and only use odd levels for the outline.
392 This has the effect that two stars are being added/taken away in
393 promotion/demotion commands. It also influences how levels are
394 handled by the exporters.
395 Changing it requires restart of `font-lock-mode' to become effective
396 for fontification also in regions already fontified."
397 :group
'org-edit-structure
398 :group
'org-font-lock
401 (defcustom org-adapt-indentation t
402 "Non-nil means, adapt indentation when promoting and demoting.
403 When this is set and the *entire* text in an entry is indented, the
404 indentation is increased by one space in a demotion command, and
405 decreased by one in a promotion command. If any line in the entry
406 body starts at column 0, indentation is not changed at all."
407 :group
'org-edit-structure
410 (defcustom org-enable-fixed-width-editor t
411 "Non-nil means, lines starting with \":\" are treated as fixed-width.
412 This currently only means, they are never auto-wrapped.
413 When nil, such lines will be treated like ordinary lines.
414 See also the QUOTE keyword."
415 :group
'org-edit-structure
418 (defgroup org-sparse-trees nil
419 "Options concerning sparse trees in Org-mode."
420 :tag
"Org Sparse Trees"
421 :group
'org-structure
)
423 (defcustom org-highlight-sparse-tree-matches t
424 "Non-nil means, highlight all matches that define a sparse tree.
425 The highlights will automatically disappear the next time the buffer is
426 changed by an edit command."
427 :group
'org-sparse-trees
430 (defcustom org-show-hierarchy-above t
431 "Non-nil means, show full hierarchy when showing a spot in the tree.
432 Turning this off makes sparse trees more compact, but also less clear."
433 :group
'org-sparse-trees
436 (defcustom org-show-following-heading t
437 "Non-nil means, show heading following match in `org-occur'.
438 When doing an `org-occur' it is useful to show the headline which
439 follows the match, even if they do not match the regexp. This makes it
440 easier to edit directly inside the sparse tree. However, if you use
441 `org-occur' mainly as an overview, the following headlines are
442 unnecessary clutter."
443 :group
'org-sparse-trees
446 (defcustom org-occur-hook
'(org-first-headline-recenter)
447 "Hook that is run after `org-occur' has constructed a sparse tree.
448 This can be used to recenter the window to show as much of the structure
450 :group
'org-sparse-trees
453 (defgroup org-plain-lists nil
454 "Options concerning plain lists in Org-mode."
455 :tag
"Org Plain lists"
456 :group
'org-structure
)
458 (defcustom org-cycle-include-plain-lists nil
459 "Non-nil means, include plain lists into visibility cycling.
460 This means that during cycling, plain list items will *temporarily* be
461 interpreted as outline headlines with a level given by 1000+i where i is the
462 indentation of the bullet. In all other operations, plain list items are
463 not seen as headlines. For example, you cannot assign a TODO keyword to
465 :group
'org-plain-lists
469 (defcustom org-plain-list-ordered-item-terminator t
470 "The character that makes a line with leading number an ordered list item.
471 Valid values are ?. and ?\). To get both terminators, use t. While
472 ?. may look nicer, it creates the danger that a line with leading
473 number may be incorrectly interpreted as an item. ?\) therefore is
475 :group
'org-plain-lists
476 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
477 (const :tag
"paren like in \"2)\"" ?\
))
478 (const :tab
"both" t
)))
480 (defcustom org-auto-renumber-ordered-lists t
481 "Non-nil means, automatically renumber ordered plain lists.
482 Renumbering happens when the sequence have been changed with
483 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
484 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
485 :group
'org-plain-lists
488 (defgroup org-archive nil
489 "Options concerning archiving in Org-mode."
491 :group
'org-structure
)
493 (defcustom org-archive-location
"%s_archive::"
494 "The location where subtrees should be archived.
495 This string consists of two parts, separated by a double-colon.
497 The first part is a file name - when omitted, archiving happens in the same
498 file. %s will be replaced by the current file name (without directory part).
499 Archiving to a different file is useful to keep archived entries from
500 contributing to the Org-mode Agenda.
502 The part after the double colon is a headline. The archived entries will be
503 filed under that headline. When omitted, the subtrees are simply filed away
504 at the end of the file, as top-level entries.
506 Here are a few examples:
508 If the current file is Projects.org, archive in file
509 Projects.org_archive, as top-level trees. This is the default.
511 \"::* Archived Tasks\"
512 Archive in the current file, under the top-level headline
513 \"* Archived Tasks\".
515 \"~/org/archive.org::\"
516 Archive in file ~/org/archive.org (absolute path), as top-level trees.
518 \"basement::** Finished Tasks\"
519 Archive in file ./basement (relative path), as level 3 trees
520 below the level 2 heading \"** Finished Tasks\".
522 You may set this option on a per-file basis by adding to the buffer a
525 #+ARCHIVE: basement::** Finished Tasks"
529 (defcustom org-archive-mark-done t
530 "Non-nil means, mark archived entries as DONE."
534 (defcustom org-archive-stamp-time t
535 "Non-nil means, add a time stamp to archived entries.
536 The time stamp will be added directly after the TODO state keyword in the
537 first line, so it is probably best to use this in combinations with
538 `org-archive-mark-done'."
542 (defgroup org-table nil
543 "Options concerning tables in Org-mode."
547 (defcustom org-enable-table-editor
'optimized
548 "Non-nil means, lines starting with \"|\" are handled by the table editor.
549 When nil, such lines will be treated like ordinary lines.
551 When equal to the symbol `optimized', the table editor will be optimized to
553 - Use automatic overwrite mode in front of whitespace in table fields.
554 This make the structure of the table stay in tact as long as the edited
555 field does not exceed the column width.
556 - Minimize the number of realigns. Normally, the table is aligned each time
557 TAB or RET are pressed to move to another field. With optimization this
558 happens only if changes to a field might have changed the column width.
559 Optimization requires replacing the functions `self-insert-command',
560 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
561 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
562 very good at guessing when a re-align will be necessary, but you can always
563 force one with \\[org-ctrl-c-ctrl-c].
565 If you would like to use the optimized version in Org-mode, but the
566 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
568 This variable can be used to turn on and off the table editor during a session,
569 but in order to toggle optimization, a restart is required.
571 See also the variable `org-table-auto-blank-field'."
574 (const :tag
"off" nil
)
576 (const :tag
"on, optimized" optimized
)))
578 (defcustom orgtbl-optimized
(eq org-enable-table-editor
'optimized
)
579 "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
580 In the optimized version, the table editor takes over all simple keys that
581 normally just insert a character. In tables, the characters are inserted
582 in a way to minimize disturbing the table structure (i.e. in overwrite mode
583 for empty fields). Outside tables, the correct binding of the keys is
586 The default for this option is t if the optimized version is also used in
587 Org-mode. See the variable `org-enable-table-editor' for details. Changing
588 this variable requires a restart of Emacs to become effective."
592 (defgroup org-table-settings nil
593 "Settings for tables in Org-mode."
594 :tag
"Org Table Settings"
597 (defcustom org-table-default-size
"5x2"
598 "The default size for newly created tables, Columns x Rows."
599 :group
'org-table-settings
602 (defcustom org-table-number-regexp
"^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$"
603 "Regular expression for recognizing numbers in table columns.
604 If a table column contains mostly numbers, it will be aligned to the
605 right. If not, it will be aligned to the left.
607 The default value of this option is a regular expression which allows
608 anything which looks remotely like a number as used in scientific
609 context. For example, all of the following will be considered a
611 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
613 Other options offered by the customize interface are more restrictive."
614 :group
'org-table-settings
616 (const :tag
"Positive Integers"
618 (const :tag
"Integers"
620 (const :tag
"Floating Point Numbers"
621 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
622 (const :tag
"Floating Point Number or Integer"
623 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
624 (const :tag
"Exponential, Floating point, Integer"
625 "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
626 (const :tag
"Very General Number-Like"
627 "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$")
628 (string :tag
"Regexp:")))
630 (defcustom org-table-number-fraction
0.5
631 "Fraction of numbers in a column required to make the column align right.
632 In a column all non-white fields are considered. If at least this
633 fraction of fields is matched by `org-table-number-fraction',
634 alignment to the right border applies."
635 :group
'org-table-settings
638 (defgroup org-table-editing nil
639 "Bahavior of tables during editing in Org-mode."
640 :tag
"Org Table Editing"
643 (defcustom org-table-automatic-realign t
644 "Non-nil means, automatically re-align table when pressing TAB or RETURN.
645 When nil, aligning is only done with \\[org-table-align], or after column
647 :group
'org-table-editing
650 (defcustom org-table-limit-column-width t
;kw
651 "Non-nil means, allow to limit the width of table columns with <N> fields."
652 :group
'org-table-editing
655 (defcustom org-table-auto-blank-field t
656 "Non-nil means, automatically blank table field when starting to type into it.
657 This only happens when typing immediately after a field motion
658 command (TAB, S-TAB or RET).
659 Only relevant when `org-enable-table-editor' is equal to `optimized'."
660 :group
'org-table-editing
663 (defcustom org-table-tab-jumps-over-hlines t
664 "Non-nil means, tab in the last column of a table with jump over a hline.
665 If a horizontal separator line is following the current line,
666 `org-table-next-field' can either create a new row before that line, or jump
667 over the line. When this option is nil, a new line will be created before
669 :group
'org-table-editing
672 (defcustom org-table-tab-recognizes-table.el t
673 "Non-nil means, TAB will automatically notice a table.el table.
674 When it sees such a table, it moves point into it and - if necessary -
675 calls `table-recognize-table'."
676 :group
'org-table-editing
679 (defgroup org-table-calculation nil
680 "Options concerning tables in Org-mode."
681 :tag
"Org Table Calculation"
684 (defcustom org-table-copy-increment t
685 "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
686 :group
'org-table-calculation
689 (defcustom org-calc-default-modes
690 '(calc-internal-prec 12
691 calc-float-format
(float 5)
694 calc-symbolic-mode nil
695 calc-date-format
(YYYY "-" MM
"-" DD
" " Www
(" " HH
":" mm
))
696 calc-display-working-message t
698 "List with Calc mode settings for use in calc-eval for table formulas.
699 The list must contain alternating symbols (Calc modes variables and values).
700 Don't remove any of the default settings, just change the values. Org-mode
701 relies on the variables to be present in the list."
702 :group
'org-table-calculation
705 (defcustom org-table-formula-evaluate-inline t
706 "Non-nil means, TAB and RET evaluate a formula in current table field.
707 If the current field starts with an equal sign, it is assumed to be a formula
708 which should be evaluated as described in the manual and in the documentation
709 string of the command `org-table-eval-formula'. This feature requires the
711 When this variable is nil, formula calculation is only available through
712 the command \\[org-table-eval-formula]."
713 :group
'org-table-calculation
717 (defcustom org-table-formula-use-constants t
718 "Non-nil means, interpret constants in formulas in tables.
719 A constant looks like `$c' or `$Grav' and will be replaced before evaluation
720 by the value given in `org-table-formula-constants', or by a value obtained
721 from the `constants.el' package."
722 :group
'org-table-calculation
725 (defcustom org-table-formula-constants nil
726 "Alist with constant names and values, for use in table formulas.
727 The car of each element is a name of a constant, without the `$' before it.
728 The cdr is the value as a string. For example, if you'd like to use the
729 speed of light in a formula, you would configure
731 (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
733 and then use it in an equation like `$1*$c'."
734 :group
'org-table-calculation
736 (cons (string :tag
"name")
737 (string :tag
"value"))))
739 (defcustom org-table-formula-numbers-only nil
740 "Non-nil means, calculate only with numbers in table formulas.
741 Then all input fields will be converted to a number, and the result
742 must also be a number. When nil, calc's full potential is available
743 in table calculations, including symbolics etc."
744 :group
'org-table-calculation
747 (defcustom org-table-allow-automatic-line-recalculation t
748 "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
749 Automatically means, when TAB or RET or C-c C-c are pressed in the line."
750 :group
'org-table-calculation
753 (defgroup org-link nil
754 "Options concerning links in Org-mode."
758 (defcustom org-descriptive-links t
759 "Non-nil means, hide link part and only show description of bracket links.
760 Bracket links are like [[link][descritpion]]. This variable sets the initial
761 state in new org-mode buffers. The setting can then be toggled on a
762 per-buffer basis from the Org->Hyperlinks menu."
766 (defcustom org-link-style
'bracket
767 "The style of links to be inserted with \\[org-insert-link].
769 bracket [[link][description]]. This is recommended
770 plain Description \\n link. The old way, no longer recommended."
773 (const :tag
"Bracket (recommended)" bracket
)
774 (const :tag
"Plain (no longer recommended)" plain
)))
776 (defcustom org-link-format
"%s"
777 "Default format for external, URL-like linkes in the buffer.
778 This is a format string for printf, %s will be replaced by the link text.
779 The recommended value is just \"%s\", since links will be protected by
780 enclosing them in double brackets. If you prefer plain links (see variable
781 `org-link-style'), \"<%s>\" is useful. Some people also recommend an
782 additional URL: prefix, so the format would be \"<URL:%s>\"."
785 (const :tag
"\"%s\" (e.g. http://www.there.com)" "%s")
786 (const :tag
"\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
787 (const :tag
"\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
788 (string :tag
"Other" :value
"<%s>")))
790 (defcustom org-activate-links
'(bracket angle plain radio tag date
)
791 "Types of links that should be activated in Org-mode files.
792 This is a list of symbols, each leading to the activation of a certain link
793 type. In principle, it does not hurt to turn on most link types - there may
794 be a small gain when turning off unused link types. The types are:
796 bracket The recommended [[link][description]] or [[link]] links with hiding.
797 angular Links in angular brackes that may contain whitespace like
798 <bbdb:Carsten Dominik>.
799 plain Plain links in normal text, no whitespace, like http://google.com.
800 radio Text that is matched by a radio target, see manual for details.
801 tag Tag settings in a headline (link to tag search).
802 date Time stamps (link to calendar).
803 camel CamelCase words defining text searches.
805 Changing this variable requires a restart of Emacs to become effective."
807 :type
'(set (const :tag
"Double bracket links (new style)" bracket
)
808 (const :tag
"Angular bracket links (old style)" angular
)
809 (const :tag
"plain text links" plain
)
810 (const :tag
"Radio target matches" radio
)
811 (const :tag
"Tags" tag
)
812 (const :tag
"Timestamps" date
)
813 (const :tag
"CamelCase words" camel
)))
815 (defgroup org-link-store nil
816 "Options concerning storing links in Org-mode"
817 :tag
"Org Store Link"
820 (defcustom org-context-in-file-links t
821 "Non-nil means, file links from `org-store-link' contain context.
822 A search string will be added to the file name with :: as separator and
823 used to find the context when the link is activated by the command
825 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
826 negates this setting for the duration of the command."
827 :group
'org-link-store
830 (defcustom org-file-link-context-use-camel-case nil
831 "Non-nil means, use CamelCase to store a search context in a file link.
832 When nil, the search string simply consists of the words of the string.
833 CamelCase is deprecated, and support for it may be dropped in the future."
834 :group
'org-link-store
837 (defcustom org-keep-stored-link-after-insertion nil
838 "Non-nil means, keep link in list for entire session.
840 The command `org-store-link' adds a link pointing to the current
841 location to an internal list. These links accumulate during a session.
842 The command `org-insert-link' can be used to insert links into any
843 Org-mode file (offering completion for all stored links). When this
844 option is nil, every link which has been inserted once using \\[org-insert-link]
845 will be removed from the list, to make completing the unused links
847 :group
'org-link-store
850 (defcustom org-usenet-links-prefer-google nil
851 "Non-nil means, `org-store-link' will create web links to Google groups.
852 When nil, Gnus will be used for such links.
853 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
854 negates this setting for the duration of the command."
855 :group
'org-link-store
858 (defgroup org-link-follow nil
859 "Options concerning following links in Org-mode"
860 :tag
"Org Follow Link"
863 (defcustom org-tab-follows-link nil
864 "Non-nil means, on links TAB will follow the link.
865 Needs to be set before org.el is loaded."
866 :group
'org-link-follow
869 (defcustom org-return-follows-link nil
870 "Non-nil means, on links RET will follow the link.
871 Needs to be set before org.el is loaded."
872 :group
'org-link-follow
875 (defcustom org-mouse-1-follows-link t
876 "Non-nil means, mouse-1 on a link will follow the link.
877 A longer mouse click will still set point. Does not wortk on XEmacs.
878 Needs to be set before org.el is loaded."
879 :group
'org-link-follow
882 (defcustom org-mark-ring-length
4
883 "Number of different positions to be recorded in the ring
884 Changing this requires a restart of Emacs to work correctly."
885 :group
'org-link-follow
888 (defcustom org-link-frame-setup
889 '((vm . vm-visit-folder-other-frame
)
890 (gnus . gnus-other-frame
)
891 (file . find-file-other-window
))
892 "Setup the frame configuration for following links.
893 When following a link with Emacs, it may often be useful to display
894 this link in another window or frame. This variable can be used to
895 set this up for the different types of links.
898 `vm-visit-folder-other-frame'
904 `find-file-other-window'
905 `find-file-other-frame'
906 For the calendar, use the variable `calendar-setup'.
907 For BBDB, it is currently only possible to display the matches in
909 :group
'org-link-follow
913 (const vm-visit-folder
)
914 (const vm-visit-folder-other-window
)
915 (const vm-visit-folder-other-frame
)))
919 (const gnus-other-frame
)))
923 (const find-file-other-window
)
924 (const find-file-other-frame
)))))
926 (defcustom org-open-non-existing-files nil
927 "Non-nil means, `org-open-file' will open non-existing file.
928 When nil, an error will be generated."
929 :group
'org-link-follow
932 (defcustom org-confirm-shell-links
'yes-or-no-p
933 "Non-nil means, ask for confirmation before executing shell links.
934 Shell links can be dangerous, just thing about a link
936 [[shell:rm -rf ~/*][Google Search]]
938 This link would show up in your Org-mode document as \"Google Search\"
939 but really it would remove your entire home directory. Dangerous indeed.
940 Therefore I *definitely* advise agains setting this varaiable to nil.
941 Just change it to `y-or-n-p' of you want to confirm with a single key press
942 rather than having to type \"yes\"."
943 :group
'org-link-follow
945 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
946 (const :tag
"with y-or-n (faster)" y-or-n-p
)
947 (const :tag
"no confirmation (dangerous)" nil
)))
949 (defconst org-file-apps-defaults-gnu
951 "Default file applications on a UNIX or GNU/Linux system.
952 See `org-file-apps'.")
954 (defconst org-file-apps-defaults-macosx
962 "Default file applications on a MacOS X system.
963 The system \"open\" is known as a default, but we use X11 applications
964 for some files for which the OS does not have a good default.
965 See `org-file-apps'.")
967 (defconst org-file-apps-defaults-windowsnt
968 '((t .
(w32-shell-execute "open" file
)))
969 "Default file applications on a Windows NT system.
970 The system \"open\" is used for most files.
971 See `org-file-apps'.")
973 (defcustom org-file-apps
981 "External applications for opening `file:path' items in a document.
982 Org-mode uses system defaults for different file types, but
983 you can use this variable to set the application for a given file
984 extension. The entries in this list are cons cells with a file extension
985 and the corresponding command. Possible values for the command are:
986 `emacs' The file will be visited by the current Emacs process.
987 `default' Use the default application for this file type.
988 string A command to be executed by a shell; %s will be replaced
989 by the path to the file.
990 sexp A Lisp form which will be evaluated. The file path will
991 be available in the Lisp variable `file'.
992 For more examples, see the system specific constants
993 `org-file-apps-defaults-macosx'
994 `org-file-apps-defaults-windowsnt'
995 `org-file-apps-defaults-gnu'."
996 :group
'org-link-follow
998 (cons (choice :value
""
999 (string :tag
"Extension")
1000 (const :tag
"Default for unrecognized files" t
)
1001 (const :tag
"Links to a directory" directory
))
1003 (const :tag
"Visit with Emacs" emacs
)
1004 (const :tag
"Use system default" default
)
1005 (string :tag
"Command")
1006 (sexp :tag
"Lisp form")))))
1008 (defcustom org-mhe-search-all-folders nil
1009 "Non-nil means, that the search for the mh-message will be extended to
1010 all folders if the message cannot be found in the folder given in the link.
1011 Searching all folders is very effective with one of the search engines
1012 supported by MH-E, but will be slow with pick."
1013 :group
'org-link-follow
1016 (defgroup org-remember nil
1017 "Options concerning interaction with remember.el."
1021 (defcustom org-directory
"~/org"
1022 "Directory with org files.
1023 This directory will be used as default to prompt for org files.
1024 Used by the hooks for remember.el."
1025 :group
'org-remember
1028 (defcustom org-default-notes-file
"~/.notes"
1029 "Default target for storing notes.
1030 Used by the hooks for remember.el. This can be a string, or nil to mean
1031 the value of `remember-data-file'."
1032 :group
'org-remember
1034 (const :tag
"Default from remember-data-file" nil
)
1037 (defcustom org-remember-templates nil
1038 "Templates for the creation of remember buffers.
1039 When nil, just let remember make the buffer.
1040 When not nil, this is a list of 3-element lists. In each entry, the first
1041 element is a character, a unique key to select this template.
1042 The second element is the template. The third element is optional and can
1043 specify a destination file for remember items created with this template.
1044 The default file is given by `org-default-notes-file'.
1046 The template specifies the structure of the remember buffer. It should have
1047 a first line starting with a star, to act as the org-mode headline.
1048 Furthermore, the following %-escapes will be replaced with content:
1049 %t time stamp, date only
1050 %T time stamp with date and time
1051 %u inactive time stamp, date only
1052 %U inactive time stamp with date and time
1054 %a annotation, normally the link created with org-store-link
1055 %i initial content, the region when remember is called with C-u.
1056 If %i is indented, the entire inserted text will be indented as well.
1057 %? This will be removed, and the cursor placed at this position."
1058 :group
'org-remember
1059 :type
'(repeat :tag
"enabled"
1060 (list :value
(?a
"\n" nil
)
1061 (character :tag
"Selection Key")
1062 (string :tag
"Template")
1063 (file :tag
"Destination file (optional)"))))
1065 (defcustom org-reverse-note-order nil
1066 "Non-nil means, store new notes at the beginning of a file or entry.
1067 When nil, new notes will be filed to the end of a file or entry."
1068 :group
'org-remember
1070 (const :tag
"Reverse always" t
)
1071 (const :tag
"Reverse never" nil
)
1072 (repeat :tag
"By file name regexp"
1073 (cons regexp boolean
))))
1075 (defgroup org-todo nil
1076 "Options concerning TODO items in Org-mode."
1080 (defcustom org-todo-keywords
'("TODO" "DONE")
1081 "List of TODO entry keywords.
1082 \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
1083 considered to mean that the entry is \"done\". All the other mean that
1084 action is required, and will make the entry show up in todo lists, diaries
1086 The command \\[org-todo] cycles an entry through these states, and an
1087 additional state where no keyword is present. For details about this
1088 cycling, see also the variable `org-todo-interpretation'
1089 Changes become only effective after restarting Emacs."
1091 :group
'org-keywords
1092 :type
'(repeat (string :tag
"Keyword")))
1094 (defcustom org-todo-interpretation
'sequence
1095 "Controls how TODO keywords are interpreted.
1096 This variable is only relevant if `org-todo-keywords' contains more than two
1097 states. \\<org-mode-map>Possible values are `sequence' and `type'.
1099 When `sequence', \\[org-todo] will always switch to the next state in the
1100 `org-todo-keywords' list. When `type', \\[org-todo] only cycles from state
1101 to state when executed several times in direct succession. Otherwise, it
1102 switches directly to DONE from any state.
1103 See the manual for more information."
1105 :group
'org-keywords
1106 :type
'(choice (const sequence
)
1109 (defcustom org-after-todo-state-change-hook nil
1110 "Hook which is run after the state of a TODO item was changed.
1111 The new state (a string with a TODO keyword, or nil) is available in the
1112 Lisp variable `state'."
1116 (defcustom org-log-done nil
1117 "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
1118 When the state of an entry is changed from nothing to TODO, remove a previous
1123 (defgroup org-priorities nil
1124 "Keywords in Org-mode."
1125 :tag
"Org Priorities"
1128 (defcustom org-default-priority ?B
1129 "The default priority of TODO items.
1130 This is the priority an item get if no explicit priority is given."
1131 :group
'org-priorities
1134 (defcustom org-lowest-priority ?C
1135 "The lowest priority of TODO items. A character like ?A, ?B etc."
1136 :group
'org-priorities
1139 (defgroup org-time nil
1140 "Options concerning time stamps and deadlines in Org-mode."
1144 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
1145 "Formats for `format-time-string' which are used for time stamps.
1146 It is not recommended to change this constant.")
1148 (defcustom org-time-stamp-rounding-minutes
0
1149 "Number of minutes to round time stamps to upon insertion.
1150 When zero, insert the time unmodified. Useful rounding numbers
1151 should be factors of 60, so for example 5, 10, 15.
1152 When this is not zero, you can still force an exact time-stamp by using
1153 a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
1157 (defcustom org-deadline-warning-days
30
1158 "No. of days before expiration during which a deadline becomes active.
1159 This variable governs the display in the org file."
1163 (defcustom org-popup-calendar-for-date-prompt t
1164 "Non-nil means, pop up a calendar when prompting for a date.
1165 In the calendar, the date can be selected with mouse-1. However, the
1166 minibuffer will also be active, and you can simply enter the date as well.
1167 When nil, only the minibuffer will be available."
1171 (defcustom org-calendar-follow-timestamp-change t
1172 "Non-nil means, make the calendar window follow timestamp changes.
1173 When a timestamp is modified and the calendar window is visible, it will be
1174 moved to the new date."
1178 (defgroup org-tags nil
1179 "Options concerning startup of Org-mode."
1183 (defcustom org-tags-column
48
1184 "The column to which tags should be indented in a headline.
1185 If this number is positive, it specifies the column. If it is negative,
1186 it means that the tags should be flushright to that column. For example,
1187 -79 works well for a normal 80 character screen."
1191 (defcustom org-auto-align-tags t
1192 "Non-nil means, realign tags after pro/demotion of TODO state change.
1193 These operations change the length of a headline and therefore shift
1194 the tags around. With this options turned on, after each such operation
1195 the tags are again aligned to `org-tags-column'."
1199 (defcustom org-use-tag-inheritance t
1200 "Non-nil means, tags in levels apply also for sublevels.
1201 When nil, only the tags directly given in a specific line apply there.
1202 If you turn off this option, you very likely want to turn on the
1203 companion option `org-tags-match-list-sublevels'."
1207 (defcustom org-tags-match-list-sublevels nil
1208 "Non-nil means list also sublevels of headlines matching tag search.
1209 Because of tag inheritance (see variable `org-use-tag-inheritance'),
1210 the sublevels of a headline matching a tag search often also match
1211 the same search. Listing all of them can create very long lists.
1212 Setting this variable to nil causes subtrees of a match to be skipped.
1213 This option is off by default, because inheritance in on. If you turn
1214 inheritance off, you very likely want to turn this option on.
1216 As a special case, if the tag search is restricted to TODO items, the
1217 value of this variable is ignored and sublevels are always checked, to
1218 make sure all corresponding TODO items find their way into the list."
1222 (defvar org-tags-history nil
1223 "History of minibuffer reads for tags.")
1224 (defvar org-last-tags-completion-table nil
1225 "The last used completion table for tags.")
1227 (defgroup org-agenda nil
1228 "Options concerning agenda display Org-mode."
1232 (defvar org-category nil
1233 "Variable used by org files to set a category for agenda display.
1234 Such files should use a file variable to set it, for example
1236 -*- mode: org; org-category: \"ELisp\"
1238 or contain a special line
1242 If the file does not specify a category, then file's base name
1244 (make-variable-buffer-local 'org-category
)
1246 (defcustom org-agenda-files nil
1247 "The files to be used for agenda display.
1248 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
1249 \\[org-remove-file]. You can also use customize to edit the list.
1251 If the value of the variable is not a list but a single file name, then
1252 the list of agenda files is actually stored and maintained in that file, one
1253 agenda file per line."
1256 (repeat :tag
"List of files" file
)
1257 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
1259 (defcustom org-agenda-custom-commands
'(("w" todo
"WAITING"))
1260 "Custom commands for the agenda.
1261 These commands will be offered on the splash screen displayed by the
1262 agenda dispatcher \\[org-agenda]. Each entry is a list of 3 items:
1264 key The key (a single char as a string) to be associated with the command.
1265 type The command type, any of the following symbols:
1266 todo Entries with a specific TODO keyword, in all agenda files.
1267 tags Tags match in all agenda files.
1268 todo-tree Sparse tree of specific TODO keyword in *current* file.
1269 tags-tree Sparse tree with all tags matches in *current* file.
1270 occur-tree Occur sparse tree for current file.
1271 match What to search for:
1272 - a single keyword for TODO keyword searches
1273 - a tags match expression for tags searches
1274 - a regular expression for occur searches"
1277 (list (string :tag
"Key")
1279 (const :tag
"Tags search in all agenda files" tags
)
1280 (const :tag
"TODO keyword search in all agenda files" todo
)
1281 (const :tag
"Tags sparse tree in current buffer" tags-tree
)
1282 (const :tag
"TODO keyword tree in current buffer" todo-tree
)
1283 (const :tag
"Occur tree in current buffer" occur-tree
))
1284 (string :tag
"Match"))))
1286 (defcustom org-agenda-include-all-todo t
1287 "Non-nil means, the agenda will always contain all TODO entries.
1288 When nil, date-less entries will only be shown if `org-agenda' is called
1289 with a prefix argument.
1290 When non-nil, the TODO entries will be listed at the top of the agenda, before
1291 the entries for specific days."
1295 (defcustom org-agenda-include-diary nil
1296 "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
1300 (defcustom org-calendar-to-agenda-key
[?c
]
1301 "The key to be installed in `calendar-mode-map' for switching to the agenda.
1302 The command `org-calendar-goto-agenda' will be bound to this key. The
1303 default is the character `c' because then `c' can be used to switch back and
1304 forth between agenda and calendar."
1308 (defgroup org-agenda-window-setup nil
1309 "Options concerning setting up the Agenda window in Org Mode."
1310 :tag
"Org Agenda Window Setup"
1313 (defcustom org-agenda-mouse-1-follows-link nil
1314 "Non-nil means, mouse-1 on a link will follow the link in the agenda.
1315 A longer mouse click will still set point. Does not wortk on XEmacs.
1316 Needs to be set before org.el is loaded."
1317 :group
'org-agenda-setup
1320 (defcustom org-select-timeline-window t
1321 "Non-nil means, after creating a timeline, move cursor into Timeline window.
1322 When nil, cursor will remain in the current window."
1323 :group
'org-agenda-setup
1326 (defcustom org-select-agenda-window t
1327 "Non-nil means, after creating an agenda, move cursor into Agenda window.
1328 When nil, cursor will remain in the current window."
1329 :group
'org-agenda-setup
1332 (defcustom org-fit-agenda-window t
1333 "Non-nil means, change window size of agenda to fit content."
1334 :group
'org-agenda-setup
1337 (defgroup org-agenda-display nil
1338 "Options concerning what to display initially in Agenda."
1339 :tag
"Org Agenda Display"
1342 (defcustom org-agenda-show-all-dates t
1343 "Non-nil means, `org-agenda' shows every day in the selected range.
1344 When nil, only the days which actually have entries are shown."
1345 :group
'org-agenda-display
1348 (defcustom org-agenda-start-on-weekday
1
1349 "Non-nil means, start the overview always on the specified weekday.
1350 0 denotes Sunday, 1 denotes Monday etc.
1351 When nil, always start on the current day."
1352 :group
'org-agenda-display
1353 :type
'(choice (const :tag
"Today" nil
)
1354 (number :tag
"Weekday No.")))
1356 (defcustom org-agenda-ndays
7
1357 "Number of days to include in overview display.
1359 :group
'org-agenda-display
1362 (defcustom org-agenda-use-time-grid t
1363 "Non-nil means, show a time grid in the agenda schedule.
1364 A time grid is a set of lines for specific times (like every two hours between
1365 8:00 and 20:00). The items scheduled for a day at specific times are
1366 sorted in between these lines.
1367 For details about when the grid will be shown, and what it will look like, see
1368 the variable `org-agenda-time-grid'."
1369 :group
'org-agenda-display
1372 (defcustom org-agenda-time-grid
1373 '((daily today require-timed
)
1375 (800 1000 1200 1400 1600 1800 2000))
1377 "The settings for time grid for agenda display.
1378 This is a list of three items. The first item is again a list. It contains
1379 symbols specifying conditions when the grid should be displayed:
1381 daily if the agenda shows a single day
1382 weekly if the agenda shows an entire week
1383 today show grid on current date, independent of daily/weekly display
1384 require-timed show grid only if at least on item has a time specification
1386 The second item is a string which will be places behing the grid time.
1388 The third item is a list of integers, indicating the times that should have
1390 :group
'org-agenda-display
1393 (set :greedy t
:tag
"Grid Display Options"
1394 (const :tag
"Show grid in single day agenda display" daily
)
1395 (const :tag
"Show grid in weekly agenda display" weekly
)
1396 (const :tag
"Always show grid for today" today
)
1397 (const :tag
"Show grid only if any timed entries are present"
1399 (const :tag
"Skip grid times already present in an entry"
1401 (string :tag
"Grid String")
1402 (repeat :tag
"Grid Times" (integer :tag
"Time"))))
1404 (defcustom org-agenda-sorting-strategy
'(time-up category-keep priority-down
)
1405 "Sorting structure for the agenda items of a single day.
1406 This is a list of symbols which will be used in sequence to determine
1407 if an entry should be listed before another entry. The following
1408 symbols are recognized:
1410 time-up Put entries with time-of-day indications first, early first
1411 time-down Put entries with time-of-day indications first, late first
1412 category-keep Keep the default order of categories, corresponding to the
1413 sequence in `org-agenda-files'.
1414 category-up Sort alphabetically by category, A-Z.
1415 category-down Sort alphabetically by category, Z-A.
1416 priority-up Sort numerically by priority, high priority last.
1417 priority-down Sort numerically by priority, high priority first.
1419 The different possibilities will be tried in sequence, and testing stops
1420 if one comparison returns a \"not-equal\". For example, the default
1421 '(time-up category-keep priority-down)
1422 means: Pull out all entries having a specified time of day and sort them,
1423 in order to make a time schedule for the current day the first thing in the
1424 agenda listing for the day. Of the entries without a time indication, keep
1425 the grouped in categories, don't sort the categories, but keep them in
1426 the sequence given in `org-agenda-files'. Within each category sort by
1429 Leaving out `category-keep' would mean that items will be sorted across
1430 categories by priority."
1431 :group
'org-agenda-display
1436 (const category-keep
)
1438 (const category-down
)
1440 (const priority-down
))))
1442 (defcustom org-sort-agenda-notime-is-late t
1443 "Non-nil means, items without time are considered late.
1444 This is only relevant for sorting. When t, items which have no explicit
1445 time like 15:30 will be considered as 24:01, i.e. later than any items which
1446 do have a time. When nil, the default time is before 0:00. You can use this
1447 option to decide if the schedule for today should come before or after timeless
1449 :group
'org-agenda-display
1453 (defgroup org-agenda-prefix nil
1454 "Options concerning the entry prefix in the Org-mode agenda display."
1455 :tag
"Org Agenda Prefix"
1458 (defcustom org-agenda-prefix-format
" %-12:c%?-12t% s"
1459 "Format specification for the prefix of items in the agenda buffer.
1460 This format works similar to a printf format, with the following meaning:
1462 %c the category of the item, \"Diary\" for entries from the diary, or
1463 as given by the CATEGORY keyword or derived from the file name.
1464 %T the first tag of the item.
1465 %t the time-of-day specification if one applies to the entry, in the
1467 %s Scheduling/Deadline information, a short string
1469 All specifiers work basically like the standard `%s' of printf, but may
1470 contain two additional characters: A question mark just after the `%' and
1471 a whitespace/punctuation character just before the final letter.
1473 If the first character after `%' is a question mark, the entire field
1474 will only be included if the corresponding value applies to the
1475 current entry. This is useful for fields which should have fixed
1476 width when present, but zero width when absent. For example,
1477 \"%?-12t\" will result in a 12 character time field if a time of the
1478 day is specified, but will completely disappear in entries which do
1481 If there is punctuation or whitespace character just before the final
1482 format letter, this character will be appended to the field value if
1483 the value is not empty. For example, the format \"%-12:c\" leads to
1484 \"Diary: \" if the category is \"Diary\". If the category were be
1485 empty, no additional colon would be interted.
1487 The default value of this option is \" %-12:c%?-12t% s\", meaning:
1488 - Indent the line with two space characters
1489 - Give the category in a 12 chars wide field, padded with whitespace on
1490 the right (because of `-'). Append a colon if there is a category
1492 - If there is a time-of-day, put it into a 12 chars wide field. If no
1493 time, don't put in an empty field, just skip it (because of '?').
1494 - Finally, put the scheduling information and append a whitespace.
1496 As another example, if you don't want the time-of-day of entries in
1497 the prefix, you could use:
1499 (setq org-agenda-prefix-format \" %-11:c% s\")
1501 See also the variables `org-agenda-remove-times-when-in-prefix' and
1502 `org-agenda-remove-tags-when-in-prefix'."
1504 :group
'org-agenda-prefix
)
1506 (defcustom org-timeline-prefix-format
" % s"
1507 "Like `org-agenda-prefix-format', but for the timeline of a single file."
1509 :group
'org-agenda-prefix
)
1511 (defvar org-prefix-format-compiled nil
1512 "The compiled version of the most recently used prefix format.
1513 Depending on which command was used last, this may be the compiled version
1514 of `org-agenda-prefix-format' or `org-timeline-prefix-format'.")
1516 ;; FIXME: There seem to be situations where this does no work.
1517 (defcustom org-agenda-remove-times-when-in-prefix t
1518 "Non-nil means, remove duplicate time specifications in agenda items.
1519 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1520 time-of-day specification in a headline or diary entry is extracted and
1521 placed into the prefix. If this option is non-nil, the original specification
1522 \(a timestamp or -range, or just a plain time(range) specification like
1523 11:30-4pm) will be removed for agenda display. This makes the agenda less
1525 The option can be t or nil. It may also be the symbol `beg', indicating
1526 that the time should only be removed what it is located at the beginning of
1527 the headline/diary entry."
1528 :group
'org-agenda-prefix
1530 (const :tag
"Always" t
)
1531 (const :tag
"Never" nil
)
1532 (const :tag
"When at beginning of entry" beg
)))
1534 (defcustom org-agenda-remove-tags-when-in-prefix nil
1535 "Non-nil means, remove the tags from the headline copy in the agenda.
1536 When this is the symbol `prefix', only remove tags when
1537 `org-agenda-prefix-format' contains a `%T' specifier."
1538 :group
'org-agenda-prefix
1540 (const :tag
"Always" t
)
1541 (const :tag
"Never" nil
)
1542 (const :tag
"When prefix format contains %T" prefix
)))
1544 (defgroup org-export nil
1545 "Options for exporting org-listings."
1549 (defgroup org-export-general nil
1550 "General options for exporting Org-mode files."
1551 :tag
"Org Export General"
1554 (defcustom org-export-language-setup
1555 '(("en" "Author" "Date" "Table of Contents")
1556 ("da" "Ophavsmand" "Dato" "Indhold")
1557 ("de" "Autor" "Datum" "Inhaltsverzeichnis")
1558 ("es" "Autor" "Fecha" "\xccndice")
1559 ("fr" "Auteur" "Date" "Table des Mati\xe8res")
1560 ("it" "Autore" "Data" "Indice")
1561 ("nl" "Auteur" "Datum" "Inhoudsopgave")
1562 ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
1563 ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
1564 "Terms used in export text, translated to different languages.
1565 Use the variable `org-export-default-language' to set the language,
1566 or use the +OPTION lines for a per-file setting."
1567 :group
'org-export-general
1570 (string :tag
"HTML language tag")
1571 (string :tag
"Author")
1572 (string :tag
"Date")
1573 (string :tag
"Table of Contents"))))
1575 (defcustom org-export-default-language
"en"
1576 "The default language of HTML export, as a string.
1577 This should have an association in `org-export-language-setup'."
1578 :group
'org-export-general
1581 (defcustom org-export-headline-levels
3
1582 "The last level which is still exported as a headline.
1583 Inferior levels will produce itemize lists when exported.
1584 Note that a numeric prefix argument to an exporter function overrides
1587 This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
1588 :group
'org-export-general
1591 (defcustom org-export-with-section-numbers t
1592 "Non-nil means, add section numbers to headlines when exporting.
1594 This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
1595 :group
'org-export-general
1598 (defcustom org-export-with-toc t
1599 "Non-nil means, create a table of contents in exported files.
1600 The TOC contains headlines with levels up to`org-export-headline-levels'.
1602 Headlines which contain any TODO items will be marked with \"(*)\" in
1603 ASCII export, and with red color in HTML output.
1605 In HTML output, the TOC will be clickable.
1607 This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
1608 :group
'org-export-general
1611 (defcustom org-export-mark-todo-in-toc nil
1612 "Non-nil means, mark TOC lines that contain any open TODO items."
1613 :group
'org-export-general
1616 (defcustom org-export-preserve-breaks nil
1617 "Non-nil means, preserve all line breaks when exporting.
1618 Normally, in HTML output paragraphs will be reformatted. In ASCII
1619 export, line breaks will always be preserved, regardless of this variable.
1621 This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
1622 :group
'org-export-general
1625 (defgroup org-export-translation nil
1626 "Options for translating special ascii sequences for the export backends."
1627 :tag
"Org Export Translation"
1630 (defcustom org-export-with-emphasize t
1631 "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
1632 If the export target supports emphasizing text, the word will be
1633 typeset in bold, italic, or underlined, respectively. Works only for
1634 single words, but you can say: I *really* *mean* *this*.
1635 Not all export backends support this.
1637 This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
1638 :group
'org-export-translation
1641 (defcustom org-export-with-sub-superscripts t
1642 "Non-nil means, interpret \"_\" and \"^\" for export.
1643 When this option is turned on, you can use TeX-like syntax for sub- and
1644 superscripts. Several characters after \"_\" or \"^\" will be
1645 considered as a single item - so grouping with {} is normally not
1646 needed. For example, the following things will be parsed as single
1647 sub- or superscripts.
1649 10^24 or 10^tau several digits will be considered 1 item.
1650 10^-12 or 10^-tau a leading sign with digits or a word
1651 x^2-y^3 will be read as x^2 - y^3, because items are
1652 terminated by almost any nonword/nondigit char.
1653 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
1655 Still, ambiguity is possible - so when in doubt use {} to enclose the
1657 Not all export backends support this, but HTML does.
1659 This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
1660 :group
'org-export-translation
1663 (defcustom org-export-with-TeX-macros t
1664 "Non-nil means, interpret simple TeX-like macros when exporting.
1665 For example, HTML export converts \\alpha to α and \\AA to Å.
1666 No only real TeX macros will work here, but the standard HTML entities
1667 for math can be used as macro names as well. For a list of supported
1668 names in HTML export, see the constant `org-html-entities'.
1669 Not all export backends support this.
1671 This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
1672 :group
'org-export-translation
1675 (defcustom org-export-with-fixed-width t
1676 "Non-nil means, lines starting with \":\" will be in fixed width font.
1677 This can be used to have pre-formatted text, fragments of code etc. For
1679 : ;; Some Lisp examples
1682 will be looking just like this in also HTML. See also the QUOTE keyword.
1683 Not all export backends support this.
1685 This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
1686 :group
'org-export-translation
1689 (defcustom org-match-sexp-depth
3
1690 "Number of stacked braces for sub/superscript matching.
1691 This has to be set before loading org.el to be effective."
1692 :group
'org-export-translation
1695 (defgroup org-export-tables nil
1696 "Options for exporting tables in Org-mode."
1697 :tag
"Org Export Tables"
1700 (defcustom org-export-with-tables t
1701 "If non-nil, lines starting with \"|\" define a table.
1704 | Name | Address | Birthday |
1705 |-------------+----------+-----------|
1706 | Arthur Dent | England | 29.2.2100 |
1708 Not all export backends support this.
1710 This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
1711 :group
'org-export-tables
1714 (defcustom org-export-highlight-first-table-line t
1715 "Non-nil means, highlight the first table line.
1716 In HTML export, this means use <th> instead of <td>.
1717 In tables created with table.el, this applies to the first table line.
1718 In Org-mode tables, all lines before the first horizontal separator
1719 line will be formatted with <th> tags."
1720 :group
'org-export-tables
1723 (defcustom org-export-table-remove-special-lines t
1724 "Remove special lines and marking characters in calculating tables.
1725 This removes the special marking character column from tables that are set
1726 up for spreadsheet calculations. It also removes the entire lines
1727 marked with `!', `_', or `^'. The lines with `$' are kept, because
1728 the values of constants may be useful to have."
1729 :group
'org-export-tables
1732 (defcustom org-export-prefer-native-exporter-for-tables nil
1733 "Non-nil means, always export tables created with table.el natively.
1734 Natively means, use the HTML code generator in table.el.
1735 When nil, Org-mode's own HTML generator is used when possible (i.e. if
1736 the table does not use row- or column-spanning). This has the
1737 advantage, that the automatic HTML conversions for math symbols and
1738 sub/superscripts can be applied. Org-mode's HTML generator is also
1740 :group
'org-export-tables
1743 (defgroup org-export-ascii nil
1744 "Options specific for ASCII export of Org-mode files."
1745 :tag
"Org Export ASCII"
1748 (defcustom org-export-ascii-show-new-buffer t
1749 "Non-nil means, popup buffer containing the exported ASCII text.
1750 Otherwise the buffer will just be saved to a file and stay hidden."
1751 :group
'org-export-ascii
1754 (defgroup org-export-xml nil
1755 "Options specific for XML export of Org-mode files."
1756 :tag
"Org Export XML"
1759 (defcustom org-export-xml-type
'xoxo
;kw, if we have only one.
1760 "The kind of XML to be produced by the XML exporter.
1762 xoxo The XOXO exporter."
1763 :group
'org-export-xml
1765 (const :tag
"XOXO" xoxo
)))
1767 (defgroup org-export-html nil
1768 "Options specific for HTML export of Org-mode files."
1769 :tag
"Org Export HTML"
1772 (defcustom org-export-html-style
1773 "<style type=\"text/css\">
1775 font-family: Times, serif;
1778 .title { text-align: center; }
1779 .todo, .deadline { color: red; }
1780 .done { color: green; }
1781 .target { background-color: lavender; }
1783 border: 1pt solid #AEBDCC;
1784 background-color: #F3F5F7;
1786 font-family: courier, monospace;
1788 table { border-collapse: collapse; }
1790 vertical-align: top;
1791 border: 1pt solid #ADB9CC;
1794 "The default style specification for exported HTML files.
1795 Since there are different ways of setting style information, this variable
1796 needs to contain the full HTML structure to provide a style, including the
1797 surrounding HTML tags. The style specifications should include definitions
1798 for new classes todo, done, title, and deadline. For example, legal values
1801 <style type=\"text/css\">
1802 p { font-weight: normal; color: gray; }
1803 h1 { color: black; }
1804 .title { text-align: center; }
1805 .todo, .deadline { color: red; }
1806 .done { color: green; }
1809 or, if you want to keep the style in a file,
1811 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
1813 As the value of this option simply gets inserted into the HTML <head> header,
1814 you can \"misuse\" it to add arbitrary text to the header."
1815 :group
'org-export-html
1818 (defcustom org-export-html-inline-images t
1819 "Non-nil means, inline images into exported HTML pages.
1820 The link will still be to the original location of the image file.
1821 So if you are moving the page, lets say to your public HTML site,
1822 you will have to move the image and maybe change the link."
1823 :group
'org-export-html
1826 (defcustom org-export-html-expand t
1827 "Non-nil means, for HTML export, treat @<...> as HTML tag.
1828 When nil, these tags will be exported as plain text and therefore
1829 not be interpreted by a browser.
1831 This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
1832 :group
'org-export-html
1835 (defcustom org-export-html-table-tag
1836 "<table border=1 cellspacing=0 cellpadding=6>"
1837 "The HTML tag used to start a table.
1838 This must be a <table> tag, but you may change the options like
1839 borders and spacing."
1840 :group
'org-export-html
1843 (defcustom org-export-html-with-timestamp nil
1844 "If non-nil, write `org-export-html-html-helper-timestamp'
1845 into the exported HTML text. Otherwise, the buffer will just be saved
1847 :group
'org-export-html
1850 (defcustom org-export-html-html-helper-timestamp
1851 "<br><br><hr><p><!-- hhmts start --> <!-- hhmts end -->\n"
1852 "The HTML tag used as timestamp delimiter for HTML-helper-mode."
1853 :group
'org-export-html
1856 (defcustom org-export-html-show-new-buffer nil
1857 "Non-nil means, popup buffer containing the exported html text.
1858 Otherwise, the buffer will just be saved to a file and stay hidden."
1859 :group
'org-export-html
1862 (defgroup org-export-icalendar nil
1863 "Options specific for iCalendar export of Org-mode files."
1864 :tag
"Org Export iCalendar"
1867 (defcustom org-combined-agenda-icalendar-file
"~/org.ics"
1868 "The file name for the iCalendar file covering all agenda files.
1869 This file is created with the command \\[org-export-icalendar-all-agenda-files]."
1870 :group
'org-export-icalendar
1873 (defcustom org-icalendar-include-todo nil
1874 "Non-nil means, export to iCalendar files should also cover TODO items."
1875 :group
'org-export-icalendar
1878 (defcustom org-icalendar-combined-name
"OrgMode"
1879 "Calendar name for the combined iCalendar representing all agenda files."
1880 :group
'org-export-icalendar
1883 (defgroup org-font-lock nil
1884 "Font-lock settings for highlighting in Org-mode."
1885 :tag
"Org Font Lock"
1888 (defcustom org-level-color-stars-only nil
1889 "Non-nil means fontify only the stars in each headline.
1890 When nil, the entire headline is fontified.
1891 Changing it requires restart of `font-lock-mode' to become effective
1892 also in regions already fontified."
1893 :group
'org-font-lock
1896 (defcustom org-hide-leading-stars nil
1897 "Non-nil means, hide the first N-1 stars in a headline.
1898 This works by using the face `org-hide' for these stars. This
1899 face is white for a light background, and black for a dark
1900 background. You may have to customize the face `org-hide' to
1902 Changing it requires restart of `font-lock-mode' to become effective
1903 also in regions already fontified."
1904 :group
'org-font-lock
1907 (defcustom org-fontify-done-headline nil
1908 "Non-nil means, change the face of a headline if it is marked DONE.
1909 Normally, only the TODO/DONE keyword indicates the state of a headline.
1910 When this is non-nil, the headline after the keyword is set to the
1911 `org-headline-done' as an additional indication."
1912 :group
'org-font-lock
1915 (defcustom org-fontify-emphasized-text t
1916 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
1917 Changing this variable requires a restart of Emacs to take effect."
1918 :group
'org-font-lock
1921 (defgroup org-faces nil
1922 "Faces in Org-mode."
1924 :group
'org-font-lock
)
1928 (((type tty
) (class color
)) (:foreground
"white"))
1929 (((class color
) (background light
)) (:foreground
"white"))
1930 (((class color
) (background dark
)) (:foreground
"black"))
1931 (t (:inverse-video nil
)))
1932 "Face used for level 1 headlines."
1935 (defface org-level-1
;; font-lock-function-name-face
1936 '((((type tty
) (class color
)) (:foreground
"blue" :weight bold
))
1937 (((class color
) (background light
)) (:foreground
"Blue"))
1938 (((class color
) (background dark
)) (:foreground
"LightSkyBlue"))
1939 (t (:inverse-video t
:bold t
)))
1940 "Face used for level 1 headlines."
1943 (defface org-level-2
;; font-lock-variable-name-face
1944 '((((type tty
) (class color
)) (:foreground
"yellow" :weight light
))
1945 (((class color
) (background light
)) (:foreground
"DarkGoldenrod"))
1946 (((class color
) (background dark
)) (:foreground
"LightGoldenrod"))
1947 (t (:bold t
:italic t
)))
1948 "Face used for level 2 headlines."
1951 (defface org-level-3
;; font-lock-keyword-face
1952 '((((type tty
) (class color
)) (:foreground
"cyan" :weight bold
))
1953 (((class color
) (background light
)) (:foreground
"Purple"))
1954 (((class color
) (background dark
)) (:foreground
"Cyan"))
1956 "Face used for level 3 headlines."
1959 (defface org-level-4
;; font-lock-comment-face
1960 '((((type tty pc
) (class color
) (background light
)) (:foreground
"red"))
1961 (((type tty pc
) (class color
) (background dark
)) (:foreground
"red1"))
1962 (((class color
) (background light
)) (:foreground
"Firebrick"))
1963 (((class color
) (background dark
)) (:foreground
"chocolate1"))
1964 (t (:bold t
:italic t
)))
1965 "Face used for level 4 headlines."
1968 (defface org-level-5
;; font-lock-type-face
1969 '((((type tty
) (class color
)) (:foreground
"green"))
1970 (((class color
) (background light
)) (:foreground
"ForestGreen"))
1971 (((class color
) (background dark
)) (:foreground
"PaleGreen"))
1972 (t (:bold t
:underline t
)))
1973 "Face used for level 5 headlines."
1976 (defface org-level-6
;; font-lock-constant-face
1977 '((((type tty
) (class color
)) (:foreground
"magenta"))
1978 (((class color
) (background light
)) (:foreground
"CadetBlue"))
1979 (((class color
) (background dark
)) (:foreground
"Aquamarine"))
1980 (t (:bold t
:underline t
)))
1981 "Face used for level 6 headlines."
1984 (defface org-level-7
;; font-lock-builtin-face
1985 '((((type tty
) (class color
)) (:foreground
"blue" :weight light
))
1986 (((class color
) (background light
)) (:foreground
"Orchid"))
1987 (((class color
) (background dark
)) (:foreground
"LightSteelBlue"))
1989 "Face used for level 7 headlines."
1992 (defface org-level-8
;; font-lock-string-face
1993 '((((type tty
) (class color
)) (:foreground
"green"))
1994 (((class color
) (background light
)) (:foreground
"RosyBrown"))
1995 (((class color
) (background dark
)) (:foreground
"LightSalmon"))
1997 "Face used for level 8 headlines."
2000 (defface org-special-keyword
;; font-lock-string-face
2001 '((((type tty
) (class color
)) (:foreground
"green"))
2002 (((class color
) (background light
)) (:foreground
"RosyBrown"))
2003 (((class color
) (background dark
)) (:foreground
"LightSalmon"))
2005 "Face used for special keywords."
2008 (defface org-warning
;; font-lock-warning-face
2009 '((((type tty
) (class color
)) (:foreground
"red"))
2010 (((class color
) (background light
)) (:foreground
"Red" :bold t
))
2011 (((class color
) (background dark
)) (:foreground
"Red1" :bold t
))
2012 ; (((class color) (background dark)) (:foreground "Pink" :bold t))
2013 (t (:inverse-video t
:bold t
)))
2014 "Face for deadlines and TODO keywords."
2017 (defface org-headline-done
;; font-lock-string-face
2018 '((((type tty
) (class color
)) (:foreground
"green"))
2019 (((class color
) (background light
)) (:foreground
"RosyBrown"))
2020 (((class color
) (background dark
)) (:foreground
"LightSalmon"))
2022 "Face used to indicate that a headline is DONE. See also the variable
2023 `org-fontify-done-headline'."
2026 ;; Inheritance does not work for xemacs. So we just copy...
2028 (defface org-deadline-announce
2029 '((((type tty
) (class color
)) (:foreground
"blue" :weight bold
))
2030 (((class color
) (background light
)) (:foreground
"Blue"))
2031 (((class color
) (background dark
)) (:foreground
"LightSkyBlue"))
2032 (t (:inverse-video t
:bold t
)))
2033 "Face for upcoming deadlines."
2036 (defface org-scheduled-today
2037 '((((type tty
) (class color
)) (:foreground
"green"))
2038 (((class color
) (background light
)) (:foreground
"DarkGreen"))
2039 (((class color
) (background dark
)) (:foreground
"PaleGreen"))
2040 (t (:bold t
:underline t
)))
2041 "Face for items scheduled for a certain day."
2044 (defface org-scheduled-previously
2045 '((((type tty pc
) (class color
) (background light
)) (:foreground
"red"))
2046 (((type tty pc
) (class color
) (background dark
)) (:foreground
"red1"))
2047 (((class color
) (background light
)) (:foreground
"Firebrick"))
2048 (((class color
) (background dark
)) (:foreground
"chocolate1"))
2049 (t (:bold t
:italic t
)))
2050 "Face for items scheduled previously, and not yet done."
2053 (defface org-formula
2054 '((((type tty pc
) (class color
) (background light
)) (:foreground
"red"))
2055 (((type tty pc
) (class color
) (background dark
)) (:foreground
"red1"))
2056 (((class color
) (background light
)) (:foreground
"Firebrick"))
2057 (((class color
) (background dark
)) (:foreground
"chocolate1"))
2058 (t (:bold t
:italic t
)))
2059 "Face for formulas."
2063 '((((type tty
) (class color
)) (:foreground
"cyan" :weight bold
))
2064 (((class color
) (background light
)) (:foreground
"Purple" :underline t
))
2065 (((class color
) (background dark
)) (:foreground
"Cyan" :underline t
))
2071 '((((type tty
) (class color
)) (:weight bold
))
2072 (((class color
) (background light
)) (:weight bold
))
2073 (((class color
) (background dark
)) (:weight bold
))
2078 (defface org-done
;; font-lock-type-face
2079 '((((type tty
) (class color
)) (:foreground
"green"))
2080 (((class color
) (background light
)) (:foreground
"ForestGreen" :bold t
))
2081 (((class color
) (background dark
)) (:foreground
"PaleGreen" :bold t
))
2082 (t (:bold t
:underline t
)))
2083 "Face used for DONE."
2086 (defface org-table
;; font-lock-function-name-face
2087 '((((type tty
) (class color
)) (:foreground
"blue" :weight bold
))
2088 (((class color
) (background light
)) (:foreground
"Blue"))
2089 (((class color
) (background dark
)) (:foreground
"LightSkyBlue"))
2090 (t (:inverse-video t
:bold t
)))
2091 "Face used for tables."
2094 (defface org-time-grid
;; font-lock-variable-name-face
2095 '((((type tty
) (class color
)) (:foreground
"yellow" :weight light
))
2096 (((class color
) (background light
)) (:foreground
"DarkGoldenrod"))
2097 (((class color
) (background dark
)) (:foreground
"LightGoldenrod"))
2098 (t (:bold t
:italic t
)))
2099 "Face used for time grids."
2102 (defvar org-level-faces
2103 '(org-level-1 org-level-2 org-level-3 org-level-4
2104 org-level-5 org-level-6 org-level-7 org-level-8
2106 (defvar org-n-levels
(length org-level-faces
))
2109 ;; Variables for pre-computed regular expressions, all buffer local
2110 (defvar org-done-string nil
2111 "The last string in `org-todo-keywords', indicating an item is DONE.")
2112 (make-variable-buffer-local 'org-done-string
)
2113 (defvar org-todo-regexp nil
2114 "Matches any of the TODO state keywords.")
2115 (make-variable-buffer-local 'org-todo-regexp
)
2116 (defvar org-not-done-regexp nil
2117 "Matches any of the TODO state keywords except the last one.")
2118 (make-variable-buffer-local 'org-not-done-regexp
)
2119 (defvar org-todo-line-regexp nil
2120 "Matches a headline and puts TODO state into group 2 if present.")
2121 (make-variable-buffer-local 'org-todo-line-regexp
)
2122 (defvar org-nl-done-regexp nil
2123 "Matches newline followed by a headline with the DONE keyword.")
2124 (make-variable-buffer-local 'org-nl-done-regexp
)
2125 (defvar org-looking-at-done-regexp nil
2126 "Matches the DONE keyword a point.")
2127 (make-variable-buffer-local 'org-looking-at-done-regexp
)
2128 (defvar org-todo-kwd-priority-p nil
2129 "Do TODO items have priorities?")
2130 (make-variable-buffer-local 'org-todo-kwd-priority-p
)
2131 (defvar org-todo-kwd-max-priority nil
2132 "Maximum priority of TODO items.")
2133 (make-variable-buffer-local 'org-todo-kwd-max-priority
)
2134 (defvar org-ds-keyword-length
12
2135 "Maximum length of the Deadline and SCHEDULED keywords.")
2136 (make-variable-buffer-local 'org-ds-keyword-length
)
2137 (defvar org-deadline-regexp nil
2138 "Matches the DEADLINE keyword.")
2139 (make-variable-buffer-local 'org-deadline-regexp
)
2140 (defvar org-deadline-time-regexp nil
2141 "Matches the DEADLINE keyword together with a time stamp.")
2142 (make-variable-buffer-local 'org-deadline-time-regexp
)
2143 (defvar org-deadline-line-regexp nil
2144 "Matches the DEADLINE keyword and the rest of the line.")
2145 (make-variable-buffer-local 'org-deadline-line-regexp
)
2146 (defvar org-scheduled-regexp nil
2147 "Matches the SCHEDULED keyword.")
2148 (make-variable-buffer-local 'org-scheduled-regexp
)
2149 (defvar org-scheduled-time-regexp nil
2150 "Matches the SCHEDULED keyword together with a time stamp.")
2151 (make-variable-buffer-local 'org-scheduled-time-regexp
)
2153 (defun org-set-regexps-and-options ()
2154 "Precompute regular expressions for current buffer."
2155 (when (eq major-mode
'org-mode
)
2156 (let ((re (org-make-options-regexp
2157 '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
2158 "STARTUP" "ARCHIVE")))
2160 kwds int key value cat arch
)
2164 (goto-char (point-min))
2165 (while (re-search-forward re nil t
)
2166 (setq key
(match-string 1) value
(match-string 2))
2168 ((equal key
"CATEGORY")
2169 (if (string-match "[ \t]+$" value
)
2170 (setq value
(replace-match "" t t value
)))
2171 (setq cat
(intern value
)))
2172 ((equal key
"SEQ_TODO")
2174 kwds
(append kwds
(org-split-string value splitre
))))
2175 ((equal key
"PRI_TODO")
2177 kwds
(append kwds
(org-split-string value splitre
))))
2178 ((equal key
"TYP_TODO")
2180 kwds
(append kwds
(org-split-string value splitre
))))
2181 ((equal key
"STARTUP")
2182 (let ((opts (org-split-string value splitre
))
2183 (set '(("fold" org-startup-folded t
)
2184 ("nofold" org-startup-folded nil
)
2185 ("content" org-startup-folded content
)
2186 ("hidestars" org-hide-leading-stars t
)
2187 ("showstars" org-hide-leading-stars nil
)
2188 ("odd" org-odd-levels-only t
)
2189 ("oddeven" org-odd-levels-only nil
)
2190 ("align" org-startup-align-all-tables t
)
2191 ("noalign" org-startup-align-all-tables nil
)
2192 ("dlcheck" org-startup-with-deadline-check t
)
2193 ("nodlcheck" org-startup-with-deadline-check nil
)))
2195 (while (setq l
(assoc (pop opts
) set
))
2196 (setq var
(nth 1 l
) val
(nth 2 l
))
2197 (set (make-local-variable var
) val
))))
2198 ((equal key
"ARCHIVE")
2199 (string-match " *$" value
)
2200 (setq arch
(replace-match "" t t value
))
2201 (remove-text-properties 0 (length arch
)
2202 '(face t fontified t
) arch
)))
2204 (and cat
(set (make-local-variable 'org-category
) cat
))
2205 (and kwds
(set (make-local-variable 'org-todo-keywords
) kwds
))
2206 (and arch
(set (make-local-variable 'org-archive-location
) arch
))
2207 (and int
(set (make-local-variable 'org-todo-interpretation
) int
)))
2208 ;; Compute the regular expressions and other local variables
2209 (setq org-todo-kwd-priority-p
(equal org-todo-interpretation
'priority
)
2210 org-todo-kwd-max-priority
(1- (length org-todo-keywords
))
2211 org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
2212 (length org-scheduled-string
)))
2214 (nth (1- (length org-todo-keywords
)) org-todo-keywords
)
2216 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
2220 (mapconcat 'regexp-quote
2221 (nreverse (cdr (reverse org-todo-keywords
)))
2224 org-todo-line-regexp
2225 (concat "^\\(\\*+\\)[ \t]*\\("
2226 (mapconcat 'regexp-quote org-todo-keywords
"\\|")
2229 (concat "[\r\n]\\*+[ \t]+" org-done-string
"\\>")
2230 org-looking-at-done-regexp
(concat "^" org-done-string
"\\>")
2231 org-deadline-regexp
(concat "\\<" org-deadline-string
)
2232 org-deadline-time-regexp
2233 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
2234 org-deadline-line-regexp
2235 (concat "\\<\\(" org-deadline-string
"\\).*")
2236 org-scheduled-regexp
2237 (concat "\\<" org-scheduled-string
)
2238 org-scheduled-time-regexp
2239 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>"))
2240 (org-set-font-lock-defaults)))
2242 ;; Tell the compiler about dynamically scoped variables,
2243 ;; and variables from other packages
2244 (defvar zmacs-regions
) ; XEmacs regions
2245 (defvar original-date
) ; dynamically scoped in calendar
2246 (defvar org-old-auto-fill-inhibit-regexp
) ; local variable used by `orgtbl-mode'
2247 (defvar orgtbl-mode-menu
) ; defined when orgtbl mode get initialized
2248 (defvar org-html-entities
) ; defined later in this file
2249 (defvar org-goto-start-pos
) ; dynamically scoped parameter
2250 (defvar org-time-was-given
) ; dynamically scoped parameter
2251 (defvar org-ts-what
) ; dynamically scoped parameter
2252 (defvar mark-active
) ; Emacs only, not available in XEmacs.
2253 (defvar timecnt
) ; dynamically scoped parameter
2254 (defvar levels-open
) ; dynamically scoped parameter
2255 (defvar title
) ; dynamically scoped parameter
2256 (defvar author
) ; dynamically scoped parameter
2257 (defvar email
) ; dynamically scoped parameter
2258 (defvar text
) ; dynamically scoped parameter
2259 (defvar entry
) ; dynamically scoped parameter
2260 (defvar date
) ; dynamically scoped parameter
2261 (defvar language
) ; dynamically scoped parameter
2262 (defvar options
) ; dynamically scoped parameter
2263 (defvar ans1
) ; dynamically scoped parameter
2264 (defvar ans2
) ; dynamically scoped parameter
2265 (defvar starting-day
) ; local variable
2266 (defvar include-all-loc
) ; local variable
2267 (defvar vm-message-pointer
) ; from vm
2268 (defvar vm-folder-directory
) ; from vm
2269 (defvar wl-summary-buffer-elmo-folder
) ; from wanderlust
2270 (defvar wl-summary-buffer-folder-name
) ; from wanderlust
2271 (defvar gnus-group-name
) ; from gnus
2272 (defvar gnus-article-current
) ; from gnus
2273 (defvar w3m-current-url
) ; from w3m
2274 (defvar mh-progs
) ; from MH-E
2275 (defvar mh-current-folder
) ; from MH-E
2276 (defvar mh-show-folder-buffer
) ; from MH-E
2277 (defvar mh-index-folder
) ; from MH-E
2278 (defvar mh-searcher
) ; from MH-E
2279 (defvar org-selected-point
) ; dynamically scoped parameter
2280 (defvar calendar-mode-map
) ; from calendar.el
2281 (defvar last-arg
) ; local variable
2282 (defvar remember-save-after-remembering
) ; from remember.el
2283 (defvar remember-data-file
) ; from remember.el
2284 (defvar annotation
) ; from remember.el, dynamically scoped in `remember-mode'
2285 (defvar initial
) ; from remember.el, dynamically scoped in `remember-mode'
2286 (defvar orgtbl-mode
) ; defined later in this file
2289 (defvar org-mode-map
(copy-keymap outline-mode-map
)
2290 "Keymap for Org-mode.")
2292 (defvar org-struct-menu
) ; defined later in this file
2293 (defvar org-org-menu
) ; defined later in this file
2294 (defvar org-tbl-menu
) ; defined later in this file
2296 ;; We use a before-change function to check if a table might need
2298 (defvar org-table-may-need-update t
2299 "Indicates that a table might need an update.
2300 This variable is set by `org-before-change-function'.
2301 `org-table-align' sets it back to nil.")
2302 (defvar org-mode-hook nil
)
2303 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
2304 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
2308 (define-derived-mode org-mode outline-mode
"Org"
2309 "Outline-based notes management and organizer, alias
2310 \"Carsten's outline-mode for keeping track of everything.\"
2312 Org-mode develops organizational tasks around a NOTES file which
2313 contains information about projects as plain text. Org-mode is
2314 implemented on top of outline-mode, which is ideal to keep the content
2315 of large files well structured. It supports ToDo items, deadlines and
2316 time stamps, which magically appear in the diary listing of the Emacs
2317 calendar. Tables are easily created with a built-in table editor.
2318 Plain text URL-like links connect to websites, emails (VM), Usenet
2319 messages (Gnus), BBDB entries, and any files related to the project.
2320 For printing and sharing of notes, an Org-mode file (or a part of it)
2321 can be exported as a structured ASCII or HTML file.
2323 The following commands are available:
2326 (easy-menu-add org-org-menu
)
2327 (easy-menu-add org-tbl-menu
)
2328 (org-install-agenda-files-menu)
2329 (if org-descriptive-links
(org-add-to-invisibility-spec '(org-link)))
2330 (org-add-to-invisibility-spec '(org-cwidth))
2331 (setq outline-regexp
"\\*+")
2332 ;;(setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
2333 (setq outline-level
'org-outline-level
)
2334 (when (and org-ellipsis
(stringp org-ellipsis
))
2335 (unless org-display-table
2336 (setq org-display-table
(make-display-table)))
2337 (set-display-table-slot org-display-table
2338 4 (string-to-vector org-ellipsis
))
2339 (setq buffer-display-table org-display-table
))
2340 (org-set-regexps-and-options)
2341 (if org-startup-truncated
(setq truncate-lines t
))
2342 (set (make-local-variable 'font-lock-unfontify-region-function
)
2343 'org-unfontify-region
)
2344 ;; Activate before-change-function
2345 (set (make-local-variable 'org-table-may-need-update
) t
)
2346 (org-add-hook 'before-change-functions
'org-before-change-function nil
2348 ;; Paragraphs and auto-filling
2349 (org-set-autofill-regexps)
2350 (org-update-radio-target-regexp)
2351 ;; Settings for Calc embedded mode
2352 (set (make-local-variable 'calc-embedded-open-formula
) "|\\|\n")
2353 (set (make-local-variable 'calc-embedded-close-formula
) "|\\|\n")
2354 (if (and org-insert-mode-line-in-empty-file
2356 (= (point-min) (point-max)))
2357 (insert " -*- mode: org -*-\n\n"))
2359 ;; Get rid of Outline menus, they are not needed
2360 ;; Need to do this here because define-derived-mode sets up
2361 ;; the keymap so late.
2362 (if (featurep 'xemacs
)
2364 (delete-menu-item '("Headings"))
2365 (delete-menu-item '("Show"))
2366 (delete-menu-item '("Hide"))
2367 (set-menubar-dirty-flag))
2368 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
2369 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
2370 (define-key org-mode-map
[menu-bar show
] 'undefined
))
2372 (unless org-inhibit-startup
2373 (if org-startup-align-all-tables
2374 (org-table-map-tables 'org-table-align
))
2375 (if org-startup-with-deadline-check
2376 (call-interactively 'org-check-deadlines
)
2378 ((eq org-startup-folded t
)
2380 ((eq org-startup-folded
'content
)
2381 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
2382 (org-cycle '(4)) (org-cycle '(4))))))))
2384 (defsubst org-current-line
(&optional pos
)
2385 (+ (if (bolp) 1 0) (count-lines (point-min) (or pos
(point)))))
2387 (defconst org-rm-props
'(invisible t face t keymap t intangible t mouse-face t
2389 "Properties to remove when a string without properties is wanted.")
2391 (defsubst org-match-string-no-properties
(num &optional string
)
2392 (if (featurep 'xemacs
)
2393 (let ((s (match-string num string
)))
2394 (remove-text-properties 0 (length s
) org-rm-props s
)
2396 (match-string-no-properties num string
)))
2398 (defun org-current-time ()
2399 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
2400 (if (> org-time-stamp-rounding-minutes
0)
2401 (let ((r org-time-stamp-rounding-minutes
)
2402 (time (decode-time)))
2404 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
2408 (defun org-add-props (string plist
&rest props
)
2409 "Add text properties to entire string, from beginning to end.
2410 PLIST may be a list of properties, PROPS are individual properties and values
2411 that will be added to PLIST. Returns the string that was modified."
2412 (add-text-properties
2413 0 (length string
) (if props
(append plist props
) plist
) string
)
2415 (put 'org-add-props
'lisp-indent-function
2)
2420 (defvar org-mouse-map
(make-sparse-keymap))
2421 (define-key org-mouse-map
2422 (if (featurep 'xemacs
) [button2] [mouse-2]) 'org-open-at-mouse)
2423 (define-key org-mouse-map
2424 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
2425 (when org-mouse-1-follows-link
2426 (define-key org-mouse-map [follow-link] 'mouse-face))
2427 (when org-tab-follows-link
2428 (define-key org-mouse-map [(tab)] 'org-open-at-point)
2429 (define-key org-mouse-map "\C-i" 'org-open-at-point))
2430 (when org-return-follows-link
2431 (define-key org-mouse-map [(return)] 'org-open-at-point)
2432 (define-key org-mouse-map "\C-m" 'org-open-at-point))
2434 (require 'font-lock)
2436 (defconst org-non-link-chars "]\t\n\r<>")
2437 (defconst org-link-types '("https?" "ftp" "mailto" "file" "news" "bbdb" "vm"
2438 "wl" "mhe" "rmail" "gnus" "shell"))
2439 (defconst org-link-re-with-space
2441 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
2442 "\\([^" org-non-link-chars " ]"
2443 "[^" org-non-link-chars "]*"
2444 "[^" org-non-link-chars " ]\\)>?")
2445 "Matches a link with spaces, optional angular brackets around it.")
2447 (defconst org-link-re-with-space2
2449 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
2450 "\\([^" org-non-link-chars " ]"
2452 "[^" org-non-link-chars " ]\\)>?")
2453 "Matches a link with spaces, optional angular brackets around it.")
2455 (defconst org-angle-link-re
2457 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
2458 "\\([^" org-non-link-chars " ]"
2459 "[^" org-non-link-chars "]*"
2461 "Matches link with angular brackets, spaces are allowed.")
2462 (defconst org-plain-link-re
2464 "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
2465 "\\([^]\t\n\r<>,;() ]+\\)")
2466 "Matches plain link, without spaces.")
2468 (defconst org-bracket-link-regexp
2469 "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
2470 "Matches a link in double brackets.")
2472 (defconst org-bracket-link-analytic-regexp
2475 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
2478 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
2487 (defconst org-ts-lengths
2488 (cons (length (format-time-string (car org-time-stamp-formats)))
2489 (length (format-time-string (cdr org-time-stamp-formats))))
2490 "This holds the lengths of the two different time formats.")
2491 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
2492 "Regular expression for fast time stamp matching.")
2493 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
2494 "Regular expression for fast time stamp matching.")
2495 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
2496 "Regular expression matching time strings for analysis.")
2497 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
2498 "Regular expression matching time stamps, with groups.")
2499 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
2500 "Regular expression matching a time stamp range.")
2501 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
2502 org-ts-regexp "\\)?")
2503 "Regular expression matching a time stamp or time stamp range.")
2505 (defun org-activate-plain-links (limit)
2506 "Run through the buffer and add overlays to links."
2507 (if (re-search-forward org-plain-link-re limit t)
2509 (add-text-properties (match-beginning 0) (match-end 0)
2510 (list 'mouse-face 'highlight
2511 'keymap org-mouse-map
2515 (defun org-activate-angle-links (limit)
2516 "Run through the buffer and add overlays to links."
2517 (if (re-search-forward org-angle-link-re limit t)
2519 (add-text-properties (match-beginning 0) (match-end 0)
2520 (list 'mouse-face 'highlight
2521 'keymap org-mouse-map
2525 (defun org-activate-bracket-links (limit)
2526 "Run through the buffer and add overlays to bracketed links."
2527 (if (re-search-forward org-bracket-link-regexp limit t)
2528 (let* ((help (concat "LINK: " (org-match-string-no-properties 1)))
2529 (ip (list 'invisible 'org-link 'intangible t 'rear-nonsticky t
2530 'keymap org-mouse-map 'mouse-face 'highlight
2532 (vp (list 'rear-nonsticky t
2533 'keymap org-mouse-map 'mouse-face 'highlight
2535 ;; We need to remove the invisible property here. Table narrowing
2536 ;; may have made some of this invisible.
2537 (remove-text-properties (match-beginning 0) (match-end 0)
2541 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
2542 (add-text-properties (match-beginning 3) (match-end 3) vp)
2543 (add-text-properties (match-end 3) (match-end 0) ip))
2544 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
2545 (add-text-properties (match-beginning 1) (match-end 1) vp)
2546 (add-text-properties (match-end 1) (match-end 0) ip))
2549 (defun org-activate-dates (limit)
2550 "Run through the buffer and add overlays to dates."
2551 (if (re-search-forward org-tsr-regexp limit t)
2553 (add-text-properties (match-beginning 0) (match-end 0)
2554 (list 'mouse-face 'highlight
2555 'keymap org-mouse-map))
2558 (defvar org-target-link-regexp nil
2559 "Regular expression matching radio targets in plain text.")
2560 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
2561 "Regular expression matching a link target.")
2562 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
2563 "Regular expression matching a link target.")
2565 (defun org-activate-target-links (limit)
2566 "Run through the buffer and add overlays to target matches."
2567 (when org-target-link-regexp
2568 (let ((case-fold-search t))
2569 (if (re-search-forward org-target-link-regexp limit t)
2571 (add-text-properties (match-beginning 0) (match-end 0)
2572 (list 'mouse-face 'highlight
2573 'keymap org-mouse-map
2574 'help-echo "Radio target link"
2575 'org-linked-text t))
2578 (defun org-update-radio-target-regexp ()
2579 "Find all radio targets in this file and update the regular expression."
2581 (when (memq 'radio org-activate-links)
2582 (setq org-target-link-regexp
2583 (org-make-target-link-regexp (org-all-targets 'radio)))
2584 (org-restart-font-lock)))
2586 (defun org-hide-wide-columns (limit)
2588 (setq s (text-property-any (point) (or limit (point-max))
2591 (setq e (next-single-property-change s 'org-cwidth))
2592 (add-text-properties s e '(invisible org-cwidth intangible t))
2596 (defun org-restart-font-lock ()
2597 "Restart font-lock-mode, to force refontification."
2598 (when (and (boundp 'font-lock-mode) font-lock-mode)
2600 (font-lock-mode 1)))
2602 (defun org-all-targets (&optional radio)
2603 "Return a list of all targets in this file.
2604 With optional argument RADIO, only find radio targets."
2605 (let ((re (if radio org-radio-target-regexp org-target-regexp))
2608 (goto-char (point-min))
2609 (while (re-search-forward re nil t)
2610 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
2613 (defun org-make-target-link-regexp (targets)
2614 "Make regular expression matching all strings in TARGETS.
2615 The regular expression finds the targets also if there is a line break
2622 (while (string-match " +" x)
2623 (setq x (replace-match "\\s-+" t t x)))
2629 (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
2630 "Matches CamelCase words, possibly with a star before it.")
2632 (defun org-activate-camels (limit)
2633 "Run through the buffer and add overlays to dates."
2634 (if (re-search-forward org-camel-regexp limit t)
2636 (add-text-properties (match-beginning 0) (match-end 0)
2637 (list 'mouse-face 'highlight
2638 'keymap org-mouse-map))
2641 (defun org-activate-tags (limit)
2642 (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
2644 (add-text-properties (match-beginning 1) (match-end 1)
2645 (list 'mouse-face 'highlight
2646 'keymap org-mouse-map))
2649 (defun org-font-lock-level ()
2651 (org-back-to-heading t)
2652 (- (match-end 0) (match-beginning 0))))
2654 (defun org-outline-level ()
2656 (looking-at outline-regexp)
2657 (if (match-beginning 1)
2658 (+ (org-get-string-indentation (match-string 1)) 1000)
2659 (- (match-end 0) (match-beginning 0)))))
2661 (defvar org-font-lock-keywords nil)
2663 (defun org-set-font-lock-defaults ()
2664 (let* ((em org-fontify-emphasized-text)
2665 (lk org-activate-links)
2666 (org-font-lock-extra-keywords
2668 '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
2669 (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
2670 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
2672 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
2673 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
2674 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
2675 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
2676 (if (memq 'date lk) '(org-activate-dates (0 'org-link t)))
2677 (if (memq 'camel lk) '(org-activate-camels (0 'org-link t)))
2678 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
2679 (if org-table-limit-column-width
2680 '(org-hide-wide-columns (0 nil append)))
2681 (list (concat "^\\*+[ \t]*" org-not-done-regexp)
2682 '(1 'org-warning t))
2683 (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
2684 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
2685 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
2686 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
2687 (if em '("\\(\\W\\|^\\)\\(\\*\\w+\\*\\)\\(\\W\\|$\\)" 2 'bold prepend))
2688 (if em '("\\(\\W\\|^\\)\\(/\\w+/\\)\\(\\W\\|$\\)" 2 'italic prepend))
2689 (if em '("\\(\\W\\|^\\)\\(_\\w+_\\)\\(\\W\\|$\\)" 2 'underline prepend))
2690 (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
2691 "\\|" org-quote-string "\\)\\>")
2692 '(1 'org-special-keyword t))
2693 '("^#.*" (0 'font-lock-comment-face t))
2694 (if org-fontify-done-headline
2695 (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
2696 '(1 'org-done t) '(2 'org-headline-done t))
2697 (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
2699 '("^[ \t]*\\(:.*\\)" (1 'org-table t))
2700 '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
2701 '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
2702 (if org-format-transports-properties-p
2703 '("| *\\(<[0-9]+>\\) *|" (1 'org-formula t)))
2705 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
2706 ;; Now set the full font-lock-keywords
2707 (set (make-local-variable 'org-font-lock-keywords)
2708 org-font-lock-extra-keywords)
2709 (set (make-local-variable 'font-lock-defaults)
2710 '(org-font-lock-keywords t nil nil backward-paragraph))
2711 (kill-local-variable 'font-lock-keywords) nil))
2716 (defun org-get-level-face (n)
2717 "Get the right face for match N in font-lock matching of healdines."
2718 (setq org-l (- (match-end 2) (match-beginning 1)))
2719 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
2720 ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
2721 (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
2723 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
2725 (t (if org-level-color-stars-only nil org-f))))
2727 (defun org-unfontify-region (beg end &optional maybe_loudly)
2728 "Remove fontification and activation overlays from links."
2729 (font-lock-default-unfontify-region beg end)
2730 (let* ((buffer-undo-list t)
2731 (inhibit-read-only t) (inhibit-point-motion-hooks t)
2732 (inhibit-modification-hooks t)
2733 deactivate-mark buffer-file-name buffer-file-truename)
2734 (remove-text-properties beg end
2735 '(mouse-face nil keymap nil org-linked-text nil
2736 invisible nil intangible nil))))
2737 ;;; Visibility cycling
2739 (defvar org-cycle-global-status nil)
2740 (defvar org-cycle-subtree-status nil)
2741 (defun org-cycle (&optional arg)
2742 "Visibility cycling for Org-mode.
2744 - When this function is called with a prefix argument, rotate the entire
2745 buffer through 3 states (global cycling)
2746 1. OVERVIEW: Show only top-level headlines.
2747 2. CONTENTS: Show all headlines of all levels, but no body text.
2748 3. SHOW ALL: Show everything.
2750 - When point is at the beginning of a headline, rotate the subtree started
2751 by this line through 3 different states (local cycling)
2752 1. FOLDED: Only the main headline is shown.
2753 2. CHILDREN: The main headline and the direct children are shown.
2754 From this state, you can move to one of the children
2755 and zoom in further.
2756 3. SUBTREE: Show the entire subtree, including body text.
2758 - When there is a numeric prefix, go up to a heading with level ARG, do
2759 a `show-subtree' and return to the previous cursor position. If ARG
2760 is negative, go up that many levels.
2762 - When point is not at the beginning of a headline, execute
2763 `indent-relative', like TAB normally does. See the option
2764 `org-cycle-emulate-tab' for details.
2766 - Special case: if point is the the beginning of the buffer and there is
2767 no headline in line 1, this function will act as if called with prefix arg."
2770 (if (or (and (bobp) (not (looking-at outline-regexp)))
2772 ;; special case: use global cycling
2775 (let ((outline-regexp
2776 (if org-cycle-include-plain-lists
2777 "\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) "
2782 ((org-at-table-p 'any)
2783 ;; Enter the table or move to the next field in the table
2784 (or (org-table-recognize-table.el)
2786 (if arg (org-table-edit-field t)
2787 (org-table-justify-field-maybe)
2788 (org-table-next-field)))))
2790 ((eq arg t) ;; Global cycling
2793 ((and (eq last-command this-command)
2794 (eq org-cycle-global-status 'overview))
2795 ;; We just created the overview - now do table of contents
2796 ;; This can be slow in very large buffers, so indicate action
2797 (message "CONTENTS...")
2799 ;; Visit all headings and show their offspring
2800 (goto-char (point-max))
2802 (while (and (progn (condition-case nil
2803 (outline-previous-visible-heading 1)
2804 (error (goto-char (point-min))))
2806 (looking-at outline-regexp))
2808 (if (bobp) (throw 'exit nil))))
2809 (message "CONTENTS...done"))
2810 (setq org-cycle-global-status 'contents)
2811 (run-hook-with-args 'org-cycle-hook 'contents))
2813 ((and (eq last-command this-command)
2814 (eq org-cycle-global-status 'contents))
2815 ;; We just showed the table of contents - now show everything
2817 (message "SHOW ALL")
2818 (setq org-cycle-global-status 'all)
2819 (run-hook-with-args 'org-cycle-hook 'all))
2822 ;; Default action: go to overview
2824 (message "OVERVIEW")
2825 (setq org-cycle-global-status 'overview)
2826 (run-hook-with-args 'org-cycle-hook 'overview))))
2829 ;; Show-subtree, ARG levels up from here.
2831 (org-back-to-heading)
2832 (outline-up-heading (if (< arg 0) (- arg)
2833 (- (funcall outline-level) arg)))
2834 (org-show-subtree)))
2836 ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
2837 ;; At a heading: rotate between three different views
2838 (org-back-to-heading)
2839 (let ((goal-column 0) eoh eol eos)
2840 ;; First, some boundaries
2842 (org-back-to-heading)
2844 (beginning-of-line 2)
2845 (while (and (not (eobp)) ;; this is like `next-line'
2846 (get-char-property (1- (point)) 'invisible))
2847 (beginning-of-line 2)) (setq eol (point)))
2848 (outline-end-of-heading) (setq eoh (point))
2849 (org-end-of-subtree t) (setq eos (point))
2850 (outline-next-heading))
2851 ;; Find out what to do next and set `this-command'
2854 ;; Nothing is hidden behind this heading
2855 (message "EMPTY ENTRY")
2856 (setq org-cycle-subtree-status nil))
2858 ;; Entire subtree is hidden in one line: open it
2861 (message "CHILDREN")
2862 (setq org-cycle-subtree-status 'children)
2863 (run-hook-with-args 'org-cycle-hook 'children))
2864 ((and (eq last-command this-command)
2865 (eq org-cycle-subtree-status 'children))
2866 ;; We just showed the children, now show everything.
2869 (setq org-cycle-subtree-status 'subtree)
2870 (run-hook-with-args 'org-cycle-hook 'subtree))
2872 ;; Default action: hide the subtree.
2875 (setq org-cycle-subtree-status 'folded)
2876 (run-hook-with-args 'org-cycle-hook 'folded)))))
2879 (buffer-read-only (org-back-to-heading))
2880 ((if (and (eq org-cycle-emulate-tab 'white)
2881 (save-excursion (beginning-of-line 1) (looking-at "[ \t]+$")))
2883 (eq org-cycle-emulate-tab t))
2884 (if (and (looking-at "[ \n\r\t]")
2885 (string-match "^[ \t]*$" (buffer-substring
2886 (point-at-bol) (point))))
2888 (beginning-of-line 1)
2889 (and (looking-at "[ \t]+") (replace-match ""))))
2893 (org-back-to-heading)
2896 (defun org-optimize-window-after-visibility-change (state)
2897 "Adjust the window after a change in outline visibility.
2898 This function is the default value of the hook `org-cycle-hook'."
2899 (when (get-buffer-window (current-buffer))
2901 ((eq state 'overview) (org-first-headline-recenter 1))
2902 ((eq state 'content) nil)
2903 ((eq state 'all) nil)
2904 ((eq state 'folded) nil)
2905 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
2906 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
2908 (defun org-subtree-end-visible-p ()
2909 "Is the end of the current subtree visible?"
2910 (pos-visible-in-window-p
2911 (save-excursion (org-end-of-subtree t) (point))))
2913 (defun org-first-headline-recenter (&optional N)
2914 "Move cursor to the first headline and recenter the headline.
2915 Optional argument N means, put the headline into the Nth line of the window."
2916 (goto-char (point-min))
2917 (when (re-search-forward (concat "^" outline-regexp) nil t)
2919 (recenter (prefix-numeric-value N))))
2921 (defvar org-goto-window-configuration nil)
2922 (defvar org-goto-marker nil)
2923 (defvar org-goto-map (make-sparse-keymap))
2924 (let ((cmds '(isearch-forward isearch-backward)) cmd)
2925 (while (setq cmd (pop cmds))
2926 (substitute-key-definition cmd cmd org-goto-map global-map)))
2927 (define-key org-goto-map "\C-m" 'org-goto-ret)
2928 (define-key org-goto-map [(left)] 'org-goto-left)
2929 (define-key org-goto-map [(right)] 'org-goto-right)
2930 (define-key org-goto-map [(?q)] 'org-goto-quit)
2931 (define-key org-goto-map [(control ?g)] 'org-goto-quit)
2932 (define-key org-goto-map "\C-i" 'org-cycle)
2933 (define-key org-goto-map [(tab)] 'org-cycle)
2934 (define-key org-goto-map [(down)] 'outline-next-visible-heading)
2935 (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
2936 (define-key org-goto-map "n" 'outline-next-visible-heading)
2937 (define-key org-goto-map "p" 'outline-previous-visible-heading)
2938 (define-key org-goto-map "f" 'outline-forward-same-level)
2939 (define-key org-goto-map "b" 'outline-backward-same-level)
2940 (define-key org-goto-map "u" 'outline-up-heading)
2941 (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
2942 (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
2943 (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
2944 (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
2945 (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
2946 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2947 (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
2949 (defconst org-goto-help
2950 "Select a location to jump to, press RET
2951 \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
2954 "Go to a different location of the document, keeping current visibility.
2956 When you want to go to a different location in a document, the fastest way
2957 is often to fold the entire buffer and then dive into the tree. This
2958 method has the disadvantage, that the previous location will be folded,
2959 which may not be what you want.
2961 This command works around this by showing a copy of the current buffer in
2962 overview mode. You can dive into the tree in that copy, to find the
2963 location you want to reach. When pressing RET, the command returns to the
2964 original buffer in which the visibility is still unchanged. It then jumps
2965 to the new location, making it and the headline hierarchy above it visible."
2967 (let* ((org-goto-start-pos (point))
2969 (org-get-location (current-buffer) org-goto-help)))
2972 (org-mark-ring-push org-goto-start-pos)
2973 (goto-char selected-point)
2974 (if (or (org-invisible-p) (org-invisible-p2))
2975 (org-show-hierarchy-above)))
2978 (defun org-get-location (buf help)
2979 "Let the user select a location in the Org-mode buffer BUF.
2980 This function uses a recursive edit. It returns the selected position
2982 (let (org-selected-point)
2984 (save-window-excursion
2985 (delete-other-windows)
2986 (switch-to-buffer (get-buffer-create "*org-goto*"))
2987 (with-output-to-temp-buffer "*Help*"
2989 (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
2990 (setq buffer-read-only nil)
2992 (insert-buffer-substring buf)
2993 (let ((org-startup-truncated t)
2994 (org-startup-folded t)
2995 (org-startup-align-all-tables nil)
2996 (org-startup-with-deadline-check nil))
2998 (setq buffer-read-only t)
2999 (if (boundp 'org-goto-start-pos)
3000 (goto-char org-goto-start-pos)
3001 (goto-char (point-min)))
3002 (org-beginning-of-line)
3003 (message "Select location and press RET")
3004 ;; now we make sure that during selection, ony very few keys work
3005 ;; and that it is impossible to switch to another window.
3006 (let ((gm (current-global-map))
3007 (overriding-local-map org-goto-map))
3010 (use-global-map org-goto-map)
3012 (use-global-map gm)))))
3013 (kill-buffer "*org-goto*")
3014 org-selected-point))
3016 ;; FIXME: It may not be a good idea to temper with the prefix argument...
3017 (defun org-goto-ret (&optional arg)
3018 "Finish `org-goto' by going to the new location."
3020 (setq org-selected-point (point)
3021 current-prefix-arg arg)
3024 (defun org-goto-left ()
3025 "Finish `org-goto' by going to the new location."
3027 (if (org-on-heading-p)
3029 (beginning-of-line 1)
3030 (setq org-selected-point (point)
3031 current-prefix-arg (- (match-end 0) (match-beginning 0)))
3033 (error "Not on a heading")))
3035 (defun org-goto-right ()
3036 "Finish `org-goto' by going to the new location."
3038 (if (org-on-heading-p)
3040 (outline-end-of-subtree)
3041 (or (eobp) (forward-char 1))
3042 (setq org-selected-point (point)
3043 current-prefix-arg (- (match-end 0) (match-beginning 0)))
3045 (error "Not on a heading")))
3047 (defun org-goto-quit ()
3048 "Finish `org-goto' without cursor motion."
3050 (setq org-selected-point nil)
3053 ;;; Promotion, Demotion, Inserting new headlines
3055 (defvar org-ignore-region nil
3056 "To temporarily disable the active region.")
3058 (defun org-insert-heading (&optional force-heading)
3059 "Insert a new heading or item with same depth at point."
3061 (when (or force-heading (not (org-insert-item)))
3062 (let* ((head (save-excursion
3064 (org-back-to-heading)
3065 (error (outline-next-heading)))
3066 (prog1 (match-string 0)
3067 (funcall outline-level)))))
3068 (unless (bolp) (newline))
3070 (if (looking-at "[ \t]*")
3071 (replace-match " "))
3072 (run-hooks 'org-insert-heading-hook))))
3074 (defun org-insert-item ()
3075 "Insert a new item at the current level.
3076 Return t when things worked, nil when we are not in an item."
3077 (when (save-excursion
3080 (org-beginning-of-item)
3084 (unless (bolp) (newline))
3085 (insert (match-string 0))
3086 (org-maybe-renumber-ordered-list)
3089 (defun org-insert-todo-heading (arg)
3090 "Insert a new heading with the same level and TODO state as current heading.
3091 If the heading has no TODO state, or if the state is DONE, use the first
3092 state (TODO by default). Also with prefix arg, force first state."
3094 (org-insert-heading)
3096 (org-back-to-heading)
3097 (outline-previous-heading)
3098 (looking-at org-todo-line-regexp))
3100 (not (match-beginning 2))
3101 (equal (match-string 2) org-done-string))
3102 (insert (car org-todo-keywords) " ")
3103 (insert (match-string 2) " ")))
3105 (defun org-promote-subtree ()
3106 "Promote the entire subtree.
3107 See also `org-promote'."
3110 (org-map-tree 'org-promote)))
3112 (defun org-demote-subtree ()
3113 "Demote the entire subtree. See `org-demote'.
3114 See also `org-promote'."
3117 (org-map-tree 'org-demote)))
3119 (defun org-do-promote ()
3120 "Promote the current heading higher up the tree.
3121 If the region is active in `transient-mark-mode', promote all headings
3125 (if (org-region-active-p)
3126 (org-map-region 'org-promote (region-beginning) (region-end))
3128 (org-fix-position-after-promote))
3130 (defun org-do-demote ()
3131 "Demote the current heading lower down the tree.
3132 If the region is active in `transient-mark-mode', demote all headings
3136 (if (org-region-active-p)
3137 (org-map-region 'org-demote (region-beginning) (region-end))
3139 (org-fix-position-after-promote))
3141 (defun org-fix-position-after-promote ()
3142 "Make sure that after pro/demotion cursor position is right."
3143 (and (equal (char-after) ?\ )
3144 (equal (char-before) ?*)
3147 (defun org-get-legal-level (level change)
3148 "Rectify a level change under the influence of `org-odd-levels-only'
3149 LEVEL is a current level, CHANGE is by how much the level should be
3150 modified. Even if CHANGE is nil, LEVEL may be returned modified because
3151 even level numbers will become the next higher odd number."
3152 (if org-odd-levels-only
3153 (cond ((not change) (1+ (* 2 (/ level 2))))
3154 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
3155 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
3156 (max 1 (+ level change))))
3158 (defun org-promote ()
3159 "Promote the current heading higher up the tree.
3160 If the region is active in `transient-mark-mode', promote all headings
3162 (org-back-to-heading t)
3163 (let* ((level (save-match-data (funcall outline-level)))
3164 (up-head (make-string (org-get-legal-level level -1) ?*))
3165 (diff (abs (- level (length up-head)))))
3166 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
3167 (replace-match up-head nil t)
3168 ;; Fixup tag positioning
3169 (and org-auto-align-tags (org-set-tags nil t))
3170 (if org-adapt-indentation
3171 (org-fixup-indentation (if (> diff 1) "^ " "^ ") ""
3172 (if (> diff 1) "^ ? ?\\S-" "^ ?\\S-")))))
3174 (defun org-demote ()
3175 "Demote the current heading lower down the tree.
3176 If the region is active in `transient-mark-mode', demote all headings
3178 (org-back-to-heading t)
3179 (let* ((level (save-match-data (funcall outline-level)))
3180 (down-head (make-string (org-get-legal-level level 1) ?*))
3181 (diff (abs (- level (length down-head)))))
3182 (replace-match down-head nil t)
3183 ;; Fixup tag positioning
3184 (and org-auto-align-tags (org-set-tags nil t))
3185 (if org-adapt-indentation
3186 (org-fixup-indentation "^ " (if (> diff 1) " " " ") "^\\S-"))))
3188 (defun org-map-tree (fun)
3189 "Call FUN for every heading underneath the current one."
3190 (org-back-to-heading)
3191 (let ((level (funcall outline-level)))
3195 (outline-next-heading)
3196 (> (funcall outline-level) level))
3200 (defun org-map-region (fun beg end)
3201 "Call FUN for every heading between BEG and END."
3202 (let ((org-ignore-region t))
3204 (setq end (copy-marker end))
3206 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
3210 (outline-next-heading)
3215 (defun org-fixup-indentation (from to prohibit)
3216 "Change the indentation in the current entry by re-replacing FROM with TO.
3217 However, if the regexp PROHIBIT matches at all, don't do anything.
3218 This is being used to change indentation along with the length of the
3219 heading marker. But if there are any lines which are not indented, nothing
3222 (let ((end (save-excursion (outline-next-heading)
3224 (unless (save-excursion (re-search-forward prohibit end t))
3225 (while (re-search-forward from end t)
3227 (beginning-of-line 2)))
3228 (move-marker end nil))))
3230 ;;; Vertical tree motion, cutting and pasting of subtrees
3232 (defun org-move-subtree-up (&optional arg)
3233 "Move the current subtree up past ARG headlines of the same level."
3235 (org-move-subtree-down (- (prefix-numeric-value arg))))
3237 (defun org-move-subtree-down (&optional arg)
3238 "Move the current subtree down past ARG headlines of the same level."
3240 (setq arg (prefix-numeric-value arg))
3241 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
3242 'outline-get-last-sibling))
3243 (ins-point (make-marker))
3247 (org-back-to-heading)
3250 (save-excursion (outline-end-of-heading)
3251 (setq folded (org-invisible-p)))
3252 (outline-end-of-subtree))
3253 (outline-next-heading)
3255 ;; Find insertion point, with error handling
3258 (or (and (funcall movfunc) (looking-at outline-regexp))
3259 (progn (goto-char beg)
3260 (error "Cannot move past superior level or buffer limit")))
3261 (setq cnt (1- cnt)))
3263 ;; Moving forward - still need to move over subtree
3264 (progn (outline-end-of-subtree)
3265 (outline-next-heading)
3266 (if (not (or (looking-at (concat "^" outline-regexp))
3269 (move-marker ins-point (point))
3270 (setq txt (buffer-substring beg end))
3271 (delete-region beg end)
3273 (goto-char ins-point)
3274 (if folded (hide-subtree))
3275 (move-marker ins-point nil)))
3277 (defvar org-subtree-clip ""
3278 "Clipboard for cut and paste of subtrees.
3279 This is actually only a copy of the kill, because we use the normal kill
3280 ring. We need it to check if the kill was created by `org-copy-subtree'.")
3282 (defvar org-subtree-clip-folded nil
3283 "Was the last copied subtree folded?
3284 This is used to fold the tree back after pasting.")
3286 (defun org-cut-subtree ()
3287 "Cut the current subtree into the clipboard.
3288 This is a short-hand for marking the subtree and then cutting it."
3290 (org-copy-subtree 'cut))
3292 (defun org-copy-subtree (&optional cut)
3293 "Cut the current subtree into the clipboard.
3294 This is a short-hand for marking the subtree and then copying it.
3295 If CUT is non nil, actually cut the subtree."
3297 (let (beg end folded)
3298 (org-back-to-heading)
3301 (save-excursion (outline-end-of-heading)
3302 (setq folded (org-invisible-p)))
3303 (outline-end-of-subtree))
3304 (if (equal (char-after) ?\n) (forward-char 1))
3308 (setq org-subtree-clip-folded folded)
3309 (if cut (kill-region beg end) (copy-region-as-kill beg end))
3310 (setq org-subtree-clip (current-kill 0))
3311 (message "%s: Subtree with %d characters"
3312 (if cut "Cut" "Copied")
3313 (length org-subtree-clip)))))
3315 (defun org-paste-subtree (&optional level tree)
3316 "Paste the clipboard as a subtree, with modification of headline level.
3317 The entire subtree is promoted or demoted in order to match a new headline
3318 level. By default, the new level is derived from the visible headings
3319 before and after the insertion point, and taken to be the inferior headline
3320 level of the two. So if the previous visible heading is level 3 and the
3321 next is level 4 (or vice versa), level 4 will be used for insertion.
3322 This makes sure that the subtree remains an independent subtree and does
3323 not swallow low level entries.
3325 You can also force a different level, either by using a numeric prefix
3326 argument, or by inserting the heading marker by hand. For example, if the
3327 cursor is after \"*****\", then the tree will be shifted to level 5.
3329 If you want to insert the tree as is, just use \\[yank].
3331 If optional TREE is given, use this text instead of the kill ring."
3333 (unless (org-kill-is-subtree-p tree)
3335 (substitute-command-keys
3336 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
3337 (let* ((txt (or tree (current-kill 0)))
3338 (^re (concat "^\\(" outline-regexp "\\)"))
3339 (re (concat "\\(" outline-regexp "\\)"))
3340 (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
3342 (old-level (if (string-match ^re txt)
3343 (- (match-end 0) (match-beginning 0))
3345 (force-level (cond (level (prefix-numeric-value level))
3347 ^re_ (buffer-substring (point-at-bol) (point)))
3348 (- (match-end 0) (match-beginning 0)))
3350 (previous-level (save-excursion
3353 (outline-previous-visible-heading 1)
3355 (- (match-end 0) (match-beginning 0))
3358 (next-level (save-excursion
3361 (outline-next-visible-heading 1)
3363 (- (match-end 0) (match-beginning 0))
3366 (new-level (or force-level (max previous-level next-level)))
3367 (shift (if (or (= old-level -1)
3369 (= old-level new-level))
3371 (- new-level old-level)))
3373 (delta (if (> shift 0) -1 1))
3374 (func (if (> shift 0) 'org-demote 'org-promote))
3375 (org-odd-levels-only nil)
3377 ;; Remove the forces level indicator
3379 (delete-region (point-at-bol) (point)))
3380 ;; Make sure we start at the beginning of an empty line
3381 (if (not (bolp)) (insert "\n"))
3382 (if (not (looking-at "[ \t]*$"))
3383 (progn (insert "\n") (backward-char 1)))
3389 ;; Shift if necessary
3391 (message "Pasted at level %d, without shift" new-level)
3393 (narrow-to-region beg end)
3394 (while (not (= shift 0))
3395 (org-map-region func (point-min) (point-max))
3396 (setq shift (+ delta shift)))
3397 (goto-char (point-min))
3398 (message "Pasted at level %d, with shift by %d levels"
3400 (if (and (eq org-subtree-clip (current-kill 0))
3401 org-subtree-clip-folded)
3402 ;; The tree was folded before it was killed/copied
3405 (defun org-kill-is-subtree-p (&optional txt)
3406 "Check if the current kill is an outline subtree, or a set of trees.
3407 Returns nil if kill does not start with a headline, or if the first
3408 headline level is not the largest headline level in the tree.
3409 So this will actually accept several entries of equal levels as well,
3410 which is OK for `org-paste-subtree'.
3411 If optional TXT is given, check this string instead of the current kill."
3412 (let* ((kill (or txt (current-kill 0) ""))
3413 (start-level (and (string-match (concat "\\`" outline-regexp) kill)
3414 (- (match-end 0) (match-beginning 0))))
3415 (re (concat "^" outline-regexp))
3417 (if (not start-level)
3418 nil ;; does not even start with a heading
3420 (while (setq start (string-match re kill (1+ start)))
3421 (if (< (- (match-end 0) (match-beginning 0)) start-level)
3425 ;;; Plain list items
3427 (defun org-at-item-p ()
3428 "Is point in a line starting a hand-formatted item?"
3429 (let ((llt org-plain-list-ordered-item-terminator))
3431 (goto-char (point-at-bol))
3434 ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
3435 ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
3436 ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
3437 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
3439 (defun org-get-indentation ()
3440 "Get the indentation of the current line, interpreting tabs."
3442 (beginning-of-line 1)
3443 (skip-chars-forward " \t")
3446 (defun org-beginning-of-item ()
3447 "Go to the beginning of the current hand-formatted item.
3448 If the cursor is not in an item, throw an error."
3450 (limit (save-excursion (org-back-to-heading)
3451 (beginning-of-line 2) (point)))
3454 (beginning-of-line 1)
3455 (beginning-of-line 1)
3456 (skip-chars-forward " \t")
3457 (setq ind (current-column))
3460 (beginning-of-line 0)
3461 (if (< (point) limit) (throw 'exit nil))
3462 (unless (looking-at " \t]*$")
3463 (skip-chars-forward " \t")
3464 (setq ind1 (current-column))
3466 (throw 'exit (org-at-item-p))))))
3469 (error "Not in an item")))))
3471 (defun org-end-of-item ()
3472 "Go to the end of the current hand-formatted item.
3473 If the cursor is not in an item, throw an error."
3475 (limit (save-excursion (outline-next-heading) (point)))
3476 (ind (save-excursion
3477 (org-beginning-of-item)
3478 (skip-chars-forward " \t")
3483 (beginning-of-line 2)
3484 (if (>= (point) limit) (throw 'exit t))
3485 (unless (looking-at "[ \t]*$")
3486 (skip-chars-forward " \t")
3487 (setq ind1 (current-column))
3488 (if (<= ind1 ind) (throw 'exit t)))))
3489 (beginning-of-line 1)
3491 (error "Not in an item"))))
3493 (defun org-move-item-down (arg)
3494 "Move the plain list item at point down, i.e. swap with following item.
3495 Subitems (items with larger indentation) are considered part of the item,
3496 so this really moves item trees."
3498 (let (beg end ind ind1 (pos (point)) txt)
3499 (org-beginning-of-item)
3501 (setq ind (org-get-indentation))
3504 (setq ind1 (org-get-indentation))
3505 (if (and (org-at-item-p) (= ind ind1))
3508 (setq txt (buffer-substring beg end))
3510 (delete-region beg end))
3514 (org-maybe-renumber-ordered-list))
3516 (error "Cannot move this item further down"))))
3518 (defun org-move-item-up (arg)
3519 "Move the plain list item at point up, i.e. swap with previous item.
3520 Subitems (items with larger indentation) are considered part of the item,
3521 so this really moves item trees."
3523 (let (beg end ind ind1 (pos (point)) txt)
3524 (org-beginning-of-item)
3526 (setq ind (org-get-indentation))
3532 (beginning-of-line 0)
3533 (if (looking-at "[ \t]*$")
3535 (if (<= (setq ind1 (org-get-indentation)) ind)
3538 (org-beginning-of-item)
3539 (error (goto-char beg)
3540 (error "Cannot move this item further up")))
3541 (setq ind1 (org-get-indentation))
3542 (if (and (org-at-item-p) (= ind ind1))
3544 (setq txt (buffer-substring beg end))
3546 (delete-region beg end))
3550 (org-maybe-renumber-ordered-list))
3552 (error "Cannot move this item further up"))))
3554 (defun org-maybe-renumber-ordered-list ()
3555 "Renumber the ordered list at point if setup allows it.
3556 This tests the user option `org-auto-renumber-ordered-lists' before
3557 doing the renumbering."
3558 (and org-auto-renumber-ordered-lists
3561 (org-renumber-ordered-list 1)))
3563 (defun org-get-string-indentation (s)
3564 "What indentation has S due to SPACE and TAB at the beginning of the string?"
3565 (let ((n -1) (i 0) (w tab-width) c)
3567 (while (< (setq n (1+ n)) (length s))
3569 (cond ((= c ?\ ) (setq i (1+ i)))
3570 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
3571 (t (throw 'exit t)))))
3574 (defun org-renumber-ordered-list (arg)
3575 "Renumber an ordered plain list.
3576 Cursor next to be in the first line of an item, the line that starts
3577 with something like \"1.\" or \"2)\"."
3579 (unless (and (org-at-item-p)
3580 (match-beginning 3))
3581 (error "This is not an ordered list"))
3582 (let ((line (org-current-line))
3583 (col (current-column))
3584 (ind (org-get-string-indentation
3585 (buffer-substring (point-at-bol) (match-beginning 3))))
3586 ;; (term (substring (match-string 3) -1))
3588 ;; find where this list begins
3592 (beginning-of-line 0)
3593 (if (looking-at "[ \t]*$") (throw 'next t))
3594 (skip-chars-forward " \t") (setq ind1 (current-column))
3595 (if (or (< ind1 ind)
3597 (not (org-at-item-p))))
3599 ;; Walk forward and replace these numbers
3603 (beginning-of-line 2)
3604 (if (eobp) (throw 'exit nil))
3605 (if (looking-at "[ \t]*$") (throw 'next nil))
3606 (skip-chars-forward " \t") (setq ind1 (current-column))
3607 (if (> ind1 ind) (throw 'next t))
3608 (if (< ind1 ind) (throw 'exit t))
3609 (if (not (org-at-item-p)) (throw 'exit nil))
3610 (if (not (match-beginning 3))
3611 (error "unordered bullet in ordered list. Press \\[undo] to recover"))
3612 (delete-region (match-beginning 3) (1- (match-end 3)))
3613 (goto-char (match-beginning 3))
3614 (insert (format "%d" (setq n (1+ n)))))))
3616 (move-to-column col)))
3618 (defvar org-last-indent-begin-marker (make-marker))
3619 (defvar org-last-indent-end-marker (make-marker))
3621 (defun org-outdent-item (arg)
3622 "Outdent a local list item."
3624 (org-indent-item (- arg)))
3626 (defun org-indent-item (arg)
3627 "Indent a local list item."
3629 (unless (org-at-item-p)
3630 (error "Not on an item"))
3631 (let (beg end ind ind1)
3632 (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
3633 (setq beg org-last-indent-begin-marker
3634 end org-last-indent-end-marker)
3635 (org-beginning-of-item)
3636 (setq beg (move-marker org-last-indent-begin-marker (point)))
3638 (setq end (move-marker org-last-indent-end-marker (point))))
3640 (skip-chars-forward " \t") (setq ind (current-column))
3641 (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
3642 (while (< (point) end)
3643 (beginning-of-line 1)
3644 (skip-chars-forward " \t") (setq ind1 (current-column))
3645 (delete-region (point-at-bol) (point))
3646 (indent-to-column (+ ind1 arg))
3647 (beginning-of-line 2))
3652 (defun org-archive-subtree ()
3653 "Move the current subtree to the archive.
3654 The archive can be a certain top-level heading in the current file, or in
3655 a different file. The tree will be moved to that location, the subtree
3656 heading be marked DONE, and the current time will be added."
3658 ;; Save all relevant TODO keyword-relatex variables
3659 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
3660 (tr-org-todo-keywords org-todo-keywords)
3661 (tr-org-todo-interpretation org-todo-interpretation)
3662 (tr-org-done-string org-done-string)
3663 (tr-org-todo-regexp org-todo-regexp)
3664 (tr-org-todo-line-regexp org-todo-line-regexp)
3665 (this-buffer (current-buffer))
3666 file heading buffer level newfile-p)
3667 (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
3669 (setq file (format (match-string 1 org-archive-location)
3670 (file-name-nondirectory buffer-file-name))
3671 heading (match-string 2 org-archive-location)))
3672 (error "Invalid `org-archive-location'"))
3673 (if (> (length file) 0)
3674 (setq newfile-p (not (file-exists-p file))
3675 buffer (find-file-noselect file))
3676 (setq buffer (current-buffer)))
3678 (error "Cannot access file \"%s\"" file))
3679 (if (and (> (length heading) 0)
3680 (string-match "^\\*+" heading))
3681 (setq level (match-end 0))
3682 (setq heading nil level 0))
3684 ;; We first only copy, in case something goes wrong
3685 ;; we need to protect this-command, to avoid kill-region sets it,
3686 ;; which would lead to duplication of subtrees
3687 (let (this-command) (org-copy-subtree))
3689 ;; Enforce org-mode for the archive buffer
3690 (if (not (eq major-mode 'org-mode))
3691 ;; Force the mode for future visits.
3692 (let ((org-insert-mode-line-in-empty-file t))
3693 (call-interactively 'org-mode)))
3695 (goto-char (point-max))
3696 (insert (format "\nArchived entries from file %s\n\n"
3697 (buffer-file-name this-buffer))))
3698 ;; Force the TODO keywords of the original buffer
3699 (let ((org-todo-line-regexp tr-org-todo-line-regexp)
3700 (org-todo-keywords tr-org-todo-keywords)
3701 (org-todo-interpretation tr-org-todo-interpretation)
3702 (org-done-string tr-org-done-string)
3703 (org-todo-regexp tr-org-todo-regexp)
3704 (org-todo-line-regexp tr-org-todo-line-regexp))
3705 (goto-char (point-min))
3708 (if (re-search-forward
3709 (concat "\\(^\\|\r\\)"
3710 (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
3712 (goto-char (match-end 0))
3713 ;; Heading not found, just insert it at the end
3714 (goto-char (point-max))
3715 (or (bolp) (insert "\n"))
3716 (insert "\n" heading "\n")
3718 ;; Make the heading visible, and the following as well
3719 (let ((org-show-following-heading t)) (org-show-hierarchy-above))
3720 (if (re-search-forward
3721 (concat "^" (regexp-quote (make-string level ?*)) "[ \t]")
3723 (progn (goto-char (match-beginning 0)) (insert "\n")
3724 (beginning-of-line 0))
3725 (goto-char (point-max)) (insert "\n")))
3726 (goto-char (point-max)) (insert "\n"))
3728 (org-paste-subtree (1+ level))
3729 ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
3730 (if org-archive-mark-done
3731 (org-todo (length org-todo-keywords)))
3732 ;; Move cursor to right after the TODO keyword
3733 (when org-archive-stamp-time
3734 (beginning-of-line 1)
3735 (looking-at org-todo-line-regexp)
3736 (goto-char (or (match-end 2) (match-beginning 3)))
3737 (insert "(" (format-time-string (cdr org-time-stamp-formats)
3740 ;; Save the buffer, if it is not the same buffer.
3741 (if (not (eq this-buffer buffer)) (save-buffer))))
3742 ;; Here we are back in the original buffer. Everything seems to have
3743 ;; worked. So now cut the tree and finish up.
3744 (let (this-command) (org-cut-subtree))
3745 (if (looking-at "[ \t]*$") (kill-line))
3746 (message "Subtree archived %s"
3747 (if (eq this-buffer buffer)
3748 (concat "under heading: " heading)
3749 (concat "in file: " (abbreviate-file-name file))))))
3753 (defun org-complete (&optional arg)
3754 "Perform completion on word at point.
3755 At the beginning of a headline, this completes TODO keywords as given in
3756 `org-todo-keywords'.
3757 If the current word is preceded by a backslash, completes the TeX symbols
3758 that are supported for HTML support.
3759 If the current word is preceded by \"#+\", completes special words for
3760 setting file options.
3761 At all other locations, this simply calls `ispell-complete-word'."
3764 (let* ((end (point))
3765 (beg1 (save-excursion
3766 (if (equal (char-before (point)) ?\ ) (backward-char 1))
3767 (skip-chars-backward "a-zA-Z_@0-9")
3769 (beg (save-excursion
3770 (if (equal (char-before (point)) ?\ ) (backward-char 1))
3771 (skip-chars-backward "a-zA-Z0-9_:$")
3773 (camel (equal (char-before beg) ?*))
3774 (tag (equal (char-before beg1) ?:))
3775 (texp (equal (char-before beg) ?\\))
3776 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
3779 (completion-ignore-case opt)
3786 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
3787 (cons (match-string 2 x) (match-string 1 x)))
3788 (org-split-string (org-get-current-options) "\n")))
3792 ((string-match "\\`\\*+[ \t]*\\'"
3793 (buffer-substring (point-at-bol) beg))
3795 (mapcar 'list org-todo-keywords))
3799 (goto-char (point-min))
3800 (while (re-search-forward org-todo-line-regexp nil t)
3802 (if org-file-link-context-use-camel-case
3803 (org-make-org-heading-camel (match-string 3) t)
3804 (org-make-org-heading-search-string
3805 (match-string 3) t)))
3808 (tag (setq type :tag beg beg1)
3809 (org-get-buffer-tags))
3810 (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
3811 (pattern (buffer-substring-no-properties beg end))
3812 (completion (try-completion pattern table)))
3813 (cond ((eq completion t)
3814 (if (equal type :opt)
3815 (insert (substring (cdr (assoc (upcase pattern) table))
3816 (length pattern)))))
3818 (message "Can't find completion for \"%s\"" pattern)
3820 ((not (string= pattern completion))
3821 (delete-region beg end)
3822 (if (string-match " +$" completion)
3823 (setq completion (replace-match "" t t completion)))
3825 (if (get-buffer-window "*Completions*")
3826 (delete-window (get-buffer-window "*Completions*")))
3827 (if (assoc completion table)
3828 (if (eq type :todo) (insert " ")
3829 (if (eq type :tag) (insert ":"))))
3830 (if (and (equal type :opt) (assoc completion table))
3831 (message "%s" (substitute-command-keys
3832 "Press \\[org-complete] again to insert example settings"))))
3834 (message "Making completion list...")
3835 (let ((list (sort (all-completions pattern table) 'string<)))
3836 (with-output-to-temp-buffer "*Completions*"
3838 ;; Protection needed for XEmacs and emacs 21
3839 (display-completion-list list pattern)
3840 (error (display-completion-list list)))))
3841 (message "Making completion list...%s" "done"))))))
3843 ;;; Comments, TODO and DEADLINE
3845 (defun org-toggle-comment ()
3846 "Change the COMMENT state of an entry."
3849 (org-back-to-heading)
3850 (if (looking-at (concat outline-regexp
3851 "\\( +\\<" org-comment-string "\\>\\)"))
3852 (replace-match "" t t nil 1)
3853 (if (looking-at outline-regexp)
3855 (goto-char (match-end 0))
3856 (insert " " org-comment-string))))))
3858 (defvar org-last-todo-state-is-todo nil
3859 "This is non-nil when the last TODO state change led to a TODO state.
3860 If the last change removed the TODO tag or switched to DONE, then
3863 (defun org-todo (&optional arg)
3864 "Change the TODO state of an item.
3865 The state of an item is given by a keyword at the start of the heading,
3867 *** TODO Write paper
3870 The different keywords are specified in the variable `org-todo-keywords'.
3871 By default the available states are \"TODO\" and \"DONE\".
3872 So for this example: when the item starts with TODO, it is changed to DONE.
3873 When it starts with DONE, the DONE is removed. And when neither TODO nor
3874 DONE are present, add TODO at the beginning of the heading.
3876 With prefix arg, use completion to determine the new state. With numeric
3877 prefix arg, switch to that state."
3880 (org-back-to-heading)
3881 (if (looking-at outline-regexp) (goto-char (match-end 0)))
3882 (or (looking-at (concat " +" org-todo-regexp " *"))
3884 (let* ((this (match-string 1))
3885 (completion-ignore-case t)
3886 (member (member this org-todo-keywords))
3890 ;; Read a state with completion
3891 (completing-read "State: " (mapcar (lambda(x) (list x))
3896 (if tail (car tail) nil)
3897 (car org-todo-keywords)))
3899 (if (equal member org-todo-keywords)
3902 (nth (- (length org-todo-keywords) (length tail) 2)
3906 ;; user requests a specific state
3907 (nth (1- (prefix-numeric-value arg))
3909 ((null member) (car org-todo-keywords))
3910 ((null tail) nil) ;; -> first entry
3911 ((eq org-todo-interpretation 'sequence)
3913 ((memq org-todo-interpretation '(type priority))
3914 (if (eq this-command last-command)
3916 (if (> (length tail) 0) org-done-string nil)))
3918 (next (if state (concat " " state " ") " ")))
3919 (replace-match next t t)
3920 (setq org-last-todo-state-is-todo
3921 (not (equal state org-done-string)))
3923 (if (equal state org-done-string)
3927 ;; Fixup tag positioning
3928 (and org-auto-align-tags (org-set-tags nil t))
3929 (run-hooks 'org-after-todo-state-change-hook)))
3930 ;; Fixup cursor location if close to the keyword
3931 (if (and (outline-on-heading-p)
3933 (save-excursion (beginning-of-line 1)
3934 (looking-at org-todo-line-regexp))
3935 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
3937 (goto-char (or (match-end 2) (match-end 1)))
3940 (defun org-log-done (&optional undone)
3941 "Add a time stamp logging that a TODO entry has been closed.
3942 When UNDONE is non-nil, remove such a time stamp again."
3946 (org-back-to-heading t)
3948 (looking-at (concat outline-regexp " *"))
3949 (goto-char (match-end 0))
3950 (setq col (current-column))
3951 (outline-next-heading)
3954 (when (re-search-forward (concat
3956 (regexp-quote org-closed-string)
3957 " *\\[.*?\\][^\n\r]*[\n\r]?\\)") end t)
3958 (delete-region (match-beginning 1) (match-end 1)))
3960 (org-back-to-heading t)
3961 (skip-chars-forward "^\n\r")
3962 (goto-char (min (1+ (point)) (point-max)))
3963 (when (not (member (char-before) '(?\r ?\n)))
3966 (insert org-closed-string " "
3968 (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
3972 (defun org-show-todo-tree (arg)
3973 "Make a compact tree which shows all headlines marked with TODO.
3974 The tree will show the lines where the regexp matches, and all higher
3975 headlines above the match.
3976 With \\[universal-argument] prefix, also show the DONE entries.
3977 With a numeric prefix N, construct a sparse tree for the Nth element
3978 of `org-todo-keywords'."
3980 (let ((case-fold-search nil)
3982 (cond ((null arg) org-not-done-regexp)
3983 ((equal arg '(4)) org-todo-regexp)
3984 ((<= (prefix-numeric-value arg) (length org-todo-keywords))
3985 (regexp-quote (nth (1- (prefix-numeric-value arg))
3986 org-todo-keywords)))
3987 (t (error "Invalid prefix argument: %s" arg)))))
3988 (message "%d TODO entries found"
3989 (org-occur (concat "^" outline-regexp " +" kwd-re )))))
3991 (defun org-deadline ()
3992 "Insert the DEADLINE: string to make a deadline.
3993 A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
3994 to modify it to the correct date."
3997 org-deadline-string " "
3998 (format-time-string (car org-time-stamp-formats)
3999 (org-read-date nil 'to-time)))
4000 (message "%s" (substitute-command-keys
4001 "Use \\[org-timestamp-up-day] and \\[org-timestamp-down-day] to change the date.")))
4003 (defun org-schedule ()
4004 "Insert the SCHEDULED: string to schedule a TODO item.
4005 A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
4006 to modify it to the correct date."
4009 org-scheduled-string " "
4010 (format-time-string (car org-time-stamp-formats)
4011 (org-read-date nil 'to-time)))
4012 (message "%s" (substitute-command-keys
4013 "Use \\[org-timestamp-up-day] and \\[org-timestamp-down-day] to change the date.")))
4016 (defun org-occur (regexp &optional callback)
4017 "Make a compact tree which shows all matches of REGEXP.
4018 The tree will show the lines where the regexp matches, and all higher
4019 headlines above the match. It will also show the heading after the match,
4020 to make sure editing the matching entry is easy.
4021 If CALLBACK is non-nil, it is a function which is called to confirm
4022 that the match should indeed be shown."
4023 (interactive "sRegexp: ")
4024 (org-remove-occur-highlights nil nil t)
4025 (setq regexp (org-check-occur-regexp regexp))
4028 (goto-char (point-min))
4030 (while (re-search-forward regexp nil t)
4031 (when (or (not callback)
4032 (save-match-data (funcall callback)))
4034 (org-highlight-new-match (match-beginning 0) (match-end 0))
4035 (org-show-hierarchy-above))))
4036 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
4038 (run-hooks 'org-occur-hook)
4040 (message "%d match(es) for regexp %s" cnt regexp))
4043 (defun org-show-hierarchy-above ()
4044 "Make sure point and the headings hierarchy above is visible."
4046 (if (org-on-heading-p t)
4047 (org-flag-heading nil) ; only show the heading
4048 (and (or (org-invisible-p) (org-invisible-p2))
4049 (org-show-hidden-entry))) ; show entire entry
4051 (and org-show-following-heading
4052 (outline-next-heading)
4053 (org-flag-heading nil))) ; show the next heading
4054 (when org-show-hierarchy-above
4055 (save-excursion ; show all higher headings
4056 (while (and (condition-case nil
4057 (progn (org-up-heading-all 1) t)
4060 (org-flag-heading nil))))))
4062 ;; Overlay compatibility functions
4063 (defun org-make-overlay (beg end &optional buffer)
4064 (if (featurep 'xemacs)
4065 (make-extent beg end buffer)
4066 (make-overlay beg end buffer)))
4067 (defun org-delete-overlay (ovl)
4068 (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
4069 (defun org-detatch-overlay (ovl)
4070 (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
4071 (defun org-move-overlay (ovl beg end &optional buffer)
4072 (if (featurep 'xemacs)
4073 (set-extent-endpoints ovl beg end buffer)
4074 (move-overlay ovl beg end buffer)))
4075 (defun org-overlay-put (ovl prop value)
4076 (if (featurep 'xemacs)
4077 (set-extent-property ovl prop value)
4078 (overlay-put ovl prop value)))
4080 (defvar org-occur-highlights nil)
4081 (defun org-highlight-new-match (beg end)
4082 "Highlight from BEG to END and mark the highlight is an occur headline."
4083 (let ((ov (org-make-overlay beg end)))
4084 (org-overlay-put ov 'face 'secondary-selection)
4085 (push ov org-occur-highlights)))
4087 (defun org-remove-occur-highlights (&optional beg end noremove)
4088 "Remove the occur highlights from the buffer.
4089 BEG and END are ignored. If NOREMOVE is nil, remove this function
4090 from the `before-change-functions' in the current buffer."
4092 (mapc 'org-delete-overlay org-occur-highlights)
4093 (setq org-occur-highlights nil)
4095 (remove-hook 'before-change-functions
4096 'org-remove-occur-highlights 'local)))
4100 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
4101 "Regular expression matching the priority indicator.")
4103 (defvar org-remove-priority-next-time nil)
4105 (defun org-priority-up ()
4106 "Increase the priority of the current item."
4110 (defun org-priority-down ()
4111 "Decrease the priority of the current item."
4113 (org-priority 'down))
4115 (defun org-priority (&optional action)
4116 "Change the priority of an item by ARG.
4117 ACTION can be set, up, or down."
4119 (setq action (or action 'set))
4120 (let (current new news have remove)
4122 (org-back-to-heading)
4123 (if (looking-at org-priority-regexp)
4124 (setq current (string-to-char (match-string 2))
4126 (setq current org-default-priority))
4129 (message "Priority A-%c, SPC to remove: " org-lowest-priority)
4130 (setq new (read-char-exclusive))
4131 (cond ((equal new ?\ ) (setq remove t))
4132 ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
4133 (error "Priority must be between `%c' and `%c'"
4134 ?A org-lowest-priority))))
4136 (setq new (1- current)))
4138 (setq new (1+ current)))
4139 (t (error "Invalid action")))
4140 (setq new (min (max ?A (upcase new)) org-lowest-priority))
4141 (setq news (format "%c" new))
4144 (replace-match "" t t nil 1)
4145 (replace-match news t t nil 2))
4147 (error "No priority cookie found in line")
4148 (looking-at org-todo-line-regexp)
4151 (goto-char (match-end 2))
4152 (insert " [#" news "]"))
4153 (goto-char (match-beginning 3))
4154 (insert "[#" news "] ")))))
4156 (message "Priority removed")
4157 (message "Priority of current item set to %s" news))))
4160 (defun org-get-priority (s)
4161 "Find priority cookie and return priority."
4163 (if (not (string-match org-priority-regexp s))
4164 (* 1000 (- org-lowest-priority org-default-priority))
4165 (* 1000 (- org-lowest-priority
4166 (string-to-char (match-string 2 s)))))))
4170 (defvar org-last-changed-timestamp nil)
4172 (defun org-time-stamp (arg)
4173 "Prompt for a date/time and insert a time stamp.
4174 If the user specifies a time like HH:MM, or if this command is called
4175 with a prefix argument, the time stamp will contain date and time.
4176 Otherwise, only the date will be included. All parts of a date not
4177 specified by the user will be filled in from the current date/time.
4178 So if you press just return without typing anything, the time stamp
4179 will represent the current date/time. If there is already a timestamp
4180 at the cursor, it will be modified."
4182 (let ((fmt (if arg (cdr org-time-stamp-formats)
4183 (car org-time-stamp-formats)))
4184 (org-time-was-given nil)
4187 ((and (org-at-timestamp-p)
4188 (eq last-command 'org-time-stamp)
4189 (eq this-command 'org-time-stamp))
4191 (setq time (let ((this-command this-command))
4192 (org-read-date arg 'totime)))
4193 (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
4194 (insert (format-time-string fmt time)))
4195 ((org-at-timestamp-p)
4196 (setq time (let ((this-command this-command))
4197 (org-read-date arg 'totime)))
4198 (and (org-at-timestamp-p) (replace-match
4199 (setq org-last-changed-timestamp
4200 (format-time-string fmt time))
4202 (message "Timestamp updated"))
4204 (setq time (let ((this-command this-command))
4205 (org-read-date arg 'totime)))
4206 (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
4207 (insert (format-time-string fmt time))))))
4209 (defun org-time-stamp-inactive (&optional arg)
4210 "Insert an inactive time stamp.
4211 An inactive time stamp is enclosed in square brackets instead of angle
4212 brackets. It is inactive in the sense that it does not trigger agenda entries,
4213 does not link to the calendar and cannot be changed with the S-cursor keys.
4214 So these are more for recording a certain time/date."
4216 (let ((fmt (if arg (cdr org-time-stamp-formats)
4217 (car org-time-stamp-formats)))
4218 (org-time-was-given nil)
4220 (setq time (org-read-date arg 'totime))
4221 (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
4222 (setq fmt (concat "[" (substring fmt 1 -1) "]"))
4223 (insert (format-time-string fmt time))))
4225 (defvar org-date-ovl (org-make-overlay 1 1))
4226 (org-overlay-put org-date-ovl 'face 'org-warning)
4227 (org-detatch-overlay org-date-ovl)
4229 (defun org-read-date (&optional with-time to-time)
4230 "Read a date and make things smooth for the user.
4231 The prompt will suggest to enter an ISO date, but you can also enter anything
4232 which will at least partially be understood by `parse-time-string'.
4233 Unrecognized parts of the date will default to the current day, month, year,
4234 hour and minute. For example,
4235 3-2-5 --> 2003-02-05
4236 feb 15 --> currentyear-02-15
4237 sep 12 9 --> 2009-09-12
4238 12:45 --> today 12:45
4239 22 sept 0:34 --> currentyear-09-22 0:34
4240 12 --> currentyear-currentmonth-12
4241 Fri --> nearest Friday (today or later)
4243 The function understands only English month and weekday abbreviations,
4244 but this can be configured with the variables `parse-time-months' and
4245 `parse-time-weekdays'.
4247 While prompting, a calendar is popped up - you can also select the
4248 date with the mouse (button 1). The calendar shows a period of three
4249 months. To scroll it to other months, use the keys `>' and `<'.
4250 If you don't like the calendar, turn it off with
4251 \(setq org-popup-calendar-for-date-prompt nil)
4253 With optional argument TO-TIME, the date will immediately be converted
4254 to an internal time.
4255 With an optional argument WITH-TIME, the prompt will suggest to also
4256 insert a time. Note that when WITH-TIME is not set, you can still
4257 enter a time, and this function will inform the calling routine about
4258 this change. The calling routine may then choose to change the format
4259 used to insert the time stamp into the buffer to include the time."
4260 (require 'parse-time)
4261 (let* ((org-time-stamp-rounding-minutes
4262 (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
4263 (ct (org-current-time))
4265 ;; Default time is either today, or, when entering a range,
4269 (concat org-ts-regexp "--\\=") ; FIXME: exactly two minuses?
4273 (mapcar (lambda(x) (or x 0))
4274 (parse-time-string (match-string 1))))
4276 (calendar-move-hook nil)
4277 (view-diary-entries-initially nil)
4278 (timestr (format-time-string
4279 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
4280 (prompt (format "YYYY-MM-DD [%s]: " timestr))
4282 second minute hour day month year tl wday wday1)
4284 (if org-popup-calendar-for-date-prompt
4286 (save-window-excursion
4288 (calendar-forward-day (- (time-to-days default-time)
4289 (calendar-absolute-from-gregorian
4290 (calendar-current-date))))
4291 (org-eval-in-calendar nil)
4292 (let* ((old-map (current-local-map))
4293 (map (copy-keymap calendar-mode-map))
4294 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
4295 (define-key map (kbd "RET") 'org-calendar-select)
4296 (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
4297 'org-calendar-select-mouse)
4298 (define-key map (if (featurep 'xemacs) [button2] [mouse-2
])
4299 'org-calendar-select-mouse
)
4300 (define-key minibuffer-local-map
[(meta shift left
)]
4301 (lambda () (interactive)
4302 (org-eval-in-calendar '(calendar-backward-month 1))))
4303 (define-key minibuffer-local-map
[(meta shift right
)]
4304 (lambda () (interactive)
4305 (org-eval-in-calendar '(calendar-forward-month 1))))
4306 (define-key minibuffer-local-map
[(shift up
)]
4307 (lambda () (interactive)
4308 (org-eval-in-calendar '(calendar-backward-week 1))))
4309 (define-key minibuffer-local-map
[(shift down
)]
4310 (lambda () (interactive)
4311 (org-eval-in-calendar '(calendar-forward-week 1))))
4312 (define-key minibuffer-local-map
[(shift left
)]
4313 (lambda () (interactive)
4314 (org-eval-in-calendar '(calendar-backward-day 1))))
4315 (define-key minibuffer-local-map
[(shift right
)]
4316 (lambda () (interactive)
4317 (org-eval-in-calendar '(calendar-forward-day 1))))
4318 (define-key minibuffer-local-map
">"
4319 (lambda () (interactive)
4320 (org-eval-in-calendar '(scroll-calendar-left 1))))
4321 (define-key minibuffer-local-map
"<"
4322 (lambda () (interactive)
4323 (org-eval-in-calendar '(scroll-calendar-right 1))))
4327 (setq ans
(read-string prompt
"" nil nil
))
4328 (if (not (string-match "\\S-" ans
)) (setq ans nil
))
4329 (setq ans
(or ans1 ans ans2
)))
4330 (use-local-map old-map
)))))
4331 ;; Naked prompt only
4332 (setq ans
(read-string prompt
"" nil timestr
)))
4333 (org-detatch-overlay org-date-ovl
)
4336 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
4338 (setq year
(if (match-end 2)
4339 (string-to-number (match-string 2 ans
))
4340 (string-to-number (format-time-string "%Y")))
4341 month
(string-to-number (match-string 3 ans
))
4342 day
(string-to-number (match-string 4 ans
)))
4343 (if (< year
100) (setq year
(+ 2000 year
)))
4344 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
4346 (setq tl
(parse-time-string ans
)
4347 year
(or (nth 5 tl
) (string-to-number (format-time-string "%Y" ct
)))
4348 month
(or (nth 4 tl
) (string-to-number (format-time-string "%m" ct
)))
4349 day
(or (nth 3 tl
) (string-to-number (format-time-string "%d" ct
)))
4350 hour
(or (nth 2 tl
) (string-to-number (format-time-string "%H" ct
)))
4351 minute
(or (nth 1 tl
) (string-to-number (format-time-string "%M" ct
)))
4352 second
(or (nth 0 tl
) 0)
4354 (when (and wday
(not (nth 3 tl
)))
4355 ;; Weekday was given, but no day, so pick that day in the week
4356 ;; on or after the derived date.
4357 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
4358 (unless (equal wday wday1
)
4359 (setq day
(+ day
(%
(- wday wday1 -
7) 7)))))
4360 (if (and (boundp 'org-time-was-given
)
4362 (setq org-time-was-given t
))
4363 (if (< year
100) (setq year
(+ 2000 year
)))
4365 (encode-time second minute hour day month year
)
4366 (if (or (nth 1 tl
) (nth 2 tl
))
4367 (format "%04d-%02d-%02d %02d:%02d" year month day hour minute
)
4368 (format "%04d-%02d-%02d" year month day
)))))
4370 (defun org-eval-in-calendar (form)
4371 "Eval FORM in the calendar window and return to current window.
4372 Also, store the cursor date in variable ans2."
4373 (let ((sw (selected-window)))
4374 (select-window (get-buffer-window "*Calendar*"))
4376 (when (calendar-cursor-to-date)
4377 (let* ((date (calendar-cursor-to-date))
4378 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
4379 (setq ans2
(format-time-string "%Y-%m-%d" time
))))
4380 (org-move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
4381 (select-window sw
)))
4383 (defun org-calendar-select ()
4384 "Return to `org-read-date' with the date currently selected.
4385 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
4387 (when (calendar-cursor-to-date)
4388 (let* ((date (calendar-cursor-to-date))
4389 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
4390 (setq ans1
(format-time-string "%Y-%m-%d" time
)))
4391 (if (active-minibuffer-window) (exit-minibuffer))))
4393 (defun org-calendar-select-mouse (ev)
4394 "Return to `org-read-date' with the date currently selected.
4395 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
4397 (mouse-set-point ev
)
4398 (when (calendar-cursor-to-date)
4399 (let* ((date (calendar-cursor-to-date))
4400 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
4401 (setq ans1
(format-time-string "%Y-%m-%d" time
)))
4402 (if (active-minibuffer-window) (exit-minibuffer))))
4404 (defun org-check-deadlines (ndays)
4405 "Check if there are any deadlines due or past due.
4406 A deadline is considered due if it happens within `org-deadline-warning-days'
4407 days from today's date. If the deadline appears in an entry marked DONE,
4408 it is not shown. The prefix arg NDAYS can be used to test that many
4409 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
4411 (let* ((org-warn-days
4413 ((equal ndays
'(4)) 100000)
4414 (ndays (prefix-numeric-value ndays
))
4415 (t org-deadline-warning-days
)))
4416 (case-fold-search nil
)
4417 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
4420 (and (let ((d1 (time-to-days (current-time)))
4422 (org-time-string-to-time (match-string 1)))))
4423 (< (- d2 d1
) org-warn-days
))
4424 (not (org-entry-is-done-p))))))
4425 (message "%d deadlines past-due or due within %d days"
4426 (org-occur regexp callback
)
4429 (defun org-evaluate-time-range (&optional to-buffer
)
4430 "Evaluate a time range by computing the difference between start and end.
4431 Normally the result is just printed in the echo area, but with prefix arg
4432 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
4433 If the time range is actually in a table, the result is inserted into the
4435 For time difference computation, a year is assumed to be exactly 365
4436 days in order to avoid rounding problems."
4439 (unless (org-at-date-range-p)
4440 (goto-char (point-at-bol))
4441 (re-search-forward org-tr-regexp
(point-at-eol) t
))
4442 (if (not (org-at-date-range-p))
4443 (error "Not at a time-stamp range, and none found in current line")))
4444 (let* ((ts1 (match-string 1))
4445 (ts2 (match-string 2))
4446 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
4447 (match-end (match-end 0))
4448 (time1 (org-time-string-to-time ts1
))
4449 (time2 (org-time-string-to-time ts2
))
4450 (t1 (time-to-seconds time1
))
4451 (t2 (time-to-seconds time2
))
4452 (diff (abs (- t2 t1
)))
4453 (negative (< (- t2 t1
) 0))
4454 ;; (ys (floor (* 365 24 60 60)))
4457 (fy "%dy %dd %02d:%02d")
4459 (fd "%dd %02d:%02d")
4464 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
4466 d
(floor (/ diff ds
)) diff
(mod diff ds
)
4467 h
(floor (/ diff hs
)) diff
(mod diff hs
)
4468 m
(floor (/ diff
60)))
4469 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
4471 d
(floor (+ (/ diff ds
) 0.5))
4474 (message (org-make-tdiff-string y d h m
))
4475 (when (org-at-table-p)
4476 (goto-char match-end
)
4478 (and (looking-at " *|") (goto-char (match-end 0))))
4480 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
4482 (if negative
(insert " -"))
4483 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
4484 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
4485 (insert " " (format fh h m
))))
4486 (if align
(org-table-align))
4487 (message "Time difference inserted"))))
4489 (defun org-make-tdiff-string (y d h m
)
4492 (if (> y
0) (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " ")
4494 (if (> d
0) (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " ")
4496 (if (> h
0) (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " ")
4498 (if (> m
0) (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " ")
4500 (apply 'format fmt
(nreverse l
))))
4502 (defun org-time-string-to-time (s)
4503 (apply 'encode-time
(org-parse-time-string s
)))
4505 (defun org-parse-time-string (s &optional nodefault
)
4506 "Parse the standard Org-mode time string.
4507 This should be a lot faster than the normal `parse-time-string'.
4508 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
4509 hour and minute fields will be nil if not given."
4510 (if (string-match org-ts-regexp1 s
)
4512 (if (or (match-beginning 8) (not nodefault
))
4513 (string-to-number (or (match-string 8 s
) "0")))
4514 (if (or (match-beginning 7) (not nodefault
))
4515 (string-to-number (or (match-string 7 s
) "0")))
4516 (string-to-number (match-string 4 s
))
4517 (string-to-number (match-string 3 s
))
4518 (string-to-number (match-string 2 s
))
4522 (defun org-timestamp-up (&optional arg
)
4523 "Increase the date item at the cursor by one.
4524 If the cursor is on the year, change the year. If it is on the month or
4525 the day, change that.
4526 With prefix ARG, change by that many units."
4528 (org-timestamp-change (prefix-numeric-value arg
)))
4530 (defun org-timestamp-down (&optional arg
)
4531 "Decrease the date item at the cursor by one.
4532 If the cursor is on the year, change the year. If it is on the month or
4533 the day, change that.
4534 With prefix ARG, change by that many units."
4536 (org-timestamp-change (- (prefix-numeric-value arg
))))
4538 (defun org-timestamp-up-day (&optional arg
)
4539 "Increase the date in the time stamp by one day.
4540 With prefix ARG, change that many days."
4542 (if (and (not (org-at-timestamp-p))
4545 (org-timestamp-change (prefix-numeric-value arg
) 'day
)))
4547 (defun org-timestamp-down-day (&optional arg
)
4548 "Decrease the date in the time stamp by one day.
4549 With prefix ARG, change that many days."
4551 (if (and (not (org-at-timestamp-p))
4554 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
)))
4556 (defsubst org-pos-in-match-range
(pos n
)
4557 (and (match-beginning n
)
4558 (<= (match-beginning n
) pos
)
4559 (>= (match-end n
) pos
)))
4561 (defun org-at-timestamp-p ()
4562 "Determine if the cursor is in or at a timestamp."
4564 (let* ((tsr org-ts-regexp2
)
4566 (ans (or (looking-at tsr
)
4568 (skip-chars-backward "^<\n\r\t")
4569 (if (> (point) 1) (backward-char 1))
4570 (and (looking-at tsr
)
4571 (> (- (match-end 0) pos
) -
1))))))
4572 (and (boundp 'org-ts-what
)
4575 ((org-pos-in-match-range pos
2) 'year
)
4576 ((org-pos-in-match-range pos
3) 'month
)
4577 ((org-pos-in-match-range pos
7) 'hour
)
4578 ((org-pos-in-match-range pos
8) 'minute
)
4579 ((or (org-pos-in-match-range pos
4)
4580 (org-pos-in-match-range pos
5)) 'day
)
4584 (defun org-timestamp-change (n &optional what
)
4585 "Change the date in the time stamp at point.
4586 The date will be changed by N times WHAT. WHAT can be `day', `month',
4587 `year', `minute', `second'. If WHAT is not given, the cursor position
4588 in the timestamp determines what will be changed."
4589 (let ((fmt (car org-time-stamp-formats
))
4593 (if (not (org-at-timestamp-p))
4594 (error "Not at a timestamp"))
4595 (setq org-ts-what
(or what org-ts-what
))
4596 (setq fmt
(if (<= (abs (- (cdr org-ts-lengths
)
4597 (- (match-end 0) (match-beginning 0))))
4599 (cdr org-time-stamp-formats
)
4600 (car org-time-stamp-formats
)))
4601 (setq ts
(match-string 0))
4603 (setq time0
(org-parse-time-string ts
))
4607 (list (or (car time0
) 0))
4608 (list (+ (if (eq org-ts-what
'minute
) n
0) (nth 1 time0
)))
4609 (list (+ (if (eq org-ts-what
'hour
) n
0) (nth 2 time0
)))
4610 (list (+ (if (eq org-ts-what
'day
) n
0) (nth 3 time0
)))
4611 (list (+ (if (eq org-ts-what
'month
) n
0) (nth 4 time0
)))
4612 (list (+ (if (eq org-ts-what
'year
) n
0) (nth 5 time0
)))
4614 (if (eq what
'calendar
)
4618 (set-buffer "*Calendar*")
4619 (calendar-cursor-to-date)))))
4620 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
4621 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
4622 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
4623 (setcar time0
(or (car time0
) 0))
4624 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
4625 (setcar (nthcdr 2 time0
) (or (nth 1 time0
) 0))
4626 (setq time
(apply 'encode-time time0
))))
4627 (insert (setq org-last-changed-timestamp
(format-time-string fmt time
)))
4629 ;; Try to recenter the calendar window, if any
4630 (if (and org-calendar-follow-timestamp-change
4631 (get-buffer-window "*Calendar*" t
)
4632 (memq org-ts-what
'(day month year
)))
4633 (org-recenter-calendar (time-to-days time
)))))
4635 (defun org-recenter-calendar (date)
4636 "If the calendar is visible, recenter it to DATE."
4637 (let* ((win (selected-window))
4638 (cwin (get-buffer-window "*Calendar*" t
))
4639 (calendar-move-hook nil
))
4641 (select-window cwin
)
4642 (calendar-goto-date (if (listp date
) date
4643 (calendar-gregorian-from-absolute date
)))
4644 (select-window win
))))
4646 (defun org-goto-calendar (&optional arg
)
4647 "Go to the Emacs calendar at the current date.
4648 If there is a time stamp in the current line, go to that date.
4649 A prefix ARG can be used to force the current date."
4651 (let ((tsr org-ts-regexp
) diff
4652 (calendar-move-hook nil
)
4653 (view-diary-entries-initially nil
))
4654 (if (or (org-at-timestamp-p)
4656 (beginning-of-line 1)
4657 (looking-at (concat ".*" tsr
))))
4658 (let ((d1 (time-to-days (current-time)))
4660 (org-time-string-to-time (match-string 1)))))
4661 (setq diff
(- d2 d1
))))
4663 (calendar-goto-today)
4664 (if (and diff
(not arg
)) (calendar-forward-day diff
))))
4666 (defun org-date-from-calendar ()
4667 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
4668 If there is already a time stamp at the cursor position, update it."
4670 (org-timestamp-change 0 'calendar
))
4672 ;;; Agenda, and Diary Integration
4676 (defvar org-agenda-mode-map
(make-sparse-keymap)
4677 "Keymap for `org-agenda-mode'.")
4679 (defvar org-agenda-menu
) ; defined later in this file.
4680 (defvar org-agenda-follow-mode nil
)
4681 (defvar org-agenda-show-log nil
)
4682 (defvar org-agenda-buffer-name
"*Org Agenda*")
4683 (defvar org-agenda-redo-command nil
)
4684 (defvar org-agenda-mode-hook nil
)
4685 (defvar org-agenda-type nil
)
4686 (defvar org-agenda-force-single-file nil
)
4689 (defun org-agenda-mode ()
4690 "Mode for time-sorted view on action items in Org-mode files.
4692 The following commands are available:
4694 \\{org-agenda-mode-map}"
4696 (kill-all-local-variables)
4697 (setq major-mode
'org-agenda-mode
)
4698 (setq mode-name
"Org-Agenda")
4699 (use-local-map org-agenda-mode-map
)
4700 (easy-menu-add org-agenda-menu
)
4701 (if org-startup-truncated
(setq truncate-lines t
))
4702 (org-add-hook 'post-command-hook
'org-agenda-post-command-hook nil
'local
)
4703 (org-add-hook 'pre-command-hook
'org-unhighlight nil
'local
)
4704 (unless org-agenda-keep-modes
4705 (setq org-agenda-follow-mode nil
4706 org-agenda-show-log nil
))
4708 '("Agenda") "Agenda Files"
4712 (if (get 'org-agenda-files
'org-restrict
)
4713 "Restricted to single file"
4715 '(org-edit-agenda-file-list)
4716 (not (get 'org-agenda-files
'org-restrict
)))
4718 (mapcar 'org-file-menu-entry
(org-agenda-files))))
4719 (org-agenda-set-mode-name)
4721 (if (fboundp 'run-mode-hooks
) 'run-mode-hooks
'run-hooks
)
4722 (list 'org-agenda-mode-hook
)))
4724 (define-key org-agenda-mode-map
"\C-i" 'org-agenda-goto
)
4725 (define-key org-agenda-mode-map
[(tab)] 'org-agenda-goto
)
4726 (define-key org-agenda-mode-map
"\C-m" 'org-agenda-switch-to
)
4727 (define-key org-agenda-mode-map
" " 'org-agenda-show
)
4728 (define-key org-agenda-mode-map
"\C-c\C-t" 'org-agenda-todo
)
4729 (define-key org-agenda-mode-map
"o" 'delete-other-windows
)
4730 (define-key org-agenda-mode-map
"L" 'org-agenda-recenter
)
4731 (define-key org-agenda-mode-map
"t" 'org-agenda-todo
)
4732 (define-key org-agenda-mode-map
":" 'org-agenda-set-tags
)
4733 (define-key org-agenda-mode-map
"." 'org-agenda-goto-today
)
4734 (define-key org-agenda-mode-map
"d" 'org-agenda-day-view
)
4735 (define-key org-agenda-mode-map
"w" 'org-agenda-week-view
)
4736 (define-key org-agenda-mode-map
(org-key 'S-right
) 'org-agenda-date-later
)
4737 (define-key org-agenda-mode-map
(org-key 'S-left
) 'org-agenda-date-earlier
)
4738 (define-key org-agenda-mode-map
[?\C-c ?\C-x
(right)] 'org-agenda-date-later
)
4739 (define-key org-agenda-mode-map
[?\C-c ?\C-x
(left)] 'org-agenda-date-earlier
)
4741 (define-key org-agenda-mode-map
">" 'org-agenda-date-prompt
)
4742 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
4743 (while l
(define-key org-agenda-mode-map
4744 (int-to-string (pop l
)) 'digit-argument
)))
4746 (define-key org-agenda-mode-map
"f" 'org-agenda-follow-mode
)
4747 (define-key org-agenda-mode-map
"l" 'org-agenda-log-mode
)
4748 (define-key org-agenda-mode-map
"D" 'org-agenda-toggle-diary
)
4749 (define-key org-agenda-mode-map
"g" 'org-agenda-toggle-time-grid
)
4750 (define-key org-agenda-mode-map
"r" 'org-agenda-redo
)
4751 (define-key org-agenda-mode-map
"q" 'org-agenda-quit
)
4752 (define-key org-agenda-mode-map
"x" 'org-agenda-exit
)
4753 (define-key org-agenda-mode-map
"P" 'org-agenda-show-priority
)
4754 (define-key org-agenda-mode-map
"T" 'org-agenda-show-tags
)
4755 (define-key org-agenda-mode-map
"n" 'next-line
)
4756 (define-key org-agenda-mode-map
"p" 'previous-line
)
4757 (define-key org-agenda-mode-map
"\C-n" 'org-agenda-next-date-line
)
4758 (define-key org-agenda-mode-map
"\C-p" 'org-agenda-previous-date-line
)
4759 (define-key org-agenda-mode-map
"," 'org-agenda-priority
)
4760 (define-key org-agenda-mode-map
"\C-c," 'org-agenda-priority
)
4761 (define-key org-agenda-mode-map
"i" 'org-agenda-diary-entry
)
4762 (define-key org-agenda-mode-map
"c" 'org-agenda-goto-calendar
)
4763 (eval-after-load "calendar"
4764 '(define-key calendar-mode-map org-calendar-to-agenda-key
4765 'org-calendar-goto-agenda
))
4766 (define-key org-agenda-mode-map
"C" 'org-agenda-convert-date
)
4767 (define-key org-agenda-mode-map
"m" 'org-agenda-phases-of-moon
)
4768 (define-key org-agenda-mode-map
"M" 'org-agenda-phases-of-moon
)
4769 (define-key org-agenda-mode-map
"s" 'org-agenda-sunrise-sunset
)
4770 (define-key org-agenda-mode-map
"S" 'org-agenda-sunrise-sunset
)
4771 (define-key org-agenda-mode-map
"h" 'org-agenda-holidays
)
4772 (define-key org-agenda-mode-map
"H" 'org-agenda-holidays
)
4773 (define-key org-agenda-mode-map
"+" 'org-agenda-priority-up
)
4774 (define-key org-agenda-mode-map
"-" 'org-agenda-priority-down
)
4775 (define-key org-agenda-mode-map
(org-key 'S-up
) 'org-agenda-priority-up
)
4776 (define-key org-agenda-mode-map
(org-key 'S-down
) 'org-agenda-priority-down
)
4777 (define-key org-agenda-mode-map
[?\C-c ?\C-x
(up)] 'org-agenda-priority-up
)
4778 (define-key org-agenda-mode-map
[?\C-c ?\C-x
(down)] 'org-agenda-priority-down
)
4779 (define-key org-agenda-mode-map
[(right)] 'org-agenda-later
)
4780 (define-key org-agenda-mode-map
[(left)] 'org-agenda-earlier
)
4781 (define-key org-agenda-mode-map
"\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files
)
4782 (defvar org-agenda-keymap
(copy-keymap org-agenda-mode-map
)
4783 "Local keymap for agenda entries from Org-mode.")
4785 (define-key org-agenda-keymap
4786 (if (featurep 'xemacs
) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse
)
4787 (define-key org-agenda-keymap
4788 (if (featurep 'xemacs
) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse
)
4789 (when org-agenda-mouse-1-follows-link
4790 (define-key org-agenda-keymap
[follow-link
] 'mouse-face
))
4791 (easy-menu-define org-agenda-menu org-agenda-mode-map
"Agenda menu"
4795 ["Show" org-agenda-show t
]
4796 ["Go To (other window)" org-agenda-goto t
]
4797 ["Go To (one window)" org-agenda-switch-to t
]
4798 ["Follow Mode" org-agenda-follow-mode
4799 :style toggle
:selected org-agenda-follow-mode
:active t
]
4801 ["Cycle TODO" org-agenda-todo t
]
4803 ["Show all Tags" org-agenda-show-tags t
]
4804 ["Set Tags" org-agenda-set-tags t
])
4806 ["Reschedule +1 day" org-agenda-date-later
(org-agenda-check-type nil
'agenda
'timeline
)]
4807 ["Reschedule -1 day" org-agenda-date-earlier
(org-agenda-check-type nil
'agenda
'timeline
)]
4809 ["Reschedule to ..." org-agenda-date-prompt
(org-agenda-check-type nil
'agenda
'timeline
)])
4811 ["Set Priority" org-agenda-priority t
]
4812 ["Increase Priority" org-agenda-priority-up t
]
4813 ["Decrease Priority" org-agenda-priority-down t
]
4814 ["Show Priority" org-agenda-show-priority t
])
4816 ;; ["New agenda command" org-agenda t]
4817 ["Rebuild buffer" org-agenda-redo t
]
4819 ["Goto Today" org-agenda-goto-today
(org-agenda-check-type nil
'agenda
'timeline
)]
4820 ["Next Dates" org-agenda-later
(org-agenda-check-type nil
'agenda
)]
4821 ["Previous Dates" org-agenda-earlier
(org-agenda-check-type nil
'agenda
)]
4823 ["Day View" org-agenda-day-view
:active
(org-agenda-check-type nil
'agenda
)
4824 :style radio
:selected
(equal org-agenda-ndays
1)]
4825 ["Week View" org-agenda-week-view
:active
(org-agenda-check-type nil
'agenda
)
4826 :style radio
:selected
(equal org-agenda-ndays
7)]
4828 ["Show Logbook entries" org-agenda-log-mode
4829 :style toggle
:selected org-agenda-show-log
:active
(org-agenda-check-type nil
'agenda
'timeline
)]
4830 ["Include Diary" org-agenda-toggle-diary
4831 :style toggle
:selected org-agenda-include-diary
:active
(org-agenda-check-type nil
'agenda
)]
4832 ["Use Time Grid" org-agenda-toggle-time-grid
4833 :style toggle
:selected org-agenda-use-time-grid
:active
(org-agenda-check-type nil
'agenda
)]
4835 ["New Diary Entry" org-agenda-diary-entry
(org-agenda-check-type nil
'agenda
'timeline
)]
4836 ("Calendar Commands"
4837 ["Goto Calendar" org-agenda-goto-calendar
(org-agenda-check-type nil
'agenda
'timeline
)]
4838 ["Phases of the Moon" org-agenda-phases-of-moon
(org-agenda-check-type nil
'agenda
'timeline
)]
4839 ["Sunrise/Sunset" org-agenda-sunrise-sunset
(org-agenda-check-type nil
'agenda
'timeline
)]
4840 ["Holidays" org-agenda-holidays
(org-agenda-check-type nil
'agenda
'timeline
)]
4841 ["Convert" org-agenda-convert-date
(org-agenda-check-type nil
'agenda
'timeline
)])
4842 ["Create iCalendar file" org-export-icalendar-combine-agenda-files t
]
4844 ["Quit" org-agenda-quit t
]
4845 ["Exit and Release Buffers" org-agenda-exit t
]
4849 (defun org-agenda (arg)
4850 "Dispatch agenda commands to collect entries to the agenda buffer.
4851 Prompts for a character to select a command. Any prefix arg will be passed
4852 on to the selected command. The default selections are:
4854 a Call `org-agenda' to display the agenda for the current day or week.
4855 t Call `org-todo-list' to display the global todo list.
4856 T Call `org-todo-list' to display the global todo list, select only
4857 entries with a specific TODO keyword (the user gets a prompt).
4858 m Call `org-tags-view' to display headlines with tags matching
4859 a condition (the user is prompted for the condition).
4860 M Like `m', but select only TODO entries, no ordinary headlines.
4862 More commands can be added by configuring the variable
4863 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
4864 searches can be pre-defined in this way.
4866 If the current buffer is in Org-mode and visiting a file, you can also
4867 first press `1' to indicate that the agenda should be temporarily (until the
4868 next use of \\[org-agenda]) restricted to the current file."
4871 (let ((restrict-ok (and buffer-file-name
(eq major-mode
'org-mode
)))
4872 (custom org-agenda-custom-commands
)
4873 c entry key type string
)
4874 (put 'org-agenda-files
'org-restrict nil
)
4875 (save-window-excursion
4876 (delete-other-windows)
4877 (switch-to-buffer-other-window " *Agenda Commands*")
4880 "Press key for an agenda command:
4881 --------------------------------
4882 a Agenda for current week or day
4883 t List of all TODO entries T Entries with special TODO kwd
4884 m Match a TAGS query M Like m, but only TODO entries
4885 C Configure your own agenda commands")
4886 (while (setq entry
(pop custom
))
4887 (setq key
(car entry
) type
(nth 1 entry
) string
(nth 2 entry
))
4888 (insert (format "\n%-4s%-14s: %s"
4891 ((eq type
'tags
) "Tags query")
4892 ((eq type
'todo
) "TODO keyword")
4893 ((eq type
'tags-tree
) "Tags tree")
4894 ((eq type
'todo-tree
) "TODO kwd tree")
4895 ((eq type
'occur-tree
) "Occur tree")
4897 (org-add-props string nil
'face
'org-warning
))))
4898 (goto-char (point-min))
4899 (if (fboundp 'fit-window-to-buffer
) (fit-window-to-buffer))
4900 (message "Press key for agenda command%s"
4901 (if restrict-ok
", or [1] to restrict to current file" ""))
4902 (setq c
(read-char-exclusive))
4906 (put 'org-agenda-files
'org-restrict
(list buffer-file-name
))
4907 (error "Cannot restrict agenda to current buffer"))
4908 (message "Press key for agenda command%s"
4909 (if restrict-ok
" (restricted to current file)" ""))
4910 (setq c
(read-char-exclusive))
4912 (require 'calendar
) ; FIXME: can we avoid this for some commands?
4913 ;; For example the todo list should not need it (but does...)
4915 ((equal c ?C
) (customize-variable 'org-agenda-custom-commands
))
4916 ((equal c ?a
) (call-interactively 'org-agenda-list
))
4917 ((equal c ?t
) (call-interactively 'org-todo-list
))
4919 (setq current-prefix-arg
(or arg
'(4)))
4920 (call-interactively 'org-todo-list
))
4921 ((equal c ?m
) (call-interactively 'org-tags-view
))
4923 (setq current-prefix-arg
(or arg
'(4)))
4924 (call-interactively 'org-tags-view
))
4925 ((setq entry
(assoc (char-to-string c
) org-agenda-custom-commands
))
4926 (setq type
(nth 1 entry
) string
(nth 2 entry
))
4929 (org-tags-view current-prefix-arg string
))
4931 (org-todo-list string
))
4932 ((eq type
'tags-tree
)
4933 (org-check-for-org-mode)
4934 (org-tags-sparse-tree current-prefix-arg string
))
4935 ((eq type
'todo-tree
)
4936 (org-check-for-org-mode)
4937 (org-occur (concat "^" outline-regexp
"[ \t]*"
4938 (regexp-quote string
) "\\>")))
4939 ((eq type
'occur-tree
)
4940 (org-check-for-org-mode)
4942 (t (error "Invalid custom agenda command type %s" type
))))
4943 (t (error "Invalid key"))))))
4945 (defun org-check-for-org-mode ()
4946 "Make sure current buffer is in org-mode. Error if not."
4947 (or (eq major-mode
'org-mode
)
4948 (error "Cannot execute org-mode agenda command on buffer in %s."
4951 (defun org-fit-agenda-window ()
4952 "Fit the window to the buffer size."
4953 (and org-fit-agenda-window
4954 (fboundp 'fit-window-to-buffer
)
4955 (fit-window-to-buffer nil
(/ (* (frame-height) 3) 4)
4956 (/ (frame-height) 2))))
4958 (defun org-agenda-files (&optional unrestricted
)
4959 "Get the list of agenda files.
4960 Optional UNRESTRICTED means return the full list even if a restriction
4961 is currently in place."
4963 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
4964 ((stringp org-agenda-files
) (org-read-agenda-file-list))
4965 ((listp org-agenda-files
) org-agenda-files
)
4966 (t (error "Invalid value of `org-agenda-files'"))))
4968 (defvar org-window-configuration
)
4970 (defun org-edit-agenda-file-list ()
4971 "Edit the list of agenda files.
4972 Depending on setup, this either uses customize to edit the variable
4973 `org-agenda-files', or it visits the file that is holding the list. In the
4974 latter case, the buffer is set up in a way that saving it automatically kills
4975 the buffer and restores the previous window configuration."
4977 (if (stringp org-agenda-files
)
4978 (let ((cw (current-window-configuration)))
4979 (find-file org-agenda-files
)
4980 (set (make-local-variable 'org-window-configuration
) cw
)
4981 (org-add-hook 'after-save-hook
4983 (set-window-configuration
4984 (prog1 org-window-configuration
4985 (kill-buffer (current-buffer))))
4986 (org-install-agenda-files-menu)
4987 (message "New agenda file list installed"))
4989 (message (substitute-command-keys
4990 "Edit list and finish with \\[save-buffer]")))
4991 (customize-variable 'org-agenda-files
)))
4993 (defun org-store-new-agenda-file-list (list)
4994 "Set new value for the agenda file list and save it correcly."
4995 (if (stringp org-agenda-files
)
4996 (let ((f org-agenda-files
) b
)
4997 (while (setq b
(find-buffer-visiting f
)) (kill-buffer b
))
4999 (insert (mapconcat 'identity list
"\n") "\n")))
5000 (let ((org-mode-hook nil
) (default-major-mode 'fundamental-mode
))
5001 (setq org-agenda-files list
)
5002 (customize-save-variable 'org-agenda-files org-agenda-files
))))
5004 (defun org-read-agenda-file-list ()
5005 "Read the list of agenda files from a file."
5006 (when (stringp org-agenda-files
)
5008 (insert-file-contents org-agenda-files
)
5009 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
5011 (defvar org-agenda-markers nil
5012 "List of all currently active markers created by `org-agenda'.")
5013 (defvar org-agenda-last-marker-time
(time-to-seconds (current-time))
5014 "Creation time of the last agenda marker.")
5016 (defun org-agenda-new-marker (&optional pos
)
5017 "Return a new agenda marker.
5018 Org-mode keeps a list of these markers and resets them when they are
5020 (let ((m (copy-marker (or pos
(point)))))
5021 (setq org-agenda-last-marker-time
(time-to-seconds (current-time)))
5022 (push m org-agenda-markers
)
5025 (defun org-agenda-maybe-reset-markers (&optional force
)
5026 "Reset markers created by `org-agenda'. But only if they are old enough."
5028 (> (- (time-to-seconds (current-time))
5029 org-agenda-last-marker-time
)
5031 (while org-agenda-markers
5032 (move-marker (pop org-agenda-markers
) nil
))))
5034 (defvar org-agenda-new-buffers nil
5035 "Buffers created to visit agenda files.")
5037 (defun org-get-agenda-file-buffer (file)
5038 "Get a buffer visiting FILE. If the buffer needs to be created, add
5039 it to the list of buffers which might be released later."
5040 (let ((buf (find-buffer-visiting file
)))
5042 buf
; just return it
5043 ;; Make a new buffer and remember it
5044 (setq buf
(find-file-noselect file
))
5045 (if buf
(push buf org-agenda-new-buffers
))
5048 (defun org-release-buffers (blist)
5049 "Release all buffers in list, asking the user for confirmation when needed.
5050 When a buffer is unmodified, it is just killed. When modified, it is saved
5051 \(if the user agrees) and then killed."
5053 (while (setq buf
(pop blist
))
5054 (setq file
(buffer-file-name buf
))
5055 (when (and (buffer-modified-p buf
)
5057 (y-or-n-p (format "Save file %s? " file
)))
5058 (with-current-buffer buf
(save-buffer)))
5059 (kill-buffer buf
))))
5061 (defvar org-respect-restriction nil
) ; Dynamically-scoped param.
5063 (defun org-timeline (&optional include-all keep-modes
)
5064 "Show a time-sorted view of the entries in the current org file.
5065 Only entries with a time stamp of today or later will be listed. With
5066 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
5067 under the current date.
5068 If the buffer contains an active region, only check the region for
5072 (org-agenda-maybe-reset-markers 'force
)
5073 (org-compile-prefix-format org-timeline-prefix-format
)
5075 (dotodo include-all
)
5076 (doclosed org-agenda-show-log
)
5077 (org-agenda-keep-modes keep-modes
)
5078 (entry buffer-file-name
)
5079 (org-agenda-files (list buffer-file-name
))
5080 (date (calendar-current-date))
5081 (win (selected-window))
5083 (beg (if (org-region-active-p) (region-beginning) (point-min)))
5084 (end (if (org-region-active-p) (region-end) (point-max)))
5085 (day-numbers (org-get-all-dates beg end
'no-ranges
5086 t doclosed
)) ; always include today
5087 (today (time-to-days (current-time)))
5088 (org-respect-restriction t
)
5092 (setq org-agenda-redo-command
5094 (list 'switch-to-buffer-other-window
(current-buffer))
5095 (list 'org-timeline
(list 'quote include-all
) t
)))
5097 ;; Remove past dates from the list of dates.
5098 (setq day-numbers
(delq nil
(mapcar (lambda(x)
5099 (if (>= x today
) x nil
))
5101 (switch-to-buffer-other-window
5102 (get-buffer-create org-agenda-buffer-name
))
5103 (setq buffer-read-only nil
)
5105 (org-agenda-mode) (setq buffer-read-only nil
)
5106 (set (make-local-variable 'org-agenda-type
) 'timeline
)
5107 (if doclosed
(push :closed args
))
5108 (push :timestamp args
)
5109 (if dotodo
(push :todo args
))
5110 (while (setq d
(pop day-numbers
))
5111 (if (and (>= d today
)
5116 (insert (make-string 79 ?-
) "\n")))
5117 (setq date
(calendar-gregorian-from-absolute d
))
5119 (setq rtn
(apply 'org-agenda-get-day-entries
5121 (if (or rtn
(equal d today
))
5123 (insert (calendar-day-name date
) " "
5124 (number-to-string (extract-calendar-day date
)) " "
5125 (calendar-month-name (extract-calendar-month date
)) " "
5126 (number-to-string (extract-calendar-year date
)) "\n")
5127 (put-text-property s
(1- (point)) 'face
5130 (put-text-property s
(1- (point)) 'org-today t
))
5131 (insert (org-finalize-agenda-entries rtn
) "\n")
5132 (put-text-property s
(1- (point)) 'day d
))))
5133 (goto-char (point-min))
5134 (setq buffer-read-only t
)
5135 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t
)
5137 (when (not org-select-timeline-window
)
5142 (defun org-agenda-list (&optional include-all start-day ndays keep-modes
)
5143 "Produce a weekly view from all files in variable `org-agenda-files'.
5144 The view will be for the current week, but from the overview buffer you
5145 will be able to go to other weeks.
5146 With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
5147 also be shown, under the current date.
5148 With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
5149 on the days are also shown. See the variable `org-log-done' for how
5151 START-DAY defaults to TODAY, or to the most recent match for the weekday
5152 given in `org-agenda-start-on-weekday'.
5153 NDAYS defaults to `org-agenda-ndays'."
5155 (org-agenda-maybe-reset-markers 'force
)
5156 (org-compile-prefix-format org-agenda-prefix-format
)
5158 (let* ((org-agenda-start-on-weekday
5159 (if (or (equal ndays
1)
5160 (and (null ndays
) (equal 1 org-agenda-ndays
)))
5161 nil org-agenda-start-on-weekday
))
5162 (org-agenda-keep-modes keep-modes
)
5163 (thefiles (org-agenda-files))
5165 (win (selected-window))
5166 (today (time-to-days (current-time)))
5167 (sd (or start-day today
))
5168 (start (if (or (null org-agenda-start-on-weekday
)
5169 (< org-agenda-ndays
7))
5171 (let* ((nt (calendar-day-of-week
5172 (calendar-gregorian-from-absolute sd
)))
5173 (n1 org-agenda-start-on-weekday
)
5175 (- sd
(+ (if (< d
0) 7 0) d
)))))
5176 (day-numbers (list start
))
5177 ;FIXME (inhibit-redisplay t)
5178 s e rtn rtnall file date d start-pos end-pos todayp nd
)
5179 (setq org-agenda-redo-command
5180 (list 'org-agenda-list
(list 'quote include-all
) start-day ndays t
))
5181 ;; Make the list of days
5182 (setq ndays
(or ndays org-agenda-ndays
)
5185 (push (1+ (car day-numbers
)) day-numbers
)
5186 (setq ndays
(1- ndays
)))
5187 (setq day-numbers
(nreverse day-numbers
))
5188 (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name
)))
5190 (delete-other-windows)
5191 (switch-to-buffer-other-window
5192 (get-buffer-create org-agenda-buffer-name
))))
5193 (setq buffer-read-only nil
)
5195 (org-agenda-mode) (setq buffer-read-only nil
)
5196 (set (make-local-variable 'org-agenda-type
) 'agenda
)
5197 (set (make-local-variable 'starting-day
) (car day-numbers
))
5198 (set (make-local-variable 'include-all-loc
) include-all
)
5199 (when (and (or include-all org-agenda-include-all-todo
)
5200 (member today day-numbers
))
5201 (setq files thefiles
5203 (while (setq file
(pop files
))
5205 (org-check-agenda-file file
)
5206 (setq date
(calendar-gregorian-from-absolute today
)
5207 rtn
(org-agenda-get-day-entries
5209 (setq rtnall
(append rtnall rtn
))))
5211 (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
5212 (add-text-properties (point-min) (1- (point))
5213 (list 'face
'org-level-3
))
5214 (insert (org-finalize-agenda-entries rtnall
) "\n")))
5215 (while (setq d
(pop day-numbers
))
5216 (setq date
(calendar-gregorian-from-absolute d
)
5218 (if (or (setq todayp
(= d today
))
5219 (and (not start-pos
) (= d sd
)))
5220 (setq start-pos
(point))
5221 (if (and start-pos
(not end-pos
))
5222 (setq end-pos
(point))))
5223 (setq files thefiles
5225 (while (setq file
(pop files
))
5227 (org-check-agenda-file file
)
5228 (if org-agenda-show-log
5229 (setq rtn
(org-agenda-get-day-entries
5231 :deadline
:scheduled
:timestamp
:closed
))
5232 (setq rtn
(org-agenda-get-day-entries
5234 :deadline
:scheduled
:timestamp
)))
5235 (setq rtnall
(append rtnall rtn
))))
5236 (if org-agenda-include-diary
5238 (require 'diary-lib
)
5239 (setq rtn
(org-get-entries-from-diary date
))
5240 (setq rtnall
(append rtnall rtn
))))
5241 (if (or rtnall org-agenda-show-all-dates
)
5243 (insert (format "%-9s %2d %s %4d\n"
5244 (calendar-day-name date
)
5245 (extract-calendar-day date
)
5246 (calendar-month-name (extract-calendar-month date
))
5247 (extract-calendar-year date
)))
5248 (put-text-property s
(1- (point)) 'face
5251 (org-finalize-agenda-entries
5252 (org-agenda-add-time-grid-maybe
5255 (put-text-property s
(1- (point)) 'day d
))))
5256 (goto-char (point-min))
5257 (setq buffer-read-only t
)
5258 (org-fit-agenda-window)
5259 (unless (and (pos-visible-in-window-p (point-min))
5260 (pos-visible-in-window-p (point-max)))
5261 (goto-char (1- (point-max)))
5263 (if (not (pos-visible-in-window-p (or start-pos
1)))
5265 (goto-char (or start-pos
1))
5267 (goto-char (or start-pos
1))
5268 (if (not org-select-agenda-window
) (select-window win
))
5271 (defvar org-select-this-todo-keyword nil
)
5274 (defun org-todo-list (arg &optional keep-modes
)
5275 "Show all TODO entries from all agenda file in a single list.
5276 The prefix arg can be used to select a specific TODO keyword and limit
5277 the list to these. When using \\[universal-argument], you will be prompted
5278 for a keyword. A numeric prefix directly selects the Nth keyword in
5279 `org-todo-keywords'."
5281 (org-agenda-maybe-reset-markers 'force
)
5282 (org-compile-prefix-format org-agenda-prefix-format
)
5283 (let* ((org-agenda-keep-modes keep-modes
)
5284 (today (time-to-days (current-time)))
5285 (date (calendar-gregorian-from-absolute today
))
5286 (win (selected-window))
5287 (kwds org-todo-keywords
)
5288 (completion-ignore-case t
)
5289 (org-select-this-todo-keyword
5290 (if (stringp arg
) arg
5291 (and arg
(integerp arg
) (nth (1- arg
) org-todo-keywords
))))
5292 rtn rtnall files file pos
)
5293 (when (equal arg
'(4))
5294 (setq org-select-this-todo-keyword
5295 (completing-read "Keyword: " (mapcar 'list org-todo-keywords
)
5297 (and (equal 0 arg
) (setq org-select-this-todo-keyword nil
))
5298 (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name
)))
5300 (delete-other-windows)
5301 (switch-to-buffer-other-window
5302 (get-buffer-create org-agenda-buffer-name
))))
5303 (setq buffer-read-only nil
)
5305 (org-agenda-mode) (setq buffer-read-only nil
)
5306 (set (make-local-variable 'org-agenda-type
) 'todo
)
5307 (set (make-local-variable 'last-arg
) arg
)
5308 (set (make-local-variable 'org-todo-keywords
) kwds
)
5309 (set (make-local-variable 'org-agenda-redo-command
)
5310 '(org-todo-list (or current-prefix-arg last-arg
) t
))
5311 (setq files
(org-agenda-files)
5313 (while (setq file
(pop files
))
5315 (org-check-agenda-file file
)
5316 (setq rtn
(org-agenda-get-day-entries file date
:todo
))
5317 (setq rtnall
(append rtnall rtn
))))
5318 (insert "Global list of TODO items of type: ")
5319 (add-text-properties (point-min) (1- (point))
5320 (list 'face
'org-level-3
))
5322 (insert (or org-select-this-todo-keyword
"ALL") "\n")
5323 (add-text-properties pos
(1- (point)) (list 'face
'org-warning
))
5326 "Available with `N r': (0)ALL "
5328 (mapconcat (lambda (x)
5329 (format "(%d)%s" (setq n
(1+ n
)) x
))
5330 org-todo-keywords
" "))
5332 (add-text-properties pos
(1- (point)) (list 'face
'org-level-3
))
5334 (insert (org-finalize-agenda-entries rtnall
) "\n"))
5335 (goto-char (point-min))
5336 (setq buffer-read-only t
)
5337 (org-fit-agenda-window)
5338 (if (not org-select-agenda-window
) (select-window win
))))
5340 (defun org-check-agenda-file (file)
5341 "Make sure FILE exists. If not, ask user what to do."
5342 (when (not (file-exists-p file
))
5343 (message "non-existent file %s. [R]emove from list or [A]bort?"
5344 (abbreviate-file-name file
))
5345 (let ((r (downcase (read-char-exclusive))))
5348 (org-remove-file file
)
5349 (throw 'nextfile t
))
5350 (t (error "Abort"))))))
5352 (defun org-agenda-check-type (error &rest types
)
5353 "Check if agenda buffer is of allowed type.
5354 If ERROR is non-nil, throw an error, otherwise just return nil."
5355 (if (memq org-agenda-type types
)
5358 (error "Now allowed in %s-type agenda buffers" org-agenda-type
)
5361 (defun org-agenda-quit ()
5362 "Exit agenda by removing the window or the buffer."
5364 (let ((buf (current-buffer)))
5365 (if (not (one-window-p)) (delete-window))
5367 (org-agenda-maybe-reset-markers 'force
)))
5369 (defun org-agenda-exit ()
5370 "Exit agenda by removing the window or the buffer.
5371 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
5372 Org-mode buffers visited directly by the user will not be touched."
5374 (org-release-buffers org-agenda-new-buffers
)
5375 (setq org-agenda-new-buffers nil
)
5378 (defun org-agenda-redo ()
5380 When this is the global TODO list, a prefix argument will be interpreted."
5382 (message "Rebuilding agenda buffer...")
5383 (eval org-agenda-redo-command
)
5384 (message "Rebuilding agenda buffer...done"))
5386 (defun org-agenda-goto-today ()
5389 (org-agenda-check-type t
'timeline
'agenda
)
5390 (if (boundp 'starting-day
)
5391 (let ((cmd (car org-agenda-redo-command
))
5392 (iall (nth 1 org-agenda-redo-command
))
5393 (nday (nth 3 org-agenda-redo-command
))
5394 (keep (nth 4 org-agenda-redo-command
)))
5395 (eval (list cmd iall nil nday keep
)))
5396 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t
)
5399 (defun org-agenda-later (arg)
5400 "Go forward in time by `org-agenda-ndays' days.
5401 With prefix ARG, go forward that many times `org-agenda-ndays'."
5403 (org-agenda-check-type t
'agenda
)
5404 (org-agenda-list (if (boundp 'include-all-loc
) include-all-loc nil
)
5405 (+ starting-day
(* arg org-agenda-ndays
)) nil t
))
5407 (defun org-agenda-earlier (arg)
5408 "Go back in time by `org-agenda-ndays' days.
5409 With prefix ARG, go back that many times `org-agenda-ndays'."
5411 (org-agenda-check-type t
'agenda
)
5412 (org-agenda-list (if (boundp 'include-all-loc
) include-all-loc nil
)
5413 (- starting-day
(* arg org-agenda-ndays
)) nil t
))
5415 (defun org-agenda-week-view ()
5416 "Switch to weekly view for agenda."
5418 (org-agenda-check-type t
'agenda
)
5419 (setq org-agenda-ndays
7)
5420 (org-agenda-list include-all-loc
5421 (or (get-text-property (point) 'day
)
5424 (org-agenda-set-mode-name)
5425 (message "Switched to week view"))
5427 (defun org-agenda-day-view ()
5428 "Switch to daily view for agenda."
5430 (org-agenda-check-type t
'agenda
)
5431 (setq org-agenda-ndays
1)
5432 (org-agenda-list include-all-loc
5433 (or (get-text-property (point) 'day
)
5436 (org-agenda-set-mode-name)
5437 (message "Switched to day view"))
5439 (defun org-agenda-next-date-line (&optional arg
)
5440 "Jump to the next line indicating a date in agenda buffer."
5442 (org-agenda-check-type t
'agenda
'timeline
)
5443 (beginning-of-line 1)
5444 (if (looking-at "^\\S-") (forward-char 1))
5445 (if (not (re-search-forward "^\\S-" nil t arg
))
5448 (error "No next date after this line in this buffer")))
5449 (goto-char (match-beginning 0)))
5451 (defun org-agenda-previous-date-line (&optional arg
)
5452 "Jump to the previous line indicating a date in agenda buffer."
5454 (org-agenda-check-type t
'agenda
'timeline
)
5455 (beginning-of-line 1)
5456 (if (not (re-search-backward "^\\S-" nil t arg
))
5457 (error "No previous date before this line in this buffer")))
5459 ;; Initialize the highlight
5460 (defvar org-hl
(org-make-overlay 1 1))
5461 (org-overlay-put org-hl
'face
'highlight
)
5463 (defun org-highlight (begin end
&optional buffer
)
5464 "Highlight a region with overlay."
5465 (funcall (if (featurep 'xemacs
) 'set-extent-endpoints
'move-overlay
)
5466 org-hl begin end
(or buffer
(current-buffer))))
5468 (defun org-unhighlight ()
5469 "Detach overlay INDEX."
5470 (funcall (if (featurep 'xemacs
) 'detach-extent
'delete-overlay
) org-hl
))
5473 (defun org-agenda-follow-mode ()
5474 "Toggle follow mode in an agenda buffer."
5476 (setq org-agenda-follow-mode
(not org-agenda-follow-mode
))
5477 (org-agenda-set-mode-name)
5478 (message "Follow mode is %s"
5479 (if org-agenda-follow-mode
"on" "off")))
5481 (defun org-agenda-log-mode ()
5482 "Toggle log mode in an agenda buffer."
5484 (org-agenda-check-type t
'agenda
'timeline
)
5485 (setq org-agenda-show-log
(not org-agenda-show-log
))
5486 (org-agenda-set-mode-name)
5488 (message "Log mode is %s"
5489 (if org-agenda-show-log
"on" "off")))
5491 (defun org-agenda-toggle-diary ()
5492 "Toggle diary inclusion in an agenda buffer."
5494 (org-agenda-check-type t
'agenda
)
5495 (setq org-agenda-include-diary
(not org-agenda-include-diary
))
5497 (org-agenda-set-mode-name)
5498 (message "Diary inclusion turned %s"
5499 (if org-agenda-include-diary
"on" "off")))
5501 (defun org-agenda-toggle-time-grid ()
5502 "Toggle time grid in an agenda buffer."
5504 (org-agenda-check-type t
'agenda
)
5505 (setq org-agenda-use-time-grid
(not org-agenda-use-time-grid
))
5507 (org-agenda-set-mode-name)
5508 (message "Time-grid turned %s"
5509 (if org-agenda-use-time-grid
"on" "off")))
5511 (defun org-agenda-set-mode-name ()
5512 "Set the mode name to indicate all the small mode settings."
5514 (concat "Org-Agenda"
5515 (if (equal org-agenda-ndays
1) " Day" "")
5516 (if (equal org-agenda-ndays
7) " Week" "")
5517 (if org-agenda-follow-mode
" Follow" "")
5518 (if org-agenda-include-diary
" Diary" "")
5519 (if org-agenda-use-time-grid
" Grid" "")
5520 (if org-agenda-show-log
" Log" "")))
5521 (force-mode-line-update))
5523 (defun org-agenda-post-command-hook ()
5524 (and (eolp) (not (bolp)) (backward-char 1))
5525 (if (and org-agenda-follow-mode
5526 (get-text-property (point) 'org-marker
))
5529 (defvar org-disable-agenda-to-diary nil
) ;Dynamically-scoped param.
5531 (defun org-get-entries-from-diary (date)
5532 "Get the (Emacs Calendar) diary entries for DATE."
5533 (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
5534 (diary-display-hook '(fancy-diary-display))
5535 (list-diary-entries-hook
5536 (cons 'org-diary-default-entry list-diary-entries-hook
))
5537 (diary-file-name-prefix-function nil
) ; turn this feature off
5538 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string
)
5540 (org-disable-agenda-to-diary t
))
5542 (save-window-excursion
5543 (list-diary-entries date
1))) ;; Keep this name for now, compatibility
5544 (if (not (get-buffer fancy-diary-buffer
))
5546 (with-current-buffer fancy-diary-buffer
5547 (setq buffer-read-only nil
)
5548 (if (= (point-max) 1)
5551 ;; Omit the date and other unnecessary stuff
5552 (org-agenda-cleanup-fancy-diary)
5553 ;; Add prefix to each line and extend the text properties
5554 (if (= (point-max) 1)
5556 (setq entries
(buffer-substring (point-min) (- (point-max) 1)))))
5557 (set-buffer-modified-p nil
)
5558 (kill-buffer fancy-diary-buffer
)))
5560 (setq entries
(org-split-string entries
"\n"))
5564 (setq x
(org-format-agenda-item "" x
"Diary" nil
'time
))
5565 ;; Extend the text properties to the beginning of the line
5566 (org-add-props x
(text-properties-at (1- (length x
)) x
)))
5569 (defun org-agenda-cleanup-fancy-diary ()
5570 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5571 This gets rid of the date, the underline under the date, and
5572 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5573 date. It also removes lines that contain only whitespace."
5574 (goto-char (point-min))
5575 (if (looking-at ".*?:[ \t]*")
5578 (re-search-forward "\n=+$" nil t
)
5580 (while (re-search-backward "^ +\n?" nil t
) (replace-match "")))
5581 (re-search-forward "\n=+$" nil t
)
5582 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5583 (goto-char (point-min))
5584 (while (re-search-forward "^ +\n" nil t
)
5586 (goto-char (point-min))
5587 (if (re-search-forward "^Org-mode dummy\n?" nil t
)
5588 (replace-match "")))
5590 ;; Make sure entries from the diary have the right text properties.
5591 (eval-after-load "diary-lib"
5592 '(if (boundp 'diary-modify-entry-list-string-function
)
5593 ;; We can rely on the hook, nothing to do
5595 ;; Hook not avaiable, must use advice to make this work
5596 (defadvice add-to-diary-list
(before org-mark-diary-entry activate
)
5597 "Make the position visible."
5598 (if (and org-disable-agenda-to-diary
;; called from org-agenda
5601 (setq string
(org-modify-diary-entry-string string
))))))
5603 (defun org-modify-diary-entry-string (string)
5604 "Add text properties to string, allowing org-mode to act on it."
5605 (org-add-props string nil
5606 'mouse-face
'highlight
5607 'keymap org-agenda-keymap
5608 'help-echo
(format "mouse-2 or RET jump to diary file %s"
5609 (abbreviate-file-name buffer-file-name
))
5610 'org-agenda-diary-link t
5611 'org-marker
(org-agenda-new-marker (point-at-bol))))
5613 (defun org-diary-default-entry ()
5614 "Add a dummy entry to the diary.
5615 Needed to avoid empty dates which mess up holiday display."
5616 ;; Catch the error if dealing with the new add-to-diary-alist
5617 (when org-disable-agenda-to-diary
5619 (add-to-diary-list original-date
"Org-mode dummy" "")
5621 (add-to-diary-list original-date
"Org-mode dummy" "" nil
)))))
5623 (defun org-cycle-agenda-files ()
5624 "Cycle through the files in `org-agenda-files'.
5625 If the current buffer visits an agenda file, find the next one in the list.
5626 If the current buffer does not, find the first agenda file."
5628 (let* ((fs (org-agenda-files t
))
5629 (files (append fs
(list (car fs
))))
5630 (tcf (if buffer-file-name
(file-truename buffer-file-name
)))
5632 (unless files
(error "No agenda files"))
5634 (while (setq file
(pop files
))
5635 (if (equal (file-truename file
) tcf
)
5637 (find-file (car files
))
5639 (find-file (car fs
)))))
5641 (defun org-agenda-file-to-end ()
5642 "Move/add the current file to the end of the agenda file list.
5643 If the file is not present in the list, it is appended to the list. If it is
5644 present, it is moved there."
5646 (org-agenda-file-to-front 'to-end
))
5648 (defun org-agenda-file-to-front (&optional to-end
)
5649 "Move/add the current file to the top of the agenda file list.
5650 If the file is not present in the list, it is added to the front. If it is
5651 present, it is moved there. With optional argument TO-END, add/move to the
5654 (let ((file-alist (mapcar (lambda (x)
5655 (cons (file-truename x
) x
))
5656 (org-agenda-files t
)))
5657 (ctf (file-truename buffer-file-name
))
5659 (setq x
(assoc ctf file-alist
) had x
)
5661 (if (not x
) (setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
5663 (setq file-alist
(append (delq x file-alist
) (list x
)))
5664 (setq file-alist
(cons x
(delq x file-alist
))))
5665 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
5666 (org-install-agenda-files-menu)
5667 (message "File %s to %s of agenda file list"
5668 (if had
"moved" "added") (if to-end
"end" "front"))))
5670 (defun org-remove-file (&optional file
)
5671 "Remove current file from the list of files in variable `org-agenda-files'.
5672 These are the files which are being checked for agenda entries.
5673 Optional argument FILE means, use this file instead of the current."
5675 (let* ((file (or file buffer-file-name
))
5676 (true-file (file-truename file
))
5677 (afile (abbreviate-file-name file
))
5678 (files (delq nil
(mapcar
5680 (if (equal true-file
5683 (org-agenda-files t
)))))
5684 (if (not (= (length files
) (length (org-agenda-files t
))))
5686 (org-store-new-agenda-file-list files
)
5687 (org-install-agenda-files-menu)
5688 (message "Removed file: %s" afile
))
5689 (message "File was not in list: %s" afile
))))
5691 (defun org-file-menu-entry (file)
5692 (vector file
(list 'find-file file
) t
))
5694 (defun org-get-all-dates (beg end
&optional no-ranges force-today inactive
)
5695 "Return a list of all relevant day numbers from BEG to END buffer positions.
5696 If NO-RANGES is non-nil, include only the start and end dates of a range,
5697 not every single day in the range. If FORCE-TODAY is non-nil, make
5698 sure that TODAY is included in the list. If INACTIVE is non-nil, also
5699 inactive time stamps (those in square brackets) are included."
5700 (let ((re (if inactive org-ts-regexp-both org-ts-regexp
))
5701 dates date day day1 day2 ts1 ts2
)
5703 (setq dates
(list (time-to-days (current-time)))))
5706 (while (re-search-forward re end t
)
5707 (setq day
(time-to-days (org-time-string-to-time
5708 (substring (match-string 1) 0 10))))
5709 (or (memq day dates
) (push day dates
)))
5712 (while (re-search-forward org-tr-regexp end t
)
5713 (setq ts1
(substring (match-string 1) 0 10)
5714 ts2
(substring (match-string 2) 0 10)
5715 day1
(time-to-days (org-time-string-to-time ts1
))
5716 day2
(time-to-days (org-time-string-to-time ts2
)))
5717 (while (< (setq day1
(1+ day1
)) day2
)
5718 (or (memq day1 dates
) (push day1 dates
)))))
5722 (defun org-diary (&rest args
)
5723 "Return diary information from org-files.
5724 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5725 It accesses org files and extracts information from those files to be
5726 listed in the diary. The function accepts arguments specifying what
5727 items should be listed. The following arguments are allowed:
5729 :timestamp List the headlines of items containing a date stamp or
5730 date range matching the selected date. Deadlines will
5731 also be listed, on the expiration day.
5733 :deadline List any deadlines past due, or due within
5734 `org-deadline-warning-days'. The listing occurs only
5735 in the diary for *today*, not at any other date. If
5736 an entry is marked DONE, it is no longer listed.
5738 :scheduled List all items which are scheduled for the given date.
5739 The diary for *today* also contains items which were
5740 scheduled earlier and are not yet marked DONE.
5742 :todo List all TODO items from the org-file. This may be a
5743 long list - so this is not turned on by default.
5744 Like deadlines, these entries only show up in the
5745 diary for *today*, not at any other date.
5747 The call in the diary file should look like this:
5749 &%%(org-diary) ~/path/to/some/orgfile.org
5751 Use a separate line for each org file to check. Or, if you omit the file name,
5752 all files listed in `org-agenda-files' will be checked automatically:
5756 If you don't give any arguments (as in the example above), the default
5757 arguments (:deadline :scheduled :timestamp) are used. So the example above may
5760 &%%(org-diary :deadline :timestamp :scheduled)
5762 The function expects the lisp variables `entry' and `date' to be provided
5763 by the caller, because this is how the calendar works. Don't use this
5764 function from a program - use `org-agenda-get-day-entries' instead."
5765 (org-agenda-maybe-reset-markers)
5766 (org-compile-prefix-format org-agenda-prefix-format
)
5767 (setq args
(or args
'(:deadline
:scheduled
:timestamp
)))
5768 (let* ((files (if (and entry
(stringp entry
) (string-match "\\S-" entry
))
5770 (org-agenda-files t
)))
5772 ;; If this is called during org-agenda, don't return any entries to
5773 ;; the calendar. Org Agenda will list these entries itself.
5774 (if org-disable-agenda-to-diary
(setq files nil
))
5775 (while (setq file
(pop files
))
5776 (setq rtn
(apply 'org-agenda-get-day-entries file date args
))
5777 (setq results
(append results rtn
)))
5779 (concat (org-finalize-agenda-entries results
) "\n"))))
5780 (defvar org-category-table nil
)
5781 (defun org-get-category-table ()
5782 "Get the table of categories and positions in current buffer."
5785 (goto-char (point-min))
5786 (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t
)
5787 (push (cons (point) (org-trim (match-string 2))) tbl
)))
5789 (defun org-get-category (&optional pos
)
5790 "Get the category applying to position POS."
5791 (if (not org-category-table
)
5793 ((null org-category
)
5795 (if buffer-file-name
5796 (file-name-sans-extension
5797 (file-name-nondirectory buffer-file-name
))
5799 ((symbolp org-category
) (symbol-name org-category
))
5801 (let ((tbl org-category-table
)
5802 (pos (or pos
(point))))
5803 (while (and tbl
(> (caar tbl
) pos
))
5805 (or (cdar tbl
) (cdr (nth (1- (length org-category-table
))
5806 org-category-table
))))))
5808 (defun org-agenda-get-day-entries (file date
&rest args
)
5809 "Does the work for `org-diary' and `org-agenda'.
5810 FILE is the path to a file to be checked for entries. DATE is date like
5811 the one returned by `calendar-current-date'. ARGS are symbols indicating
5812 which kind of entries should be extracted. For details about these, see
5813 the documentation of `org-diary'."
5814 (setq args
(or args
'(:deadline
:scheduled
:timestamp
)))
5815 (let* ((org-startup-with-deadline-check nil
)
5816 (org-startup-folded nil
)
5817 (org-startup-align-all-tables nil
)
5818 (buffer (if (file-exists-p file
)
5819 (org-get-agenda-file-buffer file
)
5820 (error "No such file %s" file
)))
5823 ;; If file does not exist, make sure an error message ends up in diary
5824 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file
))
5825 (with-current-buffer buffer
5826 (unless (eq major-mode
'org-mode
)
5827 (error "Agenda file %s is not in `org-mode'" file
))
5828 (setq org-category-table
(org-get-category-table))
5829 (let ((case-fold-search nil
))
5832 (if org-respect-restriction
5833 (if (org-region-active-p)
5834 ;; Respect a region to restrict search
5835 (narrow-to-region (region-beginning) (region-end)))
5836 ;; If we work for the calendar or many files,
5837 ;; get rid of any restriction
5839 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5840 (while (setq arg
(pop args
))
5842 ((and (eq arg
:todo
)
5843 (equal date
(calendar-current-date)))
5844 (setq rtn
(org-agenda-get-todos))
5845 (setq results
(append results rtn
)))
5846 ((eq arg
:timestamp
)
5847 (setq rtn
(org-agenda-get-blocks))
5848 (setq results
(append results rtn
))
5849 (setq rtn
(org-agenda-get-timestamps))
5850 (setq results
(append results rtn
)))
5851 ((eq arg
:scheduled
)
5852 (setq rtn
(org-agenda-get-scheduled))
5853 (setq results
(append results rtn
)))
5855 (setq rtn
(org-agenda-get-closed))
5856 (setq results
(append results rtn
)))
5857 ((and (eq arg
:deadline
)
5858 (equal date
(calendar-current-date)))
5859 (setq rtn
(org-agenda-get-deadlines))
5860 (setq results
(append results rtn
))))))))
5863 (defun org-entry-is-done-p ()
5864 "Is the current entry marked DONE?"
5866 (and (re-search-backward "[\r\n]\\*" nil t
)
5867 (looking-at org-nl-done-regexp
))))
5869 (defun org-at-date-range-p ()
5870 "Is the cursor inside a date range?"
5874 (let ((pos (point)))
5875 (skip-chars-backward "^<\r\n")
5876 (skip-chars-backward "<")
5877 (and (looking-at org-tr-regexp
)
5878 (>= (match-end 0) pos
)
5880 (skip-chars-backward "^<\r\n")
5881 (skip-chars-backward "<")
5882 (and (looking-at org-tr-regexp
)
5883 (>= (match-end 0) pos
)
5887 (defun org-agenda-get-todos ()
5888 "Return the TODO information for agenda display."
5889 (let* ((props (list 'face nil
5890 'done-face
'org-done
5891 'mouse-face
'highlight
5892 'keymap org-agenda-keymap
5894 (format "mouse-2 or RET jump to org file %s"
5895 (abbreviate-file-name buffer-file-name
))))
5896 (regexp (concat "[\n\r]\\*+ *\\("
5897 (if org-select-this-todo-keyword
5898 (concat "\\<\\(" org-select-this-todo-keyword
5900 org-not-done-regexp
)
5902 marker priority category tags
5904 (goto-char (point-min))
5905 (while (re-search-forward regexp nil t
)
5906 (goto-char (match-beginning 1))
5907 (setq marker
(org-agenda-new-marker (1+ (match-beginning 0)))
5908 category
(org-get-category)
5909 tags
(org-get-tags-at (point))
5910 txt
(org-format-agenda-item "" (match-string 1) category tags
)
5912 (+ (org-get-priority txt
)
5913 (if org-todo-kwd-priority-p
5914 (- org-todo-kwd-max-priority -
2
5916 (member (match-string 2) org-todo-keywords
)))
5918 (org-add-props txt props
5919 'org-marker marker
'org-hd-marker marker
5920 'priority priority
'category category
)
5922 (goto-char (match-end 1)))
5925 (defconst org-agenda-no-heading-message
5926 "No heading for this item in buffer or region.")
5928 (defun org-agenda-get-timestamps ()
5929 "Return the date stamp information for agenda display."
5930 (let* ((props (list 'face nil
5931 'mouse-face
'highlight
5932 'keymap org-agenda-keymap
5934 (format "mouse-2 or RET jump to org file %s"
5935 (abbreviate-file-name buffer-file-name
))))
5936 (regexp (regexp-quote
5939 (car org-time-stamp-formats
)
5940 (apply 'encode-time
; DATE bound by calendar
5941 (list 0 0 0 (nth 1 date
) (car date
) (nth 2 date
))))
5943 marker hdmarker deadlinep scheduledp donep tmp priority category
5944 ee txt timestr tags
)
5945 (goto-char (point-min))
5946 (while (re-search-forward regexp nil t
)
5947 (if (not (save-match-data (org-at-date-range-p)))
5949 (setq marker
(org-agenda-new-marker (match-beginning 0))
5950 category
(org-get-category (match-beginning 0))
5951 tmp
(buffer-substring (max (point-min)
5952 (- (match-beginning 0)
5953 org-ds-keyword-length
))
5954 (match-beginning 0))
5955 timestr
(buffer-substring (match-beginning 0) (point-at-eol))
5956 deadlinep
(string-match org-deadline-regexp tmp
)
5957 scheduledp
(string-match org-scheduled-regexp tmp
)
5958 donep
(org-entry-is-done-p))
5959 (if (string-match ">" timestr
)
5960 ;; substring should only run to end of time stamp
5961 (setq timestr
(substring timestr
0 (match-end 0))))
5963 (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t
)
5965 (goto-char (match-end 1))
5966 (setq hdmarker
(org-agenda-new-marker)
5967 tags
(org-get-tags-at))
5968 (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
5969 (setq txt
(org-format-agenda-item
5971 (if deadlinep
"Deadline: " "")
5972 (if scheduledp
"Scheduled: " ""))
5973 (match-string 1) category tags timestr
)))
5974 (setq txt org-agenda-no-heading-message
))
5976 (setq priority
(org-get-priority txt
))
5977 (org-add-props txt props
5978 'org-marker marker
'org-hd-marker hdmarker
)
5980 (org-add-props txt nil
5981 'face
(if donep
'org-done
'org-warning
)
5982 'undone-face
'org-warning
'done-face
'org-done
5983 'category category
'priority
(+ 100 priority
))
5985 (org-add-props txt nil
5986 'face
'org-scheduled-today
5987 'undone-face
'org-scheduled-today
'done-face
'org-done
5988 'category category
'priority
(+ 99 priority
))
5989 (org-add-props txt nil
'priority priority
'category category
)))
5991 (outline-next-heading))))
5994 (defun org-agenda-get-closed ()
5995 "Return the logged TODO entries for agenda display."
5996 (let* ((props (list 'mouse-face
'highlight
5997 'keymap org-agenda-keymap
5999 (format "mouse-2 or RET jump to org file %s"
6000 (abbreviate-file-name buffer-file-name
))))
6002 "\\<" org-closed-string
" *\\["
6006 (car org-time-stamp-formats
)
6007 (apply 'encode-time
; DATE bound by calendar
6008 (list 0 0 0 (nth 1 date
) (car date
) (nth 2 date
))))
6010 marker hdmarker priority category tags
6012 (goto-char (point-min))
6013 (while (re-search-forward regexp nil t
)
6014 (if (not (save-match-data (org-at-date-range-p)))
6016 (setq marker
(org-agenda-new-marker (match-beginning 0))
6017 category
(org-get-category (match-beginning 0))
6018 timestr
(buffer-substring (match-beginning 0) (point-at-eol))
6019 ;; donep (org-entry-is-done-p)
6021 (if (string-match "\\]" timestr
)
6022 ;; substring should only run to end of time stamp
6023 (setq timestr
(substring timestr
0 (match-end 0))))
6025 (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t
)
6027 (goto-char (match-end 1))
6028 (setq hdmarker
(org-agenda-new-marker)
6029 tags
(org-get-tags-at))
6030 (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
6031 (setq txt
(org-format-agenda-item
6033 (match-string 1) category tags timestr
)))
6034 (setq txt org-agenda-no-heading-message
))
6035 (setq priority
100000)
6036 (org-add-props txt props
6037 'org-marker marker
'org-hd-marker hdmarker
'face
'org-done
6038 'priority priority
'category category
6039 'undone-face
'org-warning
'done-face
'org-done
)
6041 (outline-next-heading))))
6044 (defun org-agenda-get-deadlines ()
6045 "Return the deadline information for agenda display."
6046 (let* ((wdays org-deadline-warning-days
)
6047 (props (list 'mouse-face
'highlight
6048 'keymap org-agenda-keymap
6050 (format "mouse-2 or RET jump to org file %s"
6051 (abbreviate-file-name buffer-file-name
))))
6052 (regexp org-deadline-time-regexp
)
6053 (todayp (equal date
(calendar-current-date))) ; DATE bound by calendar
6054 (d1 (calendar-absolute-from-gregorian date
)) ; DATE bound by calendar
6055 d2 diff pos pos1 category tags
6057 (goto-char (point-min))
6058 (while (re-search-forward regexp nil t
)
6059 (setq pos
(1- (match-beginning 1))
6061 (org-time-string-to-time (match-string 1)))
6063 ;; When to show a deadline in the calendar:
6064 ;; If the expiration is within wdays warning time.
6065 ;; Past-due deadlines are only shown on the current date
6066 (if (and (< diff wdays
) todayp
(not (= diff
0)))
6068 (setq category
(org-get-category))
6069 (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t
)
6071 (goto-char (match-end 0))
6072 (setq pos1
(match-end 1))
6073 (setq tags
(org-get-tags-at pos1
))
6074 (setq head
(buffer-substring-no-properties
6076 (progn (skip-chars-forward "^\r\n")
6078 (if (string-match org-looking-at-done-regexp head
)
6080 (setq txt
(org-format-agenda-item
6081 (format "In %3d d.: " diff
) head category tags
))))
6082 (setq txt org-agenda-no-heading-message
))
6084 (org-add-props txt props
6085 'org-marker
(org-agenda-new-marker pos
)
6086 'org-hd-marker
(org-agenda-new-marker pos1
)
6087 'priority
(+ (- 10 diff
) (org-get-priority txt
))
6089 'face
(cond ((<= diff
0) 'org-warning
)
6090 ((<= diff
5) 'org-scheduled-previously
)
6093 ((<= diff
0) 'org-warning
)
6094 ((<= diff
5) 'org-scheduled-previously
)
6096 'done-face
'org-done
)
6100 (defun org-agenda-get-scheduled ()
6101 "Return the scheduled information for agenda display."
6102 (let* ((props (list 'face
'org-scheduled-previously
6103 'undone-face
'org-scheduled-previously
6104 'done-face
'org-done
6105 'mouse-face
'highlight
6106 'keymap org-agenda-keymap
6108 (format "mouse-2 or RET jump to org file %s"
6109 (abbreviate-file-name buffer-file-name
))))
6110 (regexp org-scheduled-time-regexp
)
6111 (todayp (equal date
(calendar-current-date))) ; DATE bound by calendar
6112 (d1 (calendar-absolute-from-gregorian date
)) ; DATE bound by calendar
6113 d2 diff pos pos1 category tags
6115 (goto-char (point-min))
6116 (while (re-search-forward regexp nil t
)
6117 (setq pos
(1- (match-beginning 1))
6119 (org-time-string-to-time (match-string 1)))
6121 ;; When to show a scheduled item in the calendar:
6122 ;; If it is on or past the date.
6123 (if (and (< diff
0) todayp
)
6125 (setq category
(org-get-category))
6126 (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t
)
6128 (goto-char (match-end 0))
6129 (setq pos1
(match-end 1))
6130 (setq tags
(org-get-tags-at))
6131 (setq head
(buffer-substring-no-properties
6133 (progn (skip-chars-forward "^\r\n") (point))))
6134 (if (string-match org-looking-at-done-regexp head
)
6136 (setq txt
(org-format-agenda-item
6137 (format "Sched.%2dx: " (- 1 diff
)) head
6139 (setq txt org-agenda-no-heading-message
))
6141 (org-add-props txt props
6142 'org-marker
(org-agenda-new-marker pos
)
6143 'org-hd-marker
(org-agenda-new-marker pos1
)
6144 'priority
(+ (- 5 diff
) (org-get-priority txt
))
6149 (defun org-agenda-get-blocks ()
6150 "Return the date-range information for agenda display."
6151 (let* ((props (list 'face nil
6152 'mouse-face
'highlight
6153 'keymap org-agenda-keymap
6155 (format "mouse-2 or RET jump to org file %s"
6156 (abbreviate-file-name buffer-file-name
))))
6157 (regexp org-tr-regexp
)
6158 (d0 (calendar-absolute-from-gregorian date
))
6159 marker hdmarker ee txt d1 d2 s1 s2 timestr category tags
)
6160 (goto-char (point-min))
6161 (while (re-search-forward regexp nil t
)
6162 (setq timestr
(match-string 0)
6165 d1
(time-to-days (org-time-string-to-time s1
))
6166 d2
(time-to-days (org-time-string-to-time s2
)))
6167 (if (and (> (- d0 d1
) -
1) (> (- d2 d0
) -
1))
6168 ;; Only allow days between the limits, because the normal
6169 ;; date stamps will catch the limits.
6171 (setq marker
(org-agenda-new-marker (point)))
6172 (setq category
(org-get-category))
6173 (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t
)
6175 (setq hdmarker
(org-agenda-new-marker (match-end 1)))
6176 (goto-char (match-end 1))
6177 (setq tags
(org-get-tags-at))
6178 (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
6179 (setq txt
(org-format-agenda-item
6180 (format (if (= d1 d2
) "" "(%d/%d): ")
6181 (1+ (- d0 d1
)) (1+ (- d2 d1
)))
6182 (match-string 1) category tags
6183 (if (= d0 d1
) timestr
))))
6184 (setq txt org-agenda-no-heading-message
))
6185 (org-add-props txt props
6186 'org-marker marker
'org-hd-marker hdmarker
6187 'priority
(org-get-priority txt
) 'category category
)
6189 (outline-next-heading))
6190 ;; Sort the entries by expiration date.
6193 (defconst org-plain-time-of-day-regexp
6196 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
6199 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
6201 "Regular expression to match a plain time or time range.
6202 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
6203 groups carry important information:
6205 1 the first time, range or not
6206 8 the second time, if it is a range.")
6208 (defconst org-stamp-time-of-day-regexp
6210 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +[a-zA-Z]+ +\\)"
6211 "\\([012][0-9]:[0-5][0-9]\\)>"
6213 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
6214 "Regular expression to match a timestamp time or time range.
6215 After a match, the following groups carry important information:
6217 1 date plus weekday, for backreferencing to make sure both times on same day
6218 2 the first time, range or not
6219 4 the second time, if it is a range.")
6221 (defvar org-prefix-has-time nil
6222 "A flag, set by `org-compile-prefix-format'.
6223 The flag is set if the currently compiled format contains a `%t'.")
6224 (defvar org-prefix-has-tag nil
6225 "A flag, set by `org-compile-prefix-format'.
6226 The flag is set if the currently compiled format contains a `%T'.")
6228 (defun org-format-agenda-item (extra txt
&optional category tags dotime noprefix
)
6229 "Format TXT to be inserted into the agenda buffer.
6230 In particular, it adds the prefix and corresponding text properties. EXTRA
6231 must be a string and replaces the `%s' specifier in the prefix format.
6232 CATEGORY (string, symbol or nil) may be used to overrule the default
6233 category taken from local variable or file name. It will replace the `%c'
6234 specifier in the format. DOTIME, when non-nil, indicates that a
6235 time-of-day should be extracted from TXT for sorting of this entry, and for
6236 the `%t' specifier in the format. When DOTIME is a string, this string is
6237 searched for a time before TXT is. NOPREFIX is a flag and indicates that
6238 only the correctly processes TXT should be returned - this is used by
6239 `org-agenda-change-all-lines'. TAG can be the tag of the headline."
6241 ;; Diary entries sometimes have extra whitespace at the beginning
6242 (if (string-match "^ +" txt
) (setq txt
(replace-match "" nil nil txt
)))
6243 (let* ((category (or category
6245 (if buffer-file-name
6246 (file-name-sans-extension
6247 (file-name-nondirectory buffer-file-name
))
6249 (tag (if tags
(nth (1- (length tags
)) tags
) ""))
6250 ;;(tag (or (nth (1- (or (length tags) 0)) tags) "")) FIXME: rm
6251 time
;; needed for the eval of the prefix format
6252 (ts (if dotime
(concat (if (stringp dotime
) dotime
"") txt
)))
6253 (time-of-day (and dotime
(org-get-time-of-day ts
)))
6254 stamp plain s0 s1 s2 rtn
)
6255 (when (and dotime time-of-day org-prefix-has-time
)
6256 ;; Extract starting and ending time and move them to prefix
6257 (when (or (setq stamp
(string-match org-stamp-time-of-day-regexp ts
))
6258 (setq plain
(string-match org-plain-time-of-day-regexp ts
)))
6259 (setq s0
(match-string 0 ts
)
6260 s1
(match-string (if plain
1 2) ts
)
6261 s2
(match-string (if plain
8 4) ts
))
6263 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6264 ;; them, we might want to remove them there to avoid duplication.
6265 ;; The user can turn this off with a variable.
6266 (if (and org-agenda-remove-times-when-in-prefix
(or stamp plain
)
6267 (string-match (concat (regexp-quote s0
) " *") txt
)
6268 (if (eq org-agenda-remove-times-when-in-prefix
'beg
)
6269 (= (match-beginning 0) 0)
6271 (setq txt
(replace-match "" nil nil txt
))))
6272 ;; Normalize the time(s) to 24 hour
6273 (if s1
(setq s1
(org-get-time-of-day s1
'string
)))
6274 (if s2
(setq s2
(org-get-time-of-day s2
'string
))))
6276 (when (and (or (eq org-agenda-remove-tags-when-in-prefix t
)
6277 (and org-agenda-remove-tags-when-in-prefix
6278 org-prefix-has-tag
))
6279 (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" txt
))
6280 (setq txt
(replace-match "" t t txt
)))
6282 ;; Create the final string
6285 ;; Prepare the variables needed in the eval of the compiled format
6286 (setq time
(cond (s2 (concat s1
"-" s2
))
6287 (s1 (concat s1
"......"))
6290 category
(if (symbolp category
) (symbol-name category
) category
))
6291 ;; Evaluate the compiled format
6292 (setq rtn
(concat (eval org-prefix-format-compiled
) txt
)))
6294 ;; And finally add the text properties
6295 (org-add-props rtn nil
6296 'category
(downcase category
) 'tags tags
6297 'prefix-length
(- (length rtn
) (length txt
))
6298 'time-of-day time-of-day
6301 (defun org-agenda-add-time-grid-maybe (list ndays todayp
)
6303 (cond ((not org-agenda-use-time-grid
) (throw 'exit list
))
6304 ((and todayp
(member 'today
(car org-agenda-time-grid
))))
6305 ((and (= ndays
1) (member 'daily
(car org-agenda-time-grid
))))
6306 ((member 'weekly
(car org-agenda-time-grid
)))
6307 (t (throw 'exit list
)))
6308 (let* ((have (delq nil
(mapcar
6309 (lambda (x) (get-text-property 1 'time-of-day x
))
6311 (string (nth 1 org-agenda-time-grid
))
6312 (gridtimes (nth 2 org-agenda-time-grid
))
6313 (req (car org-agenda-time-grid
))
6314 (remove (member 'remove-match req
))
6316 (if (and (member 'require-timed req
) (not have
))
6317 ;; don't show empty grid
6319 (while (setq time
(pop gridtimes
))
6320 (unless (and remove
(member time have
))
6321 (setq time
(int-to-string time
))
6322 (push (org-format-agenda-item
6324 (concat (substring time
0 -
2) ":" (substring time -
2)))
6327 1 (length (car new
)) 'face
'org-time-grid
(car new
))))
6328 (if (member 'time-up org-agenda-sorting-strategy
)
6330 (append list new
)))))
6332 (defun org-compile-prefix-format (format)
6333 "Compile the prefix format into a Lisp form that can be evaluated.
6334 The resulting form is returned and stored in the variable
6335 `org-prefix-format-compiled'."
6336 (setq org-prefix-has-time nil org-prefix-has-tag nil
)
6337 (let ((start 0) varform vars var
(s format
)e c f opt
)
6338 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
6340 (setq var
(cdr (assoc (match-string 4 s
)
6341 '(("c" . category
) ("t" . time
) ("s" . extra
)
6343 c
(or (match-string 3 s
) "")
6344 opt
(match-beginning 1)
6345 start
(1+ (match-beginning 0)))
6346 (if (equal var
'time
) (setq org-prefix-has-time t
))
6347 (if (equal var
'tag
) (setq org-prefix-has-tag t
))
6348 (setq f
(concat "%" (match-string 2 s
) "s"))
6351 `(if (equal "" ,var
)
6353 (format ,f
(if (equal "" ,var
) "" (concat ,var
,c
)))))
6354 (setq varform
`(format ,f
(if (equal ,var
"") "" (concat ,var
,c
)))))
6355 (setq s
(replace-match "%s" t nil s
))
6356 (push varform vars
))
6357 (setq vars
(nreverse vars
))
6358 (setq org-prefix-format-compiled
`(format ,s
,@vars
))))
6360 (defun org-get-time-of-day (s &optional string
)
6361 "Check string S for a time of day.
6362 If found, return it as a military time number between 0 and 2400.
6363 If not found, return nil.
6364 The optional STRING argument forces conversion into a 5 character wide string
6370 "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s
)
6372 "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s
))
6373 (let* ((t0 (+ (* 100
6374 (+ (string-to-number (match-string 1 s
))
6375 (if (and (match-beginning 4)
6376 (equal (downcase (match-string 4 s
)) "pm"))
6378 (if (match-beginning 3)
6379 (string-to-number (match-string 3 s
))
6382 (if (< t0
100) "0" "") (if (< t0
10) "0" "")
6383 (int-to-string t0
))))
6384 (if string
(concat (substring t1 -
4 -
2) ":" (substring t1 -
2)) t0
)))))
6386 (defun org-finalize-agenda-entries (list)
6387 "Sort and concatenate the agenda items."
6388 (mapconcat 'identity
(sort list
'org-entries-lessp
) "\n"))
6390 (defsubst org-cmp-priority
(a b
)
6391 "Compare the priorities of string A and B."
6392 (let ((pa (or (get-text-property 1 'priority a
) 0))
6393 (pb (or (get-text-property 1 'priority b
) 0)))
6394 (cond ((> pa pb
) +1)
6398 (defsubst org-cmp-category
(a b
)
6399 "Compare the string values of categories of strings A and B."
6400 (let ((ca (or (get-text-property 1 'category a
) ""))
6401 (cb (or (get-text-property 1 'category b
) "")))
6402 (cond ((string-lessp ca cb
) -
1)
6403 ((string-lessp cb ca
) +1)
6406 (defsubst org-cmp-time
(a b
)
6407 "Compare the time-of-day values of strings A and B."
6408 (let* ((def (if org-sort-agenda-notime-is-late
2401 -
1))
6409 (ta (or (get-text-property 1 'time-of-day a
) def
))
6410 (tb (or (get-text-property 1 'time-of-day b
) def
)))
6411 (cond ((< ta tb
) -
1)
6415 (defun org-entries-lessp (a b
)
6416 "Predicate for sorting agenda entries."
6417 ;; The following variables will be used when the form is evaluated.
6418 (let* ((time-up (org-cmp-time a b
))
6419 (time-down (if time-up
(- time-up
) nil
))
6420 (priority-up (org-cmp-priority a b
))
6421 (priority-down (if priority-up
(- priority-up
) nil
))
6422 (category-up (org-cmp-category a b
))
6423 (category-down (if category-up
(- category-up
) nil
))
6424 (category-keep (if category-up
+1 nil
)))
6426 (eval (cons 'or org-agenda-sorting-strategy
))
6427 '((-1 . t
) (1 . nil
) (nil . nil
))))))
6429 (defun org-agenda-show-priority ()
6430 "Show the priority of the current item.
6431 This priority is composed of the main priority given with the [#A] cookies,
6432 and by additional input from the age of a schedules or deadline entry."
6434 (let* ((pri (get-text-property (point-at-bol) 'priority
)))
6435 (message "Priority is %d" (if pri pri -
1000))))
6437 (defun org-agenda-show-tags ()
6438 "Show the tags applicable to the current item."
6440 (let* ((tags (get-text-property (point-at-bol) 'tags
)))
6442 (message "Tags are :%s:" (mapconcat 'identity tags
":"))
6443 (message "No tags associated with this line"))))
6445 (defun org-agenda-goto (&optional highlight
)
6446 "Go to the Org-mode file which contains the item at point."
6448 (let* ((marker (or (get-text-property (point) 'org-marker
)
6449 (org-agenda-error)))
6450 (buffer (marker-buffer marker
))
6451 (pos (marker-position marker
)))
6452 (switch-to-buffer-other-window buffer
)
6455 (when (eq major-mode
'org-mode
)
6456 (org-show-hidden-entry)
6458 (and (outline-next-heading)
6459 (org-flag-heading nil
)))) ; show the next heading
6460 (and highlight
(org-highlight (point-at-bol) (point-at-eol)))))
6462 (defun org-agenda-switch-to ()
6463 "Go to the Org-mode file which contains the item at point."
6465 (let* ((marker (or (get-text-property (point) 'org-marker
)
6466 (org-agenda-error)))
6467 (buffer (marker-buffer marker
))
6468 (pos (marker-position marker
)))
6469 (switch-to-buffer buffer
)
6470 (delete-other-windows)
6473 (when (eq major-mode
'org-mode
)
6474 (org-show-hidden-entry)
6476 (and (outline-next-heading)
6477 (org-flag-heading nil
)))))) ; show the next heading
6479 (defun org-agenda-goto-mouse (ev)
6480 "Go to the Org-mode file which contains the item at the mouse click."
6482 (mouse-set-point ev
)
6485 (defun org-agenda-show ()
6486 "Display the Org-mode file which contains the item at point."
6488 (let ((win (selected-window)))
6490 (select-window win
)))
6492 (defun org-agenda-recenter (arg)
6493 "Display the Org-mode file which contains the item at point and recenter."
6495 (let ((win (selected-window)))
6498 (select-window win
)))
6500 (defun org-agenda-show-mouse (ev)
6501 "Display the Org-mode file which contains the item at the mouse click."
6503 (mouse-set-point ev
)
6506 (defun org-agenda-check-no-diary ()
6507 "Check if the entry is a diary link and abort if yes."
6508 (if (get-text-property (point) 'org-agenda-diary-link
)
6509 (org-agenda-error)))
6511 (defun org-agenda-error ()
6512 (error "Command not allowed in this line"))
6514 (defvar org-last-heading-marker
(make-marker)
6515 "Marker pointing to the headline that last changed its TODO state
6516 by a remote command from the agenda.")
6518 (defun org-agenda-todo (&optional arg
)
6519 "Cycle TODO state of line at point, also in Org-mode file.
6520 This changes the line at point, all other lines in the agenda referring to
6521 the same tree node, and the headline of the tree node in the Org-mode file."
6523 (org-agenda-check-no-diary)
6524 (let* ((col (current-column))
6525 (marker (or (get-text-property (point) 'org-marker
)
6526 (org-agenda-error)))
6527 (buffer (marker-buffer marker
))
6528 (pos (marker-position marker
))
6529 (hdmarker (get-text-property (point) 'org-hd-marker
))
6530 (buffer-read-only nil
)
6532 (with-current-buffer buffer
6535 (org-show-hidden-entry)
6537 (and (outline-next-heading)
6538 (org-flag-heading nil
))) ; show the next heading
6541 (setq newhead
(org-get-heading))
6543 (org-back-to-heading)
6544 (move-marker org-last-heading-marker
(point))))
6545 (beginning-of-line 1)
6547 (org-agenda-change-all-lines newhead hdmarker
'fixface
))
6548 (move-to-column col
)))
6550 (defun org-agenda-change-all-lines (newhead hdmarker
&optional fixface
)
6551 "Change all lines in the agenda buffer which match HDMARKER.
6552 The new content of the line will be NEWHEAD (as modified by
6553 `org-format-agenda-item'). HDMARKER is checked with
6554 `equal' against all `org-hd-marker' text properties in the file.
6555 If FIXFACE is non-nil, the face of each item is modified acording to
6556 the new TODO state."
6557 (let* (props m pl undone-face done-face finish new dotime cat tags
)
6558 ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
6560 (goto-char (point-max))
6561 (beginning-of-line 1)
6563 (setq finish
(bobp))
6564 (when (and (setq m
(get-text-property (point) 'org-hd-marker
))
6566 (setq props
(text-properties-at (point))
6567 dotime
(get-text-property (point) 'dotime
)
6568 cat
(get-text-property (point) 'category
)
6569 tags
(get-text-property (point) 'tags
)
6570 new
(org-format-agenda-item "x" newhead cat tags dotime
'noprefix
)
6571 pl
(get-text-property (point) 'prefix-length
)
6572 undone-face
(get-text-property (point) 'undone-face
)
6573 done-face
(get-text-property (point) 'done-face
))
6575 (if (looking-at ".*")
6577 (replace-match new t t
)
6578 (beginning-of-line 1)
6579 (add-text-properties (point-at-bol) (point-at-eol) props
)
6581 (add-text-properties
6582 (point-at-bol) (point-at-eol)
6584 (if org-last-todo-state-is-todo
6585 undone-face done-face
))))
6586 (beginning-of-line 1))
6587 (error "Line update did not work")))
6588 (beginning-of-line 0)))))
6590 (defun org-agenda-priority-up ()
6591 "Increase the priority of line at point, also in Org-mode file."
6593 (org-agenda-priority 'up
))
6595 (defun org-agenda-priority-down ()
6596 "Decrease the priority of line at point, also in Org-mode file."
6598 (org-agenda-priority 'down
))
6600 (defun org-agenda-priority (&optional force-direction
)
6601 "Set the priority of line at point, also in Org-mode file.
6602 This changes the line at point, all other lines in the agenda referring to
6603 the same tree node, and the headline of the tree node in the Org-mode file."
6605 (org-agenda-check-no-diary)
6606 (let* ((marker (or (get-text-property (point) 'org-marker
)
6607 (org-agenda-error)))
6608 (buffer (marker-buffer marker
))
6609 (pos (marker-position marker
))
6610 (hdmarker (get-text-property (point) 'org-hd-marker
))
6611 (buffer-read-only nil
)
6613 (with-current-buffer buffer
6616 (org-show-hidden-entry)
6618 (and (outline-next-heading)
6619 (org-flag-heading nil
))) ; show the next heading
6620 (funcall 'org-priority force-direction
)
6622 (setq newhead
(org-get-heading)))
6623 (org-agenda-change-all-lines newhead hdmarker
)
6624 (beginning-of-line 1)))
6626 (defun org-get-tags-at (&optional pos
)
6627 "Get a list of all headline targs applicable at POS.
6628 POS defaults to point. If tags are inherited, the list contains
6629 the targets in the same sequence as the headlines appear, i.e.
6630 the tags of the current headline come last."
6634 (goto-char (or pos
(point)))
6636 (org-back-to-heading t
)
6639 (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
6640 (setq tags
(append (org-split-string (match-string 1) ":") tags
)))
6641 (or org-use-tag-inheritance
(error ""))
6642 (org-up-heading-all 1))
6647 (defun org-agenda-set-tags ()
6648 "Set tags for the current headline."
6650 (org-agenda-check-no-diary)
6651 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6652 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker
)
6653 (org-agenda-error)))
6654 (buffer (marker-buffer hdmarker
))
6655 (pos (marker-position hdmarker
))
6656 (buffer-read-only nil
)
6658 (with-current-buffer buffer
6661 (org-show-hidden-entry)
6663 (and (outline-next-heading)
6664 (org-flag-heading nil
))) ; show the next heading
6665 (call-interactively 'org-set-tags
)
6667 (setq newhead
(org-get-heading)))
6668 (org-agenda-change-all-lines newhead hdmarker
)
6669 (beginning-of-line 1)))
6671 (defun org-agenda-date-later (arg &optional what
)
6672 "Change the date of this item to one day later."
6674 (org-agenda-check-type t
'agenda
'timeline
)
6675 (org-agenda-check-no-diary)
6676 (let* ((marker (or (get-text-property (point) 'org-marker
)
6677 (org-agenda-error)))
6678 (buffer (marker-buffer marker
))
6679 (pos (marker-position marker
)))
6680 (with-current-buffer buffer
6683 (if (not (org-at-timestamp-p))
6684 (error "Cannot find time stamp"))
6685 (org-timestamp-change arg
(or what
'day
))
6686 (message "Time stamp changed to %s" org-last-changed-timestamp
))))
6688 (defun org-agenda-date-earlier (arg &optional what
)
6689 "Change the date of this item to one day earlier."
6691 (org-agenda-date-later (- arg
) what
))
6693 (defun org-agenda-date-prompt (arg)
6694 "Change the date of this item. Date is prompted for, with default today.
6695 The prefix ARG is passed to the `org-time-stamp' command and can therefore
6696 be used to request time specification in the time stamp."
6698 (org-agenda-check-type t
'agenda
'timeline
)
6699 (org-agenda-check-no-diary)
6700 (let* ((marker (or (get-text-property (point) 'org-marker
)
6701 (org-agenda-error)))
6702 (buffer (marker-buffer marker
))
6703 (pos (marker-position marker
)))
6704 (with-current-buffer buffer
6707 (if (not (org-at-timestamp-p))
6708 (error "Cannot find time stamp"))
6709 (org-time-stamp arg
)
6710 (message "Time stamp changed to %s" org-last-changed-timestamp
))))
6712 (defun org-get-heading ()
6713 "Return the heading of the current entry, without the stars."
6715 (and (memq (char-before) '(?
\n ?
\r)) (skip-chars-forward "^\n\r"))
6716 (if (and (re-search-backward "[\r\n]\\*" nil t
)
6717 (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
6721 (defun org-agenda-diary-entry ()
6722 "Make a diary entry, like the `i' command from the calendar.
6723 All the standard commands work: block, weekly etc."
6725 (org-agenda-check-type t
'agenda
'timeline
)
6726 (require 'diary-lib
)
6728 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
6729 (read-char-exclusive)))
6730 (cmd (cdr (assoc char
6731 '((?d . insert-diary-entry
)
6732 (?w . insert-weekly-diary-entry
)
6733 (?m . insert-monthly-diary-entry
)
6734 (?y . insert-yearly-diary-entry
)
6735 (?a . insert-anniversary-diary-entry
)
6736 (?b . insert-block-diary-entry
)
6737 (?c . insert-cyclic-diary-entry
)))))
6738 (oldf (symbol-function 'calendar-cursor-to-date
))
6740 (mark (or (mark t
) (point))))
6742 (error "No command associated with <%c>" char
))
6743 (unless (and (get-text-property point
'day
)
6744 (or (not (equal ?b char
))
6745 (get-text-property mark
'day
)))
6746 (error "Don't know which date to use for diary entry"))
6747 ;; We implement this by hacking the `calendar-cursor-to-date' function
6748 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
6749 (let ((calendar-mark-ring
6750 (list (calendar-gregorian-from-absolute
6751 (or (get-text-property mark
'day
)
6752 (get-text-property point
'day
))))))
6755 (fset 'calendar-cursor-to-date
6756 (lambda (&optional error
)
6757 (calendar-gregorian-from-absolute
6758 (get-text-property point
'day
))))
6759 (call-interactively cmd
))
6760 (fset 'calendar-cursor-to-date oldf
)))))
6763 (defun org-agenda-execute-calendar-command (cmd)
6764 "Execute a calendar command from the agenda, with the date associated to
6765 the cursor position."
6766 (org-agenda-check-type t
'agenda
'timeline
)
6767 (require 'diary-lib
)
6768 (unless (get-text-property (point) 'day
)
6769 (error "Don't know which date to use for calendar command"))
6770 (let* ((oldf (symbol-function 'calendar-cursor-to-date
))
6772 (date (calendar-gregorian-from-absolute
6773 (get-text-property point
'day
)))
6774 (displayed-day (extract-calendar-day date
))
6775 (displayed-month (extract-calendar-month date
))
6776 (displayed-year (extract-calendar-year date
)))
6779 (fset 'calendar-cursor-to-date
6780 (lambda (&optional error
)
6781 (calendar-gregorian-from-absolute
6782 (get-text-property point
'day
))))
6783 (call-interactively cmd
))
6784 (fset 'calendar-cursor-to-date oldf
))))
6786 (defun org-agenda-phases-of-moon ()
6787 "Display the phases of the moon for the 3 months around the cursor date."
6789 (org-agenda-execute-calendar-command 'calendar-phases-of-moon
))
6791 (defun org-agenda-holidays ()
6792 "Display the holidays for the 3 months around the cursor date."
6794 (org-agenda-execute-calendar-command 'list-calendar-holidays
))
6796 (defun org-agenda-sunrise-sunset (arg)
6797 "Display sunrise and sunset for the cursor date.
6798 Latitude and longitude can be specified with the variables
6799 `calendar-latitude' and `calendar-longitude'. When called with prefix
6800 argument, latitude and longitude will be prompted for."
6802 (let ((calendar-longitude (if arg nil calendar-longitude
))
6803 (calendar-latitude (if arg nil calendar-latitude
))
6804 (calendar-location-name
6805 (if arg
"the given coordinates" calendar-location-name
)))
6806 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset
)))
6808 (defun org-agenda-goto-calendar ()
6809 "Open the Emacs calendar with the date at the cursor."
6811 (org-agenda-check-type t
'agenda
'timeline
)
6812 (let* ((day (or (get-text-property (point) 'day
)
6813 (error "Don't know which date to open in calendar")))
6814 (date (calendar-gregorian-from-absolute day
))
6815 (calendar-move-hook nil
)
6816 (view-diary-entries-initially nil
))
6818 (calendar-goto-date date
)))
6820 (defun org-calendar-goto-agenda ()
6821 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
6822 This is a command that has to be installed in `calendar-mode-map'."
6824 (org-agenda-list nil
(calendar-absolute-from-gregorian
6825 (calendar-cursor-to-date))
6828 (defun org-agenda-convert-date ()
6830 (org-agenda-check-type t
'agenda
'timeline
)
6831 (let ((day (get-text-property (point) 'day
))
6834 (error "Don't know which date to convert"))
6835 (setq date
(calendar-gregorian-from-absolute day
))
6837 "Gregorian: " (calendar-date-string date
) "\n"
6838 "ISO: " (calendar-iso-date-string date
) "\n"
6839 "Day of Yr: " (calendar-day-of-year-string date
) "\n"
6840 "Julian: " (calendar-julian-date-string date
) "\n"
6841 "Astron. JD: " (calendar-astro-date-string date
)
6842 " (Julian date number at noon UTC)\n"
6843 "Hebrew: " (calendar-hebrew-date-string date
) " (until sunset)\n"
6844 "Islamic: " (calendar-islamic-date-string date
) " (until sunset)\n"
6845 "French: " (calendar-french-date-string date
) "\n"
6846 "Mayan: " (calendar-mayan-date-string date
) "\n"
6847 "Coptic: " (calendar-coptic-date-string date
) "\n"
6848 "Ethiopic: " (calendar-ethiopic-date-string date
) "\n"
6849 "Persian: " (calendar-persian-date-string date
) "\n"
6850 "Chinese: " (calendar-chinese-date-string date
) "\n"))
6851 (with-output-to-temp-buffer "*Dates*"
6853 (if (fboundp 'fit-window-to-buffer
)
6854 (fit-window-to-buffer (get-buffer-window "*Dates*")))))
6858 (defun org-scan-tags (action matcher
&optional todo-only
)
6859 "Scan headline tags with inheritance and produce output ACTION.
6860 ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
6861 evaluated, testing if a given set of tags qualifies a headline for
6862 inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
6863 are included in the output."
6864 (let* ((re (concat "[\n\r]" outline-regexp
" *\\(\\<\\("
6865 (mapconcat 'regexp-quote
6866 (nreverse (cdr (reverse org-todo-keywords
)))
6868 "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*[\n\r]"))
6869 (props (list 'face nil
6870 'done-face
'org-done
6872 'mouse-face
'highlight
6873 'keymap org-agenda-keymap
6875 (format "mouse-2 or RET jump to org file %s"
6876 (abbreviate-file-name buffer-file-name
))))
6878 tags tags-list tags-alist
(llast 0) rtn level category i txt
6882 (goto-char (point-min))
6883 (when (eq action
'sparse-tree
) (hide-sublevels 1))
6884 (while (re-search-forward re nil t
)
6885 (setq todo
(if (match-end 1) (match-string 2))
6886 tags
(if (match-end 4) (match-string 4)))
6887 (goto-char (setq lspos
(1+ (match-beginning 0))))
6888 (setq level
(funcall outline-level
)
6889 category
(org-get-category))
6890 (setq i llast llast level
)
6891 ;; remove tag lists from same and sublevels
6893 (when (setq entry
(assoc i tags-alist
))
6894 (setq tags-alist
(delete entry tags-alist
)))
6898 (setq tags
(mapcar 'downcase
(org-split-string tags
":"))
6900 (cons (cons level tags
) tags-alist
)))
6901 ;; compile tags for current headline
6903 (if org-use-tag-inheritance
6904 (apply 'append
(mapcar 'cdr tags-alist
))
6906 (when (and (or (not todo-only
) todo
)
6908 ;; list this headline
6909 (if (eq action
'sparse-tree
)
6911 (org-show-hierarchy-above))
6912 (setq txt
(org-format-agenda-item
6915 (if org-tags-match-list-sublevels
6916 (make-string (1- level
) ?.
) "")
6918 category tags-list
))
6920 (setq marker
(org-agenda-new-marker))
6921 (org-add-props txt props
6922 'org-marker marker
'org-hd-marker marker
'category category
)
6924 ;; if we are to skip sublevels, jump to end of subtree
6926 (or org-tags-match-list-sublevels
(org-end-of-subtree)))))
6929 (defun org-tags-sparse-tree (&optional arg match
)
6930 "Create a sparse tree according to tags search string MATCH.
6931 MATCH can contain positive and negative selection of tags, like
6932 \"+WORK+URGENT-WITHBOSS\"."
6934 (let ((org-show-following-heading nil
)
6935 (org-show-hierarchy-above nil
))
6936 (org-scan-tags 'sparse-tree
(cdr (org-make-tags-matcher match
)))))
6938 (defun org-make-tags-matcher (match)
6939 "Create the TAGS matcher form for the tags-selecting string MATCH."
6941 ;; Get a new match request, with completion
6942 (setq org-last-tags-completion-table
6943 (or (org-get-buffer-tags)
6944 org-last-tags-completion-table
))
6945 (setq match
(completing-read
6946 "Tags: " 'org-tags-completion-function nil nil nil
6947 'org-tags-history
)))
6948 ;; parse the string and create a lisp form
6949 (let ((match0 match
) minus tag mm matcher orterms term orlist
)
6950 (setq orterms
(org-split-string match
"|"))
6951 (while (setq term
(pop orterms
))
6952 (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term
)
6953 (setq minus
(and (match-end 1)
6954 (equal (match-string 1 term
) "-"))
6955 tag
(match-string 2 term
)
6956 term
(substring term
(match-end 0))
6957 mm
(list 'member
(downcase tag
) 'tags-list
)
6958 mm
(if minus
(list 'not mm
) mm
))
6960 (push (if (> (length matcher
) 1) (cons 'and matcher
) (car matcher
))
6963 (setq matcher
(if (> (length orlist
) 1) (cons 'or orlist
) (car orlist
)))
6964 ;; Return the string and lisp forms of the matcher
6965 (cons match0 matcher
)))
6968 (defun org-tags-view (&optional todo-only match keep-modes
)
6969 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
6970 The prefix arg TODO-ONLY limits the search to TODO entries."
6972 (org-agenda-maybe-reset-markers 'force
)
6973 (org-compile-prefix-format org-agenda-prefix-format
)
6974 (let* ((org-agenda-keep-modes keep-modes
)
6975 (org-tags-match-list-sublevels
6976 (if todo-only t org-tags-match-list-sublevels
))
6977 (win (selected-window))
6978 (completion-ignore-case t
)
6979 rtn rtnall files file pos matcher
6981 (setq matcher
(org-make-tags-matcher match
)
6982 match
(car matcher
) matcher
(cdr matcher
))
6983 (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name
)))
6985 (delete-other-windows)
6986 (switch-to-buffer-other-window
6987 (get-buffer-create org-agenda-buffer-name
))))
6988 (setq buffer-read-only nil
)
6990 (org-agenda-mode) (setq buffer-read-only nil
)
6991 (set (make-local-variable 'org-agenda-type
) 'tags
)
6992 (set (make-local-variable 'org-agenda-redo-command
)
6993 (list 'org-tags-view
(list 'quote todo-only
)
6994 (list 'if
'current-prefix-arg nil match
) t
))
6995 (setq files
(org-agenda-files)
6997 (while (setq file
(pop files
))
6999 (org-check-agenda-file file
)
7000 (setq buffer
(if (file-exists-p file
)
7001 (org-get-agenda-file-buffer file
)
7002 (error "No such file %s" file
)))
7004 ;; If file does not exist, merror message to agenda
7006 (format "ORG-AGENDA-ERROR: No such org-file %s" file
))
7007 rtnall
(append rtnall rtn
))
7008 (with-current-buffer buffer
7009 (unless (eq major-mode
'org-mode
)
7010 (error "Agenda file %s is not in `org-mode'" file
))
7013 (if org-respect-restriction
7014 (if (org-region-active-p)
7015 ;; Respect a region to restrict search
7016 (narrow-to-region (region-beginning) (region-end)))
7017 ;; If we work for the calendar or many files,
7018 ;; get rid of any restriction
7020 (setq rtn
(org-scan-tags 'agenda matcher todo-only
))
7021 (setq rtnall
(append rtnall rtn
))))))))
7022 (insert "Headlines with TAGS match: ")
7023 (add-text-properties (point-min) (1- (point))
7024 (list 'face
'org-level-3
))
7027 (add-text-properties pos
(1- (point)) (list 'face
'org-warning
))
7029 (insert "Press `C-u r' to search again with new search string\n")
7030 (add-text-properties pos
(1- (point)) (list 'face
'org-level-3
))
7032 (insert (mapconcat 'identity rtnall
"\n")))
7033 (goto-char (point-min))
7034 (setq buffer-read-only t
)
7035 (org-fit-agenda-window)
7036 (if (not org-select-agenda-window
) (select-window win
))))
7038 (defvar org-add-colon-after-tag-completion nil
) ;; dynamically skoped param
7039 (defun org-set-tags (&optional arg just-align
)
7040 "Set the tags for the current headline.
7041 With prefix ARG, realign all tags in headings in the current buffer."
7043 (let* (;(inherit (org-get-inherited-tags))
7044 (re (concat "^" outline-regexp
))
7045 (col (current-column))
7046 (current (org-get-tags))
7047 tags hd empty invis
)
7050 (goto-char (point-min))
7051 (while (re-search-forward re nil t
)
7052 (org-set-tags nil t
))
7053 (message "All tags realigned to column %d" org-tags-column
))
7056 (setq org-last-tags-completion-table
7057 (or (org-get-buffer-tags)
7058 org-last-tags-completion-table
))
7060 (let ((org-add-colon-after-tag-completion t
))
7061 (completing-read "Tags: " 'org-tags-completion-function
7062 nil nil current
'org-tags-history
)))
7063 (while (string-match "[-+&]+" tags
)
7064 (setq tags
(replace-match ":" t t tags
))))
7065 ;; FIXME: still optimize this by not checking when JUST-ALIGN?
7066 (unless (setq empty
(string-match "\\`[\t ]*\\'" tags
))
7067 (unless (string-match ":$" tags
) (setq tags
(concat tags
":")))
7068 (unless (string-match "^:" tags
) (setq tags
(concat ":" tags
))))
7069 (if (equal current
"")
7072 (or empty
(insert-before-markers " ")))
7073 (beginning-of-line 1)
7074 (setq invis
(org-invisible-p))
7075 (looking-at (concat "\\(.*\\)\\(" (regexp-quote current
) "\\)[ \t]*"))
7076 (setq hd
(match-string 1))
7077 (delete-region (match-beginning 0) (match-end 0))
7078 (insert-before-markers (org-trim hd
) (if empty
"" " ")))
7079 (unless (equal tags
"")
7080 (move-to-column (max (current-column)
7081 (if (> org-tags-column
0)
7083 (- (- org-tags-column
) (length tags
))))
7085 (insert-before-markers tags
)
7086 (if (and (not invis
) (org-invisible-p))
7087 (outline-flag-region (point-at-bol) (point) nil
)))
7088 (move-to-column col
))))
7090 (defun org-tags-completion-function (string predicate
&optional flag
)
7091 (let (s1 s2 rtn
(ctable org-last-tags-completion-table
))
7092 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string
)
7093 (setq s1
(match-string 1 string
)
7094 s2
(match-string 2 string
))
7095 (setq s1
"" s2 string
))
7099 (setq rtn
(try-completion s2 ctable
))
7101 (concat s1 s2
(substring rtn
(length s2
))
7102 (if (and org-add-colon-after-tag-completion
7108 (all-completions s2 ctable
)
7115 (defun org-get-tags ()
7116 "Get the TAGS string in the current headline."
7117 (unless (org-on-heading-p)
7118 (error "Not on a heading"))
7120 (beginning-of-line 1)
7121 (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
7122 (org-match-string-no-properties 1)
7125 (defun org-get-buffer-tags ()
7126 "Get a table of all tags used in the buffer, for completion."
7129 (goto-char (point-min))
7130 (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t
)
7131 (mapc (lambda (x) (add-to-list 'tags x
))
7132 (org-split-string (org-match-string-no-properties 1) ":"))))
7133 (mapcar 'list tags
)))
7137 (defun org-find-file-at-mouse (ev)
7138 "Open file link or URL at mouse."
7140 (mouse-set-point ev
)
7141 (org-open-at-point 'in-emacs
))
7143 (defun org-open-at-mouse (ev)
7144 "Open file link or URL at mouse."
7146 (mouse-set-point ev
)
7147 (org-open-at-point))
7149 (defun org-open-at-point (&optional in-emacs
)
7150 "Open link at or after point.
7151 If there is no link at point, this function will search forward up to
7152 the end of the current subtree.
7153 Normally, files will be opened by an appropriate application. If the
7154 optional argument IN-EMACS is non-nil, Emacs will visit the file."
7156 (org-remove-occur-highlights nil nil t
)
7157 (if (org-at-timestamp-p)
7158 (org-agenda-list nil
(time-to-days (org-time-string-to-time
7159 (substring (match-string 1) 0 10)))
7161 (let (type path link line search
(pos (point)))
7164 (skip-chars-forward "^]\n\r")
7165 (when (and (re-search-backward "\\[\\[" nil t
)
7166 (looking-at org-bracket-link-regexp
)
7167 (<= (match-beginning 0) pos
)
7168 (>= (match-end 0) pos
))
7169 (setq link
(org-link-unescape (org-match-string-no-properties 1)))
7170 (while (string-match " *\n *" link
)
7171 (setq link
(replace-match " " t t link
)))
7172 (if (string-match org-link-re-with-space2 link
)
7173 (setq type
(match-string 1 link
)
7174 path
(match-string 2 link
))
7175 (setq type
"thisfile"
7179 (when (get-text-property (point) 'org-linked-text
)
7180 (setq type
"thisfile"
7181 pos
(if (get-text-property (1+ (point)) 'org-linked-text
)
7182 (1+ (point)) (point))
7183 path
(buffer-substring
7184 (previous-single-property-change pos
'org-linked-text
)
7185 (next-single-property-change pos
'org-linked-text
)))
7189 (skip-chars-backward (concat "^[]" org-non-link-chars
" "))
7190 (if (equal (char-before) ?
<) (backward-char 1))
7191 (when (or (looking-at org-angle-link-re
)
7192 (looking-at org-plain-link-re
)
7193 (and (or (re-search-forward org-angle-link-re
(point-at-eol) t
)
7194 (re-search-forward org-plain-link-re
(point-at-eol) t
))
7195 (<= (match-beginning 0) pos
)
7196 (>= (match-end 0) pos
)))
7197 (setq type
(match-string 1)
7198 path
(match-string 2))
7201 (skip-chars-backward "^ \t\n\r")
7202 (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
7204 path
(match-string 1))
7205 (while (string-match ":" path
)
7206 (setq path
(replace-match "+" t t path
)))
7209 (skip-chars-backward "a-zA-Z_")
7210 (when (and (memq 'camel org-activate-links
)
7211 (looking-at org-camel-regexp
))
7212 (setq type
"camel" path
(match-string 0))
7213 (if (equal (char-before) ?
*)
7214 (setq path
(concat "*" path
))))
7217 (error "No link found"))
7218 ;; Remove any trailing spaces in path
7219 (if (string-match " +\\'" path
)
7220 (setq path
(replace-match "" t t path
)))
7224 ((member type
'("http" "https" "ftp" "mailto" "news"))
7225 (browse-url (concat type
":" path
)))
7227 ((string= type
"tags")
7228 (org-tags-view in-emacs path
))
7229 ((or (string= type
"camel")
7230 (string= type
"thisfile"))
7231 (org-mark-ring-push)
7234 (cond ((equal in-emacs
'(4)) 'occur
)
7235 ((equal in-emacs
'(16)) 'org-occur
)
7238 ((string= type
"file")
7239 (if (string-match "::?\\([0-9]+\\)\\'" path
) ;; second : optional
7240 (setq line
(string-to-number (match-string 1 path
))
7241 path
(substring path
0 (match-beginning 0)))
7242 (if (string-match "::\\(.+\\)\\'" path
)
7243 (setq search
(match-string 1 path
)
7244 path
(substring path
0 (match-beginning 0)))))
7245 (org-open-file path in-emacs line search
))
7247 ((string= type
"news")
7248 (org-follow-gnus-link path
))
7250 ((string= type
"bbdb")
7251 (org-follow-bbdb-link path
))
7253 ((string= type
"gnus")
7254 (let (group article
)
7255 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path
))
7256 (error "Error in Gnus link"))
7257 (setq group
(match-string 1 path
)
7258 article
(match-string 3 path
))
7259 (org-follow-gnus-link group article
)))
7261 ((string= type
"vm")
7262 (let (folder article
)
7263 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path
))
7264 (error "Error in VM link"))
7265 (setq folder
(match-string 1 path
)
7266 article
(match-string 3 path
))
7267 ;; in-emacs is the prefix arg, will be interpreted as read-only
7268 (org-follow-vm-link folder article in-emacs
)))
7270 ((string= type
"wl")
7271 (let (folder article
)
7272 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path
))
7273 (error "Error in Wanderlust link"))
7274 (setq folder
(match-string 1 path
)
7275 article
(match-string 3 path
))
7276 (org-follow-wl-link folder article
)))
7278 ((string= type
"mhe")
7279 (let (folder article
)
7280 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path
))
7281 (error "Error in MHE link"))
7282 (setq folder
(match-string 1 path
)
7283 article
(match-string 3 path
))
7284 (org-follow-mhe-link folder article
)))
7286 ((string= type
"rmail")
7287 (let (folder article
)
7288 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path
))
7289 (error "Error in RMAIL link"))
7290 (setq folder
(match-string 1 path
)
7291 article
(match-string 3 path
))
7292 (org-follow-rmail-link folder article
)))
7294 ((string= type
"shell")
7296 (while (string-match "@{" cmd
)
7297 (setq cmd
(replace-match "<" t t cmd
)))
7298 (while (string-match "@}" cmd
)
7299 (setq cmd
(replace-match ">" t t cmd
)))
7300 (if (or (not org-confirm-shell-links
)
7301 (funcall org-confirm-shell-links
7302 (format "Execute \"%s\" in shell? "
7303 (org-add-props cmd nil
7304 'face
'org-warning
))))
7309 (browse-url-at-point))))))
7311 (defun org-link-search (s &optional type
)
7312 "Search for a link search option.
7313 When S is a CamelCaseWord, search for a target, or for a sentence containing
7314 the words. If S is surrounded by forward slashes, it is interpreted as a
7315 regular expression. In org-mode files, this will create an `org-occur'
7316 sparse tree. In ordinary files, `occur' will be used to list matches.
7317 If the current buffer is in `dired-mode', grep will be used to search
7319 (let ((case-fold-search t
)
7320 (s0 (mapconcat 'identity
(org-split-string s
"[ \t\r\n]+") " "))
7323 words re0 re1 re2 re3 re4 re5 re2a reall camel
)
7324 (cond ((save-excursion
7325 (goto-char (point-min))
7328 (concat "<<" (regexp-quote s0
) ">>") nil t
)
7329 (setq pos
(match-beginning 0))))
7330 ;; There is an exact target for this
7332 ((string-match "^/\\(.*\\)/$" s
)
7333 ;; A regular expression
7335 ((eq major-mode
'org-mode
)
7336 (org-occur (match-string 1 s
)))
7337 ;;((eq major-mode 'dired-mode)
7338 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
7339 (t (org-do-occur (match-string 1 s
)))))
7340 ((or (setq camel
(string-match (concat "^" org-camel-regexp
"$") s
))
7342 ;; A camel or a normal search string
7343 (when (equal (string-to-char s
) ?
*)
7344 ;; Anchor on headlines, post may include tags.
7345 (setq pre
"^\\*+[ \t]*\\(\\sw+\\)?[ \t]*"
7346 post
"[ \t]*\\([ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
7348 (remove-text-properties
7350 '(face nil mouse-face nil keymap nil fontified nil
) s
)
7351 ;; Make a series of regular expressions to find a match
7354 (org-camel-to-words s
)
7355 (org-split-string s
"[ \n\r\t]+"))
7356 re0
(concat "<<" (regexp-quote s0
) ">>")
7357 re2
(concat "\\<" (mapconcat 'downcase words
"[ \t]+") "\\>")
7358 re2a
(concat "\\<" (mapconcat 'downcase words
"[ \t\r\n]+") "\\>")
7359 re4
(concat "\\<" (mapconcat 'downcase words
"[^a-zA-Z_\r\n]+") "\\>")
7360 re1
(concat pre re2 post
)
7361 re3
(concat pre re4 post
)
7362 re5
(concat pre
".*" re4
)
7363 re2
(concat pre re2
)
7364 re2a
(concat pre re2a
)
7365 re4
(concat pre re4
)
7366 reall
(concat "\\(" re0
"\\)\\|\\(" re1
"\\)\\|\\(" re2
7367 "\\)\\|\\(" re3
"\\)\\|\\(" re4
"\\)\\|\\("
7371 ((eq type
'org-occur
) (org-occur reall
))
7372 ((eq type
'occur
) (org-do-occur (downcase reall
) 'cleanup
))
7373 (t (goto-char (point-min))
7374 (if (or (org-search-not-link re0 nil t
)
7375 (org-search-not-link re1 nil t
)
7376 (org-search-not-link re2 nil t
)
7377 (org-search-not-link re2a nil t
)
7378 (org-search-not-link re3 nil t
)
7379 (org-search-not-link re4 nil t
)
7380 (org-search-not-link re5 nil t
)
7382 (goto-char (match-beginning 0))
7384 (error "No match")))))
7386 ;; Normal string-search
7387 (goto-char (point-min))
7388 (if (search-forward s nil t
)
7389 (goto-char (match-beginning 0))
7390 (error "No match"))))
7391 (and (eq major-mode
'org-mode
) (org-show-hierarchy-above))))
7393 (defun org-search-not-link (&rest args
)
7394 "Execute `re-search-forward', but only accept matches that are not a link."
7396 (let ((pos (point)) p1
)
7397 (while (apply 're-search-forward args
)
7399 (if (not (save-match-data
7400 (and (re-search-backward "\\[\\[" nil t
)
7401 (looking-at org-bracket-link-regexp
)
7402 (<= (match-beginning 0) p1
)
7403 (>= (match-end 0) p1
))))
7404 (progn (goto-char (match-end 0))
7405 (throw 'exit
(point)))
7406 (goto-char (match-end 0)))))))
7408 (defun org-do-occur (regexp &optional cleanup
)
7409 "Call the Emacs command `occur'.
7410 If CLEANUP is non-nil, remove the printout of the regular expression
7411 in the *Occur* buffer. This is useful if the regex is long and not useful
7415 (let ((cwin (selected-window)) win beg end
)
7416 (when (setq win
(get-buffer-window "*Occur*"))
7417 (select-window win
))
7418 (goto-char (point-min))
7419 (when (re-search-forward "match[a-z]+" nil t
)
7420 (setq beg
(match-end 0))
7421 (if (re-search-forward "^[ \t]*[0-9]+" nil t
)
7422 (setq end
(1- (match-beginning 0)))))
7423 (and beg end
(let ((buffer-read-only)) (delete-region beg end
)))
7424 (goto-char (point-min))
7425 (select-window cwin
))))
7427 (defvar org-mark-ring nil
7428 "Mark ring for positions before jumps in Org-mode.")
7429 (defvar org-mark-ring-last-goto nil
7430 "Last position in the mark ring used to go back.")
7431 ;; Fill and close the ring
7432 (setq org-mark-ring nil org-mark-ring-last-goto nil
) ;; in case file is reloaded
7433 (loop for i from
1 to org-mark-ring-length do
7434 (push (make-marker) org-mark-ring
))
7435 (setcdr (nthcdr (1- org-mark-ring-length
) org-mark-ring
)
7438 (defun org-mark-ring-push (&optional pos buffer
)
7439 "Put the current position or POS into the mark ring and rotate it."
7441 (setq pos
(or pos
(point)))
7442 (setq org-mark-ring
(nthcdr (1- org-mark-ring-length
) org-mark-ring
))
7443 (move-marker (car org-mark-ring
)
7445 (or buffer
(current-buffer)))
7447 (substitute-command-keys
7448 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
7450 (defun org-mark-ring-goto (&optional n
)
7451 "Jump to the previous position in the mark ring.
7452 With prefix arg N, jump back that many stored positions. When
7453 called several times in succession, walk through the entire ring.
7454 Org-mode commands jumping to a different position in the current file,
7455 or to another Org-mode file, automatically push the old position
7459 (if (eq last-command this-command
)
7460 (setq p
(nthcdr n
(or org-mark-ring-last-goto org-mark-ring
)))
7461 (setq p org-mark-ring
))
7462 (setq org-mark-ring-last-goto p
)
7464 (switch-to-buffer (marker-buffer m
))
7466 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-hierarchy-above))))
7468 (defun org-camel-to-words (s)
7469 "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
7470 (let ((case-fold-search nil
)
7472 (while (string-match "[a-z][A-Z]" s
)
7473 (push (substring s
0 (1+ (match-beginning 0))) words
)
7474 (setq s
(substring s
(1+ (match-beginning 0)))))
7475 (nreverse (cons s words
))))
7477 (defun org-remove-angle-brackets (s)
7478 (if (equal (substring s
0 1) "<") (setq s
(substring s
1)))
7479 (if (equal (substring s -
1) ">") (setq s
(substring s
0 -
1)))
7481 (defun org-add-angle-brackets (s)
7482 (if (equal (substring s
0 1) "<") nil
(setq s
(concat "<" s
)))
7483 (if (equal (substring s -
1) ">") nil
(setq s
(concat s
">")))
7486 (defun org-follow-bbdb-link (name)
7487 "Follow a BBDB link to NAME."
7489 (let ((inhibit-redisplay t
)
7490 (bbdb-electric-p nil
))
7492 ;; Exact match on name
7493 (bbdb-name (concat "\\`" name
"\\'") nil
)
7494 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil
))
7495 ;; Exact match on name
7496 (bbdb-company (concat "\\`" name
"\\'") nil
)
7497 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil
))
7498 ;; Partial match on name
7499 (bbdb-name name nil
)
7500 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil
))
7501 ;; Partial match on company
7502 (bbdb-company name nil
)
7503 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil
))
7504 ;; General match including network address and notes
7506 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
7507 (delete-window (get-buffer-window "*BBDB*"))
7508 (error "No matching BBDB record")))))
7510 (defun org-follow-gnus-link (&optional group article
)
7511 "Follow a Gnus link to GROUP and ARTICLE."
7513 (funcall (cdr (assq 'gnus org-link-frame-setup
)))
7514 (if group
(gnus-fetch-group group
))
7516 (or (gnus-summary-goto-article article nil
'force
)
7517 (if (fboundp 'gnus-summary-insert-cached-articles
)
7519 (gnus-summary-insert-cached-articles)
7520 (gnus-summary-goto-article article nil
'force
))
7521 (message "Message could not be found.")))))
7523 (defun org-follow-vm-link (&optional folder article readonly
)
7524 "Follow a VM link to FOLDER and ARTICLE."
7526 (setq article
(org-add-angle-brackets article
))
7527 (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder
)
7528 ;; ange-ftp or efs or tramp access
7529 (let ((user (or (match-string 1 folder
) (user-login-name)))
7530 (host (match-string 2 folder
))
7531 (file (match-string 3 folder
)))
7534 ;; use tramp to access the file
7535 (if (featurep 'xemacs
)
7536 (setq folder
(format "[%s@%s]%s" user host file
))
7537 (setq folder
(format "/%s@%s:%s" user host file
))))
7539 ;; use ange-ftp or efs
7540 (require (if (featurep 'xemacs
) 'efs
'ange-ftp
))
7541 (setq folder
(format "/%s@%s:%s" user host file
))))))
7543 (funcall (cdr (assq 'vm org-link-frame-setup
)) folder readonly
)
7546 (vm-select-folder-buffer)
7548 (let ((case-fold-search t
))
7549 (goto-char (point-min))
7550 (if (not (re-search-forward
7551 (concat "^" "message-id: *" (regexp-quote article
))))
7552 (error "Could not find the specified message in this folder"))
7555 (vm-beginning-of-message)
7558 (defun org-follow-wl-link (folder article
)
7559 "Follow a Wanderlust link to FOLDER and ARTICLE."
7560 (setq article
(org-add-angle-brackets article
))
7561 (wl-summary-goto-folder-subr folder
'no-sync t nil t
)
7562 (if article
(wl-summary-jump-to-msg-by-message-id article
">"))
7563 (wl-summary-redisplay))
7565 (defun org-follow-rmail-link (folder article
)
7566 "Follow an RMAIL link to FOLDER and ARTICLE."
7567 (setq article
(org-add-angle-brackets article
))
7568 (let (message-number)
7570 (save-window-excursion
7571 (rmail (if (string= folder
"RMAIL") rmail-file-name folder
))
7572 (setq message-number
7575 (goto-char (point-max))
7576 (if (re-search-backward
7577 (concat "^Message-ID:\\s-+" (regexp-quote
7580 (rmail-what-message))))))
7583 (rmail (if (string= folder
"RMAIL") rmail-file-name folder
))
7584 (rmail-show-message message-number
)
7586 (error "Message not found"))))
7588 ;; mh-e integration based on planner-mode
7589 (defun org-mhe-get-message-real-folder ()
7590 "Return the name of the current message real folder, so if you use
7591 sequences, it will now work."
7594 (if (equal major-mode
'mh-folder-mode
)
7596 ;; Refer to the show buffer
7597 mh-show-folder-buffer
))
7599 (if (boundp 'mh-index-folder
)
7600 (min (length mh-index-folder
) (length folder
))))
7602 ;; a simple test on mh-index-data does not work, because
7603 ;; mh-index-data is always nil in a show buffer.
7604 (if (and (boundp 'mh-index-folder
)
7605 (string= mh-index-folder
(substring folder
0 end-index
)))
7606 (if (equal major-mode
'mh-show-mode
)
7607 (save-window-excursion
7608 (when (buffer-live-p (get-buffer folder
))
7610 (pop-to-buffer folder
)
7611 (org-mhe-get-message-folder-from-index)
7614 (org-mhe-get-message-folder-from-index)
7620 (defun org-mhe-get-message-folder-from-index ()
7621 "Returns the name of the message folder in a index folder buffer."
7623 (mh-index-previous-folder)
7624 (re-search-forward "^\\(+.*\\)$" nil t
)
7625 (message (match-string 1))))
7627 (defun org-mhe-get-message-folder ()
7628 "Return the name of the current message folder. Be careful if you
7631 (if (equal major-mode
'mh-folder-mode
)
7633 ;; Refer to the show buffer
7634 mh-show-folder-buffer
)))
7636 (defun org-mhe-get-message-num ()
7637 "Return the number of the current message. Be careful if you
7640 (if (equal major-mode
'mh-folder-mode
)
7641 (mh-get-msg-num nil
)
7642 ;; Refer to the show buffer
7643 (mh-show-buffer-message-number))))
7645 (defun org-mhe-get-header (header)
7646 "Return a header of the message in folder mode. This will create a
7647 show buffer for the corresponding message. If you have a more clever
7649 (let* ((folder (org-mhe-get-message-folder))
7650 (num (org-mhe-get-message-num))
7651 (buffer (get-buffer-create (concat "show-" folder
)))
7653 (with-current-buffer buffer
7654 (mh-display-msg num folder
)
7655 (if (equal major-mode
'mh-folder-mode
)
7657 (mh-show-header-display))
7659 (setq header-field
(mh-get-header-field header
))
7660 (if (equal major-mode
'mh-folder-mode
)
7665 (defun org-follow-mhe-link (folder article
)
7666 "Follow an MHE link to FOLDER and ARTICLE.
7667 If ARTICLE is nil FOLDER is shown. If the configuration variable
7668 `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
7669 ARTICLE is searched in all folders. Indexed searches (swish++,
7670 namazu, and others supported by MH-E) will always search in all
7673 (require 'mh-search
)
7677 (mh-visit-folder (mh-normalize-folder-name folder
))
7678 (setq article
(org-add-angle-brackets article
))
7680 (if (equal mh-searcher
'pick
)
7682 (mh-search folder
(list "--message-id" article
))
7683 (when (and org-mhe-search-all-folders
7684 (not (org-mhe-get-message-real-folder)))
7686 (mh-search "+" (list "--message-id" article
))))
7687 (mh-search "+" article
))
7688 (if (org-mhe-get-message-real-folder)
7691 (error "Message not found"))))
7693 (defun org-open-file (path &optional in-emacs line search
)
7694 "Open the file at PATH.
7695 First, this expands any special file name abbreviations. Then the
7696 configuration variable `org-file-apps' is checked if it contains an
7697 entry for this file type, and if yes, the corresponding command is launched.
7698 If no application is found, Emacs simply visits the file.
7699 With optional argument IN-EMACS, Emacs will visit the file.
7700 Optional LINE specifies a line to go to, optional SEARCH a string to
7701 search for. If LINE or SEARCH is given, the file will always be
7703 If the file does not exist, an error is thrown."
7704 (setq in-emacs
(or in-emacs line search
))
7705 (let* ((file (if (equal path
"")
7707 (convert-standard-filename (org-expand-file-name path
))))
7708 (dirp (file-directory-p file
))
7709 (dfile (downcase file
))
7710 (old-buffer (current-buffer))
7712 (old-mode major-mode
)
7714 (if (and (not (file-exists-p file
))
7715 (not org-open-non-existing-files
))
7716 (error "No such file: %s" file
))
7717 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile
)
7718 (setq ext
(match-string 1 dfile
))
7719 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile
)
7720 (setq ext
(match-string 1 dfile
))))
7721 (setq apps
(append org-file-apps
(org-default-apps)))
7724 (setq cmd
(or (and dirp
(cdr (assoc 'directory apps
)))
7725 (cdr (assoc ext apps
))
7726 (cdr (assoc t apps
)))))
7727 (when (eq cmd
'mailcap
)
7729 (mailcap-parse-mailcaps)
7730 (let* ((mime-type (mailcap-extension-to-mime (or ext
"")))
7731 (command (mailcap-mime-info mime-type
)))
7732 (if (stringp command
)
7734 (setq cmd
'emacs
))))
7736 ((and (stringp cmd
) (not (string-match "^\\s-*$" cmd
)))
7737 (setq cmd
(format cmd
(concat "\"" file
"\"")))
7738 (save-window-excursion
7739 (shell-command (concat cmd
" &"))))
7742 (unless (equal (file-truename file
) (file-truename (or buffer-file-name
"")))
7743 (funcall (cdr (assq 'file org-link-frame-setup
)) file
))
7744 (if line
(goto-line line
)
7745 (if search
(org-link-search search
))))
7748 (t (funcall (cdr (assq 'file org-link-frame-setup
)) file
)))
7749 (and (eq major-mode
'org-mode
) (eq old-mode
'org-mode
)
7750 (or (not (equal old-buffer
(current-buffer)))
7751 (not (equal old-pos
(point))))
7752 (org-mark-ring-push old-pos old-buffer
))))
7754 (defun org-default-apps ()
7755 "Return the default applications for this operating system."
7757 ((eq system-type
'darwin
)
7758 org-file-apps-defaults-macosx
)
7759 ((eq system-type
'windows-nt
)
7760 org-file-apps-defaults-windowsnt
)
7761 (t org-file-apps-defaults-gnu
)))
7763 (defun org-expand-file-name (path)
7764 "Replace special path abbreviations and expand the file name."
7765 (expand-file-name path
))
7768 (defvar org-insert-link-history nil
7769 "Minibuffer history for links inserted with `org-insert-link'.")
7771 (defvar org-stored-links nil
7772 "Contains the links stored with `org-store-link'.")
7775 (defun org-store-link (arg)
7776 "\\<org-mode-map>Store an org-link to the current location.
7777 This link can later be inserted into an org-buffer with
7778 \\[org-insert-link].
7779 For some link types, a prefix arg is interpreted:
7780 For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
7781 For file links, arg negates `org-context-in-file-links'."
7783 (let (link cpltxt desc txt
(pos (point)))
7786 ((eq major-mode
'bbdb-mode
)
7787 (setq cpltxt
(concat
7789 (or (bbdb-record-name (bbdb-current-record))
7790 (bbdb-record-company (bbdb-current-record))))
7791 link
(org-make-link cpltxt
)))
7793 ((eq major-mode
'calendar-mode
)
7794 (let ((cd (calendar-cursor-to-date)))
7797 (car org-time-stamp-formats
)
7799 (list 0 0 0 (nth 1 cd
) (nth 0 cd
) (nth 2 cd
)
7802 ((or (eq major-mode
'vm-summary-mode
)
7803 (eq major-mode
'vm-presentation-mode
))
7804 (and (eq major-mode
'vm-presentation-mode
) (vm-summarize))
7805 (vm-follow-summary-cursor)
7807 (vm-select-folder-buffer)
7808 (let* ((message (car vm-message-pointer
))
7809 (folder buffer-file-name
)
7810 (subject (vm-su-subject message
))
7811 (author (vm-su-full-name message
))
7812 (message-id (vm-su-message-id message
)))
7813 (setq message-id
(org-remove-angle-brackets message-id
))
7814 (setq folder
(abbreviate-file-name folder
))
7815 (if (string-match (concat "^" (regexp-quote vm-folder-directory
))
7817 (setq folder
(replace-match "" t t folder
)))
7818 (setq cpltxt
(concat author
" on: " subject
))
7819 (setq link
(org-make-link "vm:" folder
"#" message-id
)))))
7821 ((eq major-mode
'wl-summary-mode
)
7822 (let* ((msgnum (wl-summary-message-number))
7823 (message-id (elmo-message-field wl-summary-buffer-elmo-folder
7824 msgnum
'message-id
))
7825 (wl-message-entity (elmo-msgdb-overview-get-entity
7826 msgnum
(wl-summary-buffer-msgdb)))
7827 (author (wl-summary-line-from)) ; FIXME: correct?
7828 (subject "???")) ; FIXME:
7829 (setq message-id
(org-remove-angle-brackets message-id
))
7830 (setq cpltxt
(concat author
" on: " subject
))
7831 (setq link
(org-make-link "wl:" wl-summary-buffer-folder-name
7834 ((or (equal major-mode
'mh-folder-mode
)
7835 (equal major-mode
'mh-show-mode
))
7836 (let ((from-header (org-mhe-get-header "From:"))
7837 (to-header (org-mhe-get-header "To:"))
7838 (subject (org-mhe-get-header "Subject:")))
7839 (setq cpltxt
(concat from-header
" on: " subject
))
7840 (setq link
(org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
7841 (org-remove-angle-brackets
7842 (org-mhe-get-header "Message-Id:"))))))
7844 ((eq major-mode
'rmail-mode
)
7847 (rmail-narrow-to-non-pruned-header)
7848 (let ((folder buffer-file-name
)
7849 (message-id (mail-fetch-field "message-id"))
7850 (author (mail-fetch-field "from"))
7851 (subject (mail-fetch-field "subject")))
7852 (setq message-id
(org-remove-angle-brackets message-id
))
7853 (setq cpltxt
(concat author
" on: " subject
))
7854 (setq link
(org-make-link "rmail:" folder
"#" message-id
))))))
7856 ((eq major-mode
'gnus-group-mode
)
7857 (let ((group (cond ((fboundp 'gnus-group-group-name
) ; depending on Gnus
7858 (gnus-group-group-name)) ; version
7859 ((fboundp 'gnus-group-name
)
7862 (setq cpltxt
(concat
7863 (if (org-xor arg org-usenet-links-prefer-google
)
7864 "http://groups.google.com/groups?group="
7867 link
(org-make-link cpltxt
))))
7869 ((memq major-mode
'(gnus-summary-mode gnus-article-mode
))
7870 (and (eq major-mode
'gnus-article-mode
) (gnus-article-show-summary))
7871 (gnus-summary-beginning-of-article)
7872 (let* ((group (car gnus-article-current
))
7873 (article (cdr gnus-article-current
))
7874 (header (gnus-summary-article-header article
))
7875 (author (mail-header-from header
))
7876 (message-id (mail-header-id header
))
7877 (date (mail-header-date header
))
7878 (subject (gnus-summary-subject-string)))
7879 (setq cpltxt
(concat author
" on: " subject
))
7880 (if (org-xor arg org-usenet-links-prefer-google
)
7884 (format "http://groups.google.com/groups?as_umsgid=%s"
7885 (org-fixup-message-id-for-http message-id
))))
7886 (setq link
(org-make-link "gnus:" group
7887 "#" (number-to-string article
))))))
7889 ((eq major-mode
'w3-mode
)
7890 (setq cpltxt
(url-view-url t
)
7891 link
(org-make-link cpltxt
)))
7892 ((eq major-mode
'w3m-mode
)
7893 (setq cpltxt w3m-current-url
7894 link
(org-make-link cpltxt
)))
7896 ((eq major-mode
'org-mode
)
7897 ;; Just link to current headline
7898 (setq cpltxt
(concat "file:"
7899 (abbreviate-file-name buffer-file-name
)))
7900 ;; Add a context search string
7901 (when (org-xor org-context-in-file-links arg
)
7902 ;; Check if we are on a target
7904 (skip-chars-forward "^>\n\r")
7905 (and (re-search-backward "<<" nil t
)
7906 (looking-at "<<\\(.*?\\)>>")
7907 (<= (match-beginning 0) pos
)
7908 (>= (match-end 0) pos
)))
7909 (setq cpltxt
(concat cpltxt
"::" (match-string 1)))
7911 ((org-on-heading-p) nil
)
7912 ((org-region-active-p)
7913 (buffer-substring (region-beginning) (region-end)))
7914 (t (buffer-substring (point-at-bol) (point-at-eol)))))
7917 (if org-file-link-context-use-camel-case
7918 (org-make-org-heading-camel txt
)
7919 (org-make-org-heading-search-string txt
)))
7921 (if (string-match "::\\'" cpltxt
)
7922 (setq cpltxt
(substring cpltxt
0 -
2)))
7923 (setq link
(org-make-link cpltxt
)))
7926 ;; Just link to this file here.
7927 (setq cpltxt
(concat "file:"
7928 (abbreviate-file-name buffer-file-name
)))
7929 ;; Add a context string
7930 (when (org-xor org-context-in-file-links arg
)
7931 (setq txt
(if (org-region-active-p)
7932 (buffer-substring (region-beginning) (region-end))
7933 (buffer-substring (point-at-bol) (point-at-eol))))
7936 (if org-file-link-context-use-camel-case
7937 (org-make-org-heading-camel txt
)
7938 (org-make-org-heading-search-string txt
)))
7940 (setq link
(org-make-link cpltxt
)))
7943 (error "Cannot link to a buffer which is not visiting a file"))
7945 (t (setq link nil
)))
7947 (if (consp link
) (setq cpltxt
(car link
) link
(cdr link
)))
7948 (setq link
(or link cpltxt
)
7949 desc
(or desc cpltxt
))
7950 (if (equal desc
"NONE") (setq desc nil
))
7952 (if (and (interactive-p) link
)
7954 (setq org-stored-links
7955 (cons (list cpltxt link desc
) org-stored-links
))
7956 (message "Stored: %s" (or cpltxt link
)))
7957 (org-make-link-string link desc
))))
7959 (defun org-make-org-heading-search-string (&optional string heading
)
7960 "Make search string for STRING or current headline."
7962 (let ((s (or string
(org-get-heading))))
7963 (unless (and string
(not heading
))
7964 ;; We are using a headline, clean up garbage in there.
7965 (if (string-match org-todo-regexp s
)
7966 (setq s
(replace-match "" t t s
)))
7967 (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s
)
7968 (setq s
(replace-match "" t t s
)))
7969 (setq s
(org-trim s
))
7970 (if (string-match (concat "^\\(" org-quote-string
"\\|"
7971 org-comment-string
"\\)") s
)
7972 (setq s
(replace-match "" t t s
)))
7973 (while (string-match org-ts-regexp s
)
7974 (setq s
(replace-match "" t t s
))))
7975 (while (string-match "[^a-zA-Z_0-9 \t]+" s
)
7976 (setq s
(replace-match " " t t s
)))
7977 (or string
(setq s
(concat "*" s
))) ; Add * for headlines
7978 (mapconcat 'identity
(org-split-string s
"[ \t]+") " ")))
7980 (defun org-make-org-heading-camel (&optional string heading
)
7981 "Make a CamelCase string for STRING or the current headline."
7983 (let ((s (or string
(org-get-heading))))
7984 (unless (and string
(not heading
))
7985 ;; We are using a headline, clean up garbage in there.
7986 (if (string-match org-todo-regexp s
)
7987 (setq s
(replace-match "" t t s
)))
7988 (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s
)
7989 (setq s
(replace-match "" t t s
)))
7990 (setq s
(org-trim s
))
7991 (if (string-match (concat "^\\(" org-quote-string
"\\|"
7992 org-comment-string
"\\)") s
)
7993 (setq s
(replace-match "" t t s
)))
7994 (while (string-match org-ts-regexp s
)
7995 (setq s
(replace-match "" t t s
))))
7996 (while (string-match "[^a-zA-Z_ \t]+" s
)
7997 (setq s
(replace-match " " t t s
)))
7998 (or string
(setq s
(concat "*" s
))) ; Add * for headlines
7999 (mapconcat 'capitalize
(org-split-string s
"[ \t]+") "")))
8001 (defun org-make-link (&rest strings
)
8002 "Concatenate STRINGS, format resulting string with `org-link-format'."
8003 (format org-link-format
(apply 'concat strings
)))
8005 (defun org-make-link-string (link &optional description
)
8006 "Make a link with brackets, consisting of LINK and DESCRIPTION."
8007 (if (eq org-link-style
'plain
)
8008 (if (equal description link
)
8010 (concat description
"\n" link
))
8011 (when (stringp description
)
8012 ;; Remove brackets from the description, they are fatal.
8013 (while (string-match "\\[\\|\\]" description
)
8014 (setq description
(replace-match "" t t description
))))
8015 (when (equal (org-link-escape link
) description
)
8016 ;; No description needed, it is identical
8017 (setq description nil
))
8018 (when (and (not description
)
8019 (not (equal link
(org-link-escape link
))))
8020 (setq description link
))
8021 (concat "[[" (org-link-escape link
) "]"
8022 (if description
(concat "[" description
"]") "")
8025 (defconst org-link-escape-chars
'(("[" .
"%5B") ("]" .
"%5D") (" " .
"%20"))
8026 "Association list of escapes for some characters problematic in links.")
8028 (defun org-link-escape (text)
8029 "Escape charaters in TEXT that are problematic for links."
8031 (let ((re (mapconcat (lambda (x) (regexp-quote (car x
)))
8032 org-link-escape-chars
"\\|")))
8033 (while (string-match re text
)
8036 (cdr (assoc (match-string 0 text
) org-link-escape-chars
))
8040 (defun org-link-unescape (text)
8041 "Reverse the action of `org-link-escape'."
8043 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x
)))
8044 org-link-escape-chars
"\\|")))
8045 (while (string-match re text
)
8048 (car (rassoc (match-string 0 text
) org-link-escape-chars
))
8052 (defun org-xor (a b
)
8056 (defun org-get-header (header)
8057 "Find a header field in the current buffer."
8059 (goto-char (point-min))
8060 (let ((case-fold-search t
) s
)
8063 (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t
)
8064 (setq s
(match-string 1)))
8065 (while (string-match "\"" s
)
8066 (setq s
(replace-match "" t t s
)))
8067 (if (string-match "[<(].*" s
)
8068 (setq s
(replace-match "" t t s
))))
8069 ((eq header
'message-id
)
8070 (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t
)
8071 (setq s
(match-string 1))))
8072 ((eq header
'subject
)
8073 (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t
)
8074 (setq s
(match-string 1)))))
8075 (if (string-match "\\`[ \t\]+" s
) (setq s
(replace-match "" t t s
)))
8076 (if (string-match "[ \t\]+\\'" s
) (setq s
(replace-match "" t t s
)))
8080 (defun org-fixup-message-id-for-http (s)
8081 "Replace special characters in a message id, so it can be used in an http query."
8082 (while (string-match "<" s
)
8083 (setq s
(replace-match "%3C" t t s
)))
8084 (while (string-match ">" s
)
8085 (setq s
(replace-match "%3E" t t s
)))
8086 (while (string-match "@" s
)
8087 (setq s
(replace-match "%40" t t s
)))
8090 (defun org-insert-link (&optional complete-file
)
8091 "Insert a link. At the prompt, enter the link.
8093 Completion can be used to select a link previously stored with
8094 `org-store-link'. When the empty string is entered (i.e. if you just
8095 press RET at the prompt), the link defaults to the most recently
8096 stored link. As SPC triggers completion in the minibuffer, you need to
8097 use M-SPC or C-q SPC to force the insertion of a space character.
8099 You will also be prompted for a description, and if one is given, it will
8100 be displayed in the buffer instead of the link.
8102 If there is already a link at point, this command will allow you to edit link
8103 and description parts.
8105 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
8106 selected using completion. The path to the file will be relative to
8107 the current directory if the file is in the current directory or a
8108 subdirectory. Otherwise, the link will be the absolute path as
8109 completed in the minibuffer (i.e. normally ~/path/to/file).
8111 With two \\[universal-argument] prefixes, enforce an absolute path even if the file
8112 is in the current directory or below."
8114 (let (link desc entry remove file
(pos (point)))
8117 (skip-chars-forward "^]\n\r")
8118 (and (re-search-backward "\\[\\[" nil t
)
8119 (looking-at org-bracket-link-regexp
)
8120 (<= (match-beginning 0) pos
)
8121 (>= (match-end 0) pos
)))
8122 ;; We do have a link at point, and we are going to edit it.
8123 (setq remove
(list (match-beginning 0) (match-end 0)))
8124 (setq desc
(if (match-end 3) (org-match-string-no-properties 3)))
8125 (setq link
(read-string "Link: "
8127 (org-match-string-no-properties 1)))))
8129 ;; Completing read for file names.
8130 (setq file
(read-file-name "File: "))
8131 (let ((pwd (file-name-as-directory (expand-file-name "."))))
8133 ((equal complete-file
'(16))
8134 (setq link
(org-make-link
8136 (abbreviate-file-name (expand-file-name file
)))))
8137 ((string-match (concat "^" (regexp-quote pwd
) "\\(.+\\)")
8138 (expand-file-name file
))
8139 (setq link
(org-make-link
8140 "file:" (match-string 1 (expand-file-name file
)))))
8141 (t (setq link
(org-make-link "file:" file
))))))
8143 ;; Read link, with completion for stored links.
8144 (setq link
(org-completing-read
8145 "Link: " org-stored-links nil nil nil
8146 org-insert-link-history
8147 (or (car (car org-stored-links
)))))
8148 (setq entry
(assoc link org-stored-links
))
8149 (if (not org-keep-stored-link-after-insertion
)
8150 (setq org-stored-links
(delq (assoc link org-stored-links
)
8152 (setq link
(if entry
(nth 1 entry
) link
)
8153 desc
(or desc
(nth 2 entry
)))))
8155 (if (string-match org-plain-link-re link
)
8156 ;; URL-like link, normalize the use of angular brackets.
8157 (setq link
(org-make-link (org-remove-angle-brackets link
))))
8159 ;; Check if we are linking to the current file. If yes, simplify the link.
8160 (when (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link
)
8161 (let* ((path (match-string 1 link
))
8162 (case-fold-search nil
)
8163 (search (match-string 2 link
)))
8164 (when (save-match-data
8165 (equal (file-truename buffer-file-name
)
8166 (file-truename path
)))
8167 ;; We are linking to this same file, with a search option
8168 (setq link search
))))
8169 (setq desc
(read-string "Description: " desc
))
8170 (unless (string-match "\\S-" desc
) (setq desc nil
))
8171 (if remove
(apply 'delete-region remove
))
8172 (insert (org-make-link-string link desc
))))
8174 (defun org-completing-read (&rest args
)
8175 (let ((minibuffer-local-completion-map
8176 (copy-keymap minibuffer-local-completion-map
)))
8177 (define-key minibuffer-local-completion-map
" " 'self-insert-command
)
8178 (apply 'completing-read args
)))
8180 ;;; Hooks for remember.el
8182 (defvar org-finish-function nil
)
8185 (defun org-remember-annotation ()
8186 "Return a link to the current location as an annotation for remember.el.
8187 If you are using Org-mode files as target for data storage with
8188 remember.el, then the annotations should include a link compatible with the
8189 conventions in Org-mode. This function returns such a link."
8190 (org-store-link nil
))
8192 (defconst org-remember-help
8193 "Select a destination location for the note.
8194 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
8195 RET at beg-of-buf -> Append to file as level 2 headline
8196 RET on headline -> Store as sublevel entry to current headline
8197 <left>/<right> -> before/after current headline, same headings level")
8200 (defun org-remember-apply-template ()
8201 "Initialize *remember* buffer with template, invode `org-mode'.
8202 This function should be placed into `remember-mode-hook' and in fact requires
8203 to be run from that hook to fucntion properly."
8204 (when org-remember-templates
8205 (let* ((entry (if (= (length org-remember-templates
) 1)
8206 (cdar org-remember-templates
)
8207 (message "Select template: %s"
8209 (lambda (x) (char-to-string (car x
)))
8210 org-remember-templates
" "))
8211 (cdr (assoc (read-char-exclusive) org-remember-templates
))))
8212 (tpl (if (consp (cdr entry
)) (cadr entry
) (cdr entry
)))
8213 (file (if (consp (cdr entry
)) (nth 2 entry
)))
8214 (v-t (format-time-string (car org-time-stamp-formats
) (org-current-time)))
8215 (v-T (format-time-string (cdr org-time-stamp-formats
) (org-current-time)))
8216 (v-u (concat "[" (substring v-t
1 -
1) "]"))
8217 (v-U (concat "[" (substring v-T
1 -
1) "]"))
8218 (v-a annotation
) ; defined in `remember-mode'
8219 (v-i initial
) ; defined in `remember-mode'
8220 (v-n user-full-name
)
8222 (unless tpl
(setq tpl
"") (message "No template") (ding))
8223 (insert tpl
) (goto-char (point-min))
8224 (while (re-search-forward "%\\([tTuTai]\\)" nil t
)
8225 (when (and initial
(equal (match-string 0) "%i"))
8227 (let* ((lead (buffer-substring
8228 (point-at-bol) (match-beginning 0))))
8229 (setq v-i
(mapconcat 'identity
8230 (org-split-string initial
"\n")
8231 (concat "\n" lead
))))))
8233 (or (eval (intern (concat "v-" (match-string 1)))) "")
8235 (let ((org-startup-folded nil
)
8236 (org-startup-with-deadline-check nil
))
8238 (if (and file
(string-match "\\S-" file
) (not (file-directory-p file
)))
8239 (set (make-local-variable 'org-default-notes-file
) file
))
8240 (goto-char (point-min))
8241 (if (re-search-forward "%\\?" nil t
) (replace-match ""))
8242 (set (make-local-variable 'org-finish-function
) 'remember-buffer
))))
8245 (defun org-remember-handler ()
8246 "Store stuff from remember.el into an org file.
8247 First prompts for an org file. If the user just presses return, the value
8248 of `org-default-notes-file' is used.
8249 Then the command offers the headings tree of the selected file in order to
8250 file the text at a specific location.
8251 You can either immediately press RET to get the note appended to the
8252 file, or you can use vertical cursor motion and visibility cycling (TAB) to
8253 find a better place. Then press RET or <left> or <right> in insert the note.
8255 Key Cursor position Note gets inserted
8256 -----------------------------------------------------------------------------
8257 RET buffer-start as level 2 heading at end of file
8258 RET on headline as sublevel of the heading at cursor
8259 RET no heading at cursor position, level taken from context.
8260 Or use prefix arg to specify level manually.
8261 <left> on headline as same level, before current heading
8262 <right> on headline as same level, after current heading
8264 So the fastest way to store the note is to press RET RET to append it to
8265 the default file. This way your current train of thought is not
8266 interrupted, in accordance with the principles of remember.el. But with
8267 little extra effort, you can push it directly to the correct location.
8269 Before being stored away, the function ensures that the text has a
8270 headline, i.e. a first line that starts with a \"*\". If not, a headline
8271 is constructed from the current date and some additional data.
8273 If the variable `org-adapt-indentation' is non-nil, the entire text is
8274 also indented so that it starts in the same column as the headline
8275 \(i.e. after the stars).
8277 See also the variable `org-reverse-note-order'."
8279 (let* ((txt (buffer-substring (point-min) (point-max)))
8280 (fastp current-prefix-arg
)
8281 (file (if fastp org-default-notes-file
(org-get-org-file)))
8282 (visiting (find-buffer-visiting file
))
8283 (org-startup-with-deadline-check nil
)
8284 (org-startup-folded nil
)
8285 (org-startup-align-all-tables nil
)
8286 spos level indent reversed
)
8287 ;; Modify text so that it becomes a nice subtree which can be inserted
8288 ;; into an org tree.
8289 (let* ((lines (split-string txt
"\n"))
8291 ;; remove empty lines at the beginning
8292 (while (and lines
(string-match "^[ \t]*\n" (car lines
)))
8293 (setq lines
(cdr lines
)))
8294 (setq first
(car lines
) lines
(cdr lines
))
8295 (if (string-match "^\\*+" first
)
8296 ;; Is already a headline
8298 ;; We need to add a headline: Use time and first buffer line
8299 (setq lines
(cons first lines
)
8300 first
(concat "* " (current-time-string)
8301 " (" (remember-buffer-desc) ")")
8303 (if (and org-adapt-indentation indent
)
8304 (setq lines
(mapcar (lambda (x) (concat indent x
)) lines
)))
8305 (setq txt
(concat first
"\n"
8306 (mapconcat 'identity lines
"\n"))))
8309 (find-file-noselect file
))
8310 (with-current-buffer (get-file-buffer file
)
8311 (setq reversed
(org-notes-order-reversed-p))
8315 ;; Ask the User for a location
8316 (setq spos
(if fastp
1 (org-get-location
8318 org-remember-help
)))
8319 (if (not spos
) (throw 'quit nil
)) ; return nil to show we did
8320 ; not handle this note
8323 ;; Put it at the start or end, as level 2
8326 (goto-char (if reversed
(point-min) (point-max)))
8327 (if (not (bolp)) (newline))
8328 (org-paste-subtree 2 txt
)))
8329 ((and (org-on-heading-p nil
) (not current-prefix-arg
))
8330 ;; Put it below this entry, at the beg/end of the subtree
8331 (org-back-to-heading)
8332 (setq level
(funcall outline-level
))
8334 (outline-end-of-heading)
8335 (outline-end-of-subtree))
8336 (if (not (bolp)) (newline))
8337 (beginning-of-line 1)
8338 (org-paste-subtree (1+ level
) txt
))
8340 ;; Put it right there, with automatic level determined by
8341 ;; org-paste-subtree or from prefix arg
8342 (org-paste-subtree current-prefix-arg txt
)))
8343 (when remember-save-after-remembering
8345 (if (not visiting
) (kill-buffer (current-buffer)))))))))
8346 t
) ;; return t to indicate that we took care of this note.
8348 (defun org-get-org-file ()
8349 "Read a filename, with default directory `org-directory'."
8350 (let ((default (or org-default-notes-file remember-data-file
)))
8351 (read-file-name (format "File name [%s]: " default
)
8352 (file-name-as-directory org-directory
)
8355 (defun org-notes-order-reversed-p ()
8356 "Check if the current file should receive notes in reversed order."
8358 ((not org-reverse-note-order
) nil
)
8359 ((eq t org-reverse-note-order
) t
)
8360 ((not (listp org-reverse-note-order
)) nil
)
8362 (let ((all org-reverse-note-order
)
8364 (while (setq entry
(pop all
))
8365 (if (string-match (car entry
) buffer-file-name
)
8366 (throw 'exit
(cdr entry
))))
8371 ;; Watch out: Here we are talking about two different kind of tables.
8372 ;; Most of the code is for the tables created with the Org-mode table editor.
8373 ;; Sometimes, we talk about tables created and edited with the table.el
8374 ;; Emacs package. We call the former org-type tables, and the latter
8375 ;; table.el-type tables.
8378 (defun org-before-change-function (beg end
)
8379 "Every change indicates that a table might need an update."
8380 (setq org-table-may-need-update t
))
8382 (defconst org-table-line-regexp
"^[ \t]*|"
8383 "Detects an org-type table line.")
8384 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
8385 "Detects an org-type table line.")
8386 (defconst org-table-auto-recalculate-regexp
"^[ \t]*| *# *\\(|\\|$\\)"
8387 "Detects a table line marked for automatic recalculation.")
8388 (defconst org-table-recalculate-regexp
"^[ \t]*| *[#*] *\\(|\\|$\\)"
8389 "Detects a table line marked for automatic recalculation.")
8390 (defconst org-table-calculate-mark-regexp
"^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
8391 "Detects a table line marked for automatic recalculation.")
8392 (defconst org-table-hline-regexp
"^[ \t]*|-"
8393 "Detects an org-type table hline.")
8394 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
8395 "Detects a table-type table hline.")
8396 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
8397 "Detects an org-type or table-type table.")
8398 (defconst org-table-border-regexp
"^[ \t]*[^| \t]"
8399 "Searching from within a table (any type) this finds the first line
8400 outside the table.")
8401 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
8402 "Searching from within a table (any type) this finds the first line
8403 outside the table.")
8405 (defun org-table-create-with-table.el
()
8406 "Use the table.el package to insert a new table.
8407 If there is already a table at point, convert between Org-mode tables
8408 and table.el tables."
8412 ((org-at-table.el-p
)
8413 (if (y-or-n-p "Convert table to Org-mode table? ")
8414 (org-table-convert)))
8416 (if (y-or-n-p "Convert table to table.el table? ")
8417 (org-table-convert)))
8418 (t (call-interactively 'table-insert
))))
8420 (defun org-table-create-or-convert-from-region (arg)
8421 "Convert region to table, or create an empty table.
8422 If there is an active region, convert it to a table. If there is no such
8423 region, create an empty table."
8425 (if (org-region-active-p)
8426 (org-table-convert-region (region-beginning) (region-end) arg
)
8427 (org-table-create arg
)))
8429 (defun org-table-create (&optional size
)
8430 "Query for a size and insert a table skeleton.
8431 SIZE is a string Columns x Rows like for example \"3x2\"."
8434 (setq size
(read-string
8435 (concat "Table size Columns x Rows [e.g. "
8436 org-table-default-size
"]: ")
8437 "" nil org-table-default-size
)))
8439 (let* ((pos (point))
8440 (indent (make-string (current-column) ?\
))
8441 (split (org-split-string size
" *x *"))
8442 (rows (string-to-number (nth 1 split
)))
8443 (columns (string-to-number (car split
)))
8444 (line (concat (apply 'concat indent
"|" (make-list columns
" |"))
8446 (if (string-match "^[ \t]*$" (buffer-substring-no-properties
8447 (point-at-bol) (point)))
8448 (beginning-of-line 1)
8450 ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
8451 (dotimes (i rows
) (insert line
))
8454 ;; Insert a hline after the first row.
8461 (defun org-table-convert-region (beg0 end0
&optional nspace
)
8462 "Convert region to a table.
8463 The region goes from BEG0 to END0, but these borders will be moved
8464 slightly, to make sure a beginning of line in the first line is included.
8465 When NSPACE is non-nil, it indicates the minimum number of spaces that
8466 separate columns (default: just one space)."
8468 (let* ((beg (min beg0 end0
))
8469 (end (max beg0 end0
))
8473 (beginning-of-line 1)
8474 (setq beg
(move-marker (make-marker) (point)))
8476 (if (bolp) (backward-char 1) (end-of-line 1))
8477 (setq end
(move-marker (make-marker) (point)))
8478 ;; Lets see if this is tab-separated material. If every nonempty line
8479 ;; contains a tab, we will assume that it is tab-separated material
8483 (and (re-search-forward "^[^\n\t]+$" end t
) (setq tabsep nil
)))
8484 (if nspace
(setq tabsep nil
))
8487 (setq re
(format "^ *\\| *\t *\\| \\{%d,\\}"
8488 (max 1 (prefix-numeric-value nspace
)))))
8490 (while (re-search-forward re end t
)
8491 (replace-match "|" t t
))
8496 (defun org-table-import (file arg
)
8497 "Import FILE as a table.
8498 The file is assumed to be tab-separated. Such files can be produced by most
8499 spreadsheet and database applications. If no tabs (at least one per line)
8500 are found, lines will be split on whitespace into fields."
8501 (interactive "f\nP")
8502 (or (bolp) (newline))
8505 (insert-file-contents file
)
8506 (org-table-convert-region beg
(+ (point) (- (point-max) pm
)) arg
)))
8508 (defun org-table-export ()
8509 "Export table as a tab-separated file.
8510 Such a file can be imported into a spreadsheet program like Excel."
8512 (let* ((beg (org-table-begin))
8513 (end (org-table-end))
8514 (table (buffer-substring beg end
))
8515 (file (read-file-name "Export table to: "))
8517 (unless (or (not (file-exists-p file
))
8518 (y-or-n-p (format "Overwrite file %s? " file
)))
8520 (with-current-buffer (find-file-noselect file
)
8521 (setq buf
(current-buffer))
8525 (goto-char (point-min))
8526 (while (re-search-forward "^[ \t]*|[ \t]*" nil t
)
8527 (replace-match "" t t
)
8529 (goto-char (point-min))
8530 (while (re-search-forward "[ \t]*|[ \t]*$" nil t
)
8531 (replace-match "" t t
)
8532 (goto-char (min (1+ (point)) (point-max))))
8533 (goto-char (point-min))
8534 (while (re-search-forward "^-[-+]*$" nil t
)
8536 (if (looking-at "\n")
8538 (goto-char (point-min))
8539 (while (re-search-forward "[ \t]*|[ \t]*" nil t
)
8540 (replace-match "\t" t t
))
8544 (defvar org-table-aligned-begin-marker
(make-marker)
8545 "Marker at the beginning of the table last aligned.
8546 Used to check if cursor still is in that table, to minimize realignment.")
8547 (defvar org-table-aligned-end-marker
(make-marker)
8548 "Marker at the end of the table last aligned.
8549 Used to check if cursor still is in that table, to minimize realignment.")
8550 (defvar org-table-last-alignment nil
8551 "List of flags for flushright alignment, from the last re-alignment.
8552 This is being used to correctly align a single field after TAB or RET.")
8553 (defvar org-table-last-column-widths nil
8554 "List of max width of fields in each column.
8555 This is being used to correctly align a single field after TAB or RET.")
8557 (defvar org-last-recalc-line nil
)
8558 (defconst org-narrow-column-arrow
"=>"
8559 "Used as display property in narrowed table columns.")
8561 (defun org-table-align ()
8562 "Align the table at point by aligning all vertical bars."
8566 (beg (org-table-begin))
8567 (end (org-table-end))
8568 ;; Current cursor position
8569 (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
8570 (colpos (org-table-current-column))
8571 (winstart (window-start))
8572 text lines
(new "") lengths l typenums ty fields maxfields i
8574 (indent "") cnt frac
8580 (make-string sp2 ?\
) "%%%s%ds" (make-string sp1 ?\
) "|"))
8582 (make-string sp2 ?-
) "%s" (make-string sp1 ?-
) "+"))
8583 emptystrings xx links narrow fmax fmin f1 len c e
)
8585 (remove-text-properties beg end
'(org-cwidth t display t
))
8586 ;; Check if we have links
8588 (setq links
(re-search-forward org-bracket-link-regexp end t
))
8589 ;; Make sure the link properties are right FIXME: Can this be optimized????
8590 (when links
(goto-char beg
) (while (org-activate-bracket-links end
)))
8591 ;; Check if we are narrowing any columns
8593 (setq narrow
(and org-format-transports-properties-p
8594 (re-search-forward "<[0-9]+>" end t
)))
8596 (setq lines
(org-split-string
8597 (buffer-substring beg end
) "\n"))
8598 ;; Store the indentation of the first line
8599 (if (string-match "^ *" (car lines
))
8600 (setq indent
(make-string (- (match-end 0) (match-beginning 0)) ?\
)))
8601 ;; Mark the hlines by setting the corresponding element to nil
8602 ;; At the same time, we remove trailing space.
8603 (setq lines
(mapcar (lambda (l)
8604 (if (string-match "^ *|-" l
)
8606 (if (string-match "[ \t]+$" l
)
8607 (substring l
0 (match-beginning 0))
8610 ;; Get the data fields by splitting the lines.
8611 (setq fields
(mapcar
8613 (org-split-string l
" *| *"))
8614 (delq nil
(copy-sequence lines
))))
8615 ;; How many fields in the longest line?
8617 (setq maxfields
(apply 'max
(mapcar 'length fields
)))
8619 (kill-region beg end
)
8620 (org-table-create org-table-default-size
)
8621 (error "Empty table - created default table")))
8622 ;; A list of empty string to fill any short rows on output
8623 (setq emptystrings
(make-list maxfields
""))
8624 ;; Check for special formatting.
8626 (while (< (setq i
(1+ i
)) maxfields
) ;; Loop over all columns
8627 (setq column
(mapcar (lambda (x) (or (nth i x
) "")) fields
))
8628 ;; Check if there is an explicit width specified
8629 (when (and org-table-limit-column-width narrow
)
8630 (setq c column fmax nil
)
8633 (if (and (stringp e
) (string-match "^<\\([0-9]+\\)>$" e
))
8634 (setq fmax
(string-to-number (match-string 1 e
)) c nil
)))
8635 ;; Find fields that are wider than fmax, and shorten them
8637 (loop for xx in column do
8638 (when (and (stringp xx
)
8639 (> (org-string-width xx
) fmax
))
8640 (org-add-props xx nil
8642 (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (copy-sequence xx
)))
8643 (setq f1
(min fmax
(or (string-match org-bracket-link-regexp xx
) fmax
)))
8645 (error "Cannot narrow field starting with wide link \"%s\""
8646 (match-string 0 xx
)))
8647 (add-text-properties f1
(length xx
) (list 'org-cwidth t
) xx
)
8648 (add-text-properties (- f1
2) f1
8649 (list 'display org-narrow-column-arrow
)
8651 ;; Get the maximum width for each column
8652 (push (apply 'max
1 (mapcar 'org-string-width column
)) lengths
)
8653 ;; Get the fraction of numbers, to decide about alignment of the column
8654 (setq cnt
0 frac
0.0)
8655 (loop for x in column do
8658 (setq frac
( / (+ (* frac cnt
)
8659 (if (string-match org-table-number-regexp x
) 1 0))
8660 (setq cnt
(1+ cnt
))))))
8661 (push (>= frac org-table-number-fraction
) typenums
))
8662 (setq lengths
(nreverse lengths
) typenums
(nreverse typenums
))
8664 ;; Store the alignment of this table, for later editing of single fields
8665 (setq org-table-last-alignment typenums
8666 org-table-last-column-widths lengths
)
8668 ;; With invisible characters, `format' does not get the field width right
8669 ;; So we need to make these fields wide by hand.
8671 (loop for i from
0 upto
(1- maxfields
) do
8672 (setq len
(nth i lengths
))
8673 (loop for j from
0 upto
(1- (length fields
)) do
8674 (setq c
(nthcdr i
(car (nthcdr j fields
))))
8675 (if (and (stringp (car c
))
8676 (string-match org-bracket-link-regexp
(car c
))
8677 (< (org-string-width (car c
)) len
))
8678 (setcar c
(concat (car c
) (make-string (- len
(org-string-width (car c
))) ?\
)))))))
8680 ;; Compute the formats needed for output of the table
8681 (setq rfmt
(concat indent
"|") hfmt
(concat indent
"|"))
8682 (while (setq l
(pop lengths
))
8683 (setq ty
(if (pop typenums
) "" "-")) ; number types flushright
8684 (setq rfmt
(concat rfmt
(format rfmt1 ty l
))
8685 hfmt
(concat hfmt
(format hfmt1
(make-string l ?-
)))))
8686 (setq rfmt
(concat rfmt
"\n")
8687 hfmt
(concat (substring hfmt
0 -
1) "|\n"))
8689 (setq new
(mapconcat
8691 (if l
(apply 'format rfmt
8692 (append (pop fields
) emptystrings
))
8695 ;; Replace the old one
8696 (delete-region beg end
)
8697 (move-marker end nil
)
8698 (move-marker org-table-aligned-begin-marker
(point))
8700 (move-marker org-table-aligned-end-marker
(point))
8701 (when (and orgtbl-mode
(not (eq major-mode
'org-mode
)))
8702 (goto-char org-table-aligned-begin-marker
)
8703 (while (org-hide-wide-columns org-table-aligned-end-marker
)))
8704 ;; Try to move to the old location (approximately)
8706 (set-window-start (selected-window) winstart
'noforce
)
8707 (org-table-goto-column colpos
)
8708 (setq org-table-may-need-update nil
)
8711 (defun org-string-width (s)
8712 "Compute width of string, ignoring invisible characters.
8713 This ignores character with invisibility property `org-link', and also
8714 characters with property `org-cwidth', because these will become invisible
8715 upon the next fontification round."
8717 (when (or (eq t buffer-invisibility-spec
)
8718 (assq 'org-link buffer-invisibility-spec
))
8719 (while (setq b
(text-property-any 0 (length s
)
8720 'invisible
'org-link s
))
8721 (setq s
(concat (substring s
0 b
)
8722 (substring s
(or (next-single-property-change
8723 b
'invisible s
) (length s
)))))))
8724 (while (setq b
(text-property-any 0 (length s
) 'org-cwidth t s
))
8725 (setq s
(concat (substring s
0 b
)
8726 (substring s
(or (next-single-property-change
8727 b
'org-cwidth s
) (length s
))))))
8730 (defun org-table-begin (&optional table-type
)
8731 "Find the beginning of the table and return its position.
8732 With argument TABLE-TYPE, go to the beginning of a table.el-type table."
8734 (if (not (re-search-backward
8735 (if table-type org-table-any-border-regexp
8736 org-table-border-regexp
)
8738 (error "Can't find beginning of table")
8739 (goto-char (match-beginning 0))
8740 (beginning-of-line 2)
8743 (defun org-table-end (&optional table-type
)
8744 "Find the end of the table and return its position.
8745 With argument TABLE-TYPE, go to the end of a table.el-type table."
8747 (if (not (re-search-forward
8748 (if table-type org-table-any-border-regexp
8749 org-table-border-regexp
)
8751 (goto-char (point-max))
8752 (goto-char (match-beginning 0)))
8755 (defun org-table-justify-field-maybe (&optional new
)
8756 "Justify the current field, text to left, number to right.
8757 Optional argument NEW may specify text to replace the current field content."
8759 ((and (not new
) org-table-may-need-update
)) ; Realignment will happen anyway
8760 ((org-at-table-hline-p))
8762 (or (not (equal (marker-buffer org-table-aligned-begin-marker
)
8764 (< (point) org-table-aligned-begin-marker
)
8765 (>= (point) org-table-aligned-end-marker
)))
8766 ;; This is not the same table, force a full re-align
8767 (setq org-table-may-need-update t
))
8768 (t ;; realign the current field, based on previous full realign
8769 (let* ((pos (point)) s
8770 (col (org-table-current-column))
8771 (num (if (> col
0) (nth (1- col
) org-table-last-alignment
)))
8774 (skip-chars-backward "^|\n")
8775 (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
8777 (setq s
(match-string 1)
8779 l
(max 1 (- (match-end 0) (match-beginning 0) 3))
8780 e
(not (= (match-beginning 2) (match-end 2))))
8781 (setq f
(format (if num
" %%%ds %s" " %%-%ds %s")
8782 l
(if e
"|" (setq org-table-may-need-update t
) ""))
8785 (if (<= (length new
) l
)
8786 (setq n
(format f new t t
)) ;; FIXME: why t t?????
8787 (setq n
(concat new
"|") org-table-may-need-update t
)))
8789 (let (org-table-may-need-update)
8790 (replace-match n
))))
8791 (setq org-table-may-need-update t
))
8792 (goto-char pos
))))))
8794 (defun org-table-next-field ()
8795 "Go to the next field in the current table, creating new lines as needed.
8796 Before doing so, re-align the table if necessary."
8798 (org-table-maybe-eval-formula)
8799 (org-table-maybe-recalculate-line)
8800 (if (and org-table-automatic-realign
8801 org-table-may-need-update
)
8803 (let ((end (org-table-end)))
8804 (if (org-at-table-hline-p)
8808 (re-search-forward "|" end
)
8809 (if (looking-at "[ \t]*$")
8810 (re-search-forward "|" end
))
8811 (if (and (looking-at "-")
8812 org-table-tab-jumps-over-hlines
8813 (re-search-forward "^[ \t]*|\\([^-]\\)" end t
))
8814 (goto-char (match-beginning 1)))
8815 (if (looking-at "-")
8817 (beginning-of-line 0)
8818 (org-table-insert-row 'below
))
8819 (if (looking-at " ") (forward-char 1))))
8821 (org-table-insert-row 'below
)))))
8823 (defun org-table-previous-field ()
8824 "Go to the previous field in the table.
8825 Before doing so, re-align the table if necessary."
8827 (org-table-justify-field-maybe)
8828 (org-table-maybe-recalculate-line)
8829 (if (and org-table-automatic-realign
8830 org-table-may-need-update
)
8832 (if (org-at-table-hline-p)
8834 (re-search-backward "|" (org-table-begin))
8835 (re-search-backward "|" (org-table-begin))
8836 (while (looking-at "|\\(-\\|[ \t]*$\\)")
8837 (re-search-backward "|" (org-table-begin)))
8838 (if (looking-at "| ?")
8839 (goto-char (match-end 0))))
8841 (defun org-table-next-row ()
8842 "Go to the next row (same column) in the current table.
8843 Before doing so, re-align the table if necessary."
8845 (org-table-maybe-eval-formula)
8846 (org-table-maybe-recalculate-line)
8847 (if (or (looking-at "[ \t]*$")
8848 (save-excursion (skip-chars-backward " \t") (bolp)))
8850 (if (and org-table-automatic-realign
8851 org-table-may-need-update
)
8853 (let ((col (org-table-current-column)))
8854 (beginning-of-line 2)
8855 (if (or (not (org-at-table-p))
8856 (org-at-table-hline-p))
8858 (beginning-of-line 0)
8859 (org-table-insert-row 'below
)))
8860 (org-table-goto-column col
)
8861 (skip-chars-backward "^|\n\r")
8862 (if (looking-at " ") (forward-char 1)))))
8864 (defun org-table-copy-down (n)
8865 "Copy a field down in the current column.
8866 If the field at the cursor is empty, copy into it the content of the nearest
8867 non-empty field above. With argument N, use the Nth non-empty field.
8868 If the current field is not empty, it is copied down to the next row, and
8869 the cursor is moved with it. Therefore, repeating this command causes the
8870 column to be filled row-by-row.
8871 If the variable `org-table-copy-increment' is non-nil and the field is an
8872 integer, it will be incremented while copying."
8874 (let* ((colpos (org-table-current-column))
8875 (field (org-table-get-field))
8876 (non-empty (string-match "[^ \t]" field
))
8877 (beg (org-table-begin))
8879 (org-table-check-inside-data-field)
8882 (setq txt
(org-trim field
))
8883 (org-table-next-row)
8884 (org-table-blank-field))
8888 (while (progn (beginning-of-line 1)
8889 (re-search-backward org-table-dataline-regexp
8891 (org-table-goto-column colpos t
)
8892 (if (and (looking-at
8893 "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
8894 (= (setq n
(1- n
)) 0))
8895 (throw 'exit
(match-string 1))))))))
8898 (if (and org-table-copy-increment
8899 (string-match "^[0-9]+$" txt
))
8900 (setq txt
(format "%d" (+ (string-to-number txt
) 1))))
8902 (org-table-maybe-recalculate-line)
8904 (error "No non-empty field found"))))
8906 (defun org-table-check-inside-data-field ()
8907 "Is point inside a table data field?
8908 I.e. not on a hline or before the first or after the last column?
8909 This actually throws an error, so it aborts the current command."
8910 (if (or (not (org-at-table-p))
8911 (= (org-table-current-column) 0)
8912 (org-at-table-hline-p)
8913 (looking-at "[ \t]*$"))
8914 (error "Not in table data field")))
8916 (defvar org-table-clip nil
8917 "Clipboard for table regions.")
8919 (defun org-table-blank-field ()
8920 "Blank the current table field or active region."
8922 (org-table-check-inside-data-field)
8923 (if (and (interactive-p) (org-region-active-p))
8924 (let (org-table-clip)
8925 (org-table-cut-region (region-beginning) (region-end)))
8926 (skip-chars-backward "^|")
8928 (if (looking-at "|[^|\n]+")
8929 (let* ((pos (match-beginning 0))
8930 (match (match-string 0))
8931 (len (org-string-width match
)))
8932 (replace-match (concat "|" (make-string (1- len
) ?\
)))
8933 (goto-char (+ 2 pos
))
8934 (substring match
1)))))
8936 (defun org-table-get-field (&optional n replace
)
8937 "Return the value of the field in column N of current row.
8938 N defaults to current field.
8939 If REPLACE is a string, replace field with this value. The return value
8940 is always the old value."
8941 (and n
(org-table-goto-column n
))
8942 (skip-chars-backward "^|\n")
8944 (if (looking-at "|[^|\r\n]*")
8945 (let* ((pos (match-beginning 0))
8946 (val (buffer-substring (1+ pos
) (match-end 0))))
8948 (replace-match (concat "|" replace
)))
8949 (goto-char (min (point-at-eol) (+ 2 pos
)))
8951 (forward-char 1) ""))
8953 (defun org-table-current-column ()
8954 "Find out which column we are in.
8955 When called interactively, column is also displayed in echo area."
8957 (if (interactive-p) (org-table-check-inside-data-field))
8959 (let ((cnt 0) (pos (point)))
8960 (beginning-of-line 1)
8961 (while (search-forward "|" pos t
)
8962 (setq cnt
(1+ cnt
)))
8963 (if (interactive-p) (message "This is table column %d" cnt
))
8966 (defun org-table-goto-column (n &optional on-delim force
)
8967 "Move the cursor to the Nth column in the current table line.
8968 With optional argument ON-DELIM, stop with point before the left delimiter
8970 If there are less than N fields, just go to after the last delimiter.
8971 However, when FORCE is non-nil, create new columns if necessary."
8973 (let ((pos (point-at-eol)))
8974 (beginning-of-line 1)
8976 (while (and (> (setq n
(1- n
)) -
1)
8977 (or (search-forward "|" pos t
)
8979 (progn (end-of-line 1)
8980 (skip-chars-backward "^|")
8982 ; (backward-char 2) t)))))
8983 (when (and force
(not (looking-at ".*|")))
8984 (save-excursion (end-of-line 1) (insert " | ")))
8987 (if (looking-at " ") (forward-char 1))))))
8989 (defun org-at-table-p (&optional table-type
)
8990 "Return t if the cursor is inside an org-type table.
8991 If TABLE-TYPE is non-nil, also check for table.el-type tables."
8992 (if org-enable-table-editor
8994 (beginning-of-line 1)
8995 (looking-at (if table-type org-table-any-line-regexp
8996 org-table-line-regexp
)))
8999 (defun org-at-table.el-p
()
9000 "Return t if and only if we are at a table.el table."
9001 (and (org-at-table-p 'any
)
9003 (goto-char (org-table-begin 'any
))
9004 (looking-at org-table1-hline-regexp
))))
9006 (defun org-table-recognize-table.el
()
9007 "If there is a table.el table nearby, recognize it and move into it."
9008 (if org-table-tab-recognizes-table.el
9009 (if (org-at-table.el-p
)
9011 (beginning-of-line 1)
9012 (if (looking-at org-table-dataline-regexp
)
9014 (if (looking-at org-table1-hline-regexp
)
9016 (beginning-of-line 2)
9017 (if (looking-at org-table-any-border-regexp
)
9018 (beginning-of-line -
1)))))
9019 (if (re-search-forward "|" (org-table-end t
) t
)
9022 (if (table--at-cell-p (point))
9024 (message "recognizing table.el table...")
9025 (table-recognize-table)
9026 (message "recognizing table.el table...done")))
9027 (error "This should not happen..."))
9032 (defun org-at-table-hline-p ()
9033 "Return t if the cursor is inside a hline in a table."
9034 (if org-enable-table-editor
9036 (beginning-of-line 1)
9037 (looking-at org-table-hline-regexp
))
9040 (defun org-table-insert-column ()
9041 "Insert a new column into the table."
9043 (if (not (org-at-table-p))
9044 (error "Not at a table"))
9045 (org-table-find-dataline)
9046 (let* ((col (max 1 (org-table-current-column)))
9047 (beg (org-table-begin))
9048 (end (org-table-end))
9049 ;; Current cursor position
9050 (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
9053 (while (< (point) end
)
9054 (if (org-at-table-hline-p)
9056 (org-table-goto-column col t
)
9058 (beginning-of-line 2))
9059 (move-marker end nil
)
9061 (org-table-goto-column colpos
)
9063 (org-table-modify-formulas 'insert col
)))
9065 (defun org-table-find-dataline ()
9066 "Find a dataline in the current table, which is needed for column commands."
9067 (if (and (org-at-table-p)
9068 (not (org-at-table-hline-p)))
9070 (let ((col (current-column))
9071 (end (org-table-end)))
9072 (move-to-column col
)
9073 (while (and (< (point) end
)
9074 (or (not (= (current-column) col
))
9075 (org-at-table-hline-p)))
9076 (beginning-of-line 2)
9077 (move-to-column col
))
9078 (if (and (org-at-table-p)
9079 (not (org-at-table-hline-p)))
9082 "Please position cursor in a data line for column operations")))))
9084 (defun org-table-delete-column ()
9085 "Delete a column into the table."
9087 (if (not (org-at-table-p))
9088 (error "Not at a table"))
9089 (org-table-find-dataline)
9090 (org-table-check-inside-data-field)
9091 (let* ((col (org-table-current-column))
9092 (beg (org-table-begin))
9093 (end (org-table-end))
9094 ;; Current cursor position
9095 (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
9098 (while (< (point) end
)
9099 (if (org-at-table-hline-p)
9101 (org-table-goto-column col t
)
9102 (and (looking-at "|[^|\n]+|")
9103 (replace-match "|")))
9104 (beginning-of-line 2))
9105 (move-marker end nil
)
9107 (org-table-goto-column colpos
)
9109 (org-table-modify-formulas 'remove col
)))
9111 (defun org-table-move-column-right ()
9112 "Move column to the right."
9114 (org-table-move-column nil
))
9115 (defun org-table-move-column-left ()
9116 "Move column to the left."
9118 (org-table-move-column 'left
))
9120 (defun org-table-move-column (&optional left
)
9121 "Move the current column to the right. With arg LEFT, move to the left."
9123 (if (not (org-at-table-p))
9124 (error "Not at a table"))
9125 (org-table-find-dataline)
9126 (org-table-check-inside-data-field)
9127 (let* ((col (org-table-current-column))
9128 (col1 (if left
(1- col
) col
))
9129 (beg (org-table-begin))
9130 (end (org-table-end))
9131 ;; Current cursor position
9132 (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
9133 (colpos (if left
(1- col
) (1+ col
))))
9134 (if (and left
(= col
1))
9135 (error "Cannot move column further left"))
9136 (if (and (not left
) (looking-at "[^|\n]*|[^|\n]*$"))
9137 (error "Cannot move column further right"))
9139 (while (< (point) end
)
9140 (if (org-at-table-hline-p)
9142 (org-table-goto-column col1 t
)
9143 (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
9144 (replace-match "|\\2|\\1|")))
9145 (beginning-of-line 2))
9146 (move-marker end nil
)
9148 (org-table-goto-column colpos
)
9150 (org-table-modify-formulas 'swap col
(if left
(1- col
) (1+ col
)))))
9152 (defun org-table-move-row-down ()
9153 "Move table row down."
9155 (org-table-move-row nil
))
9156 (defun org-table-move-row-up ()
9157 "Move table row up."
9159 (org-table-move-row 'up
))
9161 (defun org-table-move-row (&optional up
)
9162 "Move the current table line down. With arg UP, move it up."
9164 (let ((col (current-column))
9168 (beginning-of-line tonew
)
9169 (if (not (org-at-table-p))
9172 (error "Cannot move row further")))
9174 (beginning-of-line 1)
9176 (setq txt
(buffer-substring (point) (1+ (point-at-eol))))
9177 (delete-region (point) (1+ (point-at-eol)))
9178 (beginning-of-line tonew
)
9180 (beginning-of-line 0)
9181 (move-to-column col
)))
9183 (defun org-table-insert-row (&optional arg
)
9184 "Insert a new row above the current line into the table.
9185 With prefix ARG, insert below the current line."
9187 (if (not (org-at-table-p))
9188 (error "Not at a table"))
9189 (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
9190 (new (org-table-clean-line line
)))
9191 ;; Fix the first field if necessary
9192 (if (string-match "^[ \t]*| *[#$] *|" line
)
9193 (setq new
(replace-match (match-string 0 line
) t t new
)))
9194 (beginning-of-line (if arg
2 1))
9195 (let (org-table-may-need-update) (insert-before-markers new
"\n"))
9196 (beginning-of-line 0)
9197 (re-search-forward "| ?" (point-at-eol) t
)
9198 (and org-table-may-need-update
(org-table-align))))
9200 (defun org-table-insert-hline (&optional arg
)
9201 "Insert a horizontal-line below the current line into the table.
9202 With prefix ARG, insert above the current line."
9204 (if (not (org-at-table-p))
9205 (error "Not at a table"))
9206 (let ((line (org-table-clean-line
9207 (buffer-substring (point-at-bol) (point-at-eol))))
9208 (col (current-column)))
9209 (while (string-match "|\\( +\\)|" line
)
9210 (setq line
(replace-match
9211 (concat "+" (make-string (- (match-end 1) (match-beginning 1))
9212 ?-
) "|") t t line
)))
9213 (and (string-match "\\+" line
) (setq line
(replace-match "|" t t line
)))
9214 (beginning-of-line (if arg
1 2))
9216 (beginning-of-line (if arg
1 -
1))
9217 (move-to-column col
)))
9219 (defun org-table-clean-line (s)
9220 "Convert a table line S into a string with only \"|\" and space.
9221 In particular, this does handle wide and invisible characters."
9222 (if (string-match "^[ \t]*|-" s
)
9223 ;; It's a hline, just map the characters
9224 (setq s
(mapcar (lambda (x) (if (member x
'(?| ?
+)) ?| ?\
)) s
))
9225 (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s
)
9226 (setq s
(replace-match
9227 (concat "|" (make-string (org-string-width (match-string 1 s
))
9232 (defun org-table-kill-row ()
9233 "Delete the current row or horizontal line from the table."
9235 (if (not (org-at-table-p))
9236 (error "Not at a table"))
9237 (let ((col (current-column)))
9238 (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
9239 (if (not (org-at-table-p)) (beginning-of-line 0))
9240 (move-to-column col
)))
9242 (defun org-table-sort-lines (beg end numericp
)
9243 "Sort table lines in region.
9244 Point and mark define the first and last line to include. Both point and
9245 mark should be in the column that is used for sorting. For example, to
9246 sort according to column 3, put the mark in the first line to sort, in
9247 table column 3. Put point into the last line to be included in the sorting,
9248 also in table column 3. The command will prompt for the sorting method
9249 \(n for numerical, a for alphanumeric)."
9250 (interactive "r\nsSorting method: [n]=numeric [a]=alpha: ")
9251 (setq numericp
(string-match "[nN]" numericp
))
9252 (org-table-align) ;; Just to be safe
9253 (let* (bcol ecol cmp column lns
)
9255 (org-table-check-inside-data-field)
9256 (setq column
(org-table-current-column)
9257 beg
(move-marker (make-marker) (point-at-bol)))
9259 (org-table-check-inside-data-field)
9260 (setq end
(move-marker (make-marker) (1+ (point-at-eol))))
9263 (org-table-goto-column column
)
9264 (skip-chars-backward "^|")
9265 (setq bcol
(current-column))
9266 (org-table-goto-column (1+ column
))
9267 (skip-chars-backward "^|")
9268 (setq ecol
(1- (current-column)))
9269 (setq cmp
(if numericp
9270 (lambda (a b
) (< (car a
) (car b
)))
9271 (lambda (a b
) (string< (car a
) (car b
)))))
9272 (setq lns
(mapcar (lambda(x) (cons (org-trim (substring x bcol ecol
)) x
))
9273 (split-string (buffer-substring beg end
) "\n")))
9275 (setq lns
(mapcar (lambda(x)
9276 (cons (string-to-number (car x
)) (cdr x
)))
9278 (delete-region beg end
)
9279 (move-marker beg nil
)
9280 (move-marker end nil
)
9281 (insert (mapconcat 'cdr
(setq lns
(sort lns cmp
)) "\n") "\n")
9282 (message "%d lines sorted %s based on column %d"
9284 (if numericp
"numerically" "alphabetically") column
)))
9286 (defun org-table-cut-region (beg end
)
9287 "Copy region in table to the clipboard and blank all relevant fields."
9289 (org-table-copy-region beg end
'cut
))
9291 (defun org-table-copy-region (beg end
&optional cut
)
9292 "Copy rectangular region in table to clipboard.
9293 A special clipboard is used which can only be accessed
9294 with `org-table-paste-rectangle'."
9296 (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
9298 (rpl (if cut
" " nil
)))
9300 (org-table-check-inside-data-field)
9301 (setq l01
(count-lines (point-min) (point))
9302 c01
(org-table-current-column))
9304 (org-table-check-inside-data-field)
9305 (setq l02
(count-lines (point-min) (point))
9306 c02
(org-table-current-column))
9307 (setq l1
(min l01 l02
) l2
(max l01 l02
)
9308 c1
(min c01 c02
) c2
(max c01 c02
))
9312 (if (> l1 l2
) (throw 'exit t
))
9314 (if (org-at-table-hline-p) (throw 'nextline
(setq l1
(1+ l1
))))
9315 (setq cols nil ic1 c1 ic2 c2
)
9316 (while (< ic1
(1+ ic2
))
9317 (push (org-table-get-field ic1 rpl
) cols
)
9318 (setq ic1
(1+ ic1
)))
9319 (push (nreverse cols
) region
)
9320 (setq l1
(1+ l1
)))))
9321 (setq org-table-clip
(nreverse region
))
9322 (if cut
(org-table-align))
9325 (defun org-table-paste-rectangle ()
9326 "Paste a rectangular region into a table.
9327 The upper right corner ends up in the current field. All involved fields
9328 will be overwritten. If the rectangle does not fit into the present table,
9329 the table is enlarged as needed. The process ignores horizontal separator
9332 (unless (and org-table-clip
(listp org-table-clip
))
9333 (error "First cut/copy a region to paste!"))
9334 (org-table-check-inside-data-field)
9335 (let* ((clip org-table-clip
)
9336 (line (count-lines (point-min) (point)))
9337 (col (org-table-current-column))
9338 (org-enable-table-editor t
)
9339 (org-table-automatic-realign nil
)
9341 (while (setq cols
(pop clip
))
9342 (while (org-at-table-hline-p) (beginning-of-line 2))
9343 (if (not (org-at-table-p))
9344 (progn (end-of-line 0) (org-table-next-field)))
9346 (while (setq field
(pop cols
))
9347 (org-table-goto-column c nil
'force
)
9348 (org-table-get-field nil field
)
9350 (beginning-of-line 2))
9352 (org-table-goto-column col
)
9355 (defun org-table-convert ()
9356 "Convert from `org-mode' table to table.el and back.
9357 Obviously, this only works within limits. When an Org-mode table is
9358 converted to table.el, all horizontal separator lines get lost, because
9359 table.el uses these as cell boundaries and has no notion of horizontal lines.
9360 A table.el table can be converted to an Org-mode table only if it does not
9361 do row or column spanning. Multiline cells will become multiple cells.
9362 Beware, Org-mode does not test if the table can be successfully converted - it
9363 blindly applies a recipe that works for simple tables."
9366 (if (org-at-table.el-p
)
9367 ;; convert to Org-mode table
9368 (let ((beg (move-marker (make-marker) (org-table-begin t
)))
9369 (end (move-marker (make-marker) (org-table-end t
))))
9370 (table-unrecognize-region beg end
)
9372 (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t
)
9375 (if (org-at-table-p)
9376 ;; convert to table.el table
9377 (let ((beg (move-marker (make-marker) (org-table-begin)))
9378 (end (move-marker (make-marker) (org-table-end))))
9379 ;; first, get rid of all horizontal lines
9381 (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t
)
9383 ;; insert a hline before first
9385 (org-table-insert-hline 'above
)
9386 (beginning-of-line -
1)
9387 ;; insert a hline after each line
9388 (while (progn (beginning-of-line 3) (< (point) end
))
9389 (org-table-insert-hline))
9391 (setq end
(move-marker end
(org-table-end)))
9392 ;; replace "+" at beginning and ending of hlines
9393 (while (re-search-forward "^\\([ \t]*\\)|-" end t
)
9394 (replace-match "\\1+-"))
9396 (while (re-search-forward "-|[ \t]*$" end t
)
9397 (replace-match "-+"))
9400 (defun org-table-wrap-region (arg)
9401 "Wrap several fields in a column like a paragraph.
9402 This is useful if you'd like to spread the contents of a field over several
9403 lines, in order to keep the table compact.
9405 If there is an active region, and both point and mark are in the same column,
9406 the text in the column is wrapped to minimum width for the given number of
9407 lines. Generally, this makes the table more compact. A prefix ARG may be
9408 used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
9409 formats the selected text to two lines. If the region was longer than two
9410 lines, the remaining lines remain empty. A negative prefix argument reduces
9411 the current number of lines by that amount. The wrapped text is pasted back
9412 into the table. If you formatted it to more lines than it was before, fields
9413 further down in the table get overwritten - so you might need to make space in
9416 If there is no region, the current field is split at the cursor position and
9417 the text fragment to the right of the cursor is prepended to the field one
9420 If there is no region, but you specify a prefix ARG, the current field gets
9421 blank, and the content is appended to the field above."
9423 (org-table-check-inside-data-field)
9424 (if (org-region-active-p)
9425 ;; There is a region: fill as a paragraph
9426 (let ((beg (region-beginning))
9428 (org-table-cut-region (region-beginning) (region-end))
9429 (if (> (length (car org-table-clip
)) 1)
9430 (error "Region must be limited to single column"))
9431 (setq nlines
(if arg
9433 (+ (length org-table-clip
) arg
)
9435 (length org-table-clip
)))
9436 (setq org-table-clip
9437 (mapcar 'list
(org-wrap (mapconcat 'car org-table-clip
" ")
9440 (org-table-paste-rectangle))
9441 ;; No region, split the current field at point
9443 ;; combine with field above
9444 (let ((s (org-table-blank-field))
9445 (col (org-table-current-column)))
9446 (beginning-of-line 0)
9447 (while (org-at-table-hline-p) (beginning-of-line 0))
9448 (org-table-goto-column col
)
9449 (skip-chars-forward "^|")
9450 (skip-chars-backward " ")
9451 (insert " " (org-trim s
))
9454 (when (looking-at "\\([^|]+\\)+|")
9455 (let ((s (match-string 1)))
9456 (replace-match " |")
9457 (goto-char (match-beginning 0))
9458 (org-table-next-row)
9459 (insert (org-trim s
) " ")
9460 (org-table-align))))))
9462 (defvar org-field-marker nil
)
9464 (defun org-table-edit-field (arg)
9465 "Edit table field in a different window.
9466 This is mainly useful for fields that contain hidden parts.
9467 When called with a \\[universal-argument] prefix, just make the full field visible so that
9468 it can be edited in place."
9471 (let ((b (save-excursion (skip-chars-backward "^|") (point)))
9472 (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
9473 (remove-text-properties b e
'(org-cwidth t invisible t
9474 display t intangible t
))
9475 (if (and (boundp 'font-lock-mode
) font-lock-mode
)
9476 (font-lock-fontify-block)))
9477 (let ((pos (move-marker (make-marker) (point)))
9478 (field (org-table-get-field))
9479 (cw (current-window-configuration))
9481 (switch-to-buffer-other-window "*Org tmp*")
9483 (insert "#\n# Edit field and finish with C-c C-c\n#\n")
9485 (goto-char (setq p
(point-max)))
9486 (insert (org-trim field
))
9487 (remove-text-properties p
(point-max)
9488 '(invisible t org-cwidth t display t
9491 (set (make-local-variable 'org-finish-function
)
9492 'org-table-finish-edit-field
)
9493 (set (make-local-variable 'org-window-configuration
) cw
)
9494 (set (make-local-variable 'org-field-marker
) pos
)
9495 (message "Edit and finish with C-c C-c"))))
9497 (defun org-table-finish-edit-field ()
9498 "Finish editing a table data field.
9499 Remove all newline characters, insert the result into the table, realign
9500 the table and kill the editing buffer."
9501 (let ((pos org-field-marker
)
9502 (cw org-window-configuration
)
9503 (cb (current-buffer))
9505 (goto-char (point-min))
9506 (while (re-search-forward "^#.*\n?" nil t
) (replace-match ""))
9507 (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t
)
9508 (replace-match " "))
9509 (setq text
(org-trim (buffer-string)))
9510 (set-window-configuration cw
)
9512 (select-window (get-buffer-window (marker-buffer pos
)))
9514 (move-marker pos nil
)
9515 (org-table-check-inside-data-field)
9516 (org-table-get-field nil text
)
9518 (message "New field value inserted")))
9521 "Remove whitespace at beginning and end of string."
9522 (if (string-match "^[ \t]+" s
) (setq s
(replace-match "" t t s
)))
9523 (if (string-match "[ \t]+$" s
) (setq s
(replace-match "" t t s
)))
9526 (defun org-wrap (string &optional width lines
)
9527 "Wrap string to either a number of lines, or a width in characters.
9528 If WIDTH is non-nil, the string is wrapped to that width, however many lines
9529 that costs. If there is a word longer than WIDTH, the text is actually
9530 wrapped to the length of that word.
9531 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
9532 many lines, whatever width that takes.
9533 The return value is a list of lines, without newlines at the end."
9534 (let* ((words (org-split-string string
"[ \t\n]+"))
9535 (maxword (apply 'max
(mapcar 'org-string-width words
)))
9538 (org-do-wrap words
(max maxword width
)))
9541 (setq ll
(org-do-wrap words maxword
))
9542 (if (<= (length ll
) lines
)
9545 (while (> (length ll
) lines
)
9547 (setq ll
(org-do-wrap words w
)))
9549 (t (error "Cannot wrap this")))))
9552 (defun org-do-wrap (words width
)
9553 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
9556 (setq line
(pop words
))
9557 (while (and words
(< (+ (length line
) (length (car words
))) width
))
9558 (setq line
(concat line
" " (pop words
))))
9559 (setq lines
(push line lines
)))
9562 (defun org-split-string (string &optional separators
)
9563 "Splits STRING into substrings at SEPARATORS.
9564 No empty strings are returned if there are matches at the beginning
9566 (let ((rexp (or separators
"[ \f\t\n\r\v]+"))
9570 (while (and (string-match rexp string
9572 (= start
(match-beginning 0))
9573 (< start
(length string
)))
9575 (< (match-beginning 0) (length string
)))
9577 (or (eq (match-beginning 0) 0)
9578 (and (eq (match-beginning 0) (match-end 0))
9579 (eq (match-beginning 0) start
))
9581 (cons (substring string start
(match-beginning 0))
9583 (setq start
(match-end 0)))
9584 (or (eq start
(length string
))
9586 (cons (substring string start
)
9590 (defun org-table-map-tables (function)
9591 "Apply FUNCTION to the start of all tables in the buffer."
9595 (goto-char (point-min))
9596 (while (re-search-forward org-table-any-line-regexp nil t
)
9597 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
9598 (beginning-of-line 1)
9599 (if (looking-at org-table-line-regexp
)
9600 (save-excursion (funcall function
)))
9601 (re-search-forward org-table-any-border-regexp nil
1))))
9602 (message "Mapping tables: done"))
9604 (defun org-table-sum (&optional beg end nlast
)
9605 "Sum numbers in region of current table column.
9606 The result will be displayed in the echo area, and will be available
9607 as kill to be inserted with \\[yank].
9609 If there is an active region, it is interpreted as a rectangle and all
9610 numbers in that rectangle will be summed. If there is no active
9611 region and point is located in a table column, sum all numbers in that
9614 If at least one number looks like a time HH:MM or HH:MM:SS, all other
9615 numbers are assumed to be times as well (in decimal hours) and the
9616 numbers are added as such.
9618 If NLAST is a number, only the NLAST fields will actually be summed."
9621 (let (col (timecnt 0) diff h m s org-table-clip
)
9623 ((and beg end
)) ; beg and end given explicitly
9624 ((org-region-active-p)
9625 (setq beg
(region-beginning) end
(region-end)))
9627 (setq col
(org-table-current-column))
9628 (goto-char (org-table-begin))
9629 (unless (re-search-forward "^[ \t]*|[^-]" nil t
)
9630 (error "No table data"))
9631 (org-table-goto-column col
)
9632 ;not needed? (skip-chars-backward "^|")
9634 (goto-char (org-table-end))
9635 (unless (re-search-backward "^[ \t]*|[^-]" nil t
)
9636 (error "No table data"))
9637 (org-table-goto-column col
)
9638 ;not needed? (skip-chars-forward "^|")
9639 (setq end
(point))))
9640 (let* ((items (apply 'append
(org-table-copy-region beg end
)))
9641 (items1 (cond ((not nlast
) items
)
9642 ((>= nlast
(length items
)) items
)
9643 (t (setq items
(reverse items
))
9644 (setcdr (nthcdr (1- nlast
) items
) nil
)
9646 (numbers (delq nil
(mapcar 'org-table-get-number-for-summing
9648 (res (apply '+ numbers
))
9649 (sres (if (= timecnt
0)
9651 (setq diff
(* 3600 res
)
9652 h
(floor (/ diff
3600)) diff
(mod diff
3600)
9653 m
(floor (/ diff
60)) diff
(mod diff
60)
9655 (format "%d:%02d:%02d" h m s
))))
9659 (substitute-command-keys
9660 (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
9661 (length numbers
) sres
))))
9664 (defun org-table-get-number-for-summing (s)
9666 (if (string-match "^ *|? *" s
)
9667 (setq s
(replace-match "" nil nil s
)))
9668 (if (string-match " *|? *$" s
)
9669 (setq s
(replace-match "" nil nil s
)))
9670 (setq n
(string-to-number s
))
9672 ((and (string-match "0" s
)
9673 (string-match "\\`[-+ \t0.edED]+\\'" s
)) 0)
9674 ((string-match "\\`[ \t]+\\'" s
) nil
)
9675 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s
)
9676 (let ((h (string-to-number (or (match-string 1 s
) "0")))
9677 (m (string-to-number (or (match-string 2 s
) "0")))
9678 (s (string-to-number (or (match-string 4 s
) "0"))))
9679 (if (boundp 'timecnt
) (setq timecnt
(1+ timecnt
)))
9680 (* 1.0 (+ h
(/ m
60.0) (/ s
3600.0)))))
9684 (defun org-table-get-vertical-vector (desc &optional tbeg col
)
9685 "Get a calc vector from a column, accorting to descriptor DESC.
9686 Optional arguments TBEG and COL can give the beginning of the table and
9687 the current column, to avoid unnecessary parsing."
9689 (or tbeg
(setq tbeg
(org-table-begin)))
9690 (or col
(setq col
(org-table-current-column)))
9691 (let (beg end nn n n1 n2 l
(thisline (org-current-line)) hline-list
)
9693 ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc
)
9694 (setq n1
(- (match-end 1) (match-beginning 1)))
9695 (if (match-beginning 3)
9696 (setq n2
(- (match-end 2) (match-beginning 3))))
9697 (setq n
(if n2
(max n1 n2
) n1
))
9698 (setq n1
(if n2
(min n1 n2
)))
9700 (while (and (> nn
0)
9701 (re-search-backward org-table-hline-regexp tbeg t
))
9702 (push (org-current-line) hline-list
)
9704 (setq hline-list
(nreverse hline-list
))
9705 (goto-line (nth (1- n
) hline-list
))
9706 (when (re-search-forward org-table-dataline-regexp
)
9707 (org-table-goto-column col
)
9709 (goto-line (if n1
(nth (1- n1
) hline-list
) thisline
))
9710 (when (re-search-backward org-table-dataline-regexp
)
9711 (org-table-goto-column col
)
9713 (setq l
(apply 'append
(org-table-copy-region beg end
)))
9714 (concat "[" (mapconcat (lambda (x) (setq x
(org-trim x
))
9715 (if (equal x
"") "0" x
))
9717 ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc
)
9718 (setq n1
(string-to-number (match-string 1 desc
))
9719 n2
(string-to-number (match-string 2 desc
)))
9720 (beginning-of-line 1)
9722 (when (re-search-backward org-table-dataline-regexp tbeg t n1
)
9723 (org-table-goto-column col
)
9724 (setq beg
(point))))
9725 (when (re-search-backward org-table-dataline-regexp tbeg t n2
)
9726 (org-table-goto-column col
)
9728 (setq l
(apply 'append
(org-table-copy-region beg end
)))
9729 (concat "[" (mapconcat
9730 (lambda (x) (setq x
(org-trim x
))
9731 (if (equal x
"") "0" x
))
9733 ((string-match "\\([0-9]+\\)" desc
)
9734 (beginning-of-line 1)
9735 (when (re-search-backward org-table-dataline-regexp tbeg t
9736 (string-to-number (match-string 0 desc
)))
9737 (org-table-goto-column col
)
9738 (org-trim (org-table-get-field))))))))
9740 (defvar org-table-formula-history nil
)
9742 (defvar org-table-column-names nil
9743 "Alist with column names, derived from the `!' line.")
9744 (defvar org-table-column-name-regexp nil
9745 "Regular expression matching the current column names.")
9746 (defvar org-table-local-parameters nil
9747 "Alist with parameter names, derived from the `$' line.")
9748 (defvar org-table-named-field-locations nil
9749 "Alist with locations of named fields.")
9751 (defun org-table-get-formula (&optional equation named
)
9752 "Read a formula from the minibuffer, offer stored formula as default."
9753 (let* ((name (car (rassoc (list (org-current-line)
9754 (org-table-current-column))
9755 org-table-named-field-locations
)))
9758 (error "Not in a named field"))
9759 (int-to-string (org-table-current-column))))
9760 (dummy (and name
(not named
)
9761 (not (y-or-n-p "Replace named-field formula with column equation? " ))
9763 (org-table-may-need-update nil
)
9764 (stored-list (org-table-get-stored-formulas))
9765 (stored (cdr (assoc scol stored-list
)))
9767 ((and stored equation
(string-match "^ *=? *$" equation
))
9772 (format "%s formula $%s=" (if named
"Field" "Column") scol
)
9773 (or stored
"") 'org-table-formula-history
9777 (when (not (string-match "\\S-" eq
))
9779 (setq stored-list
(delq (assoc scol stored-list
) stored-list
))
9780 (org-table-store-formulas stored-list
)
9781 (error "Formula removed"))
9782 (if (string-match "^ *=?" eq
) (setq eq
(replace-match "" t t eq
)))
9783 (if (string-match " *$" eq
) (setq eq
(replace-match "" t t eq
)))
9784 (if (and name
(not named
))
9785 ;; We set the column equation, delete the named one.
9786 (setq stored-list
(delq (assoc name stored-list
) stored-list
)
9789 (setcdr (assoc scol stored-list
) eq
)
9790 (setq stored-list
(cons (cons scol eq
) stored-list
)))
9791 (if (or mustsave
(not (equal stored eq
)))
9792 (org-table-store-formulas stored-list
))
9795 (defun org-table-store-formulas (alist)
9796 "Store the list of formulas below the current table."
9797 (setq alist
(sort alist
(lambda (a b
) (string< (car a
) (car b
)))))
9799 (goto-char (org-table-end))
9800 (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
9801 (delete-region (point) (match-end 0)))
9803 (mapconcat (lambda (x)
9804 (concat "$" (car x
) "=" (cdr x
)))
9808 (defun org-table-get-stored-formulas ()
9809 "Return an alist with the t=stored formulas directly after current table."
9811 (let (scol eq eq-alist strings string seen
)
9813 (goto-char (org-table-end))
9814 (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
9815 (setq strings
(org-split-string (match-string 2) " *:: *"))
9816 (while (setq string
(pop strings
))
9817 (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string
)
9818 (setq scol
(match-string 1 string
)
9819 eq
(match-string 2 string
)
9820 eq-alist
(cons (cons scol eq
) eq-alist
))
9821 (if (member scol seen
)
9822 (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol
)
9823 (push scol seen
))))))
9824 (nreverse eq-alist
)))
9826 (defun org-table-modify-formulas (action &rest columns
)
9827 "Modify the formulas stored below the current table.
9828 ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
9829 expected, for the other actions only a single column number is needed."
9830 (let ((list (org-table-get-stored-formulas))
9831 (nmax (length (org-split-string
9832 (buffer-substring (point-at-bol) (point-at-eol))
9834 col col1 col2 scol si sc1 sc2
)
9836 ((null list
)) ; No action needed if there are no stored formulas
9837 ((eq action
'remove
)
9838 (setq col
(car columns
)
9839 scol
(int-to-string col
))
9840 (org-table-replace-in-formulas list scol
"INVALID")
9841 (if (assoc scol list
) (setq list
(delq (assoc scol list
) list
)))
9842 (loop for i from
(1+ col
) upto nmax by
1 do
9843 (setq si
(int-to-string i
))
9844 (org-table-replace-in-formulas list si
(int-to-string (1- i
)))
9845 (if (assoc si list
) (setcar (assoc si list
)
9846 (int-to-string (1- i
))))))
9847 ((eq action
'insert
)
9848 (setq col
(car columns
))
9849 (loop for i from nmax downto col by
1 do
9850 (setq si
(int-to-string i
))
9851 (org-table-replace-in-formulas list si
(int-to-string (1+ i
)))
9852 (if (assoc si list
) (setcar (assoc si list
)
9853 (int-to-string (1+ i
))))))
9855 (setq col1
(car columns
) col2
(nth 1 columns
)
9856 sc1
(int-to-string col1
) sc2
(int-to-string col2
))
9857 ;; Hopefully, ZqZtZ will never be a name in a table
9858 (org-table-replace-in-formulas list sc1
"ZqZtZ")
9859 (org-table-replace-in-formulas list sc2 sc1
)
9860 (org-table-replace-in-formulas list
"ZqZtZ" sc2
)
9861 (if (assoc sc1 list
) (setcar (assoc sc1 list
) "ZqZtZ"))
9862 (if (assoc sc2 list
) (setcar (assoc sc2 list
) sc1
))
9863 (if (assoc "ZqZtZ" list
) (setcar (assoc "ZqZtZ" list
) sc2
)))
9864 (t (error "Invalid action in `org-table-modify-formulas'")))
9865 (if list
(org-table-store-formulas list
))))
9867 (defun org-table-replace-in-formulas (list s1 s2
)
9869 (setq s1
(concat "$" (if (integerp s1
) (int-to-string s1
) s1
))
9870 s2
(concat "$" (if (integerp s2
) (int-to-string s2
) s2
))
9871 re
(concat (regexp-quote s1
) "\\>"))
9872 (while (setq elt
(pop list
))
9874 (while (string-match re s
)
9875 (setq s
(replace-match s2 t t s
)))
9878 (defun org-table-get-specials ()
9879 "Get the column names and local parameters for this table."
9881 (let ((beg (org-table-begin)) (end (org-table-end))
9882 names name fields fields1 field cnt c v line col
)
9883 (setq org-table-column-names nil
9884 org-table-local-parameters nil
9885 org-table-named-field-locations nil
)
9887 (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t
)
9888 (setq names
(org-split-string (match-string 1) " *| *")
9890 (while (setq name
(pop names
))
9892 (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name
)
9893 (push (cons name
(int-to-string cnt
)) org-table-column-names
))))
9894 (setq org-table-column-names
(nreverse org-table-column-names
))
9895 (setq org-table-column-name-regexp
9896 (concat "\\$\\(" (mapconcat 'car org-table-column-names
"\\|") "\\)\\>"))
9898 (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t
)
9899 (setq fields
(org-split-string (match-string 1) " *| *"))
9900 (while (setq field
(pop fields
))
9901 (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field
)
9902 (push (cons (match-string 1 field
) (match-string 2 field
))
9903 org-table-local-parameters
))))
9905 (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t
)
9906 (setq c
(match-string 1)
9907 fields
(org-split-string (match-string 2) " *| *"))
9909 (beginning-of-line (if (equal c
"_") 2 0))
9910 (setq line
(org-current-line) col
1)
9911 (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
9912 (setq fields1
(org-split-string (match-string 1) " *| *"))))
9913 (while (and fields1
(setq field
(pop fields
)))
9914 (setq v
(pop fields1
) col
(1+ col
))
9915 (when (and (stringp field
) (stringp v
)
9916 (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field
))
9917 (push (cons field v
) org-table-local-parameters
)
9918 (push (list field line col
) org-table-named-field-locations
)))))))
9920 (defun org-this-word ()
9921 ;; Get the current word
9923 (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
9924 (end (progn (skip-chars-forward "^ \t\n") (point))))
9925 (buffer-substring-no-properties beg end
))))
9927 (defun org-table-maybe-eval-formula ()
9928 "Check if the current field starts with \"=\" or \":=\".
9929 If yes, store the formula and apply it."
9930 ;; We already know we are in a table. Get field will only return a formula
9931 ;; when appropriate. It might return a separator line, but no problem.
9932 (when org-table-formula-evaluate-inline
9933 (let* ((field (org-trim (or (org-table-get-field) "")))
9935 (when (string-match "^:?=\\(.*\\)" field
)
9936 (setq named
(equal (string-to-char field
) ?
:)
9937 eq
(match-string 1 field
))
9938 (if (fboundp 'calc-eval
)
9939 (org-table-eval-formula (if named
'(4) nil
) eq
))))))
9941 (defvar org-recalc-commands nil
9942 "List of commands triggering the recalculation of a line.
9943 Will be filled automatically during use.")
9945 (defvar org-recalc-marks
9946 '((" " .
"Unmarked: no special line, no automatic recalculation")
9947 ("#" .
"Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
9948 ("*" .
"Recalculate only when entire table is recalculated with `C-u C-c *'")
9949 ("!" .
"Column name definition line. Reference in formula as $name.")
9950 ("$" .
"Parameter definition line name=value. Reference in formula as $name.")
9951 ("_" .
"Names for values in row below this one.")
9952 ("^" .
"Names for values in row above this one.")))
9954 (defun org-table-rotate-recalc-marks (&optional newchar
)
9955 "Rotate the recalculation mark in the first column.
9956 If in any row, the first field is not consistent with a mark,
9957 insert a new column for the markers.
9958 When there is an active region, change all the lines in the region,
9959 after prompting for the marking character.
9960 After each change, a message will be displayed indicating the meaning
9963 (unless (org-at-table-p) (error "Not at a table"))
9964 (let* ((marks (append (mapcar 'car org-recalc-marks
) '(" ")))
9965 (beg (org-table-begin))
9966 (end (org-table-end))
9967 (l (org-current-line))
9968 (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
9969 (l2 (if (org-region-active-p) (org-current-line (region-end))))
9973 (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t
))))
9974 (col (org-table-current-column))
9975 (forcenew (car (assoc newchar org-recalc-marks
)))
9978 (message "Change region to what mark? Type # * ! $ or SPC: ")
9979 (setq newchar
(char-to-string (read-char-exclusive))
9980 forcenew
(car (assoc newchar org-recalc-marks
))))
9981 (if (and newchar
(not forcenew
))
9982 (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
9984 (if l1
(goto-line l1
))
9986 (beginning-of-line 1)
9987 (unless (looking-at org-table-dataline-regexp
)
9988 (error "Not at a table data line")))
9990 (org-table-goto-column 1)
9991 (org-table-insert-column)
9992 (org-table-goto-column (1+ col
)))
9993 (setq epos
(point-at-eol))
9995 (beginning-of-line 1)
9996 (org-table-get-field
9997 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
9999 (setq new
(or forcenew
10000 (cadr (member (match-string 1) marks
))))
10006 (while (progn (beginning-of-line 2) (not (= (org-current-line) l2
)))
10007 (and (looking-at org-table-dataline-regexp
)
10008 (org-table-get-field 1 (concat " " new
" "))))
10010 (if (not (= epos
(point-at-eol))) (org-table-align))
10012 (and (interactive-p) (message (cdr (assoc new org-recalc-marks
))))))
10014 (defun org-table-maybe-recalculate-line ()
10015 "Recompute the current line if marked for it, and if we haven't just done it."
10017 (and org-table-allow-automatic-line-recalculation
10018 (not (and (memq last-command org-recalc-commands
)
10019 (equal org-last-recalc-line
(org-current-line))))
10020 (save-excursion (beginning-of-line 1)
10021 (looking-at org-table-auto-recalculate-regexp
))
10022 (fboundp 'calc-eval
)
10023 (org-table-recalculate) t
))
10025 (defvar org-table-formula-debug nil
10026 "Non-nil means, debug table formulas.
10027 When nil, simply write \"#ERROR\" in corrupted fields.")
10030 (defsubst org-set-calc-mode
(var &optional value
)
10032 (setq var
(assoc var
'(("D" calc-angle-mode deg
)
10033 ("R" calc-angle-mode rad
)
10034 ("F" calc-prefer-frac t
)
10035 ("S" calc-symbolic-mode t
)))
10036 value
(nth 2 var
) var
(nth 1 var
)))
10037 (if (memq var modes
)
10038 (setcar (cdr (memq var modes
)) value
)
10039 (cons var
(cons value modes
)))
10042 (defun org-table-eval-formula (&optional arg equation
10043 suppress-align suppress-const
10045 "Replace the table field value at the cursor by the result of a calculation.
10047 This function makes use of Dave Gillespie's Calc package, in my view the
10048 most exciting program ever written for GNU Emacs. So you need to have Calc
10049 installed in order to use this function.
10051 In a table, this command replaces the value in the current field with the
10052 result of a formula. It also installs the formula as the \"current\" column
10053 formula, by storing it in a special line below the table. When called
10054 with a `C-u' prefix, the current field must ba a named field, and the
10055 formula is installed as valid in only this specific field.
10057 When called, the command first prompts for a formula, which is read in
10058 the minibuffer. Previously entered formulas are available through the
10059 history list, and the last used formula is offered as a default.
10060 These stored formulas are adapted correctly when moving, inserting, or
10061 deleting columns with the corresponding commands.
10063 The formula can be any algebraic expression understood by the Calc package.
10064 For details, see the Org-mode manual.
10066 This function can also be called from Lisp programs and offers
10067 additional arguments: EQUATION can be the formula to apply. If this
10068 argument is given, the user will not be prompted. SUPPRESS-ALIGN is
10069 used to speed-up recursive calls by by-passing unnecessary aligns.
10070 SUPPRESS-CONST suppresses the interpretation of constants in the
10071 formula, assuming that this has been done already outside the function.
10072 SUPPRESS-STORE means the formula should not be stored, either because
10073 it is already stored, or because it is a modified equation that should
10074 not overwrite the stored one."
10077 (org-table-check-inside-data-field)
10078 (org-table-get-specials)
10080 (ndown (if (integerp arg
) arg
1))
10081 (org-table-automatic-realign nil
)
10082 (case-fold-search nil
)
10084 (formula (if (and equation suppress-store
)
10086 (org-table-get-formula equation
(equal arg
'(4)))))
10087 (n0 (org-table-current-column))
10088 (modes (copy-sequence org-calc-default-modes
))
10089 n form fmt x ev orig c
)
10090 ;; Parse the format string. Since we have a lot of modes, this is
10091 ;; a lot of work. However, I think calc still uses most of the time.
10092 (if (string-match ";" formula
)
10093 (let ((tmp (org-split-string formula
";")))
10094 (setq formula
(car tmp
)
10095 fmt
(concat (cdr (assoc "%" org-table-local-parameters
))
10097 (while (string-match "[pnfse]\\(-?[0-9]+\\)" fmt
)
10098 (setq c
(string-to-char (match-string 1 fmt
))
10099 n
(string-to-number (or (match-string 1 fmt
) "")))
10100 (if (= c ?p
) (setq modes
(org-set-calc-mode 'calc-internal-prec n
))
10101 (setq modes
(org-set-calc-mode
10103 (list (cdr (assoc c
'((?n . float
) (?f . fix
)
10104 (?s . sci
) (?e . eng
))))
10106 (setq fmt
(replace-match "" t t fmt
)))
10107 (while (string-match "[DRFS]" fmt
)
10108 (setq modes
(org-set-calc-mode (match-string 0 fmt
)))
10109 (setq fmt
(replace-match "" t t fmt
)))
10110 (unless (string-match "\\S-" fmt
)
10112 (if (and (not suppress-const
) org-table-formula-use-constants
)
10113 (setq formula
(org-table-formula-substitute-names formula
)))
10114 (setq orig
(or (get-text-property 1 :orig-formula formula
) "?"))
10116 (setq fields
(org-split-string
10118 (point-at-bol) (point-at-eol)) " *| *"))
10119 (if org-table-formula-numbers-only
10120 (setq fields
(mapcar
10121 (lambda (x) (number-to-string (string-to-number x
)))
10123 (setq ndown
(1- ndown
))
10124 (setq form
(copy-sequence formula
))
10125 ;; Insert the references to fields in same row
10126 (while (string-match "\\$\\([0-9]+\\)?" form
)
10127 (setq n
(if (match-beginning 1)
10128 (string-to-number (match-string 1 form
))
10130 x
(nth (1- n
) fields
))
10131 (unless x
(error "Invalid field specifier \"%s\""
10132 (match-string 0 form
)))
10133 (if (equal x
"") (setq x
"0"))
10134 (setq form
(replace-match (concat "(" x
")") t t form
)))
10135 ;; Insert ranges in current column
10136 (while (string-match "\\&[-I0-9]+" form
)
10137 (setq form
(replace-match
10139 (org-table-get-vertical-vector (match-string 0 form
)
10142 (setq ev
(calc-eval (cons form modes
)
10143 (if org-table-formula-numbers-only
'num
)))
10145 (when org-table-formula-debug
10146 (with-output-to-temp-buffer "*Help*"
10147 (princ (format "Substitution history of formula
10150 $1-> %s\n" orig formula form
))
10152 (princ (format " %s^\nError: %s"
10153 (make-string (car ev
) ?\-
) (nth 1 ev
)))
10154 (princ (format "Result: %s\nFormat: %s\nFinal: %s"
10156 (if fmt
(format fmt
(string-to-number ev
)) ev
)))))
10157 (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
10158 (unless (and (interactive-p) (not ndown
))
10159 (unless (let (inhibit-redisplay)
10160 (y-or-n-p "Debugging Formula. Continue to next? "))
10163 (delete-window (get-buffer-window "*Help*"))
10165 (if (listp ev
) (setq fmt nil ev
"#ERROR"))
10166 (org-table-justify-field-maybe
10167 (if fmt
(format fmt
(string-to-number ev
)) ev
))
10168 (if (and down
(> ndown
0) (looking-at ".*\n[ \t]*|[^-]"))
10169 (call-interactively 'org-return
)
10171 (and down
(org-table-maybe-recalculate-line))
10172 (or suppress-align
(and org-table-may-need-update
10173 (org-table-align)))))
10175 (defun org-table-recalculate (&optional all noalign
)
10176 "Recalculate the current table line by applying all stored formulas.
10177 With prefix arg ALL, do this for all lines in the table."
10179 (or (memq this-command org-recalc-commands
)
10180 (setq org-recalc-commands
(cons this-command org-recalc-commands
)))
10181 (unless (org-at-table-p) (error "Not at a table"))
10182 (org-table-get-specials)
10183 (let* ((eqlist (sort (org-table-get-stored-formulas)
10184 (lambda (a b
) (string< (car a
) (car b
)))))
10185 (inhibit-redisplay t
)
10186 (line-re org-table-dataline-regexp
)
10187 (thisline (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
10188 (thiscol (org-table-current-column))
10189 beg end entry eqlnum eqlname eql
(cnt 0) eq a name
)
10190 ;; Insert constants in all formulas
10192 (mapcar (lambda (x)
10193 (setcdr x
(org-table-formula-substitute-names (cdr x
)))
10196 ;; Split the equation list
10197 (while (setq eq
(pop eqlist
))
10198 (if (<= (string-to-char (car eq
)) ?
9)
10200 (push eq eqlname
)))
10201 (setq eqlnum
(nreverse eqlnum
) eqlname
(nreverse eqlname
))
10204 (setq end
(move-marker (make-marker) (1+ (org-table-end))))
10205 (goto-char (setq beg
(org-table-begin)))
10206 (if (re-search-forward org-table-calculate-mark-regexp end t
)
10207 ;; This is a table with marked lines, only compute selected lines
10208 (setq line-re org-table-recalculate-regexp
)
10209 ;; Move forward to the first non-header line
10210 (if (and (re-search-forward org-table-dataline-regexp end t
)
10211 (re-search-forward org-table-hline-regexp end t
)
10212 (re-search-forward org-table-dataline-regexp end t
))
10213 (setq beg
(match-beginning 0))
10214 nil
))) ;; just leave beg where it is
10215 (setq beg
(point-at-bol)
10216 end
(move-marker (make-marker) (1+ (point-at-eol)))))
10218 (and all
(message "Re-applying formulas to full table..."))
10219 (while (re-search-forward line-re end t
)
10220 (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
10221 ;; Unprotected line, recalculate
10222 (and all
(message "Re-applying formulas to full table...(line %d)"
10223 (setq cnt
(1+ cnt
))))
10224 (setq org-last-recalc-line
(org-current-line))
10226 (while (setq entry
(pop eql
))
10227 (goto-line org-last-recalc-line
)
10228 (org-table-goto-column (string-to-number (car entry
)) nil
'force
)
10229 (org-table-eval-formula nil
(cdr entry
) 'noalign
'nocst
'nostore
))))
10230 (goto-line thisline
)
10231 (org-table-goto-column thiscol
)
10232 (or noalign
(and org-table-may-need-update
(org-table-align))
10233 (and all
(message "Re-applying formulas to %d lines...done" cnt
)))
10234 ;; Now do the names fields
10235 (while (setq eq
(pop eqlname
))
10236 (setq name
(car eq
)
10237 a
(assoc name org-table-named-field-locations
))
10239 (message "Re-applying formula to named field: %s" name
)
10240 (goto-line (nth 1 a
))
10241 (org-table-goto-column (nth 2 a
))
10242 (org-table-eval-formula nil
(cdr eq
) 'noalign
'nocst
'nostore
)))
10243 ;; back to initial position
10244 (goto-line thisline
)
10245 (org-table-goto-column thiscol
)
10246 (or noalign
(and org-table-may-need-update
(org-table-align))
10247 (and all
(message "Re-applying formulas...done")))))
10249 (defun org-table-formula-substitute-names (f)
10250 "Replace $const with values in string F."
10251 (let ((start 0) a n1 n2 nn1 nn2 s
(f1 f
))
10252 ;; First, check for column names
10253 (while (setq start
(string-match org-table-column-name-regexp f start
))
10254 (setq start
(1+ start
))
10255 (setq a
(assoc (match-string 1 f
) org-table-column-names
))
10256 (setq f
(replace-match (concat "$" (cdr a
)) t t f
)))
10257 ;; Expand ranges to vectors
10258 (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f
)
10259 (setq n1
(string-to-number (match-string 1 f
))
10260 n2
(string-to-number (match-string 2 f
))
10261 nn1
(1+ (min n1 n2
)) nn2
(max n1 n2
)
10262 s
(concat "[($" (number-to-string (1- nn1
)) ")"))
10263 (loop for i from nn1 upto nn2 do
10264 (setq s
(concat s
",($" (int-to-string i
) ")")))
10265 (setq s
(concat s
"]"))
10266 (if (< n2 n1
) (setq s
(concat "rev(" s
")")))
10267 (setq f
(replace-match s t t f
)))
10268 ;; Parameters and constants
10270 (while (setq start
(string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start
))
10271 (setq start
(1+ start
))
10272 (if (setq a
(save-match-data
10273 (org-table-get-constant (match-string 1 f
))))
10274 (setq f
(replace-match (concat "(" a
")") t t f
))))
10275 (if org-table-formula-debug
10276 (put-text-property 0 (length f
) :orig-formula f1 f
))
10279 (defun org-table-get-constant (const)
10280 "Find the value for a parameter or constant in a formula.
10281 Parameters get priority."
10282 (or (cdr (assoc const org-table-local-parameters
))
10283 (cdr (assoc const org-table-formula-constants
))
10284 (and (fboundp 'constants-get
) (constants-get const
))
10285 "#UNDEFINED_NAME"))
10287 (defvar org-edit-formulas-map
(make-sparse-keymap))
10288 (define-key org-edit-formulas-map
"\C-c\C-c" 'org-finish-edit-formulas
)
10289 (define-key org-edit-formulas-map
"\C-c\C-q" 'org-abort-edit-formulas
)
10290 (define-key org-edit-formulas-map
"\C-c?" 'org-show-variable
)
10294 (defun org-table-edit-formulas ()
10295 "Edit the formulas of the current table in a separate buffer."
10297 (unless (org-at-table-p)
10298 (error "Not at a table"))
10299 (org-table-get-specials)
10300 (let ((eql (org-table-get-stored-formulas))
10301 (pos (move-marker (make-marker) (point)))
10302 (wc (current-window-configuration))
10304 (switch-to-buffer-other-window "*Edit Formulas*")
10307 (set (make-local-variable 'org-pos
) pos
)
10308 (set (make-local-variable 'org-window-configuration
) wc
)
10309 (use-local-map org-edit-formulas-map
)
10310 (setq s
"# Edit formulas and finish with `C-c C-c'.
10311 # Use `C-u C-c C-c' to also appy them immediately to the entire table.
10312 # Use `C-c ?' to get information about $name at point.
10313 # To cancel editing, press `C-c C-q'.\n")
10314 (put-text-property 0 (length s
) 'face
'font-lock-comment-face s
)
10316 (while (setq entry
(pop eql
))
10317 (when (setq loc
(assoc (car entry
) org-table-named-field-locations
))
10318 (setq s
(format "# Named formula, referring to column %d in line %d\n"
10319 (nth 2 loc
) (nth 1 loc
)))
10320 (put-text-property 0 (length s
) 'face
'font-lock-comment-face s
)
10322 (setq s
(concat "$" (car entry
) "=" (cdr entry
) "\n"))
10323 (remove-text-properties 0 (length s
) '(face nil
) s
)
10325 (goto-char (point-min))
10326 (message "Edit formulas and finish with `C-c C-c'.")))
10328 (defun org-show-variable ()
10329 "Show the location/value of the $ expression at point."
10331 (let (var (pos org-pos
) (win (selected-window)) e
)
10333 (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
10334 (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
10335 (setq var
(match-string 1))
10336 (error "No variable at point")))
10338 ((setq e
(assoc var org-table-named-field-locations
))
10339 (switch-to-buffer-other-window (marker-buffer pos
))
10340 (goto-line (nth 1 e
))
10341 (org-table-goto-column (nth 2 e
))
10342 (select-window win
)
10343 (message "Named field, column %d of line %d" (nth 2 e
) (nth 1 e
)))
10344 ((setq e
(assoc var org-table-column-names
))
10345 (switch-to-buffer-other-window (marker-buffer pos
))
10347 (goto-char (org-table-begin))
10348 (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var
"\\) *|")
10351 (goto-char (match-beginning 1))
10352 (message "Named column (column %s)" (cdr e
)))
10353 (error "Column name not found"))
10354 (select-window win
))
10355 ((string-match "^[0-9]$" var
)
10357 (switch-to-buffer-other-window (marker-buffer pos
))
10359 (goto-char (org-table-begin))
10361 (if (re-search-forward org-table-dataline-regexp
10364 (goto-char (match-beginning 0))
10365 (org-table-goto-column (string-to-number var
))
10366 (message "Column %s" var
))
10367 (error "Column name not found"))
10368 (select-window win
))
10369 ((setq e
(assoc var org-table-local-parameters
))
10370 (switch-to-buffer-other-window (marker-buffer pos
))
10372 (goto-char (org-table-begin))
10373 (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var
"=\\)") nil t
)
10375 (goto-char (match-beginning 1))
10376 (message "Local parameter."))
10377 (error "Parameter not found"))
10378 (select-window win
))
10381 ((setq e
(assoc var org-table-formula-constants
))
10382 (message "Constant: $%s=%s in `org-table-formula-constants'." var
(cdr e
)))
10383 ((setq e
(and (fboundp 'constants-get
) (constants-get var
)))
10384 (message "Constant: $%s=%s, retrieved from `constants.el'." var e
))
10385 (t (error "Undefined name $%s" var
)))))))
10387 (defun org-finish-edit-formulas (&optional arg
)
10388 "Parse the buffer for formula definitions and install them.
10389 With prefix ARG, apply the new formulas to the table."
10391 (let ((pos org-pos
) eql
)
10392 (goto-char (point-min))
10393 (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t
)
10394 (push (cons (match-string 1) (match-string 2)) eql
))
10395 (set-window-configuration org-window-configuration
)
10396 (select-window (get-buffer-window (marker-buffer pos
)))
10398 (unless (org-at-table-p)
10399 (error "Lost table position - cannot install formulae"))
10400 (org-table-store-formulas eql
)
10401 (move-marker pos nil
)
10402 (kill-buffer "*Edit Formulas*")
10404 (org-table-recalculate 'all
)
10405 (message "New formulas installed - press C-u C-c C-c to apply."))))
10407 (defun org-abort-edit-formulas ()
10408 "Abort editing formulas, without installing the changes."
10410 (let ((pos org-pos
))
10411 (set-window-configuration org-window-configuration
)
10412 (select-window (get-buffer-window (marker-buffer pos
)))
10414 (message "Formula editing aborted without installing changes")))
10416 ;;; The orgtbl minor mode
10418 ;; Define a minor mode which can be used in other modes in order to
10419 ;; integrate the org-mode table editor.
10421 ;; This is really a hack, because the org-mode table editor uses several
10422 ;; keys which normally belong to the major mode, for example the TAB and
10423 ;; RET keys. Here is how it works: The minor mode defines all the keys
10424 ;; necessary to operate the table editor, but wraps the commands into a
10425 ;; function which tests if the cursor is currently inside a table. If that
10426 ;; is the case, the table editor command is executed. However, when any of
10427 ;; those keys is used outside a table, the function uses `key-binding' to
10428 ;; look up if the key has an associated command in another currently active
10429 ;; keymap (minor modes, major mode, global), and executes that command.
10430 ;; There might be problems if any of the keys used by the table editor is
10431 ;; otherwise used as a prefix key.
10433 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
10434 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
10435 ;; addresses this by checking explicitly for both bindings.
10437 ;; The optimized version (see variable `orgtbl-optimized') takes over
10438 ;; all keys which are bound to `self-insert-command' in the *global map*.
10439 ;; Some modes bind other commands to simple characters, for example
10440 ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
10441 ;; active, this binding is ignored inside tables and replaced with a
10442 ;; modified self-insert.
10444 (defvar orgtbl-mode nil
10445 "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
10446 table editor in arbitrary modes.")
10447 (make-variable-buffer-local 'orgtbl-mode
)
10449 (defvar orgtbl-mode-map
(make-keymap)
10450 "Keymap for `orgtbl-mode'.")
10453 (defun turn-on-orgtbl ()
10454 "Unconditionally turn on `orgtbl-mode'."
10458 (defun orgtbl-mode (&optional arg
)
10459 "The `org-mode' table editor as a minor mode for use in other modes."
10461 (if (eq major-mode
'org-mode
)
10462 ;; Exit without error, in case some hook functions calls this
10463 ;; by accident in org-mode.
10464 (message "Orgtbl-mode is not useful in org-mode, command ignored")
10466 (if arg
(> (prefix-numeric-value arg
) 0) (not orgtbl-mode
)))
10469 (and (orgtbl-setup) (defun orgtbl-setup () nil
))
10470 ;; Make sure we are first in minor-mode-map-alist
10471 (let ((c (assq 'orgtbl-mode minor-mode-map-alist
)))
10472 (and c
(setq minor-mode-map-alist
10473 (cons c
(delq c minor-mode-map-alist
)))))
10474 (set (make-local-variable (quote org-table-may-need-update
)) t
)
10475 (org-add-hook 'before-change-functions
'org-before-change-function
10477 (set (make-local-variable 'org-old-auto-fill-inhibit-regexp
)
10478 auto-fill-inhibit-regexp
)
10479 (set (make-local-variable 'auto-fill-inhibit-regexp
)
10480 (if auto-fill-inhibit-regexp
10481 (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp
)
10483 (org-add-to-invisibility-spec '(org-cwidth))
10484 (easy-menu-add orgtbl-mode-menu
)
10485 (run-hooks 'orgtbl-mode-hook
))
10486 (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp
)
10487 (org-cleanup-narrow-column-properties)
10488 (org-remove-from-invisibility-spec '(org-cwidth))
10489 (remove-hook 'before-change-functions
'org-before-change-function t
)
10490 (easy-menu-remove orgtbl-mode-menu
)
10491 (force-mode-line-update 'all
))))
10493 (defun org-cleanup-narrow-column-properties ()
10494 "Remove all properties related to narrow-column invisibility."
10496 (while (setq s
(text-property-any s
(point-max)
10497 'display org-narrow-column-arrow
))
10498 (remove-text-properties s
(1+ s
) '(display t
)))
10500 (while (setq s
(text-property-any s
(point-max) 'org-cwidth
1))
10501 (remove-text-properties s
(1+ s
) '(org-cwidth t
)))
10503 (while (setq s
(text-property-any s
(point-max) 'invisible
'org-cwidth
))
10504 (remove-text-properties s
(1+ s
) '(invisible t
)))))
10506 ;; Install it as a minor mode.
10507 (put 'orgtbl-mode
:included t
)
10508 (put 'orgtbl-mode
:menu-tag
"Org Table Mode")
10509 (add-minor-mode 'orgtbl-mode
" OrgTbl" orgtbl-mode-map
)
10511 (defun orgtbl-make-binding (fun n
&rest keys
)
10512 "Create a function for binding in the table minor mode.
10513 FUN is the command to call inside a table. N is used to create a unique
10514 command name. KEYS are keys that should be checked in for a command
10515 to execute outside of tables."
10518 (intern (concat "orgtbl-hijacker-command-" (int-to-string n
)))
10520 (concat "In tables, run `" (symbol-name fun
) "'.\n"
10521 "Outside of tables, run the binding of `"
10522 (mapconcat (lambda (x) (format "%s" x
)) keys
"' or `")
10527 (list 'call-interactively
(list 'quote fun
))
10528 (list 'let
'(orgtbl-mode)
10529 (list 'call-interactively
10531 (mapcar (lambda (k)
10532 (list 'key-binding k
))
10534 '('orgtbl-error
))))))))
10536 (defun orgtbl-error ()
10537 "Error when there is no default binding for a table key."
10539 (error "This key is has no function outside tables"))
10541 (defun orgtbl-setup ()
10542 "Setup orgtbl keymaps."
10546 '([(meta shift left
)] org-table-delete-column
)
10547 '([(meta left
)] org-table-move-column-left
)
10548 '([(meta right
)] org-table-move-column-right
)
10549 '([(meta shift right
)] org-table-insert-column
)
10550 '([(meta shift up
)] org-table-kill-row
)
10551 '([(meta shift down
)] org-table-insert-row
)
10552 '([(meta up
)] org-table-move-row-up
)
10553 '([(meta down
)] org-table-move-row-down
)
10554 '("\C-c\C-w" org-table-cut-region
)
10555 '("\C-c\M-w" org-table-copy-region
)
10556 '("\C-c\C-y" org-table-paste-rectangle
)
10557 '("\C-c-" org-table-insert-hline
)
10558 ; '([(shift tab)] org-table-previous-field)
10559 '("\C-m" org-table-next-row
)
10560 (list (org-key 'S-return
) 'org-table-copy-down
)
10561 '([(meta return
)] org-table-wrap-region
)
10562 '("\C-c\C-q" org-table-wrap-region
)
10563 '("\C-c?" org-table-current-column
)
10564 '("\C-c " org-table-blank-field
)
10565 '("\C-c+" org-table-sum
)
10566 '("\C-c=" org-table-eval-formula
)
10567 '("\C-c'" org-table-edit-formulas
)
10568 '("\C-c`" org-table-edit-field
)
10569 '("\C-c*" org-table-recalculate
)
10570 '("\C-c|" org-table-create-or-convert-from-region
)
10571 '("\C-c^" org-table-sort-lines
)
10572 '([(control ?
#)] org-table-rotate-recalc-marks
)))
10574 (while (setq elt
(pop bindings
))
10575 (setq nfunc
(1+ nfunc
))
10576 (setq key
(car elt
)
10578 cmd
(orgtbl-make-binding fun nfunc key
))
10579 (define-key orgtbl-mode-map key cmd
))
10580 ;; Special treatment needed for TAB and RET
10581 (define-key orgtbl-mode-map
[(return)]
10582 (orgtbl-make-binding 'orgtbl-ret
100 [(return)] "\C-m"))
10583 (define-key orgtbl-mode-map
"\C-m"
10584 (orgtbl-make-binding 'orgtbl-ret
101 "\C-m" [(return)]))
10585 (define-key orgtbl-mode-map
[(tab)]
10586 (orgtbl-make-binding 'orgtbl-tab
102 [(tab)] "\C-i"))
10587 (define-key orgtbl-mode-map
"\C-i"
10588 (orgtbl-make-binding 'orgtbl-tab
103 "\C-i" [(tab)])))
10589 (define-key orgtbl-mode-map
"\C-i"
10590 (orgtbl-make-binding 'orgtbl-tab
104 [(shift tab
)]))
10591 (define-key orgtbl-mode-map
"\C-c\C-c"
10592 (orgtbl-make-binding 'org-ctrl-c-ctrl-c
105 "\C-c\C-c"))
10593 (when orgtbl-optimized
10594 ;; If the user wants maximum table support, we need to hijack
10595 ;; some standard editing functions
10596 (org-remap orgtbl-mode-map
10597 'self-insert-command
'orgtbl-self-insert-command
10598 'delete-char
'org-delete-char
10599 'delete-backward-char
'org-delete-backward-char
)
10600 (define-key orgtbl-mode-map
"|" 'org-force-self-insert
))
10601 (easy-menu-define orgtbl-mode-menu orgtbl-mode-map
"OrgTbl menu"
10603 ["Align" org-ctrl-c-ctrl-c
:active
(org-at-table-p) :keys
"C-c C-c"]
10604 ["Next Field" org-cycle
:active
(org-at-table-p) :keys
"TAB"]
10605 ["Previous Field" org-shifttab
:active
(org-at-table-p) :keys
"S-TAB"]
10606 ["Next Row" org-return
:active
(org-at-table-p) :keys
"RET"]
10608 ["Blank Field" org-table-blank-field
:active
(org-at-table-p) :keys
"C-c SPC"]
10609 ["Edit Field" org-table-edit-field
:active
(org-at-table-p) :keys
"C-c ` "]
10610 ["Copy Field from Above"
10611 org-table-copy-down
:active
(org-at-table-p) :keys
"S-RET"]
10614 ["Move Column Left" org-metaleft
:active
(org-at-table-p) :keys
"M-<left>"]
10615 ["Move Column Right" org-metaright
:active
(org-at-table-p) :keys
"M-<right>"]
10616 ["Delete Column" org-shiftmetaleft
:active
(org-at-table-p) :keys
"M-S-<left>"]
10617 ["Insert Column" org-shiftmetaright
:active
(org-at-table-p) :keys
"M-S-<right>"]
10619 ["Enable Narrowing" (setq org-table-limit-column-width
(not org-table-limit-column-width
)) :active
(org-at-table-p) :selected org-table-limit-column-width
:style toggle
])
10621 ["Move Row Up" org-metaup
:active
(org-at-table-p) :keys
"M-<up>"]
10622 ["Move Row Down" org-metadown
:active
(org-at-table-p) :keys
"M-<down>"]
10623 ["Delete Row" org-shiftmetaup
:active
(org-at-table-p) :keys
"M-S-<up>"]
10624 ["Insert Row" org-shiftmetadown
:active
(org-at-table-p) :keys
"M-S-<down>"]
10625 ["Sort lines in region" org-table-sort-lines
(org-at-table-p) :keys
"C-c ^"]
10627 ["Insert Hline" org-table-insert-hline
:active
(org-at-table-p) :keys
"C-c -"])
10629 ["Copy Rectangle" org-copy-special
:active
(org-at-table-p)]
10630 ["Cut Rectangle" org-cut-special
:active
(org-at-table-p)]
10631 ["Paste Rectangle" org-paste-special
:active
(org-at-table-p)]
10632 ["Fill Rectangle" org-table-wrap-region
:active
(org-at-table-p)])
10634 ["Set Column Formula" org-table-eval-formula
:active
(org-at-table-p) :keys
"C-c ="]
10635 ["Set Named Field Formula" (org-table-eval-formula '(4)) :active
(org-at-table-p) :keys
"C-u C-c ="]
10636 ["Edit Formulas" org-table-edit-formulas
:active
(org-at-table-p) :keys
"C-c '"]
10637 ["Recalculate line" org-table-recalculate
:active
(org-at-table-p) :keys
"C-c *"]
10638 ["Recalculate all" (org-table-recalculate '(4)) :active
(org-at-table-p) :keys
"C-u C-c *"]
10639 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks
:active
(org-at-table-p) :keys
"C-c #"]
10640 ["Sum Column/Rectangle" org-table-sum
10641 :active
(or (org-at-table-p) (org-region-active-p)) :keys
"C-c +"]
10642 ["Which Column?" org-table-current-column
:active
(org-at-table-p) :keys
"C-c ?"]
10644 (setq org-table-formula-debug
(not org-table-formula-debug
))
10645 :style toggle
:selected org-table-formula-debug
]
10649 (defun orgtbl-tab (arg)
10650 "Justification and field motion for `orgtbl-mode'."
10652 (if arg
(org-table-edit-field t
)
10653 (org-table-justify-field-maybe)
10654 (org-table-next-field)))
10656 (defun orgtbl-ret ()
10657 "Justification and field motion for `orgtbl-mode'."
10659 (org-table-justify-field-maybe)
10660 (org-table-next-row))
10662 (defun orgtbl-self-insert-command (N)
10663 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
10664 If the cursor is in a table looking at whitespace, the whitespace is
10665 overwritten, and the table is not marked as requiring realignment."
10667 (if (and (org-at-table-p)
10669 (and org-table-auto-blank-field
10670 (member last-command
10671 '(orgtbl-hijacker-command-100
10672 orgtbl-hijacker-command-101
10673 orgtbl-hijacker-command-102
10674 orgtbl-hijacker-command-103
10675 orgtbl-hijacker-command-104
10676 orgtbl-hijacker-command-105
))
10677 (org-table-blank-field))
10680 (looking-at "[^|\n]* +|"))
10681 (let (org-table-may-need-update)
10682 (goto-char (1- (match-end 0)))
10683 (delete-backward-char 1)
10684 (goto-char (match-beginning 0))
10685 (self-insert-command N
))
10686 (setq org-table-may-need-update t
)
10688 (call-interactively (key-binding (vector last-input-event
))))))
10690 (defun org-force-self-insert (N)
10691 "Needed to enforce self-insert under remapping."
10693 (self-insert-command N
))
10697 (defconst org-level-max
20)
10699 (defun org-export-find-first-heading-line (list)
10700 "Remove all lines from LIST which are before the first headline."
10701 (let ((orig-list list
)
10702 (re (concat "^" outline-regexp
)))
10704 (not (string-match re
(car list
))))
10706 (or list orig-list
)))
10708 (defun org-skip-comments (lines)
10709 "Skip lines starting with \"#\" and subtrees starting with COMMENT."
10710 (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string
))
10711 (re2 "^\\(\\*+\\)[ \t\n\r]")
10713 (while (setq line
(pop lines
))
10715 ((and (string-match re1 line
)
10716 (setq level
(- (match-end 1) (match-beginning 1))))
10717 ;; Beginning of a COMMENT subtree. Skip it.
10718 (while (and (setq line
(pop lines
))
10719 (or (not (string-match re2 line
))
10720 (> (- (match-end 1) (match-beginning 1)) level
))))
10721 (setq lines
(cons line lines
)))
10722 ((string-match "^#" line
)
10723 ;; an ordinary comment line
10725 ((and org-export-table-remove-special-lines
10726 (string-match "^[ \t]*| *[!_^] *|" line
))
10727 ;; a special table line that should be removed
10729 (t (setq rtn
(cons line rtn
)))))
10734 (defconst org-ascii-underline
'(?\$ ?\
# ?^ ?\~ ?\
= ?\-
)
10735 "Characters for underlining headings in ASCII export.")
10737 (defconst org-html-entities
10777 ("Aring") ("AA".
"Å")
10866 ("varepsilon".
"ε")
10879 ("sigmaf") ("varsigma".
"ς")
10887 ("thetasym") ("vartheta".
"ϑ")
10890 ("bull") ("bullet".
"•")
10891 ("hellip") ("dots".
"…")
10901 ("larr") ("leftarrow".
"←") ("gets".
"←")
10902 ("uarr") ("uparrow".
"↑")
10903 ("rarr") ("to".
"→") ("rightarrow".
"→")
10904 ("darr")("downarrow".
"↓")
10905 ("harr") ("leftrightarrow".
"↔")
10906 ("crarr") ("hookleftarrow".
"↵") ; has round hook, not quite CR
10907 ("lArr") ("Leftarrow".
"⇐")
10908 ("uArr") ("Uparrow".
"⇑")
10909 ("rArr") ("Rightarrow".
"⇒")
10910 ("dArr") ("Downarrow".
"⇓")
10911 ("hArr") ("Leftrightarrow".
"⇔")
10913 ("part") ("partial".
"∂")
10914 ("exist") ("exists".
"∃")
10915 ("empty") ("emptyset".
"∅")
10917 ("isin") ("in".
"∈")
10923 ("lowast") ("ast".
"∗")
10925 ("prop") ("proptp".
"∝")
10926 ("infin") ("infty".
"∞")
10927 ("ang") ("angle".
"∠")
10928 ("and") ("vee".
"∧")
10929 ("or") ("wedge".
"∨")
10935 ("cong") ("simeq".
"≅")
10936 ("asymp")("approx".
"≈")
10937 ("ne") ("neq".
"≠")
10941 ("sub") ("subset".
"⊂")
10942 ("sup") ("supset".
"⊃")
10949 ("sdot") ("cdot".
"⋅")
10956 ("loz") ("Diamond".
"◊")
10957 ("spades") ("spadesuit".
"♠")
10958 ("clubs") ("clubsuit".
"♣")
10959 ("hearts") ("diamondsuit".
"♥")
10960 ("diams") ("diamondsuit".
"♦")
10994 ("arccos".
"arccos")
10995 ("arcsin".
"arcsin")
10996 ("arctan".
"arctan")
11013 ("liminf".
"liminf")
11014 ("limsup".
"limsup")
11027 "Entities for TeX->HTML translation.
11028 Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
11029 \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
11030 In that case, \"\\ent\" will be translated to \"&other;\".
11031 The list contains HTML entities for Latin-1, Greek and other symbols.
11032 It is supplemented by a number of commonly used TeX macros with appropriate
11033 translations. There is currently no way for users to extend this.")
11035 (defun org-cleaned-string-for-export (string)
11036 "Cleanup a buffer substring so that links can be created safely."
11038 (let* ((cb (current-buffer))
11039 (re-radio (and org-target-link-regexp
11040 (concat "\\([^<]\\)\\(" org-target-link-regexp
"\\)")))
11041 (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re
))
11042 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re
))
11045 (set-buffer (get-buffer-create " org-mode-tmp"))
11049 ;; Find targets in comments and move them out of comments,
11050 ;; but mark them as targets that should be invisible
11051 (goto-char (point-min))
11052 (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t
)
11053 (replace-match "\\1(INVISIBLE)"))
11054 ;; Find matches for radio targets and turn them into internal links
11055 (goto-char (point-min))
11057 (while (re-search-forward re-radio nil t
)
11058 (replace-match "\\1[[\\2]]")))
11059 ;; Find all links that contain a newline and put them into a single line
11060 (goto-char (point-min))
11061 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t
)
11062 (replace-match "\\1 \\3")
11063 (goto-char (match-beginning 0)))
11064 ;; Normalize links: Convert angle and plain links into bracket links
11065 (goto-char (point-min))
11066 (while (re-search-forward re-plain-link nil t
)
11069 (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
11071 (goto-char (point-min))
11072 (while (re-search-forward re-angle-link nil t
)
11075 (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
11079 (goto-char (point-min))
11080 (while (re-search-forward "^#.*\n?" nil t
)
11081 (replace-match ""))
11082 (setq rtn
(buffer-string)))
11083 (kill-buffer " org-mode-tmp")
11086 (defun org-solidify-link-text (s &optional alist
)
11087 "Take link text and make a safe target out of it."
11092 (org-split-string s
"[ \t\r\n]+") "--"))
11093 (a (assoc rtn alist
)))
11094 (or (cdr a
) rtn
))))
11096 (defun org-convert-to-odd-levels ()
11097 "Convert an org-mode file with all levels allowed to one with odd levels.
11098 This will leave level 1 alone, convert level 2 to level 3, level 3 to
11101 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
11102 (let ((org-odd-levels-only nil
) n
)
11104 (goto-char (point-min))
11105 (while (re-search-forward "^\\*\\*+" nil t
)
11106 (setq n
(1- (length (match-string 0))))
11107 (while (>= (setq n
(1- n
)) 0)
11109 (end-of-line 1))))))
11112 (defun org-convert-to-oddeven-levels ()
11113 "Convert an org-mode file with only odd levels to one with odd and even levels.
11114 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
11115 section with an even level, conversion would destroy the structure of the file. An error
11116 is signaled in this case."
11118 (goto-char (point-min))
11119 ;; First check if there are no even levels
11120 (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t
)
11121 (org-show-hierarchy-above)
11122 (error "Not all levels are odd in this file. Conversion not possible."))
11123 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
11124 (let ((org-odd-levels-only nil
) n
)
11126 (goto-char (point-min))
11127 (while (re-search-forward "^\\*\\*+" nil t
)
11128 (setq n
(/ (length (match-string 0)) 2))
11129 (while (>= (setq n
(1- n
)) 0)
11131 (end-of-line 1))))))
11133 (defun org-tr-level (n)
11134 "Make N odd if required."
11135 (if org-odd-levels-only
(1+ (/ n
2)) n
))
11137 (defvar org-last-level nil
) ; dynamically scoped variable
11139 (defun org-export-as-ascii (arg)
11140 "Export the outline as a pretty ASCII file.
11141 If there is an active region, export only the region.
11142 The prefix ARG specifies how many levels of the outline should become
11143 underlined headlines. The default is 3."
11145 (setq-default org-todo-line-regexp org-todo-line-regexp
)
11148 (if (org-region-active-p) (region-beginning) (point-min))
11149 (if (org-region-active-p) (region-end) (point-max))))
11150 (lines (org-export-find-first-heading-line
11153 (org-cleaned-string-for-export region
)
11155 (org-startup-with-deadline-check nil
)
11158 (case-fold-search nil
)
11159 (filename (concat (file-name-sans-extension buffer-file-name
)
11161 (buffer (find-file-noselect filename
))
11162 (levels-open (make-vector org-level-max nil
))
11163 (date (format-time-string "%Y/%m/%d" (current-time)))
11164 (time (format-time-string "%X" (org-current-time)))
11165 (author user-full-name
)
11166 (title (buffer-name))
11168 (email user-mail-address
)
11169 (language org-export-default-language
)
11174 (setq org-last-level
1)
11175 (org-init-section-numbers)
11177 (find-file-noselect filename
)
11179 ;; Search for the export key lines
11180 (org-parse-key-lines)
11182 (setq lang-words
(or (assoc language org-export-language-setup
)
11183 (assoc "en" org-export-language-setup
)))
11184 (if org-export-ascii-show-new-buffer
11185 (switch-to-buffer-other-window buffer
)
11186 (set-buffer buffer
))
11189 (if options
(org-parse-export-options options
))
11190 (setq umax
(if arg
(prefix-numeric-value arg
)
11191 org-export-headline-levels
))
11194 (if title
(org-insert-centered title ?
=))
11196 (if (or author email
)
11197 (insert (concat (nth 1 lang-words
) ": " (or author
"")
11198 (if email
(concat " <" email
">") "")
11200 (if (and date time
)
11201 (insert (concat (nth 2 lang-words
) ": " date
" " time
"\n")))
11202 (if text
(insert (concat (org-html-expand-for-ascii text
) "\n\n")))
11206 (if org-export-with-toc
11208 (insert (nth 3 lang-words
) "\n"
11209 (make-string (length (nth 3 lang-words
)) ?
=) "\n")
11210 (mapcar '(lambda (line)
11211 (if (string-match org-todo-line-regexp
11213 ;; This is a headline
11215 (setq level
(- (match-end 1) (match-beginning 1))
11216 level
(org-tr-level level
)
11217 txt
(match-string 3 line
)
11219 (or (and (match-beginning 2)
11220 (not (equal (match-string 2 line
)
11223 (and org-export-mark-todo-in-toc
11225 (org-search-todo-below
11226 line lines level
))))
11227 (setq txt
(org-html-expand-for-ascii txt
))
11229 (if org-export-with-section-numbers
11230 (setq txt
(concat (org-section-number level
)
11232 (if (<= level umax
)
11235 (make-string (* (1- level
) 4) ?\
)
11236 (format (if todo
"%s (*)\n" "%s\n") txt
))
11237 (setq org-last-level level
))
11241 (org-init-section-numbers)
11242 (while (setq line
(pop lines
))
11243 ;; Remove the quoted HTML tags.
11244 (setq line
(org-html-expand-for-ascii line
))
11246 (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line
)
11247 (setq line
(replace-match "" t t line
)))
11248 ;; Replace internal links
11249 (while (string-match org-bracket-link-regexp line
)
11250 (setq line
(replace-match
11251 (if (match-end 3) "[\\3]" "[\\1]")
11254 ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line
)
11256 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)))
11257 txt
(match-string 2 line
))
11258 (org-ascii-level-start level txt umax
))
11259 (t (insert line
"\n"))))
11262 (goto-char (point-min))))
11264 (defun org-search-todo-below (line lines level
)
11265 "Search the subtree below LINE for any TODO entries."
11266 (let ((rest (cdr (memq line lines
)))
11267 (re org-todo-line-regexp
)
11270 (while (setq line
(pop rest
))
11271 (if (string-match re line
)
11273 (setq lv
(- (match-end 1) (match-beginning 1))
11274 todo
(and (match-beginning 2)
11275 (not (equal (match-string 2 line
)
11276 org-done-string
))))
11278 (if (<= lv level
) (throw 'exit nil
))
11279 (if todo
(throw 'exit t
))))))))
11281 ;; FIXME: Try to handle <b> and <i> as faces via text properties.
11282 ;; We could also implement *bold*,/italic/ and _underline_ for ASCII export
11283 (defun org-html-expand-for-ascii (line)
11284 "Handle quoted HTML for ASCII export."
11285 (if org-export-html-expand
11286 (while (string-match "@<[^<>\n]*>" line
)
11287 ;; We just remove the tags for now.
11288 (setq line
(replace-match "" nil nil line
))))
11291 (defun org-insert-centered (s &optional underline
)
11292 "Insert the string S centered and underline it with character UNDERLINE."
11293 (let ((ind (max (/ (- 80 (string-width s
)) 2) 0)))
11294 (insert (make-string ind ?\
) s
"\n")
11296 (insert (make-string ind ?\
)
11297 (make-string (string-width s
) underline
)
11300 (defun org-ascii-level-start (level title umax
)
11301 "Insert a new level in ASCII export."
11304 (insert (make-string (* 2 (- level umax
1)) ?\
) "* " title
"\n")
11305 (if (or (not (equal (char-before) ?
\n))
11306 (not (equal (char-before (1- (point))) ?
\n)))
11308 (setq char
(nth (- umax level
) (reverse org-ascii-underline
)))
11309 (if org-export-with-section-numbers
11310 (setq title
(concat (org-section-number level
) " " title
)))
11311 (insert title
"\n" (make-string (string-width title
) char
) "\n"))))
11313 (defun org-export-copy-visible ()
11314 "Copy the visible part of the buffer to another buffer, for printing.
11315 Also removes the first line of the buffer if it specifies a mode,
11316 and all options lines."
11318 (let* ((filename (concat (file-name-sans-extension buffer-file-name
)
11320 (buffer (find-file-noselect filename
))
11322 (org-make-options-regexp
11323 '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
11324 "STARTUP" "ARCHIVE"
11325 "TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"))
11326 (if org-noutline-p
"\\(\n\\|$\\)" "")))
11328 (with-current-buffer buffer
11332 (setq s
(goto-char (point-min)))
11333 (while (not (= (point) (point-max)))
11334 (goto-char (org-find-invisible))
11335 (append-to-buffer buffer s
(point))
11336 (setq s
(goto-char (org-find-visible)))))
11337 (switch-to-buffer-other-window buffer
)
11339 (goto-char (point-min))
11340 (if (looking-at ".*-\\*- mode:.*\n")
11341 (replace-match ""))
11342 (while (re-search-forward ore nil t
)
11343 (replace-match ""))
11344 (goto-char (point-min))))
11346 (defun org-find-visible ()
11347 (if (featurep 'noutline
)
11349 (while (and (not (= (point-max) (setq s
(next-overlay-change s
))))
11350 (get-char-property s
'invisible
)))
11352 (skip-chars-forward "^\n")
11354 (defun org-find-invisible ()
11355 (if (featurep 'noutline
)
11357 (while (and (not (= (point-max) (setq s
(next-overlay-change s
))))
11358 (not (get-char-property s
'invisible
))))
11360 (skip-chars-forward "^\r")
11365 (defun org-get-current-options ()
11366 "Return a string with current options as keyword options.
11367 Does include HTML export options as well as TODO and CATEGORY stuff."
11373 #+TEXT: Some descriptive text to be emitted. Several lines OK.
11374 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s
11378 #+STARTUP: %s %s %s %s %s
11381 (buffer-name) (user-full-name) user-mail-address org-export-default-language
11382 org-export-headline-levels
11383 org-export-with-section-numbers
11384 org-export-with-toc
11385 org-export-preserve-breaks
11386 org-export-html-expand
11387 org-export-with-fixed-width
11388 org-export-with-tables
11389 org-export-with-sub-superscripts
11390 org-export-with-emphasize
11391 org-export-with-TeX-macros
11392 (file-name-nondirectory buffer-file-name
)
11393 (if (equal org-todo-interpretation
'sequence
)
11394 (mapconcat 'identity org-todo-keywords
" ")
11395 "TODO FEEDBACK VERIFY DONE")
11396 (if (equal org-todo-interpretation
'type
)
11397 (mapconcat 'identity org-todo-keywords
" ")
11398 "Me Jason Marie DONE")
11399 (cdr (assoc org-startup-folded
11400 '((nil .
"nofold")(t .
"fold")(content .
"content"))))
11401 (if org-startup-with-deadline-check
"dlcheck" "nodlcheck")
11402 (if org-odd-levels-only
"odd" "oddeven")
11403 (if org-hide-leading-stars
"hidestars" "showstars")
11404 (if org-startup-align-all-tables
"align" "noalign")
11405 org-archive-location
11408 (defun org-insert-export-options-template ()
11409 "Insert into the buffer a template with information for exporting."
11411 (if (not (bolp)) (newline))
11412 (let ((s (org-get-current-options)))
11413 (and (string-match "#\\+CATEGORY" s
)
11414 (setq s
(substring s
0 (match-beginning 0))))
11417 (defun org-toggle-fixed-width-section (arg)
11418 "Toggle the fixed-width export.
11419 If there is no active region, the QUOTE keyword at the current headline is
11420 inserted or removed. When present, it causes the text between this headline
11421 and the next to be exported as fixed-width text, and unmodified.
11422 If there is an active region, this command adds or removes a colon as the
11423 first character of this line. If the first character of a line is a colon,
11424 this line is also exported in fixed-width font."
11427 (regionp (org-region-active-p))
11428 (beg (if regionp
(region-beginning) (point)))
11429 (end (if regionp
(region-end)))
11430 (nlines (or arg
(if (and beg end
) (count-lines beg end
) 1)))
11431 (re "[ \t]*\\(:\\)")
11436 (setq cc
(current-column))
11437 (beginning-of-line 1)
11438 (setq off
(looking-at re
))
11439 (while (> nlines
0)
11440 (setq nlines
(1- nlines
))
11441 (beginning-of-line 1)
11444 (move-to-column cc t
)
11447 ((and off
(looking-at re
))
11448 (replace-match "" t t nil
1))
11449 ((not off
) (move-to-column cc t
) (insert ":")))
11452 (org-back-to-heading)
11453 (if (looking-at (concat outline-regexp
11454 "\\( +\\<" org-quote-string
"\\>\\)"))
11455 (replace-match "" t t nil
1)
11456 (if (looking-at outline-regexp
)
11458 (goto-char (match-end 0))
11459 (insert " " org-quote-string
))))))))
11461 (defun org-export-as-html-and-open (arg)
11462 "Export the outline as HTML and immediately open it with a browser.
11463 If there is an active region, export only the region.
11464 The prefix ARG specifies how many levels of the outline should become
11465 headlines. The default is 3. Lower levels will become bulleted lists."
11467 (org-export-as-html arg
'hidden
)
11468 (org-open-file buffer-file-name
))
11470 (defun org-export-as-html-batch ()
11471 "Call `org-export-as-html', may be used in batch processing as
11473 --load=$HOME/lib/emacs/org.el
11474 --eval \"(setq org-export-headline-levels 2)\"
11475 --visit=MyFile --funcall org-export-as-html-batch"
11476 (org-export-as-html org-export-headline-levels
'hidden
))
11478 (defun org-export-as-html (arg &optional hidden
)
11479 "Export the outline as a pretty HTML file.
11480 If there is an active region, export only the region.
11481 The prefix ARG specifies how many levels of the outline should become
11482 headlines. The default is 3. Lower levels will become bulleted lists."
11484 (setq-default org-todo-line-regexp org-todo-line-regexp
)
11485 (setq-default org-deadline-line-regexp org-deadline-line-regexp
)
11486 (setq-default org-done-string org-done-string
)
11487 (let* ((style org-export-html-style
)
11488 (odd org-odd-levels-only
)
11489 (region-p (org-region-active-p))
11492 (if region-p
(region-beginning) (point-min))
11493 (if region-p
(region-end) (point-max))))
11495 (org-skip-comments (org-split-string
11496 (org-cleaned-string-for-export region
)
11498 (lines (org-export-find-first-heading-line all_lines
))
11499 (level 0) (line "") (origline "") txt todo
11501 (filename (concat (file-name-sans-extension buffer-file-name
)
11503 (buffer (find-file-noselect filename
))
11504 (levels-open (make-vector org-level-max nil
))
11505 (date (format-time-string "%Y/%m/%d" (current-time)))
11506 (time (format-time-string "%X" (org-current-time)))
11507 (author user-full-name
)
11508 (title (buffer-name))
11510 (quote-re (concat "^\\*+[ \t]*" org-quote-string
"\\>"))
11513 (in-local-list nil
)
11514 (local-list-num nil
)
11515 (local-list-indent nil
)
11516 (llt org-plain-list-ordered-item-terminator
)
11517 (email user-mail-address
)
11518 (language org-export-default-language
)
11521 (target-alist nil
) tg
11524 ;; FIXME: The following returns always nil under XEmacs
11525 (coding-system (and (fboundp 'coding-system-get
)
11526 (boundp 'buffer-file-coding-system
)
11527 buffer-file-coding-system
))
11528 (coding-system-for-write (or coding-system coding-system-for-write
))
11529 (save-buffer-coding-system (or coding-system save-buffer-coding-system
))
11530 (charset (and coding-system
11531 (coding-system-get coding-system
'mime-charset
)))
11533 table-buffer table-orig-buffer
11534 ind start-is-num starter
11535 rpl path desc desc1 desc2 link
11537 (message "Exporting...")
11539 (setq org-last-level
1)
11540 (org-init-section-numbers)
11542 ;; Search for the export key lines
11543 (org-parse-key-lines)
11544 (setq lang-words
(or (assoc language org-export-language-setup
)
11545 (assoc "en" org-export-language-setup
)))
11547 ;; Switch to the output buffer
11548 (if (or hidden
(not org-export-html-show-new-buffer
))
11549 (set-buffer buffer
)
11550 (switch-to-buffer-other-window buffer
))
11553 (let ((case-fold-search nil
)
11554 (org-odd-levels-only odd
))
11555 (if options
(org-parse-export-options options
))
11556 (setq umax
(if arg
(prefix-numeric-value arg
)
11557 org-export-headline-levels
))
11561 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"
11562 \"http://www.w3.org/TR/REC-html40/loose.dtd\">
11563 <html lang=\"%s\"><head>
11565 <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\">
11566 <meta name=generator content=\"Org-mode\">
11567 <meta name=generated content=\"%s %s\">
11568 <meta name=author content=\"%s\">
11572 language
(org-html-expand title
) (or charset
"iso-8859-1")
11573 date time author style
))
11574 (if title
(insert (concat "<H1 class=\"title\">"
11575 (org-html-expand title
) "</H1>\n")))
11576 (if author
(insert (concat (nth 1 lang-words
) ": " author
"\n")))
11577 (if email
(insert (concat "<a href=\"mailto:" email
"\"><"
11578 email
"></a>\n")))
11579 (if (or author email
) (insert "<br>\n"))
11580 (if (and date time
) (insert (concat (nth 2 lang-words
) ": "
11581 date
" " time
"<br>\n")))
11582 (if text
(insert (concat "<p>\n" (org-html-expand text
))))
11583 (if org-export-with-toc
11585 (insert (format "<H2>%s</H2>\n" (nth 3 lang-words
)))
11588 (mapcar '(lambda (line)
11589 (if (string-match org-todo-line-regexp line
)
11590 ;; This is a headline
11592 (setq level
(- (match-end 1) (match-beginning 1))
11593 level
(org-tr-level level
)
11594 txt
(save-match-data
11596 (match-string 3 line
)))
11598 (or (and (match-beginning 2)
11599 (not (equal (match-string 2 line
)
11602 (and org-export-mark-todo-in-toc
11604 (org-search-todo-below
11605 line lines level
))))
11606 (if org-export-with-section-numbers
11607 (setq txt
(concat (org-section-number level
)
11609 (if (<= level umax
)
11611 (setq head-count
(+ head-count
1))
11612 (if (> level org-last-level
)
11614 (setq cnt
(- level org-last-level
))
11615 (while (>= (setq cnt
(1- cnt
)) 0)
11618 (if (< level org-last-level
)
11620 (setq cnt
(- org-last-level level
))
11621 (while (>= (setq cnt
(1- cnt
)) 0)
11624 ;; Check for targets
11625 (while (string-match org-target-regexp line
)
11626 (setq tg
(match-string 1 line
)
11627 line
(replace-match
11628 (concat "@<span class=\"target\">" tg
"@</span> ")
11630 (push (cons (org-solidify-link-text tg
)
11631 (format "sec-%d" head-count
))
11633 (while (string-match "<\\(<\\)+\\|>\\(>\\)+" txt
)
11634 (setq txt
(replace-match "" t t txt
)))
11638 "<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>\n"
11639 "<li><a href=\"#sec-%d\">%s</a>\n")
11642 (setq org-last-level level
))
11646 (while (> org-last-level
0)
11647 (setq org-last-level
(1- org-last-level
))
11648 (insert "</ul>\n"))
11650 (setq head-count
0)
11651 (org-init-section-numbers)
11653 (while (setq line
(pop lines
) origline line
)
11656 ;; end of quote section?
11657 (when (and inquote
(string-match "^\\*+" line
))
11658 (insert "</pre>\n")
11659 (setq inquote nil
))
11660 ;; inside a quote section?
11662 (insert (org-html-protect line
) "\n")
11663 (throw 'nextline nil
))
11666 (when (and org-export-with-fixed-width
11667 (string-match "^[ \t]*:\\(.*\\)" line
))
11668 (when (not infixed
)
11670 (insert "<pre>\n"))
11671 (insert (org-html-protect (match-string 1 line
)) "\n")
11673 (not (string-match "^[ \t]*\\(:.*\\)"
11676 (insert "</pre>\n"))
11677 (throw 'nextline nil
))
11680 ;; make targets to anchors
11681 (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line
)
11684 (setq line
(replace-match
11685 (concat "@<a name=\""
11686 (org-solidify-link-text (match-string 1 line
))
11689 ((and org-export-with-toc
(equal (string-to-char line
) ?
*))
11690 (setq line
(replace-match
11691 (concat "@<span class=\"target\">" (match-string 1 line
) "@</span> ")
11692 ; (concat "@<i>" (match-string 1 line) "@</i> ")
11695 (setq line
(replace-match
11696 (concat "@<a name=\""
11697 (org-solidify-link-text (match-string 1 line
))
11698 "\" class=\"target\">" (match-string 1 line
) "@</a> ")
11701 ;; replace "&" by "&", "<" and ">" by "<" and ">"
11702 ;; handle @<..> HTML tags (replace "@>..<" by "<..>")
11703 (setq line
(org-html-expand line
))
11705 ;; Format the links
11707 (while (string-match org-bracket-link-analytic-regexp line start
)
11708 (setq start
(match-beginning 0))
11709 (setq type
(if (match-end 2) (match-string 2 line
) "internal"))
11710 (setq path
(match-string 3 line
))
11711 (setq desc1
(if (match-end 5) (match-string 5 line
))
11712 desc2
(if (match-end 2) (concat type
":" path
) path
)
11713 desc
(or desc1 desc2
))
11715 ((equal type
"internal")
11719 (org-solidify-link-text path target-alist
)
11720 "\">" desc
"</a>")))
11721 ((member type
'("http" "https" "ftp" "mailto" "news"))
11723 (setq link
(concat type
":" path
))
11724 (setq rpl
(concat "<a href=\"" link
"\">" desc
"</a>")))
11725 ((string= type
"file")
11727 (let* ((filename path
)
11728 (abs-p (file-name-absolute-p filename
))
11729 (thefile (if abs-p
(expand-file-name filename
) filename
))
11730 (thefile (save-match-data
11731 (if (string-match ":[0-9]+$" thefile
)
11732 (replace-match "" t t thefile
)
11736 (string-match (org-image-file-name-regexp) thefile
))))
11737 (setq rpl
(if (and org-export-html-inline-images
11739 (concat "<img src=\"" thefile
"\"/>")
11740 (concat "<a href=\"" thefile
"\">" desc
"</a>")))))
11741 ((member type
'("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell"))
11742 (setq rpl
(concat "<i><" type
":" path
"></i>"))))
11743 ;; FIXME: We get to see the escaped links!!!!!
11744 (setq line
(replace-match rpl t t line
)
11745 start
(+ start
(length rpl
))))
11747 (if (and (string-match org-todo-line-regexp line
)
11748 (match-beginning 2))
11749 (if (equal (match-string 2 line
) org-done-string
)
11750 (setq line
(replace-match
11751 "<span class=\"done\">\\2</span>"
11753 (setq line
(replace-match "<span class=\"todo\">\\2</span>"
11757 (if (string-match org-deadline-line-regexp line
)
11759 (if (save-match-data
11760 (string-match "<a href"
11761 (substring line
0 (match-beginning 0))))
11762 nil
; Don't do the replacement - it is inside a link
11763 (setq line
(replace-match "<span class=\"deadline\">\\&</span>"
11764 nil nil line
1)))))
11766 ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line
)
11767 ;; This is a headline
11768 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)))
11769 txt
(match-string 2 line
))
11770 (if (<= level umax
) (setq head-count
(+ head-count
1)))
11771 (when in-local-list
11772 ;; Close any local lists before inserting a new header line
11773 (while local-list-num
11774 (insert (if (car local-list-num
) "</ol>\n" "</ul>"))
11775 (pop local-list-num
))
11776 (setq local-list-indent nil
11777 in-local-list nil
))
11778 (org-html-level-start level txt umax
11779 (and org-export-with-toc
(<= level umax
))
11782 (when (string-match quote-re line
)
11786 ((and org-export-with-tables
11787 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
11788 (if (not table-open
)
11789 ;; New table starts
11790 (setq table-open t table-buffer nil table-orig-buffer nil
))
11791 ;; Accumulate lines
11792 (setq table-buffer
(cons line table-buffer
)
11793 table-orig-buffer
(cons origline table-orig-buffer
))
11794 (when (or (not lines
)
11795 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
11797 (setq table-open nil
11798 table-buffer
(nreverse table-buffer
)
11799 table-orig-buffer
(nreverse table-orig-buffer
))
11800 (insert (org-format-table-html table-buffer table-orig-buffer
))))
11803 (when (and (string-match
11805 ((eq llt t
) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+[.)]\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
11806 ((= llt ?.
) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+\\.\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
11807 ((= llt ?\
)) "^\\( \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+)\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
11808 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
11810 (setq ind
(org-get-string-indentation line
)
11811 start-is-num
(match-beginning 4)
11812 starter
(if (match-beginning 2) (match-string 2 line
))
11813 line
(substring line
(match-beginning 5)))
11814 (unless (string-match "[^ \t]" line
)
11815 ;; empty line. Pretend indentation is large.
11816 (setq ind
(1+ (or (car local-list-indent
) 1))))
11817 (while (and in-local-list
11818 (or (and (= ind
(car local-list-indent
))
11820 (< ind
(car local-list-indent
))))
11821 (insert (if (car local-list-num
) "</ol>\n" "</ul>"))
11822 (pop local-list-num
) (pop local-list-indent
)
11823 (setq in-local-list local-list-indent
))
11826 (or (not in-local-list
)
11827 (> ind
(car local-list-indent
))))
11828 ;; Start new (level of ) list
11829 (insert (if start-is-num
"<ol>\n<li>\n" "<ul>\n<li>\n"))
11830 (push start-is-num local-list-num
)
11831 (push ind local-list-indent
)
11832 (setq in-local-list t
))
11834 ;; continue current list
11835 (insert "<li>\n"))))
11836 ;; Empty lines start a new paragraph. If hand-formatted lists
11837 ;; are not fully interpreted, lines starting with "-", "+", "*"
11838 ;; also start a new paragraph.
11839 (if (string-match "^ [-+*]-\\|^[ \t]*$" line
) (insert "<p>"))
11840 (insert line
(if org-export-preserve-breaks
"<br>\n" "\n"))))
11842 (if org-export-html-with-timestamp
11843 (insert org-export-html-html-helper-timestamp
))
11844 (insert "</body>\n</html>\n")
11847 (goto-char (point-min)))))
11849 (defun org-format-table-html (lines olines
)
11850 "Find out which HTML converter to use and return the HTML code."
11851 (if (string-match "^[ \t]*|" (car lines
))
11852 ;; A normal org table
11853 (org-format-org-table-html lines
)
11854 ;; Table made by table.el - test for spanning
11855 (let* ((hlines (delq nil
(mapcar
11857 (if (string-match "^[ \t]*\\+-" x
) x
11860 (first (car hlines
))
11861 (ll (and (string-match "\\S-+" first
)
11862 (match-string 0 first
)))
11863 (re (concat "^[ \t]*" (regexp-quote ll
)))
11864 (spanning (delq nil
(mapcar (lambda (x) (not (string-match re x
)))
11866 (if (and (not spanning
)
11867 (not org-export-prefer-native-exporter-for-tables
))
11868 ;; We can use my own converter with HTML conversions
11869 (org-format-table-table-html lines
)
11870 ;; Need to use the code generator in table.el, with the original text.
11871 (org-format-table-table-html-using-table-generate-source olines
)))))
11873 (defun org-format-org-table-html (lines)
11874 "Format a table into HTML."
11875 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
11876 (setq lines
(nreverse lines
))
11877 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
11878 (setq lines
(nreverse lines
))
11879 (when org-export-table-remove-special-lines
11880 ;; Check if the table has a marking column. If yes remove the
11881 ;; column and the special lines
11887 (or (string-match "^[ \t]*|-" x
)
11888 (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x
)))
11895 (if (string-match "^[ \t]*| *[!_^] *|" x
)
11896 nil
; ignore this line
11897 (and (or (string-match "^[ \t]*|-+\\+" x
)
11898 (string-match "^[ \t]*|[^|]*|" x
))
11899 (replace-match "|" t t x
))))
11902 (let ((head (and org-export-highlight-first-table-line
11904 (lambda (x) (string-match "^[ \t]*|-" x
))
11907 (setq html
(concat org-export-html-table-tag
"\n"))
11908 (while (setq line
(pop lines
))
11910 (if (string-match "^[ \t]*|-" line
)
11912 (setq head nil
) ;; head ends here, first time around
11913 ;; ignore this line
11914 (throw 'next-line t
)))
11915 ;; Break the line into fields
11916 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
11920 (mapconcat (lambda (x)
11922 (concat "<th>" x
"</th>")
11923 (concat "<td>" x
"</td>")))
11926 (setq html
(concat html
"</table>\n"))
11929 (defun org-fake-empty-table-line (line)
11930 "Replace everything except \"|\" with spaces."
11931 (let ((i (length line
))
11932 (newstr (copy-sequence line
)))
11935 (if (not (eq (aref newstr i
) ?|
))
11936 (aset newstr i ?\
)))
11939 (defun org-format-table-table-html (lines)
11940 "Format a table generated by table.el into HTML.
11941 This conversion does *not* use `table-generate-source' from table.el.
11942 This has the advantage that Org-mode's HTML conversions can be used.
11943 But it has the disadvantage, that no cell- or row-spanning is allowed."
11944 (let (line field-buffer
11945 (head org-export-highlight-first-table-line
)
11947 (setq html
(concat org-export-html-table-tag
"\n"))
11948 (while (setq line
(pop lines
))
11949 (setq empty
" ")
11951 (if (string-match "^[ \t]*\\+-" line
)
11960 (if (equal x
"") (setq x empty
))
11962 (concat "<th>" x
"</th>\n")
11963 (concat "<td>" x
"</td>\n")))
11967 (setq field-buffer nil
)))
11968 ;; Ignore this line
11969 (throw 'next-line t
)))
11970 ;; Break the line into fields and store the fields
11971 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
11973 (setq field-buffer
(mapcar
11975 (concat x
"<br>" (pop fields
)))
11977 (setq field-buffer fields
))))
11978 (setq html
(concat html
"</table>\n"))
11981 (defun org-format-table-table-html-using-table-generate-source (lines)
11982 "Format a table into html, using `table-generate-source' from table.el.
11983 This has the advantage that cell- or row-spanning is allowed.
11984 But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
11986 (with-current-buffer (get-buffer-create " org-tmp1 ")
11988 (insert (mapconcat 'identity lines
"\n"))
11989 (goto-char (point-min))
11990 (if (not (re-search-forward "|[^+]" nil t
))
11991 (error "Error processing table"))
11992 (table-recognize-table)
11993 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
11994 (table-generate-source 'html
" org-tmp2 ")
11995 (set-buffer " org-tmp2 ")
11996 (buffer-substring (point-min) (point-max))))
11998 (defun org-html-protect (s)
11999 ;; convert & to &, < to < and > to >
12001 (while (string-match "&" s start
)
12002 (setq s
(replace-match "&" t t s
)
12003 start
(1+ (match-beginning 0))))
12004 (while (string-match "<" s
)
12005 (setq s
(replace-match "<" t t s
)))
12006 (while (string-match ">" s
)
12007 (setq s
(replace-match ">" t t s
))))
12010 (defun org-html-expand (string)
12011 "Prepare STRING for HTML export. Applies all active conversions.
12012 If there are links in the string, don't modify these."
12014 (while (setq m
(string-match org-bracket-link-regexp string
))
12015 (setq s
(substring string
0 m
)
12016 l
(match-string 0 string
)
12017 string
(substring string
(match-end 0)))
12018 (push (org-html-do-expand s
) res
)
12020 (push (org-html-do-expand string
) res
)
12021 (apply 'concat
(nreverse res
))))
12023 (defun org-html-do-expand (s)
12024 "Apply all active conversions to translate special ASCII to HTML."
12025 (setq s
(org-html-protect s
))
12026 (if org-export-html-expand
12027 (while (string-match "@<\\([^&]*\\)>" s
)
12028 (setq s
(replace-match "<\\1>" nil nil s
))))
12029 (if org-export-with-emphasize
12030 (setq s
(org-export-html-convert-emphasize s
)))
12031 (if org-export-with-sub-superscripts
12032 (setq s
(org-export-html-convert-sub-super s
)))
12033 (if org-export-with-TeX-macros
12034 (let ((start 0) wd ass
)
12035 (while (setq start
(string-match "\\\\\\([a-zA-Z]+\\)" s start
))
12036 (setq wd
(match-string 1 s
))
12037 (if (setq ass
(assoc wd org-html-entities
))
12038 (setq s
(replace-match (or (cdr ass
)
12039 (concat "&" (car ass
) ";"))
12041 (setq start
(+ start
(length wd
)))))))
12044 (defun org-create-multibrace-regexp (left right n
)
12045 "Create a regular expression which will match a balanced sexp.
12046 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
12047 as single character strings.
12048 The regexp returned will match the entire expression including the
12049 delimiters. It will also define a single group which contains the
12050 match except for the outermost delimiters. The maximum depth of
12051 stacked delimiters is N. Escaping delimiters is not possible."
12052 (let* ((nothing (concat "[^" "\\" left
"\\" right
"]*?"))
12055 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
12058 re
(concat re or next
)
12059 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
12060 (concat left
"\\(" re
"\\)" right
)))
12062 (defvar org-match-substring-regexp
12064 "\\([^\\]\\)\\([_^]\\)\\("
12065 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
12067 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
12069 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
12070 "The regular expression matching a sub- or superscript.")
12072 (defun org-export-html-convert-sub-super (string)
12073 "Convert sub- and superscripts in STRING to HTML."
12075 (while (string-match org-match-substring-regexp string
)
12076 (setq key
(if (string= (match-string 2 string
) "_") "sub" "sup"))
12077 (setq c
(or (match-string 8 string
)
12078 (match-string 6 string
)
12079 (match-string 5 string
)))
12080 (setq string
(replace-match
12081 (concat (match-string 1 string
)
12082 "<" key
">" c
"</" key
">")
12084 (while (string-match "\\\\\\([_^]\\)" string
)
12085 (setq string
(replace-match (match-string 1 string
) t t string
))))
12088 (defun org-export-html-convert-emphasize (string)
12090 (while (string-match "\\(\\W\\|^\\)\\([*/_]\\)\\(\\w+\\)\\2\\(\\W\\|$\\)" string s
)
12091 (setq c
(cdr (assoc (match-string 2 string
)
12092 '(("*" .
"b") ("/" .
"i") ("_" .
"u"))))
12093 s
(+ (match-end 0) 3)
12094 string
(replace-match
12095 (concat "\\1<" c
">\\3</" c
">\\4") t nil string
)))
12098 (defun org-parse-key-lines ()
12099 "Find the special key lines with the information for exporters."
12102 (let ((re (org-make-options-regexp
12103 '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
12105 (while (re-search-forward re nil t
)
12106 (setq key
(match-string 1))
12107 (cond ((string-equal key
"TITLE")
12108 (setq title
(match-string 2)))
12109 ((string-equal key
"AUTHOR")
12110 (setq author
(match-string 2)))
12111 ((string-equal key
"EMAIL")
12112 (setq email
(match-string 2)))
12113 ((string-equal key
"LANGUAGE")
12114 (setq language
(match-string 2)))
12115 ((string-equal key
"TEXT")
12116 (setq text
(concat text
"\n" (match-string 2))))
12117 ((string-equal key
"OPTIONS")
12118 (setq options
(match-string 2))))))))
12120 (defun org-parse-export-options (s)
12121 "Parse the export options line."
12122 (let ((op '(("H" . org-export-headline-levels
)
12123 ("num" . org-export-with-section-numbers
)
12124 ("toc" . org-export-with-toc
)
12125 ("\\n" . org-export-preserve-breaks
)
12126 ("@" . org-export-html-expand
)
12127 (":" . org-export-with-fixed-width
)
12128 ("|" . org-export-with-tables
)
12129 ("^" . org-export-with-sub-superscripts
)
12130 ("*" . org-export-with-emphasize
)
12131 ("TeX" . org-export-with-TeX-macros
)))
12133 (while (setq o
(pop op
))
12134 (if (string-match (concat (regexp-quote (car o
)) ":\\([^ \t\n\r;,.]*\\)")
12136 (set (make-local-variable (cdr o
))
12137 (car (read-from-string (match-string 1 s
))))))))
12139 (defun org-html-level-start (level title umax with-toc head-count
)
12140 "Insert a new level in HTML export."
12141 (let ((l (1+ (max level umax
))))
12142 (while (<= l org-level-max
)
12143 (if (aref levels-open
(1- l
))
12145 (org-html-level-close l
)
12146 (aset levels-open
(1- l
) nil
)))
12150 (if (aref levels-open
(1- level
))
12151 (insert "<li>" title
"<p>\n")
12152 (aset levels-open
(1- level
) t
)
12153 (insert "<ul><li>" title
"<p>\n")))
12154 (if org-export-with-section-numbers
12155 (setq title
(concat (org-section-number level
) " " title
)))
12156 (setq level
(+ level
1))
12158 (insert (format "\n<H%d><a name=\"sec-%d\">%s</a></H%d>\n"
12159 level head-count title level
))
12160 (insert (format "\n<H%d>%s</H%d>\n" level title level
))))))
12162 (defun org-html-level-close (&rest args
)
12163 "Terminate one level in HTML export."
12166 ;; Variable holding the vector with section numbers
12167 (defvar org-section-numbers
(make-vector org-level-max
0))
12169 (defun org-init-section-numbers ()
12170 "Initialize the vector for the section numbers."
12172 (numbers (nreverse (org-split-string "" "\\.")))
12173 (depth (1- (length org-section-numbers
)))
12174 (i depth
) number-string
)
12177 (aset org-section-numbers i
0)
12178 (setq number-string
(or (car numbers
) "0"))
12179 (if (string-match "\\`[A-Z]\\'" number-string
)
12180 (aset org-section-numbers i
12181 (- (string-to-char number-string
) ?A -
1))
12182 (aset org-section-numbers i
(string-to-number number-string
)))
12186 (defun org-section-number (&optional level
)
12187 "Return a string with the current section number.
12188 When LEVEL is non-nil, increase section numbers on that level."
12189 (let* ((depth (1- (length org-section-numbers
))) idx n
(string ""))
12192 (aset org-section-numbers
12193 level
(1+ (aref org-section-numbers level
))))
12194 (setq idx
(1+ level
))
12195 (while (<= idx depth
)
12196 (if (not (= idx
1))
12197 (aset org-section-numbers idx
0))
12198 (setq idx
(1+ idx
))))
12200 (while (<= idx depth
)
12201 (setq n
(aref org-section-numbers idx
))
12202 (setq string
(concat string
(if (not (string= string
"")) "." "")
12203 (int-to-string n
)))
12204 (setq idx
(1+ idx
)))
12206 (if (string-match "\\`\\([@0]\\.\\)+" string
)
12207 (setq string
(replace-match "" nil nil string
)))
12208 (if (string-match "\\(\\.0\\)+\\'" string
)
12209 (setq string
(replace-match "" nil nil string
))))
12213 (defun org-export-icalendar-this-file ()
12214 "Export current file as an iCalendar file.
12215 The iCalendar file will be located in the same directory as the Org-mode
12216 file, but with extension `.ics'."
12218 (org-export-icalendar nil buffer-file-name
))
12221 (defun org-export-icalendar-all-agenda-files ()
12222 "Export all files in `org-agenda-files' to iCalendar .ics files.
12223 Each iCalendar file will be located in the same directory as the Org-mode
12224 file, but with extension `.ics'."
12226 (apply 'org-export-icalendar nil
(org-agenda-files t
)))
12229 (defun org-export-icalendar-combine-agenda-files ()
12230 "Export all files in `org-agenda-files' to a single combined iCalendar file.
12231 The file is stored under the name `org-combined-agenda-icalendar-file'."
12233 (apply 'org-export-icalendar t
(org-agenda-files t
)))
12235 (defun org-export-icalendar (combine &rest files
)
12236 "Create iCalendar files for all elements of FILES.
12237 If COMBINE is non-nil, combine all calendar entries into a single large
12238 file and store it under the name `org-combined-agenda-icalendar-file'."
12240 (let* (file ical-file ical-buffer category started org-agenda-new-buffers
)
12242 (setq ical-file org-combined-agenda-icalendar-file
12243 ical-buffer
(org-get-agenda-file-buffer ical-file
))
12244 (set-buffer ical-buffer
) (erase-buffer))
12245 (while (setq file
(pop files
))
12247 (org-check-agenda-file file
)
12249 (setq ical-file
(concat (file-name-sans-extension file
) ".ics"))
12250 (setq ical-buffer
(org-get-agenda-file-buffer ical-file
))
12251 (set-buffer ical-buffer
) (erase-buffer))
12252 (set-buffer (org-get-agenda-file-buffer file
))
12253 (setq category
(or org-category
12254 (file-name-sans-extension
12255 (file-name-nondirectory buffer-file-name
))))
12256 (if (symbolp category
) (setq category
(symbol-name category
)))
12257 (let ((standard-output ical-buffer
))
12259 (and (not started
) (setq started t
)
12260 (org-start-icalendar-file org-icalendar-combined-name
))
12261 (org-start-icalendar-file category
))
12262 (org-print-icalendar-entries combine category
)
12263 (when (or (and combine
(not files
)) (not combine
))
12264 (org-finish-icalendar-file)
12265 (set-buffer ical-buffer
)
12267 (run-hooks 'org-after-save-iCalendar-file-hook
)))))
12268 (org-release-buffers org-agenda-new-buffers
))))
12270 (defvar org-after-save-iCalendar-file-hook nil
12271 "Hook run after an iCalendar file has been saved.
12272 The iCalendar buffer is still current when this hook is run.
12273 A good way to use this is to tell a desktop calenndar application to re-read
12274 the iCalendar file.")
12276 (defun org-print-icalendar-entries (&optional combine category
)
12277 "Print iCalendar entries for the current Org-mode file to `standard-output'.
12278 When COMBINE is non nil, add the category to each line."
12279 (let ((re2 (concat "--?-?\\(" org-ts-regexp
"\\)"))
12280 (dts (org-ical-ts-to-string
12281 (format-time-string (cdr org-time-stamp-formats
) (current-time))
12283 hd ts ts2 state
(inc t
) pos scheduledp deadlinep tmp pri
)
12285 (goto-char (point-min))
12286 (while (re-search-forward org-ts-regexp nil t
)
12287 (setq pos
(match-beginning 0)
12288 ts
(match-string 0)
12290 hd
(org-get-heading))
12291 (if (looking-at re2
)
12293 (goto-char (match-end 0))
12294 (setq ts2
(match-string 1) inc nil
))
12296 tmp
(buffer-substring (max (point-min)
12297 (- pos org-ds-keyword-length
))
12299 deadlinep
(string-match org-deadline-regexp tmp
)
12300 scheduledp
(string-match org-scheduled-regexp tmp
)
12301 ;; donep (org-entry-is-done-p)
12303 (if (or (string-match org-tr-regexp hd
)
12304 (string-match org-ts-regexp hd
))
12305 (setq hd
(replace-match "" t t hd
)))
12307 (setq hd
(concat hd
" (category " category
")")))
12308 (if deadlinep
(setq hd
(concat "DL: " hd
" This is a deadline")))
12309 (if scheduledp
(setq hd
(concat "S: " hd
" Scheduled for this date")))
12310 (princ (format "BEGIN:VEVENT
12315 (org-ical-ts-to-string ts
"DTSTART")
12316 (org-ical-ts-to-string ts2
"DTEND" inc
)
12318 (when org-icalendar-include-todo
12319 (goto-char (point-min))
12320 (while (re-search-forward org-todo-line-regexp nil t
)
12321 (setq state
(match-string 1))
12322 (unless (equal state org-done-string
)
12323 (setq hd
(match-string 3))
12324 (if (string-match org-priority-regexp hd
)
12325 (setq pri
(string-to-char (match-string 2 hd
))
12326 hd
(concat (substring hd
0 (match-beginning 1))
12327 (substring hd
(- (match-end 1)))))
12328 (setq pri org-default-priority
))
12329 (setq pri
(floor (1+ (* 8.
(/ (float (- org-lowest-priority pri
))
12330 (- org-lowest-priority ?A
))))))
12332 (princ (format "BEGIN:VTODO
12340 (defun org-start-icalendar-file (name)
12341 "Start an iCalendar file by inserting the header."
12342 (let ((user user-full-name
)
12343 (name (or name
"unknown"))
12344 (timezone (cadr (current-time-zone))))
12346 (format "BEGIN:VCALENDAR
12349 PRODID:-//%s//Emacs with Org-mode//EN
12351 CALSCALE:GREGORIAN\n" name user timezone
))))
12353 (defun org-finish-icalendar-file ()
12354 "Finish an iCalendar file by inserting the END statement."
12355 (princ "END:VCALENDAR\n"))
12357 (defun org-ical-ts-to-string (s keyword
&optional inc
)
12358 "Take a time string S and convert it to iCalendar format.
12359 KEYWORD is added in front, to make a complete line like DTSTART....
12360 When INC is non-nil, increase the hour by two (if time string contains
12361 a time), or the day by one (if it does not contain a time)."
12362 (let ((t1 (org-parse-time-string s
'nodefault
))
12363 t2 fmt have-time time
)
12364 (if (and (car t1
) (nth 1 t1
) (nth 2 t1
))
12365 (setq t2 t1 have-time t
)
12366 (setq t2
(org-parse-time-string s
)))
12367 (let ((s (car t2
)) (mi (nth 1 t2
)) (h (nth 2 t2
))
12368 (d (nth 3 t2
)) (m (nth 4 t2
)) (y (nth 5 t2
)))
12370 (if have-time
(setq h
(+ 2 h
)) (setq d
(1+ d
))))
12371 (setq time
(encode-time s mi h d m y
)))
12372 (setq fmt
(if have-time
":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
12373 (concat keyword
(format-time-string fmt time
))))
12378 ;; - Bindings in Org-mode map are currently
12379 ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
12380 ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
12381 ;; e (?) useful from outline-mode
12382 ;; i k @ expendable from outline-mode
12383 ;; 0123456789 % & ()_{} " ` free
12385 ;; Make `C-c C-x' a prefix key
12386 (define-key org-mode-map
"\C-c\C-x" (make-sparse-keymap))
12388 ;; TAB key with modifiers
12389 (define-key org-mode-map
"\C-i" 'org-cycle
)
12390 (define-key org-mode-map
[(tab)] 'org-cycle
)
12391 (define-key org-mode-map
[(meta tab
)] 'org-complete
)
12392 (define-key org-mode-map
"\M-\C-i" 'org-complete
) ; for tty emacs
12393 ;; The following line is necessary under Suse GNU/Linux
12394 (unless (featurep 'xemacs
)
12395 (define-key org-mode-map
[S-iso-lefttab
] 'org-shifttab
))
12396 (define-key org-mode-map
[(shift tab
)] 'org-shifttab
)
12398 (define-key org-mode-map
(org-key 'S-return
) 'org-table-copy-down
)
12399 (define-key org-mode-map
"\C-c\C-xc" 'org-table-copy-down
) ; tty
12400 (define-key org-mode-map
[(meta shift return
)] 'org-insert-todo-heading
)
12401 (define-key org-mode-map
"\C-c\C-xM" 'org-insert-todo-heading
) ; tty
12402 (define-key org-mode-map
[(meta return
)] 'org-meta-return
)
12403 (define-key org-mode-map
"\C-c\C-xm" 'org-meta-return
) ; tty emacs
12404 (define-key org-mode-map
[?\e
(return)] 'org-meta-return
) ; tty emacs
12406 ;; Cursor keys with modifiers
12407 (define-key org-mode-map
[(meta left
)] 'org-metaleft
)
12408 (define-key org-mode-map
[?\e
(left)] 'org-metaleft
) ; for tty emacs
12409 (define-key org-mode-map
"\C-c\C-xl" 'org-metaleft
) ; for tty emacs
12410 (define-key org-mode-map
[(meta right
)] 'org-metaright
)
12411 (define-key org-mode-map
[?\e
(right)] 'org-metaright
) ; for tty emacs
12412 (define-key org-mode-map
"\C-c\C-xr" 'org-metaright
) ; for tty emacs
12413 (define-key org-mode-map
[(meta up
)] 'org-metaup
)
12414 (define-key org-mode-map
[?\e
(up)] 'org-metaup
) ; for tty emacs
12415 (define-key org-mode-map
"\C-c\C-xu" 'org-metaup
) ; for tty emacs
12416 (define-key org-mode-map
[(meta down
)] 'org-metadown
)
12417 (define-key org-mode-map
[?\e
(down)] 'org-metadown
) ; for tty emacs
12418 (define-key org-mode-map
"\C-c\C-xd" 'org-metadown
) ; for tty emacs
12420 (define-key org-mode-map
[(meta shift left
)] 'org-shiftmetaleft
)
12421 (define-key org-mode-map
"\C-c\C-xL" 'org-shiftmetaleft
) ; tty
12422 (define-key org-mode-map
[(meta shift right
)] 'org-shiftmetaright
)
12423 (define-key org-mode-map
"\C-c\C-xR" 'org-shiftmetaright
) ; tty
12424 (define-key org-mode-map
[(meta shift up
)] 'org-shiftmetaup
)
12425 (define-key org-mode-map
"\C-c\C-xU" 'org-shiftmetaup
) ; tty
12426 (define-key org-mode-map
[(meta shift down
)] 'org-shiftmetadown
)
12427 (define-key org-mode-map
"\C-c\C-xD" 'org-shiftmetadown
) ; tty
12428 (define-key org-mode-map
(org-key 'S-up
) 'org-shiftup
)
12429 (define-key org-mode-map
[?\C-c ?\C-x
(up)] 'org-shiftup
)
12430 (define-key org-mode-map
(org-key 'S-down
) 'org-shiftdown
)
12431 (define-key org-mode-map
[?\C-c ?\C-x
(down)] 'org-shiftdown
)
12432 (define-key org-mode-map
(org-key 'S-left
) 'org-shiftleft
)
12433 (define-key org-mode-map
[?\C-c ?\C-x
(left)] 'org-shiftleft
)
12434 (define-key org-mode-map
(org-key 'S-right
) 'org-shiftright
)
12435 (define-key org-mode-map
[?\C-c ?\C-x
(right)] 'org-shiftright
)
12437 ;; All the other keys
12438 (define-key org-mode-map
"\C-c$" 'org-archive-subtree
)
12439 (define-key org-mode-map
"\C-c\C-j" 'org-goto
)
12440 (define-key org-mode-map
"\C-c\C-t" 'org-todo
)
12441 (define-key org-mode-map
"\C-c\C-s" 'org-schedule
)
12442 (define-key org-mode-map
"\C-c\C-d" 'org-deadline
)
12443 (define-key org-mode-map
"\C-c;" 'org-toggle-comment
)
12444 (define-key org-mode-map
"\C-c\C-v" 'org-show-todo-tree
)
12445 (define-key org-mode-map
"\C-c\C-w" 'org-check-deadlines
)
12446 (define-key org-mode-map
"\C-c/" 'org-occur
) ; Minor-mode reserved
12447 (define-key org-mode-map
"\C-c\\" 'org-tags-sparse-tree
) ; Minor-mode res.
12448 (define-key org-mode-map
"\C-c\C-m" 'org-insert-heading
)
12449 (define-key org-mode-map
"\M-\C-m" 'org-insert-heading
)
12450 (define-key org-mode-map
"\C-c\C-l" 'org-insert-link
)
12451 (define-key org-mode-map
"\C-c\C-o" 'org-open-at-point
)
12452 (define-key org-mode-map
"\C-c%" 'org-mark-ring-push
)
12453 (define-key org-mode-map
"\C-c&" 'org-mark-ring-goto
)
12454 (define-key org-mode-map
"\C-c\C-z" 'org-time-stamp
) ; Alternative binding
12455 (define-key org-mode-map
"\C-c." 'org-time-stamp
) ; Minor-mode reserved
12456 (define-key org-mode-map
"\C-c!" 'org-time-stamp-inactive
) ; Minor-mode r.
12457 (define-key org-mode-map
"\C-c," 'org-priority
) ; Minor-mode reserved
12458 (define-key org-mode-map
"\C-c\C-y" 'org-evaluate-time-range
)
12459 (define-key org-mode-map
"\C-c>" 'org-goto-calendar
)
12460 (define-key org-mode-map
"\C-c<" 'org-date-from-calendar
)
12461 (define-key org-mode-map
[(control ?
,)] 'org-cycle-agenda-files
)
12462 (define-key org-mode-map
"\C-c[" 'org-agenda-file-to-front
)
12463 (define-key org-mode-map
"\C-c]" 'org-remove-file
)
12464 (define-key org-mode-map
"\C-c\C-r" 'org-timeline
)
12465 (define-key org-mode-map
"\C-c-" 'org-table-insert-hline
)
12466 (define-key org-mode-map
"\C-c^" 'org-table-sort-lines
)
12467 (define-key org-mode-map
"\C-c\C-c" 'org-ctrl-c-ctrl-c
)
12468 (define-key org-mode-map
"\C-m" 'org-return
)
12469 (define-key org-mode-map
"\C-c?" 'org-table-current-column
)
12470 (define-key org-mode-map
"\C-c " 'org-table-blank-field
)
12471 (define-key org-mode-map
"\C-c+" 'org-table-sum
)
12472 (define-key org-mode-map
"\C-c=" 'org-table-eval-formula
)
12473 (define-key org-mode-map
"\C-c'" 'org-table-edit-formulas
)
12474 (define-key org-mode-map
"\C-c`" 'org-table-edit-field
)
12475 (define-key org-mode-map
"\C-c|" 'org-table-create-or-convert-from-region
)
12476 (define-key org-mode-map
"\C-c*" 'org-table-recalculate
)
12477 (define-key org-mode-map
[(control ?
#)] 'org-table-rotate-recalc-marks
)
12478 (define-key org-mode-map
"\C-c~" 'org-table-create-with-table.el
)
12479 (define-key org-mode-map
"\C-c\C-q" 'org-table-wrap-region
)
12480 (define-key org-mode-map
"\C-c\C-xa" 'org-export-as-ascii
)
12481 (define-key org-mode-map
"\C-c\C-x\C-a" 'org-export-as-ascii
)
12482 (define-key org-mode-map
"\C-c\C-xv" 'org-export-copy-visible
)
12483 (define-key org-mode-map
"\C-c\C-x\C-v" 'org-export-copy-visible
)
12484 ;; OPML support is only an option for the future
12485 ;(define-key org-mode-map "\C-c\C-xo" 'org-export-as-opml)
12486 ;(define-key org-mode-map "\C-c\C-x\C-o" 'org-export-as-opml)
12487 (define-key org-mode-map
"\C-c\C-xi" 'org-export-icalendar-this-file
)
12488 (define-key org-mode-map
"\C-c\C-x\C-i" 'org-export-icalendar-all-agenda-files
)
12489 (define-key org-mode-map
"\C-c\C-xc" 'org-export-icalendar-combine-agenda-files
)
12490 (define-key org-mode-map
"\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files
)
12491 (define-key org-mode-map
"\C-c\C-xt" 'org-insert-export-options-template
)
12492 (define-key org-mode-map
"\C-c:" 'org-toggle-fixed-width-section
)
12493 (define-key org-mode-map
"\C-c\C-xh" 'org-export-as-html
)
12494 (define-key org-mode-map
"\C-c\C-xx" 'org-export-as-xml
)
12495 (define-key org-mode-map
"\C-c\C-x\C-x" 'org-export-as-xml
)
12496 (define-key org-mode-map
"\C-c\C-xb" 'org-export-as-html-and-open
)
12497 (define-key org-mode-map
"\C-c\C-x\C-b" 'org-export-as-html-and-open
)
12499 (define-key org-mode-map
"\C-c\C-x\C-k" 'org-cut-special
)
12500 (define-key org-mode-map
"\C-c\C-x\C-w" 'org-cut-special
)
12501 (define-key org-mode-map
"\C-c\C-x\M-w" 'org-copy-special
)
12502 (define-key org-mode-map
"\C-c\C-x\C-y" 'org-paste-special
)
12504 (defsubst org-table-p
() (org-at-table-p))
12506 (defun org-self-insert-command (N)
12507 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
12508 If the cursor is in a table looking at whitespace, the whitespace is
12509 overwritten, and the table is not marked as requiring realignment."
12511 (if (and (org-table-p)
12513 ;; check if we blank the field, and if that triggers align
12514 (and org-table-auto-blank-field
12515 (member last-command
12516 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c
))
12517 (if (or (equal (char-after) ?\
) (looking-at "[^|\n]* |"))
12518 ;; got extra space, this field does not determine column width
12519 (let (org-table-may-need-update) (org-table-blank-field))
12520 ;; no extra space, this field may determine column width
12521 (org-table-blank-field)))
12524 (looking-at "[^|\n]* |"))
12525 (let (org-table-may-need-update)
12526 (goto-char (1- (match-end 0)))
12527 (delete-backward-char 1)
12528 (goto-char (match-beginning 0))
12529 (self-insert-command N
))
12530 (setq org-table-may-need-update t
)
12531 (self-insert-command N
)))
12533 (defun org-delete-backward-char (N)
12534 "Like `delete-backward-char', insert whitespace at field end in tables.
12535 When deleting backwards, in tables this function will insert whitespace in
12536 front of the next \"|\" separator, to keep the table aligned. The table will
12537 still be marked for re-alignment if the field did fill the entire column,
12538 because, in this case the deletion might narrow the column."
12540 (if (and (org-table-p)
12542 (string-match "|" (buffer-substring (point-at-bol) (point)))
12543 (looking-at ".*?|"))
12544 (let ((pos (point))
12545 (noalign (looking-at "[^|\n\r]* |"))
12546 (c org-table-may-need-update
))
12547 (backward-delete-char N
)
12548 (skip-chars-forward "^|")
12550 (goto-char (1- pos
))
12551 ;; noalign: if there were two spaces at the end, this field
12552 ;; does not determine the width of the column.
12553 (if noalign
(setq org-table-may-need-update c
)))
12554 (backward-delete-char N
)))
12556 (defun org-delete-char (N)
12557 "Like `delete-char', but insert whitespace at field end in tables.
12558 When deleting characters, in tables this function will insert whitespace in
12559 front of the next \"|\" separator, to keep the table aligned. The table will
12560 still be marked for re-alignment if the field did fill the entire column,
12561 because, in this case the deletion might narrow the column."
12563 (if (and (org-table-p)
12565 (not (= (char-after) ?|
))
12567 (if (looking-at ".*?|")
12568 (let ((pos (point))
12569 (noalign (looking-at "[^|\n\r]* |"))
12570 (c org-table-may-need-update
))
12571 (replace-match (concat
12572 (substring (match-string 0) 1 -
1)
12575 ;; noalign: if there were two spaces at the end, this field
12576 ;; does not determine the width of the column.
12577 (if noalign
(setq org-table-may-need-update c
))))
12580 ;; How to do this: Measure non-white length of current string
12581 ;; If equal to column width, we should realign.
12583 (defun org-remap (map &rest commands
)
12584 "In MAP, remap the functions given in COMMANDS.
12585 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
12588 (setq old
(pop commands
) new
(pop commands
))
12589 (if (fboundp 'command-remapping
)
12590 (define-key map
(vector 'remap old
) new
)
12591 (substitute-key-definition old new map global-map
)))))
12593 (when (eq org-enable-table-editor
'optimized
)
12594 ;; If the user wants maximum table support, we need to hijack
12595 ;; some standard editing functions
12596 (org-remap org-mode-map
12597 'self-insert-command
'org-self-insert-command
12598 'delete-char
'org-delete-char
12599 'delete-backward-char
'org-delete-backward-char
)
12600 (define-key org-mode-map
"|" 'org-force-self-insert
))
12602 (defun org-shiftcursor-error ()
12603 "Throw an error because Shift-Cursor command was applied in wrong context."
12604 (error "This command is active in special context like tables, headlines or timestamps"))
12606 (defun org-shifttab ()
12607 "Global visibility cycling or move to previous table field.
12608 Calls `(org-cycle t)' or `org-table-previous-field', depending on context.
12609 See the individual commands for more information."
12612 ((org-at-table-p) (org-table-previous-field))
12613 (t (org-cycle '(4)))))
12615 (defun org-shiftmetaleft ()
12616 "Promote subtree or delete table column.
12617 Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
12618 See the individual commands for more information."
12621 ((org-at-table-p) (org-table-delete-column))
12622 ((org-on-heading-p) (org-promote-subtree))
12623 ((org-at-item-p) (call-interactively 'org-outdent-item
))
12624 (t (org-shiftcursor-error))))
12626 (defun org-shiftmetaright ()
12627 "Demote subtree or insert table column.
12628 Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
12629 See the individual commands for more information."
12632 ((org-at-table-p) (org-table-insert-column))
12633 ((org-on-heading-p) (org-demote-subtree))
12634 ((org-at-item-p) (call-interactively 'org-indent-item
))
12635 (t (org-shiftcursor-error))))
12637 (defun org-shiftmetaup (&optional arg
)
12638 "Move subtree up or kill table row.
12639 Calls `org-move-subtree-up' or `org-table-kill-row' or
12640 `org-move-item-up' depending on context. See the individual commands
12641 for more information."
12644 ((org-at-table-p) (org-table-kill-row))
12645 ((org-on-heading-p) (org-move-subtree-up arg
))
12646 ((org-at-item-p) (org-move-item-up arg
))
12647 (t (org-shiftcursor-error))))
12648 (defun org-shiftmetadown (&optional arg
)
12649 "Move subtree down or insert table row.
12650 Calls `org-move-subtree-down' or `org-table-insert-row' or
12651 `org-move-item-down', depending on context. See the individual
12652 commands for more information."
12655 ((org-at-table-p) (org-table-insert-row arg
))
12656 ((org-on-heading-p) (org-move-subtree-down arg
))
12657 ((org-at-item-p) (org-move-item-down arg
))
12658 (t (org-shiftcursor-error))))
12660 (defun org-metaleft (&optional arg
)
12661 "Promote heading or move table column to left.
12662 Calls `org-do-promote' or `org-table-move-column', depending on context.
12663 With no specific context, calls the Emacs default `backward-word'.
12664 See the individual commands for more information."
12667 ((org-at-table-p) (org-table-move-column 'left
))
12668 ((or (org-on-heading-p) (org-region-active-p)) (org-do-promote))
12669 (t (backward-word (prefix-numeric-value arg
)))))
12671 (defun org-metaright (&optional arg
)
12672 "Demote subtree or move table column to right.
12673 Calls `org-do-demote' or `org-table-move-column', depending on context.
12674 With no specific context, calls the Emacs default `forward-word'.
12675 See the individual commands for more information."
12678 ((org-at-table-p) (org-table-move-column nil
))
12679 ((or (org-on-heading-p) (org-region-active-p)) (org-do-demote))
12680 (t (forward-word (prefix-numeric-value arg
)))))
12682 (defun org-metaup (&optional arg
)
12683 "Move subtree up or move table row up.
12684 Calls `org-move-subtree-up' or `org-table-move-row' or
12685 `org-move-item-up', depending on context. See the individual commands
12686 for more information."
12689 ((org-at-table-p) (org-table-move-row 'up
))
12690 ((org-on-heading-p) (org-move-subtree-up arg
))
12691 ((org-at-item-p) (org-move-item-up arg
))
12692 (t (org-shiftcursor-error))))
12694 (defun org-metadown (&optional arg
)
12695 "Move subtree down or move table row down.
12696 Calls `org-move-subtree-down' or `org-table-move-row' or
12697 `org-move-item-down', depending on context. See the individual
12698 commands for more information."
12701 ((org-at-table-p) (org-table-move-row nil
))
12702 ((org-on-heading-p) (org-move-subtree-down arg
))
12703 ((org-at-item-p) (org-move-item-down arg
))
12704 (t (org-shiftcursor-error))))
12706 (defun org-shiftup (&optional arg
)
12707 "Increase item in timestamp or increase priority of current item.
12708 Calls `org-timestamp-up' or `org-priority-up', depending on context.
12709 See the individual commands for more information."
12712 ((org-at-timestamp-p) (org-timestamp-up arg
))
12713 (t (org-priority-up))))
12715 (defun org-shiftdown (&optional arg
)
12716 "Decrease item in timestamp or decrease priority of current item.
12717 Calls `org-timestamp-down' or `org-priority-down', depending on context.
12718 See the individual commands for more information."
12721 ((org-at-timestamp-p) (org-timestamp-down arg
))
12722 (t (org-priority-down))))
12724 (defun org-shiftright ()
12725 "Next TODO keyword or timestamp one day later, depending on context."
12728 ((org-at-timestamp-p) (org-timestamp-up-day))
12729 ((org-on-heading-p) (org-todo 'right
))
12730 (t (org-shiftcursor-error))))
12732 (defun org-shiftleft ()
12733 "Previous TODO keyword or timestamp one day earlier, depending on context."
12736 ((org-at-timestamp-p) (org-timestamp-down-day))
12737 ((org-on-heading-p) (org-todo 'left
))
12738 (t (org-shiftcursor-error))))
12740 (defun org-copy-special ()
12741 "Copy region in table or copy current subtree.
12742 Calls `org-table-copy' or `org-copy-subtree', depending on context.
12743 See the individual commands for more information."
12745 (call-interactively
12746 (if (org-at-table-p) 'org-table-copy-region
'org-copy-subtree
)))
12748 (defun org-cut-special ()
12749 "Cut region in table or cut current subtree.
12750 Calls `org-table-copy' or `org-cut-subtree', depending on context.
12751 See the individual commands for more information."
12753 (call-interactively
12754 (if (org-at-table-p) 'org-table-cut-region
'org-cut-subtree
)))
12756 (defun org-paste-special (arg)
12757 "Paste rectangular region into table, or past subtree relative to level.
12758 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
12759 See the individual commands for more information."
12761 (if (org-at-table-p)
12762 (org-table-paste-rectangle)
12763 (org-paste-subtree arg
)))
12765 (defun org-ctrl-c-ctrl-c (&optional arg
)
12766 "Set tags in headline, or update according to changed information at point.
12768 This command does many different things, depending on context:
12770 - If the cursor is in a headline, prompt for tags and insert them
12771 into the current line, aligned to `org-tags-column'. When called
12772 with prefix arg, realign all tags in the current buffer.
12774 - If the cursor is in one of the special #+KEYWORD lines, this
12775 triggers scanning the buffer for these lines and updating the
12778 - If the cursor is inside a table, realign the table. This command
12779 works even if the automatic table editor has been turned off.
12781 - If the cursor is on a #+TBLFM line, re-apply the formulas to
12784 - If the cursor is inside a table created by the table.el package,
12785 activate that table. Otherwise, if the cursor is at a normal table
12786 created with org.el, re-align that table.
12788 - If the current buffer is a remember buffer, close note and file it.
12789 with a prefix argument, file it without further interaction to the default
12792 - If the cursor is on a <<<target>>>, update radio targets and corresponding
12793 links in this buffer.
12795 - If the cursor is on a numbered item in a plain list, renumber the
12798 (let ((org-enable-table-editor t
))
12800 ((and (local-variable-p 'org-finish-function
(current-buffer))
12801 (fboundp org-finish-function
))
12802 (funcall org-finish-function
))
12803 ((org-on-target-p) (org-update-radio-target-regexp))
12804 ((org-on-heading-p) (org-set-tags arg
))
12805 ((org-at-table.el-p
)
12807 (beginning-of-line 1)
12808 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
12809 (table-recognize-table))
12811 (org-table-maybe-eval-formula)
12813 (org-table-recalculate t
)
12814 (org-table-maybe-recalculate-line))
12817 (org-renumber-ordered-list (prefix-numeric-value arg
)))
12818 ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
12820 ((equal (match-string 1) "TBLFM")
12821 ;; Recalculate the table before this line
12823 (beginning-of-line 1)
12824 (skip-chars-backward " \r\n\t")
12825 (if (org-at-table-p) (org-table-recalculate t
))))
12827 (org-mode-restart))))
12828 (t (error "C-c C-c can do nothing useful at this location.")))))
12830 (defun org-mode-restart ()
12831 "Restart Org-mode, to scan again for special lines.
12832 Also updates the keyword regular expressions."
12834 (let ((org-inhibit-startup t
)) (org-mode))
12835 (message "Org-mode restarted to refresh keyword and special line setup"))
12837 (defun org-return ()
12838 "Goto next table row or insert a newline.
12839 Calls `org-table-next-row' or `newline', depending on context.
12840 See the individual commands for more information."
12844 (org-table-justify-field-maybe)
12845 (org-table-next-row))
12848 (defun org-meta-return (&optional arg
)
12849 "Insert a new heading or wrap a region in a table.
12850 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
12851 See the individual commands for more information."
12855 (org-table-wrap-region arg
))
12856 (t (org-insert-heading arg
))))
12860 ;; Define the Org-mode menus
12861 (easy-menu-define org-tbl-menu org-mode-map
"Tbl menu"
12863 ["Align" org-ctrl-c-ctrl-c
(org-at-table-p)]
12864 ["Next Field" org-cycle
(org-at-table-p)]
12865 ["Previous Field" org-shifttab
(org-at-table-p)]
12866 ["Next Row" org-return
(org-at-table-p)]
12868 ["Blank Field" org-table-blank-field
(org-at-table-p)]
12869 ["Edit Field" org-table-edit-field
(org-at-table-p)]
12870 ["Copy Field from Above" org-table-copy-down
(org-at-table-p)]
12873 ["Move Column Left" org-metaleft
(org-at-table-p)]
12874 ["Move Column Right" org-metaright
(org-at-table-p)]
12875 ["Delete Column" org-shiftmetaleft
(org-at-table-p)]
12876 ["Insert Column" org-shiftmetaright
(org-at-table-p)]
12878 ["Enable Narrowing" (setq org-table-limit-column-width
(not org-table-limit-column-width
)) :active
(org-at-table-p) :selected org-table-limit-column-width
:style toggle
])
12880 ["Move Row Up" org-metaup
(org-at-table-p)]
12881 ["Move Row Down" org-metadown
(org-at-table-p)]
12882 ["Delete Row" org-shiftmetaup
(org-at-table-p)]
12883 ["Insert Row" org-shiftmetadown
(org-at-table-p)]
12884 ["Sort lines in region" org-table-sort-lines
(org-at-table-p)]
12886 ["Insert Hline" org-table-insert-hline
(org-at-table-p)])
12888 ["Copy Rectangle" org-copy-special
(org-at-table-p)]
12889 ["Cut Rectangle" org-cut-special
(org-at-table-p)]
12890 ["Paste Rectangle" org-paste-special
(org-at-table-p)]
12891 ["Fill Rectangle" org-table-wrap-region
(org-at-table-p)])
12894 ["Set Column Formula" org-table-eval-formula
(org-at-table-p)]
12895 ["Set Named Field Formula" (org-table-eval-formula '(4)) :active
(org-at-table-p) :keys
"C-u C-c ="]
12896 ["Edit Formulas" org-table-edit-formulas
(org-at-table-p)]
12898 ["Recalculate line" org-table-recalculate
(org-at-table-p)]
12899 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active
(org-at-table-p) :keys
"C-u C-c *"]
12900 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks
(org-at-table-p)]
12902 ["Sum Column/Rectangle" org-table-sum
12903 (or (org-at-table-p) (org-region-active-p))]
12904 ["Which Column?" org-table-current-column
(org-at-table-p)])
12906 (setq org-table-formula-debug
(not org-table-formula-debug
))
12907 :style toggle
:selected org-table-formula-debug
]
12909 ["Create" org-table-create
(and (not (org-at-table-p))
12910 org-enable-table-editor
)]
12911 ["Convert Region" org-table-convert-region
(not (org-at-table-p 'any
))]
12912 ["Import from File" org-table-import
(not (org-at-table-p))]
12913 ["Export to File" org-table-export
(org-at-table-p)]
12915 ["Create/Convert from/to table.el" org-table-create-with-table.el t
]))
12917 (easy-menu-define org-org-menu org-mode-map
"Org menu"
12919 ["Cycle Visibility" org-cycle
(or (bobp) (outline-on-heading-p))]
12920 ["Cycle Global Visibility" org-shifttab
(not (org-at-table-p))]
12921 ["Sparse Tree" org-occur t
]
12922 ["Show All" show-all t
]
12924 ["New Heading" org-insert-heading t
]
12925 ("Navigate Headings"
12926 ["Up" outline-up-heading t
]
12927 ["Next" outline-next-visible-heading t
]
12928 ["Previous" outline-previous-visible-heading t
]
12929 ["Next Same Level" outline-forward-same-level t
]
12930 ["Previous Same Level" outline-backward-same-level t
]
12932 ["Jump" org-goto t
])
12934 ["Move Subtree Up" org-shiftmetaup
(not (org-at-table-p))]
12935 ["Move Subtree Down" org-shiftmetadown
(not (org-at-table-p))]
12937 ["Copy Subtree" org-copy-special
(not (org-at-table-p))]
12938 ["Cut Subtree" org-cut-special
(not (org-at-table-p))]
12939 ["Paste Subtree" org-paste-special
(not (org-at-table-p))]
12941 ["Promote Heading" org-metaleft
(not (org-at-table-p))]
12942 ["Promote Subtree" org-shiftmetaleft
(not (org-at-table-p))]
12943 ["Demote Heading" org-metaright
(not (org-at-table-p))]
12944 ["Demote Subtree" org-shiftmetaright
(not (org-at-table-p))]
12946 ["Archive Subtree" org-archive-subtree t
]
12948 ["Convert to odd levels" org-convert-to-odd-levels t
]
12949 ["Convert to odd/even levels" org-convert-to-oddeven-levels t
])
12952 ["TODO/DONE/-" org-todo t
]
12953 ["Show TODO Tree" org-show-todo-tree t
]
12954 ["Global TODO list" org-todo-list t
]
12956 ["Set Priority" org-priority t
]
12957 ["Priority Up" org-shiftup t
]
12958 ["Priority Down" org-shiftdown t
])
12959 ("Dates and Scheduling"
12960 ["Timestamp" org-time-stamp t
]
12961 ["Timestamp (inactive)" org-time-stamp-inactive t
]
12963 ["1 Day Later" org-timestamp-up-day t
]
12964 ["1 Day Earlier" org-timestamp-down-day t
]
12965 ["1 ... Later" org-shiftup t
]
12966 ["1 ... Earlier" org-shiftdown t
])
12967 ["Compute Time Range" org-evaluate-time-range t
]
12968 ["Schedule Item" org-schedule t
]
12969 ["Deadline" org-deadline t
]
12971 ["Goto Calendar" org-goto-calendar t
]
12972 ["Date from Calendar" org-date-from-calendar t
])
12974 ["Agenda Command" org-agenda t
]
12975 ("File List for Agenda")
12976 ("Special views current file"
12977 ["TODO Tree" org-show-todo-tree t
]
12978 ["Check Deadlines" org-check-deadlines t
]
12979 ["Timeline" org-timeline t
]
12980 ["Tags Tree" org-tags-sparse-tree t
])
12983 ["Store Link (Global)" org-store-link t
]
12984 ["Insert Link" org-insert-link t
]
12985 ["Follow Link" org-open-at-point t
]
12987 ["Descriptive Links"
12988 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
12989 :style radio
:selected
(member '(org-link) buffer-invisibility-spec
)]
12992 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
12993 :style radio
:selected
(not (member '(org-link) buffer-invisibility-spec
))]
12995 ["Upgrade all <link> to [[link][desc]]" org-upgrade-old-links
12996 (save-excursion (goto-char (point-min))
12997 (re-search-forward "<[a-z]+:" nil t
))])
13000 ["ASCII" org-export-as-ascii t
]
13001 ["Extract Visible Text" org-export-copy-visible t
]
13002 ["HTML" org-export-as-html t
]
13003 ["HTML and Open" org-export-as-html-and-open t
]
13004 ["XML (XOXO)" org-export-as-xml t
]
13006 ["iCalendar this file" org-export-icalendar-this-file t
]
13007 ["iCalendar all agenda files" org-export-icalendar-all-agenda-files
13008 :active t
:keys
"C-c C-x C-i"]
13009 ["iCalendar combined" org-export-icalendar-combine-agenda-files t
]
13011 ["Option Template" org-insert-export-options-template t
]
13012 ["Toggle Fixed Width" org-toggle-fixed-width-section t
])
13015 ["Show Version" org-version t
]
13016 ["Info Documentation" org-info t
])
13018 ["Browse Org Group" org-customize t
]
13020 ["Expand This Menu" org-create-customize-menu
13021 (fboundp 'customize-menu-create
)])
13023 ["Refresh setup" org-mode-restart t
]
13026 (defun org-info (&optional node
)
13027 "Read documentation for Org-mode in the info system.
13028 With optional NODE, go directly to that node."
13031 (Info-goto-node (format "(org)%s" (or node
""))))
13033 (defun org-install-agenda-files-menu ()
13034 (let ((bl (buffer-list)))
13037 (set-buffer (pop bl
))
13038 (if (eq major-mode
'org-mode
) (setq bl nil
)))
13039 (when (eq major-mode
'org-mode
)
13041 '("Org") "File List for Agenda"
13044 ["Edit File List" (org-edit-agenda-file-list) t
]
13045 ["Add/Move Current File to Front of List" org-agenda-file-to-front t
]
13046 ["Remove Current File from List" org-remove-file t
]
13047 ["Cycle through agenda files" org-cycle-agenda-files t
]
13049 (mapcar 'org-file-menu-entry
(org-agenda-files t
))))))))
13053 (defun org-customize ()
13054 "Call the customize function with org as argument."
13056 (customize-browse 'org
))
13058 (defun org-create-customize-menu ()
13059 "Create a full customization menu for Org-mode, insert it into the menu."
13061 (if (fboundp 'customize-menu-create
)
13064 '("Org") "Customize"
13065 `(["Browse Org group" org-customize t
]
13067 ,(customize-menu-create 'org
)
13068 ["Set" Custom-set t
]
13069 ["Save" Custom-save t
]
13070 ["Reset to Current" Custom-reset-current t
]
13071 ["Reset to Saved" Custom-reset-saved t
]
13072 ["Reset to Standard Settings" Custom-reset-standard t
]))
13073 (message "\"Org\"-menu now contains full customization menu"))
13074 (error "Cannot expand menu (outdated version of cus-edit.el)")))
13076 ;;; Miscellaneous stuff
13078 (defun org-move-line-down (arg)
13079 "Move the current line down. With prefix argument, move it past ARG lines."
13081 (let ((col (current-column))
13083 (beginning-of-line 1) (setq beg
(point))
13084 (beginning-of-line 2) (setq end
(point))
13085 (beginning-of-line (+ 1 arg
))
13086 (setq pos
(move-marker (make-marker) (point)))
13087 (insert (delete-and-extract-region beg end
))
13089 (move-to-column col
)))
13091 (defun org-move-line-up (arg)
13092 "Move the current line up. With prefix argument, move it past ARG lines."
13094 (let ((col (current-column))
13096 (beginning-of-line 1) (setq beg
(point))
13097 (beginning-of-line 2) (setq end
(point))
13098 (beginning-of-line (- arg
))
13099 (setq pos
(move-marker (make-marker) (point)))
13100 (insert (delete-and-extract-region beg end
))
13102 (move-to-column col
)))
13104 ;; Paragraph filling stuff.
13105 ;; We want this to be just right, so use the full arsenal.
13106 ;; FIXME: This very likely does not work correctly for XEmacs, because the
13107 ;; filladapt package works slightly differently.
13109 (defun org-set-autofill-regexps ()
13111 ;; In the paragraph separator we include headlines, because filling
13112 ;; text in a line directly attached to a headline would otherwise
13113 ;; fill the headline as well.
13114 (set (make-local-variable 'paragraph-separate
) "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
13115 ;; The paragraph starter includes hand-formatted lists.
13116 (set (make-local-variable 'paragraph-start
)
13117 "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*]\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
13118 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
13119 ;; But only if the user has not turned off tables or fixed-width regions
13120 (set (make-local-variable 'auto-fill-inhibit-regexp
)
13122 (if (or org-enable-table-editor org-enable-fixed-width-editor
)
13125 (if org-enable-table-editor
"|" "")
13126 (if org-enable-fixed-width-editor
":" "")
13128 ;; We use our own fill-paragraph function, to make sure that tables
13129 ;; and fixed-width regions are not wrapped. That function will pass
13130 ;; through to `fill-paragraph' when appropriate.
13131 (set (make-local-variable 'fill-paragraph-function
) 'org-fill-paragraph
)
13132 ;; Adaptive filling: To get full control, first make sure that
13133 ;; `adaptive-fill-regexp' never matches. Then install our won matcher.
13134 (setq adaptive-fill-regexp
"\000")
13135 (setq adaptive-fill-function
'org-adaptive-fill-function
))
13137 (defun org-fill-paragraph (&optional justify
)
13138 "Re-align a table, pass through to fill-paragraph if no table."
13139 (let ((table-p (org-at-table-p))
13140 (table.el-p
(org-at-table.el-p
)))
13141 (cond ((equal (char-after (point-at-bol)) ?
*) t
) ; skip headlines
13142 (table.el-p t
) ; skip table.el tables
13143 (table-p (org-table-align) t
) ; align org-mode tables
13144 (t nil
)))) ; call paragraph-fill
13146 ;; For reference, this is the default value of adaptive-fill-regexp
13147 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
13149 (defun org-adaptive-fill-function ()
13150 "Return a fill prefix for org-mode files.
13151 In particular, this makes sure hanging paragraphs for hand-formatted lists
13153 (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
13154 (make-string (- (match-end 0) (match-beginning 0)) ?\
)))
13156 ;; Functions needed for Emacs/XEmacs region compatibility
13158 (defun org-add-hook (hook function
&optional append local
)
13159 "Add-hook, compatible with both Emacsen."
13160 (if (and local
(featurep 'xemacs
))
13161 (add-local-hook hook function append
)
13162 (add-hook hook function append local
)))
13164 (defun org-region-active-p ()
13165 "Is `transient-mark-mode' on and the region active?
13166 Works on both Emacs and XEmacs."
13167 (if org-ignore-region
13169 (if (featurep 'xemacs
)
13170 (and zmacs-regions
(region-active-p))
13171 (and transient-mark-mode mark-active
))))
13173 (defun org-add-to-invisibility-spec (arg)
13174 "Add elements to `buffer-invisibility-spec'.
13175 See documentation for `buffer-invisibility-spec' for the kind of elements
13176 that can be added."
13178 ((fboundp 'add-to-invisibility-spec
)
13179 (add-to-invisibility-spec arg
))
13180 ((or (null buffer-invisibility-spec
) (eq buffer-invisibility-spec t
))
13181 (setq buffer-invisibility-spec
(list arg
)))
13183 (setq buffer-invisibility-spec
13184 (cons arg buffer-invisibility-spec
)))))
13186 (defun org-remove-from-invisibility-spec (arg)
13187 "Remove elements from `buffer-invisibility-spec'."
13188 (if (fboundp 'remove-from-invisibility-spec
)
13189 (remove-from-invisibility-spec arg
)
13190 (if (consp buffer-invisibility-spec
)
13191 (setq buffer-invisibility-spec
13192 (delete arg buffer-invisibility-spec
)))))
13194 (defun org-in-invisibility-spec-p (arg)
13195 "Is ARG a member of `buffer-invisibility-spec'?"
13196 (if (consp buffer-invisibility-spec
)
13197 (member arg buffer-invisibility-spec
)
13200 (defun org-image-file-name-regexp ()
13201 "Return regexp matching the file names of images."
13202 (if (fboundp 'image-file-name-regexp
)
13203 (image-file-name-regexp)
13204 (let ((image-file-name-extensions
13205 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
13206 "xbm" "xpm" "pbm" "pgm" "ppm")))
13208 (regexp-opt (nconc (mapcar 'upcase
13209 image-file-name-extensions
)
13210 image-file-name-extensions
)
13214 ;; Functions needed for compatibility with old outline.el.
13216 ;; Programming for the old outline.el (that uses selective display
13217 ;; instead of `invisible' text properties) is a nightmare, mostly
13218 ;; because regular expressions can no longer be anchored at
13219 ;; beginning/end of line. Therefore a number of function need special
13220 ;; treatment when the old outline.el is being used.
13222 ;; The following functions capture almost the entire compatibility code
13223 ;; between the different versions of outline-mode. The only other
13224 ;; places where this is important are the font-lock-keywords, and in
13225 ;; `org-export-copy-visible'. Search for `org-noutline-p' to find them.
13227 ;; C-a should go to the beginning of a *visible* line, also in the
13228 ;; new outline.el. I guess this should be patched into Emacs?
13229 (defun org-beginning-of-line ()
13230 "Go to the beginning of the current line. If that is invisible, continue
13231 to a visible line beginning. This makes the function of C-a more intuitive."
13233 (beginning-of-line 1)
13237 (if (org-invisible-p)
13238 (while (and (not (bobp)) (org-invisible-p))
13240 (beginning-of-line 1))
13241 (forward-char 1))))
13243 (when org-noutline-p
13244 (define-key org-mode-map
"\C-a" 'org-beginning-of-line
))
13245 ;; FIXME: should I use substitute-key-definition to reach other bindings
13246 ;; of beginning-of-line?
13248 (defun org-invisible-p ()
13249 "Check if point is at a character currently not visible."
13251 ;; Early versions of noutline don't have `outline-invisible-p'.
13252 (if (fboundp 'outline-invisible-p
)
13253 (outline-invisible-p)
13254 (get-char-property (point) 'invisible
))
13256 (skip-chars-backward "^\r\n")
13257 (equal (char-before) ?
\r))))
13259 (defun org-invisible-p2 ()
13260 "Check if point is at a character currently not visible."
13264 (if (and (eolp) (not (bobp))) (backward-char 1))
13265 ;; Early versions of noutline don't have `outline-invisible-p'.
13266 (if (fboundp 'outline-invisible-p
)
13267 (outline-invisible-p)
13268 (get-char-property (point) 'invisible
)))
13269 (skip-chars-backward "^\r\n")
13270 (equal (char-before) ?
\r))))
13272 (defun org-back-to-heading (&optional invisible-ok
)
13273 "Move to previous heading line, or beg of this line if it's a heading.
13274 Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
13276 (outline-back-to-heading invisible-ok
)
13277 (if (and (memq (char-before) '(?
\n ?
\r))
13278 (looking-at outline-regexp
))
13280 (if (re-search-backward (concat (if invisible-ok
"\\([\r\n]\\|^\\)" "^")
13284 (progn (goto-char (match-end 1))
13285 (looking-at outline-regexp
)))
13286 (error "Before first heading")))))
13288 (defun org-on-heading-p (&optional invisible-ok
)
13289 "Return t if point is on a (visible) heading line.
13290 If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
13292 (outline-on-heading-p 'invisible-ok
)
13294 (skip-chars-backward "^\n\r")
13295 (and (looking-at outline-regexp
)
13298 (equal (char-before) ?
\n))))))
13300 (defun org-on-target-p ()
13301 (let ((pos (point)))
13303 (skip-chars-forward "<")
13304 (and (re-search-backward "<<" nil t
)
13305 (or (looking-at org-radio-target-regexp
)
13306 (looking-at org-target-regexp
))
13307 (<= (match-beginning 0) pos
)
13308 (>= (1+ (match-end 0)) pos
)))))
13310 (defun org-up-heading-all (arg)
13311 "Move to the heading line of which the present line is a subheading.
13312 This function considers both visible and invisible heading lines.
13313 With argument, move up ARG levels."
13315 (if (fboundp 'outline-up-heading-all
)
13316 (outline-up-heading-all arg
) ; emacs 21 version of outline.el
13317 (outline-up-heading arg t
)) ; emacs 22 version of outline.el
13318 (org-back-to-heading t
)
13319 (looking-at outline-regexp
)
13320 (if (<= (- (match-end 0) (match-beginning 0)) arg
)
13321 (error "Cannot move up %d levels" arg
)
13322 (re-search-backward
13323 (concat "[\n\r]" (regexp-quote
13324 (make-string (- (match-end 0) (match-beginning 0) arg
)
13327 (forward-char 1))))
13329 (defun org-show-hidden-entry ()
13330 "Show an entry where even the heading is hidden."
13332 (if (not org-noutline-p
)
13334 (org-back-to-heading t
)
13335 (org-flag-heading nil
)))
13338 (defun org-check-occur-regexp (regexp)
13339 "If REGEXP starts with \"^\", modify it to check for \\r as well.
13340 Of course, only for the old outline mode."
13343 (if (string-match "^\\^" regexp
)
13344 (concat "[\n\r]" (substring regexp
1))
13347 (defun org-flag-heading (flag &optional entry
)
13348 "Flag the current heading. FLAG non-nil means make invisible.
13349 When ENTRY is non-nil, show the entire entry."
13351 (org-back-to-heading t
)
13352 (if (not org-noutline-p
)
13353 ;; Make the current headline visible
13354 (outline-flag-region (max 1 (1- (point))) (point) (if flag ?
\r ?
\n)))
13355 ;; Check if we should show the entire entry
13359 (save-excursion ;; FIXME: Is this the fix for points in the -|
13360 ;; middle of text? |
13361 (and (outline-next-heading) ;; |
13362 (org-flag-heading nil
)))) ; show the next heading _|
13363 (outline-flag-region (max 1 (1- (point)))
13364 (save-excursion (outline-end-of-heading) (point))
13367 (if flag ?
\r ?
\n))))))
13369 (defun org-end-of-subtree (&optional invisible-OK
)
13370 ;; This is an exact copy of the original function, but it uses
13371 ;; `org-back-to-heading', to make it work also in invisible
13372 ;; trees. And is uses an invisible-OK argument.
13373 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
13374 (org-back-to-heading invisible-OK
)
13376 (level (funcall outline-level
)))
13377 (while (and (not (eobp))
13378 (or first
(> (funcall outline-level
) level
)))
13380 (outline-next-heading))
13381 (if (memq (preceding-char) '(?
\n ?\^M
))
13383 ;; Go to end of line before heading
13385 (if (memq (preceding-char) '(?
\n ?\^M
))
13386 ;; leave blank line before heading
13387 (forward-char -
1))))))
13389 (defun org-show-subtree ()
13390 "Show everything after this heading at deeper levels."
13391 (outline-flag-region
13394 (outline-end-of-subtree) (outline-next-heading) (point))
13395 (if org-noutline-p nil ?
\n)))
13397 (defun org-show-entry ()
13398 "Show the body directly following this heading.
13399 Show the heading too, if it is currently invisible."
13402 (org-back-to-heading t
)
13403 (outline-flag-region
13406 (re-search-forward (concat "[\r\n]\\(" outline-regexp
"\\)") nil
'move
)
13407 (or (match-beginning 1) (point-max)))
13408 (if org-noutline-p nil ?
\n))))
13411 (defun org-make-options-regexp (kwds)
13412 "Make a regular expression for keyword lines."
13414 (if org-noutline-p
"^" "[\n\r]")
13416 (mapconcat 'regexp-quote kwds
"\\|")
13418 (if org-noutline-p
"\\(.+\\)" "\\([^\n\r]+\\)")))
13420 ;; Make `bookmark-jump' show the jump location if it was hidden.
13421 (eval-after-load "bookmark"
13422 '(if (boundp 'bookmark-after-jump-hook
)
13423 ;; We can use the hook
13424 (add-hook 'bookmark-after-jump-hook
'org-bookmark-jump-unhide
)
13425 ;; Hook not available, use advice
13426 (defadvice bookmark-jump
(after org-make-visible activate
)
13427 "Make the position visible."
13428 (org-bookmark-jump-unhide))))
13430 (defun org-bookmark-jump-unhide ()
13431 "Unhide the current position, to show the bookmark location."
13432 (and (eq major-mode
'org-mode
)
13433 (or (org-invisible-p)
13434 (save-excursion (goto-char (max (point-min) (1- (point))))
13435 (org-invisible-p)))
13436 (org-show-hierarchy-above)))
13442 (run-hooks 'org-load-hook
)
13444 ;; Experimental code
13445 ;; FIXME: Move this code when it is ready.
13447 (defun org-upgrade-old-links (&optional query-description
)
13448 "Transfer old <...> style links to new [[...]] style links.
13449 With arg query-description, ask at each match for a description text to use
13451 (interactive (list (y-or-n-p "Would you like to be queried for a description at each link?")))
13453 (goto-char (point-min))
13454 (let ((re (concat "\\([^[]\\)<\\("
13455 "\\(" (mapconcat 'identity org-link-types
"\\|")
13457 "[^" org-non-link-chars
"]+\\)>"))
13459 (while (re-search-forward re nil t
)
13461 l1
(org-match-string-no-properties 2)
13462 l2
(save-match-data (org-link-escape l1
)))
13463 (when query-description
(setq l1
(read-string "Desc: " l1
)))
13465 (replace-match (concat (match-string 1) "[[" l1
"]]") t t
)
13466 (replace-match (concat (match-string 1) "[[" l2
"][" l1
"]]") t t
)))
13467 (message "%d matches have beed treated" cnt
))))
13469 (defun org-export-as-xml ()
13470 "Export current buffer as XOXO XML buffer."
13472 (cond ((eq org-export-xml-type
'xoxo
)
13473 (org-export-as-xoxo (current-buffer)))))
13475 (defun org-export-as-xoxo-insert-into (buffer &rest output
)
13476 (with-current-buffer buffer
13477 (apply 'insert output
)))
13479 (defun org-export-as-xoxo (&optional buffer
)
13480 "Export the org buffer as XOXO.
13481 The XOXO buffer is named *xoxo-<source buffer name>*"
13482 (interactive (list (current-buffer)))
13483 ;; A quickie abstraction
13485 ;; Output everything as XOXO
13486 (with-current-buffer (get-buffer buffer
)
13487 (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
13488 (let* ((filename (concat (file-name-sans-extension buffer-file-name
)
13490 (out (find-file-noselect filename
))
13493 ;; Check the output buffer is empty.
13494 (with-current-buffer out
(erase-buffer))
13495 ;; Kick off the output
13496 (org-export-as-xoxo-insert-into out
"<ol class='xoxo'>\n")
13497 (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't
)
13498 (let* ((hd (match-string-no-properties 1))
13499 (level (length hd
))
13501 (match-string-no-properties 2)
13503 (goto-char (match-end 0))
13508 (if (looking-at "^[ \t]\\(.*\\)")
13509 (setq str
(concat str
(match-string-no-properties 1)))
13510 (throw 'loop str
)))))))))
13512 ;; Handle level rendering
13514 ((> level last-level
)
13515 (org-export-as-xoxo-insert-into out
"\n<ol>\n"))
13517 ((< level last-level
)
13518 (dotimes (- (- last-level level
) 1)
13520 (org-export-as-xoxo-insert-into out
"</li>\n"))
13521 (org-export-as-xoxo-insert-into out
"</ol>\n"))
13523 (org-export-as-xoxo-insert-into out
"</li>\n")
13524 (setq hanging-li nil
)))
13526 ((equal level last-level
)
13528 (org-export-as-xoxo-insert-into out
"</li>\n")))
13531 (setq last-level level
)
13533 ;; And output the new li
13534 (setq hanging-li
't
)
13535 (if (equal ?
+ (elt text
0))
13536 (org-export-as-xoxo-insert-into out
"<li class='" (substring text
1) "'>")
13537 (org-export-as-xoxo-insert-into out
"<li>" text
))))
13539 ;; Finally finish off the ol
13540 (dotimes (- last-level
1)
13542 (org-export-as-xoxo-insert-into out
"</li>\n"))
13543 (org-export-as-xoxo-insert-into out
"</ol>\n"))
13545 ;; Finish the buffer off and clean it up.
13546 (switch-to-buffer-other-window out
)
13547 (indent-region (point-min) (point-max))
13549 (goto-char (point-min))
13552 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
13553 ;;; org.el ends here