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