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