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