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