Minor fixes than are needed to complete the previous commit
[org-mode.git] / lisp / org-agenda.el
blob6168005f15ca7961b28603ee11cb41edcce5c5dd
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.33trans
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-always-boolean nil
895 "Non-nil means, the search string is interpreted as individual parts.
897 The search string for search view can either be interpreted as a phrase,
898 or as a list of snippets that define a boolean search for a number of
899 strings.
901 When this is non-nil, the string will be split on whitespace, and each
902 snippet will be searched individually, and all must match in order to
903 select an entry. If a snippet is preceeded by \"-\", the snippet
904 must *not* match. \"+\" is syntactic sugar for positive selection.
905 Each snipped may be found as a full word or a partial word, but see
906 the variable `org-agenda-search-view-force-full-words'.
908 When this is nil, search will look for the entire search phrase as one,
909 with each space character matching any amount of whitespace, including
910 line breaks.
912 Even when this is nil, you can still switch to Boolean search dynamically
913 by preceeding the first snippet with \"+\" or \"-\". If the first snippet
914 is a regexp marked with braces like \"{abc}\", this will also switch to
915 boolean search."
916 :group 'org-agenda-search-view
917 :type 'boolean)
919 (if (fboundp 'defvaralias)
920 (defvaralias 'org-agenda-search-view-search-words-only
921 'org-agenda-search-view-always-boolean))
923 (defcustom org-agenda-search-view-force-full-words nil
924 "Non-nil me
925 ans, search words must be matches as complete words.
926 When nil, they may also match part of a word."
927 :group 'org-agenda-search-view
928 :type 'boolean)
930 (defgroup org-agenda-time-grid nil
931 "Options concerning the time grid in the Org-mode Agenda."
932 :tag "Org Agenda Time Grid"
933 :group 'org-agenda)
935 (defcustom org-agenda-search-headline-for-time t
936 "Non-nil means, search headline for a time-of-day.
937 If the headline contains a time-of-day in one format or another, it will
938 be used to sort the entry into the time sequence of items for a day.
939 Some people have time stamps in the headline that refer to the creation
940 time or so, and then this produces an unwanted side effect. If this is
941 the case for your, use this variable to turn off searching the headline
942 for a time."
943 :group 'org-agenda-time-grid
944 :type 'boolean)
946 (defcustom org-agenda-use-time-grid t
947 "Non-nil means, show a time grid in the agenda schedule.
948 A time grid is a set of lines for specific times (like every two hours between
949 8:00 and 20:00). The items scheduled for a day at specific times are
950 sorted in between these lines.
951 For details about when the grid will be shown, and what it will look like, see
952 the variable `org-agenda-time-grid'."
953 :group 'org-agenda-time-grid
954 :type 'boolean)
956 (defcustom org-agenda-time-grid
957 '((daily today require-timed)
958 "----------------"
959 (800 1000 1200 1400 1600 1800 2000))
961 "The settings for time grid for agenda display.
962 This is a list of three items. The first item is again a list. It contains
963 symbols specifying conditions when the grid should be displayed:
965 daily if the agenda shows a single day
966 weekly if the agenda shows an entire week
967 today show grid on current date, independent of daily/weekly display
968 require-timed show grid only if at least one item has a time specification
970 The second item is a string which will be placed behind the grid time.
972 The third item is a list of integers, indicating the times that should have
973 a grid line."
974 :group 'org-agenda-time-grid
975 :type
976 '(list
977 (set :greedy t :tag "Grid Display Options"
978 (const :tag "Show grid in single day agenda display" daily)
979 (const :tag "Show grid in weekly agenda display" weekly)
980 (const :tag "Always show grid for today" today)
981 (const :tag "Show grid only if any timed entries are present"
982 require-timed)
983 (const :tag "Skip grid times already present in an entry"
984 remove-match))
985 (string :tag "Grid String")
986 (repeat :tag "Grid Times" (integer :tag "Time"))))
988 (defgroup org-agenda-sorting nil
989 "Options concerning sorting in the Org-mode Agenda."
990 :tag "Org Agenda Sorting"
991 :group 'org-agenda)
993 (defcustom org-agenda-sorting-strategy
994 '((agenda habit-down time-up priority-down category-keep)
995 (todo priority-down category-keep)
996 (tags priority-down category-keep)
997 (search category-keep))
998 "Sorting structure for the agenda items of a single day.
999 This is a list of symbols which will be used in sequence to determine
1000 if an entry should be listed before another entry. The following
1001 symbols are recognized:
1003 time-up Put entries with time-of-day indications first, early first
1004 time-down Put entries with time-of-day indications first, late first
1005 category-keep Keep the default order of categories, corresponding to the
1006 sequence in `org-agenda-files'.
1007 category-up Sort alphabetically by category, A-Z.
1008 category-down Sort alphabetically by category, Z-A.
1009 tag-up Sort alphabetically by last tag, A-Z.
1010 tag-down Sort alphabetically by last tag, Z-A.
1011 priority-up Sort numerically by priority, high priority last.
1012 priority-down Sort numerically by priority, high priority first.
1013 todo-state-up Sort by todo state, tasks that are done last.
1014 todo-state-down Sort by todo state, tasks that are done first.
1015 effort-up Sort numerically by estimated effort, high effort last.
1016 effort-down Sort numerically by estimated effort, high effort first.
1017 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1018 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1019 habit-up Put entries that are habits first
1020 habit-down Put entries that are habits last
1022 The different possibilities will be tried in sequence, and testing stops
1023 if one comparison returns a \"not-equal\". For example, the default
1024 '(time-up category-keep priority-down)
1025 means: Pull out all entries having a specified time of day and sort them,
1026 in order to make a time schedule for the current day the first thing in the
1027 agenda listing for the day. Of the entries without a time indication, keep
1028 the grouped in categories, don't sort the categories, but keep them in
1029 the sequence given in `org-agenda-files'. Within each category sort by
1030 priority.
1032 Leaving out `category-keep' would mean that items will be sorted across
1033 categories by priority.
1035 Instead of a single list, this can also be a set of list for specific
1036 contents, with a context symbol in the car of the list, any of
1037 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1039 Custom commands can bind this variable in the options section."
1040 :group 'org-agenda-sorting
1041 :type `(choice
1042 (repeat :tag "General" ,org-sorting-choice)
1043 (list :tag "Individually"
1044 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1045 (repeat ,org-sorting-choice))
1046 (cons (const :tag "Strategy for TODO lists" todo)
1047 (repeat ,org-sorting-choice))
1048 (cons (const :tag "Strategy for Tags matches" tags)
1049 (repeat ,org-sorting-choice))
1050 (cons (const :tag "Strategy for search matches" search)
1051 (repeat ,org-sorting-choice)))))
1053 (defcustom org-agenda-cmp-user-defined nil
1054 "A function to define the comparison `user-defined'.
1055 This function must receive two arguments, agenda entry a and b.
1056 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1057 the user comparison, return nil.
1058 When this is defined, you can make `user-defined-up' and `user-defined-down'
1059 part of an agenda sorting strategy."
1060 :group 'org-agenda-sorting
1061 :type 'symbol)
1063 (defcustom org-sort-agenda-notime-is-late t
1064 "Non-nil means, items without time are considered late.
1065 This is only relevant for sorting. When t, items which have no explicit
1066 time like 15:30 will be considered as 99:01, i.e. later than any items which
1067 do have a time. When nil, the default time is before 0:00. You can use this
1068 option to decide if the schedule for today should come before or after timeless
1069 agenda entries."
1070 :group 'org-agenda-sorting
1071 :type 'boolean)
1073 (defcustom org-sort-agenda-noeffort-is-high t
1074 "Non-nil means, items without effort estimate are sorted as high effort.
1075 This also applies when filtering an agenda view with respect to the
1076 < or > effort operator. Then, tasks with no effort defined will be treated
1077 as tasks with high effort.
1078 When nil, such items are sorted as 0 minutes effort."
1079 :group 'org-agenda-sorting
1080 :type 'boolean)
1082 (defgroup org-agenda-line-format nil
1083 "Options concerning the entry prefix in the Org-mode agenda display."
1084 :tag "Org Agenda Line Format"
1085 :group 'org-agenda)
1087 (defcustom org-agenda-prefix-format
1088 '((agenda . " %-12:c%?-12t% s")
1089 (timeline . " % s")
1090 (todo . " %-12:c")
1091 (tags . " %-12:c")
1092 (search . " %-12:c"))
1093 "Format specifications for the prefix of items in the agenda views.
1094 An alist with four entries, for the different agenda types. The keys to the
1095 sublists are `agenda', `timeline', `todo', and `tags'. The values
1096 are format strings.
1097 This format works similar to a printf format, with the following meaning:
1099 %c the category of the item, \"Diary\" for entries from the diary, or
1100 as given by the CATEGORY keyword or derived from the file name.
1101 %T the *last* tag of the item. Last because inherited tags come
1102 first in the list.
1103 %t the time-of-day specification if one applies to the entry, in the
1104 format HH:MM
1105 %s Scheduling/Deadline information, a short string
1107 All specifiers work basically like the standard `%s' of printf, but may
1108 contain two additional characters: A question mark just after the `%' and
1109 a whitespace/punctuation character just before the final letter.
1111 If the first character after `%' is a question mark, the entire field
1112 will only be included if the corresponding value applies to the
1113 current entry. This is useful for fields which should have fixed
1114 width when present, but zero width when absent. For example,
1115 \"%?-12t\" will result in a 12 character time field if a time of the
1116 day is specified, but will completely disappear in entries which do
1117 not contain a time.
1119 If there is punctuation or whitespace character just before the final
1120 format letter, this character will be appended to the field value if
1121 the value is not empty. For example, the format \"%-12:c\" leads to
1122 \"Diary: \" if the category is \"Diary\". If the category were be
1123 empty, no additional colon would be inserted.
1125 The default value of this option is \" %-12:c%?-12t% s\", meaning:
1126 - Indent the line with two space characters
1127 - Give the category in a 12 chars wide field, padded with whitespace on
1128 the right (because of `-'). Append a colon if there is a category
1129 (because of `:').
1130 - If there is a time-of-day, put it into a 12 chars wide field. If no
1131 time, don't put in an empty field, just skip it (because of '?').
1132 - Finally, put the scheduling information and append a whitespace.
1134 As another example, if you don't want the time-of-day of entries in
1135 the prefix, you could use:
1137 (setq org-agenda-prefix-format \" %-11:c% s\")
1139 See also the variables `org-agenda-remove-times-when-in-prefix' and
1140 `org-agenda-remove-tags'.
1142 Custom commands can set this variable in the options section."
1143 :type '(choice
1144 (string :tag "General format")
1145 (list :greedy t :tag "View dependent"
1146 (cons (const agenda) (string :tag "Format"))
1147 (cons (const timeline) (string :tag "Format"))
1148 (cons (const todo) (string :tag "Format"))
1149 (cons (const tags) (string :tag "Format"))
1150 (cons (const search) (string :tag "Format"))))
1151 :group 'org-agenda-line-format)
1153 (defvar org-prefix-format-compiled nil
1154 "The compiled version of the most recently used prefix format.
1155 See the variable `org-agenda-prefix-format'.")
1157 (defcustom org-agenda-todo-keyword-format "%-1s"
1158 "Format for the TODO keyword in agenda lines.
1159 Set this to something like \"%-12s\" if you want all TODO keywords
1160 to occupy a fixed space in the agenda display."
1161 :group 'org-agenda-line-format
1162 :type 'string)
1164 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1165 "Text preceding timerange entries in the agenda view.
1166 This is a list with two strings. The first applies when the range
1167 is entirely on one day. The second applies if the range spans several days.
1168 The strings may have two \"%d\" format specifiers which will be filled
1169 with the sequence number of the days, and the total number of days in the
1170 range, respectively."
1171 :group 'org-agenda-line-format
1172 :type '(list
1173 (string :tag "Deadline today ")
1174 (choice :tag "Deadline relative"
1175 (string :tag "Format string")
1176 (function))))
1178 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1179 "Text preceeding scheduled items in the agenda view.
1180 This is a list with two strings. The first applies when the item is
1181 scheduled on the current day. The second applies when it has been scheduled
1182 previously, it may contain a %d indicating that this is the nth time that
1183 this item is scheduled, due to automatic rescheduling of unfinished items
1184 for the following day. So this number is one larger than the number of days
1185 that passed since this item was scheduled first."
1186 :group 'org-agenda-line-format
1187 :type '(list
1188 (string :tag "Scheduled today ")
1189 (string :tag "Scheduled previously")))
1191 (defcustom org-agenda-inactive-leader "["
1192 "Text preceeding item pulled into the agenda by inactive time stamps.
1193 These entries are added to the agenda when pressing \"[\"."
1194 :group 'org-agenda-line-format
1195 :type '(list
1196 (string :tag "Scheduled today ")
1197 (string :tag "Scheduled previously")))
1199 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1200 "Text preceeding deadline items in the agenda view.
1201 This is a list with two strings. The first applies when the item has its
1202 deadline on the current day. The second applies when it is in the past or
1203 in the future, it may contain %d to capture how many days away the deadline
1204 is (was)."
1205 :group 'org-agenda-line-format
1206 :type '(list
1207 (string :tag "Deadline today ")
1208 (choice :tag "Deadline relative"
1209 (string :tag "Format string")
1210 (function))))
1212 (defcustom org-agenda-remove-times-when-in-prefix t
1213 "Non-nil means, remove duplicate time specifications in agenda items.
1214 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1215 time-of-day specification in a headline or diary entry is extracted and
1216 placed into the prefix. If this option is non-nil, the original specification
1217 \(a timestamp or -range, or just a plain time(range) specification like
1218 11:30-4pm) will be removed for agenda display. This makes the agenda less
1219 cluttered.
1220 The option can be t or nil. It may also be the symbol `beg', indicating
1221 that the time should only be removed what it is located at the beginning of
1222 the headline/diary entry."
1223 :group 'org-agenda-line-format
1224 :type '(choice
1225 (const :tag "Always" t)
1226 (const :tag "Never" nil)
1227 (const :tag "When at beginning of entry" beg)))
1230 (defcustom org-agenda-default-appointment-duration nil
1231 "Default duration for appointments that only have a starting time.
1232 When nil, no duration is specified in such cases.
1233 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1234 :group 'org-agenda-line-format
1235 :type '(choice
1236 (integer :tag "Minutes")
1237 (const :tag "No default duration")))
1239 (defcustom org-agenda-show-inherited-tags t
1240 "Non-nil means, show inherited tags in each agenda line."
1241 :group 'org-agenda-line-format
1242 :type 'boolean)
1244 (defcustom org-agenda-hide-tags-regexp nil
1245 "Regular expression used to filter away specific tags in agenda views.
1246 This means that these tags will be present, but not be shown in the agenda
1247 line. Secondayt filltering will still work on the hidden tags.
1248 Nil means don't hide any tags."
1249 :group 'org-agenda-line-format
1250 :type '(choice
1251 (const :tag "Hide none" nil)
1252 (string :tag "Regexp ")))
1254 (defcustom org-agenda-remove-tags nil
1255 "Non-nil means, remove the tags from the headline copy in the agenda.
1256 When this is the symbol `prefix', only remove tags when
1257 `org-agenda-prefix-format' contains a `%T' specifier."
1258 :group 'org-agenda-line-format
1259 :type '(choice
1260 (const :tag "Always" t)
1261 (const :tag "Never" nil)
1262 (const :tag "When prefix format contains %T" prefix)))
1264 (if (fboundp 'defvaralias)
1265 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1266 'org-agenda-remove-tags))
1268 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1269 "Shift tags in agenda items to this column.
1270 If this number is positive, it specifies the column. If it is negative,
1271 it means that the tags should be flushright to that column. For example,
1272 -80 works well for a normal 80 character screen."
1273 :group 'org-agenda-line-format
1274 :type 'integer)
1276 (if (fboundp 'defvaralias)
1277 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1279 (defcustom org-agenda-fontify-priorities 'cookies
1280 "Non-nil means, highlight low and high priorities in agenda.
1281 When t, the highest priority entries are bold, lowest priority italic.
1282 However, settings in org-priority-faces will overrule these faces.
1283 When this variable is the symbol `cookies', only fontify the
1284 cookies, not the entire task.
1285 This may also be an association list of priority faces, whose
1286 keys are the character values of `org-highest-priority',
1287 `org-default-priority', and `org-lowest-priority' (the default values
1288 are ?A, ?B, and ?C, respectively). The face may be a named face,
1289 or a list like `(:background \"Red\")'."
1290 :group 'org-agenda-line-format
1291 :type '(choice
1292 (const :tag "Never" nil)
1293 (const :tag "Defaults" t)
1294 (const :tag "Cookies only" cookies)
1295 (repeat :tag "Specify"
1296 (list (character :tag "Priority" :value ?A)
1297 (sexp :tag "face")))))
1299 (defgroup org-agenda-column-view nil
1300 "Options concerning column view in the agenda."
1301 :tag "Org Agenda Column View"
1302 :group 'org-agenda)
1304 (defcustom org-agenda-columns-show-summaries t
1305 "Non-nil means, show summaries for columns displayed in the agenda view."
1306 :group 'org-agenda-column-view
1307 :type 'boolean)
1309 (defcustom org-agenda-columns-remove-prefix-from-item t
1310 "Non-nil means, remove the prefix from a headline for agenda column view.
1311 The special ITEM field in the columns format contains the current line, with
1312 all information shown in other columns (like the TODO state or a tag).
1313 When this variable is non-nil, also the agenda prefix will be removed from
1314 the content of the ITEM field, to make sure as much as possible of the
1315 headline can be shown in the limited width of the field."
1316 :group 'org-agenda
1317 :type 'boolean)
1319 (defcustom org-agenda-columns-compute-summary-properties t
1320 "Non-nil means, recompute all summary properties before column view.
1321 When column view in the agenda is listing properties that have a summary
1322 operator, it can go to all relevant buffers and recompute the summaries
1323 there. This can mean overhead for the agenda column view, but is necessary
1324 to have thing up to date.
1325 As a special case, a CLOCKSUM property also makes sure that the clock
1326 computations are current."
1327 :group 'org-agenda-column-view
1328 :type 'boolean)
1330 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1331 "Non-nil means, the duration of an appointment will add to day effort.
1332 The property to which appointment durations will be added is the one given
1333 in the option `org-effort-property'. If an appointment does not have
1334 an end time, `org-agenda-default-appointment-duration' will be used. If that
1335 is not set, an appointment without end time will not contribute to the time
1336 estimate."
1337 :group 'org-agenda-column-view
1338 :type 'boolean)
1340 (defcustom org-agenda-auto-exclude-function nil
1341 "A function called with a tag to decide if it is filtered on '/ RET'.
1342 The sole argument to the function, which is called once for each
1343 possible tag, is a string giving the name of the tag. The
1344 function should return either nil if the tag should be included
1345 as normal, or \"-<TAG>\" to exclude the tag.
1346 Note that for the purpose of tag filtering, only the lower-case version of
1347 all tags will be considered, so that this function will only ever see
1348 the lower-case version of all tags."
1349 :group 'org-agenda
1350 :type 'function)
1352 (eval-when-compile
1353 (require 'cl))
1354 (require 'org)
1356 (defun org-add-agenda-custom-command (entry)
1357 "Replace or add a command in `org-agenda-custom-commands'.
1358 This is mostly for hacking and trying a new command - once the command
1359 works you probably want to add it to `org-agenda-custom-commands' for good."
1360 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1361 (if ass
1362 (setcdr ass (cdr entry))
1363 (push entry org-agenda-custom-commands))))
1365 ;;; Define the Org-agenda-mode
1367 (defvar org-agenda-mode-map (make-sparse-keymap)
1368 "Keymap for `org-agenda-mode'.")
1369 (if (fboundp 'defvaralias)
1370 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1372 (defvar org-agenda-menu) ; defined later in this file.
1373 (defvar org-agenda-restrict) ; defined later in this file.
1374 (defvar org-agenda-follow-mode nil)
1375 (defvar org-agenda-entry-text-mode nil)
1376 (defvar org-agenda-clockreport-mode nil)
1377 (defvar org-agenda-show-log nil)
1378 (defvar org-agenda-redo-command nil)
1379 (defvar org-agenda-query-string nil)
1380 (defvar org-agenda-mode-hook nil
1381 "Hook for org-agenda-mode, run after the mode is turned on.")
1382 (defvar org-agenda-type nil)
1383 (defvar org-agenda-force-single-file nil)
1384 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1386 (defun org-agenda-mode ()
1387 "Mode for time-sorted view on action items in Org-mode files.
1389 The following commands are available:
1391 \\{org-agenda-mode-map}"
1392 (interactive)
1393 (kill-all-local-variables)
1394 (setq org-agenda-undo-list nil
1395 org-agenda-pending-undo-list nil
1396 org-agenda-bulk-marked-entries nil)
1397 (setq major-mode 'org-agenda-mode)
1398 ;; Keep global-font-lock-mode from turning on font-lock-mode
1399 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1400 (setq mode-name "Org-Agenda")
1401 (use-local-map org-agenda-mode-map)
1402 (easy-menu-add org-agenda-menu)
1403 (if org-startup-truncated (setq truncate-lines t))
1404 (org-set-local 'line-move-visual nil)
1405 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1406 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1407 ;; Make sure properties are removed when copying text
1408 (when (boundp 'buffer-substring-filters)
1409 (org-set-local 'buffer-substring-filters
1410 (cons (lambda (x)
1411 (set-text-properties 0 (length x) nil x) x)
1412 buffer-substring-filters)))
1413 (unless org-agenda-keep-modes
1414 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1415 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1416 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1417 org-agenda-show-log org-agenda-start-with-log-mode))
1419 (easy-menu-change
1420 '("Agenda") "Agenda Files"
1421 (append
1422 (list
1423 (vector
1424 (if (get 'org-agenda-files 'org-restrict)
1425 "Restricted to single file"
1426 "Edit File List")
1427 '(org-edit-agenda-file-list)
1428 (not (get 'org-agenda-files 'org-restrict)))
1429 "--")
1430 (mapcar 'org-file-menu-entry (org-agenda-files))))
1431 (org-agenda-set-mode-name)
1432 (apply
1433 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1434 (list 'org-agenda-mode-hook)))
1436 (substitute-key-definition 'undo 'org-agenda-undo
1437 org-agenda-mode-map global-map)
1438 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1439 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1440 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1441 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1442 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1443 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1444 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1445 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1446 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1447 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1448 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1449 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1450 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1451 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1452 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1453 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1454 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1455 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1456 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1457 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1458 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1459 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1460 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1461 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1462 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1463 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1464 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1465 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1466 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1467 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1468 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1469 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1470 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1471 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1472 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1473 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1474 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1475 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1476 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1477 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1478 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1479 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1480 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1482 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1483 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1484 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1485 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1486 (while l (org-defkey org-agenda-mode-map
1487 (int-to-string (pop l)) 'digit-argument)))
1489 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1490 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1491 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1492 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1493 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1494 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1495 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1496 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1497 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1498 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1499 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1500 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1501 'org-clock-modify-effort-estimate)
1502 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1503 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1504 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1505 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1506 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1507 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1508 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1509 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1510 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1511 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1512 (substitute-key-definition 'next-line 'org-agenda-next-line
1513 org-agenda-mode-map global-map)
1514 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1515 org-agenda-mode-map global-map)
1516 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1517 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1518 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1519 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1520 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1521 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1522 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1523 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1524 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1525 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1526 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1527 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1528 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1529 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1530 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1531 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1532 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1533 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1534 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1535 (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
1536 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1537 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1538 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1539 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1540 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1541 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1542 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1543 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1544 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1545 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1547 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1548 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1549 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1550 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1551 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1552 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1553 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1554 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1555 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1556 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1558 (org-defkey org-agenda-mode-map
1559 (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
1560 (org-defkey org-agenda-mode-map
1561 (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
1562 (when org-agenda-mouse-1-follows-link
1563 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1564 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1565 '("Agenda"
1566 ("Agenda Files")
1567 "--"
1568 ("Agenda Dates"
1569 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1570 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1571 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1572 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1573 "--"
1574 ("View"
1575 ["Day View" org-agenda-day-view
1576 :active (org-agenda-check-type nil 'agenda)
1577 :style radio :selected (equal org-agenda-ndays 1)
1578 :keys "v d (or just d)"]
1579 ["Week View" org-agenda-week-view
1580 :active (org-agenda-check-type nil 'agenda)
1581 :style radio :selected (equal org-agenda-ndays 7)
1582 :keys "v w (or just w)"]
1583 ["Month View" org-agenda-month-view
1584 :active (org-agenda-check-type nil 'agenda)
1585 :style radio :selected (member org-agenda-ndays '(28 29 30 31))
1586 :keys "v m"]
1587 ["Year View" org-agenda-year-view
1588 :active (org-agenda-check-type nil 'agenda)
1589 :style radio :selected (member org-agenda-ndays '(365 366))
1590 :keys "v y"]
1591 "--"
1592 ["Include Diary" org-agenda-toggle-diary
1593 :style toggle :selected org-agenda-include-diary
1594 :active (org-agenda-check-type nil 'agenda)]
1595 ["Use Time Grid" org-agenda-toggle-time-grid
1596 :style toggle :selected org-agenda-use-time-grid
1597 :active (org-agenda-check-type nil 'agenda)]
1598 "--"
1599 ["Show clock report" org-agenda-clockreport-mode
1600 :style toggle :selected org-agenda-clockreport-mode
1601 :active (org-agenda-check-type nil 'agenda)]
1602 ["Show some entry text" org-agenda-entry-text-mode
1603 :style toggle :selected org-agenda-entry-text-mode
1604 :active t]
1605 "--"
1606 ["Show Logbook entries" org-agenda-log-mode
1607 :style toggle :selected org-agenda-show-log
1608 :active (org-agenda-check-type nil 'agenda 'timeline)
1609 :keys "v l (or just l)"]
1610 ["Include archived trees" org-agenda-archives-mode
1611 :style toggle :selected org-agenda-archives-mode :active t
1612 :keys "v a"]
1613 ["Include archive files" (org-agenda-archives-mode t)
1614 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1615 :keys "v A"]
1616 "--"
1617 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1618 ["Write view to file" org-write-agenda t]
1619 ["Rebuild buffer" org-agenda-redo t]
1620 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1621 "--"
1622 ["Show original entry" org-agenda-show t]
1623 ["Go To (other window)" org-agenda-goto t]
1624 ["Go To (this window)" org-agenda-switch-to t]
1625 ["Follow Mode" org-agenda-follow-mode
1626 :style toggle :selected org-agenda-follow-mode :active t]
1627 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
1628 "--"
1629 ("TODO"
1630 ["Cycle TODO" org-agenda-todo t]
1631 ["Next TODO set" org-agenda-todo-nextset t]
1632 ["Previous TODO set" org-agenda-todo-previousset t]
1633 ["Add note" org-agenda-add-note t])
1634 ("Archive/Refile/Delete"
1635 ["Archive default" org-agenda-archive-default t]
1636 ["Archive default" org-agenda-archive-default-with-confirmation t]
1637 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
1638 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
1639 ["Archive subtree" org-agenda-archive t]
1640 "--"
1641 ["Refile" org-agenda-refile t]
1642 "--"
1643 ["Delete subtree" org-agenda-kill t])
1644 ("Bulk action"
1645 ["Mark entry" org-agenda-bulk-mark t]
1646 ["Unmark entry" org-agenda-bulk-unmark t]
1647 ["Act on all marked" org-agenda-bulk-action t]
1648 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
1649 "--"
1650 ("Tags and Properties"
1651 ["Show all Tags" org-agenda-show-tags t]
1652 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
1653 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
1654 "--"
1655 ["Column View" org-columns t])
1656 ("Deadline/Schedule"
1657 ["Schedule" org-agenda-schedule t]
1658 ["Set Deadline" org-agenda-deadline t]
1659 "--"
1660 ["Mark item" org-agenda-action :active t :keys "k m"]
1661 ["Show mark item" org-agenda-action :active t :keys "k v"]
1662 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
1663 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
1664 "--"
1665 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
1666 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
1667 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
1668 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
1669 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
1670 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
1671 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
1672 ("Clock and Effort"
1673 ["Clock in" org-agenda-clock-in t]
1674 ["Clock out" org-agenda-clock-out t]
1675 ["Clock cancel" org-agenda-clock-cancel t]
1676 ["Goto running clock" org-clock-goto t]
1677 "--"
1678 ["Set Effort" org-agenda-set-effort t]
1679 ["Change clocked effort" org-clock-modify-effort-estimate
1680 (org-clock-is-active)])
1681 ("Priority"
1682 ["Set Priority" org-agenda-priority t]
1683 ["Increase Priority" org-agenda-priority-up t]
1684 ["Decrease Priority" org-agenda-priority-down t]
1685 ["Show Priority" org-agenda-show-priority t])
1686 ("Calendar/Diary"
1687 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
1688 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
1689 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
1690 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
1691 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
1692 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
1693 "--"
1694 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
1695 "--"
1696 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
1697 "--"
1698 ("MobileOrg"
1699 ["Push Files and Views" org-mobile-push t]
1700 ["Get Captured and Flagged" org-mobile-pull t]
1701 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
1702 ["Show note / unflag" org-agenda-show-the-flagging-note t]
1703 "--"
1704 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
1705 "--"
1706 ["Quit" org-agenda-quit t]
1707 ["Exit and Release Buffers" org-agenda-exit t]
1710 ;;; Agenda undo
1712 (defvar org-agenda-allow-remote-undo t
1713 "Non-nil means, allow remote undo from the agenda buffer.")
1714 (defvar org-agenda-undo-list nil
1715 "List of undoable operations in the agenda since last refresh.")
1716 (defvar org-agenda-undo-has-started-in nil
1717 "Buffers that have already seen `undo-start' in the current undo sequence.")
1718 (defvar org-agenda-pending-undo-list nil
1719 "In a series of undo commands, this is the list of remaining undo items.")
1722 (defun org-agenda-undo ()
1723 "Undo a remote editing step in the agenda.
1724 This undoes changes both in the agenda buffer and in the remote buffer
1725 that have been changed along."
1726 (interactive)
1727 (or org-agenda-allow-remote-undo
1728 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
1729 (if (not (eq this-command last-command))
1730 (setq org-agenda-undo-has-started-in nil
1731 org-agenda-pending-undo-list org-agenda-undo-list))
1732 (if (not org-agenda-pending-undo-list)
1733 (error "No further undo information"))
1734 (let* ((entry (pop org-agenda-pending-undo-list))
1735 buf line cmd rembuf)
1736 (setq cmd (pop entry) line (pop entry))
1737 (setq rembuf (nth 2 entry))
1738 (org-with-remote-undo rembuf
1739 (while (bufferp (setq buf (pop entry)))
1740 (if (pop entry)
1741 (with-current-buffer buf
1742 (let ((last-undo-buffer buf)
1743 (inhibit-read-only t))
1744 (unless (memq buf org-agenda-undo-has-started-in)
1745 (push buf org-agenda-undo-has-started-in)
1746 (make-local-variable 'pending-undo-list)
1747 (undo-start))
1748 (while (and pending-undo-list
1749 (listp pending-undo-list)
1750 (not (car pending-undo-list)))
1751 (pop pending-undo-list))
1752 (undo-more 1))))))
1753 (org-goto-line line)
1754 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
1756 (defun org-verify-change-for-undo (l1 l2)
1757 "Verify that a real change occurred between the undo lists L1 and L2."
1758 (while (and l1 (listp l1) (null (car l1))) (pop l1))
1759 (while (and l2 (listp l2) (null (car l2))) (pop l2))
1760 (not (eq l1 l2)))
1762 ;;; Agenda dispatch
1764 (defvar org-agenda-restrict nil)
1765 (defvar org-agenda-restrict-begin (make-marker))
1766 (defvar org-agenda-restrict-end (make-marker))
1767 (defvar org-agenda-last-dispatch-buffer nil)
1768 (defvar org-agenda-overriding-restriction nil)
1770 ;;;###autoload
1771 (defun org-agenda (&optional arg keys restriction)
1772 "Dispatch agenda commands to collect entries to the agenda buffer.
1773 Prompts for a command to execute. Any prefix arg will be passed
1774 on to the selected command. The default selections are:
1776 a Call `org-agenda-list' to display the agenda for current day or week.
1777 t Call `org-todo-list' to display the global todo list.
1778 T Call `org-todo-list' to display the global todo list, select only
1779 entries with a specific TODO keyword (the user gets a prompt).
1780 m Call `org-tags-view' to display headlines with tags matching
1781 a condition (the user is prompted for the condition).
1782 M Like `m', but select only TODO entries, no ordinary headlines.
1783 L Create a timeline for the current buffer.
1784 e Export views to associated files.
1785 s Search entries for keywords.
1786 / Multi occur across all agenda files and also files listed
1787 in `org-agenda-text-search-extra-files'.
1788 < Restrict agenda commands to buffer, subtree, or region.
1789 Press several times to get the desired effect.
1790 > Remove a previous restriction.
1791 # List \"stuck\" projects.
1792 ! Configure what \"stuck\" means.
1793 C Configure custom agenda commands.
1795 More commands can be added by configuring the variable
1796 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
1797 searches can be pre-defined in this way.
1799 If the current buffer is in Org-mode and visiting a file, you can also
1800 first press `<' once to indicate that the agenda should be temporarily
1801 \(until the next use of \\[org-agenda]) restricted to the current file.
1802 Pressing `<' twice means to restrict to the current subtree or region
1803 \(if active)."
1804 (interactive "P")
1805 (catch 'exit
1806 (let* ((prefix-descriptions nil)
1807 (org-agenda-window-setup (if (equal (buffer-name)
1808 org-agenda-buffer-name)
1809 'current-window
1810 org-agenda-window-setup))
1811 (org-agenda-custom-commands-orig org-agenda-custom-commands)
1812 (org-agenda-custom-commands
1813 ;; normalize different versions
1814 (delq nil
1815 (mapcar
1816 (lambda (x)
1817 (cond ((stringp (cdr x))
1818 (push x prefix-descriptions)
1819 nil)
1820 ((stringp (nth 1 x)) x)
1821 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1822 (t (cons (car x) (cons "" (cdr x))))))
1823 org-agenda-custom-commands)))
1824 (buf (current-buffer))
1825 (bfn (buffer-file-name (buffer-base-buffer)))
1826 entry key type match lprops ans)
1827 ;; Turn off restriction unless there is an overriding one,
1828 (unless org-agenda-overriding-restriction
1829 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
1830 ;; There is a request to keep the file list in place
1831 (put 'org-agenda-files 'org-restrict nil))
1832 (setq org-agenda-restrict nil)
1833 (move-marker org-agenda-restrict-begin nil)
1834 (move-marker org-agenda-restrict-end nil))
1835 ;; Delete old local properties
1836 (put 'org-agenda-redo-command 'org-lprops nil)
1837 ;; Remember where this call originated
1838 (setq org-agenda-last-dispatch-buffer (current-buffer))
1839 (unless keys
1840 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
1841 keys (car ans)
1842 restriction (cdr ans)))
1843 ;; Establish the restriction, if any
1844 (when (and (not org-agenda-overriding-restriction) restriction)
1845 (put 'org-agenda-files 'org-restrict (list bfn))
1846 (cond
1847 ((eq restriction 'region)
1848 (setq org-agenda-restrict t)
1849 (move-marker org-agenda-restrict-begin (region-beginning))
1850 (move-marker org-agenda-restrict-end (region-end)))
1851 ((eq restriction 'subtree)
1852 (save-excursion
1853 (setq org-agenda-restrict t)
1854 (org-back-to-heading t)
1855 (move-marker org-agenda-restrict-begin (point))
1856 (move-marker org-agenda-restrict-end
1857 (progn (org-end-of-subtree t)))))))
1859 (require 'calendar) ; FIXME: can we avoid this for some commands?
1860 ;; For example the todo list should not need it (but does...)
1861 (cond
1862 ((setq entry (assoc keys org-agenda-custom-commands))
1863 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
1864 (progn
1865 (setq type (nth 2 entry) match (eval (nth 3 entry))
1866 lprops (nth 4 entry))
1867 (put 'org-agenda-redo-command 'org-lprops lprops)
1868 (cond
1869 ((eq type 'agenda)
1870 (org-let lprops '(org-agenda-list current-prefix-arg)))
1871 ((eq type 'alltodo)
1872 (org-let lprops '(org-todo-list current-prefix-arg)))
1873 ((eq type 'search)
1874 (org-let lprops '(org-search-view current-prefix-arg match nil)))
1875 ((eq type 'stuck)
1876 (org-let lprops '(org-agenda-list-stuck-projects
1877 current-prefix-arg)))
1878 ((eq type 'tags)
1879 (org-let lprops '(org-tags-view current-prefix-arg match)))
1880 ((eq type 'tags-todo)
1881 (org-let lprops '(org-tags-view '(4) match)))
1882 ((eq type 'todo)
1883 (org-let lprops '(org-todo-list match)))
1884 ((eq type 'tags-tree)
1885 (org-check-for-org-mode)
1886 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
1887 ((eq type 'todo-tree)
1888 (org-check-for-org-mode)
1889 (org-let lprops
1890 '(org-occur (concat "^" outline-regexp "[ \t]*"
1891 (regexp-quote match) "\\>"))))
1892 ((eq type 'occur-tree)
1893 (org-check-for-org-mode)
1894 (org-let lprops '(org-occur match)))
1895 ((functionp type)
1896 (org-let lprops '(funcall type match)))
1897 ((fboundp type)
1898 (org-let lprops '(funcall type match)))
1899 (t (error "Invalid custom agenda command type %s" type))))
1900 (org-run-agenda-series (nth 1 entry) (cddr entry))))
1901 ((equal keys "C")
1902 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
1903 (customize-variable 'org-agenda-custom-commands))
1904 ((equal keys "a") (call-interactively 'org-agenda-list))
1905 ((equal keys "s") (call-interactively 'org-search-view))
1906 ((equal keys "t") (call-interactively 'org-todo-list))
1907 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
1908 ((equal keys "m") (call-interactively 'org-tags-view))
1909 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
1910 ((equal keys "e") (call-interactively 'org-store-agenda-views))
1911 ((equal keys "?") (org-tags-view nil "+FLAGGED")
1912 (org-add-hook
1913 'post-command-hook
1914 (lambda ()
1915 (unless (current-message)
1916 (let* ((m (org-agenda-get-any-marker))
1917 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
1918 (when note
1919 (message (concat
1920 "FLAGGING-NOTE ([?] for more info): "
1921 (org-add-props
1922 (replace-regexp-in-string
1923 "\\\\n" "//"
1924 (copy-sequence note))
1925 nil 'face 'org-warning)))))))
1926 t t))
1927 ((equal keys "L")
1928 (unless (org-mode-p)
1929 (error "This is not an Org-mode file"))
1930 (unless restriction
1931 (put 'org-agenda-files 'org-restrict (list bfn))
1932 (org-call-with-arg 'org-timeline arg)))
1933 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
1934 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
1935 ((equal keys "!") (customize-variable 'org-stuck-projects))
1936 (t (error "Invalid agenda key"))))))
1938 (defun org-agenda-normalize-custom-commands (cmds)
1939 (delq nil
1940 (mapcar
1941 (lambda (x)
1942 (cond ((stringp (cdr x)) nil)
1943 ((stringp (nth 1 x)) x)
1944 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1945 (t (cons (car x) (cons "" (cdr x))))))
1946 cmds)))
1948 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
1949 "The user interface for selecting an agenda command."
1950 (catch 'exit
1951 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
1952 (restrict-ok (and bfn (org-mode-p)))
1953 (region-p (org-region-active-p))
1954 (custom org-agenda-custom-commands)
1955 (selstring "")
1956 restriction second-time
1957 c entry key type match prefixes rmheader header-end custom1 desc)
1958 (save-window-excursion
1959 (delete-other-windows)
1960 (org-switch-to-buffer-other-window " *Agenda Commands*")
1961 (erase-buffer)
1962 (insert (eval-when-compile
1963 (let ((header
1965 Press key for an agenda command: < Buffer, subtree/region restriction
1966 -------------------------------- > Remove restriction
1967 a Agenda for current week or day e Export agenda views
1968 t List of all TODO entries T Entries with special TODO kwd
1969 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
1970 L Timeline for current buffer # List stuck projects (!=configure)
1971 s Search for keywords C Configure custom agenda commands
1972 / Multi-occur ? Find :FLAGGED: entries
1974 (start 0))
1975 (while (string-match
1976 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
1977 header start)
1978 (setq start (match-end 0))
1979 (add-text-properties (match-beginning 2) (match-end 2)
1980 '(face bold) header))
1981 header)))
1982 (setq header-end (move-marker (make-marker) (point)))
1983 (while t
1984 (setq custom1 custom)
1985 (when (eq rmheader t)
1986 (org-goto-line 1)
1987 (re-search-forward ":" nil t)
1988 (delete-region (match-end 0) (point-at-eol))
1989 (forward-char 1)
1990 (looking-at "-+")
1991 (delete-region (match-end 0) (point-at-eol))
1992 (move-marker header-end (match-end 0)))
1993 (goto-char header-end)
1994 (delete-region (point) (point-max))
1995 (while (setq entry (pop custom1))
1996 (setq key (car entry) desc (nth 1 entry)
1997 type (nth 2 entry)
1998 match (nth 3 entry))
1999 (if (> (length key) 1)
2000 (add-to-list 'prefixes (string-to-char key))
2001 (insert
2002 (format
2003 "\n%-4s%-14s: %s"
2004 (org-add-props (copy-sequence key)
2005 '(face bold))
2006 (cond
2007 ((string-match "\\S-" desc) desc)
2008 ((eq type 'agenda) "Agenda for current week or day")
2009 ((eq type 'alltodo) "List of all TODO entries")
2010 ((eq type 'search) "Word search")
2011 ((eq type 'stuck) "List of stuck projects")
2012 ((eq type 'todo) "TODO keyword")
2013 ((eq type 'tags) "Tags query")
2014 ((eq type 'tags-todo) "Tags (TODO)")
2015 ((eq type 'tags-tree) "Tags tree")
2016 ((eq type 'todo-tree) "TODO kwd tree")
2017 ((eq type 'occur-tree) "Occur tree")
2018 ((functionp type) (if (symbolp type)
2019 (symbol-name type)
2020 "Lambda expression"))
2021 (t "???"))
2022 (cond
2023 ((stringp match)
2024 (setq match (copy-sequence match))
2025 (org-add-props match nil 'face 'org-warning))
2026 (match
2027 (format "set of %d commands" (length match)))
2028 (t ""))))))
2029 (when prefixes
2030 (mapc (lambda (x)
2031 (insert
2032 (format "\n%s %s"
2033 (org-add-props (char-to-string x)
2034 nil 'face 'bold)
2035 (or (cdr (assoc (concat selstring (char-to-string x))
2036 prefix-descriptions))
2037 "Prefix key"))))
2038 prefixes))
2039 (goto-char (point-min))
2040 (if second-time
2041 (if (not (pos-visible-in-window-p (point-max)))
2042 (org-fit-window-to-buffer))
2043 (setq second-time t)
2044 (org-fit-window-to-buffer))
2045 (message "Press key for agenda command%s:"
2046 (if (or restrict-ok org-agenda-overriding-restriction)
2047 (if org-agenda-overriding-restriction
2048 " (restriction lock active)"
2049 (if restriction
2050 (format " (restricted to %s)" restriction)
2051 " (unrestricted)"))
2052 ""))
2053 (setq c (read-char-exclusive))
2054 (message "")
2055 (cond
2056 ((assoc (char-to-string c) custom)
2057 (setq selstring (concat selstring (char-to-string c)))
2058 (throw 'exit (cons selstring restriction)))
2059 ((memq c prefixes)
2060 (setq selstring (concat selstring (char-to-string c))
2061 prefixes nil
2062 rmheader (or rmheader t)
2063 custom (delq nil (mapcar
2064 (lambda (x)
2065 (if (or (= (length (car x)) 1)
2066 (/= (string-to-char (car x)) c))
2068 (cons (substring (car x) 1) (cdr x))))
2069 custom))))
2070 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2071 (message "Restriction is only possible in Org-mode buffers")
2072 (ding) (sit-for 1))
2073 ((eq c ?1)
2074 (org-agenda-remove-restriction-lock 'noupdate)
2075 (setq restriction 'buffer))
2076 ((eq c ?0)
2077 (org-agenda-remove-restriction-lock 'noupdate)
2078 (setq restriction (if region-p 'region 'subtree)))
2079 ((eq c ?<)
2080 (org-agenda-remove-restriction-lock 'noupdate)
2081 (setq restriction
2082 (cond
2083 ((eq restriction 'buffer)
2084 (if region-p 'region 'subtree))
2085 ((memq restriction '(subtree region))
2086 nil)
2087 (t 'buffer))))
2088 ((eq c ?>)
2089 (org-agenda-remove-restriction-lock 'noupdate)
2090 (setq restriction nil))
2091 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2092 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2093 ((and (> (length selstring) 0) (eq c ?\d))
2094 (delete-window)
2095 (org-agenda-get-restriction-and-command prefix-descriptions))
2097 ((equal c ?q) (error "Abort"))
2098 (t (error "Invalid key %c" c))))))))
2100 (defun org-run-agenda-series (name series)
2101 (org-let (nth 1 series) '(org-prepare-agenda name))
2102 (let* ((org-agenda-multi t)
2103 (redo (list 'org-run-agenda-series name (list 'quote series)))
2104 (cmds (car series))
2105 (gprops (nth 1 series))
2106 match ;; The byte compiler incorrectly complains about this. Keep it!
2107 cmd type lprops)
2108 (while (setq cmd (pop cmds))
2109 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2110 (cond
2111 ((eq type 'agenda)
2112 (org-let2 gprops lprops
2113 '(call-interactively 'org-agenda-list)))
2114 ((eq type 'alltodo)
2115 (org-let2 gprops lprops
2116 '(call-interactively 'org-todo-list)))
2117 ((eq type 'search)
2118 (org-let2 gprops lprops
2119 '(org-search-view current-prefix-arg match nil)))
2120 ((eq type 'stuck)
2121 (org-let2 gprops lprops
2122 '(call-interactively 'org-agenda-list-stuck-projects)))
2123 ((eq type 'tags)
2124 (org-let2 gprops lprops
2125 '(org-tags-view current-prefix-arg match)))
2126 ((eq type 'tags-todo)
2127 (org-let2 gprops lprops
2128 '(org-tags-view '(4) match)))
2129 ((eq type 'todo)
2130 (org-let2 gprops lprops
2131 '(org-todo-list match)))
2132 ((fboundp type)
2133 (org-let2 gprops lprops
2134 '(funcall type match)))
2135 (t (error "Invalid type in command series"))))
2136 (widen)
2137 (setq org-agenda-redo-command redo)
2138 (goto-char (point-min)))
2139 (org-fit-agenda-window)
2140 (org-let (nth 1 series) '(org-finalize-agenda)))
2142 ;;;###autoload
2143 (defmacro org-batch-agenda (cmd-key &rest parameters)
2144 "Run an agenda command in batch mode and send the result to STDOUT.
2145 If CMD-KEY is a string of length 1, it is used as a key in
2146 `org-agenda-custom-commands' and triggers this command. If it is a
2147 longer string it is used as a tags/todo match string.
2148 Paramters are alternating variable names and values that will be bound
2149 before running the agenda command."
2150 (let (pars)
2151 (while parameters
2152 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2153 (if (> (length cmd-key) 2)
2154 (eval (list 'let (nreverse pars)
2155 (list 'org-tags-view nil cmd-key)))
2156 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2157 (set-buffer org-agenda-buffer-name)
2158 (princ (org-encode-for-stdout (buffer-string)))))
2160 ;(defun org-encode-for-stdout (string)
2161 ; (if (fboundp 'encode-coding-string)
2162 ; (encode-coding-string string buffer-file-coding-system)
2163 ; string))
2165 (defun org-encode-for-stdout (string)
2166 string)
2168 (defvar org-agenda-info nil)
2170 ;;;###autoload
2171 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2172 "Run an agenda command in batch mode and send the result to STDOUT.
2173 If CMD-KEY is a string of length 1, it is used as a key in
2174 `org-agenda-custom-commands' and triggers this command. If it is a
2175 longer string it is used as a tags/todo match string.
2176 Paramters are alternating variable names and values that will be bound
2177 before running the agenda command.
2179 The output gives a line for each selected agenda item. Each
2180 item is a list of comma-separated values, like this:
2182 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2184 category The category of the item
2185 head The headline, without TODO kwd, TAGS and PRIORITY
2186 type The type of the agenda entry, can be
2187 todo selected in TODO match
2188 tagsmatch selected in tags match
2189 diary imported from diary
2190 deadline a deadline on given date
2191 scheduled scheduled on given date
2192 timestamp entry has timestamp on given date
2193 closed entry was closed on given date
2194 upcoming-deadline warning about deadline
2195 past-scheduled forwarded scheduled item
2196 block entry has date block including g. date
2197 todo The todo keyword, if any
2198 tags All tags including inherited ones, separated by colons
2199 date The relevant date, like 2007-2-14
2200 time The time, like 15:00-16:50
2201 extra Sting with extra planning info
2202 priority-l The priority letter if any was given
2203 priority-n The computed numerical priority
2204 agenda-day The day in the agenda where this is listed"
2206 (let (pars)
2207 (while parameters
2208 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2209 (push (list 'org-agenda-remove-tags t) pars)
2210 (if (> (length cmd-key) 2)
2211 (eval (list 'let (nreverse pars)
2212 (list 'org-tags-view nil cmd-key)))
2213 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2214 (set-buffer org-agenda-buffer-name)
2215 (let* ((lines (org-split-string (buffer-string) "\n"))
2216 line)
2217 (while (setq line (pop lines))
2218 (catch 'next
2219 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2220 (setq org-agenda-info
2221 (org-fix-agenda-info (text-properties-at 0 line)))
2222 (princ
2223 (org-encode-for-stdout
2224 (mapconcat 'org-agenda-export-csv-mapper
2225 '(org-category txt type todo tags date time-of-day extra
2226 priority-letter priority agenda-day)
2227 ",")))
2228 (princ "\n"))))))
2230 (defun org-fix-agenda-info (props)
2231 "Make sure all properties on an agenda item have a canonical form,
2232 so the export commands can easily use it."
2233 (let (tmp re)
2234 (when (setq tmp (plist-get props 'tags))
2235 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2236 (when (setq tmp (plist-get props 'date))
2237 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2238 (let ((calendar-date-display-form '(year "-" month "-" day)))
2239 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2241 (setq tmp (calendar-date-string tmp)))
2242 (setq props (plist-put props 'date tmp)))
2243 (when (setq tmp (plist-get props 'day))
2244 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2245 (let ((calendar-date-display-form '(year "-" month "-" day)))
2246 (setq tmp (calendar-date-string tmp)))
2247 (setq props (plist-put props 'day tmp))
2248 (setq props (plist-put props 'agenda-day tmp)))
2249 (when (setq tmp (plist-get props 'txt))
2250 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2251 (plist-put props 'priority-letter (match-string 1 tmp))
2252 (setq tmp (replace-match "" t t tmp)))
2253 (when (and (setq re (plist-get props 'org-todo-regexp))
2254 (setq re (concat "\\`\\.*" re " ?"))
2255 (string-match re tmp))
2256 (plist-put props 'todo (match-string 1 tmp))
2257 (setq tmp (replace-match "" t t tmp)))
2258 (plist-put props 'txt tmp)))
2259 props)
2261 (defun org-agenda-export-csv-mapper (prop)
2262 (let ((res (plist-get org-agenda-info prop)))
2263 (setq res
2264 (cond
2265 ((not res) "")
2266 ((stringp res) res)
2267 (t (prin1-to-string res))))
2268 (while (string-match "," res)
2269 (setq res (replace-match ";" t t res)))
2270 (org-trim res)))
2273 ;;;###autoload
2274 (defun org-store-agenda-views (&rest parameters)
2275 (interactive)
2276 (eval (list 'org-batch-store-agenda-views)))
2278 ;; FIXME, why is this a macro?????
2279 ;;;###autoload
2280 (defmacro org-batch-store-agenda-views (&rest parameters)
2281 "Run all custom agenda commands that have a file argument."
2282 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2283 (pop-up-frames nil)
2284 (dir default-directory)
2285 pars cmd thiscmdkey files opts cmd-or-set)
2286 (while parameters
2287 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2288 (setq pars (reverse pars))
2289 (save-window-excursion
2290 (while cmds
2291 (setq cmd (pop cmds)
2292 thiscmdkey (car cmd)
2293 cmd-or-set (nth 2 cmd)
2294 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2295 files (nth (if (listp cmd-or-set) 4 5) cmd))
2296 (if (stringp files) (setq files (list files)))
2297 (when files
2298 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2299 (list 'org-agenda nil thiscmdkey)))
2300 (set-buffer org-agenda-buffer-name)
2301 (while files
2302 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2303 (list 'org-write-agenda
2304 (expand-file-name (pop files) dir) nil t))))
2305 (and (get-buffer org-agenda-buffer-name)
2306 (kill-buffer org-agenda-buffer-name)))))))
2308 (defun org-agenda-mark-header-line (pos)
2309 "Mark the line at POS as an agenda structure header."
2310 (save-excursion
2311 (goto-char pos)
2312 (put-text-property (point-at-bol) (point-at-eol)
2313 'org-agenda-structural-header t)
2314 (when org-agenda-title-append
2315 (put-text-property (point-at-bol) (point-at-eol)
2316 'org-agenda-title-append org-agenda-title-append))))
2318 (defvar org-mobile-creating-agendas)
2319 (defun org-write-agenda (file &optional open nosettings)
2320 "Write the current buffer (an agenda view) as a file.
2321 Depending on the extension of the file name, plain text (.txt),
2322 HTML (.html or .htm) or Postscript (.ps) is produced.
2323 If the extension is .ics, run icalendar export over all files used
2324 to construct the agenda and limit the export to entries listed in the
2325 agenda now.
2326 With prefix argument OPEN, open the new file immediately.
2327 If NOSETTINGS is given, do not scope the settings of
2328 `org-agenda-exporter-settings' into the export commands. This is used when
2329 the settings have already been scoped and we do not wish to overrule other,
2330 higher priority settings."
2331 (interactive "FWrite agenda to file: \nP")
2332 (if (not (file-writable-p file))
2333 (error "Cannot write agenda to file %s" file))
2334 (cond
2335 ((string-match "\\.html?\\'" file) (require 'htmlize))
2336 ((string-match "\\.ps\\'" file) (require 'ps-print)))
2337 (org-let (if nosettings nil org-agenda-exporter-settings)
2338 '(save-excursion
2339 (save-window-excursion
2340 (org-agenda-mark-filtered-text)
2341 (let ((bs (copy-sequence (buffer-string))) beg)
2342 (org-agenda-unmark-filtered-text)
2343 (with-temp-buffer
2344 (insert bs)
2345 (org-agenda-remove-marked-text 'org-filtered)
2346 (while (setq beg (text-property-any (point-min) (point-max)
2347 'org-filtered t))
2348 (delete-region
2349 beg (or (next-single-property-change beg 'org-filtered)
2350 (point-max))))
2351 (run-hooks 'org-agenda-before-write-hook)
2352 (cond
2353 ((org-bound-and-true-p org-mobile-creating-agendas)
2354 (org-mobile-write-agenda-for-mobile file))
2355 ((string-match "\\.html?\\'" file)
2356 (set-buffer (htmlize-buffer (current-buffer)))
2358 (when (and org-agenda-export-html-style
2359 (string-match "<style>" org-agenda-export-html-style))
2360 ;; replace <style> section with org-agenda-export-html-style
2361 (goto-char (point-min))
2362 (kill-region (- (search-forward "<style") 6)
2363 (search-forward "</style>"))
2364 (insert org-agenda-export-html-style))
2365 (write-file file)
2366 (kill-buffer (current-buffer))
2367 (message "HTML written to %s" file))
2368 ((string-match "\\.ps\\'" file)
2369 (require 'ps-print)
2370 (flet ((ps-get-buffer-name () "Agenda View"))
2371 (ps-print-buffer-with-faces file))
2372 (message "Postscript written to %s" file))
2373 ((string-match "\\.pdf\\'" file)
2374 (require 'ps-print)
2375 (flet ((ps-get-buffer-name () "Agenda View"))
2376 (ps-print-buffer-with-faces
2377 (concat (file-name-sans-extension file) ".ps")))
2378 (call-process "ps2pdf" nil nil nil
2379 (expand-file-name
2380 (concat (file-name-sans-extension file) ".ps"))
2381 (expand-file-name file))
2382 (message "PDF written to %s" file))
2383 ((string-match "\\.ics\\'" file)
2384 (require 'org-icalendar)
2385 (let ((org-agenda-marker-table
2386 (org-create-marker-find-array
2387 (org-agenda-collect-markers)))
2388 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2389 (org-combined-agenda-icalendar-file file))
2390 (apply 'org-export-icalendar 'combine
2391 (org-agenda-files nil 'ifmode))))
2393 (let ((bs (buffer-string)))
2394 (find-file file)
2395 (erase-buffer)
2396 (insert bs)
2397 (save-buffer 0)
2398 (kill-buffer (current-buffer))
2399 (message "Plain text written to %s" file))))))))
2400 (set-buffer org-agenda-buffer-name))
2401 (when open (org-open-file file)))
2403 (defvar org-agenda-filter-overlays nil)
2405 (defun org-agenda-mark-filtered-text ()
2406 "Mark all text hidden by filtering with a text property."
2407 (let ((inhibit-read-only t))
2408 (mapc
2409 (lambda (o)
2410 (when (equal (org-overlay-buffer o) (current-buffer))
2411 (put-text-property
2412 (org-overlay-start o) (org-overlay-end o)
2413 'org-filtered t)))
2414 org-agenda-filter-overlays)))
2416 (defun org-agenda-unmark-filtered-text ()
2417 "Remove the filtering text property."
2418 (let ((inhibit-read-only t))
2419 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2421 (defun org-agenda-remove-marked-text (property &optional value)
2422 "Delete all text marked with VALUE of PROPERTY.
2423 VALUE defaults to t."
2424 (let (beg)
2425 (setq value (or value t))
2426 (while (setq beg (text-property-any (point-min) (point-max)
2427 property value))
2428 (delete-region
2429 beg (or (next-single-property-change beg 'org-filtered)
2430 (point-max))))))
2432 (defun org-agenda-add-entry-text ()
2433 "Add entry text to agenda lines.
2434 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2435 entry text following headings shown in the agenda.
2436 Drawers will be excluded, also the line with scheduling/deadline info."
2437 (when (and (> org-agenda-add-entry-text-maxlines 0)
2438 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2439 (let (m txt)
2440 (goto-char (point-min))
2441 (while (not (eobp))
2442 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2443 (beginning-of-line 2)
2444 (setq txt (org-agenda-get-some-entry-text
2445 m org-agenda-add-entry-text-maxlines " > "))
2446 (end-of-line 1)
2447 (if (string-match "\\S-" txt) (insert "\n" txt)))))))
2449 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2450 &rest keep)
2451 "Extract entry text from MARKER, at most N-LINES lines.
2452 This will ignore drawers etc, just get the text.
2453 If INDENT is given, prefix every line with this string. If KEEP is
2454 given, it is a list of symbols, defining stuff that should not be
2455 removed from the entry content. Currently only `planning' is allowed here."
2456 (let (txt drawer-re kwd-time-re ind)
2457 (save-excursion
2458 (with-current-buffer (marker-buffer marker)
2459 (if (not (org-mode-p))
2460 (setq txt "")
2461 (save-excursion
2462 (save-restriction
2463 (widen)
2464 (goto-char marker)
2465 (end-of-line 1)
2466 (setq txt (buffer-substring
2467 (min (1+ (point)) (point-max))
2468 (progn (outline-next-heading) (point)))
2469 drawer-re org-drawer-regexp
2470 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2471 ".*\n?"))
2472 (with-temp-buffer
2473 (insert txt)
2474 (when org-agenda-add-entry-text-descriptive-links
2475 (goto-char (point-min))
2476 (while (org-activate-bracket-links (point-max))
2477 (add-text-properties (match-beginning 0) (match-end 0)
2478 '(face org-link))))
2479 (goto-char (point-min))
2480 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2481 (set-text-properties (match-beginning 0) (match-end 0)
2482 nil))
2483 (goto-char (point-min))
2484 (while (re-search-forward drawer-re nil t)
2485 (delete-region
2486 (match-beginning 0)
2487 (progn (re-search-forward
2488 "^[ \t]*:END:.*\n?" nil 'move)
2489 (point))))
2490 (unless (member 'planning keep)
2491 (goto-char (point-min))
2492 (while (re-search-forward kwd-time-re nil t)
2493 (replace-match "")))
2494 (goto-char (point-min))
2495 (when org-agenda-entry-text-exclude-regexps
2496 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2497 (while (setq re (pop re-list))
2498 (goto-char (point-min))
2499 (while (re-search-forward re nil t)
2500 (replace-match "")))))
2501 (goto-char (point-max))
2502 (skip-chars-backward " \t\n")
2503 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2505 ;; find and remove min common indentation
2506 (goto-char (point-min))
2507 (untabify (point-min) (point-max))
2508 (setq ind (org-get-indentation))
2509 (while (not (eobp))
2510 (unless (looking-at "[ \t]*$")
2511 (setq ind (min ind (org-get-indentation))))
2512 (beginning-of-line 2))
2513 (goto-char (point-min))
2514 (while (not (eobp))
2515 (unless (looking-at "[ \t]*$")
2516 (move-to-column ind)
2517 (delete-region (point-at-bol) (point)))
2518 (beginning-of-line 2))
2520 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2522 (goto-char (point-min))
2523 (when indent
2524 (while (and (not (eobp)) (re-search-forward "^" nil t))
2525 (replace-match indent t t)))
2526 (goto-char (point-min))
2527 (while (looking-at "[ \t]*\n") (replace-match ""))
2528 (goto-char (point-max))
2529 (when (> (org-current-line)
2530 n-lines)
2531 (org-goto-line (1+ n-lines))
2532 (backward-char 1))
2533 (setq txt (buffer-substring (point-min) (point)))))))))
2534 txt))
2536 (defun org-agenda-collect-markers ()
2537 "Collect the markers pointing to entries in the agenda buffer."
2538 (let (m markers)
2539 (save-excursion
2540 (goto-char (point-min))
2541 (while (not (eobp))
2542 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2543 (org-get-at-bol 'org-marker)))
2544 (push m markers))
2545 (beginning-of-line 2)))
2546 (nreverse markers)))
2548 (defun org-create-marker-find-array (marker-list)
2549 "Create a alist of files names with all marker positions in that file."
2550 (let (f tbl m a p)
2551 (while (setq m (pop marker-list))
2552 (setq p (marker-position m)
2553 f (buffer-file-name (or (buffer-base-buffer
2554 (marker-buffer m))
2555 (marker-buffer m))))
2556 (if (setq a (assoc f tbl))
2557 (push (marker-position m) (cdr a))
2558 (push (list f p) tbl)))
2559 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2560 tbl)))
2562 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
2563 (defun org-check-agenda-marker-table ()
2564 "Check of the current entry is on the marker list."
2565 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2567 (and (setq a (assoc file org-agenda-marker-table))
2568 (save-match-data
2569 (save-excursion
2570 (org-back-to-heading t)
2571 (member (point) (cdr a)))))))
2573 (defun org-check-for-org-mode ()
2574 "Make sure current buffer is in org-mode. Error if not."
2575 (or (org-mode-p)
2576 (error "Cannot execute org-mode agenda command on buffer in %s"
2577 major-mode)))
2579 (defun org-fit-agenda-window ()
2580 "Fit the window to the buffer size."
2581 (and (memq org-agenda-window-setup '(reorganize-frame))
2582 (fboundp 'fit-window-to-buffer)
2583 (org-fit-window-to-buffer
2585 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
2586 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
2588 ;;; Agenda prepare and finalize
2590 (defvar org-agenda-multi nil) ; dynamically scoped
2591 (defvar org-agenda-buffer-name "*Org Agenda*")
2592 (defvar org-pre-agenda-window-conf nil)
2593 (defvar org-agenda-columns-active nil)
2594 (defvar org-agenda-name nil)
2595 (defvar org-agenda-filter nil)
2596 (defvar org-agenda-filter-preset nil
2597 "A preset of the tags filter used for secondary agenda filtering.
2598 This must be a list of strings, each string must be a single tag preceeded
2599 by \"+\" or \"-\".
2600 This variable should not be set directly, but agenda custom commands can
2601 bind it in the options section.")
2603 (defun org-prepare-agenda (&optional name)
2604 (setq org-todo-keywords-for-agenda nil)
2605 (setq org-done-keywords-for-agenda nil)
2606 (setq org-drawers-for-agenda nil)
2607 (setq org-agenda-filter nil)
2608 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
2609 (if org-agenda-multi
2610 (progn
2611 (setq buffer-read-only nil)
2612 (goto-char (point-max))
2613 (unless (or (bobp) org-agenda-compact-blocks)
2614 (insert "\n"
2615 (if (stringp org-agenda-block-separator)
2616 org-agenda-block-separator
2617 (make-string (window-width) org-agenda-block-separator))
2618 "\n"))
2619 (narrow-to-region (point) (point-max)))
2620 (org-agenda-reset-markers)
2621 (setq org-agenda-contributing-files nil)
2622 (setq org-agenda-columns-active nil)
2623 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
2624 (setq org-todo-keywords-for-agenda
2625 (org-uniquify org-todo-keywords-for-agenda))
2626 (setq org-done-keywords-for-agenda
2627 (org-uniquify org-done-keywords-for-agenda))
2628 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
2629 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
2630 (awin (get-buffer-window abuf)))
2631 (cond
2632 ((equal (current-buffer) abuf) nil)
2633 (awin (select-window awin))
2634 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
2635 ((equal org-agenda-window-setup 'current-window)
2636 (switch-to-buffer abuf))
2637 ((equal org-agenda-window-setup 'other-window)
2638 (org-switch-to-buffer-other-window abuf))
2639 ((equal org-agenda-window-setup 'other-frame)
2640 (switch-to-buffer-other-frame abuf))
2641 ((equal org-agenda-window-setup 'reorganize-frame)
2642 (delete-other-windows)
2643 (org-switch-to-buffer-other-window abuf))))
2644 (setq buffer-read-only nil)
2645 (let ((inhibit-read-only t)) (erase-buffer))
2646 (org-agenda-mode)
2647 (and name (not org-agenda-name)
2648 (org-set-local 'org-agenda-name name)))
2649 (setq buffer-read-only nil))
2651 (defun org-finalize-agenda ()
2652 "Finishing touch for the agenda buffer, called just before displaying it."
2653 (unless org-agenda-multi
2654 (save-excursion
2655 (let ((inhibit-read-only t))
2656 (goto-char (point-min))
2657 (while (org-activate-bracket-links (point-max))
2658 (add-text-properties (match-beginning 0) (match-end 0)
2659 '(face org-link)))
2660 (org-agenda-align-tags)
2661 (unless org-agenda-with-colors
2662 (remove-text-properties (point-min) (point-max) '(face nil))))
2663 (if (and (boundp 'org-agenda-overriding-columns-format)
2664 org-agenda-overriding-columns-format)
2665 (org-set-local 'org-agenda-overriding-columns-format
2666 org-agenda-overriding-columns-format))
2667 (if (and (boundp 'org-agenda-view-columns-initially)
2668 org-agenda-view-columns-initially)
2669 (org-agenda-columns))
2670 (when org-agenda-fontify-priorities
2671 (org-agenda-fontify-priorities))
2672 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
2673 (org-agenda-dim-blocked-tasks))
2674 (org-agenda-mark-clocking-task)
2675 (when org-agenda-entry-text-mode
2676 (org-agenda-entry-text-hide)
2677 (org-agenda-entry-text-show))
2678 (if (functionp 'org-habit-insert-consistency-graphs)
2679 (org-habit-insert-consistency-graphs))
2680 (run-hooks 'org-finalize-agenda-hook)
2681 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
2682 (when (get 'org-agenda-filter :preset-filter)
2683 (org-agenda-filter-apply org-agenda-filter))
2686 (defun org-agenda-mark-clocking-task ()
2687 "Mark the current clock entry in the agenda if it is present."
2688 (mapc (lambda (o)
2689 (if (eq (org-overlay-get o 'type) 'org-agenda-clocking)
2690 (org-delete-overlay o)))
2691 (org-overlays-in (point-min) (point-max)))
2692 (when (marker-buffer org-clock-hd-marker)
2693 (save-excursion
2694 (goto-char (point-min))
2695 (let (s ov)
2696 (while (setq s (next-single-property-change (point) 'org-hd-marker))
2697 (goto-char s)
2698 (when (equal (org-get-at-bol 'org-hd-marker)
2699 org-clock-hd-marker)
2700 (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-eol))))
2701 (org-overlay-put ov 'type 'org-agenda-clocking)
2702 (org-overlay-put ov 'face 'org-agenda-clocking)
2703 (org-overlay-put ov 'help-echo
2704 "The clock is running in this item")))))))
2706 (defun org-agenda-fontify-priorities ()
2707 "Make highest priority lines bold, and lowest italic."
2708 (interactive)
2709 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
2710 (org-delete-overlay o)))
2711 (org-overlays-in (point-min) (point-max)))
2712 (save-excursion
2713 (let ((inhibit-read-only t)
2714 b e p ov h l)
2715 (goto-char (point-min))
2716 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
2717 (setq h (or (get-char-property (point) 'org-highest-priority)
2718 org-highest-priority)
2719 l (or (get-char-property (point) 'org-lowest-priority)
2720 org-lowest-priority)
2721 p (string-to-char (match-string 1))
2722 b (match-beginning 0)
2723 e (if (eq org-agenda-fontify-priorities 'cookies)
2724 (match-end 0)
2725 (point-at-eol))
2726 ov (org-make-overlay b e))
2727 (org-overlay-put
2728 ov 'face
2729 (cond ((cdr (assoc p org-priority-faces)))
2730 ((and (listp org-agenda-fontify-priorities)
2731 (cdr (assoc p org-agenda-fontify-priorities))))
2732 ((equal p l) 'italic)
2733 ((equal p h) 'bold)))
2734 (org-overlay-put ov 'org-type 'org-priority)))))
2736 (defun org-agenda-dim-blocked-tasks ()
2737 "Dim currently blocked TODO's in the agenda display."
2738 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-blocked-todo)
2739 (org-delete-overlay o)))
2740 (org-overlays-in (point-min) (point-max)))
2741 (save-excursion
2742 (let ((inhibit-read-only t)
2743 (org-depend-tag-blocked nil)
2744 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
2745 org-blocked-by-checkboxes
2746 invis1 b e p ov h l)
2747 (goto-char (point-min))
2748 (while (let ((pos (next-single-property-change (point) 'todo-state)))
2749 (and pos (goto-char (1+ pos))))
2750 (setq org-blocked-by-checkboxes nil invis1 invis)
2751 (let ((marker (org-get-at-bol 'org-hd-marker)))
2752 (when (and marker
2753 (not (with-current-buffer (marker-buffer marker)
2754 (save-excursion
2755 (goto-char marker)
2756 (if (org-entry-get nil "NOBLOCKING")
2757 t ;; Never block this entry
2758 (run-hook-with-args-until-failure
2759 'org-blocker-hook
2760 (list :type 'todo-state-change
2761 :position marker
2762 :from 'todo
2763 :to 'done)))))))
2764 (if org-blocked-by-checkboxes (setq invis1 nil))
2765 (setq b (if invis1
2766 (max (point-min) (1- (point-at-bol)))
2767 (point-at-bol))
2768 e (point-at-eol)
2769 ov (org-make-overlay b e))
2770 (if invis1
2771 (org-overlay-put ov 'invisible t)
2772 (org-overlay-put ov 'face 'org-agenda-dimmed-todo-face))
2773 (org-overlay-put ov 'org-type 'org-blocked-todo)))))))
2775 (defvar org-agenda-skip-function nil
2776 "Function to be called at each match during agenda construction.
2777 If this function returns nil, the current match should not be skipped.
2778 Otherwise, the function must return a position from where the search
2779 should be continued.
2780 This may also be a Lisp form, it will be evaluated.
2781 Never set this variable using `setq' or so, because then it will apply
2782 to all future agenda commands. Instead, bind it with `let' to scope
2783 it dynamically into the agenda-constructing command. A good way to set
2784 it is through options in org-agenda-custom-commands.")
2786 (defun org-agenda-skip ()
2787 "Throw to `:skip' in places that should be skipped.
2788 Also moves point to the end of the skipped region, so that search can
2789 continue from there."
2790 (let ((p (point-at-bol)) to fp)
2791 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
2792 (get-text-property p :org-archived)
2793 (org-end-of-subtree t)
2794 (throw :skip t))
2795 (and org-agenda-skip-comment-trees
2796 (get-text-property p :org-comment)
2797 (org-end-of-subtree t)
2798 (throw :skip t))
2799 (if (equal (char-after p) ?#) (throw :skip t))
2800 (when (and (or (setq fp (functionp org-agenda-skip-function))
2801 (consp org-agenda-skip-function))
2802 (setq to (save-excursion
2803 (save-match-data
2804 (if fp
2805 (funcall org-agenda-skip-function)
2806 (eval org-agenda-skip-function))))))
2807 (goto-char to)
2808 (throw :skip t))))
2810 (defvar org-agenda-markers nil
2811 "List of all currently active markers created by `org-agenda'.")
2812 (defvar org-agenda-last-marker-time (org-float-time)
2813 "Creation time of the last agenda marker.")
2815 (defun org-agenda-new-marker (&optional pos)
2816 "Return a new agenda marker.
2817 Org-mode keeps a list of these markers and resets them when they are
2818 no longer in use."
2819 (let ((m (copy-marker (or pos (point)))))
2820 (setq org-agenda-last-marker-time (org-float-time))
2821 (push m org-agenda-markers)
2824 (defun org-agenda-reset-markers ()
2825 "Reset markers created by `org-agenda'."
2826 (while org-agenda-markers
2827 (move-marker (pop org-agenda-markers) nil)))
2829 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
2830 "Save relative positions of markers in region."
2831 (mapc (lambda (m) (org-check-and-save-marker m beg end))
2832 org-agenda-markers))
2834 ;;; Entry text mode
2836 (defun org-agenda-entry-text-show-here ()
2837 "Add some text from the entry as context to the current line."
2838 (let (m txt o)
2839 (setq m (org-get-at-bol 'org-hd-marker))
2840 (unless (marker-buffer m)
2841 (error "No marker points to an entry here"))
2842 (setq txt (concat "\n" (org-no-properties
2843 (org-agenda-get-some-entry-text
2844 m org-agenda-entry-text-maxlines " > "))))
2845 (when (string-match "\\S-" txt)
2846 (setq o (org-make-overlay (point-at-bol) (point-at-eol)))
2847 (org-overlay-put o 'evaporate t)
2848 (org-overlay-put o 'org-overlay-type 'agenda-entry-content)
2849 (org-overlay-put o 'after-string txt))))
2851 (defun org-agenda-entry-text-show ()
2852 "Add entry context for all agenda lines."
2853 (interactive)
2854 (save-excursion
2855 (goto-char (point-max))
2856 (beginning-of-line 1)
2857 (while (not (bobp))
2858 (when (org-get-at-bol 'org-hd-marker)
2859 (org-agenda-entry-text-show-here))
2860 (beginning-of-line 0))))
2862 (defun org-agenda-entry-text-hide ()
2863 "Remove any shown entry context."
2864 (delq nil
2865 (mapcar (lambda (o)
2866 (if (eq (org-overlay-get o 'org-overlay-type)
2867 'agenda-entry-content)
2868 (progn (org-delete-overlay o) t)))
2869 (org-overlays-in (point-min) (point-max)))))
2871 ;;; Agenda timeline
2873 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
2875 (defun org-timeline (&optional include-all)
2876 "Show a time-sorted view of the entries in the current org file.
2877 Only entries with a time stamp of today or later will be listed. With
2878 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
2879 under the current date.
2880 If the buffer contains an active region, only check the region for
2881 dates."
2882 (interactive "P")
2883 (require 'calendar)
2884 (org-compile-prefix-format 'timeline)
2885 (org-set-sorting-strategy 'timeline)
2886 (let* ((dopast t)
2887 (dotodo include-all)
2888 (doclosed org-agenda-show-log)
2889 (entry buffer-file-name)
2890 (date (calendar-current-date))
2891 (beg (if (org-region-active-p) (region-beginning) (point-min)))
2892 (end (if (org-region-active-p) (region-end) (point-max)))
2893 (day-numbers (org-get-all-dates beg end 'no-ranges
2894 t doclosed ; always include today
2895 org-timeline-show-empty-dates))
2896 (org-deadline-warning-days 0)
2897 (org-agenda-only-exact-dates t)
2898 (today (time-to-days (current-time)))
2899 (past t)
2900 args
2901 s e rtn d emptyp wd)
2902 (setq org-agenda-redo-command
2903 (list 'progn
2904 (list 'org-switch-to-buffer-other-window (current-buffer))
2905 (list 'org-timeline (list 'quote include-all))))
2906 (if (not dopast)
2907 ;; Remove past dates from the list of dates.
2908 (setq day-numbers (delq nil (mapcar (lambda(x)
2909 (if (>= x today) x nil))
2910 day-numbers))))
2911 (org-prepare-agenda (concat "Timeline "
2912 (file-name-nondirectory buffer-file-name)))
2913 (if doclosed (push :closed args))
2914 (push :timestamp args)
2915 (push :deadline args)
2916 (push :scheduled args)
2917 (push :sexp args)
2918 (if dotodo (push :todo args))
2919 (insert "Timeline of file " entry "\n")
2920 (add-text-properties (point-min) (point)
2921 (list 'face 'org-agenda-structure))
2922 (org-agenda-mark-header-line (point-min))
2923 (while (setq d (pop day-numbers))
2924 (if (and (listp d) (eq (car d) :omitted))
2925 (progn
2926 (setq s (point))
2927 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
2928 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
2929 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
2930 (if (and (>= d today)
2931 dopast
2932 past)
2933 (progn
2934 (setq past nil)
2935 (insert (make-string 79 ?-) "\n")))
2936 (setq date (calendar-gregorian-from-absolute d)
2937 wd (calendar-day-of-week date))
2938 (setq s (point))
2939 (setq rtn (and (not emptyp)
2940 (apply 'org-agenda-get-day-entries entry
2941 date args)))
2942 (if (or rtn (equal d today) org-timeline-show-empty-dates)
2943 (progn
2944 (insert
2945 (if (stringp org-agenda-format-date)
2946 (format-time-string org-agenda-format-date
2947 (org-time-from-absolute date))
2948 (funcall org-agenda-format-date date))
2949 "\n")
2950 (put-text-property s (1- (point)) 'face
2951 (if (member wd org-agenda-weekend-days)
2952 'org-agenda-date-weekend
2953 'org-agenda-date))
2954 (put-text-property s (1- (point)) 'org-date-line t)
2955 (put-text-property s (1- (point)) 'org-agenda-date-header t)
2956 (if (equal d today)
2957 (put-text-property s (1- (point)) 'org-today t))
2958 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
2959 (put-text-property s (1- (point)) 'day d)))))
2960 (goto-char (point-min))
2961 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
2962 (point-min)))
2963 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
2964 (org-finalize-agenda)
2965 (setq buffer-read-only t)))
2967 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
2968 "Return a list of all relevant day numbers from BEG to END buffer positions.
2969 If NO-RANGES is non-nil, include only the start and end dates of a range,
2970 not every single day in the range. If FORCE-TODAY is non-nil, make
2971 sure that TODAY is included in the list. If INACTIVE is non-nil, also
2972 inactive time stamps (those in square brackets) are included.
2973 When EMPTY is non-nil, also include days without any entries."
2974 (let ((re (concat
2975 (if pre-re pre-re "")
2976 (if inactive org-ts-regexp-both org-ts-regexp)))
2977 dates dates1 date day day1 day2 ts1 ts2)
2978 (if force-today
2979 (setq dates (list (time-to-days (current-time)))))
2980 (save-excursion
2981 (goto-char beg)
2982 (while (re-search-forward re end t)
2983 (setq day (time-to-days (org-time-string-to-time
2984 (substring (match-string 1) 0 10))))
2985 (or (memq day dates) (push day dates)))
2986 (unless no-ranges
2987 (goto-char beg)
2988 (while (re-search-forward org-tr-regexp end t)
2989 (setq ts1 (substring (match-string 1) 0 10)
2990 ts2 (substring (match-string 2) 0 10)
2991 day1 (time-to-days (org-time-string-to-time ts1))
2992 day2 (time-to-days (org-time-string-to-time ts2)))
2993 (while (< (setq day1 (1+ day1)) day2)
2994 (or (memq day1 dates) (push day1 dates)))))
2995 (setq dates (sort dates '<))
2996 (when empty
2997 (while (setq day (pop dates))
2998 (setq day2 (car dates))
2999 (push day dates1)
3000 (when (and day2 empty)
3001 (if (or (eq empty t)
3002 (and (numberp empty) (<= (- day2 day) empty)))
3003 (while (< (setq day (1+ day)) day2)
3004 (push (list day) dates1))
3005 (push (cons :omitted (- day2 day)) dates1))))
3006 (setq dates (nreverse dates1)))
3007 dates)))
3009 ;;; Agenda Daily/Weekly
3011 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
3012 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3013 "Custom commands can set this variable in the options section.")
3014 (defvar org-agenda-last-arguments nil
3015 "The arguments of the previous call to org-agenda")
3016 (defvar org-starting-day nil) ; local variable in the agenda buffer
3017 (defvar org-agenda-span nil) ; local variable in the agenda buffer
3018 (defvar org-include-all-loc nil) ; local variable
3020 ;;;###autoload
3021 (defun org-agenda-list (&optional include-all start-day ndays)
3022 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3023 The view will be for the current day or week, but from the overview buffer
3024 you will be able to go to other days/weeks.
3026 With one \\[universal-argument] prefix argument INCLUDE-ALL,
3027 all unfinished TODO items will also be shown, before the agenda.
3028 This feature is considered obsolete, please use the TODO list or a block
3029 agenda instead.
3031 With a numeric prefix argument in an interactive call, the agenda will
3032 span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
3033 the number of days. NDAYS defaults to `org-agenda-ndays'.
3035 START-DAY defaults to TODAY, or to the most recent match for the weekday
3036 given in `org-agenda-start-on-weekday'."
3037 (interactive "P")
3038 (if (and (integerp include-all) (> include-all 0))
3039 (setq ndays include-all include-all nil))
3040 (setq ndays (or ndays org-agenda-ndays)
3041 start-day (or start-day org-agenda-start-day))
3042 (if org-agenda-overriding-arguments
3043 (setq include-all (car org-agenda-overriding-arguments)
3044 start-day (nth 1 org-agenda-overriding-arguments)
3045 ndays (nth 2 org-agenda-overriding-arguments)))
3046 (if (stringp start-day)
3047 ;; Convert to an absolute day number
3048 (setq start-day (time-to-days (org-read-date nil t start-day))))
3049 (setq org-agenda-last-arguments (list include-all start-day ndays))
3050 (org-compile-prefix-format 'agenda)
3051 (org-set-sorting-strategy 'agenda)
3052 (require 'calendar)
3053 (let* ((org-agenda-start-on-weekday
3054 (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
3055 org-agenda-start-on-weekday nil))
3056 (thefiles (org-agenda-files nil 'ifmode))
3057 (files thefiles)
3058 (today (time-to-days
3059 (time-subtract (current-time)
3060 (list 0 (* 3600 org-extend-today-until) 0))))
3061 (sd (or start-day today))
3062 (start (if (or (null org-agenda-start-on-weekday)
3063 (< org-agenda-ndays 7))
3065 (let* ((nt (calendar-day-of-week
3066 (calendar-gregorian-from-absolute sd)))
3067 (n1 org-agenda-start-on-weekday)
3068 (d (- nt n1)))
3069 (- sd (+ (if (< d 0) 7 0) d)))))
3070 (day-numbers (list start))
3071 (day-cnt 0)
3072 (inhibit-redisplay (not debug-on-error))
3073 s e rtn rtnall file date d start-pos end-pos todayp nd wd
3074 clocktable-start clocktable-end)
3075 (setq org-agenda-redo-command
3076 (list 'org-agenda-list (list 'quote include-all) start-day ndays))
3077 ;; Make the list of days
3078 (setq ndays (or ndays org-agenda-ndays)
3079 nd ndays)
3080 (while (> ndays 1)
3081 (push (1+ (car day-numbers)) day-numbers)
3082 (setq ndays (1- ndays)))
3083 (setq day-numbers (nreverse day-numbers))
3084 (setq clocktable-start (car day-numbers)
3085 clocktable-end (1+ (or (org-last day-numbers) 0)))
3086 (org-prepare-agenda "Day/Week")
3087 (org-set-local 'org-starting-day (car day-numbers))
3088 (org-set-local 'org-include-all-loc include-all)
3089 (org-set-local 'org-agenda-span
3090 (org-agenda-ndays-to-span nd))
3091 (when (and (or include-all org-agenda-include-all-todo)
3092 (member today day-numbers))
3093 (setq files thefiles
3094 rtnall nil)
3095 (while (setq file (pop files))
3096 (catch 'nextfile
3097 (org-check-agenda-file file)
3098 (setq date (calendar-gregorian-from-absolute today)
3099 rtn (org-agenda-get-day-entries
3100 file date :todo))
3101 (setq rtnall (append rtnall rtn))))
3102 (when rtnall
3103 (insert "All currently open TODO items:\n")
3104 (add-text-properties (point-min) (1- (point))
3105 (list 'face 'org-agenda-structure
3106 'short-heading "All TODO items"))
3107 (org-agenda-mark-header-line (point-min))
3108 (insert (org-finalize-agenda-entries rtnall) "\n")))
3109 (unless org-agenda-compact-blocks
3110 (let* ((d1 (car day-numbers))
3111 (d2 (org-last day-numbers))
3112 (w1 (org-days-to-iso-week d1))
3113 (w2 (org-days-to-iso-week d2)))
3114 (setq s (point))
3115 (if org-agenda-overriding-header
3116 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3117 nil 'face 'org-agenda-structure) "\n")
3118 (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
3119 "-agenda"
3120 (if (< (- d2 d1) 350)
3121 (if (= w1 w2)
3122 (format " (W%02d)" w1)
3123 (format " (W%02d-W%02d)" w1 w2))
3125 ":\n")))
3126 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3127 'org-date-line t))
3128 (org-agenda-mark-header-line s))
3129 (while (setq d (pop day-numbers))
3130 (setq date (calendar-gregorian-from-absolute d)
3131 wd (calendar-day-of-week date)
3132 s (point))
3133 (if (or (setq todayp (= d today))
3134 (and (not start-pos) (= d sd)))
3135 (setq start-pos (point))
3136 (if (and start-pos (not end-pos))
3137 (setq end-pos (point))))
3138 (setq files thefiles
3139 rtnall nil)
3140 (while (setq file (pop files))
3141 (catch 'nextfile
3142 (org-check-agenda-file file)
3143 (cond
3144 ((eq org-agenda-show-log 'only)
3145 (setq rtn (org-agenda-get-day-entries
3146 file date :closed)))
3147 (org-agenda-show-log
3148 (setq rtn (org-agenda-get-day-entries
3149 file date
3150 :deadline :scheduled :timestamp :sexp :closed)))
3152 (setq rtn (org-agenda-get-day-entries
3153 file date
3154 :deadline :scheduled :sexp :timestamp))))
3155 (setq rtnall (append rtnall rtn))))
3156 (if org-agenda-include-diary
3157 (let ((org-agenda-search-headline-for-time t))
3158 (require 'diary-lib)
3159 (setq rtn (org-get-entries-from-diary date))
3160 (setq rtnall (append rtnall rtn))))
3161 (if (or rtnall org-agenda-show-all-dates)
3162 (progn
3163 (setq day-cnt (1+ day-cnt))
3164 (insert
3165 (if (stringp org-agenda-format-date)
3166 (format-time-string org-agenda-format-date
3167 (org-time-from-absolute date))
3168 (funcall org-agenda-format-date date))
3169 "\n")
3170 (put-text-property s (1- (point)) 'face
3171 (if (member wd org-agenda-weekend-days)
3172 'org-agenda-date-weekend
3173 'org-agenda-date))
3174 (put-text-property s (1- (point)) 'org-date-line t)
3175 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3176 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3177 (when todayp
3178 (put-text-property s (1- (point)) 'org-today t)
3179 (put-text-property s (1- (point)) 'face 'org-agenda-date-today))
3180 (if rtnall (insert
3181 (org-finalize-agenda-entries
3182 (org-agenda-add-time-grid-maybe
3183 rtnall nd todayp))
3184 "\n"))
3185 (put-text-property s (1- (point)) 'day d)
3186 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3187 (when (and org-agenda-clockreport-mode clocktable-start)
3188 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3189 ;; the above line is to ensure the restricted range!
3190 (p org-agenda-clockreport-parameter-plist)
3191 tbl)
3192 (setq p (org-plist-delete p :block))
3193 (setq p (plist-put p :tstart clocktable-start))
3194 (setq p (plist-put p :tend clocktable-end))
3195 (setq p (plist-put p :scope 'agenda))
3196 (setq tbl (apply 'org-get-clocktable p))
3197 (insert tbl)))
3198 (goto-char (point-min))
3199 (or org-agenda-multi (org-fit-agenda-window))
3200 (unless (and (pos-visible-in-window-p (point-min))
3201 (pos-visible-in-window-p (point-max)))
3202 (goto-char (1- (point-max)))
3203 (recenter -1)
3204 (if (not (pos-visible-in-window-p (or start-pos 1)))
3205 (progn
3206 (goto-char (or start-pos 1))
3207 (recenter 1))))
3208 (goto-char (or start-pos 1))
3209 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3210 (org-finalize-agenda)
3211 (setq buffer-read-only t)
3212 (message "")))
3214 (defun org-agenda-ndays-to-span (n)
3215 (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
3217 ;;; Agenda word search
3219 (defvar org-agenda-search-history nil)
3220 (defvar org-todo-only nil)
3222 (defvar org-search-syntax-table nil
3223 "Special syntax table for org-mode search.
3224 In this table, we have single quotes not as word constituents, to
3225 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3227 (defun org-search-syntax-table ()
3228 (unless org-search-syntax-table
3229 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3230 (modify-syntax-entry ?' "." org-search-syntax-table)
3231 (modify-syntax-entry ?` "." org-search-syntax-table))
3232 org-search-syntax-table)
3234 (defvar org-agenda-last-search-view-search-was-boolean nil)
3236 ;;;###autoload
3237 (defun org-search-view (&optional todo-only string edit-at)
3238 "Show all entries that contain a phrase or words or regular expressions.
3240 With optional prefix argument TODO-ONLY, only consider entries that are
3241 TODO entries. The argument STRING can be used to pass a default search
3242 string into this function. If EDIT-AT is non-nil, it means that the
3243 user should get a chance to edit this string, with cursor at position
3244 EDIT-AT.
3246 The search string can be viewed either as a phrase that should be found as
3247 is, or it can be broken into a number of snippets, each of which must match
3248 in a Boolean way to select an entry. The default depends on the variable
3249 `org-agenda-search-view-always-boolean'.
3250 Even if this is turned off (the default) you can always switch to
3251 Boolean search dynamically by preceeding the first word with \"+\" or \"-\".
3253 The default is a direct search of the whole phrase, where each space in
3254 the search string can expand to an arbitrary amount of whitespace,
3255 including newlines.
3257 If using a Boolean search, the search string is split on whitespace and
3258 each snipped is search separately, with logical AND to select an entry.
3259 Words prefixed with a minus must *not* occur in the entry. Words without
3260 a prefix or prefixed with a plus must occur in the entry. Matching is
3261 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3262 match whole words, not parts of a word) if
3263 `org-agenda-search-view-force-full-words' is set (default is nil).
3265 Boolean search snippets enclosed by curly braces are interpreted as
3266 regular expressions that must or (when preceeded with \"-\") must not
3267 match in the entry.
3269 - If the search string starts with an asterisk, search only in headlines.
3270 - If (possibly after the leading star) the search string starts with an
3271 exclamation mark, this also means to look at TODO entries only, an effect
3272 that can also be achieved with a prefix argument.
3273 - If (possibly after star and exclamation mark) the seatch string starts
3274 with a colon, this will mean that the (non-regexp) snippets of the
3275 Boolean search must match as full words.
3277 This command searches the agenda files, and in addition the files listed
3278 in `org-agenda-text-search-extra-files'."
3279 (interactive "P")
3280 (org-compile-prefix-format 'search)
3281 (org-set-sorting-strategy 'search)
3282 (org-prepare-agenda "SEARCH")
3283 (let* ((props (list 'face nil
3284 'done-face 'org-agenda-done
3285 'org-not-done-regexp org-not-done-regexp
3286 'org-todo-regexp org-todo-regexp
3287 'org-complex-heading-regexp org-complex-heading-regexp
3288 'mouse-face 'highlight
3289 'help-echo (format "mouse-2 or RET jump to location")))
3290 (full-words org-agenda-search-view-force-full-words)
3291 regexp rtn rtnall files file pos
3292 marker category tags c neg re boolean
3293 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3294 (unless (and (not edit-at)
3295 (stringp string)
3296 (string-match "\\S-" string))
3297 (setq string (read-string
3298 (if org-agenda-search-view-always-boolean
3299 "[+-]Word/{Regexp} ...: "
3300 "Phrase, or [+-]Word/{Regexp} ...: ")
3301 (cond
3302 ((integerp edit-at) (cons string edit-at))
3303 (edit-at string))
3304 'org-agenda-search-history)))
3305 (org-set-local 'org-todo-only todo-only)
3306 (setq org-agenda-redo-command
3307 (list 'org-search-view (if todo-only t nil) string
3308 '(if current-prefix-arg 1 nil)))
3309 (setq org-agenda-query-string string)
3311 (if (equal (string-to-char string) ?*)
3312 (setq hdl-only t
3313 words (substring string 1))
3314 (setq words string))
3315 (when (equal (string-to-char words) ?!)
3316 (setq todo-only t
3317 words (substring words 1)))
3318 (when (equal (string-to-char words) ?:)
3319 (setq full-words t
3320 words (substring words 1)))
3321 (if (or org-agenda-search-view-always-boolean
3322 (member (string-to-char string) '(?- ?+ ?\{)))
3323 (setq boolean t))
3324 (setq words (org-split-string words))
3325 (setq org-agenda-last-search-view-search-was-boolean boolean)
3326 (when boolean
3327 (let (wds w)
3328 (while (setq w (pop words))
3329 (if (or (equal (substring w 0 1) "\"")
3330 (and (> (length w) 1)
3331 (member (substring w 0 1) '("+" "-"))
3332 (equal (substring w 1 2) "\"")))
3333 (while (and words (not (equal (substring w -1) "\"")))
3334 (setq w (concat w " " (pop words)))))
3335 (and (string-match "\\`\\([-+]?\\)\"" w)
3336 (setq w (replace-match "\\1" nil nil w)))
3337 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3338 (push w wds))
3339 (setq words (nreverse wds))))
3340 (if boolean
3341 (mapc (lambda (w)
3342 (setq c (string-to-char w))
3343 (if (equal c ?-)
3344 (setq neg t w (substring w 1))
3345 (if (equal c ?+)
3346 (setq neg nil w (substring w 1))
3347 (setq neg nil)))
3348 (if (string-match "\\`{.*}\\'" w)
3349 (setq re (substring w 1 -1))
3350 (if full-words
3351 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3352 (setq re (regexp-quote (downcase w)))))
3353 (if neg (push re regexps-) (push re regexps+)))
3354 words)
3355 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3356 regexps+))
3357 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3358 (if (not regexps+)
3359 (setq regexp (concat "^" org-outline-regexp))
3360 (setq regexp (pop regexps+))
3361 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3362 regexp))))
3363 (setq files (org-agenda-files nil 'ifmode))
3364 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3365 (pop org-agenda-text-search-extra-files)
3366 (setq files (org-add-archive-files files)))
3367 (setq files (append files org-agenda-text-search-extra-files)
3368 rtnall nil)
3369 (while (setq file (pop files))
3370 (setq ee nil)
3371 (catch 'nextfile
3372 (org-check-agenda-file file)
3373 (setq buffer (if (file-exists-p file)
3374 (org-get-agenda-file-buffer file)
3375 (error "No such file %s" file)))
3376 (if (not buffer)
3377 ;; If file does not exist, make sure an error message is sent
3378 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3379 file))))
3380 (with-current-buffer buffer
3381 (with-syntax-table (org-search-syntax-table)
3382 (unless (org-mode-p)
3383 (error "Agenda file %s is not in `org-mode'" file))
3384 (let ((case-fold-search t))
3385 (save-excursion
3386 (save-restriction
3387 (if org-agenda-restrict
3388 (narrow-to-region org-agenda-restrict-begin
3389 org-agenda-restrict-end)
3390 (widen))
3391 (goto-char (point-min))
3392 (unless (or (org-on-heading-p)
3393 (outline-next-heading))
3394 (throw 'nextfile t))
3395 (goto-char (max (point-min) (1- (point))))
3396 (while (re-search-forward regexp nil t)
3397 (org-back-to-heading t)
3398 (skip-chars-forward "* ")
3399 (setq beg (point-at-bol)
3400 beg1 (point)
3401 end (progn (outline-next-heading) (point)))
3402 (catch :skip
3403 (goto-char beg)
3404 (org-agenda-skip)
3405 (setq str (buffer-substring-no-properties
3406 (point-at-bol)
3407 (if hdl-only (point-at-eol) end)))
3408 (mapc (lambda (wr) (when (string-match wr str)
3409 (goto-char (1- end))
3410 (throw :skip t)))
3411 regexps-)
3412 (mapc (lambda (wr) (unless (string-match wr str)
3413 (goto-char (1- end))
3414 (throw :skip t)))
3415 (if todo-only
3416 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3417 regexps+)
3418 regexps+))
3419 (goto-char beg)
3420 (setq marker (org-agenda-new-marker (point))
3421 category (org-get-category)
3422 tags (org-get-tags-at (point))
3423 txt (org-format-agenda-item
3425 (buffer-substring-no-properties
3426 beg1 (point-at-eol))
3427 category tags))
3428 (org-add-props txt props
3429 'org-marker marker 'org-hd-marker marker
3430 'org-todo-regexp org-todo-regexp
3431 'org-complex-heading-regexp org-complex-heading-regexp
3432 'priority 1000 'org-category category
3433 'type "search")
3434 (push txt ee)
3435 (goto-char (1- end))))))))))
3436 (setq rtn (nreverse ee))
3437 (setq rtnall (append rtnall rtn)))
3438 (if org-agenda-overriding-header
3439 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3440 nil 'face 'org-agenda-structure) "\n")
3441 (insert "Search words: ")
3442 (add-text-properties (point-min) (1- (point))
3443 (list 'face 'org-agenda-structure))
3444 (setq pos (point))
3445 (insert string "\n")
3446 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3447 (setq pos (point))
3448 (unless org-agenda-multi
3449 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3450 (add-text-properties pos (1- (point))
3451 (list 'face 'org-agenda-structure))))
3452 (org-agenda-mark-header-line (point-min))
3453 (when rtnall
3454 (insert (org-finalize-agenda-entries rtnall) "\n"))
3455 (goto-char (point-min))
3456 (or org-agenda-multi (org-fit-agenda-window))
3457 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3458 (org-finalize-agenda)
3459 (setq buffer-read-only t)))
3461 ;;; Agenda TODO list
3463 (defvar org-select-this-todo-keyword nil)
3464 (defvar org-last-arg nil)
3466 ;;;###autoload
3467 (defun org-todo-list (arg)
3468 "Show all TODO entries from all agenda file in a single list.
3469 The prefix arg can be used to select a specific TODO keyword and limit
3470 the list to these. When using \\[universal-argument], you will be prompted
3471 for a keyword. A numeric prefix directly selects the Nth keyword in
3472 `org-todo-keywords-1'."
3473 (interactive "P")
3474 (require 'calendar)
3475 (org-compile-prefix-format 'todo)
3476 (org-set-sorting-strategy 'todo)
3477 (org-prepare-agenda "TODO")
3478 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
3479 (let* ((today (time-to-days (current-time)))
3480 (date (calendar-gregorian-from-absolute today))
3481 (kwds org-todo-keywords-for-agenda)
3482 (completion-ignore-case t)
3483 (org-select-this-todo-keyword
3484 (if (stringp arg) arg
3485 (and arg (integerp arg) (> arg 0)
3486 (nth (1- arg) kwds))))
3487 rtn rtnall files file pos)
3488 (when (equal arg '(4))
3489 (setq org-select-this-todo-keyword
3490 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
3491 (mapcar 'list kwds) nil nil)))
3492 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
3493 (org-set-local 'org-last-arg arg)
3494 (setq org-agenda-redo-command
3495 '(org-todo-list (or current-prefix-arg org-last-arg)))
3496 (setq files (org-agenda-files nil 'ifmode)
3497 rtnall nil)
3498 (while (setq file (pop files))
3499 (catch 'nextfile
3500 (org-check-agenda-file file)
3501 (setq rtn (org-agenda-get-day-entries file date :todo))
3502 (setq rtnall (append rtnall rtn))))
3503 (if org-agenda-overriding-header
3504 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3505 nil 'face 'org-agenda-structure) "\n")
3506 (insert "Global list of TODO items of type: ")
3507 (add-text-properties (point-min) (1- (point))
3508 (list 'face 'org-agenda-structure
3509 'short-heading
3510 (concat "ToDo: "
3511 (or org-select-this-todo-keyword "ALL"))))
3512 (org-agenda-mark-header-line (point-min))
3513 (setq pos (point))
3514 (insert (or org-select-this-todo-keyword "ALL") "\n")
3515 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3516 (setq pos (point))
3517 (unless org-agenda-multi
3518 (insert "Available with `N r': (0)ALL")
3519 (let ((n 0) s)
3520 (mapc (lambda (x)
3521 (setq s (format "(%d)%s" (setq n (1+ n)) x))
3522 (if (> (+ (current-column) (string-width s) 1) (frame-width))
3523 (insert "\n "))
3524 (insert " " s))
3525 kwds))
3526 (insert "\n"))
3527 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
3528 (org-agenda-mark-header-line (point-min))
3529 (when rtnall
3530 (insert (org-finalize-agenda-entries rtnall) "\n"))
3531 (goto-char (point-min))
3532 (or org-agenda-multi (org-fit-agenda-window))
3533 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
3534 (org-finalize-agenda)
3535 (setq buffer-read-only t)))
3537 ;;; Agenda tags match
3539 ;;;###autoload
3540 (defun org-tags-view (&optional todo-only match)
3541 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
3542 The prefix arg TODO-ONLY limits the search to TODO entries."
3543 (interactive "P")
3544 (org-compile-prefix-format 'tags)
3545 (org-set-sorting-strategy 'tags)
3546 (let* ((org-tags-match-list-sublevels
3547 org-tags-match-list-sublevels)
3548 (completion-ignore-case t)
3549 rtn rtnall files file pos matcher
3550 buffer)
3551 (when (and (stringp match) (not (string-match "\\S-" match)))
3552 (setq match nil))
3553 (setq matcher (org-make-tags-matcher match)
3554 match (car matcher) matcher (cdr matcher))
3555 (org-prepare-agenda (concat "TAGS " match))
3556 (setq org-agenda-query-string match)
3557 (setq org-agenda-redo-command
3558 (list 'org-tags-view (list 'quote todo-only)
3559 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
3560 (setq files (org-agenda-files nil 'ifmode)
3561 rtnall nil)
3562 (while (setq file (pop files))
3563 (catch 'nextfile
3564 (org-check-agenda-file file)
3565 (setq buffer (if (file-exists-p file)
3566 (org-get-agenda-file-buffer file)
3567 (error "No such file %s" file)))
3568 (if (not buffer)
3569 ;; If file does not exist, error message to agenda
3570 (setq rtn (list
3571 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3572 rtnall (append rtnall rtn))
3573 (with-current-buffer buffer
3574 (unless (org-mode-p)
3575 (error "Agenda file %s is not in `org-mode'" file))
3576 (save-excursion
3577 (save-restriction
3578 (if org-agenda-restrict
3579 (narrow-to-region org-agenda-restrict-begin
3580 org-agenda-restrict-end)
3581 (widen))
3582 (setq rtn (org-scan-tags 'agenda matcher todo-only))
3583 (setq rtnall (append rtnall rtn))))))))
3584 (if org-agenda-overriding-header
3585 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3586 nil 'face 'org-agenda-structure) "\n")
3587 (insert "Headlines with TAGS match: ")
3588 (add-text-properties (point-min) (1- (point))
3589 (list 'face 'org-agenda-structure
3590 'short-heading
3591 (concat "Match: " match)))
3592 (setq pos (point))
3593 (insert match "\n")
3594 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3595 (setq pos (point))
3596 (unless org-agenda-multi
3597 (insert "Press `C-u r' to search again with new search string\n"))
3598 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
3599 (org-agenda-mark-header-line (point-min))
3600 (when rtnall
3601 (insert (org-finalize-agenda-entries rtnall) "\n"))
3602 (goto-char (point-min))
3603 (or org-agenda-multi (org-fit-agenda-window))
3604 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
3605 (org-finalize-agenda)
3606 (setq buffer-read-only t)))
3608 ;;; Agenda Finding stuck projects
3610 (defvar org-agenda-skip-regexp nil
3611 "Regular expression used in skipping subtrees for the agenda.
3612 This is basically a temporary global variable that can be set and then
3613 used by user-defined selections using `org-agenda-skip-function'.")
3615 (defvar org-agenda-overriding-header nil
3616 "When this is set during todo and tags searches, will replace header.
3617 This variable should not be set directly, but custom commands can bind it
3618 in the options section.")
3620 (defun org-agenda-skip-entry-when-regexp-matches ()
3621 "Checks if the current entry contains match for `org-agenda-skip-regexp'.
3622 If yes, it returns the end position of this entry, causing agenda commands
3623 to skip the entry but continuing the search in the subtree. This is a
3624 function that can be put into `org-agenda-skip-function' for the duration
3625 of a command."
3626 (let ((end (save-excursion (org-end-of-subtree t)))
3627 skip)
3628 (save-excursion
3629 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3630 (and skip end)))
3632 (defun org-agenda-skip-subtree-when-regexp-matches ()
3633 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3634 If yes, it returns the end position of this tree, causing agenda commands
3635 to skip this subtree. This is a function that can be put into
3636 `org-agenda-skip-function' for the duration of a command."
3637 (let ((end (save-excursion (org-end-of-subtree t)))
3638 skip)
3639 (save-excursion
3640 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3641 (and skip end)))
3643 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
3644 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3645 If yes, it returns the end position of the current entry (NOT the tree),
3646 causing agenda commands to skip the entry but continuing the search in
3647 the subtree. This is a function that can be put into
3648 `org-agenda-skip-function' for the duration of a command. An important
3649 use of this function is for the stuck project list."
3650 (let ((end (save-excursion (org-end-of-subtree t)))
3651 (entry-end (save-excursion (outline-next-heading) (1- (point))))
3652 skip)
3653 (save-excursion
3654 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3655 (and skip entry-end)))
3657 (defun org-agenda-skip-entry-if (&rest conditions)
3658 "Skip entry if any of CONDITIONS is true.
3659 See `org-agenda-skip-if' for details."
3660 (org-agenda-skip-if nil conditions))
3662 (defun org-agenda-skip-subtree-if (&rest conditions)
3663 "Skip entry if any of CONDITIONS is true.
3664 See `org-agenda-skip-if' for details."
3665 (org-agenda-skip-if t conditions))
3667 (defun org-agenda-skip-if (subtree conditions)
3668 "Checks current entity for CONDITIONS.
3669 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
3670 the entry, i.e. the text before the next heading is checked.
3672 CONDITIONS is a list of symbols, boolean OR is used to combine the results
3673 from different tests. Valid conditions are:
3675 scheduled Check if there is a scheduled cookie
3676 notscheduled Check if there is no scheduled cookie
3677 deadline Check if there is a deadline
3678 notdeadline Check if there is no deadline
3679 timestamp Check if there is a timestamp (also deadline or scheduled)
3680 nottimestamp Check if there is no timestamp (also deadline or scheduled)
3681 regexp Check if regexp matches
3682 notregexp Check if regexp does not match.
3684 The regexp is taken from the conditions list, it must come right after
3685 the `regexp' or `notregexp' element.
3687 If any of these conditions is met, this function returns the end point of
3688 the entity, causing the search to continue from there. This is a function
3689 that can be put into `org-agenda-skip-function' for the duration of a command."
3690 (let (beg end m)
3691 (org-back-to-heading t)
3692 (setq beg (point)
3693 end (if subtree
3694 (progn (org-end-of-subtree t) (point))
3695 (progn (outline-next-heading) (1- (point)))))
3696 (goto-char beg)
3697 (and
3699 (and (memq 'scheduled conditions)
3700 (re-search-forward org-scheduled-time-regexp end t))
3701 (and (memq 'notscheduled conditions)
3702 (not (re-search-forward org-scheduled-time-regexp end t)))
3703 (and (memq 'deadline conditions)
3704 (re-search-forward org-deadline-time-regexp end t))
3705 (and (memq 'notdeadline conditions)
3706 (not (re-search-forward org-deadline-time-regexp end t)))
3707 (and (memq 'timestamp conditions)
3708 (re-search-forward org-ts-regexp end t))
3709 (and (memq 'nottimestamp conditions)
3710 (not (re-search-forward org-ts-regexp end t)))
3711 (and (setq m (memq 'regexp conditions))
3712 (stringp (nth 1 m))
3713 (re-search-forward (nth 1 m) end t))
3714 (and (setq m (memq 'notregexp conditions))
3715 (stringp (nth 1 m))
3716 (not (re-search-forward (nth 1 m) end t))))
3717 end)))
3719 ;;;###autoload
3720 (defun org-agenda-list-stuck-projects (&rest ignore)
3721 "Create agenda view for projects that are stuck.
3722 Stuck projects are project that have no next actions. For the definitions
3723 of what a project is and how to check if it stuck, customize the variable
3724 `org-stuck-projects'.
3725 MATCH is being ignored."
3726 (interactive)
3727 (let* ((org-agenda-skip-function
3728 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
3729 ;; We could have used org-agenda-skip-if here.
3730 (org-agenda-overriding-header
3731 (or org-agenda-overriding-header "List of stuck projects: "))
3732 (matcher (nth 0 org-stuck-projects))
3733 (todo (nth 1 org-stuck-projects))
3734 (todo-wds (if (member "*" todo)
3735 (progn
3736 (org-prepare-agenda-buffers (org-agenda-files
3737 nil 'ifmode))
3738 (org-delete-all
3739 org-done-keywords-for-agenda
3740 (copy-sequence org-todo-keywords-for-agenda)))
3741 todo))
3742 (todo-re (concat "^\\*+[ \t]+\\("
3743 (mapconcat 'identity todo-wds "\\|")
3744 "\\)\\>"))
3745 (tags (nth 2 org-stuck-projects))
3746 (tags-re (if (member "*" tags)
3747 (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
3748 (if tags
3749 (concat "^\\*+ .*:\\("
3750 (mapconcat 'identity tags "\\|")
3751 (org-re "\\):[[:alnum:]_@:]*[ \t]*$")))))
3752 (gen-re (nth 3 org-stuck-projects))
3753 (re-list
3754 (delq nil
3755 (list
3756 (if todo todo-re)
3757 (if tags tags-re)
3758 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
3759 gen-re)))))
3760 (setq org-agenda-skip-regexp
3761 (if re-list
3762 (mapconcat 'identity re-list "\\|")
3763 (error "No information how to identify unstuck projects")))
3764 (org-tags-view nil matcher)
3765 (with-current-buffer org-agenda-buffer-name
3766 (setq org-agenda-redo-command
3767 '(org-agenda-list-stuck-projects
3768 (or current-prefix-arg org-last-arg))))))
3770 ;;; Diary integration
3772 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
3773 (defvar list-diary-entries-hook)
3775 (defun org-get-entries-from-diary (date)
3776 "Get the (Emacs Calendar) diary entries for DATE."
3777 (require 'diary-lib)
3778 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
3779 (fancy-diary-buffer diary-fancy-buffer)
3780 (diary-display-hook '(fancy-diary-display))
3781 (diary-display-function 'fancy-diary-display)
3782 (pop-up-frames nil)
3783 (list-diary-entries-hook
3784 (cons 'org-diary-default-entry list-diary-entries-hook))
3785 (diary-file-name-prefix-function nil) ; turn this feature off
3786 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
3787 entries
3788 (org-disable-agenda-to-diary t))
3789 (save-excursion
3790 (save-window-excursion
3791 (funcall (if (fboundp 'diary-list-entries)
3792 'diary-list-entries 'list-diary-entries)
3793 date 1)))
3794 (if (not (get-buffer diary-fancy-buffer))
3795 (setq entries nil)
3796 (with-current-buffer diary-fancy-buffer
3797 (setq buffer-read-only nil)
3798 (if (zerop (buffer-size))
3799 ;; No entries
3800 (setq entries nil)
3801 ;; Omit the date and other unnecessary stuff
3802 (org-agenda-cleanup-fancy-diary)
3803 ;; Add prefix to each line and extend the text properties
3804 (if (zerop (buffer-size))
3805 (setq entries nil)
3806 (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
3807 (set-buffer-modified-p nil)
3808 (kill-buffer diary-fancy-buffer)))
3809 (when entries
3810 (setq entries (org-split-string entries "\n"))
3811 (setq entries
3812 (mapcar
3813 (lambda (x)
3814 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
3815 ;; Extend the text properties to the beginning of the line
3816 (org-add-props x (text-properties-at (1- (length x)) x)
3817 'type "diary" 'date date 'face 'org-agenda-diary))
3818 entries)))))
3820 (defvar org-agenda-cleanup-fancy-diary-hook nil
3821 "Hook run when the fancy diary buffer is cleaned up.")
3823 (defun org-agenda-cleanup-fancy-diary ()
3824 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
3825 This gets rid of the date, the underline under the date, and
3826 the dummy entry installed by `org-mode' to ensure non-empty diary for each
3827 date. It also removes lines that contain only whitespace."
3828 (goto-char (point-min))
3829 (if (looking-at ".*?:[ \t]*")
3830 (progn
3831 (replace-match "")
3832 (re-search-forward "\n=+$" nil t)
3833 (replace-match "")
3834 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
3835 (re-search-forward "\n=+$" nil t)
3836 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
3837 (goto-char (point-min))
3838 (while (re-search-forward "^ +\n" nil t)
3839 (replace-match ""))
3840 (goto-char (point-min))
3841 (if (re-search-forward "^Org-mode dummy\n?" nil t)
3842 (replace-match ""))
3843 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
3845 ;; Make sure entries from the diary have the right text properties.
3846 (eval-after-load "diary-lib"
3847 '(if (boundp 'diary-modify-entry-list-string-function)
3848 ;; We can rely on the hook, nothing to do
3850 ;; Hook not available, must use advice to make this work
3851 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
3852 "Make the position visible."
3853 (if (and org-disable-agenda-to-diary ;; called from org-agenda
3854 (stringp string)
3855 buffer-file-name)
3856 (setq string (org-modify-diary-entry-string string))))))
3858 (defun org-modify-diary-entry-string (string)
3859 "Add text properties to string, allowing org-mode to act on it."
3860 (org-add-props string nil
3861 'mouse-face 'highlight
3862 'help-echo (if buffer-file-name
3863 (format "mouse-2 or RET jump to diary file %s"
3864 (abbreviate-file-name buffer-file-name))
3866 'org-agenda-diary-link t
3867 'org-marker (org-agenda-new-marker (point-at-bol))))
3869 (defun org-diary-default-entry ()
3870 "Add a dummy entry to the diary.
3871 Needed to avoid empty dates which mess up holiday display."
3872 ;; Catch the error if dealing with the new add-to-diary-alist
3873 (when org-disable-agenda-to-diary
3874 (condition-case nil
3875 (org-add-to-diary-list original-date "Org-mode dummy" "")
3876 (error
3877 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
3879 (defun org-add-to-diary-list (&rest args)
3880 (if (fboundp 'diary-add-to-list)
3881 (apply 'diary-add-to-list args)
3882 (apply 'add-to-diary-list args)))
3884 ;;;###autoload
3885 (defun org-diary (&rest args)
3886 "Return diary information from org-files.
3887 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
3888 It accesses org files and extracts information from those files to be
3889 listed in the diary. The function accepts arguments specifying what
3890 items should be listed. The following arguments are allowed:
3892 :timestamp List the headlines of items containing a date stamp or
3893 date range matching the selected date. Deadlines will
3894 also be listed, on the expiration day.
3896 :sexp List entries resulting from diary-like sexps.
3898 :deadline List any deadlines past due, or due within
3899 `org-deadline-warning-days'. The listing occurs only
3900 in the diary for *today*, not at any other date. If
3901 an entry is marked DONE, it is no longer listed.
3903 :scheduled List all items which are scheduled for the given date.
3904 The diary for *today* also contains items which were
3905 scheduled earlier and are not yet marked DONE.
3907 :todo List all TODO items from the org-file. This may be a
3908 long list - so this is not turned on by default.
3909 Like deadlines, these entries only show up in the
3910 diary for *today*, not at any other date.
3912 The call in the diary file should look like this:
3914 &%%(org-diary) ~/path/to/some/orgfile.org
3916 Use a separate line for each org file to check. Or, if you omit the file name,
3917 all files listed in `org-agenda-files' will be checked automatically:
3919 &%%(org-diary)
3921 If you don't give any arguments (as in the example above), the default
3922 arguments (:deadline :scheduled :timestamp :sexp) are used.
3923 So the example above may also be written as
3925 &%%(org-diary :deadline :timestamp :sexp :scheduled)
3927 The function expects the lisp variables `entry' and `date' to be provided
3928 by the caller, because this is how the calendar works. Don't use this
3929 function from a program - use `org-agenda-get-day-entries' instead."
3930 (when (> (- (org-float-time)
3931 org-agenda-last-marker-time)
3933 (org-agenda-reset-markers))
3934 (org-compile-prefix-format 'agenda)
3935 (org-set-sorting-strategy 'agenda)
3936 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3937 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
3938 (list entry)
3939 (org-agenda-files t)))
3940 file rtn results)
3941 (org-prepare-agenda-buffers files)
3942 ;; If this is called during org-agenda, don't return any entries to
3943 ;; the calendar. Org Agenda will list these entries itself.
3944 (if org-disable-agenda-to-diary (setq files nil))
3945 (while (setq file (pop files))
3946 (setq rtn (apply 'org-agenda-get-day-entries file date args))
3947 (setq results (append results rtn)))
3948 (if results
3949 (concat (org-finalize-agenda-entries results) "\n"))))
3951 ;;; Agenda entry finders
3953 (defun org-agenda-get-day-entries (file date &rest args)
3954 "Does the work for `org-diary' and `org-agenda'.
3955 FILE is the path to a file to be checked for entries. DATE is date like
3956 the one returned by `calendar-current-date'. ARGS are symbols indicating
3957 which kind of entries should be extracted. For details about these, see
3958 the documentation of `org-diary'."
3959 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3960 (let* ((org-startup-folded nil)
3961 (org-startup-align-all-tables nil)
3962 (buffer (if (file-exists-p file)
3963 (org-get-agenda-file-buffer file)
3964 (error "No such file %s" file)))
3965 arg results rtn deadline-results)
3966 (if (not buffer)
3967 ;; If file does not exist, make sure an error message ends up in diary
3968 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3969 (with-current-buffer buffer
3970 (unless (org-mode-p)
3971 (error "Agenda file %s is not in `org-mode'" file))
3972 (let ((case-fold-search nil))
3973 (save-excursion
3974 (save-restriction
3975 (if org-agenda-restrict
3976 (narrow-to-region org-agenda-restrict-begin
3977 org-agenda-restrict-end)
3978 (widen))
3979 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
3980 (while (setq arg (pop args))
3981 (cond
3982 ((and (eq arg :todo)
3983 (equal date (calendar-current-date)))
3984 (setq rtn (org-agenda-get-todos))
3985 (setq results (append results rtn)))
3986 ((eq arg :timestamp)
3987 (setq rtn (org-agenda-get-blocks))
3988 (setq results (append results rtn))
3989 (setq rtn (org-agenda-get-timestamps))
3990 (setq results (append results rtn)))
3991 ((eq arg :sexp)
3992 (setq rtn (org-agenda-get-sexps))
3993 (setq results (append results rtn)))
3994 ((eq arg :scheduled)
3995 (setq rtn (org-agenda-get-scheduled deadline-results))
3996 (setq results (append results rtn)))
3997 ((eq arg :closed)
3998 (setq rtn (org-agenda-get-progress))
3999 (setq results (append results rtn)))
4000 ((eq arg :deadline)
4001 (setq rtn (org-agenda-get-deadlines))
4002 (setq deadline-results (copy-sequence rtn))
4003 (setq results (append results rtn))))))))
4004 results))))
4006 (defun org-agenda-get-todos ()
4007 "Return the TODO information for agenda display."
4008 (let* ((props (list 'face nil
4009 'done-face 'org-agenda-done
4010 'org-not-done-regexp org-not-done-regexp
4011 'org-todo-regexp org-todo-regexp
4012 'org-complex-heading-regexp org-complex-heading-regexp
4013 'mouse-face 'highlight
4014 'help-echo
4015 (format "mouse-2 or RET jump to org file %s"
4016 (abbreviate-file-name buffer-file-name))))
4017 (regexp (concat "^\\*+[ \t]+\\("
4018 (if org-select-this-todo-keyword
4019 (if (equal org-select-this-todo-keyword "*")
4020 org-todo-regexp
4021 (concat "\\<\\("
4022 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
4023 "\\)\\>"))
4024 org-not-done-regexp)
4025 "[^\n\r]*\\)"))
4026 marker priority category tags todo-state
4027 ee txt beg end)
4028 (goto-char (point-min))
4029 (while (re-search-forward regexp nil t)
4030 (catch :skip
4031 (save-match-data
4032 (beginning-of-line)
4033 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4034 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4035 (goto-char (1+ beg))
4036 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4037 (throw :skip nil)))
4038 (goto-char beg)
4039 (org-agenda-skip)
4040 (goto-char (match-beginning 1))
4041 (setq marker (org-agenda-new-marker (match-beginning 0))
4042 category (org-get-category)
4043 txt (match-string 1)
4044 tags (org-get-tags-at (point))
4045 txt (org-format-agenda-item "" txt category tags)
4046 priority (1+ (org-get-priority txt))
4047 todo-state (org-get-todo-state))
4048 (org-add-props txt props
4049 'org-marker marker 'org-hd-marker marker
4050 'priority priority 'org-category category
4051 'type "todo" 'todo-state todo-state)
4052 (push txt ee)
4053 (if org-agenda-todo-list-sublevels
4054 (goto-char (match-end 1))
4055 (org-end-of-subtree 'invisible))))
4056 (nreverse ee)))
4058 ;;;###autoload
4059 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item (&optional end)
4060 "Do we have a reason to ignore this todo entry because it has a time stamp?"
4061 (when (or org-agenda-todo-ignore-with-date
4062 org-agenda-todo-ignore-scheduled
4063 org-agenda-todo-ignore-deadlines)
4064 (setq end (or end (save-excursion (outline-next-heading) (point))))
4065 (save-excursion
4066 (or (and org-agenda-todo-ignore-with-date
4067 (re-search-forward org-ts-regexp end t))
4068 (and org-agenda-todo-ignore-scheduled
4069 (re-search-forward org-scheduled-time-regexp end t))
4070 (and org-agenda-todo-ignore-deadlines
4071 (re-search-forward org-deadline-time-regexp end t)
4072 (org-deadline-close (match-string 1)))))))
4074 (defconst org-agenda-no-heading-message
4075 "No heading for this item in buffer or region.")
4077 (defun org-agenda-get-timestamps ()
4078 "Return the date stamp information for agenda display."
4079 (let* ((props (list 'face nil
4080 'org-not-done-regexp org-not-done-regexp
4081 'org-todo-regexp org-todo-regexp
4082 'org-complex-heading-regexp org-complex-heading-regexp
4083 'mouse-face 'highlight
4084 'help-echo
4085 (format "mouse-2 or RET jump to org file %s"
4086 (abbreviate-file-name buffer-file-name))))
4087 (d1 (calendar-absolute-from-gregorian date))
4088 (remove-re
4089 (concat
4090 (regexp-quote
4091 (format-time-string
4092 "<%Y-%m-%d"
4093 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4094 ".*?>"))
4095 (regexp
4096 (concat
4097 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4098 (regexp-quote
4099 (substring
4100 (format-time-string
4101 (car org-time-stamp-formats)
4102 (apply 'encode-time ; DATE bound by calendar
4103 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4104 1 11))
4105 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4106 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4107 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4108 donep tmp priority category ee txt timestr tags b0 b3 e3 head
4109 todo-state end-of-match)
4110 (goto-char (point-min))
4111 (while (setq end-of-match (re-search-forward regexp nil t))
4112 (setq b0 (match-beginning 0)
4113 b3 (match-beginning 3) e3 (match-end 3))
4114 (catch :skip
4115 (and (org-at-date-range-p) (throw :skip nil))
4116 (org-agenda-skip)
4117 (if (and (match-end 1)
4118 (not (= d1 (org-time-string-to-absolute
4119 (match-string 1) d1 nil
4120 org-agenda-repeating-timestamp-show-all))))
4121 (throw :skip nil))
4122 (if (and e3
4123 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4124 (throw :skip nil))
4125 (setq tmp (buffer-substring (max (point-min)
4126 (- b0 org-ds-keyword-length))
4128 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4129 inactivep (= (char-after b0) ?\[)
4130 deadlinep (string-match org-deadline-regexp tmp)
4131 scheduledp (string-match org-scheduled-regexp tmp)
4132 closedp (and org-agenda-include-inactive-timestamps
4133 (string-match org-closed-string tmp))
4134 clockp (and org-agenda-include-inactive-timestamps
4135 (or (string-match org-clock-string tmp)
4136 (string-match "]-+\\'" tmp)))
4137 todo-state (org-get-todo-state)
4138 donep (member todo-state org-done-keywords))
4139 (if (or scheduledp deadlinep closedp clockp
4140 (and donep org-agenda-skip-timestamp-if-done))
4141 (throw :skip t))
4142 (if (string-match ">" timestr)
4143 ;; substring should only run to end of time stamp
4144 (setq timestr (substring timestr 0 (match-end 0))))
4145 (setq marker (org-agenda-new-marker b0)
4146 category (org-get-category b0))
4147 (save-excursion
4148 (if (not (re-search-backward "^\\*+ " nil t))
4149 (setq txt org-agenda-no-heading-message)
4150 (goto-char (match-beginning 0))
4151 (setq hdmarker (org-agenda-new-marker)
4152 tags (org-get-tags-at))
4153 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4154 (setq head (match-string 1))
4155 (setq txt (org-format-agenda-item
4156 (if inactivep org-agenda-inactive-leader nil)
4157 head category tags timestr nil
4158 remove-re)))
4159 (setq priority (org-get-priority txt))
4160 (org-add-props txt props
4161 'org-marker marker 'org-hd-marker hdmarker)
4162 (org-add-props txt nil 'priority priority
4163 'org-category category 'date date
4164 'todo-state todo-state
4165 'type "timestamp")
4166 (push txt ee))
4167 (if org-agenda-skip-additional-timestamps-same-entry
4168 (outline-next-heading)
4169 (goto-char end-of-match))))
4170 (nreverse ee)))
4172 (defun org-agenda-get-sexps ()
4173 "Return the sexp information for agenda display."
4174 (require 'diary-lib)
4175 (let* ((props (list 'face nil
4176 'mouse-face 'highlight
4177 'help-echo
4178 (format "mouse-2 or RET jump to org file %s"
4179 (abbreviate-file-name buffer-file-name))))
4180 (regexp "^&?%%(")
4181 marker category ee txt tags entry result beg b sexp sexp-entry
4182 todo-state)
4183 (goto-char (point-min))
4184 (while (re-search-forward regexp nil t)
4185 (catch :skip
4186 (org-agenda-skip)
4187 (setq beg (match-beginning 0))
4188 (goto-char (1- (match-end 0)))
4189 (setq b (point))
4190 (forward-sexp 1)
4191 (setq sexp (buffer-substring b (point)))
4192 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4193 (org-trim (match-string 1))
4194 ""))
4195 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4196 (when result
4197 (setq marker (org-agenda-new-marker beg)
4198 category (org-get-category beg)
4199 todo-state (org-get-todo-state))
4201 (if (string-match "\\S-" result)
4202 (setq txt result)
4203 (setq txt "SEXP entry returned empty string"))
4205 (setq txt (org-format-agenda-item
4206 "" txt category tags 'time))
4207 (org-add-props txt props 'org-marker marker)
4208 (org-add-props txt nil
4209 'org-category category 'date date 'todo-state todo-state
4210 'type "sexp")
4211 (push txt ee))))
4212 (nreverse ee)))
4214 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4215 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4216 Order of the parameters is M1, D1, Y1, M2, D2, Y2 if
4217 `european-calendar-style' is nil, and D1, M1, Y1, D2, M2, Y2 if
4218 `european-calendar-style' is t.
4219 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4220 is any number of ISO weeks in the block period for which the item should
4221 be skipped."
4222 (let* ((date1 (calendar-absolute-from-gregorian
4223 (if european-calendar-style
4224 (list d1 m1 y1)
4225 (list m1 d1 y1))))
4226 (date2 (calendar-absolute-from-gregorian
4227 (if european-calendar-style
4228 (list d2 m2 y2)
4229 (list m2 d2 y2))))
4230 (d (calendar-absolute-from-gregorian date)))
4231 (and
4232 (<= date1 d)
4233 (<= d date2)
4234 (= (calendar-day-of-week date) dayname)
4235 (or (not skip-weeks)
4236 (progn
4237 (require 'cal-iso)
4238 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4239 entry)))
4241 (defalias 'org-get-closed 'org-agenda-get-progress)
4242 (defun org-agenda-get-progress ()
4243 "Return the logged TODO entries for agenda display."
4244 (let* ((props (list 'mouse-face 'highlight
4245 'org-not-done-regexp org-not-done-regexp
4246 'org-todo-regexp org-todo-regexp
4247 'org-complex-heading-regexp org-complex-heading-regexp
4248 'help-echo
4249 (format "mouse-2 or RET jump to org file %s"
4250 (abbreviate-file-name buffer-file-name))))
4251 (items (if (consp org-agenda-show-log)
4252 org-agenda-show-log
4253 org-agenda-log-mode-items))
4254 (parts
4255 (delq nil
4256 (list
4257 (if (memq 'closed items) (concat "\\<" org-closed-string))
4258 (if (memq 'clock items) (concat "\\<" org-clock-string))
4259 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4260 (parts-re (if parts (mapconcat 'identity parts "\\|")
4261 (error "`org-agenda-log-mode-items' is empty")))
4262 (regexp (concat
4263 "\\(" parts-re "\\)"
4264 " *\\["
4265 (regexp-quote
4266 (substring
4267 (format-time-string
4268 (car org-time-stamp-formats)
4269 (apply 'encode-time ; DATE bound by calendar
4270 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4271 1 11))))
4272 (org-agenda-search-headline-for-time nil)
4273 marker hdmarker priority category tags closedp statep clockp state
4274 ee txt extra timestr rest clocked)
4275 (goto-char (point-min))
4276 (while (re-search-forward regexp nil t)
4277 (catch :skip
4278 (org-agenda-skip)
4279 (setq marker (org-agenda-new-marker (match-beginning 0))
4280 closedp (equal (match-string 1) org-closed-string)
4281 statep (equal (string-to-char (match-string 1)) ?-)
4282 clockp (not (or closedp statep))
4283 state (and statep (match-string 2))
4284 category (org-get-category (match-beginning 0))
4285 timestr (buffer-substring (match-beginning 0) (point-at-eol))
4287 (when (string-match "\\]" timestr)
4288 ;; substring should only run to end of time stamp
4289 (setq rest (substring timestr (match-end 0))
4290 timestr (substring timestr 0 (match-end 0)))
4291 (if (and (not closedp) (not statep)
4292 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
4293 (progn (setq timestr (concat (substring timestr 0 -1)
4294 "-" (match-string 1 rest) "]"))
4295 (setq clocked (match-string 2 rest)))
4296 (setq clocked "-")))
4297 (save-excursion
4298 (cond
4299 ((not org-agenda-log-mode-add-notes) (setq extra nil))
4300 (statep
4301 (and (looking-at ".*\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4302 (setq extra (match-string 1))))
4303 (clockp
4304 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4305 (setq extra (match-string 1))))
4306 (t (setq extra nil)))
4307 (if (not (re-search-backward "^\\*+ " nil t))
4308 (setq txt org-agenda-no-heading-message)
4309 (goto-char (match-beginning 0))
4310 (setq hdmarker (org-agenda-new-marker)
4311 tags (org-get-tags-at))
4312 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4313 (setq txt (match-string 1))
4314 (when extra
4315 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4316 (setq txt (concat (substring txt 0 (match-beginning 1))
4317 " - " extra " " (match-string 2 txt)))
4318 (setq txt (concat txt " - " extra))))
4319 (setq txt (org-format-agenda-item
4320 (cond
4321 (closedp "Closed: ")
4322 (statep (concat "State: (" state ")"))
4323 (t (concat "Clocked: (" clocked ")")))
4324 txt category tags timestr)))
4325 (setq priority 100000)
4326 (org-add-props txt props
4327 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
4328 'priority priority 'org-category category
4329 'type "closed" 'date date
4330 'undone-face 'org-warning 'done-face 'org-agenda-done)
4331 (push txt ee))
4332 (goto-char (point-at-eol))))
4333 (nreverse ee)))
4335 (defun org-agenda-get-deadlines ()
4336 "Return the deadline information for agenda display."
4337 (let* ((props (list 'mouse-face 'highlight
4338 'org-not-done-regexp org-not-done-regexp
4339 'org-todo-regexp org-todo-regexp
4340 'org-complex-heading-regexp org-complex-heading-regexp
4341 'help-echo
4342 (format "mouse-2 or RET jump to org file %s"
4343 (abbreviate-file-name buffer-file-name))))
4344 (regexp org-deadline-time-regexp)
4345 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
4346 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4347 d2 diff dfrac wdays pos pos1 category tags
4348 ee txt head face s todo-state upcomingp donep timestr)
4349 (goto-char (point-min))
4350 (while (re-search-forward regexp nil t)
4351 (catch :skip
4352 (org-agenda-skip)
4353 (setq s (match-string 1)
4354 txt nil
4355 pos (1- (match-beginning 1))
4356 d2 (org-time-string-to-absolute
4357 (match-string 1) d1 'past
4358 org-agenda-repeating-timestamp-show-all)
4359 diff (- d2 d1)
4360 wdays (org-get-wdays s)
4361 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
4362 upcomingp (and todayp (> diff 0)))
4363 ;; When to show a deadline in the calendar:
4364 ;; If the expiration is within wdays warning time.
4365 ;; Past-due deadlines are only shown on the current date
4366 (if (and (or (and (<= diff wdays)
4367 (and todayp (not org-agenda-only-exact-dates)))
4368 (= diff 0)))
4369 (save-excursion
4370 (setq todo-state (org-get-todo-state))
4371 (setq donep (member todo-state org-done-keywords))
4372 (if (and donep
4373 (or org-agenda-skip-deadline-if-done
4374 (not (= diff 0))))
4375 (setq txt nil)
4376 (setq category (org-get-category))
4377 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4378 (setq txt org-agenda-no-heading-message)
4379 (goto-char (match-end 0))
4380 (setq pos1 (match-beginning 0))
4381 (setq tags (org-get-tags-at pos1))
4382 (setq head (buffer-substring-no-properties
4383 (point)
4384 (progn (skip-chars-forward "^\r\n")
4385 (point))))
4386 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4387 (setq timestr
4388 (concat (substring s (match-beginning 1)) " "))
4389 (setq timestr 'time))
4390 (setq txt (org-format-agenda-item
4391 (if (= diff 0)
4392 (car org-agenda-deadline-leaders)
4393 (if (functionp
4394 (nth 1 org-agenda-deadline-leaders))
4395 (funcall
4396 (nth 1 org-agenda-deadline-leaders)
4397 diff date)
4398 (format (nth 1 org-agenda-deadline-leaders)
4399 diff)))
4400 head category tags
4401 (if (not (= diff 0)) nil timestr)))))
4402 (when txt
4403 (setq face (org-agenda-deadline-face dfrac wdays))
4404 (org-add-props txt props
4405 'org-marker (org-agenda-new-marker pos)
4406 'org-hd-marker (org-agenda-new-marker pos1)
4407 'priority (+ (- diff)
4408 (org-get-priority txt))
4409 'org-category category
4410 'todo-state todo-state
4411 'type (if upcomingp "upcoming-deadline" "deadline")
4412 'date (if upcomingp date d2)
4413 'face (if donep 'org-agenda-done face)
4414 'undone-face face 'done-face 'org-agenda-done)
4415 (push txt ee))))))
4416 (nreverse ee)))
4418 (defun org-agenda-deadline-face (fraction &optional wdays)
4419 "Return the face to displaying a deadline item.
4420 FRACTION is what fraction of the head-warning time has passed."
4421 (if (equal wdays 0) (setq fraction 1.))
4422 (let ((faces org-agenda-deadline-faces) f)
4423 (catch 'exit
4424 (while (setq f (pop faces))
4425 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
4427 (defun org-agenda-get-scheduled (&optional deadline-results)
4428 "Return the scheduled information for agenda display."
4429 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
4430 'org-todo-regexp org-todo-regexp
4431 'org-complex-heading-regexp org-complex-heading-regexp
4432 'done-face 'org-agenda-done
4433 'mouse-face 'highlight
4434 'help-echo
4435 (format "mouse-2 or RET jump to org file %s"
4436 (abbreviate-file-name buffer-file-name))))
4437 (regexp org-scheduled-time-regexp)
4438 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
4439 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4441 (deadline-position-alist
4442 (mapcar (lambda (a) (and (setq mm (get-text-property
4443 0 'org-hd-marker a))
4444 (cons (marker-position mm) a)))
4445 deadline-results))
4446 d2 diff pos pos1 category tags donep
4447 ee txt head pastschedp todo-state face timestr s habitp)
4448 (goto-char (point-min))
4449 (while (re-search-forward regexp nil t)
4450 (catch :skip
4451 (org-agenda-skip)
4452 (setq s (match-string 1)
4453 txt nil
4454 pos (1- (match-beginning 1))
4455 d2 (org-time-string-to-absolute
4456 (match-string 1) d1 'past
4457 org-agenda-repeating-timestamp-show-all)
4458 diff (- d2 d1))
4459 (setq pastschedp (and todayp (< diff 0)))
4460 ;; When to show a scheduled item in the calendar:
4461 ;; If it is on or past the date.
4462 (when (or (and (< diff 0)
4463 (< (abs diff) org-scheduled-past-days)
4464 (and todayp (not org-agenda-only-exact-dates)))
4465 (= diff 0))
4466 (save-excursion
4467 (setq todo-state (org-get-todo-state))
4468 (setq donep (member todo-state org-done-keywords))
4469 (setq habitp (and (functionp 'org-is-habit-p)
4470 (org-is-habit-p)))
4471 (if (and donep
4472 (or habitp org-agenda-skip-scheduled-if-done
4473 (not (= diff 0))))
4474 (setq txt nil)
4475 (setq category (org-get-category))
4476 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4477 (setq txt org-agenda-no-heading-message)
4478 (goto-char (match-end 0))
4479 (setq pos1 (match-beginning 0))
4480 (if habitp
4481 (if (or (not org-habit-show-habits)
4482 (and (not todayp)
4483 org-habit-show-habits-only-for-today))
4484 (throw :skip nil))
4485 (if (and
4486 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
4487 (and org-agenda-skip-scheduled-if-deadline-is-shown
4488 pastschedp))
4489 (setq mm (assoc pos1 deadline-position-alist)))
4490 (throw :skip nil)))
4491 (setq tags (org-get-tags-at))
4492 (setq head (buffer-substring-no-properties
4493 (point)
4494 (progn (skip-chars-forward "^\r\n") (point))))
4495 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4496 (setq timestr
4497 (concat (substring s (match-beginning 1)) " "))
4498 (setq timestr 'time))
4499 (setq txt (org-format-agenda-item
4500 (if (= diff 0)
4501 (car org-agenda-scheduled-leaders)
4502 (format (nth 1 org-agenda-scheduled-leaders)
4503 (- 1 diff)))
4504 head category tags
4505 (if (not (= diff 0)) nil timestr)
4506 nil nil habitp))))
4507 (when txt
4508 (setq face
4509 (cond
4510 ((and (not habitp) pastschedp)
4511 'org-scheduled-previously)
4512 (todayp 'org-scheduled-today)
4513 (t 'org-scheduled))
4514 habitp (and habitp (org-habit-parse-todo)))
4515 (org-add-props txt props
4516 'undone-face face
4517 'face (if donep 'org-agenda-done face)
4518 'org-marker (org-agenda-new-marker pos)
4519 'org-hd-marker (org-agenda-new-marker pos1)
4520 'type (if pastschedp "past-scheduled" "scheduled")
4521 'date (if pastschedp d2 date)
4522 'priority (if habitp
4523 (org-habit-get-priority habitp)
4524 (+ 94 (- 5 diff) (org-get-priority txt)))
4525 'org-category category
4526 'org-habit-p habitp
4527 'todo-state todo-state)
4528 (push txt ee))))))
4529 (nreverse ee)))
4531 (defun org-agenda-get-blocks ()
4532 "Return the date-range information for agenda display."
4533 (let* ((props (list 'face nil
4534 'org-not-done-regexp org-not-done-regexp
4535 'org-todo-regexp org-todo-regexp
4536 'org-complex-heading-regexp org-complex-heading-regexp
4537 'mouse-face 'highlight
4538 'help-echo
4539 (format "mouse-2 or RET jump to org file %s"
4540 (abbreviate-file-name buffer-file-name))))
4541 (regexp org-tr-regexp)
4542 (d0 (calendar-absolute-from-gregorian date))
4543 marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
4544 head donep)
4545 (goto-char (point-min))
4546 (while (re-search-forward regexp nil t)
4547 (catch :skip
4548 (org-agenda-skip)
4549 (setq pos (point))
4550 (setq timestr (match-string 0)
4551 s1 (match-string 1)
4552 s2 (match-string 2)
4553 d1 (time-to-days (org-time-string-to-time s1))
4554 d2 (time-to-days (org-time-string-to-time s2)))
4555 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
4556 ;; Only allow days between the limits, because the normal
4557 ;; date stamps will catch the limits.
4558 (save-excursion
4559 (setq todo-state (org-get-todo-state))
4560 (setq donep (member todo-state org-done-keywords))
4561 (if (and donep org-agenda-skip-timestamp-if-done)
4562 (throw :skip t))
4563 (setq marker (org-agenda-new-marker (point)))
4564 (setq category (org-get-category))
4565 (if (not (re-search-backward "^\\*+ " nil t))
4566 (setq txt org-agenda-no-heading-message)
4567 (goto-char (match-beginning 0))
4568 (setq hdmarker (org-agenda-new-marker (point)))
4569 (setq tags (org-get-tags-at))
4570 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4571 (setq head (match-string 1))
4572 (setq txt (org-format-agenda-item
4573 (format
4574 (nth (if (= d1 d2) 0 1)
4575 org-agenda-timerange-leaders)
4576 (1+ (- d0 d1)) (1+ (- d2 d1)))
4577 head category tags
4578 timestr)))
4579 (org-add-props txt props
4580 'org-marker marker 'org-hd-marker hdmarker
4581 'type "block" 'date date
4582 'todo-state todo-state
4583 'priority (org-get-priority txt) 'org-category category)
4584 (push txt ee)))
4585 (goto-char pos)))
4586 ;; Sort the entries by expiration date.
4587 (nreverse ee)))
4589 ;;; Agenda presentation and sorting
4591 (defvar org-prefix-has-time nil
4592 "A flag, set by `org-compile-prefix-format'.
4593 The flag is set if the currently compiled format contains a `%t'.")
4594 (defvar org-prefix-has-tag nil
4595 "A flag, set by `org-compile-prefix-format'.
4596 The flag is set if the currently compiled format contains a `%T'.")
4597 (defvar org-prefix-has-effort nil
4598 "A flag, set by `org-compile-prefix-format'.
4599 The flag is set if the currently compiled format contains a `%e'.")
4600 (defvar org-prefix-category-length nil
4601 "Used by `org-compile-prefix-format' to remember the category field widh.")
4602 (defvar org-prefix-category-max-length nil
4603 "Used by `org-compile-prefix-format' to remember the category field widh.")
4605 (defun org-format-agenda-item (extra txt &optional category tags dotime
4606 noprefix remove-re habitp)
4607 "Format TXT to be inserted into the agenda buffer.
4608 In particular, it adds the prefix and corresponding text properties. EXTRA
4609 must be a string and replaces the `%s' specifier in the prefix format.
4610 CATEGORY (string, symbol or nil) may be used to overrule the default
4611 category taken from local variable or file name. It will replace the `%c'
4612 specifier in the format. DOTIME, when non-nil, indicates that a
4613 time-of-day should be extracted from TXT for sorting of this entry, and for
4614 the `%t' specifier in the format. When DOTIME is a string, this string is
4615 searched for a time before TXT is. NOPREFIX is a flag and indicates that
4616 only the correctly processes TXT should be returned - this is used by
4617 `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
4618 Any match of REMOVE-RE will be removed from TXT."
4619 (save-match-data
4620 ;; Diary entries sometimes have extra whitespace at the beginning
4621 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
4623 ;; Fix the tags part in txt
4624 (setq txt (org-agenda-fix-displayed-tags
4625 txt tags
4626 org-agenda-show-inherited-tags
4627 org-agenda-hide-tags-regexp))
4628 (let* ((category (or category
4629 org-category
4630 (if buffer-file-name
4631 (file-name-sans-extension
4632 (file-name-nondirectory buffer-file-name))
4633 "")))
4634 ;; time, tag, effort are needed for the eval of the prefix format
4635 (tag (if tags (nth (1- (length tags)) tags) ""))
4636 time effort neffort
4637 (ts (if dotime (concat
4638 (if (stringp dotime) dotime "")
4639 (and org-agenda-search-headline-for-time txt))))
4640 (time-of-day (and dotime (org-get-time-of-day ts)))
4641 stamp plain s0 s1 s2 t1 t2 rtn srp l
4642 duration thecategory)
4643 (and (org-mode-p) buffer-file-name
4644 (add-to-list 'org-agenda-contributing-files buffer-file-name))
4645 (when (and dotime time-of-day)
4646 ;; Extract starting and ending time and move them to prefix
4647 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
4648 (setq plain (string-match org-plain-time-of-day-regexp ts)))
4649 (setq s0 (match-string 0 ts)
4650 srp (and stamp (match-end 3))
4651 s1 (match-string (if plain 1 2) ts)
4652 s2 (match-string (if plain 8 (if srp 4 6)) ts))
4654 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
4655 ;; them, we might want to remove them there to avoid duplication.
4656 ;; The user can turn this off with a variable.
4657 (if (and org-prefix-has-time
4658 org-agenda-remove-times-when-in-prefix (or stamp plain)
4659 (string-match (concat (regexp-quote s0) " *") txt)
4660 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
4661 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
4662 (= (match-beginning 0) 0)
4664 (setq txt (replace-match "" nil nil txt))))
4665 ;; Normalize the time(s) to 24 hour
4666 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
4667 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
4668 ;; Compute the duration
4669 (when s1
4670 (setq t1 (+ (* 60 (string-to-number (substring s1 0 2)))
4671 (string-to-number (substring s1 3)))
4672 t2 (cond
4673 (s2 (+ (* 60 (string-to-number (substring s2 0 2)))
4674 (string-to-number (substring s2 3))))
4675 (org-agenda-default-appointment-duration
4676 (+ t1 org-agenda-default-appointment-duration))
4677 (t nil)))
4678 (setq duration (if t2 (- t2 t1)))))
4680 (when (and s1 (not s2) org-agenda-default-appointment-duration
4681 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
4682 (let ((m (+ (string-to-number (match-string 2 s1))
4683 (* 60 (string-to-number (match-string 1 s1)))
4684 org-agenda-default-appointment-duration))
4686 (setq h (/ m 60) m (- m (* h 60)))
4687 (setq s2 (format "%02d:%02d" h m))))
4689 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
4690 txt)
4691 ;; Tags are in the string
4692 (if (or (eq org-agenda-remove-tags t)
4693 (and org-agenda-remove-tags
4694 org-prefix-has-tag))
4695 (setq txt (replace-match "" t t txt))
4696 (setq txt (replace-match
4697 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
4698 (match-string 2 txt))
4699 t t txt))))
4700 (when (org-mode-p)
4701 (setq effort
4702 (condition-case nil
4703 (org-get-effort
4704 (or (get-text-property 0 'org-hd-marker txt)
4705 (get-text-property 0 'org-marker txt)))
4706 (error nil)))
4707 (when effort
4708 (setq neffort (org-hh:mm-string-to-minutes effort)
4709 effort (setq effort (concat "[" effort "]" )))))
4711 (when remove-re
4712 (while (string-match remove-re txt)
4713 (setq txt (replace-match "" t t txt))))
4715 ;; Create the final string
4716 (if noprefix
4717 (setq rtn txt)
4718 ;; Prepare the variables needed in the eval of the compiled format
4719 (setq time (cond (s2 (concat s1 "-" s2))
4720 (s1 (concat s1 "......"))
4721 (t ""))
4722 extra (or (and (not habitp) extra) "")
4723 category (if (symbolp category) (symbol-name category) category)
4724 thecategory (copy-sequence category))
4725 (if (string-match org-bracket-link-regexp category)
4726 (progn
4727 (setq l (if (match-end 3)
4728 (- (match-end 3) (match-beginning 3))
4729 (- (match-end 1) (match-beginning 1))))
4730 (when (< l (or org-prefix-category-length 0))
4731 (setq category (copy-sequence category))
4732 (org-add-props category nil
4733 'extra-space (make-string
4734 (- org-prefix-category-length l 1) ?\ ))))
4735 (if (and org-prefix-category-max-length
4736 (>= (length category) org-prefix-category-max-length))
4737 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
4738 ;; Evaluate the compiled format
4739 (setq rtn (concat (eval org-prefix-format-compiled) txt)))
4741 ;; And finally add the text properties
4742 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
4743 (org-add-props rtn nil
4744 'org-category (if thecategory (downcase thecategory) category)
4745 'tags (mapcar 'org-downcase-keep-props tags)
4746 'org-highest-priority org-highest-priority
4747 'org-lowest-priority org-lowest-priority
4748 'prefix-length (- (length rtn) (length txt))
4749 'time-of-day time-of-day
4750 'duration duration
4751 'effort effort
4752 'effort-minutes neffort
4753 'txt txt
4754 'time time
4755 'extra extra
4756 'dotime dotime))))
4758 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
4759 "Remove tags string from TXT, and add a modified list of tags.
4760 The modified list may contain inherited tags, and tags matched by
4761 `org-agenda-hide-tags-regexp' will be removed."
4762 (when (or add-inherited hide-re)
4763 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$") txt)
4764 (setq txt (substring txt 0 (match-beginning 0))))
4765 (when tags
4766 (setq tags
4767 (delq nil
4768 (mapcar (lambda (tg)
4769 (if (or (and hide-re (string-match hide-re tg))
4770 (and (not add-inherited)
4771 (get-text-property 0 'inherited tg)))
4773 tg))
4774 tags)))
4775 (let ((have-i (get-text-property 0 'inherited (car tags)))
4777 (setq txt (concat txt " :"
4778 (mapconcat
4779 (lambda (x)
4780 (setq i (get-text-property 0 'inherited x))
4781 (if (and have-i (not i))
4782 (progn
4783 (setq have-i nil)
4784 (concat ":" x))
4786 tags ":")
4787 (if have-i "::" ":"))))))
4788 txt)
4790 (defun org-downcase-keep-props (s)
4791 (let ((props (text-properties-at 0 s)))
4792 (setq s (downcase s))
4793 (add-text-properties 0 (length s) props s)
4796 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
4797 (defvar org-agenda-sorting-strategy-selected nil)
4799 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
4800 (catch 'exit
4801 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
4802 ((and todayp (member 'today (car org-agenda-time-grid))))
4803 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
4804 ((member 'weekly (car org-agenda-time-grid)))
4805 (t (throw 'exit list)))
4806 (let* ((have (delq nil (mapcar
4807 (lambda (x) (get-text-property 1 'time-of-day x))
4808 list)))
4809 (string (nth 1 org-agenda-time-grid))
4810 (gridtimes (nth 2 org-agenda-time-grid))
4811 (req (car org-agenda-time-grid))
4812 (remove (member 'remove-match req))
4813 new time)
4814 (if (and (member 'require-timed req) (not have))
4815 ;; don't show empty grid
4816 (throw 'exit list))
4817 (while (setq time (pop gridtimes))
4818 (unless (and remove (member time have))
4819 (setq time (int-to-string time))
4820 (push (org-format-agenda-item
4821 nil string "" nil
4822 (concat (substring time 0 -2) ":" (substring time -2)))
4823 new)
4824 (put-text-property
4825 1 (length (car new)) 'face 'org-time-grid (car new))))
4826 (if (member 'time-up org-agenda-sorting-strategy-selected)
4827 (append new list)
4828 (append list new)))))
4830 (defun org-compile-prefix-format (key)
4831 "Compile the prefix format into a Lisp form that can be evaluated.
4832 The resulting form is returned and stored in the variable
4833 `org-prefix-format-compiled'."
4834 (setq org-prefix-has-time nil org-prefix-has-tag nil
4835 org-prefix-category-length nil org-prefix-has-effort nil)
4836 (let ((s (cond
4837 ((stringp org-agenda-prefix-format)
4838 org-agenda-prefix-format)
4839 ((assq key org-agenda-prefix-format)
4840 (cdr (assq key org-agenda-prefix-format)))
4841 (t " %-12:c%?-12t% s")))
4842 (start 0)
4843 varform vars var e c f opt)
4844 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctse]\\)"
4845 s start)
4846 (setq var (cdr (assoc (match-string 4 s)
4847 '(("c" . category) ("t" . time) ("s" . extra)
4848 ("T" . tag) ("e" . effort))))
4849 c (or (match-string 3 s) "")
4850 opt (match-beginning 1)
4851 start (1+ (match-beginning 0)))
4852 (if (equal var 'time) (setq org-prefix-has-time t))
4853 (if (equal var 'tag) (setq org-prefix-has-tag t))
4854 (if (equal var 'effort) (setq org-prefix-has-effort t))
4855 (setq f (concat "%" (match-string 2 s) "s"))
4856 (when (equal var 'category)
4857 (setq org-prefix-category-length
4858 (floor (abs (string-to-number (match-string 2 s)))))
4859 (setq org-prefix-category-max-length
4860 (let ((x (match-string 2 s)))
4861 (save-match-data
4862 (if (string-match "\\.[0-9]+" x)
4863 (string-to-number (substring (match-string 0 x) 1)))))))
4864 (if opt
4865 (setq varform
4866 `(if (equal "" ,var)
4868 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
4869 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var))))))
4870 (setq s (replace-match "%s" t nil s))
4871 (push varform vars))
4872 (setq vars (nreverse vars))
4873 (setq org-prefix-format-compiled `(format ,s ,@vars))))
4875 (defun org-set-sorting-strategy (key)
4876 (if (symbolp (car org-agenda-sorting-strategy))
4877 ;; the old format
4878 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
4879 (setq org-agenda-sorting-strategy-selected
4880 (or (cdr (assq key org-agenda-sorting-strategy))
4881 (cdr (assq 'agenda org-agenda-sorting-strategy))
4882 '(time-up category-keep priority-down)))))
4884 (defun org-get-time-of-day (s &optional string mod24)
4885 "Check string S for a time of day.
4886 If found, return it as a military time number between 0 and 2400.
4887 If not found, return nil.
4888 The optional STRING argument forces conversion into a 5 character wide string
4889 HH:MM."
4890 (save-match-data
4891 (when
4892 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
4893 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
4894 (let* ((h (string-to-number (match-string 1 s)))
4895 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
4896 (ampm (if (match-end 4) (downcase (match-string 4 s))))
4897 (am-p (equal ampm "am"))
4898 (h1 (cond ((not ampm) h)
4899 ((= h 12) (if am-p 0 12))
4900 (t (+ h (if am-p 0 12)))))
4901 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
4902 (mod h1 24) h1))
4903 (t0 (+ (* 100 h2) m))
4904 (t1 (concat (if (>= h1 24) "+" " ")
4905 (if (< t0 100) "0" "")
4906 (if (< t0 10) "0" "")
4907 (int-to-string t0))))
4908 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
4910 (defun org-finalize-agenda-entries (list &optional nosort)
4911 "Sort and concatenate the agenda items."
4912 (setq list (mapcar 'org-agenda-highlight-todo list))
4913 (if nosort
4914 list
4915 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
4917 (defun org-agenda-highlight-todo (x)
4918 (let ((org-done-keywords org-done-keywords-for-agenda)
4919 (case-fold-search nil)
4920 re pl)
4921 (if (eq x 'line)
4922 (save-excursion
4923 (beginning-of-line 1)
4924 (setq re (org-get-at-bol 'org-todo-regexp))
4925 (goto-char (+ (point) (or (org-get-at-bol 'prefix-length) 0)))
4926 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
4927 (add-text-properties (match-beginning 0) (match-end 1)
4928 (list 'face (org-get-todo-face 1)))
4929 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
4930 (delete-region (match-beginning 1) (1- (match-end 0)))
4931 (goto-char (match-beginning 1))
4932 (insert (format org-agenda-todo-keyword-format s)))))
4933 (setq re (concat (get-text-property 0 'org-todo-regexp x))
4934 pl (get-text-property 0 'prefix-length x))
4935 (when (and re
4936 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
4937 x (or pl 0)) pl))
4938 (add-text-properties
4939 (or (match-end 1) (match-end 0)) (match-end 0)
4940 (list 'face (org-get-todo-face (match-string 2 x)))
4942 (setq x (concat (substring x 0 (match-end 1))
4943 (format org-agenda-todo-keyword-format
4944 (match-string 2 x))
4945 (org-add-props " " (text-properties-at 0 x))
4946 (substring x (match-end 3)))))
4947 x)))
4949 (defsubst org-cmp-priority (a b)
4950 "Compare the priorities of string A and B."
4951 (let ((pa (or (get-text-property 1 'priority a) 0))
4952 (pb (or (get-text-property 1 'priority b) 0)))
4953 (cond ((> pa pb) +1)
4954 ((< pa pb) -1)
4955 (t nil))))
4957 (defsubst org-cmp-effort (a b)
4958 "Compare the priorities of string A and B."
4959 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
4960 (ea (or (get-text-property 1 'effort-minutes a) def))
4961 (eb (or (get-text-property 1 'effort-minutes b) def)))
4962 (cond ((> ea eb) +1)
4963 ((< ea eb) -1)
4964 (t nil))))
4966 (defsubst org-cmp-category (a b)
4967 "Compare the string values of categories of strings A and B."
4968 (let ((ca (or (get-text-property 1 'org-category a) ""))
4969 (cb (or (get-text-property 1 'org-category b) "")))
4970 (cond ((string-lessp ca cb) -1)
4971 ((string-lessp cb ca) +1)
4972 (t nil))))
4974 (defsubst org-cmp-todo-state (a b)
4975 "Compare the todo states of strings A and B."
4976 (let* ((ma (or (get-text-property 1 'org-marker a)
4977 (get-text-property 1 'org-hd-marker a)))
4978 (mb (or (get-text-property 1 'org-marker b)
4979 (get-text-property 1 'org-hd-marker b)))
4980 (fa (and ma (marker-buffer ma)))
4981 (fb (and mb (marker-buffer mb)))
4982 (todo-kwds
4983 (or (and fa (with-current-buffer fa org-todo-keywords-1))
4984 (and fb (with-current-buffer fb org-todo-keywords-1))))
4985 (ta (or (get-text-property 1 'todo-state a) ""))
4986 (tb (or (get-text-property 1 'todo-state b) ""))
4987 (la (- (length (member ta todo-kwds))))
4988 (lb (- (length (member tb todo-kwds))))
4989 (donepa (member ta org-done-keywords-for-agenda))
4990 (donepb (member tb org-done-keywords-for-agenda)))
4991 (cond ((and donepa (not donepb)) -1)
4992 ((and (not donepa) donepb) +1)
4993 ((< la lb) -1)
4994 ((< lb la) +1)
4995 (t nil))))
4997 (defsubst org-cmp-tag (a b)
4998 "Compare the string values of the first tags of A and B."
4999 (let ((ta (car (last (get-text-property 1 'tags a))))
5000 (tb (car (last (get-text-property 1 'tags b)))))
5001 (cond ((not ta) +1)
5002 ((not tb) -1)
5003 ((string-lessp ta tb) -1)
5004 ((string-lessp tb ta) +1)
5005 (t nil))))
5007 (defsubst org-cmp-time (a b)
5008 "Compare the time-of-day values of strings A and B."
5009 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5010 (ta (or (get-text-property 1 'time-of-day a) def))
5011 (tb (or (get-text-property 1 'time-of-day b) def)))
5012 (cond ((< ta tb) -1)
5013 ((< tb ta) +1)
5014 (t nil))))
5016 (defsubst org-cmp-habit-p (a b)
5017 "Compare the todo states of strings A and B."
5018 (let ((ha (get-text-property 1 'org-habit-p a))
5019 (hb (get-text-property 1 'org-habit-p b)))
5020 (cond ((and ha (not hb)) -1)
5021 ((and (not ha) hb) +1)
5022 (t nil))))
5024 (defun org-entries-lessp (a b)
5025 "Predicate for sorting agenda entries."
5026 ;; The following variables will be used when the form is evaluated.
5027 ;; So even though the compiler complains, keep them.
5028 (let* ((time-up (org-cmp-time a b))
5029 (time-down (if time-up (- time-up) nil))
5030 (priority-up (org-cmp-priority a b))
5031 (priority-down (if priority-up (- priority-up) nil))
5032 (effort-up (org-cmp-effort a b))
5033 (effort-down (if effort-up (- effort-up) nil))
5034 (category-up (org-cmp-category a b))
5035 (category-down (if category-up (- category-up) nil))
5036 (category-keep (if category-up +1 nil))
5037 (tag-up (org-cmp-tag a b))
5038 (tag-down (if tag-up (- tag-up) nil))
5039 (todo-state-up (org-cmp-todo-state a b))
5040 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5041 (habit-up (org-cmp-habit-p a b))
5042 (habit-down (if habit-up (- habit-up) nil))
5043 user-defined-up user-defined-down)
5044 (if (and org-agenda-cmp-user-defined
5045 (functionp org-agenda-cmp-user-defined))
5046 (setq user-defined-up
5047 (funcall org-agenda-cmp-user-defined a b)
5048 user-defined-down (if user-defined-up (- user-defined-up) nil)))
5049 (cdr (assoc
5050 (eval (cons 'or org-agenda-sorting-strategy-selected))
5051 '((-1 . t) (1 . nil) (nil . nil))))))
5053 ;;; Agenda restriction lock
5055 (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
5056 "Overlay to mark the headline to which agenda commands are restricted.")
5057 (org-overlay-put org-agenda-restriction-lock-overlay
5058 'face 'org-agenda-restriction-lock)
5059 (org-overlay-put org-agenda-restriction-lock-overlay
5060 'help-echo "Agendas are currently limited to this subtree.")
5061 (org-detach-overlay org-agenda-restriction-lock-overlay)
5063 (defun org-agenda-set-restriction-lock (&optional type)
5064 "Set restriction lock for agenda, to current subtree or file.
5065 Restriction will be the file if TYPE is `file', or if type is the
5066 universal prefix '(4), or if the cursor is before the first headline
5067 in the file. Otherwise, restriction will be to the current subtree."
5068 (interactive "P")
5069 (and (equal type '(4)) (setq type 'file))
5070 (setq type (cond
5071 (type type)
5072 ((org-at-heading-p) 'subtree)
5073 ((condition-case nil (org-back-to-heading t) (error nil))
5074 'subtree)
5075 (t 'file)))
5076 (if (eq type 'subtree)
5077 (progn
5078 (setq org-agenda-restrict t)
5079 (setq org-agenda-overriding-restriction 'subtree)
5080 (put 'org-agenda-files 'org-restrict
5081 (list (buffer-file-name (buffer-base-buffer))))
5082 (org-back-to-heading t)
5083 (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
5084 (move-marker org-agenda-restrict-begin (point))
5085 (move-marker org-agenda-restrict-end
5086 (save-excursion (org-end-of-subtree t)))
5087 (message "Locking agenda restriction to subtree"))
5088 (put 'org-agenda-files 'org-restrict
5089 (list (buffer-file-name (buffer-base-buffer))))
5090 (setq org-agenda-restrict nil)
5091 (setq org-agenda-overriding-restriction 'file)
5092 (move-marker org-agenda-restrict-begin nil)
5093 (move-marker org-agenda-restrict-end nil)
5094 (message "Locking agenda restriction to file"))
5095 (setq current-prefix-arg nil)
5096 (org-agenda-maybe-redo))
5098 (defun org-agenda-remove-restriction-lock (&optional noupdate)
5099 "Remove the agenda restriction lock."
5100 (interactive "P")
5101 (org-detach-overlay org-agenda-restriction-lock-overlay)
5102 (org-detach-overlay org-speedbar-restriction-lock-overlay)
5103 (setq org-agenda-overriding-restriction nil)
5104 (setq org-agenda-restrict nil)
5105 (put 'org-agenda-files 'org-restrict nil)
5106 (move-marker org-agenda-restrict-begin nil)
5107 (move-marker org-agenda-restrict-end nil)
5108 (setq current-prefix-arg nil)
5109 (message "Agenda restriction lock removed")
5110 (or noupdate (org-agenda-maybe-redo)))
5112 (defun org-agenda-maybe-redo ()
5113 "If there is any window showing the agenda view, update it."
5114 (let ((w (get-buffer-window org-agenda-buffer-name t))
5115 (w0 (selected-window)))
5116 (when w
5117 (select-window w)
5118 (org-agenda-redo)
5119 (select-window w0)
5120 (if org-agenda-overriding-restriction
5121 (message "Agenda view shifted to new %s restriction"
5122 org-agenda-overriding-restriction)
5123 (message "Agenda restriction lock removed")))))
5125 ;;; Agenda commands
5127 (defun org-agenda-check-type (error &rest types)
5128 "Check if agenda buffer is of allowed type.
5129 If ERROR is non-nil, throw an error, otherwise just return nil."
5130 (if (memq org-agenda-type types)
5132 (if error
5133 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
5134 nil)))
5136 (defun org-agenda-quit ()
5137 "Exit agenda by removing the window or the buffer."
5138 (interactive)
5139 (if org-agenda-columns-active
5140 (org-columns-quit)
5141 (let ((buf (current-buffer)))
5142 (if (eq org-agenda-window-setup 'other-frame)
5143 (progn
5144 (kill-buffer buf)
5145 (org-agenda-reset-markers)
5146 (org-columns-remove-overlays)
5147 (setq org-agenda-archives-mode nil)
5148 (delete-frame))
5149 (and (not (eq org-agenda-window-setup 'current-window))
5150 (not (one-window-p))
5151 (delete-window))
5152 (kill-buffer buf)
5153 (org-agenda-reset-markers)
5154 (org-columns-remove-overlays)
5155 (setq org-agenda-archives-mode nil)))
5156 ;; Maybe restore the pre-agenda window configuration.
5157 (and org-agenda-restore-windows-after-quit
5158 (not (eq org-agenda-window-setup 'other-frame))
5159 org-pre-agenda-window-conf
5160 (set-window-configuration org-pre-agenda-window-conf))))
5162 (defun org-agenda-exit ()
5163 "Exit agenda by removing the window or the buffer.
5164 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
5165 Org-mode buffers visited directly by the user will not be touched."
5166 (interactive)
5167 (org-release-buffers org-agenda-new-buffers)
5168 (setq org-agenda-new-buffers nil)
5169 (org-agenda-quit))
5171 (defun org-agenda-execute (arg)
5172 "Execute another agenda command, keeping same window.\\<global-map>
5173 So this is just a shortcut for `\\[org-agenda]', available in the agenda."
5174 (interactive "P")
5175 (let ((org-agenda-window-setup 'current-window))
5176 (org-agenda arg)))
5178 (defun org-agenda-redo ()
5179 "Rebuild Agenda.
5180 When this is the global TODO list, a prefix argument will be interpreted."
5181 (interactive)
5182 (let* ((org-agenda-keep-modes t)
5183 (filter org-agenda-filter)
5184 (preset (get 'org-agenda-filter :preset-filter))
5185 (cols org-agenda-columns-active)
5186 (line (org-current-line))
5187 (window-line (- line (org-current-line (window-start))))
5188 (lprops (get 'org-agenda-redo-command 'org-lprops)))
5189 (put 'org-agenda-filter :preset-filter nil)
5190 (and cols (org-columns-quit))
5191 (message "Rebuilding agenda buffer...")
5192 (org-let lprops '(eval org-agenda-redo-command))
5193 (setq org-agenda-undo-list nil
5194 org-agenda-pending-undo-list nil)
5195 (message "Rebuilding agenda buffer...done")
5196 (put 'org-agenda-filter :preset-filter preset)
5197 (and (or filter preset) (org-agenda-filter-apply filter))
5198 (and cols (interactive-p) (org-agenda-columns))
5199 (org-goto-line line)
5200 (recenter window-line)))
5203 (defvar org-global-tags-completion-table nil)
5204 (defvar org-agenda-filter-form nil)
5205 (defun org-agenda-filter-by-tag (strip &optional char narrow)
5206 "Keep only those lines in the agenda buffer that have a specific tag.
5207 The tag is selected with its fast selection letter, as configured.
5208 With prefix argument STRIP, remove all lines that do have the tag.
5209 A lisp caller can specify CHAR. NARROW means that the new tag should be
5210 used to narrow the search - the interactive user can also press `-' or `+'
5211 to switch to narrowing."
5212 (interactive "P")
5213 (let* ((alist org-tag-alist-for-agenda)
5214 (tag-chars (mapconcat
5215 (lambda (x) (if (and (not (symbolp (car x)))
5216 (cdr x))
5217 (char-to-string (cdr x))
5218 ""))
5219 alist ""))
5220 (efforts (org-split-string
5221 (or (cdr (assoc (concat org-effort-property "_ALL")
5222 org-global-properties))
5223 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00" "")))
5224 (effort-op org-agenda-filter-effort-default-operator)
5225 (effort-prompt "")
5226 (inhibit-read-only t)
5227 (current org-agenda-filter)
5228 a n tag)
5229 (unless char
5230 (message
5231 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
5232 (if narrow "Narrow" "Filter") tag-chars
5233 (if org-agenda-auto-exclude-function "[RET], " ""))
5234 (setq char (read-char)))
5235 (when (member char '(?+ ?-))
5236 ;; Narrowing down
5237 (cond ((equal char ?-) (setq strip t narrow t))
5238 ((equal char ?+) (setq strip nil narrow t)))
5239 (message
5240 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
5241 (setq char (read-char)))
5242 (when (member char '(?< ?> ?= ??))
5243 ;; An effort operator
5244 (setq effort-op (char-to-string char))
5245 (setq alist nil) ; to make sure it will be interpreted as effort.
5246 (unless (equal char ??)
5247 (loop for i from 0 to 9 do
5248 (setq effort-prompt
5249 (concat
5250 effort-prompt " ["
5251 (if (= i 9) "0" (int-to-string (1+ i)))
5252 "]" (nth i efforts))))
5253 (message "Effort%s: %s " effort-op effort-prompt)
5254 (setq char (read-char))
5255 (when (or (< char ?0) (> char ?9))
5256 (error "Need 1-9,0 to select effort" ))))
5257 (when (equal char ?\t)
5258 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
5259 (org-set-local 'org-global-tags-completion-table
5260 (org-global-tags-completion-table)))
5261 (let ((completion-ignore-case t))
5262 (setq tag (org-icompleting-read
5263 "Tag: " org-global-tags-completion-table))))
5264 (cond
5265 ((equal char ?\r)
5266 (org-agenda-filter-by-tag-show-all)
5267 (when org-agenda-auto-exclude-function
5268 (setq org-agenda-filter '())
5269 (dolist (tag (org-agenda-get-represented-tags))
5270 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
5271 (if modifier
5272 (push modifier org-agenda-filter))))
5273 (if (not (null org-agenda-filter))
5274 (org-agenda-filter-apply org-agenda-filter))))
5275 ((equal char ?/)
5276 (org-agenda-filter-by-tag-show-all)
5277 (when (get 'org-agenda-filter :preset-filter)
5278 (org-agenda-filter-apply org-agenda-filter)))
5279 ((or (equal char ?\ )
5280 (setq a (rassoc char alist))
5281 (and (>= char ?0) (<= char ?9)
5282 (setq n (if (= char ?0) 9 (- char ?0 1))
5283 tag (concat effort-op (nth n efforts))
5284 a (cons tag nil)))
5285 (and (= char ??)
5286 (setq tag "?eff")
5287 a (cons tag nil))
5288 (and tag (setq a (cons tag nil))))
5289 (org-agenda-filter-by-tag-show-all)
5290 (setq tag (car a))
5291 (setq org-agenda-filter
5292 (cons (concat (if strip "-" "+") tag)
5293 (if narrow current nil)))
5294 (org-agenda-filter-apply org-agenda-filter))
5295 (t (error "Invalid tag selection character %c" char)))))
5297 (defun org-agenda-get-represented-tags ()
5298 "Get a list of all tags currently represented in the agenda."
5299 (let (p tags)
5300 (save-excursion
5301 (goto-char (point-min))
5302 (while (setq p (next-single-property-change (point) 'tags))
5303 (goto-char p)
5304 (mapc (lambda (x) (add-to-list 'tags x))
5305 (get-text-property (point) 'tags))))
5306 tags))
5308 (defun org-agenda-filter-by-tag-refine (strip &optional char)
5309 "Refine the current filter. See `org-agenda-filter-by-tag."
5310 (interactive "P")
5311 (org-agenda-filter-by-tag strip char 'refine))
5313 (defun org-agenda-filter-make-matcher ()
5314 "Create the form that tests a line for the agenda filter."
5315 (let (f f1)
5316 (dolist (x (append (get 'org-agenda-filter :preset-filter)
5317 org-agenda-filter))
5318 (if (member x '("-" "+"))
5319 (setq f1 (if (equal x "-") 'tags '(not tags)))
5320 (if (string-match "[<=>?]" x)
5321 (setq f1 (org-agenda-filter-effort-form x))
5322 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
5323 (if (equal (string-to-char x) ?-)
5324 (setq f1 (list 'not f1))))
5325 (push f1 f))
5326 (cons 'and (nreverse f))))
5328 (defun org-agenda-filter-effort-form (e)
5329 "Return the form to compare the effort of the current line with what E says.
5330 E looks line \"+<2:25\"."
5331 (let (op)
5332 (setq e (substring e 1))
5333 (setq op (string-to-char e) e (substring e 1))
5334 (setq op (cond ((equal op ?<) '<=)
5335 ((equal op ?>) '>=)
5336 ((equal op ??) op)
5337 (t '=)))
5338 (list 'org-agenda-compare-effort (list 'quote op)
5339 (org-hh:mm-string-to-minutes e))))
5341 (defun org-agenda-compare-effort (op value)
5342 "Compare the effort of the current line with VALUE, using OP.
5343 If the line does not have an effort defined, return nil."
5344 (let ((eff (org-get-at-bol 'effort-minutes)))
5345 (if (equal op ??)
5346 (not eff)
5347 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
5348 value))))
5350 (defun org-agenda-filter-apply (filter)
5351 "Set FILTER as the new agenda filter and apply it."
5352 (let (tags)
5353 (setq org-agenda-filter filter
5354 org-agenda-filter-form (org-agenda-filter-make-matcher))
5355 (org-agenda-set-mode-name)
5356 (save-excursion
5357 (goto-char (point-min))
5358 (while (not (eobp))
5359 (if (org-get-at-bol 'org-marker)
5360 (progn
5361 (setq tags (org-get-at-bol 'tags)) ; used in eval
5362 (if (not (eval org-agenda-filter-form))
5363 (org-agenda-filter-by-tag-hide-line))
5364 (beginning-of-line 2))
5365 (beginning-of-line 2))))))
5367 (defun org-agenda-filter-by-tag-hide-line ()
5368 (let (ov)
5369 (setq ov (org-make-overlay (max (point-min) (1- (point-at-bol)))
5370 (point-at-eol)))
5371 (org-overlay-put ov 'invisible t)
5372 (org-overlay-put ov 'type 'tags-filter)
5373 (push ov org-agenda-filter-overlays)))
5375 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
5376 (setq pos (or pos (point)))
5377 (save-excursion
5378 (dolist (ov (org-overlays-at pos))
5379 (when (and (org-overlay-get ov 'invisible)
5380 (eq (org-overlay-get ov 'type) 'tags-filter))
5381 (goto-char pos)
5382 (if (< (org-overlay-start ov) (point-at-eol))
5383 (org-move-overlay ov (point-at-eol)
5384 (org-overlay-end ov)))))))
5386 (defun org-agenda-filter-by-tag-show-all ()
5387 (mapc 'org-delete-overlay org-agenda-filter-overlays)
5388 (setq org-agenda-filter-overlays nil)
5389 (setq org-agenda-filter nil)
5390 (setq org-agenda-filter-form nil)
5391 (org-agenda-set-mode-name))
5393 (defun org-agenda-manipulate-query-add ()
5394 "Manipulate the query by adding a search term with positive selection.
5395 Positive selection means, the term must be matched for selection of an entry."
5396 (interactive)
5397 (org-agenda-manipulate-query ?\[))
5398 (defun org-agenda-manipulate-query-subtract ()
5399 "Manipulate the query by adding a search term with negative selection.
5400 Negative selection means, term must not be matched for selection of an entry."
5401 (interactive)
5402 (org-agenda-manipulate-query ?\]))
5403 (defun org-agenda-manipulate-query-add-re ()
5404 "Manipulate the query by adding a search regexp with positive selection.
5405 Positive selection means, the regexp must match for selection of an entry."
5406 (interactive)
5407 (org-agenda-manipulate-query ?\{))
5408 (defun org-agenda-manipulate-query-subtract-re ()
5409 "Manipulate the query by adding a search regexp with negative selection.
5410 Negative selection means, regexp must not match for selection of an entry."
5411 (interactive)
5412 (org-agenda-manipulate-query ?\}))
5413 (defun org-agenda-manipulate-query (char)
5414 (cond
5415 ((memq org-agenda-type '(timeline agenda))
5416 (let ((org-agenda-include-inactive-timestamps t))
5417 (org-agenda-redo))
5418 (message "Display now includes inactive timestamps as well"))
5419 ((eq org-agenda-type 'search)
5420 (org-add-to-string
5421 'org-agenda-query-string
5422 (if org-agenda-last-search-view-search-was-boolean
5423 (cdr (assoc char '((?\[ . " +") (?\] . " -")
5424 (?\{ . " +{}") (?\} . " -{}"))))
5425 " "))
5426 (setq org-agenda-redo-command
5427 (list 'org-search-view
5428 org-todo-only
5429 org-agenda-query-string
5430 (+ (length org-agenda-query-string)
5431 (if (member char '(?\{ ?\})) 0 1))))
5432 (set-register org-agenda-query-register org-agenda-query-string)
5433 (org-agenda-redo))
5434 (t (error "Cannot manipulate query for %s-type agenda buffers"
5435 org-agenda-type))))
5437 (defun org-add-to-string (var string)
5438 (set var (concat (symbol-value var) string)))
5440 (defun org-agenda-goto-date (date)
5441 "Jump to DATE in agenda."
5442 (interactive (list (org-read-date)))
5443 (org-agenda-list nil date))
5445 (defun org-agenda-goto-today ()
5446 "Go to today."
5447 (interactive)
5448 (org-agenda-check-type t 'timeline 'agenda)
5449 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
5450 (cond
5451 (tdpos (goto-char tdpos))
5452 ((eq org-agenda-type 'agenda)
5453 (let* ((sd (time-to-days
5454 (time-subtract (current-time)
5455 (list 0 (* 3600 org-extend-today-until) 0))))
5456 (comp (org-agenda-compute-time-span sd org-agenda-span))
5457 (org-agenda-overriding-arguments org-agenda-last-arguments))
5458 (setf (nth 1 org-agenda-overriding-arguments) (car comp))
5459 (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
5460 (org-agenda-redo)
5461 (org-agenda-find-same-or-today-or-agenda)))
5462 (t (error "Cannot find today")))))
5464 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
5465 (goto-char
5466 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
5467 (text-property-any (point-min) (point-max) 'org-today t)
5468 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
5469 (point-min))))
5471 (defun org-agenda-later (arg)
5472 "Go forward in time by thee current span.
5473 With prefix ARG, go forward that many times the current span."
5474 (interactive "p")
5475 (org-agenda-check-type t 'agenda)
5476 (let* ((span org-agenda-span)
5477 (sd org-starting-day)
5478 (greg (calendar-gregorian-from-absolute sd))
5479 (cnt (org-get-at-bol 'org-day-cnt))
5480 greg2 nd)
5481 (cond
5482 ((eq span 'day)
5483 (setq sd (+ arg sd) nd 1))
5484 ((eq span 'week)
5485 (setq sd (+ (* 7 arg) sd) nd 7))
5486 ((eq span 'month)
5487 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
5488 sd (calendar-absolute-from-gregorian greg2))
5489 (setcar greg2 (1+ (car greg2)))
5490 (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
5491 ((eq span 'year)
5492 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
5493 sd (calendar-absolute-from-gregorian greg2))
5494 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
5495 (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
5496 (let ((org-agenda-overriding-arguments
5497 (list (car org-agenda-last-arguments) sd nd t)))
5498 (org-agenda-redo)
5499 (org-agenda-find-same-or-today-or-agenda cnt))))
5501 (defun org-agenda-earlier (arg)
5502 "Go backward in time by the current span.
5503 With prefix ARG, go backward that many times the current span."
5504 (interactive "p")
5505 (org-agenda-later (- arg)))
5507 (defun org-agenda-view-mode-dispatch ()
5508 "Call one of the view mode commands."
5509 (interactive)
5510 (message "View: [d]ay [w]eek [m]onth [y]ear [l]og [L]og-all [a]rch-trees [A]rch-files
5511 clock[R]eport time[G]rid [[]inactive [E]ntryText include[D]iary")
5512 (let ((a (read-char-exclusive)))
5513 (case a
5514 (?d (call-interactively 'org-agenda-day-view))
5515 (?w (call-interactively 'org-agenda-week-view))
5516 (?m (call-interactively 'org-agenda-month-view))
5517 (?y (call-interactively 'org-agenda-year-view))
5518 (?l (call-interactively 'org-agenda-log-mode))
5519 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
5520 (?a (call-interactively 'org-agenda-archives-mode))
5521 (?A (org-agenda-archives-mode 'files))
5522 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
5523 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
5524 (?G (call-interactively 'org-agenda-toggle-time-grid))
5525 (?D (call-interactively 'org-agenda-toggle-diary))
5526 (?\[ (let ((org-agenda-include-inactive-timestamps t))
5527 (org-agenda-check-type t 'timeline 'agenda)
5528 (org-agenda-redo))
5529 (message "Display now includes inactive timestamps as well"))
5530 (?q (message "Abort"))
5531 (otherwise (error "Invalid key" )))))
5533 (defun org-agenda-day-view (&optional day-of-year)
5534 "Switch to daily view for agenda.
5535 With argument DAY-OF-YEAR, switch to that day of the year."
5536 (interactive "P")
5537 (setq org-agenda-ndays 1)
5538 (org-agenda-change-time-span 'day day-of-year))
5539 (defun org-agenda-week-view (&optional iso-week)
5540 "Switch to daily view for agenda.
5541 With argument ISO-WEEK, switch to the corresponding ISO week.
5542 If ISO-WEEK has more then 2 digits, only the last two encode the
5543 week. Any digits before this encode a year. So 200712 means
5544 week 12 of year 2007. Years in the range 1938-2037 can also be
5545 written as 2-digit years."
5546 (interactive "P")
5547 (setq org-agenda-ndays 7)
5548 (org-agenda-change-time-span 'week iso-week))
5549 (defun org-agenda-month-view (&optional month)
5550 "Switch to monthly view for agenda.
5551 With argument MONTH, switch to that month."
5552 (interactive "P")
5553 (org-agenda-change-time-span 'month month))
5554 (defun org-agenda-year-view (&optional year)
5555 "Switch to yearly view for agenda.
5556 With argument YEAR, switch to that year.
5557 If MONTH has more then 2 digits, only the last two encode the
5558 month. Any digits before this encode a year. So 200712 means
5559 December year 2007. Years in the range 1938-2037 can also be
5560 written as 2-digit years."
5561 (interactive "P")
5562 (when year
5563 (setq year (org-small-year-to-year year)))
5564 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
5565 (org-agenda-change-time-span 'year year)
5566 (error "Abort")))
5568 (defun org-agenda-change-time-span (span &optional n)
5569 "Change the agenda view to SPAN.
5570 SPAN may be `day', `week', `month', `year'."
5571 (org-agenda-check-type t 'agenda)
5572 (if (and (not n) (equal org-agenda-span span))
5573 (error "Viewing span is already \"%s\"" span))
5574 (let* ((sd (or (org-get-at-bol 'day)
5575 org-starting-day))
5576 (computed (org-agenda-compute-time-span sd span n))
5577 (org-agenda-overriding-arguments
5578 (list (car org-agenda-last-arguments)
5579 (car computed) (cdr computed) t)))
5580 (org-agenda-redo)
5581 (org-agenda-find-same-or-today-or-agenda))
5582 (org-agenda-set-mode-name)
5583 (message "Switched to %s view" span))
5585 (defun org-agenda-compute-time-span (sd span &optional n)
5586 "Compute starting date and number of days for agenda.
5587 SPAN may be `day', `week', `month', `year'. The return value
5588 is a cons cell with the starting date and the number of days,
5589 so that the date SD will be in that range."
5590 (let* ((greg (calendar-gregorian-from-absolute sd))
5591 (dg (nth 1 greg))
5592 (mg (car greg))
5593 (yg (nth 2 greg))
5594 nd w1 y1 m1 thisweek)
5595 (cond
5596 ((eq span 'day)
5597 (when n
5598 (setq sd (+ (calendar-absolute-from-gregorian
5599 (list mg 1 yg))
5600 n -1)))
5601 (setq nd 1))
5602 ((eq span 'week)
5603 (let* ((nt (calendar-day-of-week
5604 (calendar-gregorian-from-absolute sd)))
5605 (d (if org-agenda-start-on-weekday
5606 (- nt org-agenda-start-on-weekday)
5607 0)))
5608 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
5609 (when n
5610 (require 'cal-iso)
5611 (setq thisweek (car (calendar-iso-from-absolute sd)))
5612 (when (> n 99)
5613 (setq y1 (org-small-year-to-year (/ n 100))
5614 n (mod n 100)))
5615 (setq sd
5616 (calendar-absolute-from-iso
5617 (list n 1
5618 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))
5619 (setq nd 7)))
5620 ((eq span 'month)
5621 (when (and n (> n 99))
5622 (setq y1 (org-small-year-to-year (/ n 100))
5623 n (mod n 100)))
5624 (setq sd (calendar-absolute-from-gregorian
5625 (list (or n mg) 1 (or y1 yg)))
5626 nd (- (calendar-absolute-from-gregorian
5627 (list (1+ (or n mg)) 1 (or y1 yg)))
5628 sd)))
5629 ((eq span 'year)
5630 (setq sd (calendar-absolute-from-gregorian
5631 (list 1 1 (or n yg)))
5632 nd (- (calendar-absolute-from-gregorian
5633 (list 1 1 (1+ (or n yg))))
5634 sd))))
5635 (cons sd nd)))
5637 (defun org-agenda-next-date-line (&optional arg)
5638 "Jump to the next line indicating a date in agenda buffer."
5639 (interactive "p")
5640 (org-agenda-check-type t 'agenda 'timeline)
5641 (beginning-of-line 1)
5642 ;; This does not work if user makes date format that starts with a blank
5643 (if (looking-at "^\\S-") (forward-char 1))
5644 (if (not (re-search-forward "^\\S-" nil t arg))
5645 (progn
5646 (backward-char 1)
5647 (error "No next date after this line in this buffer")))
5648 (goto-char (match-beginning 0)))
5650 (defun org-agenda-previous-date-line (&optional arg)
5651 "Jump to the previous line indicating a date in agenda buffer."
5652 (interactive "p")
5653 (org-agenda-check-type t 'agenda 'timeline)
5654 (beginning-of-line 1)
5655 (if (not (re-search-backward "^\\S-" nil t arg))
5656 (error "No previous date before this line in this buffer")))
5658 ;; Initialize the highlight
5659 (defvar org-hl (org-make-overlay 1 1))
5660 (org-overlay-put org-hl 'face 'highlight)
5662 (defun org-highlight (begin end &optional buffer)
5663 "Highlight a region with overlay."
5664 (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
5665 org-hl begin end (or buffer (current-buffer))))
5667 (defun org-unhighlight ()
5668 "Detach overlay INDEX."
5669 (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
5671 ;; FIXME this is currently not used.
5672 (defun org-highlight-until-next-command (beg end &optional buffer)
5673 "Move the highlight overlay to BEG/END, remove it before the next command."
5674 (org-highlight beg end buffer)
5675 (add-hook 'pre-command-hook 'org-unhighlight-once))
5676 (defun org-unhighlight-once ()
5677 "Remove the highlight from its position, and this function from the hook."
5678 (remove-hook 'pre-command-hook 'org-unhighlight-once)
5679 (org-unhighlight))
5681 (defun org-agenda-follow-mode ()
5682 "Toggle follow mode in an agenda buffer."
5683 (interactive)
5684 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
5685 (org-agenda-set-mode-name)
5686 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
5687 (org-agenda-show))
5688 (message "Follow mode is %s"
5689 (if org-agenda-follow-mode "on" "off")))
5691 (defun org-agenda-entry-text-mode (&optional arg)
5692 "Toggle entry text mode in an agenda buffer."
5693 (interactive "P")
5694 (if (integerp arg)
5695 (setq org-agenda-entry-text-mode t)
5696 (setq org-agenda-entry-text-mode (not org-agenda-entry-text-mode)))
5697 (org-agenda-entry-text-hide)
5698 (and org-agenda-entry-text-mode
5699 (let ((org-agenda-entry-text-maxlines
5700 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
5701 (org-agenda-entry-text-show)))
5702 (org-agenda-set-mode-name)
5703 (message "Entry text mode is %s. Maximum number of lines is %d"
5704 (if org-agenda-entry-text-mode "on" "off")
5705 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
5707 (defun org-agenda-clockreport-mode ()
5708 "Toggle clocktable mode in an agenda buffer."
5709 (interactive)
5710 (org-agenda-check-type t 'agenda)
5711 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
5712 (org-agenda-set-mode-name)
5713 (org-agenda-redo)
5714 (message "Clocktable mode is %s"
5715 (if org-agenda-clockreport-mode "on" "off")))
5717 (defun org-agenda-log-mode (&optional special)
5718 "Toggle log mode in an agenda buffer.
5719 With argument SPECIAL, show all possible log items, not only the ones
5720 configured in `org-agenda-log-mode-items'.
5721 With a double `C-u' prefix arg, show *only* log items, nothing else."
5722 (interactive "P")
5723 (org-agenda-check-type t 'agenda 'timeline)
5724 (setq org-agenda-show-log
5725 (if (equal special '(16))
5726 'only
5727 (if special '(closed clock state)
5728 (not org-agenda-show-log))))
5729 (org-agenda-set-mode-name)
5730 (org-agenda-redo)
5731 (message "Log mode is %s"
5732 (if org-agenda-show-log "on" "off")))
5734 (defun org-agenda-archives-mode (&optional with-files)
5735 "Toggle inclusion of items in trees marked with :ARCHIVE:.
5736 When called with a prefix argument, include all archive files as well."
5737 (interactive "P")
5738 (setq org-agenda-archives-mode
5739 (if with-files t (if org-agenda-archives-mode nil 'trees)))
5740 (org-agenda-set-mode-name)
5741 (org-agenda-redo)
5742 (message
5743 "%s"
5744 (cond
5745 ((eq org-agenda-archives-mode nil)
5746 "No archives are included")
5747 ((eq org-agenda-archives-mode 'trees)
5748 (format "Trees with :%s: tag are included" org-archive-tag))
5749 ((eq org-agenda-archives-mode t)
5750 (format "Trees with :%s: tag and all active archive files are included"
5751 org-archive-tag)))))
5753 (defun org-agenda-toggle-diary ()
5754 "Toggle diary inclusion in an agenda buffer."
5755 (interactive)
5756 (org-agenda-check-type t 'agenda)
5757 (setq org-agenda-include-diary (not org-agenda-include-diary))
5758 (org-agenda-redo)
5759 (org-agenda-set-mode-name)
5760 (message "Diary inclusion turned %s"
5761 (if org-agenda-include-diary "on" "off")))
5763 (defun org-agenda-toggle-time-grid ()
5764 "Toggle time grid in an agenda buffer."
5765 (interactive)
5766 (org-agenda-check-type t 'agenda)
5767 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
5768 (org-agenda-redo)
5769 (org-agenda-set-mode-name)
5770 (message "Time-grid turned %s"
5771 (if org-agenda-use-time-grid "on" "off")))
5773 (defun org-agenda-set-mode-name ()
5774 "Set the mode name to indicate all the small mode settings."
5775 (setq mode-name
5776 (concat "Org-Agenda"
5777 (if (equal org-agenda-ndays 1) " Day" "")
5778 (if (equal org-agenda-ndays 7) " Week" "")
5779 (if org-agenda-follow-mode " Follow" "")
5780 (if org-agenda-entry-text-mode " ETxt" "")
5781 (if org-agenda-include-diary " Diary" "")
5782 (if org-agenda-use-time-grid " Grid" "")
5783 (if (and (boundp 'org-habit-show-habits)
5784 org-habit-show-habits) " Habit" "")
5785 (if (consp org-agenda-show-log) " LogAll"
5786 (if org-agenda-show-log " Log" ""))
5787 (if (or org-agenda-filter (get 'org-agenda-filter
5788 :preset-filter))
5789 (concat " {" (mapconcat
5790 'identity
5791 (append (get 'org-agenda-filter
5792 :preset-filter)
5793 org-agenda-filter) "") "}")
5795 (if org-agenda-archives-mode
5796 (if (eq org-agenda-archives-mode t)
5797 " Archives"
5798 (format " :%s:" org-archive-tag))
5800 (if org-agenda-clockreport-mode " Clock" "")))
5801 (force-mode-line-update))
5803 (defun org-agenda-post-command-hook ()
5804 (setq org-agenda-type
5805 (or (get-text-property (point) 'org-agenda-type)
5806 (get-text-property (max (point-min) (1- (point)))
5807 'org-agenda-type))))
5809 (defun org-agenda-next-line ()
5810 "Move cursor to the next line, and show if follow-mode is active."
5811 (interactive)
5812 (call-interactively 'next-line)
5813 (org-agenda-do-context-action))
5815 (defun org-agenda-previous-line ()
5816 "Move cursor to the previous line, and show if follow-mode is active."
5818 (interactive)
5819 (call-interactively 'previous-line)
5820 (org-agenda-do-context-action))
5822 (defun org-agenda-do-context-action ()
5823 "Show outline path and, maybe, follow-mode window."
5824 (let ((m (org-get-at-bol 'org-marker)))
5825 (if (and org-agenda-follow-mode m)
5826 (org-agenda-show))
5827 (if (and m org-agenda-show-outline-path)
5828 (org-with-point-at m
5829 (org-display-outline-path t)))))
5831 (defun org-agenda-show-priority ()
5832 "Show the priority of the current item.
5833 This priority is composed of the main priority given with the [#A] cookies,
5834 and by additional input from the age of a schedules or deadline entry."
5835 (interactive)
5836 (let* ((pri (org-get-at-bol 'priority)))
5837 (message "Priority is %d" (if pri pri -1000))))
5839 (defun org-agenda-show-tags ()
5840 "Show the tags applicable to the current item."
5841 (interactive)
5842 (let* ((tags (org-get-at-bol 'tags)))
5843 (if tags
5844 (message "Tags are :%s:"
5845 (org-no-properties (mapconcat 'identity tags ":")))
5846 (message "No tags associated with this line"))))
5848 (defun org-agenda-goto (&optional highlight)
5849 "Go to the Org-mode file which contains the item at point."
5850 (interactive)
5851 (let* ((marker (or (org-get-at-bol 'org-marker)
5852 (org-agenda-error)))
5853 (buffer (marker-buffer marker))
5854 (pos (marker-position marker)))
5855 (switch-to-buffer-other-window buffer)
5856 (widen)
5857 (goto-char pos)
5858 (when (org-mode-p)
5859 (org-show-context 'agenda)
5860 (save-excursion
5861 (and (outline-next-heading)
5862 (org-flag-heading nil)))) ; show the next heading
5863 (recenter (/ (window-height) 2))
5864 (run-hooks 'org-agenda-after-show-hook)
5865 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
5867 (defvar org-agenda-after-show-hook nil
5868 "Normal hook run after an item has been shown from the agenda.
5869 Point is in the buffer where the item originated.")
5871 (defun org-agenda-kill ()
5872 "Kill the entry or subtree belonging to the current agenda entry."
5873 (interactive)
5874 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5875 (let* ((marker (or (org-get-at-bol 'org-marker)
5876 (org-agenda-error)))
5877 (buffer (marker-buffer marker))
5878 (pos (marker-position marker))
5879 (type (org-get-at-bol 'type))
5880 dbeg dend (n 0) conf)
5881 (org-with-remote-undo buffer
5882 (with-current-buffer buffer
5883 (save-excursion
5884 (goto-char pos)
5885 (if (and (org-mode-p) (not (member type '("sexp"))))
5886 (setq dbeg (progn (org-back-to-heading t) (point))
5887 dend (org-end-of-subtree t t))
5888 (setq dbeg (point-at-bol)
5889 dend (min (point-max) (1+ (point-at-eol)))))
5890 (goto-char dbeg)
5891 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
5892 (setq conf (or (eq t org-agenda-confirm-kill)
5893 (and (numberp org-agenda-confirm-kill)
5894 (> n org-agenda-confirm-kill))))
5895 (and conf
5896 (not (y-or-n-p
5897 (format "Delete entry with %d lines in buffer \"%s\"? "
5898 n (buffer-name buffer))))
5899 (error "Abort"))
5900 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
5901 (with-current-buffer buffer (delete-region dbeg dend))
5902 (message "Agenda item and source killed"))))
5904 (defvar org-archive-default-command)
5905 (defun org-agenda-archive-default ()
5906 "Archive the entry or subtree belonging to the current agenda entry."
5907 (interactive)
5908 (require 'org-archive)
5909 (org-agenda-archive-with org-archive-default-command))
5911 (defun org-agenda-archive-default-with-confirmation ()
5912 "Archive the entry or subtree belonging to the current agenda entry."
5913 (interactive)
5914 (require 'org-archive)
5915 (org-agenda-archive-with org-archive-default-command 'confirm))
5917 (defun org-agenda-archive ()
5918 "Archive the entry or subtree belonging to the current agenda entry."
5919 (interactive)
5920 (org-agenda-archive-with 'org-archive-subtree))
5922 (defun org-agenda-archive-to-archive-sibling ()
5923 "Move the entry to the archive sibling."
5924 (interactive)
5925 (org-agenda-archive-with 'org-archive-to-archive-sibling))
5927 (defun org-agenda-archive-with (cmd &optional confirm)
5928 "Move the entry to the archive sibling."
5929 (interactive)
5930 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5931 (let* ((marker (or (org-get-at-bol 'org-marker)
5932 (org-agenda-error)))
5933 (buffer (marker-buffer marker))
5934 (pos (marker-position marker)))
5935 (org-with-remote-undo buffer
5936 (with-current-buffer buffer
5937 (if (org-mode-p)
5938 (if (and confirm
5939 (not (y-or-n-p "Archive this subtree or entry? ")))
5940 (error "Abort")
5941 (save-excursion
5942 (goto-char pos)
5943 (org-remove-subtree-entries-from-agenda)
5944 (org-back-to-heading t)
5945 (funcall cmd)))
5946 (error "Archiving works only in Org-mode files"))))))
5948 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
5949 "Remove all lines in the agenda that correspond to a given subtree.
5950 The subtree is the one in buffer BUF, starting at BEG and ending at END.
5951 If this information is not given, the function uses the tree at point."
5952 (let ((buf (or buf (current-buffer))) m p)
5953 (save-excursion
5954 (unless (and beg end)
5955 (org-back-to-heading t)
5956 (setq beg (point))
5957 (org-end-of-subtree t)
5958 (setq end (point)))
5959 (set-buffer (get-buffer org-agenda-buffer-name))
5960 (save-excursion
5961 (goto-char (point-max))
5962 (beginning-of-line 1)
5963 (while (not (bobp))
5964 (when (and (setq m (org-get-at-bol 'org-marker))
5965 (equal buf (marker-buffer m))
5966 (setq p (marker-position m))
5967 (>= p beg)
5968 (< p end))
5969 (let ((inhibit-read-only t))
5970 (delete-region (point-at-bol) (1+ (point-at-eol)))))
5971 (beginning-of-line 0))))))
5973 (defun org-agenda-refile (&optional goto rfloc)
5974 "Refile the item at point."
5975 (interactive "P")
5976 (if (equal goto '(16))
5977 (org-refile-goto-last-stored)
5978 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
5979 (org-agenda-error)))
5980 (buffer (marker-buffer marker))
5981 (pos (marker-position marker))
5982 (rfloc (or rfloc
5983 (org-refile-get-location
5984 (if goto "Goto: " "Refile to: ") buffer
5985 org-refile-allow-creating-parent-nodes))))
5986 (with-current-buffer buffer
5987 (save-excursion
5988 (save-restriction
5989 (widen)
5990 (goto-char marker)
5991 (org-remove-subtree-entries-from-agenda)
5992 (org-refile goto buffer rfloc)))))))
5994 (defun org-agenda-open-link (&optional arg)
5995 "Follow the link in the current line, if any.
5996 This looks for a link in the displayed line in the agenda. It also looks
5997 at the text of the entry itself."
5998 (interactive "P")
5999 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6000 (org-get-at-bol 'org-marker)))
6001 (buffer (and marker (marker-buffer marker)))
6002 (prefix (buffer-substring
6003 (point-at-bol)
6004 (+ (point-at-bol)
6005 (or (org-get-at-bol 'prefix-length) 0)))))
6006 (cond
6007 (buffer
6008 (with-current-buffer buffer
6009 (save-excursion
6010 (save-restriction
6011 (widen)
6012 (goto-char marker)
6013 (org-offer-links-in-entry arg prefix)))))
6014 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6015 (save-excursion
6016 (beginning-of-line 1)
6017 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6018 (org-open-link-from-string (match-string 1)))
6019 (t (error "No link to open here")))))
6021 (defun org-agenda-copy-local-variable (var)
6022 "Get a variable from a referenced buffer and install it here."
6023 (let ((m (org-get-at-bol 'org-marker)))
6024 (when (and m (buffer-live-p (marker-buffer m)))
6025 (org-set-local var (with-current-buffer (marker-buffer m)
6026 (symbol-value var))))))
6028 (defun org-agenda-switch-to (&optional delete-other-windows)
6029 "Go to the Org-mode file which contains the item at point."
6030 (interactive)
6031 (if (and org-return-follows-link
6032 (not (org-get-at-bol 'org-marker))
6033 (org-in-regexp org-bracket-link-regexp))
6034 (org-open-link-from-string (match-string 0))
6035 (let* ((marker (or (org-get-at-bol 'org-marker)
6036 (org-agenda-error)))
6037 (buffer (marker-buffer marker))
6038 (pos (marker-position marker)))
6039 (switch-to-buffer buffer)
6040 (and delete-other-windows (delete-other-windows))
6041 (widen)
6042 (goto-char pos)
6043 (when (org-mode-p)
6044 (org-show-context 'agenda)
6045 (save-excursion
6046 (and (outline-next-heading)
6047 (org-flag-heading nil))))))) ; show the next heading
6049 (defun org-agenda-goto-mouse (ev)
6050 "Go to the Org-mode file which contains the item at the mouse click."
6051 (interactive "e")
6052 (mouse-set-point ev)
6053 (org-agenda-goto))
6055 (defun org-agenda-show (&optional full-entry)
6056 "Display the Org-mode file which contains the item at point.
6057 With prefix argument FULL-ENTRY, make the entire entry visible
6058 if it was hidden in the outline."
6059 (interactive "P")
6060 (let ((win (selected-window)))
6061 (if full-entry
6062 (let ((org-show-entry-below t))
6063 (org-agenda-goto t))
6064 (org-agenda-goto t))
6065 (select-window win)))
6067 (defvar org-agenda-show-window nil)
6068 (defun org-agenda-show-and-scroll-up ()
6069 "Display the Org-mode file which contains the item at point.
6070 When called repeatedly, scroll the window that is displaying the buffer."
6071 (interactive)
6072 (let ((win (selected-window)))
6073 (if (and (window-live-p org-agenda-show-window)
6074 (eq this-command last-command))
6075 (progn
6076 (select-window org-agenda-show-window)
6077 (ignore-errors (scroll-up)))
6078 (org-agenda-goto t)
6079 (show-subtree)
6080 (setq org-agenda-show-window (selected-window)))
6081 (select-window win)))
6083 (defun org-agenda-show-scroll-down ()
6084 "Scroll down the window showing the agenda."
6085 (interactive)
6086 (let ((win (selected-window)))
6087 (when (window-live-p org-agenda-show-window)
6088 (select-window org-agenda-show-window)
6089 (ignore-errors (scroll-down))
6090 (select-window win))))
6092 (defun org-agenda-show-1 (&optional more)
6093 "Display the Org-mode file which contains the item at point.
6094 The prefix arg selects the amount of information to display:
6096 0 hide the subtree
6097 1 just show the entry according to defaults.
6098 2 show the children view
6099 3 show the subtree view
6100 4 show the entire subtree and any LOGBOOK drawers
6101 5 show the entire subtree and any drawers
6102 With prefix argument FULL-ENTRY, make the entire entry visible
6103 if it was hidden in the outline."
6104 (interactive "p")
6105 (let ((win (selected-window)))
6106 (org-agenda-goto t)
6107 (org-recenter-heading 1)
6108 (cond
6109 ((= more 0)
6110 (hide-subtree)
6111 (save-excursion
6112 (org-back-to-heading)
6113 (run-hook-with-args 'org-cycle-hook 'folded))
6114 (message "Remote: FOLDED"))
6115 ((and (interactive-p) (= more 1))
6116 (message "Remote: show with default settings"))
6117 ((= more 2)
6118 (show-entry)
6119 (show-children)
6120 (save-excursion
6121 (org-back-to-heading)
6122 (run-hook-with-args 'org-cycle-hook 'children))
6123 (message "Remote: CHILDREN"))
6124 ((= more 3)
6125 (show-subtree)
6126 (save-excursion
6127 (org-back-to-heading)
6128 (run-hook-with-args 'org-cycle-hook 'subtree))
6129 (message "Remote: SUBTREE"))
6130 ((= more 4)
6131 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
6132 (org-drawer-regexp
6133 (concat "^[ \t]*:\\("
6134 (mapconcat 'regexp-quote org-drawers "\\|")
6135 "\\):[ \t]*$")))
6136 (show-subtree)
6137 (save-excursion
6138 (org-back-to-heading)
6139 (org-cycle-hide-drawers 'subtree)))
6140 (message "Remote: SUBTREE AND LOGBOOK"))
6141 ((> more 4)
6142 (show-subtree)
6143 (message "Remote: SUBTREE AND ALL DRAWERS")))
6144 (select-window win)))
6146 (defun org-recenter-heading (n)
6147 (save-excursion
6148 (org-back-to-heading)
6149 (recenter n)))
6151 (defvar org-agenda-cycle-counter nil)
6152 (defun org-agenda-cycle-show (&optional n)
6153 "Show the current entry in another window, with default settings.
6154 Default settings are taken from `org-show-hierarchy-above' and siblings.
6155 When use repeatedly in immediate succession, the remote entry will cycle
6156 through visibility
6158 children -> subtree -> folded
6160 When called with a numeric prefix arg, that arg will be passed through to
6161 `org-agenda-show-1'. For the interpretation of that argument, see the
6162 docstring of `org-agenda-show-1'."
6163 (interactive "P")
6164 (if (integerp n)
6165 (setq org-agenda-cycle-counter n)
6166 (if (not (eq last-command this-command))
6167 (setq org-agenda-cycle-counter 1)
6168 (if (equal org-agenda-cycle-counter 0)
6169 (setq org-agenda-cycle-counter 2)
6170 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
6171 (if (> org-agenda-cycle-counter 3)
6172 (setq org-agenda-cycle-counter 0)))))
6173 (org-agenda-show-1 org-agenda-cycle-counter))
6175 (defun org-agenda-recenter (arg)
6176 "Display the Org-mode file which contains the item at point and recenter."
6177 (interactive "P")
6178 (let ((win (selected-window)))
6179 (org-agenda-goto t)
6180 (recenter arg)
6181 (select-window win)))
6183 (defun org-agenda-show-mouse (ev)
6184 "Display the Org-mode file which contains the item at the mouse click."
6185 (interactive "e")
6186 (mouse-set-point ev)
6187 (org-agenda-show))
6189 (defun org-agenda-check-no-diary ()
6190 "Check if the entry is a diary link and abort if yes."
6191 (if (org-get-at-bol 'org-agenda-diary-link)
6192 (org-agenda-error)))
6194 (defun org-agenda-error ()
6195 (error "Command not allowed in this line"))
6197 (defun org-agenda-tree-to-indirect-buffer ()
6198 "Show the subtree corresponding to the current entry in an indirect buffer.
6199 This calls the command `org-tree-to-indirect-buffer' from the original
6200 Org-mode buffer.
6201 With numerical prefix arg ARG, go up to this level and then take that tree.
6202 With a C-u prefix, make a separate frame for this tree (i.e. don't use the
6203 dedicated frame)."
6204 (interactive)
6205 (org-agenda-check-no-diary)
6206 (let* ((marker (or (org-get-at-bol 'org-marker)
6207 (org-agenda-error)))
6208 (buffer (marker-buffer marker))
6209 (pos (marker-position marker)))
6210 (with-current-buffer buffer
6211 (save-excursion
6212 (goto-char pos)
6213 (call-interactively 'org-tree-to-indirect-buffer)))))
6215 (defvar org-last-heading-marker (make-marker)
6216 "Marker pointing to the headline that last changed its TODO state
6217 by a remote command from the agenda.")
6219 (defun org-agenda-todo-nextset ()
6220 "Switch TODO entry to next sequence."
6221 (interactive)
6222 (org-agenda-todo 'nextset))
6224 (defun org-agenda-todo-previousset ()
6225 "Switch TODO entry to previous sequence."
6226 (interactive)
6227 (org-agenda-todo 'previousset))
6229 (defun org-agenda-todo (&optional arg)
6230 "Cycle TODO state of line at point, also in Org-mode file.
6231 This changes the line at point, all other lines in the agenda referring to
6232 the same tree node, and the headline of the tree node in the Org-mode file."
6233 (interactive "P")
6234 (org-agenda-check-no-diary)
6235 (let* ((col (current-column))
6236 (marker (or (org-get-at-bol 'org-marker)
6237 (org-agenda-error)))
6238 (buffer (marker-buffer marker))
6239 (pos (marker-position marker))
6240 (hdmarker (org-get-at-bol 'org-hd-marker))
6241 (todayp (equal (org-get-at-bol 'day)
6242 (time-to-days (current-time))))
6243 (inhibit-read-only t)
6244 org-agenda-headline-snapshot-before-repeat newhead just-one)
6245 (org-with-remote-undo buffer
6246 (with-current-buffer buffer
6247 (widen)
6248 (goto-char pos)
6249 (org-show-context 'agenda)
6250 (save-excursion
6251 (and (outline-next-heading)
6252 (org-flag-heading nil))) ; show the next heading
6253 (let ((current-prefix-arg arg))
6254 (call-interactively 'org-todo))
6255 (and (bolp) (forward-char 1))
6256 (setq newhead (org-get-heading))
6257 (when (and (org-bound-and-true-p
6258 org-agenda-headline-snapshot-before-repeat)
6259 (not (equal org-agenda-headline-snapshot-before-repeat
6260 newhead))
6261 todayp)
6262 (setq newhead org-agenda-headline-snapshot-before-repeat
6263 just-one t))
6264 (save-excursion
6265 (org-back-to-heading)
6266 (move-marker org-last-heading-marker (point))))
6267 (beginning-of-line 1)
6268 (save-excursion
6269 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
6270 (org-move-to-column col))))
6272 (defun org-agenda-add-note (&optional arg)
6273 "Add a time-stamped note to the entry at point."
6274 (interactive "P")
6275 (org-agenda-check-no-diary)
6276 (let* ((marker (or (org-get-at-bol 'org-marker)
6277 (org-agenda-error)))
6278 (buffer (marker-buffer marker))
6279 (pos (marker-position marker))
6280 (hdmarker (org-get-at-bol 'org-hd-marker))
6281 (inhibit-read-only t))
6282 (with-current-buffer buffer
6283 (widen)
6284 (goto-char pos)
6285 (org-show-context 'agenda)
6286 (save-excursion
6287 (and (outline-next-heading)
6288 (org-flag-heading nil))) ; show the next heading
6289 (org-add-note))))
6291 (defun org-agenda-change-all-lines (newhead hdmarker
6292 &optional fixface just-this)
6293 "Change all lines in the agenda buffer which match HDMARKER.
6294 The new content of the line will be NEWHEAD (as modified by
6295 `org-format-agenda-item'). HDMARKER is checked with
6296 `equal' against all `org-hd-marker' text properties in the file.
6297 If FIXFACE is non-nil, the face of each item is modified according to
6298 the new TODO state.
6299 If JUST-THIS is non-nil, change just the current line, not all.
6300 If FORCE-TAGS is non nil, the car of it returns the new tags."
6301 (let* ((inhibit-read-only t)
6302 (line (org-current-line))
6303 (thetags (with-current-buffer (marker-buffer hdmarker)
6304 (save-excursion (save-restriction (widen)
6305 (goto-char hdmarker)
6306 (org-get-tags-at)))))
6307 props m pl undone-face done-face finish new dotime cat tags)
6308 (save-excursion
6309 (goto-char (point-max))
6310 (beginning-of-line 1)
6311 (while (not finish)
6312 (setq finish (bobp))
6313 (when (and (setq m (org-get-at-bol 'org-hd-marker))
6314 (or (not just-this) (= (org-current-line) line))
6315 (equal m hdmarker))
6316 (setq props (text-properties-at (point))
6317 dotime (org-get-at-bol 'dotime)
6318 cat (org-get-at-bol 'org-category)
6319 tags thetags
6320 new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
6321 pl (org-get-at-bol 'prefix-length)
6322 undone-face (org-get-at-bol 'undone-face)
6323 done-face (org-get-at-bol 'done-face))
6324 (goto-char (+ (point) pl))
6325 ;; (org-move-to-column pl) FIXME: does the above line work correctly?
6326 (cond
6327 ((equal new "")
6328 (beginning-of-line 1)
6329 (and (looking-at ".*\n?") (replace-match "")))
6330 ((looking-at ".*")
6331 (replace-match new t t)
6332 (beginning-of-line 1)
6333 (add-text-properties (point-at-bol) (point-at-eol) props)
6334 (when fixface
6335 (add-text-properties
6336 (point-at-bol) (point-at-eol)
6337 (list 'face
6338 (if org-last-todo-state-is-todo
6339 undone-face done-face))))
6340 (org-agenda-highlight-todo 'line)
6341 (beginning-of-line 1))
6342 (t (error "Line update did not work"))))
6343 (beginning-of-line 0)))
6344 (org-finalize-agenda)))
6346 (defun org-agenda-align-tags (&optional line)
6347 "Align all tags in agenda items to `org-agenda-tags-column'."
6348 (let ((inhibit-read-only t) l c)
6349 (save-excursion
6350 (goto-char (if line (point-at-bol) (point-min)))
6351 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
6352 (if line (point-at-eol) nil) t)
6353 (add-text-properties
6354 (match-beginning 2) (match-end 2)
6355 (list 'face (delq nil (let ((prop (get-text-property
6356 (match-beginning 2) 'face)))
6357 (or (listp prop) (setq prop (list prop)))
6358 (if (memq 'org-tag prop)
6359 prop
6360 (cons 'org-tag prop))))))
6361 (setq l (- (match-end 2) (match-beginning 2))
6362 c (if (< org-agenda-tags-column 0)
6363 (- (abs org-agenda-tags-column) l)
6364 org-agenda-tags-column))
6365 (delete-region (match-beginning 1) (match-end 1))
6366 (goto-char (match-beginning 1))
6367 (insert (org-add-props
6368 (make-string (max 1 (- c (current-column))) ?\ )
6369 (text-properties-at (point)))))
6370 (goto-char (point-min))
6371 (org-font-lock-add-tag-faces (point-max)))))
6373 (defun org-agenda-priority-up ()
6374 "Increase the priority of line at point, also in Org-mode file."
6375 (interactive)
6376 (org-agenda-priority 'up))
6378 (defun org-agenda-priority-down ()
6379 "Decrease the priority of line at point, also in Org-mode file."
6380 (interactive)
6381 (org-agenda-priority 'down))
6383 (defun org-agenda-priority (&optional force-direction)
6384 "Set the priority of line at point, also in Org-mode file.
6385 This changes the line at point, all other lines in the agenda referring to
6386 the same tree node, and the headline of the tree node in the Org-mode file."
6387 (interactive)
6388 (unless org-enable-priority-commands
6389 (error "Priority commands are disabled"))
6390 (org-agenda-check-no-diary)
6391 (let* ((marker (or (org-get-at-bol 'org-marker)
6392 (org-agenda-error)))
6393 (hdmarker (org-get-at-bol 'org-hd-marker))
6394 (buffer (marker-buffer hdmarker))
6395 (pos (marker-position hdmarker))
6396 (inhibit-read-only t)
6397 newhead)
6398 (org-with-remote-undo buffer
6399 (with-current-buffer buffer
6400 (widen)
6401 (goto-char pos)
6402 (org-show-context 'agenda)
6403 (save-excursion
6404 (and (outline-next-heading)
6405 (org-flag-heading nil))) ; show the next heading
6406 (funcall 'org-priority force-direction)
6407 (end-of-line 1)
6408 (setq newhead (org-get-heading)))
6409 (org-agenda-change-all-lines newhead hdmarker)
6410 (beginning-of-line 1))))
6412 ;; FIXME: should fix the tags property of the agenda line.
6413 (defun org-agenda-set-tags (&optional tag onoff)
6414 "Set tags for the current headline."
6415 (interactive)
6416 (org-agenda-check-no-diary)
6417 (if (and (org-region-active-p) (interactive-p))
6418 (call-interactively 'org-change-tag-in-region)
6419 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6420 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6421 (org-agenda-error)))
6422 (buffer (marker-buffer hdmarker))
6423 (pos (marker-position hdmarker))
6424 (inhibit-read-only t)
6425 newhead)
6426 (org-with-remote-undo buffer
6427 (with-current-buffer buffer
6428 (widen)
6429 (goto-char pos)
6430 (save-excursion
6431 (org-show-context 'agenda))
6432 (save-excursion
6433 (and (outline-next-heading)
6434 (org-flag-heading nil))) ; show the next heading
6435 (goto-char pos)
6436 (if tag
6437 (org-toggle-tag tag onoff)
6438 (call-interactively 'org-set-tags))
6439 (end-of-line 1)
6440 (setq newhead (org-get-heading)))
6441 (org-agenda-change-all-lines newhead hdmarker)
6442 (beginning-of-line 1)))))
6444 (defun org-agenda-set-property ()
6445 "Set a property for the current headline."
6446 (interactive)
6447 (org-agenda-check-no-diary)
6448 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6449 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6450 (org-agenda-error)))
6451 (buffer (marker-buffer hdmarker))
6452 (pos (marker-position hdmarker))
6453 (inhibit-read-only t)
6454 newhead)
6455 (org-with-remote-undo buffer
6456 (with-current-buffer buffer
6457 (widen)
6458 (goto-char pos)
6459 (save-excursion
6460 (org-show-context 'agenda))
6461 (save-excursion
6462 (and (outline-next-heading)
6463 (org-flag-heading nil))) ; show the next heading
6464 (goto-char pos)
6465 (call-interactively 'org-set-property)))))
6467 (defun org-agenda-set-effort ()
6468 "Set the effort property for the current headline."
6469 (interactive)
6470 (org-agenda-check-no-diary)
6471 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6472 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6473 (org-agenda-error)))
6474 (buffer (marker-buffer hdmarker))
6475 (pos (marker-position hdmarker))
6476 (inhibit-read-only t)
6477 newhead)
6478 (org-with-remote-undo buffer
6479 (with-current-buffer buffer
6480 (widen)
6481 (goto-char pos)
6482 (save-excursion
6483 (org-show-context 'agenda))
6484 (save-excursion
6485 (and (outline-next-heading)
6486 (org-flag-heading nil))) ; show the next heading
6487 (goto-char pos)
6488 (call-interactively 'org-set-effort)
6489 (end-of-line 1)))))
6491 (defun org-agenda-toggle-archive-tag ()
6492 "Toggle the archive tag for the current entry."
6493 (interactive)
6494 (org-agenda-check-no-diary)
6495 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6496 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6497 (org-agenda-error)))
6498 (buffer (marker-buffer hdmarker))
6499 (pos (marker-position hdmarker))
6500 (inhibit-read-only t)
6501 newhead)
6502 (org-with-remote-undo buffer
6503 (with-current-buffer buffer
6504 (widen)
6505 (goto-char pos)
6506 (org-show-context 'agenda)
6507 (save-excursion
6508 (and (outline-next-heading)
6509 (org-flag-heading nil))) ; show the next heading
6510 (call-interactively 'org-toggle-archive-tag)
6511 (end-of-line 1)
6512 (setq newhead (org-get-heading)))
6513 (org-agenda-change-all-lines newhead hdmarker)
6514 (beginning-of-line 1))))
6516 (defun org-agenda-do-date-later (arg)
6517 (interactive "P")
6518 (cond
6519 ((or (equal arg '(16))
6520 (memq last-command
6521 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
6522 (setq this-command 'org-agenda-date-later-minutes)
6523 (org-agenda-date-later-minutes 1))
6524 ((or (equal arg '(4))
6525 (memq last-command
6526 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
6527 (setq this-command 'org-agenda-date-later-hours)
6528 (org-agenda-date-later-hours 1))
6530 (org-agenda-date-later (prefix-numeric-value arg)))))
6532 (defun org-agenda-do-date-earlier (arg)
6533 (interactive "P")
6534 (cond
6535 ((or (equal arg '(16))
6536 (memq last-command
6537 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
6538 (setq this-command 'org-agenda-date-earlier-minutes)
6539 (org-agenda-date-earlier-minutes 1))
6540 ((or (equal arg '(4))
6541 (memq last-command
6542 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
6543 (setq this-command 'org-agenda-date-earlier-hours)
6544 (org-agenda-date-earlier-hours 1))
6546 (org-agenda-date-earlier (prefix-numeric-value arg)))))
6548 (defun org-agenda-date-later (arg &optional what)
6549 "Change the date of this item to one day later."
6550 (interactive "p")
6551 (org-agenda-check-type t 'agenda 'timeline)
6552 (org-agenda-check-no-diary)
6553 (let* ((marker (or (org-get-at-bol 'org-marker)
6554 (org-agenda-error)))
6555 (buffer (marker-buffer marker))
6556 (pos (marker-position marker)))
6557 (org-with-remote-undo buffer
6558 (with-current-buffer buffer
6559 (widen)
6560 (goto-char pos)
6561 (if (not (org-at-timestamp-p))
6562 (error "Cannot find time stamp"))
6563 (org-timestamp-change arg (or what 'day)))
6564 (org-agenda-show-new-time marker org-last-changed-timestamp))
6565 (message "Time stamp changed to %s" org-last-changed-timestamp)))
6567 (defun org-agenda-date-earlier (arg &optional what)
6568 "Change the date of this item to one day earlier."
6569 (interactive "p")
6570 (org-agenda-date-later (- arg) what))
6572 (defun org-agenda-date-later-minutes (arg)
6573 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
6574 (interactive "p")
6575 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
6576 (org-agenda-date-later arg 'minute))
6578 (defun org-agenda-date-earlier-minutes (arg)
6579 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
6580 (interactive "p")
6581 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
6582 (org-agenda-date-earlier arg 'minute))
6584 (defun org-agenda-date-later-hours (arg)
6585 "Change the time of this item, in hour steps."
6586 (interactive "p")
6587 (org-agenda-date-later arg 'hour))
6589 (defun org-agenda-date-earlier-hours (arg)
6590 "Change the time of this item, in hour steps."
6591 (interactive "p")
6592 (org-agenda-date-earlier arg 'hour))
6594 (defun org-agenda-show-new-time (marker stamp &optional prefix)
6595 "Show new date stamp via text properties."
6596 ;; We use text properties to make this undoable
6597 (let ((inhibit-read-only t)
6598 (buffer-invisibility-spec))
6599 (setq stamp (concat " " prefix " => " stamp))
6600 (save-excursion
6601 (goto-char (point-max))
6602 (while (not (bobp))
6603 (when (equal marker (org-get-at-bol 'org-marker))
6604 (org-move-to-column (- (window-width) (length stamp)) t)
6605 (org-agenda-fix-tags-filter-overlays-at (point))
6606 (if (featurep 'xemacs)
6607 ;; Use `duplicable' property to trigger undo recording
6608 (let ((ex (make-extent nil nil))
6609 (gl (make-glyph stamp)))
6610 (set-glyph-face gl 'secondary-selection)
6611 (set-extent-properties
6612 ex (list 'invisible t 'end-glyph gl 'duplicable t))
6613 (insert-extent ex (1- (point)) (point-at-eol)))
6614 (add-text-properties
6615 (1- (point)) (point-at-eol)
6616 (list 'display (org-add-props stamp nil
6617 'face 'secondary-selection))))
6618 (beginning-of-line 1))
6619 (beginning-of-line 0)))))
6621 (defun org-agenda-date-prompt (arg)
6622 "Change the date of this item. Date is prompted for, with default today.
6623 The prefix ARG is passed to the `org-time-stamp' command and can therefore
6624 be used to request time specification in the time stamp."
6625 (interactive "P")
6626 (org-agenda-check-type t 'agenda 'timeline)
6627 (org-agenda-check-no-diary)
6628 (let* ((marker (or (org-get-at-bol 'org-marker)
6629 (org-agenda-error)))
6630 (buffer (marker-buffer marker))
6631 (pos (marker-position marker)))
6632 (org-with-remote-undo buffer
6633 (with-current-buffer buffer
6634 (widen)
6635 (goto-char pos)
6636 (if (not (org-at-timestamp-p t))
6637 (error "Cannot find time stamp"))
6638 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
6639 (org-agenda-show-new-time marker org-last-changed-timestamp))
6640 (message "Time stamp changed to %s" org-last-changed-timestamp)))
6642 (defun org-agenda-schedule (arg)
6643 "Schedule the item at point."
6644 (interactive "P")
6645 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
6646 (org-agenda-check-no-diary)
6647 (let* ((marker (or (org-get-at-bol 'org-marker)
6648 (org-agenda-error)))
6649 (type (marker-insertion-type marker))
6650 (buffer (marker-buffer marker))
6651 (pos (marker-position marker))
6652 (org-insert-labeled-timestamps-at-point nil)
6654 (set-marker-insertion-type marker t)
6655 (org-with-remote-undo buffer
6656 (with-current-buffer buffer
6657 (widen)
6658 (goto-char pos)
6659 (setq ts (org-schedule arg)))
6660 (org-agenda-show-new-time marker ts "S"))
6661 (message "Item scheduled for %s" ts)))
6663 (defun org-agenda-deadline (arg)
6664 "Schedule the item at point."
6665 (interactive "P")
6666 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
6667 (org-agenda-check-no-diary)
6668 (let* ((marker (or (org-get-at-bol 'org-marker)
6669 (org-agenda-error)))
6670 (buffer (marker-buffer marker))
6671 (pos (marker-position marker))
6672 (org-insert-labeled-timestamps-at-point nil)
6674 (org-with-remote-undo buffer
6675 (with-current-buffer buffer
6676 (widen)
6677 (goto-char pos)
6678 (setq ts (org-deadline arg)))
6679 (org-agenda-show-new-time marker ts "D"))
6680 (message "Deadline for this item set to %s" ts)))
6682 (defun org-agenda-action ()
6683 "Select entry for agenda action, or execute an agenda action.
6684 This command prompts for another letter. Valid inputs are:
6686 m Mark the entry at point for an agenda action
6687 s Schedule the marked entry to the date at the cursor
6688 d Set the deadline of the marked entry to the date at the cursor
6689 r Call `org-remember' with cursor date as the default date
6690 SPC Show marked entry in other window
6691 TAB Visit marked entry in other window
6693 The cursor may be at a date in the calendar, or in the Org agenda."
6694 (interactive)
6695 (let (ans)
6696 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [ ]show")
6697 (setq ans (read-char-exclusive))
6698 (cond
6699 ((equal ans ?m)
6700 ;; Mark this entry
6701 (if (eq major-mode 'org-agenda-mode)
6702 (let ((m (or (org-get-at-bol 'org-hd-marker)
6703 (org-get-at-bol 'org-marker))))
6704 (if m
6705 (progn
6706 (move-marker org-agenda-action-marker
6707 (marker-position m) (marker-buffer m))
6708 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
6709 (error "Don't know which entry to mark")))
6710 (error "This command works only in the agenda")))
6711 ((equal ans ?s)
6712 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
6713 ((equal ans ?d)
6714 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
6715 ((equal ans ?r)
6716 (org-agenda-do-action '(org-remember) t))
6717 ((equal ans ?\ )
6718 (let ((cw (selected-window)))
6719 (org-switch-to-buffer-other-window
6720 (marker-buffer org-agenda-action-marker))
6721 (goto-char org-agenda-action-marker)
6722 (org-show-context 'agenda)
6723 (select-window cw)))
6724 ((equal ans ?\C-i)
6725 (org-switch-to-buffer-other-window
6726 (marker-buffer org-agenda-action-marker))
6727 (goto-char org-agenda-action-marker)
6728 (org-show-context 'agenda))
6729 (t (error "Invalid agenda action %c" ans)))))
6731 (defun org-agenda-do-action (form &optional current-buffer)
6732 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
6733 (let ((org-overriding-default-time (org-get-cursor-date)))
6734 (if current-buffer
6735 (eval form)
6736 (if (not (marker-buffer org-agenda-action-marker))
6737 (error "No entry has been selected for agenda action")
6738 (with-current-buffer (marker-buffer org-agenda-action-marker)
6739 (save-excursion
6740 (save-restriction
6741 (widen)
6742 (goto-char org-agenda-action-marker)
6743 (eval form))))))))
6745 (defun org-agenda-clock-in (&optional arg)
6746 "Start the clock on the currently selected item."
6747 (interactive "P")
6748 (org-agenda-check-no-diary)
6749 (if (equal arg '(4))
6750 (org-clock-in arg)
6751 (let* ((marker (or (org-get-at-bol 'org-marker)
6752 (org-agenda-error)))
6753 (hdmarker (or (org-get-at-bol 'org-hd-marker)
6754 marker))
6755 (pos (marker-position marker))
6756 newhead)
6757 (org-with-remote-undo (marker-buffer marker)
6758 (with-current-buffer (marker-buffer marker)
6759 (widen)
6760 (goto-char pos)
6761 (org-show-context 'agenda)
6762 (org-show-entry)
6763 (org-cycle-hide-drawers 'children)
6764 (org-clock-in arg)
6765 (setq newhead (org-get-heading)))
6766 (org-agenda-change-all-lines newhead hdmarker)))))
6768 (defun org-agenda-clock-out (&optional arg)
6769 "Stop the currently running clock."
6770 (interactive "P")
6771 (unless (marker-buffer org-clock-marker)
6772 (error "No running clock"))
6773 (let ((marker (make-marker)) newhead)
6774 (org-with-remote-undo (marker-buffer org-clock-marker)
6775 (with-current-buffer (marker-buffer org-clock-marker)
6776 (save-excursion
6777 (save-restriction
6778 (widen)
6779 (goto-char org-clock-marker)
6780 (org-back-to-heading t)
6781 (move-marker marker (point))
6782 (org-clock-out)
6783 (setq newhead (org-get-heading))))))
6784 (org-agenda-change-all-lines newhead marker)
6785 (move-marker marker nil)))
6787 (defun org-agenda-clock-cancel (&optional arg)
6788 "Cancel the currently running clock."
6789 (interactive "P")
6790 (unless (marker-buffer org-clock-marker)
6791 (error "No running clock"))
6792 (org-with-remote-undo (marker-buffer org-clock-marker)
6793 (org-clock-cancel)))
6795 (defun org-agenda-diary-entry-in-org-file ()
6796 "Make a diary entry in the file `org-agenda-diary-file'."
6797 (let (d1 d2 char (text "") dp1 dp2)
6798 (if (equal (buffer-name) "*Calendar*")
6799 (setq d1 (calendar-cursor-to-date t)
6800 d2 (car calendar-mark-ring))
6801 (setq dp1 (get-text-property (point-at-bol) 'day))
6802 (unless dp1 (error "No date defined in current line"))
6803 (setq d1 (calendar-gregorian-from-absolute dp1)
6804 d2 (and (ignore-errors (mark))
6805 (save-excursion
6806 (goto-char (mark))
6807 (setq dp2 (get-text-property (point-at-bol) 'day)))
6808 (calendar-gregorian-from-absolute dp2))))
6809 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
6810 (setq char (read-char-exclusive))
6811 (cond
6812 ((equal char ?d)
6813 (setq text (read-string "Day entry: "))
6814 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
6815 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
6816 ((equal char ?a)
6817 (setq d1 (list (car d1) (nth 1 d1)
6818 (read-number (format "Reference year [%d]: " (nth 2 d1))
6819 (nth 2 d1))))
6820 (setq text (read-string "Anniversary (use %d to show years): "))
6821 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
6822 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
6823 ((equal char ?b)
6824 (setq text (read-string "Block entry: "))
6825 (unless (and d1 d2 (not (equal d1 d2)))
6826 (error "No block of days selected"))
6827 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
6828 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
6829 ((equal char ?j)
6830 (org-switch-to-buffer-other-window
6831 (find-file-noselect org-agenda-diary-file))
6832 (org-datetree-find-date-create d1)
6833 (org-reveal t))
6834 (t (error "Invalid selection character `%c'" char)))))
6836 (defcustom org-agenda-insert-diary-strategy 'date-tree
6837 "Where in `org-agenda-diary-file' should new entries be added?
6838 Valid values:
6840 date-tree in the date tree, as child of the date
6841 top-level as top-level entries at the end of the file."
6842 :group 'org-agenda
6843 :type '(choice
6844 (const :tag "in a date tree" date-tree)
6845 (const :tag "as top level at end of file" top-level)))
6847 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
6848 "Add a diary entry with TYPE to `org-agenda-diary-file'.
6849 If TEXT is not empty, it will become the headline of the new entry, and
6850 the resulting entry will not be shown. When TEXT is empty, switch to
6851 `org-agenda-diary-file' and let the user finish the entry there."
6852 (let ((cw (current-window-configuration)))
6853 (org-switch-to-buffer-other-window
6854 (find-file-noselect org-agenda-diary-file))
6855 (widen)
6856 (goto-char (point-min))
6857 (cond
6858 ((eq type 'anniversary)
6859 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
6860 (progn
6861 (or (org-on-heading-p t)
6862 (progn
6863 (outline-next-heading)
6864 (insert "* Anniversaries\n\n")
6865 (beginning-of-line -1)))))
6866 (outline-next-heading)
6867 (org-back-over-empty-lines)
6868 (backward-char 1)
6869 (insert "\n")
6870 (require 'diary-lib)
6871 (let ((calendar-date-display-form
6872 (if (if (boundp 'calendar-date-style)
6873 (eq calendar-date-style 'european)
6874 european-calendar-style) ; Emacs 22
6875 '(day " " month " " year)
6876 '(month " " day " " year))))
6878 (insert (format "%%%%(diary-anniversary %s) %s"
6879 (calendar-date-string d1 nil t) text))))
6880 ((eq type 'day)
6881 (if (eq org-agenda-insert-diary-strategy 'top-level)
6882 (org-agenda-insert-diary-as-top-level text)
6883 (require 'org-datetree)
6884 (org-datetree-find-date-create d1)
6885 (org-agenda-insert-diary-make-new-entry text))
6886 (org-insert-time-stamp (org-time-from-absolute
6887 (calendar-absolute-from-gregorian d1)))
6888 (end-of-line 0))
6889 ((eq type 'block)
6890 (if (> (calendar-absolute-from-gregorian d1)
6891 (calendar-absolute-from-gregorian d2))
6892 (setq d1 (prog1 d2 (setq d2 d1))))
6893 (if (eq org-agenda-insert-diary-strategy 'top-level)
6894 (org-agenda-insert-diary-as-top-level text)
6895 (require 'org-datetree)
6896 (org-datetree-find-date-create d1)
6897 (org-agenda-insert-diary-make-new-entry text))
6898 (org-insert-time-stamp (org-time-from-absolute
6899 (calendar-absolute-from-gregorian d1)))
6900 (insert "--")
6901 (org-insert-time-stamp (org-time-from-absolute
6902 (calendar-absolute-from-gregorian d2)))
6903 (end-of-line 0)))
6904 (if (string-match "\\S-" text)
6905 (progn
6906 (set-window-configuration cw)
6907 (message "%s entry added to %s"
6908 (capitalize (symbol-name type))
6909 (abbreviate-file-name org-agenda-diary-file)))
6910 (org-reveal t)
6911 (message "Please finish entry here"))))
6913 (defun org-agenda-insert-diary-as-top-level (text)
6914 "Make new entry as a top-level entry at the end of the file.
6915 Add TEXT as headline, and position the cursor in the second line so that
6916 a timestamp can be added there."
6917 (widen)
6918 (goto-char (point-max))
6919 (or (bolp) (insert "\n"))
6920 (insert "* " text "\n")
6921 (if org-adapt-indentation (org-indent-to-column 2)))
6923 (defun org-agenda-insert-diary-make-new-entry (text)
6924 "Make new entry as last child of current entry.
6925 Add TEXT as headline, and position the cursor in the second line so that
6926 a timestamp can be added there."
6927 (let ((org-show-following-heading t)
6928 (org-show-siblings t)
6929 (org-show-hierarchy-above t)
6930 (org-show-entry-below t)
6931 col)
6932 (outline-next-heading)
6933 (org-back-over-empty-lines)
6934 (or (looking-at "[ \t]*$")
6935 (progn (insert "\n") (backward-char 1)))
6936 (org-insert-heading)
6937 (org-do-demote)
6938 (setq col (current-column))
6939 (insert text "\n")
6940 (if org-adapt-indentation (org-indent-to-column col))
6941 (let ((org-show-following-heading t)
6942 (org-show-siblings t)
6943 (org-show-hierarchy-above t)
6944 (org-show-entry-below t))
6945 (org-show-context))))
6947 (defun org-agenda-diary-entry ()
6948 "Make a diary entry, like the `i' command from the calendar.
6949 All the standard commands work: block, weekly etc.
6950 When `org-agenda-diary-file' points to a file,
6951 `org-agenda-diary-entry-in-org-file' is called instead to create
6952 entries in that Org-mode file."
6953 (interactive)
6954 (org-agenda-check-type t 'agenda 'timeline)
6955 (if (not (eq org-agenda-diary-file 'diary-file))
6956 (org-agenda-diary-entry-in-org-file)
6957 (require 'diary-lib)
6958 (let* ((char (progn
6959 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
6960 (read-char-exclusive)))
6961 (cmd (cdr (assoc char
6962 '((?d . insert-diary-entry)
6963 (?w . insert-weekly-diary-entry)
6964 (?m . insert-monthly-diary-entry)
6965 (?y . insert-yearly-diary-entry)
6966 (?a . insert-anniversary-diary-entry)
6967 (?b . insert-block-diary-entry)
6968 (?c . insert-cyclic-diary-entry)))))
6969 (oldf (symbol-function 'calendar-cursor-to-date))
6970 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
6971 (point (point))
6972 (mark (or (mark t) (point))))
6973 (unless cmd
6974 (error "No command associated with <%c>" char))
6975 (unless (and (get-text-property point 'day)
6976 (or (not (equal ?b char))
6977 (get-text-property mark 'day)))
6978 (error "Don't know which date to use for diary entry"))
6979 ;; We implement this by hacking the `calendar-cursor-to-date' function
6980 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
6981 (let ((calendar-mark-ring
6982 (list (calendar-gregorian-from-absolute
6983 (or (get-text-property mark 'day)
6984 (get-text-property point 'day))))))
6985 (unwind-protect
6986 (progn
6987 (fset 'calendar-cursor-to-date
6988 (lambda (&optional error dummy)
6989 (calendar-gregorian-from-absolute
6990 (get-text-property point 'day))))
6991 (call-interactively cmd))
6992 (fset 'calendar-cursor-to-date oldf))))))
6994 (defun org-agenda-execute-calendar-command (cmd)
6995 "Execute a calendar command from the agenda, with the date associated to
6996 the cursor position."
6997 (org-agenda-check-type t 'agenda 'timeline)
6998 (require 'diary-lib)
6999 (unless (get-text-property (point) 'day)
7000 (error "Don't know which date to use for calendar command"))
7001 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7002 (point (point))
7003 (date (calendar-gregorian-from-absolute
7004 (get-text-property point 'day)))
7005 ;; the following 2 vars are needed in the calendar
7006 (displayed-month (car date))
7007 (displayed-year (nth 2 date)))
7008 (unwind-protect
7009 (progn
7010 (fset 'calendar-cursor-to-date
7011 (lambda (&optional error dummy)
7012 (calendar-gregorian-from-absolute
7013 (get-text-property point 'day))))
7014 (call-interactively cmd))
7015 (fset 'calendar-cursor-to-date oldf))))
7017 (defun org-agenda-phases-of-moon ()
7018 "Display the phases of the moon for the 3 months around the cursor date."
7019 (interactive)
7020 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
7022 (defun org-agenda-holidays ()
7023 "Display the holidays for the 3 months around the cursor date."
7024 (interactive)
7025 (org-agenda-execute-calendar-command 'list-calendar-holidays))
7027 (defvar calendar-longitude)
7028 (defvar calendar-latitude)
7029 (defvar calendar-location-name)
7031 (defun org-agenda-sunrise-sunset (arg)
7032 "Display sunrise and sunset for the cursor date.
7033 Latitude and longitude can be specified with the variables
7034 `calendar-latitude' and `calendar-longitude'. When called with prefix
7035 argument, latitude and longitude will be prompted for."
7036 (interactive "P")
7037 (require 'solar)
7038 (let ((calendar-longitude (if arg nil calendar-longitude))
7039 (calendar-latitude (if arg nil calendar-latitude))
7040 (calendar-location-name
7041 (if arg "the given coordinates" calendar-location-name)))
7042 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
7044 (defun org-agenda-goto-calendar ()
7045 "Open the Emacs calendar with the date at the cursor."
7046 (interactive)
7047 (org-agenda-check-type t 'agenda 'timeline)
7048 (let* ((day (or (get-text-property (point) 'day)
7049 (error "Don't know which date to open in calendar")))
7050 (date (calendar-gregorian-from-absolute day))
7051 (calendar-move-hook nil)
7052 (calendar-view-holidays-initially-flag nil)
7053 (calendar-view-diary-initially-flag nil)
7054 (view-calendar-holidays-initially nil)
7055 (view-diary-entries-initially nil))
7056 (calendar)
7057 (calendar-goto-date date)))
7059 ;;;###autoload
7060 (defun org-calendar-goto-agenda ()
7061 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
7062 This is a command that has to be installed in `calendar-mode-map'."
7063 (interactive)
7064 (org-agenda-list nil (calendar-absolute-from-gregorian
7065 (calendar-cursor-to-date))
7066 nil))
7068 (defun org-agenda-convert-date ()
7069 (interactive)
7070 (org-agenda-check-type t 'agenda 'timeline)
7071 (let ((day (get-text-property (point) 'day))
7072 date s)
7073 (unless day
7074 (error "Don't know which date to convert"))
7075 (setq date (calendar-gregorian-from-absolute day))
7076 (setq s (concat
7077 "Gregorian: " (calendar-date-string date) "\n"
7078 "ISO: " (calendar-iso-date-string date) "\n"
7079 "Day of Yr: " (calendar-day-of-year-string date) "\n"
7080 "Julian: " (calendar-julian-date-string date) "\n"
7081 "Astron. JD: " (calendar-astro-date-string date)
7082 " (Julian date number at noon UTC)\n"
7083 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
7084 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
7085 "French: " (calendar-french-date-string date) "\n"
7086 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
7087 "Mayan: " (calendar-mayan-date-string date) "\n"
7088 "Coptic: " (calendar-coptic-date-string date) "\n"
7089 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
7090 "Persian: " (calendar-persian-date-string date) "\n"
7091 "Chinese: " (calendar-chinese-date-string date) "\n"))
7092 (with-output-to-temp-buffer "*Dates*"
7093 (princ s))
7094 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
7096 ;;; Bulk commands
7098 (defvar org-agenda-bulk-marked-entries nil
7099 "List of markers that refer to marked entries in the agenda.")
7101 (defun org-agenda-bulk-marked-p ()
7102 (eq (get-char-property (point-at-bol) 'type)
7103 'org-marked-entry-overlay))
7105 (defun org-agenda-bulk-mark ()
7106 "Mark the entry at point for future bulk action."
7107 (interactive)
7108 (org-agenda-check-no-diary)
7109 (let* ((m (org-get-at-bol 'org-hd-marker))
7111 (unless (org-agenda-bulk-marked-p)
7112 (unless m (error "Nothing to mark at point"))
7113 (push m org-agenda-bulk-marked-entries)
7114 (setq ov (org-make-overlay (point-at-bol) (+ 2 (point-at-bol))))
7115 (org-overlay-display ov "> "
7116 (org-get-todo-face "TODO")
7117 'evaporate)
7118 (org-overlay-put ov 'type 'org-marked-entry-overlay))
7119 (beginning-of-line 2)
7120 (while (and (get-char-property (point) 'invisible) (not (eobp)))
7121 (beginning-of-line 2))
7122 (message "%d entries marked for bulk action"
7123 (length org-agenda-bulk-marked-entries))))
7125 (defun org-agenda-bulk-unmark ()
7126 "Unmark the entry at point for future bulk action."
7127 (interactive)
7128 (when (org-agenda-bulk-marked-p)
7129 (org-agenda-bulk-remove-overlays
7130 (point-at-bol) (+ 2 (point-at-bol)))
7131 (setq org-agenda-bulk-marked-entries
7132 (delete (org-get-at-bol 'org-hd-marker)
7133 org-agenda-bulk-marked-entries)))
7134 (beginning-of-line 2)
7135 (while (and (get-char-property (point) 'invisible) (not (eobp)))
7136 (beginning-of-line 2))
7137 (message "%d entries marked for bulk action"
7138 (length org-agenda-bulk-marked-entries)))
7140 (defun org-agenda-bulk-toggle ()
7141 "Toggle marking the entry at point for bulk action."
7142 (interactive)
7143 (if (org-agenda-bulk-marked-p)
7144 (org-agenda-bulk-unmark)
7145 (org-agenda-bulk-mark)))
7147 (defun org-agenda-bulk-remove-overlays (&optional beg end)
7148 "Remove the mark overlays between BEG and END in the agenda buffer.
7149 BEG and END default to the buffer limits.
7151 This only removes the overlays, it does not remove the markers
7152 from the list in `org-agenda-bulk-marked-entries'."
7153 (interactive)
7154 (mapc (lambda (ov)
7155 (and (eq (org-overlay-get ov 'type) 'org-marked-entry-overlay)
7156 (org-delete-overlay ov)))
7157 (org-overlays-in (or beg (point-min)) (or end (point-max)))))
7159 (defun org-agenda-bulk-remove-all-marks ()
7160 "Remove all marks in the agenda buffer.
7161 This will remove the markers, and the overlays."
7162 (interactive)
7163 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
7164 (setq org-agenda-bulk-marked-entries nil)
7165 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
7167 (defun org-agenda-bulk-action ()
7168 "Execute an remote-editing action on all marked entries."
7169 (interactive)
7170 (unless org-agenda-bulk-marked-entries
7171 (error "No entries are marked"))
7172 (message "Bulk: [r]efile [$]archive [A]rch->sib [t]odo [+/-]tag [s]chedule [d]eadline")
7173 (let* ((action (read-char-exclusive))
7174 (entries (reverse org-agenda-bulk-marked-entries))
7175 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
7176 (cond
7177 ((equal action ?$)
7178 (setq cmd '(org-agenda-archive)))
7180 ((equal action ?A)
7181 (setq cmd '(org-agenda-archive-to-archive-sibling)))
7183 ((member action '(?r ?w))
7184 (setq rfloc (org-refile-get-location
7185 "Refile to: "
7186 (marker-buffer (car org-agenda-bulk-marked-entries))
7187 org-refile-allow-creating-parent-nodes))
7188 (setcar (nthcdr 3 rfloc)
7189 (move-marker (make-marker) (nth 3 rfloc)
7190 (or (get-file-buffer (nth 1 rfloc))
7191 (find-buffer-visiting (nth 1 rfloc))
7192 (error "This should not happen"))))
7194 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc))))
7196 ((equal action ?t)
7197 (setq state (org-icompleting-read
7198 "Todo state: "
7199 (with-current-buffer (marker-buffer (car entries))
7200 (mapcar 'list org-todo-keywords-1))))
7201 (setq cmd `(let ((org-inhibit-blocking t)
7202 (org-inhibit-logging 'note))
7203 (org-agenda-todo ,state))))
7205 ((memq action '(?- ?+))
7206 (setq tag (org-icompleting-read
7207 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
7208 (with-current-buffer (marker-buffer (car entries))
7209 (delq nil
7210 (mapcar (lambda (x)
7211 (if (stringp (car x)) x)) org-tag-alist)))))
7212 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
7214 ((memq action '(?s ?d))
7215 (let* ((date (org-read-date
7216 nil nil nil
7217 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to")))
7218 (ans org-read-date-final-answer)
7219 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
7220 (setq cmd `(let* ((bound (fboundp 'read-string))
7221 (old (and bound (symbol-function 'read-string))))
7222 (unwind-protect
7223 (progn
7224 (fset 'read-string (lambda (&rest ignore) ,ans))
7225 (call-interactively ',c1))
7226 (if bound
7227 (fset 'read-string old)
7228 (fmakunbound 'read-string)))))))
7229 (t (error "Invalid bulk action")))
7231 ;; Sort the markers, to make sure that parents are handled before children
7232 (setq entries (sort entries
7233 (lambda (a b)
7234 (cond
7235 ((equal (marker-buffer a) (marker-buffer b))
7236 (< (marker-position a) (marker-position b)))
7238 (string< (buffer-name (marker-buffer a))
7239 (buffer-name (marker-buffer b))))))))
7241 ;; Now loop over all markers and apply cmd
7242 (while (setq e (pop entries))
7243 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
7244 (if (not pos)
7245 (progn (message "Skipping removed entry at %s" e)
7246 (setq cntskip (1+ cntskip)))
7247 (goto-char pos)
7248 (eval cmd)
7249 (setq org-agenda-bulk-marked-entries
7250 (delete e org-agenda-bulk-marked-entries))
7251 (setq cnt (1+ cnt))))
7252 (setq org-agenda-bulk-marked-entries nil)
7253 (org-agenda-bulk-remove-all-marks)
7254 (message "Acted on %d entries%s"
7256 (if (= cntskip 0)
7258 (format ", skipped %d (disappeared before their turn)"
7259 cntskip)))))
7261 ;;; Flagging notes
7263 (defun org-agenda-show-the-flagging-note ()
7264 "Display the flagging note in the other window.
7265 When called a second time in direct sequence, offer to remove the FLAGGING
7266 tag and (if present) the flagging note."
7267 (interactive)
7268 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
7269 (win (selected-window))
7270 note heading newhead)
7271 (unless hdmarker
7272 (error "No linked entry at point"))
7273 (if (and (eq this-command last-command)
7274 (y-or-n-p "Unflag and remove any flagging note? "))
7275 (progn
7276 (org-agenda-remove-flag hdmarker)
7277 (let ((win (get-buffer-window "*Flagging Note*")))
7278 (and win (delete-window win)))
7279 (message "Entry unflaged"))
7280 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
7281 (unless note
7282 (error "No flagging note"))
7283 (org-kill-new note)
7284 (org-switch-to-buffer-other-window "*Flagging Note*")
7285 (erase-buffer)
7286 (insert note)
7287 (goto-char (point-min))
7288 (while (re-search-forward "\\\\n" nil t)
7289 (replace-match "\n" t t))
7290 (goto-char (point-min))
7291 (select-window win)
7292 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
7294 (defun org-agenda-remove-flag (marker)
7295 "Remove the FLAGGED tag and any flagging note in the entry."
7296 (let (newhead)
7297 (org-with-point-at marker
7298 (org-toggle-tag "FLAGGED" 'off)
7299 (org-entry-delete nil "THEFLAGGINGNOTE")
7300 (setq newhead (org-get-heading)))
7301 (org-agenda-change-all-lines newhead marker)
7302 (message "Entry unflaged")))
7304 (defun org-agenda-get-any-marker (&optional pos)
7305 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
7306 (get-text-property (or pos (point-at-bol)) 'org-marker)))
7308 ;;; Appointment reminders
7310 (defvar appt-time-msg-list)
7312 ;;;###autoload
7313 (defun org-agenda-to-appt (&optional refresh filter)
7314 "Activate appointments found in `org-agenda-files'.
7315 With a \\[universal-argument] prefix, refresh the list of
7316 appointments.
7318 If FILTER is t, interactively prompt the user for a regular
7319 expression, and filter out entries that don't match it.
7321 If FILTER is a string, use this string as a regular expression
7322 for filtering entries out.
7324 FILTER can also be an alist with the car of each cell being
7325 either 'headline or 'category. For example:
7327 '((headline \"IMPORTANT\")
7328 (category \"Work\"))
7330 will only add headlines containing IMPORTANT or headlines
7331 belonging to the \"Work\" category."
7332 (interactive "P")
7333 (require 'calendar)
7334 (if refresh (setq appt-time-msg-list nil))
7335 (if (eq filter t)
7336 (setq filter (read-from-minibuffer "Regexp filter: ")))
7337 (let* ((cnt 0) ; count added events
7338 (org-agenda-new-buffers nil)
7339 (org-deadline-warning-days 0)
7340 (today (org-date-to-gregorian
7341 (time-to-days (current-time))))
7342 (org-agenda-restrict nil)
7343 (files (org-agenda-files 'unrestricted)) entries file)
7344 ;; Get all entries which may contain an appt
7345 (org-prepare-agenda-buffers files)
7346 (while (setq file (pop files))
7347 (setq entries
7348 (append entries
7349 (org-agenda-get-day-entries
7350 file today :timestamp :scheduled :deadline))))
7351 (setq entries (delq nil entries))
7352 ;; Map thru entries and find if we should filter them out
7353 (mapc
7354 (lambda(x)
7355 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
7356 (cat (get-text-property 1 'org-category x))
7357 (tod (get-text-property 1 'time-of-day x))
7358 (ok (or (null filter)
7359 (and (stringp filter) (string-match filter evt))
7360 (and (listp filter)
7361 (or (string-match
7362 (cadr (assoc 'category filter)) cat)
7363 (string-match
7364 (cadr (assoc 'headline filter)) evt))))))
7365 ;; FIXME: Shall we remove text-properties for the appt text?
7366 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
7367 (when (and ok tod)
7368 (setq tod (concat "00" (number-to-string tod))
7369 tod (when (string-match
7370 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
7371 (concat (match-string 1 tod) ":"
7372 (match-string 2 tod))))
7373 (appt-add tod evt)
7374 (setq cnt (1+ cnt))))) entries)
7375 (org-release-buffers org-agenda-new-buffers)
7376 (if (eq cnt 0)
7377 (message "No event to add")
7378 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
7380 (defun org-agenda-todayp (date)
7381 "Does DATE mean today, when considering `org-extend-today-until'?"
7382 (let (today h)
7383 (if (listp date) (setq date (calendar-absolute-from-gregorian date)))
7384 (setq today (calendar-absolute-from-gregorian (calendar-current-date)))
7385 (setq h (nth 2 (decode-time (current-time))))
7386 (or (and (>= h org-extend-today-until)
7387 (= date today))
7388 (and (< h org-extend-today-until)
7389 (= date (1- today))))))
7391 (provide 'org-agenda)
7393 ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
7395 ;;; org-agenda.el ends here