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