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