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