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