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