Added support for habit consistency tracking
[org-mode.git] / lisp / org-agenda.el
blob8f5a533df4fa5a76c59ebb808ea1dd9527ee161a
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 6.31trans
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file contains the code for creating and using the Agenda for Org-mode.
31 ;;; Code:
33 (require 'org)
34 (eval-when-compile
35 (require 'cl)
36 (require 'calendar))
38 (declare-function diary-add-to-list "diary-lib"
39 (date string specifier &optional marker globcolor literal))
40 (declare-function calendar-absolute-from-iso "cal-iso" (date))
41 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
42 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
43 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
44 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
45 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
46 (declare-function calendar-french-date-string "cal-french" (&optional date))
47 (declare-function calendar-goto-date "cal-move" (date))
48 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
49 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
50 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
51 (declare-function calendar-iso-from-absolute "cal-iso" (date))
52 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
53 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
54 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
55 (declare-function org-columns-quit "org-colview" ())
56 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
57 (defvar calendar-mode-map)
58 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
60 ;; Defined somewhere in this file, but used before definition.
61 (defvar org-agenda-buffer-name)
62 (defvar org-agenda-overriding-header)
63 (defvar org-agenda-title-append nil)
64 (defvar entry)
65 (defvar date)
66 (defvar org-agenda-undo-list)
67 (defvar org-agenda-pending-undo-list)
68 (defvar original-date) ; dynamically scoped, calendar.el does scope this
70 (defcustom org-agenda-confirm-kill 1
71 "When set, remote killing from the agenda buffer needs confirmation.
72 When t, a confirmation is always needed. When a number N, confirmation is
73 only needed when the text to be killed contains more than N non-white lines."
74 :group 'org-agenda
75 :type '(choice
76 (const :tag "Never" nil)
77 (const :tag "Always" t)
78 (integer :tag "When more than N lines")))
80 (defcustom org-agenda-compact-blocks nil
81 "Non-nil means, make the block agenda more compact.
82 This is done by leaving out unnecessary lines."
83 :group 'org-agenda
84 :type 'boolean)
86 (defcustom org-agenda-block-separator ?=
87 "The separator between blocks in the agenda.
88 If this is a string, it will be used as the separator, with a newline added.
89 If it is a character, it will be repeated to fill the window width."
90 :group 'org-agenda
91 :type '(choice
92 (character)
93 (string)))
95 (defgroup org-agenda-export nil
96 "Options concerning exporting agenda views in Org-mode."
97 :tag "Org Agenda Export"
98 :group 'org-agenda)
100 (defcustom org-agenda-with-colors t
101 "Non-nil means, use colors in agenda views."
102 :group 'org-agenda-export
103 :type 'boolean)
105 (defcustom org-agenda-exporter-settings nil
106 "Alist of variable/value pairs that should be active during agenda export.
107 This is a good place to set options for ps-print and for htmlize.
108 Note that the way this is implemented, the values will be evaluated
109 before assigned to the variables. So make sure to quote values you do
110 *not* want evaluated, for example
112 (setq org-agenda-exporter-settings
113 '((ps-print-color-p 'black-white)))"
114 :group 'org-agenda-export
115 :type '(repeat
116 (list
117 (variable)
118 (sexp :tag "Value"))))
120 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
121 "Hook run in temporary buffer before writing it to an export file.
122 A useful function is `org-agenda-add-entry-text'."
123 :group 'org-agenda-export
124 :type 'hook
125 :options '(org-agenda-add-entry-text))
127 (defcustom org-agenda-add-entry-text-maxlines 0
128 "Maximum number of entry text lines to be added to agenda.
129 This is only relevant when `org-agenda-add-entry-text' is part of
130 `org-agenda-before-write-hook', which it is by default.
131 When this is 0, nothing will happen. When it is greater than 0, it
132 specifies the maximum number of lines that will be added for each entry
133 that is listed in the agenda view.
135 Note that this variable is not used during display, only when exporting
136 the agenda. For agenda display, see org-agenda-entry-text-mode and the
137 variable `org-agenda-entry-text-maxlines'."
138 :group 'org-agenda
139 :type 'integer)
141 (defcustom org-agenda-add-entry-text-descriptive-links t
142 "Non-nil means, export org-links as descriptive links in agenda added text.
143 This variable applies to the text added to the agenda when
144 `org-agenda-add-entry-text-maxlines' is larger than 0.
145 When this variable nil, the URL will (also) be shown."
146 :group 'org-agenda
147 :type 'boolean)
149 (defcustom org-agenda-export-html-style ""
150 "The style specification for exported HTML Agenda files.
151 If this variable contains a string, it will replace the default <style>
152 section as produced by `htmlize'.
153 Since there are different ways of setting style information, this variable
154 needs to contain the full HTML structure to provide a style, including the
155 surrounding HTML tags. The style specifications should include definitions
156 the fonts used by the agenda, here is an example:
158 <style type=\"text/css\">
159 p { font-weight: normal; color: gray; }
160 .org-agenda-structure {
161 font-size: 110%;
162 color: #003399;
163 font-weight: 600;
165 .org-todo {
166 color: #cc6666;
167 font-weight: bold;
169 .org-agenda-done {
170 color: #339933;
172 .org-done {
173 color: #339933;
175 .title { text-align: center; }
176 .todo, .deadline { color: red; }
177 .done { color: green; }
178 </style>
180 or, if you want to keep the style in a file,
182 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
184 As the value of this option simply gets inserted into the HTML <head> header,
185 you can \"misuse\" it to also add other text to the header. However,
186 <style>...</style> is required, if not present the variable will be ignored."
187 :group 'org-agenda-export
188 :group 'org-export-html
189 :type 'string)
191 (defgroup org-agenda-custom-commands nil
192 "Options concerning agenda views in Org-mode."
193 :tag "Org Agenda Custom Commands"
194 :group 'org-agenda)
196 (defconst org-sorting-choice
197 '(choice
198 (const time-up) (const time-down)
199 (const category-keep) (const category-up) (const category-down)
200 (const tag-down) (const tag-up)
201 (const priority-up) (const priority-down)
202 (const todo-state-up) (const todo-state-down)
203 (const effort-up) (const effort-down)
204 (const habit-up) (const habit-down)
205 (const user-defined-up) (const user-defined-down))
206 "Sorting choices.")
208 (defconst org-agenda-custom-commands-local-options
209 `(repeat :tag "Local settings for this command. Remember to quote values"
210 (choice :tag "Setting"
211 (list :tag "Heading for this block"
212 (const org-agenda-overriding-header)
213 (string :tag "Headline"))
214 (list :tag "Files to be searched"
215 (const org-agenda-files)
216 (list
217 (const :format "" quote)
218 (repeat (file))))
219 (list :tag "Sorting strategy"
220 (const org-agenda-sorting-strategy)
221 (list
222 (const :format "" quote)
223 (repeat
224 ,org-sorting-choice)))
225 (list :tag "Prefix format"
226 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
227 (string))
228 (list :tag "Number of days in agenda"
229 (const org-agenda-ndays)
230 (integer :value 1))
231 (list :tag "Fixed starting date"
232 (const org-agenda-start-day)
233 (string :value "2007-11-01"))
234 (list :tag "Start on day of week"
235 (const org-agenda-start-on-weekday)
236 (choice :value 1
237 (const :tag "Today" nil)
238 (integer :tag "Weekday No.")))
239 (list :tag "Include data from diary"
240 (const org-agenda-include-diary)
241 (boolean))
242 (list :tag "Deadline Warning days"
243 (const org-deadline-warning-days)
244 (integer :value 1))
245 (list :tag "Tags filter preset"
246 (const org-agenda-filter-preset)
247 (list
248 (const :format "" quote)
249 (repeat
250 (string :tag "+tag or -tag"))))
251 (list :tag "Standard skipping condition"
252 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
253 (const org-agenda-skip-function)
254 (list
255 (const :format "" quote)
256 (list
257 (choice
258 :tag "Skipping range"
259 (const :tag "Skip entry" org-agenda-skip-entry-if)
260 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
261 (repeat :inline t :tag "Conditions for skipping"
262 (choice
263 :tag "Condition type"
264 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
265 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
266 (const :tag "scheduled" 'scheduled)
267 (const :tag "not scheduled" 'notscheduled)
268 (const :tag "deadline" 'deadline)
269 (const :tag "no deadline" 'notdeadline)
270 (const :tag "timestamp" 'timestamp)
271 (const :tag "no timestamp" 'nottimestamp))))))
272 (list :tag "Non-standard skipping condition"
273 :value (org-agenda-skip-function)
274 (const org-agenda-skip-function)
275 (sexp :tag "Function or form (quoted!)"))
276 (list :tag "Any variable"
277 (variable :tag "Variable")
278 (sexp :tag "Value (sexp)"))))
279 "Selection of examples for agenda command settings.
280 This will be spliced into the custom type of
281 `org-agenda-custom-commands'.")
284 (defcustom org-agenda-custom-commands nil
285 "Custom commands for the agenda.
286 These commands will be offered on the splash screen displayed by the
287 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
289 (key desc type match settings files)
291 key The key (one or more characters as a string) to be associated
292 with the command.
293 desc A description of the command, when omitted or nil, a default
294 description is built using MATCH.
295 type The command type, any of the following symbols:
296 agenda The daily/weekly agenda.
297 todo Entries with a specific TODO keyword, in all agenda files.
298 search Entries containing search words entry or headline.
299 tags Tags/Property/TODO match in all agenda files.
300 tags-todo Tags/P/T match in all agenda files, TODO entries only.
301 todo-tree Sparse tree of specific TODO keyword in *current* file.
302 tags-tree Sparse tree with all tags matches in *current* file.
303 occur-tree Occur sparse tree for *current* file.
304 ... A user-defined function.
305 match What to search for:
306 - a single keyword for TODO keyword searches
307 - a tags match expression for tags searches
308 - a word search expression for text searches.
309 - a regular expression for occur searches
310 For all other commands, this should be the empty string.
311 settings A list of option settings, similar to that in a let form, so like
312 this: ((opt1 val1) (opt2 val2) ...). The values will be
313 evaluated at the moment of execution, so quote them when needed.
314 files A list of files file to write the produced agenda buffer to
315 with the command `org-store-agenda-views'.
316 If a file name ends in \".html\", an HTML version of the buffer
317 is written out. If it ends in \".ps\", a postscript version is
318 produced. Otherwise, only the plain text is written to the file.
320 You can also define a set of commands, to create a composite agenda buffer.
321 In this case, an entry looks like this:
323 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
325 where
327 desc A description string to be displayed in the dispatcher menu.
328 cmd An agenda command, similar to the above. However, tree commands
329 are no allowed, but instead you can get agenda and global todo list.
330 So valid commands for a set are:
331 (agenda \"\" settings)
332 (alltodo \"\" settings)
333 (stuck \"\" settings)
334 (todo \"match\" settings files)
335 (search \"match\" settings files)
336 (tags \"match\" settings files)
337 (tags-todo \"match\" settings files)
339 Each command can carry a list of options, and another set of options can be
340 given for the whole set of commands. Individual command options take
341 precedence over the general options.
343 When using several characters as key to a command, the first characters
344 are prefix commands. For the dispatcher to display useful information, you
345 should provide a description for the prefix, like
347 (setq org-agenda-custom-commands
348 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
349 (\"hl\" tags \"+HOME+Lisa\")
350 (\"hp\" tags \"+HOME+Peter\")
351 (\"hk\" tags \"+HOME+Kim\")))"
352 :group 'org-agenda-custom-commands
353 :type `(repeat
354 (choice :value ("x" "Describe command here" tags "" nil)
355 (list :tag "Single command"
356 (string :tag "Access Key(s) ")
357 (option (string :tag "Description"))
358 (choice
359 (const :tag "Agenda" agenda)
360 (const :tag "TODO list" alltodo)
361 (const :tag "Search words" search)
362 (const :tag "Stuck projects" stuck)
363 (const :tag "Tags/Property match (all agenda files)" tags)
364 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
365 (const :tag "TODO keyword search (all agenda files)" todo)
366 (const :tag "Tags sparse tree (current buffer)" tags-tree)
367 (const :tag "TODO keyword tree (current buffer)" todo-tree)
368 (const :tag "Occur tree (current buffer)" occur-tree)
369 (sexp :tag "Other, user-defined function"))
370 (string :tag "Match (only for some commands)")
371 ,org-agenda-custom-commands-local-options
372 (option (repeat :tag "Export" (file :tag "Export to"))))
373 (list :tag "Command series, all agenda files"
374 (string :tag "Access Key(s)")
375 (string :tag "Description ")
376 (repeat :tag "Component"
377 (choice
378 (list :tag "Agenda"
379 (const :format "" agenda)
380 (const :tag "" :format "" "")
381 ,org-agenda-custom-commands-local-options)
382 (list :tag "TODO list (all keywords)"
383 (const :format "" alltodo)
384 (const :tag "" :format "" "")
385 ,org-agenda-custom-commands-local-options)
386 (list :tag "Search words"
387 (const :format "" search)
388 (string :tag "Match")
389 ,org-agenda-custom-commands-local-options)
390 (list :tag "Stuck projects"
391 (const :format "" stuck)
392 (const :tag "" :format "" "")
393 ,org-agenda-custom-commands-local-options)
394 (list :tag "Tags search"
395 (const :format "" tags)
396 (string :tag "Match")
397 ,org-agenda-custom-commands-local-options)
398 (list :tag "Tags search, TODO entries only"
399 (const :format "" tags-todo)
400 (string :tag "Match")
401 ,org-agenda-custom-commands-local-options)
402 (list :tag "TODO keyword search"
403 (const :format "" todo)
404 (string :tag "Match")
405 ,org-agenda-custom-commands-local-options)
406 (list :tag "Other, user-defined function"
407 (symbol :tag "function")
408 (string :tag "Match")
409 ,org-agenda-custom-commands-local-options)))
411 (repeat :tag "Settings for entire command set"
412 (list (variable :tag "Any variable")
413 (sexp :tag "Value")))
414 (option (repeat :tag "Export" (file :tag "Export to"))))
415 (cons :tag "Prefix key documentation"
416 (string :tag "Access Key(s)")
417 (string :tag "Description ")))))
419 (defcustom org-agenda-query-register ?o
420 "The register holding the current query string.
421 The purpose of this is that if you construct a query string interactively,
422 you can then use it to define a custom command."
423 :group 'org-agenda-custom-commands
424 :type 'character)
426 (defcustom org-stuck-projects
427 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
428 "How to identify stuck projects.
429 This is a list of four items:
430 1. A tags/todo/property matcher string that is used to identify a project.
431 See the manual for a description of tag and property searches.
432 The entire tree below a headline matched by this is considered one project.
433 2. A list of TODO keywords identifying non-stuck projects.
434 If the project subtree contains any headline with one of these todo
435 keywords, the project is considered to be not stuck. If you specify
436 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
437 3. A list of tags identifying non-stuck projects.
438 If the project subtree contains any headline with one of these tags,
439 the project is considered to be not stuck. If you specify \"*\" as
440 a tag, any tag will mark the project unstuck. Note that this is about
441 the explicit presence of a tag somewhere in the subtree, inherited
442 tags to not count here. If inherited tags make a project not stuck,
443 use \"-TAG\" in the tags part of the matcher under (1.) above.
444 4. An arbitrary regular expression matching non-stuck projects.
446 If the project turns out to be not stuck, search continues also in the
447 subtree to see if any of the subtasks have project status.
449 See also the variable `org-tags-match-list-sublevels' which applies
450 to projects matched by this search as well.
452 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
453 or `C-c a #' to produce the list."
454 :group 'org-agenda-custom-commands
455 :type '(list
456 (string :tag "Tags/TODO match to identify a project")
457 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
458 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
459 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
461 (defcustom org-agenda-filter-effort-default-operator "<"
462 "The default operator for effort estimate filtering.
463 If you select an effort estimate limit without first pressing an operator,
464 this one will be used."
465 :group 'org-agenda-custom-commands
466 :type '(choice (const :tag "less or equal" "<")
467 (const :tag "greater or equal"">")
468 (const :tag "equal" "=")))
470 (defgroup org-agenda-skip nil
471 "Options concerning skipping parts of agenda files."
472 :tag "Org Agenda Skip"
473 :group 'org-agenda)
474 (defgroup org-agenda-daily/weekly nil
475 "Options concerning the daily/weekly agenda."
476 :tag "Org Agenda Daily/Weekly"
477 :group 'org-agenda)
478 (defgroup org-agenda-todo-list nil
479 "Options concerning the global todo list agenda view."
480 :tag "Org Agenda Todo List"
481 :group 'org-agenda)
482 (defgroup org-agenda-match-view nil
483 "Options concerning the general tags/property/todo match agenda view."
484 :tag "Org Agenda Match View"
485 :group 'org-agenda)
486 (defgroup org-agenda-search-view nil
487 "Options concerning the general tags/property/todo match agenda view."
488 :tag "Org Agenda Match View"
489 :group 'org-agenda)
491 (defvar org-agenda-archives-mode nil
492 "Non-nil means, the agenda will include archived items.
493 If this is the symbol `trees', trees in the selected agenda scope
494 that are marked with the ARCHIVE tag will be included anyway. When this is
495 t, also all archive files associated with the current selection of agenda
496 files will be included.")
498 (defcustom org-agenda-skip-comment-trees t
499 "Non-nil means, skip trees that start with the COMMENT keyword.
500 When nil, these trees are also scanned by agenda commands."
501 :group 'org-agenda-skip
502 :type 'boolean)
504 (defcustom org-agenda-todo-list-sublevels t
505 "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
506 When nil, the sublevels of a TODO entry are not checked, resulting in
507 potentially much shorter TODO lists."
508 :group 'org-agenda-skip
509 :group 'org-agenda-todo-list
510 :type 'boolean)
512 (defcustom org-agenda-todo-ignore-with-date nil
513 "Non-nil means, don't show entries with a date in the global todo list.
514 You can use this if you prefer to mark mere appointments with a TODO keyword,
515 but don't want them to show up in the TODO list.
516 When this is set, it also covers deadlines and scheduled items, the settings
517 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
518 will be ignored.
519 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
520 :group 'org-agenda-skip
521 :group 'org-agenda-todo-list
522 :type 'boolean)
524 (defcustom org-agenda-todo-ignore-scheduled nil
525 "Non-nil means, don't show scheduled entries in the global todo list.
526 The idea behind this is that by scheduling it, you have already taken care
527 of this item.
528 See also `org-agenda-todo-ignore-with-date'.
529 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
530 :group 'org-agenda-skip
531 :group 'org-agenda-todo-list
532 :type 'boolean)
534 (defcustom org-agenda-todo-ignore-deadlines nil
535 "Non-nil means, don't show near deadline entries in the global todo list.
536 Near means closer than `org-deadline-warning-days' days.
537 The idea behind this is that such items will appear in the agenda anyway.
538 See also `org-agenda-todo-ignore-with-date'.
539 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
540 :group 'org-agenda-skip
541 :group 'org-agenda-todo-list
542 :type 'boolean)
544 (defcustom org-agenda-tags-todo-honor-ignore-options nil
545 "Non-nil means, honor todo-list ...ignore options also in tags-todo search.
546 The variables
547 `org-agenda-todo-ignore-with-date',
548 `org-agenda-todo-ignore-scheduled'
549 `org-agenda-todo-ignore-deadlines'
550 make the global TODO list skip entries that have time stamps of certain
551 kinds. If this option is set, the same options will also apply for the
552 tags-todo search, which is the general tags/property matcher
553 restricted to unfinished TODO entries only."
554 :group 'org-agenda-skip
555 :group 'org-agenda-todo-list
556 :group 'org-agenda-match-view
557 :type 'boolean)
559 (defcustom org-agenda-skip-scheduled-if-done nil
560 "Non-nil means don't show scheduled items in agenda when they are done.
561 This is relevant for the daily/weekly agenda, not for the TODO list. And
562 it applies only to the actual date of the scheduling. Warnings about
563 an item with a past scheduling dates are always turned off when the item
564 is DONE."
565 :group 'org-agenda-skip
566 :group 'org-agenda-daily/weekly
567 :type 'boolean)
569 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
570 "Non-nil means skip scheduling line if same entry shows because of deadline.
571 In the agenda of today, an entry can show up multiple times because
572 it is both scheduled and has a nearby deadline, and maybe a plain time
573 stamp as well.
574 When this variable is t, then only the deadline is shown and the fact that
575 the entry is scheduled today or was scheduled previously is not shown.
576 When this variable is nil, the entry will be shown several times. When
577 the variable is the symbol `not-today', then skip scheduled previously,
578 but not scheduled today."
579 :group 'org-agenda-skip
580 :group 'org-agenda-daily/weekly
581 :type '(choice
582 (const :tag "Never" nil)
583 (const :tag "Always" t)
584 (const :tag "Not when scheduled today" not-today)))
586 (defcustom org-agenda-skip-deadline-if-done nil
587 "Non-nil means don't show deadlines when the corresponding item is done.
588 When nil, the deadline is still shown and should give you a happy feeling.
589 This is relevant for the daily/weekly agenda. And it applied only to the
590 actually date of the deadline. Warnings about approaching and past-due
591 deadlines are always turned off when the item is DONE."
592 :group 'org-agenda-skip
593 :group 'org-agenda-daily/weekly
594 :type 'boolean)
596 (defcustom org-agenda-skip-additional-timestamps-same-entry t
597 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
598 When non-nil, after the search for timestamps has matched once in an
599 entry, the rest of the entry will not be searched."
600 :group 'org-agenda-skip
601 :type 'boolean)
603 (defcustom org-agenda-skip-timestamp-if-done nil
604 "Non-nil means don't select item by timestamp or -range if it is DONE."
605 :group 'org-agenda-skip
606 :group 'org-agenda-daily/weekly
607 :type 'boolean)
609 (defcustom org-agenda-dim-blocked-tasks t
610 "Non-nil means, dim blocked tasks in the agenda display.
611 This causes some overhead during agenda construction, but if you
612 have turned on `org-enforce-todo-dependencies',
613 `org-enforce-todo-checkbox-dependencies', or any other blocking
614 mechanism, this will create useful feedback in the agenda.
616 Instead ot t, this variable can also have the value `invisible'.
617 Then blocked tasks will be invisible and only become visible when
618 they become unblocked. An exemption to this behavior is when a task is
619 blocked because of unchecked checkboxes below it. Since checkboxes do
620 not show up in the agenda views, making this task invisible you remove any
621 trace from agenda views that there is something to do. Therefore, a task
622 that is blocked because of checkboxes will never be made invisible, it
623 will only be dimmed."
624 :group 'org-agenda-daily/weekly
625 :group 'org-agenda-todo-list
626 :type '(choice
627 (const :tag "Do not dim" nil)
628 (const :tag "Dim to a grey face" t)
629 (const :tag "Make invisibe" invisible)))
631 (defcustom org-timeline-show-empty-dates 3
632 "Non-nil means, `org-timeline' also shows dates without an entry.
633 When nil, only the days which actually have entries are shown.
634 When t, all days between the first and the last date are shown.
635 When an integer, show also empty dates, but if there is a gap of more than
636 N days, just insert a special line indicating the size of the gap."
637 :group 'org-agenda-skip
638 :type '(choice
639 (const :tag "None" nil)
640 (const :tag "All" t)
641 (integer :tag "at most")))
643 (defgroup org-agenda-startup nil
644 "Options concerning initial settings in the Agenda in Org Mode."
645 :tag "Org Agenda Startup"
646 :group 'org-agenda)
648 (defcustom org-finalize-agenda-hook nil
649 "Hook run just before displaying an agenda buffer."
650 :group 'org-agenda-startup
651 :type 'hook)
653 (defcustom org-agenda-mouse-1-follows-link nil
654 "Non-nil means, mouse-1 on a link will follow the link in the agenda.
655 A longer mouse click will still set point. Does not work on XEmacs.
656 Needs to be set before org.el is loaded."
657 :group 'org-agenda-startup
658 :type 'boolean)
660 (defcustom org-agenda-start-with-follow-mode nil
661 "The initial value of follow-mode in a newly created agenda window."
662 :group 'org-agenda-startup
663 :type 'boolean)
665 (defcustom org-agenda-start-with-entry-text-mode nil
666 "The initial value of entry-text-mode in a newly created agenda window."
667 :group 'org-agenda-startup
668 :type 'boolean)
670 (defcustom org-agenda-entry-text-maxlines 5
671 "Number of text lines to be added when `E' is presed in the agenda.
673 Note that this variable only used during agenda display. Add add entry text
674 when exporting the agenda, configure the variable
675 `org-agenda-add-entry-ext-maxlines'."
676 :group 'org-agenda
677 :type 'integer)
679 (defcustom org-agenda-entry-text-exclude-regexps nil
680 "List of regular expressions to clean up entry text.
681 The complete matches of all regular expressions in this list will be
682 removed from entry text before it is shown in the agenda."
683 :group 'org-agenda
684 :type '(repeat (regexp)))
686 (defvar org-agenda-entry-text-cleanup-hook nil
687 "Hook that is run after basic cleanup of entry text to be shown in agenda.
688 This cleanup is done in a temporary buffer, so the function may inspect and
689 change the entire buffer.
690 Some default stuff like drawers and scheduling/deadline dates will already
691 have been removed when this is called, as will any matches for regular
692 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
694 (defvar org-agenda-include-inactive-timestamps nil
695 "Non-nil means, include inactive time stamps in agenda and timeline.")
697 (defgroup org-agenda-windows nil
698 "Options concerning the windows used by the Agenda in Org Mode."
699 :tag "Org Agenda Windows"
700 :group 'org-agenda)
702 (defcustom org-agenda-window-setup 'reorganize-frame
703 "How the agenda buffer should be displayed.
704 Possible values for this option are:
706 current-window Show agenda in the current window, keeping all other windows.
707 other-window Use `switch-to-buffer-other-window' to display agenda.
708 reorganize-frame Show only two windows on the current frame, the current
709 window and the agenda.
710 other-frame Use `switch-to-buffer-other-frame' to display agenda.
711 Also, when exiting the agenda, kill that frame.
712 See also the variable `org-agenda-restore-windows-after-quit'."
713 :group 'org-agenda-windows
714 :type '(choice
715 (const current-window)
716 (const other-frame)
717 (const other-window)
718 (const reorganize-frame)))
720 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
721 "The min and max height of the agenda window as a fraction of frame height.
722 The value of the variable is a cons cell with two numbers between 0 and 1.
723 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
724 :group 'org-agenda-windows
725 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
727 (defcustom org-agenda-restore-windows-after-quit nil
728 "Non-nil means, restore window configuration open exiting agenda.
729 Before the window configuration is changed for displaying the agenda,
730 the current status is recorded. When the agenda is exited with
731 `q' or `x' and this option is set, the old state is restored. If
732 `org-agenda-window-setup' is `other-frame', the value of this
733 option will be ignored."
734 :group 'org-agenda-windows
735 :type 'boolean)
737 (defcustom org-agenda-ndays 7
738 "Number of days to include in overview display.
739 Should be 1 or 7.
740 Custom commands can set this variable in the options section."
741 :group 'org-agenda-daily/weekly
742 :type 'integer)
744 (defcustom org-agenda-start-on-weekday 1
745 "Non-nil means, start the overview always on the specified weekday.
746 0 denotes Sunday, 1 denotes Monday etc.
747 When nil, always start on the current day.
748 Custom commands can set this variable in the options section."
749 :group 'org-agenda-daily/weekly
750 :type '(choice (const :tag "Today" nil)
751 (integer :tag "Weekday No.")))
753 (defcustom org-agenda-show-all-dates t
754 "Non-nil means, `org-agenda' shows every day in the selected range.
755 When nil, only the days which actually have entries are shown."
756 :group 'org-agenda-daily/weekly
757 :type 'boolean)
759 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
760 "Format string for displaying dates in the agenda.
761 Used by the daily/weekly agenda and by the timeline. This should be
762 a format string understood by `format-time-string', or a function returning
763 the formatted date as a string. The function must take a single argument,
764 a calendar-style date list like (month day year)."
765 :group 'org-agenda-daily/weekly
766 :type '(choice
767 (string :tag "Format string")
768 (function :tag "Function")))
770 (defun org-agenda-format-date-aligned (date)
771 "Format a date string for display in the daily/weekly agenda, or timeline.
772 This function makes sure that dates are aligned for easy reading."
773 (require 'cal-iso)
774 (let* ((dayname (calendar-day-name date))
775 (day (cadr date))
776 (day-of-week (calendar-day-of-week date))
777 (month (car date))
778 (monthname (calendar-month-name month))
779 (year (nth 2 date))
780 (iso-week (org-days-to-iso-week
781 (calendar-absolute-from-gregorian date)))
782 (weekyear (cond ((and (= month 1) (>= iso-week 52))
783 (1- year))
784 ((and (= month 12) (<= iso-week 1))
785 (1+ year))
786 (t year)))
787 (weekstring (if (= day-of-week 1)
788 (format " W%02d" iso-week)
789 "")))
790 (format "%-10s %2d %s %4d%s"
791 dayname day monthname year weekstring)))
793 (defcustom org-agenda-weekend-days '(6 0)
794 "Which days are weekend?
795 These days get the special face `org-agenda-date-weekend' in the agenda
796 and timeline buffers."
797 :group 'org-agenda-daily/weekly
798 :type '(set :greedy t
799 (const :tag "Monday" 1)
800 (const :tag "Tuesday" 2)
801 (const :tag "Wednesday" 3)
802 (const :tag "Thursday" 4)
803 (const :tag "Friday" 5)
804 (const :tag "Saturday" 6)
805 (const :tag "Sunday" 0)))
807 (defcustom org-agenda-include-diary nil
808 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
809 Custom commands can set this variable in the options section."
810 :group 'org-agenda-daily/weekly
811 :type 'boolean)
813 (defcustom org-agenda-include-all-todo nil
814 "Set means weekly/daily agenda will always contain all TODO entries.
815 The TODO entries will be listed at the top of the agenda, before
816 the entries for specific days.
817 This option is deprecated, it is better to define a block agenda instead."
818 :group 'org-agenda-daily/weekly
819 :type 'boolean)
821 (defcustom org-agenda-repeating-timestamp-show-all t
822 "Non-nil means, show all occurrences of a repeating stamp in the agenda.
823 When nil, only one occurrence is shown, either today or the
824 nearest into the future."
825 :group 'org-agenda-daily/weekly
826 :type 'boolean)
828 (defcustom org-scheduled-past-days 10000
829 "No. of days to continue listing scheduled items that are not marked DONE.
830 When an item is scheduled on a date, it shows up in the agenda on this
831 day and will be listed until it is marked done for the number of days
832 given here."
833 :group 'org-agenda-daily/weekly
834 :type 'integer)
836 (defcustom org-agenda-log-mode-items '(closed clock)
837 "List of items that should be shown in agenda log mode.
838 This list may contain the following symbols:
840 closed Show entries that have been closed on that day.
841 clock Show entries that have received clocked time on that day.
842 state Show all logged state changes.
843 Note that instead of changing this variable, you can also press `C-u l' in
844 the agenda to display all available LOG items temporarily."
845 :group 'org-agenda-daily/weekly
846 :type '(set :greedy t (const closed) (const clock) (const state)))
848 (defcustom org-agenda-log-mode-add-notes t
849 "Non-nil means, add first line of notes to log entries in agenda views.
850 If a log item like a state change or a clock entry is associated with
851 notes, the first line of these notes will be added to the entry in the
852 agenda display."
853 :group 'org-agenda-daily/weekly
854 :type 'boolean)
856 (defcustom org-agenda-start-with-log-mode nil
857 "The initial value of log-mode in a newly created agenda window."
858 :group 'org-agenda-startup
859 :group 'org-agenda-daily/weekly
860 :type 'boolean)
862 (defcustom org-agenda-start-with-clockreport-mode nil
863 "The initial value of clockreport-mode in a newly created agenda window."
864 :group 'org-agenda-startup
865 :group 'org-agenda-daily/weekly
866 :type 'boolean)
868 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
869 "Property list with parameters for the clocktable in clockreport mode.
870 This is the display mode that shows a clock table in the daily/weekly
871 agenda, the properties for this dynamic block can be set here.
872 The usual clocktable parameters are allowed here, but you cannot set
873 the properties :name, :tstart, :tend, :block, and :scope - these will
874 be overwritten to make sure the content accurately reflects the
875 current display in the agenda."
876 :group 'org-agenda-daily/weekly
877 :type 'plist)
879 (defcustom org-agenda-search-view-search-words-only nil
880 "Non-nil means, the search string is interpreted as individual words
881 The search then looks for each word separately in each entry and
882 selects entries that have matches for all words.
883 When nil, matching as loose words will only take place if the first
884 word is preceded by + or -. If that is not the case, the search
885 string will just be matched as a substring in the entry, but with
886 each space character allowing for any whitespace, including newlines."
887 :group 'org-agenda-search-view
888 :type 'boolean)
890 (defgroup org-agenda-time-grid nil
891 "Options concerning the time grid in the Org-mode Agenda."
892 :tag "Org Agenda Time Grid"
893 :group 'org-agenda)
895 (defcustom org-agenda-search-headline-for-time t
896 "Non-nil means, search headline for a time-of-day.
897 If the headline contains a time-of-day in one format or another, it will
898 be used to sort the entry into the time sequence of items for a day.
899 Some people have time stamps in the headline that refer to the creation
900 time or so, and then this produces an unwanted side effect. If this is
901 the case for your, use this variable to turn off searching the headline
902 for a time."
903 :group 'org-agenda-time-grid
904 :type 'boolean)
906 (defcustom org-agenda-use-time-grid t
907 "Non-nil means, show a time grid in the agenda schedule.
908 A time grid is a set of lines for specific times (like every two hours between
909 8:00 and 20:00). The items scheduled for a day at specific times are
910 sorted in between these lines.
911 For details about when the grid will be shown, and what it will look like, see
912 the variable `org-agenda-time-grid'."
913 :group 'org-agenda-time-grid
914 :type 'boolean)
916 (defcustom org-agenda-time-grid
917 '((daily today require-timed)
918 "----------------"
919 (800 1000 1200 1400 1600 1800 2000))
921 "The settings for time grid for agenda display.
922 This is a list of three items. The first item is again a list. It contains
923 symbols specifying conditions when the grid should be displayed:
925 daily if the agenda shows a single day
926 weekly if the agenda shows an entire week
927 today show grid on current date, independent of daily/weekly display
928 require-timed show grid only if at least one item has a time specification
930 The second item is a string which will be placed behind the grid time.
932 The third item is a list of integers, indicating the times that should have
933 a grid line."
934 :group 'org-agenda-time-grid
935 :type
936 '(list
937 (set :greedy t :tag "Grid Display Options"
938 (const :tag "Show grid in single day agenda display" daily)
939 (const :tag "Show grid in weekly agenda display" weekly)
940 (const :tag "Always show grid for today" today)
941 (const :tag "Show grid only if any timed entries are present"
942 require-timed)
943 (const :tag "Skip grid times already present in an entry"
944 remove-match))
945 (string :tag "Grid String")
946 (repeat :tag "Grid Times" (integer :tag "Time"))))
948 (defgroup org-agenda-sorting nil
949 "Options concerning sorting in the Org-mode Agenda."
950 :tag "Org Agenda Sorting"
951 :group 'org-agenda)
953 (defcustom org-agenda-sorting-strategy
954 '((agenda habit-down time-up priority-down category-keep)
955 (todo priority-down category-keep)
956 (tags priority-down category-keep)
957 (search category-keep))
958 "Sorting structure for the agenda items of a single day.
959 This is a list of symbols which will be used in sequence to determine
960 if an entry should be listed before another entry. The following
961 symbols are recognized:
963 time-up Put entries with time-of-day indications first, early first
964 time-down Put entries with time-of-day indications first, late first
965 category-keep Keep the default order of categories, corresponding to the
966 sequence in `org-agenda-files'.
967 category-up Sort alphabetically by category, A-Z.
968 category-down Sort alphabetically by category, Z-A.
969 tag-up Sort alphabetically by last tag, A-Z.
970 tag-down Sort alphabetically by last tag, Z-A.
971 priority-up Sort numerically by priority, high priority last.
972 priority-down Sort numerically by priority, high priority first.
973 todo-state-up Sort by todo state, tasks that are done last.
974 todo-state-down Sort by todo state, tasks that are done first.
975 effort-up Sort numerically by estimated effort, high effort last.
976 effort-down Sort numerically by estimated effort, high effort first.
977 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
978 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
979 habit-up Put entries that are habits first
980 habit-down Put entries that are habits last
982 The different possibilities will be tried in sequence, and testing stops
983 if one comparison returns a \"not-equal\". For example, the default
984 '(time-up category-keep priority-down)
985 means: Pull out all entries having a specified time of day and sort them,
986 in order to make a time schedule for the current day the first thing in the
987 agenda listing for the day. Of the entries without a time indication, keep
988 the grouped in categories, don't sort the categories, but keep them in
989 the sequence given in `org-agenda-files'. Within each category sort by
990 priority.
992 Leaving out `category-keep' would mean that items will be sorted across
993 categories by priority.
995 Instead of a single list, this can also be a set of list for specific
996 contents, with a context symbol in the car of the list, any of
997 `agenda', `todo', `tags', `search' for the corresponding agenda views.
999 Custom commands can bind this variable in the options section."
1000 :group 'org-agenda-sorting
1001 :type `(choice
1002 (repeat :tag "General" ,org-sorting-choice)
1003 (list :tag "Individually"
1004 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1005 (repeat ,org-sorting-choice))
1006 (cons (const :tag "Strategy for TODO lists" todo)
1007 (repeat ,org-sorting-choice))
1008 (cons (const :tag "Strategy for Tags matches" tags)
1009 (repeat ,org-sorting-choice))
1010 (cons (const :tag "Strategy for search matches" search)
1011 (repeat ,org-sorting-choice)))))
1013 (defcustom org-agenda-cmp-user-defined nil
1014 "A function to define the comparison `user-defined'.
1015 This function must receive two arguments, agenda entry a and b.
1016 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1017 the user comparison, return nil.
1018 When this is defined, you can make `user-defined-up' and `user-defined-down'
1019 part of an agenda sorting strategy."
1020 :group 'org-agenda-sorting
1021 :type 'symbol)
1023 (defcustom org-sort-agenda-notime-is-late t
1024 "Non-nil means, items without time are considered late.
1025 This is only relevant for sorting. When t, items which have no explicit
1026 time like 15:30 will be considered as 99:01, i.e. later than any items which
1027 do have a time. When nil, the default time is before 0:00. You can use this
1028 option to decide if the schedule for today should come before or after timeless
1029 agenda entries."
1030 :group 'org-agenda-sorting
1031 :type 'boolean)
1033 (defcustom org-sort-agenda-noeffort-is-high t
1034 "Non-nil means, items without effort estimate are sorted as high effort.
1035 This also applies when filtering an agenda view with respect to the
1036 < or > effort operator. Then, tasks with no effort defined will be treated
1037 as tasks with high effort.
1038 When nil, such items are sorted as 0 minutes effort."
1039 :group 'org-agenda-sorting
1040 :type 'boolean)
1042 (defgroup org-agenda-line-format nil
1043 "Options concerning the entry prefix in the Org-mode agenda display."
1044 :tag "Org Agenda Line Format"
1045 :group 'org-agenda)
1047 (defcustom org-agenda-prefix-format
1048 '((agenda . " %-12:c%?-12t% s")
1049 (timeline . " % s")
1050 (todo . " %-12:c")
1051 (tags . " %-12:c")
1052 (search . " %-12:c"))
1053 "Format specifications for the prefix of items in the agenda views.
1054 An alist with four entries, for the different agenda types. The keys to the
1055 sublists are `agenda', `timeline', `todo', and `tags'. The values
1056 are format strings.
1057 This format works similar to a printf format, with the following meaning:
1059 %c the category of the item, \"Diary\" for entries from the diary, or
1060 as given by the CATEGORY keyword or derived from the file name.
1061 %T the *last* tag of the item. Last because inherited tags come
1062 first in the list.
1063 %t the time-of-day specification if one applies to the entry, in the
1064 format HH:MM
1065 %s Scheduling/Deadline information, a short string
1067 All specifiers work basically like the standard `%s' of printf, but may
1068 contain two additional characters: A question mark just after the `%' and
1069 a whitespace/punctuation character just before the final letter.
1071 If the first character after `%' is a question mark, the entire field
1072 will only be included if the corresponding value applies to the
1073 current entry. This is useful for fields which should have fixed
1074 width when present, but zero width when absent. For example,
1075 \"%?-12t\" will result in a 12 character time field if a time of the
1076 day is specified, but will completely disappear in entries which do
1077 not contain a time.
1079 If there is punctuation or whitespace character just before the final
1080 format letter, this character will be appended to the field value if
1081 the value is not empty. For example, the format \"%-12:c\" leads to
1082 \"Diary: \" if the category is \"Diary\". If the category were be
1083 empty, no additional colon would be interted.
1085 The default value of this option is \" %-12:c%?-12t% s\", meaning:
1086 - Indent the line with two space characters
1087 - Give the category in a 12 chars wide field, padded with whitespace on
1088 the right (because of `-'). Append a colon if there is a category
1089 (because of `:').
1090 - If there is a time-of-day, put it into a 12 chars wide field. If no
1091 time, don't put in an empty field, just skip it (because of '?').
1092 - Finally, put the scheduling information and append a whitespace.
1094 As another example, if you don't want the time-of-day of entries in
1095 the prefix, you could use:
1097 (setq org-agenda-prefix-format \" %-11:c% s\")
1099 See also the variables `org-agenda-remove-times-when-in-prefix' and
1100 `org-agenda-remove-tags'.
1102 Custom commands can set this variable in the options section."
1103 :type '(choice
1104 (string :tag "General format")
1105 (list :greedy t :tag "View dependent"
1106 (cons (const agenda) (string :tag "Format"))
1107 (cons (const timeline) (string :tag "Format"))
1108 (cons (const todo) (string :tag "Format"))
1109 (cons (const tags) (string :tag "Format"))
1110 (cons (const search) (string :tag "Format"))))
1111 :group 'org-agenda-line-format)
1113 (defvar org-prefix-format-compiled nil
1114 "The compiled version of the most recently used prefix format.
1115 See the variable `org-agenda-prefix-format'.")
1117 (defcustom org-agenda-todo-keyword-format "%-1s"
1118 "Format for the TODO keyword in agenda lines.
1119 Set this to something like \"%-12s\" if you want all TODO keywords
1120 to occupy a fixed space in the agenda display."
1121 :group 'org-agenda-line-format
1122 :type 'string)
1124 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1125 "Text preceding timerange entries in the agenda view.
1126 This is a list with two strings. The first applies when the range
1127 is entirely on one day. The second applies if the range spans several days.
1128 The strings may have two \"%d\" format specifiers which will be filled
1129 with the sequence number of the days, and the total number of days in the
1130 range, respectively."
1131 :group 'org-agenda-line-format
1132 :type '(list
1133 (string :tag "Deadline today ")
1134 (choice :tag "Deadline relative"
1135 (string :tag "Format string")
1136 (function))))
1138 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1139 "Text preceeding scheduled items in the agenda view.
1140 This is a list with two strings. The first applies when the item is
1141 scheduled on the current day. The second applies when it has been scheduled
1142 previously, it may contain a %d indicating that this is the nth time that
1143 this item is scheduled, due to automatic rescheduling of unfinished items
1144 for the following day. So this number is one larger than the number of days
1145 that passed since this item was scheduled first."
1146 :group 'org-agenda-line-format
1147 :type '(list
1148 (string :tag "Scheduled today ")
1149 (string :tag "Scheduled previously")))
1151 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1152 "Text preceeding deadline items in the agenda view.
1153 This is a list with two strings. The first applies when the item has its
1154 deadline on the current day. The second applies when it is in the past or
1155 in the future, it may contain %d to capture how many days away the deadline
1156 is (was)."
1157 :group 'org-agenda-line-format
1158 :type '(list
1159 (string :tag "Deadline today ")
1160 (choice :tag "Deadline relative"
1161 (string :tag "Format string")
1162 (function))))
1164 (defcustom org-agenda-remove-times-when-in-prefix t
1165 "Non-nil means, remove duplicate time specifications in agenda items.
1166 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1167 time-of-day specification in a headline or diary entry is extracted and
1168 placed into the prefix. If this option is non-nil, the original specification
1169 \(a timestamp or -range, or just a plain time(range) specification like
1170 11:30-4pm) will be removed for agenda display. This makes the agenda less
1171 cluttered.
1172 The option can be t or nil. It may also be the symbol `beg', indicating
1173 that the time should only be removed what it is located at the beginning of
1174 the headline/diary entry."
1175 :group 'org-agenda-line-format
1176 :type '(choice
1177 (const :tag "Always" t)
1178 (const :tag "Never" nil)
1179 (const :tag "When at beginning of entry" beg)))
1182 (defcustom org-agenda-default-appointment-duration nil
1183 "Default duration for appointments that only have a starting time.
1184 When nil, no duration is specified in such cases.
1185 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1186 :group 'org-agenda-line-format
1187 :type '(choice
1188 (integer :tag "Minutes")
1189 (const :tag "No default duration")))
1191 (defcustom org-agenda-show-inherited-tags t
1192 "Non-nil means, show inherited tags in each agenda line."
1193 :group 'org-agenda-line-format
1194 :type 'boolean)
1196 (defcustom org-agenda-remove-tags nil
1197 "Non-nil means, remove the tags from the headline copy in the agenda.
1198 When this is the symbol `prefix', only remove tags when
1199 `org-agenda-prefix-format' contains a `%T' specifier."
1200 :group 'org-agenda-line-format
1201 :type '(choice
1202 (const :tag "Always" t)
1203 (const :tag "Never" nil)
1204 (const :tag "When prefix format contains %T" prefix)))
1206 (if (fboundp 'defvaralias)
1207 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1208 'org-agenda-remove-tags))
1210 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1211 "Shift tags in agenda items to this column.
1212 If this number is positive, it specifies the column. If it is negative,
1213 it means that the tags should be flushright to that column. For example,
1214 -80 works well for a normal 80 character screen."
1215 :group 'org-agenda-line-format
1216 :type 'integer)
1218 (if (fboundp 'defvaralias)
1219 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1221 (defcustom org-agenda-fontify-priorities 'cookies
1222 "Non-nil means, highlight low and high priorities in agenda.
1223 When t, the highest priority entries are bold, lowest priority italic.
1224 However, settings in org-priority-faces will overrule these faces.
1225 When this variable is the symbol `cookies', only fontify the
1226 cookies, not the entire task.
1227 This may also be an association list of priority faces, whose
1228 keys are the character values of `org-highest-priority',
1229 `org-default-priority', and `org-lowest-priority' (the default values
1230 are ?A, ?B, and ?C, respectively). The face may be a named face,
1231 or a list like `(:background \"Red\")'."
1232 :group 'org-agenda-line-format
1233 :type '(choice
1234 (const :tag "Never" nil)
1235 (const :tag "Defaults" t)
1236 (const :tag "Cookies only" cookies)
1237 (repeat :tag "Specify"
1238 (list (character :tag "Priority" :value ?A)
1239 (sexp :tag "face")))))
1241 (defgroup org-agenda-column-view nil
1242 "Options concerning column view in the agenda."
1243 :tag "Org Agenda Column View"
1244 :group 'org-agenda)
1246 (defcustom org-agenda-columns-show-summaries t
1247 "Non-nil means, show summaries for columns displayed in the agenda view."
1248 :group 'org-agenda-column-view
1249 :type 'boolean)
1251 (defcustom org-agenda-columns-remove-prefix-from-item t
1252 "Non-nil means, remove the prefix from a headline for agenda column view.
1253 The special ITEM field in the columns format contains the current line, with
1254 all information shown in other columns (like the TODO state or a tag).
1255 When this variable is non-nil, also the agenda prefix will be removed from
1256 the content of the ITEM field, to make sure as much as possible of the
1257 headline can be shown in the limited width of the field."
1258 :group 'org-agenda
1259 :type 'boolean)
1261 (defcustom org-agenda-columns-compute-summary-properties t
1262 "Non-nil means, recompute all summary properties before column view.
1263 When column view in the agenda is listing properties that have a summary
1264 operator, it can go to all relevant buffers and recompute the summaries
1265 there. This can mean overhead for the agenda column view, but is necessary
1266 to have thing up to date.
1267 As a special case, a CLOCKSUM property also makes sure that the clock
1268 computations are current."
1269 :group 'org-agenda-column-view
1270 :type 'boolean)
1272 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1273 "Non-nil means, the duration of an appointment will add to day effort.
1274 The property to which appointment durations will be added is the one given
1275 in the option `org-effort-property'. If an appointment does not have
1276 an end time, `org-agenda-default-appointment-duration' will be used. If that
1277 is not set, an appointment without end time will not contribute to the time
1278 estimate."
1279 :group 'org-agenda-column-view
1280 :type 'boolean)
1282 (defcustom org-agenda-auto-exclude-function nil
1283 "A function called with a tag to decide if it is filtered on '/ RET'.
1284 The sole argument to the function, which is called once for each
1285 possible tag, is a string giving the name of the tag. The
1286 function should return either nil if the tag should be included
1287 as normal, or \"-<TAG>\" to exclude the tag."
1288 :group 'org-agenda
1289 :type 'function)
1291 (eval-when-compile
1292 (require 'cl))
1293 (require 'org)
1295 (defun org-add-agenda-custom-command (entry)
1296 "Replace or add a command in `org-agenda-custom-commands'.
1297 This is mostly for hacking and trying a new command - once the command
1298 works you probably want to add it to `org-agenda-custom-commands' for good."
1299 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1300 (if ass
1301 (setcdr ass (cdr entry))
1302 (push entry org-agenda-custom-commands))))
1304 ;;; Define the Org-agenda-mode
1306 (defvar org-agenda-mode-map (make-sparse-keymap)
1307 "Keymap for `org-agenda-mode'.")
1309 (defvar org-agenda-menu) ; defined later in this file.
1310 (defvar org-agenda-restrict) ; defined later in this file.
1311 (defvar org-agenda-follow-mode nil)
1312 (defvar org-agenda-entry-text-mode nil)
1313 (defvar org-agenda-clockreport-mode nil)
1314 (defvar org-agenda-show-log nil)
1315 (defvar org-agenda-redo-command nil)
1316 (defvar org-agenda-query-string nil)
1317 (defvar org-agenda-mode-hook nil
1318 "Hook for org-agenda-mode, run after the mode is turned on.")
1319 (defvar org-agenda-type nil)
1320 (defvar org-agenda-force-single-file nil)
1321 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1323 (defun org-agenda-mode ()
1324 "Mode for time-sorted view on action items in Org-mode files.
1326 The following commands are available:
1328 \\{org-agenda-mode-map}"
1329 (interactive)
1330 (kill-all-local-variables)
1331 (setq org-agenda-undo-list nil
1332 org-agenda-pending-undo-list nil
1333 org-agenda-bulk-marked-entries nil)
1334 (setq major-mode 'org-agenda-mode)
1335 ;; Keep global-font-lock-mode from turning on font-lock-mode
1336 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1337 (setq mode-name "Org-Agenda")
1338 (use-local-map org-agenda-mode-map)
1339 (easy-menu-add org-agenda-menu)
1340 (if org-startup-truncated (setq truncate-lines t))
1341 (org-set-local 'line-move-visual nil)
1342 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1343 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1344 ;; Make sure properties are removed when copying text
1345 (when (boundp 'buffer-substring-filters)
1346 (org-set-local 'buffer-substring-filters
1347 (cons (lambda (x)
1348 (set-text-properties 0 (length x) nil x) x)
1349 buffer-substring-filters)))
1350 (unless org-agenda-keep-modes
1351 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1352 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1353 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1354 org-agenda-show-log org-agenda-start-with-log-mode))
1356 (easy-menu-change
1357 '("Agenda") "Agenda Files"
1358 (append
1359 (list
1360 (vector
1361 (if (get 'org-agenda-files 'org-restrict)
1362 "Restricted to single file"
1363 "Edit File List")
1364 '(org-edit-agenda-file-list)
1365 (not (get 'org-agenda-files 'org-restrict)))
1366 "--")
1367 (mapcar 'org-file-menu-entry (org-agenda-files))))
1368 (org-agenda-set-mode-name)
1369 (apply
1370 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1371 (list 'org-agenda-mode-hook)))
1373 (substitute-key-definition 'undo 'org-agenda-undo
1374 org-agenda-mode-map global-map)
1375 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1376 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1377 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1378 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1379 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1380 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1381 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1382 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1383 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1384 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1385 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1386 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1387 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1388 (org-defkey org-agenda-mode-map "A" 'org-agenda-archive-to-archive-sibling)
1389 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1390 (org-defkey org-agenda-mode-map " " 'org-agenda-show)
1391 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1392 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1393 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1394 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1395 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1396 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1397 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1398 (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
1399 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1400 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1401 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1402 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1403 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1404 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1405 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1406 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1407 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1408 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1409 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1410 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1411 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1412 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1413 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1415 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1416 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1417 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1418 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1419 (while l (org-defkey org-agenda-mode-map
1420 (int-to-string (pop l)) 'digit-argument)))
1422 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1423 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1424 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1425 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1426 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1427 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1428 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1429 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1430 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1431 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1432 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1433 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1434 'org-clock-modify-effort-estimate)
1435 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1436 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1437 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1438 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1439 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1440 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1441 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1442 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1443 (org-defkey org-agenda-mode-map "n" 'next-line)
1444 (org-defkey org-agenda-mode-map "p" 'previous-line)
1445 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1446 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1447 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1448 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1449 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1450 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1451 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1452 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1453 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1454 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1455 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1456 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1457 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1458 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1459 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1460 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1461 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1462 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1463 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1464 (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
1465 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1466 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1467 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1468 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1469 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1470 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1471 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1472 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1473 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1474 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1476 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1477 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1478 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1479 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1480 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1481 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1482 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1483 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1484 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1485 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1487 (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
1488 "Local keymap for agenda entries from Org-mode.")
1490 (org-defkey org-agenda-keymap
1491 (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
1492 (org-defkey org-agenda-keymap
1493 (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
1494 (when org-agenda-mouse-1-follows-link
1495 (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
1496 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1497 '("Agenda"
1498 ("Agenda Files")
1499 "--"
1500 ("Agenda Dates"
1501 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1502 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1503 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1504 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1505 "--"
1506 ("View"
1507 ["Day View" org-agenda-day-view
1508 :active (org-agenda-check-type nil 'agenda)
1509 :style radio :selected (equal org-agenda-ndays 1)
1510 :keys "v d (or just d)"]
1511 ["Week View" org-agenda-week-view
1512 :active (org-agenda-check-type nil 'agenda)
1513 :style radio :selected (equal org-agenda-ndays 7)
1514 :keys "v w (or just w)"]
1515 ["Month View" org-agenda-month-view
1516 :active (org-agenda-check-type nil 'agenda)
1517 :style radio :selected (member org-agenda-ndays '(28 29 30 31))
1518 :keys "v m"]
1519 ["Year View" org-agenda-year-view
1520 :active (org-agenda-check-type nil 'agenda)
1521 :style radio :selected (member org-agenda-ndays '(365 366))
1522 :keys "v y"]
1523 "--"
1524 ["Include Diary" org-agenda-toggle-diary
1525 :style toggle :selected org-agenda-include-diary
1526 :active (org-agenda-check-type nil 'agenda)]
1527 ["Use Time Grid" org-agenda-toggle-time-grid
1528 :style toggle :selected org-agenda-use-time-grid
1529 :active (org-agenda-check-type nil 'agenda)]
1530 "--"
1531 ["Show clock report" org-agenda-clockreport-mode
1532 :style toggle :selected org-agenda-clockreport-mode
1533 :active (org-agenda-check-type nil 'agenda)]
1534 ["Show some entry text" org-agenda-entry-text-mode
1535 :style toggle :selected org-agenda-entry-text-mode
1536 :active t]
1537 "--"
1538 ["Show Logbook entries" org-agenda-log-mode
1539 :style toggle :selected org-agenda-show-log
1540 :active (org-agenda-check-type nil 'agenda 'timeline)
1541 :keys "v l (or just l)"]
1542 ["Include archived trees" org-agenda-archives-mode
1543 :style toggle :selected org-agenda-archives-mode :active t
1544 :keys "v a"]
1545 ["Include archive files" (org-agenda-archives-mode t)
1546 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1547 :keys "v A"]
1548 "--"
1549 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1550 ["Write view to file" org-write-agenda t]
1551 ["Rebuild buffer" org-agenda-redo t]
1552 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1553 "--"
1554 ["Show original entry" org-agenda-show t]
1555 ["Go To (other window)" org-agenda-goto t]
1556 ["Go To (this window)" org-agenda-switch-to t]
1557 ["Follow Mode" org-agenda-follow-mode
1558 :style toggle :selected org-agenda-follow-mode :active t]
1559 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
1560 "--"
1561 ("TODO"
1562 ["Cycle TODO" org-agenda-todo t]
1563 ["Next TODO set" org-agenda-todo-nextset t]
1564 ["Previous TODO set" org-agenda-todo-previousset t]
1565 ["Add note" org-agenda-add-note t])
1566 ("Archive/Refile/Delete"
1567 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
1568 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
1569 ["Archive subtree" org-agenda-archive t]
1570 "--"
1571 ["Refile" org-agenda-refile t]
1572 "--"
1573 ["Delete subtree" org-agenda-kill t])
1574 ("Bulk action"
1575 ["Mark entry" org-agenda-bulk-mark t]
1576 ["Unmark entry" org-agenda-bulk-unmark t]
1577 ["Act on all marked" org-agenda-bulk-action t]
1578 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
1579 "--"
1580 ("Tags and Properties"
1581 ["Show all Tags" org-agenda-show-tags t]
1582 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
1583 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
1584 "--"
1585 ["Column View" org-columns t])
1586 ("Deadline/Schedule"
1587 ["Schedule" org-agenda-schedule t]
1588 ["Set Deadline" org-agenda-deadline t]
1589 "--"
1590 ["Mark item" org-agenda-action :active t :keys "k m"]
1591 ["Show mark item" org-agenda-action :active t :keys "k v"]
1592 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
1593 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
1594 "--"
1595 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
1596 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
1597 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
1598 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
1599 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
1600 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
1601 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
1602 ("Clock and Effort"
1603 ["Clock in" org-agenda-clock-in t]
1604 ["Clock out" org-agenda-clock-out t]
1605 ["Clock cancel" org-agenda-clock-cancel t]
1606 ["Goto running clock" org-clock-goto t]
1607 "--"
1608 ["Set Effort" org-agenda-set-effort t]
1609 ["Change clocked effort" org-clock-modify-effort-estimate
1610 (org-clock-is-active)])
1611 ("Priority"
1612 ["Set Priority" org-agenda-priority t]
1613 ["Increase Priority" org-agenda-priority-up t]
1614 ["Decrease Priority" org-agenda-priority-down t]
1615 ["Show Priority" org-agenda-show-priority t])
1616 ("Calendar/Diary"
1617 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
1618 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
1619 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
1620 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
1621 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
1622 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
1623 "--"
1624 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
1625 "--"
1626 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
1627 "--"
1628 ("MobileOrg"
1629 ["Push Files and Views" org-mobile-push t]
1630 ["Get Captured and Flagged" org-mobile-pull t]
1631 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
1632 ["Show note / unflag" org-agenda-show-the-flagging-note t]
1633 "--"
1634 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
1635 "--"
1636 ["Quit" org-agenda-quit t]
1637 ["Exit and Release Buffers" org-agenda-exit t]
1640 ;;; Agenda undo
1642 (defvar org-agenda-allow-remote-undo t
1643 "Non-nil means, allow remote undo from the agenda buffer.")
1644 (defvar org-agenda-undo-list nil
1645 "List of undoable operations in the agenda since last refresh.")
1646 (defvar org-agenda-undo-has-started-in nil
1647 "Buffers that have already seen `undo-start' in the current undo sequence.")
1648 (defvar org-agenda-pending-undo-list nil
1649 "In a series of undo commands, this is the list of remaining undo items.")
1652 (defun org-agenda-undo ()
1653 "Undo a remote editing step in the agenda.
1654 This undoes changes both in the agenda buffer and in the remote buffer
1655 that have been changed along."
1656 (interactive)
1657 (or org-agenda-allow-remote-undo
1658 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
1659 (if (not (eq this-command last-command))
1660 (setq org-agenda-undo-has-started-in nil
1661 org-agenda-pending-undo-list org-agenda-undo-list))
1662 (if (not org-agenda-pending-undo-list)
1663 (error "No further undo information"))
1664 (let* ((entry (pop org-agenda-pending-undo-list))
1665 buf line cmd rembuf)
1666 (setq cmd (pop entry) line (pop entry))
1667 (setq rembuf (nth 2 entry))
1668 (org-with-remote-undo rembuf
1669 (while (bufferp (setq buf (pop entry)))
1670 (if (pop entry)
1671 (with-current-buffer buf
1672 (let ((last-undo-buffer buf)
1673 (inhibit-read-only t))
1674 (unless (memq buf org-agenda-undo-has-started-in)
1675 (push buf org-agenda-undo-has-started-in)
1676 (make-local-variable 'pending-undo-list)
1677 (undo-start))
1678 (while (and pending-undo-list
1679 (listp pending-undo-list)
1680 (not (car pending-undo-list)))
1681 (pop pending-undo-list))
1682 (undo-more 1))))))
1683 (org-goto-line line)
1684 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
1686 (defun org-verify-change-for-undo (l1 l2)
1687 "Verify that a real change occurred between the undo lists L1 and L2."
1688 (while (and l1 (listp l1) (null (car l1))) (pop l1))
1689 (while (and l2 (listp l2) (null (car l2))) (pop l2))
1690 (not (eq l1 l2)))
1692 ;;; Agenda dispatch
1694 (defvar org-agenda-restrict nil)
1695 (defvar org-agenda-restrict-begin (make-marker))
1696 (defvar org-agenda-restrict-end (make-marker))
1697 (defvar org-agenda-last-dispatch-buffer nil)
1698 (defvar org-agenda-overriding-restriction nil)
1700 ;;;###autoload
1701 (defun org-agenda (&optional arg keys restriction)
1702 "Dispatch agenda commands to collect entries to the agenda buffer.
1703 Prompts for a command to execute. Any prefix arg will be passed
1704 on to the selected command. The default selections are:
1706 a Call `org-agenda-list' to display the agenda for current day or week.
1707 t Call `org-todo-list' to display the global todo list.
1708 T Call `org-todo-list' to display the global todo list, select only
1709 entries with a specific TODO keyword (the user gets a prompt).
1710 m Call `org-tags-view' to display headlines with tags matching
1711 a condition (the user is prompted for the condition).
1712 M Like `m', but select only TODO entries, no ordinary headlines.
1713 L Create a timeline for the current buffer.
1714 e Export views to associated files.
1715 s Search entries for keywords.
1716 / Multi occur across all agenda files and also files listed
1717 in `org-agenda-text-search-extra-files'.
1718 < Restrict agenda commands to buffer, subtree, or region.
1719 Press several times to get the desired effect.
1720 > Remove a previous restriction.
1721 # List \"stuck\" projects.
1722 ! Configure what \"stuck\" means.
1723 C Configure custom agenda commands.
1725 More commands can be added by configuring the variable
1726 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
1727 searches can be pre-defined in this way.
1729 If the current buffer is in Org-mode and visiting a file, you can also
1730 first press `<' once to indicate that the agenda should be temporarily
1731 \(until the next use of \\[org-agenda]) restricted to the current file.
1732 Pressing `<' twice means to restrict to the current subtree or region
1733 \(if active)."
1734 (interactive "P")
1735 (catch 'exit
1736 (let* ((prefix-descriptions nil)
1737 (org-agenda-window-setup (if (equal (buffer-name)
1738 org-agenda-buffer-name)
1739 'current-window
1740 org-agenda-window-setup))
1741 (org-agenda-custom-commands-orig org-agenda-custom-commands)
1742 (org-agenda-custom-commands
1743 ;; normalize different versions
1744 (delq nil
1745 (mapcar
1746 (lambda (x)
1747 (cond ((stringp (cdr x))
1748 (push x prefix-descriptions)
1749 nil)
1750 ((stringp (nth 1 x)) x)
1751 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1752 (t (cons (car x) (cons "" (cdr x))))))
1753 org-agenda-custom-commands)))
1754 (buf (current-buffer))
1755 (bfn (buffer-file-name (buffer-base-buffer)))
1756 entry key type match lprops ans)
1757 ;; Turn off restriction unless there is an overriding one,
1758 (unless org-agenda-overriding-restriction
1759 (unless (org-bound-and-true-p org-agenda-keep-restriced-file-list)
1760 ;; There is a request to keep the file list in place
1761 (put 'org-agenda-files 'org-restrict nil))
1762 (setq org-agenda-restrict nil)
1763 (move-marker org-agenda-restrict-begin nil)
1764 (move-marker org-agenda-restrict-end nil))
1765 ;; Delete old local properties
1766 (put 'org-agenda-redo-command 'org-lprops nil)
1767 ;; Remember where this call originated
1768 (setq org-agenda-last-dispatch-buffer (current-buffer))
1769 (unless keys
1770 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
1771 keys (car ans)
1772 restriction (cdr ans)))
1773 ;; Estabish the restriction, if any
1774 (when (and (not org-agenda-overriding-restriction) restriction)
1775 (put 'org-agenda-files 'org-restrict (list bfn))
1776 (cond
1777 ((eq restriction 'region)
1778 (setq org-agenda-restrict t)
1779 (move-marker org-agenda-restrict-begin (region-beginning))
1780 (move-marker org-agenda-restrict-end (region-end)))
1781 ((eq restriction 'subtree)
1782 (save-excursion
1783 (setq org-agenda-restrict t)
1784 (org-back-to-heading t)
1785 (move-marker org-agenda-restrict-begin (point))
1786 (move-marker org-agenda-restrict-end
1787 (progn (org-end-of-subtree t)))))))
1789 (require 'calendar) ; FIXME: can we avoid this for some commands?
1790 ;; For example the todo list should not need it (but does...)
1791 (cond
1792 ((setq entry (assoc keys org-agenda-custom-commands))
1793 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
1794 (progn
1795 (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
1796 (put 'org-agenda-redo-command 'org-lprops lprops)
1797 (cond
1798 ((eq type 'agenda)
1799 (org-let lprops '(org-agenda-list current-prefix-arg)))
1800 ((eq type 'alltodo)
1801 (org-let lprops '(org-todo-list current-prefix-arg)))
1802 ((eq type 'search)
1803 (org-let lprops '(org-search-view current-prefix-arg match nil)))
1804 ((eq type 'stuck)
1805 (org-let lprops '(org-agenda-list-stuck-projects
1806 current-prefix-arg)))
1807 ((eq type 'tags)
1808 (org-let lprops '(org-tags-view current-prefix-arg match)))
1809 ((eq type 'tags-todo)
1810 (org-let lprops '(org-tags-view '(4) match)))
1811 ((eq type 'todo)
1812 (org-let lprops '(org-todo-list match)))
1813 ((eq type 'tags-tree)
1814 (org-check-for-org-mode)
1815 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
1816 ((eq type 'todo-tree)
1817 (org-check-for-org-mode)
1818 (org-let lprops
1819 '(org-occur (concat "^" outline-regexp "[ \t]*"
1820 (regexp-quote match) "\\>"))))
1821 ((eq type 'occur-tree)
1822 (org-check-for-org-mode)
1823 (org-let lprops '(org-occur match)))
1824 ((functionp type)
1825 (org-let lprops '(funcall type match)))
1826 ((fboundp type)
1827 (org-let lprops '(funcall type match)))
1828 (t (error "Invalid custom agenda command type %s" type))))
1829 (org-run-agenda-series (nth 1 entry) (cddr entry))))
1830 ((equal keys "C")
1831 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
1832 (customize-variable 'org-agenda-custom-commands))
1833 ((equal keys "a") (call-interactively 'org-agenda-list))
1834 ((equal keys "s") (call-interactively 'org-search-view))
1835 ((equal keys "t") (call-interactively 'org-todo-list))
1836 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
1837 ((equal keys "m") (call-interactively 'org-tags-view))
1838 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
1839 ((equal keys "e") (call-interactively 'org-store-agenda-views))
1840 ((equal keys "?") (org-tags-view nil "+FLAGGED")
1841 (org-add-hook
1842 'post-command-hook
1843 (lambda ()
1844 (unless (current-message)
1845 (let* ((m (org-agenda-get-any-marker))
1846 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
1847 (when note
1848 (message (concat
1849 "FLAGGING-NOTE ([?] for more info): "
1850 (org-add-props
1851 (replace-regexp-in-string
1852 "\\\\n" "//"
1853 (copy-sequence note))
1854 nil 'face 'org-warning)))))))
1855 t t))
1856 ((equal keys "L")
1857 (unless (org-mode-p)
1858 (error "This is not an Org-mode file"))
1859 (unless restriction
1860 (put 'org-agenda-files 'org-restrict (list bfn))
1861 (org-call-with-arg 'org-timeline arg)))
1862 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
1863 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
1864 ((equal keys "!") (customize-variable 'org-stuck-projects))
1865 (t (error "Invalid agenda key"))))))
1867 (defun org-agenda-normalize-custom-commands (cmds)
1868 (delq nil
1869 (mapcar
1870 (lambda (x)
1871 (cond ((stringp (cdr x)) nil)
1872 ((stringp (nth 1 x)) x)
1873 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1874 (t (cons (car x) (cons "" (cdr x))))))
1875 cmds)))
1877 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
1878 "The user interface for selecting an agenda command."
1879 (catch 'exit
1880 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
1881 (restrict-ok (and bfn (org-mode-p)))
1882 (region-p (org-region-active-p))
1883 (custom org-agenda-custom-commands)
1884 (selstring "")
1885 restriction second-time
1886 c entry key type match prefixes rmheader header-end custom1 desc)
1887 (save-window-excursion
1888 (delete-other-windows)
1889 (org-switch-to-buffer-other-window " *Agenda Commands*")
1890 (erase-buffer)
1891 (insert (eval-when-compile
1892 (let ((header
1894 Press key for an agenda command: < Buffer, subtree/region restriction
1895 -------------------------------- > Remove restriction
1896 a Agenda for current week or day e Export agenda views
1897 t List of all TODO entries T Entries with special TODO kwd
1898 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
1899 L Timeline for current buffer # List stuck projects (!=configure)
1900 s Search for keywords C Configure custom agenda commands
1901 / Multi-occur ? Find :FLAGGED: entries
1903 (start 0))
1904 (while (string-match
1905 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
1906 header start)
1907 (setq start (match-end 0))
1908 (add-text-properties (match-beginning 2) (match-end 2)
1909 '(face bold) header))
1910 header)))
1911 (setq header-end (move-marker (make-marker) (point)))
1912 (while t
1913 (setq custom1 custom)
1914 (when (eq rmheader t)
1915 (org-goto-line 1)
1916 (re-search-forward ":" nil t)
1917 (delete-region (match-end 0) (point-at-eol))
1918 (forward-char 1)
1919 (looking-at "-+")
1920 (delete-region (match-end 0) (point-at-eol))
1921 (move-marker header-end (match-end 0)))
1922 (goto-char header-end)
1923 (delete-region (point) (point-max))
1924 (while (setq entry (pop custom1))
1925 (setq key (car entry) desc (nth 1 entry)
1926 type (nth 2 entry)
1927 match (nth 3 entry))
1928 (if (> (length key) 1)
1929 (add-to-list 'prefixes (string-to-char key))
1930 (insert
1931 (format
1932 "\n%-4s%-14s: %s"
1933 (org-add-props (copy-sequence key)
1934 '(face bold))
1935 (cond
1936 ((string-match "\\S-" desc) desc)
1937 ((eq type 'agenda) "Agenda for current week or day")
1938 ((eq type 'alltodo) "List of all TODO entries")
1939 ((eq type 'search) "Word search")
1940 ((eq type 'stuck) "List of stuck projects")
1941 ((eq type 'todo) "TODO keyword")
1942 ((eq type 'tags) "Tags query")
1943 ((eq type 'tags-todo) "Tags (TODO)")
1944 ((eq type 'tags-tree) "Tags tree")
1945 ((eq type 'todo-tree) "TODO kwd tree")
1946 ((eq type 'occur-tree) "Occur tree")
1947 ((functionp type) (if (symbolp type)
1948 (symbol-name type)
1949 "Lambda expression"))
1950 (t "???"))
1951 (cond
1952 ((stringp match)
1953 (setq match (copy-sequence match))
1954 (org-add-props match nil 'face 'org-warning))
1955 (match
1956 (format "set of %d commands" (length match)))
1957 (t ""))))))
1958 (when prefixes
1959 (mapc (lambda (x)
1960 (insert
1961 (format "\n%s %s"
1962 (org-add-props (char-to-string x)
1963 nil 'face 'bold)
1964 (or (cdr (assoc (concat selstring (char-to-string x))
1965 prefix-descriptions))
1966 "Prefix key"))))
1967 prefixes))
1968 (goto-char (point-min))
1969 (if second-time
1970 (if (not (pos-visible-in-window-p (point-max)))
1971 (org-fit-window-to-buffer))
1972 (setq second-time t)
1973 (org-fit-window-to-buffer))
1974 (message "Press key for agenda command%s:"
1975 (if (or restrict-ok org-agenda-overriding-restriction)
1976 (if org-agenda-overriding-restriction
1977 " (restriction lock active)"
1978 (if restriction
1979 (format " (restricted to %s)" restriction)
1980 " (unrestricted)"))
1981 ""))
1982 (setq c (read-char-exclusive))
1983 (message "")
1984 (cond
1985 ((assoc (char-to-string c) custom)
1986 (setq selstring (concat selstring (char-to-string c)))
1987 (throw 'exit (cons selstring restriction)))
1988 ((memq c prefixes)
1989 (setq selstring (concat selstring (char-to-string c))
1990 prefixes nil
1991 rmheader (or rmheader t)
1992 custom (delq nil (mapcar
1993 (lambda (x)
1994 (if (or (= (length (car x)) 1)
1995 (/= (string-to-char (car x)) c))
1997 (cons (substring (car x) 1) (cdr x))))
1998 custom))))
1999 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2000 (message "Restriction is only possible in Org-mode buffers")
2001 (ding) (sit-for 1))
2002 ((eq c ?1)
2003 (org-agenda-remove-restriction-lock 'noupdate)
2004 (setq restriction 'buffer))
2005 ((eq c ?0)
2006 (org-agenda-remove-restriction-lock 'noupdate)
2007 (setq restriction (if region-p 'region 'subtree)))
2008 ((eq c ?<)
2009 (org-agenda-remove-restriction-lock 'noupdate)
2010 (setq restriction
2011 (cond
2012 ((eq restriction 'buffer)
2013 (if region-p 'region 'subtree))
2014 ((memq restriction '(subtree region))
2015 nil)
2016 (t 'buffer))))
2017 ((eq c ?>)
2018 (org-agenda-remove-restriction-lock 'noupdate)
2019 (setq restriction nil))
2020 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2021 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2022 ((and (> (length selstring) 0) (eq c ?\d))
2023 (delete-window)
2024 (org-agenda-get-restriction-and-command prefix-descriptions))
2026 ((equal c ?q) (error "Abort"))
2027 (t (error "Invalid key %c" c))))))))
2029 (defun org-run-agenda-series (name series)
2030 (org-let (nth 1 series) '(org-prepare-agenda name))
2031 (let* ((org-agenda-multi t)
2032 (redo (list 'org-run-agenda-series name (list 'quote series)))
2033 (cmds (car series))
2034 (gprops (nth 1 series))
2035 match ;; The byte compiler incorrectly complains about this. Keep it!
2036 cmd type lprops)
2037 (while (setq cmd (pop cmds))
2038 (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
2039 (cond
2040 ((eq type 'agenda)
2041 (org-let2 gprops lprops
2042 '(call-interactively 'org-agenda-list)))
2043 ((eq type 'alltodo)
2044 (org-let2 gprops lprops
2045 '(call-interactively 'org-todo-list)))
2046 ((eq type 'search)
2047 (org-let2 gprops lprops
2048 '(org-search-view current-prefix-arg match nil)))
2049 ((eq type 'stuck)
2050 (org-let2 gprops lprops
2051 '(call-interactively 'org-agenda-list-stuck-projects)))
2052 ((eq type 'tags)
2053 (org-let2 gprops lprops
2054 '(org-tags-view current-prefix-arg match)))
2055 ((eq type 'tags-todo)
2056 (org-let2 gprops lprops
2057 '(org-tags-view '(4) match)))
2058 ((eq type 'todo)
2059 (org-let2 gprops lprops
2060 '(org-todo-list match)))
2061 ((fboundp type)
2062 (org-let2 gprops lprops
2063 '(funcall type match)))
2064 (t (error "Invalid type in command series"))))
2065 (widen)
2066 (setq org-agenda-redo-command redo)
2067 (goto-char (point-min)))
2068 (org-fit-agenda-window)
2069 (org-let (nth 1 series) '(org-finalize-agenda)))
2071 ;;;###autoload
2072 (defmacro org-batch-agenda (cmd-key &rest parameters)
2073 "Run an agenda command in batch mode and send the result to STDOUT.
2074 If CMD-KEY is a string of length 1, it is used as a key in
2075 `org-agenda-custom-commands' and triggers this command. If it is a
2076 longer string it is used as a tags/todo match string.
2077 Paramters are alternating variable names and values that will be bound
2078 before running the agenda command."
2079 (let (pars)
2080 (while parameters
2081 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2082 (if (> (length cmd-key) 2)
2083 (eval (list 'let (nreverse pars)
2084 (list 'org-tags-view nil cmd-key)))
2085 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2086 (set-buffer org-agenda-buffer-name)
2087 (princ (org-encode-for-stdout (buffer-string)))))
2089 ;(defun org-encode-for-stdout (string)
2090 ; (if (fboundp 'encode-coding-string)
2091 ; (encode-coding-string string buffer-file-coding-system)
2092 ; string))
2094 (defun org-encode-for-stdout (string)
2095 string)
2097 (defvar org-agenda-info nil)
2099 ;;;###autoload
2100 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2101 "Run an agenda command in batch mode and send the result to STDOUT.
2102 If CMD-KEY is a string of length 1, it is used as a key in
2103 `org-agenda-custom-commands' and triggers this command. If it is a
2104 longer string it is used as a tags/todo match string.
2105 Paramters are alternating variable names and values that will be bound
2106 before running the agenda command.
2108 The output gives a line for each selected agenda item. Each
2109 item is a list of comma-separated values, like this:
2111 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2113 category The category of the item
2114 head The headline, without TODO kwd, TAGS and PRIORITY
2115 type The type of the agenda entry, can be
2116 todo selected in TODO match
2117 tagsmatch selected in tags match
2118 diary imported from diary
2119 deadline a deadline on given date
2120 scheduled scheduled on given date
2121 timestamp entry has timestamp on given date
2122 closed entry was closed on given date
2123 upcoming-deadline warning about deadline
2124 past-scheduled forwarded scheduled item
2125 block entry has date block including g. date
2126 todo The todo keyword, if any
2127 tags All tags including inherited ones, separated by colons
2128 date The relevant date, like 2007-2-14
2129 time The time, like 15:00-16:50
2130 extra Sting with extra planning info
2131 priority-l The priority letter if any was given
2132 priority-n The computed numerical priority
2133 agenda-day The day in the agenda where this is listed"
2135 (let (pars)
2136 (while parameters
2137 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2138 (push (list 'org-agenda-remove-tags t) pars)
2139 (if (> (length cmd-key) 2)
2140 (eval (list 'let (nreverse pars)
2141 (list 'org-tags-view nil cmd-key)))
2142 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2143 (set-buffer org-agenda-buffer-name)
2144 (let* ((lines (org-split-string (buffer-string) "\n"))
2145 line)
2146 (while (setq line (pop lines))
2147 (catch 'next
2148 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2149 (setq org-agenda-info
2150 (org-fix-agenda-info (text-properties-at 0 line)))
2151 (princ
2152 (org-encode-for-stdout
2153 (mapconcat 'org-agenda-export-csv-mapper
2154 '(org-category txt type todo tags date time-of-day extra
2155 priority-letter priority agenda-day)
2156 ",")))
2157 (princ "\n"))))))
2159 (defun org-fix-agenda-info (props)
2160 "Make sure all properties on an agenda item have a canonical form,
2161 so the export commands can easily use it."
2162 (let (tmp re)
2163 (when (setq tmp (plist-get props 'tags))
2164 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2165 (when (setq tmp (plist-get props 'date))
2166 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2167 (let ((calendar-date-display-form '(year "-" month "-" day)))
2168 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2170 (setq tmp (calendar-date-string tmp)))
2171 (setq props (plist-put props 'date tmp)))
2172 (when (setq tmp (plist-get props 'day))
2173 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2174 (let ((calendar-date-display-form '(year "-" month "-" day)))
2175 (setq tmp (calendar-date-string tmp)))
2176 (setq props (plist-put props 'day tmp))
2177 (setq props (plist-put props 'agenda-day tmp)))
2178 (when (setq tmp (plist-get props 'txt))
2179 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2180 (plist-put props 'priority-letter (match-string 1 tmp))
2181 (setq tmp (replace-match "" t t tmp)))
2182 (when (and (setq re (plist-get props 'org-todo-regexp))
2183 (setq re (concat "\\`\\.*" re " ?"))
2184 (string-match re tmp))
2185 (plist-put props 'todo (match-string 1 tmp))
2186 (setq tmp (replace-match "" t t tmp)))
2187 (plist-put props 'txt tmp)))
2188 props)
2190 (defun org-agenda-export-csv-mapper (prop)
2191 (let ((res (plist-get org-agenda-info prop)))
2192 (setq res
2193 (cond
2194 ((not res) "")
2195 ((stringp res) res)
2196 (t (prin1-to-string res))))
2197 (while (string-match "," res)
2198 (setq res (replace-match ";" t t res)))
2199 (org-trim res)))
2202 ;;;###autoload
2203 (defun org-store-agenda-views (&rest parameters)
2204 (interactive)
2205 (eval (list 'org-batch-store-agenda-views)))
2207 ;; FIXME, why is this a macro?????
2208 ;;;###autoload
2209 (defmacro org-batch-store-agenda-views (&rest parameters)
2210 "Run all custom agenda commands that have a file argument."
2211 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2212 (pop-up-frames nil)
2213 (dir default-directory)
2214 pars cmd thiscmdkey files opts cmd-or-set)
2215 (while parameters
2216 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2217 (setq pars (reverse pars))
2218 (save-window-excursion
2219 (while cmds
2220 (setq cmd (pop cmds)
2221 thiscmdkey (car cmd)
2222 cmd-or-set (nth 2 cmd)
2223 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2224 files (nth (if (listp cmd-or-set) 4 5) cmd))
2225 (if (stringp files) (setq files (list files)))
2226 (when files
2227 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2228 (list 'org-agenda nil thiscmdkey)))
2229 (set-buffer org-agenda-buffer-name)
2230 (while files
2231 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2232 (list 'org-write-agenda
2233 (expand-file-name (pop files) dir) nil t))))
2234 (and (get-buffer org-agenda-buffer-name)
2235 (kill-buffer org-agenda-buffer-name)))))))
2237 (defun org-agenda-mark-header-line (pos)
2238 "Mark the line at POS as an agenda structure header."
2239 (save-excursion
2240 (goto-char pos)
2241 (put-text-property (point-at-bol) (point-at-eol)
2242 'org-agenda-structural-header t)
2243 (when org-agenda-title-append
2244 (put-text-property (point-at-bol) (point-at-eol)
2245 'org-agenda-title-append org-agenda-title-append))))
2247 (defvar org-mobile-creating-agendas)
2248 (defun org-write-agenda (file &optional open nosettings)
2249 "Write the current buffer (an agenda view) as a file.
2250 Depending on the extension of the file name, plain text (.txt),
2251 HTML (.html or .htm) or Postscript (.ps) is produced.
2252 If the extension is .ics, run icalendar export over all files used
2253 to construct the agenda and limit the export to entries listed in the
2254 agenda now.
2255 With prefic argument OPEN, open the new file immediately.
2256 If NOSETTINGS is given, do not scope the settings of
2257 `org-agenda-exporter-settings' into the export commands. This is used when
2258 the settings have already been scoped and we do not wish to overrule other,
2259 higher priority settings."
2260 (interactive "FWrite agenda to file: \nP")
2261 (if (not (file-writable-p file))
2262 (error "Cannot write agenda to file %s" file))
2263 (cond
2264 ((string-match "\\.html?\\'" file) (require 'htmlize))
2265 ((string-match "\\.ps\\'" file) (require 'ps-print)))
2266 (org-let (if nosettings nil org-agenda-exporter-settings)
2267 '(save-excursion
2268 (save-window-excursion
2269 (org-agenda-mark-filtered-text)
2270 (let ((bs (copy-sequence (buffer-string))) beg)
2271 (org-agenda-unmark-filtered-text)
2272 (with-temp-buffer
2273 (insert bs)
2274 (org-agenda-remove-marked-text 'org-filtered)
2275 (while (setq beg (text-property-any (point-min) (point-max)
2276 'org-filtered t))
2277 (delete-region
2278 beg (or (next-single-property-change beg 'org-filtered)
2279 (point-max))))
2280 (run-hooks 'org-agenda-before-write-hook)
2281 (cond
2282 ((org-bound-and-true-p org-mobile-creating-agendas)
2283 (org-mobile-write-agenda-for-mobile file))
2284 ((string-match "\\.html?\\'" file)
2285 (set-buffer (htmlize-buffer (current-buffer)))
2287 (when (and org-agenda-export-html-style
2288 (string-match "<style>" org-agenda-export-html-style))
2289 ;; replace <style> section with org-agenda-export-html-style
2290 (goto-char (point-min))
2291 (kill-region (- (search-forward "<style") 6)
2292 (search-forward "</style>"))
2293 (insert org-agenda-export-html-style))
2294 (write-file file)
2295 (kill-buffer (current-buffer))
2296 (message "HTML written to %s" file))
2297 ((string-match "\\.ps\\'" file)
2298 (require 'ps-print)
2299 (flet ((ps-get-buffer-name () "Agenda View"))
2300 (ps-print-buffer-with-faces file))
2301 (message "Postscript written to %s" file))
2302 ((string-match "\\.pdf\\'" file)
2303 (require 'ps-print)
2304 (flet ((ps-get-buffer-name () "Agenda View"))
2305 (ps-print-buffer-with-faces
2306 (concat (file-name-sans-extension file) ".ps")))
2307 (call-process "ps2pdf" nil nil nil
2308 (expand-file-name
2309 (concat (file-name-sans-extension file) ".ps"))
2310 (expand-file-name file))
2311 (message "PDF written to %s" file))
2312 ((string-match "\\.ics\\'" file)
2313 (require 'org-icalendar)
2314 (let ((org-agenda-marker-table
2315 (org-create-marker-find-array
2316 (org-agenda-collect-markers)))
2317 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2318 (org-combined-agenda-icalendar-file file))
2319 (apply 'org-export-icalendar 'combine
2320 (org-agenda-files nil 'ifmode))))
2322 (let ((bs (buffer-string)))
2323 (find-file file)
2324 (erase-buffer)
2325 (insert bs)
2326 (save-buffer 0)
2327 (kill-buffer (current-buffer))
2328 (message "Plain text written to %s" file))))))))
2329 (set-buffer org-agenda-buffer-name))
2330 (when open (org-open-file file)))
2332 (defvar org-agenda-filter-overlays nil)
2334 (defun org-agenda-mark-filtered-text ()
2335 "Mark all text hidden by filtering with a text property."
2336 (let ((inhibit-read-only t))
2337 (mapc
2338 (lambda (o)
2339 (when (equal (org-overlay-buffer o) (current-buffer))
2340 (put-text-property
2341 (org-overlay-start o) (org-overlay-end o)
2342 'org-filtered t)))
2343 org-agenda-filter-overlays)))
2345 (defun org-agenda-unmark-filtered-text ()
2346 "Remove the filtering text property."
2347 (let ((inhibit-read-only t))
2348 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2350 (defun org-agenda-remove-marked-text (property &optional value)
2351 "Delete all text marked with VALUE of PROPERTY.
2352 VALUE defaults to t."
2353 (let (beg)
2354 (setq value (or value t))
2355 (while (setq beg (text-property-any (point-min) (point-max)
2356 property value))
2357 (delete-region
2358 beg (or (next-single-property-change beg 'org-filtered)
2359 (point-max))))))
2361 (defun org-agenda-add-entry-text ()
2362 "Add entry text to agenda lines.
2363 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2364 entry text following headings shown in the agenda.
2365 Drawers will be excluded, also the line with scheduling/deadline info."
2366 (when (> org-agenda-add-entry-text-maxlines 0)
2367 (let (m txt)
2368 (goto-char (point-min))
2369 (while (not (eobp))
2370 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2371 (beginning-of-line 2)
2372 (setq txt (org-agenda-get-some-entry-text
2373 m org-agenda-add-entry-text-maxlines " > "))
2374 (end-of-line 1)
2375 (if (string-match "\\S-" txt) (insert "\n" txt)))))))
2377 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2378 &rest keep)
2379 "Extract entry text from MARKER, at most N-LINES lines.
2380 This will ignore drawers etc, just get the text.
2381 If INDENT is given, prefix every line with this string. If KEEP is
2382 given, it is a list of symbols, defining stuff that hould not be
2383 removed from the entry content. Currently only `planning' is allowed here."
2384 (let (txt drawer-re kwd-time-re ind)
2385 (save-excursion
2386 (with-current-buffer (marker-buffer marker)
2387 (if (not (org-mode-p))
2388 (setq txt "")
2389 (save-excursion
2390 (save-restriction
2391 (widen)
2392 (goto-char marker)
2393 (end-of-line 1)
2394 (setq txt (buffer-substring
2395 (min (1+ (point)) (point-max))
2396 (progn (outline-next-heading) (point)))
2397 drawer-re org-drawer-regexp
2398 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2399 ".*\n?"))
2400 (with-temp-buffer
2401 (insert txt)
2402 (when org-agenda-add-entry-text-descriptive-links
2403 (goto-char (point-min))
2404 (while (org-activate-bracket-links (point-max))
2405 (add-text-properties (match-beginning 0) (match-end 0)
2406 '(face org-link))))
2407 (goto-char (point-min))
2408 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2409 (set-text-properties (match-beginning 0) (match-end 0)
2410 nil))
2411 (goto-char (point-min))
2412 (while (re-search-forward drawer-re nil t)
2413 (delete-region
2414 (match-beginning 0)
2415 (progn (re-search-forward
2416 "^[ \t]*:END:.*\n?" nil 'move)
2417 (point))))
2418 (unless (member 'planning keep)
2419 (goto-char (point-min))
2420 (while (re-search-forward kwd-time-re nil t)
2421 (replace-match "")))
2422 (goto-char (point-min))
2423 (when org-agenda-entry-text-exclude-regexps
2424 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2425 (while (setq re (pop re-list))
2426 (goto-char (point-min))
2427 (while (re-search-forward re nil t)
2428 (replace-match "")))))
2429 (goto-char (point-max))
2430 (skip-chars-backward " \t\n")
2431 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2433 ;; find and remove min common indentation
2434 (goto-char (point-min))
2435 (untabify (point-min) (point-max))
2436 (setq ind (org-get-indentation))
2437 (while (not (eobp))
2438 (unless (looking-at "[ \t]*$")
2439 (setq ind (min ind (org-get-indentation))))
2440 (beginning-of-line 2))
2441 (goto-char (point-min))
2442 (while (not (eobp))
2443 (unless (looking-at "[ \t]*$")
2444 (move-to-column ind)
2445 (delete-region (point-at-bol) (point)))
2446 (beginning-of-line 2))
2448 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2450 (goto-char (point-min))
2451 (when indent
2452 (while (and (not (eobp)) (re-search-forward "^" nil t))
2453 (replace-match indent t t)))
2454 (goto-char (point-min))
2455 (while (looking-at "[ \t]*\n") (replace-match ""))
2456 (goto-char (point-max))
2457 (when (> (org-current-line)
2458 n-lines)
2459 (org-goto-line (1+ n-lines))
2460 (backward-char 1))
2461 (setq txt (buffer-substring (point-min) (point)))))))))
2462 txt))
2464 (defun org-agenda-collect-markers ()
2465 "Collect the markers pointing to entries in the agenda buffer."
2466 (let (m markers)
2467 (save-excursion
2468 (goto-char (point-min))
2469 (while (not (eobp))
2470 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2471 (org-get-at-bol 'org-marker)))
2472 (push m markers))
2473 (beginning-of-line 2)))
2474 (nreverse markers)))
2476 (defun org-create-marker-find-array (marker-list)
2477 "Create a alist of files names with all marker positions in that file."
2478 (let (f tbl m a p)
2479 (while (setq m (pop marker-list))
2480 (setq p (marker-position m)
2481 f (buffer-file-name (or (buffer-base-buffer
2482 (marker-buffer m))
2483 (marker-buffer m))))
2484 (if (setq a (assoc f tbl))
2485 (push (marker-position m) (cdr a))
2486 (push (list f p) tbl)))
2487 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2488 tbl)))
2490 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
2491 (defun org-check-agenda-marker-table ()
2492 "Check of the current entry is on the marker list."
2493 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2495 (and (setq a (assoc file org-agenda-marker-table))
2496 (save-match-data
2497 (save-excursion
2498 (org-back-to-heading t)
2499 (member (point) (cdr a)))))))
2501 (defun org-check-for-org-mode ()
2502 "Make sure current buffer is in org-mode. Error if not."
2503 (or (org-mode-p)
2504 (error "Cannot execute org-mode agenda command on buffer in %s"
2505 major-mode)))
2507 (defun org-fit-agenda-window ()
2508 "Fit the window to the buffer size."
2509 (and (memq org-agenda-window-setup '(reorganize-frame))
2510 (fboundp 'fit-window-to-buffer)
2511 (org-fit-window-to-buffer
2513 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
2514 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
2516 ;;; Agenda prepare and finalize
2518 (defvar org-agenda-multi nil) ; dynamically scoped
2519 (defvar org-agenda-buffer-name "*Org Agenda*")
2520 (defvar org-pre-agenda-window-conf nil)
2521 (defvar org-agenda-columns-active nil)
2522 (defvar org-agenda-name nil)
2523 (defvar org-agenda-filter nil)
2524 (defvar org-agenda-filter-preset nil
2525 "A preset of the tags filter used for secondary agenda filtering.
2526 This must be a list of strings, each string must be a single tag preceeded
2527 by \"+\" or \"-\".
2528 This variable should not be set directly, but agenda custom commands can
2529 bind it in the options section.")
2531 (defun org-prepare-agenda (&optional name)
2532 (setq org-todo-keywords-for-agenda nil)
2533 (setq org-done-keywords-for-agenda nil)
2534 (setq org-drawers-for-agenda nil)
2535 (setq org-agenda-filter nil)
2536 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
2537 (if org-agenda-multi
2538 (progn
2539 (setq buffer-read-only nil)
2540 (goto-char (point-max))
2541 (unless (or (bobp) org-agenda-compact-blocks)
2542 (insert "\n"
2543 (if (stringp org-agenda-block-separator)
2544 org-agenda-block-separator
2545 (make-string (window-width) org-agenda-block-separator))
2546 "\n"))
2547 (narrow-to-region (point) (point-max)))
2548 (org-agenda-reset-markers)
2549 (setq org-agenda-contributing-files nil)
2550 (setq org-agenda-columns-active nil)
2551 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
2552 (setq org-todo-keywords-for-agenda
2553 (org-uniquify org-todo-keywords-for-agenda))
2554 (setq org-done-keywords-for-agenda
2555 (org-uniquify org-done-keywords-for-agenda))
2556 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
2557 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
2558 (awin (get-buffer-window abuf)))
2559 (cond
2560 ((equal (current-buffer) abuf) nil)
2561 (awin (select-window awin))
2562 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
2563 ((equal org-agenda-window-setup 'current-window)
2564 (switch-to-buffer abuf))
2565 ((equal org-agenda-window-setup 'other-window)
2566 (org-switch-to-buffer-other-window abuf))
2567 ((equal org-agenda-window-setup 'other-frame)
2568 (switch-to-buffer-other-frame abuf))
2569 ((equal org-agenda-window-setup 'reorganize-frame)
2570 (delete-other-windows)
2571 (org-switch-to-buffer-other-window abuf))))
2572 (setq buffer-read-only nil)
2573 (let ((inhibit-read-only t)) (erase-buffer))
2574 (org-agenda-mode)
2575 (and name (not org-agenda-name)
2576 (org-set-local 'org-agenda-name name)))
2577 (setq buffer-read-only nil))
2579 (defun org-finalize-agenda ()
2580 "Finishing touch for the agenda buffer, called just before displaying it."
2581 (unless org-agenda-multi
2582 (save-excursion
2583 (let ((inhibit-read-only t))
2584 (goto-char (point-min))
2585 (while (org-activate-bracket-links (point-max))
2586 (add-text-properties (match-beginning 0) (match-end 0)
2587 '(face org-link)))
2588 (org-agenda-align-tags)
2589 (unless org-agenda-with-colors
2590 (remove-text-properties (point-min) (point-max) '(face nil))))
2591 (if (and (boundp 'org-agenda-overriding-columns-format)
2592 org-agenda-overriding-columns-format)
2593 (org-set-local 'org-agenda-overriding-columns-format
2594 org-agenda-overriding-columns-format))
2595 (if (and (boundp 'org-agenda-view-columns-initially)
2596 org-agenda-view-columns-initially)
2597 (org-agenda-columns))
2598 (when org-agenda-fontify-priorities
2599 (org-agenda-fontify-priorities))
2600 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
2601 (org-agenda-dim-blocked-tasks))
2602 (org-agenda-mark-clocking-task)
2603 (when org-agenda-entry-text-mode
2604 (org-agenda-entry-text-hide)
2605 (org-agenda-entry-text-show))
2606 (if (functionp 'org-habit-insert-consistency-graphs)
2607 (org-habit-insert-consistency-graphs))
2608 (run-hooks 'org-finalize-agenda-hook)
2609 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
2610 (when (get 'org-agenda-filter :preset-filter)
2611 (org-agenda-filter-apply org-agenda-filter))
2614 (defun org-agenda-mark-clocking-task ()
2615 "Mark the current clock entry in the agenda if it is present."
2616 (mapc (lambda (o)
2617 (if (eq (org-overlay-get o 'type) 'org-agenda-clocking)
2618 (org-delete-overlay o)))
2619 (org-overlays-in (point-min) (point-max)))
2620 (when (marker-buffer org-clock-hd-marker)
2621 (save-excursion
2622 (goto-char (point-min))
2623 (let (s ov)
2624 (while (setq s (next-single-property-change (point) 'org-hd-marker))
2625 (goto-char s)
2626 (when (equal (org-get-at-bol 'org-hd-marker)
2627 org-clock-hd-marker)
2628 (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-eol))))
2629 (org-overlay-put ov 'type 'org-agenda-clocking)
2630 (org-overlay-put ov 'face 'org-agenda-clocking)
2631 (org-overlay-put ov 'help-echo
2632 "The clock is running in this item")))))))
2634 (defun org-agenda-fontify-priorities ()
2635 "Make highest priority lines bold, and lowest italic."
2636 (interactive)
2637 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
2638 (org-delete-overlay o)))
2639 (org-overlays-in (point-min) (point-max)))
2640 (save-excursion
2641 (let ((inhibit-read-only t)
2642 b e p ov h l)
2643 (goto-char (point-min))
2644 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
2645 (setq h (or (get-char-property (point) 'org-highest-priority)
2646 org-highest-priority)
2647 l (or (get-char-property (point) 'org-lowest-priority)
2648 org-lowest-priority)
2649 p (string-to-char (match-string 1))
2650 b (match-beginning 0)
2651 e (if (eq org-agenda-fontify-priorities 'cookies)
2652 (match-end 0)
2653 (point-at-eol))
2654 ov (org-make-overlay b e))
2655 (org-overlay-put
2656 ov 'face
2657 (cond ((cdr (assoc p org-priority-faces)))
2658 ((and (listp org-agenda-fontify-priorities)
2659 (cdr (assoc p org-agenda-fontify-priorities))))
2660 ((equal p l) 'italic)
2661 ((equal p h) 'bold)))
2662 (org-overlay-put ov 'org-type 'org-priority)))))
2664 (defun org-agenda-dim-blocked-tasks ()
2665 "Dim currently blocked TODO's in the agenda display."
2666 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-blocked-todo)
2667 (org-delete-overlay o)))
2668 (org-overlays-in (point-min) (point-max)))
2669 (save-excursion
2670 (let ((inhibit-read-only t)
2671 (org-depend-tag-blocked nil)
2672 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
2673 org-blocked-by-checkboxes
2674 invis1 b e p ov h l)
2675 (goto-char (point-min))
2676 (while (let ((pos (next-single-property-change (point) 'todo-state)))
2677 (and pos (goto-char (1+ pos))))
2678 (setq org-blocked-by-checkboxes nil invis1 invis)
2679 (let ((marker (org-get-at-bol 'org-hd-marker)))
2680 (when (and marker
2681 (not (with-current-buffer (marker-buffer marker)
2682 (save-excursion
2683 (goto-char marker)
2684 (if (org-entry-get nil "NOBLOCKING")
2685 t ;; Never block this entry
2686 (run-hook-with-args-until-failure
2687 'org-blocker-hook
2688 (list :type 'todo-state-change
2689 :position marker
2690 :from 'todo
2691 :to 'done)))))))
2692 (if org-blocked-by-checkboxes (setq invis1 nil))
2693 (setq b (if invis1
2694 (max (point-min) (1- (point-at-bol)))
2695 (point-at-bol))
2696 e (point-at-eol)
2697 ov (org-make-overlay b e))
2698 (if invis1
2699 (org-overlay-put ov 'invisible t)
2700 (org-overlay-put ov 'face 'org-agenda-dimmed-todo-face))
2701 (org-overlay-put ov 'org-type 'org-blocked-todo)))))))
2703 (defvar org-agenda-skip-function nil
2704 "Function to be called at each match during agenda construction.
2705 If this function returns nil, the current match should not be skipped.
2706 Otherwise, the function must return a position from where the search
2707 should be continued.
2708 This may also be a Lisp form, it will be evaluated.
2709 Never set this variable using `setq' or so, because then it will apply
2710 to all future agenda commands. Instead, bind it with `let' to scope
2711 it dynamically into the agenda-constructing command. A good way to set
2712 it is through options in org-agenda-custom-commands.")
2714 (defun org-agenda-skip ()
2715 "Throw to `:skip' in places that should be skipped.
2716 Also moves point to the end of the skipped region, so that search can
2717 continue from there."
2718 (let ((p (point-at-bol)) to fp)
2719 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
2720 (get-text-property p :org-archived)
2721 (org-end-of-subtree t)
2722 (throw :skip t))
2723 (and org-agenda-skip-comment-trees
2724 (get-text-property p :org-comment)
2725 (org-end-of-subtree t)
2726 (throw :skip t))
2727 (if (equal (char-after p) ?#) (throw :skip t))
2728 (when (and (or (setq fp (functionp org-agenda-skip-function))
2729 (consp org-agenda-skip-function))
2730 (setq to (save-excursion
2731 (save-match-data
2732 (if fp
2733 (funcall org-agenda-skip-function)
2734 (eval org-agenda-skip-function))))))
2735 (goto-char to)
2736 (throw :skip t))))
2738 (defvar org-agenda-markers nil
2739 "List of all currently active markers created by `org-agenda'.")
2740 (defvar org-agenda-last-marker-time (org-float-time)
2741 "Creation time of the last agenda marker.")
2743 (defun org-agenda-new-marker (&optional pos)
2744 "Return a new agenda marker.
2745 Org-mode keeps a list of these markers and resets them when they are
2746 no longer in use."
2747 (let ((m (copy-marker (or pos (point)))))
2748 (setq org-agenda-last-marker-time (org-float-time))
2749 (push m org-agenda-markers)
2752 (defun org-agenda-reset-markers ()
2753 "Reset markers created by `org-agenda'."
2754 (while org-agenda-markers
2755 (move-marker (pop org-agenda-markers) nil)))
2757 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
2758 "Save relative positions of markers in region."
2759 (mapc (lambda (m) (org-check-and-save-marker m beg end))
2760 org-agenda-markers))
2762 ;;; Entry text mode
2764 (defun org-agenda-entry-text-show-here ()
2765 "Add some text from te entry as context to the current line."
2766 (let (m txt o)
2767 (setq m (org-get-at-bol 'org-hd-marker))
2768 (unless (marker-buffer m)
2769 (error "No marker points to an entry here"))
2770 (setq txt (concat "\n" (org-no-properties
2771 (org-agenda-get-some-entry-text
2772 m org-agenda-entry-text-maxlines " > "))))
2773 (when (string-match "\\S-" txt)
2774 (setq o (org-make-overlay (point-at-bol) (point-at-eol)))
2775 (org-overlay-put o 'evaporate t)
2776 (org-overlay-put o 'org-overlay-type 'agenda-entry-content)
2777 (org-overlay-put o 'after-string txt))))
2779 (defun org-agenda-entry-text-show ()
2780 "Add entry context for all agenda lines."
2781 (interactive)
2782 (save-excursion
2783 (goto-char (point-max))
2784 (beginning-of-line 1)
2785 (while (not (bobp))
2786 (when (org-get-at-bol 'org-hd-marker)
2787 (org-agenda-entry-text-show-here))
2788 (beginning-of-line 0))))
2790 (defun org-agenda-entry-text-hide ()
2791 "Remove any shown entry context."
2792 (delq nil
2793 (mapcar (lambda (o)
2794 (if (eq (org-overlay-get o 'org-overlay-type)
2795 'agenda-entry-content)
2796 (progn (org-delete-overlay o) t)))
2797 (org-overlays-in (point-min) (point-max)))))
2799 ;;; Agenda timeline
2801 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
2803 (defun org-timeline (&optional include-all)
2804 "Show a time-sorted view of the entries in the current org file.
2805 Only entries with a time stamp of today or later will be listed. With
2806 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
2807 under the current date.
2808 If the buffer contains an active region, only check the region for
2809 dates."
2810 (interactive "P")
2811 (require 'calendar)
2812 (org-compile-prefix-format 'timeline)
2813 (org-set-sorting-strategy 'timeline)
2814 (let* ((dopast t)
2815 (dotodo include-all)
2816 (doclosed org-agenda-show-log)
2817 (entry buffer-file-name)
2818 (date (calendar-current-date))
2819 (beg (if (org-region-active-p) (region-beginning) (point-min)))
2820 (end (if (org-region-active-p) (region-end) (point-max)))
2821 (day-numbers (org-get-all-dates beg end 'no-ranges
2822 t doclosed ; always include today
2823 org-timeline-show-empty-dates))
2824 (org-deadline-warning-days 0)
2825 (org-agenda-only-exact-dates t)
2826 (today (time-to-days (current-time)))
2827 (past t)
2828 args
2829 s e rtn d emptyp wd)
2830 (setq org-agenda-redo-command
2831 (list 'progn
2832 (list 'org-switch-to-buffer-other-window (current-buffer))
2833 (list 'org-timeline (list 'quote include-all))))
2834 (if (not dopast)
2835 ;; Remove past dates from the list of dates.
2836 (setq day-numbers (delq nil (mapcar (lambda(x)
2837 (if (>= x today) x nil))
2838 day-numbers))))
2839 (org-prepare-agenda (concat "Timeline "
2840 (file-name-nondirectory buffer-file-name)))
2841 (if doclosed (push :closed args))
2842 (push :timestamp args)
2843 (push :deadline args)
2844 (push :scheduled args)
2845 (push :sexp args)
2846 (if dotodo (push :todo args))
2847 (insert "Timeline of file " entry "\n")
2848 (add-text-properties (point-min) (point)
2849 (list 'face 'org-agenda-structure))
2850 (org-agenda-mark-header-line (point-min))
2851 (while (setq d (pop day-numbers))
2852 (if (and (listp d) (eq (car d) :omitted))
2853 (progn
2854 (setq s (point))
2855 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
2856 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
2857 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
2858 (if (and (>= d today)
2859 dopast
2860 past)
2861 (progn
2862 (setq past nil)
2863 (insert (make-string 79 ?-) "\n")))
2864 (setq date (calendar-gregorian-from-absolute d)
2865 wd (calendar-day-of-week date))
2866 (setq s (point))
2867 (setq rtn (and (not emptyp)
2868 (apply 'org-agenda-get-day-entries entry
2869 date args)))
2870 (if (or rtn (equal d today) org-timeline-show-empty-dates)
2871 (progn
2872 (insert
2873 (if (stringp org-agenda-format-date)
2874 (format-time-string org-agenda-format-date
2875 (org-time-from-absolute date))
2876 (funcall org-agenda-format-date date))
2877 "\n")
2878 (put-text-property s (1- (point)) 'face
2879 (if (member wd org-agenda-weekend-days)
2880 'org-agenda-date-weekend
2881 'org-agenda-date))
2882 (put-text-property s (1- (point)) 'org-date-line t)
2883 (put-text-property s (1- (point)) 'org-agenda-date-header t)
2884 (if (equal d today)
2885 (put-text-property s (1- (point)) 'org-today t))
2886 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
2887 (put-text-property s (1- (point)) 'day d)))))
2888 (goto-char (point-min))
2889 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
2890 (point-min)))
2891 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
2892 (org-finalize-agenda)
2893 (setq buffer-read-only t)))
2895 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
2896 "Return a list of all relevant day numbers from BEG to END buffer positions.
2897 If NO-RANGES is non-nil, include only the start and end dates of a range,
2898 not every single day in the range. If FORCE-TODAY is non-nil, make
2899 sure that TODAY is included in the list. If INACTIVE is non-nil, also
2900 inactive time stamps (those in square brackets) are included.
2901 When EMPTY is non-nil, also include days without any entries."
2902 (let ((re (concat
2903 (if pre-re pre-re "")
2904 (if inactive org-ts-regexp-both org-ts-regexp)))
2905 dates dates1 date day day1 day2 ts1 ts2)
2906 (if force-today
2907 (setq dates (list (time-to-days (current-time)))))
2908 (save-excursion
2909 (goto-char beg)
2910 (while (re-search-forward re end t)
2911 (setq day (time-to-days (org-time-string-to-time
2912 (substring (match-string 1) 0 10))))
2913 (or (memq day dates) (push day dates)))
2914 (unless no-ranges
2915 (goto-char beg)
2916 (while (re-search-forward org-tr-regexp end t)
2917 (setq ts1 (substring (match-string 1) 0 10)
2918 ts2 (substring (match-string 2) 0 10)
2919 day1 (time-to-days (org-time-string-to-time ts1))
2920 day2 (time-to-days (org-time-string-to-time ts2)))
2921 (while (< (setq day1 (1+ day1)) day2)
2922 (or (memq day1 dates) (push day1 dates)))))
2923 (setq dates (sort dates '<))
2924 (when empty
2925 (while (setq day (pop dates))
2926 (setq day2 (car dates))
2927 (push day dates1)
2928 (when (and day2 empty)
2929 (if (or (eq empty t)
2930 (and (numberp empty) (<= (- day2 day) empty)))
2931 (while (< (setq day (1+ day)) day2)
2932 (push (list day) dates1))
2933 (push (cons :omitted (- day2 day)) dates1))))
2934 (setq dates (nreverse dates1)))
2935 dates)))
2937 ;;; Agenda Daily/Weekly
2939 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2940 (defvar org-agenda-start-day nil ; dynamically scoped parameter
2941 "Custom commands can set this variable in the options section.")
2942 (defvar org-agenda-last-arguments nil
2943 "The arguments of the previous call to org-agenda")
2944 (defvar org-starting-day nil) ; local variable in the agenda buffer
2945 (defvar org-agenda-span nil) ; local variable in the agenda buffer
2946 (defvar org-include-all-loc nil) ; local variable
2947 (defvar org-agenda-remove-date nil) ; dynamically scoped FIXME: not used???
2949 ;;;###autoload
2950 (defun org-agenda-list (&optional include-all start-day ndays)
2951 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
2952 The view will be for the current day or week, but from the overview buffer
2953 you will be able to go to other days/weeks.
2955 With one \\[universal-argument] prefix argument INCLUDE-ALL,
2956 all unfinished TODO items will also be shown, before the agenda.
2957 This feature is considered obsolete, please use the TODO list or a block
2958 agenda instead.
2960 With a numeric prefix argument in an interactive call, the agenda will
2961 span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
2962 the number of days. NDAYS defaults to `org-agenda-ndays'.
2964 START-DAY defaults to TODAY, or to the most recent match for the weekday
2965 given in `org-agenda-start-on-weekday'."
2966 (interactive "P")
2967 (if (and (integerp include-all) (> include-all 0))
2968 (setq ndays include-all include-all nil))
2969 (setq ndays (or ndays org-agenda-ndays)
2970 start-day (or start-day org-agenda-start-day))
2971 (if org-agenda-overriding-arguments
2972 (setq include-all (car org-agenda-overriding-arguments)
2973 start-day (nth 1 org-agenda-overriding-arguments)
2974 ndays (nth 2 org-agenda-overriding-arguments)))
2975 (if (stringp start-day)
2976 ;; Convert to an absolute day number
2977 (setq start-day (time-to-days (org-read-date nil t start-day))))
2978 (setq org-agenda-last-arguments (list include-all start-day ndays))
2979 (org-compile-prefix-format 'agenda)
2980 (org-set-sorting-strategy 'agenda)
2981 (require 'calendar)
2982 (let* ((org-agenda-start-on-weekday
2983 (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
2984 org-agenda-start-on-weekday nil))
2985 (thefiles (org-agenda-files nil 'ifmode))
2986 (files thefiles)
2987 (today (time-to-days
2988 (time-subtract (current-time)
2989 (list 0 (* 3600 org-extend-today-until) 0))))
2990 (sd (or start-day today))
2991 (start (if (or (null org-agenda-start-on-weekday)
2992 (< org-agenda-ndays 7))
2994 (let* ((nt (calendar-day-of-week
2995 (calendar-gregorian-from-absolute sd)))
2996 (n1 org-agenda-start-on-weekday)
2997 (d (- nt n1)))
2998 (- sd (+ (if (< d 0) 7 0) d)))))
2999 (day-numbers (list start))
3000 (day-cnt 0)
3001 (inhibit-redisplay (not debug-on-error))
3002 s e rtn rtnall file date d start-pos end-pos todayp nd wd
3003 clocktable-start clocktable-end)
3004 (setq org-agenda-redo-command
3005 (list 'org-agenda-list (list 'quote include-all) start-day ndays))
3006 ;; Make the list of days
3007 (setq ndays (or ndays org-agenda-ndays)
3008 nd ndays)
3009 (while (> ndays 1)
3010 (push (1+ (car day-numbers)) day-numbers)
3011 (setq ndays (1- ndays)))
3012 (setq day-numbers (nreverse day-numbers))
3013 (setq clocktable-start (car day-numbers)
3014 clocktable-end (1+ (or (org-last day-numbers) 0)))
3015 (org-prepare-agenda "Day/Week")
3016 (org-set-local 'org-starting-day (car day-numbers))
3017 (org-set-local 'org-include-all-loc include-all)
3018 (org-set-local 'org-agenda-span
3019 (org-agenda-ndays-to-span nd))
3020 (when (and (or include-all org-agenda-include-all-todo)
3021 (member today day-numbers))
3022 (setq files thefiles
3023 rtnall nil)
3024 (while (setq file (pop files))
3025 (catch 'nextfile
3026 (org-check-agenda-file file)
3027 (setq date (calendar-gregorian-from-absolute today)
3028 rtn (org-agenda-get-day-entries
3029 file date :todo))
3030 (setq rtnall (append rtnall rtn))))
3031 (when rtnall
3032 (insert "All currently open TODO items:\n")
3033 (add-text-properties (point-min) (1- (point))
3034 (list 'face 'org-agenda-structure
3035 'short-heading "All TODO items"))
3036 (org-agenda-mark-header-line (point-min))
3037 (insert (org-finalize-agenda-entries rtnall) "\n")))
3038 (unless org-agenda-compact-blocks
3039 (let* ((d1 (car day-numbers))
3040 (d2 (org-last day-numbers))
3041 (w1 (org-days-to-iso-week d1))
3042 (w2 (org-days-to-iso-week d2)))
3043 (setq s (point))
3044 (if org-agenda-overriding-header
3045 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3046 nil 'face 'org-agenda-structure) "\n")
3047 (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
3048 "-agenda"
3049 (if (< (- d2 d1) 350)
3050 (if (= w1 w2)
3051 (format " (W%02d)" w1)
3052 (format " (W%02d-W%02d)" w1 w2))
3054 ":\n")))
3055 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3056 'org-date-line t))
3057 (org-agenda-mark-header-line s))
3058 (while (setq d (pop day-numbers))
3059 (setq date (calendar-gregorian-from-absolute d)
3060 wd (calendar-day-of-week date)
3061 s (point))
3062 (if (or (setq todayp (= d today))
3063 (and (not start-pos) (= d sd)))
3064 (setq start-pos (point))
3065 (if (and start-pos (not end-pos))
3066 (setq end-pos (point))))
3067 (setq files thefiles
3068 rtnall nil)
3069 (while (setq file (pop files))
3070 (catch 'nextfile
3071 (org-check-agenda-file file)
3072 (cond
3073 ((eq org-agenda-show-log 'only)
3074 (setq rtn (org-agenda-get-day-entries
3075 file date :closed)))
3076 (org-agenda-show-log
3077 (setq rtn (org-agenda-get-day-entries
3078 file date
3079 :deadline :scheduled :timestamp :sexp :closed)))
3081 (setq rtn (org-agenda-get-day-entries
3082 file date
3083 :deadline :scheduled :sexp :timestamp))))
3084 (setq rtnall (append rtnall rtn))))
3085 (if org-agenda-include-diary
3086 (let ((org-agenda-search-headline-for-time t))
3087 (require 'diary-lib)
3088 (setq rtn (org-get-entries-from-diary date))
3089 (setq rtnall (append rtnall rtn))))
3090 (if (or rtnall org-agenda-show-all-dates)
3091 (progn
3092 (setq day-cnt (1+ day-cnt))
3093 (insert
3094 (if (stringp org-agenda-format-date)
3095 (format-time-string org-agenda-format-date
3096 (org-time-from-absolute date))
3097 (funcall org-agenda-format-date date))
3098 "\n")
3099 (put-text-property s (1- (point)) 'face
3100 (if (member wd org-agenda-weekend-days)
3101 'org-agenda-date-weekend
3102 'org-agenda-date))
3103 (put-text-property s (1- (point)) 'org-date-line t)
3104 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3105 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3106 (when todayp
3107 (put-text-property s (1- (point)) 'org-today t)
3108 (put-text-property s (1- (point)) 'face 'org-agenda-date-today))
3109 (if rtnall (insert
3110 (org-finalize-agenda-entries
3111 (org-agenda-add-time-grid-maybe
3112 rtnall nd todayp))
3113 "\n"))
3114 (put-text-property s (1- (point)) 'day d)
3115 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3116 (when (and org-agenda-clockreport-mode clocktable-start)
3117 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3118 ;; the above line is to ensure the restricted range!
3119 (p org-agenda-clockreport-parameter-plist)
3120 tbl)
3121 (setq p (org-plist-delete p :block))
3122 (setq p (plist-put p :tstart clocktable-start))
3123 (setq p (plist-put p :tend clocktable-end))
3124 (setq p (plist-put p :scope 'agenda))
3125 (setq tbl (apply 'org-get-clocktable p))
3126 (insert tbl)))
3127 (goto-char (point-min))
3128 (or org-agenda-multi (org-fit-agenda-window))
3129 (unless (and (pos-visible-in-window-p (point-min))
3130 (pos-visible-in-window-p (point-max)))
3131 (goto-char (1- (point-max)))
3132 (recenter -1)
3133 (if (not (pos-visible-in-window-p (or start-pos 1)))
3134 (progn
3135 (goto-char (or start-pos 1))
3136 (recenter 1))))
3137 (goto-char (or start-pos 1))
3138 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3139 (org-finalize-agenda)
3140 (setq buffer-read-only t)
3141 (message "")))
3143 (defun org-agenda-ndays-to-span (n)
3144 (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
3146 ;;; Agenda word search
3148 (defvar org-agenda-search-history nil)
3149 (defvar org-todo-only nil)
3151 (defvar org-search-syntax-table nil
3152 "Special syntax table for org-mode search.
3153 In this table, we have single quotes not as word constituents, to
3154 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3156 (defun org-search-syntax-table ()
3157 (unless org-search-syntax-table
3158 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3159 (modify-syntax-entry ?' "." org-search-syntax-table)
3160 (modify-syntax-entry ?` "." org-search-syntax-table))
3161 org-search-syntax-table)
3163 ;;;###autoload
3164 (defun org-search-view (&optional todo-only string edit-at)
3165 "Show all entries that contain words or regular expressions.
3166 If the first character of the search string is an asterisks,
3167 search only the headlines.
3169 With optional prefix argument TODO-ONLY, only consider entries that are
3170 TODO entries. The argument STRING can be used to pass a default search
3171 string into this function. If EDIT-AT is non-nil, it means that the
3172 user should get a chance to edit this string, with cursor at position
3173 EDIT-AT.
3175 The search string is broken into \"words\" by splitting at whitespace.
3176 Depending on the variable `org-agenda-search-view-search-words-only'
3177 and on wether the first character in the search string is \"+\" or \"-\",
3178 The string is then interpreted either as a substrig with variable amounts
3179 of whitespace, or as a list or individual words that should be matched.
3181 The default is a substring match, where each space in the search string
3182 can expand to an arbitrary amount of whitespace, including newlines.
3184 If matching individual words, these words are then interpreted as a
3185 boolean expression with logical AND. Words prefixed with a minus must
3186 not occur in the entry. Words without a prefix or prefixed with a plus
3187 must occur in the entry. Matching is case-insensitive and the words
3188 are enclosed by word delimiters.
3190 Words enclosed by curly braces are interpreted as regular expressions
3191 that must or must not match in the entry.
3193 If the search string starts with an asterisk, search only in headlines.
3194 If (possibly after the leading star) the search string starts with an
3195 exclamation mark, this also means to look at TODO entries only, an effect
3196 that can also be achieved with a prefix argument.
3198 This command searches the agenda files, and in addition the files listed
3199 in `org-agenda-text-search-extra-files'."
3200 (interactive "P")
3201 (org-compile-prefix-format 'search)
3202 (org-set-sorting-strategy 'search)
3203 (org-prepare-agenda "SEARCH")
3204 (let* ((props (list 'face nil
3205 'done-face 'org-agenda-done
3206 'org-not-done-regexp org-not-done-regexp
3207 'org-todo-regexp org-todo-regexp
3208 'org-complex-heading-regexp org-complex-heading-regexp
3209 'mouse-face 'highlight
3210 'keymap org-agenda-keymap
3211 'help-echo (format "mouse-2 or RET jump to location")))
3212 regexp rtn rtnall files file pos
3213 marker category tags c neg re as-words
3214 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3215 (unless (and (not edit-at)
3216 (stringp string)
3217 (string-match "\\S-" string))
3218 (setq string (read-string "[+-]Word/{Regexp} ...: "
3219 (cond
3220 ((integerp edit-at) (cons string edit-at))
3221 (edit-at string))
3222 'org-agenda-search-history)))
3223 (org-set-local 'org-todo-only todo-only)
3224 (setq org-agenda-redo-command
3225 (list 'org-search-view (if todo-only t nil) string
3226 '(if current-prefix-arg 1 nil)))
3227 (setq org-agenda-query-string string)
3229 (if (equal (string-to-char string) ?*)
3230 (setq hdl-only t
3231 words (substring string 1))
3232 (setq words string))
3233 (when (equal (string-to-char words) ?!)
3234 (setq todo-only t
3235 words (substring words 1)))
3236 (if (or org-agenda-search-view-search-words-only
3237 (member (string-to-char string) '(?- ?+)))
3238 (setq as-words t))
3239 (setq words (org-split-string words))
3240 (if as-words
3241 (mapc (lambda (w)
3242 (setq c (string-to-char w))
3243 (if (equal c ?-)
3244 (setq neg t w (substring w 1))
3245 (if (equal c ?+)
3246 (setq neg nil w (substring w 1))
3247 (setq neg nil)))
3248 (if (string-match "\\`{.*}\\'" w)
3249 (setq re (substring w 1 -1))
3250 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
3251 (if neg (push re regexps-) (push re regexps+)))
3252 words)
3253 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3254 regexps+))
3255 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3256 (if (not regexps+)
3257 (setq regexp (concat "^" org-outline-regexp))
3258 (setq regexp (pop regexps+))
3259 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3260 regexp))))
3261 (setq files (org-agenda-files nil 'ifmode))
3262 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3263 (pop org-agenda-text-search-extra-files)
3264 (setq files (org-add-archive-files files)))
3265 (setq files (append files org-agenda-text-search-extra-files)
3266 rtnall nil)
3267 (while (setq file (pop files))
3268 (setq ee nil)
3269 (catch 'nextfile
3270 (org-check-agenda-file file)
3271 (setq buffer (if (file-exists-p file)
3272 (org-get-agenda-file-buffer file)
3273 (error "No such file %s" file)))
3274 (if (not buffer)
3275 ;; If file does not exist, make sure an error message is sent
3276 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3277 file))))
3278 (with-current-buffer buffer
3279 (with-syntax-table (org-search-syntax-table)
3280 (unless (org-mode-p)
3281 (error "Agenda file %s is not in `org-mode'" file))
3282 (let ((case-fold-search t))
3283 (save-excursion
3284 (save-restriction
3285 (if org-agenda-restrict
3286 (narrow-to-region org-agenda-restrict-begin
3287 org-agenda-restrict-end)
3288 (widen))
3289 (goto-char (point-min))
3290 (unless (or (org-on-heading-p)
3291 (outline-next-heading))
3292 (throw 'nextfile t))
3293 (goto-char (max (point-min) (1- (point))))
3294 (while (re-search-forward regexp nil t)
3295 (org-back-to-heading t)
3296 (skip-chars-forward "* ")
3297 (setq beg (point-at-bol)
3298 beg1 (point)
3299 end (progn (outline-next-heading) (point)))
3300 (catch :skip
3301 (goto-char beg)
3302 (org-agenda-skip)
3303 (setq str (buffer-substring-no-properties
3304 (point-at-bol)
3305 (if hdl-only (point-at-eol) end)))
3306 (mapc (lambda (wr) (when (string-match wr str)
3307 (goto-char (1- end))
3308 (throw :skip t)))
3309 regexps-)
3310 (mapc (lambda (wr) (unless (string-match wr str)
3311 (goto-char (1- end))
3312 (throw :skip t)))
3313 (if todo-only
3314 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3315 regexps+)
3316 regexps+))
3317 (goto-char beg)
3318 (setq marker (org-agenda-new-marker (point))
3319 category (org-get-category)
3320 tags (org-get-tags-at (point))
3321 txt (org-format-agenda-item
3323 (buffer-substring-no-properties
3324 beg1 (point-at-eol))
3325 category tags))
3326 (org-add-props txt props
3327 'org-marker marker 'org-hd-marker marker
3328 'org-todo-regexp org-todo-regexp
3329 'org-complex-heading-regexp org-complex-heading-regexp
3330 'priority 1000 'org-category category
3331 'type "search")
3332 (push txt ee)
3333 (goto-char (1- end))))))))))
3334 (setq rtn (nreverse ee))
3335 (setq rtnall (append rtnall rtn)))
3336 (if org-agenda-overriding-header
3337 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3338 nil 'face 'org-agenda-structure) "\n")
3339 (insert "Search words: ")
3340 (add-text-properties (point-min) (1- (point))
3341 (list 'face 'org-agenda-structure))
3342 (setq pos (point))
3343 (insert string "\n")
3344 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3345 (setq pos (point))
3346 (unless org-agenda-multi
3347 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3348 (add-text-properties pos (1- (point))
3349 (list 'face 'org-agenda-structure))))
3350 (org-agenda-mark-header-line (point-min))
3351 (when rtnall
3352 (insert (org-finalize-agenda-entries rtnall) "\n"))
3353 (goto-char (point-min))
3354 (or org-agenda-multi (org-fit-agenda-window))
3355 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3356 (org-finalize-agenda)
3357 (setq buffer-read-only t)))
3359 ;;; Agenda TODO list
3361 (defvar org-select-this-todo-keyword nil)
3362 (defvar org-last-arg nil)
3364 ;;;###autoload
3365 (defun org-todo-list (arg)
3366 "Show all TODO entries from all agenda file in a single list.
3367 The prefix arg can be used to select a specific TODO keyword and limit
3368 the list to these. When using \\[universal-argument], you will be prompted
3369 for a keyword. A numeric prefix directly selects the Nth keyword in
3370 `org-todo-keywords-1'."
3371 (interactive "P")
3372 (require 'calendar)
3373 (org-compile-prefix-format 'todo)
3374 (org-set-sorting-strategy 'todo)
3375 (org-prepare-agenda "TODO")
3376 (let* ((today (time-to-days (current-time)))
3377 (date (calendar-gregorian-from-absolute today))
3378 (kwds org-todo-keywords-for-agenda)
3379 (completion-ignore-case t)
3380 (org-select-this-todo-keyword
3381 (if (stringp arg) arg
3382 (and arg (integerp arg) (> arg 0)
3383 (nth (1- arg) kwds))))
3384 rtn rtnall files file pos)
3385 (when (equal arg '(4))
3386 (setq org-select-this-todo-keyword
3387 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
3388 (mapcar 'list kwds) nil nil)))
3389 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
3390 (org-set-local 'org-last-arg arg)
3391 (setq org-agenda-redo-command
3392 '(org-todo-list (or current-prefix-arg org-last-arg)))
3393 (setq files (org-agenda-files nil 'ifmode)
3394 rtnall nil)
3395 (while (setq file (pop files))
3396 (catch 'nextfile
3397 (org-check-agenda-file file)
3398 (setq rtn (org-agenda-get-day-entries file date :todo))
3399 (setq rtnall (append rtnall rtn))))
3400 (if org-agenda-overriding-header
3401 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3402 nil 'face 'org-agenda-structure) "\n")
3403 (insert "Global list of TODO items of type: ")
3404 (add-text-properties (point-min) (1- (point))
3405 (list 'face 'org-agenda-structure
3406 'short-heading
3407 (concat "ToDo: "
3408 (or org-select-this-todo-keyword "ALL"))))
3409 (org-agenda-mark-header-line (point-min))
3410 (setq pos (point))
3411 (insert (or org-select-this-todo-keyword "ALL") "\n")
3412 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3413 (setq pos (point))
3414 (unless org-agenda-multi
3415 (insert "Available with `N r': (0)ALL")
3416 (let ((n 0) s)
3417 (mapc (lambda (x)
3418 (setq s (format "(%d)%s" (setq n (1+ n)) x))
3419 (if (> (+ (current-column) (string-width s) 1) (frame-width))
3420 (insert "\n "))
3421 (insert " " s))
3422 kwds))
3423 (insert "\n"))
3424 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
3425 (org-agenda-mark-header-line (point-min))
3426 (when rtnall
3427 (insert (org-finalize-agenda-entries rtnall) "\n"))
3428 (goto-char (point-min))
3429 (or org-agenda-multi (org-fit-agenda-window))
3430 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
3431 (org-finalize-agenda)
3432 (setq buffer-read-only t)))
3434 ;;; Agenda tags match
3436 ;;;###autoload
3437 (defun org-tags-view (&optional todo-only match)
3438 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
3439 The prefix arg TODO-ONLY limits the search to TODO entries."
3440 (interactive "P")
3441 (org-compile-prefix-format 'tags)
3442 (org-set-sorting-strategy 'tags)
3443 (let* ((org-tags-match-list-sublevels
3444 ;?????? (if todo-only t org-tags-match-list-sublevels))
3445 org-tags-match-list-sublevels)
3446 (completion-ignore-case t)
3447 rtn rtnall files file pos matcher
3448 buffer)
3449 (setq matcher (org-make-tags-matcher match)
3450 match (car matcher) matcher (cdr matcher))
3451 (org-prepare-agenda (concat "TAGS " match))
3452 (setq org-agenda-query-string match)
3453 (setq org-agenda-redo-command
3454 (list 'org-tags-view (list 'quote todo-only)
3455 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
3456 (setq files (org-agenda-files nil 'ifmode)
3457 rtnall nil)
3458 (while (setq file (pop files))
3459 (catch 'nextfile
3460 (org-check-agenda-file file)
3461 (setq buffer (if (file-exists-p file)
3462 (org-get-agenda-file-buffer file)
3463 (error "No such file %s" file)))
3464 (if (not buffer)
3465 ;; If file does not exist, error message to agenda
3466 (setq rtn (list
3467 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3468 rtnall (append rtnall rtn))
3469 (with-current-buffer buffer
3470 (unless (org-mode-p)
3471 (error "Agenda file %s is not in `org-mode'" file))
3472 (save-excursion
3473 (save-restriction
3474 (if org-agenda-restrict
3475 (narrow-to-region org-agenda-restrict-begin
3476 org-agenda-restrict-end)
3477 (widen))
3478 (setq rtn (org-scan-tags 'agenda matcher todo-only))
3479 (setq rtnall (append rtnall rtn))))))))
3480 (if org-agenda-overriding-header
3481 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3482 nil 'face 'org-agenda-structure) "\n")
3483 (insert "Headlines with TAGS match: ")
3484 (add-text-properties (point-min) (1- (point))
3485 (list 'face 'org-agenda-structure
3486 'short-heading
3487 (concat "Match: " match)))
3488 (setq pos (point))
3489 (insert match "\n")
3490 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3491 (setq pos (point))
3492 (unless org-agenda-multi
3493 (insert "Press `C-u r' to search again with new search string\n"))
3494 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
3495 (org-agenda-mark-header-line (point-min))
3496 (when rtnall
3497 (insert (org-finalize-agenda-entries rtnall) "\n"))
3498 (goto-char (point-min))
3499 (or org-agenda-multi (org-fit-agenda-window))
3500 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
3501 (org-finalize-agenda)
3502 (setq buffer-read-only t)))
3504 ;;; Agenda Finding stuck projects
3506 (defvar org-agenda-skip-regexp nil
3507 "Regular expression used in skipping subtrees for the agenda.
3508 This is basically a temporary global variable that can be set and then
3509 used by user-defined selections using `org-agenda-skip-function'.")
3511 (defvar org-agenda-overriding-header nil
3512 "When this is set during todo and tags searches, will replace header.
3513 This variable should not be set directly, but custom commands can bind it
3514 in the options section.")
3516 (defun org-agenda-skip-entry-when-regexp-matches ()
3517 "Checks if the current entry contains match for `org-agenda-skip-regexp'.
3518 If yes, it returns the end position of this entry, causing agenda commands
3519 to skip the entry but continuing the search in the subtree. This is a
3520 function that can be put into `org-agenda-skip-function' for the duration
3521 of a command."
3522 (let ((end (save-excursion (org-end-of-subtree t)))
3523 skip)
3524 (save-excursion
3525 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3526 (and skip end)))
3528 (defun org-agenda-skip-subtree-when-regexp-matches ()
3529 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3530 If yes, it returns the end position of this tree, causing agenda commands
3531 to skip this subtree. This is a function that can be put into
3532 `org-agenda-skip-function' for the duration of a command."
3533 (let ((end (save-excursion (org-end-of-subtree t)))
3534 skip)
3535 (save-excursion
3536 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3537 (and skip end)))
3539 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
3540 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3541 If yes, it returns the end position of the current entry (NOT the tree),
3542 causing agenda commands to skip the entry but continuing the search in
3543 the subtree. This is a function that can be put into
3544 `org-agenda-skip-function' for the duration of a command. An important
3545 use of this function is for the stuck project list."
3546 (let ((end (save-excursion (org-end-of-subtree t)))
3547 (entry-end (save-excursion (outline-next-heading) (1- (point))))
3548 skip)
3549 (save-excursion
3550 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3551 (and skip entry-end)))
3553 (defun org-agenda-skip-entry-if (&rest conditions)
3554 "Skip entry if any of CONDITIONS is true.
3555 See `org-agenda-skip-if' for details."
3556 (org-agenda-skip-if nil conditions))
3558 (defun org-agenda-skip-subtree-if (&rest conditions)
3559 "Skip entry if any of CONDITIONS is true.
3560 See `org-agenda-skip-if' for details."
3561 (org-agenda-skip-if t conditions))
3563 (defun org-agenda-skip-if (subtree conditions)
3564 "Checks current entity for CONDITIONS.
3565 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
3566 the entry, i.e. the text before the next heading is checked.
3568 CONDITIONS is a list of symbols, boolean OR is used to combine the results
3569 from different tests. Valid conditions are:
3571 scheduled Check if there is a scheduled cookie
3572 notscheduled Check if there is no scheduled cookie
3573 deadline Check if there is a deadline
3574 notdeadline Check if there is no deadline
3575 timestamp Check if there is a timestamp (also deadline or scheduled)
3576 nottimestamp Check if there is no timestamp (also deadline or scheduled)
3577 regexp Check if regexp matches
3578 notregexp Check if regexp does not match.
3580 The regexp is taken from the conditions list, it must come right after
3581 the `regexp' or `notregexp' element.
3583 If any of these conditions is met, this function returns the end point of
3584 the entity, causing the search to continue from there. This is a function
3585 that can be put into `org-agenda-skip-function' for the duration of a command."
3586 (let (beg end m)
3587 (org-back-to-heading t)
3588 (setq beg (point)
3589 end (if subtree
3590 (progn (org-end-of-subtree t) (point))
3591 (progn (outline-next-heading) (1- (point)))))
3592 (goto-char beg)
3593 (and
3595 (and (memq 'scheduled conditions)
3596 (re-search-forward org-scheduled-time-regexp end t))
3597 (and (memq 'notscheduled conditions)
3598 (not (re-search-forward org-scheduled-time-regexp end t)))
3599 (and (memq 'deadline conditions)
3600 (re-search-forward org-deadline-time-regexp end t))
3601 (and (memq 'notdeadline conditions)
3602 (not (re-search-forward org-deadline-time-regexp end t)))
3603 (and (memq 'timestamp conditions)
3604 (re-search-forward org-ts-regexp end t))
3605 (and (memq 'nottimestamp conditions)
3606 (not (re-search-forward org-ts-regexp end t)))
3607 (and (setq m (memq 'regexp conditions))
3608 (stringp (nth 1 m))
3609 (re-search-forward (nth 1 m) end t))
3610 (and (setq m (memq 'notregexp conditions))
3611 (stringp (nth 1 m))
3612 (not (re-search-forward (nth 1 m) end t))))
3613 end)))
3615 ;;;###autoload
3616 (defun org-agenda-list-stuck-projects (&rest ignore)
3617 "Create agenda view for projects that are stuck.
3618 Stuck projects are project that have no next actions. For the definitions
3619 of what a project is and how to check if it stuck, customize the variable
3620 `org-stuck-projects'.
3621 MATCH is being ignored."
3622 (interactive)
3623 (let* ((org-agenda-skip-function
3624 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
3625 ;; We could have used org-agenda-skip-if here.
3626 (org-agenda-overriding-header
3627 (or org-agenda-overriding-header "List of stuck projects: "))
3628 (matcher (nth 0 org-stuck-projects))
3629 (todo (nth 1 org-stuck-projects))
3630 (todo-wds (if (member "*" todo)
3631 (progn
3632 (org-prepare-agenda-buffers (org-agenda-files
3633 nil 'ifmode))
3634 (org-delete-all
3635 org-done-keywords-for-agenda
3636 (copy-sequence org-todo-keywords-for-agenda)))
3637 todo))
3638 (todo-re (concat "^\\*+[ \t]+\\("
3639 (mapconcat 'identity todo-wds "\\|")
3640 "\\)\\>"))
3641 (tags (nth 2 org-stuck-projects))
3642 (tags-re (if (member "*" tags)
3643 (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
3644 (if tags
3645 (concat "^\\*+ .*:\\("
3646 (mapconcat 'identity tags "\\|")
3647 (org-re "\\):[[:alnum:]_@:]*[ \t]*$")))))
3648 (gen-re (nth 3 org-stuck-projects))
3649 (re-list
3650 (delq nil
3651 (list
3652 (if todo todo-re)
3653 (if tags tags-re)
3654 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
3655 gen-re)))))
3656 (setq org-agenda-skip-regexp
3657 (if re-list
3658 (mapconcat 'identity re-list "\\|")
3659 (error "No information how to identify unstuck projects")))
3660 (org-tags-view nil matcher)
3661 (with-current-buffer org-agenda-buffer-name
3662 (setq org-agenda-redo-command
3663 '(org-agenda-list-stuck-projects
3664 (or current-prefix-arg org-last-arg))))))
3666 ;;; Diary integration
3668 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
3669 (defvar list-diary-entries-hook)
3671 (defun org-get-entries-from-diary (date)
3672 "Get the (Emacs Calendar) diary entries for DATE."
3673 (require 'diary-lib)
3674 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
3675 (fancy-diary-buffer diary-fancy-buffer)
3676 (diary-display-hook '(fancy-diary-display))
3677 (diary-display-function 'fancy-diary-display)
3678 (pop-up-frames nil)
3679 (list-diary-entries-hook
3680 (cons 'org-diary-default-entry list-diary-entries-hook))
3681 (diary-file-name-prefix-function nil) ; turn this feature off
3682 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
3683 entries
3684 (org-disable-agenda-to-diary t))
3685 (save-excursion
3686 (save-window-excursion
3687 (funcall (if (fboundp 'diary-list-entries)
3688 'diary-list-entries 'list-diary-entries)
3689 date 1)))
3690 (if (not (get-buffer diary-fancy-buffer))
3691 (setq entries nil)
3692 (with-current-buffer diary-fancy-buffer
3693 (setq buffer-read-only nil)
3694 (if (zerop (buffer-size))
3695 ;; No entries
3696 (setq entries nil)
3697 ;; Omit the date and other unnecessary stuff
3698 (org-agenda-cleanup-fancy-diary)
3699 ;; Add prefix to each line and extend the text properties
3700 (if (zerop (buffer-size))
3701 (setq entries nil)
3702 (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
3703 (set-buffer-modified-p nil)
3704 (kill-buffer diary-fancy-buffer)))
3705 (when entries
3706 (setq entries (org-split-string entries "\n"))
3707 (setq entries
3708 (mapcar
3709 (lambda (x)
3710 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
3711 ;; Extend the text properties to the beginning of the line
3712 (org-add-props x (text-properties-at (1- (length x)) x)
3713 'type "diary" 'date date))
3714 entries)))))
3716 (defvar org-agenda-cleanup-fancy-diary-hook nil
3717 "Hook run when the fancy diary buffer is cleaned up.")
3719 (defun org-agenda-cleanup-fancy-diary ()
3720 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
3721 This gets rid of the date, the underline under the date, and
3722 the dummy entry installed by `org-mode' to ensure non-empty diary for each
3723 date. It also removes lines that contain only whitespace."
3724 (goto-char (point-min))
3725 (if (looking-at ".*?:[ \t]*")
3726 (progn
3727 (replace-match "")
3728 (re-search-forward "\n=+$" nil t)
3729 (replace-match "")
3730 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
3731 (re-search-forward "\n=+$" nil t)
3732 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
3733 (goto-char (point-min))
3734 (while (re-search-forward "^ +\n" nil t)
3735 (replace-match ""))
3736 (goto-char (point-min))
3737 (if (re-search-forward "^Org-mode dummy\n?" nil t)
3738 (replace-match ""))
3739 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
3741 ;; Make sure entries from the diary have the right text properties.
3742 (eval-after-load "diary-lib"
3743 '(if (boundp 'diary-modify-entry-list-string-function)
3744 ;; We can rely on the hook, nothing to do
3746 ;; Hook not available, must use advice to make this work
3747 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
3748 "Make the position visible."
3749 (if (and org-disable-agenda-to-diary ;; called from org-agenda
3750 (stringp string)
3751 buffer-file-name)
3752 (setq string (org-modify-diary-entry-string string))))))
3754 (defun org-modify-diary-entry-string (string)
3755 "Add text properties to string, allowing org-mode to act on it."
3756 (org-add-props string nil
3757 'mouse-face 'highlight
3758 'keymap org-agenda-keymap
3759 'help-echo (if buffer-file-name
3760 (format "mouse-2 or RET jump to diary file %s"
3761 (abbreviate-file-name buffer-file-name))
3763 'org-agenda-diary-link t
3764 'org-marker (org-agenda-new-marker (point-at-bol))))
3766 (defun org-diary-default-entry ()
3767 "Add a dummy entry to the diary.
3768 Needed to avoid empty dates which mess up holiday display."
3769 ;; Catch the error if dealing with the new add-to-diary-alist
3770 (when org-disable-agenda-to-diary
3771 (condition-case nil
3772 (org-add-to-diary-list original-date "Org-mode dummy" "")
3773 (error
3774 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
3776 (defun org-add-to-diary-list (&rest args)
3777 (if (fboundp 'diary-add-to-list)
3778 (apply 'diary-add-to-list args)
3779 (apply 'add-to-diary-list args)))
3781 ;;;###autoload
3782 (defun org-diary (&rest args)
3783 "Return diary information from org-files.
3784 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
3785 It accesses org files and extracts information from those files to be
3786 listed in the diary. The function accepts arguments specifying what
3787 items should be listed. The following arguments are allowed:
3789 :timestamp List the headlines of items containing a date stamp or
3790 date range matching the selected date. Deadlines will
3791 also be listed, on the expiration day.
3793 :sexp List entries resulting from diary-like sexps.
3795 :deadline List any deadlines past due, or due within
3796 `org-deadline-warning-days'. The listing occurs only
3797 in the diary for *today*, not at any other date. If
3798 an entry is marked DONE, it is no longer listed.
3800 :scheduled List all items which are scheduled for the given date.
3801 The diary for *today* also contains items which were
3802 scheduled earlier and are not yet marked DONE.
3804 :todo List all TODO items from the org-file. This may be a
3805 long list - so this is not turned on by default.
3806 Like deadlines, these entries only show up in the
3807 diary for *today*, not at any other date.
3809 The call in the diary file should look like this:
3811 &%%(org-diary) ~/path/to/some/orgfile.org
3813 Use a separate line for each org file to check. Or, if you omit the file name,
3814 all files listed in `org-agenda-files' will be checked automatically:
3816 &%%(org-diary)
3818 If you don't give any arguments (as in the example above), the default
3819 arguments (:deadline :scheduled :timestamp :sexp) are used.
3820 So the example above may also be written as
3822 &%%(org-diary :deadline :timestamp :sexp :scheduled)
3824 The function expects the lisp variables `entry' and `date' to be provided
3825 by the caller, because this is how the calendar works. Don't use this
3826 function from a program - use `org-agenda-get-day-entries' instead."
3827 (when (> (- (org-float-time)
3828 org-agenda-last-marker-time)
3830 (org-agenda-reset-markers))
3831 (org-compile-prefix-format 'agenda)
3832 (org-set-sorting-strategy 'agenda)
3833 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3834 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
3835 (list entry)
3836 (org-agenda-files t)))
3837 file rtn results)
3838 (org-prepare-agenda-buffers files)
3839 ;; If this is called during org-agenda, don't return any entries to
3840 ;; the calendar. Org Agenda will list these entries itself.
3841 (if org-disable-agenda-to-diary (setq files nil))
3842 (while (setq file (pop files))
3843 (setq rtn (apply 'org-agenda-get-day-entries file date args))
3844 (setq results (append results rtn)))
3845 (if results
3846 (concat (org-finalize-agenda-entries results) "\n"))))
3848 ;;; Agenda entry finders
3850 (defun org-agenda-get-day-entries (file date &rest args)
3851 "Does the work for `org-diary' and `org-agenda'.
3852 FILE is the path to a file to be checked for entries. DATE is date like
3853 the one returned by `calendar-current-date'. ARGS are symbols indicating
3854 which kind of entries should be extracted. For details about these, see
3855 the documentation of `org-diary'."
3856 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3857 (let* ((org-startup-folded nil)
3858 (org-startup-align-all-tables nil)
3859 (buffer (if (file-exists-p file)
3860 (org-get-agenda-file-buffer file)
3861 (error "No such file %s" file)))
3862 arg results rtn deadline-results)
3863 (if (not buffer)
3864 ;; If file does not exist, make sure an error message ends up in diary
3865 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3866 (with-current-buffer buffer
3867 (unless (org-mode-p)
3868 (error "Agenda file %s is not in `org-mode'" file))
3869 (let ((case-fold-search nil))
3870 (save-excursion
3871 (save-restriction
3872 (if org-agenda-restrict
3873 (narrow-to-region org-agenda-restrict-begin
3874 org-agenda-restrict-end)
3875 (widen))
3876 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
3877 (while (setq arg (pop args))
3878 (cond
3879 ((and (eq arg :todo)
3880 (equal date (calendar-current-date)))
3881 (setq rtn (org-agenda-get-todos))
3882 (setq results (append results rtn)))
3883 ((eq arg :timestamp)
3884 (setq rtn (org-agenda-get-blocks))
3885 (setq results (append results rtn))
3886 (setq rtn (org-agenda-get-timestamps))
3887 (setq results (append results rtn)))
3888 ((eq arg :sexp)
3889 (setq rtn (org-agenda-get-sexps))
3890 (setq results (append results rtn)))
3891 ((eq arg :scheduled)
3892 (setq rtn (org-agenda-get-scheduled deadline-results))
3893 (setq results (append results rtn)))
3894 ((eq arg :closed)
3895 (setq rtn (org-agenda-get-progress))
3896 (setq results (append results rtn)))
3897 ((eq arg :deadline)
3898 (setq rtn (org-agenda-get-deadlines))
3899 (setq deadline-results (copy-sequence rtn))
3900 (setq results (append results rtn))))))))
3901 results))))
3903 (defun org-agenda-get-todos ()
3904 "Return the TODO information for agenda display."
3905 (let* ((props (list 'face nil
3906 'done-face 'org-agenda-done
3907 'org-not-done-regexp org-not-done-regexp
3908 'org-todo-regexp org-todo-regexp
3909 'org-complex-heading-regexp org-complex-heading-regexp
3910 'mouse-face 'highlight
3911 'keymap org-agenda-keymap
3912 'help-echo
3913 (format "mouse-2 or RET jump to org file %s"
3914 (abbreviate-file-name buffer-file-name))))
3915 (regexp (concat "^\\*+[ \t]+\\("
3916 (if org-select-this-todo-keyword
3917 (if (equal org-select-this-todo-keyword "*")
3918 org-todo-regexp
3919 (concat "\\<\\("
3920 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
3921 "\\)\\>"))
3922 org-not-done-regexp)
3923 "[^\n\r]*\\)"))
3924 marker priority category tags todo-state
3925 ee txt beg end)
3926 (goto-char (point-min))
3927 (while (re-search-forward regexp nil t)
3928 (catch :skip
3929 (save-match-data
3930 (beginning-of-line)
3931 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
3932 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
3933 (goto-char (1+ beg))
3934 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
3935 (throw :skip nil)))
3936 (goto-char beg)
3937 (org-agenda-skip)
3938 (goto-char (match-beginning 1))
3939 (setq marker (org-agenda-new-marker (match-beginning 0))
3940 category (org-get-category)
3941 txt (match-string 1)
3942 tags (org-get-tags-at (point))
3943 txt (org-format-agenda-item "" txt category tags)
3944 priority (1+ (org-get-priority txt))
3945 todo-state (org-get-todo-state))
3946 (org-add-props txt props
3947 'org-marker marker 'org-hd-marker marker
3948 'priority priority 'org-category category
3949 'type "todo" 'todo-state todo-state)
3950 (push txt ee)
3951 (if org-agenda-todo-list-sublevels
3952 (goto-char (match-end 1))
3953 (org-end-of-subtree 'invisible))))
3954 (nreverse ee)))
3956 ;;;###autoload
3957 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item (&optional end)
3958 "Do we have a reason to ignore this todo entry because it has a time stamp?"
3959 (when (or org-agenda-todo-ignore-with-date
3960 org-agenda-todo-ignore-scheduled
3961 org-agenda-todo-ignore-deadlines)
3962 (setq end (or end (save-excursion (outline-next-heading) (point))))
3963 (save-excursion
3964 (or (and org-agenda-todo-ignore-with-date
3965 (re-search-forward org-ts-regexp end t))
3966 (and org-agenda-todo-ignore-scheduled
3967 (re-search-forward org-scheduled-time-regexp end t))
3968 (and org-agenda-todo-ignore-deadlines
3969 (re-search-forward org-deadline-time-regexp end t)
3970 (org-deadline-close (match-string 1)))))))
3972 (defconst org-agenda-no-heading-message
3973 "No heading for this item in buffer or region.")
3975 (defun org-agenda-get-timestamps ()
3976 "Return the date stamp information for agenda display."
3977 (let* ((props (list 'face nil
3978 'org-not-done-regexp org-not-done-regexp
3979 'org-todo-regexp org-todo-regexp
3980 'org-complex-heading-regexp org-complex-heading-regexp
3981 'mouse-face 'highlight
3982 'keymap org-agenda-keymap
3983 'help-echo
3984 (format "mouse-2 or RET jump to org file %s"
3985 (abbreviate-file-name buffer-file-name))))
3986 (d1 (calendar-absolute-from-gregorian date))
3987 (remove-re
3988 (concat
3989 (regexp-quote
3990 (format-time-string
3991 "<%Y-%m-%d"
3992 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
3993 ".*?>"))
3994 (regexp
3995 (concat
3996 (if org-agenda-include-inactive-timestamps "[[<]" "<")
3997 (regexp-quote
3998 (substring
3999 (format-time-string
4000 (car org-time-stamp-formats)
4001 (apply 'encode-time ; DATE bound by calendar
4002 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4003 1 11))
4004 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4005 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4006 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4007 donep tmp priority category ee txt timestr tags b0 b3 e3 head
4008 todo-state end-of-match)
4009 (goto-char (point-min))
4010 (while (setq end-of-match (re-search-forward regexp nil t))
4011 (setq b0 (match-beginning 0)
4012 b3 (match-beginning 3) e3 (match-end 3))
4013 (catch :skip
4014 (and (org-at-date-range-p) (throw :skip nil))
4015 (org-agenda-skip)
4016 (if (and (match-end 1)
4017 (not (= d1 (org-time-string-to-absolute
4018 (match-string 1) d1 nil
4019 org-agenda-repeating-timestamp-show-all))))
4020 (throw :skip nil))
4021 (if (and e3
4022 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4023 (throw :skip nil))
4024 (setq tmp (buffer-substring (max (point-min)
4025 (- b0 org-ds-keyword-length))
4027 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4028 inactivep (= (char-after b0) ?\[)
4029 deadlinep (string-match org-deadline-regexp tmp)
4030 scheduledp (string-match org-scheduled-regexp tmp)
4031 closedp (and org-agenda-include-inactive-timestamps
4032 (string-match org-closed-string tmp))
4033 clockp (and org-agenda-include-inactive-timestamps
4034 (or (string-match org-clock-string tmp)
4035 (string-match "]-+\\'" tmp)))
4036 todo-state (org-get-todo-state)
4037 donep (member todo-state org-done-keywords))
4038 (if (or scheduledp deadlinep closedp clockp
4039 (and donep org-agenda-skip-timestamp-if-done))
4040 (throw :skip t))
4041 (if (string-match ">" timestr)
4042 ;; substring should only run to end of time stamp
4043 (setq timestr (substring timestr 0 (match-end 0))))
4044 (setq marker (org-agenda-new-marker b0)
4045 category (org-get-category b0))
4046 (save-excursion
4047 (if (not (re-search-backward "^\\*+ " nil t))
4048 (setq txt org-agenda-no-heading-message)
4049 (goto-char (match-beginning 0))
4050 (setq hdmarker (org-agenda-new-marker)
4051 tags (org-get-tags-at))
4052 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4053 (setq head (match-string 1))
4054 (setq txt (org-format-agenda-item
4055 (if inactivep "[" nil)
4056 head category tags timestr nil
4057 remove-re)))
4058 (setq priority (org-get-priority txt))
4059 (org-add-props txt props
4060 'org-marker marker 'org-hd-marker hdmarker)
4061 (org-add-props txt nil 'priority priority
4062 'org-category category 'date date
4063 'todo-state todo-state
4064 'type "timestamp")
4065 (push txt ee))
4066 (if org-agenda-skip-additional-timestamps-same-entry
4067 (outline-next-heading)
4068 (goto-char end-of-match))))
4069 (nreverse ee)))
4071 (defun org-agenda-get-sexps ()
4072 "Return the sexp information for agenda display."
4073 (require 'diary-lib)
4074 (let* ((props (list 'face nil
4075 'mouse-face 'highlight
4076 'keymap org-agenda-keymap
4077 'help-echo
4078 (format "mouse-2 or RET jump to org file %s"
4079 (abbreviate-file-name buffer-file-name))))
4080 (regexp "^&?%%(")
4081 marker category ee txt tags entry result beg b sexp sexp-entry
4082 todo-state)
4083 (goto-char (point-min))
4084 (while (re-search-forward regexp nil t)
4085 (catch :skip
4086 (org-agenda-skip)
4087 (setq beg (match-beginning 0))
4088 (goto-char (1- (match-end 0)))
4089 (setq b (point))
4090 (forward-sexp 1)
4091 (setq sexp (buffer-substring b (point)))
4092 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4093 (org-trim (match-string 1))
4094 ""))
4095 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4096 (when result
4097 (setq marker (org-agenda-new-marker beg)
4098 category (org-get-category beg)
4099 todo-state (org-get-todo-state))
4101 (if (string-match "\\S-" result)
4102 (setq txt result)
4103 (setq txt "SEXP entry returned empty string"))
4105 (setq txt (org-format-agenda-item
4106 "" txt category tags 'time))
4107 (org-add-props txt props 'org-marker marker)
4108 (org-add-props txt nil
4109 'org-category category 'date date 'todo-state todo-state
4110 'type "sexp")
4111 (push txt ee))))
4112 (nreverse ee)))
4114 (defalias 'org-get-closed 'org-agenda-get-progress)
4115 (defun org-agenda-get-progress ()
4116 "Return the logged TODO entries for agenda display."
4117 (let* ((props (list 'mouse-face 'highlight
4118 'org-not-done-regexp org-not-done-regexp
4119 'org-todo-regexp org-todo-regexp
4120 'org-complex-heading-regexp org-complex-heading-regexp
4121 'keymap org-agenda-keymap
4122 'help-echo
4123 (format "mouse-2 or RET jump to org file %s"
4124 (abbreviate-file-name buffer-file-name))))
4125 (items (if (consp org-agenda-show-log)
4126 org-agenda-show-log
4127 org-agenda-log-mode-items))
4128 (parts
4129 (delq nil
4130 (list
4131 (if (memq 'closed items) (concat "\\<" org-closed-string))
4132 (if (memq 'clock items) (concat "\\<" org-clock-string))
4133 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4134 (parts-re (if parts (mapconcat 'identity parts "\\|")
4135 (error "`org-agenda-log-mode-items' is empty")))
4136 (regexp (concat
4137 "\\(" parts-re "\\)"
4138 " *\\["
4139 (regexp-quote
4140 (substring
4141 (format-time-string
4142 (car org-time-stamp-formats)
4143 (apply 'encode-time ; DATE bound by calendar
4144 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4145 1 11))))
4146 (org-agenda-search-headline-for-time nil)
4147 marker hdmarker priority category tags closedp statep clockp state
4148 ee txt extra timestr rest clocked)
4149 (goto-char (point-min))
4150 (while (re-search-forward regexp nil t)
4151 (catch :skip
4152 (org-agenda-skip)
4153 (setq marker (org-agenda-new-marker (match-beginning 0))
4154 closedp (equal (match-string 1) org-closed-string)
4155 statep (equal (string-to-char (match-string 1)) ?-)
4156 clockp (not (or closedp statep))
4157 state (and statep (match-string 2))
4158 category (org-get-category (match-beginning 0))
4159 timestr (buffer-substring (match-beginning 0) (point-at-eol))
4161 (when (string-match "\\]" timestr)
4162 ;; substring should only run to end of time stamp
4163 (setq rest (substring timestr (match-end 0))
4164 timestr (substring timestr 0 (match-end 0)))
4165 (if (and (not closedp) (not statep)
4166 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
4167 (progn (setq timestr (concat (substring timestr 0 -1)
4168 "-" (match-string 1 rest) "]"))
4169 (setq clocked (match-string 2 rest)))
4170 (setq clocked "-")))
4171 (save-excursion
4172 (cond
4173 ((not org-agenda-log-mode-add-notes) (setq extra nil))
4174 (statep
4175 (and (looking-at ".*\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4176 (setq extra (match-string 1))))
4177 (clockp
4178 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4179 (setq extra (match-string 1))))
4180 (t (setq extra nil)))
4181 (if (not (re-search-backward "^\\*+ " nil t))
4182 (setq txt org-agenda-no-heading-message)
4183 (goto-char (match-beginning 0))
4184 (setq hdmarker (org-agenda-new-marker)
4185 tags (org-get-tags-at))
4186 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4187 (setq txt (match-string 1))
4188 (when extra
4189 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4190 (setq txt (concat (substring txt 0 (match-beginning 1))
4191 " - " extra " " (match-string 2 txt)))
4192 (setq txt (concat txt " - " extra))))
4193 (setq txt (org-format-agenda-item
4194 (cond
4195 (closedp "Closed: ")
4196 (statep (concat "State: (" state ")"))
4197 (t (concat "Clocked: (" clocked ")")))
4198 txt category tags timestr)))
4199 (setq priority 100000)
4200 (org-add-props txt props
4201 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
4202 'priority priority 'org-category category
4203 'type "closed" 'date date
4204 'undone-face 'org-warning 'done-face 'org-agenda-done)
4205 (push txt ee))
4206 (goto-char (point-at-eol))))
4207 (nreverse ee)))
4209 (defun org-agenda-get-deadlines ()
4210 "Return the deadline information for agenda display."
4211 (let* ((props (list 'mouse-face 'highlight
4212 'org-not-done-regexp org-not-done-regexp
4213 'org-todo-regexp org-todo-regexp
4214 'org-complex-heading-regexp org-complex-heading-regexp
4215 'keymap org-agenda-keymap
4216 'help-echo
4217 (format "mouse-2 or RET jump to org file %s"
4218 (abbreviate-file-name buffer-file-name))))
4219 (regexp org-deadline-time-regexp)
4220 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
4221 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4222 d2 diff dfrac wdays pos pos1 category tags habitp
4223 ee txt head face s todo-state upcomingp donep timestr)
4224 (goto-char (point-min))
4225 (while (re-search-forward regexp nil t)
4226 (catch :skip
4227 (org-agenda-skip)
4228 (setq s (match-string 1)
4229 txt nil
4230 pos (1- (match-beginning 1))
4231 d2 (org-time-string-to-absolute
4232 (match-string 1) d1 'past
4233 org-agenda-repeating-timestamp-show-all)
4234 diff (- d2 d1)
4235 ;; Never show habits as deadline entries, only as scheduled
4236 ;; entries. The habit code already requires that every habit
4237 ;; have a scheduled date if it has a deadline, and that the
4238 ;; scheduled date is prior to the deadline.
4239 habitp (and (functionp 'org-is-habit-p)
4240 (org-is-habit-p))
4241 wdays (org-get-wdays s)
4242 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
4243 upcomingp (and todayp (> diff 0)))
4244 ;; When to show a deadline in the calendar:
4245 ;; If the expiration is within wdays warning time.
4246 ;; Past-due deadlines are only shown on the current date
4247 (if (and (or (and (<= diff wdays)
4248 (and todayp (not org-agenda-only-exact-dates)))
4249 (= diff 0))
4250 (not habitp))
4251 (save-excursion
4252 (setq todo-state (org-get-todo-state))
4253 (setq donep (member todo-state org-done-keywords))
4254 (if (and donep
4255 (or org-agenda-skip-deadline-if-done
4256 (not (= diff 0))))
4257 (setq txt nil)
4258 (setq category (org-get-category))
4259 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4260 (setq txt org-agenda-no-heading-message)
4261 (goto-char (match-end 0))
4262 (setq pos1 (match-beginning 0))
4263 (setq tags (org-get-tags-at pos1))
4264 (setq head (buffer-substring-no-properties
4265 (point)
4266 (progn (skip-chars-forward "^\r\n")
4267 (point))))
4268 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4269 (setq timestr
4270 (concat (substring s (match-beginning 1)) " "))
4271 (setq timestr 'time))
4272 (setq txt (org-format-agenda-item
4273 (if (= diff 0)
4274 (car org-agenda-deadline-leaders)
4275 (if (functionp
4276 (nth 1 org-agenda-deadline-leaders))
4277 (funcall
4278 (nth 1 org-agenda-deadline-leaders)
4279 diff date)
4280 (format (nth 1 org-agenda-deadline-leaders)
4281 diff)))
4282 head category tags
4283 (if (not (= diff 0)) nil timestr)))))
4284 (when txt
4285 (setq face (org-agenda-deadline-face dfrac wdays))
4286 (org-add-props txt props
4287 'org-marker (org-agenda-new-marker pos)
4288 'org-hd-marker (org-agenda-new-marker pos1)
4289 'priority (+ (- diff)
4290 (org-get-priority txt))
4291 'org-category category
4292 'todo-state todo-state
4293 'type (if upcomingp "upcoming-deadline" "deadline")
4294 'date (if upcomingp date d2)
4295 'face (if donep 'org-agenda-done face)
4296 'undone-face face 'done-face 'org-agenda-done)
4297 (push txt ee))))))
4298 (nreverse ee)))
4300 (defun org-agenda-deadline-face (fraction &optional wdays)
4301 "Return the face to displaying a deadline item.
4302 FRACTION is what fraction of the head-warning time has passed."
4303 (if (equal wdays 0) (setq fraction 1.))
4304 (let ((faces org-agenda-deadline-faces) f)
4305 (catch 'exit
4306 (while (setq f (pop faces))
4307 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
4309 (defun org-agenda-get-scheduled (&optional deadline-results)
4310 "Return the scheduled information for agenda display."
4311 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
4312 'org-todo-regexp org-todo-regexp
4313 'org-complex-heading-regexp org-complex-heading-regexp
4314 'done-face 'org-agenda-done
4315 'mouse-face 'highlight
4316 'keymap org-agenda-keymap
4317 'help-echo
4318 (format "mouse-2 or RET jump to org file %s"
4319 (abbreviate-file-name buffer-file-name))))
4320 (regexp org-scheduled-time-regexp)
4321 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
4322 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4324 (deadline-position-alist
4325 (mapcar (lambda (a) (and (setq mm (get-text-property
4326 0 'org-hd-marker a))
4327 (cons (marker-position mm) a)))
4328 deadline-results))
4329 d2 diff pos pos1 category tags donep
4330 ee txt head pastschedp todo-state face timestr s habitp)
4331 (goto-char (point-min))
4332 (while (re-search-forward regexp nil t)
4333 (catch :skip
4334 (org-agenda-skip)
4335 (setq s (match-string 1)
4336 txt nil
4337 pos (1- (match-beginning 1))
4338 d2 (org-time-string-to-absolute
4339 (match-string 1) d1 'past
4340 org-agenda-repeating-timestamp-show-all)
4341 diff (- d2 d1))
4342 (setq pastschedp (and todayp (< diff 0)))
4343 ;; When to show a scheduled item in the calendar:
4344 ;; If it is on or past the date.
4345 (when (or (and (< diff 0)
4346 (< (abs diff) org-scheduled-past-days)
4347 (and todayp (not org-agenda-only-exact-dates)))
4348 (= diff 0))
4349 (save-excursion
4350 (setq todo-state (org-get-todo-state))
4351 (setq donep (member todo-state org-done-keywords))
4352 (setq habitp (and (functionp 'org-is-habit-p)
4353 (org-is-habit-p)))
4354 (if (and donep
4355 (or habitp org-agenda-skip-scheduled-if-done
4356 (not (= diff 0))))
4357 (setq txt nil)
4358 (setq category (org-get-category))
4359 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4360 (setq txt org-agenda-no-heading-message)
4361 (goto-char (match-end 0))
4362 (setq pos1 (match-beginning 0))
4363 (if habitp
4364 (if (or (not org-habit-show-habits)
4365 (and (not todayp)
4366 org-habit-show-habits-only-for-today))
4367 (throw :skip nil))
4368 (if (and
4369 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
4370 (and org-agenda-skip-scheduled-if-deadline-is-shown
4371 pastschedp))
4372 (setq mm (assoc pos1 deadline-position-alist)))
4373 (throw :skip nil)))
4374 (setq tags (org-get-tags-at))
4375 (setq head (buffer-substring-no-properties
4376 (point)
4377 (progn (skip-chars-forward "^\r\n") (point))))
4378 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4379 (setq timestr
4380 (concat (substring s (match-beginning 1)) " "))
4381 (setq timestr 'time))
4382 (setq txt (org-format-agenda-item
4383 (if (= diff 0)
4384 (car org-agenda-scheduled-leaders)
4385 (format (nth 1 org-agenda-scheduled-leaders)
4386 (- 1 diff)))
4387 head category tags
4388 (if (not (= diff 0)) nil timestr)
4389 nil nil habitp))))
4390 (when txt
4391 (setq face
4392 (cond
4393 (pastschedp 'org-scheduled-previously)
4394 (todayp 'org-scheduled-today)
4395 (t 'org-scheduled)))
4396 (org-add-props txt props
4397 'undone-face face
4398 'face (if donep 'org-agenda-done face)
4399 'org-marker (org-agenda-new-marker pos)
4400 'org-hd-marker (org-agenda-new-marker pos1)
4401 'type (if pastschedp "past-scheduled" "scheduled")
4402 'date (if pastschedp d2 date)
4403 'priority (+ 94 (- 5 diff) (org-get-priority txt))
4404 'org-category category
4405 'org-habit-p (and habitp (org-habit-parse-todo))
4406 'todo-state todo-state)
4407 (push txt ee))))))
4408 (nreverse ee)))
4410 (defun org-agenda-get-blocks ()
4411 "Return the date-range information for agenda display."
4412 (let* ((props (list 'face nil
4413 'org-not-done-regexp org-not-done-regexp
4414 'org-todo-regexp org-todo-regexp
4415 'org-complex-heading-regexp org-complex-heading-regexp
4416 'mouse-face 'highlight
4417 'keymap org-agenda-keymap
4418 'help-echo
4419 (format "mouse-2 or RET jump to org file %s"
4420 (abbreviate-file-name buffer-file-name))))
4421 (regexp org-tr-regexp)
4422 (d0 (calendar-absolute-from-gregorian date))
4423 marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
4424 head donep)
4425 (goto-char (point-min))
4426 (while (re-search-forward regexp nil t)
4427 (catch :skip
4428 (org-agenda-skip)
4429 (setq pos (point))
4430 (setq timestr (match-string 0)
4431 s1 (match-string 1)
4432 s2 (match-string 2)
4433 d1 (time-to-days (org-time-string-to-time s1))
4434 d2 (time-to-days (org-time-string-to-time s2)))
4435 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
4436 ;; Only allow days between the limits, because the normal
4437 ;; date stamps will catch the limits.
4438 (save-excursion
4439 (setq todo-state (org-get-todo-state))
4440 (setq donep (member todo-state org-done-keywords))
4441 (if (and donep org-agenda-skip-timestamp-if-done)
4442 (throw :skip t))
4443 (setq marker (org-agenda-new-marker (point)))
4444 (setq category (org-get-category))
4445 (if (not (re-search-backward "^\\*+ " nil t))
4446 (setq txt org-agenda-no-heading-message)
4447 (goto-char (match-beginning 0))
4448 (setq hdmarker (org-agenda-new-marker (point)))
4449 (setq tags (org-get-tags-at))
4450 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4451 (setq head (match-string 1))
4452 (setq txt (org-format-agenda-item
4453 (format
4454 (nth (if (= d1 d2) 0 1)
4455 org-agenda-timerange-leaders)
4456 (1+ (- d0 d1)) (1+ (- d2 d1)))
4457 head category tags
4458 (if (= d0 d1) timestr))))
4459 (org-add-props txt props
4460 'org-marker marker 'org-hd-marker hdmarker
4461 'type "block" 'date date
4462 'todo-state todo-state
4463 'priority (org-get-priority txt) 'org-category category)
4464 (push txt ee)))
4465 (goto-char pos)))
4466 ;; Sort the entries by expiration date.
4467 (nreverse ee)))
4469 ;;; Agenda presentation and sorting
4471 (defvar org-prefix-has-time nil
4472 "A flag, set by `org-compile-prefix-format'.
4473 The flag is set if the currently compiled format contains a `%t'.")
4474 (defvar org-prefix-has-tag nil
4475 "A flag, set by `org-compile-prefix-format'.
4476 The flag is set if the currently compiled format contains a `%T'.")
4477 (defvar org-prefix-has-effort nil
4478 "A flag, set by `org-compile-prefix-format'.
4479 The flag is set if the currently compiled format contains a `%e'.")
4480 (defvar org-prefix-category-length nil
4481 "Used by `org-compile-prefix-format' to remember the category field widh.")
4482 (defvar org-prefix-category-max-length nil
4483 "Used by `org-compile-prefix-format' to remember the category field widh.")
4485 (defun org-format-agenda-item (extra txt &optional category tags dotime
4486 noprefix remove-re habitp)
4487 "Format TXT to be inserted into the agenda buffer.
4488 In particular, it adds the prefix and corresponding text properties. EXTRA
4489 must be a string and replaces the `%s' specifier in the prefix format.
4490 CATEGORY (string, symbol or nil) may be used to overrule the default
4491 category taken from local variable or file name. It will replace the `%c'
4492 specifier in the format. DOTIME, when non-nil, indicates that a
4493 time-of-day should be extracted from TXT for sorting of this entry, and for
4494 the `%t' specifier in the format. When DOTIME is a string, this string is
4495 searched for a time before TXT is. NOPREFIX is a flag and indicates that
4496 only the correctly processes TXT should be returned - this is used by
4497 `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
4498 Any match of REMOVE-RE will be removed from TXT."
4499 (save-match-data
4500 ;; Diary entries sometimes have extra whitespace at the beginning
4501 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
4502 (when org-agenda-show-inherited-tags
4503 ;; Fix the tags part in txt
4504 (setq txt (org-agenda-add-inherited-tags txt tags)))
4505 (let* ((category (or category
4506 org-category
4507 (if buffer-file-name
4508 (file-name-sans-extension
4509 (file-name-nondirectory buffer-file-name))
4510 "")))
4511 ;; time, tag, effort are needed for the eval of the prefix format
4512 (tag (if tags (nth (1- (length tags)) tags) ""))
4513 time effort neffort
4514 (ts (if dotime (concat
4515 (if (stringp dotime) dotime "")
4516 (and org-agenda-search-headline-for-time txt))))
4517 (time-of-day (and dotime (org-get-time-of-day ts)))
4518 stamp plain s0 s1 s2 t1 t2 rtn srp l
4519 duration thecategory)
4520 (and (org-mode-p) buffer-file-name
4521 (add-to-list 'org-agenda-contributing-files buffer-file-name))
4522 (when (and dotime time-of-day)
4523 ;; Extract starting and ending time and move them to prefix
4524 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
4525 (setq plain (string-match org-plain-time-of-day-regexp ts)))
4526 (setq s0 (match-string 0 ts)
4527 srp (and stamp (match-end 3))
4528 s1 (match-string (if plain 1 2) ts)
4529 s2 (match-string (if plain 8 (if srp 4 6)) ts))
4531 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
4532 ;; them, we might want to remove them there to avoid duplication.
4533 ;; The user can turn this off with a variable.
4534 (if (and org-prefix-has-time
4535 org-agenda-remove-times-when-in-prefix (or stamp plain)
4536 (string-match (concat (regexp-quote s0) " *") txt)
4537 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
4538 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
4539 (= (match-beginning 0) 0)
4541 (setq txt (replace-match "" nil nil txt))))
4542 ;; Normalize the time(s) to 24 hour
4543 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
4544 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
4545 ;; Compute the duration
4546 (when s1
4547 (setq t1 (+ (* 60 (string-to-number (substring s1 0 2)))
4548 (string-to-number (substring s1 3)))
4549 t2 (cond
4550 (s2 (+ (* 60 (string-to-number (substring s2 0 2)))
4551 (string-to-number (substring s2 3))))
4552 (org-agenda-default-appointment-duration
4553 (+ t1 org-agenda-default-appointment-duration))
4554 (t nil)))
4555 (setq duration (if t2 (- t2 t1)))))
4557 (when (and s1 (not s2) org-agenda-default-appointment-duration
4558 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
4559 (let ((m (+ (string-to-number (match-string 2 s1))
4560 (* 60 (string-to-number (match-string 1 s1)))
4561 org-agenda-default-appointment-duration))
4563 (setq h (/ m 60) m (- m (* h 60)))
4564 (setq s2 (format "%02d:%02d" h m))))
4566 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
4567 txt)
4568 ;; Tags are in the string
4569 (if (or (eq org-agenda-remove-tags t)
4570 (and org-agenda-remove-tags
4571 org-prefix-has-tag))
4572 (setq txt (replace-match "" t t txt))
4573 (setq txt (replace-match
4574 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
4575 (match-string 2 txt))
4576 t t txt))))
4577 (when (org-mode-p)
4578 (setq effort
4579 (condition-case nil
4580 (org-get-effort
4581 (or (get-text-property 0 'org-hd-marker txt)
4582 (get-text-property 0 'org-marker txt)))
4583 (error nil)))
4584 (when effort
4585 (setq neffort (org-hh:mm-string-to-minutes effort)
4586 effort (setq effort (concat "[" effort "]" )))))
4588 (when remove-re
4589 (while (string-match remove-re txt)
4590 (setq txt (replace-match "" t t txt))))
4592 ;; Create the final string
4593 (if noprefix
4594 (setq rtn txt)
4595 ;; Prepare the variables needed in the eval of the compiled format
4596 (setq time (cond (s2 (concat s1 "-" s2))
4597 (s1 (concat s1 "......"))
4598 (t ""))
4599 extra (or (and (not habitp) extra) "")
4600 category (if (symbolp category) (symbol-name category) category)
4601 thecategory (copy-sequence category))
4602 (if (string-match org-bracket-link-regexp category)
4603 (progn
4604 (setq l (if (match-end 3)
4605 (- (match-end 3) (match-beginning 3))
4606 (- (match-end 1) (match-beginning 1))))
4607 (when (< l (or org-prefix-category-length 0))
4608 (setq category (copy-sequence category))
4609 (org-add-props category nil
4610 'extra-space (make-string
4611 (- org-prefix-category-length l 1) ?\ ))))
4612 (if (and org-prefix-category-max-length
4613 (>= (length category) org-prefix-category-max-length))
4614 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
4615 ;; Evaluate the compiled format
4616 (setq rtn (concat (eval org-prefix-format-compiled) txt)))
4618 ;; And finally add the text properties
4619 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
4620 (org-add-props rtn nil
4621 'org-category (if thecategory (downcase thecategory) category)
4622 'tags (mapcar 'org-downcase-keep-props tags)
4623 'org-highest-priority org-highest-priority
4624 'org-lowest-priority org-lowest-priority
4625 'prefix-length (- (length rtn) (length txt))
4626 'time-of-day time-of-day
4627 'duration duration
4628 'effort effort
4629 'effort-minutes neffort
4630 'txt txt
4631 'time time
4632 'extra extra
4633 'dotime dotime))))
4635 (defun org-agenda-add-inherited-tags (txt tags)
4636 "Remove tags string from TXT, and add complete list of tags.
4637 The new list includes inherited tags. If any inherited tags are present,
4638 a double colon separates inherited tags from local tags."
4639 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$") txt)
4640 (setq txt (substring txt 0 (match-beginning 0))))
4641 (when tags
4642 (let ((have-i (get-text-property 0 'inherited (car tags)))
4644 (setq txt (concat txt " :"
4645 (mapconcat
4646 (lambda (x)
4647 (setq i (get-text-property 0 'inherited x))
4648 (if (and have-i (not i))
4649 (progn
4650 (setq have-i nil)
4651 (concat ":" x))
4653 tags ":")
4654 (if have-i "::" ":")))))
4655 txt)
4657 (defun org-downcase-keep-props (s)
4658 (let ((props (text-properties-at 0 s)))
4659 (setq s (downcase s))
4660 (add-text-properties 0 (length s) props s)
4663 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
4664 (defvar org-agenda-sorting-strategy-selected nil)
4666 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
4667 (catch 'exit
4668 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
4669 ((and todayp (member 'today (car org-agenda-time-grid))))
4670 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
4671 ((member 'weekly (car org-agenda-time-grid)))
4672 (t (throw 'exit list)))
4673 (let* ((have (delq nil (mapcar
4674 (lambda (x) (get-text-property 1 'time-of-day x))
4675 list)))
4676 (string (nth 1 org-agenda-time-grid))
4677 (gridtimes (nth 2 org-agenda-time-grid))
4678 (req (car org-agenda-time-grid))
4679 (remove (member 'remove-match req))
4680 new time)
4681 (if (and (member 'require-timed req) (not have))
4682 ;; don't show empty grid
4683 (throw 'exit list))
4684 (while (setq time (pop gridtimes))
4685 (unless (and remove (member time have))
4686 (setq time (int-to-string time))
4687 (push (org-format-agenda-item
4688 nil string "" nil
4689 (concat (substring time 0 -2) ":" (substring time -2)))
4690 new)
4691 (put-text-property
4692 1 (length (car new)) 'face 'org-time-grid (car new))))
4693 (if (member 'time-up org-agenda-sorting-strategy-selected)
4694 (append new list)
4695 (append list new)))))
4697 (defun org-compile-prefix-format (key)
4698 "Compile the prefix format into a Lisp form that can be evaluated.
4699 The resulting form is returned and stored in the variable
4700 `org-prefix-format-compiled'."
4701 (setq org-prefix-has-time nil org-prefix-has-tag nil
4702 org-prefix-category-length nil org-prefix-has-effort nil)
4703 (let ((s (cond
4704 ((stringp org-agenda-prefix-format)
4705 org-agenda-prefix-format)
4706 ((assq key org-agenda-prefix-format)
4707 (cdr (assq key org-agenda-prefix-format)))
4708 (t " %-12:c%?-12t% s")))
4709 (start 0)
4710 varform vars var e c f opt)
4711 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctse]\\)"
4712 s start)
4713 (setq var (cdr (assoc (match-string 4 s)
4714 '(("c" . category) ("t" . time) ("s" . extra)
4715 ("T" . tag) ("e" . effort))))
4716 c (or (match-string 3 s) "")
4717 opt (match-beginning 1)
4718 start (1+ (match-beginning 0)))
4719 (if (equal var 'time) (setq org-prefix-has-time t))
4720 (if (equal var 'tag) (setq org-prefix-has-tag t))
4721 (if (equal var 'effort) (setq org-prefix-has-effort t))
4722 (setq f (concat "%" (match-string 2 s) "s"))
4723 (when (equal var 'category)
4724 (setq org-prefix-category-length
4725 (floor (abs (string-to-number (match-string 2 s)))))
4726 (setq org-prefix-category-max-length
4727 (let ((x (match-string 2 s)))
4728 (save-match-data
4729 (if (string-match "\\.[0-9]+" x)
4730 (string-to-number (substring (match-string 0 x) 1)))))))
4731 (if opt
4732 (setq varform
4733 `(if (equal "" ,var)
4735 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
4736 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var))))))
4737 (setq s (replace-match "%s" t nil s))
4738 (push varform vars))
4739 (setq vars (nreverse vars))
4740 (setq org-prefix-format-compiled `(format ,s ,@vars))))
4742 (defun org-set-sorting-strategy (key)
4743 (if (symbolp (car org-agenda-sorting-strategy))
4744 ;; the old format
4745 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
4746 (setq org-agenda-sorting-strategy-selected
4747 (or (cdr (assq key org-agenda-sorting-strategy))
4748 (cdr (assq 'agenda org-agenda-sorting-strategy))
4749 '(time-up category-keep priority-down)))))
4751 (defun org-get-time-of-day (s &optional string mod24)
4752 "Check string S for a time of day.
4753 If found, return it as a military time number between 0 and 2400.
4754 If not found, return nil.
4755 The optional STRING argument forces conversion into a 5 character wide string
4756 HH:MM."
4757 (save-match-data
4758 (when
4759 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
4760 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
4761 (let* ((h (string-to-number (match-string 1 s)))
4762 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
4763 (ampm (if (match-end 4) (downcase (match-string 4 s))))
4764 (am-p (equal ampm "am"))
4765 (h1 (cond ((not ampm) h)
4766 ((= h 12) (if am-p 0 12))
4767 (t (+ h (if am-p 0 12)))))
4768 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
4769 (mod h1 24) h1))
4770 (t0 (+ (* 100 h2) m))
4771 (t1 (concat (if (>= h1 24) "+" " ")
4772 (if (< t0 100) "0" "")
4773 (if (< t0 10) "0" "")
4774 (int-to-string t0))))
4775 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
4777 (defun org-finalize-agenda-entries (list &optional nosort)
4778 "Sort and concatenate the agenda items."
4779 (setq list (mapcar 'org-agenda-highlight-todo list))
4780 (if nosort
4781 list
4782 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
4784 (defun org-agenda-highlight-todo (x)
4785 (let ((org-done-keywords org-done-keywords-for-agenda)
4786 re pl)
4787 (if (eq x 'line)
4788 (save-excursion
4789 (beginning-of-line 1)
4790 (setq re (org-get-at-bol 'org-todo-regexp))
4791 (goto-char (+ (point) (or (org-get-at-bol 'prefix-length) 0)))
4792 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
4793 (add-text-properties (match-beginning 0) (match-end 1)
4794 (list 'face (org-get-todo-face 1)))
4795 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
4796 (delete-region (match-beginning 1) (1- (match-end 0)))
4797 (goto-char (match-beginning 1))
4798 (insert (format org-agenda-todo-keyword-format s)))))
4799 (setq re (concat (get-text-property 0 'org-todo-regexp x))
4800 pl (get-text-property 0 'prefix-length x))
4801 (when (and re
4802 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
4803 x (or pl 0)) pl))
4804 (add-text-properties
4805 (or (match-end 1) (match-end 0)) (match-end 0)
4806 (list 'face (org-get-todo-face (match-string 2 x)))
4808 (setq x (concat (substring x 0 (match-end 1))
4809 (format org-agenda-todo-keyword-format
4810 (match-string 2 x))
4811 (org-add-props " " (text-properties-at 0 x))
4812 (substring x (match-end 3)))))
4813 x)))
4815 (defsubst org-cmp-priority (a b)
4816 "Compare the priorities of string A and B."
4817 (let ((pa (or (get-text-property 1 'priority a) 0))
4818 (pb (or (get-text-property 1 'priority b) 0)))
4819 (cond ((> pa pb) +1)
4820 ((< pa pb) -1)
4821 (t nil))))
4823 (defsubst org-cmp-effort (a b)
4824 "Compare the priorities of string A and B."
4825 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
4826 (ea (or (get-text-property 1 'effort-minutes a) def))
4827 (eb (or (get-text-property 1 'effort-minutes b) def)))
4828 (cond ((> ea eb) +1)
4829 ((< ea eb) -1)
4830 (t nil))))
4832 (defsubst org-cmp-category (a b)
4833 "Compare the string values of categories of strings A and B."
4834 (let ((ca (or (get-text-property 1 'org-category a) ""))
4835 (cb (or (get-text-property 1 'org-category b) "")))
4836 (cond ((string-lessp ca cb) -1)
4837 ((string-lessp cb ca) +1)
4838 (t nil))))
4840 (defsubst org-cmp-todo-state (a b)
4841 "Compare the todo states of strings A and B."
4842 (let* ((ma (or (get-text-property 1 'org-marker a)
4843 (get-text-property 1 'org-hd-marker a)))
4844 (mb (or (get-text-property 1 'org-marker b)
4845 (get-text-property 1 'org-hd-marker b)))
4846 (fa (and ma (marker-buffer ma)))
4847 (fb (and mb (marker-buffer mb)))
4848 (todo-kwds
4849 (or (and fa (with-current-buffer fa org-todo-keywords-1))
4850 (and fb (with-current-buffer fb org-todo-keywords-1))))
4851 (ta (or (get-text-property 1 'todo-state a) ""))
4852 (tb (or (get-text-property 1 'todo-state b) ""))
4853 (la (- (length (member ta todo-kwds))))
4854 (lb (- (length (member tb todo-kwds))))
4855 (donepa (member ta org-done-keywords-for-agenda))
4856 (donepb (member tb org-done-keywords-for-agenda)))
4857 (cond ((and donepa (not donepb)) -1)
4858 ((and (not donepa) donepb) +1)
4859 ((< la lb) -1)
4860 ((< lb la) +1)
4861 (t nil))))
4863 (defsubst org-cmp-tag (a b)
4864 "Compare the string values of the first tags of A and B."
4865 (let ((ta (car (last (get-text-property 1 'tags a))))
4866 (tb (car (last (get-text-property 1 'tags b)))))
4867 (cond ((not ta) +1)
4868 ((not tb) -1)
4869 ((string-lessp ta tb) -1)
4870 ((string-lessp tb ta) +1)
4871 (t nil))))
4873 (defsubst org-cmp-time (a b)
4874 "Compare the time-of-day values of strings A and B."
4875 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
4876 (ta (or (get-text-property 1 'time-of-day a) def))
4877 (tb (or (get-text-property 1 'time-of-day b) def)))
4878 (cond ((< ta tb) -1)
4879 ((< tb ta) +1)
4880 (t nil))))
4882 (defsubst org-cmp-habit-p (a b)
4883 "Compare the todo states of strings A and B."
4884 (let ((ha (get-text-property 1 'org-habit-p a))
4885 (hb (get-text-property 1 'org-habit-p b)))
4886 (cond ((and ha (not hb)) -1)
4887 ((and (not ha) hb) +1)
4888 (t nil))))
4890 (defun org-entries-lessp (a b)
4891 "Predicate for sorting agenda entries."
4892 ;; The following variables will be used when the form is evaluated.
4893 ;; So even though the compiler complains, keep them.
4894 (let* ((time-up (org-cmp-time a b))
4895 (time-down (if time-up (- time-up) nil))
4896 (priority-up (org-cmp-priority a b))
4897 (priority-down (if priority-up (- priority-up) nil))
4898 (effort-up (org-cmp-effort a b))
4899 (effort-down (if effort-up (- effort-up) nil))
4900 (category-up (org-cmp-category a b))
4901 (category-down (if category-up (- category-up) nil))
4902 (category-keep (if category-up +1 nil))
4903 (tag-up (org-cmp-tag a b))
4904 (tag-down (if tag-up (- tag-up) nil))
4905 (todo-state-up (org-cmp-todo-state a b))
4906 (todo-state-down (if todo-state-up (- todo-state-up) nil))
4907 (habit-up (org-cmp-habit-p a b))
4908 (habit-down (if habit-up (- habit-up) nil))
4909 user-defined-up user-defined-down)
4910 (if (and org-agenda-cmp-user-defined
4911 (functionp org-agenda-cmp-user-defined))
4912 (setq user-defined-up
4913 (funcall org-agenda-cmp-user-defined a b)
4914 user-defined-down (if user-defined-up (- user-defined-up) nil)))
4915 (cdr (assoc
4916 (eval (cons 'or org-agenda-sorting-strategy-selected))
4917 '((-1 . t) (1 . nil) (nil . nil))))))
4919 ;;; Agenda restriction lock
4921 (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
4922 "Overlay to mark the headline to which arenda commands are restricted.")
4923 (org-overlay-put org-agenda-restriction-lock-overlay
4924 'face 'org-agenda-restriction-lock)
4925 (org-overlay-put org-agenda-restriction-lock-overlay
4926 'help-echo "Agendas are currently limited to this subtree.")
4927 (org-detach-overlay org-agenda-restriction-lock-overlay)
4929 (defun org-agenda-set-restriction-lock (&optional type)
4930 "Set restriction lock for agenda, to current subtree or file.
4931 Restriction will be the file if TYPE is `file', or if type is the
4932 universal prefix '(4), or if the cursor is before the first headline
4933 in the file. Otherwise, restriction will be to the current subtree."
4934 (interactive "P")
4935 (and (equal type '(4)) (setq type 'file))
4936 (setq type (cond
4937 (type type)
4938 ((org-at-heading-p) 'subtree)
4939 ((condition-case nil (org-back-to-heading t) (error nil))
4940 'subtree)
4941 (t 'file)))
4942 (if (eq type 'subtree)
4943 (progn
4944 (setq org-agenda-restrict t)
4945 (setq org-agenda-overriding-restriction 'subtree)
4946 (put 'org-agenda-files 'org-restrict
4947 (list (buffer-file-name (buffer-base-buffer))))
4948 (org-back-to-heading t)
4949 (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
4950 (move-marker org-agenda-restrict-begin (point))
4951 (move-marker org-agenda-restrict-end
4952 (save-excursion (org-end-of-subtree t)))
4953 (message "Locking agenda restriction to subtree"))
4954 (put 'org-agenda-files 'org-restrict
4955 (list (buffer-file-name (buffer-base-buffer))))
4956 (setq org-agenda-restrict nil)
4957 (setq org-agenda-overriding-restriction 'file)
4958 (move-marker org-agenda-restrict-begin nil)
4959 (move-marker org-agenda-restrict-end nil)
4960 (message "Locking agenda restriction to file"))
4961 (setq current-prefix-arg nil)
4962 (org-agenda-maybe-redo))
4964 (defun org-agenda-remove-restriction-lock (&optional noupdate)
4965 "Remove the agenda restriction lock."
4966 (interactive "P")
4967 (org-detach-overlay org-agenda-restriction-lock-overlay)
4968 (org-detach-overlay org-speedbar-restriction-lock-overlay)
4969 (setq org-agenda-overriding-restriction nil)
4970 (setq org-agenda-restrict nil)
4971 (put 'org-agenda-files 'org-restrict nil)
4972 (move-marker org-agenda-restrict-begin nil)
4973 (move-marker org-agenda-restrict-end nil)
4974 (setq current-prefix-arg nil)
4975 (message "Agenda restriction lock removed")
4976 (or noupdate (org-agenda-maybe-redo)))
4978 (defun org-agenda-maybe-redo ()
4979 "If there is any window showing the agenda view, update it."
4980 (let ((w (get-buffer-window org-agenda-buffer-name t))
4981 (w0 (selected-window)))
4982 (when w
4983 (select-window w)
4984 (org-agenda-redo)
4985 (select-window w0)
4986 (if org-agenda-overriding-restriction
4987 (message "Agenda view shifted to new %s restriction"
4988 org-agenda-overriding-restriction)
4989 (message "Agenda restriction lock removed")))))
4991 ;;; Agenda commands
4993 (defun org-agenda-check-type (error &rest types)
4994 "Check if agenda buffer is of allowed type.
4995 If ERROR is non-nil, throw an error, otherwise just return nil."
4996 (if (memq org-agenda-type types)
4998 (if error
4999 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
5000 nil)))
5002 (defun org-agenda-quit ()
5003 "Exit agenda by removing the window or the buffer."
5004 (interactive)
5005 (if org-agenda-columns-active
5006 (org-columns-quit)
5007 (let ((buf (current-buffer)))
5008 (if (eq org-agenda-window-setup 'other-frame)
5009 (progn
5010 (kill-buffer buf)
5011 (org-agenda-reset-markers)
5012 (org-columns-remove-overlays)
5013 (setq org-agenda-archives-mode nil)
5014 (delete-frame))
5015 (and (not (eq org-agenda-window-setup 'current-window))
5016 (not (one-window-p))
5017 (delete-window))
5018 (kill-buffer buf)
5019 (org-agenda-reset-markers)
5020 (org-columns-remove-overlays)
5021 (setq org-agenda-archives-mode nil)))
5022 ;; Maybe restore the pre-agenda window configuration.
5023 (and org-agenda-restore-windows-after-quit
5024 (not (eq org-agenda-window-setup 'other-frame))
5025 org-pre-agenda-window-conf
5026 (set-window-configuration org-pre-agenda-window-conf))))
5028 (defun org-agenda-exit ()
5029 "Exit agenda by removing the window or the buffer.
5030 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
5031 Org-mode buffers visited directly by the user will not be touched."
5032 (interactive)
5033 (org-release-buffers org-agenda-new-buffers)
5034 (setq org-agenda-new-buffers nil)
5035 (org-agenda-quit))
5037 (defun org-agenda-execute (arg)
5038 "Execute another agenda command, keeping same window.\\<global-map>
5039 So this is just a shortcut for `\\[org-agenda]', available in the agenda."
5040 (interactive "P")
5041 (let ((org-agenda-window-setup 'current-window))
5042 (org-agenda arg)))
5044 (defun org-agenda-redo ()
5045 "Rebuild Agenda.
5046 When this is the global TODO list, a prefix argument will be interpreted."
5047 (interactive)
5048 (let* ((org-agenda-keep-modes t)
5049 (filter org-agenda-filter)
5050 (preset (get 'org-agenda-filter :preset-filter))
5051 (cols org-agenda-columns-active)
5052 (line (org-current-line))
5053 (window-line (- line (org-current-line (window-start))))
5054 (lprops (get 'org-agenda-redo-command 'org-lprops)))
5055 (put 'org-agenda-filter :preset-filter nil)
5056 (and cols (org-columns-quit))
5057 (message "Rebuilding agenda buffer...")
5058 (org-let lprops '(eval org-agenda-redo-command))
5059 (setq org-agenda-undo-list nil
5060 org-agenda-pending-undo-list nil)
5061 (message "Rebuilding agenda buffer...done")
5062 (put 'org-agenda-filter :preset-filter preset)
5063 (and (or filter preset) (org-agenda-filter-apply filter))
5064 (and cols (interactive-p) (org-agenda-columns))
5065 (org-goto-line line)
5066 (recenter window-line)))
5069 (defvar org-global-tags-completion-table nil)
5070 (defvar org-agenda-filter-form nil)
5071 (defun org-agenda-filter-by-tag (strip &optional char narrow)
5072 "Keep only those lines in the agenda buffer that have a specific tag.
5073 The tag is selected with its fast selection letter, as configured.
5074 With prefix argument STRIP, remove all lines that do have the tag.
5075 A lisp caller can specify CHAR. NARROW means that the new tag should be
5076 used to narrow the search - the interactive user can also press `-' or `+'
5077 to switch to narrowing."
5078 (interactive "P")
5079 (let* ((alist org-tag-alist-for-agenda)
5080 (tag-chars (mapconcat
5081 (lambda (x) (if (cdr x) (char-to-string (cdr x)) ""))
5082 alist ""))
5083 (efforts (org-split-string
5084 (or (cdr (assoc (concat org-effort-property "_ALL")
5085 org-global-properties))
5086 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00" "")))
5087 (effort-op org-agenda-filter-effort-default-operator)
5088 (effort-prompt "")
5089 (inhibit-read-only t)
5090 (current org-agenda-filter)
5091 char a n tag)
5092 (unless char
5093 (message
5094 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
5095 (if narrow "Narrow" "Filter") tag-chars
5096 (if org-agenda-auto-exclude-function "[RET], " ""))
5097 (setq char (read-char)))
5098 (when (member char '(?+ ?-))
5099 ;; Narrowing down
5100 (cond ((equal char ?-) (setq strip t narrow t))
5101 ((equal char ?+) (setq strip nil narrow t)))
5102 (message
5103 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
5104 (setq char (read-char)))
5105 (when (member char '(?< ?> ?= ??))
5106 ;; An effort operator
5107 (setq effort-op (char-to-string char))
5108 (setq alist nil) ; to make sure it will be interpreted as effort.
5109 (unless (equal char ??)
5110 (loop for i from 0 to 9 do
5111 (setq effort-prompt
5112 (concat
5113 effort-prompt " ["
5114 (if (= i 9) "0" (int-to-string (1+ i)))
5115 "]" (nth i efforts))))
5116 (message "Effort%s: %s " effort-op effort-prompt)
5117 (setq char (read-char))
5118 (when (or (< char ?0) (> char ?9))
5119 (error "Need 1-9,0 to select effort" ))))
5120 (when (equal char ?\t)
5121 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
5122 (org-set-local 'org-global-tags-completion-table
5123 (org-global-tags-completion-table)))
5124 (let ((completion-ignore-case t))
5125 (setq tag (org-icompleting-read
5126 "Tag: " org-global-tags-completion-table))))
5127 (cond
5128 ((equal char ?\r)
5129 (org-agenda-filter-by-tag-show-all)
5130 (when org-agenda-auto-exclude-function
5131 (setq org-agenda-filter '())
5132 (dolist (tag org-tag-alist-for-agenda)
5133 (let ((modifier (funcall org-agenda-auto-exclude-function
5134 (car tag))))
5135 (if modifier
5136 (push modifier org-agenda-filter))))
5137 (if (not (null org-agenda-filter))
5138 (org-agenda-filter-apply org-agenda-filter))))
5139 ((equal char ?/)
5140 (org-agenda-filter-by-tag-show-all)
5141 (when (get 'org-agenda-filter :preset-filter)
5142 (org-agenda-filter-apply org-agenda-filter)))
5143 ((or (equal char ?\ )
5144 (setq a (rassoc char alist))
5145 (and (>= char ?0) (<= char ?9)
5146 (setq n (if (= char ?0) 9 (- char ?0 1))
5147 tag (concat effort-op (nth n efforts))
5148 a (cons tag nil)))
5149 (and (= char ??)
5150 (setq tag "?eff")
5151 a (cons tag nil))
5152 (and tag (setq a (cons tag nil))))
5153 (org-agenda-filter-by-tag-show-all)
5154 (setq tag (car a))
5155 (setq org-agenda-filter
5156 (cons (concat (if strip "-" "+") tag)
5157 (if narrow current nil)))
5158 (org-agenda-filter-apply org-agenda-filter))
5159 (t (error "Invalid tag selection character %c" char)))))
5161 (defun org-agenda-filter-by-tag-refine (strip &optional char)
5162 "Refine the current filter. See `org-agenda-filter-by-tag."
5163 (interactive "P")
5164 (org-agenda-filter-by-tag strip char 'refine))
5166 (defun org-agenda-filter-make-matcher ()
5167 "Create the form that tests a line for the agenda filter."
5168 (let (f f1)
5169 (dolist (x (append (get 'org-agenda-filter :preset-filter)
5170 org-agenda-filter))
5171 (if (member x '("-" "+"))
5172 (setq f1 (if (equal x "-") 'tags '(not tags)))
5173 (if (string-match "[<=>?]" x)
5174 (setq f1 (org-agenda-filter-effort-form x))
5175 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
5176 (if (equal (string-to-char x) ?-)
5177 (setq f1 (list 'not f1))))
5178 (push f1 f))
5179 (cons 'and (nreverse f))))
5181 (defun org-agenda-filter-effort-form (e)
5182 "Return the form to compare the effort of the current line with what E says.
5183 E looks line \"+<2:25\"."
5184 (let (op)
5185 (setq e (substring e 1))
5186 (setq op (string-to-char e) e (substring e 1))
5187 (setq op (cond ((equal op ?<) '<=)
5188 ((equal op ?>) '>=)
5189 ((equal op ??) op)
5190 (t '=)))
5191 (list 'org-agenda-compare-effort (list 'quote op)
5192 (org-hh:mm-string-to-minutes e))))
5194 (defun org-agenda-compare-effort (op value)
5195 "Compare the effort of the current line with VALUE, using OP.
5196 If the line does not have an effort defined, return nil."
5197 (let ((eff (org-get-at-bol 'effort-minutes)))
5198 (if (equal op ??)
5199 (not eff)
5200 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
5201 value))))
5203 (defun org-agenda-filter-apply (filter)
5204 "Set FILTER as the new agenda filter and apply it."
5205 (let (tags)
5206 (setq org-agenda-filter filter
5207 org-agenda-filter-form (org-agenda-filter-make-matcher))
5208 (org-agenda-set-mode-name)
5209 (save-excursion
5210 (goto-char (point-min))
5211 (while (not (eobp))
5212 (if (org-get-at-bol 'org-marker)
5213 (progn
5214 (setq tags (org-get-at-bol 'tags)) ; used in eval
5215 (if (not (eval org-agenda-filter-form))
5216 (org-agenda-filter-by-tag-hide-line))
5217 (beginning-of-line 2))
5218 (beginning-of-line 2))))))
5220 (defun org-agenda-filter-by-tag-hide-line ()
5221 (let (ov)
5222 (setq ov (org-make-overlay (max (point-min) (1- (point-at-bol)))
5223 (point-at-eol)))
5224 (org-overlay-put ov 'invisible t)
5225 (org-overlay-put ov 'type 'tags-filter)
5226 (push ov org-agenda-filter-overlays)))
5228 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
5229 (setq pos (or pos (point)))
5230 (save-excursion
5231 (dolist (ov (org-overlays-at pos))
5232 (when (and (org-overlay-get ov 'invisible)
5233 (eq (org-overlay-get ov 'type) 'tags-filter))
5234 (goto-char pos)
5235 (if (< (org-overlay-start ov) (point-at-eol))
5236 (org-move-overlay ov (point-at-eol)
5237 (org-overlay-end ov)))))))
5239 (defun org-agenda-filter-by-tag-show-all ()
5240 (mapc 'org-delete-overlay org-agenda-filter-overlays)
5241 (setq org-agenda-filter-overlays nil)
5242 (setq org-agenda-filter nil)
5243 (setq org-agenda-filter-form nil)
5244 (org-agenda-set-mode-name))
5246 (defun org-agenda-manipulate-query-add ()
5247 "Manipulate the query by adding a search term with positive selection.
5248 Positive selection means, the term must be matched for selection of an entry."
5249 (interactive)
5250 (org-agenda-manipulate-query ?\[))
5251 (defun org-agenda-manipulate-query-subtract ()
5252 "Manipulate the query by adding a search term with negative selection.
5253 Negative selection means, term must not be matched for selection of an entry."
5254 (interactive)
5255 (org-agenda-manipulate-query ?\]))
5256 (defun org-agenda-manipulate-query-add-re ()
5257 "Manipulate the query by adding a search regexp with positive selection.
5258 Positive selection means, the regexp must match for selection of an entry."
5259 (interactive)
5260 (org-agenda-manipulate-query ?\{))
5261 (defun org-agenda-manipulate-query-subtract-re ()
5262 "Manipulate the query by adding a search regexp with negative selection.
5263 Negative selection means, regexp must not match for selection of an entry."
5264 (interactive)
5265 (org-agenda-manipulate-query ?\}))
5266 (defun org-agenda-manipulate-query (char)
5267 (cond
5268 ((memq org-agenda-type '(timeline agenda))
5269 (let ((org-agenda-include-inactive-timestamps t))
5270 (org-agenda-redo))
5271 (message "Display now includes inactive timestamps as well"))
5272 ((eq org-agenda-type 'search)
5273 (org-add-to-string
5274 'org-agenda-query-string
5275 (cdr (assoc char '((?\[ . " +") (?\] . " -")
5276 (?\{ . " +{}") (?\} . " -{}")))))
5277 (setq org-agenda-redo-command
5278 (list 'org-search-view
5279 org-todo-only
5280 org-agenda-query-string
5281 (+ (length org-agenda-query-string)
5282 (if (member char '(?\{ ?\})) 0 1))))
5283 (set-register org-agenda-query-register org-agenda-query-string)
5284 (org-agenda-redo))
5285 (t (error "Cannot manipulate query for %s-type agenda buffers"
5286 org-agenda-type))))
5288 (defun org-add-to-string (var string)
5289 (set var (concat (symbol-value var) string)))
5291 (defun org-agenda-goto-date (date)
5292 "Jump to DATE in agenda."
5293 (interactive (list (org-read-date)))
5294 (org-agenda-list nil date))
5296 (defun org-agenda-goto-today ()
5297 "Go to today."
5298 (interactive)
5299 (org-agenda-check-type t 'timeline 'agenda)
5300 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
5301 (cond
5302 (tdpos (goto-char tdpos))
5303 ((eq org-agenda-type 'agenda)
5304 (let* ((sd (time-to-days
5305 (time-subtract (current-time)
5306 (list 0 (* 3600 org-extend-today-until) 0))))
5307 (comp (org-agenda-compute-time-span sd org-agenda-span))
5308 (org-agenda-overriding-arguments org-agenda-last-arguments))
5309 (setf (nth 1 org-agenda-overriding-arguments) (car comp))
5310 (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
5311 (org-agenda-redo)
5312 (org-agenda-find-same-or-today-or-agenda)))
5313 (t (error "Cannot find today")))))
5315 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
5316 (goto-char
5317 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
5318 (text-property-any (point-min) (point-max) 'org-today t)
5319 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
5320 (point-min))))
5322 (defun org-agenda-later (arg)
5323 "Go forward in time by thee current span.
5324 With prefix ARG, go forward that many times the current span."
5325 (interactive "p")
5326 (org-agenda-check-type t 'agenda)
5327 (let* ((span org-agenda-span)
5328 (sd org-starting-day)
5329 (greg (calendar-gregorian-from-absolute sd))
5330 (cnt (org-get-at-bol 'org-day-cnt))
5331 greg2 nd)
5332 (cond
5333 ((eq span 'day)
5334 (setq sd (+ arg sd) nd 1))
5335 ((eq span 'week)
5336 (setq sd (+ (* 7 arg) sd) nd 7))
5337 ((eq span 'month)
5338 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
5339 sd (calendar-absolute-from-gregorian greg2))
5340 (setcar greg2 (1+ (car greg2)))
5341 (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
5342 ((eq span 'year)
5343 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
5344 sd (calendar-absolute-from-gregorian greg2))
5345 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
5346 (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
5347 (let ((org-agenda-overriding-arguments
5348 (list (car org-agenda-last-arguments) sd nd t)))
5349 (org-agenda-redo)
5350 (org-agenda-find-same-or-today-or-agenda cnt))))
5352 (defun org-agenda-earlier (arg)
5353 "Go backward in time by the current span.
5354 With prefix ARG, go backward that many times the current span."
5355 (interactive "p")
5356 (org-agenda-later (- arg)))
5358 (defun org-agenda-view-mode-dispatch ()
5359 "Call one of the view mode commands."
5360 (interactive)
5361 (message "View: [d]ay [w]eek [m]onth [y]ear [l]og [L]og-all [a]rch-trees [A]rch-files
5362 clock[R]eport time[G]rid [[]inactive [E]ntryText include[D]iary")
5363 (let ((a (read-char-exclusive)))
5364 (case a
5365 (?d (call-interactively 'org-agenda-day-view))
5366 (?w (call-interactively 'org-agenda-week-view))
5367 (?m (call-interactively 'org-agenda-month-view))
5368 (?y (call-interactively 'org-agenda-year-view))
5369 (?l (call-interactively 'org-agenda-log-mode))
5370 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
5371 (?a (call-interactively 'org-agenda-archives-mode))
5372 (?A (org-agenda-archives-mode 'files))
5373 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
5374 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
5375 (?G (call-interactively 'org-agenda-toggle-time-grid))
5376 (?D (call-interactively 'org-agenda-toggle-diary))
5377 (?\[ (let ((org-agenda-include-inactive-timestamps t))
5378 (org-agenda-check-type t 'timeline 'agenda)
5379 (org-agenda-redo))
5380 (message "Display now includes inactive timestamps as well"))
5381 (?q (message "Abort"))
5382 (otherwise (error "Invalid key" )))))
5384 (defun org-agenda-day-view (&optional day-of-year)
5385 "Switch to daily view for agenda.
5386 With argument DAY-OF-YEAR, switch to that day of the year."
5387 (interactive "P")
5388 (setq org-agenda-ndays 1)
5389 (org-agenda-change-time-span 'day day-of-year))
5390 (defun org-agenda-week-view (&optional iso-week)
5391 "Switch to daily view for agenda.
5392 With argument ISO-WEEK, switch to the corresponding ISO week.
5393 If ISO-WEEK has more then 2 digits, only the last two encode the
5394 week. Any digits before this encode a year. So 200712 means
5395 week 12 of year 2007. Years in the range 1938-2037 can also be
5396 written as 2-digit years."
5397 (interactive "P")
5398 (setq org-agenda-ndays 7)
5399 (org-agenda-change-time-span 'week iso-week))
5400 (defun org-agenda-month-view (&optional month)
5401 "Switch to monthly view for agenda.
5402 With argument MONTH, switch to that month."
5403 (interactive "P")
5404 (org-agenda-change-time-span 'month month))
5405 (defun org-agenda-year-view (&optional year)
5406 "Switch to yearly view for agenda.
5407 With argument YEAR, switch to that year.
5408 If MONTH has more then 2 digits, only the last two encode the
5409 month. Any digits before this encode a year. So 200712 means
5410 December year 2007. Years in the range 1938-2037 can also be
5411 written as 2-digit years."
5412 (interactive "P")
5413 (when year
5414 (setq year (org-small-year-to-year year)))
5415 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
5416 (org-agenda-change-time-span 'year year)
5417 (error "Abort")))
5419 (defun org-agenda-change-time-span (span &optional n)
5420 "Change the agenda view to SPAN.
5421 SPAN may be `day', `week', `month', `year'."
5422 (org-agenda-check-type t 'agenda)
5423 (if (and (not n) (equal org-agenda-span span))
5424 (error "Viewing span is already \"%s\"" span))
5425 (let* ((sd (or (org-get-at-bol 'day)
5426 org-starting-day))
5427 (computed (org-agenda-compute-time-span sd span n))
5428 (org-agenda-overriding-arguments
5429 (list (car org-agenda-last-arguments)
5430 (car computed) (cdr computed) t)))
5431 (org-agenda-redo)
5432 (org-agenda-find-same-or-today-or-agenda))
5433 (org-agenda-set-mode-name)
5434 (message "Switched to %s view" span))
5436 (defun org-agenda-compute-time-span (sd span &optional n)
5437 "Compute starting date and number of days for agenda.
5438 SPAN may be `day', `week', `month', `year'. The return value
5439 is a cons cell with the starting date and the number of days,
5440 so that the date SD will be in that range."
5441 (let* ((greg (calendar-gregorian-from-absolute sd))
5442 (dg (nth 1 greg))
5443 (mg (car greg))
5444 (yg (nth 2 greg))
5445 nd w1 y1 m1 thisweek)
5446 (cond
5447 ((eq span 'day)
5448 (when n
5449 (setq sd (+ (calendar-absolute-from-gregorian
5450 (list mg 1 yg))
5451 n -1)))
5452 (setq nd 1))
5453 ((eq span 'week)
5454 (let* ((nt (calendar-day-of-week
5455 (calendar-gregorian-from-absolute sd)))
5456 (d (if org-agenda-start-on-weekday
5457 (- nt org-agenda-start-on-weekday)
5458 0)))
5459 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
5460 (when n
5461 (require 'cal-iso)
5462 (setq thisweek (car (calendar-iso-from-absolute sd)))
5463 (when (> n 99)
5464 (setq y1 (org-small-year-to-year (/ n 100))
5465 n (mod n 100)))
5466 (setq sd
5467 (calendar-absolute-from-iso
5468 (list n 1
5469 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))
5470 (setq nd 7)))
5471 ((eq span 'month)
5472 (when (and n (> n 99))
5473 (setq y1 (org-small-year-to-year (/ n 100))
5474 n (mod n 100)))
5475 (setq sd (calendar-absolute-from-gregorian
5476 (list (or n mg) 1 (or y1 yg)))
5477 nd (- (calendar-absolute-from-gregorian
5478 (list (1+ (or n mg)) 1 (or y1 yg)))
5479 sd)))
5480 ((eq span 'year)
5481 (setq sd (calendar-absolute-from-gregorian
5482 (list 1 1 (or n yg)))
5483 nd (- (calendar-absolute-from-gregorian
5484 (list 1 1 (1+ (or n yg))))
5485 sd))))
5486 (cons sd nd)))
5488 (defun org-agenda-next-date-line (&optional arg)
5489 "Jump to the next line indicating a date in agenda buffer."
5490 (interactive "p")
5491 (org-agenda-check-type t 'agenda 'timeline)
5492 (beginning-of-line 1)
5493 ;; This does not work if user makes date format that starts with a blank
5494 (if (looking-at "^\\S-") (forward-char 1))
5495 (if (not (re-search-forward "^\\S-" nil t arg))
5496 (progn
5497 (backward-char 1)
5498 (error "No next date after this line in this buffer")))
5499 (goto-char (match-beginning 0)))
5501 (defun org-agenda-previous-date-line (&optional arg)
5502 "Jump to the previous line indicating a date in agenda buffer."
5503 (interactive "p")
5504 (org-agenda-check-type t 'agenda 'timeline)
5505 (beginning-of-line 1)
5506 (if (not (re-search-backward "^\\S-" nil t arg))
5507 (error "No previous date before this line in this buffer")))
5509 ;; Initialize the highlight
5510 (defvar org-hl (org-make-overlay 1 1))
5511 (org-overlay-put org-hl 'face 'highlight)
5513 (defun org-highlight (begin end &optional buffer)
5514 "Highlight a region with overlay."
5515 (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
5516 org-hl begin end (or buffer (current-buffer))))
5518 (defun org-unhighlight ()
5519 "Detach overlay INDEX."
5520 (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
5522 ;; FIXME this is currently not used.
5523 (defun org-highlight-until-next-command (beg end &optional buffer)
5524 "Move the highlight overlay to BEG/END, remove it before the next command."
5525 (org-highlight beg end buffer)
5526 (add-hook 'pre-command-hook 'org-unhighlight-once))
5527 (defun org-unhighlight-once ()
5528 "Remove the highlight from its position, and this function from the hook."
5529 (remove-hook 'pre-command-hook 'org-unhighlight-once)
5530 (org-unhighlight))
5532 (defun org-agenda-follow-mode ()
5533 "Toggle follow mode in an agenda buffer."
5534 (interactive)
5535 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
5536 (org-agenda-set-mode-name)
5537 (message "Follow mode is %s"
5538 (if org-agenda-follow-mode "on" "off")))
5540 (defun org-agenda-entry-text-mode (&optional arg)
5541 "Toggle entry text mode in an agenda buffer."
5542 (interactive "P")
5543 (if (integerp arg)
5544 (setq org-agenda-entry-text-mode t)
5545 (setq org-agenda-entry-text-mode (not org-agenda-entry-text-mode)))
5546 (org-agenda-entry-text-hide)
5547 (and org-agenda-entry-text-mode
5548 (let ((org-agenda-entry-text-maxlines
5549 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
5550 (org-agenda-entry-text-show)))
5551 (org-agenda-set-mode-name)
5552 (message "Entry text mode is %s. Maximum number of lines is %d"
5553 (if org-agenda-entry-text-mode "on" "off")
5554 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
5556 (defun org-agenda-clockreport-mode ()
5557 "Toggle clocktable mode in an agenda buffer."
5558 (interactive)
5559 (org-agenda-check-type t 'agenda)
5560 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
5561 (org-agenda-set-mode-name)
5562 (org-agenda-redo)
5563 (message "Clocktable mode is %s"
5564 (if org-agenda-clockreport-mode "on" "off")))
5566 (defun org-agenda-log-mode (&optional special)
5567 "Toggle log mode in an agenda buffer.
5568 With argument SPECIAL, show all possible log items, not only the ones
5569 configured in `org-agenda-log-mode-items'.
5570 With a double `C-u' prefix arg, show *only* log items, nothing else."
5571 (interactive "P")
5572 (org-agenda-check-type t 'agenda 'timeline)
5573 (setq org-agenda-show-log
5574 (if (equal special '(16))
5575 'only
5576 (if special '(closed clock state)
5577 (not org-agenda-show-log))))
5578 (org-agenda-set-mode-name)
5579 (org-agenda-redo)
5580 (message "Log mode is %s"
5581 (if org-agenda-show-log "on" "off")))
5583 (defun org-agenda-archives-mode (&optional with-files)
5584 "Toggle inclusion of items in trees marked with :ARCHIVE:.
5585 When called with a prefix argument, include all archive files as well."
5586 (interactive "P")
5587 (setq org-agenda-archives-mode
5588 (if with-files t (if org-agenda-archives-mode nil 'trees)))
5589 (org-agenda-set-mode-name)
5590 (org-agenda-redo)
5591 (message
5592 "%s"
5593 (cond
5594 ((eq org-agenda-archives-mode nil)
5595 "No archives are included")
5596 ((eq org-agenda-archives-mode 'trees)
5597 (format "Trees with :%s: tag are included" org-archive-tag))
5598 ((eq org-agenda-archives-mode t)
5599 (format "Trees with :%s: tag and all active archive files are included"
5600 org-archive-tag)))))
5602 (defun org-agenda-toggle-diary ()
5603 "Toggle diary inclusion in an agenda buffer."
5604 (interactive)
5605 (org-agenda-check-type t 'agenda)
5606 (setq org-agenda-include-diary (not org-agenda-include-diary))
5607 (org-agenda-redo)
5608 (org-agenda-set-mode-name)
5609 (message "Diary inclusion turned %s"
5610 (if org-agenda-include-diary "on" "off")))
5612 (defun org-agenda-toggle-time-grid ()
5613 "Toggle time grid in an agenda buffer."
5614 (interactive)
5615 (org-agenda-check-type t 'agenda)
5616 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
5617 (org-agenda-redo)
5618 (org-agenda-set-mode-name)
5619 (message "Time-grid turned %s"
5620 (if org-agenda-use-time-grid "on" "off")))
5622 (defun org-agenda-set-mode-name ()
5623 "Set the mode name to indicate all the small mode settings."
5624 (setq mode-name
5625 (concat "Org-Agenda"
5626 (if (equal org-agenda-ndays 1) " Day" "")
5627 (if (equal org-agenda-ndays 7) " Week" "")
5628 (if org-agenda-follow-mode " Follow" "")
5629 (if org-agenda-entry-text-mode " ETxt" "")
5630 (if org-agenda-include-diary " Diary" "")
5631 (if org-agenda-use-time-grid " Grid" "")
5632 (if (consp org-agenda-show-log) " LogAll"
5633 (if org-agenda-show-log " Log" ""))
5634 (if (or org-agenda-filter (get 'org-agenda-filter
5635 :preset-filter))
5636 (concat " {" (mapconcat
5637 'identity
5638 (append (get 'org-agenda-filter
5639 :preset-filter)
5640 org-agenda-filter) "") "}")
5642 (if org-agenda-archives-mode
5643 (if (eq org-agenda-archives-mode t)
5644 " Archives"
5645 (format " :%s:" org-archive-tag))
5647 (if org-agenda-clockreport-mode " Clock" "")))
5648 (force-mode-line-update))
5650 (defun org-agenda-post-command-hook ()
5651 (setq org-agenda-type
5652 (or (get-text-property (point) 'org-agenda-type)
5653 (get-text-property (max (point-min) (1- (point)))
5654 'org-agenda-type)))
5655 (if (and org-agenda-follow-mode
5656 (org-get-at-bol 'org-marker))
5657 (org-agenda-show)))
5659 (defun org-agenda-show-priority ()
5660 "Show the priority of the current item.
5661 This priority is composed of the main priority given with the [#A] cookies,
5662 and by additional input from the age of a schedules or deadline entry."
5663 (interactive)
5664 (let* ((pri (org-get-at-bol 'priority)))
5665 (message "Priority is %d" (if pri pri -1000))))
5667 (defun org-agenda-show-tags ()
5668 "Show the tags applicable to the current item."
5669 (interactive)
5670 (let* ((tags (org-get-at-bol 'tags)))
5671 (if tags
5672 (message "Tags are :%s:"
5673 (org-no-properties (mapconcat 'identity tags ":")))
5674 (message "No tags associated with this line"))))
5676 (defun org-agenda-goto (&optional highlight)
5677 "Go to the Org-mode file which contains the item at point."
5678 (interactive)
5679 (let* ((marker (or (org-get-at-bol 'org-marker)
5680 (org-agenda-error)))
5681 (buffer (marker-buffer marker))
5682 (pos (marker-position marker)))
5683 (switch-to-buffer-other-window buffer)
5684 (widen)
5685 (goto-char pos)
5686 (when (org-mode-p)
5687 (org-show-context 'agenda)
5688 (save-excursion
5689 (and (outline-next-heading)
5690 (org-flag-heading nil)))) ; show the next heading
5691 (recenter (/ (window-height) 2))
5692 (run-hooks 'org-agenda-after-show-hook)
5693 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
5695 (defvar org-agenda-after-show-hook nil
5696 "Normal hook run after an item has been shown from the agenda.
5697 Point is in the buffer where the item originated.")
5699 (defun org-agenda-kill ()
5700 "Kill the entry or subtree belonging to the current agenda entry."
5701 (interactive)
5702 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5703 (let* ((marker (or (org-get-at-bol 'org-marker)
5704 (org-agenda-error)))
5705 (buffer (marker-buffer marker))
5706 (pos (marker-position marker))
5707 (type (org-get-at-bol 'type))
5708 dbeg dend (n 0) conf)
5709 (org-with-remote-undo buffer
5710 (with-current-buffer buffer
5711 (save-excursion
5712 (goto-char pos)
5713 (if (and (org-mode-p) (not (member type '("sexp"))))
5714 (setq dbeg (progn (org-back-to-heading t) (point))
5715 dend (org-end-of-subtree t t))
5716 (setq dbeg (point-at-bol)
5717 dend (min (point-max) (1+ (point-at-eol)))))
5718 (goto-char dbeg)
5719 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
5720 (setq conf (or (eq t org-agenda-confirm-kill)
5721 (and (numberp org-agenda-confirm-kill)
5722 (> n org-agenda-confirm-kill))))
5723 (and conf
5724 (not (y-or-n-p
5725 (format "Delete entry with %d lines in buffer \"%s\"? "
5726 n (buffer-name buffer))))
5727 (error "Abort"))
5728 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
5729 (with-current-buffer buffer (delete-region dbeg dend))
5730 (message "Agenda item and source killed"))))
5732 (defun org-agenda-archive ()
5733 "Archive the entry or subtree belonging to the current agenda entry."
5734 (interactive)
5735 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5736 (let* ((marker (or (org-get-at-bol 'org-marker)
5737 (org-agenda-error)))
5738 (buffer (marker-buffer marker))
5739 (pos (marker-position marker)))
5740 (org-with-remote-undo buffer
5741 (with-current-buffer buffer
5742 (if (org-mode-p)
5743 (save-excursion
5744 (goto-char pos)
5745 (org-remove-subtree-entries-from-agenda)
5746 (org-back-to-heading t)
5747 (org-archive-subtree))
5748 (error "Archiving works only in Org-mode files"))))))
5750 (defun org-agenda-archive-to-archive-sibling ()
5751 "Move the entry to the archive sibling."
5752 (interactive)
5753 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5754 (let* ((marker (or (org-get-at-bol 'org-marker)
5755 (org-agenda-error)))
5756 (buffer (marker-buffer marker))
5757 (pos (marker-position marker)))
5758 (org-with-remote-undo buffer
5759 (with-current-buffer buffer
5760 (if (org-mode-p)
5761 (save-excursion
5762 (goto-char pos)
5763 (org-remove-subtree-entries-from-agenda)
5764 (org-back-to-heading t)
5765 (org-archive-to-archive-sibling))
5766 (error "Archiving works only in Org-mode files"))))))
5768 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
5769 "Remove all lines in the agenda that correspond to a given subtree.
5770 The subtree is the one in buffer BUF, starting at BEG and ending at END.
5771 If this information is not given, the function uses the tree at point."
5772 (let ((buf (or buf (current-buffer))) m p)
5773 (save-excursion
5774 (unless (and beg end)
5775 (org-back-to-heading t)
5776 (setq beg (point))
5777 (org-end-of-subtree t)
5778 (setq end (point)))
5779 (set-buffer (get-buffer org-agenda-buffer-name))
5780 (save-excursion
5781 (goto-char (point-max))
5782 (beginning-of-line 1)
5783 (while (not (bobp))
5784 (when (and (setq m (org-get-at-bol 'org-marker))
5785 (equal buf (marker-buffer m))
5786 (setq p (marker-position m))
5787 (>= p beg)
5788 (< p end))
5789 (let ((inhibit-read-only t))
5790 (delete-region (point-at-bol) (1+ (point-at-eol)))))
5791 (beginning-of-line 0))))))
5793 (defun org-agenda-refile (&optional goto rfloc)
5794 "Refile the item at point."
5795 (interactive "P")
5796 (if (equal goto '(16))
5797 (org-refile-goto-last-stored)
5798 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
5799 (org-agenda-error)))
5800 (buffer (marker-buffer marker))
5801 (pos (marker-position marker))
5802 (rfloc (or rfloc
5803 (org-refile-get-location
5804 (if goto "Goto: " "Refile to: ") buffer
5805 org-refile-allow-creating-parent-nodes))))
5806 (with-current-buffer buffer
5807 (save-excursion
5808 (save-restriction
5809 (widen)
5810 (goto-char marker)
5811 (org-remove-subtree-entries-from-agenda)
5812 (org-refile goto buffer rfloc)))))))
5814 (defun org-agenda-open-link (&optional arg)
5815 "Follow the link in the current line, if any.
5816 This looks for a link in the displayed lin in the agenda. It also looks
5817 at the text of the entry itself."
5818 (interactive "P")
5819 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
5820 (org-get-at-bol 'org-marker)))
5821 (buffer (and marker (marker-buffer marker)))
5822 (prefix (buffer-substring
5823 (point-at-bol)
5824 (+ (point-at-bol)
5825 (org-get-at-bol 'prefix-length)))))
5826 (unless buffer (error "Don't know where to look for links"))
5827 (with-current-buffer buffer
5828 (save-excursion
5829 (save-restriction
5830 (widen)
5831 (goto-char marker)
5832 (org-offer-links-in-entry arg prefix))))))
5834 (defun org-agenda-copy-local-variable (var)
5835 "Get a variable from a referenced buffer and install it here."
5836 (let ((m (org-get-at-bol 'org-marker)))
5837 (when (and m (buffer-live-p (marker-buffer m)))
5838 (org-set-local var (with-current-buffer (marker-buffer m)
5839 (symbol-value var))))))
5841 (defun org-agenda-switch-to (&optional delete-other-windows)
5842 "Go to the Org-mode file which contains the item at point."
5843 (interactive)
5844 (let* ((marker (or (org-get-at-bol 'org-marker)
5845 (org-agenda-error)))
5846 (buffer (marker-buffer marker))
5847 (pos (marker-position marker)))
5848 (switch-to-buffer buffer)
5849 (and delete-other-windows (delete-other-windows))
5850 (widen)
5851 (goto-char pos)
5852 (when (org-mode-p)
5853 (org-show-context 'agenda)
5854 (save-excursion
5855 (and (outline-next-heading)
5856 (org-flag-heading nil)))))) ; show the next heading
5858 (defun org-agenda-goto-mouse (ev)
5859 "Go to the Org-mode file which contains the item at the mouse click."
5860 (interactive "e")
5861 (mouse-set-point ev)
5862 (org-agenda-goto))
5864 (defun org-agenda-show (&optional full-entry)
5865 "Display the Org-mode file which contains the item at point.
5866 With prefix argument FULL-ENTRY, make the entire entry visible
5867 if it was hidden in the outline."
5868 (interactive "P")
5869 (let ((win (selected-window)))
5870 (if full-entry
5871 (let ((org-show-entry-below t))
5872 (org-agenda-goto t))
5873 (org-agenda-goto t))
5874 (select-window win)))
5876 (defun org-agenda-show-1 (&optional more)
5877 "Display the Org-mode file which contains the item at point.
5878 The prefix arg causes further revieling:
5880 0 hide the subtree
5881 1 just show the entry according to defaults.
5882 2 show the children view
5883 3 show the subtree view
5884 4 show the entire subtree and any LOGBOOK drawers
5885 5 show the entire subtree and any drawers
5886 With prefix argument FULL-ENTRY, make the entire entry visible
5887 if it was hidden in the outline."
5888 (interactive "p")
5889 (let ((win (selected-window)))
5890 (org-agenda-goto t)
5891 (org-recenter-heading 1)
5892 (cond
5893 ((= more 0)
5894 (hide-subtree)
5895 (save-excursion
5896 (org-back-to-heading)
5897 (run-hook-with-args 'org-cycle-hook 'folded))
5898 (message "Remote: FOLDED"))
5899 ((and (interactive-p) (= more 1))
5900 (message "Remote: show with default settings"))
5901 ((= more 2)
5902 (show-entry)
5903 (show-children)
5904 (save-excursion
5905 (org-back-to-heading)
5906 (run-hook-with-args 'org-cycle-hook 'children))
5907 (message "Remote: CHILDREN"))
5908 ((= more 3)
5909 (show-subtree)
5910 (save-excursion
5911 (org-back-to-heading)
5912 (run-hook-with-args 'org-cycle-hook 'subtree))
5913 (message "Remote: SUBTREE"))
5914 ((= more 4)
5915 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
5916 (org-drawer-regexp
5917 (concat "^[ \t]*:\\("
5918 (mapconcat 'regexp-quote org-drawers "\\|")
5919 "\\):[ \t]*$")))
5920 (show-subtree)
5921 (save-excursion
5922 (org-back-to-heading)
5923 (org-cycle-hide-drawers 'subtree)))
5924 (message "Remote: SUBTREE AND LOGBOOK"))
5925 ((> more 4)
5926 (show-subtree)
5927 (message "Remote: SUBTREE AND ALL DRAWERS")))
5928 (select-window win)))
5930 (defun org-recenter-heading (n)
5931 (save-excursion
5932 (org-back-to-heading)
5933 (recenter n)))
5935 (defvar org-agenda-cycle-counter nil)
5936 (defun org-agenda-cycle-show (&optional n)
5937 "Show the current entry in another window, with default settings.
5938 Default settings are taken from `org-show-hierarchy-above' and siblings.
5939 When use repeatedly in immediate succession, the remote entry will cycle
5940 through visibility
5942 children -> subtree -> folded
5944 When called with a numeric prefix arg, that arg will be passed through to
5945 `org-agenda-show-1'. For the interpretation of that argument, see the
5946 docstring of `org-agenda-show-1'."
5947 (interactive "P")
5948 (if (integerp n)
5949 (setq org-agenda-cycle-counter n)
5950 (if (not (eq last-command this-command))
5951 (setq org-agenda-cycle-counter 1)
5952 (if (equal org-agenda-cycle-counter 0)
5953 (setq org-agenda-cycle-counter 2)
5954 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
5955 (if (> org-agenda-cycle-counter 3)
5956 (setq org-agenda-cycle-counter 0)))))
5957 (org-agenda-show-1 org-agenda-cycle-counter))
5959 (defun org-agenda-recenter (arg)
5960 "Display the Org-mode file which contains the item at point and recenter."
5961 (interactive "P")
5962 (let ((win (selected-window)))
5963 (org-agenda-goto t)
5964 (recenter arg)
5965 (select-window win)))
5967 (defun org-agenda-show-mouse (ev)
5968 "Display the Org-mode file which contains the item at the mouse click."
5969 (interactive "e")
5970 (mouse-set-point ev)
5971 (org-agenda-show))
5973 (defun org-agenda-check-no-diary ()
5974 "Check if the entry is a diary link and abort if yes."
5975 (if (org-get-at-bol 'org-agenda-diary-link)
5976 (org-agenda-error)))
5978 (defun org-agenda-error ()
5979 (error "Command not allowed in this line"))
5981 (defun org-agenda-tree-to-indirect-buffer ()
5982 "Show the subtree corresponding to the current entry in an indirect buffer.
5983 This calls the command `org-tree-to-indirect-buffer' from the original
5984 Org-mode buffer.
5985 With numerical prefix arg ARG, go up to this level and then take that tree.
5986 With a C-u prefix, make a separate frame for this tree (i.e. don't use the
5987 dedicated frame)."
5988 (interactive)
5989 (org-agenda-check-no-diary)
5990 (let* ((marker (or (org-get-at-bol 'org-marker)
5991 (org-agenda-error)))
5992 (buffer (marker-buffer marker))
5993 (pos (marker-position marker)))
5994 (with-current-buffer buffer
5995 (save-excursion
5996 (goto-char pos)
5997 (call-interactively 'org-tree-to-indirect-buffer)))))
5999 (defvar org-last-heading-marker (make-marker)
6000 "Marker pointing to the headline that last changed its TODO state
6001 by a remote command from the agenda.")
6003 (defun org-agenda-todo-nextset ()
6004 "Switch TODO entry to next sequence."
6005 (interactive)
6006 (org-agenda-todo 'nextset))
6008 (defun org-agenda-todo-previousset ()
6009 "Switch TODO entry to previous sequence."
6010 (interactive)
6011 (org-agenda-todo 'previousset))
6013 (defun org-agenda-todo (&optional arg)
6014 "Cycle TODO state of line at point, also in Org-mode file.
6015 This changes the line at point, all other lines in the agenda referring to
6016 the same tree node, and the headline of the tree node in the Org-mode file."
6017 (interactive "P")
6018 (org-agenda-check-no-diary)
6019 (let* ((col (current-column))
6020 (marker (or (org-get-at-bol 'org-marker)
6021 (org-agenda-error)))
6022 (buffer (marker-buffer marker))
6023 (pos (marker-position marker))
6024 (hdmarker (org-get-at-bol 'org-hd-marker))
6025 (todayp (equal (org-get-at-bol 'day)
6026 (time-to-days (current-time))))
6027 (inhibit-read-only t)
6028 org-agenda-headline-snapshot-before-repeat newhead just-one)
6029 (org-with-remote-undo buffer
6030 (with-current-buffer buffer
6031 (widen)
6032 (goto-char pos)
6033 (org-show-context 'agenda)
6034 (save-excursion
6035 (and (outline-next-heading)
6036 (org-flag-heading nil))) ; show the next heading
6037 (let ((current-prefix-arg arg))
6038 (call-interactively 'org-todo))
6039 (and (bolp) (forward-char 1))
6040 (setq newhead (org-get-heading))
6041 (when (and (org-bound-and-true-p
6042 org-agenda-headline-snapshot-before-repeat)
6043 (not (equal org-agenda-headline-snapshot-before-repeat
6044 newhead))
6045 todayp)
6046 (setq newhead org-agenda-headline-snapshot-before-repeat
6047 just-one t))
6048 (save-excursion
6049 (org-back-to-heading)
6050 (move-marker org-last-heading-marker (point))))
6051 (beginning-of-line 1)
6052 (save-excursion
6053 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
6054 (org-move-to-column col))))
6056 (defun org-agenda-add-note (&optional arg)
6057 "Add a time-stamped note to the entry at point."
6058 (interactive "P")
6059 (org-agenda-check-no-diary)
6060 (let* ((marker (or (org-get-at-bol 'org-marker)
6061 (org-agenda-error)))
6062 (buffer (marker-buffer marker))
6063 (pos (marker-position marker))
6064 (hdmarker (org-get-at-bol 'org-hd-marker))
6065 (inhibit-read-only t))
6066 (with-current-buffer buffer
6067 (widen)
6068 (goto-char pos)
6069 (org-show-context 'agenda)
6070 (save-excursion
6071 (and (outline-next-heading)
6072 (org-flag-heading nil))) ; show the next heading
6073 (org-add-note))))
6075 (defun org-agenda-change-all-lines (newhead hdmarker
6076 &optional fixface just-this)
6077 "Change all lines in the agenda buffer which match HDMARKER.
6078 The new content of the line will be NEWHEAD (as modified by
6079 `org-format-agenda-item'). HDMARKER is checked with
6080 `equal' against all `org-hd-marker' text properties in the file.
6081 If FIXFACE is non-nil, the face of each item is modified according to
6082 the new TODO state.
6083 If JUST-THIS is non-nil, change just the current line, not all.
6084 If FORCE-TAGS is non nil, the car of it returns the new tags."
6085 (let* ((inhibit-read-only t)
6086 (line (org-current-line))
6087 (thetags (with-current-buffer (marker-buffer hdmarker)
6088 (save-excursion (save-restriction (widen)
6089 (goto-char hdmarker)
6090 (org-get-tags-at)))))
6091 props m pl undone-face done-face finish new dotime cat tags)
6092 (save-excursion
6093 (goto-char (point-max))
6094 (beginning-of-line 1)
6095 (while (not finish)
6096 (setq finish (bobp))
6097 (when (and (setq m (org-get-at-bol 'org-hd-marker))
6098 (or (not just-this) (= (org-current-line) line))
6099 (equal m hdmarker))
6100 (setq props (text-properties-at (point))
6101 dotime (org-get-at-bol 'dotime)
6102 cat (org-get-at-bol 'org-category)
6103 tags thetags
6104 new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
6105 pl (org-get-at-bol 'prefix-length)
6106 undone-face (org-get-at-bol 'undone-face)
6107 done-face (org-get-at-bol 'done-face))
6108 (goto-char (+ (point) pl))
6109 ;; (org-move-to-column pl) FIXME: does the above line work correctly?
6110 (cond
6111 ((equal new "")
6112 (beginning-of-line 1)
6113 (and (looking-at ".*\n?") (replace-match "")))
6114 ((looking-at ".*")
6115 (replace-match new t t)
6116 (beginning-of-line 1)
6117 (add-text-properties (point-at-bol) (point-at-eol) props)
6118 (when fixface
6119 (add-text-properties
6120 (point-at-bol) (point-at-eol)
6121 (list 'face
6122 (if org-last-todo-state-is-todo
6123 undone-face done-face))))
6124 (org-agenda-highlight-todo 'line)
6125 (beginning-of-line 1))
6126 (t (error "Line update did not work"))))
6127 (beginning-of-line 0)))
6128 (org-finalize-agenda)))
6130 (defun org-agenda-align-tags (&optional line)
6131 "Align all tags in agenda items to `org-agenda-tags-column'."
6132 (let ((inhibit-read-only t) l c)
6133 (save-excursion
6134 (goto-char (if line (point-at-bol) (point-min)))
6135 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
6136 (if line (point-at-eol) nil) t)
6137 (add-text-properties
6138 (match-beginning 2) (match-end 2)
6139 (list 'face (delq nil (let ((prop (get-text-property
6140 (match-beginning 2) 'face)))
6141 (or (listp prop) (setq prop (list prop)))
6142 (if (memq 'org-tag prop)
6143 prop
6144 (cons 'org-tag prop))))))
6145 (setq l (- (match-end 2) (match-beginning 2))
6146 c (if (< org-agenda-tags-column 0)
6147 (- (abs org-agenda-tags-column) l)
6148 org-agenda-tags-column))
6149 (delete-region (match-beginning 1) (match-end 1))
6150 (goto-char (match-beginning 1))
6151 (insert (org-add-props
6152 (make-string (max 1 (- c (current-column))) ?\ )
6153 (text-properties-at (point)))))
6154 (goto-char (point-min))
6155 (org-font-lock-add-tag-faces (point-max)))))
6157 (defun org-agenda-priority-up ()
6158 "Increase the priority of line at point, also in Org-mode file."
6159 (interactive)
6160 (org-agenda-priority 'up))
6162 (defun org-agenda-priority-down ()
6163 "Decrease the priority of line at point, also in Org-mode file."
6164 (interactive)
6165 (org-agenda-priority 'down))
6167 (defun org-agenda-priority (&optional force-direction)
6168 "Set the priority of line at point, also in Org-mode file.
6169 This changes the line at point, all other lines in the agenda referring to
6170 the same tree node, and the headline of the tree node in the Org-mode file."
6171 (interactive)
6172 (unless org-enable-priority-commands
6173 (error "Priority commands are disabled"))
6174 (org-agenda-check-no-diary)
6175 (let* ((marker (or (org-get-at-bol 'org-marker)
6176 (org-agenda-error)))
6177 (hdmarker (org-get-at-bol 'org-hd-marker))
6178 (buffer (marker-buffer hdmarker))
6179 (pos (marker-position hdmarker))
6180 (inhibit-read-only t)
6181 newhead)
6182 (org-with-remote-undo buffer
6183 (with-current-buffer buffer
6184 (widen)
6185 (goto-char pos)
6186 (org-show-context 'agenda)
6187 (save-excursion
6188 (and (outline-next-heading)
6189 (org-flag-heading nil))) ; show the next heading
6190 (funcall 'org-priority force-direction)
6191 (end-of-line 1)
6192 (setq newhead (org-get-heading)))
6193 (org-agenda-change-all-lines newhead hdmarker)
6194 (beginning-of-line 1))))
6196 ;; FIXME: should fix the tags property of the agenda line.
6197 (defun org-agenda-set-tags (&optional tag onoff)
6198 "Set tags for the current headline."
6199 (interactive)
6200 (org-agenda-check-no-diary)
6201 (if (and (org-region-active-p) (interactive-p))
6202 (call-interactively 'org-change-tag-in-region)
6203 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6204 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6205 (org-agenda-error)))
6206 (buffer (marker-buffer hdmarker))
6207 (pos (marker-position hdmarker))
6208 (inhibit-read-only t)
6209 newhead)
6210 (org-with-remote-undo buffer
6211 (with-current-buffer buffer
6212 (widen)
6213 (goto-char pos)
6214 (save-excursion
6215 (org-show-context 'agenda))
6216 (save-excursion
6217 (and (outline-next-heading)
6218 (org-flag-heading nil))) ; show the next heading
6219 (goto-char pos)
6220 (if tag
6221 (org-toggle-tag tag onoff)
6222 (call-interactively 'org-set-tags))
6223 (end-of-line 1)
6224 (setq newhead (org-get-heading)))
6225 (org-agenda-change-all-lines newhead hdmarker)
6226 (beginning-of-line 1)))))
6228 (defun org-agenda-set-property ()
6229 "Set a property for the current headline."
6230 (interactive)
6231 (org-agenda-check-no-diary)
6232 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6233 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6234 (org-agenda-error)))
6235 (buffer (marker-buffer hdmarker))
6236 (pos (marker-position hdmarker))
6237 (inhibit-read-only t)
6238 newhead)
6239 (org-with-remote-undo buffer
6240 (with-current-buffer buffer
6241 (widen)
6242 (goto-char pos)
6243 (save-excursion
6244 (org-show-context 'agenda))
6245 (save-excursion
6246 (and (outline-next-heading)
6247 (org-flag-heading nil))) ; show the next heading
6248 (goto-char pos)
6249 (call-interactively 'org-set-property)))))
6251 (defun org-agenda-set-effort ()
6252 "Set the effort property for the current headline."
6253 (interactive)
6254 (org-agenda-check-no-diary)
6255 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6256 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6257 (org-agenda-error)))
6258 (buffer (marker-buffer hdmarker))
6259 (pos (marker-position hdmarker))
6260 (inhibit-read-only t)
6261 newhead)
6262 (org-with-remote-undo buffer
6263 (with-current-buffer buffer
6264 (widen)
6265 (goto-char pos)
6266 (save-excursion
6267 (org-show-context 'agenda))
6268 (save-excursion
6269 (and (outline-next-heading)
6270 (org-flag-heading nil))) ; show the next heading
6271 (goto-char pos)
6272 (call-interactively 'org-set-effort)
6273 (end-of-line 1)))))
6275 (defun org-agenda-toggle-archive-tag ()
6276 "Toggle the archive tag for the current entry."
6277 (interactive)
6278 (org-agenda-check-no-diary)
6279 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6280 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6281 (org-agenda-error)))
6282 (buffer (marker-buffer hdmarker))
6283 (pos (marker-position hdmarker))
6284 (inhibit-read-only t)
6285 newhead)
6286 (org-with-remote-undo buffer
6287 (with-current-buffer buffer
6288 (widen)
6289 (goto-char pos)
6290 (org-show-context 'agenda)
6291 (save-excursion
6292 (and (outline-next-heading)
6293 (org-flag-heading nil))) ; show the next heading
6294 (call-interactively 'org-toggle-archive-tag)
6295 (end-of-line 1)
6296 (setq newhead (org-get-heading)))
6297 (org-agenda-change-all-lines newhead hdmarker)
6298 (beginning-of-line 1))))
6300 (defun org-agenda-do-date-later (arg)
6301 (interactive "P")
6302 (cond
6303 ((or (equal arg '(16))
6304 (memq last-command
6305 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
6306 (setq this-command 'org-agenda-date-later-minutes)
6307 (org-agenda-date-later-minutes 1))
6308 ((or (equal arg '(4))
6309 (memq last-command
6310 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
6311 (setq this-command 'org-agenda-date-later-hours)
6312 (org-agenda-date-later-hours 1))
6314 (org-agenda-date-later (prefix-numeric-value arg)))))
6316 (defun org-agenda-do-date-earlier (arg)
6317 (interactive "P")
6318 (cond
6319 ((or (equal arg '(16))
6320 (memq last-command
6321 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
6322 (setq this-command 'org-agenda-date-earlier-minutes)
6323 (org-agenda-date-earlier-minutes 1))
6324 ((or (equal arg '(4))
6325 (memq last-command
6326 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
6327 (setq this-command 'org-agenda-date-earlier-hours)
6328 (org-agenda-date-earlier-hours 1))
6330 (org-agenda-date-earlier (prefix-numeric-value arg)))))
6332 (defun org-agenda-date-later (arg &optional what)
6333 "Change the date of this item to one day later."
6334 (interactive "p")
6335 (org-agenda-check-type t 'agenda 'timeline)
6336 (org-agenda-check-no-diary)
6337 (let* ((marker (or (org-get-at-bol 'org-marker)
6338 (org-agenda-error)))
6339 (buffer (marker-buffer marker))
6340 (pos (marker-position marker)))
6341 (org-with-remote-undo buffer
6342 (with-current-buffer buffer
6343 (widen)
6344 (goto-char pos)
6345 (if (not (org-at-timestamp-p))
6346 (error "Cannot find time stamp"))
6347 (org-timestamp-change arg (or what 'day)))
6348 (org-agenda-show-new-time marker org-last-changed-timestamp))
6349 (message "Time stamp changed to %s" org-last-changed-timestamp)))
6351 (defun org-agenda-date-earlier (arg &optional what)
6352 "Change the date of this item to one day earlier."
6353 (interactive "p")
6354 (org-agenda-date-later (- arg) what))
6356 (defun org-agenda-date-later-minutes (arg)
6357 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
6358 (interactive "p")
6359 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
6360 (org-agenda-date-later arg 'minute))
6362 (defun org-agenda-date-earlier-minutes (arg)
6363 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
6364 (interactive "p")
6365 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
6366 (org-agenda-date-earlier arg 'minute))
6368 (defun org-agenda-date-later-hours (arg)
6369 "Change the time of this item, in hour steps."
6370 (interactive "p")
6371 (org-agenda-date-later arg 'hour))
6373 (defun org-agenda-date-earlier-hours (arg)
6374 "Change the time of this item, in hour steps."
6375 (interactive "p")
6376 (org-agenda-date-earlier arg 'hour))
6378 (defun org-agenda-show-new-time (marker stamp &optional prefix)
6379 "Show new date stamp via text properties."
6380 ;; We use text properties to make this undoable
6381 (let ((inhibit-read-only t)
6382 (buffer-invisibility-spec))
6383 (setq stamp (concat " " prefix " => " stamp))
6384 (save-excursion
6385 (goto-char (point-max))
6386 (while (not (bobp))
6387 (when (equal marker (org-get-at-bol 'org-marker))
6388 (org-move-to-column (- (window-width) (length stamp)) t)
6389 (org-agenda-fix-tags-filter-overlays-at (point))
6390 (if (featurep 'xemacs)
6391 ;; Use `duplicable' property to trigger undo recording
6392 (let ((ex (make-extent nil nil))
6393 (gl (make-glyph stamp)))
6394 (set-glyph-face gl 'secondary-selection)
6395 (set-extent-properties
6396 ex (list 'invisible t 'end-glyph gl 'duplicable t))
6397 (insert-extent ex (1- (point)) (point-at-eol)))
6398 (add-text-properties
6399 (1- (point)) (point-at-eol)
6400 (list 'display (org-add-props stamp nil
6401 'face 'secondary-selection))))
6402 (beginning-of-line 1))
6403 (beginning-of-line 0)))))
6405 (defun org-agenda-date-prompt (arg)
6406 "Change the date of this item. Date is prompted for, with default today.
6407 The prefix ARG is passed to the `org-time-stamp' command and can therefore
6408 be used to request time specification in the time stamp."
6409 (interactive "P")
6410 (org-agenda-check-type t 'agenda 'timeline)
6411 (org-agenda-check-no-diary)
6412 (let* ((marker (or (org-get-at-bol 'org-marker)
6413 (org-agenda-error)))
6414 (buffer (marker-buffer marker))
6415 (pos (marker-position marker)))
6416 (org-with-remote-undo buffer
6417 (with-current-buffer buffer
6418 (widen)
6419 (goto-char pos)
6420 (if (not (org-at-timestamp-p))
6421 (error "Cannot find time stamp"))
6422 (org-time-stamp arg))
6423 (org-agenda-show-new-time marker org-last-changed-timestamp))
6424 (message "Time stamp changed to %s" org-last-changed-timestamp)))
6426 (defun org-agenda-schedule (arg)
6427 "Schedule the item at point."
6428 (interactive "P")
6429 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
6430 (org-agenda-check-no-diary)
6431 (let* ((marker (or (org-get-at-bol 'org-marker)
6432 (org-agenda-error)))
6433 (type (marker-insertion-type marker))
6434 (buffer (marker-buffer marker))
6435 (pos (marker-position marker))
6436 (org-insert-labeled-timestamps-at-point nil)
6438 (set-marker-insertion-type marker t)
6439 (org-with-remote-undo buffer
6440 (with-current-buffer buffer
6441 (widen)
6442 (goto-char pos)
6443 (setq ts (org-schedule arg)))
6444 (org-agenda-show-new-time marker ts "S"))
6445 (message "Item scheduled for %s" ts)))
6447 (defun org-agenda-deadline (arg)
6448 "Schedule the item at point."
6449 (interactive "P")
6450 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
6451 (org-agenda-check-no-diary)
6452 (let* ((marker (or (org-get-at-bol 'org-marker)
6453 (org-agenda-error)))
6454 (buffer (marker-buffer marker))
6455 (pos (marker-position marker))
6456 (org-insert-labeled-timestamps-at-point nil)
6458 (org-with-remote-undo buffer
6459 (with-current-buffer buffer
6460 (widen)
6461 (goto-char pos)
6462 (setq ts (org-deadline arg)))
6463 (org-agenda-show-new-time marker ts "D"))
6464 (message "Deadline for this item set to %s" ts)))
6466 (defun org-agenda-action ()
6467 "Select entry for agenda action, or execute an agenda action.
6468 This command prompts for another letter. Valid inputs are:
6470 m Mark the entry at point for an agenda action
6471 s Schedule the marked entry to the date at the cursor
6472 d Set the deadline of the marked entry to the date at the cursor
6473 r Call `org-remember' with cursor date as the default date
6474 SPC Show marked entry in other window
6475 TAB Visit marked entry in other window
6477 The cursor may be at a date in the calendar, or in the Org agenda."
6478 (interactive)
6479 (let (ans)
6480 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [ ]show")
6481 (setq ans (read-char-exclusive))
6482 (cond
6483 ((equal ans ?m)
6484 ;; Mark this entry
6485 (if (eq major-mode 'org-agenda-mode)
6486 (let ((m (or (org-get-at-bol 'org-hd-marker)
6487 (org-get-at-bol 'org-marker))))
6488 (if m
6489 (progn
6490 (move-marker org-agenda-action-marker
6491 (marker-position m) (marker-buffer m))
6492 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
6493 (error "Don't know which entry to mark")))
6494 (error "This command works only in the agenda")))
6495 ((equal ans ?s)
6496 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
6497 ((equal ans ?d)
6498 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
6499 ((equal ans ?r)
6500 (org-agenda-do-action '(org-remember) t))
6501 ((equal ans ?\ )
6502 (let ((cw (selected-window)))
6503 (org-switch-to-buffer-other-window
6504 (marker-buffer org-agenda-action-marker))
6505 (goto-char org-agenda-action-marker)
6506 (org-show-context 'agenda)
6507 (select-window cw)))
6508 ((equal ans ?\C-i)
6509 (org-switch-to-buffer-other-window
6510 (marker-buffer org-agenda-action-marker))
6511 (goto-char org-agenda-action-marker)
6512 (org-show-context 'agenda))
6513 (t (error "Invalid agenda action %c" ans)))))
6515 (defun org-agenda-do-action (form &optional current-buffer)
6516 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
6517 (let ((org-overriding-default-time (org-get-cursor-date)))
6518 (if current-buffer
6519 (eval form)
6520 (if (not (marker-buffer org-agenda-action-marker))
6521 (error "No entry has been selected for agenda action")
6522 (with-current-buffer (marker-buffer org-agenda-action-marker)
6523 (save-excursion
6524 (save-restriction
6525 (widen)
6526 (goto-char org-agenda-action-marker)
6527 (eval form))))))))
6529 (defun org-agenda-clock-in (&optional arg)
6530 "Start the clock on the currently selected item."
6531 (interactive "P")
6532 (org-agenda-check-no-diary)
6533 (if (equal arg '(4))
6534 (org-clock-in arg)
6535 (let* ((marker (or (org-get-at-bol 'org-marker)
6536 (org-agenda-error)))
6537 (hdmarker (or (org-get-at-bol 'org-hd-marker)
6538 marker))
6539 (pos (marker-position marker))
6540 newhead)
6541 (org-with-remote-undo (marker-buffer marker)
6542 (with-current-buffer (marker-buffer marker)
6543 (widen)
6544 (goto-char pos)
6545 (org-show-context 'agenda)
6546 (org-show-entry)
6547 (org-cycle-hide-drawers 'children)
6548 (org-clock-in arg)
6549 (setq newhead (org-get-heading)))
6550 (org-agenda-change-all-lines newhead hdmarker)))))
6552 (defun org-agenda-clock-out (&optional arg)
6553 "Stop the currently running clock."
6554 (interactive "P")
6555 (unless (marker-buffer org-clock-marker)
6556 (error "No running clock"))
6557 (let ((marker (make-marker)) newhead)
6558 (org-with-remote-undo (marker-buffer org-clock-marker)
6559 (with-current-buffer (marker-buffer org-clock-marker)
6560 (save-excursion
6561 (save-restriction
6562 (widen)
6563 (goto-char org-clock-marker)
6564 (org-back-to-heading t)
6565 (move-marker marker (point))
6566 (org-clock-out)
6567 (setq newhead (org-get-heading))))))
6568 (org-agenda-change-all-lines newhead marker)
6569 (move-marker marker nil)))
6571 (defun org-agenda-clock-cancel (&optional arg)
6572 "Cancel the currently running clock."
6573 (interactive "P")
6574 (unless (marker-buffer org-clock-marker)
6575 (error "No running clock"))
6576 (org-with-remote-undo (marker-buffer org-clock-marker)
6577 (org-clock-cancel)))
6579 (defun org-agenda-diary-entry ()
6580 "Make a diary entry, like the `i' command from the calendar.
6581 All the standard commands work: block, weekly etc."
6582 (interactive)
6583 (org-agenda-check-type t 'agenda 'timeline)
6584 (require 'diary-lib)
6585 (let* ((char (progn
6586 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
6587 (read-char-exclusive)))
6588 (cmd (cdr (assoc char
6589 '((?d . insert-diary-entry)
6590 (?w . insert-weekly-diary-entry)
6591 (?m . insert-monthly-diary-entry)
6592 (?y . insert-yearly-diary-entry)
6593 (?a . insert-anniversary-diary-entry)
6594 (?b . insert-block-diary-entry)
6595 (?c . insert-cyclic-diary-entry)))))
6596 (oldf (symbol-function 'calendar-cursor-to-date))
6597 ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
6598 (point (point))
6599 (mark (or (mark t) (point))))
6600 (unless cmd
6601 (error "No command associated with <%c>" char))
6602 (unless (and (get-text-property point 'day)
6603 (or (not (equal ?b char))
6604 (get-text-property mark 'day)))
6605 (error "Don't know which date to use for diary entry"))
6606 ;; We implement this by hacking the `calendar-cursor-to-date' function
6607 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
6608 (let ((calendar-mark-ring
6609 (list (calendar-gregorian-from-absolute
6610 (or (get-text-property mark 'day)
6611 (get-text-property point 'day))))))
6612 (unwind-protect
6613 (progn
6614 (fset 'calendar-cursor-to-date
6615 (lambda (&optional error dummy)
6616 (calendar-gregorian-from-absolute
6617 (get-text-property point 'day))))
6618 (call-interactively cmd))
6619 (fset 'calendar-cursor-to-date oldf)))))
6621 (defun org-agenda-execute-calendar-command (cmd)
6622 "Execute a calendar command from the agenda, with the date associated to
6623 the cursor position."
6624 (org-agenda-check-type t 'agenda 'timeline)
6625 (require 'diary-lib)
6626 (unless (get-text-property (point) 'day)
6627 (error "Don't know which date to use for calendar command"))
6628 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
6629 (point (point))
6630 (date (calendar-gregorian-from-absolute
6631 (get-text-property point 'day)))
6632 ;; the following 2 vars are needed in the calendar
6633 (displayed-month (car date))
6634 (displayed-year (nth 2 date)))
6635 (unwind-protect
6636 (progn
6637 (fset 'calendar-cursor-to-date
6638 (lambda (&optional error dummy)
6639 (calendar-gregorian-from-absolute
6640 (get-text-property point 'day))))
6641 (call-interactively cmd))
6642 (fset 'calendar-cursor-to-date oldf))))
6644 (defun org-agenda-phases-of-moon ()
6645 "Display the phases of the moon for the 3 months around the cursor date."
6646 (interactive)
6647 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
6649 (defun org-agenda-holidays ()
6650 "Display the holidays for the 3 months around the cursor date."
6651 (interactive)
6652 (org-agenda-execute-calendar-command 'list-calendar-holidays))
6654 (defvar calendar-longitude)
6655 (defvar calendar-latitude)
6656 (defvar calendar-location-name)
6658 (defun org-agenda-sunrise-sunset (arg)
6659 "Display sunrise and sunset for the cursor date.
6660 Latitude and longitude can be specified with the variables
6661 `calendar-latitude' and `calendar-longitude'. When called with prefix
6662 argument, latitude and longitude will be prompted for."
6663 (interactive "P")
6664 (require 'solar)
6665 (let ((calendar-longitude (if arg nil calendar-longitude))
6666 (calendar-latitude (if arg nil calendar-latitude))
6667 (calendar-location-name
6668 (if arg "the given coordinates" calendar-location-name)))
6669 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
6671 (defun org-agenda-goto-calendar ()
6672 "Open the Emacs calendar with the date at the cursor."
6673 (interactive)
6674 (org-agenda-check-type t 'agenda 'timeline)
6675 (let* ((day (or (get-text-property (point) 'day)
6676 (error "Don't know which date to open in calendar")))
6677 (date (calendar-gregorian-from-absolute day))
6678 (calendar-move-hook nil)
6679 (calendar-view-holidays-initially-flag nil)
6680 (calendar-view-diary-initially-flag nil)
6681 (view-calendar-holidays-initially nil)
6682 (view-diary-entries-initially nil))
6683 (calendar)
6684 (calendar-goto-date date)))
6686 ;;;###autoload
6687 (defun org-calendar-goto-agenda ()
6688 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
6689 This is a command that has to be installed in `calendar-mode-map'."
6690 (interactive)
6691 (org-agenda-list nil (calendar-absolute-from-gregorian
6692 (calendar-cursor-to-date))
6693 nil))
6695 (defun org-agenda-convert-date ()
6696 (interactive)
6697 (org-agenda-check-type t 'agenda 'timeline)
6698 (let ((day (get-text-property (point) 'day))
6699 date s)
6700 (unless day
6701 (error "Don't know which date to convert"))
6702 (setq date (calendar-gregorian-from-absolute day))
6703 (setq s (concat
6704 "Gregorian: " (calendar-date-string date) "\n"
6705 "ISO: " (calendar-iso-date-string date) "\n"
6706 "Day of Yr: " (calendar-day-of-year-string date) "\n"
6707 "Julian: " (calendar-julian-date-string date) "\n"
6708 "Astron. JD: " (calendar-astro-date-string date)
6709 " (Julian date number at noon UTC)\n"
6710 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
6711 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
6712 "French: " (calendar-french-date-string date) "\n"
6713 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
6714 "Mayan: " (calendar-mayan-date-string date) "\n"
6715 "Coptic: " (calendar-coptic-date-string date) "\n"
6716 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
6717 "Persian: " (calendar-persian-date-string date) "\n"
6718 "Chinese: " (calendar-chinese-date-string date) "\n"))
6719 (with-output-to-temp-buffer "*Dates*"
6720 (princ s))
6721 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
6723 ;;; Bulk commands
6725 (defvar org-agenda-bulk-marked-entries nil
6726 "List of markers that refer to marked entries in the agenda.")
6728 (defun org-agenda-bulk-marked-p ()
6729 (eq (get-char-property (point-at-bol) 'type)
6730 'org-marked-entry-overlay))
6732 (defun org-agenda-bulk-mark ()
6733 "Mark the entry at point for future bulk action."
6734 (interactive)
6735 (org-agenda-check-no-diary)
6736 (let* ((m (org-get-at-bol 'org-hd-marker))
6738 (unless (org-agenda-bulk-marked-p)
6739 (unless m (error "Nothing to mark at point"))
6740 (push m org-agenda-bulk-marked-entries)
6741 (setq ov (org-make-overlay (point-at-bol) (+ 2 (point-at-bol))))
6742 (org-overlay-display ov "> "
6743 (org-get-todo-face "TODO")
6744 'evaporate)
6745 (org-overlay-put ov 'type 'org-marked-entry-overlay))
6746 (beginning-of-line 2)
6747 (message "%d entries marked for bulk action"
6748 (length org-agenda-bulk-marked-entries))))
6750 (defun org-agenda-bulk-unmark ()
6751 "Unmark the entry at point for future bulk action."
6752 (interactive)
6753 (when (org-agenda-bulk-marked-p)
6754 (org-agenda-bulk-remove-overlays
6755 (point-at-bol) (+ 2 (point-at-bol)))
6756 (setq org-agenda-bulk-marked-entries
6757 (delete (org-get-at-bol 'org-hd-marker)
6758 org-agenda-bulk-marked-entries)))
6759 (beginning-of-line 2)
6760 (message "%d entries marked for bulk action"
6761 (length org-agenda-bulk-marked-entries)))
6763 (defun org-agenda-bulk-toggle ()
6764 "Toggle marking the entry at point for bulk action."
6765 (interactive)
6766 (if (org-agenda-bulk-marked-p)
6767 (org-agenda-bulk-unmark)
6768 (org-agenda-bulk-mark)))
6770 (defun org-agenda-bulk-remove-overlays (&optional beg end)
6771 "Remove the mark overlays between BEG and END in the agenda buffer.
6772 BEG and END default to the buffer limits.
6774 This only removes the overlays, it does not remove the markers
6775 from the list in `org-agenda-bulk-marked-entries'."
6776 (interactive)
6777 (mapc (lambda (ov)
6778 (and (eq (org-overlay-get ov 'type) 'org-marked-entry-overlay)
6779 (org-delete-overlay ov)))
6780 (org-overlays-in (or beg (point-min)) (or end (point-max)))))
6782 (defun org-agenda-bulk-remove-all-marks ()
6783 "Remove all marks in the agenda buffer.
6784 This will remove the markers, and the overlays."
6785 (interactive)
6786 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
6787 (setq org-agenda-bulk-marked-entries nil)
6788 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
6790 (defun org-agenda-bulk-action ()
6791 "Execute an remote-editing action on all marked entries."
6792 (interactive)
6793 (unless org-agenda-bulk-marked-entries
6794 (error "No entries are marked"))
6795 (message "Bulk: [r]efile [$]archive [A]rch->sib [t]odo [+/-]tag [s]chedule [d]eadline")
6796 (let* ((action (read-char-exclusive))
6797 (entries (reverse org-agenda-bulk-marked-entries))
6798 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
6799 (cond
6800 ((equal action ?$)
6801 (setq cmd '(org-agenda-archive)))
6803 ((equal action ?A)
6804 (setq cmd '(org-agenda-archive-to-archive-sibling)))
6806 ((member action '(?r ?w))
6807 (setq rfloc (org-refile-get-location
6808 "Refile to: "
6809 (marker-buffer (car org-agenda-bulk-marked-entries))
6810 org-refile-allow-creating-parent-nodes))
6811 (setcar (nthcdr 3 rfloc)
6812 (move-marker (make-marker) (nth 3 rfloc)
6813 (or (get-file-buffer (nth 1 rfloc))
6814 (find-buffer-visiting (nth 1 rfloc))
6815 (error "This should not happen"))))
6817 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc))))
6819 ((equal action ?t)
6820 (setq state (org-icompleting-read
6821 "Todo state: "
6822 (with-current-buffer (marker-buffer (car entries))
6823 (mapcar 'list org-todo-keywords-1))))
6824 (setq cmd `(let ((org-inhibit-blocking t)
6825 (org-inhibit-logging 'note))
6826 (org-agenda-todo ,state))))
6828 ((memq action '(?- ?+))
6829 (setq tag (org-icompleting-read
6830 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
6831 (with-current-buffer (marker-buffer (car entries))
6832 (delq nil
6833 (mapcar (lambda (x)
6834 (if (stringp (car x)) x)) org-tag-alist)))))
6835 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
6837 ((memq action '(?s ?d))
6838 (let* ((date (org-read-date
6839 nil nil nil
6840 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to")))
6841 (ans org-read-date-final-answer)
6842 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
6843 (setq cmd `(let* ((bound (fboundp 'read-string))
6844 (old (and bound (symbol-function 'read-string))))
6845 (unwind-protect
6846 (progn
6847 (fset 'read-string (lambda (&rest ignore) ,ans))
6848 (call-interactively ',c1))
6849 (if bound
6850 (fset 'read-string old)
6851 (fmakunbound 'read-string)))))))
6852 (t (error "Invalid bulk action")))
6854 ;; Sort the markers, to make sure that parents are handled before children
6855 (setq entries (sort entries
6856 (lambda (a b)
6857 (cond
6858 ((equal (marker-buffer a) (marker-buffer b))
6859 (< (marker-position a) (marker-position b)))
6861 (string< (buffer-name (marker-buffer a))
6862 (buffer-name (marker-buffer b))))))))
6864 ;; Now loop over all markers and apply cmd
6865 (while (setq e (pop entries))
6866 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
6867 (if (not pos)
6868 (progn (message "Skipping removed entry at %s" e)
6869 (setq cntskip (1+ cntskip)))
6870 (goto-char pos)
6871 (eval cmd)
6872 (setq org-agenda-bulk-marked-entries
6873 (delete e org-agenda-bulk-marked-entries))
6874 (setq cnt (1+ cnt))))
6875 (setq org-agenda-bulk-marked-entries nil)
6876 (org-agenda-bulk-remove-all-marks)
6877 (message "Acted on %d entries%s"
6879 (if (= cntskip 0)
6881 (format ", skipped %d (disappeared before their turn)"
6882 cntskip)))))
6884 ;;; Flagging notes
6886 (defun org-agenda-show-the-flagging-note ()
6887 "Display the flagging note in the other window.
6888 When called a second time in direct sequence, offer to remove the FLAGGING
6889 tag and (if present) the flagging note."
6890 (interactive)
6891 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
6892 (win (selected-window))
6893 note heading newhead)
6894 (unless hdmarker
6895 (error "No linked entry at point"))
6896 (if (and (eq this-command last-command)
6897 (y-or-n-p "Unflag and remove any flagging note? "))
6898 (progn
6899 (org-agenda-remove-flag hdmarker)
6900 (let ((win (get-buffer-window "*Flagging Note*")))
6901 (and win (delete-window win)))
6902 (message "Entry unflaged"))
6903 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
6904 (unless note
6905 (error "No flagging note"))
6906 (org-kill-new note)
6907 (org-switch-to-buffer-other-window "*Flagging Note*")
6908 (erase-buffer)
6909 (insert note)
6910 (goto-char (point-min))
6911 (while (re-search-forward "\\\\n" nil t)
6912 (replace-match "\n" t t))
6913 (goto-char (point-min))
6914 (select-window win)
6915 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
6917 (defun org-agenda-remove-flag (marker)
6918 "Remove the FLAGGED tag and any flaging note in the entry."
6919 (let (newhead)
6920 (org-with-point-at marker
6921 (org-toggle-tag "FLAGGED" 'off)
6922 (org-entry-delete nil "THEFLAGGINGNOTE")
6923 (setq newhead (org-get-heading)))
6924 (org-agenda-change-all-lines newhead marker)
6925 (message "Entry unflaged")))
6927 (defun org-agenda-get-any-marker (&optional pos)
6928 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
6929 (get-text-property (or pos (point-at-bol)) 'org-marker)))
6931 ;;; Appointment reminders
6933 (defvar appt-time-msg-list)
6935 ;;;###autoload
6936 (defun org-agenda-to-appt (&optional refresh filter)
6937 "Activate appointments found in `org-agenda-files'.
6938 With a \\[universal-argument] prefix, refresh the list of
6939 appointments.
6941 If FILTER is t, interactively prompt the user for a regular
6942 expression, and filter out entries that don't match it.
6944 If FILTER is a string, use this string as a regular expression
6945 for filtering entries out.
6947 FILTER can also be an alist with the car of each cell being
6948 either 'headline or 'category. For example:
6950 '((headline \"IMPORTANT\")
6951 (category \"Work\"))
6953 will only add headlines containing IMPORTANT or headlines
6954 belonging to the \"Work\" category."
6955 (interactive "P")
6956 (require 'calendar)
6957 (if refresh (setq appt-time-msg-list nil))
6958 (if (eq filter t)
6959 (setq filter (read-from-minibuffer "Regexp filter: ")))
6960 (let* ((cnt 0) ; count added events
6961 (org-agenda-new-buffers nil)
6962 (org-deadline-warning-days 0)
6963 (today (org-date-to-gregorian
6964 (time-to-days (current-time))))
6965 (org-agenda-restrict nil)
6966 (files (org-agenda-files 'unrestricted)) entries file)
6967 ;; Get all entries which may contain an appt
6968 (org-prepare-agenda-buffers files)
6969 (while (setq file (pop files))
6970 (setq entries
6971 (append entries
6972 (org-agenda-get-day-entries
6973 file today :timestamp :scheduled :deadline))))
6974 (setq entries (delq nil entries))
6975 ;; Map thru entries and find if we should filter them out
6976 (mapc
6977 (lambda(x)
6978 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
6979 (cat (get-text-property 1 'org-category x))
6980 (tod (get-text-property 1 'time-of-day x))
6981 (ok (or (null filter)
6982 (and (stringp filter) (string-match filter evt))
6983 (and (listp filter)
6984 (or (string-match
6985 (cadr (assoc 'category filter)) cat)
6986 (string-match
6987 (cadr (assoc 'headline filter)) evt))))))
6988 ;; FIXME: Shall we remove text-properties for the appt text?
6989 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
6990 (when (and ok tod)
6991 (setq tod (concat "00" (number-to-string tod))
6992 tod (when (string-match
6993 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
6994 (concat (match-string 1 tod) ":"
6995 (match-string 2 tod))))
6996 (appt-add tod evt)
6997 (setq cnt (1+ cnt))))) entries)
6998 (org-release-buffers org-agenda-new-buffers)
6999 (if (eq cnt 0)
7000 (message "No event to add")
7001 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
7003 (defun org-agenda-todayp (date)
7004 "Does DATE mean today, when considering `org-extend-today-until'?"
7005 (let (today h)
7006 (if (listp date) (setq date (calendar-absolute-from-gregorian date)))
7007 (setq today (calendar-absolute-from-gregorian (calendar-current-date)))
7008 (setq h (nth 2 (decode-time (current-time))))
7009 (or (and (>= h org-extend-today-until)
7010 (= date today))
7011 (and (< h org-extend-today-until)
7012 (= date (1- today))))))
7014 (provide 'org-agenda)
7016 ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
7018 ;;; org-agenda.el ends here