Fix documentation of using <link> for style
[org-mode.git] / lisp / org-agenda.el
blobba36bd563c1483e5d831dff971f463bb60b7e7a0
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.28trans
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 (require 'org-icalendar)
2161 (let ((org-agenda-marker-table
2162 (org-create-marker-find-array
2163 (org-agenda-collect-markers)))
2164 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2165 (org-combined-agenda-icalendar-file file))
2166 (apply 'org-export-icalendar 'combine
2167 (org-agenda-files nil 'ifmode))))
2169 (let ((bs (buffer-string)))
2170 (find-file file)
2171 (erase-buffer)
2172 (insert bs)
2173 (save-buffer 0)
2174 (kill-buffer (current-buffer))
2175 (message "Plain text written to %s" file))))))))
2176 (set-buffer org-agenda-buffer-name))
2177 (when open (org-open-file file)))
2179 (defvar org-agenda-filter-overlays nil)
2181 (defun org-agenda-mark-filtered-text ()
2182 "Mark all text hidden by filtering with a text property."
2183 (let ((inhibit-read-only t))
2184 (mapc
2185 (lambda (o)
2186 (when (equal (org-overlay-buffer o) (current-buffer))
2187 (put-text-property
2188 (org-overlay-start o) (org-overlay-end o)
2189 'org-filtered t)))
2190 org-agenda-filter-overlays)))
2192 (defun org-agenda-unmark-filtered-text ()
2193 "Remove the filtering text property."
2194 (let ((inhibit-read-only t))
2195 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2197 (defun org-agenda-remove-marked-text (property &optional value)
2198 "Delete all text marked with VALUE of PROPERTY.
2199 VALUE defaults to t."
2200 (let (beg)
2201 (setq value (or value t))
2202 (while (setq beg (text-property-any (point-min) (point-max)
2203 property value))
2204 (delete-region
2205 beg (or (next-single-property-change beg 'org-filtered)
2206 (point-max))))))
2208 (defun org-agenda-add-entry-text ()
2209 "Add entry text to agenda lines.
2210 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2211 entry text following headings shown in the agenda.
2212 Drawers will be excluded, also the line with scheduling/deadline info."
2213 (when (> org-agenda-add-entry-text-maxlines 0)
2214 (let (m txt drawer-re kwd-time-re ind)
2215 (goto-char (point-min))
2216 (while (not (eobp))
2217 (if (not (setq m (get-text-property (point) 'org-hd-marker)))
2218 (beginning-of-line 2)
2219 (save-excursion
2220 (with-current-buffer (marker-buffer m)
2221 (if (not (org-mode-p))
2222 (setq txt "")
2223 (save-excursion
2224 (save-restriction
2225 (widen)
2226 (goto-char m)
2227 (beginning-of-line 2)
2228 (setq txt (buffer-substring
2229 (point)
2230 (progn (outline-next-heading) (point)))
2231 drawer-re org-drawer-regexp
2232 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2233 ".*\n?"))
2234 (with-temp-buffer
2235 (insert txt)
2236 (when org-agenda-add-entry-text-descriptive-links
2237 (goto-char (point-min))
2238 (while (org-activate-bracket-links (point-max))
2239 (add-text-properties (match-beginning 0) (match-end 0)
2240 '(face org-link))))
2241 (goto-char (point-min))
2242 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2243 (set-text-properties (match-beginning 0) (match-end 0)
2244 nil))
2245 (goto-char (point-min))
2246 (while (re-search-forward drawer-re nil t)
2247 (delete-region
2248 (match-beginning 0)
2249 (progn (re-search-forward
2250 "^[ \t]*:END:.*\n?" nil 'move)
2251 (point))))
2252 (goto-char (point-min))
2253 (while (re-search-forward kwd-time-re nil t)
2254 (replace-match ""))
2255 (if (re-search-forward "[ \t\n]+\\'" nil t)
2256 (replace-match ""))
2257 (goto-char (point-min))
2258 ;; find min indentation
2259 (goto-char (point-min))
2260 (untabify (point-min) (point-max))
2261 (setq ind (org-get-indentation))
2262 (while (not (eobp))
2263 (unless (looking-at "[ \t]*$")
2264 (setq ind (min ind (org-get-indentation))))
2265 (beginning-of-line 2))
2266 (goto-char (point-min))
2267 (while (not (eobp))
2268 (unless (looking-at "[ \t]*$")
2269 (move-to-column ind)
2270 (delete-region (point-at-bol) (point)))
2271 (beginning-of-line 2))
2272 (goto-char (point-min))
2273 (while (and (not (eobp)) (re-search-forward "^" nil t))
2274 (replace-match " > "))
2275 (goto-char (point-min))
2276 (while (looking-at "[ \t]*\n") (replace-match ""))
2277 (goto-char (point-max))
2278 (when (> (org-current-line)
2279 (1+ org-agenda-add-entry-text-maxlines))
2280 (goto-line (1+ org-agenda-add-entry-text-maxlines))
2281 (backward-char 1))
2282 (setq txt (buffer-substring (point-min) (point)))))))))
2283 (end-of-line 1)
2284 (if (string-match "\\S-" txt) (insert "\n" txt)))))))
2286 (defun org-agenda-collect-markers ()
2287 "Collect the markers pointing to entries in the agenda buffer."
2288 (let (m markers)
2289 (save-excursion
2290 (goto-char (point-min))
2291 (while (not (eobp))
2292 (when (setq m (or (get-text-property (point) 'org-hd-marker)
2293 (get-text-property (point) 'org-marker)))
2294 (push m markers))
2295 (beginning-of-line 2)))
2296 (nreverse markers)))
2298 (defun org-create-marker-find-array (marker-list)
2299 "Create a alist of files names with all marker positions in that file."
2300 (let (f tbl m a p)
2301 (while (setq m (pop marker-list))
2302 (setq p (marker-position m)
2303 f (buffer-file-name (or (buffer-base-buffer
2304 (marker-buffer m))
2305 (marker-buffer m))))
2306 (if (setq a (assoc f tbl))
2307 (push (marker-position m) (cdr a))
2308 (push (list f p) tbl)))
2309 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2310 tbl)))
2312 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
2313 (defun org-check-agenda-marker-table ()
2314 "Check of the current entry is on the marker list."
2315 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2317 (and (setq a (assoc file org-agenda-marker-table))
2318 (save-match-data
2319 (save-excursion
2320 (org-back-to-heading t)
2321 (member (point) (cdr a)))))))
2323 (defun org-check-for-org-mode ()
2324 "Make sure current buffer is in org-mode. Error if not."
2325 (or (org-mode-p)
2326 (error "Cannot execute org-mode agenda command on buffer in %s."
2327 major-mode)))
2329 (defun org-fit-agenda-window ()
2330 "Fit the window to the buffer size."
2331 (and (memq org-agenda-window-setup '(reorganize-frame))
2332 (fboundp 'fit-window-to-buffer)
2333 (org-fit-window-to-buffer
2335 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
2336 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
2338 ;;; Agenda prepare and finalize
2340 (defvar org-agenda-multi nil) ; dynamically scoped
2341 (defvar org-agenda-buffer-name "*Org Agenda*")
2342 (defvar org-pre-agenda-window-conf nil)
2343 (defvar org-agenda-columns-active nil)
2344 (defvar org-agenda-name nil)
2345 (defvar org-agenda-filter nil)
2346 (defvar org-agenda-filter-preset nil
2347 "A preset of the tags filter used for secondary agenda filtering.
2348 This must be a list of strings, each string must be a single tag preceeded
2349 by \"+\" or \"-\".
2350 This variable should not be set directly, but agenda custom commands can
2351 bind it in the options section.")
2353 (defun org-prepare-agenda (&optional name)
2354 (setq org-todo-keywords-for-agenda nil)
2355 (setq org-done-keywords-for-agenda nil)
2356 (setq org-agenda-filter nil)
2357 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
2358 (if org-agenda-multi
2359 (progn
2360 (setq buffer-read-only nil)
2361 (goto-char (point-max))
2362 (unless (or (bobp) org-agenda-compact-blocks)
2363 (insert "\n"
2364 (if (stringp org-agenda-block-separator)
2365 org-agenda-block-separator
2366 (make-string (window-width) org-agenda-block-separator))
2367 "\n"))
2368 (narrow-to-region (point) (point-max)))
2369 (org-agenda-reset-markers)
2370 (setq org-agenda-contributing-files nil)
2371 (setq org-agenda-columns-active nil)
2372 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
2373 (setq org-todo-keywords-for-agenda
2374 (org-uniquify org-todo-keywords-for-agenda))
2375 (setq org-done-keywords-for-agenda
2376 (org-uniquify org-done-keywords-for-agenda))
2377 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
2378 (awin (get-buffer-window abuf)))
2379 (cond
2380 ((equal (current-buffer) abuf) nil)
2381 (awin (select-window awin))
2382 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
2383 ((equal org-agenda-window-setup 'current-window)
2384 (switch-to-buffer abuf))
2385 ((equal org-agenda-window-setup 'other-window)
2386 (org-switch-to-buffer-other-window abuf))
2387 ((equal org-agenda-window-setup 'other-frame)
2388 (switch-to-buffer-other-frame abuf))
2389 ((equal org-agenda-window-setup 'reorganize-frame)
2390 (delete-other-windows)
2391 (org-switch-to-buffer-other-window abuf))))
2392 (setq buffer-read-only nil)
2393 (let ((inhibit-read-only t)) (erase-buffer))
2394 (org-agenda-mode)
2395 (and name (not org-agenda-name)
2396 (org-set-local 'org-agenda-name name)))
2397 (setq buffer-read-only nil))
2399 (defun org-finalize-agenda ()
2400 "Finishing touch for the agenda buffer, called just before displaying it."
2401 (unless org-agenda-multi
2402 (save-excursion
2403 (let ((inhibit-read-only t))
2404 (goto-char (point-min))
2405 (while (org-activate-bracket-links (point-max))
2406 (add-text-properties (match-beginning 0) (match-end 0)
2407 '(face org-link)))
2408 (org-agenda-align-tags)
2409 (unless org-agenda-with-colors
2410 (remove-text-properties (point-min) (point-max) '(face nil))))
2411 (if (and (boundp 'org-agenda-overriding-columns-format)
2412 org-agenda-overriding-columns-format)
2413 (org-set-local 'org-agenda-overriding-columns-format
2414 org-agenda-overriding-columns-format))
2415 (if (and (boundp 'org-agenda-view-columns-initially)
2416 org-agenda-view-columns-initially)
2417 (org-agenda-columns))
2418 (when org-agenda-fontify-priorities
2419 (org-agenda-fontify-priorities))
2420 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
2421 (org-agenda-dim-blocked-tasks))
2422 (run-hooks 'org-finalize-agenda-hook)
2423 (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
2424 (when (get 'org-agenda-filter :preset-filter)
2425 (org-agenda-filter-apply org-agenda-filter))
2428 (defun org-agenda-fontify-priorities ()
2429 "Make highest priority lines bold, and lowest italic."
2430 (interactive)
2431 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
2432 (org-delete-overlay o)))
2433 (org-overlays-in (point-min) (point-max)))
2434 (save-excursion
2435 (let ((inhibit-read-only t)
2436 b e p ov h l)
2437 (goto-char (point-min))
2438 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
2439 (setq h (or (get-char-property (point) 'org-highest-priority)
2440 org-highest-priority)
2441 l (or (get-char-property (point) 'org-lowest-priority)
2442 org-lowest-priority)
2443 p (string-to-char (match-string 1))
2444 b (match-beginning 0)
2445 e (if (eq org-agenda-fontify-priorities 'cookies)
2446 (match-end 0)
2447 (point-at-eol))
2448 ov (org-make-overlay b e))
2449 (org-overlay-put
2450 ov 'face
2451 (cond ((cdr (assoc p org-priority-faces)))
2452 ((and (listp org-agenda-fontify-priorities)
2453 (cdr (assoc p org-agenda-fontify-priorities))))
2454 ((equal p l) 'italic)
2455 ((equal p h) 'bold)))
2456 (org-overlay-put ov 'org-type 'org-priority)))))
2458 (defun org-agenda-dim-blocked-tasks ()
2459 "Dim currently blocked TODO's in the agenda display."
2460 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-blocked-todo)
2461 (org-delete-overlay o)))
2462 (org-overlays-in (point-min) (point-max)))
2463 (save-excursion
2464 (let ((inhibit-read-only t)
2465 (org-depend-tag-blocked nil)
2466 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
2467 org-blocked-by-checkboxes
2468 invis1 b e p ov h l)
2469 (goto-char (point-min))
2470 (while (let ((pos (next-single-property-change (point) 'todo-state)))
2471 (and pos (goto-char (1+ pos))))
2472 (setq org-blocked-by-checkboxes nil invis1 invis)
2473 (let ((marker (get-text-property (point) 'org-hd-marker)))
2474 (when (and marker
2475 (not (with-current-buffer (marker-buffer marker)
2476 (save-excursion
2477 (goto-char marker)
2478 (if (org-entry-get nil "NOBLOCKING")
2479 t ;; Never block this entry
2480 (run-hook-with-args-until-failure
2481 'org-blocker-hook
2482 (list :type 'todo-state-change
2483 :position marker
2484 :from 'todo
2485 :to 'done)))))))
2486 (if org-blocked-by-checkboxes (setq invis1 nil))
2487 (setq b (if invis1 (max (point-min) (1- (point))) (point))
2488 e (point-at-eol)
2489 ov (org-make-overlay b e))
2490 (if invis1
2491 (org-overlay-put ov 'invisible t)
2492 (org-overlay-put ov 'face 'org-agenda-dimmed-todo-face))
2493 (org-overlay-put ov 'org-type 'org-blocked-todo)))))))
2495 (defvar org-agenda-skip-function nil
2496 "Function to be called at each match during agenda construction.
2497 If this function returns nil, the current match should not be skipped.
2498 Otherwise, the function must return a position from where the search
2499 should be continued.
2500 This may also be a Lisp form, it will be evaluated.
2501 Never set this variable using `setq' or so, because then it will apply
2502 to all future agenda commands. Instead, bind it with `let' to scope
2503 it dynamically into the agenda-constructing command. A good way to set
2504 it is through options in org-agenda-custom-commands.")
2506 (defun org-agenda-skip ()
2507 "Throw to `:skip' in places that should be skipped.
2508 Also moves point to the end of the skipped region, so that search can
2509 continue from there."
2510 (let ((p (point-at-bol)) to fp)
2511 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
2512 (get-text-property p :org-archived)
2513 (org-end-of-subtree t)
2514 (throw :skip t))
2515 (and org-agenda-skip-comment-trees
2516 (get-text-property p :org-comment)
2517 (org-end-of-subtree t)
2518 (throw :skip t))
2519 (if (equal (char-after p) ?#) (throw :skip t))
2520 (when (and (or (setq fp (functionp org-agenda-skip-function))
2521 (consp org-agenda-skip-function))
2522 (setq to (save-excursion
2523 (save-match-data
2524 (if fp
2525 (funcall org-agenda-skip-function)
2526 (eval org-agenda-skip-function))))))
2527 (goto-char to)
2528 (throw :skip t))))
2530 (defvar org-agenda-markers nil
2531 "List of all currently active markers created by `org-agenda'.")
2532 (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
2533 "Creation time of the last agenda marker.")
2535 (defun org-agenda-new-marker (&optional pos)
2536 "Return a new agenda marker.
2537 Org-mode keeps a list of these markers and resets them when they are
2538 no longer in use."
2539 (let ((m (copy-marker (or pos (point)))))
2540 (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
2541 (push m org-agenda-markers)
2544 (defun org-agenda-reset-markers ()
2545 "Reset markers created by `org-agenda'."
2546 (while org-agenda-markers
2547 (move-marker (pop org-agenda-markers) nil)))
2549 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
2550 "Save relative positions of markers in region."
2551 (mapc (lambda (m) (org-check-and-save-marker m beg end))
2552 org-agenda-markers))
2554 ;;; Agenda timeline
2556 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
2558 (defun org-timeline (&optional include-all)
2559 "Show a time-sorted view of the entries in the current org file.
2560 Only entries with a time stamp of today or later will be listed. With
2561 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
2562 under the current date.
2563 If the buffer contains an active region, only check the region for
2564 dates."
2565 (interactive "P")
2566 (require 'calendar)
2567 (org-compile-prefix-format 'timeline)
2568 (org-set-sorting-strategy 'timeline)
2569 (let* ((dopast t)
2570 (dotodo include-all)
2571 (doclosed org-agenda-show-log)
2572 (entry buffer-file-name)
2573 (date (calendar-current-date))
2574 (beg (if (org-region-active-p) (region-beginning) (point-min)))
2575 (end (if (org-region-active-p) (region-end) (point-max)))
2576 (day-numbers (org-get-all-dates beg end 'no-ranges
2577 t doclosed ; always include today
2578 org-timeline-show-empty-dates))
2579 (org-deadline-warning-days 0)
2580 (org-agenda-only-exact-dates t)
2581 (today (time-to-days (current-time)))
2582 (past t)
2583 args
2584 s e rtn d emptyp wd)
2585 (setq org-agenda-redo-command
2586 (list 'progn
2587 (list 'org-switch-to-buffer-other-window (current-buffer))
2588 (list 'org-timeline (list 'quote include-all))))
2589 (if (not dopast)
2590 ;; Remove past dates from the list of dates.
2591 (setq day-numbers (delq nil (mapcar (lambda(x)
2592 (if (>= x today) x nil))
2593 day-numbers))))
2594 (org-prepare-agenda (concat "Timeline "
2595 (file-name-nondirectory buffer-file-name)))
2596 (if doclosed (push :closed args))
2597 (push :timestamp args)
2598 (push :deadline args)
2599 (push :scheduled args)
2600 (push :sexp args)
2601 (if dotodo (push :todo args))
2602 (while (setq d (pop day-numbers))
2603 (if (and (listp d) (eq (car d) :omitted))
2604 (progn
2605 (setq s (point))
2606 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
2607 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
2608 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
2609 (if (and (>= d today)
2610 dopast
2611 past)
2612 (progn
2613 (setq past nil)
2614 (insert (make-string 79 ?-) "\n")))
2615 (setq date (calendar-gregorian-from-absolute d)
2616 wd (calendar-day-of-week date))
2617 (setq s (point))
2618 (setq rtn (and (not emptyp)
2619 (apply 'org-agenda-get-day-entries entry
2620 date args)))
2621 (if (or rtn (equal d today) org-timeline-show-empty-dates)
2622 (progn
2623 (insert
2624 (if (stringp org-agenda-format-date)
2625 (format-time-string org-agenda-format-date
2626 (org-time-from-absolute date))
2627 (funcall org-agenda-format-date date))
2628 "\n")
2629 (put-text-property s (1- (point)) 'face
2630 (if (member wd org-agenda-weekend-days)
2631 'org-agenda-date-weekend
2632 'org-agenda-date))
2633 (put-text-property s (1- (point)) 'org-date-line t)
2634 (if (equal d today)
2635 (put-text-property s (1- (point)) 'org-today t))
2636 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
2637 (put-text-property s (1- (point)) 'day d)))))
2638 (goto-char (point-min))
2639 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
2640 (point-min)))
2641 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
2642 (org-finalize-agenda)
2643 (setq buffer-read-only t)))
2645 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
2646 "Return a list of all relevant day numbers from BEG to END buffer positions.
2647 If NO-RANGES is non-nil, include only the start and end dates of a range,
2648 not every single day in the range. If FORCE-TODAY is non-nil, make
2649 sure that TODAY is included in the list. If INACTIVE is non-nil, also
2650 inactive time stamps (those in square brackets) are included.
2651 When EMPTY is non-nil, also include days without any entries."
2652 (let ((re (concat
2653 (if pre-re pre-re "")
2654 (if inactive org-ts-regexp-both org-ts-regexp)))
2655 dates dates1 date day day1 day2 ts1 ts2)
2656 (if force-today
2657 (setq dates (list (time-to-days (current-time)))))
2658 (save-excursion
2659 (goto-char beg)
2660 (while (re-search-forward re end t)
2661 (setq day (time-to-days (org-time-string-to-time
2662 (substring (match-string 1) 0 10))))
2663 (or (memq day dates) (push day dates)))
2664 (unless no-ranges
2665 (goto-char beg)
2666 (while (re-search-forward org-tr-regexp end t)
2667 (setq ts1 (substring (match-string 1) 0 10)
2668 ts2 (substring (match-string 2) 0 10)
2669 day1 (time-to-days (org-time-string-to-time ts1))
2670 day2 (time-to-days (org-time-string-to-time ts2)))
2671 (while (< (setq day1 (1+ day1)) day2)
2672 (or (memq day1 dates) (push day1 dates)))))
2673 (setq dates (sort dates '<))
2674 (when empty
2675 (while (setq day (pop dates))
2676 (setq day2 (car dates))
2677 (push day dates1)
2678 (when (and day2 empty)
2679 (if (or (eq empty t)
2680 (and (numberp empty) (<= (- day2 day) empty)))
2681 (while (< (setq day (1+ day)) day2)
2682 (push (list day) dates1))
2683 (push (cons :omitted (- day2 day)) dates1))))
2684 (setq dates (nreverse dates1)))
2685 dates)))
2687 ;;; Agenda Daily/Weekly
2689 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2690 (defvar org-agenda-start-day nil ; dynamically scoped parameter
2691 "Custom commands can set this variable in the options section.")
2692 (defvar org-agenda-last-arguments nil
2693 "The arguments of the previous call to org-agenda")
2694 (defvar org-starting-day nil) ; local variable in the agenda buffer
2695 (defvar org-agenda-span nil) ; local variable in the agenda buffer
2696 (defvar org-include-all-loc nil) ; local variable
2697 (defvar org-agenda-remove-date nil) ; dynamically scoped FIXME: not used???
2699 ;;;###autoload
2700 (defun org-agenda-list (&optional include-all start-day ndays)
2701 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
2702 The view will be for the current day or week, but from the overview buffer
2703 you will be able to go to other days/weeks.
2705 With one \\[universal-argument] prefix argument INCLUDE-ALL,
2706 all unfinished TODO items will also be shown, before the agenda.
2707 This feature is considered obsolete, please use the TODO list or a block
2708 agenda instead.
2710 With a numeric prefix argument in an interactive call, the agenda will
2711 span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
2712 the number of days. NDAYS defaults to `org-agenda-ndays'.
2714 START-DAY defaults to TODAY, or to the most recent match for the weekday
2715 given in `org-agenda-start-on-weekday'."
2716 (interactive "P")
2717 (if (and (integerp include-all) (> include-all 0))
2718 (setq ndays include-all include-all nil))
2719 (setq ndays (or ndays org-agenda-ndays)
2720 start-day (or start-day org-agenda-start-day))
2721 (if org-agenda-overriding-arguments
2722 (setq include-all (car org-agenda-overriding-arguments)
2723 start-day (nth 1 org-agenda-overriding-arguments)
2724 ndays (nth 2 org-agenda-overriding-arguments)))
2725 (if (stringp start-day)
2726 ;; Convert to an absolute day number
2727 (setq start-day (time-to-days (org-read-date nil t start-day))))
2728 (setq org-agenda-last-arguments (list include-all start-day ndays))
2729 (org-compile-prefix-format 'agenda)
2730 (org-set-sorting-strategy 'agenda)
2731 (require 'calendar)
2732 (let* ((org-agenda-start-on-weekday
2733 (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
2734 org-agenda-start-on-weekday nil))
2735 (thefiles (org-agenda-files nil 'ifmode))
2736 (files thefiles)
2737 (today (time-to-days
2738 (time-subtract (current-time)
2739 (list 0 (* 3600 org-extend-today-until) 0))))
2740 (sd (or start-day today))
2741 (start (if (or (null org-agenda-start-on-weekday)
2742 (< org-agenda-ndays 7))
2744 (let* ((nt (calendar-day-of-week
2745 (calendar-gregorian-from-absolute sd)))
2746 (n1 org-agenda-start-on-weekday)
2747 (d (- nt n1)))
2748 (- sd (+ (if (< d 0) 7 0) d)))))
2749 (day-numbers (list start))
2750 (day-cnt 0)
2751 (inhibit-redisplay (not debug-on-error))
2752 s e rtn rtnall file date d start-pos end-pos todayp nd wd
2753 clocktable-start clocktable-end)
2754 (setq org-agenda-redo-command
2755 (list 'org-agenda-list (list 'quote include-all) start-day ndays))
2756 ;; Make the list of days
2757 (setq ndays (or ndays org-agenda-ndays)
2758 nd ndays)
2759 (while (> ndays 1)
2760 (push (1+ (car day-numbers)) day-numbers)
2761 (setq ndays (1- ndays)))
2762 (setq day-numbers (nreverse day-numbers))
2763 (setq clocktable-start (car day-numbers)
2764 clocktable-end (1+ (or (org-last day-numbers) 0)))
2765 (org-prepare-agenda "Day/Week")
2766 (org-set-local 'org-starting-day (car day-numbers))
2767 (org-set-local 'org-include-all-loc include-all)
2768 (org-set-local 'org-agenda-span
2769 (org-agenda-ndays-to-span nd))
2770 (when (and (or include-all org-agenda-include-all-todo)
2771 (member today day-numbers))
2772 (setq files thefiles
2773 rtnall nil)
2774 (while (setq file (pop files))
2775 (catch 'nextfile
2776 (org-check-agenda-file file)
2777 (setq date (calendar-gregorian-from-absolute today)
2778 rtn (org-agenda-get-day-entries
2779 file date :todo))
2780 (setq rtnall (append rtnall rtn))))
2781 (when rtnall
2782 (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
2783 (add-text-properties (point-min) (1- (point))
2784 (list 'face 'org-agenda-structure))
2785 (insert (org-finalize-agenda-entries rtnall) "\n")))
2786 (unless org-agenda-compact-blocks
2787 (let* ((d1 (car day-numbers))
2788 (d2 (org-last day-numbers))
2789 (w1 (org-days-to-iso-week d1))
2790 (w2 (org-days-to-iso-week d2)))
2791 (setq s (point))
2792 (if org-agenda-overriding-header
2793 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
2794 nil 'face 'org-agenda-structure) "\n")
2795 (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
2796 "-agenda"
2797 (if (< (- d2 d1) 350)
2798 (if (= w1 w2)
2799 (format " (W%02d)" w1)
2800 (format " (W%02d-W%02d)" w1 w2))
2802 ":\n")))
2803 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
2804 'org-date-line t)))
2805 (while (setq d (pop day-numbers))
2806 (setq date (calendar-gregorian-from-absolute d)
2807 wd (calendar-day-of-week date)
2808 s (point))
2809 (if (or (setq todayp (= d today))
2810 (and (not start-pos) (= d sd)))
2811 (setq start-pos (point))
2812 (if (and start-pos (not end-pos))
2813 (setq end-pos (point))))
2814 (setq files thefiles
2815 rtnall nil)
2816 (while (setq file (pop files))
2817 (catch 'nextfile
2818 (org-check-agenda-file file)
2819 (cond
2820 ((eq org-agenda-show-log 'only)
2821 (setq rtn (org-agenda-get-day-entries
2822 file date :closed)))
2823 (org-agenda-show-log
2824 (setq rtn (org-agenda-get-day-entries
2825 file date
2826 :deadline :scheduled :timestamp :sexp :closed)))
2828 (setq rtn (org-agenda-get-day-entries
2829 file date
2830 :deadline :scheduled :sexp :timestamp))))
2831 (setq rtnall (append rtnall rtn))))
2832 (if org-agenda-include-diary
2833 (let ((org-agenda-search-headline-for-time t))
2834 (require 'diary-lib)
2835 (setq rtn (org-get-entries-from-diary date))
2836 (setq rtnall (append rtnall rtn))))
2837 (if (or rtnall org-agenda-show-all-dates)
2838 (progn
2839 (setq day-cnt (1+ day-cnt))
2840 (insert
2841 (if (stringp org-agenda-format-date)
2842 (format-time-string org-agenda-format-date
2843 (org-time-from-absolute date))
2844 (funcall org-agenda-format-date date))
2845 "\n")
2846 (put-text-property s (1- (point)) 'face
2847 (if (member wd org-agenda-weekend-days)
2848 'org-agenda-date-weekend
2849 'org-agenda-date))
2850 (put-text-property s (1- (point)) 'org-date-line t)
2851 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
2852 (when todayp
2853 (put-text-property s (1- (point)) 'org-today t)
2854 (put-text-property s (1- (point)) 'face 'org-agenda-date-today))
2855 (if rtnall (insert
2856 (org-finalize-agenda-entries
2857 (org-agenda-add-time-grid-maybe
2858 rtnall nd todayp))
2859 "\n"))
2860 (put-text-property s (1- (point)) 'day d)
2861 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
2862 (when (and org-agenda-clockreport-mode clocktable-start)
2863 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
2864 ;; the above line is to ensure the restricted range!
2865 (p org-agenda-clockreport-parameter-plist)
2866 tbl)
2867 (setq p (org-plist-delete p :block))
2868 (setq p (plist-put p :tstart clocktable-start))
2869 (setq p (plist-put p :tend clocktable-end))
2870 (setq p (plist-put p :scope 'agenda))
2871 (setq tbl (apply 'org-get-clocktable p))
2872 (insert tbl)))
2873 (goto-char (point-min))
2874 (or org-agenda-multi (org-fit-agenda-window))
2875 (unless (and (pos-visible-in-window-p (point-min))
2876 (pos-visible-in-window-p (point-max)))
2877 (goto-char (1- (point-max)))
2878 (recenter -1)
2879 (if (not (pos-visible-in-window-p (or start-pos 1)))
2880 (progn
2881 (goto-char (or start-pos 1))
2882 (recenter 1))))
2883 (goto-char (or start-pos 1))
2884 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
2885 (org-finalize-agenda)
2886 (setq buffer-read-only t)
2887 (message "")))
2889 (defun org-agenda-ndays-to-span (n)
2890 (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
2892 ;;; Agenda word search
2894 (defvar org-agenda-search-history nil)
2895 (defvar org-todo-only nil)
2897 (defvar org-search-syntax-table nil
2898 "Special syntax table for org-mode search.
2899 In this table, we have single quotes not as word constituents, to
2900 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
2902 (defun org-search-syntax-table ()
2903 (unless org-search-syntax-table
2904 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
2905 (modify-syntax-entry ?' "." org-search-syntax-table)
2906 (modify-syntax-entry ?` "." org-search-syntax-table))
2907 org-search-syntax-table)
2909 ;;;###autoload
2910 (defun org-search-view (&optional todo-only string edit-at)
2911 "Show all entries that contain words or regular expressions.
2912 If the first character of the search string is an asterisks,
2913 search only the headlines.
2915 With optional prefix argument TODO-ONLY, only consider entries that are
2916 TODO entries. The argument STRING can be used to pass a default search
2917 string into this function. If EDIT-AT is non-nil, it means that the
2918 user should get a chance to edit this string, with cursor at position
2919 EDIT-AT.
2921 The search string is broken into \"words\" by splitting at whitespace.
2922 The individual words are then interpreted as a boolean expression with
2923 logical AND. Words prefixed with a minus must not occur in the entry.
2924 Words without a prefix or prefixed with a plus must occur in the entry.
2925 Matching is case-insensitive and the words are enclosed by word delimiters.
2927 Words enclosed by curly braces are interpreted as regular expressions
2928 that must or must not match in the entry.
2930 If the search string starts with an asterisk, search only in headlines.
2931 If (possibly after the leading star) the search string starts with an
2932 exclamation mark, this also means to look at TODO entries only, an effect
2933 that can also be achieved with a prefix argument.
2935 This command searches the agenda files, and in addition the files listed
2936 in `org-agenda-text-search-extra-files'."
2937 (interactive "P")
2938 (org-compile-prefix-format 'search)
2939 (org-set-sorting-strategy 'search)
2940 (org-prepare-agenda "SEARCH")
2941 (let* ((props (list 'face nil
2942 'done-face 'org-agenda-done
2943 'org-not-done-regexp org-not-done-regexp
2944 'org-todo-regexp org-todo-regexp
2945 'org-complex-heading-regexp org-complex-heading-regexp
2946 'mouse-face 'highlight
2947 'keymap org-agenda-keymap
2948 'help-echo (format "mouse-2 or RET jump to location")))
2949 regexp rtn rtnall files file pos
2950 marker category tags c neg re
2951 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
2952 (unless (and (not edit-at)
2953 (stringp string)
2954 (string-match "\\S-" string))
2955 (setq string (read-string "[+-]Word/{Regexp} ...: "
2956 (cond
2957 ((integerp edit-at) (cons string edit-at))
2958 (edit-at string))
2959 'org-agenda-search-history)))
2960 (org-set-local 'org-todo-only todo-only)
2961 (setq org-agenda-redo-command
2962 (list 'org-search-view (if todo-only t nil) string
2963 '(if current-prefix-arg 1 nil)))
2964 (setq org-agenda-query-string string)
2966 (if (equal (string-to-char string) ?*)
2967 (setq hdl-only t
2968 words (substring string 1))
2969 (setq words string))
2970 (when (equal (string-to-char words) ?!)
2971 (setq todo-only t
2972 words (substring words 1)))
2973 (setq words (org-split-string words))
2974 (mapc (lambda (w)
2975 (setq c (string-to-char w))
2976 (if (equal c ?-)
2977 (setq neg t w (substring w 1))
2978 (if (equal c ?+)
2979 (setq neg nil w (substring w 1))
2980 (setq neg nil)))
2981 (if (string-match "\\`{.*}\\'" w)
2982 (setq re (substring w 1 -1))
2983 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
2984 (if neg (push re regexps-) (push re regexps+)))
2985 words)
2986 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
2987 (if (not regexps+)
2988 (setq regexp (concat "^" org-outline-regexp))
2989 (setq regexp (pop regexps+))
2990 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
2991 regexp))))
2992 (setq files (org-agenda-files nil 'ifmode))
2993 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
2994 (pop org-agenda-text-search-extra-files)
2995 (setq files (org-add-archive-files files)))
2996 (setq files (append files org-agenda-text-search-extra-files)
2997 rtnall nil)
2998 (while (setq file (pop files))
2999 (setq ee nil)
3000 (catch 'nextfile
3001 (org-check-agenda-file file)
3002 (setq buffer (if (file-exists-p file)
3003 (org-get-agenda-file-buffer file)
3004 (error "No such file %s" file)))
3005 (if (not buffer)
3006 ;; If file does not exist, make sure an error message is sent
3007 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3008 file))))
3009 (with-current-buffer buffer
3010 (with-syntax-table (org-search-syntax-table)
3011 (unless (org-mode-p)
3012 (error "Agenda file %s is not in `org-mode'" file))
3013 (let ((case-fold-search t))
3014 (save-excursion
3015 (save-restriction
3016 (if org-agenda-restrict
3017 (narrow-to-region org-agenda-restrict-begin
3018 org-agenda-restrict-end)
3019 (widen))
3020 (goto-char (point-min))
3021 (unless (or (org-on-heading-p)
3022 (outline-next-heading))
3023 (throw 'nextfile t))
3024 (goto-char (max (point-min) (1- (point))))
3025 (while (re-search-forward regexp nil t)
3026 (org-back-to-heading t)
3027 (skip-chars-forward "* ")
3028 (setq beg (point-at-bol)
3029 beg1 (point)
3030 end (progn (outline-next-heading) (point)))
3031 (catch :skip
3032 (goto-char beg)
3033 (org-agenda-skip)
3034 (setq str (buffer-substring-no-properties
3035 (point-at-bol)
3036 (if hdl-only (point-at-eol) end)))
3037 (mapc (lambda (wr) (when (string-match wr str)
3038 (goto-char (1- end))
3039 (throw :skip t)))
3040 regexps-)
3041 (mapc (lambda (wr) (unless (string-match wr str)
3042 (goto-char (1- end))
3043 (throw :skip t)))
3044 (if todo-only
3045 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3046 regexps+)
3047 regexps+))
3048 (goto-char beg)
3049 (setq marker (org-agenda-new-marker (point))
3050 category (org-get-category)
3051 tags (org-get-tags-at (point))
3052 txt (org-format-agenda-item
3054 (buffer-substring-no-properties
3055 beg1 (point-at-eol))
3056 category tags))
3057 (org-add-props txt props
3058 'org-marker marker 'org-hd-marker marker
3059 'org-todo-regexp org-todo-regexp
3060 'org-complex-heading-regexp org-complex-heading-regexp
3061 'priority 1000 'org-category category
3062 'type "search")
3063 (push txt ee)
3064 (goto-char (1- end))))))))))
3065 (setq rtn (nreverse ee))
3066 (setq rtnall (append rtnall rtn)))
3067 (if org-agenda-overriding-header
3068 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3069 nil 'face 'org-agenda-structure) "\n")
3070 (insert "Search words: ")
3071 (add-text-properties (point-min) (1- (point))
3072 (list 'face 'org-agenda-structure))
3073 (setq pos (point))
3074 (insert string "\n")
3075 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3076 (setq pos (point))
3077 (unless org-agenda-multi
3078 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3079 (add-text-properties pos (1- (point))
3080 (list 'face 'org-agenda-structure))))
3081 (when rtnall
3082 (insert (org-finalize-agenda-entries rtnall) "\n"))
3083 (goto-char (point-min))
3084 (or org-agenda-multi (org-fit-agenda-window))
3085 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3086 (org-finalize-agenda)
3087 (setq buffer-read-only t)))
3089 ;;; Agenda TODO list
3091 (defvar org-select-this-todo-keyword nil)
3092 (defvar org-last-arg nil)
3094 ;;;###autoload
3095 (defun org-todo-list (arg)
3096 "Show all TODO entries from all agenda file in a single list.
3097 The prefix arg can be used to select a specific TODO keyword and limit
3098 the list to these. When using \\[universal-argument], you will be prompted
3099 for a keyword. A numeric prefix directly selects the Nth keyword in
3100 `org-todo-keywords-1'."
3101 (interactive "P")
3102 (require 'calendar)
3103 (org-compile-prefix-format 'todo)
3104 (org-set-sorting-strategy 'todo)
3105 (org-prepare-agenda "TODO")
3106 (let* ((today (time-to-days (current-time)))
3107 (date (calendar-gregorian-from-absolute today))
3108 (kwds org-todo-keywords-for-agenda)
3109 (completion-ignore-case t)
3110 (org-select-this-todo-keyword
3111 (if (stringp arg) arg
3112 (and arg (integerp arg) (> arg 0)
3113 (nth (1- arg) kwds))))
3114 rtn rtnall files file pos)
3115 (when (equal arg '(4))
3116 (setq org-select-this-todo-keyword
3117 (org-ido-completing-read "Keyword (or KWD1|K2D2|...): "
3118 (mapcar 'list kwds) nil nil)))
3119 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
3120 (org-set-local 'org-last-arg arg)
3121 (setq org-agenda-redo-command
3122 '(org-todo-list (or current-prefix-arg org-last-arg)))
3123 (setq files (org-agenda-files nil 'ifmode)
3124 rtnall nil)
3125 (while (setq file (pop files))
3126 (catch 'nextfile
3127 (org-check-agenda-file file)
3128 (setq rtn (org-agenda-get-day-entries file date :todo))
3129 (setq rtnall (append rtnall rtn))))
3130 (if org-agenda-overriding-header
3131 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3132 nil 'face 'org-agenda-structure) "\n")
3133 (insert "Global list of TODO items of type: ")
3134 (add-text-properties (point-min) (1- (point))
3135 (list 'face 'org-agenda-structure))
3136 (setq pos (point))
3137 (insert (or org-select-this-todo-keyword "ALL") "\n")
3138 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3139 (setq pos (point))
3140 (unless org-agenda-multi
3141 (insert "Available with `N r': (0)ALL")
3142 (let ((n 0) s)
3143 (mapc (lambda (x)
3144 (setq s (format "(%d)%s" (setq n (1+ n)) x))
3145 (if (> (+ (current-column) (string-width s) 1) (frame-width))
3146 (insert "\n "))
3147 (insert " " s))
3148 kwds))
3149 (insert "\n"))
3150 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
3151 (when rtnall
3152 (insert (org-finalize-agenda-entries rtnall) "\n"))
3153 (goto-char (point-min))
3154 (or org-agenda-multi (org-fit-agenda-window))
3155 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
3156 (org-finalize-agenda)
3157 (setq buffer-read-only t)))
3159 ;;; Agenda tags match
3161 ;;;###autoload
3162 (defun org-tags-view (&optional todo-only match)
3163 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
3164 The prefix arg TODO-ONLY limits the search to TODO entries."
3165 (interactive "P")
3166 (org-compile-prefix-format 'tags)
3167 (org-set-sorting-strategy 'tags)
3168 (let* ((org-tags-match-list-sublevels
3169 ;?????? (if todo-only t org-tags-match-list-sublevels))
3170 org-tags-match-list-sublevels)
3171 (completion-ignore-case t)
3172 rtn rtnall files file pos matcher
3173 buffer)
3174 (setq matcher (org-make-tags-matcher match)
3175 match (car matcher) matcher (cdr matcher))
3176 (org-prepare-agenda (concat "TAGS " match))
3177 (setq org-agenda-query-string match)
3178 (setq org-agenda-redo-command
3179 (list 'org-tags-view (list 'quote todo-only)
3180 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
3181 (setq files (org-agenda-files nil 'ifmode)
3182 rtnall nil)
3183 (while (setq file (pop files))
3184 (catch 'nextfile
3185 (org-check-agenda-file file)
3186 (setq buffer (if (file-exists-p file)
3187 (org-get-agenda-file-buffer file)
3188 (error "No such file %s" file)))
3189 (if (not buffer)
3190 ;; If file does not exist, error message to agenda
3191 (setq rtn (list
3192 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3193 rtnall (append rtnall rtn))
3194 (with-current-buffer buffer
3195 (unless (org-mode-p)
3196 (error "Agenda file %s is not in `org-mode'" file))
3197 (save-excursion
3198 (save-restriction
3199 (if org-agenda-restrict
3200 (narrow-to-region org-agenda-restrict-begin
3201 org-agenda-restrict-end)
3202 (widen))
3203 (setq rtn (org-scan-tags 'agenda matcher todo-only))
3204 (setq rtnall (append rtnall rtn))))))))
3205 (if org-agenda-overriding-header
3206 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3207 nil 'face 'org-agenda-structure) "\n")
3208 (insert "Headlines with TAGS match: ")
3209 (add-text-properties (point-min) (1- (point))
3210 (list 'face 'org-agenda-structure))
3211 (setq pos (point))
3212 (insert match "\n")
3213 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3214 (setq pos (point))
3215 (unless org-agenda-multi
3216 (insert "Press `C-u r' to search again with new search string\n"))
3217 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
3218 (when rtnall
3219 (insert (org-finalize-agenda-entries rtnall) "\n"))
3220 (goto-char (point-min))
3221 (or org-agenda-multi (org-fit-agenda-window))
3222 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
3223 (org-finalize-agenda)
3224 (setq buffer-read-only t)))
3226 ;;; Agenda Finding stuck projects
3228 (defvar org-agenda-skip-regexp nil
3229 "Regular expression used in skipping subtrees for the agenda.
3230 This is basically a temporary global variable that can be set and then
3231 used by user-defined selections using `org-agenda-skip-function'.")
3233 (defvar org-agenda-overriding-header nil
3234 "When this is set during todo and tags searches, will replace header.
3235 This variable should not be set directly, but custom commands can bind it
3236 in the options section.")
3238 (defun org-agenda-skip-entry-when-regexp-matches ()
3239 "Checks if the current entry contains match for `org-agenda-skip-regexp'.
3240 If yes, it returns the end position of this entry, causing agenda commands
3241 to skip the entry but continuing the search in the subtree. This is a
3242 function that can be put into `org-agenda-skip-function' for the duration
3243 of a command."
3244 (let ((end (save-excursion (org-end-of-subtree t)))
3245 skip)
3246 (save-excursion
3247 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3248 (and skip end)))
3250 (defun org-agenda-skip-subtree-when-regexp-matches ()
3251 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3252 If yes, it returns the end position of this tree, causing agenda commands
3253 to skip this subtree. This is a function that can be put into
3254 `org-agenda-skip-function' for the duration of a command."
3255 (let ((end (save-excursion (org-end-of-subtree t)))
3256 skip)
3257 (save-excursion
3258 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3259 (and skip end)))
3261 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
3262 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3263 If yes, it returns the end position of the current entry (NOT the tree),
3264 causing agenda commands to skip the entry but continuing the search in
3265 the subtree. This is a function that can be put into
3266 `org-agenda-skip-function' for the duration of a command. An important
3267 use of this function is for the stuck project list."
3268 (let ((end (save-excursion (org-end-of-subtree t)))
3269 (entry-end (save-excursion (outline-next-heading) (1- (point))))
3270 skip)
3271 (save-excursion
3272 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3273 (and skip entry-end)))
3275 (defun org-agenda-skip-entry-if (&rest conditions)
3276 "Skip entry if any of CONDITIONS is true.
3277 See `org-agenda-skip-if' for details."
3278 (org-agenda-skip-if nil conditions))
3280 (defun org-agenda-skip-subtree-if (&rest conditions)
3281 "Skip entry if any of CONDITIONS is true.
3282 See `org-agenda-skip-if' for details."
3283 (org-agenda-skip-if t conditions))
3285 (defun org-agenda-skip-if (subtree conditions)
3286 "Checks current entity for CONDITIONS.
3287 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
3288 the entry, i.e. the text before the next heading is checked.
3290 CONDITIONS is a list of symbols, boolean OR is used to combine the results
3291 from different tests. Valid conditions are:
3293 scheduled Check if there is a scheduled cookie
3294 notscheduled Check if there is no scheduled cookie
3295 deadline Check if there is a deadline
3296 notdeadline Check if there is no deadline
3297 timestamp Check if there is a timestamp (also deadline or scheduled)
3298 nottimestamp Check if there is no timestamp (also deadline or scheduled)
3299 regexp Check if regexp matches
3300 notregexp Check if regexp does not match.
3302 The regexp is taken from the conditions list, it must come right after
3303 the `regexp' or `notregexp' element.
3305 If any of these conditions is met, this function returns the end point of
3306 the entity, causing the search to continue from there. This is a function
3307 that can be put into `org-agenda-skip-function' for the duration of a command."
3308 (let (beg end m)
3309 (org-back-to-heading t)
3310 (setq beg (point)
3311 end (if subtree
3312 (progn (org-end-of-subtree t) (point))
3313 (progn (outline-next-heading) (1- (point)))))
3314 (goto-char beg)
3315 (and
3317 (and (memq 'scheduled conditions)
3318 (re-search-forward org-scheduled-time-regexp end t))
3319 (and (memq 'notscheduled conditions)
3320 (not (re-search-forward org-scheduled-time-regexp end t)))
3321 (and (memq 'deadline conditions)
3322 (re-search-forward org-deadline-time-regexp end t))
3323 (and (memq 'notdeadline conditions)
3324 (not (re-search-forward org-deadline-time-regexp end t)))
3325 (and (memq 'timestamp conditions)
3326 (re-search-forward org-ts-regexp end t))
3327 (and (memq 'nottimestamp conditions)
3328 (not (re-search-forward org-ts-regexp end t)))
3329 (and (setq m (memq 'regexp conditions))
3330 (stringp (nth 1 m))
3331 (re-search-forward (nth 1 m) end t))
3332 (and (setq m (memq 'notregexp conditions))
3333 (stringp (nth 1 m))
3334 (not (re-search-forward (nth 1 m) end t))))
3335 end)))
3337 ;;;###autoload
3338 (defun org-agenda-list-stuck-projects (&rest ignore)
3339 "Create agenda view for projects that are stuck.
3340 Stuck projects are project that have no next actions. For the definitions
3341 of what a project is and how to check if it stuck, customize the variable
3342 `org-stuck-projects'.
3343 MATCH is being ignored."
3344 (interactive)
3345 (let* ((org-agenda-skip-function
3346 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
3347 ;; We could have used org-agenda-skip-if here.
3348 (org-agenda-overriding-header
3349 (or org-agenda-overriding-header "List of stuck projects: "))
3350 (matcher (nth 0 org-stuck-projects))
3351 (todo (nth 1 org-stuck-projects))
3352 (todo-wds (if (member "*" todo)
3353 (progn
3354 (org-prepare-agenda-buffers (org-agenda-files
3355 nil 'ifmode))
3356 (org-delete-all
3357 org-done-keywords-for-agenda
3358 (copy-sequence org-todo-keywords-for-agenda)))
3359 todo))
3360 (todo-re (concat "^\\*+[ \t]+\\("
3361 (mapconcat 'identity todo-wds "\\|")
3362 "\\)\\>"))
3363 (tags (nth 2 org-stuck-projects))
3364 (tags-re (if (member "*" tags)
3365 (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
3366 (if tags
3367 (concat "^\\*+ .*:\\("
3368 (mapconcat 'identity tags "\\|")
3369 (org-re "\\):[[:alnum:]_@:]*[ \t]*$")))))
3370 (gen-re (nth 3 org-stuck-projects))
3371 (re-list
3372 (delq nil
3373 (list
3374 (if todo todo-re)
3375 (if tags tags-re)
3376 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
3377 gen-re)))))
3378 (setq org-agenda-skip-regexp
3379 (if re-list
3380 (mapconcat 'identity re-list "\\|")
3381 (error "No information how to identify unstuck projects")))
3382 (org-tags-view nil matcher)
3383 (with-current-buffer org-agenda-buffer-name
3384 (setq org-agenda-redo-command
3385 '(org-agenda-list-stuck-projects
3386 (or current-prefix-arg org-last-arg))))))
3388 ;;; Diary integration
3390 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
3391 (defvar list-diary-entries-hook)
3393 (defun org-get-entries-from-diary (date)
3394 "Get the (Emacs Calendar) diary entries for DATE."
3395 (require 'diary-lib)
3396 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
3397 (fancy-diary-buffer diary-fancy-buffer)
3398 (diary-display-hook '(fancy-diary-display))
3399 (diary-display-function 'fancy-diary-display)
3400 (pop-up-frames nil)
3401 (list-diary-entries-hook
3402 (cons 'org-diary-default-entry list-diary-entries-hook))
3403 (diary-file-name-prefix-function nil) ; turn this feature off
3404 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
3405 entries
3406 (org-disable-agenda-to-diary t))
3407 (save-excursion
3408 (save-window-excursion
3409 (funcall (if (fboundp 'diary-list-entries)
3410 'diary-list-entries 'list-diary-entries)
3411 date 1)))
3412 (if (not (get-buffer diary-fancy-buffer))
3413 (setq entries nil)
3414 (with-current-buffer diary-fancy-buffer
3415 (setq buffer-read-only nil)
3416 (if (zerop (buffer-size))
3417 ;; No entries
3418 (setq entries nil)
3419 ;; Omit the date and other unnecessary stuff
3420 (org-agenda-cleanup-fancy-diary)
3421 ;; Add prefix to each line and extend the text properties
3422 (if (zerop (buffer-size))
3423 (setq entries nil)
3424 (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
3425 (set-buffer-modified-p nil)
3426 (kill-buffer diary-fancy-buffer)))
3427 (when entries
3428 (setq entries (org-split-string entries "\n"))
3429 (setq entries
3430 (mapcar
3431 (lambda (x)
3432 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
3433 ;; Extend the text properties to the beginning of the line
3434 (org-add-props x (text-properties-at (1- (length x)) x)
3435 'type "diary" 'date date))
3436 entries)))))
3438 (defvar org-agenda-cleanup-fancy-diary-hook nil
3439 "Hook run when the fancy diary buffer is cleaned up.")
3441 (defun org-agenda-cleanup-fancy-diary ()
3442 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
3443 This gets rid of the date, the underline under the date, and
3444 the dummy entry installed by `org-mode' to ensure non-empty diary for each
3445 date. It also removes lines that contain only whitespace."
3446 (goto-char (point-min))
3447 (if (looking-at ".*?:[ \t]*")
3448 (progn
3449 (replace-match "")
3450 (re-search-forward "\n=+$" nil t)
3451 (replace-match "")
3452 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
3453 (re-search-forward "\n=+$" nil t)
3454 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
3455 (goto-char (point-min))
3456 (while (re-search-forward "^ +\n" nil t)
3457 (replace-match ""))
3458 (goto-char (point-min))
3459 (if (re-search-forward "^Org-mode dummy\n?" nil t)
3460 (replace-match ""))
3461 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
3463 ;; Make sure entries from the diary have the right text properties.
3464 (eval-after-load "diary-lib"
3465 '(if (boundp 'diary-modify-entry-list-string-function)
3466 ;; We can rely on the hook, nothing to do
3468 ;; Hook not available, must use advice to make this work
3469 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
3470 "Make the position visible."
3471 (if (and org-disable-agenda-to-diary ;; called from org-agenda
3472 (stringp string)
3473 buffer-file-name)
3474 (setq string (org-modify-diary-entry-string string))))))
3476 (defun org-modify-diary-entry-string (string)
3477 "Add text properties to string, allowing org-mode to act on it."
3478 (org-add-props string nil
3479 'mouse-face 'highlight
3480 'keymap org-agenda-keymap
3481 'help-echo (if buffer-file-name
3482 (format "mouse-2 or RET jump to diary file %s"
3483 (abbreviate-file-name buffer-file-name))
3485 'org-agenda-diary-link t
3486 'org-marker (org-agenda-new-marker (point-at-bol))))
3488 (defun org-diary-default-entry ()
3489 "Add a dummy entry to the diary.
3490 Needed to avoid empty dates which mess up holiday display."
3491 ;; Catch the error if dealing with the new add-to-diary-alist
3492 (when org-disable-agenda-to-diary
3493 (condition-case nil
3494 (org-add-to-diary-list original-date "Org-mode dummy" "")
3495 (error
3496 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
3498 (defun org-add-to-diary-list (&rest args)
3499 (if (fboundp 'diary-add-to-list)
3500 (apply 'diary-add-to-list args)
3501 (apply 'add-to-diary-list args)))
3503 ;;;###autoload
3504 (defun org-diary (&rest args)
3505 "Return diary information from org-files.
3506 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
3507 It accesses org files and extracts information from those files to be
3508 listed in the diary. The function accepts arguments specifying what
3509 items should be listed. The following arguments are allowed:
3511 :timestamp List the headlines of items containing a date stamp or
3512 date range matching the selected date. Deadlines will
3513 also be listed, on the expiration day.
3515 :sexp List entries resulting from diary-like sexps.
3517 :deadline List any deadlines past due, or due within
3518 `org-deadline-warning-days'. The listing occurs only
3519 in the diary for *today*, not at any other date. If
3520 an entry is marked DONE, it is no longer listed.
3522 :scheduled List all items which are scheduled for the given date.
3523 The diary for *today* also contains items which were
3524 scheduled earlier and are not yet marked DONE.
3526 :todo List all TODO items from the org-file. This may be a
3527 long list - so this is not turned on by default.
3528 Like deadlines, these entries only show up in the
3529 diary for *today*, not at any other date.
3531 The call in the diary file should look like this:
3533 &%%(org-diary) ~/path/to/some/orgfile.org
3535 Use a separate line for each org file to check. Or, if you omit the file name,
3536 all files listed in `org-agenda-files' will be checked automatically:
3538 &%%(org-diary)
3540 If you don't give any arguments (as in the example above), the default
3541 arguments (:deadline :scheduled :timestamp :sexp) are used.
3542 So the example above may also be written as
3544 &%%(org-diary :deadline :timestamp :sexp :scheduled)
3546 The function expects the lisp variables `entry' and `date' to be provided
3547 by the caller, because this is how the calendar works. Don't use this
3548 function from a program - use `org-agenda-get-day-entries' instead."
3549 (when (> (- (time-to-seconds (current-time))
3550 org-agenda-last-marker-time)
3552 (org-agenda-reset-markers))
3553 (org-compile-prefix-format 'agenda)
3554 (org-set-sorting-strategy 'agenda)
3555 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3556 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
3557 (list entry)
3558 (org-agenda-files t)))
3559 file rtn results)
3560 (org-prepare-agenda-buffers files)
3561 ;; If this is called during org-agenda, don't return any entries to
3562 ;; the calendar. Org Agenda will list these entries itself.
3563 (if org-disable-agenda-to-diary (setq files nil))
3564 (while (setq file (pop files))
3565 (setq rtn (apply 'org-agenda-get-day-entries file date args))
3566 (setq results (append results rtn)))
3567 (if results
3568 (concat (org-finalize-agenda-entries results) "\n"))))
3570 ;;; Agenda entry finders
3572 (defun org-agenda-get-day-entries (file date &rest args)
3573 "Does the work for `org-diary' and `org-agenda'.
3574 FILE is the path to a file to be checked for entries. DATE is date like
3575 the one returned by `calendar-current-date'. ARGS are symbols indicating
3576 which kind of entries should be extracted. For details about these, see
3577 the documentation of `org-diary'."
3578 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3579 (let* ((org-startup-folded nil)
3580 (org-startup-align-all-tables nil)
3581 (buffer (if (file-exists-p file)
3582 (org-get-agenda-file-buffer file)
3583 (error "No such file %s" file)))
3584 arg results rtn)
3585 (if (not buffer)
3586 ;; If file does not exist, make sure an error message ends up in diary
3587 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3588 (with-current-buffer buffer
3589 (unless (org-mode-p)
3590 (error "Agenda file %s is not in `org-mode'" file))
3591 (let ((case-fold-search nil))
3592 (save-excursion
3593 (save-restriction
3594 (if org-agenda-restrict
3595 (narrow-to-region org-agenda-restrict-begin
3596 org-agenda-restrict-end)
3597 (widen))
3598 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
3599 (while (setq arg (pop args))
3600 (cond
3601 ((and (eq arg :todo)
3602 (equal date (calendar-current-date)))
3603 (setq rtn (org-agenda-get-todos))
3604 (setq results (append results rtn)))
3605 ((eq arg :timestamp)
3606 (setq rtn (org-agenda-get-blocks))
3607 (setq results (append results rtn))
3608 (setq rtn (org-agenda-get-timestamps))
3609 (setq results (append results rtn)))
3610 ((eq arg :sexp)
3611 (setq rtn (org-agenda-get-sexps))
3612 (setq results (append results rtn)))
3613 ((eq arg :scheduled)
3614 (setq rtn (org-agenda-get-scheduled))
3615 (setq results (append results rtn)))
3616 ((eq arg :closed)
3617 (setq rtn (org-agenda-get-progress))
3618 (setq results (append results rtn)))
3619 ((eq arg :deadline)
3620 (setq rtn (org-agenda-get-deadlines))
3621 (setq results (append results rtn))))))))
3622 results))))
3624 (defun org-agenda-get-todos ()
3625 "Return the TODO information for agenda display."
3626 (let* ((props (list 'face nil
3627 'done-face 'org-agenda-done
3628 'org-not-done-regexp org-not-done-regexp
3629 'org-todo-regexp org-todo-regexp
3630 'org-complex-heading-regexp org-complex-heading-regexp
3631 'mouse-face 'highlight
3632 'keymap org-agenda-keymap
3633 'help-echo
3634 (format "mouse-2 or RET jump to org file %s"
3635 (abbreviate-file-name buffer-file-name))))
3636 (regexp (concat "^\\*+[ \t]+\\("
3637 (if org-select-this-todo-keyword
3638 (if (equal org-select-this-todo-keyword "*")
3639 org-todo-regexp
3640 (concat "\\<\\("
3641 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
3642 "\\)\\>"))
3643 org-not-done-regexp)
3644 "[^\n\r]*\\)"))
3645 marker priority category tags todo-state
3646 ee txt beg end)
3647 (goto-char (point-min))
3648 (while (re-search-forward regexp nil t)
3649 (catch :skip
3650 (save-match-data
3651 (beginning-of-line)
3652 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
3653 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
3654 (goto-char (1+ beg))
3655 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
3656 (throw :skip nil)))
3657 (goto-char beg)
3658 (org-agenda-skip)
3659 (goto-char (match-beginning 1))
3660 (setq marker (org-agenda-new-marker (match-beginning 0))
3661 category (org-get-category)
3662 txt (match-string 1)
3663 tags (org-get-tags-at (point))
3664 txt (org-format-agenda-item "" txt category tags)
3665 priority (1+ (org-get-priority txt))
3666 todo-state (org-get-todo-state))
3667 (org-add-props txt props
3668 'org-marker marker 'org-hd-marker marker
3669 'priority priority 'org-category category
3670 'type "todo" 'todo-state todo-state)
3671 (push txt ee)
3672 (if org-agenda-todo-list-sublevels
3673 (goto-char (match-end 1))
3674 (org-end-of-subtree 'invisible))))
3675 (nreverse ee)))
3677 ;;;###autoload
3678 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item (&optional end)
3679 "Do we have a reason to ignore this todo entry because it has a time stamp?"
3680 (when (or org-agenda-todo-ignore-with-date
3681 org-agenda-todo-ignore-scheduled
3682 org-agenda-todo-ignore-deadlines)
3683 (setq end (or end (save-excursion (outline-next-heading) (point))))
3684 (save-excursion
3685 (or (and org-agenda-todo-ignore-with-date
3686 (re-search-forward org-ts-regexp end t))
3687 (and org-agenda-todo-ignore-scheduled
3688 (re-search-forward org-scheduled-time-regexp end t))
3689 (and org-agenda-todo-ignore-deadlines
3690 (re-search-forward org-deadline-time-regexp end t)
3691 (org-deadline-close (match-string 1)))))))
3693 (defconst org-agenda-no-heading-message
3694 "No heading for this item in buffer or region.")
3696 (defun org-agenda-get-timestamps ()
3697 "Return the date stamp information for agenda display."
3698 (let* ((props (list 'face nil
3699 'org-not-done-regexp org-not-done-regexp
3700 'org-todo-regexp org-todo-regexp
3701 'org-complex-heading-regexp org-complex-heading-regexp
3702 'mouse-face 'highlight
3703 'keymap org-agenda-keymap
3704 'help-echo
3705 (format "mouse-2 or RET jump to org file %s"
3706 (abbreviate-file-name buffer-file-name))))
3707 (d1 (calendar-absolute-from-gregorian date))
3708 (remove-re
3709 (concat
3710 (regexp-quote
3711 (format-time-string
3712 "<%Y-%m-%d"
3713 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
3714 ".*?>"))
3715 (regexp
3716 (concat
3717 (if org-agenda-include-inactive-timestamps "[[<]" "<")
3718 (regexp-quote
3719 (substring
3720 (format-time-string
3721 (car org-time-stamp-formats)
3722 (apply 'encode-time ; DATE bound by calendar
3723 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
3724 1 11))
3725 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3726 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
3727 marker hdmarker deadlinep scheduledp clockp closedp inactivep
3728 donep tmp priority category ee txt timestr tags b0 b3 e3 head
3729 todo-state end-of-match)
3730 (goto-char (point-min))
3731 (while (setq end-of-match (re-search-forward regexp nil t))
3732 (setq b0 (match-beginning 0)
3733 b3 (match-beginning 3) e3 (match-end 3))
3734 (catch :skip
3735 (and (org-at-date-range-p) (throw :skip nil))
3736 (org-agenda-skip)
3737 (if (and (match-end 1)
3738 (not (= d1 (org-time-string-to-absolute
3739 (match-string 1) d1 nil
3740 org-agenda-repeating-timestamp-show-all))))
3741 (throw :skip nil))
3742 (if (and e3
3743 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
3744 (throw :skip nil))
3745 (setq tmp (buffer-substring (max (point-min)
3746 (- b0 org-ds-keyword-length))
3748 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
3749 inactivep (= (char-after b0) ?\[)
3750 deadlinep (string-match org-deadline-regexp tmp)
3751 scheduledp (string-match org-scheduled-regexp tmp)
3752 closedp (and org-agenda-include-inactive-timestamps
3753 (string-match org-closed-string tmp))
3754 clockp (and org-agenda-include-inactive-timestamps
3755 (or (string-match org-clock-string tmp)
3756 (string-match "]-+\\'" tmp)))
3757 todo-state (org-get-todo-state)
3758 donep (member todo-state org-done-keywords))
3759 (if (or scheduledp deadlinep closedp clockp
3760 (and donep org-agenda-skip-timestamp-if-done))
3761 (throw :skip t))
3762 (if (string-match ">" timestr)
3763 ;; substring should only run to end of time stamp
3764 (setq timestr (substring timestr 0 (match-end 0))))
3765 (setq marker (org-agenda-new-marker b0)
3766 category (org-get-category b0))
3767 (save-excursion
3768 (if (not (re-search-backward "^\\*+ " nil t))
3769 (setq txt org-agenda-no-heading-message)
3770 (goto-char (match-beginning 0))
3771 (setq hdmarker (org-agenda-new-marker)
3772 tags (org-get-tags-at))
3773 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
3774 (setq head (match-string 1))
3775 (setq txt (org-format-agenda-item
3776 (if inactivep "[" nil)
3777 head category tags timestr nil
3778 remove-re)))
3779 (setq priority (org-get-priority txt))
3780 (org-add-props txt props
3781 'org-marker marker 'org-hd-marker hdmarker)
3782 (org-add-props txt nil 'priority priority
3783 'org-category category 'date date
3784 'todo-state todo-state
3785 'type "timestamp")
3786 (push txt ee))
3787 (if org-agenda-skip-additional-timestamps-same-entry
3788 (outline-next-heading)
3789 (goto-char end-of-match))))
3790 (nreverse ee)))
3792 (defun org-agenda-get-sexps ()
3793 "Return the sexp information for agenda display."
3794 (require 'diary-lib)
3795 (let* ((props (list 'face nil
3796 'mouse-face 'highlight
3797 'keymap org-agenda-keymap
3798 'help-echo
3799 (format "mouse-2 or RET jump to org file %s"
3800 (abbreviate-file-name buffer-file-name))))
3801 (regexp "^&?%%(")
3802 marker category ee txt tags entry result beg b sexp sexp-entry
3803 todo-state)
3804 (goto-char (point-min))
3805 (while (re-search-forward regexp nil t)
3806 (catch :skip
3807 (org-agenda-skip)
3808 (setq beg (match-beginning 0))
3809 (goto-char (1- (match-end 0)))
3810 (setq b (point))
3811 (forward-sexp 1)
3812 (setq sexp (buffer-substring b (point)))
3813 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
3814 (org-trim (match-string 1))
3815 ""))
3816 (setq result (org-diary-sexp-entry sexp sexp-entry date))
3817 (when result
3818 (setq marker (org-agenda-new-marker beg)
3819 category (org-get-category beg)
3820 todo-state (org-get-todo-state))
3822 (if (string-match "\\S-" result)
3823 (setq txt result)
3824 (setq txt "SEXP entry returned empty string"))
3826 (setq txt (org-format-agenda-item
3827 "" txt category tags 'time))
3828 (org-add-props txt props 'org-marker marker)
3829 (org-add-props txt nil
3830 'org-category category 'date date 'todo-state todo-state
3831 'type "sexp")
3832 (push txt ee))))
3833 (nreverse ee)))
3835 (defalias 'org-get-closed 'org-agenda-get-progress)
3836 (defun org-agenda-get-progress ()
3837 "Return the logged TODO entries for agenda display."
3838 (let* ((props (list 'mouse-face 'highlight
3839 'org-not-done-regexp org-not-done-regexp
3840 'org-todo-regexp org-todo-regexp
3841 'org-complex-heading-regexp org-complex-heading-regexp
3842 'keymap org-agenda-keymap
3843 'help-echo
3844 (format "mouse-2 or RET jump to org file %s"
3845 (abbreviate-file-name buffer-file-name))))
3846 (items (if (consp org-agenda-show-log)
3847 org-agenda-show-log
3848 org-agenda-log-mode-items))
3849 (parts
3850 (delq nil
3851 (list
3852 (if (memq 'closed items) (concat "\\<" org-closed-string))
3853 (if (memq 'clock items) (concat "\\<" org-clock-string))
3854 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
3855 (parts-re (if parts (mapconcat 'identity parts "\\|")
3856 (error "`org-agenda-log-mode-items' is empty")))
3857 (regexp (concat
3858 "\\(" parts-re "\\)"
3859 " *\\["
3860 (regexp-quote
3861 (substring
3862 (format-time-string
3863 (car org-time-stamp-formats)
3864 (apply 'encode-time ; DATE bound by calendar
3865 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
3866 1 11))))
3867 (org-agenda-search-headline-for-time nil)
3868 marker hdmarker priority category tags closedp statep clockp state
3869 ee txt extra timestr rest clocked)
3870 (goto-char (point-min))
3871 (while (re-search-forward regexp nil t)
3872 (catch :skip
3873 (org-agenda-skip)
3874 (setq marker (org-agenda-new-marker (match-beginning 0))
3875 closedp (equal (match-string 1) org-closed-string)
3876 statep (equal (string-to-char (match-string 1)) ?-)
3877 clockp (not (or closedp statep))
3878 state (and statep (match-string 2))
3879 category (org-get-category (match-beginning 0))
3880 timestr (buffer-substring (match-beginning 0) (point-at-eol))
3882 (when (string-match "\\]" timestr)
3883 ;; substring should only run to end of time stamp
3884 (setq rest (substring timestr (match-end 0))
3885 timestr (substring timestr 0 (match-end 0)))
3886 (if (and (not closedp) (not statep)
3887 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
3888 (progn (setq timestr (concat (substring timestr 0 -1)
3889 "-" (match-string 1 rest) "]"))
3890 (setq clocked (match-string 2 rest)))
3891 (setq clocked "-")))
3892 (save-excursion
3893 (cond
3894 ((not org-agenda-log-mode-add-notes) (setq extra nil))
3895 (statep
3896 (and (looking-at ".*\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
3897 (setq extra (match-string 1))))
3898 (clockp
3899 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
3900 (setq extra (match-string 1))))
3901 (t (setq extra nil)))
3902 (if (not (re-search-backward "^\\*+ " nil t))
3903 (setq txt org-agenda-no-heading-message)
3904 (goto-char (match-beginning 0))
3905 (setq hdmarker (org-agenda-new-marker)
3906 tags (org-get-tags-at))
3907 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
3908 (setq txt (match-string 1))
3909 (when extra
3910 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
3911 (setq txt (concat (substring txt 0 (match-beginning 1))
3912 " - " extra " " (match-string 2 txt)))
3913 (setq txt (concat txt " - " extra))))
3914 (setq txt (org-format-agenda-item
3915 (cond
3916 (closedp "Closed: ")
3917 (statep (concat "State: (" state ")"))
3918 (t (concat "Clocked: (" clocked ")")))
3919 txt category tags timestr)))
3920 (setq priority 100000)
3921 (org-add-props txt props
3922 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
3923 'priority priority 'org-category category
3924 'type "closed" 'date date
3925 'undone-face 'org-warning 'done-face 'org-agenda-done)
3926 (push txt ee))
3927 (goto-char (point-at-eol))))
3928 (nreverse ee)))
3930 (defun org-agenda-get-deadlines ()
3931 "Return the deadline information for agenda display."
3932 (let* ((props (list 'mouse-face 'highlight
3933 'org-not-done-regexp org-not-done-regexp
3934 'org-todo-regexp org-todo-regexp
3935 'org-complex-heading-regexp org-complex-heading-regexp
3936 'keymap org-agenda-keymap
3937 'help-echo
3938 (format "mouse-2 or RET jump to org file %s"
3939 (abbreviate-file-name buffer-file-name))))
3940 (regexp org-deadline-time-regexp)
3941 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
3942 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
3943 d2 diff dfrac wdays pos pos1 category tags
3944 ee txt head face s todo-state upcomingp donep timestr)
3945 (goto-char (point-min))
3946 (while (re-search-forward regexp nil t)
3947 (catch :skip
3948 (org-agenda-skip)
3949 (setq s (match-string 1)
3950 txt nil
3951 pos (1- (match-beginning 1))
3952 d2 (org-time-string-to-absolute
3953 (match-string 1) d1 'past
3954 org-agenda-repeating-timestamp-show-all)
3955 diff (- d2 d1)
3956 wdays (org-get-wdays s)
3957 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
3958 upcomingp (and todayp (> diff 0)))
3959 ;; When to show a deadline in the calendar:
3960 ;; If the expiration is within wdays warning time.
3961 ;; Past-due deadlines are only shown on the current date
3962 (if (or (and (<= diff wdays)
3963 (and todayp (not org-agenda-only-exact-dates)))
3964 (= diff 0))
3965 (save-excursion
3966 (setq todo-state (org-get-todo-state))
3967 (setq donep (member todo-state org-done-keywords))
3968 (if (and donep
3969 (or org-agenda-skip-deadline-if-done
3970 (not (= diff 0))))
3971 (setq txt nil)
3972 (setq category (org-get-category))
3973 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
3974 (setq txt org-agenda-no-heading-message)
3975 (goto-char (match-end 0))
3976 (setq pos1 (match-beginning 0))
3977 (setq tags (org-get-tags-at pos1))
3978 (setq head (buffer-substring-no-properties
3979 (point)
3980 (progn (skip-chars-forward "^\r\n")
3981 (point))))
3982 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
3983 (setq timestr
3984 (concat (substring s (match-beginning 1)) " "))
3985 (setq timestr 'time))
3986 (setq txt (org-format-agenda-item
3987 (if (= diff 0)
3988 (car org-agenda-deadline-leaders)
3989 (if (functionp
3990 (nth 1 org-agenda-deadline-leaders))
3991 (funcall
3992 (nth 1 org-agenda-deadline-leaders)
3993 diff date)
3994 (format (nth 1 org-agenda-deadline-leaders)
3995 diff)))
3996 head category tags
3997 (if (not (= diff 0)) nil timestr)))))
3998 (when txt
3999 (setq face (org-agenda-deadline-face dfrac wdays))
4000 (org-add-props txt props
4001 'org-marker (org-agenda-new-marker pos)
4002 'org-hd-marker (org-agenda-new-marker pos1)
4003 'priority (+ (- diff)
4004 (org-get-priority txt))
4005 'org-category category
4006 'todo-state todo-state
4007 'type (if upcomingp "upcoming-deadline" "deadline")
4008 'date (if upcomingp date d2)
4009 'face (if donep 'org-agenda-done face)
4010 'undone-face face 'done-face 'org-agenda-done)
4011 (push txt ee))))))
4012 (nreverse ee)))
4014 (defun org-agenda-deadline-face (fraction &optional wdays)
4015 "Return the face to displaying a deadline item.
4016 FRACTION is what fraction of the head-warning time has passed."
4017 (if (equal wdays 0) (setq fraction 1.))
4018 (let ((faces org-agenda-deadline-faces) f)
4019 (catch 'exit
4020 (while (setq f (pop faces))
4021 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
4023 (defun org-agenda-get-scheduled ()
4024 "Return the scheduled information for agenda display."
4025 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
4026 'org-todo-regexp org-todo-regexp
4027 'org-complex-heading-regexp org-complex-heading-regexp
4028 'done-face 'org-agenda-done
4029 'mouse-face 'highlight
4030 'keymap org-agenda-keymap
4031 'help-echo
4032 (format "mouse-2 or RET jump to org file %s"
4033 (abbreviate-file-name buffer-file-name))))
4034 (regexp org-scheduled-time-regexp)
4035 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
4036 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4037 d2 diff pos pos1 category tags donep
4038 ee txt head pastschedp todo-state face timestr s)
4039 (goto-char (point-min))
4040 (while (re-search-forward regexp nil t)
4041 (catch :skip
4042 (org-agenda-skip)
4043 (setq s (match-string 1)
4044 txt nil
4045 pos (1- (match-beginning 1))
4046 d2 (org-time-string-to-absolute
4047 (match-string 1) d1 'past
4048 org-agenda-repeating-timestamp-show-all)
4049 diff (- d2 d1))
4050 (setq pastschedp (and todayp (< diff 0)))
4051 ;; When to show a scheduled item in the calendar:
4052 ;; If it is on or past the date.
4053 (if (or (and (< diff 0)
4054 (< (abs diff) org-scheduled-past-days)
4055 (and todayp (not org-agenda-only-exact-dates)))
4056 (= diff 0))
4057 (save-excursion
4058 (setq todo-state (org-get-todo-state))
4059 (setq donep (member todo-state org-done-keywords))
4060 (if (and donep
4061 (or org-agenda-skip-scheduled-if-done
4062 (not (= diff 0))))
4063 (setq txt nil)
4064 (setq category (org-get-category))
4065 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4066 (setq txt org-agenda-no-heading-message)
4067 (goto-char (match-end 0))
4068 (setq pos1 (match-beginning 0))
4069 (setq tags (org-get-tags-at))
4070 (setq head (buffer-substring-no-properties
4071 (point)
4072 (progn (skip-chars-forward "^\r\n") (point))))
4073 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4074 (setq timestr
4075 (concat (substring s (match-beginning 1)) " "))
4076 (setq timestr 'time))
4077 (setq txt (org-format-agenda-item
4078 (if (= diff 0)
4079 (car org-agenda-scheduled-leaders)
4080 (format (nth 1 org-agenda-scheduled-leaders)
4081 (- 1 diff)))
4082 head category tags
4083 (if (not (= diff 0)) nil timestr)))))
4084 (when txt
4085 (setq face
4086 (cond
4087 (pastschedp 'org-scheduled-previously)
4088 (todayp 'org-scheduled-today)
4089 (t 'org-scheduled)))
4090 (org-add-props txt props
4091 'undone-face face
4092 'face (if donep 'org-agenda-done face)
4093 'org-marker (org-agenda-new-marker pos)
4094 'org-hd-marker (org-agenda-new-marker pos1)
4095 'type (if pastschedp "past-scheduled" "scheduled")
4096 'date (if pastschedp d2 date)
4097 'priority (+ 94 (- 5 diff) (org-get-priority txt))
4098 'org-category category
4099 'todo-state todo-state)
4100 (push txt ee))))))
4101 (nreverse ee)))
4103 (defun org-agenda-get-blocks ()
4104 "Return the date-range information for agenda display."
4105 (let* ((props (list 'face nil
4106 'org-not-done-regexp org-not-done-regexp
4107 'org-todo-regexp org-todo-regexp
4108 'org-complex-heading-regexp org-complex-heading-regexp
4109 'mouse-face 'highlight
4110 'keymap org-agenda-keymap
4111 'help-echo
4112 (format "mouse-2 or RET jump to org file %s"
4113 (abbreviate-file-name buffer-file-name))))
4114 (regexp org-tr-regexp)
4115 (d0 (calendar-absolute-from-gregorian date))
4116 marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
4117 head donep)
4118 (goto-char (point-min))
4119 (while (re-search-forward regexp nil t)
4120 (catch :skip
4121 (org-agenda-skip)
4122 (setq pos (point))
4123 (setq timestr (match-string 0)
4124 s1 (match-string 1)
4125 s2 (match-string 2)
4126 d1 (time-to-days (org-time-string-to-time s1))
4127 d2 (time-to-days (org-time-string-to-time s2)))
4128 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
4129 ;; Only allow days between the limits, because the normal
4130 ;; date stamps will catch the limits.
4131 (save-excursion
4132 (setq todo-state (org-get-todo-state))
4133 (setq donep (member todo-state org-done-keywords))
4134 (if (and donep org-agenda-skip-timestamp-if-done)
4135 (throw :skip t))
4136 (setq marker (org-agenda-new-marker (point)))
4137 (setq category (org-get-category))
4138 (if (not (re-search-backward "^\\*+ " nil t))
4139 (setq txt org-agenda-no-heading-message)
4140 (goto-char (match-beginning 0))
4141 (setq hdmarker (org-agenda-new-marker (point)))
4142 (setq tags (org-get-tags-at))
4143 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4144 (setq head (match-string 1))
4145 (setq txt (org-format-agenda-item
4146 (format
4147 (nth (if (= d1 d2) 0 1)
4148 org-agenda-timerange-leaders)
4149 (1+ (- d0 d1)) (1+ (- d2 d1)))
4150 head category tags
4151 (if (= d0 d1) timestr))))
4152 (org-add-props txt props
4153 'org-marker marker 'org-hd-marker hdmarker
4154 'type "block" 'date date
4155 'todo-state todo-state
4156 'priority (org-get-priority txt) 'org-category category)
4157 (push txt ee)))
4158 (goto-char pos)))
4159 ;; Sort the entries by expiration date.
4160 (nreverse ee)))
4162 ;;; Agenda presentation and sorting
4164 (defvar org-prefix-has-time nil
4165 "A flag, set by `org-compile-prefix-format'.
4166 The flag is set if the currently compiled format contains a `%t'.")
4167 (defvar org-prefix-has-tag nil
4168 "A flag, set by `org-compile-prefix-format'.
4169 The flag is set if the currently compiled format contains a `%T'.")
4170 (defvar org-prefix-has-effort nil
4171 "A flag, set by `org-compile-prefix-format'.
4172 The flag is set if the currently compiled format contains a `%e'.")
4174 (defun org-format-agenda-item (extra txt &optional category tags dotime
4175 noprefix remove-re)
4176 "Format TXT to be inserted into the agenda buffer.
4177 In particular, it adds the prefix and corresponding text properties. EXTRA
4178 must be a string and replaces the `%s' specifier in the prefix format.
4179 CATEGORY (string, symbol or nil) may be used to overrule the default
4180 category taken from local variable or file name. It will replace the `%c'
4181 specifier in the format. DOTIME, when non-nil, indicates that a
4182 time-of-day should be extracted from TXT for sorting of this entry, and for
4183 the `%t' specifier in the format. When DOTIME is a string, this string is
4184 searched for a time before TXT is. NOPREFIX is a flag and indicates that
4185 only the correctly processes TXT should be returned - this is used by
4186 `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
4187 Any match of REMOVE-RE will be removed from TXT."
4188 (save-match-data
4189 ;; Diary entries sometimes have extra whitespace at the beginning
4190 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
4191 (when org-agenda-show-inherited-tags
4192 ;; Fix the tags part in txt
4193 (setq txt (org-agenda-add-inherited-tags txt tags)))
4194 (let* ((category (or category
4195 org-category
4196 (if buffer-file-name
4197 (file-name-sans-extension
4198 (file-name-nondirectory buffer-file-name))
4199 "")))
4200 ;; time, tag, effort are needed for the eval of the prefix format
4201 (tag (if tags (nth (1- (length tags)) tags) ""))
4202 time effort neffort
4203 (ts (if dotime (concat
4204 (if (stringp dotime) dotime "")
4205 (and org-agenda-search-headline-for-time txt))))
4206 (time-of-day (and dotime (org-get-time-of-day ts)))
4207 stamp plain s0 s1 s2 t1 t2 rtn srp
4208 duration)
4209 (and (org-mode-p) buffer-file-name
4210 (add-to-list 'org-agenda-contributing-files buffer-file-name))
4211 (when (and dotime time-of-day)
4212 ;; Extract starting and ending time and move them to prefix
4213 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
4214 (setq plain (string-match org-plain-time-of-day-regexp ts)))
4215 (setq s0 (match-string 0 ts)
4216 srp (and stamp (match-end 3))
4217 s1 (match-string (if plain 1 2) ts)
4218 s2 (match-string (if plain 8 (if srp 4 6)) ts))
4220 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
4221 ;; them, we might want to remove them there to avoid duplication.
4222 ;; The user can turn this off with a variable.
4223 (if (and org-prefix-has-time
4224 org-agenda-remove-times-when-in-prefix (or stamp plain)
4225 (string-match (concat (regexp-quote s0) " *") txt)
4226 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
4227 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
4228 (= (match-beginning 0) 0)
4230 (setq txt (replace-match "" nil nil txt))))
4231 ;; Normalize the time(s) to 24 hour
4232 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
4233 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
4234 ;; Compute the duration
4235 (when s1
4236 (setq t1 (+ (* 60 (string-to-number (substring s1 0 2)))
4237 (string-to-number (substring s1 3)))
4238 t2 (cond
4239 (s2 (+ (* 60 (string-to-number (substring s2 0 2)))
4240 (string-to-number (substring s2 3))))
4241 (org-agenda-default-appointment-duration
4242 (+ t1 org-agenda-default-appointment-duration))
4243 (t nil)))
4244 (setq duration (if t2 (- t2 t1)))))
4246 (when (and s1 (not s2) org-agenda-default-appointment-duration
4247 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
4248 (let ((m (+ (string-to-number (match-string 2 s1))
4249 (* 60 (string-to-number (match-string 1 s1)))
4250 org-agenda-default-appointment-duration))
4252 (setq h (/ m 60) m (- m (* h 60)))
4253 (setq s2 (format "%02d:%02d" h m))))
4255 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
4256 txt)
4257 ;; Tags are in the string
4258 (if (or (eq org-agenda-remove-tags t)
4259 (and org-agenda-remove-tags
4260 org-prefix-has-tag))
4261 (setq txt (replace-match "" t t txt))
4262 (setq txt (replace-match
4263 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
4264 (match-string 2 txt))
4265 t t txt))))
4266 (when (org-mode-p)
4267 (setq effort
4268 (condition-case nil
4269 (org-get-effort
4270 (or (get-text-property 0 'org-hd-marker txt)
4271 (get-text-property 0 'org-marker txt)))
4272 (error nil)))
4273 (when effort
4274 (setq neffort (org-hh:mm-string-to-minutes effort)
4275 effort (setq effort (concat "[" effort"]" )))))
4277 (when remove-re
4278 (while (string-match remove-re txt)
4279 (setq txt (replace-match "" t t txt))))
4281 ;; Create the final string
4282 (if noprefix
4283 (setq rtn txt)
4284 ;; Prepare the variables needed in the eval of the compiled format
4285 (setq time (cond (s2 (concat s1 "-" s2))
4286 (s1 (concat s1 "......"))
4287 (t ""))
4288 extra (or extra "")
4289 category (if (symbolp category) (symbol-name category) category))
4290 ;; Evaluate the compiled format
4291 (setq rtn (concat (eval org-prefix-format-compiled) txt)))
4293 ;; And finally add the text properties
4294 (org-add-props rtn nil
4295 'org-category (downcase category)
4296 'tags (mapcar 'org-downcase-keep-props tags)
4297 'org-highest-priority org-highest-priority
4298 'org-lowest-priority org-lowest-priority
4299 'prefix-length (- (length rtn) (length txt))
4300 'time-of-day time-of-day
4301 'duration duration
4302 'effort effort
4303 'effort-minutes neffort
4304 'txt txt
4305 'time time
4306 'extra extra
4307 'dotime dotime))))
4309 (defun org-agenda-add-inherited-tags (txt tags)
4310 "Remove tags string from TXT, and add complete list of tags.
4311 The new list includes inherited tags. If any inherited tags are present,
4312 a double colon separates inherited tags from local tags."
4313 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$") txt)
4314 (setq txt (substring txt 0 (match-beginning 0))))
4315 (when tags
4316 (let ((have-i (get-text-property 0 'inherited (car tags)))
4318 (setq txt (concat txt " :"
4319 (mapconcat
4320 (lambda (x)
4321 (setq i (get-text-property 0 'inherited x))
4322 (if (and have-i (not i))
4323 (progn
4324 (setq have-i nil)
4325 (concat ":" x))
4327 tags ":")
4328 (if have-i "::" ":")))))
4329 txt)
4331 (defun org-downcase-keep-props (s)
4332 (let ((props (text-properties-at 0 s)))
4333 (setq s (downcase s))
4334 (add-text-properties 0 (length s) props s)
4337 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
4338 (defvar org-agenda-sorting-strategy-selected nil)
4340 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
4341 (catch 'exit
4342 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
4343 ((and todayp (member 'today (car org-agenda-time-grid))))
4344 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
4345 ((member 'weekly (car org-agenda-time-grid)))
4346 (t (throw 'exit list)))
4347 (let* ((have (delq nil (mapcar
4348 (lambda (x) (get-text-property 1 'time-of-day x))
4349 list)))
4350 (string (nth 1 org-agenda-time-grid))
4351 (gridtimes (nth 2 org-agenda-time-grid))
4352 (req (car org-agenda-time-grid))
4353 (remove (member 'remove-match req))
4354 new time)
4355 (if (and (member 'require-timed req) (not have))
4356 ;; don't show empty grid
4357 (throw 'exit list))
4358 (while (setq time (pop gridtimes))
4359 (unless (and remove (member time have))
4360 (setq time (int-to-string time))
4361 (push (org-format-agenda-item
4362 nil string "" nil
4363 (concat (substring time 0 -2) ":" (substring time -2)))
4364 new)
4365 (put-text-property
4366 1 (length (car new)) 'face 'org-time-grid (car new))))
4367 (if (member 'time-up org-agenda-sorting-strategy-selected)
4368 (append new list)
4369 (append list new)))))
4371 (defun org-compile-prefix-format (key)
4372 "Compile the prefix format into a Lisp form that can be evaluated.
4373 The resulting form is returned and stored in the variable
4374 `org-prefix-format-compiled'."
4375 (setq org-prefix-has-time nil org-prefix-has-tag nil
4376 org-prefix-has-effort nil)
4377 (let ((s (cond
4378 ((stringp org-agenda-prefix-format)
4379 org-agenda-prefix-format)
4380 ((assq key org-agenda-prefix-format)
4381 (cdr (assq key org-agenda-prefix-format)))
4382 (t " %-12:c%?-12t% s")))
4383 (start 0)
4384 varform vars var e c f opt)
4385 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctse]\\)"
4386 s start)
4387 (setq var (cdr (assoc (match-string 4 s)
4388 '(("c" . category) ("t" . time) ("s" . extra)
4389 ("T" . tag) ("e" . effort))))
4390 c (or (match-string 3 s) "")
4391 opt (match-beginning 1)
4392 start (1+ (match-beginning 0)))
4393 (if (equal var 'time) (setq org-prefix-has-time t))
4394 (if (equal var 'tag) (setq org-prefix-has-tag t))
4395 (if (equal var 'effort) (setq org-prefix-has-effort t))
4396 (setq f (concat "%" (match-string 2 s) "s"))
4397 (if opt
4398 (setq varform
4399 `(if (equal "" ,var)
4401 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
4402 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
4403 (setq s (replace-match "%s" t nil s))
4404 (push varform vars))
4405 (setq vars (nreverse vars))
4406 (setq org-prefix-format-compiled `(format ,s ,@vars))))
4408 (defun org-set-sorting-strategy (key)
4409 (if (symbolp (car org-agenda-sorting-strategy))
4410 ;; the old format
4411 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
4412 (setq org-agenda-sorting-strategy-selected
4413 (or (cdr (assq key org-agenda-sorting-strategy))
4414 (cdr (assq 'agenda org-agenda-sorting-strategy))
4415 '(time-up category-keep priority-down)))))
4417 (defun org-get-time-of-day (s &optional string mod24)
4418 "Check string S for a time of day.
4419 If found, return it as a military time number between 0 and 2400.
4420 If not found, return nil.
4421 The optional STRING argument forces conversion into a 5 character wide string
4422 HH:MM."
4423 (save-match-data
4424 (when
4425 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
4426 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
4427 (let* ((h (string-to-number (match-string 1 s)))
4428 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
4429 (ampm (if (match-end 4) (downcase (match-string 4 s))))
4430 (am-p (equal ampm "am"))
4431 (h1 (cond ((not ampm) h)
4432 ((= h 12) (if am-p 0 12))
4433 (t (+ h (if am-p 0 12)))))
4434 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
4435 (mod h1 24) h1))
4436 (t0 (+ (* 100 h2) m))
4437 (t1 (concat (if (>= h1 24) "+" " ")
4438 (if (< t0 100) "0" "")
4439 (if (< t0 10) "0" "")
4440 (int-to-string t0))))
4441 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
4443 (defun org-finalize-agenda-entries (list &optional nosort)
4444 "Sort and concatenate the agenda items."
4445 (setq list (mapcar 'org-agenda-highlight-todo list))
4446 (if nosort
4447 list
4448 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
4450 (defun org-agenda-highlight-todo (x)
4451 (let ((org-done-keywords org-done-keywords-for-agenda)
4452 re pl)
4453 (if (eq x 'line)
4454 (save-excursion
4455 (beginning-of-line 1)
4456 (setq re (get-text-property (point) 'org-todo-regexp))
4457 (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
4458 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
4459 (add-text-properties (match-beginning 0) (match-end 1)
4460 (list 'face (org-get-todo-face 1)))
4461 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
4462 (delete-region (match-beginning 1) (1- (match-end 0)))
4463 (goto-char (match-beginning 1))
4464 (insert (format org-agenda-todo-keyword-format s)))))
4465 (setq re (concat (get-text-property 0 'org-todo-regexp x))
4466 pl (get-text-property 0 'prefix-length x))
4467 (when (and re
4468 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
4469 x (or pl 0)) pl))
4470 (add-text-properties
4471 (or (match-end 1) (match-end 0)) (match-end 0)
4472 (list 'face (org-get-todo-face (match-string 2 x)))
4474 (setq x (concat (substring x 0 (match-end 1))
4475 (format org-agenda-todo-keyword-format
4476 (match-string 2 x))
4478 (substring x (match-end 3)))))
4479 x)))
4481 (defsubst org-cmp-priority (a b)
4482 "Compare the priorities of string A and B."
4483 (let ((pa (or (get-text-property 1 'priority a) 0))
4484 (pb (or (get-text-property 1 'priority b) 0)))
4485 (cond ((> pa pb) +1)
4486 ((< pa pb) -1)
4487 (t nil))))
4489 (defsubst org-cmp-effort (a b)
4490 "Compare the priorities of string A and B."
4491 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
4492 (ea (or (get-text-property 1 'effort-minutes a) def))
4493 (eb (or (get-text-property 1 'effort-minutes b) def)))
4494 (cond ((> ea eb) +1)
4495 ((< ea eb) -1)
4496 (t nil))))
4498 (defsubst org-cmp-category (a b)
4499 "Compare the string values of categories of strings A and B."
4500 (let ((ca (or (get-text-property 1 'org-category a) ""))
4501 (cb (or (get-text-property 1 'org-category b) "")))
4502 (cond ((string-lessp ca cb) -1)
4503 ((string-lessp cb ca) +1)
4504 (t nil))))
4506 (defsubst org-cmp-todo-state (a b)
4507 "Compare the todo states of strings A and B."
4508 (let* ((ma (or (get-text-property 1 'org-marker a)
4509 (get-text-property 1 'org-hd-marker a)))
4510 (mb (or (get-text-property 1 'org-marker b)
4511 (get-text-property 1 'org-hd-marker b)))
4512 (fa (and ma (marker-buffer ma)))
4513 (fb (and mb (marker-buffer mb)))
4514 (todo-kwds
4515 (or (and fa (with-current-buffer fa org-todo-keywords-1))
4516 (and fb (with-current-buffer fb org-todo-keywords-1))))
4517 (ta (or (get-text-property 1 'todo-state a) ""))
4518 (tb (or (get-text-property 1 'todo-state b) ""))
4519 (la (- (length (member ta todo-kwds))))
4520 (lb (- (length (member tb todo-kwds))))
4521 (donepa (member ta org-done-keywords-for-agenda))
4522 (donepb (member tb org-done-keywords-for-agenda)))
4523 (cond ((and donepa (not donepb)) -1)
4524 ((and (not donepa) donepb) +1)
4525 ((< la lb) -1)
4526 ((< lb la) +1)
4527 (t nil))))
4529 (defsubst org-cmp-tag (a b)
4530 "Compare the string values of the first tags of A and B."
4531 (let ((ta (car (last (get-text-property 1 'tags a))))
4532 (tb (car (last (get-text-property 1 'tags b)))))
4533 (cond ((not ta) +1)
4534 ((not tb) -1)
4535 ((string-lessp ta tb) -1)
4536 ((string-lessp tb ta) +1)
4537 (t nil))))
4539 (defsubst org-cmp-time (a b)
4540 "Compare the time-of-day values of strings A and B."
4541 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
4542 (ta (or (get-text-property 1 'time-of-day a) def))
4543 (tb (or (get-text-property 1 'time-of-day b) def)))
4544 (cond ((< ta tb) -1)
4545 ((< tb ta) +1)
4546 (t nil))))
4548 (defun org-entries-lessp (a b)
4549 "Predicate for sorting agenda entries."
4550 ;; The following variables will be used when the form is evaluated.
4551 ;; So even though the compiler complains, keep them.
4552 (let* ((time-up (org-cmp-time a b))
4553 (time-down (if time-up (- time-up) nil))
4554 (priority-up (org-cmp-priority a b))
4555 (priority-down (if priority-up (- priority-up) nil))
4556 (effort-up (org-cmp-effort a b))
4557 (effort-down (if effort-up (- effort-up) nil))
4558 (category-up (org-cmp-category a b))
4559 (category-down (if category-up (- category-up) nil))
4560 (category-keep (if category-up +1 nil))
4561 (tag-up (org-cmp-tag a b))
4562 (tag-down (if tag-up (- tag-up) nil))
4563 (todo-state-up (org-cmp-todo-state a b))
4564 (todo-state-down (if todo-state-up (- todo-state-up) nil))
4565 user-defined-up user-defined-down)
4566 (if (and org-agenda-cmp-user-defined
4567 (functionp org-agenda-cmp-user-defined))
4568 (setq user-defined-up
4569 (funcall org-agenda-cmp-user-defined a b)
4570 user-defined-down (if user-defined-up (- user-defined-up) nil)))
4571 (cdr (assoc
4572 (eval (cons 'or org-agenda-sorting-strategy-selected))
4573 '((-1 . t) (1 . nil) (nil . nil))))))
4575 ;;; Agenda restriction lock
4577 (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
4578 "Overlay to mark the headline to which arenda commands are restricted.")
4579 (org-overlay-put org-agenda-restriction-lock-overlay
4580 'face 'org-agenda-restriction-lock)
4581 (org-overlay-put org-agenda-restriction-lock-overlay
4582 'help-echo "Agendas are currently limited to this subtree.")
4583 (org-detach-overlay org-agenda-restriction-lock-overlay)
4585 (defun org-agenda-set-restriction-lock (&optional type)
4586 "Set restriction lock for agenda, to current subtree or file.
4587 Restriction will be the file if TYPE is `file', or if type is the
4588 universal prefix '(4), or if the cursor is before the first headline
4589 in the file. Otherwise, restriction will be to the current subtree."
4590 (interactive "P")
4591 (and (equal type '(4)) (setq type 'file))
4592 (setq type (cond
4593 (type type)
4594 ((org-at-heading-p) 'subtree)
4595 ((condition-case nil (org-back-to-heading t) (error nil))
4596 'subtree)
4597 (t 'file)))
4598 (if (eq type 'subtree)
4599 (progn
4600 (setq org-agenda-restrict t)
4601 (setq org-agenda-overriding-restriction 'subtree)
4602 (put 'org-agenda-files 'org-restrict
4603 (list (buffer-file-name (buffer-base-buffer))))
4604 (org-back-to-heading t)
4605 (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
4606 (move-marker org-agenda-restrict-begin (point))
4607 (move-marker org-agenda-restrict-end
4608 (save-excursion (org-end-of-subtree t)))
4609 (message "Locking agenda restriction to subtree"))
4610 (put 'org-agenda-files 'org-restrict
4611 (list (buffer-file-name (buffer-base-buffer))))
4612 (setq org-agenda-restrict nil)
4613 (setq org-agenda-overriding-restriction 'file)
4614 (move-marker org-agenda-restrict-begin nil)
4615 (move-marker org-agenda-restrict-end nil)
4616 (message "Locking agenda restriction to file"))
4617 (setq current-prefix-arg nil)
4618 (org-agenda-maybe-redo))
4620 (defun org-agenda-remove-restriction-lock (&optional noupdate)
4621 "Remove the agenda restriction lock."
4622 (interactive "P")
4623 (org-detach-overlay org-agenda-restriction-lock-overlay)
4624 (org-detach-overlay org-speedbar-restriction-lock-overlay)
4625 (setq org-agenda-overriding-restriction nil)
4626 (setq org-agenda-restrict nil)
4627 (put 'org-agenda-files 'org-restrict nil)
4628 (move-marker org-agenda-restrict-begin nil)
4629 (move-marker org-agenda-restrict-end nil)
4630 (setq current-prefix-arg nil)
4631 (message "Agenda restriction lock removed")
4632 (or noupdate (org-agenda-maybe-redo)))
4634 (defun org-agenda-maybe-redo ()
4635 "If there is any window showing the agenda view, update it."
4636 (let ((w (get-buffer-window org-agenda-buffer-name t))
4637 (w0 (selected-window)))
4638 (when w
4639 (select-window w)
4640 (org-agenda-redo)
4641 (select-window w0)
4642 (if org-agenda-overriding-restriction
4643 (message "Agenda view shifted to new %s restriction"
4644 org-agenda-overriding-restriction)
4645 (message "Agenda restriction lock removed")))))
4647 ;;; Agenda commands
4649 (defun org-agenda-check-type (error &rest types)
4650 "Check if agenda buffer is of allowed type.
4651 If ERROR is non-nil, throw an error, otherwise just return nil."
4652 (if (memq org-agenda-type types)
4654 (if error
4655 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
4656 nil)))
4658 (defun org-agenda-quit ()
4659 "Exit agenda by removing the window or the buffer."
4660 (interactive)
4661 (if org-agenda-columns-active
4662 (org-columns-quit)
4663 (let ((buf (current-buffer)))
4664 (and (not (eq org-agenda-window-setup 'current-window))
4665 (not (one-window-p))
4666 (delete-window))
4667 (kill-buffer buf)
4668 (org-agenda-reset-markers)
4669 (org-columns-remove-overlays)
4670 (setq org-agenda-archives-mode nil))
4671 ;; Maybe restore the pre-agenda window configuration.
4672 (and org-agenda-restore-windows-after-quit
4673 (not (eq org-agenda-window-setup 'other-frame))
4674 org-pre-agenda-window-conf
4675 (set-window-configuration org-pre-agenda-window-conf))))
4677 (defun org-agenda-exit ()
4678 "Exit agenda by removing the window or the buffer.
4679 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
4680 Org-mode buffers visited directly by the user will not be touched."
4681 (interactive)
4682 (org-release-buffers org-agenda-new-buffers)
4683 (setq org-agenda-new-buffers nil)
4684 (org-agenda-quit))
4686 (defun org-agenda-execute (arg)
4687 "Execute another agenda command, keeping same window.\\<global-map>
4688 So this is just a shortcut for `\\[org-agenda]', available in the agenda."
4689 (interactive "P")
4690 (let ((org-agenda-window-setup 'current-window))
4691 (org-agenda arg)))
4693 (defun org-agenda-redo ()
4694 "Rebuild Agenda.
4695 When this is the global TODO list, a prefix argument will be interpreted."
4696 (interactive)
4697 (let* ((org-agenda-keep-modes t)
4698 (filter org-agenda-filter)
4699 (preset (get 'org-agenda-filter :preset-filter))
4700 (cols org-agenda-columns-active)
4701 (line (org-current-line))
4702 (window-line (- line (org-current-line (window-start))))
4703 (lprops (get 'org-agenda-redo-command 'org-lprops)))
4704 (put 'org-agenda-filter :preset-filter nil)
4705 (and cols (org-columns-quit))
4706 (message "Rebuilding agenda buffer...")
4707 (org-let lprops '(eval org-agenda-redo-command))
4708 (setq org-agenda-undo-list nil
4709 org-agenda-pending-undo-list nil)
4710 (message "Rebuilding agenda buffer...done")
4711 (put 'org-agenda-filter :preset-filter preset)
4712 (and (or filter preset) (org-agenda-filter-apply filter))
4713 (and cols (interactive-p) (org-agenda-columns))
4714 (goto-line line)
4715 (recenter window-line)))
4718 (defvar org-global-tags-completion-table nil)
4719 (defvar org-agenda-filter-form nil)
4720 (defun org-agenda-filter-by-tag (strip &optional char narrow)
4721 "Keep only those lines in the agenda buffer that have a specific tag.
4722 The tag is selected with its fast selection letter, as configured.
4723 With prefix argument STRIP, remove all lines that do have the tag.
4724 A lisp caller can specify CHAR. NARROW means that the new tag should be
4725 used to narrow the search - the interactive user can also press `-' or `+'
4726 to switch to narrowing."
4727 (interactive "P")
4728 (let* ((alist org-tag-alist-for-agenda)
4729 (tag-chars (mapconcat
4730 (lambda (x) (if (cdr x) (char-to-string (cdr x)) ""))
4731 alist ""))
4732 (efforts (org-split-string
4733 (or (cdr (assoc (concat org-effort-property "_ALL")
4734 org-global-properties))
4735 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00" "")))
4736 (effort-op org-agenda-filter-effort-default-operator)
4737 (effort-prompt "")
4738 (inhibit-read-only t)
4739 (current org-agenda-filter)
4740 char a n tag)
4741 (unless char
4742 (message
4743 "%s by tag [%s ], [TAB], [/]:off, [+-]:narrow, [>=<?]:effort: "
4744 (if narrow "Narrow" "Filter") tag-chars)
4745 (setq char (read-char)))
4746 (when (member char '(?+ ?-))
4747 ;; Narrowing down
4748 (cond ((equal char ?-) (setq strip t narrow t))
4749 ((equal char ?+) (setq strip nil narrow t)))
4750 (message
4751 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
4752 (setq char (read-char)))
4753 (when (member char '(?< ?> ?= ??))
4754 ;; An effort operator
4755 (setq effort-op (char-to-string char))
4756 (setq alist nil) ; to make sure it will be interpreted as effort.
4757 (unless (equal char ??)
4758 (loop for i from 0 to 9 do
4759 (setq effort-prompt
4760 (concat
4761 effort-prompt " ["
4762 (if (= i 9) "0" (int-to-string (1+ i)))
4763 "]" (nth i efforts))))
4764 (message "Effort%s: %s " effort-op effort-prompt)
4765 (setq char (read-char))
4766 (when (or (< char ?0) (> char ?9))
4767 (error "Need 1-9,0 to select effort" ))))
4768 (when (equal char ?\t)
4769 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
4770 (org-set-local 'org-global-tags-completion-table
4771 (org-global-tags-completion-table)))
4772 (let ((completion-ignore-case t))
4773 (setq tag (org-ido-completing-read
4774 "Tag: " org-global-tags-completion-table))))
4775 (cond
4776 ((equal char ?/)
4777 (org-agenda-filter-by-tag-show-all)
4778 (when (get 'org-agenda-filter :preset-filter)
4779 (org-agenda-filter-apply org-agenda-filter)))
4780 ((or (equal char ?\ )
4781 (setq a (rassoc char alist))
4782 (and (>= char ?0) (<= char ?9)
4783 (setq n (if (= char ?0) 9 (- char ?0 1))
4784 tag (concat effort-op (nth n efforts))
4785 a (cons tag nil)))
4786 (and (= char ??)
4787 (setq tag "?eff")
4788 a (cons tag nil))
4789 (and tag (setq a (cons tag nil))))
4790 (org-agenda-filter-by-tag-show-all)
4791 (setq tag (car a))
4792 (setq org-agenda-filter
4793 (cons (concat (if strip "-" "+") tag)
4794 (if narrow current nil)))
4795 (org-agenda-filter-apply org-agenda-filter))
4796 (t (error "Invalid tag selection character %c" char)))))
4798 (defun org-agenda-filter-by-tag-refine (strip &optional char)
4799 "Refine the current filter. See `org-agenda-filter-by-tag."
4800 (interactive "P")
4801 (org-agenda-filter-by-tag strip char 'refine))
4803 (defun org-agenda-filter-make-matcher ()
4804 "Create the form that tests a line for the agenda filter."
4805 (let (f f1)
4806 (dolist (x (append (get 'org-agenda-filter :preset-filter)
4807 org-agenda-filter))
4808 (if (member x '("-" "+"))
4809 (setq f1 '(not tags))
4810 (if (string-match "[<=>?]" x)
4811 (setq f1 (org-agenda-filter-effort-form x))
4812 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
4813 (if (equal (string-to-char x) ?-)
4814 (setq f1 (list 'not f1))))
4815 (push f1 f))
4816 (cons 'and (nreverse f))))
4818 (defun org-agenda-filter-effort-form (e)
4819 "Return the form to compare the effort of the current line with what E says.
4820 E looks line \"+<2:25\"."
4821 (let (op)
4822 (setq e (substring e 1))
4823 (setq op (string-to-char e) e (substring e 1))
4824 (setq op (cond ((equal op ?<) '<=)
4825 ((equal op ?>) '>=)
4826 ((equal op ??) op)
4827 (t '=)))
4828 (list 'org-agenda-compare-effort (list 'quote op)
4829 (org-hh:mm-string-to-minutes e))))
4831 (defun org-agenda-compare-effort (op value)
4832 "Compare the effort of the current line with VALUE, using OP.
4833 If the line does not have an effort defined, return nil."
4834 (let ((eff (get-text-property (point) 'effort-minutes)))
4835 (if (equal op ??)
4836 (not eff)
4837 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
4838 value))))
4840 (defun org-agenda-filter-apply (filter)
4841 "Set FILTER as the new agenda filter and apply it."
4842 (let (tags)
4843 (setq org-agenda-filter filter
4844 org-agenda-filter-form (org-agenda-filter-make-matcher))
4845 (org-agenda-set-mode-name)
4846 (save-excursion
4847 (goto-char (point-min))
4848 (while (not (eobp))
4849 (if (get-text-property (point) 'org-marker)
4850 (progn
4851 (setq tags (get-text-property (point) 'tags)) ; used in eval
4852 (if (not (eval org-agenda-filter-form))
4853 (org-agenda-filter-by-tag-hide-line))
4854 (beginning-of-line 2))
4855 (beginning-of-line 2))))))
4857 (defun org-agenda-filter-by-tag-hide-line ()
4858 (let (ov)
4859 (setq ov (org-make-overlay (max (point-min) (1- (point-at-bol)))
4860 (point-at-eol)))
4861 (org-overlay-put ov 'invisible t)
4862 (org-overlay-put ov 'type 'tags-filter)
4863 (push ov org-agenda-filter-overlays)))
4865 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
4866 (setq pos (or pos (point)))
4867 (save-excursion
4868 (dolist (ov (org-overlays-at pos))
4869 (when (and (org-overlay-get ov 'invisible)
4870 (eq (org-overlay-get ov 'type) 'tags-filter))
4871 (goto-char pos)
4872 (if (< (org-overlay-start ov) (point-at-eol))
4873 (org-move-overlay ov (point-at-eol)
4874 (org-overlay-end ov)))))))
4876 (defun org-agenda-filter-by-tag-show-all ()
4877 (mapc 'org-delete-overlay org-agenda-filter-overlays)
4878 (setq org-agenda-filter-overlays nil)
4879 (setq org-agenda-filter nil)
4880 (setq org-agenda-filter-form nil)
4881 (org-agenda-set-mode-name))
4883 (defun org-agenda-manipulate-query-add ()
4884 "Manipulate the query by adding a search term with positive selection.
4885 Positive selection means, the term must be matched for selection of an entry."
4886 (interactive)
4887 (org-agenda-manipulate-query ?\[))
4888 (defun org-agenda-manipulate-query-subtract ()
4889 "Manipulate the query by adding a search term with negative selection.
4890 Negative selection means, term must not be matched for selection of an entry."
4891 (interactive)
4892 (org-agenda-manipulate-query ?\]))
4893 (defun org-agenda-manipulate-query-add-re ()
4894 "Manipulate the query by adding a search regexp with positive selection.
4895 Positive selection means, the regexp must match for selection of an entry."
4896 (interactive)
4897 (org-agenda-manipulate-query ?\{))
4898 (defun org-agenda-manipulate-query-subtract-re ()
4899 "Manipulate the query by adding a search regexp with negative selection.
4900 Negative selection means, regexp must not match for selection of an entry."
4901 (interactive)
4902 (org-agenda-manipulate-query ?\}))
4903 (defun org-agenda-manipulate-query (char)
4904 (cond
4905 ((memq org-agenda-type '(timeline agenda))
4906 (if (y-or-n-p "Re-display with inactive time stamps included? ")
4907 (let ((org-agenda-include-inactive-timestamps t))
4908 (org-agenda-redo))
4909 (error "Abort")))
4910 ((eq org-agenda-type 'search)
4911 (org-add-to-string
4912 'org-agenda-query-string
4913 (cdr (assoc char '((?\[ . " +") (?\] . " -")
4914 (?\{ . " +{}") (?\} . " -{}")))))
4915 (setq org-agenda-redo-command
4916 (list 'org-search-view
4917 org-todo-only
4918 org-agenda-query-string
4919 (+ (length org-agenda-query-string)
4920 (if (member char '(?\{ ?\})) 0 1))))
4921 (set-register org-agenda-query-register org-agenda-query-string)
4922 (org-agenda-redo))
4923 (t (error "Cannot manipulate query for %s-type agenda buffers"
4924 org-agenda-type))))
4926 (defun org-add-to-string (var string)
4927 (set var (concat (symbol-value var) string)))
4929 (defun org-agenda-goto-date (date)
4930 "Jump to DATE in agenda."
4931 (interactive (list (org-read-date)))
4932 (org-agenda-list nil date))
4934 (defun org-agenda-goto-today ()
4935 "Go to today."
4936 (interactive)
4937 (org-agenda-check-type t 'timeline 'agenda)
4938 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
4939 (cond
4940 (tdpos (goto-char tdpos))
4941 ((eq org-agenda-type 'agenda)
4942 (let* ((sd (time-to-days
4943 (time-subtract (current-time)
4944 (list 0 (* 3600 org-extend-today-until) 0))))
4945 (comp (org-agenda-compute-time-span sd org-agenda-span))
4946 (org-agenda-overriding-arguments org-agenda-last-arguments))
4947 (setf (nth 1 org-agenda-overriding-arguments) (car comp))
4948 (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
4949 (org-agenda-redo)
4950 (org-agenda-find-same-or-today-or-agenda)))
4951 (t (error "Cannot find today")))))
4953 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
4954 (goto-char
4955 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
4956 (text-property-any (point-min) (point-max) 'org-today t)
4957 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
4958 (point-min))))
4960 (defun org-agenda-later (arg)
4961 "Go forward in time by thee current span.
4962 With prefix ARG, go forward that many times the current span."
4963 (interactive "p")
4964 (org-agenda-check-type t 'agenda)
4965 (let* ((span org-agenda-span)
4966 (sd org-starting-day)
4967 (greg (calendar-gregorian-from-absolute sd))
4968 (cnt (get-text-property (point) 'org-day-cnt))
4969 greg2 nd)
4970 (cond
4971 ((eq span 'day)
4972 (setq sd (+ arg sd) nd 1))
4973 ((eq span 'week)
4974 (setq sd (+ (* 7 arg) sd) nd 7))
4975 ((eq span 'month)
4976 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
4977 sd (calendar-absolute-from-gregorian greg2))
4978 (setcar greg2 (1+ (car greg2)))
4979 (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
4980 ((eq span 'year)
4981 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
4982 sd (calendar-absolute-from-gregorian greg2))
4983 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
4984 (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
4985 (let ((org-agenda-overriding-arguments
4986 (list (car org-agenda-last-arguments) sd nd t)))
4987 (org-agenda-redo)
4988 (org-agenda-find-same-or-today-or-agenda cnt))))
4990 (defun org-agenda-earlier (arg)
4991 "Go backward in time by the current span.
4992 With prefix ARG, go backward that many times the current span."
4993 (interactive "p")
4994 (org-agenda-later (- arg)))
4996 (defun org-agenda-view-mode-dispatch ()
4997 "Call one of the view mode commands."
4998 (interactive)
4999 (message "View: [d]ay [w]eek [m]onth [y]ear [l]og [L]og-all [a]rch-trees [A]rch-files
5000 clock[R]eport time[G]rid include[D]iary")
5001 (let ((a (read-char-exclusive)))
5002 (case a
5003 (?d (call-interactively 'org-agenda-day-view))
5004 (?w (call-interactively 'org-agenda-week-view))
5005 (?m (call-interactively 'org-agenda-month-view))
5006 (?y (call-interactively 'org-agenda-year-view))
5007 (?l (call-interactively 'org-agenda-log-mode))
5008 (?a (call-interactively 'org-agenda-archives-mode))
5009 (?A (org-agenda-archives-mode 'files))
5010 (?R (call-interactively 'org-agenda-clockreport-mode))
5011 (?G (call-interactively 'org-agenda-toggle-time-grid))
5012 (?D (call-interactively 'org-agenda-toggle-diary))
5013 (?q (message "Abort"))
5014 (otherwise (error "Invalid key" )))))
5016 (defun org-agenda-day-view (&optional day-of-year)
5017 "Switch to daily view for agenda.
5018 With argument DAY-OF-YEAR, switch to that day of the year."
5019 (interactive "P")
5020 (setq org-agenda-ndays 1)
5021 (org-agenda-change-time-span 'day day-of-year))
5022 (defun org-agenda-week-view (&optional iso-week)
5023 "Switch to daily view for agenda.
5024 With argument ISO-WEEK, switch to the corresponding ISO week.
5025 If ISO-WEEK has more then 2 digits, only the last two encode the
5026 week. Any digits before this encode a year. So 200712 means
5027 week 12 of year 2007. Years in the range 1938-2037 can also be
5028 written as 2-digit years."
5029 (interactive "P")
5030 (setq org-agenda-ndays 7)
5031 (org-agenda-change-time-span 'week iso-week))
5032 (defun org-agenda-month-view (&optional month)
5033 "Switch to monthly view for agenda.
5034 With argument MONTH, switch to that month."
5035 (interactive "P")
5036 (org-agenda-change-time-span 'month month))
5037 (defun org-agenda-year-view (&optional year)
5038 "Switch to yearly view for agenda.
5039 With argument YEAR, switch to that year.
5040 If MONTH has more then 2 digits, only the last two encode the
5041 month. Any digits before this encode a year. So 200712 means
5042 December year 2007. Years in the range 1938-2037 can also be
5043 written as 2-digit years."
5044 (interactive "P")
5045 (when year
5046 (setq year (org-small-year-to-year year)))
5047 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
5048 (org-agenda-change-time-span 'year year)
5049 (error "Abort")))
5051 (defun org-agenda-change-time-span (span &optional n)
5052 "Change the agenda view to SPAN.
5053 SPAN may be `day', `week', `month', `year'."
5054 (org-agenda-check-type t 'agenda)
5055 (if (and (not n) (equal org-agenda-span span))
5056 (error "Viewing span is already \"%s\"" span))
5057 (let* ((sd (or (get-text-property (point) 'day)
5058 org-starting-day))
5059 (computed (org-agenda-compute-time-span sd span n))
5060 (org-agenda-overriding-arguments
5061 (list (car org-agenda-last-arguments)
5062 (car computed) (cdr computed) t)))
5063 (org-agenda-redo)
5064 (org-agenda-find-same-or-today-or-agenda))
5065 (org-agenda-set-mode-name)
5066 (message "Switched to %s view" span))
5068 (defun org-agenda-compute-time-span (sd span &optional n)
5069 "Compute starting date and number of days for agenda.
5070 SPAN may be `day', `week', `month', `year'. The return value
5071 is a cons cell with the starting date and the number of days,
5072 so that the date SD will be in that range."
5073 (let* ((greg (calendar-gregorian-from-absolute sd))
5074 (dg (nth 1 greg))
5075 (mg (car greg))
5076 (yg (nth 2 greg))
5077 nd w1 y1 m1 thisweek)
5078 (cond
5079 ((eq span 'day)
5080 (when n
5081 (setq sd (+ (calendar-absolute-from-gregorian
5082 (list mg 1 yg))
5083 n -1)))
5084 (setq nd 1))
5085 ((eq span 'week)
5086 (let* ((nt (calendar-day-of-week
5087 (calendar-gregorian-from-absolute sd)))
5088 (d (if org-agenda-start-on-weekday
5089 (- nt org-agenda-start-on-weekday)
5090 0)))
5091 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
5092 (when n
5093 (require 'cal-iso)
5094 (setq thisweek (car (calendar-iso-from-absolute sd)))
5095 (when (> n 99)
5096 (setq y1 (org-small-year-to-year (/ n 100))
5097 n (mod n 100)))
5098 (setq sd
5099 (calendar-absolute-from-iso
5100 (list n 1
5101 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))
5102 (setq nd 7)))
5103 ((eq span 'month)
5104 (when (and n (> n 99))
5105 (setq y1 (org-small-year-to-year (/ n 100))
5106 n (mod n 100)))
5107 (setq sd (calendar-absolute-from-gregorian
5108 (list (or n mg) 1 (or y1 yg)))
5109 nd (- (calendar-absolute-from-gregorian
5110 (list (1+ (or n mg)) 1 (or y1 yg)))
5111 sd)))
5112 ((eq span 'year)
5113 (setq sd (calendar-absolute-from-gregorian
5114 (list 1 1 (or n yg)))
5115 nd (- (calendar-absolute-from-gregorian
5116 (list 1 1 (1+ (or n yg))))
5117 sd))))
5118 (cons sd nd)))
5120 (defun org-agenda-next-date-line (&optional arg)
5121 "Jump to the next line indicating a date in agenda buffer."
5122 (interactive "p")
5123 (org-agenda-check-type t 'agenda 'timeline)
5124 (beginning-of-line 1)
5125 ;; This does not work if user makes date format that starts with a blank
5126 (if (looking-at "^\\S-") (forward-char 1))
5127 (if (not (re-search-forward "^\\S-" nil t arg))
5128 (progn
5129 (backward-char 1)
5130 (error "No next date after this line in this buffer")))
5131 (goto-char (match-beginning 0)))
5133 (defun org-agenda-previous-date-line (&optional arg)
5134 "Jump to the previous line indicating a date in agenda buffer."
5135 (interactive "p")
5136 (org-agenda-check-type t 'agenda 'timeline)
5137 (beginning-of-line 1)
5138 (if (not (re-search-backward "^\\S-" nil t arg))
5139 (error "No previous date before this line in this buffer")))
5141 ;; Initialize the highlight
5142 (defvar org-hl (org-make-overlay 1 1))
5143 (org-overlay-put org-hl 'face 'highlight)
5145 (defun org-highlight (begin end &optional buffer)
5146 "Highlight a region with overlay."
5147 (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
5148 org-hl begin end (or buffer (current-buffer))))
5150 (defun org-unhighlight ()
5151 "Detach overlay INDEX."
5152 (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
5154 ;; FIXME this is currently not used.
5155 (defun org-highlight-until-next-command (beg end &optional buffer)
5156 "Move the highlight overlay to BEG/END, remove it before the next command."
5157 (org-highlight beg end buffer)
5158 (add-hook 'pre-command-hook 'org-unhighlight-once))
5159 (defun org-unhighlight-once ()
5160 "Remove the highlight from its position, and this function from the hook."
5161 (remove-hook 'pre-command-hook 'org-unhighlight-once)
5162 (org-unhighlight))
5164 (defun org-agenda-follow-mode ()
5165 "Toggle follow mode in an agenda buffer."
5166 (interactive)
5167 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
5168 (org-agenda-set-mode-name)
5169 (message "Follow mode is %s"
5170 (if org-agenda-follow-mode "on" "off")))
5172 (defun org-agenda-clockreport-mode ()
5173 "Toggle clocktable mode in an agenda buffer."
5174 (interactive)
5175 (org-agenda-check-type t 'agenda)
5176 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
5177 (org-agenda-set-mode-name)
5178 (org-agenda-redo)
5179 (message "Clocktable mode is %s"
5180 (if org-agenda-clockreport-mode "on" "off")))
5182 (defun org-agenda-log-mode (&optional special)
5183 "Toggle log mode in an agenda buffer.
5184 With argument SPECIAL, show all possible log items, not only the ones
5185 configured in `org-agenda-log-mode-items'.
5186 With a double `C-u' prefix arg, show *only* log items, nothing else."
5187 (interactive "P")
5188 (org-agenda-check-type t 'agenda 'timeline)
5189 (setq org-agenda-show-log
5190 (if (equal special '(16))
5191 'only
5192 (if special '(closed clock state)
5193 (not org-agenda-show-log))))
5194 (org-agenda-set-mode-name)
5195 (org-agenda-redo)
5196 (message "Log mode is %s"
5197 (if org-agenda-show-log "on" "off")))
5199 (defun org-agenda-archives-mode (&optional with-files)
5200 "Toggle inclusion of items in trees marked with :ARCHIVE:.
5201 When called with a prefix argument, include all archive files as well."
5202 (interactive "P")
5203 (setq org-agenda-archives-mode
5204 (if with-files t (if org-agenda-archives-mode nil 'trees)))
5205 (org-agenda-set-mode-name)
5206 (org-agenda-redo)
5207 (message
5208 "%s"
5209 (cond
5210 ((eq org-agenda-archives-mode nil)
5211 "No archives are included")
5212 ((eq org-agenda-archives-mode 'trees)
5213 (format "Trees with :%s: tag are included" org-archive-tag))
5214 ((eq org-agenda-archives-mode t)
5215 (format "Trees with :%s: tag and all active archive files are included"
5216 org-archive-tag)))))
5218 (defun org-agenda-toggle-diary ()
5219 "Toggle diary inclusion in an agenda buffer."
5220 (interactive)
5221 (org-agenda-check-type t 'agenda)
5222 (setq org-agenda-include-diary (not org-agenda-include-diary))
5223 (org-agenda-redo)
5224 (org-agenda-set-mode-name)
5225 (message "Diary inclusion turned %s"
5226 (if org-agenda-include-diary "on" "off")))
5228 (defun org-agenda-toggle-time-grid ()
5229 "Toggle time grid in an agenda buffer."
5230 (interactive)
5231 (org-agenda-check-type t 'agenda)
5232 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
5233 (org-agenda-redo)
5234 (org-agenda-set-mode-name)
5235 (message "Time-grid turned %s"
5236 (if org-agenda-use-time-grid "on" "off")))
5238 (defun org-agenda-set-mode-name ()
5239 "Set the mode name to indicate all the small mode settings."
5240 (setq mode-name
5241 (concat "Org-Agenda"
5242 (if (equal org-agenda-ndays 1) " Day" "")
5243 (if (equal org-agenda-ndays 7) " Week" "")
5244 (if org-agenda-follow-mode " Follow" "")
5245 (if org-agenda-include-diary " Diary" "")
5246 (if org-agenda-use-time-grid " Grid" "")
5247 (if (consp org-agenda-show-log) " LogAll"
5248 (if org-agenda-show-log " Log" ""))
5249 (if (or org-agenda-filter (get 'org-agenda-filter
5250 :preset-filter))
5251 (concat " {" (mapconcat
5252 'identity
5253 (append (get 'org-agenda-filter
5254 :preset-filter)
5255 org-agenda-filter) "") "}")
5257 (if org-agenda-archives-mode
5258 (if (eq org-agenda-archives-mode t)
5259 " Archives"
5260 (format " :%s:" org-archive-tag))
5262 (if org-agenda-clockreport-mode " Clock" "")))
5263 (force-mode-line-update))
5265 (defun org-agenda-post-command-hook ()
5266 (and (eolp) (not (bolp)) (backward-char 1))
5267 (setq org-agenda-type
5268 (or (get-text-property (point) 'org-agenda-type)
5269 (get-text-property (max (point-min) (1- (point)))
5270 'org-agenda-type)))
5271 (if (and org-agenda-follow-mode
5272 (get-text-property (point) 'org-marker))
5273 (org-agenda-show)))
5275 (defun org-agenda-show-priority ()
5276 "Show the priority of the current item.
5277 This priority is composed of the main priority given with the [#A] cookies,
5278 and by additional input from the age of a schedules or deadline entry."
5279 (interactive)
5280 (let* ((pri (get-text-property (point-at-bol) 'priority)))
5281 (message "Priority is %d" (if pri pri -1000))))
5283 (defun org-agenda-show-tags ()
5284 "Show the tags applicable to the current item."
5285 (interactive)
5286 (let* ((tags (get-text-property (point-at-bol) 'tags)))
5287 (if tags
5288 (message "Tags are :%s:"
5289 (org-no-properties (mapconcat 'identity tags ":")))
5290 (message "No tags associated with this line"))))
5292 (defun org-agenda-goto (&optional highlight)
5293 "Go to the Org-mode file which contains the item at point."
5294 (interactive)
5295 (let* ((marker (or (get-text-property (point) 'org-marker)
5296 (org-agenda-error)))
5297 (buffer (marker-buffer marker))
5298 (pos (marker-position marker)))
5299 (switch-to-buffer-other-window buffer)
5300 (widen)
5301 (goto-char pos)
5302 (when (org-mode-p)
5303 (org-show-context 'agenda)
5304 (save-excursion
5305 (and (outline-next-heading)
5306 (org-flag-heading nil)))) ; show the next heading
5307 (recenter (/ (window-height) 2))
5308 (run-hooks 'org-agenda-after-show-hook)
5309 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
5311 (defvar org-agenda-after-show-hook nil
5312 "Normal hook run after an item has been shown from the agenda.
5313 Point is in the buffer where the item originated.")
5315 (defun org-agenda-kill ()
5316 "Kill the entry or subtree belonging to the current agenda entry."
5317 (interactive)
5318 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5319 (let* ((marker (or (get-text-property (point) 'org-marker)
5320 (org-agenda-error)))
5321 (buffer (marker-buffer marker))
5322 (pos (marker-position marker))
5323 (type (get-text-property (point) 'type))
5324 dbeg dend (n 0) conf)
5325 (org-with-remote-undo buffer
5326 (with-current-buffer buffer
5327 (save-excursion
5328 (goto-char pos)
5329 (if (and (org-mode-p) (not (member type '("sexp"))))
5330 (setq dbeg (progn (org-back-to-heading t) (point))
5331 dend (org-end-of-subtree t t))
5332 (setq dbeg (point-at-bol)
5333 dend (min (point-max) (1+ (point-at-eol)))))
5334 (goto-char dbeg)
5335 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
5336 (setq conf (or (eq t org-agenda-confirm-kill)
5337 (and (numberp org-agenda-confirm-kill)
5338 (> n org-agenda-confirm-kill))))
5339 (and conf
5340 (not (y-or-n-p
5341 (format "Delete entry with %d lines in buffer \"%s\"? "
5342 n (buffer-name buffer))))
5343 (error "Abort"))
5344 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
5345 (with-current-buffer buffer (delete-region dbeg dend))
5346 (message "Agenda item and source killed"))))
5348 (defun org-agenda-archive ()
5349 "Archive the entry or subtree belonging to the current agenda entry."
5350 (interactive)
5351 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5352 (let* ((marker (or (get-text-property (point) 'org-marker)
5353 (org-agenda-error)))
5354 (buffer (marker-buffer marker))
5355 (pos (marker-position marker)))
5356 (org-with-remote-undo buffer
5357 (with-current-buffer buffer
5358 (if (org-mode-p)
5359 (save-excursion
5360 (goto-char pos)
5361 (org-remove-subtree-entries-from-agenda)
5362 (org-back-to-heading t)
5363 (org-archive-subtree))
5364 (error "Archiving works only in Org-mode files"))))))
5366 (defun org-agenda-archive-to-archive-sibling ()
5367 "Move the entry to the archive sibling."
5368 (interactive)
5369 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5370 (let* ((marker (or (get-text-property (point) 'org-marker)
5371 (org-agenda-error)))
5372 (buffer (marker-buffer marker))
5373 (pos (marker-position marker)))
5374 (org-with-remote-undo buffer
5375 (with-current-buffer buffer
5376 (if (org-mode-p)
5377 (save-excursion
5378 (goto-char pos)
5379 (org-remove-subtree-entries-from-agenda)
5380 (org-back-to-heading t)
5381 (org-archive-to-archive-sibling))
5382 (error "Archiving works only in Org-mode files"))))))
5384 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
5385 "Remove all lines in the agenda that correspond to a given subtree.
5386 The subtree is the one in buffer BUF, starting at BEG and ending at END.
5387 If this information is not given, the function uses the tree at point."
5388 (let ((buf (or buf (current-buffer))) m p)
5389 (save-excursion
5390 (unless (and beg end)
5391 (org-back-to-heading t)
5392 (setq beg (point))
5393 (org-end-of-subtree t)
5394 (setq end (point)))
5395 (set-buffer (get-buffer org-agenda-buffer-name))
5396 (save-excursion
5397 (goto-char (point-max))
5398 (beginning-of-line 1)
5399 (while (not (bobp))
5400 (when (and (setq m (get-text-property (point) 'org-marker))
5401 (equal buf (marker-buffer m))
5402 (setq p (marker-position m))
5403 (>= p beg)
5404 (< p end))
5405 (let ((inhibit-read-only t))
5406 (delete-region (point-at-bol) (1+ (point-at-eol)))))
5407 (beginning-of-line 0))))))
5409 (defun org-agenda-refile (&optional goto rfloc)
5410 "Refile the item at point."
5411 (interactive "P")
5412 (let* ((marker (or (get-text-property (point) 'org-hd-marker)
5413 (org-agenda-error)))
5414 (buffer (marker-buffer marker))
5415 (pos (marker-position marker))
5416 (rfloc (or rfloc
5417 (org-refile-get-location
5418 (if goto "Goto: " "Refile to: ") buffer
5419 org-refile-allow-creating-parent-nodes))))
5420 (with-current-buffer buffer
5421 (save-excursion
5422 (save-restriction
5423 (widen)
5424 (goto-char marker)
5425 (org-remove-subtree-entries-from-agenda)
5426 (org-refile goto buffer rfloc))))))
5431 (defun org-agenda-open-link ()
5432 "Follow the link in the current line, if any."
5433 (interactive)
5434 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
5435 (save-excursion
5436 (save-restriction
5437 (narrow-to-region (point-at-bol) (point-at-eol))
5438 (org-open-at-point))))
5440 (defun org-agenda-copy-local-variable (var)
5441 "Get a variable from a referenced buffer and install it here."
5442 (let ((m (get-text-property (point) 'org-marker)))
5443 (when (and m (buffer-live-p (marker-buffer m)))
5444 (org-set-local var (with-current-buffer (marker-buffer m)
5445 (symbol-value var))))))
5447 (defun org-agenda-switch-to (&optional delete-other-windows)
5448 "Go to the Org-mode file which contains the item at point."
5449 (interactive)
5450 (let* ((marker (or (get-text-property (point) 'org-marker)
5451 (org-agenda-error)))
5452 (buffer (marker-buffer marker))
5453 (pos (marker-position marker)))
5454 (switch-to-buffer buffer)
5455 (and delete-other-windows (delete-other-windows))
5456 (widen)
5457 (goto-char pos)
5458 (when (org-mode-p)
5459 (org-show-context 'agenda)
5460 (save-excursion
5461 (and (outline-next-heading)
5462 (org-flag-heading nil)))))) ; show the next heading
5464 (defun org-agenda-goto-mouse (ev)
5465 "Go to the Org-mode file which contains the item at the mouse click."
5466 (interactive "e")
5467 (mouse-set-point ev)
5468 (org-agenda-goto))
5470 (defun org-agenda-show (&optional full-entry)
5471 "Display the Org-mode file which contains the item at point.
5472 With prefix argument FULL-ENTRY, make the entire entry visible
5473 if it was hidden in the outline."
5474 (interactive "P")
5475 (let ((win (selected-window)))
5476 (if full-entry
5477 (let ((org-show-entry-below t))
5478 (org-agenda-goto t))
5479 (org-agenda-goto t))
5480 (select-window win)))
5482 (defun org-agenda-show-1 (&optional more)
5483 "Display the Org-mode file which contains the item at point.
5484 The prefix arg causes further revieling:
5486 0 hide the subtree
5487 1 just show the entry according to defaults.
5488 2 show the text below the heading
5489 3 show the entire subtree
5490 4 show the entire subtree and any LOGBOOK drawers
5491 5 show the entire subtree and any drawers
5492 With prefix argument FULL-ENTRY, make the entire entry visible
5493 if it was hidden in the outline."
5494 (interactive "p")
5495 (let ((win (selected-window)))
5496 (org-agenda-goto t)
5497 (org-recenter-heading 1)
5498 (cond
5499 ((= more 0)
5500 (hide-subtree)
5501 (message "Remote: hide subtree"))
5502 ((and (interactive-p) (= more 1))
5503 (message "Remote: show with default settings"))
5504 ((= more 2)
5505 (show-entry)
5506 (save-excursion
5507 (org-back-to-heading)
5508 (org-cycle-hide-drawers 'children))
5509 (message "Remote: show entry"))
5510 ((= more 3)
5511 (show-subtree)
5512 (save-excursion
5513 (org-back-to-heading)
5514 (org-cycle-hide-drawers 'subtree))
5515 (message "Remote: show subtree"))
5516 ((= more 4)
5517 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
5518 (org-drawer-regexp
5519 (concat "^[ \t]*:\\("
5520 (mapconcat 'regexp-quote org-drawers "\\|")
5521 "\\):[ \t]*$")))
5522 (show-subtree)
5523 (save-excursion
5524 (org-back-to-heading)
5525 (org-cycle-hide-drawers 'subtree)))
5526 (message "Remote: show subtree and LOGBOOK"))
5527 ((> more 4)
5528 (show-subtree)
5529 (message "Remote: show subtree and LOGBOOK")))
5530 (select-window win)))
5532 (defun org-recenter-heading (n)
5533 (save-excursion
5534 (org-back-to-heading)
5535 (recenter n)))
5537 (defvar org-agenda-cycle-counter nil)
5538 (defun org-agenda-cycle-show (n)
5539 "Show the current entry in another window, with default settings.
5540 Default settings are taken from `org-show-hierarchy-above' and siblings.
5541 When use repeadedly in immediate succession, the remote entry will cycle
5542 through visibility
5544 entry -> subtree -> subtree with logbook"
5545 (interactive "p")
5546 (when (and (= n 1)
5547 (not (eq last-command this-command)))
5548 (setq org-agenda-cycle-counter 0))
5549 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
5550 (if (> org-agenda-cycle-counter 4)
5551 (setq org-agenda-cycle-counter 0))
5552 (org-agenda-show-1 org-agenda-cycle-counter))
5554 (defun org-agenda-recenter (arg)
5555 "Display the Org-mode file which contains the item at point and recenter."
5556 (interactive "P")
5557 (let ((win (selected-window)))
5558 (org-agenda-goto t)
5559 (recenter arg)
5560 (select-window win)))
5562 (defun org-agenda-show-mouse (ev)
5563 "Display the Org-mode file which contains the item at the mouse click."
5564 (interactive "e")
5565 (mouse-set-point ev)
5566 (org-agenda-show))
5568 (defun org-agenda-check-no-diary ()
5569 "Check if the entry is a diary link and abort if yes."
5570 (if (get-text-property (point) 'org-agenda-diary-link)
5571 (org-agenda-error)))
5573 (defun org-agenda-error ()
5574 (error "Command not allowed in this line"))
5576 (defun org-agenda-tree-to-indirect-buffer ()
5577 "Show the subtree corresponding to the current entry in an indirect buffer.
5578 This calls the command `org-tree-to-indirect-buffer' from the original
5579 Org-mode buffer.
5580 With numerical prefix arg ARG, go up to this level and then take that tree.
5581 With a C-u prefix, make a separate frame for this tree (i.e. don't use the
5582 dedicated frame)."
5583 (interactive)
5584 (org-agenda-check-no-diary)
5585 (let* ((marker (or (get-text-property (point) 'org-marker)
5586 (org-agenda-error)))
5587 (buffer (marker-buffer marker))
5588 (pos (marker-position marker)))
5589 (with-current-buffer buffer
5590 (save-excursion
5591 (goto-char pos)
5592 (call-interactively 'org-tree-to-indirect-buffer)))))
5594 (defvar org-last-heading-marker (make-marker)
5595 "Marker pointing to the headline that last changed its TODO state
5596 by a remote command from the agenda.")
5598 (defun org-agenda-todo-nextset ()
5599 "Switch TODO entry to next sequence."
5600 (interactive)
5601 (org-agenda-todo 'nextset))
5603 (defun org-agenda-todo-previousset ()
5604 "Switch TODO entry to previous sequence."
5605 (interactive)
5606 (org-agenda-todo 'previousset))
5608 (defun org-agenda-todo (&optional arg)
5609 "Cycle TODO state of line at point, also in Org-mode file.
5610 This changes the line at point, all other lines in the agenda referring to
5611 the same tree node, and the headline of the tree node in the Org-mode file."
5612 (interactive "P")
5613 (org-agenda-check-no-diary)
5614 (let* ((col (current-column))
5615 (marker (or (get-text-property (point) 'org-marker)
5616 (org-agenda-error)))
5617 (buffer (marker-buffer marker))
5618 (pos (marker-position marker))
5619 (hdmarker (get-text-property (point) 'org-hd-marker))
5620 (todayp (equal (get-text-property (point) 'day)
5621 (time-to-days (current-time))))
5622 (inhibit-read-only t)
5623 org-agenda-headline-snapshot-before-repeat newhead just-one)
5624 (org-with-remote-undo buffer
5625 (with-current-buffer buffer
5626 (widen)
5627 (goto-char pos)
5628 (org-show-context 'agenda)
5629 (save-excursion
5630 (and (outline-next-heading)
5631 (org-flag-heading nil))) ; show the next heading
5632 (let ((current-prefix-arg arg))
5633 (call-interactively 'org-todo))
5634 (and (bolp) (forward-char 1))
5635 (setq newhead (org-get-heading))
5636 (when (and (org-bound-and-true-p
5637 org-agenda-headline-snapshot-before-repeat)
5638 (not (equal org-agenda-headline-snapshot-before-repeat
5639 newhead))
5640 todayp)
5641 (setq newhead org-agenda-headline-snapshot-before-repeat
5642 just-one t))
5643 (save-excursion
5644 (org-back-to-heading)
5645 (move-marker org-last-heading-marker (point))))
5646 (beginning-of-line 1)
5647 (save-excursion
5648 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
5649 (org-move-to-column col))))
5651 (defun org-agenda-add-note (&optional arg)
5652 "Add a time-stamped note to the entry at point."
5653 (interactive "P")
5654 (org-agenda-check-no-diary)
5655 (let* ((marker (or (get-text-property (point) 'org-marker)
5656 (org-agenda-error)))
5657 (buffer (marker-buffer marker))
5658 (pos (marker-position marker))
5659 (hdmarker (get-text-property (point) 'org-hd-marker))
5660 (inhibit-read-only t))
5661 (with-current-buffer buffer
5662 (widen)
5663 (goto-char pos)
5664 (org-show-context 'agenda)
5665 (save-excursion
5666 (and (outline-next-heading)
5667 (org-flag-heading nil))) ; show the next heading
5668 (org-add-note))))
5670 (defun org-agenda-change-all-lines (newhead hdmarker
5671 &optional fixface just-this)
5672 "Change all lines in the agenda buffer which match HDMARKER.
5673 The new content of the line will be NEWHEAD (as modified by
5674 `org-format-agenda-item'). HDMARKER is checked with
5675 `equal' against all `org-hd-marker' text properties in the file.
5676 If FIXFACE is non-nil, the face of each item is modified according to
5677 the new TODO state.
5678 If JUST-THIS is non-nil, change just the current line, not all.
5679 If FORCE-TAGS is non nil, the car of it returns the new tags."
5680 (let* ((inhibit-read-only t)
5681 (line (org-current-line))
5682 (thetags (with-current-buffer (marker-buffer hdmarker)
5683 (save-excursion (save-restriction (widen)
5684 (goto-char hdmarker)
5685 (org-get-tags-at)))))
5686 props m pl undone-face done-face finish new dotime cat tags)
5687 (save-excursion
5688 (goto-char (point-max))
5689 (beginning-of-line 1)
5690 (while (not finish)
5691 (setq finish (bobp))
5692 (when (and (setq m (get-text-property (point) 'org-hd-marker))
5693 (or (not just-this) (= (org-current-line) line))
5694 (equal m hdmarker))
5695 (setq props (text-properties-at (point))
5696 dotime (get-text-property (point) 'dotime)
5697 cat (get-text-property (point) 'org-category)
5698 tags thetags
5699 new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
5700 pl (get-text-property (point) 'prefix-length)
5701 undone-face (get-text-property (point) 'undone-face)
5702 done-face (get-text-property (point) 'done-face))
5703 (org-move-to-column pl)
5704 (cond
5705 ((equal new "")
5706 (beginning-of-line 1)
5707 (and (looking-at ".*\n?") (replace-match "")))
5708 ((looking-at ".*")
5709 (replace-match new t t)
5710 (beginning-of-line 1)
5711 (add-text-properties (point-at-bol) (point-at-eol) props)
5712 (when fixface
5713 (add-text-properties
5714 (point-at-bol) (point-at-eol)
5715 (list 'face
5716 (if org-last-todo-state-is-todo
5717 undone-face done-face))))
5718 (org-agenda-highlight-todo 'line)
5719 (beginning-of-line 1))
5720 (t (error "Line update did not work"))))
5721 (beginning-of-line 0)))
5722 (org-finalize-agenda)))
5724 (defun org-agenda-align-tags (&optional line)
5725 "Align all tags in agenda items to `org-agenda-tags-column'."
5726 (let ((inhibit-read-only t) l c)
5727 (save-excursion
5728 (goto-char (if line (point-at-bol) (point-min)))
5729 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
5730 (if line (point-at-eol) nil) t)
5731 (add-text-properties
5732 (match-beginning 2) (match-end 2)
5733 (list 'face (delq nil (let ((prop (get-text-property
5734 (match-beginning 2) 'face)))
5735 (or (listp prop) (setq prop (list prop)))
5736 (if (memq 'org-tag prop)
5737 prop
5738 (cons 'org-tag prop))))))
5739 (setq l (- (match-end 2) (match-beginning 2))
5740 c (if (< org-agenda-tags-column 0)
5741 (- (abs org-agenda-tags-column) l)
5742 org-agenda-tags-column))
5743 (delete-region (match-beginning 1) (match-end 1))
5744 (goto-char (match-beginning 1))
5745 (insert (org-add-props
5746 (make-string (max 1 (- c (current-column))) ?\ )
5747 (text-properties-at (point)))))
5748 (goto-char (point-min))
5749 (org-font-lock-add-tag-faces (point-max)))))
5751 (defun org-agenda-priority-up ()
5752 "Increase the priority of line at point, also in Org-mode file."
5753 (interactive)
5754 (org-agenda-priority 'up))
5756 (defun org-agenda-priority-down ()
5757 "Decrease the priority of line at point, also in Org-mode file."
5758 (interactive)
5759 (org-agenda-priority 'down))
5761 (defun org-agenda-priority (&optional force-direction)
5762 "Set the priority of line at point, also in Org-mode file.
5763 This changes the line at point, all other lines in the agenda referring to
5764 the same tree node, and the headline of the tree node in the Org-mode file."
5765 (interactive)
5766 (unless org-enable-priority-commands
5767 (error "Priority commands are disabled"))
5768 (org-agenda-check-no-diary)
5769 (let* ((marker (or (get-text-property (point) 'org-marker)
5770 (org-agenda-error)))
5771 (hdmarker (get-text-property (point) 'org-hd-marker))
5772 (buffer (marker-buffer hdmarker))
5773 (pos (marker-position hdmarker))
5774 (inhibit-read-only t)
5775 newhead)
5776 (org-with-remote-undo buffer
5777 (with-current-buffer buffer
5778 (widen)
5779 (goto-char pos)
5780 (org-show-context 'agenda)
5781 (save-excursion
5782 (and (outline-next-heading)
5783 (org-flag-heading nil))) ; show the next heading
5784 (funcall 'org-priority force-direction)
5785 (end-of-line 1)
5786 (setq newhead (org-get-heading)))
5787 (org-agenda-change-all-lines newhead hdmarker)
5788 (beginning-of-line 1))))
5790 ;; FIXME: should fix the tags property of the agenda line.
5791 (defun org-agenda-set-tags (&optional tag onoff)
5792 "Set tags for the current headline."
5793 (interactive)
5794 (org-agenda-check-no-diary)
5795 (if (and (org-region-active-p) (interactive-p))
5796 (call-interactively 'org-change-tag-in-region)
5797 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
5798 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
5799 (org-agenda-error)))
5800 (buffer (marker-buffer hdmarker))
5801 (pos (marker-position hdmarker))
5802 (inhibit-read-only t)
5803 newhead)
5804 (org-with-remote-undo buffer
5805 (with-current-buffer buffer
5806 (widen)
5807 (goto-char pos)
5808 (save-excursion
5809 (org-show-context 'agenda))
5810 (save-excursion
5811 (and (outline-next-heading)
5812 (org-flag-heading nil))) ; show the next heading
5813 (goto-char pos)
5814 (if tag
5815 (org-toggle-tag tag onoff)
5816 (call-interactively 'org-set-tags))
5817 (end-of-line 1)
5818 (setq newhead (org-get-heading)))
5819 (org-agenda-change-all-lines newhead hdmarker)
5820 (beginning-of-line 1)))))
5822 (defun org-agenda-toggle-archive-tag ()
5823 "Toggle the archive tag for the current entry."
5824 (interactive)
5825 (org-agenda-check-no-diary)
5826 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
5827 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
5828 (org-agenda-error)))
5829 (buffer (marker-buffer hdmarker))
5830 (pos (marker-position hdmarker))
5831 (inhibit-read-only t)
5832 newhead)
5833 (org-with-remote-undo buffer
5834 (with-current-buffer buffer
5835 (widen)
5836 (goto-char pos)
5837 (org-show-context 'agenda)
5838 (save-excursion
5839 (and (outline-next-heading)
5840 (org-flag-heading nil))) ; show the next heading
5841 (call-interactively 'org-toggle-archive-tag)
5842 (end-of-line 1)
5843 (setq newhead (org-get-heading)))
5844 (org-agenda-change-all-lines newhead hdmarker)
5845 (beginning-of-line 1))))
5847 (defun org-agenda-do-date-later (arg)
5848 (interactive "P")
5849 (cond
5850 ((or (equal arg '(16))
5851 (memq last-command
5852 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
5853 (setq this-command 'org-agenda-date-later-minutes)
5854 (org-agenda-date-later-minutes 1))
5855 ((or (equal arg '(4))
5856 (memq last-command
5857 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
5858 (setq this-command 'org-agenda-date-later-hours)
5859 (org-agenda-date-later-hours 1))
5861 (org-agenda-date-later (prefix-numeric-value arg)))))
5863 (defun org-agenda-do-date-earlier (arg)
5864 (interactive "P")
5865 (cond
5866 ((or (equal arg '(16))
5867 (memq last-command
5868 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
5869 (setq this-command 'org-agenda-date-earlier-minutes)
5870 (org-agenda-date-earlier-minutes 1))
5871 ((or (equal arg '(4))
5872 (memq last-command
5873 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
5874 (setq this-command 'org-agenda-date-earlier-hours)
5875 (org-agenda-date-earlier-hours 1))
5877 (org-agenda-date-earlier (prefix-numeric-value arg)))))
5879 (defun org-agenda-date-later (arg &optional what)
5880 "Change the date of this item to one day later."
5881 (interactive "p")
5882 (org-agenda-check-type t 'agenda 'timeline)
5883 (org-agenda-check-no-diary)
5884 (let* ((marker (or (get-text-property (point) 'org-marker)
5885 (org-agenda-error)))
5886 (buffer (marker-buffer marker))
5887 (pos (marker-position marker)))
5888 (org-with-remote-undo buffer
5889 (with-current-buffer buffer
5890 (widen)
5891 (goto-char pos)
5892 (if (not (org-at-timestamp-p))
5893 (error "Cannot find time stamp"))
5894 (org-timestamp-change arg (or what 'day)))
5895 (org-agenda-show-new-time marker org-last-changed-timestamp))
5896 (message "Time stamp changed to %s" org-last-changed-timestamp)))
5898 (defun org-agenda-date-earlier (arg &optional what)
5899 "Change the date of this item to one day earlier."
5900 (interactive "p")
5901 (org-agenda-date-later (- arg) what))
5903 (defun org-agenda-date-later-minutes (arg)
5904 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
5905 (interactive "p")
5906 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
5907 (org-agenda-date-later arg 'minute))
5909 (defun org-agenda-date-earlier-minutes (arg)
5910 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
5911 (interactive "p")
5912 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
5913 (org-agenda-date-earlier arg 'minute))
5915 (defun org-agenda-date-later-hours (arg)
5916 "Change the time of this item, in hour steps."
5917 (interactive "p")
5918 (org-agenda-date-later arg 'hour))
5920 (defun org-agenda-date-earlier-hours (arg)
5921 "Change the time of this item, in hour steps."
5922 (interactive "p")
5923 (org-agenda-date-earlier arg 'hour))
5925 (defun org-agenda-show-new-time (marker stamp &optional prefix)
5926 "Show new date stamp via text properties."
5927 ;; We use text properties to make this undoable
5928 (let ((inhibit-read-only t)
5929 (buffer-invisibility-spec))
5930 (setq stamp (concat " " prefix " => " stamp))
5931 (save-excursion
5932 (goto-char (point-max))
5933 (while (not (bobp))
5934 (when (equal marker (get-text-property (point) 'org-marker))
5935 (org-move-to-column (- (window-width) (length stamp)) t)
5936 (org-agenda-fix-tags-filter-overlays-at (point))
5937 (if (featurep 'xemacs)
5938 ;; Use `duplicable' property to trigger undo recording
5939 (let ((ex (make-extent nil nil))
5940 (gl (make-glyph stamp)))
5941 (set-glyph-face gl 'secondary-selection)
5942 (set-extent-properties
5943 ex (list 'invisible t 'end-glyph gl 'duplicable t))
5944 (insert-extent ex (1- (point)) (point-at-eol)))
5945 (add-text-properties
5946 (1- (point)) (point-at-eol)
5947 (list 'display (org-add-props stamp nil
5948 'face 'secondary-selection))))
5949 (beginning-of-line 1))
5950 (beginning-of-line 0)))))
5952 (defun org-agenda-date-prompt (arg)
5953 "Change the date of this item. Date is prompted for, with default today.
5954 The prefix ARG is passed to the `org-time-stamp' command and can therefore
5955 be used to request time specification in the time stamp."
5956 (interactive "P")
5957 (org-agenda-check-type t 'agenda 'timeline)
5958 (org-agenda-check-no-diary)
5959 (let* ((marker (or (get-text-property (point) 'org-marker)
5960 (org-agenda-error)))
5961 (buffer (marker-buffer marker))
5962 (pos (marker-position marker)))
5963 (org-with-remote-undo buffer
5964 (with-current-buffer buffer
5965 (widen)
5966 (goto-char pos)
5967 (if (not (org-at-timestamp-p))
5968 (error "Cannot find time stamp"))
5969 (org-time-stamp arg)
5970 (message "Time stamp changed to %s" org-last-changed-timestamp)))))
5972 (defun org-agenda-schedule (arg)
5973 "Schedule the item at point."
5974 (interactive "P")
5975 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
5976 (org-agenda-check-no-diary)
5977 (let* ((marker (or (get-text-property (point) 'org-marker)
5978 (org-agenda-error)))
5979 (type (marker-insertion-type marker))
5980 (buffer (marker-buffer marker))
5981 (pos (marker-position marker))
5982 (org-insert-labeled-timestamps-at-point nil)
5984 (set-marker-insertion-type marker t)
5985 (org-with-remote-undo buffer
5986 (with-current-buffer buffer
5987 (widen)
5988 (goto-char pos)
5989 (setq ts (org-schedule arg)))
5990 (org-agenda-show-new-time marker ts "S"))
5991 (message "Item scheduled for %s" ts)))
5993 (defun org-agenda-deadline (arg)
5994 "Schedule the item at point."
5995 (interactive "P")
5996 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
5997 (org-agenda-check-no-diary)
5998 (let* ((marker (or (get-text-property (point) 'org-marker)
5999 (org-agenda-error)))
6000 (buffer (marker-buffer marker))
6001 (pos (marker-position marker))
6002 (org-insert-labeled-timestamps-at-point nil)
6004 (org-with-remote-undo buffer
6005 (with-current-buffer buffer
6006 (widen)
6007 (goto-char pos)
6008 (setq ts (org-deadline arg)))
6009 (org-agenda-show-new-time marker ts "S"))
6010 (message "Deadline for this item set to %s" ts)))
6012 (defun org-agenda-action ()
6013 "Select entry for agenda action, or execute an agenda action.
6014 This command prompts for another letter. Valid inputs are:
6016 m Mark the entry at point for an agenda action
6017 s Schedule the marked entry to the date at the cursor
6018 d Set the deadline of the marked entry to the date at the cursor
6019 r Call `org-remember' with cursor date as the default date
6020 SPC Show marked entry in other window
6021 TAB Visit marked entry in other window
6023 The cursor may be at a date in the calendar, or in the Org agenda."
6024 (interactive)
6025 (let (ans)
6026 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [ ]show")
6027 (setq ans (read-char-exclusive))
6028 (cond
6029 ((equal ans ?m)
6030 ;; Mark this entry
6031 (if (eq major-mode 'org-agenda-mode)
6032 (let ((m (or (get-text-property (point) 'org-hd-marker)
6033 (get-text-property (point) 'org-marker))))
6034 (if m
6035 (progn
6036 (move-marker org-agenda-action-marker
6037 (marker-position m) (marker-buffer m))
6038 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
6039 (error "Don't know which entry to mark")))
6040 (error "This command works only in the agenda")))
6041 ((equal ans ?s)
6042 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
6043 ((equal ans ?d)
6044 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
6045 ((equal ans ?r)
6046 (org-agenda-do-action '(org-remember) t))
6047 ((equal ans ?\ )
6048 (let ((cw (selected-window)))
6049 (org-switch-to-buffer-other-window
6050 (marker-buffer org-agenda-action-marker))
6051 (goto-char org-agenda-action-marker)
6052 (org-show-context 'agenda)
6053 (select-window cw)))
6054 ((equal ans ?\C-i)
6055 (org-switch-to-buffer-other-window
6056 (marker-buffer org-agenda-action-marker))
6057 (goto-char org-agenda-action-marker)
6058 (org-show-context 'agenda))
6059 (t (error "Invalid agenda action %c" ans)))))
6061 (defun org-agenda-do-action (form &optional current-buffer)
6062 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
6063 (let ((org-overriding-default-time (org-get-cursor-date)))
6064 (if current-buffer
6065 (eval form)
6066 (if (not (marker-buffer org-agenda-action-marker))
6067 (error "No entry has bee selected for agenda action")
6068 (with-current-buffer (marker-buffer org-agenda-action-marker)
6069 (save-excursion
6070 (save-restriction
6071 (widen)
6072 (goto-char org-agenda-action-marker)
6073 (eval form))))))))
6075 (defun org-agenda-clock-in (&optional arg)
6076 "Start the clock on the currently selected item."
6077 (interactive "P")
6078 (org-agenda-check-no-diary)
6079 (if (equal arg '(4))
6080 (org-clock-in arg)
6081 (let* ((marker (or (get-text-property (point) 'org-marker)
6082 (org-agenda-error)))
6083 (hdmarker (or (get-text-property (point) 'org-hd-marker)
6084 marker))
6085 (pos (marker-position marker))
6086 newhead)
6087 (org-with-remote-undo (marker-buffer marker)
6088 (with-current-buffer (marker-buffer marker)
6089 (widen)
6090 (goto-char pos)
6091 (org-show-context 'agenda)
6092 (org-show-entry)
6093 (org-cycle-hide-drawers 'children)
6094 (org-clock-in arg)
6095 (setq newhead (org-get-heading)))
6096 (org-agenda-change-all-lines newhead hdmarker)))))
6098 (defun org-agenda-clock-out (&optional arg)
6099 "Stop the currently running clock."
6100 (interactive "P")
6101 (unless (marker-buffer org-clock-marker)
6102 (error "No running clock"))
6103 (let ((marker (make-marker)) newhead)
6104 (org-with-remote-undo (marker-buffer org-clock-marker)
6105 (with-current-buffer (marker-buffer org-clock-marker)
6106 (save-excursion
6107 (save-restriction
6108 (widen)
6109 (goto-char org-clock-marker)
6110 (org-back-to-heading t)
6111 (move-marker marker (point))
6112 (org-clock-out)
6113 (setq newhead (org-get-heading))))))
6114 (org-agenda-change-all-lines newhead marker)
6115 (move-marker marker nil)))
6117 (defun org-agenda-clock-cancel (&optional arg)
6118 "Cancel the currently running clock."
6119 (interactive "P")
6120 (unless (marker-buffer org-clock-marker)
6121 (error "No running clock"))
6122 (org-with-remote-undo (marker-buffer org-clock-marker)
6123 (org-clock-cancel)))
6125 (defun org-agenda-diary-entry ()
6126 "Make a diary entry, like the `i' command from the calendar.
6127 All the standard commands work: block, weekly etc."
6128 (interactive)
6129 (org-agenda-check-type t 'agenda 'timeline)
6130 (require 'diary-lib)
6131 (let* ((char (progn
6132 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
6133 (read-char-exclusive)))
6134 (cmd (cdr (assoc char
6135 '((?d . insert-diary-entry)
6136 (?w . insert-weekly-diary-entry)
6137 (?m . insert-monthly-diary-entry)
6138 (?y . insert-yearly-diary-entry)
6139 (?a . insert-anniversary-diary-entry)
6140 (?b . insert-block-diary-entry)
6141 (?c . insert-cyclic-diary-entry)))))
6142 (oldf (symbol-function 'calendar-cursor-to-date))
6143 ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
6144 (point (point))
6145 (mark (or (mark t) (point))))
6146 (unless cmd
6147 (error "No command associated with <%c>" char))
6148 (unless (and (get-text-property point 'day)
6149 (or (not (equal ?b char))
6150 (get-text-property mark 'day)))
6151 (error "Don't know which date to use for diary entry"))
6152 ;; We implement this by hacking the `calendar-cursor-to-date' function
6153 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
6154 (let ((calendar-mark-ring
6155 (list (calendar-gregorian-from-absolute
6156 (or (get-text-property mark 'day)
6157 (get-text-property point 'day))))))
6158 (unwind-protect
6159 (progn
6160 (fset 'calendar-cursor-to-date
6161 (lambda (&optional error dummy)
6162 (calendar-gregorian-from-absolute
6163 (get-text-property point 'day))))
6164 (call-interactively cmd))
6165 (fset 'calendar-cursor-to-date oldf)))))
6167 (defun org-agenda-execute-calendar-command (cmd)
6168 "Execute a calendar command from the agenda, with the date associated to
6169 the cursor position."
6170 (org-agenda-check-type t 'agenda 'timeline)
6171 (require 'diary-lib)
6172 (unless (get-text-property (point) 'day)
6173 (error "Don't know which date to use for calendar command"))
6174 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
6175 (point (point))
6176 (date (calendar-gregorian-from-absolute
6177 (get-text-property point 'day)))
6178 ;; the following 2 vars are needed in the calendar
6179 (displayed-month (car date))
6180 (displayed-year (nth 2 date)))
6181 (unwind-protect
6182 (progn
6183 (fset 'calendar-cursor-to-date
6184 (lambda (&optional error dummy)
6185 (calendar-gregorian-from-absolute
6186 (get-text-property point 'day))))
6187 (call-interactively cmd))
6188 (fset 'calendar-cursor-to-date oldf))))
6190 (defun org-agenda-phases-of-moon ()
6191 "Display the phases of the moon for the 3 months around the cursor date."
6192 (interactive)
6193 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
6195 (defun org-agenda-holidays ()
6196 "Display the holidays for the 3 months around the cursor date."
6197 (interactive)
6198 (org-agenda-execute-calendar-command 'list-calendar-holidays))
6200 (defvar calendar-longitude)
6201 (defvar calendar-latitude)
6202 (defvar calendar-location-name)
6204 (defun org-agenda-sunrise-sunset (arg)
6205 "Display sunrise and sunset for the cursor date.
6206 Latitude and longitude can be specified with the variables
6207 `calendar-latitude' and `calendar-longitude'. When called with prefix
6208 argument, latitude and longitude will be prompted for."
6209 (interactive "P")
6210 (require 'solar)
6211 (let ((calendar-longitude (if arg nil calendar-longitude))
6212 (calendar-latitude (if arg nil calendar-latitude))
6213 (calendar-location-name
6214 (if arg "the given coordinates" calendar-location-name)))
6215 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
6217 (defun org-agenda-goto-calendar ()
6218 "Open the Emacs calendar with the date at the cursor."
6219 (interactive)
6220 (org-agenda-check-type t 'agenda 'timeline)
6221 (let* ((day (or (get-text-property (point) 'day)
6222 (error "Don't know which date to open in calendar")))
6223 (date (calendar-gregorian-from-absolute day))
6224 (calendar-move-hook nil)
6225 (calendar-view-holidays-initially-flag nil)
6226 (calendar-view-diary-initially-flag nil)
6227 (view-calendar-holidays-initially nil)
6228 (view-diary-entries-initially nil))
6229 (calendar)
6230 (calendar-goto-date date)))
6232 ;;;###autoload
6233 (defun org-calendar-goto-agenda ()
6234 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
6235 This is a command that has to be installed in `calendar-mode-map'."
6236 (interactive)
6237 (org-agenda-list nil (calendar-absolute-from-gregorian
6238 (calendar-cursor-to-date))
6239 nil))
6241 (defun org-agenda-convert-date ()
6242 (interactive)
6243 (org-agenda-check-type t 'agenda 'timeline)
6244 (let ((day (get-text-property (point) 'day))
6245 date s)
6246 (unless day
6247 (error "Don't know which date to convert"))
6248 (setq date (calendar-gregorian-from-absolute day))
6249 (setq s (concat
6250 "Gregorian: " (calendar-date-string date) "\n"
6251 "ISO: " (calendar-iso-date-string date) "\n"
6252 "Day of Yr: " (calendar-day-of-year-string date) "\n"
6253 "Julian: " (calendar-julian-date-string date) "\n"
6254 "Astron. JD: " (calendar-astro-date-string date)
6255 " (Julian date number at noon UTC)\n"
6256 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
6257 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
6258 "French: " (calendar-french-date-string date) "\n"
6259 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
6260 "Mayan: " (calendar-mayan-date-string date) "\n"
6261 "Coptic: " (calendar-coptic-date-string date) "\n"
6262 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
6263 "Persian: " (calendar-persian-date-string date) "\n"
6264 "Chinese: " (calendar-chinese-date-string date) "\n"))
6265 (with-output-to-temp-buffer "*Dates*"
6266 (princ s))
6267 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
6269 ;;; Bulk commands
6271 (defvar org-agenda-bulk-marked-entries nil
6272 "List of markers that refer to marked entries in the agenda.")
6274 (defun org-agenda-bulk-mark ()
6275 "Mark the entry at point for future bulk action."
6276 (interactive)
6277 (org-agenda-check-no-diary)
6278 (let* ((m (get-text-property (point) 'org-hd-marker))
6280 (unless (eq (get-char-property (point-at-bol) 'type)
6281 'org-marked-entry-overlay)
6282 (unless m (error "Nothing to mark at point"))
6283 (push m org-agenda-bulk-marked-entries)
6284 (setq ov (org-make-overlay (point-at-bol) (+ 2 (point-at-bol))))
6285 (org-overlay-display ov ">>"
6286 (org-get-todo-face "TODO")
6287 'evaporate)
6288 (org-overlay-put ov 'type 'org-marked-entry-overlay))
6289 (beginning-of-line 2)
6290 (message "%d entries marked for bulk action"
6291 (length org-agenda-bulk-marked-entries))))
6293 (defun org-agenda-bulk-unmark ()
6294 "Unmark the entry at point for future bulk action."
6295 (interactive)
6296 (when (eq (get-char-property (point-at-bol) 'type)
6297 'org-marked-entry-overlay)
6298 (org-agenda-bulk-remove-overlays
6299 (point-at-bol) (+ 2 (point-at-bol)))
6300 (setq org-agenda-bulk-marked-entries
6301 (delete (get-text-property (point-at-bol) 'org-hd-marker)
6302 org-agenda-bulk-marked-entries)))
6303 (beginning-of-line 2)
6304 (message "%d entries marked for bulk action"
6305 (length org-agenda-bulk-marked-entries)))
6308 (defun org-agenda-bulk-remove-overlays (&optional beg end)
6309 "Remove the mark overlays between BEG and END in the agenda buffer.
6310 BEG and END default to the buffer limits.
6312 This only removes the overlays, it does not remove the markers
6313 from the list in `org-agenda-bulk-marked-entries'."
6314 (interactive)
6315 (mapc (lambda (ov)
6316 (and (eq (org-overlay-get ov 'type) 'org-marked-entry-overlay)
6317 (org-delete-overlay ov)))
6318 (org-overlays-in (or beg (point-min)) (or end (point-max)))))
6320 (defun org-agenda-bulk-remove-all-marks ()
6321 "Remove all marks in the agenda buffer.
6322 This will remove the markers, and the overlays."
6323 (interactive)
6324 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
6325 (setq org-agenda-bulk-marked-entries nil)
6326 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
6328 (defun org-agenda-bulk-action ()
6329 "Execute an remote-editing action on all marked entries."
6330 (interactive)
6331 (unless org-agenda-bulk-marked-entries
6332 (error "No entries are marked"))
6333 (message "Bulk: [r]efile [$]archive [A]rch->sib [t]odo [+/-]tag [s]chedule [d]eadline")
6334 (let* ((action (read-char-exclusive))
6335 (entries (reverse org-agenda-bulk-marked-entries))
6336 cmd rfloc state e tag (cnt 0))
6337 (cond
6338 ((equal action ?$)
6339 (setq cmd '(org-agenda-archive)))
6341 ((equal action ?A)
6342 (setq cmd '(org-agenda-archive-to-archive-sibling)))
6344 ((member action '(?r ?w))
6345 (setq rfloc (org-refile-get-location
6346 "Refile to: "
6347 (marker-buffer (car org-agenda-bulk-marked-entries))
6348 org-refile-allow-creating-parent-nodes))
6349 (setcar (nthcdr 3 rfloc)
6350 (move-marker (make-marker) (nth 3 rfloc)
6351 (or (get-file-buffer (nth 1 rfloc))
6352 (find-buffer-visiting (nth 1 rfloc))
6353 (error "This should not happen"))))
6355 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc))))
6357 ((equal action ?t)
6358 (setq state (org-ido-completing-read
6359 "Todo state: "
6360 (with-current-buffer (marker-buffer (car entries))
6361 (mapcar 'list org-todo-keywords-1))))
6362 (setq cmd `(let ((org-inhibit-blocking t)
6363 (org-inhibit-logging 'note))
6364 (org-agenda-todo ,state))))
6366 ((memq action '(?- ?+))
6367 (setq tag (org-ido-completing-read
6368 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
6369 (with-current-buffer (marker-buffer (car entries))
6370 (delq nil
6371 (mapcar (lambda (x)
6372 (if (stringp (car x)) x)) org-tag-alist)))))
6373 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
6375 ((memq action '(?s ?d))
6376 (let* ((date (org-read-date
6377 nil nil nil
6378 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to")))
6379 (ans org-read-date-final-answer)
6380 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
6381 (setq cmd `(let* ((bound (fboundp 'read-string))
6382 (old (and bound (symbol-function 'read-string))))
6383 (unwind-protect
6384 (progn
6385 (fset 'read-string (lambda (&rest ignore) ,ans))
6386 (call-interactively ',c1))
6387 (if bound
6388 (fset 'read-string old)
6389 (fmakunbound 'read-string)))))))
6390 (t (error "Invalid bulk action")))
6392 ;; Now loop over all markers and apply cmd
6393 (while (setq e (pop entries))
6394 (goto-char
6395 (or (text-property-any (point-min) (point-max) 'org-hd-marker e)
6396 (error "Cannot find entry for marker %s" e)))
6397 (eval cmd)
6398 (setq org-agenda-bulk-marked-entries (delete e org-agenda-bulk-marked-entries))
6399 (setq cnt (1+ cnt)))
6400 (setq org-agenda-bulk-marked-entries nil)
6401 (org-agenda-bulk-remove-all-marks)
6402 (message "Acted on %d entries" cnt)))
6404 ;;; Appointment reminders
6406 (defvar appt-time-msg-list)
6408 ;;;###autoload
6409 (defun org-agenda-to-appt (&optional refresh filter)
6410 "Activate appointments found in `org-agenda-files'.
6411 With a \\[universal-argument] prefix, refresh the list of
6412 appointments.
6414 If FILTER is t, interactively prompt the user for a regular
6415 expression, and filter out entries that don't match it.
6417 If FILTER is a string, use this string as a regular expression
6418 for filtering entries out.
6420 FILTER can also be an alist with the car of each cell being
6421 either 'headline or 'category. For example:
6423 '((headline \"IMPORTANT\")
6424 (category \"Work\"))
6426 will only add headlines containing IMPORTANT or headlines
6427 belonging to the \"Work\" category."
6428 (interactive "P")
6429 (require 'calendar)
6430 (if refresh (setq appt-time-msg-list nil))
6431 (if (eq filter t)
6432 (setq filter (read-from-minibuffer "Regexp filter: ")))
6433 (let* ((cnt 0) ; count added events
6434 (org-agenda-new-buffers nil)
6435 (org-deadline-warning-days 0)
6436 (today (org-date-to-gregorian
6437 (time-to-days (current-time))))
6438 (org-agenda-restrict nil)
6439 (files (org-agenda-files 'unrestricted)) entries file)
6440 ;; Get all entries which may contain an appt
6441 (org-prepare-agenda-buffers files)
6442 (while (setq file (pop files))
6443 (setq entries
6444 (append entries
6445 (org-agenda-get-day-entries
6446 file today :timestamp :scheduled :deadline))))
6447 (setq entries (delq nil entries))
6448 ;; Map thru entries and find if we should filter them out
6449 (mapc
6450 (lambda(x)
6451 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
6452 (cat (get-text-property 1 'org-category x))
6453 (tod (get-text-property 1 'time-of-day x))
6454 (ok (or (null filter)
6455 (and (stringp filter) (string-match filter evt))
6456 (and (listp filter)
6457 (or (string-match
6458 (cadr (assoc 'category filter)) cat)
6459 (string-match
6460 (cadr (assoc 'headline filter)) evt))))))
6461 ;; FIXME: Shall we remove text-properties for the appt text?
6462 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
6463 (when (and ok tod)
6464 (setq tod (concat "00" (number-to-string tod))
6465 tod (when (string-match
6466 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
6467 (concat (match-string 1 tod) ":"
6468 (match-string 2 tod))))
6469 (appt-add tod evt)
6470 (setq cnt (1+ cnt))))) entries)
6471 (org-release-buffers org-agenda-new-buffers)
6472 (if (eq cnt 0)
6473 (message "No event to add")
6474 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
6476 (defun org-agenda-todayp (date)
6477 "Does DATE mean today, when considering `org-extend-today-until'?"
6478 (let (today h)
6479 (if (listp date) (setq date (calendar-absolute-from-gregorian date)))
6480 (setq today (calendar-absolute-from-gregorian (calendar-current-date)))
6481 (setq h (nth 2 (decode-time (current-time))))
6482 (or (and (>= h org-extend-today-until)
6483 (= date today))
6484 (and (< h org-extend-today-until)
6485 (= date (1- today))))))
6487 (provide 'org-agenda)
6489 ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
6491 ;;; org-agenda.el ends here