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