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