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