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